enc_recon_frame_test.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. /* A test for AV_CODEC_FLAG_RECON_FRAME
  21. * TODO: dump reconstructed frames to disk */
  22. #include <stdio.h>
  23. #include <stdint.h>
  24. #include <stdlib.h>
  25. #include "decode_simple.h"
  26. #include "libavutil/adler32.h"
  27. #include "libavutil/avassert.h"
  28. #include "libavutil/common.h"
  29. #include "libavutil/error.h"
  30. #include "libavutil/frame.h"
  31. #include "libavutil/imgutils.h"
  32. #include "libavutil/mem.h"
  33. #include "libavutil/opt.h"
  34. #include "libavformat/avformat.h"
  35. #include "libavcodec/avcodec.h"
  36. #include "libavcodec/codec.h"
  37. #include "libswscale/swscale.h"
  38. typedef struct FrameChecksum {
  39. int64_t ts;
  40. uint32_t checksum[4];
  41. } FrameChecksum;
  42. typedef struct PrivData {
  43. AVCodecContext *enc;
  44. AVCodecContext *dec;
  45. int64_t pts_in;
  46. AVPacket *pkt;
  47. AVFrame *frame, *frame_recon;
  48. struct SwsContext *scaler;
  49. FrameChecksum *checksums_decoded;
  50. size_t nb_checksums_decoded;
  51. FrameChecksum *checksums_recon;
  52. size_t nb_checksums_recon;
  53. } PrivData;
  54. static int frame_hash(FrameChecksum **pc, size_t *nb_c, int64_t ts,
  55. const AVFrame *frame)
  56. {
  57. FrameChecksum *c;
  58. int shift_h[4] = { 0 }, shift_v[4] = { 0 };
  59. c = av_realloc_array(*pc, *nb_c + 1, sizeof(*c));
  60. if (!c)
  61. return AVERROR(ENOMEM);
  62. *pc = c;
  63. (*nb_c)++;
  64. c += *nb_c - 1;
  65. memset(c, 0, sizeof(*c));
  66. av_pix_fmt_get_chroma_sub_sample(frame->format, &shift_h[1], &shift_v[1]);
  67. shift_h[2] = shift_h[1];
  68. shift_v[2] = shift_v[1];
  69. c->ts = ts;
  70. for (int p = 0; frame->data[p]; p++) {
  71. const uint8_t *data = frame->data[p];
  72. int linesize = av_image_get_linesize(frame->format, frame->width, p);
  73. uint32_t checksum = 0;
  74. av_assert0(linesize >= 0);
  75. for (int j = 0; j < frame->height >> shift_v[p]; j++) {
  76. checksum = av_adler32_update(checksum, data, linesize);
  77. data += frame->linesize[p];
  78. }
  79. c->checksum[p] = checksum;
  80. }
  81. return 0;
  82. }
  83. static int recon_frame_process(PrivData *pd, const AVPacket *pkt)
  84. {
  85. AVFrame *f = pd->frame_recon;
  86. int ret;
  87. ret = avcodec_receive_frame(pd->enc, f);
  88. if (ret < 0) {
  89. fprintf(stderr, "Error retrieving a reconstructed frame\n");
  90. return ret;
  91. }
  92. // the encoder's internal format (in which the reconsturcted frames are
  93. // exported) may be different from the user-facing pixel format
  94. if (f->format != pd->enc->pix_fmt) {
  95. if (!pd->scaler) {
  96. pd->scaler = sws_getContext(f->width, f->height, f->format,
  97. f->width, f->height, pd->enc->pix_fmt,
  98. SWS_BITEXACT, NULL, NULL, NULL);
  99. if (!pd->scaler)
  100. return AVERROR(ENOMEM);
  101. }
  102. ret = sws_scale_frame(pd->scaler, pd->frame, f);
  103. if (ret < 0) {
  104. fprintf(stderr, "Error converting pixel formats\n");
  105. return ret;
  106. }
  107. av_frame_unref(f);
  108. f = pd->frame;
  109. }
  110. ret = frame_hash(&pd->checksums_recon, &pd->nb_checksums_recon,
  111. pkt->pts, f);
  112. av_frame_unref(f);
  113. return 0;
  114. }
  115. static int process_frame(DecodeContext *dc, AVFrame *frame)
  116. {
  117. PrivData *pd = dc->opaque;
  118. int ret;
  119. if (!avcodec_is_open(pd->enc)) {
  120. if (!frame) {
  121. fprintf(stderr, "No input frames were decoded\n");
  122. return AVERROR_INVALIDDATA;
  123. }
  124. pd->enc->width = frame->width;
  125. pd->enc->height = frame->height;
  126. pd->enc->pix_fmt = frame->format;
  127. pd->enc->thread_count = dc->decoder->thread_count;
  128. pd->enc->thread_type = dc->decoder->thread_type;
  129. // real timestamps do not matter for this test, so we just
  130. // pretend the input is 25fps CFR to avoid any timestamp issues
  131. pd->enc->time_base = (AVRational){ 1, 25 };
  132. ret = avcodec_open2(pd->enc, NULL, NULL);
  133. if (ret < 0) {
  134. fprintf(stderr, "Error opening the encoder\n");
  135. return ret;
  136. }
  137. }
  138. if (frame) {
  139. frame->pts = pd->pts_in++;
  140. // avoid forcing coded frame type
  141. frame->pict_type = AV_PICTURE_TYPE_NONE;
  142. }
  143. ret = avcodec_send_frame(pd->enc, frame);
  144. if (ret < 0) {
  145. fprintf(stderr, "Error submitting a frame for encoding\n");
  146. return ret;
  147. }
  148. while (1) {
  149. AVPacket *pkt = pd->pkt;
  150. ret = avcodec_receive_packet(pd->enc, pkt);
  151. if (ret == AVERROR(EAGAIN))
  152. break;
  153. else if (ret == AVERROR_EOF)
  154. pkt = NULL;
  155. else if (ret < 0) {
  156. fprintf(stderr, "Error receiving a frame from the encoder\n");
  157. return ret;
  158. }
  159. if (pkt) {
  160. ret = recon_frame_process(pd, pkt);
  161. if (ret < 0)
  162. return ret;
  163. }
  164. if (!avcodec_is_open(pd->dec)) {
  165. if (!pkt) {
  166. fprintf(stderr, "No packets were received from the encoder\n");
  167. return AVERROR(EINVAL);
  168. }
  169. pd->dec->width = pd->enc->width;
  170. pd->dec->height = pd->enc->height;
  171. pd->dec->pix_fmt = pd->enc->pix_fmt;
  172. pd->dec->thread_count = dc->decoder->thread_count;
  173. pd->dec->thread_type = dc->decoder->thread_type;
  174. if (pd->enc->extradata_size) {
  175. pd->dec->extradata = av_memdup(pd->enc->extradata,
  176. pd->enc->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
  177. if (!pd->dec->extradata)
  178. return AVERROR(ENOMEM);
  179. }
  180. ret = avcodec_open2(pd->dec, NULL, NULL);
  181. if (ret < 0) {
  182. fprintf(stderr, "Error opening the decoder\n");
  183. return ret;
  184. }
  185. }
  186. ret = avcodec_send_packet(pd->dec, pkt);
  187. if (ret < 0) {
  188. fprintf(stderr, "Error sending a packet to decoder\n");
  189. return ret;
  190. }
  191. while (1) {
  192. ret = avcodec_receive_frame(pd->dec, pd->frame);
  193. if (ret == AVERROR(EAGAIN))
  194. break;
  195. else if (ret == AVERROR_EOF)
  196. return 0;
  197. else if (ret < 0) {
  198. fprintf(stderr, "Error receving a frame from decoder\n");
  199. return ret;
  200. }
  201. ret = frame_hash(&pd->checksums_decoded, &pd->nb_checksums_decoded,
  202. pd->frame->pts, pd->frame);
  203. av_frame_unref(pd->frame);
  204. if (ret < 0)
  205. return ret;
  206. }
  207. }
  208. return 0;
  209. }
  210. static int frame_checksum_compare(const void *a, const void *b)
  211. {
  212. const FrameChecksum *ca = a;
  213. const FrameChecksum *cb = b;
  214. if (ca->ts == cb->ts)
  215. return 0;
  216. return FFSIGN(ca->ts - cb->ts);
  217. }
  218. int main(int argc, char **argv)
  219. {
  220. PrivData pd;
  221. DecodeContext dc;
  222. const char *filename, *enc_name, *enc_opts, *thread_type = NULL, *nb_threads = NULL;
  223. const AVCodec *enc, *dec;
  224. int ret = 0, max_frames = 0;
  225. if (argc < 4) {
  226. fprintf(stderr,
  227. "Usage: %s <input file> <encoder> <encoder options> "
  228. "[<max frame count> [<thread count> <thread type>]\n",
  229. argv[0]);
  230. return 0;
  231. }
  232. filename = argv[1];
  233. enc_name = argv[2];
  234. enc_opts = argv[3];
  235. if (argc >= 5)
  236. max_frames = strtol(argv[4], NULL, 0);
  237. if (argc >= 6)
  238. nb_threads = argv[5];
  239. if (argc >= 7)
  240. thread_type = argv[6];
  241. memset(&dc, 0, sizeof(dc));
  242. memset(&pd, 0, sizeof(pd));
  243. enc = avcodec_find_encoder_by_name(enc_name);
  244. if (!enc) {
  245. fprintf(stderr, "No such encoder: %s\n", enc_name);
  246. return 1;
  247. }
  248. if (!(enc->capabilities & AV_CODEC_CAP_ENCODER_RECON_FRAME)) {
  249. fprintf(stderr, "Encoder '%s' cannot output reconstructed frames\n",
  250. enc->name);
  251. return 1;
  252. }
  253. dec = avcodec_find_decoder(enc->id);
  254. if (!dec) {
  255. fprintf(stderr, "No decoder for: %s\n", avcodec_get_name(enc->id));
  256. return 1;
  257. }
  258. pd.enc = avcodec_alloc_context3(enc);
  259. if (!pd.enc) {
  260. fprintf(stderr, "Error allocating encoder\n");
  261. return 1;
  262. }
  263. ret = av_set_options_string(pd.enc, enc_opts, "=", ",");
  264. if (ret < 0) {
  265. fprintf(stderr, "Error setting encoder options\n");
  266. goto fail;
  267. }
  268. pd.enc->flags |= AV_CODEC_FLAG_RECON_FRAME | AV_CODEC_FLAG_BITEXACT;
  269. pd.dec = avcodec_alloc_context3(dec);
  270. if (!pd.dec) {
  271. fprintf(stderr, "Error allocating decoder\n");
  272. goto fail;
  273. }
  274. pd.dec->flags |= AV_CODEC_FLAG_BITEXACT;
  275. pd.dec->err_recognition |= AV_EF_CRCCHECK;
  276. pd.frame = av_frame_alloc();
  277. pd.frame_recon = av_frame_alloc();
  278. pd.pkt = av_packet_alloc();
  279. if (!pd.frame ||!pd.frame_recon || !pd.pkt) {
  280. ret = 1;
  281. goto fail;
  282. }
  283. ret = ds_open(&dc, filename, 0);
  284. if (ret < 0) {
  285. fprintf(stderr, "Error opening the file\n");
  286. goto fail;
  287. }
  288. dc.process_frame = process_frame;
  289. dc.opaque = &pd;
  290. dc.max_frames = max_frames;
  291. ret = av_dict_set(&dc.decoder_opts, "threads", nb_threads, 0);
  292. ret |= av_dict_set(&dc.decoder_opts, "thread_type", thread_type, 0);
  293. ret = ds_run(&dc);
  294. if (ret < 0)
  295. goto fail;
  296. if (pd.nb_checksums_decoded != pd.nb_checksums_recon) {
  297. fprintf(stderr, "Mismatching frame counts: recon=%zu decoded=%zu\n",
  298. pd.nb_checksums_recon, pd.nb_checksums_decoded);
  299. ret = 1;
  300. goto fail;
  301. }
  302. // reconstructed frames are in coded order, sort them by pts into presentation order
  303. qsort(pd.checksums_recon, pd.nb_checksums_recon, sizeof(*pd.checksums_recon),
  304. frame_checksum_compare);
  305. for (size_t i = 0; i < pd.nb_checksums_decoded; i++) {
  306. const FrameChecksum *d = &pd.checksums_decoded[i];
  307. const FrameChecksum *r = &pd.checksums_recon[i];
  308. for (int p = 0; p < FF_ARRAY_ELEMS(d->checksum); p++)
  309. if (d->checksum[p] != r->checksum[p]) {
  310. fprintf(stderr, "Checksum mismatch in frame ts=%"PRId64", plane %d\n",
  311. d->ts, p);
  312. ret = 1;
  313. goto fail;
  314. }
  315. }
  316. fprintf(stderr, "All %zu encoded frames match\n", pd.nb_checksums_decoded);
  317. fail:
  318. avcodec_free_context(&pd.enc);
  319. avcodec_free_context(&pd.dec);
  320. av_freep(&pd.checksums_decoded);
  321. av_freep(&pd.checksums_recon);
  322. av_frame_free(&pd.frame);
  323. av_frame_free(&pd.frame_recon);
  324. av_packet_free(&pd.pkt);
  325. ds_free(&dc);
  326. return !!ret;
  327. }