comparison mpegvideo.h @ 324:9c6f056f0e41 libavcodec

fixed mpeg4 time stuff on encoding mpeg4 b-frame enoding support removed old, out-commented ratecontrol reuse motion compensation code between encoding & decoding prefix newly added global functions with ff_ to reduce namespace polution b-frame ME (unfinished, but working) added some comments to mpegvideo.h do MC on encoding only once if possible bugs? ;)
author michaelni
date Wed, 17 Apr 2002 04:32:12 +0000
parents cda7d0857baf
children 15efd80cf51e
comparison
equal deleted inserted replaced
323:68cc7650c645 324:9c6f056f0e41
24 #define S_TYPE 4 //S(GMC)-VOP MPEG4 24 #define S_TYPE 4 //S(GMC)-VOP MPEG4
25 25
26 enum OutputFormat { 26 enum OutputFormat {
27 FMT_MPEG1, 27 FMT_MPEG1,
28 FMT_H263, 28 FMT_H263,
29 FMT_MJPEG, 29 FMT_MJPEG,
30 }; 30 };
31 31
32 #define MPEG_BUF_SIZE (16 * 1024) 32 #define MPEG_BUF_SIZE (16 * 1024)
33 33
34 #define QMAT_SHIFT_MMX 19 34 #define QMAT_SHIFT_MMX 19
35 #define QMAT_SHIFT 25 35 #define QMAT_SHIFT 25
36 36
37 #define MAX_FCODE 7 37 #define MAX_FCODE 7
38 #define MAX_MV 2048 38 #define MAX_MV 2048
39 #define REORDER_BUFFER_SIZE (FF_MAX_B_FRAMES+2)
39 40
40 typedef struct Predictor{ 41 typedef struct Predictor{
41 double coeff; 42 double coeff;
42 double count; 43 double count;
43 double decay; 44 double decay;
44 } Predictor; 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;
45 55
46 typedef struct MpegEncContext { 56 typedef struct MpegEncContext {
47 struct AVCodecContext *avctx; 57 struct AVCodecContext *avctx;
48 /* the following parameters must be initialized before encoding */ 58 /* the following parameters must be initialized before encoding */
49 int width, height; /* picture size. must be a multiple of 16 */ 59 int width, height; /* picture size. must be a multiple of 16 */
64 int qmin; /* min qscale */ 74 int qmin; /* min qscale */
65 int qmax; /* max qscale */ 75 int qmax; /* max qscale */
66 int max_qdiff; /* max qscale difference between frames */ 76 int max_qdiff; /* max qscale difference between frames */
67 int encoding; /* true if we are encoding (vs decoding) */ 77 int encoding; /* true if we are encoding (vs decoding) */
68 int flags; /* AVCodecContext.flags (HQ, MV4, ...) */ 78 int flags; /* AVCodecContext.flags (HQ, MV4, ...) */
69 int force_type; /* 0= no force, otherwise I_TYPE, P_TYPE, ... */ 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 */
70 /* the following fields are managed internally by the encoder */ 81 /* the following fields are managed internally by the encoder */
71 82
72 /* bit output */ 83 /* bit output */
73 PutBitContext pb; 84 PutBitContext pb;
74 85
75 /* sequence parameters */ 86 /* sequence parameters */
76 int context_initialized; 87 int context_initialized;
88 int input_picture_number;
89 int input_picture_in_gop_number; /* 0-> first pic in gop, ... */
77 int picture_number; 90 int picture_number;
78 int fake_picture_number; /* picture number at the bitstream frame rate */ 91 int fake_picture_number; /* picture number at the bitstream frame rate */
79 int gop_picture_number; /* index of the first picture of a GOP based on fake_pic_num & mpeg1 specific */ 92 int gop_picture_number; /* index of the first picture of a GOP based on fake_pic_num & mpeg1 specific */
80 int picture_in_gop_number; /* 0-> first pic in gop, ... */ 93 int picture_in_gop_number; /* 0-> first pic in gop, ... */
81 int mb_width, mb_height; 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 */
82 int mb_num; /* number of MBs of a picture */ 96 int mb_num; /* number of MBs of a picture */
83 int linesize; /* line size, in bytes, may be different from width */ 97 int linesize; /* line size, in bytes, may be different from width */
84 UINT8 *new_picture[3]; /* picture to be compressed */ 98 UINT8 *new_picture[3]; /* picture to be compressed */
85 UINT8 *last_picture[3]; /* previous picture */ 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 */
86 UINT8 *last_picture_base[3]; /* real start of the picture */ 103 UINT8 *last_picture_base[3]; /* real start of the picture */
87 UINT8 *next_picture[3]; /* previous picture (for bidir pred) */ 104 UINT8 *next_picture[3]; /* previous picture (for bidir pred) */
88 UINT8 *next_picture_base[3]; /* real start of the picture */ 105 UINT8 *next_picture_base[3]; /* real start of the picture */
89 UINT8 *aux_picture[3]; /* aux picture (for B frames only) */ 106 UINT8 *aux_picture[3]; /* aux picture (for B frames only) */
90 UINT8 *aux_picture_base[3]; /* real start of the picture */ 107 UINT8 *aux_picture_base[3]; /* real start of the picture */
91 UINT8 *current_picture[3]; /* buffer to store the decompressed current picture */ 108 UINT8 *current_picture[3]; /* buffer to store the decompressed current picture */
92 int last_dc[3]; /* last DC values for MPEG1 */ 109 int last_dc[3]; /* last DC values for MPEG1 */
93 INT16 *dc_val[3]; /* used for mpeg4 DC prediction, all 3 arrays must be continuous */ 110 INT16 *dc_val[3]; /* used for mpeg4 DC prediction, all 3 arrays must be continuous */
94 int y_dc_scale, c_dc_scale; 111 int y_dc_scale, c_dc_scale;
95 UINT8 *coded_block; /* used for coded block pattern prediction */ 112 UINT8 *coded_block; /* used for coded block pattern prediction (msmpeg4v3, wmv1)*/
96 INT16 (*ac_val[3])[16]; /* used for for mpeg4 AC prediction, all 3 arrays must be continuous */ 113 INT16 (*ac_val[3])[16]; /* used for for mpeg4 AC prediction, all 3 arrays must be continuous */
97 int ac_pred; 114 int ac_pred;
98 int mb_skiped; /* MUST BE SET only during DECODING */ 115 int mb_skiped; /* MUST BE SET only during DECODING */
99 UINT8 *mbskip_table; /* used to avoid copy if macroblock 116 UINT8 *mbskip_table; /* used to avoid copy if macroblock skipped (for black regions for example)
100 skipped (for black regions for example) */ 117 and used for b-frame encoding & decoding (contains skip table of next P Frame) */
101 UINT8 *mbintra_table; /* used to kill a few memsets */ 118 UINT8 *mbintra_table; /* used to avoid setting {ac, dc, cbp}-pred stuff to zero on inter MB decoding */
102 119
103 int qscale; 120 int input_qscale; /* qscale prior to reordering of frames */
104 int pict_type; 121 int input_pict_type; /* pict_type prior to reordering of frames */
105 int last_non_b_pict_type; /* used for mpeg4 gmc b-frames */ 122 int force_type; /* 0= no force, otherwise I_TYPE, P_TYPE, ... */
106 int last_pict_type; /* used for bit rate stuff (needs that to update the right predictor) */ 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) */
107 int frame_rate_index; 127 int frame_rate_index;
108 /* motion compensation */ 128 /* motion compensation */
109 int unrestricted_mv; 129 int unrestricted_mv;
110 int h263_long_vectors; /* use horrible h263v1 long vector mode */ 130 int h263_long_vectors; /* use horrible h263v1 long vector mode */
111 131
112 int f_code; /* resolution */ 132 int f_code; /* forward MV resolution */
113 int b_code; /* backward resolution for B Frames (mpeg4) */ 133 int b_code; /* backward MV resolution for B Frames (mpeg4) */
114 INT16 *mv_table[2]; /* MV table (1MV per MB)*/ 134 INT16 (*motion_val)[2]; /* used for MV prediction (4MV per MB) */
115 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 */
116 int me_method; /* ME algorithm */ 144 int me_method; /* ME algorithm */
117 int mv_dir; 145 int mv_dir;
118 #define MV_DIR_BACKWARD 1 146 #define MV_DIR_BACKWARD 1
119 #define MV_DIR_FORWARD 2 147 #define MV_DIR_FORWARD 2
120 #define MV_DIRECT 4 // bidirectional mode where the difference equals the MV of the last P/S/I-Frame (mpeg4) 148 #define MV_DIRECT 4 // bidirectional mode where the difference equals the MV of the last P/S/I-Frame (mpeg4)
129 second " : depend on type 157 second " : depend on type
130 third " : 0 = x, 1 = y 158 third " : 0 = x, 1 = y
131 */ 159 */
132 int mv[2][4][2]; 160 int mv[2][4][2];
133 int field_select[2][2]; 161 int field_select[2][2];
134 int last_mv[2][2][2]; 162 int last_mv[2][2][2]; /* last MV, used for MV prediction in MPEG1 & B-frame MPEG4 */
135 UINT16 (*mv_penalty)[MAX_MV*2+1]; /* amount of bits needed to encode a MV, used for ME */ 163 UINT16 (*mv_penalty)[MAX_MV*2+1]; /* amount of bits needed to encode a MV, used for ME */
136 UINT8 *fcode_tab; /* smallest fcode needed for each MV */ 164 UINT8 *fcode_tab; /* smallest fcode needed for each MV */
137 165
138 int has_b_frames; 166 int has_b_frames;
139 int no_rounding; /* apply no rounding to motion estimation (MPEG4) */ 167 int no_rounding; /* apply no rounding to motion compensation (MPEG4, msmpeg4, ...) */
140 168
141 /* macroblock layer */ 169 /* macroblock layer */
142 int mb_x, mb_y; 170 int mb_x, mb_y;
143 int mb_incr; 171 int mb_incr;
144 int mb_intra; 172 int mb_intra;
148 #define MB_TYPE_INTER 0x02 176 #define MB_TYPE_INTER 0x02
149 #define MB_TYPE_INTER4V 0x04 177 #define MB_TYPE_INTER4V 0x04
150 #define MB_TYPE_SKIPED 0x08 178 #define MB_TYPE_SKIPED 0x08
151 #define MB_TYPE_DIRECT 0x10 179 #define MB_TYPE_DIRECT 0x10
152 #define MB_TYPE_FORWARD 0x20 180 #define MB_TYPE_FORWARD 0x20
153 #define MB_TYPE_BACKWAD 0x40 181 #define MB_TYPE_BACKWARD 0x40
154 #define MB_TYPE_BIDIR 0x80 182 #define MB_TYPE_BIDIR 0x80
155 183
156 int block_index[6]; 184 int block_index[6]; /* index to current MB in block based arrays with edges*/
157 int block_wrap[6]; 185 int block_wrap[6];
158 186
159 /* matrix transmitted in the bitstream */ 187 /* matrix transmitted in the bitstream */
160 UINT16 intra_matrix[64]; 188 UINT16 intra_matrix[64];
161 UINT16 chroma_intra_matrix[64]; 189 UINT16 chroma_intra_matrix[64];
170 int block_last_index[6]; /* last non zero coefficient in block */ 198 int block_last_index[6]; /* last non zero coefficient in block */
171 199
172 void *opaque; /* private data for the user */ 200 void *opaque; /* private data for the user */
173 201
174 /* bit rate control */ 202 /* bit rate control */
175 int I_frame_bits; /* wanted number of bits per I frame */ 203 int I_frame_bits; //FIXME used in mpeg12 ...
176 int P_frame_bits; /* same for P frame */
177 int avg_mb_var; /* average MB variance for current frame */ 204 int avg_mb_var; /* average MB variance for current frame */
178 int mc_mb_var; /* motion compensated MB variance for current frame */ 205 int mc_mb_var; /* motion compensated MB variance for current frame */
179 int last_mc_mb_var; /* motion compensated MB variance for last frame */ 206 int last_mc_mb_var; /* motion compensated MB variance for last frame */
180 INT64 wanted_bits; 207 INT64 wanted_bits;
181 INT64 total_bits; 208 INT64 total_bits;
210 int h263_aic; /* Advanded INTRA Coding (AIC) */ 237 int h263_aic; /* Advanded INTRA Coding (AIC) */
211 int h263_aic_dir; /* AIC direction: 0 = left, 1 = top */ 238 int h263_aic_dir; /* AIC direction: 0 = left, 1 = top */
212 239
213 /* mpeg4 specific */ 240 /* mpeg4 specific */
214 int time_increment_resolution; 241 int time_increment_resolution;
215 int time_increment_bits; 242 int time_increment_bits; /* number of bits to represent the fractional part of time */
216 int time_increment; 243 int last_time_base;
217 int time_base; 244 int time_base; /* time in seconds of last I,P,S Frame */
218 int time; 245 int64_t time; /* time of current frame */
219 int last_non_b_time[2]; 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 */
220 int shape; 249 int shape;
221 int vol_sprite_usage; 250 int vol_sprite_usage;
222 int sprite_width; 251 int sprite_width;
223 int sprite_height; 252 int sprite_height;
224 int sprite_left; 253 int sprite_left;
229 int sprite_offset[2][2]; 258 int sprite_offset[2][2];
230 int sprite_delta[2][2][2]; 259 int sprite_delta[2][2][2];
231 int sprite_shift[2][2]; 260 int sprite_shift[2][2];
232 int mcsel; 261 int mcsel;
233 int quant_precision; 262 int quant_precision;
234 int quarter_sample; 263 int quarter_sample; /* 1->qpel, 0->half pel ME/MC */
235 int scalability; 264 int scalability;
236 int new_pred; 265 int new_pred;
237 int reduced_res_vop; 266 int reduced_res_vop;
238 int aspect_ratio_info; 267 int aspect_ratio_info;
239 int sprite_warping_accuracy; 268 int sprite_warping_accuracy;
325 #ifdef HAVE_MMX 354 #ifdef HAVE_MMX
326 void MPV_common_init_mmx(MpegEncContext *s); 355 void MPV_common_init_mmx(MpegEncContext *s);
327 #endif 356 #endif
328 357
329 /* motion_est.c */ 358 /* motion_est.c */
330 359 void ff_estimate_p_frame_motion(MpegEncContext * s,
331 void estimate_motion(MpegEncContext *s, 360 int mb_x, int mb_y);
332 int mb_x, int mb_y); 361 void ff_estimate_b_frame_motion(MpegEncContext * s,
362 int mb_x, int mb_y);
363 int ff_get_best_fcode(MpegEncContext * s, int16_t (*mv_table)[2], int type);
364 void ff_fix_long_p_mvs(MpegEncContext * s);
365 void ff_fix_long_b_mvs(MpegEncContext * s, int16_t (*mv_table)[2], int f_code, int type);
333 366
334 /* mpeg12.c */ 367 /* mpeg12.c */
335 extern INT16 default_intra_matrix[64]; 368 extern INT16 default_intra_matrix[64];
336 extern INT16 default_non_intra_matrix[64]; 369 extern INT16 default_non_intra_matrix[64];
337 370