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.

906 lines
37KB

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