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.

949 lines
40KB

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