mpegvideo.h 37.8 KB
Newer Older
Fabrice Bellard's avatar
Fabrice Bellard committed
1 2
/*
 * Generic DCT based hybrid video encoder
3
 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
4
 * Copyright (c) 2002-2004 Michael Niedermayer
Fabrice Bellard's avatar
Fabrice Bellard committed
5
 *
6 7 8
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
9 10
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
11
 * version 2.1 of the License, or (at your option) any later version.
Fabrice Bellard's avatar
Fabrice Bellard committed
12
 *
13
 * FFmpeg is distributed in the hope that it will be useful,
Fabrice Bellard's avatar
Fabrice Bellard committed
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
Fabrice Bellard's avatar
Fabrice Bellard committed
17
 *
18
 * You should have received a copy of the GNU Lesser General Public
19
 * License along with FFmpeg; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Fabrice Bellard's avatar
Fabrice Bellard committed
21 22
 */

Michael Niedermayer's avatar
Michael Niedermayer committed
23
/**
24
 * @file
Michael Niedermayer's avatar
Michael Niedermayer committed
25 26
 * mpegvideo header.
 */
27

28 29
#ifndef AVCODEC_MPEGVIDEO_H
#define AVCODEC_MPEGVIDEO_H
30

31 32
#include <float.h>

33
#include "avcodec.h"
34
#include "blockdsp.h"
35
#include "error_resilience.h"
36
#include "fdctdsp.h"
37
#include "get_bits.h"
38
#include "h264chroma.h"
39
#include "h263dsp.h"
40
#include "hpeldsp.h"
41
#include "idctdsp.h"
42
#include "internal.h"
43
#include "me_cmp.h"
44
#include "motion_est.h"
45
#include "mpegpicture.h"
46
#include "mpegvideodsp.h"
47
#include "mpegvideoencdsp.h"
48
#include "pixblockdsp.h"
49
#include "put_bits.h"
50
#include "ratecontrol.h"
51
#include "parser.h"
52
#include "mpegutils.h"
53
#include "mpeg12data.h"
54
#include "qpeldsp.h"
55
#include "thread.h"
56
#include "videodsp.h"
57

58
#include "libavutil/opt.h"
59
#include "libavutil/timecode.h"
60

61
#define MAX_THREADS 32
62

63 64
#define MAX_B_FRAMES 16

65 66 67 68 69 70 71 72 73 74
/* Start codes. */
#define SEQ_END_CODE            0x000001b7
#define SEQ_START_CODE          0x000001b3
#define GOP_START_CODE          0x000001b8
#define PICTURE_START_CODE      0x00000100
#define SLICE_MIN_START_CODE    0x00000101
#define SLICE_MAX_START_CODE    0x000001af
#define EXT_START_CODE          0x000001b5
#define USER_START_CODE         0x000001b2

Michael Niedermayer's avatar
Michael Niedermayer committed
75 76 77
/**
 * MpegEncContext.
 */
Fabrice Bellard's avatar
Fabrice Bellard committed
78
typedef struct MpegEncContext {
79
    AVClass *class;
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94

    int y_dc_scale, c_dc_scale;
    int ac_pred;
    int block_last_index[12];  ///< last non zero coefficient in block
    int h263_aic;              ///< Advanded INTRA Coding (AIC)

    /* scantables */
    ScanTable inter_scantable; ///< if inter == intra then intra should be used to reduce tha cache usage
    ScanTable intra_scantable;
    ScanTable intra_h_scantable;
    ScanTable intra_v_scantable;

    /* WARNING: changes above this line require updates to hardcoded
     *          offsets used in asm. */

95
    struct AVCodecContext *avctx;
Fabrice Bellard's avatar
Fabrice Bellard committed
96
    /* the following parameters must be initialized before encoding */
97
    int width, height;///< picture size. must be a multiple of 16
Fabrice Bellard's avatar
Fabrice Bellard committed
98
    int gop_size;
99
    int intra_only;   ///< if true, only intra pictures are generated
100
    int64_t bit_rate; ///< wanted bit rate
101 102
    enum OutputFormat out_format; ///< output format
    int h263_pred;    ///< use mpeg4/h263 ac/dc predictions
103
    int pb_frame;     ///< PB frame mode (0 = none, 1 = base, 2 = improved)
104 105

/* the following codec id fields are deprecated in favor of codec_id */
106 107 108
    int h263_plus;    ///< h263 plus headers
    int h263_flv;     ///< use flv h263 header

109
    enum AVCodecID codec_id;     /* see AV_CODEC_ID_xxx */
110 111 112
    int fixed_qscale; ///< fixed qscale if non zero
    int encoding;     ///< true if we are encoding (vs decoding)
    int max_b_frames; ///< max number of b-frames for encoding
113 114
    int luma_elim_threshold;
    int chroma_elim_threshold;
115 116
    int strict_std_compliance; ///< strictly follow the std (MPEG4, ...)
    int workaround_bugs;       ///< workaround bugs in encoders which cannot be detected automatically
117
    int codec_tag;             ///< internal codec_tag upper case converted from avctx codec_tag
Fabrice Bellard's avatar
Fabrice Bellard committed
118 119 120 121
    /* the following fields are managed internally by the encoder */

    /* sequence parameters */
    int context_initialized;
Diego Biurrun's avatar
Diego Biurrun committed
122 123
    int input_picture_number;  ///< used to set pic->display_picture_number, should not be used for/by anything else
    int coded_picture_number;  ///< used to set pic->coded_picture_number, should not be used for/by anything else
124
    int picture_number;       //FIXME remove, unclear definition
125 126
    int picture_in_gop_number; ///< 0-> first pic in gop, ...
    int mb_width, mb_height;   ///< number of MBs horizontally & vertically
Diego Biurrun's avatar
Diego Biurrun committed
127 128 129
    int mb_stride;             ///< mb_width+1 used for some arrays to allow simple addressing of left & top MBs without sig11
    int b8_stride;             ///< 2*mb_width+1 used for some 8x8 block arrays to allow simple addressing
    int h_edge_pos, v_edge_pos;///< horizontal / vertical position of the right/bottom edge (pixel replication)
130
    int mb_num;                ///< number of MBs of a picture
131 132
    ptrdiff_t linesize;        ///< line size, in bytes, may be different from width
    ptrdiff_t uvlinesize;      ///< line size, for chroma in bytes, may be different from width
133
    Picture *picture;          ///< main picture buffer
134 135
    Picture **input_picture;   ///< next pictures on display order for encoding
    Picture **reordered_input_picture; ///< pointer to the next pictures in codedorder for encoding
136

137
    int64_t user_specified_pts; ///< last non-zero pts from AVFrame which was passed into avcodec_encode_video2()
138 139 140 141 142 143 144 145
    /**
     * pts difference between the first and second input frame, used for
     * calculating dts of the first frame when there's a delay */
    int64_t dts_delta;
    /**
     * reordered pts to be used as dts for the next output frame when there's
     * a delay */
    int64_t reordered_pts;
146

147 148 149
    /** bit output */
    PutBitContext pb;

150 151 152
    int start_mb_y;            ///< start mb_y of this thread (so current thread should process start_mb_y <= row < end_mb_y)
    int end_mb_y;              ///< end   mb_y of this thread (so current thread should process start_mb_y <= row < end_mb_y)
    struct MpegEncContext *thread_context[MAX_THREADS];
153
    int slice_context_count;   ///< number of used thread_contexts
154 155

    /**
156 157 158
     * copy of the previous picture structure.
     * note, linesize & data, might not match the previous picture (for field pictures)
     */
159 160 161
    Picture last_picture;

    /**
162 163 164 165
     * copy of the next picture structure.
     * note, linesize & data, might not match the next picture (for field pictures)
     */
    Picture next_picture;
166 167

    /**
168 169 170 171
     * copy of the source picture structure for encoding.
     * note, linesize & data, might not match the source picture (for field pictures)
     */
    Picture new_picture;
172 173

    /**
174 175 176
     * copy of the current picture structure.
     * note, linesize & data, might not match the current picture (for field pictures)
     */
177 178
    Picture current_picture;    ///< buffer to store the decompressed current picture

179
    Picture *last_picture_ptr;     ///< pointer to the previous picture.
180
    Picture *next_picture_ptr;     ///< pointer to the next picture (for bidir pred)
181
    Picture *current_picture_ptr;  ///< pointer to the current picture
182
    int last_dc[3];                ///< last DC values for MPEG1
183 184
    int16_t *dc_val_base;
    int16_t *dc_val[3];            ///< used for mpeg4 DC prediction, all 3 arrays must be continuous
185 186
    const uint8_t *y_dc_scale_table;     ///< qscale -> y_dc_scale table
    const uint8_t *c_dc_scale_table;     ///< qscale -> c_dc_scale table
Michael Niedermayer's avatar
Michael Niedermayer committed
187
    const uint8_t *chroma_qscale_table;  ///< qscale -> chroma_qscale (h263)
188
    uint8_t *coded_block_base;
Michael Niedermayer's avatar
Michael Niedermayer committed
189
    uint8_t *coded_block;          ///< used for coded block pattern prediction (msmpeg4v3, wmv1)
190
    int16_t (*ac_val_base)[16];
191
    int16_t (*ac_val[3])[16];      ///< used for mpeg4 AC prediction, all 3 arrays must be continuous
192 193
    int mb_skipped;                ///< MUST BE SET only during DECODING
    uint8_t *mbskip_table;        /**< used to avoid copy if macroblock skipped (for black regions for example)
194
                                   and used for b-frame encoding & decoding (contains skip table of next P Frame) */
195 196 197
    uint8_t *mbintra_table;       ///< used to avoid setting {ac, dc, cbp}-pred stuff to zero on inter MB decoding
    uint8_t *cbp_table;           ///< used to store cbp, ac_pred for partitioned decoding
    uint8_t *pred_dir_table;      ///< used to store pred_dir for partitioned decoding
198 199

    ScratchpadContext sc;
Fabrice Bellard's avatar
Fabrice Bellard committed
200

201 202
    int qscale;                 ///< QP
    int chroma_qscale;          ///< chroma QP
203 204
    unsigned int lambda;        ///< lagrange multipler used in rate distortion
    unsigned int lambda2;       ///< (lambda*lambda) >> FF_LAMBDA_SHIFT
205
    int *lambda_table;
206 207
    int adaptive_quant;         ///< use adaptive quantization
    int dquant;                 ///< qscale difference to prev qscale
208
    int closed_gop;             ///< MPEG1/2 GOP is closed
209
    int pict_type;              ///< AV_PICTURE_TYPE_I, AV_PICTURE_TYPE_P, AV_PICTURE_TYPE_B, ...
210
    int vbv_delay;
211
    int last_pict_type; //FIXME removes
212
    int last_non_b_pict_type;   ///< used for mpeg4 gmc b-frames & ratecontrol
213
    int droppable;
Fabrice Bellard's avatar
Fabrice Bellard committed
214
    int frame_rate_index;
215
    AVRational mpeg2_frame_rate_ext;
216
    int last_lambda_for[5];     ///< last lambda for a specific pict type
Michael Niedermayer's avatar
Michael Niedermayer committed
217
    int skipdct;                ///< skip dct and code zero residual
218

Fabrice Bellard's avatar
Fabrice Bellard committed
219
    /* motion compensation */
220 221
    int unrestricted_mv;        ///< mv can point outside of the coded picture
    int h263_long_vectors;      ///< use horrible h263v1 long vector mode
Michael Niedermayer's avatar
Michael Niedermayer committed
222

223
    BlockDSPContext bdsp;
224
    FDCTDSPContext fdsp;
225
    H264ChromaContext h264chroma;
226
    HpelDSPContext hdsp;
227
    IDCTDSPContext idsp;
228
    MECmpContext mecc;
229
    MpegVideoDSPContext mdsp;
230
    MpegvideoEncDSPContext mpvencdsp;
231
    PixblockDSPContext pdsp;
232
    QpelDSPContext qdsp;
233
    VideoDSPContext vdsp;
234
    H263DSPContext h263dsp;
235 236
    int f_code;                 ///< forward MV resolution
    int b_code;                 ///< backward MV resolution for B Frames (mpeg4)
237 238 239
    int16_t (*p_mv_table_base)[2];
    int16_t (*b_forw_mv_table_base)[2];
    int16_t (*b_back_mv_table_base)[2];
240 241
    int16_t (*b_bidir_forw_mv_table_base)[2];
    int16_t (*b_bidir_back_mv_table_base)[2];
242
    int16_t (*b_direct_mv_table_base)[2];
243 244
    int16_t (*p_field_mv_table_base[2][2])[2];
    int16_t (*b_field_mv_table_base[2][2][2])[2];
245 246 247 248 249 250
    int16_t (*p_mv_table)[2];            ///< MV table (1MV per MB) p-frame encoding
    int16_t (*b_forw_mv_table)[2];       ///< MV table (1MV per MB) forward mode b-frame encoding
    int16_t (*b_back_mv_table)[2];       ///< MV table (1MV per MB) backward mode b-frame encoding
    int16_t (*b_bidir_forw_mv_table)[2]; ///< MV table (1MV per MB) bidir mode b-frame encoding
    int16_t (*b_bidir_back_mv_table)[2]; ///< MV table (1MV per MB) bidir mode b-frame encoding
    int16_t (*b_direct_mv_table)[2];     ///< MV table (1MV per MB) direct mode b-frame encoding
251 252 253 254
    int16_t (*p_field_mv_table[2][2])[2];   ///< MV table (2MV per MB) interlaced p-frame encoding
    int16_t (*b_field_mv_table[2][2][2])[2];///< MV table (4MV per MB) interlaced b-frame encoding
    uint8_t (*p_field_select_table[2]);
    uint8_t (*b_field_select_table[2][2]);
255
#if FF_API_MOTION_EST
256
    int me_method;                       ///< ME algorithm
257 258
#endif
    int motion_est;                      ///< ME algorithm
259
    int me_penalty_compensation;
260
    int me_pre;                          ///< prepass for motion estimation
Fabrice Bellard's avatar
Fabrice Bellard committed
261
    int mv_dir;
262 263
#define MV_DIR_FORWARD   1
#define MV_DIR_BACKWARD  2
Michael Niedermayer's avatar
Michael Niedermayer committed
264
#define MV_DIRECT        4 ///< bidirectional mode where the difference equals the MV of the last P/S/I-Frame (mpeg4)
Fabrice Bellard's avatar
Fabrice Bellard committed
265
    int mv_type;
266 267 268 269 270 271
#define MV_TYPE_16X16       0   ///< 1 vector for the whole mb
#define MV_TYPE_8X8         1   ///< 4 vectors (h263, mpeg4 4MV)
#define MV_TYPE_16X8        2   ///< 2 vectors, one per 16x8 block
#define MV_TYPE_FIELD       3   ///< 2 vectors, one per field
#define MV_TYPE_DMV         4   ///< 2 vectors, special mpeg2 Dual Prime Vectors
    /**motion vectors for a macroblock
Fabrice Bellard's avatar
Fabrice Bellard committed
272 273 274 275 276 277
       first coordinate : 0 = forward 1 = backward
       second "         : depend on type
       third  "         : 0 = x, 1 = y
    */
    int mv[2][4][2];
    int field_select[2][2];
278 279
    int last_mv[2][2][2];             ///< last MV, used for MV prediction in MPEG1 & B-frame MPEG4
    uint8_t *fcode_tab;               ///< smallest fcode needed for each MV
280
    int16_t direct_scale_mv[2][64];   ///< precomputed to avoid divisions in ff_mpeg4_set_direct_mv
281

Michael Niedermayer's avatar
Michael Niedermayer committed
282
    MotionEstContext me;
Fabrice Bellard's avatar
Fabrice Bellard committed
283

284
    int no_rounding;  /**< apply no rounding to motion compensation (MPEG4, msmpeg4, ...)
Diego Biurrun's avatar
Diego Biurrun committed
285
                        for b-frames rounding mode is always 0 */
Fabrice Bellard's avatar
Fabrice Bellard committed
286 287 288

    /* macroblock layer */
    int mb_x, mb_y;
289
    int mb_skip_run;
Fabrice Bellard's avatar
Fabrice Bellard committed
290
    int mb_intra;
291
    uint16_t *mb_type;  ///< Table for candidate MB types for encoding (defines in mpegutils.h)
292

Michael Niedermayer's avatar
Michael Niedermayer committed
293
    int block_index[6]; ///< index to current MB in block based arrays with edges
Michael Niedermayer's avatar
Michael Niedermayer committed
294
    int block_wrap[6];
295
    uint8_t *dest[3];
296

297
    int *mb_index2xy;        ///< mb_index -> mb_x + mb_y*mb_stride
Michael Niedermayer's avatar
Michael Niedermayer committed
298

Michael Niedermayer's avatar
Michael Niedermayer committed
299
    /** matrix transmitted in the bitstream */
300 301 302 303
    uint16_t intra_matrix[64];
    uint16_t chroma_intra_matrix[64];
    uint16_t inter_matrix[64];
    uint16_t chroma_inter_matrix[64];
304
    int force_duplicated_matrix; ///< Force duplication of mjpeg matrices, useful for rtp streaming
305

306 307 308 309 310
    int intra_quant_bias;    ///< bias for the quantizer
    int inter_quant_bias;    ///< bias for the quantizer
    int min_qcoeff;          ///< minimum encodable coefficient
    int max_qcoeff;          ///< maximum encodable coefficient
    int ac_esc_length;       ///< num of bits needed to encode the longest esc
311 312
    uint8_t *intra_ac_vlc_length;
    uint8_t *intra_ac_vlc_last_length;
313 314
    uint8_t *intra_chroma_ac_vlc_length;
    uint8_t *intra_chroma_ac_vlc_last_length;
315 316
    uint8_t *inter_ac_vlc_length;
    uint8_t *inter_ac_vlc_last_length;
317
    uint8_t *luma_dc_vlc_length;
318
#define UNI_AC_ENC_INDEX(run,level) ((run)*128 + (level))
319

320
    int coded_score[12];
321

Michael Niedermayer's avatar
Michael Niedermayer committed
322
    /** precomputed matrix (combine qscale and DCT renorm) */
323
    int (*q_intra_matrix)[64];
324
    int (*q_chroma_intra_matrix)[64];
325
    int (*q_inter_matrix)[64];
326 327
    /** identical to the above but for MMX & these are not permutated, second 64 entries are bias*/
    uint16_t (*q_intra_matrix16)[2][64];
328
    uint16_t (*q_chroma_intra_matrix16)[2][64];
329
    uint16_t (*q_inter_matrix16)[2][64];
330

331 332 333 334
    /* noise reduction */
    int (*dct_error_sum)[64];
    int dct_count[2];
    uint16_t (*dct_offset)[64];
Fabrice Bellard's avatar
Fabrice Bellard committed
335 336

    /* bit rate control */
337
    int64_t total_bits;
338
    int frame_bits;                ///< bits used for the current frame
339
    int stuffing_bits;             ///< bits used for stuffing
340
    int next_lambda;               ///< next lambda used for retrying to encode a frame
Michael Niedermayer's avatar
Michael Niedermayer committed
341
    RateControlContext rc_context; ///< contains stuff only accessed in ratecontrol.c
342

343 344 345 346 347 348
    /* statistics, used for 2-pass encoding */
    int mv_bits;
    int header_bits;
    int i_tex_bits;
    int p_tex_bits;
    int i_count;
349 350
    int f_count;
    int b_count;
351
    int skip_count;
Michael Niedermayer's avatar
Michael Niedermayer committed
352 353
    int misc_bits; ///< cbp, mb_type
    int last_bits; ///< temp var used for calculating the above vars
354

355
    /* error concealment / resync */
356 357 358
    int resync_mb_x;                 ///< x position of last resync marker
    int resync_mb_y;                 ///< y position of last resync marker
    GetBitContext last_resync_gb;    ///< used to search for the next resync marker
Michael Niedermayer's avatar
Michael Niedermayer committed
359
    int mb_num_left;                 ///< number of MBs left in this video packet (for partitioned Slices only)
360 361
    int next_p_frame_damaged;        ///< set if the next p frame is damaged, to avoid showing trashed b frames

362
    ParseContext parse_context;
363

364
    /* H.263 specific */
365
    int gob_index;
366
    int obmc;                       ///< overlapped block motion compensation
367 368 369 370
    int mb_info;                    ///< interval for outputting info about mb offsets as side data
    int prev_mb_info, last_mb_info;
    uint8_t *mb_info_ptr;
    int mb_info_size;
371
    int ehc_mode;
372
    int rc_strategy;
373

374
    /* H.263+ specific */
375
    int umvplus;                    ///< == H263+ && unrestricted_mv
376 377
    int h263_aic_dir;               ///< AIC direction: 0 = left, 1 = top
    int h263_slice_structured;
378
    int alt_inter_vlc;              ///< alternative inter vlc
Michael Niedermayer's avatar
Michael Niedermayer committed
379
    int modified_quant;
380
    int loop_filter;
381
    int custom_pcf;
382

Fabrice Bellard's avatar
Fabrice Bellard committed
383
    /* mpeg4 specific */
384 385
    ///< number of bits to represent the fractional part of time (encoder only)
    int time_increment_bits;
386
    int last_time_base;
387 388
    int time_base;                  ///< time in seconds of last I,P,S Frame
    int64_t time;                   ///< time of current frame
389
    int64_t last_non_b_time;
390 391
    uint16_t pp_time;               ///< time distance between the last 2 p,s,i frames
    uint16_t pb_time;               ///< time distance between the last b and p,s,i frame
392
    uint16_t pp_field_time;
393
    uint16_t pb_field_time;         ///< like above, just for interlaced
Michael Niedermayer's avatar
Michael Niedermayer committed
394
    int real_sprite_warping_points;
395 396
    int sprite_offset[2][2];         ///< sprite offset[isChroma][isMVY]
    int sprite_delta[2][2];          ///< sprite_delta [isY][isMVY]
397
    int mcsel;
Fabrice Bellard's avatar
Fabrice Bellard committed
398
    int quant_precision;
399
    int quarter_sample;              ///< 1->qpel, 0->half pel ME/MC
400
    int aspect_ratio_info; //FIXME remove
401
    int sprite_warping_accuracy;
402 403 404
    int data_partitioning;           ///< data partitioning flag from header
    int partitioned_frame;           ///< is current frame partitioned
    int low_delay;                   ///< no reordering needed / has no b-frames
Michael Niedermayer's avatar
Michael Niedermayer committed
405
    int vo_type;
406 407
    PutBitContext tex_pb;            ///< used for data partitioned VOPs
    PutBitContext pb2;               ///< used for data partitioned VOPs
408
    int mpeg_quant;
409
    int padding_bug_score;             ///< used to detect the VERY common padding bug in MPEG4
Michael Niedermayer's avatar
Michael Niedermayer committed
410 411

    /* divx specific, used to workaround (many) bugs in divx5 */
412
    int divx_packed;
413
    uint8_t *bitstream_buffer; //Divx 5.01 puts several frames in a single one, this is used to reorder them
Michael Niedermayer's avatar
Michael Niedermayer committed
414
    int bitstream_buffer_size;
415
    unsigned int allocated_bitstream_buffer_size;
416

Fabrice Bellard's avatar
Fabrice Bellard committed
417
    /* RV10 specific */
418
    int rv10_version; ///< RV10 version: 0 or 3
Fabrice Bellard's avatar
Fabrice Bellard committed
419
    int rv10_first_dc_coded[3];
420

Fabrice Bellard's avatar
Fabrice Bellard committed
421 422
    /* MJPEG specific */
    struct MJpegContext *mjpeg_ctx;
423
    int esc_pos;
424
    int pred;
Fabrice Bellard's avatar
Fabrice Bellard committed
425 426 427 428 429 430 431

    /* MSMPEG4 specific */
    int mv_table_index;
    int rl_table_index;
    int rl_chroma_table_index;
    int dc_table_index;
    int use_skip_mb_code;
432 433
    int slice_height;      ///< in macroblocks
    int first_slice_line;  ///< used in mpeg4 too to handle resync markers
434
    int flipflop_rounding;
Michael Niedermayer's avatar
Michael Niedermayer committed
435
    int msmpeg4_version;   ///< 0=not msmpeg4, 1=mp41, 2=mp42, 3=mp43/divx3 4=wmv1/7 5=wmv2/8
Michael Niedermayer's avatar
Michael Niedermayer committed
436 437 438
    int per_mb_rl_table;
    int esc3_level_length;
    int esc3_run_length;
Michael Niedermayer's avatar
Michael Niedermayer committed
439
    /** [mb_intra][isChroma][level][run][last] */
440
    int (*ac_stats)[2][MAX_LEVEL+1][MAX_RUN+1][2];
441
    int inter_intra_pred;
Michael Niedermayer's avatar
Michael Niedermayer committed
442
    int mspel;
Michael Niedermayer's avatar
Michael Niedermayer committed
443

Fabrice Bellard's avatar
Fabrice Bellard committed
444 445 446
    /* decompression specific */
    GetBitContext gb;

Michael Niedermayer's avatar
Michael Niedermayer committed
447
    /* Mpeg1 specific */
448 449 450 451
    int gop_picture_number;  ///< index of the first picture of a GOP based on fake_pic_num & mpeg1 specific
    int last_mv_dir;         ///< last mv_dir, used for b frame encoding
    uint8_t *vbv_delay_ptr;  ///< pointer to vbv_delay in the bitstream

452
    /* MPEG-2-specific - I wished not to have to support this mess. */
Fabrice Bellard's avatar
Fabrice Bellard committed
453 454
    int progressive_sequence;
    int mpeg_f_code[2][2];
455 456

    // picture structure defines are loaded from mpegutils.h
Fabrice Bellard's avatar
Fabrice Bellard committed
457 458
    int picture_structure;

459
    int64_t timecode_frame_start; ///< GOP timecode frame start number, in non drop frame format
Fabrice Bellard's avatar
Fabrice Bellard committed
460 461 462 463 464
    int intra_dc_precision;
    int frame_pred_frame_dct;
    int top_field_first;
    int concealment_motion_vectors;
    int q_scale_type;
465
    int brd_scale;
Fabrice Bellard's avatar
Fabrice Bellard committed
466 467
    int intra_vlc_format;
    int alternate_scan;
468
    int seq_disp_ext;
Fabrice Bellard's avatar
Fabrice Bellard committed
469 470
    int repeat_first_field;
    int chroma_420_type;
471 472 473 474
    int chroma_format;
#define CHROMA_420 1
#define CHROMA_422 2
#define CHROMA_444 3
475 476
    int chroma_x_shift;//depend on pix_format, that depend on chroma_format
    int chroma_y_shift;
477

Fabrice Bellard's avatar
Fabrice Bellard committed
478
    int progressive_frame;
479
    int full_pel[2];
Fabrice Bellard's avatar
Fabrice Bellard committed
480
    int interlaced_dct;
481
    int first_field;         ///< is 1 for the first field of a field picture 0 otherwise
482
    int drop_frame_timecode; ///< timecode is in drop frame format.
483
    int scan_offset;         ///< reserve space for SVCD scan offset user data.
484

485
    /* RTP specific */
486
    int rtp_mode;
487
    int rtp_payload_size;
488

489 490
    char *tc_opt_str;        ///< timecode option string
    AVTimecode tc;           ///< timecode context
491

492
    uint8_t *ptr_lastgob;
493
    int swap_uv;             //vcr2 codec is an MPEG-2 variant with U and V swapped
494
    int pack_pblocks;        //xvmc needs to keep blocks without gaps.
Diego Biurrun's avatar
Diego Biurrun committed
495
    int16_t (*pblocks[12])[64];
496

Diego Biurrun's avatar
Diego Biurrun committed
497
    int16_t (*block)[64]; ///< points to one of the following blocks
498
    int16_t (*blocks)[12][64]; // for HQ mode we need to keep the best block
Diego Biurrun's avatar
Diego Biurrun committed
499
    int (*decode_mb)(struct MpegEncContext *s, int16_t block[6][64]); // used by some codecs to avoid a switch()
500 501
#define SLICE_OK         0
#define SLICE_ERROR     -1
502 503
#define SLICE_END       -2 ///<end marker found
#define SLICE_NOEND     -3 ///<no end marker or error found but mb count exceeded
504 505

    void (*dct_unquantize_mpeg1_intra)(struct MpegEncContext *s,
Diego Biurrun's avatar
Diego Biurrun committed
506
                           int16_t *block/*align 16*/, int n, int qscale);
507
    void (*dct_unquantize_mpeg1_inter)(struct MpegEncContext *s,
Diego Biurrun's avatar
Diego Biurrun committed
508
                           int16_t *block/*align 16*/, int n, int qscale);
509
    void (*dct_unquantize_mpeg2_intra)(struct MpegEncContext *s,
Diego Biurrun's avatar
Diego Biurrun committed
510
                           int16_t *block/*align 16*/, int n, int qscale);
511
    void (*dct_unquantize_mpeg2_inter)(struct MpegEncContext *s,
Diego Biurrun's avatar
Diego Biurrun committed
512
                           int16_t *block/*align 16*/, int n, int qscale);
513
    void (*dct_unquantize_h263_intra)(struct MpegEncContext *s,
Diego Biurrun's avatar
Diego Biurrun committed
514
                           int16_t *block/*align 16*/, int n, int qscale);
515
    void (*dct_unquantize_h263_inter)(struct MpegEncContext *s,
Diego Biurrun's avatar
Diego Biurrun committed
516
                           int16_t *block/*align 16*/, int n, int qscale);
517
    void (*dct_unquantize_intra)(struct MpegEncContext *s, // unquantizer to use (mpeg4 can use both)
Diego Biurrun's avatar
Diego Biurrun committed
518
                           int16_t *block/*align 16*/, int n, int qscale);
519
    void (*dct_unquantize_inter)(struct MpegEncContext *s, // unquantizer to use (mpeg4 can use both)
Diego Biurrun's avatar
Diego Biurrun committed
520 521 522 523
                           int16_t *block/*align 16*/, int n, int qscale);
    int (*dct_quantize)(struct MpegEncContext *s, int16_t *block/*align 16*/, int n, int qscale, int *overflow);
    int (*fast_dct_quantize)(struct MpegEncContext *s, int16_t *block/*align 16*/, int n, int qscale, int *overflow);
    void (*denoise_dct)(struct MpegEncContext *s, int16_t *block);
524 525

    int mpv_flags;      ///< flags set by private options
526
    int quantizer_noise_shaping;
527

528 529
    /**
     * ratecontrol qmin qmax limiting method
530
     * 0-> clipping, 1-> use a nice continuous function to limit qscale within qmin/qmax.
531 532
     */
    float rc_qsquish;
533 534
    float rc_qmod_amp;
    int   rc_qmod_freq;
535 536
    float rc_initial_cplx;
    float rc_buffer_aggressivity;
537
    float border_masking;
538
    int lmin, lmax;
539
    int vbv_ignore_qmax;
540

541 542
    char *rc_eq;

543 544
    /* temp buffers for rate control */
    float *cplx_tab, *bits_tab;
545 546 547 548

    /* flag to indicate a reinitialization is required, e.g. after
     * a frame size change */
    int context_reinit;
549 550

    ERContext er;
551 552

    int error_rate;
553 554 555

    /* temporary frames used by b_frame_strategy = 2 */
    AVFrame *tmp_frames[MAX_B_FRAMES + 2];
556 557
    int b_frame_strategy;
    int b_sensitivity;
558 559 560 561 562 563

    /* frame skip options for encoding */
    int frame_skip_threshold;
    int frame_skip_factor;
    int frame_skip_exp;
    int frame_skip_cmp;
564 565

    int scenechange_threshold;
566
    int noise_reduction;
Fabrice Bellard's avatar
Fabrice Bellard committed
567 568
} MpegEncContext;

569 570
/* mpegvideo_enc common options */
#define FF_MPV_FLAG_SKIP_RD      0x0001
571
#define FF_MPV_FLAG_STRICT_GOP   0x0002
572
#define FF_MPV_FLAG_QP_RD        0x0004
573
#define FF_MPV_FLAG_CBP_RD       0x0008
574
#define FF_MPV_FLAG_NAQ          0x0010
575
#define FF_MPV_FLAG_MV0          0x0020
576

577 578 579 580 581 582
enum rc_strategy {
    MPV_RC_STRATEGY_FFMPEG,
    MPV_RC_STRATEGY_XVID,
    NB_MPV_RC_STRATEGY
};

583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598
#define FF_MPV_OPT_CMP_FUNC \
{ "sad",    "Sum of absolute differences, fast", 0, AV_OPT_TYPE_CONST, {.i64 = FF_CMP_SAD }, INT_MIN, INT_MAX, FF_MPV_OPT_FLAGS, "cmp_func" }, \
{ "sse",    "Sum of squared errors", 0, AV_OPT_TYPE_CONST, {.i64 = FF_CMP_SSE }, INT_MIN, INT_MAX, FF_MPV_OPT_FLAGS, "cmp_func" }, \
{ "satd",   "Sum of absolute Hadamard transformed differences", 0, AV_OPT_TYPE_CONST, {.i64 = FF_CMP_SATD }, INT_MIN, INT_MAX, FF_MPV_OPT_FLAGS, "cmp_func" }, \
{ "dct",    "Sum of absolute DCT transformed differences", 0, AV_OPT_TYPE_CONST, {.i64 = FF_CMP_DCT }, INT_MIN, INT_MAX, FF_MPV_OPT_FLAGS, "cmp_func" }, \
{ "psnr",   "Sum of squared quantization errors, low quality", 0, AV_OPT_TYPE_CONST, {.i64 = FF_CMP_PSNR }, INT_MIN, INT_MAX, FF_MPV_OPT_FLAGS, "cmp_func" }, \
{ "bit",    "Number of bits needed for the block", 0, AV_OPT_TYPE_CONST, {.i64 = FF_CMP_BIT }, INT_MIN, INT_MAX, FF_MPV_OPT_FLAGS, "cmp_func" }, \
{ "rd",     "Rate distortion optimal, slow", 0, AV_OPT_TYPE_CONST, {.i64 = FF_CMP_RD }, INT_MIN, INT_MAX, FF_MPV_OPT_FLAGS, "cmp_func" }, \
{ "zero",   "Zero", 0, AV_OPT_TYPE_CONST, {.i64 = FF_CMP_ZERO }, INT_MIN, INT_MAX, FF_MPV_OPT_FLAGS, "cmp_func" }, \
{ "vsad",   "Sum of absolute vertical differences", 0, AV_OPT_TYPE_CONST, {.i64 = FF_CMP_VSAD }, INT_MIN, INT_MAX, FF_MPV_OPT_FLAGS, "cmp_func" }, \
{ "vsse",   "Sum of squared vertical differences", 0, AV_OPT_TYPE_CONST, {.i64 = FF_CMP_VSSE }, INT_MIN, INT_MAX, FF_MPV_OPT_FLAGS, "cmp_func" }, \
{ "nsse",   "Noise preserving sum of squared differences", 0, AV_OPT_TYPE_CONST, {.i64 = FF_CMP_NSSE }, INT_MIN, INT_MAX, FF_MPV_OPT_FLAGS, "cmp_func" }, \
{ "dct264", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = FF_CMP_DCT264 }, INT_MIN, INT_MAX, FF_MPV_OPT_FLAGS, "cmp_func" }, \
{ "dctmax", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = FF_CMP_DCTMAX }, INT_MIN, INT_MAX, FF_MPV_OPT_FLAGS, "cmp_func" }, \
{ "chroma", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = FF_CMP_CHROMA }, INT_MIN, INT_MAX, FF_MPV_OPT_FLAGS, "cmp_func" }

599
#ifndef FF_MPV_OFFSET
600
#define FF_MPV_OFFSET(x) offsetof(MpegEncContext, x)
601
#endif
602 603
#define FF_MPV_OPT_FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
#define FF_MPV_COMMON_OPTS \
604
FF_MPV_OPT_CMP_FUNC, \
605
{ "mpv_flags",      "Flags common for all mpegvideo-based encoders.", FF_MPV_OFFSET(mpv_flags), AV_OPT_TYPE_FLAGS, { .i64 = 0 }, INT_MIN, INT_MAX, FF_MPV_OPT_FLAGS, "mpv_flags" },\
606 607 608 609
{ "skip_rd",        "RD optimal MB level residual skipping", 0, AV_OPT_TYPE_CONST, { .i64 = FF_MPV_FLAG_SKIP_RD },    0, 0, FF_MPV_OPT_FLAGS, "mpv_flags" },\
{ "strict_gop",     "Strictly enforce gop size",             0, AV_OPT_TYPE_CONST, { .i64 = FF_MPV_FLAG_STRICT_GOP }, 0, 0, FF_MPV_OPT_FLAGS, "mpv_flags" },\
{ "qp_rd",          "Use rate distortion optimization for qp selection", 0, AV_OPT_TYPE_CONST, { .i64 = FF_MPV_FLAG_QP_RD },  0, 0, FF_MPV_OPT_FLAGS, "mpv_flags" },\
{ "cbp_rd",         "use rate distortion optimization for CBP",          0, AV_OPT_TYPE_CONST, { .i64 = FF_MPV_FLAG_CBP_RD }, 0, 0, FF_MPV_OPT_FLAGS, "mpv_flags" },\
610
{ "naq",            "normalize adaptive quantization",                   0, AV_OPT_TYPE_CONST, { .i64 = FF_MPV_FLAG_NAQ },    0, 0, FF_MPV_OPT_FLAGS, "mpv_flags" },\
611
{ "mv0",            "always try a mb with mv=<0,0>",                     0, AV_OPT_TYPE_CONST, { .i64 = FF_MPV_FLAG_MV0 },    0, 0, FF_MPV_OPT_FLAGS, "mpv_flags" },\
612
{ "luma_elim_threshold",   "single coefficient elimination threshold for luminance (negative values also consider dc coefficient)",\
613
                                                                      FF_MPV_OFFSET(luma_elim_threshold), AV_OPT_TYPE_INT, { .i64 = 0 }, INT_MIN, INT_MAX, FF_MPV_OPT_FLAGS },\
614
{ "chroma_elim_threshold", "single coefficient elimination threshold for chrominance (negative values also consider dc coefficient)",\
615
                                                                      FF_MPV_OFFSET(chroma_elim_threshold), AV_OPT_TYPE_INT, { .i64 = 0 }, INT_MIN, INT_MAX, FF_MPV_OPT_FLAGS },\
616 617
{ "quantizer_noise_shaping", NULL,                                  FF_MPV_OFFSET(quantizer_noise_shaping), AV_OPT_TYPE_INT, { .i64 = 0 },       0, INT_MAX, FF_MPV_OPT_FLAGS },\
{ "error_rate", "Simulate errors in the bitstream to test error concealment.",                                                                                                  \
618 619
                                                                    FF_MPV_OFFSET(error_rate),              AV_OPT_TYPE_INT, { .i64 = 0 },       0, INT_MAX, FF_MPV_OPT_FLAGS },\
{"qsquish", "how to keep quantizer between qmin and qmax (0 = clip, 1 = use differentiable function)",                                                                          \
620 621
                                                                    FF_MPV_OFFSET(rc_qsquish), AV_OPT_TYPE_FLOAT, {.dbl = 0 }, 0, 99, FF_MPV_OPT_FLAGS},                        \
{"rc_qmod_amp", "experimental quantizer modulation",                FF_MPV_OFFSET(rc_qmod_amp), AV_OPT_TYPE_FLOAT, {.dbl = 0 }, -FLT_MAX, FLT_MAX, FF_MPV_OPT_FLAGS},           \
622 623 624 625 626
{"rc_qmod_freq", "experimental quantizer modulation",               FF_MPV_OFFSET(rc_qmod_freq), AV_OPT_TYPE_INT, {.i64 = 0 }, INT_MIN, INT_MAX, FF_MPV_OPT_FLAGS},             \
{"rc_eq", "Set rate control equation. When computing the expression, besides the standard functions "                                                                           \
          "defined in the section 'Expression Evaluation', the following functions are available: "                                                                             \
          "bits2qp(bits), qp2bits(qp). Also the following constants are available: iTex pTex tex mv "                                                                           \
          "fCode iCount mcVar var isI isP isB avgQP qComp avgIITex avgPITex avgPPTex avgBPTex avgTex.",                                                                         \
627 628 629
                                                                    FF_MPV_OFFSET(rc_eq), AV_OPT_TYPE_STRING,                           .flags = FF_MPV_OPT_FLAGS },            \
{"rc_init_cplx", "initial complexity for 1-pass encoding",          FF_MPV_OFFSET(rc_initial_cplx), AV_OPT_TYPE_FLOAT, {.dbl = 0 }, -FLT_MAX, FLT_MAX, FF_MPV_OPT_FLAGS},       \
{"rc_buf_aggressivity", "currently useless",                        FF_MPV_OFFSET(rc_buffer_aggressivity), AV_OPT_TYPE_FLOAT, {.dbl = 1.0 }, -FLT_MAX, FLT_MAX, FF_MPV_OPT_FLAGS}, \
630
{"border_mask", "increase the quantizer for macroblocks close to borders", FF_MPV_OFFSET(border_masking), AV_OPT_TYPE_FLOAT, {.dbl = 0 }, -FLT_MAX, FLT_MAX, FF_MPV_OPT_FLAGS},    \
631 632
{"lmin", "minimum Lagrange factor (VBR)",                           FF_MPV_OFFSET(lmin), AV_OPT_TYPE_INT, {.i64 =  2*FF_QP2LAMBDA }, 0, INT_MAX, FF_MPV_OPT_FLAGS },            \
{"lmax", "maximum Lagrange factor (VBR)",                           FF_MPV_OFFSET(lmax), AV_OPT_TYPE_INT, {.i64 = 31*FF_QP2LAMBDA }, 0, INT_MAX, FF_MPV_OPT_FLAGS },            \
633 634
{"ibias", "intra quant bias",                                       FF_MPV_OFFSET(intra_quant_bias), AV_OPT_TYPE_INT, {.i64 = FF_DEFAULT_QUANT_BIAS }, INT_MIN, INT_MAX, FF_MPV_OPT_FLAGS },   \
{"pbias", "inter quant bias",                                       FF_MPV_OFFSET(inter_quant_bias), AV_OPT_TYPE_INT, {.i64 = FF_DEFAULT_QUANT_BIAS }, INT_MIN, INT_MAX, FF_MPV_OPT_FLAGS },   \
635 636 637
{"rc_strategy", "ratecontrol method",                               FF_MPV_OFFSET(rc_strategy), AV_OPT_TYPE_INT, {.i64 = MPV_RC_STRATEGY_FFMPEG }, 0, NB_MPV_RC_STRATEGY-1, FF_MPV_OPT_FLAGS, "rc_strategy" },   \
    { "ffmpeg", "default native rate control", 0, AV_OPT_TYPE_CONST, { .i64 = MPV_RC_STRATEGY_FFMPEG }, 0, 0, FF_MPV_OPT_FLAGS, "rc_strategy" }, \
    { "xvid",   "libxvid (2 pass only)",       0, AV_OPT_TYPE_CONST, { .i64 = MPV_RC_STRATEGY_XVID },   0, 0, FF_MPV_OPT_FLAGS, "rc_strategy" }, \
638 639 640 641
{"motion_est", "motion estimation algorithm",                       FF_MPV_OFFSET(motion_est), AV_OPT_TYPE_INT, {.i64 = FF_ME_EPZS }, FF_ME_ZERO, FF_ME_XONE, FF_MPV_OPT_FLAGS, "motion_est" },   \
{ "zero", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FF_ME_ZERO }, 0, 0, FF_MPV_OPT_FLAGS, "motion_est" }, \
{ "epzs", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FF_ME_EPZS }, 0, 0, FF_MPV_OPT_FLAGS, "motion_est" }, \
{ "xone", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FF_ME_XONE }, 0, 0, FF_MPV_OPT_FLAGS, "motion_est" }, \
642
{ "force_duplicated_matrix", "Always write luma and chroma matrix for mjpeg, useful for rtp streaming.", FF_MPV_OFFSET(force_duplicated_matrix), AV_OPT_TYPE_BOOL, {.i64 = 0 }, 0, 1, FF_MPV_OPT_FLAGS },   \
643 644
{"b_strategy", "Strategy to choose between I/P/B-frames",           FF_MPV_OFFSET(b_frame_strategy), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 2, FF_MPV_OPT_FLAGS }, \
{"b_sensitivity", "Adjust sensitivity of b_frame_strategy 1",       FF_MPV_OFFSET(b_sensitivity), AV_OPT_TYPE_INT, {.i64 = 40 }, 1, INT_MAX, FF_MPV_OPT_FLAGS }, \
645
{"brd_scale", "Downscale frames for dynamic B-frame decision",      FF_MPV_OFFSET(brd_scale), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 3, FF_MPV_OPT_FLAGS }, \
646 647 648 649
{"skip_threshold", "Frame skip threshold",                          FF_MPV_OFFSET(frame_skip_threshold), AV_OPT_TYPE_INT, {.i64 = 0 }, INT_MIN, INT_MAX, FF_MPV_OPT_FLAGS }, \
{"skip_factor", "Frame skip factor",                                FF_MPV_OFFSET(frame_skip_factor), AV_OPT_TYPE_INT, {.i64 = 0 }, INT_MIN, INT_MAX, FF_MPV_OPT_FLAGS }, \
{"skip_exp", "Frame skip exponent",                                 FF_MPV_OFFSET(frame_skip_exp), AV_OPT_TYPE_INT, {.i64 = 0 }, INT_MIN, INT_MAX, FF_MPV_OPT_FLAGS }, \
{"skip_cmp", "Frame skip compare function",                         FF_MPV_OFFSET(frame_skip_cmp), AV_OPT_TYPE_INT, {.i64 = FF_CMP_DCTMAX }, INT_MIN, INT_MAX, FF_MPV_OPT_FLAGS, "cmp_func" }, \
650
{"sc_threshold", "Scene change threshold",                          FF_MPV_OFFSET(scenechange_threshold), AV_OPT_TYPE_INT, {.i64 = 0 }, INT_MIN, INT_MAX, FF_MPV_OPT_FLAGS }, \
651
{"noise_reduction", "Noise reduction",                              FF_MPV_OFFSET(noise_reduction), AV_OPT_TYPE_INT, {.i64 = 0 }, INT_MIN, INT_MAX, FF_MPV_OPT_FLAGS }, \
652
{"mpeg_quant", "Use MPEG quantizers instead of H.263",              FF_MPV_OFFSET(mpeg_quant), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 1, FF_MPV_OPT_FLAGS }, \
653
{"ps", "RTP payload size in bytes",                             FF_MPV_OFFSET(rtp_payload_size), AV_OPT_TYPE_INT, {.i64 = 0 }, INT_MIN, INT_MAX, FF_MPV_OPT_FLAGS }, \
654
{"mepc", "Motion estimation bitrate penalty compensation (1.0 = 256)", FF_MPV_OFFSET(me_penalty_compensation), AV_OPT_TYPE_INT, {.i64 = 256 }, INT_MIN, INT_MAX, FF_MPV_OPT_FLAGS }, \
655
{"mepre", "pre motion estimation", FF_MPV_OFFSET(me_pre), AV_OPT_TYPE_INT, {.i64 = 0 }, INT_MIN, INT_MAX, FF_MPV_OPT_FLAGS }, \
656 657 658

extern const AVOption ff_mpv_generic_options[];

659 660 661 662 663
/**
 * Set the given MpegEncContext to common defaults (same for encoding
 * and decoding).  The changed fields will not depend upon the prior
 * state of the MpegEncContext.
 */
664 665
void ff_mpv_common_defaults(MpegEncContext *s);

666
void ff_dct_encode_init_x86(MpegEncContext *s);
667

668 669
int ff_mpv_common_init(MpegEncContext *s);
void ff_mpv_common_init_arm(MpegEncContext *s);
670
void ff_mpv_common_init_axp(MpegEncContext *s);
671 672 673
void ff_mpv_common_init_neon(MpegEncContext *s);
void ff_mpv_common_init_ppc(MpegEncContext *s);
void ff_mpv_common_init_x86(MpegEncContext *s);
674
void ff_mpv_common_init_mips(MpegEncContext *s);
675 676 677 678 679

int ff_mpv_common_frame_size_change(MpegEncContext *s);
void ff_mpv_common_end(MpegEncContext *s);

void ff_mpv_decode_defaults(MpegEncContext *s);
680
void ff_mpv_decode_init(MpegEncContext *s, AVCodecContext *avctx);
681 682 683 684 685 686 687 688 689 690 691
void ff_mpv_decode_mb(MpegEncContext *s, int16_t block[12][64]);
void ff_mpv_report_decode_progress(MpegEncContext *s);

int ff_mpv_frame_start(MpegEncContext *s, AVCodecContext *avctx);
void ff_mpv_frame_end(MpegEncContext *s);

int ff_mpv_encode_init(AVCodecContext *avctx);
void ff_mpv_encode_init_x86(MpegEncContext *s);

int ff_mpv_encode_end(AVCodecContext *avctx);
int ff_mpv_encode_picture(AVCodecContext *avctx, AVPacket *pkt,
692
                          const AVFrame *frame, int *got_packet);
693
int ff_mpv_reallocate_putbitbuffer(MpegEncContext *s, size_t threshold, size_t size_increase);
694

695
void ff_clean_intra_table_entries(MpegEncContext *s);
696
void ff_mpeg_draw_horiz_band(MpegEncContext *s, int y, int h);
Michael Niedermayer's avatar
Michael Niedermayer committed
697
void ff_mpeg_flush(AVCodecContext *avctx);
698

699
void ff_print_debug_info(MpegEncContext *s, Picture *p, AVFrame *pict);
700 701
void ff_print_debug_info2(AVCodecContext *avctx, AVFrame *pict, uint8_t *mbskip_table,
                         uint32_t *mbtype_table, int8_t *qscale_table, int16_t (*motion_val[2])[2],
702
                         int *low_delay,
703 704
                         int mb_width, int mb_height, int mb_stride, int quarter_sample);

705 706
int ff_mpv_export_qp_table(MpegEncContext *s, AVFrame *f, Picture *p, int qp_type);

707
void ff_write_quant_matrix(PutBitContext *pb, uint16_t *matrix);
708

709
int ff_update_duplicate_context(MpegEncContext *dst, MpegEncContext *src);
710
int ff_mpeg_update_thread_context(AVCodecContext *dst, const AVCodecContext *src);
711
void ff_set_qscale(MpegEncContext * s, int qscale);
Michael Niedermayer's avatar
Michael Niedermayer committed
712

713
void ff_mpv_idct_init(MpegEncContext *s);
714
int ff_dct_encode_init(MpegEncContext *s);
715
void ff_convert_matrix(MpegEncContext *s, int (*qmat)[64], uint16_t (*qmat16)[2][64],
716
                       const uint16_t *quant_matrix, int bias, int qmin, int qmax, int intra);
Diego Biurrun's avatar
Diego Biurrun committed
717
int ff_dct_quantize_c(MpegEncContext *s, int16_t *block, int n, int qscale, int *overflow);
718 719
void ff_block_permute(int16_t *block, uint8_t *permutation,
                      const uint8_t *scantable, int last);
720
void ff_init_block_index(MpegEncContext *s);
721

722
void ff_mpv_motion(MpegEncContext *s,
723 724 725 726 727 728
                   uint8_t *dest_y, uint8_t *dest_cb,
                   uint8_t *dest_cr, int dir,
                   uint8_t **ref_picture,
                   op_pixels_func (*pix_op)[4],
                   qpel_mc_func (*qpix_op)[16]);

729
static inline void ff_update_block_index(MpegEncContext *s){
Michael Niedermayer's avatar
Michael Niedermayer committed
730
    const int block_size= 8 >> s->avctx->lowres;
731

732 733 734 735 736 737
    s->block_index[0]+=2;
    s->block_index[1]+=2;
    s->block_index[2]+=2;
    s->block_index[3]+=2;
    s->block_index[4]++;
    s->block_index[5]++;
738 739 740
    s->dest[0]+= 2*block_size;
    s->dest[1]+= block_size;
    s->dest[2]+= block_size;
741 742
}

743
static inline int get_bits_diff(MpegEncContext *s){
744
    const int bits= put_bits_count(&s->pb);
745 746 747 748 749 750
    const int last= s->last_bits;

    s->last_bits = bits;

    return bits - last;
}
751

752
#endif /* AVCODEC_MPEGVIDEO_H */