Statistics
| Branch: | Revision:

ffmpeg / libavcodec / mpegvideo.h @ 4278e7a6

History | View | Annotate | Download (12.4 KB)

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
#define S_TYPE 4 //S(GMC)-VOP MPEG4
25

    
26
enum OutputFormat {
27
    FMT_MPEG1,
28
    FMT_H263,
29
    FMT_MJPEG,
30
};
31

    
32
#define MPEG_BUF_SIZE (16 * 1024)
33

    
34
#define QMAT_SHIFT_MMX 19
35
#define QMAT_SHIFT 25
36

    
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 */
41
    int gop_size;
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 */
54

    
55
    /* bit output */
56
    PutBitContext pb;
57

    
58
    /* sequence parameters */
59
    int context_initialized;
60
    int picture_number;
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, all 3 arrays must be continuous */
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, all 3 arrays must be continuous */
79
    int ac_pred;
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 */
84

    
85
    int qscale;
86
    int pict_type;
87
    int last_non_b_pict_type; /* used for mpeg4 gmc b-frames */
88
    int frame_rate_index;
89
    /* motion compensation */
90
    int unrestricted_mv;
91
    int h263_long_vectors; /* use horrible h263v1 long vector mode */
92

    
93
    int f_code; /* resolution */
94
    int b_code; /* backward resolution for B Frames (mpeg4) */
95
    INT16 *mv_table[2];    /* MV table (1MV per MB)*/
96
    INT16 (*motion_val)[2]; /* used for MV prediction (4MV per MB)*/
97
    int full_search;
98
    int mv_dir;
99
#define MV_DIR_BACKWARD  1
100
#define MV_DIR_FORWARD   2
101
#define MV_DIRECT        4 // bidirectional mode where the difference equals the MV of the last P/S/I-Frame (mpeg4)
102
    int mv_type;
103
#define MV_TYPE_16X16       0   /* 1 vector for the whole mb */
104
#define MV_TYPE_8X8         1   /* 4 vectors (h263) */
105
#define MV_TYPE_16X8        2   /* 2 vectors, one per 16x8 block */ 
106
#define MV_TYPE_FIELD       3   /* 2 vectors, one per field */ 
107
#define MV_TYPE_DMV         4   /* 2 vectors, special mpeg2 Dual Prime Vectors */
108
    /* motion vectors for a macroblock 
109
       first coordinate : 0 = forward 1 = backward
110
       second "         : depend on type
111
       third  "         : 0 = x, 1 = y
112
    */
113
    int mv[2][4][2];
114
    int field_select[2][2];
115
    int last_mv[2][2][2];
116

    
117
    int has_b_frames;
118
    int no_rounding; /* apply no rounding to motion estimation (MPEG4) */
119

    
120
    /* macroblock layer */
121
    int mb_x, mb_y;
122
    int mb_incr;
123
    int mb_intra;
124
    INT16 *mb_var;      /* Table for MB variances */
125
    char *mb_type;    /* Table for MB type */
126

    
127
    int block_index[6];
128
    int block_wrap[6];
129

    
130
    /* matrix transmitted in the bitstream */
131
    UINT16 intra_matrix[64];
132
    UINT16 chroma_intra_matrix[64];
133
    UINT16 non_intra_matrix[64];
134
    UINT16 chroma_non_intra_matrix[64];
135
    /* precomputed matrix (combine qscale and DCT renorm) */
136
    int q_intra_matrix[64];
137
    int q_non_intra_matrix[64];
138
    /* identical to the above but for MMX & these are not permutated */
139
    UINT16 __align8 q_intra_matrix16[64] ;
140
    UINT16 __align8 q_non_intra_matrix16[64];
141
    int block_last_index[6];  /* last non zero coefficient in block */
142

    
143
    void *opaque; /* private data for the user */
144

    
145
    /* bit rate control */
146
    int I_frame_bits;    /* wanted number of bits per I frame */
147
    int P_frame_bits;    /* same for P frame */
148
    int avg_mb_var;        /* average MB variance for current frame */
149
    INT64 wanted_bits;
150
    INT64 total_bits;
151
 
152
    /* H.263 specific */
153
    int gob_number;
154
    int gob_index;
155
    int first_gob_line;
156
        
157
    /* H.263+ specific */
158
    int umvplus;
159
    int umvplus_dec;
160
    int h263_aic; /* Advanded INTRA Coding (AIC) */
161
    int h263_aic_dir; /* AIC direction: 0 = left, 1 = top */
162
    
163
    /* mpeg4 specific */
164
    int time_increment_resolution;
165
    int time_increment_bits;
166
    int time_increment;
167
    int time_base;
168
    int time;
169
    int last_non_b_time[2];
170
    int shape;
171
    int vol_sprite_usage;
172
    int sprite_width;
173
    int sprite_height;
174
    int sprite_left;
175
    int sprite_top;
176
    int sprite_brightness_change;
177
    int num_sprite_warping_points;
178
    int real_sprite_warping_points;
179
    int sprite_offset[2][2];
180
    int sprite_delta[2][2][2];
181
    int sprite_shift[2][2];
182
    int mcsel;
183
    int quant_precision;
184
    int quarter_sample;
185
    int scalability;
186
    int new_pred;
187
    int reduced_res_vop;
188
    int aspect_ratio_info;
189
    int sprite_warping_accuracy;
190
    int low_latency_sprite;
191
    int data_partioning;
192

    
193
    /* divx specific, used to workaround (many) bugs in divx5 */
194
    int divx_version;
195
    int divx_build;
196

    
197
    /* RV10 specific */
198
    int rv10_version; /* RV10 version: 0 or 3 */
199
    int rv10_first_dc_coded[3];
200
    
201
    /* MJPEG specific */
202
    struct MJpegContext *mjpeg_ctx;
203
    int mjpeg_vsample[3]; /* vertical sampling factors, default = {2, 1, 1} */
204
    int mjpeg_hsample[3]; /* horizontal sampling factors, default = {2, 1, 1} */
205
    int mjpeg_write_tables; /* do we want to have quantisation- and
206
                               huffmantables in the jpeg file ? */
207

    
208
    /* MSMPEG4 specific */
209
    int mv_table_index;
210
    int rl_table_index;
211
    int rl_chroma_table_index;
212
    int dc_table_index;
213
    int use_skip_mb_code;
214
    int slice_height;      /* in macroblocks */
215
    int first_slice_line;  
216
    int flipflop_rounding;
217
    int bitrate;
218
    /* decompression specific */
219
    GetBitContext gb;
220

    
221
    /* MPEG2 specific - I wish I had not to support this mess. */
222
    int progressive_sequence;
223
    int mpeg_f_code[2][2];
224
    int picture_structure;
225
/* picture type */
226
#define PICT_TOP_FIELD     1
227
#define PICT_BOTTOM_FIELD  2
228
#define PICT_FRAME         3
229

    
230
    int intra_dc_precision;
231
    int frame_pred_frame_dct;
232
    int top_field_first;
233
    int concealment_motion_vectors;
234
    int q_scale_type;
235
    int intra_vlc_format;
236
    int alternate_scan;
237
    int repeat_first_field;
238
    int chroma_420_type;
239
    int progressive_frame;
240
    int mpeg2;
241
    int full_pel[2];
242
    int interlaced_dct;
243
    int last_qscale;
244
    int first_slice;
245
    
246
    /* RTP specific */
247
    /* These are explained on avcodec.h */
248
    int rtp_mode;
249
    int rtp_payload_size;
250
    void (*rtp_callback)(void *data, int size, int packet_number);
251
    UINT8 *ptr_lastgob;
252
    UINT8 *ptr_last_mb_line;
253
    UINT32 mb_line_avgsize;
254
    
255
    DCTELEM block[6][64] __align8;
256
    void (*dct_unquantize)(struct MpegEncContext *s, 
257
                           DCTELEM *block, int n, int qscale);
258
} MpegEncContext;
259

    
260
int MPV_common_init(MpegEncContext *s);
261
void MPV_common_end(MpegEncContext *s);
262
void MPV_decode_mb(MpegEncContext *s, DCTELEM block[6][64]);
263
void MPV_frame_start(MpegEncContext *s);
264
void MPV_frame_end(MpegEncContext *s);
265
#ifdef HAVE_MMX
266
void MPV_common_init_mmx(MpegEncContext *s);
267
#endif
268

    
269
/* motion_est.c */
270

    
271
int estimate_motion(MpegEncContext *s, 
272
                    int mb_x, int mb_y,
273
                    int *mx_ptr, int *my_ptr);
274

    
275
/* mpeg12.c */
276
extern INT16 default_intra_matrix[64];
277
extern INT16 default_non_intra_matrix[64];
278

    
279
void mpeg1_encode_picture_header(MpegEncContext *s, int picture_number);
280
void mpeg1_encode_mb(MpegEncContext *s,
281
                     DCTELEM block[6][64],
282
                     int motion_x, int motion_y);
283

    
284
/* h263enc.c */
285

    
286
/* run length table */
287
#define MAX_RUN    64
288
#define MAX_LEVEL  64
289

    
290
typedef struct RLTable {
291
    int n; /* number of entries of table_vlc minus 1 */
292
    int last; /* number of values for last = 0 */
293
    const UINT16 (*table_vlc)[2];
294
    const INT8 *table_run;
295
    const INT8 *table_level;
296
    UINT8 *index_run[2]; /* encoding only */
297
    INT8 *max_level[2]; /* encoding & decoding */
298
    INT8 *max_run[2];   /* encoding & decoding */
299
    VLC vlc;            /* decoding only */
300
} RLTable;
301

    
302
void init_rl(RLTable *rl);
303
void init_vlc_rl(RLTable *rl);
304

    
305
static inline int get_rl_index(const RLTable *rl, int last, int run, int level)
306
{
307
    int index;
308
    index = rl->index_run[last][run];
309
    if (index >= rl->n)
310
        return rl->n;
311
    if (level > rl->max_level[last][run])
312
        return rl->n;
313
    return index + level - 1;
314
}
315

    
316
void h263_encode_mb(MpegEncContext *s, 
317
                    DCTELEM block[6][64],
318
                    int motion_x, int motion_y);
319
void mpeg4_encode_mb(MpegEncContext *s, 
320
                    DCTELEM block[6][64],
321
                    int motion_x, int motion_y);
322
void h263_encode_picture_header(MpegEncContext *s, int picture_number);
323
int h263_encode_gob_header(MpegEncContext * s, int mb_line);
324
void h263_dc_scale(MpegEncContext *s);
325
INT16 *h263_pred_motion(MpegEncContext * s, int block, 
326
                        int *px, int *py);
327
void mpeg4_pred_ac(MpegEncContext * s, INT16 *block, int n, 
328
                   int dir);
329
void mpeg4_encode_picture_header(MpegEncContext *s, int picture_number);
330
void h263_encode_init_vlc(MpegEncContext *s);
331

    
332
void h263_decode_init_vlc(MpegEncContext *s);
333
int h263_decode_picture_header(MpegEncContext *s);
334
int h263_decode_gob_header(MpegEncContext *s);
335
int mpeg4_decode_picture_header(MpegEncContext * s);
336
int intel_h263_decode_picture_header(MpegEncContext *s);
337
int h263_decode_mb(MpegEncContext *s,
338
                   DCTELEM block[6][64]);
339
int h263_get_picture_format(int width, int height);
340

    
341
/* rv10.c */
342
void rv10_encode_picture_header(MpegEncContext *s, int picture_number);
343
int rv_decode_dc(MpegEncContext *s, int n);
344

    
345
/* msmpeg4.c */
346
void msmpeg4_encode_picture_header(MpegEncContext * s, int picture_number);
347
void msmpeg4_encode_ext_header(MpegEncContext * s);
348
void msmpeg4_encode_mb(MpegEncContext * s, 
349
                       DCTELEM block[6][64],
350
                       int motion_x, int motion_y);
351
void msmpeg4_dc_scale(MpegEncContext * s);
352
int msmpeg4_decode_picture_header(MpegEncContext * s);
353
int msmpeg4_decode_ext_header(MpegEncContext * s, int buf_size);
354
int msmpeg4_decode_mb(MpegEncContext *s, 
355
                      DCTELEM block[6][64]);
356
int msmpeg4_decode_init_vlc(MpegEncContext *s);
357

    
358
/* mjpegenc.c */
359

    
360
int mjpeg_init(MpegEncContext *s);
361
void mjpeg_close(MpegEncContext *s);
362
void mjpeg_encode_mb(MpegEncContext *s, 
363
                     DCTELEM block[6][64]);
364
void mjpeg_picture_header(MpegEncContext *s);
365
void mjpeg_picture_trailer(MpegEncContext *s);