vp8cx_set_ref.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * Copyright (c) 2010 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. // VP8 Set Reference Frame
  11. // =======================
  12. //
  13. // This is an example demonstrating how to overwrite the VP8 encoder's
  14. // internal reference frame. In the sample we set the last frame to the
  15. // current frame. If this is done at a cut scene it will avoid a keyframe.
  16. // This technique could be used to bounce between two cameras.
  17. //
  18. // Note that the decoder would also have to set the reference frame to the
  19. // same value on the same frame, or the video will become corrupt.
  20. //
  21. // Usage
  22. // -----
  23. // This example adds a single argument to the `simple_encoder` example,
  24. // which specifies the frame number to update the reference frame on.
  25. // The parameter is parsed as follows:
  26. //
  27. //
  28. // Extra Variables
  29. // ---------------
  30. // This example maintains the frame number passed on the command line
  31. // in the `update_frame_num` variable.
  32. //
  33. //
  34. // Configuration
  35. // -------------
  36. //
  37. // The reference frame is updated on the frame specified on the command
  38. // line.
  39. //
  40. // Observing The Effects
  41. // ---------------------
  42. // Use the `simple_encoder` example to encode a sample with a cut scene.
  43. // Determine the frame number of the cut scene by looking for a generated
  44. // key-frame (indicated by a 'K'). Supply that frame number as an argument
  45. // to this example, and observe that no key-frame is generated.
  46. #include <stdio.h>
  47. #include <stdlib.h>
  48. #include <string.h>
  49. #include "vpx/vp8cx.h"
  50. #include "vpx/vpx_encoder.h"
  51. #include "vp8/common/common.h"
  52. #include "../tools_common.h"
  53. #include "../video_writer.h"
  54. static const char *exec_name;
  55. void usage_exit(void) {
  56. fprintf(stderr, "Usage: %s <width> <height> <infile> <outfile> <frame>\n",
  57. exec_name);
  58. exit(EXIT_FAILURE);
  59. }
  60. static int encode_frame(vpx_codec_ctx_t *codec, vpx_image_t *img,
  61. int frame_index, VpxVideoWriter *writer) {
  62. int got_pkts = 0;
  63. vpx_codec_iter_t iter = NULL;
  64. const vpx_codec_cx_pkt_t *pkt = NULL;
  65. const vpx_codec_err_t res =
  66. vpx_codec_encode(codec, img, frame_index, 1, 0, VPX_DL_GOOD_QUALITY);
  67. if (res != VPX_CODEC_OK) die_codec(codec, "Failed to encode frame");
  68. while ((pkt = vpx_codec_get_cx_data(codec, &iter)) != NULL) {
  69. got_pkts = 1;
  70. if (pkt->kind == VPX_CODEC_CX_FRAME_PKT) {
  71. const int keyframe = (pkt->data.frame.flags & VPX_FRAME_IS_KEY) != 0;
  72. if (!vpx_video_writer_write_frame(writer, pkt->data.frame.buf,
  73. pkt->data.frame.sz,
  74. pkt->data.frame.pts)) {
  75. die_codec(codec, "Failed to write compressed frame");
  76. }
  77. printf(keyframe ? "K" : ".");
  78. fflush(stdout);
  79. }
  80. }
  81. return got_pkts;
  82. }
  83. int main(int argc, char **argv) {
  84. FILE *infile = NULL;
  85. vpx_codec_ctx_t codec;
  86. vpx_codec_enc_cfg_t cfg;
  87. int frame_count = 0;
  88. vpx_image_t raw;
  89. vpx_codec_err_t res;
  90. VpxVideoInfo info;
  91. VpxVideoWriter *writer = NULL;
  92. const VpxInterface *encoder = NULL;
  93. int update_frame_num = 0;
  94. const int fps = 30; // TODO(dkovalev) add command line argument
  95. const int bitrate = 200; // kbit/s TODO(dkovalev) add command line argument
  96. vp8_zero(codec);
  97. vp8_zero(cfg);
  98. vp8_zero(info);
  99. exec_name = argv[0];
  100. if (argc != 6) die("Invalid number of arguments");
  101. // TODO(dkovalev): add vp9 support and rename the file accordingly
  102. encoder = get_vpx_encoder_by_name("vp8");
  103. if (!encoder) die("Unsupported codec.");
  104. update_frame_num = atoi(argv[5]);
  105. if (!update_frame_num) die("Couldn't parse frame number '%s'\n", argv[5]);
  106. info.codec_fourcc = encoder->fourcc;
  107. info.frame_width = (int)strtol(argv[1], NULL, 0);
  108. info.frame_height = (int)strtol(argv[2], NULL, 0);
  109. info.time_base.numerator = 1;
  110. info.time_base.denominator = fps;
  111. if (info.frame_width <= 0 || info.frame_height <= 0 ||
  112. (info.frame_width % 2) != 0 || (info.frame_height % 2) != 0) {
  113. die("Invalid frame size: %dx%d", info.frame_width, info.frame_height);
  114. }
  115. if (!vpx_img_alloc(&raw, VPX_IMG_FMT_I420, info.frame_width,
  116. info.frame_height, 1)) {
  117. die("Failed to allocate image.");
  118. }
  119. printf("Using %s\n", vpx_codec_iface_name(encoder->codec_interface()));
  120. res = vpx_codec_enc_config_default(encoder->codec_interface(), &cfg, 0);
  121. if (res) die_codec(&codec, "Failed to get default codec config.");
  122. cfg.g_w = info.frame_width;
  123. cfg.g_h = info.frame_height;
  124. cfg.g_timebase.num = info.time_base.numerator;
  125. cfg.g_timebase.den = info.time_base.denominator;
  126. cfg.rc_target_bitrate = bitrate;
  127. writer = vpx_video_writer_open(argv[4], kContainerIVF, &info);
  128. if (!writer) die("Failed to open %s for writing.", argv[4]);
  129. if (!(infile = fopen(argv[3], "rb")))
  130. die("Failed to open %s for reading.", argv[3]);
  131. if (vpx_codec_enc_init(&codec, encoder->codec_interface(), &cfg, 0))
  132. die_codec(&codec, "Failed to initialize encoder");
  133. // Encode frames.
  134. while (vpx_img_read(&raw, infile)) {
  135. if (frame_count + 1 == update_frame_num) {
  136. vpx_ref_frame_t ref;
  137. ref.frame_type = VP8_LAST_FRAME;
  138. ref.img = raw;
  139. if (vpx_codec_control(&codec, VP8_SET_REFERENCE, &ref))
  140. die_codec(&codec, "Failed to set reference frame");
  141. }
  142. encode_frame(&codec, &raw, frame_count++, writer);
  143. }
  144. // Flush encoder.
  145. while (encode_frame(&codec, NULL, -1, writer)) {
  146. }
  147. printf("\n");
  148. fclose(infile);
  149. printf("Processed %d frames.\n", frame_count);
  150. vpx_img_free(&raw);
  151. if (vpx_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec.");
  152. vpx_video_writer_close(writer);
  153. return EXIT_SUCCESS;
  154. }