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
37 typedef struct MpegEncContext {
38 struct AVCodecContext *avctx;
39 /* the following parameters must be initialized before encoding */
40 int width, height; /* picture size. must be a multiple of 16 */
42 int frame_rate; /* number of frames per second */
43 int intra_only; /* if true, only intra pictures are generated */
44 int bit_rate; /* wanted bit rate */
45 enum OutputFormat out_format; /* output format */
46 int h263_plus; /* h263 plus headers */
47 int h263_rv10; /* use RV10 variation for H263 */
48 int h263_pred; /* use mpeg4/h263 ac/dc predictions */
49 int h263_msmpeg4; /* generate MSMPEG4 compatible stream */
50 int h263_intel; /* use I263 intel h263 header */
51 int fixed_qscale; /* fixed qscale if non zero */
52 int encoding; /* true if we are encoding (vs decoding) */
53 /* the following fields are managed internally by the encoder */
58 /* sequence parameters */
59 int context_initialized;
61 int fake_picture_number; /* picture number at the bitstream frame rate */
62 int gop_picture_number; /* index of the first picture of a GOP */
63 int mb_width, mb_height;
64 int mb_num; /* number of MBs of a picture */
65 int linesize; /* line size, in bytes, may be different from width */
66 UINT8 *new_picture[3]; /* picture to be compressed */
67 UINT8 *last_picture[3]; /* previous picture */
68 UINT8 *last_picture_base[3]; /* real start of the picture */
69 UINT8 *next_picture[3]; /* previous picture (for bidir pred) */
70 UINT8 *next_picture_base[3]; /* real start of the picture */
71 UINT8 *aux_picture[3]; /* aux picture (for B frames only) */
72 UINT8 *aux_picture_base[3]; /* real start of the picture */
73 UINT8 *current_picture[3]; /* buffer to store the decompressed current picture */
74 int last_dc[3]; /* last DC values for MPEG1 */
75 INT16 *dc_val[3]; /* used for mpeg4 DC prediction */
76 int y_dc_scale, c_dc_scale;
77 UINT8 *coded_block; /* used for coded block pattern prediction */
78 INT16 (*ac_val[3])[16]; /* used for for mpeg4 AC prediction */
80 int mb_skiped; /* MUST BE SET only during DECODING */
81 UINT8 *mbskip_table; /* used to avoid copy if macroblock
82 skipped (for black regions for example) */
83 UINT8 *mbintra_table; /* used to kill a few memsets */
88 /* motion compensation */
90 int h263_long_vectors; /* use horrible h263v1 long vector mode */
92 int f_code; /* resolution */
93 int b_code; /* resolution for B Frames*/
94 INT16 *mv_table[2]; /* MV table */
95 INT16 (*motion_val)[2]; /* used for MV prediction */
98 #define MV_DIR_BACKWARD 1
99 #define MV_DIR_FORWARD 2
101 #define MV_TYPE_16X16 0 /* 1 vector for the whole mb */
102 #define MV_TYPE_8X8 1 /* 4 vectors (h263) */
103 #define MV_TYPE_16X8 2 /* 2 vectors, one per 16x8 block */
104 #define MV_TYPE_FIELD 3 /* 2 vectors, one per field */
105 #define MV_TYPE_DMV 4 /* 2 vectors, special mpeg2 Dual Prime Vectors */
106 /* motion vectors for a macroblock
107 first coordinate : 0 = forward 1 = backward
108 second " : depend on type
109 third " : 0 = x, 1 = y
112 int field_select[2][2];
113 int last_mv[2][2][2];
116 int no_rounding; /* apply no rounding to motion estimation (MPEG4) */
118 /* macroblock layer */
122 INT16 *mb_var; /* Table for MB variances */
123 char *mb_type; /* Table for MB type */
125 /* matrix transmitted in the bitstream */
126 UINT16 intra_matrix[64];
127 UINT16 chroma_intra_matrix[64];
128 UINT16 non_intra_matrix[64];
129 UINT16 chroma_non_intra_matrix[64];
130 /* precomputed matrix (combine qscale and DCT renorm) */
131 int q_intra_matrix[64];
132 int q_non_intra_matrix[64];
133 /* identical to the above but for MMX & these are not permutated */
134 UINT16 __align8 q_intra_matrix16[64] ;
135 UINT16 __align8 q_non_intra_matrix16[64];
136 int block_last_index[6]; /* last non zero coefficient in block */
138 void *opaque; /* private data for the user */
140 /* bit rate control */
141 int I_frame_bits; /* wanted number of bits per I frame */
142 int P_frame_bits; /* same for P frame */
143 int avg_mb_var; /* average MB variance for current frame */
152 /* H.263+ specific */
155 int h263_aic; /* Advanded INTRA Coding (AIC) */
156 int h263_aic_dir; /* AIC direction: 0 = left, 1 = top */
159 int time_increment_bits;
161 int vol_sprite_usage;
166 int sprite_brightness_change;
167 int num_sprite_warping_points;
168 int real_sprite_warping_points;
169 int sprite_offset[2][2];
170 int sprite_delta[2][2][2];
171 int sprite_shift[2][2];
178 int aspect_ratio_info;
179 int sprite_warping_accuracy;
180 int low_latency_sprite;
183 /* divx specific, used to workaround (many) bugs in divx5 */
188 int rv10_version; /* RV10 version: 0 or 3 */
189 int rv10_first_dc_coded[3];
192 struct MJpegContext *mjpeg_ctx;
193 int mjpeg_vsample[3]; /* vertical sampling factors, default = {2, 1, 1} */
194 int mjpeg_hsample[3]; /* horizontal sampling factors, default = {2, 1, 1} */
195 int mjpeg_write_tables; /* do we want to have quantisation- and
196 huffmantables in the jpeg file ? */
198 /* MSMPEG4 specific */
201 int rl_chroma_table_index;
203 int use_skip_mb_code;
204 int slice_height; /* in macroblocks */
205 int first_slice_line;
206 int flipflop_rounding;
208 /* decompression specific */
211 /* MPEG2 specific - I wish I had not to support this mess. */
212 int progressive_sequence;
213 int mpeg_f_code[2][2];
214 int picture_structure;
216 #define PICT_TOP_FIELD 1
217 #define PICT_BOTTOM_FIELD 2
220 int intra_dc_precision;
221 int frame_pred_frame_dct;
223 int concealment_motion_vectors;
225 int intra_vlc_format;
227 int repeat_first_field;
229 int progressive_frame;
237 /* These are explained on avcodec.h */
239 int rtp_payload_size;
240 void (*rtp_callback)(void *data, int size, int packet_number);
242 UINT8 *ptr_last_mb_line;
243 UINT32 mb_line_avgsize;
245 DCTELEM block[6][64] __align8;
246 void (*dct_unquantize)(struct MpegEncContext *s,
247 DCTELEM *block, int n, int qscale);
250 int MPV_common_init(MpegEncContext *s);
251 void MPV_common_end(MpegEncContext *s);
252 void MPV_decode_mb(MpegEncContext *s, DCTELEM block[6][64]);
253 void MPV_frame_start(MpegEncContext *s);
254 void MPV_frame_end(MpegEncContext *s);
256 void MPV_common_init_mmx(MpegEncContext *s);
261 int estimate_motion(MpegEncContext *s,
263 int *mx_ptr, int *my_ptr);
266 extern INT16 default_intra_matrix[64];
267 extern INT16 default_non_intra_matrix[64];
269 void mpeg1_encode_picture_header(MpegEncContext *s, int picture_number);
270 void mpeg1_encode_mb(MpegEncContext *s,
271 DCTELEM block[6][64],
272 int motion_x, int motion_y);
276 /* run length table */
280 typedef struct RLTable {
281 int n; /* number of entries of table_vlc minus 1 */
282 int last; /* number of values for last = 0 */
283 const UINT16 (*table_vlc)[2];
284 const INT8 *table_run;
285 const INT8 *table_level;
286 UINT8 *index_run[2]; /* encoding only */
287 INT8 *max_level[2]; /* encoding & decoding */
288 INT8 *max_run[2]; /* encoding & decoding */
289 VLC vlc; /* decoding only */
292 void init_rl(RLTable *rl);
293 void init_vlc_rl(RLTable *rl);
295 static inline int get_rl_index(const RLTable *rl, int last, int run, int level)
298 index = rl->index_run[last][run];
301 if (level > rl->max_level[last][run])
303 return index + level - 1;
306 void h263_encode_mb(MpegEncContext *s,
307 DCTELEM block[6][64],
308 int motion_x, int motion_y);
309 void h263_encode_picture_header(MpegEncContext *s, int picture_number);
310 int h263_encode_gob_header(MpegEncContext * s, int mb_line);
311 void h263_dc_scale(MpegEncContext *s);
312 INT16 *h263_pred_motion(MpegEncContext * s, int block,
314 void mpeg4_pred_ac(MpegEncContext * s, INT16 *block, int n,
316 void mpeg4_encode_picture_header(MpegEncContext *s, int picture_number);
317 void h263_encode_init_vlc(MpegEncContext *s);
319 void h263_decode_init_vlc(MpegEncContext *s);
320 int h263_decode_picture_header(MpegEncContext *s);
321 int h263_decode_gob_header(MpegEncContext *s);
322 int mpeg4_decode_picture_header(MpegEncContext * s);
323 int intel_h263_decode_picture_header(MpegEncContext *s);
324 int h263_decode_mb(MpegEncContext *s,
325 DCTELEM block[6][64]);
326 int h263_get_picture_format(int width, int height);
329 void rv10_encode_picture_header(MpegEncContext *s, int picture_number);
330 int rv_decode_dc(MpegEncContext *s, int n);
333 void msmpeg4_encode_picture_header(MpegEncContext * s, int picture_number);
334 void msmpeg4_encode_ext_header(MpegEncContext * s);
335 void msmpeg4_encode_mb(MpegEncContext * s,
336 DCTELEM block[6][64],
337 int motion_x, int motion_y);
338 void msmpeg4_dc_scale(MpegEncContext * s);
339 int msmpeg4_decode_picture_header(MpegEncContext * s);
340 int msmpeg4_decode_ext_header(MpegEncContext * s, int buf_size);
341 int msmpeg4_decode_mb(MpegEncContext *s,
342 DCTELEM block[6][64]);
343 int msmpeg4_decode_init_vlc(MpegEncContext *s);
347 int mjpeg_init(MpegEncContext *s);
348 void mjpeg_close(MpegEncContext *s);
349 void mjpeg_encode_mb(MpegEncContext *s,
350 DCTELEM block[6][64]);
351 void mjpeg_picture_header(MpegEncContext *s);
352 void mjpeg_picture_trailer(MpegEncContext *s);