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 passes_arg =
50 ARG_DEF("p", "passes", 1, "Number of passes (1/2)");
51 static const arg_def_t pass_arg =
52 ARG_DEF(NULL, "pass", 1, "Pass to execute (1/2)");
53 static const arg_def_t fpf_name_arg =
54 ARG_DEF(NULL, "fpf", 1, "First pass statistics file name");
55 static const arg_def_t min_q_arg =
56 ARG_DEF(NULL, "min-q", 1, "Minimum quantizer");
57 static const arg_def_t max_q_arg =
58 ARG_DEF(NULL, "max-q", 1, "Maximum quantizer");
59 static const arg_def_t min_bitrate_arg =
60 ARG_DEF(NULL, "min-bitrate", 1, "Minimum bitrate");
61 static const arg_def_t max_bitrate_arg =
62 ARG_DEF(NULL, "max-bitrate", 1, "Maximum bitrate");
64 static const arg_def_t *svc_args[] = {
65 &frames_arg, &width_arg, &height_arg,
66 &timebase_arg, &bitrate_arg, &skip_frames_arg, &spatial_layers_arg,
67 &kf_dist_arg, &scale_factors_arg, &passes_arg, &pass_arg,
68 &fpf_name_arg, &min_q_arg, &max_q_arg, &min_bitrate_arg,
69 &max_bitrate_arg, &temporal_layers_arg, NULL
72 static const uint32_t default_frames_to_skip = 0;
73 static const uint32_t default_frames_to_code = 60 * 60;
74 static const uint32_t default_width = 1920;
75 static const uint32_t default_height = 1080;
76 static const uint32_t default_timebase_num = 1;
77 static const uint32_t default_timebase_den = 60;
78 static const uint32_t default_bitrate = 1000;
79 static const uint32_t default_spatial_layers = 5;
80 static const uint32_t default_temporal_layers = 1;
81 static const uint32_t default_kf_dist = 100;
84 const char *input_filename;
85 const char *output_filename;
86 uint32_t frames_to_code;
87 uint32_t frames_to_skip;
88 struct VpxInputContext input_ctx;
94 static const char *exec_name;
97 fprintf(stderr, "Usage: %s <options> input_filename output_filename\n",
99 fprintf(stderr, "Options:\n");
100 arg_show_usage(stderr, svc_args);
104 static void parse_command_line(int argc, const char **argv_,
105 AppInput *app_input, SvcContext *svc_ctx,
106 vpx_codec_enc_cfg_t *enc_cfg) {
107 struct arg arg = {0};
114 const char *fpf_file_name = NULL;
115 unsigned int min_bitrate = 0;
116 unsigned int max_bitrate = 0;
117 char string_options[1024] = {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;
122 svc_ctx->temporal_layers = default_temporal_layers;
124 // start with default encoder configuration
125 res = vpx_codec_enc_config_default(vpx_codec_vp9_cx(), enc_cfg, 0);
127 die("Failed to get config: %s\n", vpx_codec_err_to_string(res));
129 // update enc_cfg with app default values
130 enc_cfg->g_w = default_width;
131 enc_cfg->g_h = default_height;
132 enc_cfg->g_timebase.num = default_timebase_num;
133 enc_cfg->g_timebase.den = default_timebase_den;
134 enc_cfg->rc_target_bitrate = default_bitrate;
135 enc_cfg->kf_min_dist = default_kf_dist;
136 enc_cfg->kf_max_dist = default_kf_dist;
137 enc_cfg->rc_end_usage = VPX_CQ;
139 // initialize AppInput with default values
140 app_input->frames_to_code = default_frames_to_code;
141 app_input->frames_to_skip = default_frames_to_skip;
143 // process command line options
144 argv = argv_dup(argc - 1, argv_ + 1);
145 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
148 if (arg_match(&arg, &frames_arg, argi)) {
149 app_input->frames_to_code = arg_parse_uint(&arg);
150 } else if (arg_match(&arg, &width_arg, argi)) {
151 enc_cfg->g_w = arg_parse_uint(&arg);
152 } else if (arg_match(&arg, &height_arg, argi)) {
153 enc_cfg->g_h = arg_parse_uint(&arg);
154 } else if (arg_match(&arg, &timebase_arg, argi)) {
155 enc_cfg->g_timebase = arg_parse_rational(&arg);
156 } else if (arg_match(&arg, &bitrate_arg, argi)) {
157 enc_cfg->rc_target_bitrate = arg_parse_uint(&arg);
158 } else if (arg_match(&arg, &skip_frames_arg, argi)) {
159 app_input->frames_to_skip = arg_parse_uint(&arg);
160 } else if (arg_match(&arg, &spatial_layers_arg, argi)) {
161 svc_ctx->spatial_layers = arg_parse_uint(&arg);
162 } else if (arg_match(&arg, &temporal_layers_arg, argi)) {
163 svc_ctx->temporal_layers = arg_parse_uint(&arg);
164 } else if (arg_match(&arg, &kf_dist_arg, argi)) {
165 enc_cfg->kf_min_dist = arg_parse_uint(&arg);
166 enc_cfg->kf_max_dist = enc_cfg->kf_min_dist;
167 } else if (arg_match(&arg, &scale_factors_arg, argi)) {
168 snprintf(string_options, 1024, "%s scale-factors=%s",
169 string_options, arg.val);
170 } else if (arg_match(&arg, &passes_arg, argi)) {
171 passes = arg_parse_uint(&arg);
172 if (passes < 1 || passes > 2) {
173 die("Error: Invalid number of passes (%d)\n", passes);
175 } else if (arg_match(&arg, &pass_arg, argi)) {
176 pass = arg_parse_uint(&arg);
177 if (pass < 1 || pass > 2) {
178 die("Error: Invalid pass selected (%d)\n", pass);
180 } else if (arg_match(&arg, &fpf_name_arg, argi)) {
181 fpf_file_name = arg.val;
182 } else if (arg_match(&arg, &min_q_arg, argi)) {
183 snprintf(string_options, 1024, "%s min-quantizers=%s",
184 string_options, arg.val);
185 } else if (arg_match(&arg, &max_q_arg, argi)) {
186 snprintf(string_options, 1024, "%s max-quantizers=%s",
187 string_options, arg.val);
188 } else if (arg_match(&arg, &min_bitrate_arg, argi)) {
189 min_bitrate = arg_parse_uint(&arg);
190 } else if (arg_match(&arg, &max_bitrate_arg, argi)) {
191 max_bitrate = arg_parse_uint(&arg);
197 // There will be a space in front of the string options
198 if (strlen(string_options) > 0)
199 vpx_svc_set_options(svc_ctx, string_options + 1);
201 if (passes == 0 || passes == 1) {
203 fprintf(stderr, "pass is ignored since there's only one pass\n");
205 enc_cfg->g_pass = VPX_RC_ONE_PASS;
208 die("pass must be specified when passes is 2\n");
211 if (fpf_file_name == NULL) {
212 die("fpf must be specified when passes is 2\n");
216 enc_cfg->g_pass = VPX_RC_FIRST_PASS;
217 if (!stats_open_file(&app_input->rc_stats, fpf_file_name, 0)) {
218 fatal("Failed to open statistics store");
221 enc_cfg->g_pass = VPX_RC_LAST_PASS;
222 if (!stats_open_file(&app_input->rc_stats, fpf_file_name, 1)) {
223 fatal("Failed to open statistics store");
225 enc_cfg->rc_twopass_stats_in = stats_get(&app_input->rc_stats);
227 app_input->passes = passes;
228 app_input->pass = pass;
231 if (enc_cfg->rc_target_bitrate > 0) {
232 if (min_bitrate > 0) {
233 enc_cfg->rc_2pass_vbr_minsection_pct =
234 min_bitrate * 100 / enc_cfg->rc_target_bitrate;
236 if (max_bitrate > 0) {
237 enc_cfg->rc_2pass_vbr_maxsection_pct =
238 max_bitrate * 100 / enc_cfg->rc_target_bitrate;
242 // Check for unrecognized options
243 for (argi = argv; *argi; ++argi)
244 if (argi[0][0] == '-' && strlen(argi[0]) > 1)
245 die("Error: Unrecognized option %s\n", *argi);
247 if (argv[0] == NULL || argv[1] == 0) {
250 app_input->input_filename = argv[0];
251 app_input->output_filename = argv[1];
254 if (enc_cfg->g_w < 16 || enc_cfg->g_w % 2 || enc_cfg->g_h < 16 ||
256 die("Invalid resolution: %d x %d\n", enc_cfg->g_w, enc_cfg->g_h);
259 "Codec %s\nframes: %d, skip: %d\n"
261 "width %d, height: %d,\n"
262 "num: %d, den: %d, bitrate: %d,\n"
264 vpx_codec_iface_name(vpx_codec_vp9_cx()), app_input->frames_to_code,
265 app_input->frames_to_skip,
266 svc_ctx->spatial_layers, enc_cfg->g_w, enc_cfg->g_h,
267 enc_cfg->g_timebase.num, enc_cfg->g_timebase.den,
268 enc_cfg->rc_target_bitrate, enc_cfg->kf_max_dist);
271 int main(int argc, const char **argv) {
272 AppInput app_input = {0};
273 VpxVideoWriter *writer = NULL;
274 VpxVideoInfo info = {0};
275 vpx_codec_ctx_t codec;
276 vpx_codec_enc_cfg_t enc_cfg;
279 uint32_t frame_cnt = 0;
282 int pts = 0; /* PTS starts at 0 */
283 int frame_duration = 1; /* 1 timebase tick per frame */
285 int end_of_stream = 0;
286 int frames_received = 0;
288 memset(&svc_ctx, 0, sizeof(svc_ctx));
289 svc_ctx.log_print = 1;
291 parse_command_line(argc, argv, &app_input, &svc_ctx, &enc_cfg);
293 // Allocate image buffer
294 if (!vpx_img_alloc(&raw, VPX_IMG_FMT_I420, enc_cfg.g_w, enc_cfg.g_h, 32))
295 die("Failed to allocate image %dx%d\n", enc_cfg.g_w, enc_cfg.g_h);
297 if (!(infile = fopen(app_input.input_filename, "rb")))
298 die("Failed to open %s for reading\n", app_input.input_filename);
301 if (vpx_svc_init(&svc_ctx, &codec, vpx_codec_vp9_cx(), &enc_cfg) !=
303 die("Failed to initialize encoder\n");
305 info.codec_fourcc = VP9_FOURCC;
306 info.time_base.numerator = enc_cfg.g_timebase.num;
307 info.time_base.denominator = enc_cfg.g_timebase.den;
309 if (!(app_input.passes == 2 && app_input.pass == 1)) {
310 // We don't save the bitstream for the 1st pass on two pass rate control
311 writer = vpx_video_writer_open(app_input.output_filename, kContainerIVF,
314 die("Failed to open %s for writing\n", app_input.output_filename);
317 // skip initial frames
318 for (i = 0; i < app_input.frames_to_skip; ++i)
319 vpx_img_read(&raw, infile);
322 while (!end_of_stream) {
323 vpx_codec_iter_t iter = NULL;
324 const vpx_codec_cx_pkt_t *cx_pkt;
325 if (frame_cnt >= app_input.frames_to_code || !vpx_img_read(&raw, infile)) {
326 // We need one extra vpx_svc_encode call at end of stream to flush
327 // encoder and get remaining data
331 res = vpx_svc_encode(&svc_ctx, &codec, (end_of_stream ? NULL : &raw),
332 pts, frame_duration, VPX_DL_GOOD_QUALITY);
333 printf("%s", vpx_svc_get_message(&svc_ctx));
334 if (res != VPX_CODEC_OK) {
335 die_codec(&codec, "Failed to encode frame");
338 while ((cx_pkt = vpx_codec_get_cx_data(&codec, &iter)) != NULL) {
339 switch (cx_pkt->kind) {
340 case VPX_CODEC_CX_FRAME_PKT: {
341 if (cx_pkt->data.frame.sz > 0)
342 vpx_video_writer_write_frame(writer,
343 cx_pkt->data.frame.buf,
344 cx_pkt->data.frame.sz,
345 cx_pkt->data.frame.pts);
347 printf("SVC frame: %d, kf: %d, size: %d, pts: %d\n", frames_received,
348 !!(cx_pkt->data.frame.flags & VPX_FRAME_IS_KEY),
349 (int)cx_pkt->data.frame.sz, (int)cx_pkt->data.frame.pts);
353 case VPX_CODEC_STATS_PKT: {
354 stats_write(&app_input.rc_stats,
355 cx_pkt->data.twopass_stats.buf,
356 cx_pkt->data.twopass_stats.sz);
365 if (!end_of_stream) {
367 pts += frame_duration;
371 printf("Processed %d frames\n", frame_cnt);
374 if (vpx_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec");
376 if (app_input.passes == 2)
377 stats_close(&app_input.rc_stats, 1);
380 vpx_video_writer_close(writer);
385 // display average size, psnr
386 printf("%s", vpx_svc_dump_statistics(&svc_ctx));
388 vpx_svc_release(&svc_ctx);