FFmpeg
Loading...
Searching...
No Matches
api-h264-flush-test.c
Go to the documentation of this file.
1/*
2 * This file is part of FFmpeg.
3 *
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19#include <inttypes.h>
20#include <stdio.h>
21#include <stdlib.h>
22
23#include "libavcodec/avcodec.h"
25#include "libavutil/adler32.h"
26#include "libavutil/imgutils.h"
27#include "libavutil/mem.h"
28
29static void check(int ret)
30{
31 if (ret < 0) {
32 fprintf(stderr, "%s\n", av_err2str(ret));
33 exit(1);
34 }
35}
36
41
42/* A limit ends the first input after that many packets. If there is a
43 * second input, continue with it without draining; otherwise stop. */
44static void decode(AVCodecContext *ctx, const char *path,
45 const char *next_path, int limit, int report)
46{
47 AVFormatContext *fmt = NULL;
50 AVAdler checksum = 1;
51 int packets = 0, frames = 0, first_packet = 0, eof = 0, ret;
52
53 if (!pkt || !frame)
54 exit(1);
56 while (!eof) {
57 const int at_limit = limit && packets == limit;
58
59 if (at_limit && !next_path)
60 break;
61 ret = at_limit ? AVERROR_EOF : av_read_frame(fmt, pkt);
62 if (ret == AVERROR_EOF) {
63 if (limit && !at_limit)
64 check(ret);
65 if (next_path) {
67 check(avformat_open_input(&fmt, next_path, av_find_input_format("h264"), NULL));
68 next_path = NULL;
69 limit = 0;
70 continue;
71 }
72 eof = 1;
74 } else {
75 check(ret);
78 packets++;
79 }
80 while ((ret = avcodec_receive_frame(ctx, frame)) >= 0) {
81 int size;
82 uint8_t *data;
83
84 if (!report)
85 continue;
86 if (!frames++)
87 first_packet = packets + eof;
88 if (!(report & REPORT_FRAMES))
89 continue;
90 size = av_image_get_buffer_size(frame->format, frame->width,
91 frame->height, 1);
92 check(size);
94 if (!data)
95 exit(1);
97 (const uint8_t *const *)frame->data,
98 frame->linesize, frame->format,
99 frame->width, frame->height, 1));
100 checksum = av_adler32_update(checksum, data, size);
101 av_free(data);
102 }
103 if (ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
104 check(ret);
105 }
106 if (report & REPORT_TIMING)
107 printf("first output after %d packet(s) plus thread delay\n",
108 first_packet - ctx->delay);
109 if (report & REPORT_FRAMES)
110 printf("%d frames, checksum %08" PRIx32 "\n", frames, checksum);
114}
115
116static AVCodecContext *open_decoder(int threads, int video_delay)
117{
120
121 if (!ctx)
122 exit(1);
123 if (video_delay) {
125
126 if (!par)
127 exit(1);
130 par->video_delay = video_delay;
133 }
134 ctx->thread_count = threads;
135 ctx->thread_type = FF_THREAD_FRAME;
136 check(avcodec_open2(ctx, codec, NULL));
137 if (threads > 1 && !(ctx->active_thread_type & FF_THREAD_FRAME)) {
138 fprintf(stderr, "Could not activate frame threading\n");
139 exit(1);
140 }
141 return ctx;
142}
143
144int main(int argc, char **argv)
145{
147 int threads;
148
149 if (argc != 6)
150 return 1;
152
153 threads = atoi(argv[1]);
154 ctx = open_decoder(threads, 0);
155
156 /* BA1 should output immediately after the flush. The following SPS
157 * needs the learned reorder depth to output every picture. */
158 decode(ctx, argv[2], NULL, 0, 0);
160 decode(ctx, argv[3], argv[4], 0, REPORT_TIMING | REPORT_FRAMES);
162
163 /* A one-packet BA1 prefix switches back to CAMACI3 on the next worker.
164 * Count both inputs because the prefix picture may still be queued. */
165 ctx = open_decoder(threads, 0);
166 decode(ctx, argv[2], NULL, 0, 0);
168 decode(ctx, argv[3], argv[2], 1, REPORT_FRAMES);
170
171 /* Seek back to the active SPS before the prefix learns its depth. */
172 ctx = open_decoder(threads, 0);
173 decode(ctx, argv[2], NULL, 0, 0);
175 decode(ctx, argv[4], NULL, 8, 0);
177 decode(ctx, argv[4], NULL, 0, REPORT_FRAMES);
179
180 /* Replay MR7 after a flush while its zero-reorder SPS is still active,
181 * with and without one frame of delay supplied by the caller. */
182 for (int video_delay = 0; video_delay <= 1; video_delay++) {
183 ctx = open_decoder(threads, video_delay);
184 decode(ctx, argv[4], NULL, 20, 0);
185 decode(ctx, argv[5], NULL, 1, 0);
187 decode(ctx, argv[5], NULL, 0, REPORT_TIMING | REPORT_FRAMES);
189 }
190 return 0;
191}
Public header for Adler-32 hash function implementation.
@ REPORT_TIMING
@ REPORT_FRAMES
static AVCodecContext * open_decoder(int threads, int video_delay)
static void decode(AVCodecContext *ctx, const char *path, const char *next_path, int limit, int report)
static int frames
static AVFormatContext * ctx
Libavcodec external API header.
#define FF_THREAD_FRAME
Decode more than one frame at once.
Definition avcodec.h:1600
Main libavformat public API header.
AVCodecParameters * avcodec_parameters_alloc(void)
Definition codec_par.c:57
void avcodec_parameters_free(AVCodecParameters **ppar)
Definition codec_par.c:67
int avcodec_parameters_to_context(AVCodecContext *codec, const AVCodecParameters *par)
Definition codec_par.c:206
#define NULL
Definition coverity.c:32
__device__ int printf(const char *,...)
static AVPacket * pkt
static AVFrame * frame
int main
Definition dovi_rpuenc.c:38
char * path
Definition ffplay.c:385
int ret
Definition ffplay.c:391
#define report
Definition test.h:480
int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
Initialize the AVCodecContext to use the given AVCodec.
Definition avcodec.c:142
AVCodecContext * avcodec_alloc_context3(const AVCodec *codec)
Allocate an AVCodecContext and set its fields to default values.
Definition options.c:149
const AVCodec * avcodec_find_decoder(enum AVCodecID id)
Find a registered decoder with a matching codec ID.
Definition allcodecs.c:999
void avcodec_free_context(AVCodecContext **avctx)
Free the codec context and everything associated with it and write NULL to the provided pointer.
Definition options.c:168
@ AV_CODEC_ID_H264
Definition codec_id.h:77
int avcodec_receive_frame(AVCodecContext *avctx, AVFrame *frame)
Alias for avcodec_receive_frame_flags(avctx, frame, 0).
Definition avcodec.c:718
int avcodec_send_packet(AVCodecContext *avctx, const AVPacket *avpkt)
Supply raw packet data as input to a decoder.
Definition decode.c:738
void avcodec_flush_buffers(AVCodecContext *avctx)
Reset the internal codec state / flush internal buffers.
Definition avcodec.c:387
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
Definition packet.c:74
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition packet.c:435
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
Definition packet.c:63
const AVInputFormat * av_find_input_format(const char *short_name)
Find AVInputFormat based on the short name of the input format.
Definition format.c:146
int av_read_frame(AVFormatContext *s, AVPacket *pkt)
Return the next frame of a stream.
Definition demux.c:1719
int avformat_open_input(AVFormatContext **ps, const char *url, const AVInputFormat *fmt, AVDictionary **options)
Open an input stream and read the header.
Definition demux.c:231
void avformat_close_input(AVFormatContext **s)
Close an opened input AVFormatContext.
Definition demux.c:400
uint32_t AVAdler
Definition adler32.h:42
AVAdler av_adler32_update(AVAdler adler, const uint8_t *buf, size_t len)
Calculate the Adler32 checksum of a buffer.
Definition adler32.c:44
#define AVERROR_EOF
End of file.
Definition error.h:57
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition error.h:122
#define AVERROR(e)
Definition error.h:45
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:65
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition frame.c:53
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
void av_log_set_level(int level)
Set the log level.
Definition log.c:482
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
int av_image_get_buffer_size(enum AVPixelFormat pix_fmt, int width, int height, int align)
Return the size in bytes of the amount of data required to store an image with the given parameters.
Definition imgutils.c:466
int av_image_copy_to_buffer(uint8_t *dst, int dst_size, const uint8_t *const src_data[4], const int src_linesize[4], enum AVPixelFormat pix_fmt, int width, int height, int align)
Copy image data from an image into a buffer.
Definition imgutils.c:501
misc image utilities
Memory handling functions.
#define check(x, y, S, v)
const char data[16]
Definition mxf.c:155
#define av_malloc(s)
Definition ops_static.c:52
main external API structure.
Definition avcodec.h:443
This struct describes the properties of an encoded stream.
Definition codec_par.h:49
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
int video_delay
Number of delayed frames.
Definition codec_par.h:200
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
AVCodec.
Definition codec.h:175
Format I/O context.
Definition avformat.h:1401
This structure describes decoded (raw) audio or video data.
Definition frame.h:479
This structure stores compressed data.
Definition packet.h:587
#define av_free(p)
int size
static double limit(double x)