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