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