| 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 | #include "libavutil/mem_internal.h" | ||
| 42 | |||
| 43 | #define MAX_THREADS 32 | ||
| 44 | |||
| 45 | /** | ||
| 46 | * Scantable. | ||
| 47 | */ | ||
| 48 | typedef struct ScanTable { | ||
| 49 | const uint8_t *scantable; | ||
| 50 | uint8_t permutated[64]; | ||
| 51 | uint8_t raster_end[64]; | ||
| 52 | } ScanTable; | ||
| 53 | |||
| 54 | enum OutputFormat { | ||
| 55 | FMT_MPEG1, | ||
| 56 | FMT_H261, | ||
| 57 | FMT_H263, | ||
| 58 | FMT_MJPEG, | ||
| 59 | FMT_SPEEDHQ, | ||
| 60 | }; | ||
| 61 | |||
| 62 | typedef struct MpegEncContext MPVContext; | ||
| 63 | |||
| 64 | /** | ||
| 65 | * MpegEncContext. | ||
| 66 | */ | ||
| 67 | typedef struct MpegEncContext { | ||
| 68 | AVClass *class; | ||
| 69 | |||
| 70 | int y_dc_scale, c_dc_scale; | ||
| 71 | int ac_pred; | ||
| 72 | int block_last_index[12]; ///< last non zero coefficient in block | ||
| 73 | int h263_aic; ///< Advanced INTRA Coding (AIC) | ||
| 74 | |||
| 75 | /* scantables */ | ||
| 76 | ScanTable inter_scantable; ///< if inter == intra then intra should be used to reduce the cache usage | ||
| 77 | ScanTable intra_scantable; | ||
| 78 | |||
| 79 | /* WARNING: changes above this line require updates to hardcoded | ||
| 80 | * offsets used in ASM. */ | ||
| 81 | |||
| 82 | struct AVCodecContext *avctx; | ||
| 83 | /* the following parameters must be initialized before encoding */ | ||
| 84 | int width, height;///< picture size. must be a multiple of 16 | ||
| 85 | enum OutputFormat out_format; ///< output format | ||
| 86 | int h263_pred; ///< use MPEG-4/H.263 ac/dc predictions | ||
| 87 | |||
| 88 | enum AVCodecID codec_id; /* see AV_CODEC_ID_xxx */ | ||
| 89 | int encoding; ///< true if we are encoding (vs decoding) | ||
| 90 | int workaround_bugs; ///< workaround bugs in encoders which cannot be detected automatically | ||
| 91 | int codec_tag; ///< internal codec_tag upper case converted from avctx codec_tag | ||
| 92 | /* the following fields are managed internally by the encoder */ | ||
| 93 | |||
| 94 | /* sequence parameters */ | ||
| 95 | int context_initialized; | ||
| 96 | int mb_width, mb_height; ///< number of MBs horizontally & vertically | ||
| 97 | int mb_stride; ///< mb_width+1 used for some arrays to allow simple addressing of left & top MBs without sig11 | ||
| 98 | int b8_stride; ///< 2*mb_width+1 used for some 8x8 block arrays to allow simple addressing | ||
| 99 | int h_edge_pos, v_edge_pos;///< horizontal / vertical position of the right/bottom edge (pixel replication) | ||
| 100 | int mb_num; ///< number of MBs of a picture | ||
| 101 | ptrdiff_t linesize; ///< line size, in bytes, may be different from width | ||
| 102 | ptrdiff_t uvlinesize; ///< line size, for chroma in bytes, may be different from width | ||
| 103 | struct AVRefStructPool *picture_pool; ///< Pool for MPVPictures | ||
| 104 | |||
| 105 | BufferPoolContext buffer_pools; | ||
| 106 | |||
| 107 | int start_mb_y; ///< start mb_y of this thread (so current thread should process start_mb_y <= row < end_mb_y) | ||
| 108 | int end_mb_y; ///< end mb_y of this thread (so current thread should process start_mb_y <= row < end_mb_y) | ||
| 109 | union { | ||
| 110 | struct MpegEncContext *thread_context[MAX_THREADS]; | ||
| 111 | struct Mpeg12SliceContext *mpeg12_contexts[MAX_THREADS]; | ||
| 112 | struct MPVEncContext *enc_contexts[MAX_THREADS]; | ||
| 113 | }; | ||
| 114 | int slice_context_count; ///< number of used thread_contexts | ||
| 115 | |||
| 116 | /** | ||
| 117 | * copy of the previous picture structure. | ||
| 118 | * note, linesize & data, might not match the previous picture (for field pictures) | ||
| 119 | */ | ||
| 120 | MPVWorkPicture last_pic; | ||
| 121 | |||
| 122 | /** | ||
| 123 | * copy of the next picture structure. | ||
| 124 | * note, linesize & data, might not match the next picture (for field pictures) | ||
| 125 | */ | ||
| 126 | MPVWorkPicture next_pic; | ||
| 127 | |||
| 128 | /** | ||
| 129 | * copy of the current picture structure. | ||
| 130 | * note, linesize & data, might not match the current picture (for field pictures) | ||
| 131 | */ | ||
| 132 | MPVWorkPicture cur_pic; | ||
| 133 | |||
| 134 | int16_t *dc_val_base; | ||
| 135 | const uint8_t *y_dc_scale_table; ///< qscale -> y_dc_scale table | ||
| 136 | const uint8_t *c_dc_scale_table; ///< qscale -> c_dc_scale table | ||
| 137 | const uint8_t *chroma_qscale_table; ///< qscale -> chroma_qscale (H.263) | ||
| 138 | uint8_t *coded_block_base; | ||
| 139 | uint8_t *coded_block; ///< used for coded block pattern prediction (msmpeg4v3, wmv1) | ||
| 140 | int16_t (*ac_val_base)[16]; | ||
| 141 | int16_t *dc_val; ///< used for H.263 AIC/MPEG-4 DC prediction and ER | ||
| 142 | int16_t (*ac_val)[16]; ///< used for H.263 AIC, MPEG-4 AC prediction | ||
| 143 | int mb_skipped; ///< MUST BE SET only during DECODING | ||
| 144 | uint8_t *mbskip_table; /**< used to avoid copy if macroblock skipped (for black regions for example) | ||
| 145 | and used for B-frame encoding & decoding (contains skip table of next P-frame) */ | ||
| 146 | uint8_t *mbintra_table; ///< used to avoid setting {ac, dc, cbp}-pred stuff to zero on inter MB decoding | ||
| 147 | uint8_t *cbp_table; ///< used to store cbp, ac_pred for partitioned decoding | ||
| 148 | uint8_t *pred_dir_table; ///< used to store pred_dir for partitioned decoding | ||
| 149 | |||
| 150 | ScratchpadContext sc; | ||
| 151 | |||
| 152 | int qscale; ///< QP | ||
| 153 | int chroma_qscale; ///< chroma QP | ||
| 154 | enum AVPictureType pict_type; ///< AV_PICTURE_TYPE_I, AV_PICTURE_TYPE_P, AV_PICTURE_TYPE_B, ... | ||
| 155 | int droppable; | ||
| 156 | |||
| 157 | BlockDSPContext bdsp; | ||
| 158 | H264ChromaContext h264chroma; | ||
| 159 | HpelDSPContext hdsp; | ||
| 160 | IDCTDSPContext idsp; | ||
| 161 | QpelDSPContext qdsp; | ||
| 162 | VideoDSPContext vdsp; | ||
| 163 | H263DSPContext h263dsp; | ||
| 164 | int16_t (*p_field_mv_table_base)[2]; | ||
| 165 | int16_t (*p_field_mv_table[2][2])[2]; ///< MV table (2MV per MB) interlaced P-frame encoding | ||
| 166 | |||
| 167 | int mv_dir; | ||
| 168 | #define MV_DIR_FORWARD 1 | ||
| 169 | #define MV_DIR_BACKWARD 2 | ||
| 170 | #define MV_DIRECT 4 ///< bidirectional mode where the difference equals the MV of the last P/S/I-Frame (MPEG-4) | ||
| 171 | int mv_type; | ||
| 172 | #define MV_TYPE_16X16 0 ///< 1 vector for the whole mb | ||
| 173 | #define MV_TYPE_8X8 1 ///< 4 vectors (H.263, MPEG-4 4MV) | ||
| 174 | #define MV_TYPE_16X8 2 ///< 2 vectors, one per 16x8 block | ||
| 175 | #define MV_TYPE_FIELD 3 ///< 2 vectors, one per field | ||
| 176 | #define MV_TYPE_DMV 4 ///< 2 vectors, special mpeg2 Dual Prime Vectors | ||
| 177 | /**motion vectors for a macroblock | ||
| 178 | first coordinate : 0 = forward 1 = backward | ||
| 179 | second " : depend on type | ||
| 180 | third " : 0 = x, 1 = y | ||
| 181 | */ | ||
| 182 | int mv[2][4][2]; | ||
| 183 | int field_select[2][2]; | ||
| 184 | int last_mv[2][2][2]; ///< last MV, used for MV prediction in MPEG-1 & B-frame MPEG-4 | ||
| 185 | int16_t direct_scale_mv[2][64]; ///< precomputed to avoid divisions in ff_mpeg4_set_direct_mv | ||
| 186 | |||
| 187 | int no_rounding; /**< apply no rounding to motion compensation (MPEG-4, msmpeg4, ...) | ||
| 188 | for B-frames rounding mode is always 0 */ | ||
| 189 | |||
| 190 | /* macroblock layer */ | ||
| 191 | int mb_x, mb_y; | ||
| 192 | int mb_intra; | ||
| 193 | |||
| 194 | int block_index[6]; ///< index to current MB in block based arrays with edges | ||
| 195 | int block_wrap[6]; | ||
| 196 | uint8_t *dest[3]; | ||
| 197 | |||
| 198 | int *mb_index2xy; ///< mb_index -> mb_x + mb_y*mb_stride | ||
| 199 | |||
| 200 | /** matrix transmitted in the bitstream */ | ||
| 201 | DECLARE_ALIGNED(16, uint16_t, intra_matrix)[64]; | ||
| 202 | DECLARE_ALIGNED(16, uint16_t, chroma_intra_matrix)[64]; | ||
| 203 | DECLARE_ALIGNED(16, uint16_t, inter_matrix)[64]; | ||
| 204 | DECLARE_ALIGNED(16, uint16_t, chroma_inter_matrix)[64]; | ||
| 205 | |||
| 206 | /* error concealment / resync */ | ||
| 207 | int resync_mb_x; ///< x position of last resync marker | ||
| 208 | int resync_mb_y; ///< y position of last resync marker | ||
| 209 | |||
| 210 | /* H.261 specific */ | ||
| 211 | int mtype; | ||
| 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)(const MPVContext *s, // unquantizer to use (MPEG-4 can use both) | ||
| 276 | int16_t *block/*align 16*/, int n, int qscale); | ||
| 277 | void (*dct_unquantize_inter)(const MPVContext *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 | /** | ||
| 291 | * Set the given MpegEncContext to common defaults (same for encoding | ||
| 292 | * and decoding). The changed fields will not depend upon the prior | ||
| 293 | * state of the MpegEncContext. | ||
| 294 | */ | ||
| 295 | void ff_mpv_common_defaults(MpegEncContext *s); | ||
| 296 | |||
| 297 | int ff_mpv_common_init(MpegEncContext *s); | ||
| 298 | /** | ||
| 299 | * Initialize an MpegEncContext's thread contexts. Presumes that | ||
| 300 | * slice_context_count is already set and that all the fields | ||
| 301 | * that are freed/reset in free_duplicate_context() are NULL. | ||
| 302 | */ | ||
| 303 | int ff_mpv_init_duplicate_contexts(MpegEncContext *s); | ||
| 304 | /** | ||
| 305 | * Initialize and allocates MpegEncContext fields dependent on the resolution. | ||
| 306 | */ | ||
| 307 | int ff_mpv_init_context_frame(MpegEncContext *s); | ||
| 308 | /** | ||
| 309 | * Frees and resets MpegEncContext fields depending on the resolution | ||
| 310 | * as well as the slice thread contexts. | ||
| 311 | * Is used during resolution changes to avoid a full reinitialization of the | ||
| 312 | * codec. | ||
| 313 | */ | ||
| 314 | void ff_mpv_free_context_frame(MpegEncContext *s); | ||
| 315 | |||
| 316 | void ff_mpv_common_end(MpegEncContext *s); | ||
| 317 | |||
| 318 | void ff_clean_intra_table_entries(MpegEncContext *s); | ||
| 319 | |||
| 320 | int ff_update_duplicate_context(MpegEncContext *dst, const MpegEncContext *src); | ||
| 321 | void ff_set_qscale(MpegEncContext * s, int qscale); | ||
| 322 | |||
| 323 | void ff_mpv_idct_init(MpegEncContext *s); | ||
| 324 | void ff_init_scantable(const uint8_t *permutation, ScanTable *st, | ||
| 325 | const uint8_t *src_scantable); | ||
| 326 | void ff_init_block_index(MpegEncContext *s); | ||
| 327 | |||
| 328 | void ff_mpv_motion(MpegEncContext *s, | ||
| 329 | uint8_t *dest_y, uint8_t *dest_cb, | ||
| 330 | uint8_t *dest_cr, int dir, | ||
| 331 | uint8_t *const *ref_picture, | ||
| 332 | const op_pixels_func (*pix_op)[4], | ||
| 333 | const qpel_mc_func (*qpix_op)[16]); | ||
| 334 | |||
| 335 | 7026009 | static inline void ff_update_block_index(MpegEncContext *s, int bits_per_raw_sample, | |
| 336 | int lowres, int chroma_x_shift) | ||
| 337 | { | ||
| 338 |
2/2✓ Branch 0 taken 1350 times.
✓ Branch 1 taken 7024659 times.
|
7026009 | const int bytes_per_pixel = 1 + (bits_per_raw_sample > 8); |
| 339 | 7026009 | const int block_size = (8 * bytes_per_pixel) >> lowres; | |
| 340 | |||
| 341 | 7026009 | s->block_index[0]+=2; | |
| 342 | 7026009 | s->block_index[1]+=2; | |
| 343 | 7026009 | s->block_index[2]+=2; | |
| 344 | 7026009 | s->block_index[3]+=2; | |
| 345 | 7026009 | s->block_index[4]++; | |
| 346 | 7026009 | s->block_index[5]++; | |
| 347 | 7026009 | s->dest[0]+= 2*block_size; | |
| 348 | 7026009 | s->dest[1] += (2 >> chroma_x_shift) * block_size; | |
| 349 | 7026009 | s->dest[2] += (2 >> chroma_x_shift) * block_size; | |
| 350 | 7026009 | } | |
| 351 | |||
| 352 | #endif /* AVCODEC_MPEGVIDEO_H */ | ||
| 353 |