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