]> git.sesse.net Git - ffmpeg/blob - libavcodec/mpegvideo.h
mpeg2/mpeg4 dequantizer support (c & mmx)
[ffmpeg] / libavcodec / mpegvideo.h
1 /*
2  * Generic DCT based hybrid video encoder
3  * Copyright (c) 2000,2001 Gerard Lantau.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program 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
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19
20 /* Macros for picture code type. */
21 #define I_TYPE 1
22 #define P_TYPE 2
23 #define B_TYPE 3
24 #define S_TYPE 4 //S(GMC)-VOP MPEG4
25
26 enum OutputFormat {
27     FMT_MPEG1,
28     FMT_H263,
29     FMT_MJPEG, 
30 };
31
32 #define MPEG_BUF_SIZE (16 * 1024)
33
34 #define QMAT_SHIFT_MMX 19
35 #define QMAT_SHIFT 25
36
37 #define MAX_FCODE 7
38 #define MAX_MV 2048
39 #define REORDER_BUFFER_SIZE (FF_MAX_B_FRAMES+2)
40
41 typedef struct Predictor{
42     double coeff;
43     double count;
44     double decay;
45 } Predictor;
46
47 typedef struct ReorderBuffer{
48     UINT8 *picture[3];
49     int pict_type;
50     int qscale;
51     int force_type;
52     int picture_number;
53     int picture_in_gop_number;
54 } ReorderBuffer;
55
56 typedef struct MpegEncContext {
57     struct AVCodecContext *avctx;
58     /* the following parameters must be initialized before encoding */
59     int width, height; /* picture size. must be a multiple of 16 */
60     int gop_size;
61     int frame_rate; /* number of frames per second */
62     int intra_only; /* if true, only intra pictures are generated */
63     int bit_rate;        /* wanted bit rate */
64     int bit_rate_tolerance; /* amount of +- bits (>0)*/
65     enum OutputFormat out_format; /* output format */
66     int h263_plus; /* h263 plus headers */
67     int h263_rv10; /* use RV10 variation for H263 */
68     int h263_pred; /* use mpeg4/h263 ac/dc predictions */
69     int h263_msmpeg4; /* generate MSMPEG4 compatible stream */
70     int h263_intel; /* use I263 intel h263 header */
71     int fixed_qscale; /* fixed qscale if non zero */
72     float qcompress;  /* amount of qscale change between easy & hard scenes (0.0-1.0) */
73     float qblur;      /* amount of qscale smoothing over time (0.0-1.0) */
74     int qmin;         /* min qscale */
75     int qmax;         /* max qscale */
76     int max_qdiff;    /* max qscale difference between frames */
77     int encoding;     /* true if we are encoding (vs decoding) */
78     int flags;        /* AVCodecContext.flags (HQ, MV4, ...) */
79     int force_input_type;/* 0= no force, otherwise I_TYPE, P_TYPE, ... */
80     int max_b_frames; /* max number of b-frames for encoding */
81     /* the following fields are managed internally by the encoder */
82
83     /* bit output */
84     PutBitContext pb;
85
86     /* sequence parameters */
87     int context_initialized;
88     int input_picture_number;
89     int input_picture_in_gop_number; /* 0-> first pic in gop, ... */
90     int picture_number;
91     int fake_picture_number; /* picture number at the bitstream frame rate */
92     int gop_picture_number;  /* index of the first picture of a GOP based on fake_pic_num & mpeg1 specific */
93     int picture_in_gop_number; /* 0-> first pic in gop, ... */
94     int b_frames_since_non_b;  /* used for encoding, relative to not yet reordered input */
95     int mb_width, mb_height;   /* number of MBs horizontally & vertically */
96     int mb_num;                /* number of MBs of a picture */
97     int linesize;              /* line size, in bytes, may be different from width */
98     UINT8 *new_picture[3];     /* picture to be compressed */
99     UINT8 *picture_buffer[REORDER_BUFFER_SIZE][3]; /* internal buffers used for reordering of input pictures */
100     int picture_buffer_index;
101     ReorderBuffer coded_order[REORDER_BUFFER_SIZE];
102     UINT8 *last_picture[3];      /* previous picture */
103     UINT8 *last_picture_base[3]; /* real start of the picture */
104     UINT8 *next_picture[3];      /* previous picture (for bidir pred) */
105     UINT8 *next_picture_base[3]; /* real start of the picture */
106     UINT8 *aux_picture[3];       /* aux picture (for B frames only) */
107     UINT8 *aux_picture_base[3];  /* real start of the picture */
108     UINT8 *current_picture[3];   /* buffer to store the decompressed current picture */
109     int last_dc[3];              /* last DC values for MPEG1 */
110     INT16 *dc_val[3];            /* used for mpeg4 DC prediction, all 3 arrays must be continuous */
111     int y_dc_scale, c_dc_scale;
112     UINT8 *coded_block;          /* used for coded block pattern prediction (msmpeg4v3, wmv1)*/
113     INT16 (*ac_val[3])[16];      /* used for for mpeg4 AC prediction, all 3 arrays must be continuous */
114     int ac_pred;
115     int mb_skiped;              /* MUST BE SET only during DECODING */
116     UINT8 *mbskip_table;        /* used to avoid copy if macroblock skipped (for black regions for example) 
117                                    and used for b-frame encoding & decoding (contains skip table of next P Frame) */
118     UINT8 *mbintra_table;       /* used to avoid setting {ac, dc, cbp}-pred stuff to zero on inter MB decoding */
119
120     int input_qscale;           /* qscale prior to reordering of frames */
121     int input_pict_type;        /* pict_type prior to reordering of frames */
122     int force_type;             /* 0= no force, otherwise I_TYPE, P_TYPE, ... */
123     int qscale;                 /* QP */
124     int pict_type;              /* I_TYPE, P_TYPE, B_TYPE, ... */
125     int last_non_b_pict_type;   /* used for mpeg4 gmc b-frames */
126     int last_pict_type;         /* used for bit rate stuff (needs that to update the right predictor) */
127     int frame_rate_index;
128     /* motion compensation */
129     int unrestricted_mv;
130     int h263_long_vectors; /* use horrible h263v1 long vector mode */
131
132     int f_code; /* forward MV resolution */
133     int b_code; /* backward MV resolution for B Frames (mpeg4) */
134     INT16 (*motion_val)[2];            /* used for MV prediction (4MV per MB) */
135     INT16 (*p_mv_table)[2];            /* MV table (1MV per MB) p-frame encoding */
136     INT16 (*last_p_mv_table)[2];       /* MV table (1MV per MB) p-frame encoding */
137     INT16 (*b_forw_mv_table)[2];       /* MV table (1MV per MB) forward mode b-frame encoding */
138     INT16 (*b_back_mv_table)[2];       /* MV table (1MV per MB) backward mode b-frame encoding */
139     INT16 (*b_bidir_forw_mv_table)[2]; /* MV table (1MV per MB) bidir mode b-frame encoding */
140     INT16 (*b_bidir_back_mv_table)[2]; /* MV table (1MV per MB) bidir mode b-frame encoding */
141     INT16 (*b_direct_forw_mv_table)[2];/* MV table (1MV per MB) direct mode b-frame encoding */
142     INT16 (*b_direct_back_mv_table)[2];/* MV table (1MV per MB) direct mode b-frame encoding */
143     INT16 (*b_direct_mv_table)[2];     /* MV table (1MV per MB) direct mode b-frame encoding */
144     int me_method;          /* ME algorithm */
145     int mv_dir;
146 #define MV_DIR_BACKWARD  1
147 #define MV_DIR_FORWARD   2
148 #define MV_DIRECT        4 // bidirectional mode where the difference equals the MV of the last P/S/I-Frame (mpeg4)
149     int mv_type;
150 #define MV_TYPE_16X16       0   /* 1 vector for the whole mb */
151 #define MV_TYPE_8X8         1   /* 4 vectors (h263, mpeg4 4MV) */
152 #define MV_TYPE_16X8        2   /* 2 vectors, one per 16x8 block */ 
153 #define MV_TYPE_FIELD       3   /* 2 vectors, one per field */ 
154 #define MV_TYPE_DMV         4   /* 2 vectors, special mpeg2 Dual Prime Vectors */
155     /* motion vectors for a macroblock 
156        first coordinate : 0 = forward 1 = backward
157        second "         : depend on type
158        third  "         : 0 = x, 1 = y
159     */
160     int mv[2][4][2];
161     int field_select[2][2];
162     int last_mv[2][2][2];             /* last MV, used for MV prediction in MPEG1 & B-frame MPEG4 */
163     UINT16 (*mv_penalty)[MAX_MV*2+1]; /* amount of bits needed to encode a MV, used for ME */
164     UINT8 *fcode_tab; /* smallest fcode needed for each MV */
165
166     int has_b_frames;
167     int no_rounding; /* apply no rounding to motion compensation (MPEG4, msmpeg4, ...) */
168
169     /* macroblock layer */
170     int mb_x, mb_y;
171     int mb_incr;
172     int mb_intra;
173     UINT16 *mb_var;    /* Table for MB variances */
174     UINT8 *mb_type;    /* Table for MB type */
175 #define MB_TYPE_INTRA    0x01
176 #define MB_TYPE_INTER    0x02
177 #define MB_TYPE_INTER4V  0x04
178 #define MB_TYPE_SKIPED   0x08
179 #define MB_TYPE_DIRECT   0x10
180 #define MB_TYPE_FORWARD  0x20
181 #define MB_TYPE_BACKWARD 0x40
182 #define MB_TYPE_BIDIR    0x80
183
184     int block_index[6]; /* index to current MB in block based arrays with edges*/
185     int block_wrap[6];
186
187     /* matrix transmitted in the bitstream */
188     UINT16 intra_matrix[64];
189     UINT16 chroma_intra_matrix[64];
190     UINT16 non_intra_matrix[64];
191     UINT16 chroma_non_intra_matrix[64];
192     /* precomputed matrix (combine qscale and DCT renorm) */
193     int q_intra_matrix[64];
194     int q_non_intra_matrix[64];
195     /* identical to the above but for MMX & these are not permutated */
196     UINT16 __align8 q_intra_matrix16[64] ;
197     UINT16 __align8 q_non_intra_matrix16[64];
198     int block_last_index[6];  /* last non zero coefficient in block */
199
200     void *opaque; /* private data for the user */
201
202     /* bit rate control */
203     int I_frame_bits; //FIXME used in mpeg12 ...
204     int avg_mb_var;        /* average MB variance for current frame */
205     int mc_mb_var;     /* motion compensated MB variance for current frame */
206     int last_mc_mb_var;     /* motion compensated MB variance for last frame */
207     INT64 wanted_bits;
208     INT64 total_bits;
209     int frame_bits;      /* bits used for the current frame */
210     int last_frame_bits; /* bits used for the last frame */
211     Predictor i_pred;
212     Predictor p_pred;
213     double qsum;         /* sum of qscales */
214     double qcount;       /* count of qscales */
215     double short_term_qsum;   /* sum of recent qscales */
216     double short_term_qcount; /* count of recent qscales */
217
218     /* statistics, used for 2-pass encoding */
219     int mv_bits;
220     int header_bits;
221     int i_tex_bits;
222     int p_tex_bits;
223     int i_count;
224     int p_count;
225     int skip_count;
226     int misc_bits; // cbp, mb_type
227     int last_bits; //temp var used for calculating the above vars
228
229     /* H.263 specific */
230     int gob_number;
231     int gob_index;
232     int first_gob_line;
233         
234     /* H.263+ specific */
235     int umvplus;
236     int umvplus_dec;
237     int h263_aic; /* Advanded INTRA Coding (AIC) */
238     int h263_aic_dir; /* AIC direction: 0 = left, 1 = top */
239     
240     /* mpeg4 specific */
241     int time_increment_resolution;
242     int time_increment_bits;        /* number of bits to represent the fractional part of time */
243     int last_time_base;
244     int time_base;                  /* time in seconds of last I,P,S Frame */
245     int64_t time;                   /* time of current frame */ 
246     int64_t last_non_b_time;
247     uint16_t pp_time;               /* time distance between the last 2 p,s,i frames */
248     uint16_t bp_time;               /* time distance between the last b and p,s,i frame */
249     int shape;
250     int vol_sprite_usage;
251     int sprite_width;
252     int sprite_height;
253     int sprite_left;
254     int sprite_top;
255     int sprite_brightness_change;
256     int num_sprite_warping_points;
257     int real_sprite_warping_points;
258     int sprite_offset[2][2];
259     int sprite_delta[2][2][2];
260     int sprite_shift[2][2];
261     int mcsel;
262     int quant_precision;
263     int quarter_sample;              /* 1->qpel, 0->half pel ME/MC */ 
264     int scalability;
265     int new_pred;
266     int reduced_res_vop;
267     int aspect_ratio_info;
268     int sprite_warping_accuracy;
269     int low_latency_sprite;
270     int data_partioning;
271     int resync_marker;
272     int resync_x_pos;
273
274     /* divx specific, used to workaround (many) bugs in divx5 */
275     int divx_version;
276     int divx_build;
277
278     /* RV10 specific */
279     int rv10_version; /* RV10 version: 0 or 3 */
280     int rv10_first_dc_coded[3];
281     
282     /* MJPEG specific */
283     struct MJpegContext *mjpeg_ctx;
284     int mjpeg_vsample[3]; /* vertical sampling factors, default = {2, 1, 1} */
285     int mjpeg_hsample[3]; /* horizontal sampling factors, default = {2, 1, 1} */
286     int mjpeg_write_tables; /* do we want to have quantisation- and
287                                huffmantables in the jpeg file ? */
288
289     /* MSMPEG4 specific */
290     int mv_table_index;
291     int rl_table_index;
292     int rl_chroma_table_index;
293     int dc_table_index;
294     int use_skip_mb_code;
295     int slice_height;      /* in macroblocks */
296     int first_slice_line;  /* used in mpeg4 too to handle resync markers */
297     int flipflop_rounding;
298     int bitrate;
299     int msmpeg4_version;   /* 1=mp41, 2=mp42, 3=mp43/divx3 */
300     /* decompression specific */
301     GetBitContext gb;
302
303     /* MPEG2 specific - I wish I had not to support this mess. */
304     int progressive_sequence;
305     int mpeg_f_code[2][2];
306     int picture_structure;
307 /* picture type */
308 #define PICT_TOP_FIELD     1
309 #define PICT_BOTTOM_FIELD  2
310 #define PICT_FRAME         3
311
312     int intra_dc_precision;
313     int frame_pred_frame_dct;
314     int top_field_first;
315     int concealment_motion_vectors;
316     int q_scale_type;
317     int intra_vlc_format;
318     int alternate_scan;
319     int repeat_first_field;
320     int chroma_420_type;
321     int progressive_frame;
322     int mpeg2;
323     int full_pel[2];
324     int interlaced_dct;
325     int last_qscale;
326     int first_slice;
327     
328     /* RTP specific */
329     /* These are explained on avcodec.h */
330     int rtp_mode;
331     int rtp_payload_size;
332     void (*rtp_callback)(void *data, int size, int packet_number);
333     UINT8 *ptr_lastgob;
334     UINT8 *ptr_last_mb_line;
335     UINT32 mb_line_avgsize;
336     
337     DCTELEM (*block)[64]; /* points to one of the following blocks */
338     DCTELEM intra_block[6][64] __align8;
339     DCTELEM inter_block[6][64] __align8;
340     DCTELEM inter4v_block[6][64] __align8;
341     void (*dct_unquantize_mpeg1)(struct MpegEncContext *s, 
342                            DCTELEM *block, int n, int qscale);
343     void (*dct_unquantize_mpeg2)(struct MpegEncContext *s, 
344                            DCTELEM *block, int n, int qscale);
345     void (*dct_unquantize_h263)(struct MpegEncContext *s, 
346                            DCTELEM *block, int n, int qscale);
347     void (*dct_unquantize)(struct MpegEncContext *s, // unquantizer to use (mpeg4 can use both)
348                            DCTELEM *block, int n, int qscale);
349 } MpegEncContext;
350
351 int MPV_common_init(MpegEncContext *s);
352 void MPV_common_end(MpegEncContext *s);
353 void MPV_decode_mb(MpegEncContext *s, DCTELEM block[6][64]);
354 void MPV_frame_start(MpegEncContext *s);
355 void MPV_frame_end(MpegEncContext *s);
356 #ifdef HAVE_MMX
357 void MPV_common_init_mmx(MpegEncContext *s);
358 #endif
359
360 /* motion_est.c */
361 void ff_estimate_p_frame_motion(MpegEncContext * s,
362                              int mb_x, int mb_y);
363 void ff_estimate_b_frame_motion(MpegEncContext * s,
364                              int mb_x, int mb_y);
365 int ff_get_best_fcode(MpegEncContext * s, int16_t (*mv_table)[2], int type);
366 void ff_fix_long_p_mvs(MpegEncContext * s);
367 void ff_fix_long_b_mvs(MpegEncContext * s, int16_t (*mv_table)[2], int f_code, int type);
368
369 /* mpeg12.c */
370 extern INT16 default_intra_matrix[64];
371 extern INT16 default_non_intra_matrix[64];
372
373 void mpeg1_encode_picture_header(MpegEncContext *s, int picture_number);
374 void mpeg1_encode_mb(MpegEncContext *s,
375                      DCTELEM block[6][64],
376                      int motion_x, int motion_y);
377 void mpeg1_encode_init(MpegEncContext *s);
378
379 /* h263enc.c */
380
381 /* run length table */
382 #define MAX_RUN    64
383 #define MAX_LEVEL  64
384
385 typedef struct RLTable {
386     int n; /* number of entries of table_vlc minus 1 */
387     int last; /* number of values for last = 0 */
388     const UINT16 (*table_vlc)[2];
389     const INT8 *table_run;
390     const INT8 *table_level;
391     UINT8 *index_run[2]; /* encoding only */
392     INT8 *max_level[2]; /* encoding & decoding */
393     INT8 *max_run[2];   /* encoding & decoding */
394     VLC vlc;            /* decoding only */
395 } RLTable;
396
397 void init_rl(RLTable *rl);
398 void init_vlc_rl(RLTable *rl);
399
400 static inline int get_rl_index(const RLTable *rl, int last, int run, int level)
401 {
402     int index;
403     index = rl->index_run[last][run];
404     if (index >= rl->n)
405         return rl->n;
406     if (level > rl->max_level[last][run])
407         return rl->n;
408     return index + level - 1;
409 }
410
411 void h263_encode_mb(MpegEncContext *s, 
412                     DCTELEM block[6][64],
413                     int motion_x, int motion_y);
414 void mpeg4_encode_mb(MpegEncContext *s, 
415                     DCTELEM block[6][64],
416                     int motion_x, int motion_y);
417 void h263_encode_picture_header(MpegEncContext *s, int picture_number);
418 int h263_encode_gob_header(MpegEncContext * s, int mb_line);
419 void h263_dc_scale(MpegEncContext *s);
420 INT16 *h263_pred_motion(MpegEncContext * s, int block, 
421                         int *px, int *py);
422 void mpeg4_pred_ac(MpegEncContext * s, INT16 *block, int n, 
423                    int dir);
424 void mpeg4_encode_picture_header(MpegEncContext *s, int picture_number);
425 void h263_encode_init(MpegEncContext *s);
426
427 void h263_decode_init_vlc(MpegEncContext *s);
428 int h263_decode_picture_header(MpegEncContext *s);
429 int h263_decode_gob_header(MpegEncContext *s);
430 int mpeg4_decode_picture_header(MpegEncContext * s);
431 int intel_h263_decode_picture_header(MpegEncContext *s);
432 int h263_decode_mb(MpegEncContext *s,
433                    DCTELEM block[6][64]);
434 int h263_get_picture_format(int width, int height);
435
436 /* rv10.c */
437 void rv10_encode_picture_header(MpegEncContext *s, int picture_number);
438 int rv_decode_dc(MpegEncContext *s, int n);
439
440 /* msmpeg4.c */
441 void msmpeg4_encode_picture_header(MpegEncContext * s, int picture_number);
442 void msmpeg4_encode_ext_header(MpegEncContext * s);
443 void msmpeg4_encode_mb(MpegEncContext * s, 
444                        DCTELEM block[6][64],
445                        int motion_x, int motion_y);
446 void msmpeg4_dc_scale(MpegEncContext * s);
447 int msmpeg4_decode_picture_header(MpegEncContext * s);
448 int msmpeg4_decode_ext_header(MpegEncContext * s, int buf_size);
449 int msmpeg4_decode_mb(MpegEncContext *s, 
450                       DCTELEM block[6][64]);
451 int msmpeg4_decode_init_vlc(MpegEncContext *s);
452
453 /* mjpegenc.c */
454
455 int mjpeg_init(MpegEncContext *s);
456 void mjpeg_close(MpegEncContext *s);
457 void mjpeg_encode_mb(MpegEncContext *s, 
458                      DCTELEM block[6][64]);
459 void mjpeg_picture_header(MpegEncContext *s);
460 void mjpeg_picture_trailer(MpegEncContext *s);