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 arg_def_t skip_frames_arg =
32    ARG_DEF("s", "skip-frames", 1, "input frames to skip");
33static const arg_def_t frames_arg =
34    ARG_DEF("f", "frames", 1, "number of frames to encode");
35static const arg_def_t width_arg = ARG_DEF("w", "width", 1, "source width");
36static const arg_def_t height_arg = ARG_DEF("h", "height", 1, "source height");
37static const arg_def_t timebase_arg =
38    ARG_DEF("t", "timebase", 1, "timebase (num/den)");
39static const arg_def_t bitrate_arg = ARG_DEF(
40    "b", "target-bitrate", 1, "encoding bitrate, in kilobits per second");
41static const arg_def_t spatial_layers_arg =
42    ARG_DEF("sl", "spatial-layers", 1, "number of spatial SVC layers");
43static const arg_def_t temporal_layers_arg =
44    ARG_DEF("tl", "temporal-layers", 1, "number of temporal SVC layers");
45static const arg_def_t kf_dist_arg =
46    ARG_DEF("k", "kf-dist", 1, "number of frames between keyframes");
47static const arg_def_t scale_factors_arg =
48    ARG_DEF("r", "scale-factors", 1, "scale factors (lowest to highest layer)");
49static const arg_def_t passes_arg =
50    ARG_DEF("p", "passes", 1, "Number of passes (1/2)");
51static const arg_def_t pass_arg =
52    ARG_DEF(NULL, "pass", 1, "Pass to execute (1/2)");
53static const arg_def_t fpf_name_arg =
54    ARG_DEF(NULL, "fpf", 1, "First pass statistics file name");
55static const arg_def_t min_q_arg =
56    ARG_DEF(NULL, "min-q", 1, "Minimum quantizer");
57static const arg_def_t max_q_arg =
58    ARG_DEF(NULL, "max-q", 1, "Maximum quantizer");
59static const arg_def_t min_bitrate_arg =
60    ARG_DEF(NULL, "min-bitrate", 1, "Minimum bitrate");
61static const arg_def_t max_bitrate_arg =
62    ARG_DEF(NULL, "max-bitrate", 1, "Maximum bitrate");
63
64static 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
70};
71
72static const uint32_t default_frames_to_skip = 0;
73static const uint32_t default_frames_to_code = 60 * 60;
74static const uint32_t default_width = 1920;
75static const uint32_t default_height = 1080;
76static const uint32_t default_timebase_num = 1;
77static const uint32_t default_timebase_den = 60;
78static const uint32_t default_bitrate = 1000;
79static const uint32_t default_spatial_layers = 5;
80static const uint32_t default_temporal_layers = 1;
81static const uint32_t default_kf_dist = 100;
82
83typedef struct {
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;
89  stats_io_t rc_stats;
90  int passes;
91  int pass;
92} AppInput;
93
94static const char *exec_name;
95
96void usage_exit() {
97  fprintf(stderr, "Usage: %s <options> input_filename output_filename\n",
98          exec_name);
99  fprintf(stderr, "Options:\n");
100  arg_show_usage(stderr, svc_args);
101  exit(EXIT_FAILURE);
102}
103
104static 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};
108  char **argv = NULL;
109  char **argi = NULL;
110  char **argj = NULL;
111  vpx_codec_err_t res;
112  int passes = 0;
113  int pass = 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};
118
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;
123
124  // start with default encoder configuration
125  res = vpx_codec_enc_config_default(vpx_codec_vp9_cx(), enc_cfg, 0);
126  if (res) {
127    die("Failed to get config: %s\n", vpx_codec_err_to_string(res));
128  }
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;
138
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;
142
143  // process command line options
144  argv = argv_dup(argc - 1, argv_ + 1);
145  for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
146    arg.argv_step = 1;
147
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);
174      }
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);
179      }
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);
192    } else {
193      ++argj;
194    }
195  }
196
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);
200
201  if (passes == 0 || passes == 1) {
202    if (pass) {
203      fprintf(stderr, "pass is ignored since there's only one pass\n");
204    }
205    enc_cfg->g_pass = VPX_RC_ONE_PASS;
206  } else {
207    if (pass == 0) {
208      die("pass must be specified when passes is 2\n");
209    }
210
211    if (fpf_file_name == NULL) {
212      die("fpf must be specified when passes is 2\n");
213    }
214
215    if (pass == 1) {
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");
219      }
220    } else {
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");
224      }
225      enc_cfg->rc_twopass_stats_in = stats_get(&app_input->rc_stats);
226    }
227    app_input->passes = passes;
228    app_input->pass = pass;
229  }
230
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;
235    }
236    if (max_bitrate > 0) {
237      enc_cfg->rc_2pass_vbr_maxsection_pct =
238          max_bitrate * 100 / enc_cfg->rc_target_bitrate;
239    }
240  }
241
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);
246
247  if (argv[0] == NULL || argv[1] == 0) {
248    usage_exit();
249  }
250  app_input->input_filename = argv[0];
251  app_input->output_filename = argv[1];
252  free(argv);
253
254  if (enc_cfg->g_w < 16 || enc_cfg->g_w % 2 || enc_cfg->g_h < 16 ||
255      enc_cfg->g_h % 2)
256    die("Invalid resolution: %d x %d\n", enc_cfg->g_w, enc_cfg->g_h);
257
258  printf(
259      "Codec %s\nframes: %d, skip: %d\n"
260      "layers: %d\n"
261      "width %d, height: %d,\n"
262      "num: %d, den: %d, bitrate: %d,\n"
263      "gop size: %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);
269}
270
271int 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;
277  SvcContext svc_ctx;
278  uint32_t i;
279  uint32_t frame_cnt = 0;
280  vpx_image_t raw;
281  vpx_codec_err_t res;
282  int pts = 0;            /* PTS starts at 0 */
283  int frame_duration = 1; /* 1 timebase tick per frame */
284  FILE *infile = NULL;
285  int end_of_stream = 0;
286  int frames_received = 0;
287
288  memset(&svc_ctx, 0, sizeof(svc_ctx));
289  svc_ctx.log_print = 1;
290  exec_name = argv[0];
291  parse_command_line(argc, argv, &app_input, &svc_ctx, &enc_cfg);
292
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);
296
297  if (!(infile = fopen(app_input.input_filename, "rb")))
298    die("Failed to open %s for reading\n", app_input.input_filename);
299
300  // Initialize codec
301  if (vpx_svc_init(&svc_ctx, &codec, vpx_codec_vp9_cx(), &enc_cfg) !=
302      VPX_CODEC_OK)
303    die("Failed to initialize encoder\n");
304
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;
308
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,
312                                   &info);
313    if (!writer)
314      die("Failed to open %s for writing\n", app_input.output_filename);
315  }
316
317  // skip initial frames
318  for (i = 0; i < app_input.frames_to_skip; ++i)
319    vpx_img_read(&raw, infile);
320
321  // Encode frames
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
328      end_of_stream = 1;
329    }
330
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");
336    }
337
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);
346
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);
350          ++frames_received;
351          break;
352        }
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);
357          break;
358        }
359        default: {
360          break;
361        }
362      }
363    }
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