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.

909 lines
38KB

  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; ///< used to set pic->display_picture_number, shouldnt be used for/by anything else
  235. int coded_picture_number; ///< used to set pic->coded_picture_number, shouldnt be used for/by anything else
  236. int picture_number; //FIXME remove, unclear definition
  237. int picture_in_gop_number; ///< 0-> first pic in gop, ...
  238. int b_frames_since_non_b; ///< used for encoding, relative to not yet reordered input
  239. int mb_width, mb_height; ///< number of MBs horizontally & vertically
  240. int mb_stride; ///< mb_width+1 used for some arrays to allow simple addressng of left & top MBs withoutt sig11
  241. int b8_stride; ///< 2*mb_width+1 used for some 8x8 block arrays to allow simple addressng
  242. int b4_stride; ///< 4*mb_width+1 used for some 4x4 block arrays to allow simple addressng
  243. int h_edge_pos, v_edge_pos;///< horizontal / vertical position of the right/bottom edge (pixel replicateion)
  244. int mb_num; ///< number of MBs of a picture
  245. int linesize; ///< line size, in bytes, may be different from width
  246. int uvlinesize; ///< line size, for chroma in bytes, may be different from width
  247. Picture *picture; ///< main picture buffer
  248. Picture **input_picture; ///< next pictures on display order for encoding
  249. Picture **reordered_input_picture; ///< pointer to the next pictures in codedorder for encoding
  250. /**
  251. * copy of the previous picture structure.
  252. * note, linesize & data, might not match the previous picture (for field pictures)
  253. */
  254. Picture last_picture;
  255. /**
  256. * copy of the next picture structure.
  257. * note, linesize & data, might not match the next picture (for field pictures)
  258. */
  259. Picture next_picture;
  260. /**
  261. * copy of the source picture structure for encoding.
  262. * note, linesize & data, might not match the source picture (for field pictures)
  263. */
  264. Picture new_picture;
  265. /**
  266. * copy of the current picture structure.
  267. * note, linesize & data, might not match the current picture (for field pictures)
  268. */
  269. Picture current_picture; ///< buffer to store the decompressed current picture
  270. Picture *last_picture_ptr; ///< pointer to the previous picture.
  271. Picture *next_picture_ptr; ///< pointer to the next picture (for bidir pred)
  272. Picture *current_picture_ptr; ///< pointer to the current picture
  273. uint8_t *visualization_buffer[3]; //< temporary buffer vor MV visualization
  274. int last_dc[3]; ///< last DC values for MPEG1
  275. int16_t *dc_val[3]; ///< used for mpeg4 DC prediction, all 3 arrays must be continuous
  276. int16_t dc_cache[4*5];
  277. int y_dc_scale, c_dc_scale;
  278. uint8_t *y_dc_scale_table; ///< qscale -> y_dc_scale table
  279. uint8_t *c_dc_scale_table; ///< qscale -> c_dc_scale table
  280. const uint8_t *chroma_qscale_table; ///< qscale -> chroma_qscale (h263)
  281. uint8_t *coded_block; ///< used for coded block pattern prediction (msmpeg4v3, wmv1)
  282. int16_t (*ac_val[3])[16]; ///< used for for mpeg4 AC prediction, all 3 arrays must be continuous
  283. int ac_pred;
  284. uint8_t *prev_pict_types; ///< previous picture types in bitstream order, used for mb skip
  285. #define PREV_PICT_TYPES_BUFFER_SIZE 256
  286. int mb_skiped; ///< MUST BE SET only during DECODING
  287. uint8_t *mbskip_table; /**< used to avoid copy if macroblock skipped (for black regions for example)
  288. and used for b-frame encoding & decoding (contains skip table of next P Frame) */
  289. uint8_t *mbintra_table; ///< used to avoid setting {ac, dc, cbp}-pred stuff to zero on inter MB decoding
  290. uint8_t *cbp_table; ///< used to store cbp, ac_pred for partitioned decoding
  291. uint8_t *pred_dir_table; ///< used to store pred_dir for partitioned decoding
  292. uint8_t *allocated_edge_emu_buffer;
  293. uint8_t *edge_emu_buffer; ///< points into the middle of allocated_edge_emu_buffer
  294. int qscale; ///< QP
  295. int chroma_qscale; ///< chroma QP
  296. int lambda; ///< lagrange multipler used in rate distortion
  297. int lambda2; ///< (lambda*lambda) >> FF_LAMBDA_SHIFT
  298. int *lambda_table;
  299. int adaptive_quant; ///< use adaptive quantization
  300. int dquant; ///< qscale difference to prev qscale
  301. int pict_type; ///< I_TYPE, P_TYPE, B_TYPE, ...
  302. int last_pict_type;
  303. int last_non_b_pict_type; ///< used for mpeg4 gmc b-frames & ratecontrol
  304. int frame_rate_index;
  305. /* motion compensation */
  306. int unrestricted_mv; ///< mv can point outside of the coded picture
  307. int h263_long_vectors; ///< use horrible h263v1 long vector mode
  308. int decode; ///< if 0 then decoding will be skiped (for encoding b frames for example)
  309. DSPContext dsp; ///< pointers for accelerated dsp fucntions
  310. int f_code; ///< forward MV resolution
  311. int b_code; ///< backward MV resolution for B Frames (mpeg4)
  312. int16_t (*p_mv_table_base)[2];
  313. int16_t (*b_forw_mv_table_base)[2];
  314. int16_t (*b_back_mv_table_base)[2];
  315. int16_t (*b_bidir_forw_mv_table_base)[2];
  316. int16_t (*b_bidir_back_mv_table_base)[2];
  317. int16_t (*b_direct_mv_table_base)[2];
  318. int16_t (*p_mv_table)[2]; ///< MV table (1MV per MB) p-frame encoding
  319. int16_t (*b_forw_mv_table)[2]; ///< MV table (1MV per MB) forward mode b-frame encoding
  320. int16_t (*b_back_mv_table)[2]; ///< MV table (1MV per MB) backward mode b-frame encoding
  321. int16_t (*b_bidir_forw_mv_table)[2]; ///< MV table (1MV per MB) bidir mode b-frame encoding
  322. int16_t (*b_bidir_back_mv_table)[2]; ///< MV table (1MV per MB) bidir mode b-frame encoding
  323. int16_t (*b_direct_mv_table)[2]; ///< MV table (1MV per MB) direct mode b-frame encoding
  324. int me_method; ///< ME algorithm
  325. int scene_change_score;
  326. int mv_dir;
  327. #define MV_DIR_BACKWARD 1
  328. #define MV_DIR_FORWARD 2
  329. #define MV_DIRECT 4 ///< bidirectional mode where the difference equals the MV of the last P/S/I-Frame (mpeg4)
  330. int mv_type;
  331. #define MV_TYPE_16X16 0 ///< 1 vector for the whole mb
  332. #define MV_TYPE_8X8 1 ///< 4 vectors (h263, mpeg4 4MV)
  333. #define MV_TYPE_16X8 2 ///< 2 vectors, one per 16x8 block
  334. #define MV_TYPE_FIELD 3 ///< 2 vectors, one per field
  335. #define MV_TYPE_DMV 4 ///< 2 vectors, special mpeg2 Dual Prime Vectors
  336. /**motion vectors for a macroblock
  337. first coordinate : 0 = forward 1 = backward
  338. second " : depend on type
  339. third " : 0 = x, 1 = y
  340. */
  341. int mv[2][4][2];
  342. int field_select[2][2];
  343. int last_mv[2][2][2]; ///< last MV, used for MV prediction in MPEG1 & B-frame MPEG4
  344. uint8_t *fcode_tab; ///< smallest fcode needed for each MV
  345. MotionEstContext me;
  346. int no_rounding; /**< apply no rounding to motion compensation (MPEG4, msmpeg4, ...)
  347. for b-frames rounding mode is allways 0 */
  348. int hurry_up; /**< when set to 1 during decoding, b frames will be skiped
  349. when set to 2 idct/dequant will be skipped too */
  350. /* macroblock layer */
  351. int mb_x, mb_y;
  352. int mb_skip_run;
  353. int mb_intra;
  354. uint8_t *mb_type; ///< Table for MB type FIXME remove and use picture->mb_type
  355. #define MB_TYPE_INTRA 0x01
  356. #define MB_TYPE_INTER 0x02
  357. #define MB_TYPE_INTER4V 0x04
  358. #define MB_TYPE_SKIPED 0x08
  359. //#define MB_TYPE_GMC 0x10
  360. #define MB_TYPE_DIRECT 0x10
  361. #define MB_TYPE_FORWARD 0x20
  362. #define MB_TYPE_BACKWARD 0x40
  363. #define MB_TYPE_BIDIR 0x80
  364. int block_index[6]; ///< index to current MB in block based arrays with edges
  365. int block_wrap[6];
  366. uint8_t *dest[3];
  367. int *mb_index2xy; ///< mb_index -> mb_x + mb_y*mb_stride
  368. /** matrix transmitted in the bitstream */
  369. uint16_t intra_matrix[64];
  370. uint16_t chroma_intra_matrix[64];
  371. uint16_t inter_matrix[64];
  372. uint16_t chroma_inter_matrix[64];
  373. #define QUANT_BIAS_SHIFT 8
  374. int intra_quant_bias; ///< bias for the quantizer
  375. int inter_quant_bias; ///< bias for the quantizer
  376. int min_qcoeff; ///< minimum encodable coefficient
  377. int max_qcoeff; ///< maximum encodable coefficient
  378. int ac_esc_length; ///< num of bits needed to encode the longest esc
  379. uint8_t *intra_ac_vlc_length;
  380. uint8_t *intra_ac_vlc_last_length;
  381. uint8_t *inter_ac_vlc_length;
  382. uint8_t *inter_ac_vlc_last_length;
  383. uint8_t *luma_dc_vlc_length;
  384. uint8_t *chroma_dc_vlc_length;
  385. #define UNI_AC_ENC_INDEX(run,level) ((run)*128 + (level))
  386. int coded_score[6];
  387. /** precomputed matrix (combine qscale and DCT renorm) */
  388. int (*q_intra_matrix)[64];
  389. int (*q_inter_matrix)[64];
  390. /** identical to the above but for MMX & these are not permutated, second 64 entries are bias*/
  391. uint16_t (*q_intra_matrix16)[2][64];
  392. uint16_t (*q_inter_matrix16)[2][64];
  393. int block_last_index[6]; ///< last non zero coefficient in block
  394. /* scantables */
  395. ScanTable __align8 intra_scantable;
  396. ScanTable intra_h_scantable;
  397. ScanTable intra_v_scantable;
  398. ScanTable inter_scantable; ///< if inter == intra then intra should be used to reduce tha cache usage
  399. /* noise reduction */
  400. int (*dct_error_sum)[64];
  401. int dct_count[2];
  402. uint16_t (*dct_offset)[64];
  403. void *opaque; ///< private data for the user
  404. /* bit rate control */
  405. int I_frame_bits; //FIXME used in mpeg12 ...
  406. int64_t wanted_bits;
  407. int64_t total_bits;
  408. int frame_bits; ///< bits used for the current frame
  409. RateControlContext rc_context; ///< contains stuff only accessed in ratecontrol.c
  410. /* statistics, used for 2-pass encoding */
  411. int mv_bits;
  412. int header_bits;
  413. int i_tex_bits;
  414. int p_tex_bits;
  415. int i_count;
  416. int f_count;
  417. int b_count;
  418. int skip_count;
  419. int misc_bits; ///< cbp, mb_type
  420. int last_bits; ///< temp var used for calculating the above vars
  421. /* error concealment / resync */
  422. int error_count;
  423. uint8_t *error_status_table; ///< table of the error status of each MB
  424. #define VP_START 1 ///< current MB is the first after a resync marker
  425. #define AC_ERROR 2
  426. #define DC_ERROR 4
  427. #define MV_ERROR 8
  428. #define AC_END 16
  429. #define DC_END 32
  430. #define MV_END 64
  431. //FIXME some prefix?
  432. int resync_mb_x; ///< x position of last resync marker
  433. int resync_mb_y; ///< y position of last resync marker
  434. GetBitContext last_resync_gb; ///< used to search for the next resync marker
  435. int mb_num_left; ///< number of MBs left in this video packet (for partitioned Slices only)
  436. int next_p_frame_damaged; ///< set if the next p frame is damaged, to avoid showing trashed b frames
  437. int error_resilience;
  438. ParseContext parse_context;
  439. /* H.263 specific */
  440. int gob_index;
  441. int obmc; ///< overlapped block motion compensation
  442. /* H.263+ specific */
  443. int umvplus; ///< == H263+ && unrestricted_mv
  444. int h263_aic; ///< Advanded INTRA Coding (AIC)
  445. int h263_aic_dir; ///< AIC direction: 0 = left, 1 = top
  446. int h263_slice_structured;
  447. int alt_inter_vlc; ///< alternative inter vlc
  448. int modified_quant;
  449. int loop_filter;
  450. /* mpeg4 specific */
  451. int time_increment_resolution;
  452. int time_increment_bits; ///< number of bits to represent the fractional part of time
  453. int last_time_base;
  454. int time_base; ///< time in seconds of last I,P,S Frame
  455. int64_t time; ///< time of current frame
  456. int64_t last_non_b_time;
  457. uint16_t pp_time; ///< time distance between the last 2 p,s,i frames
  458. uint16_t pb_time; ///< time distance between the last b and p,s,i frame
  459. uint16_t pp_field_time;
  460. uint16_t pb_field_time; ///< like above, just for interlaced
  461. int shape;
  462. int vol_sprite_usage;
  463. int sprite_width;
  464. int sprite_height;
  465. int sprite_left;
  466. int sprite_top;
  467. int sprite_brightness_change;
  468. int num_sprite_warping_points;
  469. int real_sprite_warping_points;
  470. int sprite_offset[2][2]; ///< sprite offset[isChroma][isMVY]
  471. int sprite_delta[2][2]; ///< sprite_delta [isY][isMVY]
  472. int sprite_shift[2]; ///< sprite shift [isChroma]
  473. int mcsel;
  474. int quant_precision;
  475. int quarter_sample; ///< 1->qpel, 0->half pel ME/MC
  476. int scalability;
  477. int hierachy_type;
  478. int enhancement_type;
  479. int new_pred;
  480. int reduced_res_vop;
  481. int aspect_ratio_info; //FIXME remove
  482. int sprite_warping_accuracy;
  483. int low_latency_sprite;
  484. int data_partitioning; ///< data partitioning flag from header
  485. int partitioned_frame; ///< is current frame partitioned
  486. int rvlc; ///< reversible vlc
  487. int resync_marker; ///< could this stream contain resync markers
  488. int low_delay; ///< no reordering needed / has no b-frames
  489. int vo_type;
  490. int vol_control_parameters; ///< does the stream contain the low_delay flag, used to workaround buggy encoders
  491. int intra_dc_threshold; ///< QP above whch the ac VLC should be used for intra dc
  492. PutBitContext tex_pb; ///< used for data partitioned VOPs
  493. PutBitContext pb2; ///< used for data partitioned VOPs
  494. #define PB_BUFFER_SIZE 1024*256
  495. uint8_t *tex_pb_buffer;
  496. uint8_t *pb2_buffer;
  497. int mpeg_quant;
  498. int16_t (*field_mv_table)[2][2]; ///< used for interlaced b frame decoding
  499. int8_t (*field_select_table)[2]; ///< wtf, no really another table for interlaced b frames
  500. int t_frame; ///< time distance of first I -> B, used for interlaced b frames
  501. int padding_bug_score; ///< used to detect the VERY common padding bug in MPEG4
  502. /* divx specific, used to workaround (many) bugs in divx5 */
  503. int divx_version;
  504. int divx_build;
  505. int divx_packed;
  506. #define BITSTREAM_BUFFER_SIZE 1024*256
  507. uint8_t *bitstream_buffer; //Divx 5.01 puts several frames in a single one, this is used to reorder them
  508. int bitstream_buffer_size;
  509. int xvid_build;
  510. /* lavc specific stuff, used to workaround bugs in libavcodec */
  511. int ffmpeg_version;
  512. int lavc_build;
  513. /* RV10 specific */
  514. int rv10_version; ///< RV10 version: 0 or 3
  515. int rv10_first_dc_coded[3];
  516. /* MJPEG specific */
  517. struct MJpegContext *mjpeg_ctx;
  518. int mjpeg_vsample[3]; ///< vertical sampling factors, default = {2, 1, 1}
  519. int mjpeg_hsample[3]; ///< horizontal sampling factors, default = {2, 1, 1}
  520. int mjpeg_write_tables; ///< do we want to have quantisation- and huffmantables in the jpeg file ?
  521. int mjpeg_data_only_frames; ///< frames only with SOI, SOS and EOI markers
  522. /* MSMPEG4 specific */
  523. int mv_table_index;
  524. int rl_table_index;
  525. int rl_chroma_table_index;
  526. int dc_table_index;
  527. int use_skip_mb_code;
  528. int slice_height; ///< in macroblocks
  529. int first_slice_line; ///< used in mpeg4 too to handle resync markers
  530. int flipflop_rounding;
  531. int msmpeg4_version; ///< 0=not msmpeg4, 1=mp41, 2=mp42, 3=mp43/divx3 4=wmv1/7 5=wmv2/8
  532. int per_mb_rl_table;
  533. int esc3_level_length;
  534. int esc3_run_length;
  535. /** [mb_intra][isChroma][level][run][last] */
  536. int (*ac_stats)[2][MAX_LEVEL+1][MAX_RUN+1][2];
  537. int inter_intra_pred;
  538. int mspel;
  539. /* decompression specific */
  540. GetBitContext gb;
  541. /* Mpeg1 specific */
  542. int fake_picture_number; ///< picture number at the bitstream frame rate
  543. int gop_picture_number; ///< index of the first picture of a GOP based on fake_pic_num & mpeg1 specific
  544. int last_mv_dir; ///< last mv_dir, used for b frame encoding
  545. int broken_link; ///< no_output_of_prior_pics_flag
  546. uint8_t *vbv_delay_ptr; ///< pointer to vbv_delay in the bitstream
  547. /* MPEG2 specific - I wish I had not to support this mess. */
  548. int progressive_sequence;
  549. int mpeg_f_code[2][2];
  550. int picture_structure;
  551. /* picture type */
  552. #define PICT_TOP_FIELD 1
  553. #define PICT_BOTTOM_FIELD 2
  554. #define PICT_FRAME 3
  555. int intra_dc_precision;
  556. int frame_pred_frame_dct;
  557. int top_field_first;
  558. int concealment_motion_vectors;
  559. int q_scale_type;
  560. int intra_vlc_format;
  561. int alternate_scan;
  562. int repeat_first_field;
  563. int chroma_420_type;
  564. int progressive_frame;
  565. int full_pel[2];
  566. int interlaced_dct;
  567. int first_slice;
  568. int first_field; ///< is 1 for the first field of a field picture 0 otherwise
  569. /* RTP specific */
  570. int rtp_mode;
  571. uint8_t *ptr_lastgob;
  572. int swap_uv;//vcr2 codec is mpeg2 varint with UV swaped
  573. short * pblocks[12];
  574. DCTELEM (*block)[64]; ///< points to one of the following blocks
  575. DCTELEM (*blocks)[6][64]; // for HQ mode we need to keep the best block
  576. int (*decode_mb)(struct MpegEncContext *s, DCTELEM block[6][64]); // used by some codecs to avoid a switch()
  577. #define SLICE_OK 0
  578. #define SLICE_ERROR -1
  579. #define SLICE_END -2 ///<end marker found
  580. #define SLICE_NOEND -3 ///<no end marker or error found but mb count exceeded
  581. void (*dct_unquantize_mpeg1_intra)(struct MpegEncContext *s,
  582. DCTELEM *block/*align 16*/, int n, int qscale);
  583. void (*dct_unquantize_mpeg1_inter)(struct MpegEncContext *s,
  584. DCTELEM *block/*align 16*/, int n, int qscale);
  585. void (*dct_unquantize_mpeg2_intra)(struct MpegEncContext *s,
  586. DCTELEM *block/*align 16*/, int n, int qscale);
  587. void (*dct_unquantize_mpeg2_inter)(struct MpegEncContext *s,
  588. DCTELEM *block/*align 16*/, int n, int qscale);
  589. void (*dct_unquantize_h263_intra)(struct MpegEncContext *s,
  590. DCTELEM *block/*align 16*/, int n, int qscale);
  591. void (*dct_unquantize_h263_inter)(struct MpegEncContext *s,
  592. DCTELEM *block/*align 16*/, int n, int qscale);
  593. void (*dct_unquantize_intra)(struct MpegEncContext *s, // unquantizer to use (mpeg4 can use both)
  594. DCTELEM *block/*align 16*/, int n, int qscale);
  595. void (*dct_unquantize_inter)(struct MpegEncContext *s, // unquantizer to use (mpeg4 can use both)
  596. DCTELEM *block/*align 16*/, int n, int qscale);
  597. int (*dct_quantize)(struct MpegEncContext *s, DCTELEM *block/*align 16*/, int n, int qscale, int *overflow);
  598. int (*fast_dct_quantize)(struct MpegEncContext *s, DCTELEM *block/*align 16*/, int n, int qscale, int *overflow);
  599. } MpegEncContext;
  600. int DCT_common_init(MpegEncContext *s);
  601. int MPV_common_init(MpegEncContext *s);
  602. void MPV_common_end(MpegEncContext *s);
  603. void MPV_decode_mb(MpegEncContext *s, DCTELEM block[6][64]);
  604. int MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx);
  605. void MPV_frame_end(MpegEncContext *s);
  606. int MPV_encode_init(AVCodecContext *avctx);
  607. int MPV_encode_end(AVCodecContext *avctx);
  608. int MPV_encode_picture(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data);
  609. #ifdef HAVE_MMX
  610. void MPV_common_init_mmx(MpegEncContext *s);
  611. #endif
  612. #ifdef ARCH_ALPHA
  613. void MPV_common_init_axp(MpegEncContext *s);
  614. #endif
  615. #ifdef HAVE_MLIB
  616. void MPV_common_init_mlib(MpegEncContext *s);
  617. #endif
  618. #ifdef HAVE_MMI
  619. void MPV_common_init_mmi(MpegEncContext *s);
  620. #endif
  621. #ifdef ARCH_ARMV4L
  622. void MPV_common_init_armv4l(MpegEncContext *s);
  623. #endif
  624. #ifdef ARCH_POWERPC
  625. void MPV_common_init_ppc(MpegEncContext *s);
  626. #endif
  627. extern void (*draw_edges)(uint8_t *buf, int wrap, int width, int height, int w);
  628. void ff_copy_bits(PutBitContext *pb, uint8_t *src, int length);
  629. void ff_clean_intra_table_entries(MpegEncContext *s);
  630. void ff_init_scantable(uint8_t *, ScanTable *st, const uint8_t *src_scantable);
  631. void ff_draw_horiz_band(MpegEncContext *s, int y, int h);
  632. void ff_emulated_edge_mc(uint8_t *buf, uint8_t *src, int linesize, int block_w, int block_h,
  633. int src_x, int src_y, int w, int h);
  634. #define END_NOT_FOUND -100
  635. int ff_combine_frame( MpegEncContext *s, int next, uint8_t **buf, int *buf_size);
  636. void ff_mpeg_flush(AVCodecContext *avctx);
  637. void ff_print_debug_info(MpegEncContext *s, AVFrame *pict);
  638. void ff_write_quant_matrix(PutBitContext *pb, int16_t *matrix);
  639. int ff_find_unused_picture(MpegEncContext *s, int shared);
  640. void ff_denoise_dct(MpegEncContext *s, DCTELEM *block);
  641. void ff_er_frame_start(MpegEncContext *s);
  642. void ff_er_frame_end(MpegEncContext *s);
  643. void ff_er_add_slice(MpegEncContext *s, int startx, int starty, int endx, int endy, int status);
  644. extern enum PixelFormat ff_yuv420p_list[2];
  645. void ff_init_block_index(MpegEncContext *s);
  646. static inline void ff_update_block_index(MpegEncContext *s){
  647. s->block_index[0]+=2;
  648. s->block_index[1]+=2;
  649. s->block_index[2]+=2;
  650. s->block_index[3]+=2;
  651. s->block_index[4]++;
  652. s->block_index[5]++;
  653. s->dest[0]+= 16;
  654. s->dest[1]+= 8;
  655. s->dest[2]+= 8;
  656. }
  657. static inline int get_bits_diff(MpegEncContext *s){
  658. const int bits= get_bit_count(&s->pb);
  659. const int last= s->last_bits;
  660. s->last_bits = bits;
  661. return bits - last;
  662. }
  663. /* motion_est.c */
  664. void ff_estimate_p_frame_motion(MpegEncContext * s,
  665. int mb_x, int mb_y);
  666. void ff_estimate_b_frame_motion(MpegEncContext * s,
  667. int mb_x, int mb_y);
  668. int ff_get_best_fcode(MpegEncContext * s, int16_t (*mv_table)[2], int type);
  669. void ff_fix_long_p_mvs(MpegEncContext * s);
  670. void ff_fix_long_b_mvs(MpegEncContext * s, int16_t (*mv_table)[2], int f_code, int type);
  671. void ff_init_me(MpegEncContext *s);
  672. int ff_pre_estimate_p_frame_motion(MpegEncContext * s, int mb_x, int mb_y);
  673. /* mpeg12.c */
  674. extern const int16_t ff_mpeg1_default_intra_matrix[64];
  675. extern const int16_t ff_mpeg1_default_non_intra_matrix[64];
  676. extern uint8_t ff_mpeg1_dc_scale_table[128];
  677. void mpeg1_encode_picture_header(MpegEncContext *s, int picture_number);
  678. void mpeg1_encode_mb(MpegEncContext *s,
  679. DCTELEM block[6][64],
  680. int motion_x, int motion_y);
  681. void ff_mpeg1_encode_init(MpegEncContext *s);
  682. void ff_mpeg1_encode_slice_header(MpegEncContext *s);
  683. void ff_mpeg1_clean_buffers(MpegEncContext *s);
  684. /** RLTable. */
  685. typedef struct RLTable {
  686. int n; ///< number of entries of table_vlc minus 1
  687. int last; ///< number of values for last = 0
  688. const uint16_t (*table_vlc)[2];
  689. const int8_t *table_run;
  690. const int8_t *table_level;
  691. uint8_t *index_run[2]; ///< encoding only
  692. int8_t *max_level[2]; ///< encoding & decoding
  693. int8_t *max_run[2]; ///< encoding & decoding
  694. VLC vlc; ///< decoding only deprected FIXME remove
  695. RL_VLC_ELEM *rl_vlc[32]; ///< decoding only
  696. } RLTable;
  697. void init_rl(RLTable *rl);
  698. void init_vlc_rl(RLTable *rl);
  699. static inline int get_rl_index(const RLTable *rl, int last, int run, int level)
  700. {
  701. int index;
  702. index = rl->index_run[last][run];
  703. if (index >= rl->n)
  704. return rl->n;
  705. if (level > rl->max_level[last][run])
  706. return rl->n;
  707. return index + level - 1;
  708. }
  709. extern uint8_t ff_mpeg4_y_dc_scale_table[32];
  710. extern uint8_t ff_mpeg4_c_dc_scale_table[32];
  711. extern uint8_t ff_aic_dc_scale_table[32];
  712. extern const int16_t ff_mpeg4_default_intra_matrix[64];
  713. extern const int16_t ff_mpeg4_default_non_intra_matrix[64];
  714. extern const uint8_t ff_h263_chroma_qscale_table[32];
  715. extern const uint8_t ff_h263_loop_filter_strength[32];
  716. int ff_h263_decode_init(AVCodecContext *avctx);
  717. int ff_h263_decode_frame(AVCodecContext *avctx,
  718. void *data, int *data_size,
  719. uint8_t *buf, int buf_size);
  720. int ff_h263_decode_end(AVCodecContext *avctx);
  721. void h263_encode_mb(MpegEncContext *s,
  722. DCTELEM block[6][64],
  723. int motion_x, int motion_y);
  724. void mpeg4_encode_mb(MpegEncContext *s,
  725. DCTELEM block[6][64],
  726. int motion_x, int motion_y);
  727. void h263_encode_picture_header(MpegEncContext *s, int picture_number);
  728. void ff_flv_encode_picture_header(MpegEncContext *s, int picture_number);
  729. void h263_encode_gob_header(MpegEncContext * s, int mb_line);
  730. int16_t *h263_pred_motion(MpegEncContext * s, int block,
  731. int *px, int *py);
  732. void mpeg4_pred_ac(MpegEncContext * s, DCTELEM *block, int n,
  733. int dir);
  734. void ff_set_mpeg4_time(MpegEncContext * s, int picture_number);
  735. void mpeg4_encode_picture_header(MpegEncContext *s, int picture_number);
  736. void h263_encode_init(MpegEncContext *s);
  737. void h263_decode_init_vlc(MpegEncContext *s);
  738. int h263_decode_picture_header(MpegEncContext *s);
  739. int ff_h263_decode_gob_header(MpegEncContext *s);
  740. int ff_mpeg4_decode_picture_header(MpegEncContext * s, GetBitContext *gb);
  741. void ff_h263_update_motion_val(MpegEncContext * s);
  742. void ff_h263_loop_filter(MpegEncContext * s);
  743. void ff_set_qscale(MpegEncContext * s, int qscale);
  744. int ff_h263_decode_mba(MpegEncContext *s);
  745. void ff_h263_encode_mba(MpegEncContext *s);
  746. int intel_h263_decode_picture_header(MpegEncContext *s);
  747. int flv_h263_decode_picture_header(MpegEncContext *s);
  748. int ff_h263_decode_mb(MpegEncContext *s,
  749. DCTELEM block[6][64]);
  750. int ff_mpeg4_decode_mb(MpegEncContext *s,
  751. DCTELEM block[6][64]);
  752. int h263_get_picture_format(int width, int height);
  753. void ff_mpeg4_encode_video_packet_header(MpegEncContext *s);
  754. void ff_mpeg4_clean_buffers(MpegEncContext *s);
  755. void ff_mpeg4_stuffing(PutBitContext * pbc);
  756. void ff_mpeg4_init_partitions(MpegEncContext *s);
  757. void ff_mpeg4_merge_partitions(MpegEncContext *s);
  758. void ff_clean_mpeg4_qscales(MpegEncContext *s);
  759. void ff_clean_h263_qscales(MpegEncContext *s);
  760. int ff_mpeg4_decode_partitions(MpegEncContext *s);
  761. int ff_mpeg4_get_video_packet_prefix_length(MpegEncContext *s);
  762. int ff_h263_resync(MpegEncContext *s);
  763. int ff_h263_get_gob_height(MpegEncContext *s);
  764. int ff_mpeg4_set_direct_mv(MpegEncContext *s, int mx, int my);
  765. inline int ff_h263_round_chroma(int x);
  766. /* rv10.c */
  767. void rv10_encode_picture_header(MpegEncContext *s, int picture_number);
  768. int rv_decode_dc(MpegEncContext *s, int n);
  769. /* msmpeg4.c */
  770. void msmpeg4_encode_picture_header(MpegEncContext * s, int picture_number);
  771. void msmpeg4_encode_ext_header(MpegEncContext * s);
  772. void msmpeg4_encode_mb(MpegEncContext * s,
  773. DCTELEM block[6][64],
  774. int motion_x, int motion_y);
  775. int msmpeg4_decode_picture_header(MpegEncContext * s);
  776. int msmpeg4_decode_ext_header(MpegEncContext * s, int buf_size);
  777. int ff_msmpeg4_decode_init(MpegEncContext *s);
  778. void ff_msmpeg4_encode_init(MpegEncContext *s);
  779. int ff_wmv2_decode_picture_header(MpegEncContext * s);
  780. int ff_wmv2_decode_secondary_picture_header(MpegEncContext * s);
  781. void ff_wmv2_add_mb(MpegEncContext *s, DCTELEM block[6][64], uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr);
  782. void ff_mspel_motion(MpegEncContext *s,
  783. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  784. uint8_t **ref_picture, op_pixels_func (*pix_op)[4],
  785. int motion_x, int motion_y, int h);
  786. int ff_wmv2_encode_picture_header(MpegEncContext * s, int picture_number);
  787. void ff_wmv2_encode_mb(MpegEncContext * s,
  788. DCTELEM block[6][64],
  789. int motion_x, int motion_y);
  790. /* mjpeg.c */
  791. int mjpeg_init(MpegEncContext *s);
  792. void mjpeg_close(MpegEncContext *s);
  793. void mjpeg_encode_mb(MpegEncContext *s,
  794. DCTELEM block[6][64]);
  795. void mjpeg_picture_header(MpegEncContext *s);
  796. void mjpeg_picture_trailer(MpegEncContext *s);
  797. /* rate control */
  798. int ff_rate_control_init(MpegEncContext *s);
  799. float ff_rate_estimate_qscale(MpegEncContext *s);
  800. void ff_write_pass1_stats(MpegEncContext *s);
  801. void ff_rate_control_uninit(MpegEncContext *s);
  802. double ff_eval(char *s, double *const_value, const char **const_name,
  803. double (**func1)(void *, double), const char **func1_name,
  804. double (**func2)(void *, double, double), char **func2_name,
  805. void *opaque);
  806. int ff_vbv_update(MpegEncContext *s, int frame_size);
  807. #endif /* AVCODEC_MPEGVIDEO_H */