FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mpegvideo.h
Date: 2025-07-14 21:09:40
Exec Total Coverage
Lines: 13 13 100.0%
Functions: 1 1 100.0%
Branches: 2 2 100.0%

Line Branch Exec Source
1 /*
2 * Generic DCT based hybrid video encoder
3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
4 * Copyright (c) 2002-2004 Michael Niedermayer
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
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
21 */
22
23 /**
24 * @file
25 * mpegvideo header.
26 */
27
28 #ifndef AVCODEC_MPEGVIDEO_H
29 #define AVCODEC_MPEGVIDEO_H
30
31 #include "blockdsp.h"
32 #include "error_resilience.h"
33 #include "h264chroma.h"
34 #include "h263dsp.h"
35 #include "hpeldsp.h"
36 #include "idctdsp.h"
37 #include "mpegpicture.h"
38 #include "qpeldsp.h"
39 #include "videodsp.h"
40
41 #define MAX_THREADS 32
42
43 /**
44 * Scantable.
45 */
46 typedef struct ScanTable {
47 const uint8_t *scantable;
48 uint8_t permutated[64];
49 uint8_t raster_end[64];
50 } ScanTable;
51
52 enum OutputFormat {
53 FMT_MPEG1,
54 FMT_H261,
55 FMT_H263,
56 FMT_MJPEG,
57 FMT_SPEEDHQ,
58 };
59
60 /**
61 * MpegEncContext.
62 */
63 typedef struct MpegEncContext {
64 AVClass *class;
65
66 int y_dc_scale, c_dc_scale;
67 int ac_pred;
68 int block_last_index[12]; ///< last non zero coefficient in block
69 int h263_aic; ///< Advanced INTRA Coding (AIC)
70
71 /* scantables */
72 ScanTable inter_scantable; ///< if inter == intra then intra should be used to reduce the cache usage
73
74 /* WARNING: changes above this line require updates to hardcoded
75 * offsets used in ASM. */
76
77 ScanTable intra_scantable;
78 uint8_t permutated_intra_h_scantable[64];
79 uint8_t permutated_intra_v_scantable[64];
80
81 struct AVCodecContext *avctx;
82 /* The following pointer is intended for codecs sharing code
83 * between decoder and encoder and in need of a common context to do so. */
84 void *private_ctx;
85 /* the following parameters must be initialized before encoding */
86 int width, height;///< picture size. must be a multiple of 16
87 enum OutputFormat out_format; ///< output format
88 int h263_pred; ///< use MPEG-4/H.263 ac/dc predictions
89
90 enum AVCodecID codec_id; /* see AV_CODEC_ID_xxx */
91 int encoding; ///< true if we are encoding (vs decoding)
92 int workaround_bugs; ///< workaround bugs in encoders which cannot be detected automatically
93 int codec_tag; ///< internal codec_tag upper case converted from avctx codec_tag
94 /* the following fields are managed internally by the encoder */
95
96 /* sequence parameters */
97 int context_initialized;
98 int mb_width, mb_height; ///< number of MBs horizontally & vertically
99 int mb_stride; ///< mb_width+1 used for some arrays to allow simple addressing of left & top MBs without sig11
100 int b8_stride; ///< 2*mb_width+1 used for some 8x8 block arrays to allow simple addressing
101 int h_edge_pos, v_edge_pos;///< horizontal / vertical position of the right/bottom edge (pixel replication)
102 int mb_num; ///< number of MBs of a picture
103 ptrdiff_t linesize; ///< line size, in bytes, may be different from width
104 ptrdiff_t uvlinesize; ///< line size, for chroma in bytes, may be different from width
105 struct AVRefStructPool *picture_pool; ///< Pool for MPVPictures
106
107 BufferPoolContext buffer_pools;
108
109 int start_mb_y; ///< start mb_y of this thread (so current thread should process start_mb_y <= row < end_mb_y)
110 int end_mb_y; ///< end mb_y of this thread (so current thread should process start_mb_y <= row < end_mb_y)
111 union {
112 struct MpegEncContext *thread_context[MAX_THREADS];
113 struct Mpeg12SliceContext *mpeg12_contexts[MAX_THREADS];
114 struct MPVEncContext *enc_contexts[MAX_THREADS];
115 };
116 int slice_context_count; ///< number of used thread_contexts
117
118 /**
119 * copy of the previous picture structure.
120 * note, linesize & data, might not match the previous picture (for field pictures)
121 */
122 MPVWorkPicture last_pic;
123
124 /**
125 * copy of the next picture structure.
126 * note, linesize & data, might not match the next picture (for field pictures)
127 */
128 MPVWorkPicture next_pic;
129
130 /**
131 * copy of the current picture structure.
132 * note, linesize & data, might not match the current picture (for field pictures)
133 */
134 MPVWorkPicture cur_pic;
135
136 int last_dc[3]; ///< last DC values for MPEG-1
137 int16_t *dc_val_base;
138 const uint8_t *y_dc_scale_table; ///< qscale -> y_dc_scale table
139 const uint8_t *c_dc_scale_table; ///< qscale -> c_dc_scale table
140 const uint8_t *chroma_qscale_table; ///< qscale -> chroma_qscale (H.263)
141 uint8_t *coded_block_base;
142 uint8_t *coded_block; ///< used for coded block pattern prediction (msmpeg4v3, wmv1)
143 int16_t (*ac_val_base)[16];
144 int16_t *dc_val; ///< used for H.263 AIC/MPEG-4 DC prediction and ER
145 int16_t (*ac_val)[16]; ///< used for H.263 AIC, MPEG-4 AC prediction
146 int mb_skipped; ///< MUST BE SET only during DECODING
147 uint8_t *mbskip_table; /**< used to avoid copy if macroblock skipped (for black regions for example)
148 and used for B-frame encoding & decoding (contains skip table of next P-frame) */
149 uint8_t *mbintra_table; ///< used to avoid setting {ac, dc, cbp}-pred stuff to zero on inter MB decoding
150 uint8_t *cbp_table; ///< used to store cbp, ac_pred for partitioned decoding
151 uint8_t *pred_dir_table; ///< used to store pred_dir for partitioned decoding
152
153 ScratchpadContext sc;
154
155 int qscale; ///< QP
156 int chroma_qscale; ///< chroma QP
157 int pict_type; ///< AV_PICTURE_TYPE_I, AV_PICTURE_TYPE_P, AV_PICTURE_TYPE_B, ...
158 int droppable;
159
160 BlockDSPContext bdsp;
161 H264ChromaContext h264chroma;
162 HpelDSPContext hdsp;
163 IDCTDSPContext idsp;
164 QpelDSPContext qdsp;
165 VideoDSPContext vdsp;
166 H263DSPContext h263dsp;
167 int16_t (*p_field_mv_table_base)[2];
168 int16_t (*p_field_mv_table[2][2])[2]; ///< MV table (2MV per MB) interlaced P-frame encoding
169
170 int mv_dir;
171 #define MV_DIR_FORWARD 1
172 #define MV_DIR_BACKWARD 2
173 #define MV_DIRECT 4 ///< bidirectional mode where the difference equals the MV of the last P/S/I-Frame (MPEG-4)
174 int mv_type;
175 #define MV_TYPE_16X16 0 ///< 1 vector for the whole mb
176 #define MV_TYPE_8X8 1 ///< 4 vectors (H.263, MPEG-4 4MV)
177 #define MV_TYPE_16X8 2 ///< 2 vectors, one per 16x8 block
178 #define MV_TYPE_FIELD 3 ///< 2 vectors, one per field
179 #define MV_TYPE_DMV 4 ///< 2 vectors, special mpeg2 Dual Prime Vectors
180 /**motion vectors for a macroblock
181 first coordinate : 0 = forward 1 = backward
182 second " : depend on type
183 third " : 0 = x, 1 = y
184 */
185 int mv[2][4][2];
186 int field_select[2][2];
187 int last_mv[2][2][2]; ///< last MV, used for MV prediction in MPEG-1 & B-frame MPEG-4
188 int16_t direct_scale_mv[2][64]; ///< precomputed to avoid divisions in ff_mpeg4_set_direct_mv
189
190 int no_rounding; /**< apply no rounding to motion compensation (MPEG-4, msmpeg4, ...)
191 for B-frames rounding mode is always 0 */
192
193 /* macroblock layer */
194 int mb_x, mb_y;
195 int mb_intra;
196
197 int block_index[6]; ///< index to current MB in block based arrays with edges
198 int block_wrap[6];
199 uint8_t *dest[3];
200
201 int *mb_index2xy; ///< mb_index -> mb_x + mb_y*mb_stride
202
203 /** matrix transmitted in the bitstream */
204 uint16_t intra_matrix[64];
205 uint16_t chroma_intra_matrix[64];
206 uint16_t inter_matrix[64];
207 uint16_t chroma_inter_matrix[64];
208
209 /* error concealment / resync */
210 int resync_mb_x; ///< x position of last resync marker
211 int resync_mb_y; ///< y position of last resync marker
212
213 /* H.263 specific */
214 int obmc; ///< overlapped block motion compensation
215
216 /* H.263+ specific */
217 int h263_aic_dir; ///< AIC direction: 0 = left, 1 = top
218
219 /* MPEG-4 specific */
220 int studio_profile;
221 int last_time_base;
222 int time_base; ///< time in seconds of last I,P,S Frame
223 int64_t time; ///< time of current frame
224 int64_t last_non_b_time;
225 uint16_t pp_time; ///< time distance between the last 2 p,s,i frames
226 uint16_t pb_time; ///< time distance between the last b and p,s,i frame
227 uint16_t pp_field_time;
228 uint16_t pb_field_time; ///< like above, just for interlaced
229 int mcsel;
230 int quarter_sample; ///< 1->qpel, 0->half pel ME/MC
231 int low_delay; ///< no reordering needed / has no B-frames
232
233 /* MSMPEG4 specific */
234 int first_slice_line; ///< used in MPEG-4 too to handle resync markers
235 enum {
236 MSMP4_UNUSED,
237 MSMP4_V1,
238 MSMP4_V2,
239 MSMP4_V3,
240 MSMP4_WMV1,
241 MSMP4_WMV2,
242 MSMP4_VC1, ///< for VC1 (image), WMV3 (image) and MSS2.
243 } msmpeg4_version;
244 int inter_intra_pred;
245 int mspel;
246
247 /* MPEG-2-specific - I wished not to have to support this mess. */
248 int progressive_sequence;
249 int mpeg_f_code[2][2];
250
251 // picture structure defines are loaded from mpegutils.h
252 int picture_structure;
253
254 int intra_dc_precision;
255 int frame_pred_frame_dct;
256 int top_field_first;
257 int concealment_motion_vectors;
258 int q_scale_type;
259 int intra_vlc_format;
260 int alternate_scan;
261 int repeat_first_field;
262 int chroma_420_type;
263 int chroma_format;
264 #define CHROMA_420 1
265 #define CHROMA_422 2
266 #define CHROMA_444 3
267 int chroma_x_shift;//depend on pix_format, that depend on chroma_format
268 int chroma_y_shift;
269
270 int progressive_frame;
271 int full_pel[2];
272 int interlaced_dct;
273 int first_field; ///< is 1 for the first field of a field picture 0 otherwise
274
275 void (*dct_unquantize_intra)(struct MpegEncContext *s, // unquantizer to use (MPEG-4 can use both)
276 int16_t *block/*align 16*/, int n, int qscale);
277 void (*dct_unquantize_inter)(struct MpegEncContext *s, // unquantizer to use (MPEG-4 can use both)
278 int16_t *block/*align 16*/, int n, int qscale);
279
280 /* flag to indicate a reinitialization is required, e.g. after
281 * a frame size change */
282 int context_reinit;
283
284 /// If set, ff_mpv_common_init() will allocate slice contexts of this size
285 unsigned slice_ctx_size;
286
287 ERContext er;
288 } MpegEncContext;
289
290 typedef MpegEncContext MPVContext;
291
292 /**
293 * Set the given MpegEncContext to common defaults (same for encoding
294 * and decoding). The changed fields will not depend upon the prior
295 * state of the MpegEncContext.
296 */
297 void ff_mpv_common_defaults(MpegEncContext *s);
298
299 int ff_mpv_common_init(MpegEncContext *s);
300 /**
301 * Initialize an MpegEncContext's thread contexts. Presumes that
302 * slice_context_count is already set and that all the fields
303 * that are freed/reset in free_duplicate_context() are NULL.
304 */
305 int ff_mpv_init_duplicate_contexts(MpegEncContext *s);
306 /**
307 * Initialize and allocates MpegEncContext fields dependent on the resolution.
308 */
309 int ff_mpv_init_context_frame(MpegEncContext *s);
310 /**
311 * Frees and resets MpegEncContext fields depending on the resolution
312 * as well as the slice thread contexts.
313 * Is used during resolution changes to avoid a full reinitialization of the
314 * codec.
315 */
316 void ff_mpv_free_context_frame(MpegEncContext *s);
317
318 void ff_mpv_common_end(MpegEncContext *s);
319
320 void ff_clean_intra_table_entries(MpegEncContext *s);
321
322 int ff_update_duplicate_context(MpegEncContext *dst, const MpegEncContext *src);
323 void ff_set_qscale(MpegEncContext * s, int qscale);
324
325 void ff_mpv_idct_init(MpegEncContext *s);
326 void ff_init_scantable(const uint8_t *permutation, ScanTable *st,
327 const uint8_t *src_scantable);
328 void ff_init_block_index(MpegEncContext *s);
329
330 void ff_mpv_motion(MpegEncContext *s,
331 uint8_t *dest_y, uint8_t *dest_cb,
332 uint8_t *dest_cr, int dir,
333 uint8_t *const *ref_picture,
334 const op_pixels_func (*pix_op)[4],
335 const qpel_mc_func (*qpix_op)[16]);
336
337 6748129 static inline void ff_update_block_index(MpegEncContext *s, int bits_per_raw_sample,
338 int lowres, int chroma_x_shift)
339 {
340
2/2
✓ Branch 0 taken 1350 times.
✓ Branch 1 taken 6746779 times.
6748129 const int bytes_per_pixel = 1 + (bits_per_raw_sample > 8);
341 6748129 const int block_size = (8 * bytes_per_pixel) >> lowres;
342
343 6748129 s->block_index[0]+=2;
344 6748129 s->block_index[1]+=2;
345 6748129 s->block_index[2]+=2;
346 6748129 s->block_index[3]+=2;
347 6748129 s->block_index[4]++;
348 6748129 s->block_index[5]++;
349 6748129 s->dest[0]+= 2*block_size;
350 6748129 s->dest[1] += (2 >> chroma_x_shift) * block_size;
351 6748129 s->dest[2] += (2 >> chroma_x_shift) * block_size;
352 6748129 }
353
354 #endif /* AVCODEC_MPEGVIDEO_H */
355