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 "get_bits.h" | ||
34 | #include "h264chroma.h" | ||
35 | #include "h263dsp.h" | ||
36 | #include "hpeldsp.h" | ||
37 | #include "idctdsp.h" | ||
38 | #include "mpegpicture.h" | ||
39 | #include "qpeldsp.h" | ||
40 | #include "videodsp.h" | ||
41 | |||
42 | #define MAX_THREADS 32 | ||
43 | |||
44 | /** | ||
45 | * Scantable. | ||
46 | */ | ||
47 | typedef struct ScanTable { | ||
48 | const uint8_t *scantable; | ||
49 | uint8_t permutated[64]; | ||
50 | uint8_t raster_end[64]; | ||
51 | } ScanTable; | ||
52 | |||
53 | enum OutputFormat { | ||
54 | FMT_MPEG1, | ||
55 | FMT_H261, | ||
56 | FMT_H263, | ||
57 | FMT_MJPEG, | ||
58 | FMT_SPEEDHQ, | ||
59 | }; | ||
60 | |||
61 | /** | ||
62 | * MpegEncContext. | ||
63 | */ | ||
64 | typedef struct MpegEncContext { | ||
65 | AVClass *class; | ||
66 | |||
67 | int y_dc_scale, c_dc_scale; | ||
68 | int ac_pred; | ||
69 | int block_last_index[12]; ///< last non zero coefficient in block | ||
70 | int h263_aic; ///< Advanced INTRA Coding (AIC) | ||
71 | |||
72 | /* scantables */ | ||
73 | ScanTable inter_scantable; ///< if inter == intra then intra should be used to reduce the cache usage | ||
74 | |||
75 | /* WARNING: changes above this line require updates to hardcoded | ||
76 | * offsets used in ASM. */ | ||
77 | |||
78 | ScanTable intra_scantable; | ||
79 | uint8_t permutated_intra_h_scantable[64]; | ||
80 | uint8_t permutated_intra_v_scantable[64]; | ||
81 | |||
82 | struct AVCodecContext *avctx; | ||
83 | union { | ||
84 | const struct MpegEncContext *parent; | ||
85 | const struct MPVMainEncContext *encparent; | ||
86 | }; | ||
87 | /* The following pointer is intended for codecs sharing code | ||
88 | * between decoder and encoder and in need of a common context to do so. */ | ||
89 | void *private_ctx; | ||
90 | /* the following parameters must be initialized before encoding */ | ||
91 | int width, height;///< picture size. must be a multiple of 16 | ||
92 | enum OutputFormat out_format; ///< output format | ||
93 | int h263_pred; ///< use MPEG-4/H.263 ac/dc predictions | ||
94 | int pb_frame; ///< PB-frame mode (0 = none, 1 = base, 2 = improved) | ||
95 | |||
96 | /* the following codec id fields are deprecated in favor of codec_id */ | ||
97 | int h263_plus; ///< H.263+ headers | ||
98 | int h263_flv; ///< use flv H.263 header | ||
99 | |||
100 | enum AVCodecID codec_id; /* see AV_CODEC_ID_xxx */ | ||
101 | int encoding; ///< true if we are encoding (vs decoding) | ||
102 | int workaround_bugs; ///< workaround bugs in encoders which cannot be detected automatically | ||
103 | int codec_tag; ///< internal codec_tag upper case converted from avctx codec_tag | ||
104 | /* the following fields are managed internally by the encoder */ | ||
105 | |||
106 | /* sequence parameters */ | ||
107 | int context_initialized; | ||
108 | int picture_number; //FIXME remove, unclear definition | ||
109 | int mb_width, mb_height; ///< number of MBs horizontally & vertically | ||
110 | int mb_stride; ///< mb_width+1 used for some arrays to allow simple addressing of left & top MBs without sig11 | ||
111 | int b8_stride; ///< 2*mb_width+1 used for some 8x8 block arrays to allow simple addressing | ||
112 | int h_edge_pos, v_edge_pos;///< horizontal / vertical position of the right/bottom edge (pixel replication) | ||
113 | int mb_num; ///< number of MBs of a picture | ||
114 | ptrdiff_t linesize; ///< line size, in bytes, may be different from width | ||
115 | ptrdiff_t uvlinesize; ///< line size, for chroma in bytes, may be different from width | ||
116 | struct AVRefStructPool *picture_pool; ///< Pool for MPVPictures | ||
117 | |||
118 | BufferPoolContext buffer_pools; | ||
119 | |||
120 | int start_mb_y; ///< start mb_y of this thread (so current thread should process start_mb_y <= row < end_mb_y) | ||
121 | int end_mb_y; ///< end mb_y of this thread (so current thread should process start_mb_y <= row < end_mb_y) | ||
122 | union { | ||
123 | struct MpegEncContext *thread_context[MAX_THREADS]; | ||
124 | struct MPVEncContext *enc_contexts[MAX_THREADS]; | ||
125 | }; | ||
126 | int slice_context_count; ///< number of used thread_contexts | ||
127 | |||
128 | /** | ||
129 | * copy of the previous picture structure. | ||
130 | * note, linesize & data, might not match the previous picture (for field pictures) | ||
131 | */ | ||
132 | MPVWorkPicture last_pic; | ||
133 | |||
134 | /** | ||
135 | * copy of the next picture structure. | ||
136 | * note, linesize & data, might not match the next picture (for field pictures) | ||
137 | */ | ||
138 | MPVWorkPicture next_pic; | ||
139 | |||
140 | /** | ||
141 | * copy of the current picture structure. | ||
142 | * note, linesize & data, might not match the current picture (for field pictures) | ||
143 | */ | ||
144 | MPVWorkPicture cur_pic; | ||
145 | |||
146 | int skipped_last_frame; | ||
147 | int last_dc[3]; ///< last DC values for MPEG-1 | ||
148 | int16_t *dc_val_base; | ||
149 | int16_t *dc_val[3]; ///< used for MPEG-4 DC prediction, all 3 arrays must be continuous | ||
150 | const uint8_t *y_dc_scale_table; ///< qscale -> y_dc_scale table | ||
151 | const uint8_t *c_dc_scale_table; ///< qscale -> c_dc_scale table | ||
152 | const uint8_t *chroma_qscale_table; ///< qscale -> chroma_qscale (H.263) | ||
153 | uint8_t *coded_block_base; | ||
154 | uint8_t *coded_block; ///< used for coded block pattern prediction (msmpeg4v3, wmv1) | ||
155 | int16_t (*ac_val_base)[16]; | ||
156 | int16_t (*ac_val[3])[16]; ///< used for MPEG-4 AC prediction, all 3 arrays must be continuous | ||
157 | int mb_skipped; ///< MUST BE SET only during DECODING | ||
158 | uint8_t *mbskip_table; /**< used to avoid copy if macroblock skipped (for black regions for example) | ||
159 | and used for B-frame encoding & decoding (contains skip table of next P-frame) */ | ||
160 | uint8_t *mbintra_table; ///< used to avoid setting {ac, dc, cbp}-pred stuff to zero on inter MB decoding | ||
161 | uint8_t *cbp_table; ///< used to store cbp, ac_pred for partitioned decoding | ||
162 | uint8_t *pred_dir_table; ///< used to store pred_dir for partitioned decoding | ||
163 | |||
164 | ScratchpadContext sc; | ||
165 | |||
166 | int qscale; ///< QP | ||
167 | int chroma_qscale; ///< chroma QP | ||
168 | int pict_type; ///< AV_PICTURE_TYPE_I, AV_PICTURE_TYPE_P, AV_PICTURE_TYPE_B, ... | ||
169 | int droppable; | ||
170 | |||
171 | /* motion compensation */ | ||
172 | int unrestricted_mv; ///< mv can point outside of the coded picture | ||
173 | int h263_long_vectors; ///< use horrible H.263v1 long vector mode | ||
174 | |||
175 | BlockDSPContext bdsp; | ||
176 | H264ChromaContext h264chroma; | ||
177 | HpelDSPContext hdsp; | ||
178 | IDCTDSPContext idsp; | ||
179 | QpelDSPContext qdsp; | ||
180 | VideoDSPContext vdsp; | ||
181 | H263DSPContext h263dsp; | ||
182 | int f_code; ///< forward MV resolution | ||
183 | int b_code; ///< backward MV resolution for B-frames (MPEG-4) | ||
184 | int16_t (*p_field_mv_table_base)[2]; | ||
185 | int16_t (*p_field_mv_table[2][2])[2]; ///< MV table (2MV per MB) interlaced P-frame encoding | ||
186 | |||
187 | int mv_dir; | ||
188 | #define MV_DIR_FORWARD 1 | ||
189 | #define MV_DIR_BACKWARD 2 | ||
190 | #define MV_DIRECT 4 ///< bidirectional mode where the difference equals the MV of the last P/S/I-Frame (MPEG-4) | ||
191 | int mv_type; | ||
192 | #define MV_TYPE_16X16 0 ///< 1 vector for the whole mb | ||
193 | #define MV_TYPE_8X8 1 ///< 4 vectors (H.263, MPEG-4 4MV) | ||
194 | #define MV_TYPE_16X8 2 ///< 2 vectors, one per 16x8 block | ||
195 | #define MV_TYPE_FIELD 3 ///< 2 vectors, one per field | ||
196 | #define MV_TYPE_DMV 4 ///< 2 vectors, special mpeg2 Dual Prime Vectors | ||
197 | /**motion vectors for a macroblock | ||
198 | first coordinate : 0 = forward 1 = backward | ||
199 | second " : depend on type | ||
200 | third " : 0 = x, 1 = y | ||
201 | */ | ||
202 | int mv[2][4][2]; | ||
203 | int field_select[2][2]; | ||
204 | int last_mv[2][2][2]; ///< last MV, used for MV prediction in MPEG-1 & B-frame MPEG-4 | ||
205 | int16_t direct_scale_mv[2][64]; ///< precomputed to avoid divisions in ff_mpeg4_set_direct_mv | ||
206 | |||
207 | int no_rounding; /**< apply no rounding to motion compensation (MPEG-4, msmpeg4, ...) | ||
208 | for B-frames rounding mode is always 0 */ | ||
209 | |||
210 | /* macroblock layer */ | ||
211 | int mb_x, mb_y; | ||
212 | int mb_skip_run; | ||
213 | int mb_intra; | ||
214 | |||
215 | int block_index[6]; ///< index to current MB in block based arrays with edges | ||
216 | int block_wrap[6]; | ||
217 | uint8_t *dest[3]; | ||
218 | |||
219 | int *mb_index2xy; ///< mb_index -> mb_x + mb_y*mb_stride | ||
220 | |||
221 | /** matrix transmitted in the bitstream */ | ||
222 | uint16_t intra_matrix[64]; | ||
223 | uint16_t chroma_intra_matrix[64]; | ||
224 | uint16_t inter_matrix[64]; | ||
225 | uint16_t chroma_inter_matrix[64]; | ||
226 | |||
227 | /* error concealment / resync */ | ||
228 | int resync_mb_x; ///< x position of last resync marker | ||
229 | int resync_mb_y; ///< y position of last resync marker | ||
230 | GetBitContext last_resync_gb; ///< used to search for the next resync marker | ||
231 | int mb_num_left; ///< number of MBs left in this video packet (for partitioned Slices only) | ||
232 | |||
233 | /* H.263 specific */ | ||
234 | int gob_index; | ||
235 | int obmc; ///< overlapped block motion compensation | ||
236 | int ehc_mode; | ||
237 | |||
238 | /* H.263+ specific */ | ||
239 | int umvplus; ///< == H.263+ && unrestricted_mv | ||
240 | int h263_aic_dir; ///< AIC direction: 0 = left, 1 = top | ||
241 | int h263_slice_structured; | ||
242 | int alt_inter_vlc; ///< alternative inter vlc | ||
243 | int modified_quant; | ||
244 | int loop_filter; | ||
245 | int custom_pcf; | ||
246 | |||
247 | /* MPEG-4 specific */ | ||
248 | int studio_profile; | ||
249 | int dct_precision; | ||
250 | int last_time_base; | ||
251 | int time_base; ///< time in seconds of last I,P,S Frame | ||
252 | int64_t time; ///< time of current frame | ||
253 | int64_t last_non_b_time; | ||
254 | uint16_t pp_time; ///< time distance between the last 2 p,s,i frames | ||
255 | uint16_t pb_time; ///< time distance between the last b and p,s,i frame | ||
256 | uint16_t pp_field_time; | ||
257 | uint16_t pb_field_time; ///< like above, just for interlaced | ||
258 | int mcsel; | ||
259 | int quarter_sample; ///< 1->qpel, 0->half pel ME/MC | ||
260 | int data_partitioning; ///< data partitioning flag from header | ||
261 | int partitioned_frame; ///< is current frame partitioned | ||
262 | int low_delay; ///< no reordering needed / has no B-frames | ||
263 | int mpeg_quant; | ||
264 | int padding_bug_score; ///< used to detect the VERY common padding bug in MPEG-4 | ||
265 | |||
266 | /* divx specific, used to workaround (many) bugs in divx5 */ | ||
267 | int divx_packed; | ||
268 | |||
269 | /* RV10 specific */ | ||
270 | int rv10_version; ///< RV10 version: 0 or 3 | ||
271 | int rv10_first_dc_coded[3]; | ||
272 | |||
273 | /* MSMPEG4 specific */ | ||
274 | int slice_height; ///< in macroblocks | ||
275 | int first_slice_line; ///< used in MPEG-4 too to handle resync markers | ||
276 | int flipflop_rounding; | ||
277 | enum { | ||
278 | MSMP4_UNUSED, | ||
279 | MSMP4_V1, | ||
280 | MSMP4_V2, | ||
281 | MSMP4_V3, | ||
282 | MSMP4_WMV1, | ||
283 | MSMP4_WMV2, | ||
284 | MSMP4_VC1, ///< for VC1 (image), WMV3 (image) and MSS2. | ||
285 | } msmpeg4_version; | ||
286 | int inter_intra_pred; | ||
287 | int mspel; | ||
288 | |||
289 | /* decompression specific */ | ||
290 | GetBitContext gb; | ||
291 | |||
292 | /* MPEG-2-specific - I wished not to have to support this mess. */ | ||
293 | int progressive_sequence; | ||
294 | int mpeg_f_code[2][2]; | ||
295 | |||
296 | // picture structure defines are loaded from mpegutils.h | ||
297 | int picture_structure; | ||
298 | |||
299 | int intra_dc_precision; | ||
300 | int frame_pred_frame_dct; | ||
301 | int top_field_first; | ||
302 | int concealment_motion_vectors; | ||
303 | int q_scale_type; | ||
304 | int intra_vlc_format; | ||
305 | int alternate_scan; | ||
306 | int repeat_first_field; | ||
307 | int chroma_420_type; | ||
308 | int chroma_format; | ||
309 | #define CHROMA_420 1 | ||
310 | #define CHROMA_422 2 | ||
311 | #define CHROMA_444 3 | ||
312 | int chroma_x_shift;//depend on pix_format, that depend on chroma_format | ||
313 | int chroma_y_shift; | ||
314 | |||
315 | int progressive_frame; | ||
316 | int full_pel[2]; | ||
317 | int interlaced_dct; | ||
318 | int first_field; ///< is 1 for the first field of a field picture 0 otherwise | ||
319 | |||
320 | int16_t (*block)[64]; ///< points to one of the following blocks | ||
321 | int16_t (*blocks)[12][64]; // for HQ mode we need to keep the best block | ||
322 | int (*decode_mb)(struct MpegEncContext *s, int16_t block[12][64]); // used by some codecs to avoid a switch() | ||
323 | |||
324 | #define SLICE_OK 0 | ||
325 | #define SLICE_ERROR -1 | ||
326 | #define SLICE_END -2 ///<end marker found | ||
327 | #define SLICE_NOEND -3 ///<no end marker or error found but mb count exceeded | ||
328 | |||
329 | void (*dct_unquantize_intra)(struct MpegEncContext *s, // unquantizer to use (MPEG-4 can use both) | ||
330 | int16_t *block/*align 16*/, int n, int qscale); | ||
331 | void (*dct_unquantize_inter)(struct MpegEncContext *s, // unquantizer to use (MPEG-4 can use both) | ||
332 | int16_t *block/*align 16*/, int n, int qscale); | ||
333 | |||
334 | /* flag to indicate a reinitialization is required, e.g. after | ||
335 | * a frame size change */ | ||
336 | int context_reinit; | ||
337 | |||
338 | /// If set, ff_mpv_common_init() will allocate slice contexts of this size | ||
339 | unsigned slice_ctx_size; | ||
340 | |||
341 | ERContext er; | ||
342 | } MpegEncContext; | ||
343 | |||
344 | |||
345 | /** | ||
346 | * Set the given MpegEncContext to common defaults (same for encoding | ||
347 | * and decoding). The changed fields will not depend upon the prior | ||
348 | * state of the MpegEncContext. | ||
349 | */ | ||
350 | void ff_mpv_common_defaults(MpegEncContext *s); | ||
351 | |||
352 | int ff_mpv_common_init(MpegEncContext *s); | ||
353 | /** | ||
354 | * Initialize an MpegEncContext's thread contexts. Presumes that | ||
355 | * slice_context_count is already set and that all the fields | ||
356 | * that are freed/reset in free_duplicate_context() are NULL. | ||
357 | */ | ||
358 | int ff_mpv_init_duplicate_contexts(MpegEncContext *s); | ||
359 | /** | ||
360 | * Initialize and allocates MpegEncContext fields dependent on the resolution. | ||
361 | */ | ||
362 | int ff_mpv_init_context_frame(MpegEncContext *s); | ||
363 | /** | ||
364 | * Frees and resets MpegEncContext fields depending on the resolution | ||
365 | * as well as the slice thread contexts. | ||
366 | * Is used during resolution changes to avoid a full reinitialization of the | ||
367 | * codec. | ||
368 | */ | ||
369 | void ff_mpv_free_context_frame(MpegEncContext *s); | ||
370 | |||
371 | void ff_mpv_common_end(MpegEncContext *s); | ||
372 | |||
373 | void ff_clean_intra_table_entries(MpegEncContext *s); | ||
374 | |||
375 | int ff_update_duplicate_context(MpegEncContext *dst, const MpegEncContext *src); | ||
376 | void ff_set_qscale(MpegEncContext * s, int qscale); | ||
377 | |||
378 | void ff_mpv_idct_init(MpegEncContext *s); | ||
379 | void ff_init_scantable(const uint8_t *permutation, ScanTable *st, | ||
380 | const uint8_t *src_scantable); | ||
381 | void ff_init_block_index(MpegEncContext *s); | ||
382 | |||
383 | void ff_mpv_motion(MpegEncContext *s, | ||
384 | uint8_t *dest_y, uint8_t *dest_cb, | ||
385 | uint8_t *dest_cr, int dir, | ||
386 | uint8_t *const *ref_picture, | ||
387 | const op_pixels_func (*pix_op)[4], | ||
388 | const qpel_mc_func (*qpix_op)[16]); | ||
389 | |||
390 | 6752801 | static inline void ff_update_block_index(MpegEncContext *s, int bits_per_raw_sample, | |
391 | int lowres, int chroma_x_shift) | ||
392 | { | ||
393 |
2/2✓ Branch 0 taken 1350 times.
✓ Branch 1 taken 6751451 times.
|
6752801 | const int bytes_per_pixel = 1 + (bits_per_raw_sample > 8); |
394 | 6752801 | const int block_size = (8 * bytes_per_pixel) >> lowres; | |
395 | |||
396 | 6752801 | s->block_index[0]+=2; | |
397 | 6752801 | s->block_index[1]+=2; | |
398 | 6752801 | s->block_index[2]+=2; | |
399 | 6752801 | s->block_index[3]+=2; | |
400 | 6752801 | s->block_index[4]++; | |
401 | 6752801 | s->block_index[5]++; | |
402 | 6752801 | s->dest[0]+= 2*block_size; | |
403 | 6752801 | s->dest[1] += (2 >> chroma_x_shift) * block_size; | |
404 | 6752801 | s->dest[2] += (2 >> chroma_x_shift) * block_size; | |
405 | 6752801 | } | |
406 | |||
407 | #endif /* AVCODEC_MPEGVIDEO_H */ | ||
408 |