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.

927 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 max_b_frames; ///< max number of b-frames for encoding
  232. int luma_elim_threshold;
  233. int chroma_elim_threshold;
  234. int strict_std_compliance; ///< strictly follow the std (MPEG4, ...)
  235. int workaround_bugs; ///< workaround bugs in encoders which cannot be detected automatically
  236. /* the following fields are managed internally by the encoder */
  237. /** bit output */
  238. PutBitContext pb;
  239. /* sequence parameters */
  240. int context_initialized;
  241. int input_picture_number; ///< used to set pic->display_picture_number, shouldnt be used for/by anything else
  242. int coded_picture_number; ///< used to set pic->coded_picture_number, shouldnt be used for/by anything else
  243. int picture_number; //FIXME remove, unclear definition
  244. int picture_in_gop_number; ///< 0-> first pic in gop, ...
  245. int b_frames_since_non_b; ///< used for encoding, relative to not yet reordered input
  246. int mb_width, mb_height; ///< number of MBs horizontally & vertically
  247. int mb_stride; ///< mb_width+1 used for some arrays to allow simple addressng of left & top MBs withoutt sig11
  248. int b8_stride; ///< 2*mb_width+1 used for some 8x8 block arrays to allow simple addressng
  249. int b4_stride; ///< 4*mb_width+1 used for some 4x4 block arrays to allow simple addressng
  250. int h_edge_pos, v_edge_pos;///< horizontal / vertical position of the right/bottom edge (pixel replicateion)
  251. int mb_num; ///< number of MBs of a picture
  252. int linesize; ///< line size, in bytes, may be different from width
  253. int uvlinesize; ///< line size, for chroma in bytes, may be different from width
  254. Picture *picture; ///< main picture buffer
  255. Picture **input_picture; ///< next pictures on display order for encoding
  256. Picture **reordered_input_picture; ///< pointer to the next pictures in codedorder for encoding
  257. /**
  258. * copy of the previous picture structure.
  259. * note, linesize & data, might not match the previous picture (for field pictures)
  260. */
  261. Picture last_picture;
  262. /**
  263. * copy of the next picture structure.
  264. * note, linesize & data, might not match the next picture (for field pictures)
  265. */
  266. Picture next_picture;
  267. /**
  268. * copy of the source picture structure for encoding.
  269. * note, linesize & data, might not match the source picture (for field pictures)
  270. */
  271. Picture new_picture;
  272. /**
  273. * copy of the current picture structure.
  274. * note, linesize & data, might not match the current picture (for field pictures)
  275. */
  276. Picture current_picture; ///< buffer to store the decompressed current picture
  277. Picture *last_picture_ptr; ///< pointer to the previous picture.
  278. Picture *next_picture_ptr; ///< pointer to the next picture (for bidir pred)
  279. Picture *current_picture_ptr; ///< pointer to the current picture
  280. uint8_t *visualization_buffer[3]; //< temporary buffer vor MV visualization
  281. int last_dc[3]; ///< last DC values for MPEG1
  282. int16_t *dc_val[3]; ///< used for mpeg4 DC prediction, all 3 arrays must be continuous
  283. int16_t dc_cache[4*5];
  284. int y_dc_scale, c_dc_scale;
  285. uint8_t *y_dc_scale_table; ///< qscale -> y_dc_scale table
  286. uint8_t *c_dc_scale_table; ///< qscale -> c_dc_scale table
  287. const uint8_t *chroma_qscale_table; ///< qscale -> chroma_qscale (h263)
  288. uint8_t *coded_block; ///< used for coded block pattern prediction (msmpeg4v3, wmv1)
  289. int16_t (*ac_val[3])[16]; ///< used for for mpeg4 AC prediction, all 3 arrays must be continuous
  290. int ac_pred;
  291. uint8_t *prev_pict_types; ///< previous picture types in bitstream order, used for mb skip
  292. #define PREV_PICT_TYPES_BUFFER_SIZE 256
  293. int mb_skiped; ///< MUST BE SET only during DECODING
  294. uint8_t *mbskip_table; /**< used to avoid copy if macroblock skipped (for black regions for example)
  295. and used for b-frame encoding & decoding (contains skip table of next P Frame) */
  296. uint8_t *mbintra_table; ///< used to avoid setting {ac, dc, cbp}-pred stuff to zero on inter MB decoding
  297. uint8_t *cbp_table; ///< used to store cbp, ac_pred for partitioned decoding
  298. uint8_t *pred_dir_table; ///< used to store pred_dir for partitioned decoding
  299. uint8_t *allocated_edge_emu_buffer;
  300. uint8_t *edge_emu_buffer; ///< points into the middle of allocated_edge_emu_buffer
  301. int qscale; ///< QP
  302. int chroma_qscale; ///< chroma QP
  303. int lambda; ///< lagrange multipler used in rate distortion
  304. int lambda2; ///< (lambda*lambda) >> FF_LAMBDA_SHIFT
  305. int *lambda_table;
  306. int adaptive_quant; ///< use adaptive quantization
  307. int dquant; ///< qscale difference to prev qscale
  308. int pict_type; ///< I_TYPE, P_TYPE, B_TYPE, ...
  309. int last_pict_type;
  310. int last_non_b_pict_type; ///< used for mpeg4 gmc b-frames & ratecontrol
  311. int frame_rate_index;
  312. /* motion compensation */
  313. int unrestricted_mv; ///< mv can point outside of the coded picture
  314. int h263_long_vectors; ///< use horrible h263v1 long vector mode
  315. int decode; ///< if 0 then decoding will be skiped (for encoding b frames for example)
  316. DSPContext dsp; ///< pointers for accelerated dsp fucntions
  317. int f_code; ///< forward MV resolution
  318. int b_code; ///< backward MV resolution for B Frames (mpeg4)
  319. int16_t (*p_mv_table_base)[2];
  320. int16_t (*b_forw_mv_table_base)[2];
  321. int16_t (*b_back_mv_table_base)[2];
  322. int16_t (*b_bidir_forw_mv_table_base)[2];
  323. int16_t (*b_bidir_back_mv_table_base)[2];
  324. int16_t (*b_direct_mv_table_base)[2];
  325. int16_t (*p_field_mv_table_base[2][2])[2];
  326. int16_t (*b_field_mv_table_base[2][2][2])[2];
  327. int16_t (*p_mv_table)[2]; ///< MV table (1MV per MB) p-frame encoding
  328. int16_t (*b_forw_mv_table)[2]; ///< MV table (1MV per MB) forward mode b-frame encoding
  329. int16_t (*b_back_mv_table)[2]; ///< MV table (1MV per MB) backward mode b-frame encoding
  330. int16_t (*b_bidir_forw_mv_table)[2]; ///< MV table (1MV per MB) bidir mode b-frame encoding
  331. int16_t (*b_bidir_back_mv_table)[2]; ///< MV table (1MV per MB) bidir mode b-frame encoding
  332. int16_t (*b_direct_mv_table)[2]; ///< MV table (1MV per MB) direct mode b-frame encoding
  333. int16_t (*p_field_mv_table[2][2])[2]; ///< MV table (2MV per MB) interlaced p-frame encoding
  334. int16_t (*b_field_mv_table[2][2][2])[2];///< MV table (4MV per MB) interlaced b-frame encoding
  335. uint8_t (*p_field_select_table[2]);
  336. uint8_t (*b_field_select_table[2][2]);
  337. int me_method; ///< ME algorithm
  338. int scene_change_score;
  339. int mv_dir;
  340. #define MV_DIR_BACKWARD 1
  341. #define MV_DIR_FORWARD 2
  342. #define MV_DIRECT 4 ///< bidirectional mode where the difference equals the MV of the last P/S/I-Frame (mpeg4)
  343. int mv_type;
  344. #define MV_TYPE_16X16 0 ///< 1 vector for the whole mb
  345. #define MV_TYPE_8X8 1 ///< 4 vectors (h263, mpeg4 4MV)
  346. #define MV_TYPE_16X8 2 ///< 2 vectors, one per 16x8 block
  347. #define MV_TYPE_FIELD 3 ///< 2 vectors, one per field
  348. #define MV_TYPE_DMV 4 ///< 2 vectors, special mpeg2 Dual Prime Vectors
  349. /**motion vectors for a macroblock
  350. first coordinate : 0 = forward 1 = backward
  351. second " : depend on type
  352. third " : 0 = x, 1 = y
  353. */
  354. int mv[2][4][2];
  355. int field_select[2][2];
  356. int last_mv[2][2][2]; ///< last MV, used for MV prediction in MPEG1 & B-frame MPEG4
  357. uint8_t *fcode_tab; ///< smallest fcode needed for each MV
  358. MotionEstContext me;
  359. int no_rounding; /**< apply no rounding to motion compensation (MPEG4, msmpeg4, ...)
  360. for b-frames rounding mode is allways 0 */
  361. int hurry_up; /**< when set to 1 during decoding, b frames will be skiped
  362. when set to 2 idct/dequant will be skipped too */
  363. /* macroblock layer */
  364. int mb_x, mb_y;
  365. int mb_skip_run;
  366. int mb_intra;
  367. uint16_t *mb_type; ///< Table for candidate MB types for encoding
  368. #define CANDIDATE_MB_TYPE_INTRA 0x01
  369. #define CANDIDATE_MB_TYPE_INTER 0x02
  370. #define CANDIDATE_MB_TYPE_INTER4V 0x04
  371. #define CANDIDATE_MB_TYPE_SKIPED 0x08
  372. //#define MB_TYPE_GMC 0x10
  373. #define CANDIDATE_MB_TYPE_DIRECT 0x10
  374. #define CANDIDATE_MB_TYPE_FORWARD 0x20
  375. #define CANDIDATE_MB_TYPE_BACKWARD 0x40
  376. #define CANDIDATE_MB_TYPE_BIDIR 0x80
  377. #define CANDIDATE_MB_TYPE_INTER_I 0x100
  378. #define CANDIDATE_MB_TYPE_FORWARD_I 0x200
  379. #define CANDIDATE_MB_TYPE_BACKWARD_I 0x400
  380. #define CANDIDATE_MB_TYPE_BIDIR_I 0x800
  381. int block_index[6]; ///< index to current MB in block based arrays with edges
  382. int block_wrap[6];
  383. uint8_t *dest[3];
  384. int *mb_index2xy; ///< mb_index -> mb_x + mb_y*mb_stride
  385. /** matrix transmitted in the bitstream */
  386. uint16_t intra_matrix[64];
  387. uint16_t chroma_intra_matrix[64];
  388. uint16_t inter_matrix[64];
  389. uint16_t chroma_inter_matrix[64];
  390. #define QUANT_BIAS_SHIFT 8
  391. int intra_quant_bias; ///< bias for the quantizer
  392. int inter_quant_bias; ///< bias for the quantizer
  393. int min_qcoeff; ///< minimum encodable coefficient
  394. int max_qcoeff; ///< maximum encodable coefficient
  395. int ac_esc_length; ///< num of bits needed to encode the longest esc
  396. uint8_t *intra_ac_vlc_length;
  397. uint8_t *intra_ac_vlc_last_length;
  398. uint8_t *inter_ac_vlc_length;
  399. uint8_t *inter_ac_vlc_last_length;
  400. uint8_t *luma_dc_vlc_length;
  401. uint8_t *chroma_dc_vlc_length;
  402. #define UNI_AC_ENC_INDEX(run,level) ((run)*128 + (level))
  403. int coded_score[6];
  404. /** precomputed matrix (combine qscale and DCT renorm) */
  405. int (*q_intra_matrix)[64];
  406. int (*q_inter_matrix)[64];
  407. /** identical to the above but for MMX & these are not permutated, second 64 entries are bias*/
  408. uint16_t (*q_intra_matrix16)[2][64];
  409. uint16_t (*q_inter_matrix16)[2][64];
  410. int block_last_index[6]; ///< last non zero coefficient in block
  411. /* scantables */
  412. ScanTable __align8 intra_scantable;
  413. ScanTable intra_h_scantable;
  414. ScanTable intra_v_scantable;
  415. ScanTable inter_scantable; ///< if inter == intra then intra should be used to reduce tha cache usage
  416. /* noise reduction */
  417. int (*dct_error_sum)[64];
  418. int dct_count[2];
  419. uint16_t (*dct_offset)[64];
  420. void *opaque; ///< private data for the user
  421. /* bit rate control */
  422. int I_frame_bits; //FIXME used in mpeg12 ...
  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 fake_picture_number; ///< picture number at the bitstream frame rate
  558. int gop_picture_number; ///< index of the first picture of a GOP based on fake_pic_num & mpeg1 specific
  559. int last_mv_dir; ///< last mv_dir, used for b frame encoding
  560. int broken_link; ///< no_output_of_prior_pics_flag
  561. uint8_t *vbv_delay_ptr; ///< pointer to vbv_delay in the bitstream
  562. /* MPEG2 specific - I wish I had not to support this mess. */
  563. int progressive_sequence;
  564. int mpeg_f_code[2][2];
  565. int picture_structure;
  566. /* picture type */
  567. #define PICT_TOP_FIELD 1
  568. #define PICT_BOTTOM_FIELD 2
  569. #define PICT_FRAME 3
  570. int intra_dc_precision;
  571. int frame_pred_frame_dct;
  572. int top_field_first;
  573. int concealment_motion_vectors;
  574. int q_scale_type;
  575. int intra_vlc_format;
  576. int alternate_scan;
  577. int repeat_first_field;
  578. int chroma_420_type;
  579. int progressive_frame;
  580. int full_pel[2];
  581. int interlaced_dct;
  582. int first_slice;
  583. int first_field; ///< is 1 for the first field of a field picture 0 otherwise
  584. /* RTP specific */
  585. int rtp_mode;
  586. uint8_t *ptr_lastgob;
  587. int swap_uv;//vcr2 codec is mpeg2 varint with UV swaped
  588. short * pblocks[12];
  589. DCTELEM (*block)[64]; ///< points to one of the following blocks
  590. DCTELEM (*blocks)[6][64]; // for HQ mode we need to keep the best block
  591. int (*decode_mb)(struct MpegEncContext *s, DCTELEM block[6][64]); // used by some codecs to avoid a switch()
  592. #define SLICE_OK 0
  593. #define SLICE_ERROR -1
  594. #define SLICE_END -2 ///<end marker found
  595. #define SLICE_NOEND -3 ///<no end marker or error found but mb count exceeded
  596. void (*dct_unquantize_mpeg1_intra)(struct MpegEncContext *s,
  597. DCTELEM *block/*align 16*/, int n, int qscale);
  598. void (*dct_unquantize_mpeg1_inter)(struct MpegEncContext *s,
  599. DCTELEM *block/*align 16*/, int n, int qscale);
  600. void (*dct_unquantize_mpeg2_intra)(struct MpegEncContext *s,
  601. DCTELEM *block/*align 16*/, int n, int qscale);
  602. void (*dct_unquantize_mpeg2_inter)(struct MpegEncContext *s,
  603. DCTELEM *block/*align 16*/, int n, int qscale);
  604. void (*dct_unquantize_h263_intra)(struct MpegEncContext *s,
  605. DCTELEM *block/*align 16*/, int n, int qscale);
  606. void (*dct_unquantize_h263_inter)(struct MpegEncContext *s,
  607. DCTELEM *block/*align 16*/, int n, int qscale);
  608. void (*dct_unquantize_intra)(struct MpegEncContext *s, // unquantizer to use (mpeg4 can use both)
  609. DCTELEM *block/*align 16*/, int n, int qscale);
  610. void (*dct_unquantize_inter)(struct MpegEncContext *s, // unquantizer to use (mpeg4 can use both)
  611. DCTELEM *block/*align 16*/, int n, int qscale);
  612. int (*dct_quantize)(struct MpegEncContext *s, DCTELEM *block/*align 16*/, int n, int qscale, int *overflow);
  613. int (*fast_dct_quantize)(struct MpegEncContext *s, DCTELEM *block/*align 16*/, int n, int qscale, int *overflow);
  614. void (*denoise_dct)(struct MpegEncContext *s, DCTELEM *block);
  615. } MpegEncContext;
  616. int DCT_common_init(MpegEncContext *s);
  617. int MPV_common_init(MpegEncContext *s);
  618. void MPV_common_end(MpegEncContext *s);
  619. void MPV_decode_mb(MpegEncContext *s, DCTELEM block[6][64]);
  620. int MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx);
  621. void MPV_frame_end(MpegEncContext *s);
  622. int MPV_encode_init(AVCodecContext *avctx);
  623. int MPV_encode_end(AVCodecContext *avctx);
  624. int MPV_encode_picture(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data);
  625. #ifdef HAVE_MMX
  626. void MPV_common_init_mmx(MpegEncContext *s);
  627. #endif
  628. #ifdef ARCH_ALPHA
  629. void MPV_common_init_axp(MpegEncContext *s);
  630. #endif
  631. #ifdef HAVE_MLIB
  632. void MPV_common_init_mlib(MpegEncContext *s);
  633. #endif
  634. #ifdef HAVE_MMI
  635. void MPV_common_init_mmi(MpegEncContext *s);
  636. #endif
  637. #ifdef ARCH_ARMV4L
  638. void MPV_common_init_armv4l(MpegEncContext *s);
  639. #endif
  640. #ifdef ARCH_POWERPC
  641. void MPV_common_init_ppc(MpegEncContext *s);
  642. #endif
  643. extern void (*draw_edges)(uint8_t *buf, int wrap, int width, int height, int w);
  644. void ff_copy_bits(PutBitContext *pb, uint8_t *src, int length);
  645. void ff_clean_intra_table_entries(MpegEncContext *s);
  646. void ff_init_scantable(uint8_t *, ScanTable *st, const uint8_t *src_scantable);
  647. void ff_draw_horiz_band(MpegEncContext *s, int y, int h);
  648. void ff_emulated_edge_mc(uint8_t *buf, uint8_t *src, int linesize, int block_w, int block_h,
  649. int src_x, int src_y, int w, int h);
  650. #define END_NOT_FOUND -100
  651. int ff_combine_frame( MpegEncContext *s, int next, uint8_t **buf, int *buf_size);
  652. void ff_mpeg_flush(AVCodecContext *avctx);
  653. void ff_print_debug_info(MpegEncContext *s, AVFrame *pict);
  654. void ff_write_quant_matrix(PutBitContext *pb, int16_t *matrix);
  655. int ff_find_unused_picture(MpegEncContext *s, int shared);
  656. void ff_denoise_dct(MpegEncContext *s, DCTELEM *block);
  657. void ff_er_frame_start(MpegEncContext *s);
  658. void ff_er_frame_end(MpegEncContext *s);
  659. void ff_er_add_slice(MpegEncContext *s, int startx, int starty, int endx, int endy, int status);
  660. extern enum PixelFormat ff_yuv420p_list[2];
  661. void ff_init_block_index(MpegEncContext *s);
  662. static inline void ff_update_block_index(MpegEncContext *s){
  663. s->block_index[0]+=2;
  664. s->block_index[1]+=2;
  665. s->block_index[2]+=2;
  666. s->block_index[3]+=2;
  667. s->block_index[4]++;
  668. s->block_index[5]++;
  669. s->dest[0]+= 16;
  670. s->dest[1]+= 8;
  671. s->dest[2]+= 8;
  672. }
  673. static inline int get_bits_diff(MpegEncContext *s){
  674. const int bits= get_bit_count(&s->pb);
  675. const int last= s->last_bits;
  676. s->last_bits = bits;
  677. return bits - last;
  678. }
  679. /* motion_est.c */
  680. void ff_estimate_p_frame_motion(MpegEncContext * s,
  681. int mb_x, int mb_y);
  682. void ff_estimate_b_frame_motion(MpegEncContext * s,
  683. int mb_x, int mb_y);
  684. int ff_get_best_fcode(MpegEncContext * s, int16_t (*mv_table)[2], int type);
  685. void ff_fix_long_p_mvs(MpegEncContext * s);
  686. void ff_fix_long_mvs(MpegEncContext * s, uint8_t *field_select_table, int field_select,
  687. int16_t (*mv_table)[2], int f_code, int type, int truncate);
  688. void ff_init_me(MpegEncContext *s);
  689. int ff_pre_estimate_p_frame_motion(MpegEncContext * s, int mb_x, int mb_y);
  690. /* mpeg12.c */
  691. extern const int16_t ff_mpeg1_default_intra_matrix[64];
  692. extern const int16_t ff_mpeg1_default_non_intra_matrix[64];
  693. extern uint8_t ff_mpeg1_dc_scale_table[128];
  694. void mpeg1_encode_picture_header(MpegEncContext *s, int picture_number);
  695. void mpeg1_encode_mb(MpegEncContext *s,
  696. DCTELEM block[6][64],
  697. int motion_x, int motion_y);
  698. void ff_mpeg1_encode_init(MpegEncContext *s);
  699. void ff_mpeg1_encode_slice_header(MpegEncContext *s);
  700. void ff_mpeg1_clean_buffers(MpegEncContext *s);
  701. /** RLTable. */
  702. typedef struct RLTable {
  703. int n; ///< number of entries of table_vlc minus 1
  704. int last; ///< number of values for last = 0
  705. const uint16_t (*table_vlc)[2];
  706. const int8_t *table_run;
  707. const int8_t *table_level;
  708. uint8_t *index_run[2]; ///< encoding only
  709. int8_t *max_level[2]; ///< encoding & decoding
  710. int8_t *max_run[2]; ///< encoding & decoding
  711. VLC vlc; ///< decoding only deprected FIXME remove
  712. RL_VLC_ELEM *rl_vlc[32]; ///< decoding only
  713. } RLTable;
  714. void init_rl(RLTable *rl);
  715. void init_vlc_rl(RLTable *rl);
  716. static inline int get_rl_index(const RLTable *rl, int last, int run, int level)
  717. {
  718. int index;
  719. index = rl->index_run[last][run];
  720. if (index >= rl->n)
  721. return rl->n;
  722. if (level > rl->max_level[last][run])
  723. return rl->n;
  724. return index + level - 1;
  725. }
  726. extern uint8_t ff_mpeg4_y_dc_scale_table[32];
  727. extern uint8_t ff_mpeg4_c_dc_scale_table[32];
  728. extern uint8_t ff_aic_dc_scale_table[32];
  729. extern const int16_t ff_mpeg4_default_intra_matrix[64];
  730. extern const int16_t ff_mpeg4_default_non_intra_matrix[64];
  731. extern const uint8_t ff_h263_chroma_qscale_table[32];
  732. extern const uint8_t ff_h263_loop_filter_strength[32];
  733. int ff_h263_decode_init(AVCodecContext *avctx);
  734. int ff_h263_decode_frame(AVCodecContext *avctx,
  735. void *data, int *data_size,
  736. uint8_t *buf, int buf_size);
  737. int ff_h263_decode_end(AVCodecContext *avctx);
  738. void h263_encode_mb(MpegEncContext *s,
  739. DCTELEM block[6][64],
  740. int motion_x, int motion_y);
  741. void mpeg4_encode_mb(MpegEncContext *s,
  742. DCTELEM block[6][64],
  743. int motion_x, int motion_y);
  744. void h263_encode_picture_header(MpegEncContext *s, int picture_number);
  745. void ff_flv_encode_picture_header(MpegEncContext *s, int picture_number);
  746. void h263_encode_gob_header(MpegEncContext * s, int mb_line);
  747. int16_t *h263_pred_motion(MpegEncContext * s, int block,
  748. int *px, int *py);
  749. void mpeg4_pred_ac(MpegEncContext * s, DCTELEM *block, int n,
  750. int dir);
  751. void ff_set_mpeg4_time(MpegEncContext * s, int picture_number);
  752. void mpeg4_encode_picture_header(MpegEncContext *s, int picture_number);
  753. void h263_encode_init(MpegEncContext *s);
  754. void h263_decode_init_vlc(MpegEncContext *s);
  755. int h263_decode_picture_header(MpegEncContext *s);
  756. int ff_h263_decode_gob_header(MpegEncContext *s);
  757. int ff_mpeg4_decode_picture_header(MpegEncContext * s, GetBitContext *gb);
  758. void ff_h263_update_motion_val(MpegEncContext * s);
  759. void ff_h263_loop_filter(MpegEncContext * s);
  760. void ff_set_qscale(MpegEncContext * s, int qscale);
  761. int ff_h263_decode_mba(MpegEncContext *s);
  762. void ff_h263_encode_mba(MpegEncContext *s);
  763. int intel_h263_decode_picture_header(MpegEncContext *s);
  764. int flv_h263_decode_picture_header(MpegEncContext *s);
  765. int ff_h263_decode_mb(MpegEncContext *s,
  766. DCTELEM block[6][64]);
  767. int ff_mpeg4_decode_mb(MpegEncContext *s,
  768. DCTELEM block[6][64]);
  769. int h263_get_picture_format(int width, int height);
  770. void ff_mpeg4_encode_video_packet_header(MpegEncContext *s);
  771. void ff_mpeg4_clean_buffers(MpegEncContext *s);
  772. void ff_mpeg4_stuffing(PutBitContext * pbc);
  773. void ff_mpeg4_init_partitions(MpegEncContext *s);
  774. void ff_mpeg4_merge_partitions(MpegEncContext *s);
  775. void ff_clean_mpeg4_qscales(MpegEncContext *s);
  776. void ff_clean_h263_qscales(MpegEncContext *s);
  777. int ff_mpeg4_decode_partitions(MpegEncContext *s);
  778. int ff_mpeg4_get_video_packet_prefix_length(MpegEncContext *s);
  779. int ff_h263_resync(MpegEncContext *s);
  780. int ff_h263_get_gob_height(MpegEncContext *s);
  781. int ff_mpeg4_set_direct_mv(MpegEncContext *s, int mx, int my);
  782. inline int ff_h263_round_chroma(int x);
  783. /* rv10.c */
  784. void rv10_encode_picture_header(MpegEncContext *s, int picture_number);
  785. int rv_decode_dc(MpegEncContext *s, int n);
  786. /* msmpeg4.c */
  787. void msmpeg4_encode_picture_header(MpegEncContext * s, int picture_number);
  788. void msmpeg4_encode_ext_header(MpegEncContext * s);
  789. void msmpeg4_encode_mb(MpegEncContext * s,
  790. DCTELEM block[6][64],
  791. int motion_x, int motion_y);
  792. int msmpeg4_decode_picture_header(MpegEncContext * s);
  793. int msmpeg4_decode_ext_header(MpegEncContext * s, int buf_size);
  794. int ff_msmpeg4_decode_init(MpegEncContext *s);
  795. void ff_msmpeg4_encode_init(MpegEncContext *s);
  796. int ff_wmv2_decode_picture_header(MpegEncContext * s);
  797. int ff_wmv2_decode_secondary_picture_header(MpegEncContext * s);
  798. void ff_wmv2_add_mb(MpegEncContext *s, DCTELEM block[6][64], uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr);
  799. void ff_mspel_motion(MpegEncContext *s,
  800. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  801. uint8_t **ref_picture, op_pixels_func (*pix_op)[4],
  802. int motion_x, int motion_y, int h);
  803. int ff_wmv2_encode_picture_header(MpegEncContext * s, int picture_number);
  804. void ff_wmv2_encode_mb(MpegEncContext * s,
  805. DCTELEM block[6][64],
  806. int motion_x, int motion_y);
  807. /* mjpeg.c */
  808. int mjpeg_init(MpegEncContext *s);
  809. void mjpeg_close(MpegEncContext *s);
  810. void mjpeg_encode_mb(MpegEncContext *s,
  811. DCTELEM block[6][64]);
  812. void mjpeg_picture_header(MpegEncContext *s);
  813. void mjpeg_picture_trailer(MpegEncContext *s);
  814. /* rate control */
  815. int ff_rate_control_init(MpegEncContext *s);
  816. float ff_rate_estimate_qscale(MpegEncContext *s);
  817. void ff_write_pass1_stats(MpegEncContext *s);
  818. void ff_rate_control_uninit(MpegEncContext *s);
  819. double ff_eval(char *s, double *const_value, const char **const_name,
  820. double (**func1)(void *, double), const char **func1_name,
  821. double (**func2)(void *, double, double), char **func2_name,
  822. void *opaque);
  823. int ff_vbv_update(MpegEncContext *s, int frame_size);
  824. #endif /* AVCODEC_MPEGVIDEO_H */