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 arg_def_t skip_frames_arg =
32 ARG_DEF("s", "skip-frames", 1, "input frames to skip");
33 static const arg_def_t frames_arg =
34 ARG_DEF("f", "frames", 1, "number of frames to encode");
35 static const arg_def_t width_arg = ARG_DEF("w", "width", 1, "source width");
36 static const arg_def_t height_arg = ARG_DEF("h", "height", 1, "source height");
37 static const arg_def_t timebase_arg =
38 ARG_DEF("t", "timebase", 1, "timebase (num/den)");
39 static const arg_def_t bitrate_arg = ARG_DEF(
40 "b", "target-bitrate", 1, "encoding bitrate, in kilobits per second");
41 static const arg_def_t layers_arg =
42 ARG_DEF("l", "layers", 1, "number of SVC layers");
43 static const arg_def_t kf_dist_arg =
44 ARG_DEF("k", "kf-dist", 1, "number of frames between keyframes");
45 static const arg_def_t scale_factors_arg =
46 ARG_DEF("r", "scale-factors", 1, "scale factors (lowest to highest layer)");
47 static const arg_def_t quantizers_arg =
48 ARG_DEF("q", "quantizers", 1, "quantizers for non key frames, also will "
49 "be applied to key frames if -qn is not specified (lowest to "
51 static const arg_def_t passes_arg =
52 ARG_DEF("p", "passes", 1, "Number of passes (1/2)");
53 static const arg_def_t pass_arg =
54 ARG_DEF(NULL, "pass", 1, "Pass to execute (1/2)");
55 static const arg_def_t fpf_name_arg =
56 ARG_DEF(NULL, "fpf", 1, "First pass statistics file name");
57 static const arg_def_t min_q_arg =
58 ARG_DEF(NULL, "min-q", 1, "Minimum quantizer");
59 static const arg_def_t max_q_arg =
60 ARG_DEF(NULL, "max-q", 1, "Maximum quantizer");
61 static const arg_def_t min_bitrate_arg =
62 ARG_DEF(NULL, "min-bitrate", 1, "Minimum bitrate");
63 static const arg_def_t max_bitrate_arg =
64 ARG_DEF(NULL, "max-bitrate", 1, "Maximum bitrate");
66 static const arg_def_t *svc_args[] = {
67 &frames_arg, &width_arg, &height_arg,
68 &timebase_arg, &bitrate_arg, &skip_frames_arg, &layers_arg,
69 &kf_dist_arg, &scale_factors_arg, &quantizers_arg, &passes_arg,
70 &pass_arg, &fpf_name_arg, &min_q_arg, &max_q_arg,
71 &min_bitrate_arg, &max_bitrate_arg, NULL
74 static const uint32_t default_frames_to_skip = 0;
75 static const uint32_t default_frames_to_code = 60 * 60;
76 static const uint32_t default_width = 1920;
77 static const uint32_t default_height = 1080;
78 static const uint32_t default_timebase_num = 1;
79 static const uint32_t default_timebase_den = 60;
80 static const uint32_t default_bitrate = 1000;
81 static const uint32_t default_spatial_layers = 5;
82 static const uint32_t default_kf_dist = 100;
85 const char *input_filename;
86 const char *output_filename;
87 uint32_t frames_to_code;
88 uint32_t frames_to_skip;
89 struct VpxInputContext input_ctx;
95 static const char *exec_name;
98 fprintf(stderr, "Usage: %s <options> input_filename output_filename\n",
100 fprintf(stderr, "Options:\n");
101 arg_show_usage(stderr, svc_args);
105 static void parse_command_line(int argc, const char **argv_,
106 AppInput *app_input, SvcContext *svc_ctx,
107 vpx_codec_enc_cfg_t *enc_cfg) {
108 struct arg arg = {0};
115 const char *fpf_file_name = NULL;
116 unsigned int min_bitrate = 0;
117 unsigned int max_bitrate = 0;
119 // initialize SvcContext with parameters that will be passed to vpx_svc_init
120 svc_ctx->log_level = SVC_LOG_DEBUG;
121 svc_ctx->spatial_layers = default_spatial_layers;
123 // start with default encoder configuration
124 res = vpx_codec_enc_config_default(vpx_codec_vp9_cx(), enc_cfg, 0);
126 die("Failed to get config: %s\n", vpx_codec_err_to_string(res));
128 // update enc_cfg with app default values
129 enc_cfg->g_w = default_width;
130 enc_cfg->g_h = default_height;
131 enc_cfg->g_timebase.num = default_timebase_num;
132 enc_cfg->g_timebase.den = default_timebase_den;
133 enc_cfg->rc_target_bitrate = default_bitrate;
134 enc_cfg->kf_min_dist = default_kf_dist;
135 enc_cfg->kf_max_dist = default_kf_dist;
136 enc_cfg->rc_end_usage = VPX_CQ;
138 // initialize AppInput with default values
139 app_input->frames_to_code = default_frames_to_code;
140 app_input->frames_to_skip = default_frames_to_skip;
142 // process command line options
143 argv = argv_dup(argc - 1, argv_ + 1);
144 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
147 if (arg_match(&arg, &frames_arg, argi)) {
148 app_input->frames_to_code = arg_parse_uint(&arg);
149 } else if (arg_match(&arg, &width_arg, argi)) {
150 enc_cfg->g_w = arg_parse_uint(&arg);
151 } else if (arg_match(&arg, &height_arg, argi)) {
152 enc_cfg->g_h = arg_parse_uint(&arg);
153 } else if (arg_match(&arg, &timebase_arg, argi)) {
154 enc_cfg->g_timebase = arg_parse_rational(&arg);
155 } else if (arg_match(&arg, &bitrate_arg, argi)) {
156 enc_cfg->rc_target_bitrate = arg_parse_uint(&arg);
157 } else if (arg_match(&arg, &skip_frames_arg, argi)) {
158 app_input->frames_to_skip = arg_parse_uint(&arg);
159 } else if (arg_match(&arg, &layers_arg, argi)) {
160 svc_ctx->spatial_layers = arg_parse_uint(&arg);
161 } else if (arg_match(&arg, &kf_dist_arg, argi)) {
162 enc_cfg->kf_min_dist = arg_parse_uint(&arg);
163 enc_cfg->kf_max_dist = enc_cfg->kf_min_dist;
164 } else if (arg_match(&arg, &scale_factors_arg, argi)) {
165 vpx_svc_set_scale_factors(svc_ctx, arg.val);
166 } else if (arg_match(&arg, &quantizers_arg, argi)) {
167 vpx_svc_set_quantizers(svc_ctx, arg.val);
168 } else if (arg_match(&arg, &passes_arg, argi)) {
169 passes = arg_parse_uint(&arg);
170 if (passes < 1 || passes > 2) {
171 die("Error: Invalid number of passes (%d)\n", passes);
173 } else if (arg_match(&arg, &pass_arg, argi)) {
174 pass = arg_parse_uint(&arg);
175 if (pass < 1 || pass > 2) {
176 die("Error: Invalid pass selected (%d)\n", pass);
178 } else if (arg_match(&arg, &fpf_name_arg, argi)) {
179 fpf_file_name = arg.val;
180 } else if (arg_match(&arg, &min_q_arg, argi)) {
181 enc_cfg->rc_min_quantizer = arg_parse_uint(&arg);
182 } else if (arg_match(&arg, &max_q_arg, argi)) {
183 enc_cfg->rc_max_quantizer = arg_parse_uint(&arg);
184 } else if (arg_match(&arg, &min_bitrate_arg, argi)) {
185 min_bitrate = arg_parse_uint(&arg);
186 } else if (arg_match(&arg, &max_bitrate_arg, argi)) {
187 max_bitrate = arg_parse_uint(&arg);
193 if (passes == 0 || passes == 1) {
195 fprintf(stderr, "pass is ignored since there's only one pass\n");
197 enc_cfg->g_pass = VPX_RC_ONE_PASS;
200 die("pass must be specified when passes is 2\n");
203 if (fpf_file_name == NULL) {
204 die("fpf must be specified when passes is 2\n");
208 enc_cfg->g_pass = VPX_RC_FIRST_PASS;
209 if (!stats_open_file(&app_input->rc_stats, fpf_file_name, 0)) {
210 fatal("Failed to open statistics store");
213 enc_cfg->g_pass = VPX_RC_LAST_PASS;
214 if (!stats_open_file(&app_input->rc_stats, fpf_file_name, 1)) {
215 fatal("Failed to open statistics store");
217 enc_cfg->rc_twopass_stats_in = stats_get(&app_input->rc_stats);
219 app_input->passes = passes;
220 app_input->pass = pass;
223 if (enc_cfg->rc_target_bitrate > 0) {
224 if (min_bitrate > 0) {
225 enc_cfg->rc_2pass_vbr_minsection_pct =
226 min_bitrate * 100 / enc_cfg->rc_target_bitrate;
228 if (max_bitrate > 0) {
229 enc_cfg->rc_2pass_vbr_maxsection_pct =
230 max_bitrate * 100 / enc_cfg->rc_target_bitrate;
234 // Check for unrecognized options
235 for (argi = argv; *argi; ++argi)
236 if (argi[0][0] == '-' && strlen(argi[0]) > 1)
237 die("Error: Unrecognized option %s\n", *argi);
239 if (argv[0] == NULL || argv[1] == 0) {
242 app_input->input_filename = argv[0];
243 app_input->output_filename = argv[1];
246 if (enc_cfg->g_w < 16 || enc_cfg->g_w % 2 || enc_cfg->g_h < 16 ||
248 die("Invalid resolution: %d x %d\n", enc_cfg->g_w, enc_cfg->g_h);
251 "Codec %s\nframes: %d, skip: %d\n"
253 "width %d, height: %d,\n"
254 "num: %d, den: %d, bitrate: %d,\n"
256 vpx_codec_iface_name(vpx_codec_vp9_cx()), app_input->frames_to_code,
257 app_input->frames_to_skip,
258 svc_ctx->spatial_layers, enc_cfg->g_w, enc_cfg->g_h,
259 enc_cfg->g_timebase.num, enc_cfg->g_timebase.den,
260 enc_cfg->rc_target_bitrate, enc_cfg->kf_max_dist);
263 int main(int argc, const char **argv) {
264 AppInput app_input = {0};
265 VpxVideoWriter *writer = NULL;
266 VpxVideoInfo info = {0};
267 vpx_codec_ctx_t codec;
268 vpx_codec_enc_cfg_t enc_cfg;
271 uint32_t frame_cnt = 0;
274 int pts = 0; /* PTS starts at 0 */
275 int frame_duration = 1; /* 1 timebase tick per frame */
277 int end_of_stream = 0;
280 memset(&svc_ctx, 0, sizeof(svc_ctx));
281 svc_ctx.log_print = 1;
283 parse_command_line(argc, argv, &app_input, &svc_ctx, &enc_cfg);
285 // Allocate image buffer
286 if (!vpx_img_alloc(&raw, VPX_IMG_FMT_I420, enc_cfg.g_w, enc_cfg.g_h, 32))
287 die("Failed to allocate image %dx%d\n", enc_cfg.g_w, enc_cfg.g_h);
289 if (!(infile = fopen(app_input.input_filename, "rb")))
290 die("Failed to open %s for reading\n", app_input.input_filename);
293 if (vpx_svc_init(&svc_ctx, &codec, vpx_codec_vp9_cx(), &enc_cfg) !=
295 die("Failed to initialize encoder\n");
297 info.codec_fourcc = VP9_FOURCC;
298 info.time_base.numerator = enc_cfg.g_timebase.num;
299 info.time_base.denominator = enc_cfg.g_timebase.den;
300 if (vpx_svc_get_layer_resolution(&svc_ctx, svc_ctx.spatial_layers - 1,
301 (unsigned int *)&info.frame_width,
302 (unsigned int *)&info.frame_height) !=
304 die("Failed to get output resolution");
307 if (!(app_input.passes == 2 && app_input.pass == 1)) {
308 // We don't save the bitstream for the 1st pass on two pass rate control
309 writer = vpx_video_writer_open(app_input.output_filename, kContainerIVF,
312 die("Failed to open %s for writing\n", app_input.output_filename);
315 // skip initial frames
316 for (i = 0; i < app_input.frames_to_skip; ++i)
317 vpx_img_read(&raw, infile);
320 while (!end_of_stream) {
321 if (frame_cnt >= app_input.frames_to_code || !vpx_img_read(&raw, infile)) {
322 // We need one extra vpx_svc_encode call at end of stream to flush
323 // encoder and get remaining data
327 res = vpx_svc_encode(&svc_ctx, &codec, (end_of_stream ? NULL : &raw),
328 pts, frame_duration, VPX_DL_GOOD_QUALITY);
329 printf("%s", vpx_svc_get_message(&svc_ctx));
330 if (res != VPX_CODEC_OK) {
331 die_codec(&codec, "Failed to encode frame");
333 if (!(app_input.passes == 2 && app_input.pass == 1)) {
334 while ((frame_size = vpx_svc_get_frame_size(&svc_ctx)) > 0) {
335 vpx_video_writer_write_frame(writer,
336 vpx_svc_get_buffer(&svc_ctx),
340 if (vpx_svc_get_rc_stats_buffer_size(&svc_ctx) > 0) {
341 stats_write(&app_input.rc_stats,
342 vpx_svc_get_rc_stats_buffer(&svc_ctx),
343 vpx_svc_get_rc_stats_buffer_size(&svc_ctx));
345 if (!end_of_stream) {
347 pts += frame_duration;
351 printf("Processed %d frames\n", frame_cnt);
354 if (vpx_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec");
356 if (app_input.passes == 2)
357 stats_close(&app_input.rc_stats, 1);
360 vpx_video_writer_close(writer);
365 // display average size, psnr
366 printf("%s", vpx_svc_dump_statistics(&svc_ctx));
368 vpx_svc_release(&svc_ctx);