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