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
40 typedef struct Predictor{
46 typedef struct MpegEncContext {
47 struct AVCodecContext *avctx;
48 /* the following parameters must be initialized before encoding */
49 int width, height; /* picture size. must be a multiple of 16 */
51 int frame_rate; /* number of frames per second */
52 int intra_only; /* if true, only intra pictures are generated */
53 int bit_rate; /* wanted bit rate */
54 int bit_rate_tolerance; /* amount of +- bits (>0)*/
55 enum OutputFormat out_format; /* output format */
56 int h263_plus; /* h263 plus headers */
57 int h263_rv10; /* use RV10 variation for H263 */
58 int h263_pred; /* use mpeg4/h263 ac/dc predictions */
59 int h263_msmpeg4; /* generate MSMPEG4 compatible stream */
60 int h263_intel; /* use I263 intel h263 header */
61 int fixed_qscale; /* fixed qscale if non zero */
62 float qcompress; /* amount of qscale change between easy & hard scenes (0.0-1.0) */
63 float qblur; /* amount of qscale smoothing over time (0.0-1.0) */
64 int qmin; /* min qscale */
65 int qmax; /* max qscale */
66 int max_qdiff; /* max qscale difference between frames */
67 int encoding; /* true if we are encoding (vs decoding) */
68 int hq; /* set if CODEC_FLAG_HQ is used in AVCodecContext.flags */
69 /* the following fields are managed internally by the encoder */
74 /* sequence parameters */
75 int context_initialized;
77 int fake_picture_number; /* picture number at the bitstream frame rate */
78 int gop_picture_number; /* index of the first picture of a GOP based on fake_pic_num & mpeg1 specific */
79 int picture_in_gop_number; /* 0-> first pic in gop, ... */
80 int mb_width, mb_height;
81 int mb_num; /* number of MBs of a picture */
82 int linesize; /* line size, in bytes, may be different from width */
83 UINT8 *new_picture[3]; /* picture to be compressed */
84 UINT8 *last_picture[3]; /* previous picture */
85 UINT8 *last_picture_base[3]; /* real start of the picture */
86 UINT8 *next_picture[3]; /* previous picture (for bidir pred) */
87 UINT8 *next_picture_base[3]; /* real start of the picture */
88 UINT8 *aux_picture[3]; /* aux picture (for B frames only) */
89 UINT8 *aux_picture_base[3]; /* real start of the picture */
90 UINT8 *current_picture[3]; /* buffer to store the decompressed current picture */
91 int last_dc[3]; /* last DC values for MPEG1 */
92 INT16 *dc_val[3]; /* used for mpeg4 DC prediction, all 3 arrays must be continuous */
93 int y_dc_scale, c_dc_scale;
94 UINT8 *coded_block; /* used for coded block pattern prediction */
95 INT16 (*ac_val[3])[16]; /* used for for mpeg4 AC prediction, all 3 arrays must be continuous */
97 int mb_skiped; /* MUST BE SET only during DECODING */
98 UINT8 *mbskip_table; /* used to avoid copy if macroblock
99 skipped (for black regions for example) */
100 UINT8 *mbintra_table; /* used to kill a few memsets */
104 int last_non_b_pict_type; /* used for mpeg4 gmc b-frames */
105 int last_pict_type; /* used for bit rate stuff (needs that to update the right predictor) */
106 int frame_rate_index;
107 /* motion compensation */
109 int h263_long_vectors; /* use horrible h263v1 long vector mode */
111 int f_code; /* resolution */
112 int b_code; /* backward resolution for B Frames (mpeg4) */
113 INT16 *mv_table[2]; /* MV table (1MV per MB)*/
114 INT16 (*motion_val)[2]; /* used for MV prediction (4MV per MB)*/
117 #define MV_DIR_BACKWARD 1
118 #define MV_DIR_FORWARD 2
119 #define MV_DIRECT 4 // bidirectional mode where the difference equals the MV of the last P/S/I-Frame (mpeg4)
121 #define MV_TYPE_16X16 0 /* 1 vector for the whole mb */
122 #define MV_TYPE_8X8 1 /* 4 vectors (h263, mpeg4 4MV) */
123 #define MV_TYPE_16X8 2 /* 2 vectors, one per 16x8 block */
124 #define MV_TYPE_FIELD 3 /* 2 vectors, one per field */
125 #define MV_TYPE_DMV 4 /* 2 vectors, special mpeg2 Dual Prime Vectors */
126 /* motion vectors for a macroblock
127 first coordinate : 0 = forward 1 = backward
128 second " : depend on type
129 third " : 0 = x, 1 = y
132 int field_select[2][2];
133 int last_mv[2][2][2];
134 UINT16 (*mv_penalty)[MAX_MV*2+1]; /* amount of bits needed to encode a MV, used for ME */
135 UINT8 *fcode_tab; /* smallest fcode needed for each MV */
138 int no_rounding; /* apply no rounding to motion estimation (MPEG4) */
140 /* macroblock layer */
144 INT16 *mb_var; /* Table for MB variances */
145 char *mb_type; /* Table for MB type */
150 /* matrix transmitted in the bitstream */
151 UINT16 intra_matrix[64];
152 UINT16 chroma_intra_matrix[64];
153 UINT16 non_intra_matrix[64];
154 UINT16 chroma_non_intra_matrix[64];
155 /* precomputed matrix (combine qscale and DCT renorm) */
156 int q_intra_matrix[64];
157 int q_non_intra_matrix[64];
158 /* identical to the above but for MMX & these are not permutated */
159 UINT16 __align8 q_intra_matrix16[64] ;
160 UINT16 __align8 q_non_intra_matrix16[64];
161 int block_last_index[6]; /* last non zero coefficient in block */
163 void *opaque; /* private data for the user */
165 /* bit rate control */
166 int I_frame_bits; /* wanted number of bits per I frame */
167 int P_frame_bits; /* same for P frame */
168 int avg_mb_var; /* average MB variance for current frame */
169 int mc_mb_var; /* motion compensated MB variance for current frame */
170 int last_mc_mb_var; /* motion compensated MB variance for last frame */
173 int frame_bits; /* bits used for the current frame */
174 int last_frame_bits; /* bits used for the last frame */
177 double qsum; /* sum of qscales */
178 double qcount; /* count of qscales */
179 double short_term_qsum; /* sum of recent qscales */
180 double short_term_qcount; /* count of recent qscales */
182 /* statistics, used for 2-pass encoding */
190 int misc_bits; // cbp, mb_type
191 int last_bits; //temp var used for calculating the above vars
198 /* H.263+ specific */
201 int h263_aic; /* Advanded INTRA Coding (AIC) */
202 int h263_aic_dir; /* AIC direction: 0 = left, 1 = top */
205 int time_increment_resolution;
206 int time_increment_bits;
210 int last_non_b_time[2];
212 int vol_sprite_usage;
217 int sprite_brightness_change;
218 int num_sprite_warping_points;
219 int real_sprite_warping_points;
220 int sprite_offset[2][2];
221 int sprite_delta[2][2][2];
222 int sprite_shift[2][2];
229 int aspect_ratio_info;
230 int sprite_warping_accuracy;
231 int low_latency_sprite;
236 /* divx specific, used to workaround (many) bugs in divx5 */
241 int rv10_version; /* RV10 version: 0 or 3 */
242 int rv10_first_dc_coded[3];
245 struct MJpegContext *mjpeg_ctx;
246 int mjpeg_vsample[3]; /* vertical sampling factors, default = {2, 1, 1} */
247 int mjpeg_hsample[3]; /* horizontal sampling factors, default = {2, 1, 1} */
248 int mjpeg_write_tables; /* do we want to have quantisation- and
249 huffmantables in the jpeg file ? */
251 /* MSMPEG4 specific */
254 int rl_chroma_table_index;
256 int use_skip_mb_code;
257 int slice_height; /* in macroblocks */
258 int first_slice_line; /* used in mpeg4 too to handle resync markers */
259 int flipflop_rounding;
261 /* decompression specific */
264 /* MPEG2 specific - I wish I had not to support this mess. */
265 int progressive_sequence;
266 int mpeg_f_code[2][2];
267 int picture_structure;
269 #define PICT_TOP_FIELD 1
270 #define PICT_BOTTOM_FIELD 2
273 int intra_dc_precision;
274 int frame_pred_frame_dct;
276 int concealment_motion_vectors;
278 int intra_vlc_format;
280 int repeat_first_field;
282 int progressive_frame;
290 /* These are explained on avcodec.h */
292 int rtp_payload_size;
293 void (*rtp_callback)(void *data, int size, int packet_number);
295 UINT8 *ptr_last_mb_line;
296 UINT32 mb_line_avgsize;
298 DCTELEM block[6][64] __align8;
299 void (*dct_unquantize)(struct MpegEncContext *s,
300 DCTELEM *block, int n, int qscale);
303 int MPV_common_init(MpegEncContext *s);
304 void MPV_common_end(MpegEncContext *s);
305 void MPV_decode_mb(MpegEncContext *s, DCTELEM block[6][64]);
306 void MPV_frame_start(MpegEncContext *s);
307 void MPV_frame_end(MpegEncContext *s);
309 void MPV_common_init_mmx(MpegEncContext *s);
314 int estimate_motion(MpegEncContext *s,
316 int *mx_ptr, int *my_ptr);
319 extern INT16 default_intra_matrix[64];
320 extern INT16 default_non_intra_matrix[64];
322 void mpeg1_encode_picture_header(MpegEncContext *s, int picture_number);
323 void mpeg1_encode_mb(MpegEncContext *s,
324 DCTELEM block[6][64],
325 int motion_x, int motion_y);
326 void mpeg1_encode_init(MpegEncContext *s);
330 /* run length table */
334 typedef struct RLTable {
335 int n; /* number of entries of table_vlc minus 1 */
336 int last; /* number of values for last = 0 */
337 const UINT16 (*table_vlc)[2];
338 const INT8 *table_run;
339 const INT8 *table_level;
340 UINT8 *index_run[2]; /* encoding only */
341 INT8 *max_level[2]; /* encoding & decoding */
342 INT8 *max_run[2]; /* encoding & decoding */
343 VLC vlc; /* decoding only */
346 void init_rl(RLTable *rl);
347 void init_vlc_rl(RLTable *rl);
349 static inline int get_rl_index(const RLTable *rl, int last, int run, int level)
352 index = rl->index_run[last][run];
355 if (level > rl->max_level[last][run])
357 return index + level - 1;
360 void h263_encode_mb(MpegEncContext *s,
361 DCTELEM block[6][64],
362 int motion_x, int motion_y);
363 void mpeg4_encode_mb(MpegEncContext *s,
364 DCTELEM block[6][64],
365 int motion_x, int motion_y);
366 void h263_encode_picture_header(MpegEncContext *s, int picture_number);
367 int h263_encode_gob_header(MpegEncContext * s, int mb_line);
368 void h263_dc_scale(MpegEncContext *s);
369 INT16 *h263_pred_motion(MpegEncContext * s, int block,
371 void mpeg4_pred_ac(MpegEncContext * s, INT16 *block, int n,
373 void mpeg4_encode_picture_header(MpegEncContext *s, int picture_number);
374 void h263_encode_init(MpegEncContext *s);
376 void h263_decode_init_vlc(MpegEncContext *s);
377 int h263_decode_picture_header(MpegEncContext *s);
378 int h263_decode_gob_header(MpegEncContext *s);
379 int mpeg4_decode_picture_header(MpegEncContext * s);
380 int intel_h263_decode_picture_header(MpegEncContext *s);
381 int h263_decode_mb(MpegEncContext *s,
382 DCTELEM block[6][64]);
383 int h263_get_picture_format(int width, int height);
386 void rv10_encode_picture_header(MpegEncContext *s, int picture_number);
387 int rv_decode_dc(MpegEncContext *s, int n);
390 void msmpeg4_encode_picture_header(MpegEncContext * s, int picture_number);
391 void msmpeg4_encode_ext_header(MpegEncContext * s);
392 void msmpeg4_encode_mb(MpegEncContext * s,
393 DCTELEM block[6][64],
394 int motion_x, int motion_y);
395 void msmpeg4_dc_scale(MpegEncContext * s);
396 int msmpeg4_decode_picture_header(MpegEncContext * s);
397 int msmpeg4_decode_ext_header(MpegEncContext * s, int buf_size);
398 int msmpeg4_decode_mb(MpegEncContext *s,
399 DCTELEM block[6][64]);
400 int msmpeg4_decode_init_vlc(MpegEncContext *s);
404 int mjpeg_init(MpegEncContext *s);
405 void mjpeg_close(MpegEncContext *s);
406 void mjpeg_encode_mb(MpegEncContext *s,
407 DCTELEM block[6][64]);
408 void mjpeg_picture_header(MpegEncContext *s);
409 void mjpeg_picture_trailer(MpegEncContext *s);