WebM Codec SDK
vp8_multi_resolution_encoder
1 /*
2  * Copyright (c) 2010 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 multi-resolution encoding in VP8.
13  * High-resolution input video is down-sampled to lower-resolutions. The
14  * encoder then encodes the video and outputs multiple bitstreams with
15  * different resolutions.
16  *
17  * This test also allows for settings temporal layers for each spatial layer.
18  * Different number of temporal layers per spatial stream may be used.
19  * Currently up to 3 temporal layers per spatial stream (encoder) are supported
20  * in this test.
21  */
22 
23 #include "./vpx_config.h"
24 
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <stdarg.h>
28 #include <string.h>
29 #include <math.h>
30 #include <assert.h>
31 #include <sys/time.h>
32 #include "vpx_ports/vpx_timer.h"
33 #include "vpx/vpx_encoder.h"
34 #include "vpx/vp8cx.h"
35 #include "vpx_ports/mem_ops.h"
36 #include "../tools_common.h"
37 #define interface (vpx_codec_vp8_cx())
38 #define fourcc 0x30385056
39 
40 void usage_exit(void) {
41  exit(EXIT_FAILURE);
42 }
43 
44 /*
45  * The input video frame is downsampled several times to generate a multi-level
46  * hierarchical structure. NUM_ENCODERS is defined as the number of encoding
47  * levels required. For example, if the size of input video is 1280x720,
48  * NUM_ENCODERS is 3, and down-sampling factor is 2, the encoder outputs 3
49  * bitstreams with resolution of 1280x720(level 0), 640x360(level 1), and
50  * 320x180(level 2) respectively.
51  */
52 
53 /* Number of encoders (spatial resolutions) used in this test. */
54 #define NUM_ENCODERS 3
55 
56 /* Maximum number of temporal layers allowed for this test. */
57 #define MAX_NUM_TEMPORAL_LAYERS 3
58 
59 /* This example uses the scaler function in libyuv. */
60 #include "third_party/libyuv/include/libyuv/basic_types.h"
61 #include "third_party/libyuv/include/libyuv/scale.h"
62 #include "third_party/libyuv/include/libyuv/cpu_id.h"
63 
64 int (*read_frame_p)(FILE *f, vpx_image_t *img);
65 
66 static int read_frame(FILE *f, vpx_image_t *img) {
67  size_t nbytes, to_read;
68  int res = 1;
69 
70  to_read = img->w*img->h*3/2;
71  nbytes = fread(img->planes[0], 1, to_read, f);
72  if(nbytes != to_read) {
73  res = 0;
74  if(nbytes > 0)
75  printf("Warning: Read partial frame. Check your width & height!\n");
76  }
77  return res;
78 }
79 
80 static int read_frame_by_row(FILE *f, vpx_image_t *img) {
81  size_t nbytes, to_read;
82  int res = 1;
83  int plane;
84 
85  for (plane = 0; plane < 3; plane++)
86  {
87  unsigned char *ptr;
88  int w = (plane ? (1 + img->d_w) / 2 : img->d_w);
89  int h = (plane ? (1 + img->d_h) / 2 : img->d_h);
90  int r;
91 
92  /* Determine the correct plane based on the image format. The for-loop
93  * always counts in Y,U,V order, but this may not match the order of
94  * the data on disk.
95  */
96  switch (plane)
97  {
98  case 1:
99  ptr = img->planes[img->fmt==VPX_IMG_FMT_YV12? VPX_PLANE_V : VPX_PLANE_U];
100  break;
101  case 2:
102  ptr = img->planes[img->fmt==VPX_IMG_FMT_YV12?VPX_PLANE_U : VPX_PLANE_V];
103  break;
104  default:
105  ptr = img->planes[plane];
106  }
107 
108  for (r = 0; r < h; r++)
109  {
110  to_read = w;
111 
112  nbytes = fread(ptr, 1, to_read, f);
113  if(nbytes != to_read) {
114  res = 0;
115  if(nbytes > 0)
116  printf("Warning: Read partial frame. Check your width & height!\n");
117  break;
118  }
119 
120  ptr += img->stride[plane];
121  }
122  if (!res)
123  break;
124  }
125 
126  return res;
127 }
128 
129 static void write_ivf_file_header(FILE *outfile,
130  const vpx_codec_enc_cfg_t *cfg,
131  int frame_cnt) {
132  char header[32];
133 
134  if(cfg->g_pass != VPX_RC_ONE_PASS && cfg->g_pass != VPX_RC_LAST_PASS)
135  return;
136  header[0] = 'D';
137  header[1] = 'K';
138  header[2] = 'I';
139  header[3] = 'F';
140  mem_put_le16(header+4, 0); /* version */
141  mem_put_le16(header+6, 32); /* headersize */
142  mem_put_le32(header+8, fourcc); /* headersize */
143  mem_put_le16(header+12, cfg->g_w); /* width */
144  mem_put_le16(header+14, cfg->g_h); /* height */
145  mem_put_le32(header+16, cfg->g_timebase.den); /* rate */
146  mem_put_le32(header+20, cfg->g_timebase.num); /* scale */
147  mem_put_le32(header+24, frame_cnt); /* length */
148  mem_put_le32(header+28, 0); /* unused */
149 
150  (void) fwrite(header, 1, 32, outfile);
151 }
152 
153 static void write_ivf_frame_header(FILE *outfile,
154  const vpx_codec_cx_pkt_t *pkt)
155 {
156  char header[12];
157  vpx_codec_pts_t pts;
158 
159  if(pkt->kind != VPX_CODEC_CX_FRAME_PKT)
160  return;
161 
162  pts = pkt->data.frame.pts;
163  mem_put_le32(header, pkt->data.frame.sz);
164  mem_put_le32(header+4, pts&0xFFFFFFFF);
165  mem_put_le32(header+8, pts >> 32);
166 
167  (void) fwrite(header, 1, 12, outfile);
168 }
169 
170 /* Temporal scaling parameters */
171 /* This sets all the temporal layer parameters given |num_temporal_layers|,
172  * including the target bit allocation across temporal layers. Bit allocation
173  * parameters will be passed in as user parameters in another version.
174  */
175 static void set_temporal_layer_pattern(int num_temporal_layers,
176  vpx_codec_enc_cfg_t *cfg,
177  int bitrate,
178  int *layer_flags)
179 {
180  assert(num_temporal_layers <= MAX_NUM_TEMPORAL_LAYERS);
181  switch (num_temporal_layers)
182  {
183  case 1:
184  {
185  /* 1-layer */
186  cfg->ts_number_layers = 1;
187  cfg->ts_periodicity = 1;
188  cfg->ts_rate_decimator[0] = 1;
189  cfg->ts_layer_id[0] = 0;
190  cfg->ts_target_bitrate[0] = bitrate;
191 
192  // Update L only.
193  layer_flags[0] = VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF;
194  break;
195  }
196 
197  case 2:
198  {
199  /* 2-layers, with sync point at first frame of layer 1. */
200  cfg->ts_number_layers = 2;
201  cfg->ts_periodicity = 2;
202  cfg->ts_rate_decimator[0] = 2;
203  cfg->ts_rate_decimator[1] = 1;
204  cfg->ts_layer_id[0] = 0;
205  cfg->ts_layer_id[1] = 1;
206  // Use 60/40 bit allocation as example.
207  cfg->ts_target_bitrate[0] = 0.6f * bitrate;
208  cfg->ts_target_bitrate[1] = bitrate;
209 
210  /* 0=L, 1=GF */
211  // ARF is used as predictor for all frames, and is only updated on
212  // key frame. Sync point every 8 frames.
213 
214  // Layer 0: predict from L and ARF, update L and G.
215  layer_flags[0] = VP8_EFLAG_NO_REF_GF |
217 
218  // Layer 1: sync point: predict from L and ARF, and update G.
219  layer_flags[1] = VP8_EFLAG_NO_REF_GF |
222 
223  // Layer 0, predict from L and ARF, update L.
224  layer_flags[2] = VP8_EFLAG_NO_REF_GF |
227 
228  // Layer 1: predict from L, G and ARF, and update G.
229  layer_flags[3] = VP8_EFLAG_NO_UPD_ARF |
232 
233  // Layer 0
234  layer_flags[4] = layer_flags[2];
235 
236  // Layer 1
237  layer_flags[5] = layer_flags[3];
238 
239  // Layer 0
240  layer_flags[6] = layer_flags[4];
241 
242  // Layer 1
243  layer_flags[7] = layer_flags[5];
244  break;
245  }
246 
247  case 3:
248  default:
249  {
250  // 3-layers structure where ARF is used as predictor for all frames,
251  // and is only updated on key frame.
252  // Sync points for layer 1 and 2 every 8 frames.
253  cfg->ts_number_layers = 3;
254  cfg->ts_periodicity = 4;
255  cfg->ts_rate_decimator[0] = 4;
256  cfg->ts_rate_decimator[1] = 2;
257  cfg->ts_rate_decimator[2] = 1;
258  cfg->ts_layer_id[0] = 0;
259  cfg->ts_layer_id[1] = 2;
260  cfg->ts_layer_id[2] = 1;
261  cfg->ts_layer_id[3] = 2;
262  // Use 40/20/40 bit allocation as example.
263  cfg->ts_target_bitrate[0] = 0.4f * bitrate;
264  cfg->ts_target_bitrate[1] = 0.6f * bitrate;
265  cfg->ts_target_bitrate[2] = bitrate;
266 
267  /* 0=L, 1=GF, 2=ARF */
268 
269  // Layer 0: predict from L and ARF; update L and G.
270  layer_flags[0] = VP8_EFLAG_NO_UPD_ARF |
272 
273  // Layer 2: sync point: predict from L and ARF; update none.
274  layer_flags[1] = VP8_EFLAG_NO_REF_GF |
279 
280  // Layer 1: sync point: predict from L and ARF; update G.
281  layer_flags[2] = VP8_EFLAG_NO_REF_GF |
284 
285  // Layer 2: predict from L, G, ARF; update none.
286  layer_flags[3] = VP8_EFLAG_NO_UPD_GF |
290 
291  // Layer 0: predict from L and ARF; update L.
292  layer_flags[4] = VP8_EFLAG_NO_UPD_GF |
295 
296  // Layer 2: predict from L, G, ARF; update none.
297  layer_flags[5] = layer_flags[3];
298 
299  // Layer 1: predict from L, G, ARF; update G.
300  layer_flags[6] = VP8_EFLAG_NO_UPD_ARF |
302 
303  // Layer 2: predict from L, G, ARF; update none.
304  layer_flags[7] = layer_flags[3];
305  break;
306  }
307  }
308 }
309 
310 /* The periodicity of the pattern given the number of temporal layers. */
311 static int periodicity_to_num_layers[MAX_NUM_TEMPORAL_LAYERS] = {1, 8, 8};
312 
313 int main(int argc, char **argv)
314 {
315  FILE *infile, *outfile[NUM_ENCODERS];
316  FILE *downsampled_input[NUM_ENCODERS - 1];
317  char filename[50];
318  vpx_codec_ctx_t codec[NUM_ENCODERS];
319  vpx_codec_enc_cfg_t cfg[NUM_ENCODERS];
320  int frame_cnt = 0;
321  vpx_image_t raw[NUM_ENCODERS];
322  vpx_codec_err_t res[NUM_ENCODERS];
323 
324  int i;
325  long width;
326  long height;
327  int length_frame;
328  int frame_avail;
329  int got_data;
330  int flags = 0;
331  int layer_id = 0;
332 
333  int layer_flags[VPX_TS_MAX_PERIODICITY * NUM_ENCODERS]
334  = {0};
335  int flag_periodicity;
336 
337  /*Currently, only realtime mode is supported in multi-resolution encoding.*/
338  int arg_deadline = VPX_DL_REALTIME;
339 
340  /* Set show_psnr to 1/0 to show/not show PSNR. Choose show_psnr=0 if you
341  don't need to know PSNR, which will skip PSNR calculation and save
342  encoding time. */
343  int show_psnr = 0;
344  int key_frame_insert = 0;
345  uint64_t psnr_sse_total[NUM_ENCODERS] = {0};
346  uint64_t psnr_samples_total[NUM_ENCODERS] = {0};
347  double psnr_totals[NUM_ENCODERS][4] = {{0,0}};
348  int psnr_count[NUM_ENCODERS] = {0};
349 
350  int64_t cx_time = 0;
351 
352  /* Set the required target bitrates for each resolution level.
353  * If target bitrate for highest-resolution level is set to 0,
354  * (i.e. target_bitrate[0]=0), we skip encoding at that level.
355  */
356  unsigned int target_bitrate[NUM_ENCODERS]={1000, 500, 100};
357 
358  /* Enter the frame rate of the input video */
359  int framerate = 30;
360 
361  /* Set down-sampling factor for each resolution level.
362  dsf[0] controls down sampling from level 0 to level 1;
363  dsf[1] controls down sampling from level 1 to level 2;
364  dsf[2] is not used. */
365  vpx_rational_t dsf[NUM_ENCODERS] = {{2, 1}, {2, 1}, {1, 1}};
366 
367  /* Set the number of temporal layers for each encoder/resolution level,
368  * starting from highest resoln down to lowest resoln. */
369  unsigned int num_temporal_layers[NUM_ENCODERS] = {3, 3, 3};
370 
371  if(argc!= (7 + 3 * NUM_ENCODERS))
372  die("Usage: %s <width> <height> <frame_rate> <infile> <outfile(s)> "
373  "<rate_encoder(s)> <temporal_layer(s)> <key_frame_insert> <output psnr?> \n",
374  argv[0]);
375 
376  printf("Using %s\n",vpx_codec_iface_name(interface));
377 
378  width = strtol(argv[1], NULL, 0);
379  height = strtol(argv[2], NULL, 0);
380  framerate = strtol(argv[3], NULL, 0);
381 
382  if(width < 16 || width%2 || height <16 || height%2)
383  die("Invalid resolution: %ldx%ld", width, height);
384 
385  /* Open input video file for encoding */
386  if(!(infile = fopen(argv[4], "rb")))
387  die("Failed to open %s for reading", argv[4]);
388 
389  /* Open output file for each encoder to output bitstreams */
390  for (i=0; i< NUM_ENCODERS; i++)
391  {
392  if(!target_bitrate[i])
393  {
394  outfile[i] = NULL;
395  continue;
396  }
397 
398  if(!(outfile[i] = fopen(argv[i+5], "wb")))
399  die("Failed to open %s for writing", argv[i+4]);
400  }
401 
402  // Bitrates per spatial layer: overwrite default rates above.
403  for (i=0; i< NUM_ENCODERS; i++)
404  {
405  target_bitrate[i] = strtol(argv[NUM_ENCODERS + 5 + i], NULL, 0);
406  }
407 
408  // Temporal layers per spatial layers: overwrite default settings above.
409  for (i=0; i< NUM_ENCODERS; i++)
410  {
411  num_temporal_layers[i] = strtol(argv[2 * NUM_ENCODERS + 5 + i], NULL, 0);
412  if (num_temporal_layers[i] < 1 || num_temporal_layers[i] > 3)
413  die("Invalid temporal layers: %d, Must be 1, 2, or 3. \n",
414  num_temporal_layers);
415  }
416 
417  /* Open file to write out each spatially downsampled input stream. */
418  for (i=0; i< NUM_ENCODERS - 1; i++)
419  {
420  // Highest resoln is encoder 0.
421  if (sprintf(filename,"ds%d.yuv",NUM_ENCODERS - i) < 0)
422  {
423  return EXIT_FAILURE;
424  }
425  downsampled_input[i] = fopen(filename,"wb");
426  }
427 
428  key_frame_insert = strtol(argv[3 * NUM_ENCODERS + 5], NULL, 0);
429 
430  show_psnr = strtol(argv[3 * NUM_ENCODERS + 6], NULL, 0);
431 
432 
433  /* Populate default encoder configuration */
434  for (i=0; i< NUM_ENCODERS; i++)
435  {
436  res[i] = vpx_codec_enc_config_default(interface, &cfg[i], 0);
437  if(res[i]) {
438  printf("Failed to get config: %s\n", vpx_codec_err_to_string(res[i]));
439  return EXIT_FAILURE;
440  }
441  }
442 
443  /*
444  * Update the default configuration according to needs of the application.
445  */
446  /* Highest-resolution encoder settings */
447  cfg[0].g_w = width;
448  cfg[0].g_h = height;
449  cfg[0].rc_dropframe_thresh = 0;
450  cfg[0].rc_end_usage = VPX_CBR;
451  cfg[0].rc_resize_allowed = 0;
452  cfg[0].rc_min_quantizer = 2;
453  cfg[0].rc_max_quantizer = 56;
454  cfg[0].rc_undershoot_pct = 100;
455  cfg[0].rc_overshoot_pct = 15;
456  cfg[0].rc_buf_initial_sz = 500;
457  cfg[0].rc_buf_optimal_sz = 600;
458  cfg[0].rc_buf_sz = 1000;
459  cfg[0].g_error_resilient = 1; /* Enable error resilient mode */
460  cfg[0].g_lag_in_frames = 0;
461 
462  /* Disable automatic keyframe placement */
463  /* Note: These 3 settings are copied to all levels. But, except the lowest
464  * resolution level, all other levels are set to VPX_KF_DISABLED internally.
465  */
466  cfg[0].kf_mode = VPX_KF_AUTO;
467  cfg[0].kf_min_dist = 3000;
468  cfg[0].kf_max_dist = 3000;
469 
470  cfg[0].rc_target_bitrate = target_bitrate[0]; /* Set target bitrate */
471  cfg[0].g_timebase.num = 1; /* Set fps */
472  cfg[0].g_timebase.den = framerate;
473 
474  /* Other-resolution encoder settings */
475  for (i=1; i< NUM_ENCODERS; i++)
476  {
477  memcpy(&cfg[i], &cfg[0], sizeof(vpx_codec_enc_cfg_t));
478 
479  cfg[i].rc_target_bitrate = target_bitrate[i];
480 
481  /* Note: Width & height of other-resolution encoders are calculated
482  * from the highest-resolution encoder's size and the corresponding
483  * down_sampling_factor.
484  */
485  {
486  unsigned int iw = cfg[i-1].g_w*dsf[i-1].den + dsf[i-1].num - 1;
487  unsigned int ih = cfg[i-1].g_h*dsf[i-1].den + dsf[i-1].num - 1;
488  cfg[i].g_w = iw/dsf[i-1].num;
489  cfg[i].g_h = ih/dsf[i-1].num;
490  }
491 
492  /* Make width & height to be multiplier of 2. */
493  // Should support odd size ???
494  if((cfg[i].g_w)%2)cfg[i].g_w++;
495  if((cfg[i].g_h)%2)cfg[i].g_h++;
496  }
497 
498 
499  // Set the number of threads per encode/spatial layer.
500  // (1, 1, 1) means no encoder threading.
501  cfg[0].g_threads = 2;
502  cfg[1].g_threads = 1;
503  cfg[2].g_threads = 1;
504 
505  /* Allocate image for each encoder */
506  for (i=0; i< NUM_ENCODERS; i++)
507  if(!vpx_img_alloc(&raw[i], VPX_IMG_FMT_I420, cfg[i].g_w, cfg[i].g_h, 32))
508  die("Failed to allocate image", cfg[i].g_w, cfg[i].g_h);
509 
510  if (raw[0].stride[VPX_PLANE_Y] == raw[0].d_w)
511  read_frame_p = read_frame;
512  else
513  read_frame_p = read_frame_by_row;
514 
515  for (i=0; i< NUM_ENCODERS; i++)
516  if(outfile[i])
517  write_ivf_file_header(outfile[i], &cfg[i], 0);
518 
519  /* Temporal layers settings */
520  for ( i=0; i<NUM_ENCODERS; i++)
521  {
522  set_temporal_layer_pattern(num_temporal_layers[i],
523  &cfg[i],
524  cfg[i].rc_target_bitrate,
525  &layer_flags[i * VPX_TS_MAX_PERIODICITY]);
526  }
527 
528  /* Initialize multi-encoder */
529  if(vpx_codec_enc_init_multi(&codec[0], interface, &cfg[0], NUM_ENCODERS,
530  (show_psnr ? VPX_CODEC_USE_PSNR : 0), &dsf[0]))
531  die_codec(&codec[0], "Failed to initialize encoder");
532 
533  /* The extra encoding configuration parameters can be set as follows. */
534  /* Set encoding speed */
535  for ( i=0; i<NUM_ENCODERS; i++)
536  {
537  int speed = -6;
538  /* Lower speed for the lowest resolution. */
539  if (i == NUM_ENCODERS - 1) speed = -4;
540  if(vpx_codec_control(&codec[i], VP8E_SET_CPUUSED, speed))
541  die_codec(&codec[i], "Failed to set cpu_used");
542  }
543 
544  /* Set static threshold = 1 for all encoders */
545  for ( i=0; i<NUM_ENCODERS; i++)
546  {
548  die_codec(&codec[i], "Failed to set static threshold");
549  }
550 
551  /* Set NOISE_SENSITIVITY to do TEMPORAL_DENOISING */
552  /* Enable denoising for the highest-resolution encoder. */
554  die_codec(&codec[0], "Failed to set noise_sensitivity");
555  for ( i=1; i< NUM_ENCODERS; i++)
556  {
558  die_codec(&codec[i], "Failed to set noise_sensitivity");
559  }
560 
561  /* Set the number of token partitions */
562  for ( i=0; i<NUM_ENCODERS; i++)
563  {
565  die_codec(&codec[i], "Failed to set static threshold");
566  }
567 
568  /* Set the max intra target bitrate */
569  for ( i=0; i<NUM_ENCODERS; i++)
570  {
571  unsigned int max_intra_size_pct =
572  (int)(((double)cfg[0].rc_buf_optimal_sz * 0.5) * framerate / 10);
574  max_intra_size_pct))
575  die_codec(&codec[i], "Failed to set static threshold");
576  //printf("%d %d \n",i,max_intra_size_pct);
577  }
578 
579  frame_avail = 1;
580  got_data = 0;
581 
582  while(frame_avail || got_data)
583  {
584  struct vpx_usec_timer timer;
585  vpx_codec_iter_t iter[NUM_ENCODERS]={NULL};
586  const vpx_codec_cx_pkt_t *pkt[NUM_ENCODERS];
587 
588  flags = 0;
589  frame_avail = read_frame_p(infile, &raw[0]);
590 
591  if(frame_avail)
592  {
593  for ( i=1; i<NUM_ENCODERS; i++)
594  {
595  /*Scale the image down a number of times by downsampling factor*/
596  /* FilterMode 1 or 2 give better psnr than FilterMode 0. */
597  I420Scale(raw[i-1].planes[VPX_PLANE_Y], raw[i-1].stride[VPX_PLANE_Y],
598  raw[i-1].planes[VPX_PLANE_U], raw[i-1].stride[VPX_PLANE_U],
599  raw[i-1].planes[VPX_PLANE_V], raw[i-1].stride[VPX_PLANE_V],
600  raw[i-1].d_w, raw[i-1].d_h,
601  raw[i].planes[VPX_PLANE_Y], raw[i].stride[VPX_PLANE_Y],
602  raw[i].planes[VPX_PLANE_U], raw[i].stride[VPX_PLANE_U],
603  raw[i].planes[VPX_PLANE_V], raw[i].stride[VPX_PLANE_V],
604  raw[i].d_w, raw[i].d_h, 1);
605  /* Write out down-sampled input. */
606  length_frame = cfg[i].g_w * cfg[i].g_h *3/2;
607  if (fwrite(raw[i].planes[0], 1, length_frame,
608  downsampled_input[NUM_ENCODERS - i - 1]) !=
609  length_frame)
610  {
611  return EXIT_FAILURE;
612  }
613  }
614  }
615 
616  /* Set the flags (reference and update) for all the encoders.*/
617  for ( i=0; i<NUM_ENCODERS; i++)
618  {
619  layer_id = cfg[i].ts_layer_id[frame_cnt % cfg[i].ts_periodicity];
620  flags = 0;
621  flag_periodicity = periodicity_to_num_layers
622  [num_temporal_layers[i] - 1];
623  flags = layer_flags[i * VPX_TS_MAX_PERIODICITY +
624  frame_cnt % flag_periodicity];
625  // Key frame flag for first frame.
626  if (frame_cnt == 0)
627  {
628  flags |= VPX_EFLAG_FORCE_KF;
629  }
630  if (frame_cnt > 0 && frame_cnt == key_frame_insert)
631  {
632  flags = VPX_EFLAG_FORCE_KF;
633  }
634 
635  vpx_codec_control(&codec[i], VP8E_SET_FRAME_FLAGS, flags);
636  vpx_codec_control(&codec[i], VP8E_SET_TEMPORAL_LAYER_ID, layer_id);
637  }
638 
639  /* Encode each frame at multi-levels */
640  /* Note the flags must be set to 0 in the encode call if they are set
641  for each frame with the vpx_codec_control(), as done above. */
642  vpx_usec_timer_start(&timer);
643  if(vpx_codec_encode(&codec[0], frame_avail? &raw[0] : NULL,
644  frame_cnt, 1, 0, arg_deadline))
645  {
646  die_codec(&codec[0], "Failed to encode frame");
647  }
648  vpx_usec_timer_mark(&timer);
649  cx_time += vpx_usec_timer_elapsed(&timer);
650 
651  for (i=NUM_ENCODERS-1; i>=0 ; i--)
652  {
653  got_data = 0;
654  while( (pkt[i] = vpx_codec_get_cx_data(&codec[i], &iter[i])) )
655  {
656  got_data = 1;
657  switch(pkt[i]->kind) {
659  write_ivf_frame_header(outfile[i], pkt[i]);
660  (void) fwrite(pkt[i]->data.frame.buf, 1,
661  pkt[i]->data.frame.sz, outfile[i]);
662  break;
663  case VPX_CODEC_PSNR_PKT:
664  if (show_psnr)
665  {
666  int j;
667 
668  psnr_sse_total[i] += pkt[i]->data.psnr.sse[0];
669  psnr_samples_total[i] += pkt[i]->data.psnr.samples[0];
670  for (j = 0; j < 4; j++)
671  {
672  psnr_totals[i][j] += pkt[i]->data.psnr.psnr[j];
673  }
674  psnr_count[i]++;
675  }
676 
677  break;
678  default:
679  break;
680  }
681  printf(pkt[i]->kind == VPX_CODEC_CX_FRAME_PKT
682  && (pkt[i]->data.frame.flags & VPX_FRAME_IS_KEY)? "K":"");
683  fflush(stdout);
684  }
685  }
686  frame_cnt++;
687  }
688  printf("\n");
689  printf("Frame cnt and encoding time/FPS stats for encoding: %d %f %f \n",
690  frame_cnt,
691  1000 * (float)cx_time / (double)(frame_cnt * 1000000),
692  1000000 * (double)frame_cnt / (double)cx_time);
693 
694  fclose(infile);
695 
696  printf("Processed %ld frames.\n",(long int)frame_cnt-1);
697  for (i=0; i< NUM_ENCODERS; i++)
698  {
699  /* Calculate PSNR and print it out */
700  if ( (show_psnr) && (psnr_count[i]>0) )
701  {
702  int j;
703  double ovpsnr = sse_to_psnr(psnr_samples_total[i], 255.0,
704  psnr_sse_total[i]);
705 
706  fprintf(stderr, "\n ENC%d PSNR (Overall/Avg/Y/U/V)", i);
707 
708  fprintf(stderr, " %.3lf", ovpsnr);
709  for (j = 0; j < 4; j++)
710  {
711  fprintf(stderr, " %.3lf", psnr_totals[i][j]/psnr_count[i]);
712  }
713  }
714 
715  if(vpx_codec_destroy(&codec[i]))
716  die_codec(&codec[i], "Failed to destroy codec");
717 
718  vpx_img_free(&raw[i]);
719 
720  if(!outfile[i])
721  continue;
722 
723  /* Try to rewrite the file header with the actual frame count */
724  if(!fseek(outfile[i], 0, SEEK_SET))
725  write_ivf_file_header(outfile[i], &cfg[i], frame_cnt-1);
726  fclose(outfile[i]);
727  }
728  printf("\n");
729 
730  return EXIT_SUCCESS;
731 }
Rational Number.
Definition: vpx_encoder.h:259
unsigned int rc_buf_initial_sz
Decoder Buffer Initial Size.
Definition: vpx_encoder.h:610
unsigned int ts_number_layers
Number of temporal coding layers.
Definition: vpx_encoder.h:715
Codec control function to set encoder internal speed settings.
Definition: vp8cx.h:164
#define VP8_EFLAG_NO_UPD_GF
Don't update the golden frame.
Definition: vp8cx.h:92
Image Descriptor.
Definition: vpx_image.h:88
Describes the encoder algorithm interface to applications.
const char * vpx_codec_iface_name(vpx_codec_iface_t *iface)
Return the name for a given interface.
Definition: vpx_image.h:55
const char * vpx_codec_err_to_string(vpx_codec_err_t err)
Convert error number to printable string.
struct vpx_rational g_timebase
Stream timebase units.
Definition: vpx_encoder.h:397
Definition: vpx_encoder.h:276
unsigned int rc_buf_sz
Decoder Buffer Size.
Definition: vpx_encoder.h:600
#define VP8_EFLAG_NO_REF_GF
Don't reference the golden frame.
Definition: vp8cx.h:67
Codec control function to set reference and update frame flags.
Definition: vp8cx.h:266
enum vpx_kf_mode kf_mode
Keyframe placement mode.
Definition: vpx_encoder.h:665
int den
Definition: vpx_encoder.h:261
vpx_codec_err_t vpx_codec_encode(vpx_codec_ctx_t *ctx, const vpx_image_t *img, vpx_codec_pts_t pts, unsigned long duration, vpx_enc_frame_flags_t flags, unsigned long deadline)
Encode a frame.
unsigned int rc_max_quantizer
Maximum (Worst Quality) Quantizer.
Definition: vpx_encoder.h:552
unsigned int rc_min_quantizer
Minimum (Best Quality) Quantizer.
Definition: vpx_encoder.h:541
unsigned int kf_max_dist
Keyframe maximum interval.
Definition: vpx_encoder.h:685
unsigned int g_lag_in_frames
Allow lagged encoding.
Definition: vpx_encoder.h:429
Encoder configuration structure.
Definition: vpx_encoder.h:314
Definition: vpx_encoder.h:179
Definition: vpx_encoder.h:292
Codec control function to set Max data rate for Intra frames.
Definition: vp8cx.h:260
Encoder output packet.
Definition: vpx_encoder.h:195
unsigned int rc_overshoot_pct
Rate control adaptation overshoot control.
Definition: vpx_encoder.h:583
unsigned int ts_rate_decimator[5]
Frame rate decimation factor for each temporal layer.
Definition: vpx_encoder.h:729
unsigned int rc_buf_optimal_sz
Decoder Buffer Optimal Size.
Definition: vpx_encoder.h:620
#define VPX_PLANE_V
Definition: vpx_image.h:114
unsigned int kf_min_dist
Keyframe minimum interval.
Definition: vpx_encoder.h:675
Definition: vpx_encoder.h:269
unsigned int ts_layer_id[16]
Template defining the membership of frames to temporal layers.
Definition: vpx_encoder.h:747
struct vpx_codec_cx_pkt::@1::@2 frame
vpx_image_t * vpx_img_alloc(vpx_image_t *img, vpx_img_fmt_t fmt, unsigned int d_w, unsigned int d_h, unsigned int align)
Open a descriptor, allocating storage for the underlying image.
Definition: vpx_image.h:56
unsigned int d_w
Definition: vpx_image.h:99
unsigned int g_w
Width of the frame.
Definition: vpx_encoder.h:357
unsigned int ts_target_bitrate[5]
Target bitrate for each temporal layer.
Definition: vpx_encoder.h:722
unsigned int rc_undershoot_pct
Rate control adaptation undershoot control.
Definition: vpx_encoder.h:570
unsigned int g_h
Height of the frame.
Definition: vpx_encoder.h:367
int stride[4]
Definition: vpx_image.h:117
enum vpx_codec_cx_pkt_kind kind
Definition: vpx_encoder.h:196
unsigned int rc_dropframe_thresh
Temporal resampling configuration, if supported by the codec.
Definition: vpx_encoder.h:452
Codec control function to set the temporal layer id.
Definition: vp8cx.h:307
#define VP8_EFLAG_NO_UPD_LAST
Don't update the last frame.
Definition: vp8cx.h:84
void vpx_img_free(vpx_image_t *img)
Close an image descriptor.
vpx_img_fmt_t fmt
Definition: vpx_image.h:89
unsigned char * planes[4]
Definition: vpx_image.h:116
Codec control function to set the number of token partitions.
Definition: vp8cx.h:197
unsigned int rc_target_bitrate
Target data rate.
Definition: vpx_encoder.h:525
#define VPX_DL_REALTIME
Definition: vpx_encoder.h:911
int num
Definition: vpx_encoder.h:260
control function to set noise sensitivity
Definition: vp8cx.h:179
enum vpx_enc_pass g_pass
Multi-pass Encoding Mode.
Definition: vpx_encoder.h:414
double psnr[4]
Definition: vpx_encoder.h:219
unsigned int g_threads
Maximum number of threads to use.
Definition: vpx_encoder.h:335
Provides definitions for using VP8 or VP9 encoder algorithm within the vpx Codec Interface.
#define VPX_PLANE_U
Definition: vpx_image.h:113
unsigned int rc_resize_allowed
Enable/disable spatial resampling, if supported by the codec.
Definition: vpx_encoder.h:462
unsigned int h
Definition: vpx_image.h:95
vpx_codec_err_t
Algorithm return codes.
Definition: vpx_codec.h:89
const vpx_codec_cx_pkt_t * vpx_codec_get_cx_data(vpx_codec_ctx_t *ctx, vpx_codec_iter_t *iter)
Encoded data iterator.
union vpx_codec_cx_pkt::@1 data
#define vpx_codec_enc_init_multi(ctx, iface, cfg, num_enc, flags, dsf)
Convenience macro for vpx_codec_enc_init_multi_ver()
Definition: vpx_encoder.h:850
int64_t vpx_codec_pts_t
Time Stamp Type.
Definition: vpx_encoder.h:119
vpx_codec_err_t vpx_codec_enc_config_default(vpx_codec_iface_t *iface, vpx_codec_enc_cfg_t *cfg, unsigned int reserved)
Get a default configuration.
#define VPX_TS_MAX_PERIODICITY
Definition: vpx_encoder.h:37
#define vpx_codec_control(ctx, id, data)
vpx_codec_control wrapper macro
Definition: vpx_codec.h:407
unsigned int ts_periodicity
Length of the sequence defining frame temporal layer membership.
Definition: vpx_encoder.h:738
vpx_codec_err_t vpx_codec_destroy(vpx_codec_ctx_t *ctx)
Destroy a codec instance.
unsigned int d_h
Definition: vpx_image.h:100
unsigned int w
Definition: vpx_image.h:94
Codec control function to set the threshold for MBs treated static.
Definition: vp8cx.h:191
#define VPX_FRAME_IS_KEY
Definition: vpx_encoder.h:130
#define VPX_EFLAG_FORCE_KF
Definition: vpx_encoder.h:305
const void * vpx_codec_iter_t
Iterator.
Definition: vpx_codec.h:188
Definition: vpx_encoder.h:176
vpx_codec_er_flags_t g_error_resilient
Enable error resilient modes.
Definition: vpx_encoder.h:406
#define VP8_EFLAG_NO_UPD_ARF
Don't update the alternate reference frame.
Definition: vp8cx.h:100
#define VP8_EFLAG_NO_UPD_ENTROPY
Disable entropy update.
Definition: vp8cx.h:124
enum vpx_rc_mode rc_end_usage
Rate control algorithm to use.
Definition: vpx_encoder.h:504
Definition: vpx_encoder.h:267
Codec context structure.
Definition: vpx_codec.h:199