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