]> git.sesse.net Git - ffmpeg/blob - libavcodec/mpegvideo.h
split intra / inter dequantization
[ffmpeg] / libavcodec / mpegvideo.h
1 /*
2  * Generic DCT based hybrid video encoder
3  * Copyright (c) 2000, 2001, 2002 Fabrice Bellard.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19
20 /**
21  * @file mpegvideo.h
22  * mpegvideo header.
23  */
24  
25 #ifndef AVCODEC_MPEGVIDEO_H
26 #define AVCODEC_MPEGVIDEO_H
27
28 #include "dsputil.h"
29
30 #define FRAME_SKIPED 100 ///< return value for header parsers if frame is not coded
31
32 enum OutputFormat {
33     FMT_MPEG1,
34     FMT_H263,
35     FMT_MJPEG, 
36     FMT_H264,
37 };
38
39 #define EDGE_WIDTH 16
40
41 #define MPEG_BUF_SIZE (16 * 1024)
42
43 #define QMAT_SHIFT_MMX 16
44 #define QMAT_SHIFT 22
45
46 #define MAX_FCODE 7
47 #define MAX_MV 2048
48
49 #define MAX_PICTURE_COUNT 15
50
51 #define ME_MAP_SIZE 64
52 #define ME_MAP_SHIFT 3
53 #define ME_MAP_MV_BITS 11
54
55 /* run length table */
56 #define MAX_RUN    64
57 #define MAX_LEVEL  64
58
59 #define I_TYPE FF_I_TYPE  ///< Intra
60 #define P_TYPE FF_P_TYPE  ///< Predicted
61 #define B_TYPE FF_B_TYPE  ///< Bi-dir predicted
62 #define S_TYPE FF_S_TYPE  ///< S(GMC)-VOP MPEG4
63 #define SI_TYPE FF_SI_TYPE  ///< Switching Intra
64 #define SP_TYPE FF_SP_TYPE  ///< Switching Predicted
65
66 typedef struct Predictor{
67     double coeff;
68     double count;
69     double decay;
70 } Predictor;
71
72 typedef struct RateControlEntry{
73     int pict_type;
74     float qscale;
75     int mv_bits;
76     int i_tex_bits;
77     int p_tex_bits;
78     int misc_bits;
79     uint64_t expected_bits;
80     int new_pict_type;
81     float new_qscale;
82     int mc_mb_var_sum;
83     int mb_var_sum;
84     int i_count;
85     int f_code;
86     int b_code;
87 }RateControlEntry;
88
89 /**
90  * rate control context.
91  */
92 typedef struct RateControlContext{
93     FILE *stats_file;
94     int num_entries;              ///< number of RateControlEntries 
95     RateControlEntry *entry;
96     double buffer_index;          ///< amount of bits in the video/audio buffer 
97     Predictor pred[5];
98     double short_term_qsum;       ///< sum of recent qscales 
99     double short_term_qcount;     ///< count of recent qscales 
100     double pass1_rc_eq_output_sum;///< sum of the output of the rc equation, this is used for normalization  
101     double pass1_wanted_bits;     ///< bits which should have been outputed by the pass1 code (including complexity init) 
102     double last_qscale;
103     double last_qscale_for[5];    ///< last qscale for a specific pict type, used for max_diff & ipb factor stuff 
104     int last_mc_mb_var_sum;
105     int last_mb_var_sum;
106     uint64_t i_cplx_sum[5];
107     uint64_t p_cplx_sum[5];
108     uint64_t mv_bits_sum[5];
109     uint64_t qscale_sum[5];
110     int frame_count[5];
111     int last_non_b_pict_type;
112 }RateControlContext;
113
114 /**
115  * Scantable.
116  */
117 typedef struct ScanTable{
118     const uint8_t *scantable;
119     uint8_t permutated[64];
120     uint8_t raster_end[64];
121 #ifdef ARCH_POWERPC
122                 /** Used by dct_quantise_alitvec to find last-non-zero */
123     uint8_t __align8 inverse[64];
124 #endif
125 } ScanTable;
126
127 /**
128  * Picture.
129  */
130 typedef struct Picture{
131     FF_COMMON_FRAME
132
133     /**
134      * halfpel luma planes.
135      */
136     uint8_t *interpolated[3];
137     int16_t (*motion_val_base[2])[2];
138     int8_t *ref_index[2];
139     uint32_t *mb_type_base;
140 #define IS_INTRA4x4(a)   ((a)&MB_TYPE_INTRA4x4)
141 #define IS_INTRA16x16(a) ((a)&MB_TYPE_INTRA16x16)
142 #define IS_PCM(a)        ((a)&MB_TYPE_INTRA_PCM)
143 #define IS_INTRA(a)      ((a)&7)
144 #define IS_INTER(a)      ((a)&(MB_TYPE_16x16|MB_TYPE_16x8|MB_TYPE_8x16|MB_TYPE_8x8))
145 #define IS_SKIP(a)       ((a)&MB_TYPE_SKIP)
146 #define IS_INTRA_PCM(a)  ((a)&MB_TYPE_INTRA_PCM)
147 #define IS_INTERLACED(a) ((a)&MB_TYPE_INTERLACED)
148 #define IS_DIRECT(a)     ((a)&MB_TYPE_DIRECT2)
149 #define IS_GMC(a)        ((a)&MB_TYPE_GMC)
150 #define IS_16X16(a)      ((a)&MB_TYPE_16x16)
151 #define IS_16X8(a)       ((a)&MB_TYPE_16x8)
152 #define IS_8X16(a)       ((a)&MB_TYPE_8x16)
153 #define IS_8X8(a)        ((a)&MB_TYPE_8x8)
154 #define IS_SUB_8X8(a)    ((a)&MB_TYPE_16x16) //note reused
155 #define IS_SUB_8X4(a)    ((a)&MB_TYPE_16x8)  //note reused
156 #define IS_SUB_4X8(a)    ((a)&MB_TYPE_8x16)  //note reused
157 #define IS_SUB_4X4(a)    ((a)&MB_TYPE_8x8)   //note reused
158 #define IS_ACPRED(a)     ((a)&MB_TYPE_ACPRED)
159 #define IS_QUANT(a)      ((a)&MB_TYPE_QUANT)
160 #define IS_DIR(a, part, list) ((a) & (MB_TYPE_P0L0<<((part)+2*(list))))
161 #define USES_LIST(a, list) ((a) & ((MB_TYPE_P0L0|MB_TYPE_P1L0)<<(2*(list)))) ///< does this mb use listX, note doesnt work if subMBs
162 #define HAS_CBP(a)        ((a)&MB_TYPE_CBP)
163
164     int field_poc[2];           ///< h264 top/bottom POC
165     int poc;                    ///< h264 frame POC
166     int frame_num;              ///< h264 frame_num
167     int pic_id;                 ///< h264 pic_num or long_term_pic_idx
168     int long_ref;               ///< 1->long term reference 0->short term reference
169
170     int mb_var_sum;             ///< sum of MB variance for current frame 
171     int mc_mb_var_sum;          ///< motion compensated MB variance for current frame 
172     uint16_t *mb_var;           ///< Table for MB variances 
173     uint16_t *mc_mb_var;        ///< Table for motion compensated MB variances 
174     uint8_t *mb_mean;           ///< Table for MB luminance 
175     int32_t *mb_cmp_score;      ///< Table for MB cmp scores, for mb decission FIXME remove
176     int b_frame_score;          /* */
177 } Picture;
178
179 typedef struct ParseContext{
180     uint8_t *buffer;
181     int index;
182     int last_index;
183     int buffer_size;
184     uint32_t state;             ///< contains the last few bytes in MSB order
185     int frame_start_found;
186     int overread;               ///< the number of bytes which where irreversibly read from the next frame
187     int overread_index;         ///< the index into ParseContext.buffer of the overreaded bytes
188 } ParseContext;
189
190 struct MpegEncContext;
191
192 /**
193  * Motion estimation context.
194  */
195 typedef struct MotionEstContext{
196     int skip;                          ///< set if ME is skiped for the current MB 
197     int co_located_mv[4][2];           ///< mv from last p frame for direct mode ME 
198     int direct_basis_mv[4][2];
199     uint8_t *scratchpad;               ///< data area for the me algo, so that the ME doesnt need to malloc/free 
200     uint32_t *map;                     ///< map to avoid duplicate evaluations 
201     uint32_t *score_map;               ///< map to store the scores 
202     int map_generation;  
203     int pre_penalty_factor;
204     int penalty_factor;
205     int sub_penalty_factor;
206     int mb_penalty_factor;
207     int pre_pass;                      ///< = 1 for the pre pass 
208     int dia_size;
209     uint8_t (*mv_penalty)[MAX_MV*2+1];  ///< amount of bits needed to encode a MV 
210     int (*sub_motion_search)(struct MpegEncContext * s,
211                                   int *mx_ptr, int *my_ptr, int dmin,
212                                   int xmin, int ymin, int xmax, int ymax,
213                                   int pred_x, int pred_y, Picture *ref_picture, 
214                                   int n, int size, uint8_t * const mv_penalty);
215     int (*motion_search[7])(struct MpegEncContext * s, int block,
216                              int *mx_ptr, int *my_ptr,
217                              int P[10][2], int pred_x, int pred_y,
218                              int xmin, int ymin, int xmax, int ymax, Picture *ref_picture, int16_t (*last_mv)[2], 
219                              int ref_mv_scale, uint8_t * const mv_penalty);
220     int (*pre_motion_search)(struct MpegEncContext * s, int block,
221                              int *mx_ptr, int *my_ptr,
222                              int P[10][2], int pred_x, int pred_y,
223                              int xmin, int ymin, int xmax, int ymax, Picture *ref_picture, int16_t (*last_mv)[2], 
224                              int ref_mv_scale, uint8_t * const mv_penalty);
225     int (*get_mb_score)(struct MpegEncContext * s, int mx, int my, int pred_x, int pred_y, Picture *ref_picture, 
226                                   uint8_t * const mv_penalty);
227 }MotionEstContext;
228
229 /**
230  * MpegEncContext.
231  */
232 typedef struct MpegEncContext {
233     struct AVCodecContext *avctx;
234     /* the following parameters must be initialized before encoding */
235     int width, height;///< picture size. must be a multiple of 16 
236     int gop_size;
237     int intra_only;   ///< if true, only intra pictures are generated 
238     int bit_rate;     ///< wanted bit rate 
239     enum OutputFormat out_format; ///< output format 
240     int h263_pred;    ///< use mpeg4/h263 ac/dc predictions 
241
242 /* the following codec id fields are deprecated in favor of codec_id */
243     int h263_plus;    ///< h263 plus headers 
244     int h263_msmpeg4; ///< generate MSMPEG4 compatible stream (deprecated, use msmpeg4_version instead)
245     int h263_flv;     ///< use flv h263 header 
246     
247     int codec_id;     /* see CODEC_ID_xxx */
248     int fixed_qscale; ///< fixed qscale if non zero 
249     int encoding;     ///< true if we are encoding (vs decoding) 
250     int flags;        ///< AVCodecContext.flags (HQ, MV4, ...) 
251     int max_b_frames; ///< max number of b-frames for encoding 
252     int luma_elim_threshold;
253     int chroma_elim_threshold;
254     int strict_std_compliance; ///< strictly follow the std (MPEG4, ...) 
255     int workaround_bugs;       ///< workaround bugs in encoders which cannot be detected automatically 
256     /* the following fields are managed internally by the encoder */
257
258     /** bit output */
259     PutBitContext pb;
260
261     /* sequence parameters */
262     int context_initialized;
263     int input_picture_number;
264     int picture_number;
265     int picture_in_gop_number; ///< 0-> first pic in gop, ... 
266     int b_frames_since_non_b;  ///< used for encoding, relative to not yet reordered input 
267     int mb_width, mb_height;   ///< number of MBs horizontally & vertically 
268     int mb_stride;             ///< mb_width+1 used for some arrays to allow simple addressng of left & top MBs withoutt sig11
269     int b8_stride;             ///< 2*mb_width+1 used for some 8x8 block arrays to allow simple addressng
270     int b4_stride;             ///< 4*mb_width+1 used for some 4x4 block arrays to allow simple addressng
271     int h_edge_pos, v_edge_pos;///< horizontal / vertical position of the right/bottom edge (pixel replicateion)
272     int mb_num;                ///< number of MBs of a picture 
273     int linesize;              ///< line size, in bytes, may be different from width 
274     int uvlinesize;            ///< line size, for chroma in bytes, may be different from width 
275     Picture *picture;          ///< main picture buffer 
276     Picture **input_picture;   ///< next pictures on display order for encoding
277     Picture **reordered_input_picture; ///< pointer to the next pictures in codedorder for encoding
278     
279     /** 
280      * copy of the previous picture structure.
281      * note, linesize & data, might not match the previous picture (for field pictures)
282      */
283     Picture last_picture;       
284     
285     /** 
286      * copy of the next picture structure.
287      * note, linesize & data, might not match the next picture (for field pictures)
288      */
289     Picture next_picture;
290     
291     /** 
292      * copy of the source picture structure for encoding.
293      * note, linesize & data, might not match the source picture (for field pictures)
294      */
295     Picture new_picture;
296     
297     /** 
298      * copy of the current picture structure.
299      * note, linesize & data, might not match the current picture (for field pictures)
300      */
301     Picture current_picture;    ///< buffer to store the decompressed current picture 
302     
303     Picture *last_picture_ptr;     ///< pointer to the previous picture.
304     Picture *next_picture_ptr;     ///< pointer to the next picture (for bidir pred) 
305     Picture *current_picture_ptr;  ///< pointer to the current picture
306     int last_dc[3];                ///< last DC values for MPEG1 
307     int16_t *dc_val[3];            ///< used for mpeg4 DC prediction, all 3 arrays must be continuous 
308     int16_t dc_cache[4*5];
309     int y_dc_scale, c_dc_scale;
310     uint8_t *y_dc_scale_table;     ///< qscale -> y_dc_scale table 
311     uint8_t *c_dc_scale_table;     ///< qscale -> c_dc_scale table 
312     const uint8_t *chroma_qscale_table;  ///< qscale -> chroma_qscale (h263)
313     uint8_t *coded_block;          ///< used for coded block pattern prediction (msmpeg4v3, wmv1)
314     int16_t (*ac_val[3])[16];      ///< used for for mpeg4 AC prediction, all 3 arrays must be continuous 
315     int ac_pred;
316     uint8_t *prev_pict_types;     ///< previous picture types in bitstream order, used for mb skip 
317 #define PREV_PICT_TYPES_BUFFER_SIZE 256
318     int mb_skiped;                ///< MUST BE SET only during DECODING 
319     uint8_t *mbskip_table;        /**< used to avoid copy if macroblock skipped (for black regions for example) 
320                                    and used for b-frame encoding & decoding (contains skip table of next P Frame) */
321     uint8_t *mbintra_table;       ///< used to avoid setting {ac, dc, cbp}-pred stuff to zero on inter MB decoding 
322     uint8_t *cbp_table;           ///< used to store cbp, ac_pred for partitioned decoding 
323     uint8_t *pred_dir_table;      ///< used to store pred_dir for partitioned decoding 
324     uint8_t *allocated_edge_emu_buffer;
325     uint8_t *edge_emu_buffer;     ///< points into the middle of allocated_edge_emu_buffer  
326
327     int qscale;                 ///< QP 
328     int chroma_qscale;          ///< chroma QP 
329     int lambda;                 ///< lagrange multipler used in rate distortion
330     int lambda2;                ///< (lambda*lambda) >> FF_LAMBDA_SHIFT 
331     int *lambda_table;
332     int adaptive_quant;         ///< use adaptive quantization 
333     int dquant;                 ///< qscale difference to prev qscale  
334     int pict_type;              ///< I_TYPE, P_TYPE, B_TYPE, ... 
335     int last_pict_type;
336     int last_non_b_pict_type;   ///< used for mpeg4 gmc b-frames & ratecontrol 
337     int frame_rate_index;
338     /* motion compensation */
339     int unrestricted_mv;        ///< mv can point outside of the coded picture 
340     int h263_long_vectors;      ///< use horrible h263v1 long vector mode 
341     int decode;                 ///< if 0 then decoding will be skiped (for encoding b frames for example)
342
343     DSPContext dsp;             ///< pointers for accelerated dsp fucntions 
344     int f_code;                 ///< forward MV resolution 
345     int b_code;                 ///< backward MV resolution for B Frames (mpeg4) 
346     int16_t (*p_mv_table_base)[2];
347     int16_t (*b_forw_mv_table_base)[2];
348     int16_t (*b_back_mv_table_base)[2];
349     int16_t (*b_bidir_forw_mv_table_base)[2]; 
350     int16_t (*b_bidir_back_mv_table_base)[2]; 
351     int16_t (*b_direct_mv_table_base)[2];
352     int16_t (*p_mv_table)[2];            ///< MV table (1MV per MB) p-frame encoding 
353     int16_t (*b_forw_mv_table)[2];       ///< MV table (1MV per MB) forward mode b-frame encoding 
354     int16_t (*b_back_mv_table)[2];       ///< MV table (1MV per MB) backward mode b-frame encoding 
355     int16_t (*b_bidir_forw_mv_table)[2]; ///< MV table (1MV per MB) bidir mode b-frame encoding 
356     int16_t (*b_bidir_back_mv_table)[2]; ///< MV table (1MV per MB) bidir mode b-frame encoding 
357     int16_t (*b_direct_mv_table)[2];     ///< MV table (1MV per MB) direct mode b-frame encoding 
358     int me_method;                       ///< ME algorithm 
359     int scene_change_score;
360     int mv_dir;
361 #define MV_DIR_BACKWARD  1
362 #define MV_DIR_FORWARD   2
363 #define MV_DIRECT        4 ///< bidirectional mode where the difference equals the MV of the last P/S/I-Frame (mpeg4)
364     int mv_type;
365 #define MV_TYPE_16X16       0   ///< 1 vector for the whole mb 
366 #define MV_TYPE_8X8         1   ///< 4 vectors (h263, mpeg4 4MV) 
367 #define MV_TYPE_16X8        2   ///< 2 vectors, one per 16x8 block  
368 #define MV_TYPE_FIELD       3   ///< 2 vectors, one per field  
369 #define MV_TYPE_DMV         4   ///< 2 vectors, special mpeg2 Dual Prime Vectors 
370     /**motion vectors for a macroblock 
371        first coordinate : 0 = forward 1 = backward
372        second "         : depend on type
373        third  "         : 0 = x, 1 = y
374     */
375     int mv[2][4][2];
376     int field_select[2][2];
377     int last_mv[2][2][2];             ///< last MV, used for MV prediction in MPEG1 & B-frame MPEG4 
378     uint8_t *fcode_tab;               ///< smallest fcode needed for each MV 
379     
380     MotionEstContext me;
381
382     int no_rounding;  /**< apply no rounding to motion compensation (MPEG4, msmpeg4, ...) 
383                         for b-frames rounding mode is allways 0 */
384
385     int hurry_up;     /**< when set to 1 during decoding, b frames will be skiped
386                          when set to 2 idct/dequant will be skipped too */
387                         
388     /* macroblock layer */
389     int mb_x, mb_y;
390     int mb_skip_run;
391     int mb_intra;
392     uint8_t *mb_type;       ///< Table for MB type FIXME remove and use picture->mb_type
393 #define MB_TYPE_INTRA    0x01
394 #define MB_TYPE_INTER    0x02
395 #define MB_TYPE_INTER4V  0x04
396 #define MB_TYPE_SKIPED   0x08
397 //#define MB_TYPE_GMC      0x10
398
399 #define MB_TYPE_DIRECT   0x10
400 #define MB_TYPE_FORWARD  0x20
401 #define MB_TYPE_BACKWARD 0x40
402 #define MB_TYPE_BIDIR    0x80
403
404     int block_index[6]; ///< index to current MB in block based arrays with edges
405     int block_wrap[6];
406     uint8_t *dest[3];
407     
408     int *mb_index2xy;        ///< mb_index -> mb_x + mb_y*mb_stride
409
410     /** matrix transmitted in the bitstream */
411     uint16_t intra_matrix[64];
412     uint16_t chroma_intra_matrix[64];
413     uint16_t inter_matrix[64];
414     uint16_t chroma_inter_matrix[64];
415 #define QUANT_BIAS_SHIFT 8
416     int intra_quant_bias;    ///< bias for the quantizer 
417     int inter_quant_bias;    ///< bias for the quantizer 
418     int min_qcoeff;          ///< minimum encodable coefficient 
419     int max_qcoeff;          ///< maximum encodable coefficient 
420     int ac_esc_length;       ///< num of bits needed to encode the longest esc 
421     uint8_t *intra_ac_vlc_length;
422     uint8_t *intra_ac_vlc_last_length;
423     uint8_t *inter_ac_vlc_length;
424     uint8_t *inter_ac_vlc_last_length;
425     uint8_t *luma_dc_vlc_length;
426     uint8_t *chroma_dc_vlc_length;
427 #define UNI_AC_ENC_INDEX(run,level) ((run)*128 + (level))
428
429     int coded_score[6];
430
431     /** precomputed matrix (combine qscale and DCT renorm) */
432     int (*q_intra_matrix)[64];
433     int (*q_inter_matrix)[64];
434     /** identical to the above but for MMX & these are not permutated, second 64 entries are bias*/
435     uint16_t (*q_intra_matrix16)[2][64];
436     uint16_t (*q_inter_matrix16)[2][64];
437     int block_last_index[6];  ///< last non zero coefficient in block
438     /* scantables */
439     ScanTable __align8 intra_scantable;
440     ScanTable intra_h_scantable;
441     ScanTable intra_v_scantable;
442     ScanTable inter_scantable; ///< if inter == intra then intra should be used to reduce tha cache usage
443     
444     /* noise reduction */
445     int (*dct_error_sum)[64];
446     int dct_count[2];
447     uint16_t (*dct_offset)[64];
448
449     void *opaque;              ///< private data for the user
450
451     /* bit rate control */
452     int I_frame_bits; //FIXME used in mpeg12 ...
453     int64_t wanted_bits;
454     int64_t total_bits;
455     int frame_bits;                ///< bits used for the current frame 
456     RateControlContext rc_context; ///< contains stuff only accessed in ratecontrol.c
457
458     /* statistics, used for 2-pass encoding */
459     int mv_bits;
460     int header_bits;
461     int i_tex_bits;
462     int p_tex_bits;
463     int i_count;
464     int f_count;
465     int b_count;
466     int skip_count;
467     int misc_bits; ///< cbp, mb_type
468     int last_bits; ///< temp var used for calculating the above vars
469     
470     /* error concealment / resync */
471     int error_count;
472     uint8_t *error_status_table;       ///< table of the error status of each MB  
473 #define VP_START            1          ///< current MB is the first after a resync marker 
474 #define AC_ERROR            2
475 #define DC_ERROR            4
476 #define MV_ERROR            8
477 #define AC_END              16
478 #define DC_END              32
479 #define MV_END              64
480 //FIXME some prefix?
481     
482     int resync_mb_x;                 ///< x position of last resync marker 
483     int resync_mb_y;                 ///< y position of last resync marker 
484     GetBitContext last_resync_gb;    ///< used to search for the next resync marker 
485     int mb_num_left;                 ///< number of MBs left in this video packet (for partitioned Slices only)
486     int next_p_frame_damaged;        ///< set if the next p frame is damaged, to avoid showing trashed b frames 
487     int error_resilience;
488     
489     ParseContext parse_context;
490
491     /* H.263 specific */
492     int gob_index;
493     int obmc;                       ///< overlapped block motion compensation
494         
495     /* H.263+ specific */
496     int umvplus;                    ///< == H263+ && unrestricted_mv 
497     int h263_aic;                   ///< Advanded INTRA Coding (AIC) 
498     int h263_aic_dir;               ///< AIC direction: 0 = left, 1 = top
499     int h263_slice_structured;
500     int alt_inter_vlc;              ///< alternative inter vlc
501     int modified_quant;
502     int loop_filter;    
503     
504     /* mpeg4 specific */
505     int time_increment_resolution;
506     int time_increment_bits;        ///< number of bits to represent the fractional part of time 
507     int last_time_base;
508     int time_base;                  ///< time in seconds of last I,P,S Frame 
509     int64_t time;                   ///< time of current frame  
510     int64_t last_non_b_time;
511     uint16_t pp_time;               ///< time distance between the last 2 p,s,i frames 
512     uint16_t pb_time;               ///< time distance between the last b and p,s,i frame 
513     uint16_t pp_field_time;
514     uint16_t pb_field_time;         ///< like above, just for interlaced 
515     int shape;
516     int vol_sprite_usage;
517     int sprite_width;
518     int sprite_height;
519     int sprite_left;
520     int sprite_top;
521     int sprite_brightness_change;
522     int num_sprite_warping_points;
523     int real_sprite_warping_points;
524     int sprite_offset[2][2];         ///< sprite offset[isChroma][isMVY] 
525     int sprite_delta[2][2];          ///< sprite_delta [isY][isMVY]  
526     int sprite_shift[2];             ///< sprite shift [isChroma] 
527     int mcsel;
528     int quant_precision;
529     int quarter_sample;              ///< 1->qpel, 0->half pel ME/MC  
530     int scalability;
531     int hierachy_type;
532     int enhancement_type;
533     int new_pred;
534     int reduced_res_vop;
535     int aspect_ratio_info; //FIXME remove
536     int sprite_warping_accuracy;
537     int low_latency_sprite;
538     int data_partitioning;           ///< data partitioning flag from header 
539     int partitioned_frame;           ///< is current frame partitioned 
540     int rvlc;                        ///< reversible vlc 
541     int resync_marker;               ///< could this stream contain resync markers
542     int low_delay;                   ///< no reordering needed / has no b-frames 
543     int vo_type;
544     int vol_control_parameters;      ///< does the stream contain the low_delay flag, used to workaround buggy encoders 
545     int intra_dc_threshold;          ///< QP above whch the ac VLC should be used for intra dc 
546     PutBitContext tex_pb;            ///< used for data partitioned VOPs 
547     PutBitContext pb2;               ///< used for data partitioned VOPs 
548 #define PB_BUFFER_SIZE 1024*256
549     uint8_t *tex_pb_buffer;          
550     uint8_t *pb2_buffer;
551     int mpeg_quant;
552     int16_t (*field_mv_table)[2][2];   ///< used for interlaced b frame decoding 
553     int8_t (*field_select_table)[2];   ///< wtf, no really another table for interlaced b frames 
554     int t_frame;                       ///< time distance of first I -> B, used for interlaced b frames 
555     int padding_bug_score;             ///< used to detect the VERY common padding bug in MPEG4 
556
557     /* divx specific, used to workaround (many) bugs in divx5 */
558     int divx_version;
559     int divx_build;
560     int divx_packed;
561 #define BITSTREAM_BUFFER_SIZE 1024*256
562     uint8_t *bitstream_buffer; //Divx 5.01 puts several frames in a single one, this is used to reorder them
563     int bitstream_buffer_size;
564     
565     int xvid_build;
566     
567     /* lavc specific stuff, used to workaround bugs in libavcodec */
568     int ffmpeg_version;
569     int lavc_build;
570     
571     /* RV10 specific */
572     int rv10_version; ///< RV10 version: 0 or 3 
573     int rv10_first_dc_coded[3];
574     
575     /* MJPEG specific */
576     struct MJpegContext *mjpeg_ctx;
577     int mjpeg_vsample[3];       ///< vertical sampling factors, default = {2, 1, 1} 
578     int mjpeg_hsample[3];       ///< horizontal sampling factors, default = {2, 1, 1} 
579     int mjpeg_write_tables;     ///< do we want to have quantisation- and huffmantables in the jpeg file ? 
580     int mjpeg_data_only_frames; ///< frames only with SOI, SOS and EOI markers 
581
582     /* MSMPEG4 specific */
583     int mv_table_index;
584     int rl_table_index;
585     int rl_chroma_table_index;
586     int dc_table_index;
587     int use_skip_mb_code;
588     int slice_height;      ///< in macroblocks 
589     int first_slice_line;  ///< used in mpeg4 too to handle resync markers 
590     int flipflop_rounding;
591     int msmpeg4_version;   ///< 0=not msmpeg4, 1=mp41, 2=mp42, 3=mp43/divx3 4=wmv1/7 5=wmv2/8
592     int per_mb_rl_table;
593     int esc3_level_length;
594     int esc3_run_length;
595     /** [mb_intra][isChroma][level][run][last] */
596     int (*ac_stats)[2][MAX_LEVEL+1][MAX_RUN+1][2];
597     int inter_intra_pred;
598     int mspel;
599
600     /* decompression specific */
601     GetBitContext gb;
602
603     /* Mpeg1 specific */
604     int fake_picture_number; ///< picture number at the bitstream frame rate 
605     int gop_picture_number;  ///< index of the first picture of a GOP based on fake_pic_num & mpeg1 specific 
606     int last_mv_dir;         ///< last mv_dir, used for b frame encoding 
607     int broken_link;         ///< no_output_of_prior_pics_flag
608     
609     /* MPEG2 specific - I wish I had not to support this mess. */
610     int progressive_sequence;
611     int mpeg_f_code[2][2];
612     int picture_structure;
613 /* picture type */
614 #define PICT_TOP_FIELD     1
615 #define PICT_BOTTOM_FIELD  2
616 #define PICT_FRAME         3
617
618     int intra_dc_precision;
619     int frame_pred_frame_dct;
620     int top_field_first;
621     int concealment_motion_vectors;
622     int q_scale_type;
623     int intra_vlc_format;
624     int alternate_scan;
625     int repeat_first_field;
626     int chroma_420_type;
627     int progressive_frame;
628     int full_pel[2];
629     int interlaced_dct;
630     int first_slice;
631     int first_field;         ///< is 1 for the first field of a field picture 0 otherwise
632
633     /* RTP specific */
634     int rtp_mode;
635     
636     uint8_t *ptr_lastgob;
637     int swap_uv;//vcr2 codec is mpeg2 varint with UV swaped
638     short * pblocks[12];
639     
640     DCTELEM (*block)[64]; ///< points to one of the following blocks 
641     DCTELEM (*blocks)[6][64]; // for HQ mode we need to keep the best block
642     int (*decode_mb)(struct MpegEncContext *s, DCTELEM block[6][64]); // used by some codecs to avoid a switch()
643 #define SLICE_OK         0
644 #define SLICE_ERROR     -1
645 #define SLICE_END       -2 ///<end marker found
646 #define SLICE_NOEND     -3 ///<no end marker or error found but mb count exceeded
647     
648     void (*dct_unquantize_mpeg1_intra)(struct MpegEncContext *s, 
649                            DCTELEM *block/*align 16*/, int n, int qscale);
650     void (*dct_unquantize_mpeg1_inter)(struct MpegEncContext *s, 
651                            DCTELEM *block/*align 16*/, int n, int qscale);
652     void (*dct_unquantize_mpeg2_intra)(struct MpegEncContext *s, 
653                            DCTELEM *block/*align 16*/, int n, int qscale);
654     void (*dct_unquantize_mpeg2_inter)(struct MpegEncContext *s, 
655                            DCTELEM *block/*align 16*/, int n, int qscale);
656     void (*dct_unquantize_h263_intra)(struct MpegEncContext *s, 
657                            DCTELEM *block/*align 16*/, int n, int qscale);
658     void (*dct_unquantize_h263_inter)(struct MpegEncContext *s, 
659                            DCTELEM *block/*align 16*/, int n, int qscale);
660     void (*dct_unquantize_intra)(struct MpegEncContext *s, // unquantizer to use (mpeg4 can use both)
661                            DCTELEM *block/*align 16*/, int n, int qscale);
662     void (*dct_unquantize_inter)(struct MpegEncContext *s, // unquantizer to use (mpeg4 can use both)
663                            DCTELEM *block/*align 16*/, int n, int qscale);
664     int (*dct_quantize)(struct MpegEncContext *s, DCTELEM *block/*align 16*/, int n, int qscale, int *overflow);
665     int (*fast_dct_quantize)(struct MpegEncContext *s, DCTELEM *block/*align 16*/, int n, int qscale, int *overflow);
666 } MpegEncContext;
667
668
669 int DCT_common_init(MpegEncContext *s);
670 int MPV_common_init(MpegEncContext *s);
671 void MPV_common_end(MpegEncContext *s);
672 void MPV_decode_mb(MpegEncContext *s, DCTELEM block[6][64]);
673 int MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx);
674 void MPV_frame_end(MpegEncContext *s);
675 int MPV_encode_init(AVCodecContext *avctx);
676 int MPV_encode_end(AVCodecContext *avctx);
677 int MPV_encode_picture(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data);
678 #ifdef HAVE_MMX
679 void MPV_common_init_mmx(MpegEncContext *s);
680 #endif
681 #ifdef ARCH_ALPHA
682 void MPV_common_init_axp(MpegEncContext *s);
683 #endif
684 #ifdef HAVE_MLIB
685 void MPV_common_init_mlib(MpegEncContext *s);
686 #endif
687 #ifdef HAVE_MMI
688 void MPV_common_init_mmi(MpegEncContext *s);
689 #endif
690 #ifdef ARCH_ARMV4L
691 void MPV_common_init_armv4l(MpegEncContext *s);
692 #endif
693 #ifdef ARCH_POWERPC
694 void MPV_common_init_ppc(MpegEncContext *s);
695 #endif
696 extern void (*draw_edges)(uint8_t *buf, int wrap, int width, int height, int w);
697 void ff_copy_bits(PutBitContext *pb, uint8_t *src, int length);
698 void ff_clean_intra_table_entries(MpegEncContext *s);
699 void ff_init_scantable(uint8_t *, ScanTable *st, const uint8_t *src_scantable);
700 void ff_draw_horiz_band(MpegEncContext *s, int y, int h);
701 void ff_emulated_edge_mc(uint8_t *buf, uint8_t *src, int linesize, int block_w, int block_h, 
702                                     int src_x, int src_y, int w, int h);
703 #define END_NOT_FOUND -100
704 int ff_combine_frame( MpegEncContext *s, int next, uint8_t **buf, int *buf_size);
705 void ff_mpeg_flush(AVCodecContext *avctx);
706 void ff_print_debug_info(MpegEncContext *s, Picture *pict);
707 void ff_write_quant_matrix(PutBitContext *pb, int16_t *matrix);
708 int ff_find_unused_picture(MpegEncContext *s, int shared);
709 void ff_denoise_dct(MpegEncContext *s, DCTELEM *block);
710
711 void ff_er_frame_start(MpegEncContext *s);
712 void ff_er_frame_end(MpegEncContext *s);
713 void ff_er_add_slice(MpegEncContext *s, int startx, int starty, int endx, int endy, int status);
714
715
716 extern enum PixelFormat ff_yuv420p_list[2];
717
718 void ff_init_block_index(MpegEncContext *s);
719
720 static inline void ff_update_block_index(MpegEncContext *s){
721     s->block_index[0]+=2;
722     s->block_index[1]+=2;
723     s->block_index[2]+=2;
724     s->block_index[3]+=2;
725     s->block_index[4]++;
726     s->block_index[5]++;
727     s->dest[0]+= 16;
728     s->dest[1]+= 8;
729     s->dest[2]+= 8;
730 }
731
732 static inline int get_bits_diff(MpegEncContext *s){
733     const int bits= get_bit_count(&s->pb);
734     const int last= s->last_bits;
735
736     s->last_bits = bits;
737
738     return bits - last;
739 }
740
741 /* motion_est.c */
742 void ff_estimate_p_frame_motion(MpegEncContext * s,
743                              int mb_x, int mb_y);
744 void ff_estimate_b_frame_motion(MpegEncContext * s,
745                              int mb_x, int mb_y);
746 int ff_get_best_fcode(MpegEncContext * s, int16_t (*mv_table)[2], int type);
747 void ff_fix_long_p_mvs(MpegEncContext * s);
748 void ff_fix_long_b_mvs(MpegEncContext * s, int16_t (*mv_table)[2], int f_code, int type);
749 void ff_init_me(MpegEncContext *s);
750 int ff_pre_estimate_p_frame_motion(MpegEncContext * s, int mb_x, int mb_y);
751
752
753 /* mpeg12.c */
754 extern const int16_t ff_mpeg1_default_intra_matrix[64];
755 extern const int16_t ff_mpeg1_default_non_intra_matrix[64];
756 extern uint8_t ff_mpeg1_dc_scale_table[128];
757
758 void mpeg1_encode_picture_header(MpegEncContext *s, int picture_number);
759 void mpeg1_encode_mb(MpegEncContext *s,
760                      DCTELEM block[6][64],
761                      int motion_x, int motion_y);
762 void ff_mpeg1_encode_init(MpegEncContext *s);
763 void ff_mpeg1_encode_slice_header(MpegEncContext *s);
764 void ff_mpeg1_clean_buffers(MpegEncContext *s);
765
766
767 /** RLTable. */
768 typedef struct RLTable {
769     int n;                         ///< number of entries of table_vlc minus 1 
770     int last;                      ///< number of values for last = 0 
771     const uint16_t (*table_vlc)[2];
772     const int8_t *table_run;
773     const int8_t *table_level;
774     uint8_t *index_run[2];         ///< encoding only 
775     int8_t *max_level[2];          ///< encoding & decoding 
776     int8_t *max_run[2];            ///< encoding & decoding 
777     VLC vlc;                       ///< decoding only deprected FIXME remove
778     RL_VLC_ELEM *rl_vlc[32];       ///< decoding only 
779 } RLTable;
780
781 void init_rl(RLTable *rl);
782 void init_vlc_rl(RLTable *rl);
783
784 static inline int get_rl_index(const RLTable *rl, int last, int run, int level)
785 {
786     int index;
787     index = rl->index_run[last][run];
788     if (index >= rl->n)
789         return rl->n;
790     if (level > rl->max_level[last][run])
791         return rl->n;
792     return index + level - 1;
793 }
794
795 extern uint8_t ff_mpeg4_y_dc_scale_table[32];
796 extern uint8_t ff_mpeg4_c_dc_scale_table[32];
797 extern uint8_t ff_aic_dc_scale_table[32];
798 extern const int16_t ff_mpeg4_default_intra_matrix[64];
799 extern const int16_t ff_mpeg4_default_non_intra_matrix[64];
800 extern const uint8_t ff_h263_chroma_qscale_table[32];
801 extern const uint8_t ff_h263_loop_filter_strength[32];
802
803
804 int ff_h263_decode_init(AVCodecContext *avctx);
805 int ff_h263_decode_frame(AVCodecContext *avctx, 
806                              void *data, int *data_size,
807                              uint8_t *buf, int buf_size);
808 int ff_h263_decode_end(AVCodecContext *avctx);
809 void h263_encode_mb(MpegEncContext *s, 
810                     DCTELEM block[6][64],
811                     int motion_x, int motion_y);
812 void mpeg4_encode_mb(MpegEncContext *s, 
813                     DCTELEM block[6][64],
814                     int motion_x, int motion_y);
815 void h263_encode_picture_header(MpegEncContext *s, int picture_number);
816 void ff_flv_encode_picture_header(MpegEncContext *s, int picture_number);
817 void h263_encode_gob_header(MpegEncContext * s, int mb_line);
818 int16_t *h263_pred_motion(MpegEncContext * s, int block, 
819                         int *px, int *py);
820 void mpeg4_pred_ac(MpegEncContext * s, DCTELEM *block, int n, 
821                    int dir);
822 void ff_set_mpeg4_time(MpegEncContext * s, int picture_number);
823 void mpeg4_encode_picture_header(MpegEncContext *s, int picture_number);
824 void h263_encode_init(MpegEncContext *s);
825 void h263_decode_init_vlc(MpegEncContext *s);
826 int h263_decode_picture_header(MpegEncContext *s);
827 int ff_h263_decode_gob_header(MpegEncContext *s);
828 int ff_mpeg4_decode_picture_header(MpegEncContext * s, GetBitContext *gb);
829 void ff_h263_update_motion_val(MpegEncContext * s);
830 void ff_h263_loop_filter(MpegEncContext * s);
831 void ff_set_qscale(MpegEncContext * s, int qscale);
832 int ff_h263_decode_mba(MpegEncContext *s);
833 void ff_h263_encode_mba(MpegEncContext *s);
834
835 int intel_h263_decode_picture_header(MpegEncContext *s);
836 int flv_h263_decode_picture_header(MpegEncContext *s);
837 int ff_h263_decode_mb(MpegEncContext *s,
838                       DCTELEM block[6][64]);
839 int ff_mpeg4_decode_mb(MpegEncContext *s,
840                       DCTELEM block[6][64]);
841 int h263_get_picture_format(int width, int height);
842 void ff_mpeg4_encode_video_packet_header(MpegEncContext *s);
843 void ff_mpeg4_clean_buffers(MpegEncContext *s);
844 void ff_mpeg4_stuffing(PutBitContext * pbc);
845 void ff_mpeg4_init_partitions(MpegEncContext *s);
846 void ff_mpeg4_merge_partitions(MpegEncContext *s);
847 void ff_clean_mpeg4_qscales(MpegEncContext *s);
848 void ff_clean_h263_qscales(MpegEncContext *s);
849 int ff_mpeg4_decode_partitions(MpegEncContext *s);
850 int ff_mpeg4_get_video_packet_prefix_length(MpegEncContext *s);
851 int ff_h263_resync(MpegEncContext *s);
852 int ff_h263_get_gob_height(MpegEncContext *s);
853 int ff_mpeg4_set_direct_mv(MpegEncContext *s, int mx, int my);
854 inline int ff_h263_round_chroma(int x);
855
856
857 /* rv10.c */
858 void rv10_encode_picture_header(MpegEncContext *s, int picture_number);
859 int rv_decode_dc(MpegEncContext *s, int n);
860
861
862 /* msmpeg4.c */
863 void msmpeg4_encode_picture_header(MpegEncContext * s, int picture_number);
864 void msmpeg4_encode_ext_header(MpegEncContext * s);
865 void msmpeg4_encode_mb(MpegEncContext * s, 
866                        DCTELEM block[6][64],
867                        int motion_x, int motion_y);
868 int msmpeg4_decode_picture_header(MpegEncContext * s);
869 int msmpeg4_decode_ext_header(MpegEncContext * s, int buf_size);
870 int ff_msmpeg4_decode_init(MpegEncContext *s);
871 void ff_msmpeg4_encode_init(MpegEncContext *s);
872 int ff_wmv2_decode_picture_header(MpegEncContext * s);
873 int ff_wmv2_decode_secondary_picture_header(MpegEncContext * s);
874 void ff_wmv2_add_mb(MpegEncContext *s, DCTELEM block[6][64], uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr);
875 void ff_mspel_motion(MpegEncContext *s,
876                                uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
877                                uint8_t **ref_picture, op_pixels_func (*pix_op)[4],
878                                int motion_x, int motion_y, int h);
879 int ff_wmv2_encode_picture_header(MpegEncContext * s, int picture_number);
880 void ff_wmv2_encode_mb(MpegEncContext * s, 
881                        DCTELEM block[6][64],
882                        int motion_x, int motion_y);
883
884 /* mjpeg.c */
885 int mjpeg_init(MpegEncContext *s);
886 void mjpeg_close(MpegEncContext *s);
887 void mjpeg_encode_mb(MpegEncContext *s, 
888                      DCTELEM block[6][64]);
889 void mjpeg_picture_header(MpegEncContext *s);
890 void mjpeg_picture_trailer(MpegEncContext *s);
891
892
893 /* rate control */
894 int ff_rate_control_init(MpegEncContext *s);
895 float ff_rate_estimate_qscale(MpegEncContext *s);
896 void ff_write_pass1_stats(MpegEncContext *s);
897 void ff_rate_control_uninit(MpegEncContext *s);
898 double ff_eval(char *s, double *const_value, const char **const_name,
899                double (**func1)(void *, double), const char **func1_name,
900                double (**func2)(void *, double, double), char **func2_name,
901                void *opaque);
902 int ff_vbv_update(MpegEncContext *s, int frame_size);
903
904
905 #endif /* AVCODEC_MPEGVIDEO_H */