* Also include time.h for nanosleep()
[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
24
25enum OutputFormat {
26 FMT_MPEG1,
27 FMT_H263,
28 FMT_MJPEG,
29};
30
31#define MPEG_BUF_SIZE (16 * 1024)
32
2f349de2
MN
33#define QMAT_SHIFT_MMX 19
34#define QMAT_SHIFT 25
35
de6d9b64 36typedef struct MpegEncContext {
58f26ba9 37 struct AVCodecContext *avctx;
de6d9b64
FB
38 /* the following parameters must be initialized before encoding */
39 int width, height; /* picture size. must be a multiple of 16 */
40 int gop_size;
41 int frame_rate; /* number of frames per second */
42 int intra_only; /* if true, only intra pictures are generated */
43 int bit_rate; /* wanted bit rate */
44 enum OutputFormat out_format; /* output format */
45 int h263_plus; /* h263 plus headers */
46 int h263_rv10; /* use RV10 variation for H263 */
58f26ba9 47 int h263_pred; /* use mpeg4/h263 ac/dc predictions */
de6d9b64
FB
48 int h263_msmpeg4; /* generate MSMPEG4 compatible stream */
49 int h263_intel; /* use I263 intel h263 header */
50 int fixed_qscale; /* fixed qscale if non zero */
3bb4e23a 51 int encoding; /* true if we are encoding (vs decoding) */
de6d9b64
FB
52 /* the following fields are managed internally by the encoder */
53
54 /* bit output */
55 PutBitContext pb;
56
57 /* sequence parameters */
58 int context_initialized;
59 int picture_number;
60 int fake_picture_number; /* picture number at the bitstream frame rate */
e03c341e 61 int gop_picture_number; /* index of the first picture of a GOP */
de6d9b64 62 int mb_width, mb_height;
e03c341e 63 int mb_num; /* number of MBs of a picture */
de6d9b64
FB
64 int linesize; /* line size, in bytes, may be different from width */
65 UINT8 *new_picture[3]; /* picture to be compressed */
66 UINT8 *last_picture[3]; /* previous picture */
67 UINT8 *last_picture_base[3]; /* real start of the picture */
68 UINT8 *next_picture[3]; /* previous picture (for bidir pred) */
69 UINT8 *next_picture_base[3]; /* real start of the picture */
70 UINT8 *aux_picture[3]; /* aux picture (for B frames only) */
71 UINT8 *aux_picture_base[3]; /* real start of the picture */
72 UINT8 *current_picture[3]; /* buffer to store the decompressed current picture */
73 int last_dc[3]; /* last DC values for MPEG1 */
74 INT16 *dc_val[3]; /* used for mpeg4 DC prediction */
75 int y_dc_scale, c_dc_scale;
76 UINT8 *coded_block; /* used for coded block pattern prediction */
77 INT16 (*ac_val[3])[16]; /* used for for mpeg4 AC prediction */
78 int ac_pred;
3bb4e23a
FB
79 int mb_skiped; /* MUST BE SET only during DECODING */
80 UINT8 *mbskip_table; /* used to avoid copy if macroblock
81 skipped (for black regions for example) */
bff6ecaa 82 UINT8 *mbintra_table; /* used to kill a few memsets */
de6d9b64
FB
83
84 int qscale;
85 int pict_type;
86 int frame_rate_index;
87 /* motion compensation */
88 int unrestricted_mv;
89 int h263_long_vectors; /* use horrible h263v1 long vector mode */
90
91 int f_code; /* resolution */
37fbfd0a 92 INT16 *mv_table[2]; /* MV table */
de6d9b64
FB
93 INT16 (*motion_val)[2]; /* used for MV prediction */
94 int full_search;
95 int mv_dir;
96#define MV_DIR_BACKWARD 1
97#define MV_DIR_FORWARD 2
98 int mv_type;
99#define MV_TYPE_16X16 0 /* 1 vector for the whole mb */
100#define MV_TYPE_8X8 1 /* 4 vectors (h263) */
101#define MV_TYPE_16X8 2 /* 2 vectors, one per 16x8 block */
102#define MV_TYPE_FIELD 3 /* 2 vectors, one per field */
103#define MV_TYPE_DMV 4 /* 2 vectors, special mpeg2 Dual Prime Vectors */
104 /* motion vectors for a macroblock
105 first coordinate : 0 = forward 1 = backward
106 second " : depend on type
107 third " : 0 = x, 1 = y
108 */
109 int mv[2][4][2];
110 int field_select[2][2];
111 int last_mv[2][2][2];
112
113 int has_b_frames;
114 int no_rounding; /* apply no rounding to motion estimation (MPEG4) */
115
116 /* macroblock layer */
117 int mb_x, mb_y;
118 int mb_incr;
119 int mb_intra;
b4dbd87c 120 INT16 *mb_var; /* Table for MB variances */
37fbfd0a
J
121 char *mb_type; /* Table for MB type */
122
de6d9b64
FB
123 /* matrix transmitted in the bitstream */
124 UINT16 intra_matrix[64];
125 UINT16 chroma_intra_matrix[64];
126 UINT16 non_intra_matrix[64];
127 UINT16 chroma_non_intra_matrix[64];
128 /* precomputed matrix (combine qscale and DCT renorm) */
129 int q_intra_matrix[64];
130 int q_non_intra_matrix[64];
2f349de2
MN
131 /* identical to the above but for MMX & these are not permutated */
132 UINT16 __align8 q_intra_matrix16[64] ;
133 UINT16 __align8 q_non_intra_matrix16[64];
de6d9b64
FB
134 int block_last_index[6]; /* last non zero coefficient in block */
135
136 void *opaque; /* private data for the user */
137
138 /* bit rate control */
139 int I_frame_bits; /* wanted number of bits per I frame */
140 int P_frame_bits; /* same for P frame */
e03c341e 141 int avg_mb_var; /* average MB variance for current frame */
1a565432
FB
142 INT64 wanted_bits;
143 INT64 total_bits;
544286b3 144
102d3908
J
145 /* H.263 specific */
146 int gob_number;
644d98a4 147 int gob_index;
102d3908
J
148 int first_gob_line;
149
544286b3
J
150 /* H.263+ specific */
151 int umvplus;
152 int umvplus_dec;
153
de6d9b64
FB
154 /* mpeg4 specific */
155 int time_increment_bits;
1a565432
FB
156 int shape;
157 int vol_sprite_usage;
158 int quant_precision;
de6d9b64
FB
159
160 /* RV10 specific */
161 int rv10_version; /* RV10 version: 0 or 3 */
162 int rv10_first_dc_coded[3];
163
164 /* MJPEG specific */
165 struct MJpegContext *mjpeg_ctx;
80e103d0
A
166 int mjpeg_vsample[3]; /* vertical sampling factors, default = {2, 1, 1} */
167 int mjpeg_hsample[3]; /* horizontal sampling factors, default = {2, 1, 1} */
168 int mjpeg_write_tables; /* do we want to have quantisation- and
169 huffmantables in the jpeg file ? */
de6d9b64
FB
170
171 /* MSMPEG4 specific */
172 int mv_table_index;
173 int rl_table_index;
174 int rl_chroma_table_index;
175 int dc_table_index;
176 int use_skip_mb_code;
177 int slice_height; /* in macroblocks */
178 int first_slice_line;
ae40484c
MN
179 int flipflop_rounding;
180 int bitrate;
de6d9b64
FB
181 /* decompression specific */
182 GetBitContext gb;
183
184 /* MPEG2 specific - I wish I had not to support this mess. */
185 int progressive_sequence;
186 int mpeg_f_code[2][2];
187 int picture_structure;
188/* picture type */
189#define PICT_TOP_FIELD 1
190#define PICT_BOTTOM_FIELD 2
191#define PICT_FRAME 3
192
193 int intra_dc_precision;
194 int frame_pred_frame_dct;
195 int top_field_first;
196 int concealment_motion_vectors;
197 int q_scale_type;
198 int intra_vlc_format;
199 int alternate_scan;
200 int repeat_first_field;
201 int chroma_420_type;
202 int progressive_frame;
203 int mpeg2;
204 int full_pel[2];
205 int interlaced_dct;
206 int last_qscale;
207 int first_slice;
644d98a4
J
208
209 /* RTP specific */
81401c1f 210 /* These are explained on avcodec.h */
644d98a4
J
211 int rtp_mode;
212 int rtp_payload_size;
81401c1f 213 void (*rtp_callback)(void *data, int size, int packet_number);
644d98a4
J
214 UINT8 *ptr_lastgob;
215 UINT8 *ptr_last_mb_line;
216 UINT32 mb_line_avgsize;
217
21af69f7
FB
218 DCTELEM block[6][64] __align8;
219 void (*dct_unquantize)(struct MpegEncContext *s,
220 DCTELEM *block, int n, int qscale);
de6d9b64
FB
221} MpegEncContext;
222
de6d9b64
FB
223int MPV_common_init(MpegEncContext *s);
224void MPV_common_end(MpegEncContext *s);
225void MPV_decode_mb(MpegEncContext *s, DCTELEM block[6][64]);
226void MPV_frame_start(MpegEncContext *s);
227void MPV_frame_end(MpegEncContext *s);
21af69f7
FB
228#ifdef HAVE_MMX
229void MPV_common_init_mmx(MpegEncContext *s);
230#endif
de6d9b64
FB
231
232/* motion_est.c */
233
234int estimate_motion(MpegEncContext *s,
235 int mb_x, int mb_y,
236 int *mx_ptr, int *my_ptr);
237
238/* mpeg12.c */
3c054a1c
FB
239extern INT16 default_intra_matrix[64];
240extern INT16 default_non_intra_matrix[64];
de6d9b64
FB
241
242void mpeg1_encode_picture_header(MpegEncContext *s, int picture_number);
243void mpeg1_encode_mb(MpegEncContext *s,
244 DCTELEM block[6][64],
245 int motion_x, int motion_y);
246
247/* h263enc.c */
248
249/* run length table */
250#define MAX_RUN 64
251#define MAX_LEVEL 64
252
253typedef struct RLTable {
254 int n; /* number of entries of table_vlc minus 1 */
255 int last; /* number of values for last = 0 */
256 const UINT16 (*table_vlc)[2];
257 const INT8 *table_run;
258 const INT8 *table_level;
259 UINT8 *index_run[2]; /* encoding only */
260 INT8 *max_level[2]; /* encoding & decoding */
261 INT8 *max_run[2]; /* encoding & decoding */
262 VLC vlc; /* decoding only */
263} RLTable;
264
265void init_rl(RLTable *rl);
266void init_vlc_rl(RLTable *rl);
267
3232bc88 268static inline int get_rl_index(const RLTable *rl, int last, int run, int level)
de6d9b64
FB
269{
270 int index;
271 index = rl->index_run[last][run];
272 if (index >= rl->n)
273 return rl->n;
274 if (level > rl->max_level[last][run])
275 return rl->n;
276 return index + level - 1;
277}
278
279void h263_encode_mb(MpegEncContext *s,
280 DCTELEM block[6][64],
281 int motion_x, int motion_y);
282void h263_encode_picture_header(MpegEncContext *s, int picture_number);
644d98a4 283int h263_encode_gob_header(MpegEncContext * s, int mb_line);
de6d9b64
FB
284void h263_dc_scale(MpegEncContext *s);
285INT16 *h263_pred_motion(MpegEncContext * s, int block,
286 int *px, int *py);
287void mpeg4_pred_ac(MpegEncContext * s, INT16 *block, int n,
288 int dir);
289void mpeg4_encode_picture_header(MpegEncContext *s, int picture_number);
290void h263_encode_init_vlc(MpegEncContext *s);
291
292void h263_decode_init_vlc(MpegEncContext *s);
293int h263_decode_picture_header(MpegEncContext *s);
644d98a4 294int h263_decode_gob_header(MpegEncContext *s);
de6d9b64
FB
295int mpeg4_decode_picture_header(MpegEncContext * s);
296int intel_h263_decode_picture_header(MpegEncContext *s);
297int h263_decode_mb(MpegEncContext *s,
298 DCTELEM block[6][64]);
299int h263_get_picture_format(int width, int height);
de6d9b64
FB
300
301/* rv10.c */
302void rv10_encode_picture_header(MpegEncContext *s, int picture_number);
303int rv_decode_dc(MpegEncContext *s, int n);
304
305/* msmpeg4.c */
306void msmpeg4_encode_picture_header(MpegEncContext * s, int picture_number);
ae40484c 307void msmpeg4_encode_ext_header(MpegEncContext * s);
de6d9b64
FB
308void msmpeg4_encode_mb(MpegEncContext * s,
309 DCTELEM block[6][64],
310 int motion_x, int motion_y);
311void msmpeg4_dc_scale(MpegEncContext * s);
312int msmpeg4_decode_picture_header(MpegEncContext * s);
ae40484c 313int msmpeg4_decode_ext_header(MpegEncContext * s, int buf_size);
de6d9b64
FB
314int msmpeg4_decode_mb(MpegEncContext *s,
315 DCTELEM block[6][64]);
316int msmpeg4_decode_init_vlc(MpegEncContext *s);
317
318/* mjpegenc.c */
319
320int mjpeg_init(MpegEncContext *s);
321void mjpeg_close(MpegEncContext *s);
322void mjpeg_encode_mb(MpegEncContext *s,
323 DCTELEM block[6][64]);
324void mjpeg_picture_header(MpegEncContext *s);
325void mjpeg_picture_trailer(MpegEncContext *s);