* solaris does not support -q
[libav.git] / libavcodec / mpegvideo.h
CommitLineData
de6d9b64
FB
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
cc9ba006 24#define S_TYPE 4 //S(GMC)-VOP MPEG4
de6d9b64
FB
25
26enum OutputFormat {
27 FMT_MPEG1,
28 FMT_H263,
29 FMT_MJPEG,
30};
31
32#define MPEG_BUF_SIZE (16 * 1024)
33
2f349de2
MN
34#define QMAT_SHIFT_MMX 19
35#define QMAT_SHIFT 25
36
45870f57
MN
37#define MAX_FCODE 7
38#define MAX_MV 2048
39
9cdd6a24
MN
40typedef struct Predictor{
41 double coeff;
42 double count;
43 double decay;
44} Predictor;
45
de6d9b64 46typedef struct MpegEncContext {
58f26ba9 47 struct AVCodecContext *avctx;
de6d9b64
FB
48 /* the following parameters must be initialized before encoding */
49 int width, height; /* picture size. must be a multiple of 16 */
50 int gop_size;
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 */
9cdd6a24 54 int bit_rate_tolerance; /* amount of +- bits (>0)*/
de6d9b64
FB
55 enum OutputFormat out_format; /* output format */
56 int h263_plus; /* h263 plus headers */
57 int h263_rv10; /* use RV10 variation for H263 */
58f26ba9 58 int h263_pred; /* use mpeg4/h263 ac/dc predictions */
de6d9b64
FB
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 */
9cdd6a24
MN
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 */
3bb4e23a 67 int encoding; /* true if we are encoding (vs decoding) */
ba6802de 68 int flags; /* AVCodecContext.flags (HQ, MV4, ...) */
4d69fbc9 69 int force_type; /* 0= no force, otherwise I_TYPE, P_TYPE, ... */
de6d9b64
FB
70 /* the following fields are managed internally by the encoder */
71
72 /* bit output */
73 PutBitContext pb;
74
75 /* sequence parameters */
76 int context_initialized;
77 int picture_number;
78 int fake_picture_number; /* picture number at the bitstream frame rate */
45870f57
MN
79 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, ... */
de6d9b64 81 int mb_width, mb_height;
e03c341e 82 int mb_num; /* number of MBs of a picture */
de6d9b64
FB
83 int linesize; /* line size, in bytes, may be different from width */
84 UINT8 *new_picture[3]; /* picture to be compressed */
85 UINT8 *last_picture[3]; /* previous picture */
86 UINT8 *last_picture_base[3]; /* real start of the picture */
87 UINT8 *next_picture[3]; /* previous picture (for bidir pred) */
88 UINT8 *next_picture_base[3]; /* real start of the picture */
89 UINT8 *aux_picture[3]; /* aux picture (for B frames only) */
90 UINT8 *aux_picture_base[3]; /* real start of the picture */
91 UINT8 *current_picture[3]; /* buffer to store the decompressed current picture */
92 int last_dc[3]; /* last DC values for MPEG1 */
4278e7a6 93 INT16 *dc_val[3]; /* used for mpeg4 DC prediction, all 3 arrays must be continuous */
de6d9b64
FB
94 int y_dc_scale, c_dc_scale;
95 UINT8 *coded_block; /* used for coded block pattern prediction */
4278e7a6 96 INT16 (*ac_val[3])[16]; /* used for for mpeg4 AC prediction, all 3 arrays must be continuous */
de6d9b64 97 int ac_pred;
3bb4e23a
FB
98 int mb_skiped; /* MUST BE SET only during DECODING */
99 UINT8 *mbskip_table; /* used to avoid copy if macroblock
100 skipped (for black regions for example) */
bff6ecaa 101 UINT8 *mbintra_table; /* used to kill a few memsets */
de6d9b64
FB
102
103 int qscale;
104 int pict_type;
6f91bcd1 105 int last_non_b_pict_type; /* used for mpeg4 gmc b-frames */
9cdd6a24 106 int last_pict_type; /* used for bit rate stuff (needs that to update the right predictor) */
de6d9b64
FB
107 int frame_rate_index;
108 /* motion compensation */
109 int unrestricted_mv;
110 int h263_long_vectors; /* use horrible h263v1 long vector mode */
111
112 int f_code; /* resolution */
6f91bcd1
MN
113 int b_code; /* backward resolution for B Frames (mpeg4) */
114 INT16 *mv_table[2]; /* MV table (1MV per MB)*/
115 INT16 (*motion_val)[2]; /* used for MV prediction (4MV per MB)*/
e4986da9 116 int me_method; /* ME algorithm */
de6d9b64
FB
117 int mv_dir;
118#define MV_DIR_BACKWARD 1
119#define MV_DIR_FORWARD 2
6f91bcd1 120#define MV_DIRECT 4 // bidirectional mode where the difference equals the MV of the last P/S/I-Frame (mpeg4)
de6d9b64
FB
121 int mv_type;
122#define MV_TYPE_16X16 0 /* 1 vector for the whole mb */
45870f57 123#define MV_TYPE_8X8 1 /* 4 vectors (h263, mpeg4 4MV) */
de6d9b64
FB
124#define MV_TYPE_16X8 2 /* 2 vectors, one per 16x8 block */
125#define MV_TYPE_FIELD 3 /* 2 vectors, one per field */
126#define MV_TYPE_DMV 4 /* 2 vectors, special mpeg2 Dual Prime Vectors */
127 /* motion vectors for a macroblock
128 first coordinate : 0 = forward 1 = backward
129 second " : depend on type
130 third " : 0 = x, 1 = y
131 */
132 int mv[2][4][2];
133 int field_select[2][2];
134 int last_mv[2][2][2];
45870f57
MN
135 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 */
de6d9b64
FB
137
138 int has_b_frames;
139 int no_rounding; /* apply no rounding to motion estimation (MPEG4) */
140
141 /* macroblock layer */
142 int mb_x, mb_y;
143 int mb_incr;
144 int mb_intra;
ba6802de
MN
145 UINT16 *mb_var; /* Table for MB variances */
146 UINT8 *mb_type; /* Table for MB type */
147#define MB_TYPE_INTRA 0x01
148#define MB_TYPE_INTER 0x02
149#define MB_TYPE_INTER4V 0x04
150#define MB_TYPE_SKIPED 0x08
151#define MB_TYPE_DIRECT 0x10
152#define MB_TYPE_FORWARD 0x20
153#define MB_TYPE_BACKWAD 0x40
154#define MB_TYPE_BIDIR 0x80
4278e7a6
MN
155
156 int block_index[6];
157 int block_wrap[6];
158
de6d9b64
FB
159 /* matrix transmitted in the bitstream */
160 UINT16 intra_matrix[64];
161 UINT16 chroma_intra_matrix[64];
162 UINT16 non_intra_matrix[64];
163 UINT16 chroma_non_intra_matrix[64];
164 /* precomputed matrix (combine qscale and DCT renorm) */
165 int q_intra_matrix[64];
166 int q_non_intra_matrix[64];
2f349de2
MN
167 /* identical to the above but for MMX & these are not permutated */
168 UINT16 __align8 q_intra_matrix16[64] ;
169 UINT16 __align8 q_non_intra_matrix16[64];
de6d9b64
FB
170 int block_last_index[6]; /* last non zero coefficient in block */
171
172 void *opaque; /* private data for the user */
173
174 /* bit rate control */
175 int I_frame_bits; /* wanted number of bits per I frame */
176 int P_frame_bits; /* same for P frame */
e03c341e 177 int avg_mb_var; /* average MB variance for current frame */
9cdd6a24
MN
178 int mc_mb_var; /* motion compensated MB variance for current frame */
179 int last_mc_mb_var; /* motion compensated MB variance for last frame */
1a565432
FB
180 INT64 wanted_bits;
181 INT64 total_bits;
9cdd6a24
MN
182 int frame_bits; /* bits used for the current frame */
183 int last_frame_bits; /* bits used for the last frame */
184 Predictor i_pred;
185 Predictor p_pred;
186 double qsum; /* sum of qscales */
187 double qcount; /* count of qscales */
188 double short_term_qsum; /* sum of recent qscales */
189 double short_term_qcount; /* count of recent qscales */
190
098eefe1
MN
191 /* statistics, used for 2-pass encoding */
192 int mv_bits;
193 int header_bits;
194 int i_tex_bits;
195 int p_tex_bits;
196 int i_count;
197 int p_count;
198 int skip_count;
199 int misc_bits; // cbp, mb_type
200 int last_bits; //temp var used for calculating the above vars
201
102d3908
J
202 /* H.263 specific */
203 int gob_number;
644d98a4 204 int gob_index;
102d3908 205 int first_gob_line;
d140623f 206
544286b3
J
207 /* H.263+ specific */
208 int umvplus;
209 int umvplus_dec;
d140623f
J
210 int h263_aic; /* Advanded INTRA Coding (AIC) */
211 int h263_aic_dir; /* AIC direction: 0 = left, 1 = top */
544286b3 212
de6d9b64 213 /* mpeg4 specific */
6f91bcd1 214 int time_increment_resolution;
de6d9b64 215 int time_increment_bits;
6f91bcd1
MN
216 int time_increment;
217 int time_base;
218 int time;
219 int last_non_b_time[2];
1a565432
FB
220 int shape;
221 int vol_sprite_usage;
cc9ba006
MN
222 int sprite_width;
223 int sprite_height;
224 int sprite_left;
225 int sprite_top;
226 int sprite_brightness_change;
73c8e514 227 int num_sprite_warping_points;
44eb4951 228 int real_sprite_warping_points;
73c8e514
MN
229 int sprite_offset[2][2];
230 int sprite_delta[2][2][2];
231 int sprite_shift[2][2];
232 int mcsel;
1a565432 233 int quant_precision;
cc9ba006
MN
234 int quarter_sample;
235 int scalability;
236 int new_pred;
237 int reduced_res_vop;
238 int aspect_ratio_info;
239 int sprite_warping_accuracy;
240 int low_latency_sprite;
241 int data_partioning;
49092244
MN
242 int resync_marker;
243 int resync_x_pos;
44eb4951
MN
244
245 /* divx specific, used to workaround (many) bugs in divx5 */
246 int divx_version;
247 int divx_build;
248
de6d9b64
FB
249 /* RV10 specific */
250 int rv10_version; /* RV10 version: 0 or 3 */
251 int rv10_first_dc_coded[3];
252
253 /* MJPEG specific */
254 struct MJpegContext *mjpeg_ctx;
80e103d0
A
255 int mjpeg_vsample[3]; /* vertical sampling factors, default = {2, 1, 1} */
256 int mjpeg_hsample[3]; /* horizontal sampling factors, default = {2, 1, 1} */
257 int mjpeg_write_tables; /* do we want to have quantisation- and
258 huffmantables in the jpeg file ? */
de6d9b64
FB
259
260 /* MSMPEG4 specific */
261 int mv_table_index;
262 int rl_table_index;
263 int rl_chroma_table_index;
264 int dc_table_index;
265 int use_skip_mb_code;
266 int slice_height; /* in macroblocks */
49092244 267 int first_slice_line; /* used in mpeg4 too to handle resync markers */
ae40484c
MN
268 int flipflop_rounding;
269 int bitrate;
84afee34 270 int msmpeg4_version; /* 1=mp41, 2=mp42, 3=mp43/divx3 */
de6d9b64
FB
271 /* decompression specific */
272 GetBitContext gb;
273
274 /* MPEG2 specific - I wish I had not to support this mess. */
275 int progressive_sequence;
276 int mpeg_f_code[2][2];
277 int picture_structure;
278/* picture type */
279#define PICT_TOP_FIELD 1
280#define PICT_BOTTOM_FIELD 2
281#define PICT_FRAME 3
282
283 int intra_dc_precision;
284 int frame_pred_frame_dct;
285 int top_field_first;
286 int concealment_motion_vectors;
287 int q_scale_type;
288 int intra_vlc_format;
289 int alternate_scan;
290 int repeat_first_field;
291 int chroma_420_type;
292 int progressive_frame;
293 int mpeg2;
294 int full_pel[2];
295 int interlaced_dct;
296 int last_qscale;
297 int first_slice;
644d98a4
J
298
299 /* RTP specific */
81401c1f 300 /* These are explained on avcodec.h */
644d98a4
J
301 int rtp_mode;
302 int rtp_payload_size;
81401c1f 303 void (*rtp_callback)(void *data, int size, int packet_number);
644d98a4
J
304 UINT8 *ptr_lastgob;
305 UINT8 *ptr_last_mb_line;
306 UINT32 mb_line_avgsize;
307
ba6802de
MN
308 DCTELEM (*block)[64]; /* points to one of the following blocks */
309 DCTELEM intra_block[6][64] __align8;
310 DCTELEM inter_block[6][64] __align8;
311 DCTELEM inter4v_block[6][64] __align8;
3bf43d42
MN
312 void (*dct_unquantize_mpeg)(struct MpegEncContext *s,
313 DCTELEM *block, int n, int qscale);
314 void (*dct_unquantize_h263)(struct MpegEncContext *s,
315 DCTELEM *block, int n, int qscale);
316 void (*dct_unquantize)(struct MpegEncContext *s, // unquantizer to use (mpeg4 can use both)
21af69f7 317 DCTELEM *block, int n, int qscale);
de6d9b64
FB
318} MpegEncContext;
319
de6d9b64
FB
320int MPV_common_init(MpegEncContext *s);
321void MPV_common_end(MpegEncContext *s);
322void MPV_decode_mb(MpegEncContext *s, DCTELEM block[6][64]);
323void MPV_frame_start(MpegEncContext *s);
324void MPV_frame_end(MpegEncContext *s);
21af69f7
FB
325#ifdef HAVE_MMX
326void MPV_common_init_mmx(MpegEncContext *s);
327#endif
de6d9b64
FB
328
329/* motion_est.c */
330
ba6802de
MN
331void estimate_motion(MpegEncContext *s,
332 int mb_x, int mb_y);
de6d9b64
FB
333
334/* mpeg12.c */
3c054a1c
FB
335extern INT16 default_intra_matrix[64];
336extern INT16 default_non_intra_matrix[64];
de6d9b64
FB
337
338void mpeg1_encode_picture_header(MpegEncContext *s, int picture_number);
339void mpeg1_encode_mb(MpegEncContext *s,
340 DCTELEM block[6][64],
341 int motion_x, int motion_y);
11ce8834 342void mpeg1_encode_init(MpegEncContext *s);
de6d9b64
FB
343
344/* h263enc.c */
345
346/* run length table */
347#define MAX_RUN 64
348#define MAX_LEVEL 64
349
350typedef struct RLTable {
351 int n; /* number of entries of table_vlc minus 1 */
352 int last; /* number of values for last = 0 */
353 const UINT16 (*table_vlc)[2];
354 const INT8 *table_run;
355 const INT8 *table_level;
356 UINT8 *index_run[2]; /* encoding only */
357 INT8 *max_level[2]; /* encoding & decoding */
358 INT8 *max_run[2]; /* encoding & decoding */
359 VLC vlc; /* decoding only */
360} RLTable;
361
362void init_rl(RLTable *rl);
363void init_vlc_rl(RLTable *rl);
364
3232bc88 365static inline int get_rl_index(const RLTable *rl, int last, int run, int level)
de6d9b64
FB
366{
367 int index;
368 index = rl->index_run[last][run];
369 if (index >= rl->n)
370 return rl->n;
371 if (level > rl->max_level[last][run])
372 return rl->n;
373 return index + level - 1;
374}
375
376void h263_encode_mb(MpegEncContext *s,
377 DCTELEM block[6][64],
378 int motion_x, int motion_y);
d6231b9e
MN
379void mpeg4_encode_mb(MpegEncContext *s,
380 DCTELEM block[6][64],
381 int motion_x, int motion_y);
de6d9b64 382void h263_encode_picture_header(MpegEncContext *s, int picture_number);
644d98a4 383int h263_encode_gob_header(MpegEncContext * s, int mb_line);
de6d9b64
FB
384void h263_dc_scale(MpegEncContext *s);
385INT16 *h263_pred_motion(MpegEncContext * s, int block,
386 int *px, int *py);
387void mpeg4_pred_ac(MpegEncContext * s, INT16 *block, int n,
388 int dir);
389void mpeg4_encode_picture_header(MpegEncContext *s, int picture_number);
45870f57 390void h263_encode_init(MpegEncContext *s);
de6d9b64
FB
391
392void h263_decode_init_vlc(MpegEncContext *s);
393int h263_decode_picture_header(MpegEncContext *s);
644d98a4 394int h263_decode_gob_header(MpegEncContext *s);
de6d9b64
FB
395int mpeg4_decode_picture_header(MpegEncContext * s);
396int intel_h263_decode_picture_header(MpegEncContext *s);
397int h263_decode_mb(MpegEncContext *s,
398 DCTELEM block[6][64]);
399int h263_get_picture_format(int width, int height);
de6d9b64
FB
400
401/* rv10.c */
402void rv10_encode_picture_header(MpegEncContext *s, int picture_number);
403int rv_decode_dc(MpegEncContext *s, int n);
404
405/* msmpeg4.c */
406void msmpeg4_encode_picture_header(MpegEncContext * s, int picture_number);
ae40484c 407void msmpeg4_encode_ext_header(MpegEncContext * s);
de6d9b64
FB
408void msmpeg4_encode_mb(MpegEncContext * s,
409 DCTELEM block[6][64],
410 int motion_x, int motion_y);
411void msmpeg4_dc_scale(MpegEncContext * s);
412int msmpeg4_decode_picture_header(MpegEncContext * s);
ae40484c 413int msmpeg4_decode_ext_header(MpegEncContext * s, int buf_size);
de6d9b64
FB
414int msmpeg4_decode_mb(MpegEncContext *s,
415 DCTELEM block[6][64]);
416int msmpeg4_decode_init_vlc(MpegEncContext *s);
417
418/* mjpegenc.c */
419
420int mjpeg_init(MpegEncContext *s);
421void mjpeg_close(MpegEncContext *s);
422void mjpeg_encode_mb(MpegEncContext *s,
423 DCTELEM block[6][64]);
424void mjpeg_picture_header(MpegEncContext *s);
425void mjpeg_picture_trailer(MpegEncContext *s);