lavc: Move brd_scale to codec private options
[libav.git] / libavcodec / mpegvideo.h
CommitLineData
de6d9b64
FB
1/*
2 * Generic DCT based hybrid video encoder
406792e7 3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
8f2ab833 4 * Copyright (c) 2002-2004 Michael Niedermayer
de6d9b64 5 *
2912e87a 6 * This file is part of Libav.
b78e7197 7 *
2912e87a 8 * Libav is free software; you can redistribute it and/or
ff4ec49e
FB
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
b78e7197 11 * version 2.1 of the License, or (at your option) any later version.
de6d9b64 12 *
2912e87a 13 * Libav is distributed in the hope that it will be useful,
de6d9b64 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ff4ec49e
FB
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
de6d9b64 17 *
ff4ec49e 18 * You should have received a copy of the GNU Lesser General Public
2912e87a 19 * License along with Libav; if not, write to the Free Software
5509bffa 20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
de6d9b64
FB
21 */
22
983e3246 23/**
ba87f080 24 * @file
983e3246
MN
25 * mpegvideo header.
26 */
115329f1 27
98790382
SS
28#ifndef AVCODEC_MPEGVIDEO_H
29#define AVCODEC_MPEGVIDEO_H
cd4af68a 30
946f9535
AK
31#include <float.h>
32
657ccb5a 33#include "avcodec.h"
e74433a8 34#include "blockdsp.h"
54974c62 35#include "error_resilience.h"
a9aee08d 36#include "fdctdsp.h"
9106a698 37#include "get_bits.h"
0338c396 38#include "h263dsp.h"
f4fed5a2 39#include "hpeldsp.h"
e3fcb143 40#include "idctdsp.h"
21c90d86 41#include "internal.h"
2d604443 42#include "me_cmp.h"
149fa0b7 43#include "motion_est.h"
7061bf09 44#include "mpegpicture.h"
fab9df63 45#include "mpegvideodsp.h"
8d686ca5 46#include "mpegvideoencdsp.h"
f46bb608 47#include "pixblockdsp.h"
b2755007 48#include "put_bits.h"
0de9926f 49#include "ratecontrol.h"
4067d81b 50#include "parser.h"
925b80d6 51#include "mpegutils.h"
3bfe9260 52#include "mpeg12data.h"
368f5035 53#include "qpeldsp.h"
759001c5 54#include "thread.h"
8c53d39e 55#include "videodsp.h"
5c91a675 56
ed019b8e
AK
57#include "libavutil/opt.h"
58
e8b78541 59#define MAX_THREADS 16
9c3d33d6 60
aa241229
AK
61#define MAX_B_FRAMES 16
62
7f50d4ac
AJ
63/* Start codes. */
64#define SEQ_END_CODE 0x000001b7
65#define SEQ_START_CODE 0x000001b3
66#define GOP_START_CODE 0x000001b8
67#define PICTURE_START_CODE 0x00000100
68#define SLICE_MIN_START_CODE 0x00000101
69#define SLICE_MAX_START_CODE 0x000001af
70#define EXT_START_CODE 0x000001b5
71#define USER_START_CODE 0x000001b2
72
277f4827 73/**
277f4827
MN
74 * MpegEncContext.
75 */
de6d9b64 76typedef struct MpegEncContext {
297d9cb3 77 AVClass *class;
6a13505c
AK
78
79 int y_dc_scale, c_dc_scale;
80 int ac_pred;
81 int block_last_index[12]; ///< last non zero coefficient in block
82 int h263_aic; ///< Advanded INTRA Coding (AIC)
83
84 /* scantables */
85 ScanTable inter_scantable; ///< if inter == intra then intra should be used to reduce tha cache usage
86 ScanTable intra_scantable;
87 ScanTable intra_h_scantable;
88 ScanTable intra_v_scantable;
89
90 /* WARNING: changes above this line require updates to hardcoded
91 * offsets used in asm. */
92
58f26ba9 93 struct AVCodecContext *avctx;
de6d9b64 94 /* the following parameters must be initialized before encoding */
115329f1 95 int width, height;///< picture size. must be a multiple of 16
de6d9b64 96 int gop_size;
115329f1
DB
97 int intra_only; ///< if true, only intra pictures are generated
98 int bit_rate; ///< wanted bit rate
99 enum OutputFormat out_format; ///< output format
100 int h263_pred; ///< use mpeg4/h263 ac/dc predictions
91ba181a 101 int pb_frame; ///< PB frame mode (0 = none, 1 = base, 2 = improved)
d7e9533a
MN
102
103/* the following codec id fields are deprecated in favor of codec_id */
115329f1 104 int h263_plus; ///< h263 plus headers
115329f1
DB
105 int h263_flv; ///< use flv h263 header
106
36ef5369 107 enum AVCodecID codec_id; /* see AV_CODEC_ID_xxx */
115329f1
DB
108 int fixed_qscale; ///< fixed qscale if non zero
109 int encoding; ///< true if we are encoding (vs decoding)
115329f1 110 int max_b_frames; ///< max number of b-frames for encoding
7f2fe444
MN
111 int luma_elim_threshold;
112 int chroma_elim_threshold;
115329f1
DB
113 int strict_std_compliance; ///< strictly follow the std (MPEG4, ...)
114 int workaround_bugs; ///< workaround bugs in encoders which cannot be detected automatically
17662955 115 int codec_tag; ///< internal codec_tag upper case converted from avctx codec_tag
de6d9b64
FB
116 /* the following fields are managed internally by the encoder */
117
de6d9b64
FB
118 /* sequence parameters */
119 int context_initialized;
755bfeab
DB
120 int input_picture_number; ///< used to set pic->display_picture_number, should not be used for/by anything else
121 int coded_picture_number; ///< used to set pic->coded_picture_number, should not be used for/by anything else
1031fabd 122 int picture_number; //FIXME remove, unclear definition
115329f1 123 int picture_in_gop_number; ///< 0-> first pic in gop, ...
115329f1 124 int mb_width, mb_height; ///< number of MBs horizontally & vertically
bb628dae
DB
125 int mb_stride; ///< mb_width+1 used for some arrays to allow simple addressing of left & top MBs without sig11
126 int b8_stride; ///< 2*mb_width+1 used for some 8x8 block arrays to allow simple addressing
bb628dae 127 int h_edge_pos, v_edge_pos;///< horizontal / vertical position of the right/bottom edge (pixel replication)
115329f1 128 int mb_num; ///< number of MBs of a picture
93f30547
RB
129 ptrdiff_t linesize; ///< line size, in bytes, may be different from width
130 ptrdiff_t uvlinesize; ///< line size, for chroma in bytes, may be different from width
115329f1 131 Picture *picture; ///< main picture buffer
9d9e3172
MN
132 Picture **input_picture; ///< next pictures on display order for encoding
133 Picture **reordered_input_picture; ///< pointer to the next pictures in codedorder for encoding
115329f1 134
187105ff 135 int64_t user_specified_pts; ///< last non-zero pts from AVFrame which was passed into avcodec_encode_video2()
445a7d48
AK
136 /**
137 * pts difference between the first and second input frame, used for
138 * calculating dts of the first frame when there's a delay */
139 int64_t dts_delta;
140 /**
141 * reordered pts to be used as dts for the next output frame when there's
142 * a delay */
143 int64_t reordered_pts;
d0a2f0af 144
d461a473
MR
145 /** bit output */
146 PutBitContext pb;
147
9c3d33d6
MN
148 int start_mb_y; ///< start mb_y of this thread (so current thread should process start_mb_y <= row < end_mb_y)
149 int end_mb_y; ///< end mb_y of this thread (so current thread should process start_mb_y <= row < end_mb_y)
150 struct MpegEncContext *thread_context[MAX_THREADS];
881a5e04 151 int slice_context_count; ///< number of used thread_contexts
115329f1
DB
152
153 /**
b536d0aa
MN
154 * copy of the previous picture structure.
155 * note, linesize & data, might not match the previous picture (for field pictures)
156 */
115329f1
DB
157 Picture last_picture;
158
159 /**
b536d0aa
MN
160 * copy of the next picture structure.
161 * note, linesize & data, might not match the next picture (for field pictures)
162 */
163 Picture next_picture;
115329f1
DB
164
165 /**
b536d0aa
MN
166 * copy of the source picture structure for encoding.
167 * note, linesize & data, might not match the source picture (for field pictures)
168 */
169 Picture new_picture;
115329f1
DB
170
171 /**
b536d0aa
MN
172 * copy of the current picture structure.
173 * note, linesize & data, might not match the current picture (for field pictures)
174 */
115329f1
DB
175 Picture current_picture; ///< buffer to store the decompressed current picture
176
b536d0aa 177 Picture *last_picture_ptr; ///< pointer to the previous picture.
115329f1 178 Picture *next_picture_ptr; ///< pointer to the next picture (for bidir pred)
b536d0aa 179 Picture *current_picture_ptr; ///< pointer to the current picture
115329f1 180 int last_dc[3]; ///< last DC values for MPEG1
b86216de
MR
181 int16_t *dc_val_base;
182 int16_t *dc_val[3]; ///< used for mpeg4 DC prediction, all 3 arrays must be continuous
115329f1
DB
183 const uint8_t *y_dc_scale_table; ///< qscale -> y_dc_scale table
184 const uint8_t *c_dc_scale_table; ///< qscale -> c_dc_scale table
332f9ac4 185 const uint8_t *chroma_qscale_table; ///< qscale -> chroma_qscale (h263)
137c8468 186 uint8_t *coded_block_base;
277f4827 187 uint8_t *coded_block; ///< used for coded block pattern prediction (msmpeg4v3, wmv1)
137c8468 188 int16_t (*ac_val_base)[16];
115329f1 189 int16_t (*ac_val[3])[16]; ///< used for for mpeg4 AC prediction, all 3 arrays must be continuous
115329f1
DB
190 int mb_skipped; ///< MUST BE SET only during DECODING
191 uint8_t *mbskip_table; /**< used to avoid copy if macroblock skipped (for black regions for example)
9dbcbd92 192 and used for b-frame encoding & decoding (contains skip table of next P Frame) */
115329f1
DB
193 uint8_t *mbintra_table; ///< used to avoid setting {ac, dc, cbp}-pred stuff to zero on inter MB decoding
194 uint8_t *cbp_table; ///< used to store cbp, ac_pred for partitioned decoding
195 uint8_t *pred_dir_table; ///< used to store pred_dir for partitioned decoding
da0c8664
VG
196
197 ScratchpadContext sc;
de6d9b64 198
115329f1
DB
199 int qscale; ///< QP
200 int chroma_qscale; ///< chroma QP
973cbc2a
MN
201 unsigned int lambda; ///< lagrange multipler used in rate distortion
202 unsigned int lambda2; ///< (lambda*lambda) >> FF_LAMBDA_SHIFT
158c7f05 203 int *lambda_table;
115329f1
DB
204 int adaptive_quant; ///< use adaptive quantization
205 int dquant; ///< qscale difference to prev qscale
975a1447 206 int pict_type; ///< AV_PICTURE_TYPE_I, AV_PICTURE_TYPE_P, AV_PICTURE_TYPE_B, ...
14e2a940 207 int last_pict_type; //FIXME removes
115329f1 208 int last_non_b_pict_type; ///< used for mpeg4 gmc b-frames & ratecontrol
ba0c8981 209 int droppable;
de6d9b64 210 int frame_rate_index;
88e51e1f 211 int last_lambda_for[5]; ///< last lambda for a specific pict type
c52f5d66 212 int skipdct; ///< skip dct and code zero residual
5e5c247a 213
de6d9b64 214 /* motion compensation */
115329f1
DB
215 int unrestricted_mv; ///< mv can point outside of the coded picture
216 int h263_long_vectors; ///< use horrible h263v1 long vector mode
277f4827 217
e74433a8 218 BlockDSPContext bdsp;
a9aee08d 219 FDCTDSPContext fdsp;
f4fed5a2 220 HpelDSPContext hdsp;
e3fcb143 221 IDCTDSPContext idsp;
2d604443 222 MECmpContext mecc;
fab9df63 223 MpegVideoDSPContext mdsp;
8d686ca5 224 MpegvideoEncDSPContext mpvencdsp;
f46bb608 225 PixblockDSPContext pdsp;
368f5035 226 QpelDSPContext qdsp;
8c53d39e 227 VideoDSPContext vdsp;
0338c396 228 H263DSPContext h263dsp;
115329f1
DB
229 int f_code; ///< forward MV resolution
230 int b_code; ///< backward MV resolution for B Frames (mpeg4)
7bc9090a
MN
231 int16_t (*p_mv_table_base)[2];
232 int16_t (*b_forw_mv_table_base)[2];
233 int16_t (*b_back_mv_table_base)[2];
115329f1
DB
234 int16_t (*b_bidir_forw_mv_table_base)[2];
235 int16_t (*b_bidir_back_mv_table_base)[2];
7bc9090a 236 int16_t (*b_direct_mv_table_base)[2];
bb198e19
MN
237 int16_t (*p_field_mv_table_base[2][2])[2];
238 int16_t (*b_field_mv_table_base[2][2][2])[2];
115329f1
DB
239 int16_t (*p_mv_table)[2]; ///< MV table (1MV per MB) p-frame encoding
240 int16_t (*b_forw_mv_table)[2]; ///< MV table (1MV per MB) forward mode b-frame encoding
241 int16_t (*b_back_mv_table)[2]; ///< MV table (1MV per MB) backward mode b-frame encoding
242 int16_t (*b_bidir_forw_mv_table)[2]; ///< MV table (1MV per MB) bidir mode b-frame encoding
243 int16_t (*b_bidir_back_mv_table)[2]; ///< MV table (1MV per MB) bidir mode b-frame encoding
244 int16_t (*b_direct_mv_table)[2]; ///< MV table (1MV per MB) direct mode b-frame encoding
bb198e19
MN
245 int16_t (*p_field_mv_table[2][2])[2]; ///< MV table (2MV per MB) interlaced p-frame encoding
246 int16_t (*b_field_mv_table[2][2][2])[2];///< MV table (4MV per MB) interlaced b-frame encoding
247 uint8_t (*p_field_select_table[2]);
248 uint8_t (*b_field_select_table[2][2]);
4b6b1082 249#if FF_API_MOTION_EST
115329f1 250 int me_method; ///< ME algorithm
4b6b1082
VG
251#endif
252 int motion_est; ///< ME algorithm
de6d9b64 253 int mv_dir;
653f7387
MN
254#define MV_DIR_FORWARD 1
255#define MV_DIR_BACKWARD 2
277f4827 256#define MV_DIRECT 4 ///< bidirectional mode where the difference equals the MV of the last P/S/I-Frame (mpeg4)
de6d9b64 257 int mv_type;
115329f1
DB
258#define MV_TYPE_16X16 0 ///< 1 vector for the whole mb
259#define MV_TYPE_8X8 1 ///< 4 vectors (h263, mpeg4 4MV)
260#define MV_TYPE_16X8 2 ///< 2 vectors, one per 16x8 block
261#define MV_TYPE_FIELD 3 ///< 2 vectors, one per field
262#define MV_TYPE_DMV 4 ///< 2 vectors, special mpeg2 Dual Prime Vectors
263 /**motion vectors for a macroblock
de6d9b64
FB
264 first coordinate : 0 = forward 1 = backward
265 second " : depend on type
266 third " : 0 = x, 1 = y
267 */
268 int mv[2][4][2];
269 int field_select[2][2];
115329f1
DB
270 int last_mv[2][2][2]; ///< last MV, used for MV prediction in MPEG1 & B-frame MPEG4
271 uint8_t *fcode_tab; ///< smallest fcode needed for each MV
841f65f2 272 int16_t direct_scale_mv[2][64]; ///< precomputed to avoid divisions in ff_mpeg4_set_direct_mv
115329f1 273
1457ab52 274 MotionEstContext me;
de6d9b64 275
115329f1 276 int no_rounding; /**< apply no rounding to motion compensation (MPEG4, msmpeg4, ...)
e42dba48 277 for b-frames rounding mode is always 0 */
de6d9b64
FB
278
279 /* macroblock layer */
280 int mb_x, mb_y;
9b8709d1 281 int mb_skip_run;
de6d9b64 282 int mb_intra;
e0c16e4e 283 uint16_t *mb_type; ///< Table for candidate MB types for encoding (defines in mpegutils.h)
2f16af06 284
277f4827 285 int block_index[6]; ///< index to current MB in block based arrays with edges
4278e7a6 286 int block_wrap[6];
7d1c3fc1 287 uint8_t *dest[3];
115329f1 288
7bc9090a 289 int *mb_index2xy; ///< mb_index -> mb_x + mb_y*mb_stride
4278e7a6 290
277f4827 291 /** matrix transmitted in the bitstream */
0c1a9eda
ZK
292 uint16_t intra_matrix[64];
293 uint16_t chroma_intra_matrix[64];
294 uint16_t inter_matrix[64];
295 uint16_t chroma_inter_matrix[64];
2b6ab3a2 296
115329f1
DB
297 int intra_quant_bias; ///< bias for the quantizer
298 int inter_quant_bias; ///< bias for the quantizer
299 int min_qcoeff; ///< minimum encodable coefficient
300 int max_qcoeff; ///< maximum encodable coefficient
301 int ac_esc_length; ///< num of bits needed to encode the longest esc
477ab036
MN
302 uint8_t *intra_ac_vlc_length;
303 uint8_t *intra_ac_vlc_last_length;
304 uint8_t *inter_ac_vlc_length;
305 uint8_t *inter_ac_vlc_last_length;
67725183 306 uint8_t *luma_dc_vlc_length;
c442d75c 307#define UNI_AC_ENC_INDEX(run,level) ((run)*128 + (level))
477ab036 308
2d974017 309 int coded_score[8];
f2f6134b 310
277f4827 311 /** precomputed matrix (combine qscale and DCT renorm) */
7e4995c3
MN
312 int (*q_intra_matrix)[64];
313 int (*q_inter_matrix)[64];
642ccefb
MN
314 /** identical to the above but for MMX & these are not permutated, second 64 entries are bias*/
315 uint16_t (*q_intra_matrix16)[2][64];
316 uint16_t (*q_inter_matrix16)[2][64];
115329f1 317
821cb11f
MN
318 /* noise reduction */
319 int (*dct_error_sum)[64];
320 int dct_count[2];
321 uint16_t (*dct_offset)[64];
de6d9b64 322
de6d9b64 323 /* bit rate control */
0c1a9eda 324 int64_t total_bits;
115329f1 325 int frame_bits; ///< bits used for the current frame
07506002 326 int next_lambda; ///< next lambda used for retrying to encode a frame
277f4827 327 RateControlContext rc_context; ///< contains stuff only accessed in ratecontrol.c
9cdd6a24 328
098eefe1
MN
329 /* statistics, used for 2-pass encoding */
330 int mv_bits;
331 int header_bits;
332 int i_tex_bits;
333 int p_tex_bits;
334 int i_count;
66370d3f
MN
335 int f_count;
336 int b_count;
098eefe1 337 int skip_count;
277f4827
MN
338 int misc_bits; ///< cbp, mb_type
339 int last_bits; ///< temp var used for calculating the above vars
115329f1 340
7f2fe444 341 /* error concealment / resync */
115329f1
DB
342 int resync_mb_x; ///< x position of last resync marker
343 int resync_mb_y; ///< y position of last resync marker
344 GetBitContext last_resync_gb; ///< used to search for the next resync marker
277f4827 345 int mb_num_left; ///< number of MBs left in this video packet (for partitioned Slices only)
115329f1 346 int next_p_frame_damaged; ///< set if the next p frame is damaged, to avoid showing trashed b frames
115329f1 347
d7425f59 348 ParseContext parse_context;
098eefe1 349
102d3908 350 /* H.263 specific */
644d98a4 351 int gob_index;
dba019da 352 int obmc; ///< overlapped block motion compensation
bdc1220e
MS
353 int mb_info; ///< interval for outputting info about mb offsets as side data
354 int prev_mb_info, last_mb_info;
355 uint8_t *mb_info_ptr;
356 int mb_info_size;
1761ab83 357 int rc_strategy;
115329f1 358
544286b3 359 /* H.263+ specific */
115329f1 360 int umvplus; ///< == H263+ && unrestricted_mv
ba58dabc
MN
361 int h263_aic_dir; ///< AIC direction: 0 = left, 1 = top
362 int h263_slice_structured;
dba019da 363 int alt_inter_vlc; ///< alternative inter vlc
68b94c35 364 int modified_quant;
115329f1 365 int loop_filter;
b056e73c 366 int custom_pcf;
115329f1 367
de6d9b64 368 /* mpeg4 specific */
e62a43f6
AK
369 ///< number of bits to represent the fractional part of time (encoder only)
370 int time_increment_bits;
9dbcbd92 371 int last_time_base;
115329f1
DB
372 int time_base; ///< time in seconds of last I,P,S Frame
373 int64_t time; ///< time of current frame
0c1a9eda 374 int64_t last_non_b_time;
115329f1
DB
375 uint16_t pp_time; ///< time distance between the last 2 p,s,i frames
376 uint16_t pb_time; ///< time distance between the last b and p,s,i frame
0c1a9eda 377 uint16_t pp_field_time;
115329f1 378 uint16_t pb_field_time; ///< like above, just for interlaced
44eb4951 379 int real_sprite_warping_points;
115329f1
DB
380 int sprite_offset[2][2]; ///< sprite offset[isChroma][isMVY]
381 int sprite_delta[2][2]; ///< sprite_delta [isY][isMVY]
73c8e514 382 int mcsel;
1a565432 383 int quant_precision;
115329f1 384 int quarter_sample; ///< 1->qpel, 0->half pel ME/MC
0da71265 385 int aspect_ratio_info; //FIXME remove
cc9ba006 386 int sprite_warping_accuracy;
115329f1
DB
387 int data_partitioning; ///< data partitioning flag from header
388 int partitioned_frame; ///< is current frame partitioned
115329f1 389 int low_delay; ///< no reordering needed / has no b-frames
1ff662cc 390 int vo_type;
115329f1
DB
391 PutBitContext tex_pb; ///< used for data partitioned VOPs
392 PutBitContext pb2; ///< used for data partitioned VOPs
d930ef19 393 int mpeg_quant;
115329f1 394 int padding_bug_score; ///< used to detect the VERY common padding bug in MPEG4
44eb4951
MN
395
396 /* divx specific, used to workaround (many) bugs in divx5 */
d5a21172 397 int divx_packed;
0c1a9eda 398 uint8_t *bitstream_buffer; //Divx 5.01 puts several frames in a single one, this is used to reorder them
eec1c6b9 399 int bitstream_buffer_size;
f038fe8b 400 unsigned int allocated_bitstream_buffer_size;
115329f1 401
de6d9b64 402 /* RV10 specific */
115329f1 403 int rv10_version; ///< RV10 version: 0 or 3
de6d9b64 404 int rv10_first_dc_coded[3];
115329f1 405
de6d9b64
FB
406 /* MJPEG specific */
407 struct MJpegContext *mjpeg_ctx;
408
409 /* MSMPEG4 specific */
410 int mv_table_index;
411 int rl_table_index;
412 int rl_chroma_table_index;
413 int dc_table_index;
414 int use_skip_mb_code;
115329f1
DB
415 int slice_height; ///< in macroblocks
416 int first_slice_line; ///< used in mpeg4 too to handle resync markers
ae40484c 417 int flipflop_rounding;
277f4827 418 int msmpeg4_version; ///< 0=not msmpeg4, 1=mp41, 2=mp42, 3=mp43/divx3 4=wmv1/7 5=wmv2/8
0151a6f5
MN
419 int per_mb_rl_table;
420 int esc3_level_length;
421 int esc3_run_length;
277f4827 422 /** [mb_intra][isChroma][level][run][last] */
6b460aa3 423 int (*ac_stats)[2][MAX_LEVEL+1][MAX_RUN+1][2];
de0f2f4c 424 int inter_intra_pred;
1457ab52 425 int mspel;
0151a6f5 426
de6d9b64
FB
427 /* decompression specific */
428 GetBitContext gb;
429
1e491e29 430 /* Mpeg1 specific */
115329f1
DB
431 int gop_picture_number; ///< index of the first picture of a GOP based on fake_pic_num & mpeg1 specific
432 int last_mv_dir; ///< last mv_dir, used for b frame encoding
115329f1
DB
433 uint8_t *vbv_delay_ptr; ///< pointer to vbv_delay in the bitstream
434
7eb2d654 435 /* MPEG-2-specific - I wished not to have to support this mess. */
de6d9b64
FB
436 int progressive_sequence;
437 int mpeg_f_code[2][2];
e0c16e4e
VG
438
439 // picture structure defines are loaded from mpegutils.h
de6d9b64 440 int picture_structure;
de6d9b64
FB
441
442 int intra_dc_precision;
443 int frame_pred_frame_dct;
444 int top_field_first;
445 int concealment_motion_vectors;
446 int q_scale_type;
84c4714f 447 int brd_scale;
de6d9b64
FB
448 int intra_vlc_format;
449 int alternate_scan;
450 int repeat_first_field;
451 int chroma_420_type;
5e5c247a
IK
452 int chroma_format;
453#define CHROMA_420 1
454#define CHROMA_422 2
ffdff4d7
IK
455 int chroma_x_shift;//depend on pix_format, that depend on chroma_format
456 int chroma_y_shift;
5e5c247a 457
de6d9b64 458 int progressive_frame;
cd8c64e1 459 int full_pel[2];
de6d9b64 460 int interlaced_dct;
c3bf0288 461 int first_field; ///< is 1 for the first field of a field picture 0 otherwise
2c5e1efc 462 int drop_frame_timecode; ///< timecode is in drop frame format.
aed79007 463 int scan_offset; ///< reserve space for SVCD scan offset user data.
ba58dabc 464
644d98a4 465 /* RTP specific */
e4eadb4b 466 int rtp_mode;
115329f1 467
0c1a9eda 468 uint8_t *ptr_lastgob;
88bd7fdc 469 int16_t (*pblocks[12])[64];
115329f1 470
88bd7fdc 471 int16_t (*block)[64]; ///< points to one of the following blocks
f349f4b5 472 int16_t (*blocks)[12][64]; // for HQ mode we need to keep the best block
88bd7fdc 473 int (*decode_mb)(struct MpegEncContext *s, int16_t block[6][64]); // used by some codecs to avoid a switch()
4d2858de
MN
474#define SLICE_OK 0
475#define SLICE_ERROR -1
c3bf0288
MN
476#define SLICE_END -2 ///<end marker found
477#define SLICE_NOEND -3 ///<no end marker or error found but mb count exceeded
115329f1
DB
478
479 void (*dct_unquantize_mpeg1_intra)(struct MpegEncContext *s,
88bd7fdc 480 int16_t *block/*align 16*/, int n, int qscale);
115329f1 481 void (*dct_unquantize_mpeg1_inter)(struct MpegEncContext *s,
88bd7fdc 482 int16_t *block/*align 16*/, int n, int qscale);
115329f1 483 void (*dct_unquantize_mpeg2_intra)(struct MpegEncContext *s,
88bd7fdc 484 int16_t *block/*align 16*/, int n, int qscale);
115329f1 485 void (*dct_unquantize_mpeg2_inter)(struct MpegEncContext *s,
88bd7fdc 486 int16_t *block/*align 16*/, int n, int qscale);
115329f1 487 void (*dct_unquantize_h263_intra)(struct MpegEncContext *s,
88bd7fdc 488 int16_t *block/*align 16*/, int n, int qscale);
115329f1 489 void (*dct_unquantize_h263_inter)(struct MpegEncContext *s,
88bd7fdc 490 int16_t *block/*align 16*/, int n, int qscale);
d50635cd 491 void (*dct_unquantize_intra)(struct MpegEncContext *s, // unquantizer to use (mpeg4 can use both)
88bd7fdc 492 int16_t *block/*align 16*/, int n, int qscale);
d50635cd 493 void (*dct_unquantize_inter)(struct MpegEncContext *s, // unquantizer to use (mpeg4 can use both)
88bd7fdc
DB
494 int16_t *block/*align 16*/, int n, int qscale);
495 int (*dct_quantize)(struct MpegEncContext *s, int16_t *block/*align 16*/, int n, int qscale, int *overflow);
496 int (*fast_dct_quantize)(struct MpegEncContext *s, int16_t *block/*align 16*/, int n, int qscale, int *overflow);
497 void (*denoise_dct)(struct MpegEncContext *s, int16_t *block);
ed019b8e
AK
498
499 int mpv_flags; ///< flags set by private options
23bfcc06 500 int quantizer_noise_shaping;
89b81a1c 501
a6e4ce9f
AK
502 /**
503 * ratecontrol qmin qmax limiting method
504 * 0-> clipping, 1-> use a nice continuous function to limit qscale wthin qmin/qmax.
505 */
506 float rc_qsquish;
946f9535
AK
507 float rc_qmod_amp;
508 int rc_qmod_freq;
66a68ddd
AK
509 float rc_initial_cplx;
510 float rc_buffer_aggressivity;
15ec053c 511 float border_masking;
530c1441 512 int lmin, lmax;
a6e4ce9f 513
a75c2eb2
AK
514 char *rc_eq;
515
89b81a1c
RB
516 /* temp buffers for rate control */
517 float *cplx_tab, *bits_tab;
8701f4f8
JG
518
519 /* flag to indicate a reinitialization is required, e.g. after
520 * a frame size change */
521 int context_reinit;
54974c62
AK
522
523 ERContext er;
8941971a
AK
524
525 int error_rate;
1c01b025
AK
526
527 /* temporary frames used by b_frame_strategy = 2 */
528 AVFrame *tmp_frames[MAX_B_FRAMES + 2];
0e6c8532
VG
529 int b_frame_strategy;
530 int b_sensitivity;
de6d9b64
FB
531} MpegEncContext;
532
ed019b8e
AK
533/* mpegvideo_enc common options */
534#define FF_MPV_FLAG_SKIP_RD 0x0001
a249f0cc 535#define FF_MPV_FLAG_STRICT_GOP 0x0002
ff71a383 536#define FF_MPV_FLAG_QP_RD 0x0004
af3d804f 537#define FF_MPV_FLAG_CBP_RD 0x0008
b2c31710 538#define FF_MPV_FLAG_NAQ 0x0010
a2941c8c 539#define FF_MPV_FLAG_MV0 0x0020
ed019b8e
AK
540
541#define FF_MPV_OFFSET(x) offsetof(MpegEncContext, x)
542#define FF_MPV_OPT_FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
543#define FF_MPV_COMMON_OPTS \
d58dd4b5 544{ "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" },\
124134e4
MS
545{ "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" },\
546{ "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" },\
547{ "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" },\
548{ "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" },\
b2c31710 549{ "naq", "normalize adaptive quantization", 0, AV_OPT_TYPE_CONST, { .i64 = FF_MPV_FLAG_NAQ }, 0, 0, FF_MPV_OPT_FLAGS, "mpv_flags" },\
a2941c8c 550{ "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" },\
63efd83a 551{ "luma_elim_threshold", "single coefficient elimination threshold for luminance (negative values also consider dc coefficient)",\
e6153f17 552 FF_MPV_OFFSET(luma_elim_threshold), AV_OPT_TYPE_INT, { .i64 = 0 }, INT_MIN, INT_MAX, FF_MPV_OPT_FLAGS },\
63efd83a 553{ "chroma_elim_threshold", "single coefficient elimination threshold for chrominance (negative values also consider dc coefficient)",\
e6153f17 554 FF_MPV_OFFSET(chroma_elim_threshold), AV_OPT_TYPE_INT, { .i64 = 0 }, INT_MIN, INT_MAX, FF_MPV_OPT_FLAGS },\
8941971a
AK
555{ "quantizer_noise_shaping", NULL, FF_MPV_OFFSET(quantizer_noise_shaping), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FF_MPV_OPT_FLAGS },\
556{ "error_rate", "Simulate errors in the bitstream to test error concealment.", \
a6e4ce9f
AK
557 FF_MPV_OFFSET(error_rate), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FF_MPV_OPT_FLAGS },\
558{"qsquish", "how to keep quantizer between qmin and qmax (0 = clip, 1 = use differentiable function)", \
946f9535
AK
559 FF_MPV_OFFSET(rc_qsquish), AV_OPT_TYPE_FLOAT, {.dbl = 0 }, 0, 99, FF_MPV_OPT_FLAGS}, \
560{"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}, \
a75c2eb2
AK
561{"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}, \
562{"rc_eq", "Set rate control equation. When computing the expression, besides the standard functions " \
563 "defined in the section 'Expression Evaluation', the following functions are available: " \
564 "bits2qp(bits), qp2bits(qp). Also the following constants are available: iTex pTex tex mv " \
565 "fCode iCount mcVar var isI isP isB avgQP qComp avgIITex avgPITex avgPPTex avgBPTex avgTex.", \
66a68ddd
AK
566 FF_MPV_OFFSET(rc_eq), AV_OPT_TYPE_STRING, .flags = FF_MPV_OPT_FLAGS }, \
567{"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}, \
568{"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}, \
15ec053c 569{"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}, \
530c1441
AK
570{"lmin", "minimum Lagrange factor (VBR)", FF_MPV_OFFSET(lmin), AV_OPT_TYPE_INT, {.i64 = 2*FF_QP2LAMBDA }, 0, INT_MAX, FF_MPV_OPT_FLAGS }, \
571{"lmax", "maximum Lagrange factor (VBR)", FF_MPV_OFFSET(lmax), AV_OPT_TYPE_INT, {.i64 = 31*FF_QP2LAMBDA }, 0, INT_MAX, FF_MPV_OPT_FLAGS }, \
910247f1
VG
572{"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 }, \
573{"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 }, \
1761ab83 574{"rc_strategy", "ratecontrol method", FF_MPV_OFFSET(rc_strategy), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 1, FF_MPV_OPT_FLAGS }, \
4b6b1082
VG
575{"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" }, \
576{ "zero", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FF_ME_ZERO }, 0, 0, FF_MPV_OPT_FLAGS, "motion_est" }, \
577{ "epzs", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FF_ME_EPZS }, 0, 0, FF_MPV_OPT_FLAGS, "motion_est" }, \
578{ "xone", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FF_ME_XONE }, 0, 0, FF_MPV_OPT_FLAGS, "motion_est" }, \
0e6c8532
VG
579{"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 }, \
580{"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 }, \
84c4714f 581{"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 }, \
ed019b8e
AK
582
583extern const AVOption ff_mpv_generic_options[];
584
7a851153
MR
585/**
586 * Set the given MpegEncContext to common defaults (same for encoding
587 * and decoding). The changed fields will not depend upon the prior
588 * state of the MpegEncContext.
589 */
835f798c
DB
590void ff_mpv_common_defaults(MpegEncContext *s);
591
592int ff_mpv_common_init(MpegEncContext *s);
593void ff_mpv_common_init_arm(MpegEncContext *s);
594void ff_mpv_common_init_neon(MpegEncContext *s);
595void ff_mpv_common_init_ppc(MpegEncContext *s);
596void ff_mpv_common_init_x86(MpegEncContext *s);
597
598int ff_mpv_common_frame_size_change(MpegEncContext *s);
599void ff_mpv_common_end(MpegEncContext *s);
600
601void ff_mpv_decode_defaults(MpegEncContext *s);
602void ff_mpv_decode_mb(MpegEncContext *s, int16_t block[12][64]);
603void ff_mpv_report_decode_progress(MpegEncContext *s);
604
605int ff_mpv_frame_start(MpegEncContext *s, AVCodecContext *avctx);
606void ff_mpv_frame_end(MpegEncContext *s);
607
835f798c
DB
608int ff_mpv_encode_init(AVCodecContext *avctx);
609void ff_mpv_encode_init_x86(MpegEncContext *s);
610
611int ff_mpv_encode_end(AVCodecContext *avctx);
612int ff_mpv_encode_picture(AVCodecContext *avctx, AVPacket *pkt,
445a7d48 613 const AVFrame *frame, int *got_packet);
835f798c 614
7f2fe444 615void ff_clean_intra_table_entries(MpegEncContext *s);
1d0feb5d 616void ff_mpeg_draw_horiz_band(MpegEncContext *s, int y, int h);
7a06ff14 617void ff_mpeg_flush(AVCodecContext *avctx);
759001c5 618void ff_print_debug_info(MpegEncContext *s, Picture *p);
191e8ca7 619void ff_write_quant_matrix(PutBitContext *pb, uint16_t *matrix);
4e17946f 620
f1d8763a 621int ff_update_duplicate_context(MpegEncContext *dst, MpegEncContext *src);
6a9c8594 622int ff_mpeg_update_thread_context(AVCodecContext *dst, const AVCodecContext *src);
bb174ff1 623void ff_set_qscale(MpegEncContext * s, int qscale);
59b571c1 624
998c9f15 625void ff_mpv_idct_init(MpegEncContext *s);
5dd8c08f 626void ff_convert_matrix(MpegEncContext *s, int (*qmat)[64], uint16_t (*qmat16)[2][64],
69cea75f 627 const uint16_t *quant_matrix, int bias, int qmin, int qmax, int intra);
88bd7fdc 628int ff_dct_quantize_c(MpegEncContext *s, int16_t *block, int n, int qscale, int *overflow);
46b4feec 629
7d1c3fc1 630void ff_init_block_index(MpegEncContext *s);
4d2858de 631
835f798c 632void ff_mpv_motion(MpegEncContext *s,
7a851153
MR
633 uint8_t *dest_y, uint8_t *dest_cb,
634 uint8_t *dest_cr, int dir,
635 uint8_t **ref_picture,
636 op_pixels_func (*pix_op)[4],
637 qpel_mc_func (*qpix_op)[16]);
638
4d2858de 639static inline void ff_update_block_index(MpegEncContext *s){
2bcbd984 640 const int block_size = 8;
178fcca8 641
4d2858de
MN
642 s->block_index[0]+=2;
643 s->block_index[1]+=2;
644 s->block_index[2]+=2;
645 s->block_index[3]+=2;
646 s->block_index[4]++;
647 s->block_index[5]++;
178fcca8
MN
648 s->dest[0]+= 2*block_size;
649 s->dest[1]+= block_size;
650 s->dest[2]+= block_size;
4d2858de
MN
651}
652
4d2a4834 653static inline int get_bits_diff(MpegEncContext *s){
fe455f33 654 const int bits= put_bits_count(&s->pb);
4d2a4834
MN
655 const int last= s->last_bits;
656
657 s->last_bits = bits;
658
659 return bits - last;
660}
4d2858de 661
98790382 662#endif /* AVCODEC_MPEGVIDEO_H */