1/*
2 *  Copyright (c) 2012 The WebM project authors. All Rights Reserved.
3 *
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.
9 */
10
11/*
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.
15 */
16
17#include <stdarg.h>
18#include <stdlib.h>
19#include <string.h>
20#include <time.h>
21
22#include "./args.h"
23#include "./tools_common.h"
24#include "./video_writer.h"
25
26#include "vpx/svc_context.h"
27#include "vpx/vp8cx.h"
28#include "vpx/vpx_encoder.h"
29#include "./vpxstats.h"
30
31static 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},
36  {NULL, 0}
37};
38
39static const arg_def_t encoding_mode_arg = ARG_DEF_ENUM(
40    "m", "encoding-mode", 1, "Encoding mode algorithm", encoding_mode_enum);
41static const arg_def_t skip_frames_arg =
42    ARG_DEF("s", "skip-frames", 1, "input frames to skip");
43static const arg_def_t frames_arg =
44    ARG_DEF("f", "frames", 1, "number of frames to encode");
45static const arg_def_t width_arg = ARG_DEF("w", "width", 1, "source width");
46static const arg_def_t height_arg = ARG_DEF("h", "height", 1, "source height");
47static const arg_def_t timebase_arg =
48    ARG_DEF("t", "timebase", 1, "timebase (num/den)");
49static const arg_def_t bitrate_arg = ARG_DEF(
50    "b", "target-bitrate", 1, "encoding bitrate, in kilobits per second");
51static const arg_def_t layers_arg =
52    ARG_DEF("l", "layers", 1, "number of SVC layers");
53static const arg_def_t kf_dist_arg =
54    ARG_DEF("k", "kf-dist", 1, "number of frames between keyframes");
55static const arg_def_t scale_factors_arg =
56    ARG_DEF("r", "scale-factors", 1, "scale factors (lowest to highest layer)");
57static 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 "
60            "highest layer)");
61static const arg_def_t quantizers_keyframe_arg =
62    ARG_DEF("qn", "quantizers-keyframe", 1, "quantizers for key frames (lowest "
63        "to highest layer)");
64static const arg_def_t passes_arg =
65    ARG_DEF("p", "passes", 1, "Number of passes (1/2)");
66static const arg_def_t pass_arg =
67    ARG_DEF(NULL, "pass", 1, "Pass to execute (1/2)");
68static const arg_def_t fpf_name_arg =
69    ARG_DEF(NULL, "fpf", 1, "First pass statistics file name");
70static const arg_def_t min_q_arg =
71    ARG_DEF(NULL, "min-q", 1, "Minimum quantizer");
72static const arg_def_t max_q_arg =
73    ARG_DEF(NULL, "max-q", 1, "Maximum quantizer");
74static const arg_def_t min_bitrate_arg =
75    ARG_DEF(NULL, "min-bitrate", 1, "Minimum bitrate");
76static const arg_def_t max_bitrate_arg =
77    ARG_DEF(NULL, "max-bitrate", 1, "Maximum bitrate");
78
79static 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
86};
87
88static const SVC_ENCODING_MODE default_encoding_mode =
89    INTER_LAYER_PREDICTION_IP;
90static const uint32_t default_frames_to_skip = 0;
91static const uint32_t default_frames_to_code = 60 * 60;
92static const uint32_t default_width = 1920;
93static const uint32_t default_height = 1080;
94static const uint32_t default_timebase_num = 1;
95static const uint32_t default_timebase_den = 60;
96static const uint32_t default_bitrate = 1000;
97static const uint32_t default_spatial_layers = 5;
98static const uint32_t default_kf_dist = 100;
99
100typedef struct {
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;
106  stats_io_t rc_stats;
107  int passes;
108  int pass;
109} AppInput;
110
111static const char *exec_name;
112
113void usage_exit() {
114  fprintf(stderr, "Usage: %s <options> input_filename output_filename\n",
115          exec_name);
116  fprintf(stderr, "Options:\n");
117  arg_show_usage(stderr, svc_args);
118  exit(EXIT_FAILURE);
119}
120
121static 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};
125  char **argv = NULL;
126  char **argi = NULL;
127  char **argj = NULL;
128  vpx_codec_err_t res;
129  int passes = 0;
130  int pass = 0;
131  const char *fpf_file_name = NULL;
132  unsigned int min_bitrate = 0;
133  unsigned int max_bitrate = 0;
134
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;
139
140  // start with default encoder configuration
141  res = vpx_codec_enc_config_default(vpx_codec_vp9_cx(), enc_cfg, 0);
142  if (res) {
143    die("Failed to get config: %s\n", vpx_codec_err_to_string(res));
144  }
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
154  // initialize AppInput with default values
155  app_input->frames_to_code = default_frames_to_code;
156  app_input->frames_to_skip = default_frames_to_skip;
157
158  // process command line options
159  argv = argv_dup(argc - 1, argv_ + 1);
160  for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
161    arg.argv_step = 1;
162
163    if (arg_match(&arg, &encoding_mode_arg, argi)) {
164      svc_ctx->encoding_mode = arg_parse_enum_or_int(&arg);
165    } else if (arg_match(&arg, &frames_arg, argi)) {
166      app_input->frames_to_code = arg_parse_uint(&arg);
167    } else if (arg_match(&arg, &width_arg, argi)) {
168      enc_cfg->g_w = arg_parse_uint(&arg);
169    } else if (arg_match(&arg, &height_arg, argi)) {
170      enc_cfg->g_h = arg_parse_uint(&arg);
171    } else if (arg_match(&arg, &timebase_arg, argi)) {
172      enc_cfg->g_timebase = arg_parse_rational(&arg);
173    } else if (arg_match(&arg, &bitrate_arg, argi)) {
174      enc_cfg->rc_target_bitrate = arg_parse_uint(&arg);
175    } else if (arg_match(&arg, &skip_frames_arg, argi)) {
176      app_input->frames_to_skip = arg_parse_uint(&arg);
177    } else if (arg_match(&arg, &layers_arg, argi)) {
178      svc_ctx->spatial_layers = arg_parse_uint(&arg);
179    } else if (arg_match(&arg, &kf_dist_arg, argi)) {
180      enc_cfg->kf_min_dist = arg_parse_uint(&arg);
181      enc_cfg->kf_max_dist = enc_cfg->kf_min_dist;
182    } else if (arg_match(&arg, &scale_factors_arg, argi)) {
183      vpx_svc_set_scale_factors(svc_ctx, arg.val);
184    } else if (arg_match(&arg, &quantizers_arg, argi)) {
185      vpx_svc_set_quantizers(svc_ctx, arg.val, 0);
186    } else if (arg_match(&arg, &quantizers_keyframe_arg, argi)) {
187      vpx_svc_set_quantizers(svc_ctx, arg.val, 1);
188    } else if (arg_match(&arg, &passes_arg, argi)) {
189      passes = arg_parse_uint(&arg);
190      if (passes < 1 || passes > 2) {
191        die("Error: Invalid number of passes (%d)\n", passes);
192      }
193    } else if (arg_match(&arg, &pass_arg, argi)) {
194      pass = arg_parse_uint(&arg);
195      if (pass < 1 || pass > 2) {
196        die("Error: Invalid pass selected (%d)\n", pass);
197      }
198    } else if (arg_match(&arg, &fpf_name_arg, argi)) {
199      fpf_file_name = arg.val;
200    } else if (arg_match(&arg, &min_q_arg, argi)) {
201      enc_cfg->rc_min_quantizer = arg_parse_uint(&arg);
202    } else if (arg_match(&arg, &max_q_arg, argi)) {
203      enc_cfg->rc_max_quantizer = arg_parse_uint(&arg);
204    } else if (arg_match(&arg, &min_bitrate_arg, argi)) {
205      min_bitrate = arg_parse_uint(&arg);
206    } else if (arg_match(&arg, &max_bitrate_arg, argi)) {
207      max_bitrate = arg_parse_uint(&arg);
208    } else {
209      ++argj;
210    }
211  }
212
213  if (passes == 0 || passes == 1) {
214    if (pass) {
215      fprintf(stderr, "pass is ignored since there's only one pass\n");
216    }
217    enc_cfg->g_pass = VPX_RC_ONE_PASS;
218  } else {
219    if (pass == 0) {
220      die("pass must be specified when passes is 2\n");
221    }
222
223    if (fpf_file_name == NULL) {
224      die("fpf must be specified when passes is 2\n");
225    }
226
227    if (pass == 1) {
228      enc_cfg->g_pass = VPX_RC_FIRST_PASS;
229      if (!stats_open_file(&app_input->rc_stats, fpf_file_name, 0)) {
230        fatal("Failed to open statistics store");
231      }
232    } else {
233      enc_cfg->g_pass = VPX_RC_LAST_PASS;
234      if (!stats_open_file(&app_input->rc_stats, fpf_file_name, 1)) {
235        fatal("Failed to open statistics store");
236      }
237      enc_cfg->rc_twopass_stats_in = stats_get(&app_input->rc_stats);
238    }
239    app_input->passes = passes;
240    app_input->pass = pass;
241  }
242
243  if (enc_cfg->rc_target_bitrate > 0) {
244    if (min_bitrate > 0) {
245      enc_cfg->rc_2pass_vbr_minsection_pct =
246          min_bitrate * 100 / enc_cfg->rc_target_bitrate;
247    }
248    if (max_bitrate > 0) {
249      enc_cfg->rc_2pass_vbr_maxsection_pct =
250          max_bitrate * 100 / enc_cfg->rc_target_bitrate;
251    }
252  }
253
254  // Check for unrecognized options
255  for (argi = argv; *argi; ++argi)
256    if (argi[0][0] == '-' && strlen(argi[0]) > 1)
257      die("Error: Unrecognized option %s\n", *argi);
258
259  if (argv[0] == NULL || argv[1] == 0) {
260    usage_exit();
261  }
262  app_input->input_filename = argv[0];
263  app_input->output_filename = argv[1];
264  free(argv);
265
266  if (enc_cfg->g_w < 16 || enc_cfg->g_w % 2 || enc_cfg->g_h < 16 ||
267      enc_cfg->g_h % 2)
268    die("Invalid resolution: %d x %d\n", enc_cfg->g_w, enc_cfg->g_h);
269
270  printf(
271      "Codec %s\nframes: %d, skip: %d\n"
272      "mode: %d, layers: %d\n"
273      "width %d, height: %d,\n"
274      "num: %d, den: %d, bitrate: %d,\n"
275      "gop size: %d\n",
276      vpx_codec_iface_name(vpx_codec_vp9_cx()), app_input->frames_to_code,
277      app_input->frames_to_skip, svc_ctx->encoding_mode,
278      svc_ctx->spatial_layers, enc_cfg->g_w, enc_cfg->g_h,
279      enc_cfg->g_timebase.num, enc_cfg->g_timebase.den,
280      enc_cfg->rc_target_bitrate, enc_cfg->kf_max_dist);
281}
282
283int main(int argc, const char **argv) {
284  AppInput app_input = {0};
285  VpxVideoWriter *writer = NULL;
286  VpxVideoInfo info = {0};
287  vpx_codec_ctx_t codec;
288  vpx_codec_enc_cfg_t enc_cfg;
289  SvcContext svc_ctx;
290  uint32_t i;
291  uint32_t frame_cnt = 0;
292  vpx_image_t raw;
293  vpx_codec_err_t res;
294  int pts = 0;            /* PTS starts at 0 */
295  int frame_duration = 1; /* 1 timebase tick per frame */
296  FILE *infile = NULL;
297  int end_of_stream = 0;
298
299  memset(&svc_ctx, 0, sizeof(svc_ctx));
300  svc_ctx.log_print = 1;
301  exec_name = argv[0];
302  parse_command_line(argc, argv, &app_input, &svc_ctx, &enc_cfg);
303
304  // Allocate image buffer
305  if (!vpx_img_alloc(&raw, VPX_IMG_FMT_I420, enc_cfg.g_w, enc_cfg.g_h, 32))
306    die("Failed to allocate image %dx%d\n", enc_cfg.g_w, enc_cfg.g_h);
307
308  if (!(infile = fopen(app_input.input_filename, "rb")))
309    die("Failed to open %s for reading\n", app_input.input_filename);
310
311  // Initialize codec
312  if (vpx_svc_init(&svc_ctx, &codec, vpx_codec_vp9_cx(), &enc_cfg) !=
313      VPX_CODEC_OK)
314    die("Failed to initialize encoder\n");
315
316  info.codec_fourcc = VP9_FOURCC;
317  info.time_base.numerator = enc_cfg.g_timebase.num;
318  info.time_base.denominator = enc_cfg.g_timebase.den;
319  if (vpx_svc_get_layer_resolution(&svc_ctx, svc_ctx.spatial_layers - 1,
320                                   (unsigned int *)&info.frame_width,
321                                   (unsigned int *)&info.frame_height) !=
322      VPX_CODEC_OK) {
323    die("Failed to get output resolution");
324  }
325
326  if (!(app_input.passes == 2 && app_input.pass == 1)) {
327    // We don't save the bitstream for the 1st pass on two pass rate control
328    writer = vpx_video_writer_open(app_input.output_filename, kContainerIVF,
329                                   &info);
330    if (!writer)
331      die("Failed to open %s for writing\n", app_input.output_filename);
332  }
333
334  // skip initial frames
335  for (i = 0; i < app_input.frames_to_skip; ++i)
336    vpx_img_read(&raw, infile);
337
338  // Encode frames
339  while (!end_of_stream) {
340    if (frame_cnt >= app_input.frames_to_code || !vpx_img_read(&raw, infile)) {
341      // We need one extra vpx_svc_encode call at end of stream to flush
342      // encoder and get remaining data
343      end_of_stream = 1;
344    }
345
346    res = vpx_svc_encode(&svc_ctx, &codec, (end_of_stream ? NULL : &raw),
347                         pts, frame_duration, VPX_DL_REALTIME);
348    printf("%s", vpx_svc_get_message(&svc_ctx));
349    if (res != VPX_CODEC_OK) {
350      die_codec(&codec, "Failed to encode frame");
351    }
352    if (!(app_input.passes == 2 && app_input.pass == 1)) {
353      if (vpx_svc_get_frame_size(&svc_ctx) > 0) {
354        vpx_video_writer_write_frame(writer,
355                                     vpx_svc_get_buffer(&svc_ctx),
356                                     vpx_svc_get_frame_size(&svc_ctx),
357                                     pts);
358      }
359    }
360    if (vpx_svc_get_rc_stats_buffer_size(&svc_ctx) > 0) {
361      stats_write(&app_input.rc_stats,
362                  vpx_svc_get_rc_stats_buffer(&svc_ctx),
363                  vpx_svc_get_rc_stats_buffer_size(&svc_ctx));
364    }
365    if (!end_of_stream) {
366      ++frame_cnt;
367      pts += frame_duration;
368    }
369  }
370
371  printf("Processed %d frames\n", frame_cnt);
372
373  fclose(infile);
374  if (vpx_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec");
375
376  if (app_input.passes == 2)
377    stats_close(&app_input.rc_stats, 1);
378
379  if (writer) {
380    vpx_video_writer_close(writer);
381  }
382
383  vpx_img_free(&raw);
384
385  // display average size, psnr
386  printf("%s", vpx_svc_dump_statistics(&svc_ctx));
387
388  vpx_svc_release(&svc_ctx);
389
390  return EXIT_SUCCESS;
391}
392