00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include <stdarg.h>
00018 #include <stdlib.h>
00019 #include <string.h>
00020 #include <time.h>
00021 #include "./args.h"
00022 #include "vpx/svc_context.h"
00023 #include "vpx/vp8cx.h"
00024 #include "vpx/vpx_encoder.h"
00025
00026 #define VP90_FOURCC 0x30395056
00027
00028 static const struct arg_enum_list encoding_mode_enum[] = {
00029 {"i", INTER_LAYER_PREDICTION_I},
00030 {"alt-ip", ALT_INTER_LAYER_PREDICTION_IP},
00031 {"ip", INTER_LAYER_PREDICTION_IP},
00032 {"gf", USE_GOLDEN_FRAME},
00033 {NULL, 0}
00034 };
00035
00036 static const arg_def_t encoding_mode_arg = ARG_DEF_ENUM(
00037 "m", "encoding-mode", 1, "Encoding mode algorithm", encoding_mode_enum);
00038 static const arg_def_t skip_frames_arg =
00039 ARG_DEF("s", "skip-frames", 1, "input frames to skip");
00040 static const arg_def_t frames_arg =
00041 ARG_DEF("f", "frames", 1, "number of frames to encode");
00042 static const arg_def_t width_arg = ARG_DEF("w", "width", 1, "source width");
00043 static const arg_def_t height_arg = ARG_DEF("h", "height", 1, "source height");
00044 static const arg_def_t timebase_arg =
00045 ARG_DEF("t", "timebase", 1, "timebase (num/den)");
00046 static const arg_def_t bitrate_arg = ARG_DEF(
00047 "b", "target-bitrate", 1, "encoding bitrate, in kilobits per second");
00048 static const arg_def_t layers_arg =
00049 ARG_DEF("l", "layers", 1, "number of SVC layers");
00050 static const arg_def_t kf_dist_arg =
00051 ARG_DEF("k", "kf-dist", 1, "number of frames between keyframes");
00052 static const arg_def_t scale_factors_arg =
00053 ARG_DEF("r", "scale-factors", 1, "scale factors (lowest to highest layer)");
00054 static const arg_def_t quantizers_arg =
00055 ARG_DEF("q", "quantizers", 1, "quantizers (lowest to highest layer)");
00056 static const arg_def_t dummy_frame_arg =
00057 ARG_DEF("z", "dummy-frame", 1, "make first frame blank and full size");
00058
00059 static const arg_def_t *svc_args[] = {
00060 &encoding_mode_arg, &frames_arg, &width_arg, &height_arg,
00061 &timebase_arg, &bitrate_arg, &skip_frames_arg, &layers_arg,
00062 &kf_dist_arg, &scale_factors_arg, &quantizers_arg, &dummy_frame_arg,
00063 NULL
00064 };
00065
00066 static const SVC_ENCODING_MODE default_encoding_mode =
00067 INTER_LAYER_PREDICTION_IP;
00068 static const uint32_t default_frames_to_skip = 0;
00069 static const uint32_t default_frames_to_code = 60 * 60;
00070 static const uint32_t default_width = 1920;
00071 static const uint32_t default_height = 1080;
00072 static const uint32_t default_timebase_num = 1;
00073 static const uint32_t default_timebase_den = 60;
00074 static const uint32_t default_bitrate = 1000;
00075 static const uint32_t default_spatial_layers = 5;
00076 static const uint32_t default_kf_dist = 100;
00077 static const int default_use_dummy_frame = 1;
00078
00079 typedef struct {
00080 char *input_filename;
00081 char *output_filename;
00082 uint32_t frames_to_code;
00083 uint32_t frames_to_skip;
00084 } AppInput;
00085
00086 static void mem_put_le16(char *mem, uint32_t val) {
00087 mem[0] = val;
00088 mem[1] = val >> 8;
00089 }
00090
00091 static void mem_put_le32(char *mem, uint32_t val) {
00092 mem[0] = val;
00093 mem[1] = val >> 8;
00094 mem[2] = val >> 16;
00095 mem[3] = val >> 24;
00096 }
00097
00098 static void usage(const char *exec_name) {
00099 fprintf(stderr, "Usage: %s <options> input_filename output_filename\n",
00100 exec_name);
00101 fprintf(stderr, "Options:\n");
00102 arg_show_usage(stderr, svc_args);
00103 exit(EXIT_FAILURE);
00104 }
00105
00106 void die(const char *fmt, ...) {
00107 va_list ap;
00108
00109 va_start(ap, fmt);
00110 vfprintf(stderr, fmt, ap);
00111 if (fmt[strlen(fmt) - 1] != '\n') printf("\n");
00112 exit(EXIT_FAILURE);
00113 }
00114
00115 static void die_codec(vpx_codec_ctx_t *ctx, const char *s) {
00116 const char *detail = vpx_codec_error_detail(ctx);
00117
00118 printf("%s: %s\n", s, vpx_codec_error(ctx));
00119 if (detail) printf(" %s\n", detail);
00120 exit(EXIT_FAILURE);
00121 }
00122
00123 static int read_frame(FILE *f, vpx_image_t *img) {
00124 size_t nbytes;
00125 int res = 1;
00126 int plane;
00127
00128 for (plane = 0; plane < 3; ++plane) {
00129 uint8_t *ptr;
00130 const int w = (plane ? (1 + img->d_w) / 2 : img->d_w);
00131 const int h = (plane ? (1 + img->d_h) / 2 : img->d_h);
00132 int r;
00133
00134 switch (plane) {
00135 case 1:
00136 ptr = img->planes[VPX_PLANE_U];
00137 break;
00138 case 2:
00139 ptr = img->planes[VPX_PLANE_V];
00140 break;
00141 default:
00142 ptr = img->planes[plane];
00143 }
00144 for (r = 0; r < h; ++r) {
00145 const int to_read = w;
00146
00147 nbytes = fread(ptr, 1, to_read, f);
00148 if (nbytes != to_read) {
00149 res = 0;
00150 if (nbytes > 0)
00151 printf("Warning: Read partial frame. Check your width & height!\n");
00152 break;
00153 }
00154 ptr += img->stride[plane];
00155 }
00156 if (!res) break;
00157 }
00158 return res;
00159 }
00160
00161 static int create_dummy_frame(vpx_image_t *img) {
00162 const size_t buf_size = img->w * img->h * 3 / 2;
00163 memset(img->planes[0], 129, buf_size);
00164 return 1;
00165 }
00166
00167 static void write_ivf_file_header(FILE *outfile,
00168 uint32_t width, uint32_t height,
00169 int timebase_num, int timebase_den,
00170 int frame_cnt) {
00171 char header[32];
00172
00173 header[0] = 'D';
00174 header[1] = 'K';
00175 header[2] = 'I';
00176 header[3] = 'F';
00177 mem_put_le16(header + 4, 0);
00178 mem_put_le16(header + 6, 32);
00179 mem_put_le32(header + 8, VP90_FOURCC);
00180 mem_put_le16(header + 12, width);
00181 mem_put_le16(header + 14, height);
00182 mem_put_le32(header + 16, timebase_den);
00183 mem_put_le32(header + 20, timebase_num);
00184 mem_put_le32(header + 24, frame_cnt);
00185 mem_put_le32(header + 28, 0);
00186
00187 (void)fwrite(header, 1, 32, outfile);
00188 }
00189
00190 static void write_ivf_frame_header(FILE *outfile, vpx_codec_pts_t pts,
00191 size_t sz) {
00192 char header[12];
00193 mem_put_le32(header, (uint32_t)sz);
00194 mem_put_le32(header + 4, pts & 0xFFFFFFFF);
00195 mem_put_le32(header + 8, pts >> 32);
00196
00197 (void)fwrite(header, 1, 12, outfile);
00198 }
00199
00200 static void parse_command_line(int argc, const char **argv_,
00201 AppInput *app_input, SvcContext *svc_ctx,
00202 vpx_codec_enc_cfg_t *enc_cfg) {
00203 struct arg arg;
00204 char **argv, **argi, **argj;
00205 vpx_codec_err_t res;
00206
00207
00208 svc_ctx->log_level = SVC_LOG_DEBUG;
00209 svc_ctx->spatial_layers = default_spatial_layers;
00210 svc_ctx->encoding_mode = default_encoding_mode;
00211
00212 svc_ctx->first_frame_full_size = default_use_dummy_frame;
00213
00214
00215 res = vpx_codec_enc_config_default(vpx_codec_vp9_cx(), enc_cfg, 0);
00216 if (res) {
00217 die("Failed to get config: %s\n", vpx_codec_err_to_string(res));
00218 }
00219
00220 enc_cfg->g_w = default_width;
00221 enc_cfg->g_h = default_height;
00222 enc_cfg->g_timebase.num = default_timebase_num;
00223 enc_cfg->g_timebase.den = default_timebase_den;
00224 enc_cfg->rc_target_bitrate = default_bitrate;
00225 enc_cfg->kf_min_dist = default_kf_dist;
00226 enc_cfg->kf_max_dist = default_kf_dist;
00227
00228
00229 app_input->frames_to_code = default_frames_to_code;
00230 app_input->frames_to_skip = default_frames_to_skip;
00231
00232
00233 argv = argv_dup(argc - 1, argv_ + 1);
00234 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
00235 arg.argv_step = 1;
00236
00237 if (arg_match(&arg, &encoding_mode_arg, argi)) {
00238 svc_ctx->encoding_mode = arg_parse_enum_or_int(&arg);
00239 } else if (arg_match(&arg, &frames_arg, argi)) {
00240 app_input->frames_to_code = arg_parse_uint(&arg);
00241 } else if (arg_match(&arg, &width_arg, argi)) {
00242 enc_cfg->g_w = arg_parse_uint(&arg);
00243 } else if (arg_match(&arg, &height_arg, argi)) {
00244 enc_cfg->g_h = arg_parse_uint(&arg);
00245 } else if (arg_match(&arg, &height_arg, argi)) {
00246 enc_cfg->g_h = arg_parse_uint(&arg);
00247 } else if (arg_match(&arg, &timebase_arg, argi)) {
00248 enc_cfg->g_timebase = arg_parse_rational(&arg);
00249 } else if (arg_match(&arg, &bitrate_arg, argi)) {
00250 enc_cfg->rc_target_bitrate = arg_parse_uint(&arg);
00251 } else if (arg_match(&arg, &skip_frames_arg, argi)) {
00252 app_input->frames_to_skip = arg_parse_uint(&arg);
00253 } else if (arg_match(&arg, &layers_arg, argi)) {
00254 svc_ctx->spatial_layers = arg_parse_uint(&arg);
00255 } else if (arg_match(&arg, &kf_dist_arg, argi)) {
00256 enc_cfg->kf_min_dist = arg_parse_uint(&arg);
00257 enc_cfg->kf_max_dist = enc_cfg->kf_min_dist;
00258 } else if (arg_match(&arg, &scale_factors_arg, argi)) {
00259 vpx_svc_set_scale_factors(svc_ctx, arg.val);
00260 } else if (arg_match(&arg, &quantizers_arg, argi)) {
00261 vpx_svc_set_quantizers(svc_ctx, arg.val);
00262 } else if (arg_match(&arg, &dummy_frame_arg, argi)) {
00263 svc_ctx->first_frame_full_size = arg_parse_int(&arg);
00264 } else {
00265 ++argj;
00266 }
00267 }
00268
00269
00270 for (argi = argv; *argi; ++argi)
00271 if (argi[0][0] == '-' && strlen(argi[0]) > 1)
00272 die("Error: Unrecognized option %s\n", *argi);
00273
00274 if (argv[0] == NULL || argv[1] == 0) {
00275 usage(argv_[0]);
00276 }
00277 app_input->input_filename = argv[0];
00278 app_input->output_filename = argv[1];
00279 free(argv);
00280
00281 if (enc_cfg->g_w < 16 || enc_cfg->g_w % 2 || enc_cfg->g_h < 16 ||
00282 enc_cfg->g_h % 2)
00283 die("Invalid resolution: %d x %d\n", enc_cfg->g_w, enc_cfg->g_h);
00284
00285 printf(
00286 "Codec %s\nframes: %d, skip: %d\n"
00287 "mode: %d, layers: %d\n"
00288 "width %d, height: %d,\n"
00289 "num: %d, den: %d, bitrate: %d,\n"
00290 "gop size: %d, use_dummy_frame: %d\n",
00291 vpx_codec_iface_name(vpx_codec_vp9_cx()), app_input->frames_to_code,
00292 app_input->frames_to_skip, svc_ctx->encoding_mode,
00293 svc_ctx->spatial_layers, enc_cfg->g_w, enc_cfg->g_h,
00294 enc_cfg->g_timebase.num, enc_cfg->g_timebase.den,
00295 enc_cfg->rc_target_bitrate, enc_cfg->kf_max_dist,
00296 svc_ctx->first_frame_full_size);
00297 }
00298
00299 int main(int argc, const char **argv) {
00300 AppInput app_input = {0};
00301 FILE *infile, *outfile;
00302 vpx_codec_ctx_t codec;
00303 vpx_codec_enc_cfg_t enc_cfg;
00304 SvcContext svc_ctx;
00305 uint32_t i;
00306 uint32_t frame_cnt = 0;
00307 vpx_image_t raw;
00308 vpx_codec_err_t res;
00309 int pts = 0;
00310 int frame_duration = 1;
00311
00312 memset(&svc_ctx, 0, sizeof(svc_ctx));
00313 svc_ctx.log_print = 1;
00314 parse_command_line(argc, argv, &app_input, &svc_ctx, &enc_cfg);
00315
00316
00317 if (!vpx_img_alloc(&raw, VPX_IMG_FMT_I420, enc_cfg.g_w, enc_cfg.g_h, 32))
00318 die("Failed to allocate image %dx%d\n", enc_cfg.g_w, enc_cfg.g_h);
00319
00320 if (!(infile = fopen(app_input.input_filename, "rb")))
00321 die("Failed to open %s for reading\n", app_input.input_filename);
00322
00323 if (!(outfile = fopen(app_input.output_filename, "wb")))
00324 die("Failed to open %s for writing\n", app_input.output_filename);
00325
00326
00327 if (vpx_svc_init(&svc_ctx, &codec, vpx_codec_vp9_cx(), &enc_cfg) !=
00328 VPX_CODEC_OK)
00329 die("Failed to initialize encoder\n");
00330
00331 write_ivf_file_header(outfile, enc_cfg.g_w, enc_cfg.g_h,
00332 enc_cfg.g_timebase.num, enc_cfg.g_timebase.den, 0);
00333
00334
00335 for (i = 0; i < app_input.frames_to_skip; ++i) {
00336 read_frame(infile, &raw);
00337 }
00338
00339
00340 while (frame_cnt <= app_input.frames_to_code) {
00341 if (frame_cnt == 0 && svc_ctx.first_frame_full_size) {
00342 create_dummy_frame(&raw);
00343 } else {
00344 if (!read_frame(infile, &raw)) break;
00345 }
00346 res = vpx_svc_encode(&svc_ctx, &codec, &raw, pts, frame_duration,
00347 VPX_DL_REALTIME);
00348 printf("%s", vpx_svc_get_message(&svc_ctx));
00349 if (res != VPX_CODEC_OK) {
00350 die_codec(&codec, "Failed to encode frame");
00351 }
00352 if (vpx_svc_get_frame_size(&svc_ctx) > 0) {
00353 write_ivf_frame_header(outfile, pts, vpx_svc_get_frame_size(&svc_ctx));
00354 (void)fwrite(vpx_svc_get_buffer(&svc_ctx), 1,
00355 vpx_svc_get_frame_size(&svc_ctx), outfile);
00356 }
00357 ++frame_cnt;
00358 pts += frame_duration;
00359 }
00360
00361 printf("Processed %d frames\n", frame_cnt - svc_ctx.first_frame_full_size);
00362
00363 fclose(infile);
00364 if (vpx_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec");
00365
00366
00367 if (!fseek(outfile, 0, SEEK_SET)) {
00368 write_ivf_file_header(outfile, enc_cfg.g_w, enc_cfg.g_h,
00369 enc_cfg.g_timebase.num, enc_cfg.g_timebase.den,
00370 frame_cnt);
00371 }
00372 fclose(outfile);
00373 vpx_img_free(&raw);
00374
00375
00376 printf("%s", vpx_svc_dump_statistics(&svc_ctx));
00377
00378 vpx_svc_release(&svc_ctx);
00379
00380 return EXIT_SUCCESS;
00381 }