set_maps

00001 /*
00002  *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
00003  *
00004  *  Use of this source code is governed by a BSD-style license
00005  *  that can be found in the LICENSE file in the root of the source
00006  *  tree. An additional intellectual property rights grant can be found
00007  *  in the file PATENTS.  All contributing project authors may
00008  *  be found in the AUTHORS file in the root of the source tree.
00009  */
00010 
00011 // VP8 Set Active and ROI Maps
00012 // ===========================
00013 //
00014 // This is an example demonstrating how to control the VP8 encoder's
00015 // ROI and Active maps.
00016 //
00017 // ROI (Reigon of Interest) maps are a way for the application to assign
00018 // each macroblock in the image to a region, and then set quantizer and
00019 // filtering parameters on that image.
00020 //
00021 // Active maps are a way for the application to specify on a
00022 // macroblock-by-macroblock basis whether there is any activity in that
00023 // macroblock.
00024 //
00025 //
00026 // Configuration
00027 // -------------
00028 // An ROI map is set on frame 22. If the width of the image in macroblocks
00029 // is evenly divisble by 4, then the output will appear to have distinct
00030 // columns, where the quantizer, loopfilter, and static threshold differ
00031 // from column to column.
00032 //
00033 // An active map is set on frame 33. If the width of the image in macroblocks
00034 // is evenly divisble by 4, then the output will appear to have distinct
00035 // columns, where one column will have motion and the next will not.
00036 //
00037 // The active map is cleared on frame 44.
00038 //
00039 // Observing The Effects
00040 // ---------------------
00041 // Use the `simple_decoder` example to decode this sample, and observe
00042 // the change in the image at frames 22, 33, and 44.
00043 
00044 #include <assert.h>
00045 #include <stdio.h>
00046 #include <stdlib.h>
00047 #include <string.h>
00048 
00049 #include "vpx/vp8cx.h"
00050 #include "vpx/vpx_encoder.h"
00051 
00052 #include "../tools_common.h"
00053 #include "../video_writer.h"
00054 
00055 static const char *exec_name;
00056 
00057 void usage_exit(void) {
00058   fprintf(stderr, "Usage: %s <codec> <width> <height> <infile> <outfile>\n",
00059           exec_name);
00060   exit(EXIT_FAILURE);
00061 }
00062 
00063 static void set_roi_map(const vpx_codec_enc_cfg_t *cfg,
00064                         vpx_codec_ctx_t *codec) {
00065   unsigned int i;
00066   vpx_roi_map_t roi;
00067   memset(&roi, 0, sizeof(roi));
00068 
00069   roi.rows = (cfg->g_h + 15) / 16;
00070   roi.cols = (cfg->g_w + 15) / 16;
00071 
00072   roi.delta_q[0] = 0;
00073   roi.delta_q[1] = -2;
00074   roi.delta_q[2] = -4;
00075   roi.delta_q[3] = -6;
00076 
00077   roi.delta_lf[0] = 0;
00078   roi.delta_lf[1] = 1;
00079   roi.delta_lf[2] = 2;
00080   roi.delta_lf[3] = 3;
00081 
00082   roi.static_threshold[0] = 1500;
00083   roi.static_threshold[1] = 1000;
00084   roi.static_threshold[2] = 500;
00085   roi.static_threshold[3] = 0;
00086 
00087   roi.roi_map = (uint8_t *)malloc(roi.rows * roi.cols);
00088   for (i = 0; i < roi.rows * roi.cols; ++i) roi.roi_map[i] = i % 4;
00089 
00090   if (vpx_codec_control(codec, VP8E_SET_ROI_MAP, &roi))
00091     die_codec(codec, "Failed to set ROI map");
00092 
00093   free(roi.roi_map);
00094 }
00095 
00096 static void set_active_map(const vpx_codec_enc_cfg_t *cfg,
00097                            vpx_codec_ctx_t *codec) {
00098   unsigned int i;
00099   vpx_active_map_t map = { 0, 0, 0 };
00100 
00101   map.rows = (cfg->g_h + 15) / 16;
00102   map.cols = (cfg->g_w + 15) / 16;
00103 
00104   map.active_map = (uint8_t *)malloc(map.rows * map.cols);
00105   for (i = 0; i < map.rows * map.cols; ++i) map.active_map[i] = i % 2;
00106 
00107   if (vpx_codec_control(codec, VP8E_SET_ACTIVEMAP, &map))
00108     die_codec(codec, "Failed to set active map");
00109 
00110   free(map.active_map);
00111 }
00112 
00113 static void unset_active_map(const vpx_codec_enc_cfg_t *cfg,
00114                              vpx_codec_ctx_t *codec) {
00115   vpx_active_map_t map = { 0, 0, 0 };
00116 
00117   map.rows = (cfg->g_h + 15) / 16;
00118   map.cols = (cfg->g_w + 15) / 16;
00119   map.active_map = NULL;
00120 
00121   if (vpx_codec_control(codec, VP8E_SET_ACTIVEMAP, &map))
00122     die_codec(codec, "Failed to set active map");
00123 }
00124 
00125 static int encode_frame(vpx_codec_ctx_t *codec, vpx_image_t *img,
00126                         int frame_index, VpxVideoWriter *writer) {
00127   int got_pkts = 0;
00128   vpx_codec_iter_t iter = NULL;
00129   const vpx_codec_cx_pkt_t *pkt = NULL;
00130   const vpx_codec_err_t res =
00131       vpx_codec_encode(codec, img, frame_index, 1, 0, VPX_DL_GOOD_QUALITY);
00132   if (res != VPX_CODEC_OK) die_codec(codec, "Failed to encode frame");
00133 
00134   while ((pkt = vpx_codec_get_cx_data(codec, &iter)) != NULL) {
00135     got_pkts = 1;
00136 
00137     if (pkt->kind == VPX_CODEC_CX_FRAME_PKT) {
00138       const int keyframe = (pkt->data.frame.flags & VPX_FRAME_IS_KEY) != 0;
00139       if (!vpx_video_writer_write_frame(writer, pkt->data.frame.buf,
00140                                         pkt->data.frame.sz,
00141                                         pkt->data.frame.pts)) {
00142         die_codec(codec, "Failed to write compressed frame");
00143       }
00144 
00145       printf(keyframe ? "K" : ".");
00146       fflush(stdout);
00147     }
00148   }
00149 
00150   return got_pkts;
00151 }
00152 
00153 int main(int argc, char **argv) {
00154   FILE *infile = NULL;
00155   vpx_codec_ctx_t codec;
00156   vpx_codec_enc_cfg_t cfg;
00157   int frame_count = 0;
00158   vpx_image_t raw;
00159   vpx_codec_err_t res;
00160   VpxVideoInfo info;
00161   VpxVideoWriter *writer = NULL;
00162   const VpxInterface *encoder = NULL;
00163   const int fps = 2;  // TODO(dkovalev) add command line argument
00164   const double bits_per_pixel_per_frame = 0.067;
00165 
00166   exec_name = argv[0];
00167   if (argc != 6) die("Invalid number of arguments");
00168 
00169   memset(&info, 0, sizeof(info));
00170 
00171   encoder = get_vpx_encoder_by_name(argv[1]);
00172   if (encoder == NULL) {
00173     die("Unsupported codec.");
00174   }
00175   assert(encoder != NULL);
00176   info.codec_fourcc = encoder->fourcc;
00177   info.frame_width = (int)strtol(argv[2], NULL, 0);
00178   info.frame_height = (int)strtol(argv[3], NULL, 0);
00179   info.time_base.numerator = 1;
00180   info.time_base.denominator = fps;
00181 
00182   if (info.frame_width <= 0 || info.frame_height <= 0 ||
00183       (info.frame_width % 2) != 0 || (info.frame_height % 2) != 0) {
00184     die("Invalid frame size: %dx%d", info.frame_width, info.frame_height);
00185   }
00186 
00187   if (!vpx_img_alloc(&raw, VPX_IMG_FMT_I420, info.frame_width,
00188                      info.frame_height, 1)) {
00189     die("Failed to allocate image.");
00190   }
00191 
00192   printf("Using %s\n", vpx_codec_iface_name(encoder->codec_interface()));
00193 
00194   res = vpx_codec_enc_config_default(encoder->codec_interface(), &cfg, 0);
00195   if (res) die_codec(&codec, "Failed to get default codec config.");
00196 
00197   cfg.g_w = info.frame_width;
00198   cfg.g_h = info.frame_height;
00199   cfg.g_timebase.num = info.time_base.numerator;
00200   cfg.g_timebase.den = info.time_base.denominator;
00201   cfg.rc_target_bitrate =
00202       (unsigned int)(bits_per_pixel_per_frame * cfg.g_w * cfg.g_h * fps / 1000);
00203   cfg.g_lag_in_frames = 0;
00204 
00205   writer = vpx_video_writer_open(argv[5], kContainerIVF, &info);
00206   if (!writer) die("Failed to open %s for writing.", argv[5]);
00207 
00208   if (!(infile = fopen(argv[4], "rb")))
00209     die("Failed to open %s for reading.", argv[4]);
00210 
00211   if (vpx_codec_enc_init(&codec, encoder->codec_interface(), &cfg, 0))
00212     die_codec(&codec, "Failed to initialize encoder");
00213 
00214   // Encode frames.
00215   while (vpx_img_read(&raw, infile)) {
00216     ++frame_count;
00217 
00218     if (frame_count == 22 && encoder->fourcc == VP8_FOURCC) {
00219       set_roi_map(&cfg, &codec);
00220     } else if (frame_count == 33) {
00221       set_active_map(&cfg, &codec);
00222     } else if (frame_count == 44) {
00223       unset_active_map(&cfg, &codec);
00224     }
00225 
00226     encode_frame(&codec, &raw, frame_count, writer);
00227   }
00228 
00229   // Flush encoder.
00230   while (encode_frame(&codec, NULL, -1, writer)) {
00231   }
00232 
00233   printf("\n");
00234   fclose(infile);
00235   printf("Processed %d frames.\n", frame_count);
00236 
00237   vpx_img_free(&raw);
00238   if (vpx_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec.");
00239 
00240   vpx_video_writer_close(writer);
00241 
00242   return EXIT_SUCCESS;
00243 }

Generated on 16 Jun 2017 for WebM Codec SDK by  doxygen 1.6.1