2 * Copyright (c) 2012 The WebM project authors. All Rights Reserved.
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.
12 * This is an example demonstrating how to implement a multi-layer
13 * VP9 encoding scheme based on spatial scalability for video applications
14 * that benefit from a scalable bitstream.
23 #include "./tools_common.h"
24 #include "./video_writer.h"
26 #include "vpx/svc_context.h"
27 #include "vpx/vp8cx.h"
28 #include "vpx/vpx_encoder.h"
29 #include "./vpxstats.h"
31 static const struct arg_enum_list encoding_mode_enum[] = {
32 {"i", INTER_LAYER_PREDICTION_I},
33 {"alt-ip", ALT_INTER_LAYER_PREDICTION_IP},
34 {"ip", INTER_LAYER_PREDICTION_IP},
35 {"gf", USE_GOLDEN_FRAME},
39 static const arg_def_t encoding_mode_arg = ARG_DEF_ENUM(
40 "m", "encoding-mode", 1, "Encoding mode algorithm", encoding_mode_enum);
41 static const arg_def_t skip_frames_arg =
42 ARG_DEF("s", "skip-frames", 1, "input frames to skip");
43 static const arg_def_t frames_arg =
44 ARG_DEF("f", "frames", 1, "number of frames to encode");
45 static const arg_def_t width_arg = ARG_DEF("w", "width", 1, "source width");
46 static const arg_def_t height_arg = ARG_DEF("h", "height", 1, "source height");
47 static const arg_def_t timebase_arg =
48 ARG_DEF("t", "timebase", 1, "timebase (num/den)");
49 static const arg_def_t bitrate_arg = ARG_DEF(
50 "b", "target-bitrate", 1, "encoding bitrate, in kilobits per second");
51 static const arg_def_t layers_arg =
52 ARG_DEF("l", "layers", 1, "number of SVC layers");
53 static const arg_def_t kf_dist_arg =
54 ARG_DEF("k", "kf-dist", 1, "number of frames between keyframes");
55 static const arg_def_t scale_factors_arg =
56 ARG_DEF("r", "scale-factors", 1, "scale factors (lowest to highest layer)");
57 static const arg_def_t quantizers_arg =
58 ARG_DEF("q", "quantizers", 1, "quantizers for non key frames, also will "
59 "be applied to key frames if -qn is not specified (lowest to "
61 static const arg_def_t quantizers_keyframe_arg =
62 ARG_DEF("qn", "quantizers-keyframe", 1, "quantizers for key frames (lowest "
64 static const arg_def_t passes_arg =
65 ARG_DEF("p", "passes", 1, "Number of passes (1/2)");
66 static const arg_def_t pass_arg =
67 ARG_DEF(NULL, "pass", 1, "Pass to execute (1/2)");
68 static const arg_def_t fpf_name_arg =
69 ARG_DEF(NULL, "fpf", 1, "First pass statistics file name");
70 static const arg_def_t min_q_arg =
71 ARG_DEF(NULL, "min-q", 1, "Minimum quantizer");
72 static const arg_def_t max_q_arg =
73 ARG_DEF(NULL, "max-q", 1, "Maximum quantizer");
74 static const arg_def_t min_bitrate_arg =
75 ARG_DEF(NULL, "min-bitrate", 1, "Minimum bitrate");
76 static const arg_def_t max_bitrate_arg =
77 ARG_DEF(NULL, "max-bitrate", 1, "Maximum bitrate");
79 static const arg_def_t *svc_args[] = {
80 &encoding_mode_arg, &frames_arg, &width_arg, &height_arg,
81 &timebase_arg, &bitrate_arg, &skip_frames_arg, &layers_arg,
82 &kf_dist_arg, &scale_factors_arg, &quantizers_arg,
83 &quantizers_keyframe_arg, &passes_arg, &pass_arg,
84 &fpf_name_arg, &min_q_arg, &max_q_arg, &min_bitrate_arg,
85 &max_bitrate_arg, NULL
88 static const SVC_ENCODING_MODE default_encoding_mode =
89 INTER_LAYER_PREDICTION_IP;
90 static const uint32_t default_frames_to_skip = 0;
91 static const uint32_t default_frames_to_code = 60 * 60;
92 static const uint32_t default_width = 1920;
93 static const uint32_t default_height = 1080;
94 static const uint32_t default_timebase_num = 1;
95 static const uint32_t default_timebase_den = 60;
96 static const uint32_t default_bitrate = 1000;
97 static const uint32_t default_spatial_layers = 5;
98 static const uint32_t default_kf_dist = 100;
101 const char *input_filename;
102 const char *output_filename;
103 uint32_t frames_to_code;
104 uint32_t frames_to_skip;
105 struct VpxInputContext input_ctx;
111 static const char *exec_name;
114 fprintf(stderr, "Usage: %s <options> input_filename output_filename\n",
116 fprintf(stderr, "Options:\n");
117 arg_show_usage(stderr, svc_args);
121 static void parse_command_line(int argc, const char **argv_,
122 AppInput *app_input, SvcContext *svc_ctx,
123 vpx_codec_enc_cfg_t *enc_cfg) {
124 struct arg arg = {0};
131 const char *fpf_file_name = NULL;
132 unsigned int min_bitrate = 0;
133 unsigned int max_bitrate = 0;
135 // initialize SvcContext with parameters that will be passed to vpx_svc_init
136 svc_ctx->log_level = SVC_LOG_DEBUG;
137 svc_ctx->spatial_layers = default_spatial_layers;
138 svc_ctx->encoding_mode = default_encoding_mode;
140 // start with default encoder configuration
141 res = vpx_codec_enc_config_default(vpx_codec_vp9_cx(), enc_cfg, 0);
143 die("Failed to get config: %s\n", vpx_codec_err_to_string(res));
145 // update enc_cfg with app default values
146 enc_cfg->g_w = default_width;
147 enc_cfg->g_h = default_height;
148 enc_cfg->g_timebase.num = default_timebase_num;
149 enc_cfg->g_timebase.den = default_timebase_den;
150 enc_cfg->rc_target_bitrate = default_bitrate;
151 enc_cfg->kf_min_dist = default_kf_dist;
152 enc_cfg->kf_max_dist = default_kf_dist;
153 enc_cfg->rc_end_usage = VPX_CQ;
155 // initialize AppInput with default values
156 app_input->frames_to_code = default_frames_to_code;
157 app_input->frames_to_skip = default_frames_to_skip;
159 // process command line options
160 argv = argv_dup(argc - 1, argv_ + 1);
161 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
164 if (arg_match(&arg, &encoding_mode_arg, argi)) {
165 svc_ctx->encoding_mode = arg_parse_enum_or_int(&arg);
166 } else if (arg_match(&arg, &frames_arg, argi)) {
167 app_input->frames_to_code = arg_parse_uint(&arg);
168 } else if (arg_match(&arg, &width_arg, argi)) {
169 enc_cfg->g_w = arg_parse_uint(&arg);
170 } else if (arg_match(&arg, &height_arg, argi)) {
171 enc_cfg->g_h = arg_parse_uint(&arg);
172 } else if (arg_match(&arg, &timebase_arg, argi)) {
173 enc_cfg->g_timebase = arg_parse_rational(&arg);
174 } else if (arg_match(&arg, &bitrate_arg, argi)) {
175 enc_cfg->rc_target_bitrate = arg_parse_uint(&arg);
176 } else if (arg_match(&arg, &skip_frames_arg, argi)) {
177 app_input->frames_to_skip = arg_parse_uint(&arg);
178 } else if (arg_match(&arg, &layers_arg, argi)) {
179 svc_ctx->spatial_layers = arg_parse_uint(&arg);
180 } else if (arg_match(&arg, &kf_dist_arg, argi)) {
181 enc_cfg->kf_min_dist = arg_parse_uint(&arg);
182 enc_cfg->kf_max_dist = enc_cfg->kf_min_dist;
183 } else if (arg_match(&arg, &scale_factors_arg, argi)) {
184 vpx_svc_set_scale_factors(svc_ctx, arg.val);
185 } else if (arg_match(&arg, &quantizers_arg, argi)) {
186 vpx_svc_set_quantizers(svc_ctx, arg.val, 0);
187 } else if (arg_match(&arg, &quantizers_keyframe_arg, argi)) {
188 vpx_svc_set_quantizers(svc_ctx, arg.val, 1);
189 } else if (arg_match(&arg, &passes_arg, argi)) {
190 passes = arg_parse_uint(&arg);
191 if (passes < 1 || passes > 2) {
192 die("Error: Invalid number of passes (%d)\n", passes);
194 } else if (arg_match(&arg, &pass_arg, argi)) {
195 pass = arg_parse_uint(&arg);
196 if (pass < 1 || pass > 2) {
197 die("Error: Invalid pass selected (%d)\n", pass);
199 } else if (arg_match(&arg, &fpf_name_arg, argi)) {
200 fpf_file_name = arg.val;
201 } else if (arg_match(&arg, &min_q_arg, argi)) {
202 enc_cfg->rc_min_quantizer = arg_parse_uint(&arg);
203 } else if (arg_match(&arg, &max_q_arg, argi)) {
204 enc_cfg->rc_max_quantizer = arg_parse_uint(&arg);
205 } else if (arg_match(&arg, &min_bitrate_arg, argi)) {
206 min_bitrate = arg_parse_uint(&arg);
207 } else if (arg_match(&arg, &max_bitrate_arg, argi)) {
208 max_bitrate = arg_parse_uint(&arg);
214 if (passes == 0 || passes == 1) {
216 fprintf(stderr, "pass is ignored since there's only one pass\n");
218 enc_cfg->g_pass = VPX_RC_ONE_PASS;
221 die("pass must be specified when passes is 2\n");
224 if (fpf_file_name == NULL) {
225 die("fpf must be specified when passes is 2\n");
229 enc_cfg->g_pass = VPX_RC_FIRST_PASS;
230 if (!stats_open_file(&app_input->rc_stats, fpf_file_name, 0)) {
231 fatal("Failed to open statistics store");
234 enc_cfg->g_pass = VPX_RC_LAST_PASS;
235 if (!stats_open_file(&app_input->rc_stats, fpf_file_name, 1)) {
236 fatal("Failed to open statistics store");
238 enc_cfg->rc_twopass_stats_in = stats_get(&app_input->rc_stats);
240 app_input->passes = passes;
241 app_input->pass = pass;
244 if (enc_cfg->rc_target_bitrate > 0) {
245 if (min_bitrate > 0) {
246 enc_cfg->rc_2pass_vbr_minsection_pct =
247 min_bitrate * 100 / enc_cfg->rc_target_bitrate;
249 if (max_bitrate > 0) {
250 enc_cfg->rc_2pass_vbr_maxsection_pct =
251 max_bitrate * 100 / enc_cfg->rc_target_bitrate;
255 // Check for unrecognized options
256 for (argi = argv; *argi; ++argi)
257 if (argi[0][0] == '-' && strlen(argi[0]) > 1)
258 die("Error: Unrecognized option %s\n", *argi);
260 if (argv[0] == NULL || argv[1] == 0) {
263 app_input->input_filename = argv[0];
264 app_input->output_filename = argv[1];
267 if (enc_cfg->g_w < 16 || enc_cfg->g_w % 2 || enc_cfg->g_h < 16 ||
269 die("Invalid resolution: %d x %d\n", enc_cfg->g_w, enc_cfg->g_h);
272 "Codec %s\nframes: %d, skip: %d\n"
273 "mode: %d, layers: %d\n"
274 "width %d, height: %d,\n"
275 "num: %d, den: %d, bitrate: %d,\n"
277 vpx_codec_iface_name(vpx_codec_vp9_cx()), app_input->frames_to_code,
278 app_input->frames_to_skip, svc_ctx->encoding_mode,
279 svc_ctx->spatial_layers, enc_cfg->g_w, enc_cfg->g_h,
280 enc_cfg->g_timebase.num, enc_cfg->g_timebase.den,
281 enc_cfg->rc_target_bitrate, enc_cfg->kf_max_dist);
284 int main(int argc, const char **argv) {
285 AppInput app_input = {0};
286 VpxVideoWriter *writer = NULL;
287 VpxVideoInfo info = {0};
288 vpx_codec_ctx_t codec;
289 vpx_codec_enc_cfg_t enc_cfg;
292 uint32_t frame_cnt = 0;
295 int pts = 0; /* PTS starts at 0 */
296 int frame_duration = 1; /* 1 timebase tick per frame */
298 int end_of_stream = 0;
300 memset(&svc_ctx, 0, sizeof(svc_ctx));
301 svc_ctx.log_print = 1;
303 parse_command_line(argc, argv, &app_input, &svc_ctx, &enc_cfg);
305 // Allocate image buffer
306 if (!vpx_img_alloc(&raw, VPX_IMG_FMT_I420, enc_cfg.g_w, enc_cfg.g_h, 32))
307 die("Failed to allocate image %dx%d\n", enc_cfg.g_w, enc_cfg.g_h);
309 if (!(infile = fopen(app_input.input_filename, "rb")))
310 die("Failed to open %s for reading\n", app_input.input_filename);
313 if (vpx_svc_init(&svc_ctx, &codec, vpx_codec_vp9_cx(), &enc_cfg) !=
315 die("Failed to initialize encoder\n");
317 info.codec_fourcc = VP9_FOURCC;
318 info.time_base.numerator = enc_cfg.g_timebase.num;
319 info.time_base.denominator = enc_cfg.g_timebase.den;
320 if (vpx_svc_get_layer_resolution(&svc_ctx, svc_ctx.spatial_layers - 1,
321 (unsigned int *)&info.frame_width,
322 (unsigned int *)&info.frame_height) !=
324 die("Failed to get output resolution");
327 if (!(app_input.passes == 2 && app_input.pass == 1)) {
328 // We don't save the bitstream for the 1st pass on two pass rate control
329 writer = vpx_video_writer_open(app_input.output_filename, kContainerIVF,
332 die("Failed to open %s for writing\n", app_input.output_filename);
335 // skip initial frames
336 for (i = 0; i < app_input.frames_to_skip; ++i)
337 vpx_img_read(&raw, infile);
340 while (!end_of_stream) {
341 if (frame_cnt >= app_input.frames_to_code || !vpx_img_read(&raw, infile)) {
342 // We need one extra vpx_svc_encode call at end of stream to flush
343 // encoder and get remaining data
347 res = vpx_svc_encode(&svc_ctx, &codec, (end_of_stream ? NULL : &raw),
348 pts, frame_duration, VPX_DL_GOOD_QUALITY);
349 printf("%s", vpx_svc_get_message(&svc_ctx));
350 if (res != VPX_CODEC_OK) {
351 die_codec(&codec, "Failed to encode frame");
353 if (!(app_input.passes == 2 && app_input.pass == 1)) {
354 if (vpx_svc_get_frame_size(&svc_ctx) > 0) {
355 vpx_video_writer_write_frame(writer,
356 vpx_svc_get_buffer(&svc_ctx),
357 vpx_svc_get_frame_size(&svc_ctx),
361 if (vpx_svc_get_rc_stats_buffer_size(&svc_ctx) > 0) {
362 stats_write(&app_input.rc_stats,
363 vpx_svc_get_rc_stats_buffer(&svc_ctx),
364 vpx_svc_get_rc_stats_buffer_size(&svc_ctx));
366 if (!end_of_stream) {
368 pts += frame_duration;
372 printf("Processed %d frames\n", frame_cnt);
375 if (vpx_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec");
377 if (app_input.passes == 2)
378 stats_close(&app_input.rc_stats, 1);
381 vpx_video_writer_close(writer);
386 // display average size, psnr
387 printf("%s", vpx_svc_dump_statistics(&svc_ctx));
389 vpx_svc_release(&svc_ctx);