You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

926 lines
39KB

  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 library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. /**
  21. * @file mpegvideo.h
  22. * mpegvideo header.
  23. */
  24. #ifndef AVCODEC_MPEGVIDEO_H
  25. #define AVCODEC_MPEGVIDEO_H
  26. #include "dsputil.h"
  27. #define FRAME_SKIPED 100 ///< return value for header parsers if frame is not coded
  28. enum OutputFormat {
  29. FMT_MPEG1,
  30. FMT_H263,
  31. FMT_MJPEG,
  32. FMT_H264,
  33. };
  34. #define EDGE_WIDTH 16
  35. #define MPEG_BUF_SIZE (16 * 1024)
  36. #define QMAT_SHIFT_MMX 16
  37. #define QMAT_SHIFT 22
  38. #define MAX_FCODE 7
  39. #define MAX_MV 2048
  40. #define MAX_PICTURE_COUNT 15
  41. #define ME_MAP_SIZE 64
  42. #define ME_MAP_SHIFT 3
  43. #define ME_MAP_MV_BITS 11
  44. /* run length table */
  45. #define MAX_RUN 64
  46. #define MAX_LEVEL 64
  47. #define I_TYPE FF_I_TYPE ///< Intra
  48. #define P_TYPE FF_P_TYPE ///< Predicted
  49. #define B_TYPE FF_B_TYPE ///< Bi-dir predicted
  50. #define S_TYPE FF_S_TYPE ///< S(GMC)-VOP MPEG4
  51. #define SI_TYPE FF_SI_TYPE ///< Switching Intra
  52. #define SP_TYPE FF_SP_TYPE ///< Switching Predicted
  53. typedef struct Predictor{
  54. double coeff;
  55. double count;
  56. double decay;
  57. } Predictor;
  58. typedef struct RateControlEntry{
  59. int pict_type;
  60. float qscale;
  61. int mv_bits;
  62. int i_tex_bits;
  63. int p_tex_bits;
  64. int misc_bits;
  65. uint64_t expected_bits;
  66. int new_pict_type;
  67. float new_qscale;
  68. int mc_mb_var_sum;
  69. int mb_var_sum;
  70. int i_count;
  71. int f_code;
  72. int b_code;
  73. }RateControlEntry;
  74. /**
  75. * rate control context.
  76. */
  77. typedef struct RateControlContext{
  78. FILE *stats_file;
  79. int num_entries; ///< number of RateControlEntries
  80. RateControlEntry *entry;
  81. double buffer_index; ///< amount of bits in the video/audio buffer
  82. Predictor pred[5];
  83. double short_term_qsum; ///< sum of recent qscales
  84. double short_term_qcount; ///< count of recent qscales
  85. double pass1_rc_eq_output_sum;///< sum of the output of the rc equation, this is used for normalization
  86. double pass1_wanted_bits; ///< bits which should have been outputed by the pass1 code (including complexity init)
  87. double last_qscale;
  88. double last_qscale_for[5]; ///< last qscale for a specific pict type, used for max_diff & ipb factor stuff
  89. int last_mc_mb_var_sum;
  90. int last_mb_var_sum;
  91. uint64_t i_cplx_sum[5];
  92. uint64_t p_cplx_sum[5];
  93. uint64_t mv_bits_sum[5];
  94. uint64_t qscale_sum[5];
  95. int frame_count[5];
  96. int last_non_b_pict_type;
  97. }RateControlContext;
  98. /**
  99. * Scantable.
  100. */
  101. typedef struct ScanTable{
  102. const uint8_t *scantable;
  103. uint8_t permutated[64];
  104. uint8_t raster_end[64];
  105. #ifdef ARCH_POWERPC
  106. /** Used by dct_quantise_alitvec to find last-non-zero */
  107. uint8_t __align8 inverse[64];
  108. #endif
  109. } ScanTable;
  110. /**
  111. * Picture.
  112. */
  113. typedef struct Picture{
  114. FF_COMMON_FRAME
  115. /**
  116. * halfpel luma planes.
  117. */
  118. uint8_t *interpolated[3];
  119. int16_t (*motion_val_base[2])[2];
  120. int8_t *ref_index[2];
  121. uint32_t *mb_type_base;
  122. #define MB_TYPE_INTRA MB_TYPE_INTRA4x4 //default mb_type if theres just one type
  123. #define IS_INTRA4x4(a) ((a)&MB_TYPE_INTRA4x4)
  124. #define IS_INTRA16x16(a) ((a)&MB_TYPE_INTRA16x16)
  125. #define IS_PCM(a) ((a)&MB_TYPE_INTRA_PCM)
  126. #define IS_INTRA(a) ((a)&7)
  127. #define IS_INTER(a) ((a)&(MB_TYPE_16x16|MB_TYPE_16x8|MB_TYPE_8x16|MB_TYPE_8x8))
  128. #define IS_SKIP(a) ((a)&MB_TYPE_SKIP)
  129. #define IS_INTRA_PCM(a) ((a)&MB_TYPE_INTRA_PCM)
  130. #define IS_INTERLACED(a) ((a)&MB_TYPE_INTERLACED)
  131. #define IS_DIRECT(a) ((a)&MB_TYPE_DIRECT2)
  132. #define IS_GMC(a) ((a)&MB_TYPE_GMC)
  133. #define IS_16X16(a) ((a)&MB_TYPE_16x16)
  134. #define IS_16X8(a) ((a)&MB_TYPE_16x8)
  135. #define IS_8X16(a) ((a)&MB_TYPE_8x16)
  136. #define IS_8X8(a) ((a)&MB_TYPE_8x8)
  137. #define IS_SUB_8X8(a) ((a)&MB_TYPE_16x16) //note reused
  138. #define IS_SUB_8X4(a) ((a)&MB_TYPE_16x8) //note reused
  139. #define IS_SUB_4X8(a) ((a)&MB_TYPE_8x16) //note reused
  140. #define IS_SUB_4X4(a) ((a)&MB_TYPE_8x8) //note reused
  141. #define IS_ACPRED(a) ((a)&MB_TYPE_ACPRED)
  142. #define IS_QUANT(a) ((a)&MB_TYPE_QUANT)
  143. #define IS_DIR(a, part, list) ((a) & (MB_TYPE_P0L0<<((part)+2*(list))))
  144. #define USES_LIST(a, list) ((a) & ((MB_TYPE_P0L0|MB_TYPE_P1L0)<<(2*(list)))) ///< does this mb use listX, note doesnt work if subMBs
  145. #define HAS_CBP(a) ((a)&MB_TYPE_CBP)
  146. int field_poc[2]; ///< h264 top/bottom POC
  147. int poc; ///< h264 frame POC
  148. int frame_num; ///< h264 frame_num
  149. int pic_id; ///< h264 pic_num or long_term_pic_idx
  150. int long_ref; ///< 1->long term reference 0->short term reference
  151. int mb_var_sum; ///< sum of MB variance for current frame
  152. int mc_mb_var_sum; ///< motion compensated MB variance for current frame
  153. uint16_t *mb_var; ///< Table for MB variances
  154. uint16_t *mc_mb_var; ///< Table for motion compensated MB variances
  155. uint8_t *mb_mean; ///< Table for MB luminance
  156. int32_t *mb_cmp_score; ///< Table for MB cmp scores, for mb decission FIXME remove
  157. int b_frame_score; /* */
  158. } Picture;
  159. typedef struct ParseContext{
  160. uint8_t *buffer;
  161. int index;
  162. int last_index;
  163. int buffer_size;
  164. uint32_t state; ///< contains the last few bytes in MSB order
  165. int frame_start_found;
  166. int overread; ///< the number of bytes which where irreversibly read from the next frame
  167. int overread_index; ///< the index into ParseContext.buffer of the overreaded bytes
  168. } ParseContext;
  169. struct MpegEncContext;
  170. /**
  171. * Motion estimation context.
  172. */
  173. typedef struct MotionEstContext{
  174. int skip; ///< set if ME is skiped for the current MB
  175. int co_located_mv[4][2]; ///< mv from last p frame for direct mode ME
  176. int direct_basis_mv[4][2];
  177. uint8_t *scratchpad; ///< data area for the me algo, so that the ME doesnt need to malloc/free
  178. uint32_t *map; ///< map to avoid duplicate evaluations
  179. uint32_t *score_map; ///< map to store the scores
  180. int map_generation;
  181. int pre_penalty_factor;
  182. int penalty_factor;
  183. int sub_penalty_factor;
  184. int mb_penalty_factor;
  185. int pre_pass; ///< = 1 for the pre pass
  186. int dia_size;
  187. int xmin;
  188. int xmax;
  189. int ymin;
  190. int ymax;
  191. uint8_t (*mv_penalty)[MAX_MV*2+1]; ///< amount of bits needed to encode a MV
  192. int (*sub_motion_search)(struct MpegEncContext * s,
  193. int *mx_ptr, int *my_ptr, int dmin,
  194. int pred_x, int pred_y, uint8_t *src_data[3],
  195. uint8_t *ref_data[6], int stride, int uvstride,
  196. int size, int h, uint8_t * const mv_penalty);
  197. int (*motion_search[7])(struct MpegEncContext * s,
  198. int *mx_ptr, int *my_ptr,
  199. int P[10][2], int pred_x, int pred_y, uint8_t *src_data[3],
  200. uint8_t *ref_data[6], int stride, int uvstride, int16_t (*last_mv)[2],
  201. int ref_mv_scale, uint8_t * const mv_penalty);
  202. int (*pre_motion_search)(struct MpegEncContext * s,
  203. int *mx_ptr, int *my_ptr,
  204. int P[10][2], int pred_x, int pred_y, uint8_t *src_data[3],
  205. uint8_t *ref_data[6], int stride, int uvstride, int16_t (*last_mv)[2],
  206. int ref_mv_scale, uint8_t * const mv_penalty);
  207. int (*get_mb_score)(struct MpegEncContext * s, int mx, int my, int pred_x, int pred_y, uint8_t *src_data[3],
  208. uint8_t *ref_data[6], int stride, int uvstride,
  209. uint8_t * const mv_penalty);
  210. }MotionEstContext;
  211. /**
  212. * MpegEncContext.
  213. */
  214. typedef struct MpegEncContext {
  215. struct AVCodecContext *avctx;
  216. /* the following parameters must be initialized before encoding */
  217. int width, height;///< picture size. must be a multiple of 16
  218. int gop_size;
  219. int intra_only; ///< if true, only intra pictures are generated
  220. int bit_rate; ///< wanted bit rate
  221. enum OutputFormat out_format; ///< output format
  222. int h263_pred; ///< use mpeg4/h263 ac/dc predictions
  223. /* the following codec id fields are deprecated in favor of codec_id */
  224. int h263_plus; ///< h263 plus headers
  225. int h263_msmpeg4; ///< generate MSMPEG4 compatible stream (deprecated, use msmpeg4_version instead)
  226. int h263_flv; ///< use flv h263 header
  227. int codec_id; /* see CODEC_ID_xxx */
  228. int fixed_qscale; ///< fixed qscale if non zero
  229. int encoding; ///< true if we are encoding (vs decoding)
  230. int flags; ///< AVCodecContext.flags (HQ, MV4, ...)
  231. int flags2; ///< AVCodecContext.flags2
  232. int max_b_frames; ///< max number of b-frames for encoding
  233. int luma_elim_threshold;
  234. int chroma_elim_threshold;
  235. int strict_std_compliance; ///< strictly follow the std (MPEG4, ...)
  236. int workaround_bugs; ///< workaround bugs in encoders which cannot be detected automatically
  237. /* the following fields are managed internally by the encoder */
  238. /** bit output */
  239. PutBitContext pb;
  240. /* sequence parameters */
  241. int context_initialized;
  242. int input_picture_number; ///< used to set pic->display_picture_number, shouldnt be used for/by anything else
  243. int coded_picture_number; ///< used to set pic->coded_picture_number, shouldnt be used for/by anything else
  244. int picture_number; //FIXME remove, unclear definition
  245. int picture_in_gop_number; ///< 0-> first pic in gop, ...
  246. int b_frames_since_non_b; ///< used for encoding, relative to not yet reordered input
  247. int mb_width, mb_height; ///< number of MBs horizontally & vertically
  248. int mb_stride; ///< mb_width+1 used for some arrays to allow simple addressng of left & top MBs withoutt sig11
  249. int b8_stride; ///< 2*mb_width+1 used for some 8x8 block arrays to allow simple addressng
  250. int b4_stride; ///< 4*mb_width+1 used for some 4x4 block arrays to allow simple addressng
  251. int h_edge_pos, v_edge_pos;///< horizontal / vertical position of the right/bottom edge (pixel replicateion)
  252. int mb_num; ///< number of MBs of a picture
  253. int linesize; ///< line size, in bytes, may be different from width
  254. int uvlinesize; ///< line size, for chroma in bytes, may be different from width
  255. Picture *picture; ///< main picture buffer
  256. Picture **input_picture; ///< next pictures on display order for encoding
  257. Picture **reordered_input_picture; ///< pointer to the next pictures in codedorder for encoding
  258. /**
  259. * copy of the previous picture structure.
  260. * note, linesize & data, might not match the previous picture (for field pictures)
  261. */
  262. Picture last_picture;
  263. /**
  264. * copy of the next picture structure.
  265. * note, linesize & data, might not match the next picture (for field pictures)
  266. */
  267. Picture next_picture;
  268. /**
  269. * copy of the source picture structure for encoding.
  270. * note, linesize & data, might not match the source picture (for field pictures)
  271. */
  272. Picture new_picture;
  273. /**
  274. * copy of the current picture structure.
  275. * note, linesize & data, might not match the current picture (for field pictures)
  276. */
  277. Picture current_picture; ///< buffer to store the decompressed current picture
  278. Picture *last_picture_ptr; ///< pointer to the previous picture.
  279. Picture *next_picture_ptr; ///< pointer to the next picture (for bidir pred)
  280. Picture *current_picture_ptr; ///< pointer to the current picture
  281. uint8_t *visualization_buffer[3]; //< temporary buffer vor MV visualization
  282. int last_dc[3]; ///< last DC values for MPEG1
  283. int16_t *dc_val[3]; ///< used for mpeg4 DC prediction, all 3 arrays must be continuous
  284. int16_t dc_cache[4*5];
  285. int y_dc_scale, c_dc_scale;
  286. uint8_t *y_dc_scale_table; ///< qscale -> y_dc_scale table
  287. uint8_t *c_dc_scale_table; ///< qscale -> c_dc_scale table
  288. const uint8_t *chroma_qscale_table; ///< qscale -> chroma_qscale (h263)
  289. uint8_t *coded_block; ///< used for coded block pattern prediction (msmpeg4v3, wmv1)
  290. int16_t (*ac_val[3])[16]; ///< used for for mpeg4 AC prediction, all 3 arrays must be continuous
  291. int ac_pred;
  292. uint8_t *prev_pict_types; ///< previous picture types in bitstream order, used for mb skip
  293. #define PREV_PICT_TYPES_BUFFER_SIZE 256
  294. int mb_skiped; ///< MUST BE SET only during DECODING
  295. uint8_t *mbskip_table; /**< used to avoid copy if macroblock skipped (for black regions for example)
  296. and used for b-frame encoding & decoding (contains skip table of next P Frame) */
  297. uint8_t *mbintra_table; ///< used to avoid setting {ac, dc, cbp}-pred stuff to zero on inter MB decoding
  298. uint8_t *cbp_table; ///< used to store cbp, ac_pred for partitioned decoding
  299. uint8_t *pred_dir_table; ///< used to store pred_dir for partitioned decoding
  300. uint8_t *allocated_edge_emu_buffer;
  301. uint8_t *edge_emu_buffer; ///< points into the middle of allocated_edge_emu_buffer
  302. int qscale; ///< QP
  303. int chroma_qscale; ///< chroma QP
  304. int lambda; ///< lagrange multipler used in rate distortion
  305. int lambda2; ///< (lambda*lambda) >> FF_LAMBDA_SHIFT
  306. int *lambda_table;
  307. int adaptive_quant; ///< use adaptive quantization
  308. int dquant; ///< qscale difference to prev qscale
  309. int pict_type; ///< I_TYPE, P_TYPE, B_TYPE, ...
  310. int last_pict_type;
  311. int last_non_b_pict_type; ///< used for mpeg4 gmc b-frames & ratecontrol
  312. int frame_rate_index;
  313. /* motion compensation */
  314. int unrestricted_mv; ///< mv can point outside of the coded picture
  315. int h263_long_vectors; ///< use horrible h263v1 long vector mode
  316. int decode; ///< if 0 then decoding will be skiped (for encoding b frames for example)
  317. DSPContext dsp; ///< pointers for accelerated dsp fucntions
  318. int f_code; ///< forward MV resolution
  319. int b_code; ///< backward MV resolution for B Frames (mpeg4)
  320. int16_t (*p_mv_table_base)[2];
  321. int16_t (*b_forw_mv_table_base)[2];
  322. int16_t (*b_back_mv_table_base)[2];
  323. int16_t (*b_bidir_forw_mv_table_base)[2];
  324. int16_t (*b_bidir_back_mv_table_base)[2];
  325. int16_t (*b_direct_mv_table_base)[2];
  326. int16_t (*p_field_mv_table_base[2][2])[2];
  327. int16_t (*b_field_mv_table_base[2][2][2])[2];
  328. int16_t (*p_mv_table)[2]; ///< MV table (1MV per MB) p-frame encoding
  329. int16_t (*b_forw_mv_table)[2]; ///< MV table (1MV per MB) forward mode b-frame encoding
  330. int16_t (*b_back_mv_table)[2]; ///< MV table (1MV per MB) backward mode b-frame encoding
  331. int16_t (*b_bidir_forw_mv_table)[2]; ///< MV table (1MV per MB) bidir mode b-frame encoding
  332. int16_t (*b_bidir_back_mv_table)[2]; ///< MV table (1MV per MB) bidir mode b-frame encoding
  333. int16_t (*b_direct_mv_table)[2]; ///< MV table (1MV per MB) direct mode b-frame encoding
  334. int16_t (*p_field_mv_table[2][2])[2]; ///< MV table (2MV per MB) interlaced p-frame encoding
  335. int16_t (*b_field_mv_table[2][2][2])[2];///< MV table (4MV per MB) interlaced b-frame encoding
  336. uint8_t (*p_field_select_table[2]);
  337. uint8_t (*b_field_select_table[2][2]);
  338. int me_method; ///< ME algorithm
  339. int scene_change_score;
  340. int mv_dir;
  341. #define MV_DIR_BACKWARD 1
  342. #define MV_DIR_FORWARD 2
  343. #define MV_DIRECT 4 ///< bidirectional mode where the difference equals the MV of the last P/S/I-Frame (mpeg4)
  344. int mv_type;
  345. #define MV_TYPE_16X16 0 ///< 1 vector for the whole mb
  346. #define MV_TYPE_8X8 1 ///< 4 vectors (h263, mpeg4 4MV)
  347. #define MV_TYPE_16X8 2 ///< 2 vectors, one per 16x8 block
  348. #define MV_TYPE_FIELD 3 ///< 2 vectors, one per field
  349. #define MV_TYPE_DMV 4 ///< 2 vectors, special mpeg2 Dual Prime Vectors
  350. /**motion vectors for a macroblock
  351. first coordinate : 0 = forward 1 = backward
  352. second " : depend on type
  353. third " : 0 = x, 1 = y
  354. */
  355. int mv[2][4][2];
  356. int field_select[2][2];
  357. int last_mv[2][2][2]; ///< last MV, used for MV prediction in MPEG1 & B-frame MPEG4
  358. uint8_t *fcode_tab; ///< smallest fcode needed for each MV
  359. MotionEstContext me;
  360. int no_rounding; /**< apply no rounding to motion compensation (MPEG4, msmpeg4, ...)
  361. for b-frames rounding mode is allways 0 */
  362. int hurry_up; /**< when set to 1 during decoding, b frames will be skiped
  363. when set to 2 idct/dequant will be skipped too */
  364. /* macroblock layer */
  365. int mb_x, mb_y;
  366. int mb_skip_run;
  367. int mb_intra;
  368. uint16_t *mb_type; ///< Table for candidate MB types for encoding
  369. #define CANDIDATE_MB_TYPE_INTRA 0x01
  370. #define CANDIDATE_MB_TYPE_INTER 0x02
  371. #define CANDIDATE_MB_TYPE_INTER4V 0x04
  372. #define CANDIDATE_MB_TYPE_SKIPED 0x08
  373. //#define MB_TYPE_GMC 0x10
  374. #define CANDIDATE_MB_TYPE_DIRECT 0x10
  375. #define CANDIDATE_MB_TYPE_FORWARD 0x20
  376. #define CANDIDATE_MB_TYPE_BACKWARD 0x40
  377. #define CANDIDATE_MB_TYPE_BIDIR 0x80
  378. #define CANDIDATE_MB_TYPE_INTER_I 0x100
  379. #define CANDIDATE_MB_TYPE_FORWARD_I 0x200
  380. #define CANDIDATE_MB_TYPE_BACKWARD_I 0x400
  381. #define CANDIDATE_MB_TYPE_BIDIR_I 0x800
  382. int block_index[6]; ///< index to current MB in block based arrays with edges
  383. int block_wrap[6];
  384. uint8_t *dest[3];
  385. int *mb_index2xy; ///< mb_index -> mb_x + mb_y*mb_stride
  386. /** matrix transmitted in the bitstream */
  387. uint16_t intra_matrix[64];
  388. uint16_t chroma_intra_matrix[64];
  389. uint16_t inter_matrix[64];
  390. uint16_t chroma_inter_matrix[64];
  391. #define QUANT_BIAS_SHIFT 8
  392. int intra_quant_bias; ///< bias for the quantizer
  393. int inter_quant_bias; ///< bias for the quantizer
  394. int min_qcoeff; ///< minimum encodable coefficient
  395. int max_qcoeff; ///< maximum encodable coefficient
  396. int ac_esc_length; ///< num of bits needed to encode the longest esc
  397. uint8_t *intra_ac_vlc_length;
  398. uint8_t *intra_ac_vlc_last_length;
  399. uint8_t *inter_ac_vlc_length;
  400. uint8_t *inter_ac_vlc_last_length;
  401. uint8_t *luma_dc_vlc_length;
  402. uint8_t *chroma_dc_vlc_length;
  403. #define UNI_AC_ENC_INDEX(run,level) ((run)*128 + (level))
  404. int coded_score[6];
  405. /** precomputed matrix (combine qscale and DCT renorm) */
  406. int (*q_intra_matrix)[64];
  407. int (*q_inter_matrix)[64];
  408. /** identical to the above but for MMX & these are not permutated, second 64 entries are bias*/
  409. uint16_t (*q_intra_matrix16)[2][64];
  410. uint16_t (*q_inter_matrix16)[2][64];
  411. int block_last_index[6]; ///< last non zero coefficient in block
  412. /* scantables */
  413. ScanTable __align8 intra_scantable;
  414. ScanTable intra_h_scantable;
  415. ScanTable intra_v_scantable;
  416. ScanTable inter_scantable; ///< if inter == intra then intra should be used to reduce tha cache usage
  417. /* noise reduction */
  418. int (*dct_error_sum)[64];
  419. int dct_count[2];
  420. uint16_t (*dct_offset)[64];
  421. void *opaque; ///< private data for the user
  422. /* bit rate control */
  423. int64_t wanted_bits;
  424. int64_t total_bits;
  425. int frame_bits; ///< bits used for the current frame
  426. RateControlContext rc_context; ///< contains stuff only accessed in ratecontrol.c
  427. /* statistics, used for 2-pass encoding */
  428. int mv_bits;
  429. int header_bits;
  430. int i_tex_bits;
  431. int p_tex_bits;
  432. int i_count;
  433. int f_count;
  434. int b_count;
  435. int skip_count;
  436. int misc_bits; ///< cbp, mb_type
  437. int last_bits; ///< temp var used for calculating the above vars
  438. /* error concealment / resync */
  439. int error_count;
  440. uint8_t *error_status_table; ///< table of the error status of each MB
  441. #define VP_START 1 ///< current MB is the first after a resync marker
  442. #define AC_ERROR 2
  443. #define DC_ERROR 4
  444. #define MV_ERROR 8
  445. #define AC_END 16
  446. #define DC_END 32
  447. #define MV_END 64
  448. //FIXME some prefix?
  449. int resync_mb_x; ///< x position of last resync marker
  450. int resync_mb_y; ///< y position of last resync marker
  451. GetBitContext last_resync_gb; ///< used to search for the next resync marker
  452. int mb_num_left; ///< number of MBs left in this video packet (for partitioned Slices only)
  453. int next_p_frame_damaged; ///< set if the next p frame is damaged, to avoid showing trashed b frames
  454. int error_resilience;
  455. ParseContext parse_context;
  456. /* H.263 specific */
  457. int gob_index;
  458. int obmc; ///< overlapped block motion compensation
  459. /* H.263+ specific */
  460. int umvplus; ///< == H263+ && unrestricted_mv
  461. int h263_aic; ///< Advanded INTRA Coding (AIC)
  462. int h263_aic_dir; ///< AIC direction: 0 = left, 1 = top
  463. int h263_slice_structured;
  464. int alt_inter_vlc; ///< alternative inter vlc
  465. int modified_quant;
  466. int loop_filter;
  467. /* mpeg4 specific */
  468. int time_increment_resolution;
  469. int time_increment_bits; ///< number of bits to represent the fractional part of time
  470. int last_time_base;
  471. int time_base; ///< time in seconds of last I,P,S Frame
  472. int64_t time; ///< time of current frame
  473. int64_t last_non_b_time;
  474. uint16_t pp_time; ///< time distance between the last 2 p,s,i frames
  475. uint16_t pb_time; ///< time distance between the last b and p,s,i frame
  476. uint16_t pp_field_time;
  477. uint16_t pb_field_time; ///< like above, just for interlaced
  478. int shape;
  479. int vol_sprite_usage;
  480. int sprite_width;
  481. int sprite_height;
  482. int sprite_left;
  483. int sprite_top;
  484. int sprite_brightness_change;
  485. int num_sprite_warping_points;
  486. int real_sprite_warping_points;
  487. int sprite_offset[2][2]; ///< sprite offset[isChroma][isMVY]
  488. int sprite_delta[2][2]; ///< sprite_delta [isY][isMVY]
  489. int sprite_shift[2]; ///< sprite shift [isChroma]
  490. int mcsel;
  491. int quant_precision;
  492. int quarter_sample; ///< 1->qpel, 0->half pel ME/MC
  493. int scalability;
  494. int hierachy_type;
  495. int enhancement_type;
  496. int new_pred;
  497. int reduced_res_vop;
  498. int aspect_ratio_info; //FIXME remove
  499. int sprite_warping_accuracy;
  500. int low_latency_sprite;
  501. int data_partitioning; ///< data partitioning flag from header
  502. int partitioned_frame; ///< is current frame partitioned
  503. int rvlc; ///< reversible vlc
  504. int resync_marker; ///< could this stream contain resync markers
  505. int low_delay; ///< no reordering needed / has no b-frames
  506. int vo_type;
  507. int vol_control_parameters; ///< does the stream contain the low_delay flag, used to workaround buggy encoders
  508. int intra_dc_threshold; ///< QP above whch the ac VLC should be used for intra dc
  509. PutBitContext tex_pb; ///< used for data partitioned VOPs
  510. PutBitContext pb2; ///< used for data partitioned VOPs
  511. #define PB_BUFFER_SIZE 1024*256
  512. uint8_t *tex_pb_buffer;
  513. uint8_t *pb2_buffer;
  514. int mpeg_quant;
  515. int t_frame; ///< time distance of first I -> B, used for interlaced b frames
  516. int padding_bug_score; ///< used to detect the VERY common padding bug in MPEG4
  517. /* divx specific, used to workaround (many) bugs in divx5 */
  518. int divx_version;
  519. int divx_build;
  520. int divx_packed;
  521. #define BITSTREAM_BUFFER_SIZE 1024*256
  522. uint8_t *bitstream_buffer; //Divx 5.01 puts several frames in a single one, this is used to reorder them
  523. int bitstream_buffer_size;
  524. int xvid_build;
  525. /* lavc specific stuff, used to workaround bugs in libavcodec */
  526. int ffmpeg_version;
  527. int lavc_build;
  528. /* RV10 specific */
  529. int rv10_version; ///< RV10 version: 0 or 3
  530. int rv10_first_dc_coded[3];
  531. /* MJPEG specific */
  532. struct MJpegContext *mjpeg_ctx;
  533. int mjpeg_vsample[3]; ///< vertical sampling factors, default = {2, 1, 1}
  534. int mjpeg_hsample[3]; ///< horizontal sampling factors, default = {2, 1, 1}
  535. int mjpeg_write_tables; ///< do we want to have quantisation- and huffmantables in the jpeg file ?
  536. int mjpeg_data_only_frames; ///< frames only with SOI, SOS and EOI markers
  537. /* MSMPEG4 specific */
  538. int mv_table_index;
  539. int rl_table_index;
  540. int rl_chroma_table_index;
  541. int dc_table_index;
  542. int use_skip_mb_code;
  543. int slice_height; ///< in macroblocks
  544. int first_slice_line; ///< used in mpeg4 too to handle resync markers
  545. int flipflop_rounding;
  546. int msmpeg4_version; ///< 0=not msmpeg4, 1=mp41, 2=mp42, 3=mp43/divx3 4=wmv1/7 5=wmv2/8
  547. int per_mb_rl_table;
  548. int esc3_level_length;
  549. int esc3_run_length;
  550. /** [mb_intra][isChroma][level][run][last] */
  551. int (*ac_stats)[2][MAX_LEVEL+1][MAX_RUN+1][2];
  552. int inter_intra_pred;
  553. int mspel;
  554. /* decompression specific */
  555. GetBitContext gb;
  556. /* Mpeg1 specific */
  557. int gop_picture_number; ///< index of the first picture of a GOP based on fake_pic_num & mpeg1 specific
  558. int last_mv_dir; ///< last mv_dir, used for b frame encoding
  559. int broken_link; ///< no_output_of_prior_pics_flag
  560. uint8_t *vbv_delay_ptr; ///< pointer to vbv_delay in the bitstream
  561. /* MPEG2 specific - I wish I had not to support this mess. */
  562. int progressive_sequence;
  563. int mpeg_f_code[2][2];
  564. int picture_structure;
  565. /* picture type */
  566. #define PICT_TOP_FIELD 1
  567. #define PICT_BOTTOM_FIELD 2
  568. #define PICT_FRAME 3
  569. int intra_dc_precision;
  570. int frame_pred_frame_dct;
  571. int top_field_first;
  572. int concealment_motion_vectors;
  573. int q_scale_type;
  574. int intra_vlc_format;
  575. int alternate_scan;
  576. int repeat_first_field;
  577. int chroma_420_type;
  578. int progressive_frame;
  579. int full_pel[2];
  580. int interlaced_dct;
  581. int first_slice;
  582. int first_field; ///< is 1 for the first field of a field picture 0 otherwise
  583. /* RTP specific */
  584. int rtp_mode;
  585. uint8_t *ptr_lastgob;
  586. int swap_uv;//vcr2 codec is mpeg2 varint with UV swaped
  587. short * pblocks[12];
  588. DCTELEM (*block)[64]; ///< points to one of the following blocks
  589. DCTELEM (*blocks)[6][64]; // for HQ mode we need to keep the best block
  590. int (*decode_mb)(struct MpegEncContext *s, DCTELEM block[6][64]); // used by some codecs to avoid a switch()
  591. #define SLICE_OK 0
  592. #define SLICE_ERROR -1
  593. #define SLICE_END -2 ///<end marker found
  594. #define SLICE_NOEND -3 ///<no end marker or error found but mb count exceeded
  595. void (*dct_unquantize_mpeg1_intra)(struct MpegEncContext *s,
  596. DCTELEM *block/*align 16*/, int n, int qscale);
  597. void (*dct_unquantize_mpeg1_inter)(struct MpegEncContext *s,
  598. DCTELEM *block/*align 16*/, int n, int qscale);
  599. void (*dct_unquantize_mpeg2_intra)(struct MpegEncContext *s,
  600. DCTELEM *block/*align 16*/, int n, int qscale);
  601. void (*dct_unquantize_mpeg2_inter)(struct MpegEncContext *s,
  602. DCTELEM *block/*align 16*/, int n, int qscale);
  603. void (*dct_unquantize_h263_intra)(struct MpegEncContext *s,
  604. DCTELEM *block/*align 16*/, int n, int qscale);
  605. void (*dct_unquantize_h263_inter)(struct MpegEncContext *s,
  606. DCTELEM *block/*align 16*/, int n, int qscale);
  607. void (*dct_unquantize_intra)(struct MpegEncContext *s, // unquantizer to use (mpeg4 can use both)
  608. DCTELEM *block/*align 16*/, int n, int qscale);
  609. void (*dct_unquantize_inter)(struct MpegEncContext *s, // unquantizer to use (mpeg4 can use both)
  610. DCTELEM *block/*align 16*/, int n, int qscale);
  611. int (*dct_quantize)(struct MpegEncContext *s, DCTELEM *block/*align 16*/, int n, int qscale, int *overflow);
  612. int (*fast_dct_quantize)(struct MpegEncContext *s, DCTELEM *block/*align 16*/, int n, int qscale, int *overflow);
  613. void (*denoise_dct)(struct MpegEncContext *s, DCTELEM *block);
  614. } MpegEncContext;
  615. int DCT_common_init(MpegEncContext *s);
  616. int MPV_common_init(MpegEncContext *s);
  617. void MPV_common_end(MpegEncContext *s);
  618. void MPV_decode_mb(MpegEncContext *s, DCTELEM block[6][64]);
  619. int MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx);
  620. void MPV_frame_end(MpegEncContext *s);
  621. int MPV_encode_init(AVCodecContext *avctx);
  622. int MPV_encode_end(AVCodecContext *avctx);
  623. int MPV_encode_picture(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data);
  624. #ifdef HAVE_MMX
  625. void MPV_common_init_mmx(MpegEncContext *s);
  626. #endif
  627. #ifdef ARCH_ALPHA
  628. void MPV_common_init_axp(MpegEncContext *s);
  629. #endif
  630. #ifdef HAVE_MLIB
  631. void MPV_common_init_mlib(MpegEncContext *s);
  632. #endif
  633. #ifdef HAVE_MMI
  634. void MPV_common_init_mmi(MpegEncContext *s);
  635. #endif
  636. #ifdef ARCH_ARMV4L
  637. void MPV_common_init_armv4l(MpegEncContext *s);
  638. #endif
  639. #ifdef ARCH_POWERPC
  640. void MPV_common_init_ppc(MpegEncContext *s);
  641. #endif
  642. extern void (*draw_edges)(uint8_t *buf, int wrap, int width, int height, int w);
  643. void ff_copy_bits(PutBitContext *pb, uint8_t *src, int length);
  644. void ff_clean_intra_table_entries(MpegEncContext *s);
  645. void ff_init_scantable(uint8_t *, ScanTable *st, const uint8_t *src_scantable);
  646. void ff_draw_horiz_band(MpegEncContext *s, int y, int h);
  647. void ff_emulated_edge_mc(uint8_t *buf, uint8_t *src, int linesize, int block_w, int block_h,
  648. int src_x, int src_y, int w, int h);
  649. #define END_NOT_FOUND -100
  650. int ff_combine_frame( MpegEncContext *s, int next, uint8_t **buf, int *buf_size);
  651. void ff_mpeg_flush(AVCodecContext *avctx);
  652. void ff_print_debug_info(MpegEncContext *s, AVFrame *pict);
  653. void ff_write_quant_matrix(PutBitContext *pb, int16_t *matrix);
  654. int ff_find_unused_picture(MpegEncContext *s, int shared);
  655. void ff_denoise_dct(MpegEncContext *s, DCTELEM *block);
  656. void ff_er_frame_start(MpegEncContext *s);
  657. void ff_er_frame_end(MpegEncContext *s);
  658. void ff_er_add_slice(MpegEncContext *s, int startx, int starty, int endx, int endy, int status);
  659. extern enum PixelFormat ff_yuv420p_list[2];
  660. void ff_init_block_index(MpegEncContext *s);
  661. static inline void ff_update_block_index(MpegEncContext *s){
  662. s->block_index[0]+=2;
  663. s->block_index[1]+=2;
  664. s->block_index[2]+=2;
  665. s->block_index[3]+=2;
  666. s->block_index[4]++;
  667. s->block_index[5]++;
  668. s->dest[0]+= 16;
  669. s->dest[1]+= 8;
  670. s->dest[2]+= 8;
  671. }
  672. static inline int get_bits_diff(MpegEncContext *s){
  673. const int bits= get_bit_count(&s->pb);
  674. const int last= s->last_bits;
  675. s->last_bits = bits;
  676. return bits - last;
  677. }
  678. /* motion_est.c */
  679. void ff_estimate_p_frame_motion(MpegEncContext * s,
  680. int mb_x, int mb_y);
  681. void ff_estimate_b_frame_motion(MpegEncContext * s,
  682. int mb_x, int mb_y);
  683. int ff_get_best_fcode(MpegEncContext * s, int16_t (*mv_table)[2], int type);
  684. void ff_fix_long_p_mvs(MpegEncContext * s);
  685. void ff_fix_long_mvs(MpegEncContext * s, uint8_t *field_select_table, int field_select,
  686. int16_t (*mv_table)[2], int f_code, int type, int truncate);
  687. void ff_init_me(MpegEncContext *s);
  688. int ff_pre_estimate_p_frame_motion(MpegEncContext * s, int mb_x, int mb_y);
  689. /* mpeg12.c */
  690. extern const int16_t ff_mpeg1_default_intra_matrix[64];
  691. extern const int16_t ff_mpeg1_default_non_intra_matrix[64];
  692. extern uint8_t ff_mpeg1_dc_scale_table[128];
  693. void mpeg1_encode_picture_header(MpegEncContext *s, int picture_number);
  694. void mpeg1_encode_mb(MpegEncContext *s,
  695. DCTELEM block[6][64],
  696. int motion_x, int motion_y);
  697. void ff_mpeg1_encode_init(MpegEncContext *s);
  698. void ff_mpeg1_encode_slice_header(MpegEncContext *s);
  699. void ff_mpeg1_clean_buffers(MpegEncContext *s);
  700. /** RLTable. */
  701. typedef struct RLTable {
  702. int n; ///< number of entries of table_vlc minus 1
  703. int last; ///< number of values for last = 0
  704. const uint16_t (*table_vlc)[2];
  705. const int8_t *table_run;
  706. const int8_t *table_level;
  707. uint8_t *index_run[2]; ///< encoding only
  708. int8_t *max_level[2]; ///< encoding & decoding
  709. int8_t *max_run[2]; ///< encoding & decoding
  710. VLC vlc; ///< decoding only deprected FIXME remove
  711. RL_VLC_ELEM *rl_vlc[32]; ///< decoding only
  712. } RLTable;
  713. void init_rl(RLTable *rl);
  714. void init_vlc_rl(RLTable *rl);
  715. static inline int get_rl_index(const RLTable *rl, int last, int run, int level)
  716. {
  717. int index;
  718. index = rl->index_run[last][run];
  719. if (index >= rl->n)
  720. return rl->n;
  721. if (level > rl->max_level[last][run])
  722. return rl->n;
  723. return index + level - 1;
  724. }
  725. extern uint8_t ff_mpeg4_y_dc_scale_table[32];
  726. extern uint8_t ff_mpeg4_c_dc_scale_table[32];
  727. extern uint8_t ff_aic_dc_scale_table[32];
  728. extern const int16_t ff_mpeg4_default_intra_matrix[64];
  729. extern const int16_t ff_mpeg4_default_non_intra_matrix[64];
  730. extern const uint8_t ff_h263_chroma_qscale_table[32];
  731. extern const uint8_t ff_h263_loop_filter_strength[32];
  732. int ff_h263_decode_init(AVCodecContext *avctx);
  733. int ff_h263_decode_frame(AVCodecContext *avctx,
  734. void *data, int *data_size,
  735. uint8_t *buf, int buf_size);
  736. int ff_h263_decode_end(AVCodecContext *avctx);
  737. void h263_encode_mb(MpegEncContext *s,
  738. DCTELEM block[6][64],
  739. int motion_x, int motion_y);
  740. void mpeg4_encode_mb(MpegEncContext *s,
  741. DCTELEM block[6][64],
  742. int motion_x, int motion_y);
  743. void h263_encode_picture_header(MpegEncContext *s, int picture_number);
  744. void ff_flv_encode_picture_header(MpegEncContext *s, int picture_number);
  745. void h263_encode_gob_header(MpegEncContext * s, int mb_line);
  746. int16_t *h263_pred_motion(MpegEncContext * s, int block,
  747. int *px, int *py);
  748. void mpeg4_pred_ac(MpegEncContext * s, DCTELEM *block, int n,
  749. int dir);
  750. void ff_set_mpeg4_time(MpegEncContext * s, int picture_number);
  751. void mpeg4_encode_picture_header(MpegEncContext *s, int picture_number);
  752. void h263_encode_init(MpegEncContext *s);
  753. void h263_decode_init_vlc(MpegEncContext *s);
  754. int h263_decode_picture_header(MpegEncContext *s);
  755. int ff_h263_decode_gob_header(MpegEncContext *s);
  756. int ff_mpeg4_decode_picture_header(MpegEncContext * s, GetBitContext *gb);
  757. void ff_h263_update_motion_val(MpegEncContext * s);
  758. void ff_h263_loop_filter(MpegEncContext * s);
  759. void ff_set_qscale(MpegEncContext * s, int qscale);
  760. int ff_h263_decode_mba(MpegEncContext *s);
  761. void ff_h263_encode_mba(MpegEncContext *s);
  762. int intel_h263_decode_picture_header(MpegEncContext *s);
  763. int flv_h263_decode_picture_header(MpegEncContext *s);
  764. int ff_h263_decode_mb(MpegEncContext *s,
  765. DCTELEM block[6][64]);
  766. int ff_mpeg4_decode_mb(MpegEncContext *s,
  767. DCTELEM block[6][64]);
  768. int h263_get_picture_format(int width, int height);
  769. void ff_mpeg4_encode_video_packet_header(MpegEncContext *s);
  770. void ff_mpeg4_clean_buffers(MpegEncContext *s);
  771. void ff_mpeg4_stuffing(PutBitContext * pbc);
  772. void ff_mpeg4_init_partitions(MpegEncContext *s);
  773. void ff_mpeg4_merge_partitions(MpegEncContext *s);
  774. void ff_clean_mpeg4_qscales(MpegEncContext *s);
  775. void ff_clean_h263_qscales(MpegEncContext *s);
  776. int ff_mpeg4_decode_partitions(MpegEncContext *s);
  777. int ff_mpeg4_get_video_packet_prefix_length(MpegEncContext *s);
  778. int ff_h263_resync(MpegEncContext *s);
  779. int ff_h263_get_gob_height(MpegEncContext *s);
  780. int ff_mpeg4_set_direct_mv(MpegEncContext *s, int mx, int my);
  781. inline int ff_h263_round_chroma(int x);
  782. /* rv10.c */
  783. void rv10_encode_picture_header(MpegEncContext *s, int picture_number);
  784. int rv_decode_dc(MpegEncContext *s, int n);
  785. /* msmpeg4.c */
  786. void msmpeg4_encode_picture_header(MpegEncContext * s, int picture_number);
  787. void msmpeg4_encode_ext_header(MpegEncContext * s);
  788. void msmpeg4_encode_mb(MpegEncContext * s,
  789. DCTELEM block[6][64],
  790. int motion_x, int motion_y);
  791. int msmpeg4_decode_picture_header(MpegEncContext * s);
  792. int msmpeg4_decode_ext_header(MpegEncContext * s, int buf_size);
  793. int ff_msmpeg4_decode_init(MpegEncContext *s);
  794. void ff_msmpeg4_encode_init(MpegEncContext *s);
  795. int ff_wmv2_decode_picture_header(MpegEncContext * s);
  796. int ff_wmv2_decode_secondary_picture_header(MpegEncContext * s);
  797. void ff_wmv2_add_mb(MpegEncContext *s, DCTELEM block[6][64], uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr);
  798. void ff_mspel_motion(MpegEncContext *s,
  799. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  800. uint8_t **ref_picture, op_pixels_func (*pix_op)[4],
  801. int motion_x, int motion_y, int h);
  802. int ff_wmv2_encode_picture_header(MpegEncContext * s, int picture_number);
  803. void ff_wmv2_encode_mb(MpegEncContext * s,
  804. DCTELEM block[6][64],
  805. int motion_x, int motion_y);
  806. /* mjpeg.c */
  807. int mjpeg_init(MpegEncContext *s);
  808. void mjpeg_close(MpegEncContext *s);
  809. void mjpeg_encode_mb(MpegEncContext *s,
  810. DCTELEM block[6][64]);
  811. void mjpeg_picture_header(MpegEncContext *s);
  812. void mjpeg_picture_trailer(MpegEncContext *s);
  813. /* rate control */
  814. int ff_rate_control_init(MpegEncContext *s);
  815. float ff_rate_estimate_qscale(MpegEncContext *s);
  816. void ff_write_pass1_stats(MpegEncContext *s);
  817. void ff_rate_control_uninit(MpegEncContext *s);
  818. double ff_eval(char *s, double *const_value, const char **const_name,
  819. double (**func1)(void *, double), const char **func1_name,
  820. double (**func2)(void *, double, double), char **func2_name,
  821. void *opaque);
  822. int ff_vbv_update(MpegEncContext *s, int frame_size);
  823. #endif /* AVCODEC_MPEGVIDEO_H */