onyx_int.h revision ba164dffc5a6795bce97fae02b51ccf3330e15e4
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 frame_rate;
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    /* frame in src_buffers has been identified to be encoded as an alt ref */
324    int source_alt_ref_pending;
325    /* an alt ref frame has been encoded and is usable */
326    int source_alt_ref_active;
327    /* source of frame to encode is an exact copy of an alt ref frame */
328    int is_src_frame_alt_ref;
329
330    /* golden frame same as last frame ( short circuit gold searches) */
331    int gold_is_last;
332    /* Alt reference frame same as last ( short circuit altref search) */
333    int alt_is_last;
334    /* don't do both alt and gold search ( just do gold). */
335    int gold_is_alt;
336
337    YV12_BUFFER_CONFIG pick_lf_lvl_frame;
338
339    TOKENEXTRA *tok;
340    unsigned int tok_count;
341
342
343    unsigned int frames_since_key;
344    unsigned int key_frame_frequency;
345    unsigned int this_key_frame_forced;
346    unsigned int next_key_frame_forced;
347
348    /* Ambient reconstruction err target for force key frames */
349    int ambient_err;
350
351    unsigned int mode_check_freq[MAX_MODES];
352
353    int rd_baseline_thresh[MAX_MODES];
354
355    int RDMULT;
356    int RDDIV ;
357
358    CODING_CONTEXT coding_context;
359
360    /* Rate targetting variables */
361    int64_t last_prediction_error;
362    int64_t last_intra_error;
363
364    int this_frame_target;
365    int projected_frame_size;
366    int last_q[2];                   /* Separate values for Intra/Inter */
367
368    double rate_correction_factor;
369    double key_frame_rate_correction_factor;
370    double gf_rate_correction_factor;
371
372    /* Count down till next GF */
373    int frames_till_gf_update_due;
374
375    /* GF interval chosen when we coded the last GF */
376    int current_gf_interval;
377
378    /* Total bits overspent becasue of GF boost (cumulative) */
379    int gf_overspend_bits;
380
381    /* Used in the few frames following a GF to recover the extra bits
382     * spent in that GF
383     */
384    int non_gf_bitrate_adjustment;
385
386    /* Extra bits spent on key frames that need to be recovered */
387    int kf_overspend_bits;
388
389    /* Current number of bit s to try and recover on each inter frame. */
390    int kf_bitrate_adjustment;
391    int max_gf_interval;
392    int baseline_gf_interval;
393    int active_arnr_frames;
394
395    int64_t key_frame_count;
396    int prior_key_frame_distance[KEY_FRAME_CONTEXT];
397    /* Current section per frame bandwidth target */
398    int per_frame_bandwidth;
399    /* Average frame size target for clip */
400    int av_per_frame_bandwidth;
401    /* Minimum allocation that should be used for any frame */
402    int min_frame_bandwidth;
403    int inter_frame_target;
404    double output_frame_rate;
405    int64_t last_time_stamp_seen;
406    int64_t last_end_time_stamp_seen;
407    int64_t first_time_stamp_ever;
408
409    int ni_av_qi;
410    int ni_tot_qi;
411    int ni_frames;
412    int avg_frame_qindex;
413
414    int64_t total_byte_count;
415
416    int buffered_mode;
417
418    double frame_rate;
419    double ref_frame_rate;
420    int64_t buffer_level;
421    int64_t bits_off_target;
422
423    int rolling_target_bits;
424    int rolling_actual_bits;
425
426    int long_rolling_target_bits;
427    int long_rolling_actual_bits;
428
429    int64_t total_actual_bits;
430    int total_target_vs_actual; /* debug stats */
431
432    int worst_quality;
433    int active_worst_quality;
434    int best_quality;
435    int active_best_quality;
436
437    int cq_target_quality;
438
439    int drop_frames_allowed; /* Are we permitted to drop frames? */
440    int drop_frame;          /* Drop this frame? */
441
442    vp8_prob frame_coef_probs [BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [ENTROPY_NODES];
443    char update_probs [BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [ENTROPY_NODES];
444
445    unsigned int frame_branch_ct [BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [ENTROPY_NODES][2];
446
447    int gfu_boost;
448    int kf_boost;
449    int last_boost;
450
451    int target_bandwidth;
452    struct vpx_codec_pkt_list  *output_pkt_list;
453
454#if 0
455    /* Experimental code for lagged and one pass */
456    ONEPASS_FRAMESTATS one_pass_frame_stats[MAX_LAG_BUFFERS];
457    int one_pass_frame_index;
458#endif
459
460    int decimation_factor;
461    int decimation_count;
462
463    /* for real time encoding */
464    int avg_encode_time;     /* microsecond */
465    int avg_pick_mode_time;  /* microsecond */
466    int Speed;
467    int compressor_speed;
468
469    int auto_gold;
470    int auto_adjust_gold_quantizer;
471    int auto_worst_q;
472    int cpu_used;
473    int pass;
474
475
476    int prob_intra_coded;
477    int prob_last_coded;
478    int prob_gf_coded;
479    int prob_skip_false;
480    int last_skip_false_probs[3];
481    int last_skip_probs_q[3];
482    int recent_ref_frame_usage[MAX_REF_FRAMES];
483
484    int this_frame_percent_intra;
485    int last_frame_percent_intra;
486
487    int ref_frame_flags;
488
489    SPEED_FEATURES sf;
490
491    /* Count ZEROMV on all reference frames. */
492    int zeromv_count;
493    int lf_zeromv_pct;
494
495    unsigned char *segmentation_map;
496    signed char segment_feature_data[MB_LVL_MAX][MAX_MB_SEGMENTS];
497    int  segment_encode_breakout[MAX_MB_SEGMENTS];
498
499    unsigned char *active_map;
500    unsigned int active_map_enabled;
501
502    /* Video conferencing cyclic refresh mode flags. This is a mode
503     * designed to clean up the background over time in live encoding
504     * scenarious. It uses segmentation.
505     */
506    int cyclic_refresh_mode_enabled;
507    int cyclic_refresh_mode_max_mbs_perframe;
508    int cyclic_refresh_mode_index;
509    int cyclic_refresh_q;
510    signed char *cyclic_refresh_map;
511
512    // Frame counter for the temporal pattern. Counter is rest when the temporal
513    // layers are changed dynamically (run-time change).
514    unsigned int temporal_pattern_counter;
515
516#if CONFIG_MULTITHREAD
517    /* multithread data */
518    int * mt_current_mb_col;
519    int mt_sync_range;
520    int b_multi_threaded;
521    int encoding_thread_count;
522    int b_lpf_running;
523
524    pthread_t *h_encoding_thread;
525    pthread_t h_filter_thread;
526
527    MB_ROW_COMP *mb_row_ei;
528    ENCODETHREAD_DATA *en_thread_data;
529    LPFTHREAD_DATA lpf_thread_data;
530
531    /* events */
532    sem_t *h_event_start_encoding;
533    sem_t h_event_end_encoding;
534    sem_t h_event_start_lpf;
535    sem_t h_event_end_lpf;
536#endif
537
538    TOKENLIST *tplist;
539    unsigned int partition_sz[MAX_PARTITIONS];
540    unsigned char *partition_d[MAX_PARTITIONS];
541    unsigned char *partition_d_end[MAX_PARTITIONS];
542
543
544    fractional_mv_step_fp *find_fractional_mv_step;
545    vp8_full_search_fn_t full_search_sad;
546    vp8_refining_search_fn_t refining_search_sad;
547    vp8_diamond_search_fn_t diamond_search_sad;
548    vp8_variance_fn_ptr_t fn_ptr[BLOCK_MAX_SEGMENTS];
549    uint64_t time_receive_data;
550    uint64_t time_compress_data;
551    uint64_t time_pick_lpf;
552    uint64_t time_encode_mb_row;
553
554    int base_skip_false_prob[128];
555
556    FRAME_CONTEXT lfc_n; /* last frame entropy */
557    FRAME_CONTEXT lfc_a; /* last alt ref entropy */
558    FRAME_CONTEXT lfc_g; /* last gold ref entropy */
559
560
561    struct twopass_rc
562    {
563        unsigned int section_intra_rating;
564        double section_max_qfactor;
565        unsigned int next_iiratio;
566        unsigned int this_iiratio;
567        FIRSTPASS_STATS total_stats;
568        FIRSTPASS_STATS this_frame_stats;
569        FIRSTPASS_STATS *stats_in, *stats_in_end, *stats_in_start;
570        FIRSTPASS_STATS total_left_stats;
571        int first_pass_done;
572        int64_t bits_left;
573        int64_t clip_bits_total;
574        double avg_iiratio;
575        double modified_error_total;
576        double modified_error_used;
577        double modified_error_left;
578        double kf_intra_err_min;
579        double gf_intra_err_min;
580        int frames_to_key;
581        int maxq_max_limit;
582        int maxq_min_limit;
583        int gf_decay_rate;
584        int static_scene_max_gf_interval;
585        int kf_bits;
586        /* Remaining error from uncoded frames in a gf group. */
587        int gf_group_error_left;
588        /* Projected total bits available for a key frame group of frames */
589        int64_t kf_group_bits;
590        /* Error score of frames still to be coded in kf group */
591        int64_t kf_group_error_left;
592        /* Projected Bits available for a group including 1 GF or ARF */
593        int64_t gf_group_bits;
594        /* Bits for the golden frame or ARF */
595        int gf_bits;
596        int alt_extra_bits;
597        double est_max_qcorrection_factor;
598    } twopass;
599
600#if VP8_TEMPORAL_ALT_REF
601    YV12_BUFFER_CONFIG alt_ref_buffer;
602    YV12_BUFFER_CONFIG *frames[MAX_LAG_BUFFERS];
603    int fixed_divide[512];
604#endif
605
606#if CONFIG_INTERNAL_STATS
607    int    count;
608    double total_y;
609    double total_u;
610    double total_v;
611    double total ;
612    double total_sq_error;
613    double totalp_y;
614    double totalp_u;
615    double totalp_v;
616    double totalp;
617    double total_sq_error2;
618    int    bytes;
619    double summed_quality;
620    double summed_weights;
621    unsigned int tot_recode_hits;
622
623
624    double total_ssimg_y;
625    double total_ssimg_u;
626    double total_ssimg_v;
627    double total_ssimg_all;
628
629    int b_calculate_ssimg;
630#endif
631    int b_calculate_psnr;
632
633    /* Per MB activity measurement */
634    unsigned int activity_avg;
635    unsigned int * mb_activity_map;
636
637    /* Record of which MBs still refer to last golden frame either
638     * directly or through 0,0
639     */
640    unsigned char *gf_active_flags;
641    int gf_active_count;
642
643    int output_partition;
644
645    /* Store last frame's MV info for next frame MV prediction */
646    int_mv *lfmv;
647    int *lf_ref_frame_sign_bias;
648    int *lf_ref_frame;
649
650    /* force next frame to intra when kf_auto says so */
651    int force_next_frame_intra;
652
653    int droppable;
654
655#if CONFIG_TEMPORAL_DENOISING
656    VP8_DENOISER denoiser;
657#endif
658
659    /* Coding layer state variables */
660    unsigned int current_layer;
661    LAYER_CONTEXT layer_context[VPX_TS_MAX_LAYERS];
662
663    int64_t frames_in_layer[VPX_TS_MAX_LAYERS];
664    int64_t bytes_in_layer[VPX_TS_MAX_LAYERS];
665    double sum_psnr[VPX_TS_MAX_LAYERS];
666    double sum_psnr_p[VPX_TS_MAX_LAYERS];
667    double total_error2[VPX_TS_MAX_LAYERS];
668    double total_error2_p[VPX_TS_MAX_LAYERS];
669    double sum_ssim[VPX_TS_MAX_LAYERS];
670    double sum_weights[VPX_TS_MAX_LAYERS];
671
672    double total_ssimg_y_in_layer[VPX_TS_MAX_LAYERS];
673    double total_ssimg_u_in_layer[VPX_TS_MAX_LAYERS];
674    double total_ssimg_v_in_layer[VPX_TS_MAX_LAYERS];
675    double total_ssimg_all_in_layer[VPX_TS_MAX_LAYERS];
676
677#if CONFIG_MULTI_RES_ENCODING
678    /* Number of MBs per row at lower-resolution level */
679    int    mr_low_res_mb_cols;
680    /* Indicate if lower-res mv info is available */
681    unsigned char  mr_low_res_mv_avail;
682    /* The frame number of each reference frames */
683    unsigned int current_ref_frames[MAX_REF_FRAMES];
684#endif
685
686    struct rd_costs_struct
687    {
688        int mvcosts[2][MVvals+1];
689        int mvsadcosts[2][MVfpvals+1];
690        int mbmode_cost[2][MB_MODE_COUNT];
691        int intra_uv_mode_cost[2][MB_MODE_COUNT];
692        int bmode_costs[10][10][10];
693        int inter_bmode_costs[B_MODE_COUNT];
694        int token_costs[BLOCK_TYPES][COEF_BANDS]
695        [PREV_COEF_CONTEXTS][MAX_ENTROPY_TOKENS];
696    } rd_costs;
697} VP8_COMP;
698
699void vp8_pack_bitstream(VP8_COMP *cpi, unsigned char *dest,
700                        unsigned char *dest_end, unsigned long *size);
701
702void vp8_tokenize_mb(VP8_COMP *, MACROBLOCK *, TOKENEXTRA **);
703
704void vp8_set_speed_features(VP8_COMP *cpi);
705
706#if CONFIG_DEBUG
707#define CHECK_MEM_ERROR(lval,expr) do {\
708        lval = (expr); \
709        if(!lval) \
710            vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,\
711                               "Failed to allocate "#lval" at %s:%d", \
712                               __FILE__,__LINE__);\
713    } while(0)
714#else
715#define CHECK_MEM_ERROR(lval,expr) do {\
716        lval = (expr); \
717        if(!lval) \
718            vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,\
719                               "Failed to allocate "#lval);\
720    } while(0)
721#endif
722#endif
723