vp9cx_set_ref.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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 void testing_decode(vpx_codec_ctx_t *encoder, vpx_codec_ctx_t *decoder,
  65. unsigned int frame_out, int *mismatch_seen) {
  66. vpx_image_t enc_img, dec_img;
  67. struct vp9_ref_frame ref_enc, ref_dec;
  68. if (*mismatch_seen) return;
  69. ref_enc.idx = 0;
  70. ref_dec.idx = 0;
  71. if (vpx_codec_control(encoder, VP9_GET_REFERENCE, &ref_enc))
  72. die_codec(encoder, "Failed to get encoder reference frame");
  73. enc_img = ref_enc.img;
  74. if (vpx_codec_control(decoder, VP9_GET_REFERENCE, &ref_dec))
  75. die_codec(decoder, "Failed to get decoder reference frame");
  76. dec_img = ref_dec.img;
  77. if (!compare_img(&enc_img, &dec_img)) {
  78. int y[4], u[4], v[4];
  79. *mismatch_seen = 1;
  80. find_mismatch(&enc_img, &dec_img, y, u, v);
  81. printf(
  82. "Encode/decode mismatch on frame %d at"
  83. " Y[%d, %d] {%d/%d},"
  84. " U[%d, %d] {%d/%d},"
  85. " V[%d, %d] {%d/%d}",
  86. frame_out, y[0], y[1], y[2], y[3], u[0], u[1], u[2], u[3], v[0], v[1],
  87. v[2], v[3]);
  88. }
  89. vpx_img_free(&enc_img);
  90. vpx_img_free(&dec_img);
  91. }
  92. static int encode_frame(vpx_codec_ctx_t *ecodec, vpx_image_t *img,
  93. unsigned int frame_in, VpxVideoWriter *writer,
  94. int test_decode, vpx_codec_ctx_t *dcodec,
  95. unsigned int *frame_out, int *mismatch_seen) {
  96. int got_pkts = 0;
  97. vpx_codec_iter_t iter = NULL;
  98. const vpx_codec_cx_pkt_t *pkt = NULL;
  99. int got_data;
  100. const vpx_codec_err_t res =
  101. vpx_codec_encode(ecodec, img, frame_in, 1, 0, VPX_DL_GOOD_QUALITY);
  102. if (res != VPX_CODEC_OK) die_codec(ecodec, "Failed to encode frame");
  103. got_data = 0;
  104. while ((pkt = vpx_codec_get_cx_data(ecodec, &iter)) != NULL) {
  105. got_pkts = 1;
  106. if (pkt->kind == VPX_CODEC_CX_FRAME_PKT) {
  107. const int keyframe = (pkt->data.frame.flags & VPX_FRAME_IS_KEY) != 0;
  108. if (!(pkt->data.frame.flags & VPX_FRAME_IS_FRAGMENT)) {
  109. *frame_out += 1;
  110. }
  111. if (!vpx_video_writer_write_frame(writer, pkt->data.frame.buf,
  112. pkt->data.frame.sz,
  113. pkt->data.frame.pts)) {
  114. die_codec(ecodec, "Failed to write compressed frame");
  115. }
  116. printf(keyframe ? "K" : ".");
  117. fflush(stdout);
  118. got_data = 1;
  119. // Decode 1 frame.
  120. if (test_decode) {
  121. if (vpx_codec_decode(dcodec, pkt->data.frame.buf,
  122. (unsigned int)pkt->data.frame.sz, NULL, 0))
  123. die_codec(dcodec, "Failed to decode frame.");
  124. }
  125. }
  126. }
  127. // Mismatch checking
  128. if (got_data && test_decode) {
  129. testing_decode(ecodec, dcodec, *frame_out, mismatch_seen);
  130. }
  131. return got_pkts;
  132. }
  133. int main(int argc, char **argv) {
  134. FILE *infile = NULL;
  135. // Encoder
  136. vpx_codec_ctx_t ecodec;
  137. vpx_codec_enc_cfg_t cfg;
  138. unsigned int frame_in = 0;
  139. vpx_image_t raw;
  140. vpx_codec_err_t res;
  141. VpxVideoInfo info;
  142. VpxVideoWriter *writer = NULL;
  143. const VpxInterface *encoder = NULL;
  144. // Test encoder/decoder mismatch.
  145. int test_decode = 1;
  146. // Decoder
  147. vpx_codec_ctx_t dcodec;
  148. unsigned int frame_out = 0;
  149. // The frame number to set reference frame on
  150. unsigned int update_frame_num = 0;
  151. int mismatch_seen = 0;
  152. const int fps = 30;
  153. const int bitrate = 500;
  154. const char *width_arg = NULL;
  155. const char *height_arg = NULL;
  156. const char *infile_arg = NULL;
  157. const char *outfile_arg = NULL;
  158. const char *update_frame_num_arg = NULL;
  159. unsigned int limit = 0;
  160. vp9_zero(ecodec);
  161. vp9_zero(cfg);
  162. vp9_zero(info);
  163. exec_name = argv[0];
  164. if (argc < 6) die("Invalid number of arguments");
  165. width_arg = argv[1];
  166. height_arg = argv[2];
  167. infile_arg = argv[3];
  168. outfile_arg = argv[4];
  169. update_frame_num_arg = argv[5];
  170. encoder = get_vpx_encoder_by_name("vp9");
  171. if (!encoder) die("Unsupported codec.");
  172. update_frame_num = (unsigned int)strtoul(update_frame_num_arg, NULL, 0);
  173. // In VP9, the reference buffers (cm->buffer_pool->frame_bufs[i].buf) are
  174. // allocated while calling vpx_codec_encode(), thus, setting reference for
  175. // 1st frame isn't supported.
  176. if (update_frame_num <= 1) {
  177. die("Couldn't parse frame number '%s'\n", update_frame_num_arg);
  178. }
  179. if (argc > 6) {
  180. limit = (unsigned int)strtoul(argv[6], NULL, 0);
  181. if (update_frame_num > limit)
  182. die("Update frame number couldn't larger than limit\n");
  183. }
  184. info.codec_fourcc = encoder->fourcc;
  185. info.frame_width = (int)strtol(width_arg, NULL, 0);
  186. info.frame_height = (int)strtol(height_arg, NULL, 0);
  187. info.time_base.numerator = 1;
  188. info.time_base.denominator = fps;
  189. if (info.frame_width <= 0 || info.frame_height <= 0 ||
  190. (info.frame_width % 2) != 0 || (info.frame_height % 2) != 0) {
  191. die("Invalid frame size: %dx%d", info.frame_width, info.frame_height);
  192. }
  193. if (!vpx_img_alloc(&raw, VPX_IMG_FMT_I420, info.frame_width,
  194. info.frame_height, 1)) {
  195. die("Failed to allocate image.");
  196. }
  197. printf("Using %s\n", vpx_codec_iface_name(encoder->codec_interface()));
  198. res = vpx_codec_enc_config_default(encoder->codec_interface(), &cfg, 0);
  199. if (res) die_codec(&ecodec, "Failed to get default codec config.");
  200. cfg.g_w = info.frame_width;
  201. cfg.g_h = info.frame_height;
  202. cfg.g_timebase.num = info.time_base.numerator;
  203. cfg.g_timebase.den = info.time_base.denominator;
  204. cfg.rc_target_bitrate = bitrate;
  205. cfg.g_lag_in_frames = 3;
  206. writer = vpx_video_writer_open(outfile_arg, kContainerIVF, &info);
  207. if (!writer) die("Failed to open %s for writing.", outfile_arg);
  208. if (!(infile = fopen(infile_arg, "rb")))
  209. die("Failed to open %s for reading.", infile_arg);
  210. if (vpx_codec_enc_init(&ecodec, encoder->codec_interface(), &cfg, 0))
  211. die_codec(&ecodec, "Failed to initialize encoder");
  212. // Disable alt_ref.
  213. if (vpx_codec_control(&ecodec, VP8E_SET_ENABLEAUTOALTREF, 0))
  214. die_codec(&ecodec, "Failed to set enable auto alt ref");
  215. if (test_decode) {
  216. const VpxInterface *decoder = get_vpx_decoder_by_name("vp9");
  217. if (vpx_codec_dec_init(&dcodec, decoder->codec_interface(), NULL, 0))
  218. die_codec(&dcodec, "Failed to initialize decoder.");
  219. }
  220. // Encode frames.
  221. while (vpx_img_read(&raw, infile)) {
  222. if (limit && frame_in >= limit) break;
  223. if (update_frame_num > 1 && frame_out + 1 == update_frame_num) {
  224. vpx_ref_frame_t ref;
  225. ref.frame_type = VP8_LAST_FRAME;
  226. ref.img = raw;
  227. // Set reference frame in encoder.
  228. if (vpx_codec_control(&ecodec, VP8_SET_REFERENCE, &ref))
  229. die_codec(&ecodec, "Failed to set reference frame");
  230. printf(" <SET_REF>");
  231. // If set_reference in decoder is commented out, the enc/dec mismatch
  232. // would be seen.
  233. if (test_decode) {
  234. if (vpx_codec_control(&dcodec, VP8_SET_REFERENCE, &ref))
  235. die_codec(&dcodec, "Failed to set reference frame");
  236. }
  237. }
  238. encode_frame(&ecodec, &raw, frame_in, writer, test_decode, &dcodec,
  239. &frame_out, &mismatch_seen);
  240. frame_in++;
  241. if (mismatch_seen) break;
  242. }
  243. // Flush encoder.
  244. if (!mismatch_seen)
  245. while (encode_frame(&ecodec, NULL, frame_in, writer, test_decode, &dcodec,
  246. &frame_out, &mismatch_seen)) {
  247. }
  248. printf("\n");
  249. fclose(infile);
  250. printf("Processed %d frames.\n", frame_out);
  251. if (test_decode) {
  252. if (!mismatch_seen)
  253. printf("Encoder/decoder results are matching.\n");
  254. else
  255. printf("Encoder/decoder results are NOT matching.\n");
  256. }
  257. if (test_decode)
  258. if (vpx_codec_destroy(&dcodec))
  259. die_codec(&dcodec, "Failed to destroy decoder");
  260. vpx_img_free(&raw);
  261. if (vpx_codec_destroy(&ecodec))
  262. die_codec(&ecodec, "Failed to destroy encoder.");
  263. vpx_video_writer_close(writer);
  264. return EXIT_SUCCESS;
  265. }