• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files

hvirtual/quicktime/ffmpeg/libavcodec/mpegvideo.h

Go to the documentation of this file.
00001 /*
00002  * Generic DCT based hybrid video encoder
00003  * Copyright (c) 2000, 2001, 2002 Fabrice Bellard.
00004  * Copyright (c) 2002-2004 Michael Niedermayer
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2 of the License, or (at your option) any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with this library; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  */
00020 
00026 #ifndef AVCODEC_MPEGVIDEO_H
00027 #define AVCODEC_MPEGVIDEO_H
00028 
00029 #include "dsputil.h"
00030 #include "bitstream.h"
00031 
00032 #define FRAME_SKIPPED 100 
00033 
00034 enum OutputFormat {
00035     FMT_MPEG1,
00036     FMT_H261,
00037     FMT_H263,
00038     FMT_MJPEG, 
00039     FMT_H264,
00040 };
00041 
00042 #define EDGE_WIDTH 16
00043 
00044 #define MPEG_BUF_SIZE (16 * 1024)
00045 
00046 #define QMAT_SHIFT_MMX 16
00047 #define QMAT_SHIFT 22
00048 
00049 #define MAX_FCODE 7
00050 #define MAX_MV 2048
00051 
00052 #define MAX_THREADS 8
00053 
00054 #define MAX_PICTURE_COUNT 32
00055 
00056 #define ME_MAP_SIZE 64
00057 #define ME_MAP_SHIFT 3
00058 #define ME_MAP_MV_BITS 11
00059 
00060 /* run length table */
00061 #define MAX_RUN    64
00062 #define MAX_LEVEL  64
00063 
00064 #define I_TYPE FF_I_TYPE  
00065 #define P_TYPE FF_P_TYPE  
00066 #define B_TYPE FF_B_TYPE  
00067 #define S_TYPE FF_S_TYPE  
00068 #define SI_TYPE FF_SI_TYPE  
00069 #define SP_TYPE FF_SP_TYPE  
00070 
00071 #define MAX_MB_BYTES (30*16*16*3/8 + 120)
00072 
00073 typedef struct Predictor{
00074     double coeff;
00075     double count;
00076     double decay;
00077 } Predictor;
00078 
00079 typedef struct RateControlEntry{
00080     int pict_type;
00081     float qscale;
00082     int mv_bits;
00083     int i_tex_bits;
00084     int p_tex_bits;
00085     int misc_bits;
00086     uint64_t expected_bits;
00087     int new_pict_type;
00088     float new_qscale;
00089     int mc_mb_var_sum;
00090     int mb_var_sum;
00091     int i_count;
00092     int f_code;
00093     int b_code;
00094 }RateControlEntry;
00095 
00099 typedef struct RateControlContext{
00100     FILE *stats_file;
00101     int num_entries;              
00102     RateControlEntry *entry;
00103     double buffer_index;          
00104     Predictor pred[5];
00105     double short_term_qsum;       
00106     double short_term_qcount;     
00107     double pass1_rc_eq_output_sum;
00108     double pass1_wanted_bits;     
00109     double last_qscale;
00110     double last_qscale_for[5];    
00111     int last_mc_mb_var_sum;
00112     int last_mb_var_sum;
00113     uint64_t i_cplx_sum[5];
00114     uint64_t p_cplx_sum[5];
00115     uint64_t mv_bits_sum[5];
00116     uint64_t qscale_sum[5];
00117     int frame_count[5];
00118     int last_non_b_pict_type;
00119 }RateControlContext;
00120 
00124 typedef struct ScanTable{
00125     const uint8_t *scantable;
00126     uint8_t permutated[64];
00127     uint8_t raster_end[64];
00128 #ifdef ARCH_POWERPC
00129 
00130     uint8_t __align8 inverse[64];
00131 #endif
00132 } ScanTable;
00133 
00137 typedef struct Picture{
00138     FF_COMMON_FRAME
00139 
00143     uint8_t *interpolated[3];
00144     int16_t (*motion_val_base[2])[2];
00145     uint32_t *mb_type_base;
00146 #define MB_TYPE_INTRA MB_TYPE_INTRA4x4 //default mb_type if theres just one type
00147 #define IS_INTRA4x4(a)   ((a)&MB_TYPE_INTRA4x4)
00148 #define IS_INTRA16x16(a) ((a)&MB_TYPE_INTRA16x16)
00149 #define IS_PCM(a)        ((a)&MB_TYPE_INTRA_PCM)
00150 #define IS_INTRA(a)      ((a)&7)
00151 #define IS_INTER(a)      ((a)&(MB_TYPE_16x16|MB_TYPE_16x8|MB_TYPE_8x16|MB_TYPE_8x8))
00152 #define IS_SKIP(a)       ((a)&MB_TYPE_SKIP)
00153 #define IS_INTRA_PCM(a)  ((a)&MB_TYPE_INTRA_PCM)
00154 #define IS_INTERLACED(a) ((a)&MB_TYPE_INTERLACED)
00155 #define IS_DIRECT(a)     ((a)&MB_TYPE_DIRECT2)
00156 #define IS_GMC(a)        ((a)&MB_TYPE_GMC)
00157 #define IS_16X16(a)      ((a)&MB_TYPE_16x16)
00158 #define IS_16X8(a)       ((a)&MB_TYPE_16x8)
00159 #define IS_8X16(a)       ((a)&MB_TYPE_8x16)
00160 #define IS_8X8(a)        ((a)&MB_TYPE_8x8)
00161 #define IS_SUB_8X8(a)    ((a)&MB_TYPE_16x16) //note reused
00162 #define IS_SUB_8X4(a)    ((a)&MB_TYPE_16x8)  //note reused
00163 #define IS_SUB_4X8(a)    ((a)&MB_TYPE_8x16)  //note reused
00164 #define IS_SUB_4X4(a)    ((a)&MB_TYPE_8x8)   //note reused
00165 #define IS_ACPRED(a)     ((a)&MB_TYPE_ACPRED)
00166 #define IS_QUANT(a)      ((a)&MB_TYPE_QUANT)
00167 #define IS_DIR(a, part, list) ((a) & (MB_TYPE_P0L0<<((part)+2*(list))))
00168 #define USES_LIST(a, list) ((a) & ((MB_TYPE_P0L0|MB_TYPE_P1L0)<<(2*(list)))) 
00169 #define HAS_CBP(a)        ((a)&MB_TYPE_CBP)
00170 
00171     int field_poc[2];           
00172     int poc;                    
00173     int frame_num;              
00174     int pic_id;                 
00175     int long_ref;               
00176     int ref_poc[2][16];         
00177     int ref_count[2];           
00178 
00179     int mb_var_sum;             
00180     int mc_mb_var_sum;          
00181     uint16_t *mb_var;           
00182     uint16_t *mc_mb_var;        
00183     uint8_t *mb_mean;           
00184     int32_t *mb_cmp_score;      
00185     int b_frame_score;          /* */
00186 } Picture;
00187 
00188 typedef struct ParseContext{
00189     uint8_t *buffer;
00190     int index;
00191     int last_index;
00192     int buffer_size;
00193     uint32_t state;             
00194     int frame_start_found;
00195     int overread;               
00196     int overread_index;         
00197 } ParseContext;
00198 
00199 struct MpegEncContext;
00200 
00204 typedef struct MotionEstContext{
00205     AVCodecContext *avctx;
00206     int skip;                          
00207     int co_located_mv[4][2];           
00208     int direct_basis_mv[4][2];
00209     uint8_t *scratchpad;               
00210     uint8_t *best_mb;
00211     uint8_t *temp_mb[2];
00212     uint8_t *temp;
00213     int best_bits;
00214     uint32_t *map;                     
00215     uint32_t *score_map;               
00216     int map_generation;  
00217     int pre_penalty_factor;
00218     int penalty_factor;
00219     int sub_penalty_factor;
00220     int mb_penalty_factor;
00221     int flags;
00222     int sub_flags;
00223     int mb_flags;
00224     int pre_pass;                      
00225     int dia_size;
00226     int xmin;
00227     int xmax;
00228     int ymin;
00229     int ymax;
00230     int pred_x;
00231     int pred_y;
00232     uint8_t *src[4][4];
00233     uint8_t *ref[4][4];
00234     int stride;
00235     int uvstride;
00236     /* temp variables for picture complexity calculation */
00237     int mc_mb_var_sum_temp;
00238     int mb_var_sum_temp;
00239     int scene_change_score;
00240 /*    cmp, chroma_cmp;*/
00241     op_pixels_func (*hpel_put)[4];
00242     op_pixels_func (*hpel_avg)[4];
00243     qpel_mc_func (*qpel_put)[16];
00244     qpel_mc_func (*qpel_avg)[16];
00245     uint8_t (*mv_penalty)[MAX_MV*2+1];  
00246     uint8_t *current_mv_penalty;
00247     int (*sub_motion_search)(struct MpegEncContext * s,
00248                                   int *mx_ptr, int *my_ptr, int dmin,
00249                                   int src_index, int ref_index,
00250                                   int size, int h);
00251 }MotionEstContext;
00252 
00256 typedef struct MpegEncContext {
00257     struct AVCodecContext *avctx;
00258     /* the following parameters must be initialized before encoding */
00259     int width, height;
00260     int gop_size;
00261     int intra_only;   
00262     int bit_rate;     
00263     enum OutputFormat out_format; 
00264     int h263_pred;    
00265 
00266 /* the following codec id fields are deprecated in favor of codec_id */
00267     int h263_plus;    
00268     int h263_msmpeg4; 
00269     int h263_flv;     
00270     
00271     enum CodecID codec_id;     /* see CODEC_ID_xxx */
00272     int fixed_qscale; 
00273     int encoding;     
00274     int flags;        
00275     int flags2;       
00276     int max_b_frames; 
00277     int luma_elim_threshold;
00278     int chroma_elim_threshold;
00279     int strict_std_compliance; 
00280     int workaround_bugs;       
00281     /* the following fields are managed internally by the encoder */
00282 
00284     PutBitContext pb;
00285 
00286     /* sequence parameters */
00287     int context_initialized;
00288     int input_picture_number;  
00289     int coded_picture_number;  
00290     int picture_number;       //FIXME remove, unclear definition
00291     int picture_in_gop_number; 
00292     int b_frames_since_non_b;  
00293     int64_t user_specified_pts;
00294     int mb_width, mb_height;   
00295     int mb_stride;             
00296     int b8_stride;             
00297     int b4_stride;             
00298     int h_edge_pos, v_edge_pos;
00299     int mb_num;                
00300     int linesize;              
00301     int uvlinesize;            
00302     Picture *picture;          
00303     Picture **input_picture;   
00304     Picture **reordered_input_picture; 
00305     
00306     int start_mb_y;            
00307     int end_mb_y;              
00308     struct MpegEncContext *thread_context[MAX_THREADS];
00309     
00314     Picture last_picture;       
00315     
00320     Picture next_picture;
00321     
00326     Picture new_picture;
00327     
00332     Picture current_picture;    
00333     
00334     Picture *last_picture_ptr;     
00335     Picture *next_picture_ptr;     
00336     Picture *current_picture_ptr;  
00337     uint8_t *visualization_buffer[3]; //< temporary buffer vor MV visualization
00338     int last_dc[3];                
00339     int16_t *dc_val_base;
00340     int16_t *dc_val[3];            
00341     int16_t dc_cache[4*5];
00342     int y_dc_scale, c_dc_scale;
00343     const uint8_t *y_dc_scale_table;     
00344     const uint8_t *c_dc_scale_table;     
00345     const uint8_t *chroma_qscale_table;  
00346     uint8_t *coded_block_base;
00347     uint8_t *coded_block;          
00348     int16_t (*ac_val_base)[16];
00349     int16_t (*ac_val[3])[16];      
00350     int ac_pred;
00351     uint8_t *prev_pict_types;     
00352 #define PREV_PICT_TYPES_BUFFER_SIZE 256
00353     int mb_skipped;                
00354     uint8_t *mbskip_table;        
00356     uint8_t *mbintra_table;       
00357     uint8_t *cbp_table;           
00358     uint8_t *pred_dir_table;      
00359     uint8_t *allocated_edge_emu_buffer;
00360     uint8_t *edge_emu_buffer;     
00361     uint8_t *rd_scratchpad;       
00362     uint8_t *obmc_scratchpad;
00363     uint8_t *b_scratchpad;        
00364 
00365     int qscale;                 
00366     int chroma_qscale;          
00367     int lambda;                 
00368     int lambda2;                
00369     int *lambda_table;
00370     int adaptive_quant;         
00371     int dquant;                 
00372     int pict_type;              
00373     int last_pict_type; //FIXME removes
00374     int last_non_b_pict_type;   
00375     int dropable;
00376     int frame_rate_index;
00377 
00378     /* motion compensation */
00379     int unrestricted_mv;        
00380     int h263_long_vectors;      
00381     int decode;                 
00382 
00383     DSPContext dsp;             
00384     int f_code;                 
00385     int b_code;                 
00386     int16_t (*p_mv_table_base)[2];
00387     int16_t (*b_forw_mv_table_base)[2];
00388     int16_t (*b_back_mv_table_base)[2];
00389     int16_t (*b_bidir_forw_mv_table_base)[2]; 
00390     int16_t (*b_bidir_back_mv_table_base)[2]; 
00391     int16_t (*b_direct_mv_table_base)[2];
00392     int16_t (*p_field_mv_table_base[2][2])[2];
00393     int16_t (*b_field_mv_table_base[2][2][2])[2];
00394     int16_t (*p_mv_table)[2];            
00395     int16_t (*b_forw_mv_table)[2];       
00396     int16_t (*b_back_mv_table)[2];       
00397     int16_t (*b_bidir_forw_mv_table)[2]; 
00398     int16_t (*b_bidir_back_mv_table)[2]; 
00399     int16_t (*b_direct_mv_table)[2];     
00400     int16_t (*p_field_mv_table[2][2])[2];   
00401     int16_t (*b_field_mv_table[2][2][2])[2];
00402     uint8_t (*p_field_select_table[2]);
00403     uint8_t (*b_field_select_table[2][2]);
00404     int me_method;                       
00405     int mv_dir;
00406 #define MV_DIR_BACKWARD  1
00407 #define MV_DIR_FORWARD   2
00408 #define MV_DIRECT        4 
00409     int mv_type;
00410 #define MV_TYPE_16X16       0   
00411 #define MV_TYPE_8X8         1   
00412 #define MV_TYPE_16X8        2   
00413 #define MV_TYPE_FIELD       3   
00414 #define MV_TYPE_DMV         4   
00415 
00420     int mv[2][4][2];
00421     int field_select[2][2];
00422     int last_mv[2][2][2];             
00423     uint8_t *fcode_tab;               
00424     
00425     MotionEstContext me;
00426 
00427     int no_rounding;  
00430     int hurry_up;     
00433     /* macroblock layer */
00434     int mb_x, mb_y;
00435     int mb_skip_run;
00436     int mb_intra;
00437     uint16_t *mb_type;           
00438 #define CANDIDATE_MB_TYPE_INTRA    0x01
00439 #define CANDIDATE_MB_TYPE_INTER    0x02
00440 #define CANDIDATE_MB_TYPE_INTER4V  0x04
00441 #define CANDIDATE_MB_TYPE_SKIPPED   0x08
00442 //#define MB_TYPE_GMC      0x10
00443 
00444 #define CANDIDATE_MB_TYPE_DIRECT   0x10
00445 #define CANDIDATE_MB_TYPE_FORWARD  0x20
00446 #define CANDIDATE_MB_TYPE_BACKWARD 0x40
00447 #define CANDIDATE_MB_TYPE_BIDIR    0x80
00448 
00449 #define CANDIDATE_MB_TYPE_INTER_I    0x100
00450 #define CANDIDATE_MB_TYPE_FORWARD_I  0x200
00451 #define CANDIDATE_MB_TYPE_BACKWARD_I 0x400
00452 #define CANDIDATE_MB_TYPE_BIDIR_I    0x800
00453 
00454     int block_index[6]; 
00455     int block_wrap[6];
00456     uint8_t *dest[3];
00457     
00458     int *mb_index2xy;        
00459 
00461     uint16_t intra_matrix[64];
00462     uint16_t chroma_intra_matrix[64];
00463     uint16_t inter_matrix[64];
00464     uint16_t chroma_inter_matrix[64];
00465 #define QUANT_BIAS_SHIFT 8
00466     int intra_quant_bias;    
00467     int inter_quant_bias;    
00468     int min_qcoeff;          
00469     int max_qcoeff;          
00470     int ac_esc_length;       
00471     uint8_t *intra_ac_vlc_length;
00472     uint8_t *intra_ac_vlc_last_length;
00473     uint8_t *inter_ac_vlc_length;
00474     uint8_t *inter_ac_vlc_last_length;
00475     uint8_t *luma_dc_vlc_length;
00476     uint8_t *chroma_dc_vlc_length;
00477 #define UNI_AC_ENC_INDEX(run,level) ((run)*128 + (level))
00478 
00479     int coded_score[6];
00480 
00482     int (*q_intra_matrix)[64];
00483     int (*q_inter_matrix)[64];
00485     uint16_t (*q_intra_matrix16)[2][64];
00486     uint16_t (*q_inter_matrix16)[2][64];
00487     int block_last_index[12];  
00488     /* scantables */
00489     ScanTable __align8 intra_scantable;
00490     ScanTable intra_h_scantable;
00491     ScanTable intra_v_scantable;
00492     ScanTable inter_scantable; 
00493     
00494     /* noise reduction */
00495     int (*dct_error_sum)[64];
00496     int dct_count[2];
00497     uint16_t (*dct_offset)[64];
00498 
00499     void *opaque;              
00500 
00501     /* bit rate control */
00502     int64_t wanted_bits;
00503     int64_t total_bits;
00504     int frame_bits;                
00505     RateControlContext rc_context; 
00506 
00507     /* statistics, used for 2-pass encoding */
00508     int mv_bits;
00509     int header_bits;
00510     int i_tex_bits;
00511     int p_tex_bits;
00512     int i_count;
00513     int f_count;
00514     int b_count;
00515     int skip_count;
00516     int misc_bits; 
00517     int last_bits; 
00518     
00519     /* error concealment / resync */
00520     int error_count;
00521     uint8_t *error_status_table;       
00522 #define VP_START            1          
00523 #define AC_ERROR            2
00524 #define DC_ERROR            4
00525 #define MV_ERROR            8
00526 #define AC_END              16
00527 #define DC_END              32
00528 #define MV_END              64
00529 //FIXME some prefix?
00530     
00531     int resync_mb_x;                 
00532     int resync_mb_y;                 
00533     GetBitContext last_resync_gb;    
00534     int mb_num_left;                 
00535     int next_p_frame_damaged;        
00536     int error_resilience;
00537     
00538     ParseContext parse_context;
00539 
00540     /* H.263 specific */
00541     int gob_index;
00542     int obmc;                       
00543         
00544     /* H.263+ specific */
00545     int umvplus;                    
00546     int h263_aic;                   
00547     int h263_aic_dir;               
00548     int h263_slice_structured;
00549     int alt_inter_vlc;              
00550     int modified_quant;
00551     int loop_filter;    
00552     int custom_pcf;
00553     
00554     /* mpeg4 specific */
00555     int time_increment_bits;        
00556     int last_time_base;
00557     int time_base;                  
00558     int64_t time;                   
00559     int64_t last_non_b_time;
00560     uint16_t pp_time;               
00561     uint16_t pb_time;               
00562     uint16_t pp_field_time;
00563     uint16_t pb_field_time;         
00564     int shape;
00565     int vol_sprite_usage;
00566     int sprite_width;
00567     int sprite_height;
00568     int sprite_left;
00569     int sprite_top;
00570     int sprite_brightness_change;
00571     int num_sprite_warping_points;
00572     int real_sprite_warping_points;
00573     int sprite_offset[2][2];         
00574     int sprite_delta[2][2];          
00575     int sprite_shift[2];             
00576     int mcsel;
00577     int quant_precision;
00578     int quarter_sample;              
00579     int scalability;
00580     int hierachy_type;
00581     int enhancement_type;
00582     int new_pred;
00583     int reduced_res_vop;
00584     int aspect_ratio_info; //FIXME remove
00585     int sprite_warping_accuracy;
00586     int low_latency_sprite;
00587     int data_partitioning;           
00588     int partitioned_frame;           
00589     int rvlc;                        
00590     int resync_marker;               
00591     int low_delay;                   
00592     int vo_type;
00593     int vol_control_parameters;      
00594     int intra_dc_threshold;          
00595     PutBitContext tex_pb;            
00596     PutBitContext pb2;               
00597     int mpeg_quant;
00598     int t_frame;                       
00599     int padding_bug_score;             
00600 
00601     /* divx specific, used to workaround (many) bugs in divx5 */
00602     int divx_version;
00603     int divx_build;
00604     int divx_packed;
00605     uint8_t *bitstream_buffer; //Divx 5.01 puts several frames in a single one, this is used to reorder them
00606     int bitstream_buffer_size;
00607     int allocated_bitstream_buffer_size;
00608     
00609     int xvid_build;
00610     
00611     /* lavc specific stuff, used to workaround bugs in libavcodec */
00612     int lavc_build;
00613     
00614     /* RV10 specific */
00615     int rv10_version; 
00616     int rv10_first_dc_coded[3];
00617     
00618     /* MJPEG specific */
00619     struct MJpegContext *mjpeg_ctx;
00620     int mjpeg_vsample[3];       
00621     int mjpeg_hsample[3];       
00622     int mjpeg_write_tables;     
00623     int mjpeg_data_only_frames; 
00624 
00625     /* MSMPEG4 specific */
00626     int mv_table_index;
00627     int rl_table_index;
00628     int rl_chroma_table_index;
00629     int dc_table_index;
00630     int use_skip_mb_code;
00631     int slice_height;      
00632     int first_slice_line;  
00633     int flipflop_rounding;
00634     int