00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifndef FFMPEG_MPEGVIDEO_H
00029 #define FFMPEG_MPEGVIDEO_H
00030
00031 #include "dsputil.h"
00032 #include "bitstream.h"
00033 #include "ratecontrol.h"
00034 #include "parser.h"
00035 #include "mpeg12data.h"
00036 #include "rl.h"
00037
00038 #define FRAME_SKIPPED 100
00039
00040 enum OutputFormat {
00041 FMT_MPEG1,
00042 FMT_H261,
00043 FMT_H263,
00044 FMT_MJPEG,
00045 FMT_H264,
00046 };
00047
00048 #define MPEG_BUF_SIZE (16 * 1024)
00049
00050 #define QMAT_SHIFT_MMX 16
00051 #define QMAT_SHIFT 22
00052
00053 #define MAX_FCODE 7
00054 #define MAX_MV 2048
00055
00056 #define MAX_THREADS 8
00057
00058 #define MAX_PICTURE_COUNT 32
00059
00060 #define ME_MAP_SIZE 64
00061 #define ME_MAP_SHIFT 3
00062 #define ME_MAP_MV_BITS 11
00063
00064 #define MAX_MB_BYTES (30*16*16*3/8 + 120)
00065
00066 #define INPLACE_OFFSET 16
00067
00068
00069 #define SEQ_END_CODE 0x000001b7
00070 #define SEQ_START_CODE 0x000001b3
00071 #define GOP_START_CODE 0x000001b8
00072 #define PICTURE_START_CODE 0x00000100
00073 #define SLICE_MIN_START_CODE 0x00000101
00074 #define SLICE_MAX_START_CODE 0x000001af
00075 #define EXT_START_CODE 0x000001b5
00076 #define USER_START_CODE 0x000001b2
00077
00078
00079
00080
00081 typedef struct Picture{
00082 FF_COMMON_FRAME
00083
00084
00085
00086
00087 uint8_t *interpolated[3];
00088 int16_t (*motion_val_base[2])[2];
00089 uint32_t *mb_type_base;
00090 #define MB_TYPE_INTRA MB_TYPE_INTRA4x4 //default mb_type if there is just one type
00091 #define IS_INTRA4x4(a) ((a)&MB_TYPE_INTRA4x4)
00092 #define IS_INTRA16x16(a) ((a)&MB_TYPE_INTRA16x16)
00093 #define IS_PCM(a) ((a)&MB_TYPE_INTRA_PCM)
00094 #define IS_INTRA(a) ((a)&7)
00095 #define IS_INTER(a) ((a)&(MB_TYPE_16x16|MB_TYPE_16x8|MB_TYPE_8x16|MB_TYPE_8x8))
00096 #define IS_SKIP(a) ((a)&MB_TYPE_SKIP)
00097 #define IS_INTRA_PCM(a) ((a)&MB_TYPE_INTRA_PCM)
00098 #define IS_INTERLACED(a) ((a)&MB_TYPE_INTERLACED)
00099 #define IS_DIRECT(a) ((a)&MB_TYPE_DIRECT2)
00100 #define IS_GMC(a) ((a)&MB_TYPE_GMC)
00101 #define IS_16X16(a) ((a)&MB_TYPE_16x16)
00102 #define IS_16X8(a) ((a)&MB_TYPE_16x8)
00103 #define IS_8X16(a) ((a)&MB_TYPE_8x16)
00104 #define IS_8X8(a) ((a)&MB_TYPE_8x8)
00105 #define IS_SUB_8X8(a) ((a)&MB_TYPE_16x16) //note reused
00106 #define IS_SUB_8X4(a) ((a)&MB_TYPE_16x8) //note reused
00107 #define IS_SUB_4X8(a) ((a)&MB_TYPE_8x16) //note reused
00108 #define IS_SUB_4X4(a) ((a)&MB_TYPE_8x8) //note reused
00109 #define IS_ACPRED(a) ((a)&MB_TYPE_ACPRED)
00110 #define IS_QUANT(a) ((a)&MB_TYPE_QUANT)
00111 #define IS_DIR(a, part, list) ((a) & (MB_TYPE_P0L0<<((part)+2*(list))))
00112 #define USES_LIST(a, list) ((a) & ((MB_TYPE_P0L0|MB_TYPE_P1L0)<<(2*(list))))
00113 #define HAS_CBP(a) ((a)&MB_TYPE_CBP)
00114
00115 int field_poc[2];
00116 int poc;
00117 int frame_num;
00118 int pic_id;
00119
00120 int long_ref;
00121 int ref_poc[2][16];
00122 int ref_count[2];
00123
00124 int mb_var_sum;
00125 int mc_mb_var_sum;
00126 uint16_t *mb_var;
00127 uint16_t *mc_mb_var;
00128 uint8_t *mb_mean;
00129 int32_t *mb_cmp_score;
00130 int b_frame_score;
00131 } Picture;
00132
00133 struct MpegEncContext;
00134
00135
00136
00137
00138 typedef struct MotionEstContext{
00139 AVCodecContext *avctx;
00140 int skip;
00141 int co_located_mv[4][2];
00142 int direct_basis_mv[4][2];
00143 uint8_t *scratchpad;
00144 uint8_t *best_mb;
00145 uint8_t *temp_mb[2];
00146 uint8_t *temp;
00147 int best_bits;
00148 uint32_t *map;
00149 uint32_t *score_map;
00150 int map_generation;
00151 int pre_penalty_factor;
00152 int penalty_factor;
00153
00154
00155
00156
00157 int sub_penalty_factor;
00158 int mb_penalty_factor;
00159 int flags;
00160 int sub_flags;
00161 int mb_flags;
00162 int pre_pass;
00163 int dia_size;
00164 int xmin;
00165 int xmax;
00166 int ymin;
00167 int ymax;
00168 int pred_x;
00169 int pred_y;
00170 uint8_t *src[4][4];
00171 uint8_t *ref[4][4];
00172 int stride;
00173 int uvstride;
00174
00175 int mc_mb_var_sum_temp;
00176 int mb_var_sum_temp;
00177 int scene_change_score;
00178
00179 op_pixels_func (*hpel_put)[4];
00180 op_pixels_func (*hpel_avg)[4];
00181 qpel_mc_func (*qpel_put)[16];
00182 qpel_mc_func (*qpel_avg)[16];
00183 uint8_t (*mv_penalty)[MAX_MV*2+1];
00184 uint8_t *current_mv_penalty;
00185 int (*sub_motion_search)(struct MpegEncContext * s,
00186 int *mx_ptr, int *my_ptr, int dmin,
00187 int src_index, int ref_index,
00188 int size, int h);
00189 }MotionEstContext;
00190
00191
00192
00193
00194 typedef struct MpegEncContext {
00195 struct AVCodecContext *avctx;
00196
00197 int width, height;
00198 int gop_size;
00199 int intra_only;
00200 int bit_rate;
00201 enum OutputFormat out_format;
00202 int h263_pred;
00203
00204
00205 int h263_plus;
00206 int h263_msmpeg4;
00207 int h263_flv;
00208
00209 enum CodecID codec_id;
00210 int fixed_qscale;
00211 int encoding;
00212 int flags;
00213 int flags2;
00214 int max_b_frames;
00215 int luma_elim_threshold;
00216 int chroma_elim_threshold;
00217 int strict_std_compliance;
00218 int workaround_bugs;
00219 int codec_tag;
00220 int stream_codec_tag;
00221
00222
00223
00224 PutBitContext pb;
00225
00226
00227 int context_initialized;
00228 int input_picture_number;
00229 int coded_picture_number;
00230 int picture_number;
00231 int picture_in_gop_number;
00232 int b_frames_since_non_b;
00233 int64_t user_specified_pts;
00234 int mb_width, mb_height;
00235 int mb_stride;
00236 int b8_stride;
00237 int b4_stride;
00238 int h_edge_pos, v_edge_pos;
00239 int mb_num;
00240 int linesize;
00241 int uvlinesize;
00242 Picture *picture;
00243 Picture **input_picture;
00244 Picture **reordered_input_picture;
00245
00246 int start_mb_y;
00247 int end_mb_y;
00248 struct MpegEncContext *thread_context[MAX_THREADS];
00249
00250
00251
00252
00253
00254 Picture last_picture;
00255
00256
00257
00258
00259
00260 Picture next_picture;
00261
00262
00263
00264
00265
00266 Picture new_picture;
00267
00268
00269
00270
00271
00272 Picture current_picture;
00273
00274 Picture *last_picture_ptr;
00275 Picture *next_picture_ptr;
00276 Picture *current_picture_ptr;
00277 uint8_t *visualization_buffer[3];
00278 int last_dc[3];
00279 int16_t *dc_val_base;
00280 int16_t *dc_val[3];
00281 int16_t dc_cache[4*5];
00282 int y_dc_scale, c_dc_scale;
00283 const uint8_t *y_dc_scale_table;
00284 const uint8_t *c_dc_scale_table;
00285 const uint8_t *chroma_qscale_table;
00286 uint8_t *coded_block_base;
00287 uint8_t *coded_block;
00288 int16_t (*ac_val_base)[16];
00289 int16_t (*ac_val[3])[16];
00290 int ac_pred;
00291 uint8_t *prev_pict_types;
00292 #define PREV_PICT_TYPES_BUFFER_SIZE 256
00293 int mb_skipped;
00294 uint8_t *mbskip_table;
00295
00296 uint8_t *mbintra_table;
00297 uint8_t *cbp_table;
00298 uint8_t *pred_dir_table;
00299 uint8_t *allocated_edge_emu_buffer;
00300 uint8_t *edge_emu_buffer;
00301 uint8_t *rd_scratchpad;
00302 uint8_t *obmc_scratchpad;
00303 uint8_t *b_scratchpad;
00304
00305 int qscale;
00306 int chroma_qscale;
00307 unsigned int lambda;
00308 unsigned int lambda2;
00309 int *lambda_table;
00310 int adaptive_quant;
00311 int dquant;
00312 int pict_type;
00313 int last_pict_type;
00314 int last_non_b_pict_type;
00315 int dropable;
00316 int frame_rate_index;
00317 int last_lambda_for[5];
00318 int skipdct;
00319
00320
00321 int unrestricted_mv;
00322 int h263_long_vectors;
00323 int decode;
00324
00325 DSPContext dsp;
00326 int f_code;
00327 int b_code;
00328 int16_t (*p_mv_table_base)[2];
00329 int16_t (*b_forw_mv_table_base)[2];
00330 int16_t (*b_back_mv_table_base)[2];
00331 int16_t (*b_bidir_forw_mv_table_base)[2];
00332 int16_t (*b_bidir_back_mv_table_base)[2];
00333 int16_t (*b_direct_mv_table_base)[2];
00334 int16_t (*p_field_mv_table_base[2][2])[2];
00335 int16_t (*b_field_mv_table_base[2][2][2])[2];
00336 int16_t (*p_mv_table)[2];
00337 int16_t (*b_forw_mv_table)[2];
00338 int16_t (*b_back_mv_table)[2];
00339 int16_t (*b_bidir_forw_mv_table)[2];
00340 int16_t (*b_bidir_back_mv_table)[2];
00341 int16_t (*b_direct_mv_table)[2];
00342 int16_t (*p_field_mv_table[2][2])[2];
00343 int16_t (*b_field_mv_table[2][2][2])[2];
00344 uint8_t (*p_field_select_table[2]);
00345 uint8_t (*b_field_select_table[2][2]);
00346 int me_method;
00347 int mv_dir;
00348 #define MV_DIR_FORWARD 1
00349 #define MV_DIR_BACKWARD 2
00350 #define MV_DIRECT 4
00351 int mv_type;
00352 #define MV_TYPE_16X16 0
00353 #define MV_TYPE_8X8 1
00354 #define MV_TYPE_16X8 2
00355 #define MV_TYPE_FIELD 3
00356 #define MV_TYPE_DMV 4
00357
00358
00359
00360
00361
00362 int mv[2][4][2];
00363 int field_select[2][2];
00364 int last_mv[2][2][2];
00365 uint8_t *fcode_tab;
00366 int16_t direct_scale_mv[2][64];
00367
00368 MotionEstContext me;
00369
00370 int no_rounding;
00371
00372
00373 int hurry_up;
00374
00375
00376
00377 int mb_x, mb_y;
00378 int mb_skip_run;
00379 int mb_intra;
00380 uint16_t *mb_type;
00381 #define CANDIDATE_MB_TYPE_INTRA 0x01
00382 #define CANDIDATE_MB_TYPE_INTER 0x02
00383 #define CANDIDATE_MB_TYPE_INTER4V 0x04
00384 #define CANDIDATE_MB_TYPE_SKIPPED 0x08
00385
00386
00387 #define CANDIDATE_MB_TYPE_DIRECT 0x10
00388 #define CANDIDATE_MB_TYPE_FORWARD 0x20
00389 #define CANDIDATE_MB_TYPE_BACKWARD 0x40
00390 #define CANDIDATE_MB_TYPE_BIDIR 0x80
00391
00392 #define CANDIDATE_MB_TYPE_INTER_I 0x100
00393 #define CANDIDATE_MB_TYPE_FORWARD_I 0x200
00394 #define CANDIDATE_MB_TYPE_BACKWARD_I 0x400
00395 #define CANDIDATE_MB_TYPE_BIDIR_I 0x800
00396
00397 #define CANDIDATE_MB_TYPE_DIRECT0 0x1000
00398
00399 int block_index[6];
00400 int block_wrap[6];
00401 uint8_t *dest[3];
00402
00403 int *mb_index2xy;
00404
00405
00406 uint16_t intra_matrix[64];
00407 uint16_t chroma_intra_matrix[64];
00408 uint16_t inter_matrix[64];
00409 uint16_t chroma_inter_matrix[64];
00410 #define QUANT_BIAS_SHIFT 8
00411 int intra_quant_bias;
00412 int inter_quant_bias;
00413 int min_qcoeff;
00414 int max_qcoeff;
00415 int ac_esc_length;
00416 uint8_t *intra_ac_vlc_length;
00417 uint8_t *intra_ac_vlc_last_length;
00418 uint8_t *inter_ac_vlc_length;
00419 uint8_t *inter_ac_vlc_last_length;
00420 uint8_t *luma_dc_vlc_length;
00421 uint8_t *chroma_dc_vlc_length;
00422 #define UNI_AC_ENC_INDEX(run,level) ((run)*128 + (level))
00423
00424 int coded_score[8];
00425
00426
00427 int (*q_intra_matrix)[64];
00428 int (*q_inter_matrix)[64];
00429
00430 uint16_t (*q_intra_matrix16)[2][64];
00431 uint16_t (*q_inter_matrix16)[2][64];
00432 int block_last_index[12];
00433
00434 DECLARE_ALIGNED_8(ScanTable, intra_scantable);
00435 ScanTable intra_h_scantable;
00436 ScanTable intra_v_scantable;
00437 ScanTable inter_scantable;
00438
00439
00440 int (*dct_error_sum)[64];
00441 int dct_count[2];
00442 uint16_t (*dct_offset)[64];
00443
00444 void *opaque;
00445
00446
00447 int64_t wanted_bits;
00448 int64_t total_bits;
00449 int frame_bits;
00450 int next_lambda;
00451 RateControlContext rc_context;
00452
00453
00454 int mv_bits;
00455 int header_bits;
00456 int i_tex_bits;
00457 int p_tex_bits;
00458 int i_count;
00459 int f_count;
00460 int b_count;
00461 int skip_count;
00462 int misc_bits;
00463 int last_bits;
00464
00465
00466 int error_count;
00467 uint8_t *error_status_table;
00468 #define VP_START 1
00469 #define AC_ERROR 2
00470 #define DC_ERROR 4
00471 #define MV_ERROR 8
00472 #define AC_END 16
00473 #define DC_END 32
00474 #define MV_END 64
00475
00476
00477 int resync_mb_x;
00478 int resync_mb_y;
00479 GetBitContext last_resync_gb;
00480 int mb_num_left;
00481 int next_p_frame_damaged;
00482 int error_resilience;
00483
00484 ParseContext parse_context;
00485
00486
00487 int gob_index;
00488 int obmc;
00489
00490
00491 int umvplus;
00492 int h263_aic;
00493 int h263_aic_dir;
00494 int h263_slice_structured;
00495 int alt_inter_vlc;
00496 int modified_quant;
00497 int loop_filter;
00498 int custom_pcf;
00499
00500
00501 int time_increment_bits;
00502 int last_time_base;
00503 int time_base;
00504 int64_t time;
00505 int64_t last_non_b_time;
00506 uint16_t pp_time;
00507 uint16_t pb_time;
00508 uint16_t pp_field_time;
00509 uint16_t pb_field_time;
00510 int shape;
00511 int vol_sprite_usage;
00512 int sprite_width;
00513 int sprite_height;
00514 int sprite_left;
00515 int sprite_top;
00516 int sprite_brightness_change;
00517 int num_sprite_warping_points;
00518 int real_sprite_warping_points;
00519 int sprite_offset[2][2];
00520 int sprite_delta[2][2];
00521 int sprite_shift[2];
00522 int mcsel;
00523 int quant_precision;
00524 int quarter_sample;
00525 int scalability;
00526 int hierachy_type;
00527 int enhancement_type;
00528 int new_pred;
00529 int reduced_res_vop;
00530 int aspect_ratio_info;
00531 int sprite_warping_accuracy;
00532 int low_latency_sprite;
00533 int data_partitioning;
00534 int partitioned_frame;
00535 int rvlc;
00536 int resync_marker;
00537 int low_delay;
00538 int vo_type;
00539 int vol_control_parameters;
00540 int intra_dc_threshold;
00541 int use_intra_dc_vlc;
00542 PutBitContext tex_pb;
00543 PutBitContext pb2;
00544 int mpeg_quant;
00545 int t_frame;
00546 int padding_bug_score;
00547
00548
00549 int divx_version;
00550 int divx_build;
00551 int divx_packed;
00552 uint8_t *bitstream_buffer;
00553 int bitstream_buffer_size;
00554 unsigned int allocated_bitstream_buffer_size;
00555
00556 int xvid_build;
00557
00558
00559 int lavc_build;
00560
00561
00562 int rv10_version;
00563 int rv10_first_dc_coded[3];
00564
00565
00566 struct MJpegContext *mjpeg_ctx;
00567 int mjpeg_vsample[3];
00568 int mjpeg_hsample[3];
00569
00570
00571 int mv_table_index;