enc_recon_frame_test.c 11 KB

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