23 #include "./vpx_config.h"
32 #include "vpx_ports/vpx_timer.h"
35 #include "vpx_ports/mem_ops.h"
36 #include "../tools_common.h"
37 #define interface (vpx_codec_vp8_cx())
38 #define fourcc 0x30385056
40 void usage_exit(
void) {
54 #define NUM_ENCODERS 3
57 #define MAX_NUM_TEMPORAL_LAYERS 3
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"
67 size_t nbytes, to_read;
70 to_read = img->
w*img->
h*3/2;
71 nbytes = fread(img->
planes[0], 1, to_read, f);
72 if(nbytes != to_read) {
75 printf(
"Warning: Read partial frame. Check your width & height!\n");
80 static int read_frame_by_row(FILE *f,
vpx_image_t *img) {
81 size_t nbytes, to_read;
85 for (plane = 0; plane < 3; plane++)
88 int w = (plane ? (1 + img->
d_w) / 2 : img->
d_w);
89 int h = (plane ? (1 + img->
d_h) / 2 : img->
d_h);
108 for (r = 0; r < h; r++)
112 nbytes = fread(ptr, 1, to_read, f);
113 if(nbytes != to_read) {
116 printf(
"Warning: Read partial frame. Check your width & height!\n");
120 ptr += img->
stride[plane];
129 static void write_ivf_file_header(FILE *outfile,
140 mem_put_le16(header+4, 0);
141 mem_put_le16(header+6, 32);
142 mem_put_le32(header+8, fourcc);
143 mem_put_le16(header+12, cfg->
g_w);
144 mem_put_le16(header+14, cfg->
g_h);
147 mem_put_le32(header+24, frame_cnt);
148 mem_put_le32(header+28, 0);
150 (void) fwrite(header, 1, 32, outfile);
153 static void write_ivf_frame_header(FILE *outfile,
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);
167 (void) fwrite(header, 1, 12, outfile);
175 static void set_temporal_layer_pattern(
int num_temporal_layers,
180 assert(num_temporal_layers <= MAX_NUM_TEMPORAL_LAYERS);
181 switch (num_temporal_layers)
234 layer_flags[4] = layer_flags[2];
237 layer_flags[5] = layer_flags[3];
240 layer_flags[6] = layer_flags[4];
243 layer_flags[7] = layer_flags[5];
297 layer_flags[5] = layer_flags[3];
304 layer_flags[7] = layer_flags[3];
311 static int periodicity_to_num_layers[MAX_NUM_TEMPORAL_LAYERS] = {1, 8, 8};
313 int main(
int argc,
char **argv)
315 FILE *infile, *outfile[NUM_ENCODERS];
316 FILE *downsampled_input[NUM_ENCODERS - 1];
335 int flag_periodicity;
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};
356 unsigned int target_bitrate[NUM_ENCODERS]={1000, 500, 100};
369 unsigned int num_temporal_layers[NUM_ENCODERS] = {3, 3, 3};
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",
378 width = strtol(argv[1], NULL, 0);
379 height = strtol(argv[2], NULL, 0);
380 framerate = strtol(argv[3], NULL, 0);
382 if(width < 16 || width%2 || height <16 || height%2)
383 die(
"Invalid resolution: %ldx%ld", width, height);
386 if(!(infile = fopen(argv[4],
"rb")))
387 die(
"Failed to open %s for reading", argv[4]);
390 for (i=0; i< NUM_ENCODERS; i++)
392 if(!target_bitrate[i])
398 if(!(outfile[i] = fopen(argv[i+5],
"wb")))
399 die(
"Failed to open %s for writing", argv[i+4]);
403 for (i=0; i< NUM_ENCODERS; i++)
405 target_bitrate[i] = strtol(argv[NUM_ENCODERS + 5 + i], NULL, 0);
409 for (i=0; i< NUM_ENCODERS; i++)
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);
418 for (i=0; i< NUM_ENCODERS - 1; i++)
421 if (sprintf(filename,
"ds%d.yuv",NUM_ENCODERS - i) < 0)
425 downsampled_input[i] = fopen(filename,
"wb");
428 key_frame_insert = strtol(argv[3 * NUM_ENCODERS + 5], NULL, 0);
430 show_psnr = strtol(argv[3 * NUM_ENCODERS + 6], NULL, 0);
434 for (i=0; i< NUM_ENCODERS; i++)
475 for (i=1; i< NUM_ENCODERS; i++)
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;
494 if((cfg[i].g_w)%2)cfg[i].
g_w++;
495 if((cfg[i].g_h)%2)cfg[i].
g_h++;
506 for (i=0; i< NUM_ENCODERS; i++)
508 die(
"Failed to allocate image", cfg[i].g_w, cfg[i].g_h);
510 if (raw[0].stride[VPX_PLANE_Y] == raw[0].d_w)
511 read_frame_p = read_frame;
513 read_frame_p = read_frame_by_row;
515 for (i=0; i< NUM_ENCODERS; i++)
517 write_ivf_file_header(outfile[i], &cfg[i], 0);
520 for ( i=0; i<NUM_ENCODERS; i++)
522 set_temporal_layer_pattern(num_temporal_layers[i],
524 cfg[i].rc_target_bitrate,
530 (show_psnr ? VPX_CODEC_USE_PSNR : 0), &dsf[0]))
531 die_codec(&codec[0],
"Failed to initialize encoder");
535 for ( i=0; i<NUM_ENCODERS; i++)
539 if (i == NUM_ENCODERS - 1) speed = -4;
541 die_codec(&codec[i],
"Failed to set cpu_used");
545 for ( i=0; i<NUM_ENCODERS; i++)
548 die_codec(&codec[i],
"Failed to set static threshold");
554 die_codec(&codec[0],
"Failed to set noise_sensitivity");
555 for ( i=1; i< NUM_ENCODERS; i++)
558 die_codec(&codec[i],
"Failed to set noise_sensitivity");
562 for ( i=0; i<NUM_ENCODERS; i++)
565 die_codec(&codec[i],
"Failed to set static threshold");
569 for ( i=0; i<NUM_ENCODERS; i++)
571 unsigned int max_intra_size_pct =
572 (int)(((
double)cfg[0].rc_buf_optimal_sz * 0.5) * framerate / 10);
575 die_codec(&codec[i],
"Failed to set static threshold");
582 while(frame_avail || got_data)
584 struct vpx_usec_timer timer;
589 frame_avail = read_frame_p(infile, &raw[0]);
593 for ( i=1; i<NUM_ENCODERS; i++)
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);
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]) !=
617 for ( i=0; i<NUM_ENCODERS; i++)
621 flag_periodicity = periodicity_to_num_layers
622 [num_temporal_layers[i] - 1];
624 frame_cnt % flag_periodicity];
630 if (frame_cnt > 0 && frame_cnt == key_frame_insert)
642 vpx_usec_timer_start(&timer);
644 frame_cnt, 1, 0, arg_deadline))
646 die_codec(&codec[0],
"Failed to encode frame");
648 vpx_usec_timer_mark(&timer);
649 cx_time += vpx_usec_timer_elapsed(&timer);
651 for (i=NUM_ENCODERS-1; i>=0 ; i--)
657 switch(pkt[i]->kind) {
659 write_ivf_frame_header(outfile[i], pkt[i]);
660 (void) fwrite(pkt[i]->data.
frame.buf, 1,
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++)
672 psnr_totals[i][j] += pkt[i]->
data.
psnr.psnr[j];
689 printf(
"Frame cnt and encoding time/FPS stats for encoding: %d %f %f \n",
691 1000 * (
float)cx_time / (
double)(frame_cnt * 1000000),
692 1000000 * (
double)frame_cnt / (
double)cx_time);
696 printf(
"Processed %ld frames.\n",(
long int)frame_cnt-1);
697 for (i=0; i< NUM_ENCODERS; i++)
700 if ( (show_psnr) && (psnr_count[i]>0) )
703 double ovpsnr = sse_to_psnr(psnr_samples_total[i], 255.0,
706 fprintf(stderr,
"\n ENC%d PSNR (Overall/Avg/Y/U/V)", i);
708 fprintf(stderr,
" %.3lf", ovpsnr);
709 for (j = 0; j < 4; j++)
711 fprintf(stderr,
" %.3lf", psnr_totals[i][j]/psnr_count[i]);
716 die_codec(&codec[i],
"Failed to destroy codec");
724 if(!fseek(outfile[i], 0, SEEK_SET))
725 write_ivf_file_header(outfile[i], &cfg[i], frame_cnt-1);
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