Take out VP9_ prefix from mips/msa macros
[aom.git] / examples / vp8_multi_resolution_encoder.c
blob2b032049c0bd5cfe7b332a7305d903742bc09fe4
1 /*
2 * Copyright (c) 2010 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.
9 */
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.
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.
23 #include "./vpx_config.h"
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 #if USE_POSIX_MMAP
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <sys/mman.h>
36 #include <fcntl.h>
37 #include <unistd.h>
38 #endif
39 #include "vpx_ports/vpx_timer.h"
40 #include "vpx/vpx_encoder.h"
41 #include "vpx/vp8cx.h"
42 #include "vpx_ports/mem_ops.h"
43 #include "../tools_common.h"
44 #define interface (vpx_codec_vp8_cx())
45 #define fourcc 0x30385056
47 void usage_exit(void) {
48 exit(EXIT_FAILURE);
52 * The input video frame is downsampled several times to generate a multi-level
53 * hierarchical structure. NUM_ENCODERS is defined as the number of encoding
54 * levels required. For example, if the size of input video is 1280x720,
55 * NUM_ENCODERS is 3, and down-sampling factor is 2, the encoder outputs 3
56 * bitstreams with resolution of 1280x720(level 0), 640x360(level 1), and
57 * 320x180(level 2) respectively.
60 /* Number of encoders (spatial resolutions) used in this test. */
61 #define NUM_ENCODERS 3
63 /* Maximum number of temporal layers allowed for this test. */
64 #define MAX_NUM_TEMPORAL_LAYERS 3
66 /* This example uses the scaler function in libyuv. */
67 #include "third_party/libyuv/include/libyuv/basic_types.h"
68 #include "third_party/libyuv/include/libyuv/scale.h"
69 #include "third_party/libyuv/include/libyuv/cpu_id.h"
71 int (*read_frame_p)(FILE *f, vpx_image_t *img);
73 static int read_frame(FILE *f, vpx_image_t *img) {
74 size_t nbytes, to_read;
75 int res = 1;
77 to_read = img->w*img->h*3/2;
78 nbytes = fread(img->planes[0], 1, to_read, f);
79 if(nbytes != to_read) {
80 res = 0;
81 if(nbytes > 0)
82 printf("Warning: Read partial frame. Check your width & height!\n");
84 return res;
87 static int read_frame_by_row(FILE *f, vpx_image_t *img) {
88 size_t nbytes, to_read;
89 int res = 1;
90 int plane;
92 for (plane = 0; plane < 3; plane++)
94 unsigned char *ptr;
95 int w = (plane ? (1 + img->d_w) / 2 : img->d_w);
96 int h = (plane ? (1 + img->d_h) / 2 : img->d_h);
97 int r;
99 /* Determine the correct plane based on the image format. The for-loop
100 * always counts in Y,U,V order, but this may not match the order of
101 * the data on disk.
103 switch (plane)
105 case 1:
106 ptr = img->planes[img->fmt==VPX_IMG_FMT_YV12? VPX_PLANE_V : VPX_PLANE_U];
107 break;
108 case 2:
109 ptr = img->planes[img->fmt==VPX_IMG_FMT_YV12?VPX_PLANE_U : VPX_PLANE_V];
110 break;
111 default:
112 ptr = img->planes[plane];
115 for (r = 0; r < h; r++)
117 to_read = w;
119 nbytes = fread(ptr, 1, to_read, f);
120 if(nbytes != to_read) {
121 res = 0;
122 if(nbytes > 0)
123 printf("Warning: Read partial frame. Check your width & height!\n");
124 break;
127 ptr += img->stride[plane];
129 if (!res)
130 break;
133 return res;
136 static void write_ivf_file_header(FILE *outfile,
137 const vpx_codec_enc_cfg_t *cfg,
138 int frame_cnt) {
139 char header[32];
141 if(cfg->g_pass != VPX_RC_ONE_PASS && cfg->g_pass != VPX_RC_LAST_PASS)
142 return;
143 header[0] = 'D';
144 header[1] = 'K';
145 header[2] = 'I';
146 header[3] = 'F';
147 mem_put_le16(header+4, 0); /* version */
148 mem_put_le16(header+6, 32); /* headersize */
149 mem_put_le32(header+8, fourcc); /* headersize */
150 mem_put_le16(header+12, cfg->g_w); /* width */
151 mem_put_le16(header+14, cfg->g_h); /* height */
152 mem_put_le32(header+16, cfg->g_timebase.den); /* rate */
153 mem_put_le32(header+20, cfg->g_timebase.num); /* scale */
154 mem_put_le32(header+24, frame_cnt); /* length */
155 mem_put_le32(header+28, 0); /* unused */
157 (void) fwrite(header, 1, 32, outfile);
160 static void write_ivf_frame_header(FILE *outfile,
161 const vpx_codec_cx_pkt_t *pkt)
163 char header[12];
164 vpx_codec_pts_t pts;
166 if(pkt->kind != VPX_CODEC_CX_FRAME_PKT)
167 return;
169 pts = pkt->data.frame.pts;
170 mem_put_le32(header, pkt->data.frame.sz);
171 mem_put_le32(header+4, pts&0xFFFFFFFF);
172 mem_put_le32(header+8, pts >> 32);
174 (void) fwrite(header, 1, 12, outfile);
177 /* Temporal scaling parameters */
178 /* This sets all the temporal layer parameters given |num_temporal_layers|,
179 * including the target bit allocation across temporal layers. Bit allocation
180 * parameters will be passed in as user parameters in another version.
182 static void set_temporal_layer_pattern(int num_temporal_layers,
183 vpx_codec_enc_cfg_t *cfg,
184 int bitrate,
185 int *layer_flags)
187 assert(num_temporal_layers <= MAX_NUM_TEMPORAL_LAYERS);
188 switch (num_temporal_layers)
190 case 1:
192 /* 1-layer */
193 cfg->ts_number_layers = 1;
194 cfg->ts_periodicity = 1;
195 cfg->ts_rate_decimator[0] = 1;
196 cfg->ts_layer_id[0] = 0;
197 cfg->ts_target_bitrate[0] = bitrate;
199 // Update L only.
200 layer_flags[0] = VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF;
201 break;
204 case 2:
206 /* 2-layers, with sync point at first frame of layer 1. */
207 cfg->ts_number_layers = 2;
208 cfg->ts_periodicity = 2;
209 cfg->ts_rate_decimator[0] = 2;
210 cfg->ts_rate_decimator[1] = 1;
211 cfg->ts_layer_id[0] = 0;
212 cfg->ts_layer_id[1] = 1;
213 // Use 60/40 bit allocation as example.
214 cfg->ts_target_bitrate[0] = 0.6f * bitrate;
215 cfg->ts_target_bitrate[1] = bitrate;
217 /* 0=L, 1=GF */
218 // ARF is used as predictor for all frames, and is only updated on
219 // key frame. Sync point every 8 frames.
221 // Layer 0: predict from L and ARF, update L and G.
222 layer_flags[0] = VP8_EFLAG_NO_REF_GF |
223 VP8_EFLAG_NO_UPD_ARF;
225 // Layer 1: sync point: predict from L and ARF, and update G.
226 layer_flags[1] = VP8_EFLAG_NO_REF_GF |
227 VP8_EFLAG_NO_UPD_LAST |
228 VP8_EFLAG_NO_UPD_ARF;
230 // Layer 0, predict from L and ARF, update L.
231 layer_flags[2] = VP8_EFLAG_NO_REF_GF |
232 VP8_EFLAG_NO_UPD_GF |
233 VP8_EFLAG_NO_UPD_ARF;
235 // Layer 1: predict from L, G and ARF, and update G.
236 layer_flags[3] = VP8_EFLAG_NO_UPD_ARF |
237 VP8_EFLAG_NO_UPD_LAST |
238 VP8_EFLAG_NO_UPD_ENTROPY;
240 // Layer 0
241 layer_flags[4] = layer_flags[2];
243 // Layer 1
244 layer_flags[5] = layer_flags[3];
246 // Layer 0
247 layer_flags[6] = layer_flags[4];
249 // Layer 1
250 layer_flags[7] = layer_flags[5];
251 break;
254 case 3:
255 default:
257 // 3-layers structure where ARF is used as predictor for all frames,
258 // and is only updated on key frame.
259 // Sync points for layer 1 and 2 every 8 frames.
260 cfg->ts_number_layers = 3;
261 cfg->ts_periodicity = 4;
262 cfg->ts_rate_decimator[0] = 4;
263 cfg->ts_rate_decimator[1] = 2;
264 cfg->ts_rate_decimator[2] = 1;
265 cfg->ts_layer_id[0] = 0;
266 cfg->ts_layer_id[1] = 2;
267 cfg->ts_layer_id[2] = 1;
268 cfg->ts_layer_id[3] = 2;
269 // Use 40/20/40 bit allocation as example.
270 cfg->ts_target_bitrate[0] = 0.4f * bitrate;
271 cfg->ts_target_bitrate[1] = 0.6f * bitrate;
272 cfg->ts_target_bitrate[2] = bitrate;
274 /* 0=L, 1=GF, 2=ARF */
276 // Layer 0: predict from L and ARF; update L and G.
277 layer_flags[0] = VP8_EFLAG_NO_UPD_ARF |
278 VP8_EFLAG_NO_REF_GF;
280 // Layer 2: sync point: predict from L and ARF; update none.
281 layer_flags[1] = VP8_EFLAG_NO_REF_GF |
282 VP8_EFLAG_NO_UPD_GF |
283 VP8_EFLAG_NO_UPD_ARF |
284 VP8_EFLAG_NO_UPD_LAST |
285 VP8_EFLAG_NO_UPD_ENTROPY;
287 // Layer 1: sync point: predict from L and ARF; update G.
288 layer_flags[2] = VP8_EFLAG_NO_REF_GF |
289 VP8_EFLAG_NO_UPD_ARF |
290 VP8_EFLAG_NO_UPD_LAST;
292 // Layer 2: predict from L, G, ARF; update none.
293 layer_flags[3] = VP8_EFLAG_NO_UPD_GF |
294 VP8_EFLAG_NO_UPD_ARF |
295 VP8_EFLAG_NO_UPD_LAST |
296 VP8_EFLAG_NO_UPD_ENTROPY;
298 // Layer 0: predict from L and ARF; update L.
299 layer_flags[4] = VP8_EFLAG_NO_UPD_GF |
300 VP8_EFLAG_NO_UPD_ARF |
301 VP8_EFLAG_NO_REF_GF;
303 // Layer 2: predict from L, G, ARF; update none.
304 layer_flags[5] = layer_flags[3];
306 // Layer 1: predict from L, G, ARF; update G.
307 layer_flags[6] = VP8_EFLAG_NO_UPD_ARF |
308 VP8_EFLAG_NO_UPD_LAST;
310 // Layer 2: predict from L, G, ARF; update none.
311 layer_flags[7] = layer_flags[3];
312 break;
317 /* The periodicity of the pattern given the number of temporal layers. */
318 static int periodicity_to_num_layers[MAX_NUM_TEMPORAL_LAYERS] = {1, 8, 8};
320 int main(int argc, char **argv)
322 FILE *infile, *outfile[NUM_ENCODERS];
323 FILE *downsampled_input[NUM_ENCODERS - 1];
324 char filename[50];
325 vpx_codec_ctx_t codec[NUM_ENCODERS];
326 vpx_codec_enc_cfg_t cfg[NUM_ENCODERS];
327 int frame_cnt = 0;
328 vpx_image_t raw[NUM_ENCODERS];
329 vpx_codec_err_t res[NUM_ENCODERS];
331 int i;
332 long width;
333 long height;
334 int length_frame;
335 int frame_avail;
336 int got_data;
337 int flags = 0;
338 int layer_id = 0;
340 int layer_flags[VPX_TS_MAX_PERIODICITY * NUM_ENCODERS]
341 = {0};
342 int flag_periodicity;
344 /*Currently, only realtime mode is supported in multi-resolution encoding.*/
345 int arg_deadline = VPX_DL_REALTIME;
347 /* Set show_psnr to 1/0 to show/not show PSNR. Choose show_psnr=0 if you
348 don't need to know PSNR, which will skip PSNR calculation and save
349 encoding time. */
350 int show_psnr = 0;
351 int key_frame_insert = 0;
352 uint64_t psnr_sse_total[NUM_ENCODERS] = {0};
353 uint64_t psnr_samples_total[NUM_ENCODERS] = {0};
354 double psnr_totals[NUM_ENCODERS][4] = {{0,0}};
355 int psnr_count[NUM_ENCODERS] = {0};
357 double cx_time = 0;
358 struct timeval tv1, tv2, difftv;
360 /* Set the required target bitrates for each resolution level.
361 * If target bitrate for highest-resolution level is set to 0,
362 * (i.e. target_bitrate[0]=0), we skip encoding at that level.
364 unsigned int target_bitrate[NUM_ENCODERS]={1000, 500, 100};
366 /* Enter the frame rate of the input video */
367 int framerate = 30;
369 /* Set down-sampling factor for each resolution level.
370 dsf[0] controls down sampling from level 0 to level 1;
371 dsf[1] controls down sampling from level 1 to level 2;
372 dsf[2] is not used. */
373 vpx_rational_t dsf[NUM_ENCODERS] = {{2, 1}, {2, 1}, {1, 1}};
375 /* Set the number of temporal layers for each encoder/resolution level,
376 * starting from highest resoln down to lowest resoln. */
377 unsigned int num_temporal_layers[NUM_ENCODERS] = {3, 3, 3};
379 if(argc!= (7 + 3 * NUM_ENCODERS))
380 die("Usage: %s <width> <height> <frame_rate> <infile> <outfile(s)> "
381 "<rate_encoder(s)> <temporal_layer(s)> <key_frame_insert> <output psnr?> \n",
382 argv[0]);
384 printf("Using %s\n",vpx_codec_iface_name(interface));
386 width = strtol(argv[1], NULL, 0);
387 height = strtol(argv[2], NULL, 0);
388 framerate = strtol(argv[3], NULL, 0);
390 if(width < 16 || width%2 || height <16 || height%2)
391 die("Invalid resolution: %ldx%ld", width, height);
393 /* Open input video file for encoding */
394 if(!(infile = fopen(argv[4], "rb")))
395 die("Failed to open %s for reading", argv[4]);
397 /* Open output file for each encoder to output bitstreams */
398 for (i=0; i< NUM_ENCODERS; i++)
400 if(!target_bitrate[i])
402 outfile[i] = NULL;
403 continue;
406 if(!(outfile[i] = fopen(argv[i+5], "wb")))
407 die("Failed to open %s for writing", argv[i+4]);
410 // Bitrates per spatial layer: overwrite default rates above.
411 for (i=0; i< NUM_ENCODERS; i++)
413 target_bitrate[i] = strtol(argv[NUM_ENCODERS + 5 + i], NULL, 0);
416 // Temporal layers per spatial layers: overwrite default settings above.
417 for (i=0; i< NUM_ENCODERS; i++)
419 num_temporal_layers[i] = strtol(argv[2 * NUM_ENCODERS + 5 + i], NULL, 0);
420 if (num_temporal_layers[i] < 1 || num_temporal_layers[i] > 3)
421 die("Invalid temporal layers: %d, Must be 1, 2, or 3. \n",
422 num_temporal_layers);
425 /* Open file to write out each spatially downsampled input stream. */
426 for (i=0; i< NUM_ENCODERS - 1; i++)
428 // Highest resoln is encoder 0.
429 if (sprintf(filename,"ds%d.yuv",NUM_ENCODERS - i) < 0)
431 return EXIT_FAILURE;
433 downsampled_input[i] = fopen(filename,"wb");
436 key_frame_insert = strtol(argv[3 * NUM_ENCODERS + 5], NULL, 0);
438 show_psnr = strtol(argv[3 * NUM_ENCODERS + 6], NULL, 0);
441 /* Populate default encoder configuration */
442 for (i=0; i< NUM_ENCODERS; i++)
444 res[i] = vpx_codec_enc_config_default(interface, &cfg[i], 0);
445 if(res[i]) {
446 printf("Failed to get config: %s\n", vpx_codec_err_to_string(res[i]));
447 return EXIT_FAILURE;
452 * Update the default configuration according to needs of the application.
454 /* Highest-resolution encoder settings */
455 cfg[0].g_w = width;
456 cfg[0].g_h = height;
457 cfg[0].rc_dropframe_thresh = 0;
458 cfg[0].rc_end_usage = VPX_CBR;
459 cfg[0].rc_resize_allowed = 0;
460 cfg[0].rc_min_quantizer = 2;
461 cfg[0].rc_max_quantizer = 56;
462 cfg[0].rc_undershoot_pct = 100;
463 cfg[0].rc_overshoot_pct = 15;
464 cfg[0].rc_buf_initial_sz = 500;
465 cfg[0].rc_buf_optimal_sz = 600;
466 cfg[0].rc_buf_sz = 1000;
467 cfg[0].g_error_resilient = 1; /* Enable error resilient mode */
468 cfg[0].g_lag_in_frames = 0;
470 /* Disable automatic keyframe placement */
471 /* Note: These 3 settings are copied to all levels. But, except the lowest
472 * resolution level, all other levels are set to VPX_KF_DISABLED internally.
474 cfg[0].kf_mode = VPX_KF_AUTO;
475 cfg[0].kf_min_dist = 3000;
476 cfg[0].kf_max_dist = 3000;
478 cfg[0].rc_target_bitrate = target_bitrate[0]; /* Set target bitrate */
479 cfg[0].g_timebase.num = 1; /* Set fps */
480 cfg[0].g_timebase.den = framerate;
482 /* Other-resolution encoder settings */
483 for (i=1; i< NUM_ENCODERS; i++)
485 memcpy(&cfg[i], &cfg[0], sizeof(vpx_codec_enc_cfg_t));
487 cfg[i].rc_target_bitrate = target_bitrate[i];
489 /* Note: Width & height of other-resolution encoders are calculated
490 * from the highest-resolution encoder's size and the corresponding
491 * down_sampling_factor.
494 unsigned int iw = cfg[i-1].g_w*dsf[i-1].den + dsf[i-1].num - 1;
495 unsigned int ih = cfg[i-1].g_h*dsf[i-1].den + dsf[i-1].num - 1;
496 cfg[i].g_w = iw/dsf[i-1].num;
497 cfg[i].g_h = ih/dsf[i-1].num;
500 /* Make width & height to be multiplier of 2. */
501 // Should support odd size ???
502 if((cfg[i].g_w)%2)cfg[i].g_w++;
503 if((cfg[i].g_h)%2)cfg[i].g_h++;
507 // Set the number of threads per encode/spatial layer.
508 // (1, 1, 1) means no encoder threading.
509 cfg[0].g_threads = 2;
510 cfg[1].g_threads = 1;
511 cfg[2].g_threads = 1;
513 /* Allocate image for each encoder */
514 for (i=0; i< NUM_ENCODERS; i++)
515 if(!vpx_img_alloc(&raw[i], VPX_IMG_FMT_I420, cfg[i].g_w, cfg[i].g_h, 32))
516 die("Failed to allocate image", cfg[i].g_w, cfg[i].g_h);
518 if (raw[0].stride[VPX_PLANE_Y] == raw[0].d_w)
519 read_frame_p = read_frame;
520 else
521 read_frame_p = read_frame_by_row;
523 for (i=0; i< NUM_ENCODERS; i++)
524 if(outfile[i])
525 write_ivf_file_header(outfile[i], &cfg[i], 0);
527 /* Temporal layers settings */
528 for ( i=0; i<NUM_ENCODERS; i++)
530 set_temporal_layer_pattern(num_temporal_layers[i],
531 &cfg[i],
532 cfg[i].rc_target_bitrate,
533 &layer_flags[i * VPX_TS_MAX_PERIODICITY]);
536 /* Initialize multi-encoder */
537 if(vpx_codec_enc_init_multi(&codec[0], interface, &cfg[0], NUM_ENCODERS,
538 (show_psnr ? VPX_CODEC_USE_PSNR : 0), &dsf[0]))
539 die_codec(&codec[0], "Failed to initialize encoder");
541 /* The extra encoding configuration parameters can be set as follows. */
542 /* Set encoding speed */
543 for ( i=0; i<NUM_ENCODERS; i++)
545 int speed = -6;
546 /* Lower speed for the lowest resolution. */
547 if (i == NUM_ENCODERS - 1) speed = -4;
548 if(vpx_codec_control(&codec[i], VP8E_SET_CPUUSED, speed))
549 die_codec(&codec[i], "Failed to set cpu_used");
552 /* Set static threshold = 1 for all encoders */
553 for ( i=0; i<NUM_ENCODERS; i++)
555 if(vpx_codec_control(&codec[i], VP8E_SET_STATIC_THRESHOLD, 1))
556 die_codec(&codec[i], "Failed to set static threshold");
559 /* Set NOISE_SENSITIVITY to do TEMPORAL_DENOISING */
560 /* Enable denoising for the highest-resolution encoder. */
561 if(vpx_codec_control(&codec[0], VP8E_SET_NOISE_SENSITIVITY, 1))
562 die_codec(&codec[0], "Failed to set noise_sensitivity");
563 for ( i=1; i< NUM_ENCODERS; i++)
565 if(vpx_codec_control(&codec[i], VP8E_SET_NOISE_SENSITIVITY, 0))
566 die_codec(&codec[i], "Failed to set noise_sensitivity");
569 /* Set the number of token partitions */
570 for ( i=0; i<NUM_ENCODERS; i++)
572 if(vpx_codec_control(&codec[i], VP8E_SET_TOKEN_PARTITIONS, 1))
573 die_codec(&codec[i], "Failed to set static threshold");
576 /* Set the max intra target bitrate */
577 for ( i=0; i<NUM_ENCODERS; i++)
579 unsigned int max_intra_size_pct =
580 (int)(((double)cfg[0].rc_buf_optimal_sz * 0.5) * framerate / 10);
581 if(vpx_codec_control(&codec[i], VP8E_SET_MAX_INTRA_BITRATE_PCT,
582 max_intra_size_pct))
583 die_codec(&codec[i], "Failed to set static threshold");
584 //printf("%d %d \n",i,max_intra_size_pct);
587 frame_avail = 1;
588 got_data = 0;
590 while(frame_avail || got_data)
592 vpx_codec_iter_t iter[NUM_ENCODERS]={NULL};
593 const vpx_codec_cx_pkt_t *pkt[NUM_ENCODERS];
595 flags = 0;
596 frame_avail = read_frame_p(infile, &raw[0]);
598 if(frame_avail)
600 for ( i=1; i<NUM_ENCODERS; i++)
602 /*Scale the image down a number of times by downsampling factor*/
603 /* FilterMode 1 or 2 give better psnr than FilterMode 0. */
604 I420Scale(raw[i-1].planes[VPX_PLANE_Y], raw[i-1].stride[VPX_PLANE_Y],
605 raw[i-1].planes[VPX_PLANE_U], raw[i-1].stride[VPX_PLANE_U],
606 raw[i-1].planes[VPX_PLANE_V], raw[i-1].stride[VPX_PLANE_V],
607 raw[i-1].d_w, raw[i-1].d_h,
608 raw[i].planes[VPX_PLANE_Y], raw[i].stride[VPX_PLANE_Y],
609 raw[i].planes[VPX_PLANE_U], raw[i].stride[VPX_PLANE_U],
610 raw[i].planes[VPX_PLANE_V], raw[i].stride[VPX_PLANE_V],
611 raw[i].d_w, raw[i].d_h, 1);
612 /* Write out down-sampled input. */
613 length_frame = cfg[i].g_w * cfg[i].g_h *3/2;
614 if (fwrite(raw[i].planes[0], 1, length_frame,
615 downsampled_input[NUM_ENCODERS - i - 1]) !=
616 length_frame)
618 return EXIT_FAILURE;
623 /* Set the flags (reference and update) for all the encoders.*/
624 for ( i=0; i<NUM_ENCODERS; i++)
626 layer_id = cfg[i].ts_layer_id[frame_cnt % cfg[i].ts_periodicity];
627 flags = 0;
628 flag_periodicity = periodicity_to_num_layers
629 [num_temporal_layers[i] - 1];
630 flags = layer_flags[i * VPX_TS_MAX_PERIODICITY +
631 frame_cnt % flag_periodicity];
632 // Key frame flag for first frame.
633 if (frame_cnt == 0)
635 flags |= VPX_EFLAG_FORCE_KF;
637 if (frame_cnt > 0 && frame_cnt == key_frame_insert)
639 flags = VPX_EFLAG_FORCE_KF;
642 vpx_codec_control(&codec[i], VP8E_SET_FRAME_FLAGS, flags);
643 vpx_codec_control(&codec[i], VP8E_SET_TEMPORAL_LAYER_ID, layer_id);
646 gettimeofday(&tv1, NULL);
647 /* Encode each frame at multi-levels */
648 /* Note the flags must be set to 0 in the encode call if they are set
649 for each frame with the vpx_codec_control(), as done above. */
650 if(vpx_codec_encode(&codec[0], frame_avail? &raw[0] : NULL,
651 frame_cnt, 1, 0, arg_deadline))
653 die_codec(&codec[0], "Failed to encode frame");
655 gettimeofday(&tv2, NULL);
656 timersub(&tv2, &tv1, &difftv);
657 cx_time += (double)(difftv.tv_sec * 1000000 + difftv.tv_usec);
658 for (i=NUM_ENCODERS-1; i>=0 ; i--)
660 got_data = 0;
661 while( (pkt[i] = vpx_codec_get_cx_data(&codec[i], &iter[i])) )
663 got_data = 1;
664 switch(pkt[i]->kind) {
665 case VPX_CODEC_CX_FRAME_PKT:
666 write_ivf_frame_header(outfile[i], pkt[i]);
667 (void) fwrite(pkt[i]->data.frame.buf, 1,
668 pkt[i]->data.frame.sz, outfile[i]);
669 break;
670 case VPX_CODEC_PSNR_PKT:
671 if (show_psnr)
673 int j;
675 psnr_sse_total[i] += pkt[i]->data.psnr.sse[0];
676 psnr_samples_total[i] += pkt[i]->data.psnr.samples[0];
677 for (j = 0; j < 4; j++)
679 psnr_totals[i][j] += pkt[i]->data.psnr.psnr[j];
681 psnr_count[i]++;
684 break;
685 default:
686 break;
688 printf(pkt[i]->kind == VPX_CODEC_CX_FRAME_PKT
689 && (pkt[i]->data.frame.flags & VPX_FRAME_IS_KEY)? "K":"");
690 fflush(stdout);
693 frame_cnt++;
695 printf("\n");
696 printf("FPS for encoding %d %f %f \n", frame_cnt, (float)cx_time / 1000000,
697 1000000 * (double)frame_cnt / (double)cx_time);
699 fclose(infile);
701 printf("Processed %ld frames.\n",(long int)frame_cnt-1);
702 for (i=0; i< NUM_ENCODERS; i++)
704 /* Calculate PSNR and print it out */
705 if ( (show_psnr) && (psnr_count[i]>0) )
707 int j;
708 double ovpsnr = sse_to_psnr(psnr_samples_total[i], 255.0,
709 psnr_sse_total[i]);
711 fprintf(stderr, "\n ENC%d PSNR (Overall/Avg/Y/U/V)", i);
713 fprintf(stderr, " %.3lf", ovpsnr);
714 for (j = 0; j < 4; j++)
716 fprintf(stderr, " %.3lf", psnr_totals[i][j]/psnr_count[i]);
720 if(vpx_codec_destroy(&codec[i]))
721 die_codec(&codec[i], "Failed to destroy codec");
723 vpx_img_free(&raw[i]);
725 if(!outfile[i])
726 continue;
728 /* Try to rewrite the file header with the actual frame count */
729 if(!fseek(outfile[i], 0, SEEK_SET))
730 write_ivf_file_header(outfile[i], &cfg[i], frame_cnt-1);
731 fclose(outfile[i]);
733 printf("\n");
735 return EXIT_SUCCESS;