FFmpeg
Loading...
Searching...
No Matches
enc_recon_frame_test.c
Go to the documentation of this file.
1/*
2 * copyright (c) 2022 Anton Khirnov <anton@khirnov.net>
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21/* A test for AV_CODEC_FLAG_RECON_FRAME
22 * TODO: dump reconstructed frames to disk */
23
24#include <stdio.h>
25#include <stdint.h>
26#include <stdlib.h>
27
28#include "decode_simple.h"
29
30#include "libavutil/adler32.h"
31#include "libavutil/avassert.h"
32#include "libavutil/common.h"
33#include "libavutil/error.h"
34#include "libavutil/frame.h"
35#include "libavutil/imgutils.h"
36#include "libavutil/mem.h"
37#include "libavutil/opt.h"
38
40
41#include "libavcodec/avcodec.h"
42#include "libavcodec/codec.h"
43
44#include "libswscale/swscale.h"
45
46typedef struct FrameChecksum {
48 uint32_t checksum[4];
50
67
68static int frame_hash(FrameChecksum **pc, size_t *nb_c, int64_t ts,
69 const AVFrame *frame)
70{
72 int shift_h[4] = { 0 }, shift_v[4] = { 0 };
73
74 c = av_realloc_array(*pc, *nb_c + 1, sizeof(*c));
75 if (!c)
76 return AVERROR(ENOMEM);
77 *pc = c;
78 (*nb_c)++;
79
80 c += *nb_c - 1;
81 memset(c, 0, sizeof(*c));
82
83 av_pix_fmt_get_chroma_sub_sample(frame->format, &shift_h[1], &shift_v[1]);
84 shift_h[2] = shift_h[1];
85 shift_v[2] = shift_v[1];
86
87 c->ts = ts;
88 for (int p = 0; frame->data[p]; p++) {
89 const uint8_t *data = frame->data[p];
90 int linesize = av_image_get_linesize(frame->format, frame->width, p);
91 uint32_t checksum = 0;
92
93 av_assert0(linesize >= 0);
94
95 for (int j = 0; j < frame->height >> shift_v[p]; j++) {
96 checksum = av_adler32_update(checksum, data, linesize);
97 data += frame->linesize[p];
98 }
99
100 c->checksum[p] = checksum;
101 }
102
103 return 0;
104}
105
107{
108 AVFrame *f = pd->frame_recon;
109 int ret;
110
111 ret = avcodec_receive_frame(pd->enc, f);
112 if (ret < 0) {
113 fprintf(stderr, "Error retrieving a reconstructed frame\n");
114 return ret;
115 }
116
117 // the encoder's internal format (in which the reconstructed frames are
118 // exported) may be different from the user-facing pixel format
119 if (f->format != pd->enc->pix_fmt) {
120 if (!pd->scaler) {
121 pd->scaler = sws_getContext(f->width, f->height, f->format,
122 f->width, f->height, pd->enc->pix_fmt,
124 if (!pd->scaler)
125 return AVERROR(ENOMEM);
126 }
127
128 ret = sws_scale_frame(pd->scaler, pd->frame, f);
129 if (ret < 0) {
130 fprintf(stderr, "Error converting pixel formats\n");
131 return ret;
132 }
133
135 f = pd->frame;
136 }
137
139 pkt->pts, f);
141
142 return 0;
143}
144
146{
147 PrivData *pd = dc->opaque;
148 int ret;
149
150 if (!avcodec_is_open(pd->enc)) {
151 if (!frame) {
152 fprintf(stderr, "No input frames were decoded\n");
153 return AVERROR_INVALIDDATA;
154 }
155
156 pd->enc->width = frame->width;
157 pd->enc->height = frame->height;
158 pd->enc->pix_fmt = frame->format;
161
162 // real timestamps do not matter for this test, so we just
163 // pretend the input is 25fps CFR to avoid any timestamp issues
164 pd->enc->time_base = (AVRational){ 1, 25 };
165
166 ret = avcodec_open2(pd->enc, NULL, NULL);
167 if (ret < 0) {
168 fprintf(stderr, "Error opening the encoder\n");
169 return ret;
170 }
171 }
172
173 if (frame) {
174 frame->pts = pd->pts_in++;
175
176 // avoid forcing coded frame type
177 frame->pict_type = AV_PICTURE_TYPE_NONE;
178 }
179
180 ret = avcodec_send_frame(pd->enc, frame);
181 if (ret == AVERROR_EOF && !frame)
182 return 0;
183 if (ret < 0) {
184 fprintf(stderr, "Error submitting a frame for encoding\n");
185 return ret;
186 }
187
188 while (1) {
189 AVPacket *pkt = pd->pkt;
190
191 ret = avcodec_receive_packet(pd->enc, pkt);
192 if (ret == AVERROR(EAGAIN))
193 break;
194 else if (ret == AVERROR_EOF)
195 pkt = NULL;
196 else if (ret < 0) {
197 fprintf(stderr, "Error receiving a frame from the encoder\n");
198 return ret;
199 }
200
201 if (pkt) {
202 ret = recon_frame_process(pd, pkt);
203 if (ret < 0)
204 return ret;
205 }
206
207 if (!avcodec_is_open(pd->dec)) {
208 if (!pkt) {
209 fprintf(stderr, "No packets were received from the encoder\n");
210 return AVERROR(EINVAL);
211 }
212
213 pd->dec->width = pd->enc->width;
214 pd->dec->height = pd->enc->height;
215 pd->dec->pix_fmt = pd->enc->pix_fmt;
218 if (pd->enc->extradata_size) {
219 pd->dec->extradata = av_memdup(pd->enc->extradata,
221 if (!pd->dec->extradata)
222 return AVERROR(ENOMEM);
223 }
224
225 ret = avcodec_open2(pd->dec, NULL, NULL);
226 if (ret < 0) {
227 fprintf(stderr, "Error opening the decoder\n");
228 return ret;
229 }
230 }
231
232 ret = avcodec_send_packet(pd->dec, pkt);
233 if (ret < 0) {
234 fprintf(stderr, "Error sending a packet to decoder\n");
235 return ret;
236 }
237
238 while (1) {
239 ret = avcodec_receive_frame(pd->dec, pd->frame);
240 if (ret == AVERROR(EAGAIN))
241 break;
242 else if (ret == AVERROR_EOF)
243 return 0;
244 else if (ret < 0) {
245 fprintf(stderr, "Error receiving a frame from decoder\n");
246 return ret;
247 }
248
250 pd->frame->pts, pd->frame);
252 if (ret < 0)
253 return ret;
254 }
255
256 }
257
258 return 0;
259}
260
261static int frame_checksum_compare(const void *a, const void *b)
262{
263 const FrameChecksum *ca = a;
264 const FrameChecksum *cb = b;
265 if (ca->ts == cb->ts)
266 return 0;
267 return FFSIGN(ca->ts - cb->ts);
268}
269
270int main(int argc, char **argv)
271{
272 PrivData pd;
273 DecodeContext dc;
274
275 const char *filename, *enc_name, *enc_opts, *thread_type = NULL, *nb_threads = NULL;
276 const AVCodec *enc, *dec;
277 int ret = 0, max_frames = 0;
278
279 if (argc < 4) {
280 fprintf(stderr,
281 "Usage: %s <input file> <encoder> <encoder options> "
282 "[<max frame count> [<thread count> <thread type>]\n",
283 argv[0]);
284 return 0;
285 }
286
287 filename = argv[1];
288 enc_name = argv[2];
289 enc_opts = argv[3];
290 if (argc >= 5)
291 max_frames = strtol(argv[4], NULL, 0);
292 if (argc >= 6)
293 nb_threads = argv[5];
294 if (argc >= 7)
295 thread_type = argv[6];
296
297 memset(&dc, 0, sizeof(dc));
298 memset(&pd, 0, sizeof(pd));
299
301 if (!enc) {
302 fprintf(stderr, "No such encoder: %s\n", enc_name);
303 return 1;
304 }
306 fprintf(stderr, "Encoder '%s' cannot output reconstructed frames\n",
307 enc->name);
308 return 1;
309 }
310
311 dec = avcodec_find_decoder(enc->id);
312 if (!dec) {
313 fprintf(stderr, "No decoder for: %s\n", avcodec_get_name(enc->id));
314 return 1;
315 }
316
317 pd.enc = avcodec_alloc_context3(enc);
318 if (!pd.enc) {
319 fprintf(stderr, "Error allocating encoder\n");
320 return 1;
321 }
322
323 ret = av_set_options_string(pd.enc, enc_opts, "=", ",");
324 if (ret < 0) {
325 fprintf(stderr, "Error setting encoder options\n");
326 goto fail;
327 }
329
330 pd.dec = avcodec_alloc_context3(dec);
331 if (!pd.dec) {
332 fprintf(stderr, "Error allocating decoder\n");
333 goto fail;
334 }
335
338
339 pd.frame = av_frame_alloc();
341 pd.pkt = av_packet_alloc();
342 if (!pd.frame ||!pd.frame_recon || !pd.pkt) {
343 ret = 1;
344 goto fail;
345 }
346
347 ret = ds_open(&dc, filename, 0);
348 if (ret < 0) {
349 fprintf(stderr, "Error opening the file\n");
350 goto fail;
351 }
352
354 dc.opaque = &pd;
355 dc.max_frames = max_frames;
356
357 ret = av_dict_set(&dc.decoder_opts, "threads", nb_threads, 0);
358 ret |= av_dict_set(&dc.decoder_opts, "thread_type", thread_type, 0);
359
360 ret = ds_run(&dc);
361 if (ret < 0)
362 goto fail;
363
365 fprintf(stderr, "Mismatching frame counts: recon=%zu decoded=%zu\n",
367 ret = 1;
368 goto fail;
369 }
370
371 // reconstructed frames are in coded order, sort them by pts into presentation order
372 qsort(pd.checksums_recon, pd.nb_checksums_recon, sizeof(*pd.checksums_recon),
374
375 for (size_t i = 0; i < pd.nb_checksums_decoded; i++) {
376 const FrameChecksum *d = &pd.checksums_decoded[i];
377 const FrameChecksum *r = &pd.checksums_recon[i];
378
379 for (int p = 0; p < FF_ARRAY_ELEMS(d->checksum); p++)
380 if (d->checksum[p] != r->checksum[p]) {
381 fprintf(stderr, "Checksum mismatch in frame ts=%"PRId64", plane %d\n",
382 d->ts, p);
383 ret = 1;
384 goto fail;
385 }
386 }
387 fprintf(stderr, "All %zu encoded frames match\n", pd.nb_checksums_decoded);
388
389fail:
394 av_frame_free(&pd.frame);
396 av_packet_free(&pd.pkt);
397 ds_free(&dc);
398 return !!ret;
399}
Public header for Adler-32 hash function implementation.
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
Libavcodec external API header.
Main libavformat public API header.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define f(width, name)
Definition cbs_vp8.c:236
common internal and external API header
#define FFSIGN(a)
Definition common.h:75
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
int ds_open(DecodeContext *dc, const char *url, int stream_idx)
void ds_free(DecodeContext *dc)
int ds_run(DecodeContext *dc)
#define AV_EF_CRCCHECK
Verify checksums embedded in the bitstream (could be of either encoded or decoded data,...
Definition defs.h:48
static AVPacket * pkt
static AVFrame * frame
int main
Definition dovi_rpuenc.c:38
static int process_frame(DecodeContext *dc, AVFrame *frame)
static int recon_frame_process(PrivData *pd, const AVPacket *pkt)
static int frame_hash(FrameChecksum **pc, size_t *nb_c, int64_t ts, const AVFrame *frame)
static int frame_checksum_compare(const void *a, const void *b)
error code definitions
static av_always_inline int process_frame(AVTextFormatContext *tfc, InputFile *ifile, AVFrame *frame, const AVPacket *pkt, int *packet_new)
Definition ffprobe.c:1596
reference-counted frame API
#define fail
Definition test.h:479
int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
Initialize the AVCodecContext to use the given AVCodec.
Definition avcodec.c:144
#define AV_CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
Definition avcodec.h:322
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:990
const char * avcodec_get_name(enum AVCodecID id)
Get the name of a codec.
Definition utils.c:421
const AVCodec * avcodec_find_encoder_by_name(const char *name)
Find a registered encoder with the specified name.
Definition allcodecs.c:1013
#define AV_CODEC_CAP_ENCODER_RECON_FRAME
The encoder is able to output reconstructed frame data, i.e.
Definition codec.h:162
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:164
#define AV_CODEC_FLAG_RECON_FRAME
Request the encoder to output reconstructed frames, i.e. frames that would be produced by decoding th...
Definition avcodec.h:244
int avcodec_receive_frame(AVCodecContext *avctx, AVFrame *frame)
Alias for avcodec_receive_frame_flags(avctx, frame, 0).
Definition avcodec.c:720
int avcodec_send_packet(AVCodecContext *avctx, const AVPacket *avpkt)
Supply raw packet data as input to a decoder.
Definition decode.c:730
int avcodec_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
Read encoded data from the encoder.
Definition encode.c:578
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding.
Definition defs.h:40
int avcodec_send_frame(AVCodecContext *avctx, const AVFrame *frame)
Supply a raw video or audio frame to the encoder.
Definition encode.c:545
int avcodec_is_open(AVCodecContext *s)
Definition avcodec.c:702
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
Definition packet.c:74
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
Definition packet.c:63
AVAdler av_adler32_update(AVAdler adler, const uint8_t *buf, size_t len)
Calculate the Adler32 checksum of a buffer.
Definition adler32.c:44
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition dict.c:86
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR_EOF
End of file.
Definition error.h:57
#define AVERROR(e)
Definition error.h:45
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition frame.c:496
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:64
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition frame.c:52
void * av_realloc_array(void *ptr, size_t nmemb, size_t size)
Definition mem.c:217
void * av_memdup(const void *p, size_t size)
Duplicate a buffer with av_malloc().
Definition mem.c:302
int av_image_get_linesize(enum AVPixelFormat pix_fmt, int width, int plane)
Compute the size of an image line with format pix_fmt and width width for the plane plane.
Definition imgutils.c:76
@ AV_PICTURE_TYPE_NONE
Undefined.
Definition avutil.h:277
int sws_scale_frame(SwsContext *sws, AVFrame *dst, const AVFrame *src)
Scale source data from src and write the output to dst.
Definition swscale.c:1405
SwsContext * sws_getContext(int srcW, int srcH, enum AVPixelFormat srcFormat, int dstW, int dstH, enum AVPixelFormat dstFormat, int flags, SwsFilter *srcFilter, SwsFilter *dstFilter, const double *param)
Allocate and return an SwsContext.
Definition utils.c:1919
@ SWS_BITEXACT
Definition swscale.h:178
int av_set_options_string(void *ctx, const char *opts, const char *key_val_sep, const char *pairs_sep)
Parse the key/value pairs list in opts.
Definition opt.c:1893
int a
misc image utilities
#define r
Definition input.c:42
#define b
Definition input.c:43
Memory handling functions.
const char data[16]
Definition mxf.c:149
AVOptions.
int av_pix_fmt_get_chroma_sub_sample(enum AVPixelFormat pix_fmt, int *h_shift, int *v_shift)
Utility function to access log2_chroma_w log2_chroma_h from the pixel format AVPixFmtDescriptor.
Definition pixdesc.c:3488
const char enc_name[6]
Definition rtp.c:36
#define FF_ARRAY_ELEMS(a)
main external API structure.
Definition avcodec.h:443
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition avcodec.h:643
int width
picture width / height.
Definition avcodec.h:604
int thread_type
Which multithreading methods to use.
Definition avcodec.h:1589
int thread_count
thread count is used to decide how many independent tasks should be passed to execute()
Definition avcodec.h:1579
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition avcodec.h:547
int flags
AV_CODEC_FLAG_*.
Definition avcodec.h:500
uint8_t * extradata
Out-of-band global headers that may be used by some codecs.
Definition avcodec.h:526
int extradata_size
Definition avcodec.h:527
int err_recognition
Error recognition; may misdetect some more or less valid parts as errors.
Definition avcodec.h:1416
AVCodec.
Definition codec.h:175
enum AVCodecID id
Definition codec.h:189
const char * name
Name of the codec implementation.
Definition codec.h:182
int capabilities
Codec capabilities.
Definition codec.h:194
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition frame.h:574
This structure stores compressed data.
Definition packet.h:580
Rational number (pair of numerator and denominator).
Definition rational.h:58
AVDictionary * decoder_opts
int(* process_frame)(struct DecodeContext *dc, AVFrame *frame)
AVCodecContext * decoder
size_t nb_checksums_decoded
AVFrame * frame_recon
size_t nb_checksums_recon
AVCodecContext * dec
FrameChecksum * checksums_decoded
FrameChecksum * checksums_recon
AVCodecContext * enc
struct SwsContext * scaler
Main external API structure.
Definition swscale.h:227
external API header
#define av_freep(p)
static double cb(void *priv, double x, double y)
Definition vf_geq.c:247
static double c[64]