set_maps.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 Active and ROI Maps
  11. // ===========================
  12. //
  13. // This is an example demonstrating how to control the VP8 encoder's
  14. // ROI and Active maps.
  15. //
  16. // ROI (Reigon of Interest) maps are a way for the application to assign
  17. // each macroblock in the image to a region, and then set quantizer and
  18. // filtering parameters on that image.
  19. //
  20. // Active maps are a way for the application to specify on a
  21. // macroblock-by-macroblock basis whether there is any activity in that
  22. // macroblock.
  23. //
  24. //
  25. // Configuration
  26. // -------------
  27. // An ROI map is set on frame 22. If the width of the image in macroblocks
  28. // is evenly divisble by 4, then the output will appear to have distinct
  29. // columns, where the quantizer, loopfilter, and static threshold differ
  30. // from column to column.
  31. //
  32. // An active map is set on frame 33. If the width of the image in macroblocks
  33. // is evenly divisble by 4, then the output will appear to have distinct
  34. // columns, where one column will have motion and the next will not.
  35. //
  36. // The active map is cleared on frame 44.
  37. //
  38. // Observing The Effects
  39. // ---------------------
  40. // Use the `simple_decoder` example to decode this sample, and observe
  41. // the change in the image at frames 22, 33, and 44.
  42. #include <assert.h>
  43. #include <stdio.h>
  44. #include <stdlib.h>
  45. #include <string.h>
  46. #include "vpx/vp8cx.h"
  47. #include "vpx/vpx_encoder.h"
  48. #include "../tools_common.h"
  49. #include "../video_writer.h"
  50. static const char *exec_name;
  51. void usage_exit(void) {
  52. fprintf(stderr, "Usage: %s <codec> <width> <height> <infile> <outfile>\n",
  53. exec_name);
  54. exit(EXIT_FAILURE);
  55. }
  56. static void set_roi_map(const vpx_codec_enc_cfg_t *cfg,
  57. vpx_codec_ctx_t *codec) {
  58. unsigned int i;
  59. vpx_roi_map_t roi;
  60. memset(&roi, 0, sizeof(roi));
  61. roi.rows = (cfg->g_h + 15) / 16;
  62. roi.cols = (cfg->g_w + 15) / 16;
  63. roi.delta_q[0] = 0;
  64. roi.delta_q[1] = -2;
  65. roi.delta_q[2] = -4;
  66. roi.delta_q[3] = -6;
  67. roi.delta_lf[0] = 0;
  68. roi.delta_lf[1] = 1;
  69. roi.delta_lf[2] = 2;
  70. roi.delta_lf[3] = 3;
  71. roi.static_threshold[0] = 1500;
  72. roi.static_threshold[1] = 1000;
  73. roi.static_threshold[2] = 500;
  74. roi.static_threshold[3] = 0;
  75. roi.roi_map = (uint8_t *)malloc(roi.rows * roi.cols);
  76. for (i = 0; i < roi.rows * roi.cols; ++i) roi.roi_map[i] = i % 4;
  77. if (vpx_codec_control(codec, VP8E_SET_ROI_MAP, &roi))
  78. die_codec(codec, "Failed to set ROI map");
  79. free(roi.roi_map);
  80. }
  81. static void set_active_map(const vpx_codec_enc_cfg_t *cfg,
  82. vpx_codec_ctx_t *codec) {
  83. unsigned int i;
  84. vpx_active_map_t map = { 0, 0, 0 };
  85. map.rows = (cfg->g_h + 15) / 16;
  86. map.cols = (cfg->g_w + 15) / 16;
  87. map.active_map = (uint8_t *)malloc(map.rows * map.cols);
  88. for (i = 0; i < map.rows * map.cols; ++i) map.active_map[i] = i % 2;
  89. if (vpx_codec_control(codec, VP8E_SET_ACTIVEMAP, &map))
  90. die_codec(codec, "Failed to set active map");
  91. free(map.active_map);
  92. }
  93. static void unset_active_map(const vpx_codec_enc_cfg_t *cfg,
  94. vpx_codec_ctx_t *codec) {
  95. vpx_active_map_t map = { 0, 0, 0 };
  96. map.rows = (cfg->g_h + 15) / 16;
  97. map.cols = (cfg->g_w + 15) / 16;
  98. map.active_map = NULL;
  99. if (vpx_codec_control(codec, VP8E_SET_ACTIVEMAP, &map))
  100. die_codec(codec, "Failed to set active map");
  101. }
  102. static int encode_frame(vpx_codec_ctx_t *codec, vpx_image_t *img,
  103. int frame_index, VpxVideoWriter *writer) {
  104. int got_pkts = 0;
  105. vpx_codec_iter_t iter = NULL;
  106. const vpx_codec_cx_pkt_t *pkt = NULL;
  107. const vpx_codec_err_t res =
  108. vpx_codec_encode(codec, img, frame_index, 1, 0, VPX_DL_GOOD_QUALITY);
  109. if (res != VPX_CODEC_OK) die_codec(codec, "Failed to encode frame");
  110. while ((pkt = vpx_codec_get_cx_data(codec, &iter)) != NULL) {
  111. got_pkts = 1;
  112. if (pkt->kind == VPX_CODEC_CX_FRAME_PKT) {
  113. const int keyframe = (pkt->data.frame.flags & VPX_FRAME_IS_KEY) != 0;
  114. if (!vpx_video_writer_write_frame(writer, pkt->data.frame.buf,
  115. pkt->data.frame.sz,
  116. pkt->data.frame.pts)) {
  117. die_codec(codec, "Failed to write compressed frame");
  118. }
  119. printf(keyframe ? "K" : ".");
  120. fflush(stdout);
  121. }
  122. }
  123. return got_pkts;
  124. }
  125. int main(int argc, char **argv) {
  126. FILE *infile = NULL;
  127. vpx_codec_ctx_t codec;
  128. vpx_codec_enc_cfg_t cfg;
  129. int frame_count = 0;
  130. vpx_image_t raw;
  131. vpx_codec_err_t res;
  132. VpxVideoInfo info;
  133. VpxVideoWriter *writer = NULL;
  134. const VpxInterface *encoder = NULL;
  135. const int fps = 2; // TODO(dkovalev) add command line argument
  136. const double bits_per_pixel_per_frame = 0.067;
  137. exec_name = argv[0];
  138. if (argc != 6) die("Invalid number of arguments");
  139. memset(&info, 0, sizeof(info));
  140. encoder = get_vpx_encoder_by_name(argv[1]);
  141. if (encoder == NULL) {
  142. die("Unsupported codec.");
  143. }
  144. assert(encoder != NULL);
  145. info.codec_fourcc = encoder->fourcc;
  146. info.frame_width = (int)strtol(argv[2], NULL, 0);
  147. info.frame_height = (int)strtol(argv[3], NULL, 0);
  148. info.time_base.numerator = 1;
  149. info.time_base.denominator = fps;
  150. if (info.frame_width <= 0 || info.frame_height <= 0 ||
  151. (info.frame_width % 2) != 0 || (info.frame_height % 2) != 0) {
  152. die("Invalid frame size: %dx%d", info.frame_width, info.frame_height);
  153. }
  154. if (!vpx_img_alloc(&raw, VPX_IMG_FMT_I420, info.frame_width,
  155. info.frame_height, 1)) {
  156. die("Failed to allocate image.");
  157. }
  158. printf("Using %s\n", vpx_codec_iface_name(encoder->codec_interface()));
  159. res = vpx_codec_enc_config_default(encoder->codec_interface(), &cfg, 0);
  160. if (res) die_codec(&codec, "Failed to get default codec config.");
  161. cfg.g_w = info.frame_width;
  162. cfg.g_h = info.frame_height;
  163. cfg.g_timebase.num = info.time_base.numerator;
  164. cfg.g_timebase.den = info.time_base.denominator;
  165. cfg.rc_target_bitrate =
  166. (unsigned int)(bits_per_pixel_per_frame * cfg.g_w * cfg.g_h * fps / 1000);
  167. cfg.g_lag_in_frames = 0;
  168. writer = vpx_video_writer_open(argv[5], kContainerIVF, &info);
  169. if (!writer) die("Failed to open %s for writing.", argv[5]);
  170. if (!(infile = fopen(argv[4], "rb")))
  171. die("Failed to open %s for reading.", argv[4]);
  172. if (vpx_codec_enc_init(&codec, encoder->codec_interface(), &cfg, 0))
  173. die_codec(&codec, "Failed to initialize encoder");
  174. // Encode frames.
  175. while (vpx_img_read(&raw, infile)) {
  176. ++frame_count;
  177. if (frame_count == 22 && encoder->fourcc == VP8_FOURCC) {
  178. set_roi_map(&cfg, &codec);
  179. } else if (frame_count == 33) {
  180. set_active_map(&cfg, &codec);
  181. } else if (frame_count == 44) {
  182. unset_active_map(&cfg, &codec);
  183. }
  184. encode_frame(&codec, &raw, frame_count, writer);
  185. }
  186. // Flush encoder.
  187. while (encode_frame(&codec, NULL, -1, writer)) {
  188. }
  189. printf("\n");
  190. fclose(infile);
  191. printf("Processed %d frames.\n", frame_count);
  192. vpx_img_free(&raw);
  193. if (vpx_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec.");
  194. vpx_video_writer_close(writer);
  195. return EXIT_SUCCESS;
  196. }