Yunqing Wang | aa7335e | 2011-10-25 15:14:16 -0400 | [diff] [blame] | 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 | #include <stdio.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <stdarg.h> |
| 20 | #include <string.h> |
| 21 | #include "math.h" |
| 22 | #define VPX_CODEC_DISABLE_COMPAT 1 |
| 23 | #include "vpx/vpx_encoder.h" |
| 24 | #include "vpx/vp8cx.h" |
| 25 | #include "vpx_ports/mem_ops.h" |
| 26 | #define interface (vpx_codec_vp8_cx()) |
| 27 | #define fourcc 0x30385056 |
| 28 | |
| 29 | #define IVF_FILE_HDR_SZ (32) |
| 30 | #define IVF_FRAME_HDR_SZ (12) |
| 31 | |
| 32 | /* |
| 33 | * The input video frame is downsampled several times to generate a multi-level |
| 34 | * hierarchical structure. NUM_ENCODERS is defined as the number of encoding |
| 35 | * levels required. For example, if the size of input video is 1280x720, |
| 36 | * NUM_ENCODERS is 3, and down-sampling factor is 2, the encoder outputs 3 |
| 37 | * bitstreams with resolution of 1280x720(level 0), 640x360(level 1), and |
| 38 | * 320x180(level 2) respectively. |
| 39 | */ |
| 40 | #define NUM_ENCODERS 3 |
| 41 | |
| 42 | /* This example uses the scaler function in libyuv. */ |
| 43 | #include "third_party/libyuv/include/libyuv/basic_types.h" |
| 44 | #include "third_party/libyuv/include/libyuv/scale.h" |
| 45 | #include "third_party/libyuv/include/libyuv/cpu_id.h" |
| 46 | |
| 47 | static double vp8_mse2psnr(double Samples, double Peak, double Mse) |
| 48 | { |
| 49 | double psnr; |
| 50 | |
| 51 | if ((double)Mse > 0.0) |
| 52 | psnr = 10.0 * log10(Peak * Peak * Samples / Mse); |
| 53 | else |
| 54 | psnr = 60; // Limit to prevent / 0 |
| 55 | |
| 56 | if (psnr > 60) |
| 57 | psnr = 60; |
| 58 | |
| 59 | return psnr; |
| 60 | } |
| 61 | |
| 62 | static void die(const char *fmt, ...) { |
| 63 | va_list ap; |
| 64 | |
| 65 | va_start(ap, fmt); |
| 66 | vprintf(fmt, ap); |
| 67 | if(fmt[strlen(fmt)-1] != '\n') |
| 68 | printf("\n"); |
| 69 | exit(EXIT_FAILURE); |
| 70 | } |
| 71 | |
| 72 | static void die_codec(vpx_codec_ctx_t *ctx, const char *s) { |
| 73 | const char *detail = vpx_codec_error_detail(ctx); |
| 74 | |
| 75 | printf("%s: %s\n", s, vpx_codec_error(ctx)); |
| 76 | if(detail) |
| 77 | printf(" %s\n",detail); |
| 78 | exit(EXIT_FAILURE); |
| 79 | } |
| 80 | |
Yunqing Wang | 153eec4 | 2011-12-08 12:31:01 -0500 | [diff] [blame] | 81 | int (*read_frame_p)(FILE *f, vpx_image_t *img); |
| 82 | |
Yunqing Wang | aa7335e | 2011-10-25 15:14:16 -0400 | [diff] [blame] | 83 | static int read_frame(FILE *f, vpx_image_t *img) { |
| 84 | size_t nbytes, to_read; |
| 85 | int res = 1; |
| 86 | |
| 87 | to_read = img->w*img->h*3/2; |
| 88 | nbytes = fread(img->planes[0], 1, to_read, f); |
| 89 | if(nbytes != to_read) { |
| 90 | res = 0; |
| 91 | if(nbytes > 0) |
| 92 | printf("Warning: Read partial frame. Check your width & height!\n"); |
| 93 | } |
| 94 | return res; |
| 95 | } |
| 96 | |
Yunqing Wang | 153eec4 | 2011-12-08 12:31:01 -0500 | [diff] [blame] | 97 | static int read_frame_by_row(FILE *f, vpx_image_t *img) { |
| 98 | size_t nbytes, to_read; |
| 99 | int res = 1; |
| 100 | int plane; |
| 101 | |
| 102 | for (plane = 0; plane < 3; plane++) |
| 103 | { |
| 104 | unsigned char *ptr; |
| 105 | int w = (plane ? (1 + img->d_w) / 2 : img->d_w); |
| 106 | int h = (plane ? (1 + img->d_h) / 2 : img->d_h); |
| 107 | int r; |
| 108 | |
| 109 | /* Determine the correct plane based on the image format. The for-loop |
| 110 | * always counts in Y,U,V order, but this may not match the order of |
| 111 | * the data on disk. |
| 112 | */ |
| 113 | switch (plane) |
| 114 | { |
| 115 | case 1: |
| 116 | ptr = img->planes[img->fmt==VPX_IMG_FMT_YV12? VPX_PLANE_V : VPX_PLANE_U]; |
| 117 | break; |
| 118 | case 2: |
| 119 | ptr = img->planes[img->fmt==VPX_IMG_FMT_YV12?VPX_PLANE_U : VPX_PLANE_V]; |
| 120 | break; |
| 121 | default: |
| 122 | ptr = img->planes[plane]; |
| 123 | } |
| 124 | |
| 125 | for (r = 0; r < h; r++) |
| 126 | { |
| 127 | to_read = w; |
| 128 | |
| 129 | nbytes = fread(ptr, 1, to_read, f); |
| 130 | if(nbytes != to_read) { |
| 131 | res = 0; |
| 132 | if(nbytes > 0) |
| 133 | printf("Warning: Read partial frame. Check your width & height!\n"); |
| 134 | break; |
| 135 | } |
| 136 | |
| 137 | ptr += img->stride[plane]; |
| 138 | } |
| 139 | if (!res) |
| 140 | break; |
| 141 | } |
| 142 | |
| 143 | return res; |
| 144 | } |
| 145 | |
Yunqing Wang | aa7335e | 2011-10-25 15:14:16 -0400 | [diff] [blame] | 146 | static void write_ivf_file_header(FILE *outfile, |
| 147 | const vpx_codec_enc_cfg_t *cfg, |
| 148 | int frame_cnt) { |
| 149 | char header[32]; |
| 150 | |
| 151 | if(cfg->g_pass != VPX_RC_ONE_PASS && cfg->g_pass != VPX_RC_LAST_PASS) |
| 152 | return; |
| 153 | header[0] = 'D'; |
| 154 | header[1] = 'K'; |
| 155 | header[2] = 'I'; |
| 156 | header[3] = 'F'; |
| 157 | mem_put_le16(header+4, 0); /* version */ |
| 158 | mem_put_le16(header+6, 32); /* headersize */ |
| 159 | mem_put_le32(header+8, fourcc); /* headersize */ |
| 160 | mem_put_le16(header+12, cfg->g_w); /* width */ |
| 161 | mem_put_le16(header+14, cfg->g_h); /* height */ |
| 162 | mem_put_le32(header+16, cfg->g_timebase.den); /* rate */ |
| 163 | mem_put_le32(header+20, cfg->g_timebase.num); /* scale */ |
| 164 | mem_put_le32(header+24, frame_cnt); /* length */ |
| 165 | mem_put_le32(header+28, 0); /* unused */ |
| 166 | |
Johann | c8a88a7 | 2012-05-02 14:12:57 -0700 | [diff] [blame] | 167 | (void) fwrite(header, 1, 32, outfile); |
Yunqing Wang | aa7335e | 2011-10-25 15:14:16 -0400 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | static void write_ivf_frame_header(FILE *outfile, |
| 171 | const vpx_codec_cx_pkt_t *pkt) |
| 172 | { |
| 173 | char header[12]; |
| 174 | vpx_codec_pts_t pts; |
| 175 | |
| 176 | if(pkt->kind != VPX_CODEC_CX_FRAME_PKT) |
| 177 | return; |
| 178 | |
| 179 | pts = pkt->data.frame.pts; |
| 180 | mem_put_le32(header, pkt->data.frame.sz); |
| 181 | mem_put_le32(header+4, pts&0xFFFFFFFF); |
| 182 | mem_put_le32(header+8, pts >> 32); |
| 183 | |
Johann | c8a88a7 | 2012-05-02 14:12:57 -0700 | [diff] [blame] | 184 | (void) fwrite(header, 1, 12, outfile); |
Yunqing Wang | aa7335e | 2011-10-25 15:14:16 -0400 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | int main(int argc, char **argv) |
| 188 | { |
| 189 | FILE *infile, *outfile[NUM_ENCODERS]; |
| 190 | vpx_codec_ctx_t codec[NUM_ENCODERS]; |
| 191 | vpx_codec_enc_cfg_t cfg[NUM_ENCODERS]; |
| 192 | vpx_codec_pts_t frame_cnt = 0; |
| 193 | vpx_image_t raw[NUM_ENCODERS]; |
| 194 | vpx_codec_err_t res[NUM_ENCODERS]; |
| 195 | |
| 196 | int i; |
| 197 | long width; |
| 198 | long height; |
| 199 | int frame_avail; |
| 200 | int got_data; |
| 201 | int flags = 0; |
| 202 | |
| 203 | /*Currently, only realtime mode is supported in multi-resolution encoding.*/ |
| 204 | int arg_deadline = VPX_DL_REALTIME; |
| 205 | |
| 206 | /* Set show_psnr to 1/0 to show/not show PSNR. Choose show_psnr=0 if you |
| 207 | don't need to know PSNR, which will skip PSNR calculation and save |
| 208 | encoding time. */ |
| 209 | int show_psnr = 0; |
| 210 | uint64_t psnr_sse_total[NUM_ENCODERS] = {0}; |
| 211 | uint64_t psnr_samples_total[NUM_ENCODERS] = {0}; |
| 212 | double psnr_totals[NUM_ENCODERS][4] = {{0,0}}; |
| 213 | int psnr_count[NUM_ENCODERS] = {0}; |
| 214 | |
Yunqing Wang | fa1a929 | 2012-02-02 14:27:04 -0500 | [diff] [blame] | 215 | /* Set the required target bitrates for each resolution level. |
| 216 | * If target bitrate for highest-resolution level is set to 0, |
| 217 | * (i.e. target_bitrate[0]=0), we skip encoding at that level. |
| 218 | */ |
Marco Paniconi | 89826f1 | 2013-01-09 18:26:22 -0800 | [diff] [blame] | 219 | unsigned int target_bitrate[NUM_ENCODERS]={1000, 500, 100}; |
Yunqing Wang | aa7335e | 2011-10-25 15:14:16 -0400 | [diff] [blame] | 220 | /* Enter the frame rate of the input video */ |
| 221 | int framerate = 30; |
| 222 | /* Set down-sampling factor for each resolution level. |
| 223 | dsf[0] controls down sampling from level 0 to level 1; |
| 224 | dsf[1] controls down sampling from level 1 to level 2; |
| 225 | dsf[2] is not used. */ |
| 226 | vpx_rational_t dsf[NUM_ENCODERS] = {{2, 1}, {2, 1}, {1, 1}}; |
| 227 | |
| 228 | if(argc!= (5+NUM_ENCODERS)) |
| 229 | die("Usage: %s <width> <height> <infile> <outfile(s)> <output psnr?>\n", |
| 230 | argv[0]); |
| 231 | |
| 232 | printf("Using %s\n",vpx_codec_iface_name(interface)); |
| 233 | |
| 234 | width = strtol(argv[1], NULL, 0); |
| 235 | height = strtol(argv[2], NULL, 0); |
| 236 | |
| 237 | if(width < 16 || width%2 || height <16 || height%2) |
| 238 | die("Invalid resolution: %ldx%ld", width, height); |
| 239 | |
| 240 | /* Open input video file for encoding */ |
| 241 | if(!(infile = fopen(argv[3], "rb"))) |
| 242 | die("Failed to open %s for reading", argv[3]); |
| 243 | |
| 244 | /* Open output file for each encoder to output bitstreams */ |
| 245 | for (i=0; i< NUM_ENCODERS; i++) |
| 246 | { |
John Koleszar | 25a36d6 | 2012-04-19 10:00:33 -0700 | [diff] [blame] | 247 | if(!target_bitrate[i]) |
| 248 | { |
| 249 | outfile[i] = NULL; |
| 250 | continue; |
| 251 | } |
| 252 | |
Yunqing Wang | aa7335e | 2011-10-25 15:14:16 -0400 | [diff] [blame] | 253 | if(!(outfile[i] = fopen(argv[i+4], "wb"))) |
| 254 | die("Failed to open %s for writing", argv[i+4]); |
| 255 | } |
| 256 | |
| 257 | show_psnr = strtol(argv[NUM_ENCODERS + 4], NULL, 0); |
| 258 | |
| 259 | /* Populate default encoder configuration */ |
| 260 | for (i=0; i< NUM_ENCODERS; i++) |
| 261 | { |
| 262 | res[i] = vpx_codec_enc_config_default(interface, &cfg[i], 0); |
| 263 | if(res[i]) { |
| 264 | printf("Failed to get config: %s\n", vpx_codec_err_to_string(res[i])); |
| 265 | return EXIT_FAILURE; |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | /* |
| 270 | * Update the default configuration according to needs of the application. |
| 271 | */ |
| 272 | /* Highest-resolution encoder settings */ |
| 273 | cfg[0].g_w = width; |
| 274 | cfg[0].g_h = height; |
| 275 | cfg[0].g_threads = 1; /* number of threads used */ |
Yunqing Wang | 4066c8b | 2012-06-08 11:17:50 -0400 | [diff] [blame] | 276 | cfg[0].rc_dropframe_thresh = 30; |
Yunqing Wang | aa7335e | 2011-10-25 15:14:16 -0400 | [diff] [blame] | 277 | cfg[0].rc_end_usage = VPX_CBR; |
| 278 | cfg[0].rc_resize_allowed = 0; |
| 279 | cfg[0].rc_min_quantizer = 4; |
| 280 | cfg[0].rc_max_quantizer = 56; |
| 281 | cfg[0].rc_undershoot_pct = 98; |
| 282 | cfg[0].rc_overshoot_pct = 100; |
| 283 | cfg[0].rc_buf_initial_sz = 500; |
| 284 | cfg[0].rc_buf_optimal_sz = 600; |
| 285 | cfg[0].rc_buf_sz = 1000; |
Yunqing Wang | aa7335e | 2011-10-25 15:14:16 -0400 | [diff] [blame] | 286 | cfg[0].g_error_resilient = 1; /* Enable error resilient mode */ |
| 287 | cfg[0].g_lag_in_frames = 0; |
| 288 | |
| 289 | /* Disable automatic keyframe placement */ |
Yunqing Wang | 65dd157 | 2012-05-16 15:06:42 -0400 | [diff] [blame] | 290 | /* Note: These 3 settings are copied to all levels. But, except the lowest |
| 291 | * resolution level, all other levels are set to VPX_KF_DISABLED internally. |
| 292 | */ |
Yunqing Wang | aa7335e | 2011-10-25 15:14:16 -0400 | [diff] [blame] | 293 | //cfg[0].kf_mode = VPX_KF_DISABLED; |
Yunqing Wang | 65dd157 | 2012-05-16 15:06:42 -0400 | [diff] [blame] | 294 | cfg[0].kf_mode = VPX_KF_AUTO; |
Yunqing Wang | 4066c8b | 2012-06-08 11:17:50 -0400 | [diff] [blame] | 295 | cfg[0].kf_min_dist = 3000; |
| 296 | cfg[0].kf_max_dist = 3000; |
Yunqing Wang | aa7335e | 2011-10-25 15:14:16 -0400 | [diff] [blame] | 297 | |
| 298 | cfg[0].rc_target_bitrate = target_bitrate[0]; /* Set target bitrate */ |
| 299 | cfg[0].g_timebase.num = 1; /* Set fps */ |
| 300 | cfg[0].g_timebase.den = framerate; |
| 301 | |
| 302 | /* Other-resolution encoder settings */ |
| 303 | for (i=1; i< NUM_ENCODERS; i++) |
| 304 | { |
| 305 | memcpy(&cfg[i], &cfg[0], sizeof(vpx_codec_enc_cfg_t)); |
| 306 | |
| 307 | cfg[i].g_threads = 1; /* number of threads used */ |
| 308 | cfg[i].rc_target_bitrate = target_bitrate[i]; |
| 309 | |
| 310 | /* Note: Width & height of other-resolution encoders are calculated |
| 311 | * from the highest-resolution encoder's size and the corresponding |
| 312 | * down_sampling_factor. |
| 313 | */ |
| 314 | { |
| 315 | unsigned int iw = cfg[i-1].g_w*dsf[i-1].den + dsf[i-1].num - 1; |
| 316 | unsigned int ih = cfg[i-1].g_h*dsf[i-1].den + dsf[i-1].num - 1; |
| 317 | cfg[i].g_w = iw/dsf[i-1].num; |
| 318 | cfg[i].g_h = ih/dsf[i-1].num; |
| 319 | } |
| 320 | |
| 321 | /* Make width & height to be multiplier of 2. */ |
| 322 | // Should support odd size ??? |
| 323 | if((cfg[i].g_w)%2)cfg[i].g_w++; |
| 324 | if((cfg[i].g_h)%2)cfg[i].g_h++; |
| 325 | } |
| 326 | |
| 327 | /* Allocate image for each encoder */ |
| 328 | for (i=0; i< NUM_ENCODERS; i++) |
Yunqing Wang | 153eec4 | 2011-12-08 12:31:01 -0500 | [diff] [blame] | 329 | if(!vpx_img_alloc(&raw[i], VPX_IMG_FMT_I420, cfg[i].g_w, cfg[i].g_h, 32)) |
Yunqing Wang | aa7335e | 2011-10-25 15:14:16 -0400 | [diff] [blame] | 330 | die("Failed to allocate image", cfg[i].g_w, cfg[i].g_h); |
| 331 | |
Yunqing Wang | 153eec4 | 2011-12-08 12:31:01 -0500 | [diff] [blame] | 332 | if (raw[0].stride[VPX_PLANE_Y] == raw[0].d_w) |
| 333 | read_frame_p = read_frame; |
| 334 | else |
| 335 | read_frame_p = read_frame_by_row; |
| 336 | |
John Koleszar | 25a36d6 | 2012-04-19 10:00:33 -0700 | [diff] [blame] | 337 | for (i=0; i< NUM_ENCODERS; i++) |
| 338 | if(outfile[i]) |
| 339 | write_ivf_file_header(outfile[i], &cfg[i], 0); |
| 340 | |
Yunqing Wang | aa7335e | 2011-10-25 15:14:16 -0400 | [diff] [blame] | 341 | /* Initialize multi-encoder */ |
John Koleszar | 25a36d6 | 2012-04-19 10:00:33 -0700 | [diff] [blame] | 342 | if(vpx_codec_enc_init_multi(&codec[0], interface, &cfg[0], NUM_ENCODERS, |
| 343 | (show_psnr ? VPX_CODEC_USE_PSNR : 0), &dsf[0])) |
| 344 | die_codec(&codec[0], "Failed to initialize encoder"); |
Yunqing Wang | aa7335e | 2011-10-25 15:14:16 -0400 | [diff] [blame] | 345 | |
| 346 | /* The extra encoding configuration parameters can be set as follows. */ |
| 347 | /* Set encoding speed */ |
John Koleszar | 25a36d6 | 2012-04-19 10:00:33 -0700 | [diff] [blame] | 348 | for ( i=0; i<NUM_ENCODERS; i++) |
Yunqing Wang | aa7335e | 2011-10-25 15:14:16 -0400 | [diff] [blame] | 349 | { |
| 350 | int speed = -6; |
| 351 | if(vpx_codec_control(&codec[i], VP8E_SET_CPUUSED, speed)) |
| 352 | die_codec(&codec[i], "Failed to set cpu_used"); |
| 353 | } |
Marco Paniconi | 89826f1 | 2013-01-09 18:26:22 -0800 | [diff] [blame] | 354 | |
| 355 | /* Set static threshold. */ |
| 356 | for ( i=0; i<NUM_ENCODERS; i++) |
Yunqing Wang | aa7335e | 2011-10-25 15:14:16 -0400 | [diff] [blame] | 357 | { |
Marco Paniconi | 89826f1 | 2013-01-09 18:26:22 -0800 | [diff] [blame] | 358 | unsigned int static_thresh = 1; |
John Koleszar | 25a36d6 | 2012-04-19 10:00:33 -0700 | [diff] [blame] | 359 | if(vpx_codec_control(&codec[i], VP8E_SET_STATIC_THRESHOLD, static_thresh)) |
Yunqing Wang | aa7335e | 2011-10-25 15:14:16 -0400 | [diff] [blame] | 360 | die_codec(&codec[i], "Failed to set static threshold"); |
| 361 | } |
Marco Paniconi | 89826f1 | 2013-01-09 18:26:22 -0800 | [diff] [blame] | 362 | |
Yunqing Wang | 4066c8b | 2012-06-08 11:17:50 -0400 | [diff] [blame] | 363 | /* Set NOISE_SENSITIVITY to do TEMPORAL_DENOISING */ |
Marco Paniconi | 89826f1 | 2013-01-09 18:26:22 -0800 | [diff] [blame] | 364 | /* Enable denoising for the highest-resolution encoder. */ |
| 365 | if(vpx_codec_control(&codec[0], VP8E_SET_NOISE_SENSITIVITY, 1)) |
| 366 | die_codec(&codec[0], "Failed to set noise_sensitivity"); |
| 367 | for ( i=1; i< NUM_ENCODERS; i++) |
Yunqing Wang | 4066c8b | 2012-06-08 11:17:50 -0400 | [diff] [blame] | 368 | { |
| 369 | if(vpx_codec_control(&codec[i], VP8E_SET_NOISE_SENSITIVITY, 0)) |
| 370 | die_codec(&codec[i], "Failed to set noise_sensitivity"); |
| 371 | } |
Yunqing Wang | aa7335e | 2011-10-25 15:14:16 -0400 | [diff] [blame] | 372 | |
Marco Paniconi | 89826f1 | 2013-01-09 18:26:22 -0800 | [diff] [blame] | 373 | |
Yunqing Wang | aa7335e | 2011-10-25 15:14:16 -0400 | [diff] [blame] | 374 | frame_avail = 1; |
| 375 | got_data = 0; |
| 376 | |
| 377 | while(frame_avail || got_data) |
| 378 | { |
| 379 | vpx_codec_iter_t iter[NUM_ENCODERS]={NULL}; |
| 380 | const vpx_codec_cx_pkt_t *pkt[NUM_ENCODERS]; |
| 381 | |
| 382 | flags = 0; |
Yunqing Wang | 153eec4 | 2011-12-08 12:31:01 -0500 | [diff] [blame] | 383 | frame_avail = read_frame_p(infile, &raw[0]); |
Yunqing Wang | aa7335e | 2011-10-25 15:14:16 -0400 | [diff] [blame] | 384 | |
Yunqing Wang | 153eec4 | 2011-12-08 12:31:01 -0500 | [diff] [blame] | 385 | if(frame_avail) |
Yunqing Wang | aa7335e | 2011-10-25 15:14:16 -0400 | [diff] [blame] | 386 | { |
Yunqing Wang | 153eec4 | 2011-12-08 12:31:01 -0500 | [diff] [blame] | 387 | for ( i=1; i<NUM_ENCODERS; i++) |
Yunqing Wang | aa7335e | 2011-10-25 15:14:16 -0400 | [diff] [blame] | 388 | { |
| 389 | /*Scale the image down a number of times by downsampling factor*/ |
Yunqing Wang | aa7335e | 2011-10-25 15:14:16 -0400 | [diff] [blame] | 390 | /* FilterMode 1 or 2 give better psnr than FilterMode 0. */ |
Yunqing Wang | 153eec4 | 2011-12-08 12:31:01 -0500 | [diff] [blame] | 391 | I420Scale(raw[i-1].planes[VPX_PLANE_Y], raw[i-1].stride[VPX_PLANE_Y], |
| 392 | raw[i-1].planes[VPX_PLANE_U], raw[i-1].stride[VPX_PLANE_U], |
| 393 | raw[i-1].planes[VPX_PLANE_V], raw[i-1].stride[VPX_PLANE_V], |
| 394 | raw[i-1].d_w, raw[i-1].d_h, |
| 395 | raw[i].planes[VPX_PLANE_Y], raw[i].stride[VPX_PLANE_Y], |
| 396 | raw[i].planes[VPX_PLANE_U], raw[i].stride[VPX_PLANE_U], |
| 397 | raw[i].planes[VPX_PLANE_V], raw[i].stride[VPX_PLANE_V], |
| 398 | raw[i].d_w, raw[i].d_h, 1); |
Yunqing Wang | aa7335e | 2011-10-25 15:14:16 -0400 | [diff] [blame] | 399 | } |
| 400 | } |
| 401 | |
| 402 | /* Encode each frame at multi-levels */ |
John Koleszar | 25a36d6 | 2012-04-19 10:00:33 -0700 | [diff] [blame] | 403 | if(vpx_codec_encode(&codec[0], frame_avail? &raw[0] : NULL, |
Yunqing Wang | aa7335e | 2011-10-25 15:14:16 -0400 | [diff] [blame] | 404 | frame_cnt, 1, flags, arg_deadline)) |
John Koleszar | 25a36d6 | 2012-04-19 10:00:33 -0700 | [diff] [blame] | 405 | die_codec(&codec[0], "Failed to encode frame"); |
Yunqing Wang | aa7335e | 2011-10-25 15:14:16 -0400 | [diff] [blame] | 406 | |
John Koleszar | 25a36d6 | 2012-04-19 10:00:33 -0700 | [diff] [blame] | 407 | for (i=NUM_ENCODERS-1; i>=0 ; i--) |
Yunqing Wang | aa7335e | 2011-10-25 15:14:16 -0400 | [diff] [blame] | 408 | { |
| 409 | got_data = 0; |
| 410 | |
| 411 | while( (pkt[i] = vpx_codec_get_cx_data(&codec[i], &iter[i])) ) |
| 412 | { |
| 413 | got_data = 1; |
| 414 | switch(pkt[i]->kind) { |
| 415 | case VPX_CODEC_CX_FRAME_PKT: |
| 416 | write_ivf_frame_header(outfile[i], pkt[i]); |
Johann | c8a88a7 | 2012-05-02 14:12:57 -0700 | [diff] [blame] | 417 | (void) fwrite(pkt[i]->data.frame.buf, 1, |
| 418 | pkt[i]->data.frame.sz, outfile[i]); |
Yunqing Wang | aa7335e | 2011-10-25 15:14:16 -0400 | [diff] [blame] | 419 | break; |
| 420 | case VPX_CODEC_PSNR_PKT: |
| 421 | if (show_psnr) |
| 422 | { |
| 423 | int j; |
| 424 | |
| 425 | psnr_sse_total[i] += pkt[i]->data.psnr.sse[0]; |
| 426 | psnr_samples_total[i] += pkt[i]->data.psnr.samples[0]; |
| 427 | for (j = 0; j < 4; j++) |
| 428 | { |
| 429 | //fprintf(stderr, "%.3lf ", pkt[i]->data.psnr.psnr[j]); |
| 430 | psnr_totals[i][j] += pkt[i]->data.psnr.psnr[j]; |
| 431 | } |
| 432 | psnr_count[i]++; |
| 433 | } |
| 434 | |
| 435 | break; |
| 436 | default: |
| 437 | break; |
| 438 | } |
| 439 | printf(pkt[i]->kind == VPX_CODEC_CX_FRAME_PKT |
| 440 | && (pkt[i]->data.frame.flags & VPX_FRAME_IS_KEY)? "K":"."); |
| 441 | fflush(stdout); |
| 442 | } |
| 443 | } |
| 444 | frame_cnt++; |
| 445 | } |
| 446 | printf("\n"); |
| 447 | |
| 448 | fclose(infile); |
| 449 | |
John Koleszar | 25a36d6 | 2012-04-19 10:00:33 -0700 | [diff] [blame] | 450 | printf("Processed %ld frames.\n",(long int)frame_cnt-1); |
| 451 | for (i=0; i< NUM_ENCODERS; i++) |
Yunqing Wang | aa7335e | 2011-10-25 15:14:16 -0400 | [diff] [blame] | 452 | { |
Yunqing Wang | aa7335e | 2011-10-25 15:14:16 -0400 | [diff] [blame] | 453 | /* Calculate PSNR and print it out */ |
| 454 | if ( (show_psnr) && (psnr_count[i]>0) ) |
| 455 | { |
| 456 | int j; |
| 457 | double ovpsnr = vp8_mse2psnr(psnr_samples_total[i], 255.0, |
| 458 | psnr_sse_total[i]); |
| 459 | |
| 460 | fprintf(stderr, "\n ENC%d PSNR (Overall/Avg/Y/U/V)", i); |
| 461 | |
| 462 | fprintf(stderr, " %.3lf", ovpsnr); |
| 463 | for (j = 0; j < 4; j++) |
| 464 | { |
| 465 | fprintf(stderr, " %.3lf", psnr_totals[i][j]/psnr_count[i]); |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | if(vpx_codec_destroy(&codec[i])) |
| 470 | die_codec(&codec[i], "Failed to destroy codec"); |
| 471 | |
John Koleszar | 25a36d6 | 2012-04-19 10:00:33 -0700 | [diff] [blame] | 472 | vpx_img_free(&raw[i]); |
| 473 | |
| 474 | if(!outfile[i]) |
| 475 | continue; |
| 476 | |
Yunqing Wang | aa7335e | 2011-10-25 15:14:16 -0400 | [diff] [blame] | 477 | /* Try to rewrite the file header with the actual frame count */ |
| 478 | if(!fseek(outfile[i], 0, SEEK_SET)) |
| 479 | write_ivf_file_header(outfile[i], &cfg[i], frame_cnt-1); |
Yunqing Wang | fa1a929 | 2012-02-02 14:27:04 -0500 | [diff] [blame] | 480 | fclose(outfile[i]); |
Yunqing Wang | aa7335e | 2011-10-25 15:14:16 -0400 | [diff] [blame] | 481 | } |
John Koleszar | 25a36d6 | 2012-04-19 10:00:33 -0700 | [diff] [blame] | 482 | printf("\n"); |
Yunqing Wang | aa7335e | 2011-10-25 15:14:16 -0400 | [diff] [blame] | 483 | |
| 484 | return EXIT_SUCCESS; |
| 485 | } |