00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
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;
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
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
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 }