vp9cx_set_ref.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. /*
  2. * Copyright (c) 2016 The WebM project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. // VP9 Set Reference Frame
  11. // ============================
  12. //
  13. // This is an example demonstrating how to overwrite the VP9 encoder's
  14. // internal reference frame. In the sample we set the last frame to the
  15. // current frame. This technique could be used to bounce between two cameras.
  16. //
  17. // The decoder would also have to set the reference frame to the same value
  18. // on the same frame, or the video will become corrupt. The 'test_decode'
  19. // variable is set to 1 in this example that tests if the encoder and decoder
  20. // results are matching.
  21. //
  22. // Usage
  23. // -----
  24. // This example encodes a raw video. And the last argument passed in specifies
  25. // the frame number to update the reference frame on. For example, run
  26. // examples/vp9cx_set_ref 352 288 in.yuv out.ivf 4 30
  27. // The parameter is parsed as follows:
  28. //
  29. //
  30. // Extra Variables
  31. // ---------------
  32. // This example maintains the frame number passed on the command line
  33. // in the `update_frame_num` variable.
  34. //
  35. //
  36. // Configuration
  37. // -------------
  38. //
  39. // The reference frame is updated on the frame specified on the command
  40. // line.
  41. //
  42. // Observing The Effects
  43. // ---------------------
  44. // The encoder and decoder results should be matching when the same reference
  45. // frame setting operation is done in both encoder and decoder. Otherwise,
  46. // the encoder/decoder mismatch would be seen.
  47. #include <stdio.h>
  48. #include <stdlib.h>
  49. #include <string.h>
  50. #include "vpx/vp8cx.h"
  51. #include "vpx/vpx_decoder.h"
  52. #include "vpx/vpx_encoder.h"
  53. #include "vp9/common/vp9_common.h"
  54. #include "./tools_common.h"
  55. #include "./video_writer.h"
  56. static const char *exec_name;
  57. void usage_exit() {
  58. fprintf(stderr,
  59. "Usage: %s <width> <height> <infile> <outfile> "
  60. "<frame> <limit(optional)>\n",
  61. exec_name);
  62. exit(EXIT_FAILURE);
  63. }
  64. static int compare_img(const vpx_image_t *const img1,
  65. const vpx_image_t *const img2) {
  66. uint32_t l_w = img1->d_w;
  67. uint32_t c_w = (img1->d_w + img1->x_chroma_shift) >> img1->x_chroma_shift;
  68. const uint32_t c_h =
  69. (img1->d_h + img1->y_chroma_shift) >> img1->y_chroma_shift;
  70. uint32_t i;
  71. int match = 1;
  72. match &= (img1->fmt == img2->fmt);
  73. match &= (img1->d_w == img2->d_w);
  74. match &= (img1->d_h == img2->d_h);
  75. for (i = 0; i < img1->d_h; ++i)
  76. match &= (memcmp(img1->planes[VPX_PLANE_Y] + i * img1->stride[VPX_PLANE_Y],
  77. img2->planes[VPX_PLANE_Y] + i * img2->stride[VPX_PLANE_Y],
  78. l_w) == 0);
  79. for (i = 0; i < c_h; ++i)
  80. match &= (memcmp(img1->planes[VPX_PLANE_U] + i * img1->stride[VPX_PLANE_U],
  81. img2->planes[VPX_PLANE_U] + i * img2->stride[VPX_PLANE_U],
  82. c_w) == 0);
  83. for (i = 0; i < c_h; ++i)
  84. match &= (memcmp(img1->planes[VPX_PLANE_V] + i * img1->stride[VPX_PLANE_V],
  85. img2->planes[VPX_PLANE_V] + i * img2->stride[VPX_PLANE_V],
  86. c_w) == 0);
  87. return match;
  88. }
  89. #define mmin(a, b) ((a) < (b) ? (a) : (b))
  90. static void find_mismatch(const vpx_image_t *const img1,
  91. const vpx_image_t *const img2, int yloc[4],
  92. int uloc[4], int vloc[4]) {
  93. const uint32_t bsize = 64;
  94. const uint32_t bsizey = bsize >> img1->y_chroma_shift;
  95. const uint32_t bsizex = bsize >> img1->x_chroma_shift;
  96. const uint32_t c_w =
  97. (img1->d_w + img1->x_chroma_shift) >> img1->x_chroma_shift;
  98. const uint32_t c_h =
  99. (img1->d_h + img1->y_chroma_shift) >> img1->y_chroma_shift;
  100. int match = 1;
  101. uint32_t i, j;
  102. yloc[0] = yloc[1] = yloc[2] = yloc[3] = -1;
  103. for (i = 0, match = 1; match && i < img1->d_h; i += bsize) {
  104. for (j = 0; match && j < img1->d_w; j += bsize) {
  105. int k, l;
  106. const int si = mmin(i + bsize, img1->d_h) - i;
  107. const int sj = mmin(j + bsize, img1->d_w) - j;
  108. for (k = 0; match && k < si; ++k) {
  109. for (l = 0; match && l < sj; ++l) {
  110. if (*(img1->planes[VPX_PLANE_Y] +
  111. (i + k) * img1->stride[VPX_PLANE_Y] + j + l) !=
  112. *(img2->planes[VPX_PLANE_Y] +
  113. (i + k) * img2->stride[VPX_PLANE_Y] + j + l)) {
  114. yloc[0] = i + k;
  115. yloc[1] = j + l;
  116. yloc[2] = *(img1->planes[VPX_PLANE_Y] +
  117. (i + k) * img1->stride[VPX_PLANE_Y] + j + l);
  118. yloc[3] = *(img2->planes[VPX_PLANE_Y] +
  119. (i + k) * img2->stride[VPX_PLANE_Y] + j + l);
  120. match = 0;
  121. break;
  122. }
  123. }
  124. }
  125. }
  126. }
  127. uloc[0] = uloc[1] = uloc[2] = uloc[3] = -1;
  128. for (i = 0, match = 1; match && i < c_h; i += bsizey) {
  129. for (j = 0; match && j < c_w; j += bsizex) {
  130. int k, l;
  131. const int si = mmin(i + bsizey, c_h - i);
  132. const int sj = mmin(j + bsizex, c_w - j);
  133. for (k = 0; match && k < si; ++k) {
  134. for (l = 0; match && l < sj; ++l) {
  135. if (*(img1->planes[VPX_PLANE_U] +
  136. (i + k) * img1->stride[VPX_PLANE_U] + j + l) !=
  137. *(img2->planes[VPX_PLANE_U] +
  138. (i + k) * img2->stride[VPX_PLANE_U] + j + l)) {
  139. uloc[0] = i + k;
  140. uloc[1] = j + l;
  141. uloc[2] = *(img1->planes[VPX_PLANE_U] +
  142. (i + k) * img1->stride[VPX_PLANE_U] + j + l);
  143. uloc[3] = *(img2->planes[VPX_PLANE_U] +
  144. (i + k) * img2->stride[VPX_PLANE_U] + j + l);
  145. match = 0;
  146. break;
  147. }
  148. }
  149. }
  150. }
  151. }
  152. vloc[0] = vloc[1] = vloc[2] = vloc[3] = -1;
  153. for (i = 0, match = 1; match && i < c_h; i += bsizey) {
  154. for (j = 0; match && j < c_w; j += bsizex) {
  155. int k, l;
  156. const int si = mmin(i + bsizey, c_h - i);
  157. const int sj = mmin(j + bsizex, c_w - j);
  158. for (k = 0; match && k < si; ++k) {
  159. for (l = 0; match && l < sj; ++l) {
  160. if (*(img1->planes[VPX_PLANE_V] +
  161. (i + k) * img1->stride[VPX_PLANE_V] + j + l) !=
  162. *(img2->planes[VPX_PLANE_V] +
  163. (i + k) * img2->stride[VPX_PLANE_V] + j + l)) {
  164. vloc[0] = i + k;
  165. vloc[1] = j + l;
  166. vloc[2] = *(img1->planes[VPX_PLANE_V] +
  167. (i + k) * img1->stride[VPX_PLANE_V] + j + l);
  168. vloc[3] = *(img2->planes[VPX_PLANE_V] +
  169. (i + k) * img2->stride[VPX_PLANE_V] + j + l);
  170. match = 0;
  171. break;
  172. }
  173. }
  174. }
  175. }
  176. }
  177. }
  178. static void testing_decode(vpx_codec_ctx_t *encoder, vpx_codec_ctx_t *decoder,
  179. unsigned int frame_out, int *mismatch_seen) {
  180. vpx_image_t enc_img, dec_img;
  181. struct vp9_ref_frame ref_enc, ref_dec;
  182. if (*mismatch_seen) return;
  183. ref_enc.idx = 0;
  184. ref_dec.idx = 0;
  185. if (vpx_codec_control(encoder, VP9_GET_REFERENCE, &ref_enc))
  186. die_codec(encoder, "Failed to get encoder reference frame");
  187. enc_img = ref_enc.img;
  188. if (vpx_codec_control(decoder, VP9_GET_REFERENCE, &ref_dec))
  189. die_codec(decoder, "Failed to get decoder reference frame");
  190. dec_img = ref_dec.img;
  191. if (!compare_img(&enc_img, &dec_img)) {
  192. int y[4], u[4], v[4];
  193. *mismatch_seen = 1;
  194. find_mismatch(&enc_img, &dec_img, y, u, v);
  195. printf(
  196. "Encode/decode mismatch on frame %d at"
  197. " Y[%d, %d] {%d/%d},"
  198. " U[%d, %d] {%d/%d},"
  199. " V[%d, %d] {%d/%d}",
  200. frame_out, y[0], y[1], y[2], y[3], u[0], u[1], u[2], u[3], v[0], v[1],
  201. v[2], v[3]);
  202. }
  203. vpx_img_free(&enc_img);
  204. vpx_img_free(&dec_img);
  205. }
  206. static int encode_frame(vpx_codec_ctx_t *ecodec, vpx_image_t *img,
  207. unsigned int frame_in, VpxVideoWriter *writer,
  208. int test_decode, vpx_codec_ctx_t *dcodec,
  209. unsigned int *frame_out, int *mismatch_seen) {
  210. int got_pkts = 0;
  211. vpx_codec_iter_t iter = NULL;
  212. const vpx_codec_cx_pkt_t *pkt = NULL;
  213. int got_data;
  214. const vpx_codec_err_t res =
  215. vpx_codec_encode(ecodec, img, frame_in, 1, 0, VPX_DL_GOOD_QUALITY);
  216. if (res != VPX_CODEC_OK) die_codec(ecodec, "Failed to encode frame");
  217. got_data = 0;
  218. while ((pkt = vpx_codec_get_cx_data(ecodec, &iter)) != NULL) {
  219. got_pkts = 1;
  220. if (pkt->kind == VPX_CODEC_CX_FRAME_PKT) {
  221. const int keyframe = (pkt->data.frame.flags & VPX_FRAME_IS_KEY) != 0;
  222. if (!(pkt->data.frame.flags & VPX_FRAME_IS_FRAGMENT)) {
  223. *frame_out += 1;
  224. }
  225. if (!vpx_video_writer_write_frame(writer, pkt->data.frame.buf,
  226. pkt->data.frame.sz,
  227. pkt->data.frame.pts)) {
  228. die_codec(ecodec, "Failed to write compressed frame");
  229. }
  230. printf(keyframe ? "K" : ".");
  231. fflush(stdout);
  232. got_data = 1;
  233. // Decode 1 frame.
  234. if (test_decode) {
  235. if (vpx_codec_decode(dcodec, pkt->data.frame.buf,
  236. (unsigned int)pkt->data.frame.sz, NULL, 0))
  237. die_codec(dcodec, "Failed to decode frame.");
  238. }
  239. }
  240. }
  241. // Mismatch checking
  242. if (got_data && test_decode) {
  243. testing_decode(ecodec, dcodec, *frame_out, mismatch_seen);
  244. }
  245. return got_pkts;
  246. }
  247. int main(int argc, char **argv) {
  248. FILE *infile = NULL;
  249. // Encoder
  250. vpx_codec_ctx_t ecodec;
  251. vpx_codec_enc_cfg_t cfg;
  252. unsigned int frame_in = 0;
  253. vpx_image_t raw;
  254. vpx_codec_err_t res;
  255. VpxVideoInfo info;
  256. VpxVideoWriter *writer = NULL;
  257. const VpxInterface *encoder = NULL;
  258. // Test encoder/decoder mismatch.
  259. int test_decode = 1;
  260. // Decoder
  261. vpx_codec_ctx_t dcodec;
  262. unsigned int frame_out = 0;
  263. // The frame number to set reference frame on
  264. unsigned int update_frame_num = 0;
  265. int mismatch_seen = 0;
  266. const int fps = 30;
  267. const int bitrate = 500;
  268. const char *width_arg = NULL;
  269. const char *height_arg = NULL;
  270. const char *infile_arg = NULL;
  271. const char *outfile_arg = NULL;
  272. const char *update_frame_num_arg = NULL;
  273. unsigned int limit = 0;
  274. vp9_zero(ecodec);
  275. vp9_zero(cfg);
  276. vp9_zero(info);
  277. exec_name = argv[0];
  278. if (argc < 6) die("Invalid number of arguments");
  279. width_arg = argv[1];
  280. height_arg = argv[2];
  281. infile_arg = argv[3];
  282. outfile_arg = argv[4];
  283. update_frame_num_arg = argv[5];
  284. encoder = get_vpx_encoder_by_name("vp9");
  285. if (!encoder) die("Unsupported codec.");
  286. update_frame_num = (unsigned int)strtoul(update_frame_num_arg, NULL, 0);
  287. // In VP9, the reference buffers (cm->buffer_pool->frame_bufs[i].buf) are
  288. // allocated while calling vpx_codec_encode(), thus, setting reference for
  289. // 1st frame isn't supported.
  290. if (update_frame_num <= 1) {
  291. die("Couldn't parse frame number '%s'\n", update_frame_num_arg);
  292. }
  293. if (argc > 6) {
  294. limit = (unsigned int)strtoul(argv[6], NULL, 0);
  295. if (update_frame_num > limit)
  296. die("Update frame number couldn't larger than limit\n");
  297. }
  298. info.codec_fourcc = encoder->fourcc;
  299. info.frame_width = (int)strtol(width_arg, NULL, 0);
  300. info.frame_height = (int)strtol(height_arg, NULL, 0);
  301. info.time_base.numerator = 1;
  302. info.time_base.denominator = fps;
  303. if (info.frame_width <= 0 || info.frame_height <= 0 ||
  304. (info.frame_width % 2) != 0 || (info.frame_height % 2) != 0) {
  305. die("Invalid frame size: %dx%d", info.frame_width, info.frame_height);
  306. }
  307. if (!vpx_img_alloc(&raw, VPX_IMG_FMT_I420, info.frame_width,
  308. info.frame_height, 1)) {
  309. die("Failed to allocate image.");
  310. }
  311. printf("Using %s\n", vpx_codec_iface_name(encoder->codec_interface()));
  312. res = vpx_codec_enc_config_default(encoder->codec_interface(), &cfg, 0);
  313. if (res) die_codec(&ecodec, "Failed to get default codec config.");
  314. cfg.g_w = info.frame_width;
  315. cfg.g_h = info.frame_height;
  316. cfg.g_timebase.num = info.time_base.numerator;
  317. cfg.g_timebase.den = info.time_base.denominator;
  318. cfg.rc_target_bitrate = bitrate;
  319. cfg.g_lag_in_frames = 3;
  320. writer = vpx_video_writer_open(outfile_arg, kContainerIVF, &info);
  321. if (!writer) die("Failed to open %s for writing.", outfile_arg);
  322. if (!(infile = fopen(infile_arg, "rb")))
  323. die("Failed to open %s for reading.", infile_arg);
  324. if (vpx_codec_enc_init(&ecodec, encoder->codec_interface(), &cfg, 0))
  325. die_codec(&ecodec, "Failed to initialize encoder");
  326. // Disable alt_ref.
  327. if (vpx_codec_control(&ecodec, VP8E_SET_ENABLEAUTOALTREF, 0))
  328. die_codec(&ecodec, "Failed to set enable auto alt ref");
  329. if (test_decode) {
  330. const VpxInterface *decoder = get_vpx_decoder_by_name("vp9");
  331. if (vpx_codec_dec_init(&dcodec, decoder->codec_interface(), NULL, 0))
  332. die_codec(&dcodec, "Failed to initialize decoder.");
  333. }
  334. // Encode frames.
  335. while (vpx_img_read(&raw, infile)) {
  336. if (limit && frame_in >= limit) break;
  337. if (update_frame_num > 1 && frame_out + 1 == update_frame_num) {
  338. vpx_ref_frame_t ref;
  339. ref.frame_type = VP8_LAST_FRAME;
  340. ref.img = raw;
  341. // Set reference frame in encoder.
  342. if (vpx_codec_control(&ecodec, VP8_SET_REFERENCE, &ref))
  343. die_codec(&ecodec, "Failed to set reference frame");
  344. printf(" <SET_REF>");
  345. // If set_reference in decoder is commented out, the enc/dec mismatch
  346. // would be seen.
  347. if (test_decode) {
  348. if (vpx_codec_control(&dcodec, VP8_SET_REFERENCE, &ref))
  349. die_codec(&dcodec, "Failed to set reference frame");
  350. }
  351. }
  352. encode_frame(&ecodec, &raw, frame_in, writer, test_decode, &dcodec,
  353. &frame_out, &mismatch_seen);
  354. frame_in++;
  355. if (mismatch_seen) break;
  356. }
  357. // Flush encoder.
  358. if (!mismatch_seen)
  359. while (encode_frame(&ecodec, NULL, frame_in, writer, test_decode, &dcodec,
  360. &frame_out, &mismatch_seen)) {
  361. }
  362. printf("\n");
  363. fclose(infile);
  364. printf("Processed %d frames.\n", frame_out);
  365. if (test_decode) {
  366. if (!mismatch_seen)
  367. printf("Encoder/decoder results are matching.\n");
  368. else
  369. printf("Encoder/decoder results are NOT matching.\n");
  370. }
  371. if (test_decode)
  372. if (vpx_codec_destroy(&dcodec))
  373. die_codec(&dcodec, "Failed to destroy decoder");
  374. vpx_img_free(&raw);
  375. if (vpx_codec_destroy(&ecodec))
  376. die_codec(&ecodec, "Failed to destroy encoder.");
  377. vpx_video_writer_close(writer);
  378. return EXIT_SUCCESS;
  379. }