onyx_int.h revision 91037db265ecdd914a26e056cf69207b4f50924e
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#ifndef __INC_VP8_INT_H
13#define __INC_VP8_INT_H
14
15#include <stdio.h>
16#include "vpx_config.h"
17#include "vp8/common/onyx.h"
18#include "treewriter.h"
19#include "tokenize.h"
20#include "vp8/common/onyxc_int.h"
21#include "vp8/common/variance.h"
22#include "encodemb.h"
23#include "quantize.h"
24#include "vp8/common/entropy.h"
25#include "vp8/common/threading.h"
26#include "vpx_ports/mem.h"
27#include "vpx/internal/vpx_codec_internal.h"
28#include "vpx/vp8.h"
29#include "mcomp.h"
30#include "vp8/common/findnearmv.h"
31#include "lookahead.h"
32#if CONFIG_TEMPORAL_DENOISING
33#include "vp8/encoder/denoising.h"
34#endif
35
36#define MIN_GF_INTERVAL             4
37#define DEFAULT_GF_INTERVAL         7
38
39#define KEY_FRAME_CONTEXT 5
40
41#define MAX_LAG_BUFFERS (CONFIG_REALTIME_ONLY? 1 : 25)
42
43#define AF_THRESH   25
44#define AF_THRESH2  100
45#define ARF_DECAY_THRESH 12
46
47
48#define MIN_THRESHMULT  32
49#define MAX_THRESHMULT  512
50
51#define GF_ZEROMV_ZBIN_BOOST 12
52#define LF_ZEROMV_ZBIN_BOOST 6
53#define MV_ZBIN_BOOST        4
54#define ZBIN_OQ_MAX 192
55
56#if !(CONFIG_REALTIME_ONLY)
57#define VP8_TEMPORAL_ALT_REF 1
58#endif
59
60#define MAX(x,y) (((x)>(y))?(x):(y))
61#define MIN(x,y) (((x)<(y))?(x):(y))
62
63typedef struct
64{
65    int kf_indicated;
66    unsigned int frames_since_key;
67    unsigned int frames_since_golden;
68    int filter_level;
69    int frames_till_gf_update_due;
70    int recent_ref_frame_usage[MAX_REF_FRAMES];
71
72    MV_CONTEXT mvc[2];
73    int mvcosts[2][MVvals+1];
74
75#ifdef MODE_STATS
76    int y_modes[5];
77    int uv_modes[4];
78    int b_modes[10];
79    int inter_y_modes[10];
80    int inter_uv_modes[4];
81    int inter_b_modes[10];
82#endif
83
84    vp8_prob ymode_prob[4], uv_mode_prob[3];   /* interframe intra mode probs */
85    vp8_prob kf_ymode_prob[4], kf_uv_mode_prob[3];   /* keyframe "" */
86
87    int ymode_count[5], uv_mode_count[4];  /* intra MB type cts this frame */
88
89    int count_mb_ref_frame_usage[MAX_REF_FRAMES];
90
91    int this_frame_percent_intra;
92    int last_frame_percent_intra;
93
94
95} CODING_CONTEXT;
96
97typedef struct
98{
99    double frame;
100    double intra_error;
101    double coded_error;
102    double ssim_weighted_pred_err;
103    double pcnt_inter;
104    double pcnt_motion;
105    double pcnt_second_ref;
106    double pcnt_neutral;
107    double MVr;
108    double mvr_abs;
109    double MVc;
110    double mvc_abs;
111    double MVrv;
112    double MVcv;
113    double mv_in_out_count;
114    double new_mv_count;
115    double duration;
116    double count;
117}
118FIRSTPASS_STATS;
119
120typedef struct
121{
122    int frames_so_far;
123    double frame_intra_error;
124    double frame_coded_error;
125    double frame_pcnt_inter;
126    double frame_pcnt_motion;
127    double frame_mvr;
128    double frame_mvr_abs;
129    double frame_mvc;
130    double frame_mvc_abs;
131
132} ONEPASS_FRAMESTATS;
133
134
135typedef enum
136{
137    THR_ZERO1          = 0,
138    THR_DC             = 1,
139
140    THR_NEAREST1       = 2,
141    THR_NEAR1          = 3,
142
143    THR_ZERO2          = 4,
144    THR_NEAREST2       = 5,
145
146    THR_ZERO3          = 6,
147    THR_NEAREST3       = 7,
148
149    THR_NEAR2          = 8,
150    THR_NEAR3          = 9,
151
152    THR_V_PRED         = 10,
153    THR_H_PRED         = 11,
154    THR_TM             = 12,
155
156    THR_NEW1           = 13,
157    THR_NEW2           = 14,
158    THR_NEW3           = 15,
159
160    THR_SPLIT1         = 16,
161    THR_SPLIT2         = 17,
162    THR_SPLIT3         = 18,
163
164    THR_B_PRED         = 19
165}
166THR_MODES;
167
168typedef enum
169{
170    DIAMOND = 0,
171    NSTEP = 1,
172    HEX = 2
173} SEARCH_METHODS;
174
175typedef struct
176{
177    int RD;
178    SEARCH_METHODS search_method;
179    int improved_quant;
180    int improved_dct;
181    int auto_filter;
182    int recode_loop;
183    int iterative_sub_pixel;
184    int half_pixel_search;
185    int quarter_pixel_search;
186    int thresh_mult[MAX_MODES];
187    int max_step_search_steps;
188    int first_step;
189    int optimize_coefficients;
190
191    int use_fastquant_for_pick;
192    int no_skip_block4x4_search;
193    int improved_mv_pred;
194
195} SPEED_FEATURES;
196
197typedef struct
198{
199    MACROBLOCK  mb;
200    int segment_counts[MAX_MB_SEGMENTS];
201    int totalrate;
202} MB_ROW_COMP;
203
204typedef struct
205{
206    TOKENEXTRA *start;
207    TOKENEXTRA *stop;
208} TOKENLIST;
209
210typedef struct
211{
212    int ithread;
213    void *ptr1;
214    void *ptr2;
215} ENCODETHREAD_DATA;
216typedef struct
217{
218    int ithread;
219    void *ptr1;
220} LPFTHREAD_DATA;
221
222enum
223{
224    BLOCK_16X8,
225    BLOCK_8X16,
226    BLOCK_8X8,
227    BLOCK_4X4,
228    BLOCK_16X16,
229    BLOCK_MAX_SEGMENTS
230};
231
232typedef struct
233{
234    /* Layer configuration */
235    double framerate;
236    int target_bandwidth;
237
238    /* Layer specific coding parameters */
239    int64_t starting_buffer_level;
240    int64_t optimal_buffer_level;
241    int64_t maximum_buffer_size;
242    int64_t starting_buffer_level_in_ms;
243    int64_t optimal_buffer_level_in_ms;
244    int64_t maximum_buffer_size_in_ms;
245
246    int avg_frame_size_for_layer;
247
248    int64_t buffer_level;
249    int64_t bits_off_target;
250
251    int64_t total_actual_bits;
252    int total_target_vs_actual;
253
254    int worst_quality;
255    int active_worst_quality;
256    int best_quality;
257    int active_best_quality;
258
259    int ni_av_qi;
260    int ni_tot_qi;
261    int ni_frames;
262    int avg_frame_qindex;
263
264    double rate_correction_factor;
265    double key_frame_rate_correction_factor;
266    double gf_rate_correction_factor;
267
268    int zbin_over_quant;
269
270    int inter_frame_target;
271    int64_t total_byte_count;
272
273    int filter_level;
274
275    int last_frame_percent_intra;
276
277    int count_mb_ref_frame_usage[MAX_REF_FRAMES];
278
279} LAYER_CONTEXT;
280
281typedef struct VP8_COMP
282{
283
284    DECLARE_ALIGNED(16, short, Y1quant[QINDEX_RANGE][16]);
285    DECLARE_ALIGNED(16, short, Y1quant_shift[QINDEX_RANGE][16]);
286    DECLARE_ALIGNED(16, short, Y1zbin[QINDEX_RANGE][16]);
287    DECLARE_ALIGNED(16, short, Y1round[QINDEX_RANGE][16]);
288
289    DECLARE_ALIGNED(16, short, Y2quant[QINDEX_RANGE][16]);
290    DECLARE_ALIGNED(16, short, Y2quant_shift[QINDEX_RANGE][16]);
291    DECLARE_ALIGNED(16, short, Y2zbin[QINDEX_RANGE][16]);
292    DECLARE_ALIGNED(16, short, Y2round[QINDEX_RANGE][16]);
293
294    DECLARE_ALIGNED(16, short, UVquant[QINDEX_RANGE][16]);
295    DECLARE_ALIGNED(16, short, UVquant_shift[QINDEX_RANGE][16]);
296    DECLARE_ALIGNED(16, short, UVzbin[QINDEX_RANGE][16]);
297    DECLARE_ALIGNED(16, short, UVround[QINDEX_RANGE][16]);
298
299    DECLARE_ALIGNED(16, short, zrun_zbin_boost_y1[QINDEX_RANGE][16]);
300    DECLARE_ALIGNED(16, short, zrun_zbin_boost_y2[QINDEX_RANGE][16]);
301    DECLARE_ALIGNED(16, short, zrun_zbin_boost_uv[QINDEX_RANGE][16]);
302    DECLARE_ALIGNED(16, short, Y1quant_fast[QINDEX_RANGE][16]);
303    DECLARE_ALIGNED(16, short, Y2quant_fast[QINDEX_RANGE][16]);
304    DECLARE_ALIGNED(16, short, UVquant_fast[QINDEX_RANGE][16]);
305
306
307    MACROBLOCK mb;
308    VP8_COMMON common;
309    vp8_writer bc[9]; /* one boolcoder for each partition */
310
311    VP8_CONFIG oxcf;
312
313    struct lookahead_ctx    *lookahead;
314    struct lookahead_entry  *source;
315    struct lookahead_entry  *alt_ref_source;
316    struct lookahead_entry  *last_source;
317
318    YV12_BUFFER_CONFIG *Source;
319    YV12_BUFFER_CONFIG *un_scaled_source;
320    YV12_BUFFER_CONFIG scaled_source;
321    YV12_BUFFER_CONFIG *last_frame_unscaled_source;
322
323    unsigned int frames_till_alt_ref_frame;
324    /* frame in src_buffers has been identified to be encoded as an alt ref */
325    int source_alt_ref_pending;
326    /* an alt ref frame has been encoded and is usable */
327    int source_alt_ref_active;
328    /* source of frame to encode is an exact copy of an alt ref frame */
329    int is_src_frame_alt_ref;
330
331    /* golden frame same as last frame ( short circuit gold searches) */
332    int gold_is_last;
333    /* Alt reference frame same as last ( short circuit altref search) */
334    int alt_is_last;
335    /* don't do both alt and gold search ( just do gold). */
336    int gold_is_alt;
337
338    YV12_BUFFER_CONFIG pick_lf_lvl_frame;
339
340    TOKENEXTRA *tok;
341    unsigned int tok_count;
342
343
344    unsigned int frames_since_key;
345    unsigned int key_frame_frequency;
346    unsigned int this_key_frame_forced;
347    unsigned int next_key_frame_forced;
348
349    /* Ambient reconstruction err target for force key frames */
350    int ambient_err;
351
352    unsigned int mode_check_freq[MAX_MODES];
353
354    int rd_baseline_thresh[MAX_MODES];
355
356    int RDMULT;
357    int RDDIV ;
358
359    CODING_CONTEXT coding_context;
360
361    /* Rate targetting variables */
362    int64_t last_prediction_error;
363    int64_t last_intra_error;
364
365    int this_frame_target;
366    int projected_frame_size;
367    int last_q[2];                   /* Separate values for Intra/Inter */
368
369    double rate_correction_factor;
370    double key_frame_rate_correction_factor;
371    double gf_rate_correction_factor;
372
373    unsigned int frames_since_golden;
374    /* Count down till next GF */
375    int frames_till_gf_update_due;
376
377    /* GF interval chosen when we coded the last GF */
378    int current_gf_interval;
379
380    /* Total bits overspent becasue of GF boost (cumulative) */
381    int gf_overspend_bits;
382
383    /* Used in the few frames following a GF to recover the extra bits
384     * spent in that GF
385     */
386    int non_gf_bitrate_adjustment;
387
388    /* Extra bits spent on key frames that need to be recovered */
389    int kf_overspend_bits;
390
391    /* Current number of bit s to try and recover on each inter frame. */
392    int kf_bitrate_adjustment;
393    int max_gf_interval;
394    int baseline_gf_interval;
395    int active_arnr_frames;
396
397    int64_t key_frame_count;
398    int prior_key_frame_distance[KEY_FRAME_CONTEXT];
399    /* Current section per frame bandwidth target */
400    int per_frame_bandwidth;
401    /* Average frame size target for clip */
402    int av_per_frame_bandwidth;
403    /* Minimum allocation that should be used for any frame */
404    int min_frame_bandwidth;
405    int inter_frame_target;
406    double output_framerate;
407    int64_t last_time_stamp_seen;
408    int64_t last_end_time_stamp_seen;
409    int64_t first_time_stamp_ever;
410
411    int ni_av_qi;
412    int ni_tot_qi;
413    int ni_frames;
414    int avg_frame_qindex;
415
416    int64_t total_byte_count;
417
418    int buffered_mode;
419
420    double framerate;
421    double ref_framerate;
422    int64_t buffer_level;
423    int64_t bits_off_target;
424
425    int rolling_target_bits;
426    int rolling_actual_bits;
427
428    int long_rolling_target_bits;
429    int long_rolling_actual_bits;
430
431    int64_t total_actual_bits;
432    int total_target_vs_actual; /* debug stats */
433
434    int worst_quality;
435    int active_worst_quality;
436    int best_quality;
437    int active_best_quality;
438
439    int cq_target_quality;
440
441    int drop_frames_allowed; /* Are we permitted to drop frames? */
442    int drop_frame;          /* Drop this frame? */
443
444    vp8_prob frame_coef_probs [BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [ENTROPY_NODES];
445    char update_probs [BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [ENTROPY_NODES];
446
447    unsigned int frame_branch_ct [BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [ENTROPY_NODES][2];
448
449    int gfu_boost;
450    int kf_boost;
451    int last_boost;
452
453    int target_bandwidth;
454    struct vpx_codec_pkt_list  *output_pkt_list;
455
456#if 0
457    /* Experimental code for lagged and one pass */
458    ONEPASS_FRAMESTATS one_pass_frame_stats[MAX_LAG_BUFFERS];
459    int one_pass_frame_index;
460#endif
461
462    int decimation_factor;
463    int decimation_count;
464
465    /* for real time encoding */
466    int avg_encode_time;     /* microsecond */
467    int avg_pick_mode_time;  /* microsecond */
468    int Speed;
469    int compressor_speed;
470
471    int auto_gold;
472    int auto_adjust_gold_quantizer;
473    int auto_worst_q;
474    int cpu_used;
475    int pass;
476
477
478    int prob_intra_coded;
479    int prob_last_coded;
480    int prob_gf_coded;
481    int prob_skip_false;
482    int last_skip_false_probs[3];
483    int last_skip_probs_q[3];
484    int recent_ref_frame_usage[MAX_REF_FRAMES];
485
486    int this_frame_percent_intra;
487    int last_frame_percent_intra;
488
489    int ref_frame_flags;
490
491    SPEED_FEATURES sf;
492
493    /* Count ZEROMV on all reference frames. */
494    int zeromv_count;
495    int lf_zeromv_pct;
496
497    unsigned char *segmentation_map;
498    signed char segment_feature_data[MB_LVL_MAX][MAX_MB_SEGMENTS];
499    int  segment_encode_breakout[MAX_MB_SEGMENTS];
500
501    unsigned char *active_map;
502    unsigned int active_map_enabled;
503
504    /* Video conferencing cyclic refresh mode flags. This is a mode
505     * designed to clean up the background over time in live encoding
506     * scenarious. It uses segmentation.
507     */
508    int cyclic_refresh_mode_enabled;
509    int cyclic_refresh_mode_max_mbs_perframe;
510    int cyclic_refresh_mode_index;
511    int cyclic_refresh_q;
512    signed char *cyclic_refresh_map;
513
514    // Frame counter for the temporal pattern. Counter is rest when the temporal
515    // layers are changed dynamically (run-time change).
516    unsigned int temporal_pattern_counter;
517
518#if CONFIG_MULTITHREAD
519    /* multithread data */
520    int * mt_current_mb_col;
521    int mt_sync_range;
522    int b_multi_threaded;
523    int encoding_thread_count;
524    int b_lpf_running;
525
526    pthread_t *h_encoding_thread;
527    pthread_t h_filter_thread;
528
529    MB_ROW_COMP *mb_row_ei;
530    ENCODETHREAD_DATA *en_thread_data;
531    LPFTHREAD_DATA lpf_thread_data;
532
533    /* events */
534    sem_t *h_event_start_encoding;
535    sem_t h_event_end_encoding;
536    sem_t h_event_start_lpf;
537    sem_t h_event_end_lpf;
538#endif
539
540    TOKENLIST *tplist;
541    unsigned int partition_sz[MAX_PARTITIONS];
542    unsigned char *partition_d[MAX_PARTITIONS];
543    unsigned char *partition_d_end[MAX_PARTITIONS];
544
545
546    fractional_mv_step_fp *find_fractional_mv_step;
547    vp8_full_search_fn_t full_search_sad;
548    vp8_refining_search_fn_t refining_search_sad;
549    vp8_diamond_search_fn_t diamond_search_sad;
550    vp8_variance_fn_ptr_t fn_ptr[BLOCK_MAX_SEGMENTS];
551    uint64_t time_receive_data;
552    uint64_t time_compress_data;
553    uint64_t time_pick_lpf;
554    uint64_t time_encode_mb_row;
555
556    int base_skip_false_prob[128];
557
558    FRAME_CONTEXT lfc_n; /* last frame entropy */
559    FRAME_CONTEXT lfc_a; /* last alt ref entropy */
560    FRAME_CONTEXT lfc_g; /* last gold ref entropy */
561
562
563    struct twopass_rc
564    {
565        unsigned int section_intra_rating;
566        double section_max_qfactor;
567        unsigned int next_iiratio;
568        unsigned int this_iiratio;
569        FIRSTPASS_STATS total_stats;
570        FIRSTPASS_STATS this_frame_stats;
571        FIRSTPASS_STATS *stats_in, *stats_in_end, *stats_in_start;
572        FIRSTPASS_STATS total_left_stats;
573        int first_pass_done;
574        int64_t bits_left;
575        int64_t clip_bits_total;
576        double avg_iiratio;
577        double modified_error_total;
578        double modified_error_used;
579        double modified_error_left;
580        double kf_intra_err_min;
581        double gf_intra_err_min;
582        int frames_to_key;
583        int maxq_max_limit;
584        int maxq_min_limit;
585        int gf_decay_rate;
586        int static_scene_max_gf_interval;
587        int kf_bits;
588        /* Remaining error from uncoded frames in a gf group. */
589        int gf_group_error_left;
590        /* Projected total bits available for a key frame group of frames */
591        int64_t kf_group_bits;
592        /* Error score of frames still to be coded in kf group */
593        int64_t kf_group_error_left;
594        /* Projected Bits available for a group including 1 GF or ARF */
595        int64_t gf_group_bits;
596        /* Bits for the golden frame or ARF */
597        int gf_bits;
598        int alt_extra_bits;
599        double est_max_qcorrection_factor;
600    } twopass;
601
602#if VP8_TEMPORAL_ALT_REF
603    YV12_BUFFER_CONFIG alt_ref_buffer;
604    YV12_BUFFER_CONFIG *frames[MAX_LAG_BUFFERS];
605    int fixed_divide[512];
606#endif
607
608#if CONFIG_INTERNAL_STATS
609    int    count;
610    double total_y;
611    double total_u;
612    double total_v;
613    double total ;
614    double total_sq_error;
615    double totalp_y;
616    double totalp_u;
617    double totalp_v;
618    double totalp;
619    double total_sq_error2;
620    int    bytes;
621    double summed_quality;
622    double summed_weights;
623    unsigned int tot_recode_hits;
624
625
626    double total_ssimg_y;
627    double total_ssimg_u;
628    double total_ssimg_v;
629    double total_ssimg_all;
630
631    int b_calculate_ssimg;
632#endif
633    int b_calculate_psnr;
634
635    /* Per MB activity measurement */
636    unsigned int activity_avg;
637    unsigned int * mb_activity_map;
638
639    /* Record of which MBs still refer to last golden frame either
640     * directly or through 0,0
641     */
642    unsigned char *gf_active_flags;
643    int gf_active_count;
644
645    int output_partition;
646
647    /* Store last frame's MV info for next frame MV prediction */
648    int_mv *lfmv;
649    int *lf_ref_frame_sign_bias;
650    int *lf_ref_frame;
651
652    /* force next frame to intra when kf_auto says so */
653    int force_next_frame_intra;
654
655    int droppable;
656
657#if CONFIG_TEMPORAL_DENOISING
658    VP8_DENOISER denoiser;
659#endif
660
661    /* Coding layer state variables */
662    unsigned int current_layer;
663    LAYER_CONTEXT layer_context[VPX_TS_MAX_LAYERS];
664
665    int64_t frames_in_layer[VPX_TS_MAX_LAYERS];
666    int64_t bytes_in_layer[VPX_TS_MAX_LAYERS];
667    double sum_psnr[VPX_TS_MAX_LAYERS];
668    double sum_psnr_p[VPX_TS_MAX_LAYERS];
669    double total_error2[VPX_TS_MAX_LAYERS];
670    double total_error2_p[VPX_TS_MAX_LAYERS];
671    double sum_ssim[VPX_TS_MAX_LAYERS];
672    double sum_weights[VPX_TS_MAX_LAYERS];
673
674    double total_ssimg_y_in_layer[VPX_TS_MAX_LAYERS];
675    double total_ssimg_u_in_layer[VPX_TS_MAX_LAYERS];
676    double total_ssimg_v_in_layer[VPX_TS_MAX_LAYERS];
677    double total_ssimg_all_in_layer[VPX_TS_MAX_LAYERS];
678
679#if CONFIG_MULTI_RES_ENCODING
680    /* Number of MBs per row at lower-resolution level */
681    int    mr_low_res_mb_cols;
682    /* Indicate if lower-res mv info is available */
683    unsigned char  mr_low_res_mv_avail;
684    /* The frame number of each reference frames */
685    unsigned int current_ref_frames[MAX_REF_FRAMES];
686#endif
687
688    struct rd_costs_struct
689    {
690        int mvcosts[2][MVvals+1];
691        int mvsadcosts[2][MVfpvals+1];
692        int mbmode_cost[2][MB_MODE_COUNT];
693        int intra_uv_mode_cost[2][MB_MODE_COUNT];
694        int bmode_costs[10][10][10];
695        int inter_bmode_costs[B_MODE_COUNT];
696        int token_costs[BLOCK_TYPES][COEF_BANDS]
697        [PREV_COEF_CONTEXTS][MAX_ENTROPY_TOKENS];
698    } rd_costs;
699} VP8_COMP;
700
701void vp8_pack_bitstream(VP8_COMP *cpi, unsigned char *dest,
702                        unsigned char *dest_end, unsigned long *size);
703
704void vp8_tokenize_mb(VP8_COMP *, MACROBLOCK *, TOKENEXTRA **);
705
706void vp8_set_speed_features(VP8_COMP *cpi);
707
708#if CONFIG_DEBUG
709#define CHECK_MEM_ERROR(lval,expr) do {\
710        lval = (expr); \
711        if(!lval) \
712            vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,\
713                               "Failed to allocate "#lval" at %s:%d", \
714                               __FILE__,__LINE__);\
715    } while(0)
716#else
717#define CHECK_MEM_ERROR(lval,expr) do {\
718        lval = (expr); \
719        if(!lval) \
720            vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,\
721                               "Failed to allocate "#lval);\
722    } while(0)
723#endif
724#endif
725