2 * Generic DCT based hybrid video encoder
3 * Copyright (c) 2000,2001 Gerard Lantau.
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.
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.
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.
20 /* Macros for picture code type. */
24 #define S_TYPE 4 //S(GMC)-VOP MPEG4
32 #define MPEG_BUF_SIZE (16 * 1024)
34 #define QMAT_SHIFT_MMX 19
39 #define REORDER_BUFFER_SIZE (FF_MAX_B_FRAMES+2)
41 typedef struct Predictor{
47 typedef struct ReorderBuffer{
53 int picture_in_gop_number;
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 */
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 */
86 /* sequence parameters */
87 int context_initialized;
88 int input_picture_number;
89 int input_picture_in_gop_number; /* 0-> first pic in gop, ... */
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 */
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 */
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, ... */
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 */
130 int h263_long_vectors; /* use horrible h263v1 long vector mode */
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 */
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)
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
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 */
167 int no_rounding; /* apply no rounding to motion compensation (MPEG4, msmpeg4, ...) */
169 /* macroblock layer */
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
184 int block_index[6]; /* index to current MB in block based arrays with edges*/
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 */
200 void *opaque; /* private data for the user */
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 */
209 int frame_bits; /* bits used for the current frame */
210 int last_frame_bits; /* bits used for the last frame */
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 */
218 /* statistics, used for 2-pass encoding */
226 int misc_bits; // cbp, mb_type
227 int last_bits; //temp var used for calculating the above vars
234 /* H.263+ specific */
237 int h263_aic; /* Advanded INTRA Coding (AIC) */
238 int h263_aic_dir; /* AIC direction: 0 = left, 1 = top */
241 int time_increment_resolution;
242 int time_increment_bits; /* number of bits to represent the fractional part of time */
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 */
250 int vol_sprite_usage;
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];
263 int quarter_sample; /* 1->qpel, 0->half pel ME/MC */
267 int aspect_ratio_info;
268 int sprite_warping_accuracy;
269 int low_latency_sprite;
274 /* divx specific, used to workaround (many) bugs in divx5 */
279 int rv10_version; /* RV10 version: 0 or 3 */
280 int rv10_first_dc_coded[3];
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 ? */
289 /* MSMPEG4 specific */
292 int rl_chroma_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;
299 int msmpeg4_version; /* 1=mp41, 2=mp42, 3=mp43/divx3 */
300 /* decompression specific */
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;
308 #define PICT_TOP_FIELD 1
309 #define PICT_BOTTOM_FIELD 2
312 int intra_dc_precision;
313 int frame_pred_frame_dct;
315 int concealment_motion_vectors;
317 int intra_vlc_format;
319 int repeat_first_field;
321 int progressive_frame;
329 /* These are explained on avcodec.h */
331 int rtp_payload_size;
332 void (*rtp_callback)(void *data, int size, int packet_number);
334 UINT8 *ptr_last_mb_line;
335 UINT32 mb_line_avgsize;
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);
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);
357 void MPV_common_init_mmx(MpegEncContext *s);
361 void ff_estimate_p_frame_motion(MpegEncContext * s,
363 void ff_estimate_b_frame_motion(MpegEncContext * s,
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);
370 extern INT16 default_intra_matrix[64];
371 extern INT16 default_non_intra_matrix[64];
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);
381 /* run length table */
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 */
397 void init_rl(RLTable *rl);
398 void init_vlc_rl(RLTable *rl);
400 static inline int get_rl_index(const RLTable *rl, int last, int run, int level)
403 index = rl->index_run[last][run];
406 if (level > rl->max_level[last][run])
408 return index + level - 1;
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,
422 void mpeg4_pred_ac(MpegEncContext * s, INT16 *block, int n,
424 void mpeg4_encode_picture_header(MpegEncContext *s, int picture_number);
425 void h263_encode_init(MpegEncContext *s);
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);
437 void rv10_encode_picture_header(MpegEncContext *s, int picture_number);
438 int rv_decode_dc(MpegEncContext *s, int n);
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);
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);