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 "vpx/svc_context.h"
25 #include "vpx/vp8cx.h"
26 #include "vpx/vpx_encoder.h"
28 static const struct arg_enum_list encoding_mode_enum[] = {
29 {"i", INTER_LAYER_PREDICTION_I},
30 {"alt-ip", ALT_INTER_LAYER_PREDICTION_IP},
31 {"ip", INTER_LAYER_PREDICTION_IP},
32 {"gf", USE_GOLDEN_FRAME},
36 static const arg_def_t encoding_mode_arg = ARG_DEF_ENUM(
37 "m", "encoding-mode", 1, "Encoding mode algorithm", encoding_mode_enum);
38 static const arg_def_t skip_frames_arg =
39 ARG_DEF("s", "skip-frames", 1, "input frames to skip");
40 static const arg_def_t frames_arg =
41 ARG_DEF("f", "frames", 1, "number of frames to encode");
42 static const arg_def_t width_arg = ARG_DEF("w", "width", 1, "source width");
43 static const arg_def_t height_arg = ARG_DEF("h", "height", 1, "source height");
44 static const arg_def_t timebase_arg =
45 ARG_DEF("t", "timebase", 1, "timebase (num/den)");
46 static const arg_def_t bitrate_arg = ARG_DEF(
47 "b", "target-bitrate", 1, "encoding bitrate, in kilobits per second");
48 static const arg_def_t layers_arg =
49 ARG_DEF("l", "layers", 1, "number of SVC layers");
50 static const arg_def_t kf_dist_arg =
51 ARG_DEF("k", "kf-dist", 1, "number of frames between keyframes");
52 static const arg_def_t scale_factors_arg =
53 ARG_DEF("r", "scale-factors", 1, "scale factors (lowest to highest layer)");
54 static const arg_def_t quantizers_arg =
55 ARG_DEF("q", "quantizers", 1, "quantizers (lowest to highest layer)");
56 static const arg_def_t dummy_frame_arg =
57 ARG_DEF("z", "dummy-frame", 1, "make first frame blank and full size");
59 static const arg_def_t *svc_args[] = {
60 &encoding_mode_arg, &frames_arg, &width_arg, &height_arg,
61 &timebase_arg, &bitrate_arg, &skip_frames_arg, &layers_arg,
62 &kf_dist_arg, &scale_factors_arg, &quantizers_arg, &dummy_frame_arg,
66 static const SVC_ENCODING_MODE default_encoding_mode =
67 INTER_LAYER_PREDICTION_IP;
68 static const uint32_t default_frames_to_skip = 0;
69 static const uint32_t default_frames_to_code = 60 * 60;
70 static const uint32_t default_width = 1920;
71 static const uint32_t default_height = 1080;
72 static const uint32_t default_timebase_num = 1;
73 static const uint32_t default_timebase_den = 60;
74 static const uint32_t default_bitrate = 1000;
75 static const uint32_t default_spatial_layers = 5;
76 static const uint32_t default_kf_dist = 100;
77 static const int default_use_dummy_frame = 1;
80 char *output_filename;
81 uint32_t frames_to_code;
82 uint32_t frames_to_skip;
83 struct VpxInputContext input_ctx;
86 void usage_exit(const char *exec_name) {
87 fprintf(stderr, "Usage: %s <options> input_filename output_filename\n",
89 fprintf(stderr, "Options:\n");
90 arg_show_usage(stderr, svc_args);
94 static void die_codec(vpx_codec_ctx_t *ctx, const char *s) {
95 const char *detail = vpx_codec_error_detail(ctx);
97 printf("%s: %s\n", s, vpx_codec_error(ctx));
98 if (detail) printf(" %s\n", detail);
102 static int create_dummy_frame(vpx_image_t *img) {
103 const size_t buf_size = img->w * img->h * 3 / 2;
104 memset(img->planes[0], 129, buf_size);
108 static void parse_command_line(int argc, const char **argv_,
109 AppInput *app_input, SvcContext *svc_ctx,
110 vpx_codec_enc_cfg_t *enc_cfg) {
112 char **argv, **argi, **argj;
115 // initialize SvcContext with parameters that will be passed to vpx_svc_init
116 svc_ctx->log_level = SVC_LOG_DEBUG;
117 svc_ctx->spatial_layers = default_spatial_layers;
118 svc_ctx->encoding_mode = default_encoding_mode;
119 // when using a dummy frame, that frame is only encoded to be full size
120 svc_ctx->first_frame_full_size = default_use_dummy_frame;
122 // start with default encoder configuration
123 res = vpx_codec_enc_config_default(vpx_codec_vp9_cx(), enc_cfg, 0);
125 die("Failed to get config: %s\n", vpx_codec_err_to_string(res));
127 // update enc_cfg with app default values
128 enc_cfg->g_w = default_width;
129 enc_cfg->g_h = default_height;
130 enc_cfg->g_timebase.num = default_timebase_num;
131 enc_cfg->g_timebase.den = default_timebase_den;
132 enc_cfg->rc_target_bitrate = default_bitrate;
133 enc_cfg->kf_min_dist = default_kf_dist;
134 enc_cfg->kf_max_dist = default_kf_dist;
136 // initialize AppInput with default values
137 app_input->frames_to_code = default_frames_to_code;
138 app_input->frames_to_skip = default_frames_to_skip;
140 // process command line options
141 argv = argv_dup(argc - 1, argv_ + 1);
142 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
145 if (arg_match(&arg, &encoding_mode_arg, argi)) {
146 svc_ctx->encoding_mode = arg_parse_enum_or_int(&arg);
147 } else 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, &height_arg, argi)) {
154 enc_cfg->g_h = arg_parse_uint(&arg);
155 } else if (arg_match(&arg, &timebase_arg, argi)) {
156 enc_cfg->g_timebase = arg_parse_rational(&arg);
157 } else if (arg_match(&arg, &bitrate_arg, argi)) {
158 enc_cfg->rc_target_bitrate = arg_parse_uint(&arg);
159 } else if (arg_match(&arg, &skip_frames_arg, argi)) {
160 app_input->frames_to_skip = arg_parse_uint(&arg);
161 } else if (arg_match(&arg, &layers_arg, argi)) {
162 svc_ctx->spatial_layers = arg_parse_uint(&arg);
163 } else if (arg_match(&arg, &kf_dist_arg, argi)) {
164 enc_cfg->kf_min_dist = arg_parse_uint(&arg);
165 enc_cfg->kf_max_dist = enc_cfg->kf_min_dist;
166 } else if (arg_match(&arg, &scale_factors_arg, argi)) {
167 vpx_svc_set_scale_factors(svc_ctx, arg.val);
168 } else if (arg_match(&arg, &quantizers_arg, argi)) {
169 vpx_svc_set_quantizers(svc_ctx, arg.val);
170 } else if (arg_match(&arg, &dummy_frame_arg, argi)) {
171 svc_ctx->first_frame_full_size = arg_parse_int(&arg);
177 // Check for unrecognized options
178 for (argi = argv; *argi; ++argi)
179 if (argi[0][0] == '-' && strlen(argi[0]) > 1)
180 die("Error: Unrecognized option %s\n", *argi);
182 if (argv[0] == NULL || argv[1] == 0) {
183 usage_exit(argv_[0]);
185 app_input->input_ctx.filename = argv[0];
186 app_input->output_filename = argv[1];
189 if (enc_cfg->g_w < 16 || enc_cfg->g_w % 2 || enc_cfg->g_h < 16 ||
191 die("Invalid resolution: %d x %d\n", enc_cfg->g_w, enc_cfg->g_h);
194 "Codec %s\nframes: %d, skip: %d\n"
195 "mode: %d, layers: %d\n"
196 "width %d, height: %d,\n"
197 "num: %d, den: %d, bitrate: %d,\n"
198 "gop size: %d, use_dummy_frame: %d\n",
199 vpx_codec_iface_name(vpx_codec_vp9_cx()), app_input->frames_to_code,
200 app_input->frames_to_skip, svc_ctx->encoding_mode,
201 svc_ctx->spatial_layers, enc_cfg->g_w, enc_cfg->g_h,
202 enc_cfg->g_timebase.num, enc_cfg->g_timebase.den,
203 enc_cfg->rc_target_bitrate, enc_cfg->kf_max_dist,
204 svc_ctx->first_frame_full_size);
207 int main(int argc, const char **argv) {
208 AppInput app_input = {0};
210 vpx_codec_ctx_t codec;
211 vpx_codec_enc_cfg_t enc_cfg;
214 uint32_t frame_cnt = 0;
217 int pts = 0; /* PTS starts at 0 */
218 int frame_duration = 1; /* 1 timebase tick per frame */
219 vpx_codec_cx_pkt_t packet = {0};
220 packet.kind = VPX_CODEC_CX_FRAME_PKT;
222 memset(&svc_ctx, 0, sizeof(svc_ctx));
223 svc_ctx.log_print = 1;
224 parse_command_line(argc, argv, &app_input, &svc_ctx, &enc_cfg);
226 // Allocate image buffer
227 if (!vpx_img_alloc(&raw, VPX_IMG_FMT_I420, enc_cfg.g_w, enc_cfg.g_h, 32))
228 die("Failed to allocate image %dx%d\n", enc_cfg.g_w, enc_cfg.g_h);
230 if (!(app_input.input_ctx.file = fopen(app_input.input_ctx.filename, "rb")))
231 die("Failed to open %s for reading\n", app_input.input_ctx.filename);
233 if (!(outfile = fopen(app_input.output_filename, "wb")))
234 die("Failed to open %s for writing\n", app_input.output_filename);
237 if (vpx_svc_init(&svc_ctx, &codec, vpx_codec_vp9_cx(), &enc_cfg) !=
239 die("Failed to initialize encoder\n");
241 ivf_write_file_header(outfile, &enc_cfg, VP9_FOURCC, 0);
243 // skip initial frames
244 for (i = 0; i < app_input.frames_to_skip; ++i) {
245 read_yuv_frame(&app_input.input_ctx, &raw);
249 while (frame_cnt <= app_input.frames_to_code) {
250 if (frame_cnt == 0 && svc_ctx.first_frame_full_size) {
251 create_dummy_frame(&raw);
253 if (!read_yuv_frame(&app_input.input_ctx, &raw)) break;
255 res = vpx_svc_encode(&svc_ctx, &codec, &raw, pts, frame_duration,
257 printf("%s", vpx_svc_get_message(&svc_ctx));
258 if (res != VPX_CODEC_OK) {
259 die_codec(&codec, "Failed to encode frame");
261 if (vpx_svc_get_frame_size(&svc_ctx) > 0) {
262 packet.data.frame.pts = pts;
263 packet.data.frame.sz = vpx_svc_get_frame_size(&svc_ctx);
264 ivf_write_frame_header(outfile, &packet);
265 (void)fwrite(vpx_svc_get_buffer(&svc_ctx), 1,
266 vpx_svc_get_frame_size(&svc_ctx), outfile);
269 pts += frame_duration;
272 printf("Processed %d frames\n", frame_cnt - svc_ctx.first_frame_full_size);
274 fclose(app_input.input_ctx.file);
275 if (vpx_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec");
277 // rewrite the output file headers with the actual frame count
278 if (!fseek(outfile, 0, SEEK_SET)) {
279 ivf_write_file_header(outfile, &enc_cfg, VP9_FOURCC, frame_cnt);
284 // display average size, psnr
285 printf("%s", vpx_svc_dump_statistics(&svc_ctx));
287 vpx_svc_release(&svc_ctx);