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 spatial_layers_arg =
42 ARG_DEF("sl", "spatial-layers", 1, "number of spatial SVC layers");
43 static const arg_def_t temporal_layers_arg =
44 ARG_DEF("tl", "temporal-layers", 1, "number of temporal SVC layers");
45 static const arg_def_t kf_dist_arg =
46 ARG_DEF("k", "kf-dist", 1, "number of frames between keyframes");
47 static const arg_def_t scale_factors_arg =
48 ARG_DEF("r", "scale-factors", 1, "scale factors (lowest to highest layer)");
49 static const arg_def_t quantizers_arg =
50 ARG_DEF("q", "quantizers", 1, "quantizers for non key frames, also will "
51 "be applied to key frames if -qn is not specified (lowest to "
53 static const arg_def_t passes_arg =
54 ARG_DEF("p", "passes", 1, "Number of passes (1/2)");
55 static const arg_def_t pass_arg =
56 ARG_DEF(NULL, "pass", 1, "Pass to execute (1/2)");
57 static const arg_def_t fpf_name_arg =
58 ARG_DEF(NULL, "fpf", 1, "First pass statistics file name");
59 static const arg_def_t min_q_arg =
60 ARG_DEF(NULL, "min-q", 1, "Minimum quantizer");
61 static const arg_def_t max_q_arg =
62 ARG_DEF(NULL, "max-q", 1, "Maximum quantizer");
63 static const arg_def_t min_bitrate_arg =
64 ARG_DEF(NULL, "min-bitrate", 1, "Minimum bitrate");
65 static const arg_def_t max_bitrate_arg =
66 ARG_DEF(NULL, "max-bitrate", 1, "Maximum bitrate");
68 static const arg_def_t *svc_args[] = {
69 &frames_arg, &width_arg, &height_arg,
70 &timebase_arg, &bitrate_arg, &skip_frames_arg, &spatial_layers_arg,
71 &kf_dist_arg, &scale_factors_arg, &quantizers_arg, &passes_arg,
72 &pass_arg, &fpf_name_arg, &min_q_arg, &max_q_arg,
73 &min_bitrate_arg, &max_bitrate_arg, &temporal_layers_arg,
77 static const uint32_t default_frames_to_skip = 0;
78 static const uint32_t default_frames_to_code = 60 * 60;
79 static const uint32_t default_width = 1920;
80 static const uint32_t default_height = 1080;
81 static const uint32_t default_timebase_num = 1;
82 static const uint32_t default_timebase_den = 60;
83 static const uint32_t default_bitrate = 1000;
84 static const uint32_t default_spatial_layers = 5;
85 static const uint32_t default_temporal_layers = 1;
86 static const uint32_t default_kf_dist = 100;
89 const char *input_filename;
90 const char *output_filename;
91 uint32_t frames_to_code;
92 uint32_t frames_to_skip;
93 struct VpxInputContext input_ctx;
99 static const char *exec_name;
102 fprintf(stderr, "Usage: %s <options> input_filename output_filename\n",
104 fprintf(stderr, "Options:\n");
105 arg_show_usage(stderr, svc_args);
109 static void parse_command_line(int argc, const char **argv_,
110 AppInput *app_input, SvcContext *svc_ctx,
111 vpx_codec_enc_cfg_t *enc_cfg) {
112 struct arg arg = {0};
119 const char *fpf_file_name = NULL;
120 unsigned int min_bitrate = 0;
121 unsigned int max_bitrate = 0;
123 // initialize SvcContext with parameters that will be passed to vpx_svc_init
124 svc_ctx->log_level = SVC_LOG_DEBUG;
125 svc_ctx->spatial_layers = default_spatial_layers;
126 svc_ctx->temporal_layers = default_temporal_layers;
128 // start with default encoder configuration
129 res = vpx_codec_enc_config_default(vpx_codec_vp9_cx(), enc_cfg, 0);
131 die("Failed to get config: %s\n", vpx_codec_err_to_string(res));
133 // update enc_cfg with app default values
134 enc_cfg->g_w = default_width;
135 enc_cfg->g_h = default_height;
136 enc_cfg->g_timebase.num = default_timebase_num;
137 enc_cfg->g_timebase.den = default_timebase_den;
138 enc_cfg->rc_target_bitrate = default_bitrate;
139 enc_cfg->kf_min_dist = default_kf_dist;
140 enc_cfg->kf_max_dist = default_kf_dist;
141 enc_cfg->rc_end_usage = VPX_CQ;
143 // initialize AppInput with default values
144 app_input->frames_to_code = default_frames_to_code;
145 app_input->frames_to_skip = default_frames_to_skip;
147 // process command line options
148 argv = argv_dup(argc - 1, argv_ + 1);
149 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
152 if (arg_match(&arg, &frames_arg, argi)) {
153 app_input->frames_to_code = arg_parse_uint(&arg);
154 } else if (arg_match(&arg, &width_arg, argi)) {
155 enc_cfg->g_w = arg_parse_uint(&arg);
156 } else if (arg_match(&arg, &height_arg, argi)) {
157 enc_cfg->g_h = arg_parse_uint(&arg);
158 } else if (arg_match(&arg, &timebase_arg, argi)) {
159 enc_cfg->g_timebase = arg_parse_rational(&arg);
160 } else if (arg_match(&arg, &bitrate_arg, argi)) {
161 enc_cfg->rc_target_bitrate = arg_parse_uint(&arg);
162 } else if (arg_match(&arg, &skip_frames_arg, argi)) {
163 app_input->frames_to_skip = arg_parse_uint(&arg);
164 } else if (arg_match(&arg, &spatial_layers_arg, argi)) {
165 svc_ctx->spatial_layers = arg_parse_uint(&arg);
166 } else if (arg_match(&arg, &temporal_layers_arg, argi)) {
167 svc_ctx->temporal_layers = arg_parse_uint(&arg);
168 } else if (arg_match(&arg, &kf_dist_arg, argi)) {
169 enc_cfg->kf_min_dist = arg_parse_uint(&arg);
170 enc_cfg->kf_max_dist = enc_cfg->kf_min_dist;
171 } else if (arg_match(&arg, &scale_factors_arg, argi)) {
172 vpx_svc_set_scale_factors(svc_ctx, arg.val);
173 } else if (arg_match(&arg, &quantizers_arg, argi)) {
174 vpx_svc_set_quantizers(svc_ctx, arg.val);
175 } else if (arg_match(&arg, &passes_arg, argi)) {
176 passes = arg_parse_uint(&arg);
177 if (passes < 1 || passes > 2) {
178 die("Error: Invalid number of passes (%d)\n", passes);
180 } else if (arg_match(&arg, &pass_arg, argi)) {
181 pass = arg_parse_uint(&arg);
182 if (pass < 1 || pass > 2) {
183 die("Error: Invalid pass selected (%d)\n", pass);
185 } else if (arg_match(&arg, &fpf_name_arg, argi)) {
186 fpf_file_name = arg.val;
187 } else if (arg_match(&arg, &min_q_arg, argi)) {
188 enc_cfg->rc_min_quantizer = arg_parse_uint(&arg);
189 } else if (arg_match(&arg, &max_q_arg, argi)) {
190 enc_cfg->rc_max_quantizer = arg_parse_uint(&arg);
191 } else if (arg_match(&arg, &min_bitrate_arg, argi)) {
192 min_bitrate = arg_parse_uint(&arg);
193 } else if (arg_match(&arg, &max_bitrate_arg, argi)) {
194 max_bitrate = arg_parse_uint(&arg);
200 if (passes == 0 || passes == 1) {
202 fprintf(stderr, "pass is ignored since there's only one pass\n");
204 enc_cfg->g_pass = VPX_RC_ONE_PASS;
207 die("pass must be specified when passes is 2\n");
210 if (fpf_file_name == NULL) {
211 die("fpf must be specified when passes is 2\n");
215 enc_cfg->g_pass = VPX_RC_FIRST_PASS;
216 if (!stats_open_file(&app_input->rc_stats, fpf_file_name, 0)) {
217 fatal("Failed to open statistics store");
220 enc_cfg->g_pass = VPX_RC_LAST_PASS;
221 if (!stats_open_file(&app_input->rc_stats, fpf_file_name, 1)) {
222 fatal("Failed to open statistics store");
224 enc_cfg->rc_twopass_stats_in = stats_get(&app_input->rc_stats);
226 app_input->passes = passes;
227 app_input->pass = pass;
230 if (enc_cfg->rc_target_bitrate > 0) {
231 if (min_bitrate > 0) {
232 enc_cfg->rc_2pass_vbr_minsection_pct =
233 min_bitrate * 100 / enc_cfg->rc_target_bitrate;
235 if (max_bitrate > 0) {
236 enc_cfg->rc_2pass_vbr_maxsection_pct =
237 max_bitrate * 100 / enc_cfg->rc_target_bitrate;
241 // Check for unrecognized options
242 for (argi = argv; *argi; ++argi)
243 if (argi[0][0] == '-' && strlen(argi[0]) > 1)
244 die("Error: Unrecognized option %s\n", *argi);
246 if (argv[0] == NULL || argv[1] == 0) {
249 app_input->input_filename = argv[0];
250 app_input->output_filename = argv[1];
253 if (enc_cfg->g_w < 16 || enc_cfg->g_w % 2 || enc_cfg->g_h < 16 ||
255 die("Invalid resolution: %d x %d\n", enc_cfg->g_w, enc_cfg->g_h);
258 "Codec %s\nframes: %d, skip: %d\n"
260 "width %d, height: %d,\n"
261 "num: %d, den: %d, bitrate: %d,\n"
263 vpx_codec_iface_name(vpx_codec_vp9_cx()), app_input->frames_to_code,
264 app_input->frames_to_skip,
265 svc_ctx->spatial_layers, enc_cfg->g_w, enc_cfg->g_h,
266 enc_cfg->g_timebase.num, enc_cfg->g_timebase.den,
267 enc_cfg->rc_target_bitrate, enc_cfg->kf_max_dist);
270 int main(int argc, const char **argv) {
271 AppInput app_input = {0};
272 VpxVideoWriter *writer = NULL;
273 VpxVideoInfo info = {0};
274 vpx_codec_ctx_t codec;
275 vpx_codec_enc_cfg_t enc_cfg;
278 uint32_t frame_cnt = 0;
281 int pts = 0; /* PTS starts at 0 */
282 int frame_duration = 1; /* 1 timebase tick per frame */
284 int end_of_stream = 0;
285 int frames_received = 0;
287 memset(&svc_ctx, 0, sizeof(svc_ctx));
288 svc_ctx.log_print = 1;
290 parse_command_line(argc, argv, &app_input, &svc_ctx, &enc_cfg);
292 // Allocate image buffer
293 if (!vpx_img_alloc(&raw, VPX_IMG_FMT_I420, enc_cfg.g_w, enc_cfg.g_h, 32))
294 die("Failed to allocate image %dx%d\n", enc_cfg.g_w, enc_cfg.g_h);
296 if (!(infile = fopen(app_input.input_filename, "rb")))
297 die("Failed to open %s for reading\n", app_input.input_filename);
300 if (vpx_svc_init(&svc_ctx, &codec, vpx_codec_vp9_cx(), &enc_cfg) !=
302 die("Failed to initialize encoder\n");
304 info.codec_fourcc = VP9_FOURCC;
305 info.time_base.numerator = enc_cfg.g_timebase.num;
306 info.time_base.denominator = enc_cfg.g_timebase.den;
307 if (vpx_svc_get_layer_resolution(&svc_ctx, svc_ctx.spatial_layers - 1,
308 (unsigned int *)&info.frame_width,
309 (unsigned int *)&info.frame_height) !=
311 die("Failed to get output resolution");
314 if (!(app_input.passes == 2 && app_input.pass == 1)) {
315 // We don't save the bitstream for the 1st pass on two pass rate control
316 writer = vpx_video_writer_open(app_input.output_filename, kContainerIVF,
319 die("Failed to open %s for writing\n", app_input.output_filename);
322 // skip initial frames
323 for (i = 0; i < app_input.frames_to_skip; ++i)
324 vpx_img_read(&raw, infile);
327 while (!end_of_stream) {
328 vpx_codec_iter_t iter = NULL;
329 const vpx_codec_cx_pkt_t *cx_pkt;
330 if (frame_cnt >= app_input.frames_to_code || !vpx_img_read(&raw, infile)) {
331 // We need one extra vpx_svc_encode call at end of stream to flush
332 // encoder and get remaining data
336 res = vpx_svc_encode(&svc_ctx, &codec, (end_of_stream ? NULL : &raw),
337 pts, frame_duration, VPX_DL_GOOD_QUALITY);
338 printf("%s", vpx_svc_get_message(&svc_ctx));
339 if (res != VPX_CODEC_OK) {
340 die_codec(&codec, "Failed to encode frame");
343 while ((cx_pkt = vpx_codec_get_cx_data(&codec, &iter)) != NULL) {
344 switch (cx_pkt->kind) {
345 case VPX_CODEC_CX_FRAME_PKT: {
346 if (cx_pkt->data.frame.sz > 0)
347 vpx_video_writer_write_frame(writer,
348 cx_pkt->data.frame.buf,
349 cx_pkt->data.frame.sz,
350 cx_pkt->data.frame.pts);
352 printf("SVC frame: %d, kf: %d, size: %d, pts: %d\n", frames_received,
353 !!(cx_pkt->data.frame.flags & VPX_FRAME_IS_KEY),
354 (int)cx_pkt->data.frame.sz, (int)cx_pkt->data.frame.pts);
358 case VPX_CODEC_STATS_PKT: {
359 stats_write(&app_input.rc_stats,
360 cx_pkt->data.twopass_stats.buf,
361 cx_pkt->data.twopass_stats.sz);
370 if (!end_of_stream) {
372 pts += frame_duration;
376 printf("Processed %d frames\n", frame_cnt);
379 if (vpx_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec");
381 if (app_input.passes == 2)
382 stats_close(&app_input.rc_stats, 1);
385 vpx_video_writer_close(writer);
390 // display average size, psnr
391 printf("%s", vpx_svc_dump_statistics(&svc_ctx));
393 vpx_svc_release(&svc_ctx);