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.

459 lines
18KB

  1. /*
  2. * Generic DCT based hybrid video encoder
  3. * Copyright (c) 2000,2001 Gerard Lantau.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program 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
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. /* Macros for picture code type. */
  20. #define I_TYPE 1
  21. #define P_TYPE 2
  22. #define B_TYPE 3
  23. #define S_TYPE 4 //S(GMC)-VOP MPEG4
  24. enum OutputFormat {
  25. FMT_MPEG1,
  26. FMT_H263,
  27. FMT_MJPEG,
  28. };
  29. #define MPEG_BUF_SIZE (16 * 1024)
  30. #define QMAT_SHIFT_MMX 19
  31. #define QMAT_SHIFT 25
  32. #define MAX_FCODE 7
  33. #define MAX_MV 2048
  34. #define REORDER_BUFFER_SIZE (FF_MAX_B_FRAMES+2)
  35. typedef struct Predictor{
  36. double coeff;
  37. double count;
  38. double decay;
  39. } Predictor;
  40. typedef struct ReorderBuffer{
  41. UINT8 *picture[3];
  42. int pict_type;
  43. int qscale;
  44. int force_type;
  45. int picture_number;
  46. int picture_in_gop_number;
  47. } ReorderBuffer;
  48. typedef struct MpegEncContext {
  49. struct AVCodecContext *avctx;
  50. /* the following parameters must be initialized before encoding */
  51. int width, height; /* picture size. must be a multiple of 16 */
  52. int gop_size;
  53. int frame_rate; /* number of frames per second */
  54. int intra_only; /* if true, only intra pictures are generated */
  55. int bit_rate; /* wanted bit rate */
  56. int bit_rate_tolerance; /* amount of +- bits (>0)*/
  57. enum OutputFormat out_format; /* output format */
  58. int h263_plus; /* h263 plus headers */
  59. int h263_rv10; /* use RV10 variation for H263 */
  60. int h263_pred; /* use mpeg4/h263 ac/dc predictions */
  61. int h263_msmpeg4; /* generate MSMPEG4 compatible stream */
  62. int h263_intel; /* use I263 intel h263 header */
  63. int fixed_qscale; /* fixed qscale if non zero */
  64. float qcompress; /* amount of qscale change between easy & hard scenes (0.0-1.0) */
  65. float qblur; /* amount of qscale smoothing over time (0.0-1.0) */
  66. int qmin; /* min qscale */
  67. int qmax; /* max qscale */
  68. int max_qdiff; /* max qscale difference between frames */
  69. int encoding; /* true if we are encoding (vs decoding) */
  70. int flags; /* AVCodecContext.flags (HQ, MV4, ...) */
  71. int force_input_type;/* 0= no force, otherwise I_TYPE, P_TYPE, ... */
  72. int max_b_frames; /* max number of b-frames for encoding */
  73. /* the following fields are managed internally by the encoder */
  74. /* bit output */
  75. PutBitContext pb;
  76. /* sequence parameters */
  77. int context_initialized;
  78. int input_picture_number;
  79. int input_picture_in_gop_number; /* 0-> first pic in gop, ... */
  80. int picture_number;
  81. int fake_picture_number; /* picture number at the bitstream frame rate */
  82. int gop_picture_number; /* index of the first picture of a GOP based on fake_pic_num & mpeg1 specific */
  83. int picture_in_gop_number; /* 0-> first pic in gop, ... */
  84. int b_frames_since_non_b; /* used for encoding, relative to not yet reordered input */
  85. int mb_width, mb_height; /* number of MBs horizontally & vertically */
  86. int mb_num; /* number of MBs of a picture */
  87. int linesize; /* line size, in bytes, may be different from width */
  88. UINT8 *new_picture[3]; /* picture to be compressed */
  89. UINT8 *picture_buffer[REORDER_BUFFER_SIZE][3]; /* internal buffers used for reordering of input pictures */
  90. int picture_buffer_index;
  91. ReorderBuffer coded_order[REORDER_BUFFER_SIZE];
  92. UINT8 *last_picture[3]; /* previous picture */
  93. UINT8 *last_picture_base[3]; /* real start of the picture */
  94. UINT8 *next_picture[3]; /* previous picture (for bidir pred) */
  95. UINT8 *next_picture_base[3]; /* real start of the picture */
  96. UINT8 *aux_picture[3]; /* aux picture (for B frames only) */
  97. UINT8 *aux_picture_base[3]; /* real start of the picture */
  98. UINT8 *current_picture[3]; /* buffer to store the decompressed current picture */
  99. int last_dc[3]; /* last DC values for MPEG1 */
  100. INT16 *dc_val[3]; /* used for mpeg4 DC prediction, all 3 arrays must be continuous */
  101. int y_dc_scale, c_dc_scale;
  102. UINT8 *coded_block; /* used for coded block pattern prediction (msmpeg4v3, wmv1)*/
  103. INT16 (*ac_val[3])[16]; /* used for for mpeg4 AC prediction, all 3 arrays must be continuous */
  104. int ac_pred;
  105. int mb_skiped; /* MUST BE SET only during DECODING */
  106. UINT8 *mbskip_table; /* used to avoid copy if macroblock skipped (for black regions for example)
  107. and used for b-frame encoding & decoding (contains skip table of next P Frame) */
  108. UINT8 *mbintra_table; /* used to avoid setting {ac, dc, cbp}-pred stuff to zero on inter MB decoding */
  109. int input_qscale; /* qscale prior to reordering of frames */
  110. int input_pict_type; /* pict_type prior to reordering of frames */
  111. int force_type; /* 0= no force, otherwise I_TYPE, P_TYPE, ... */
  112. int qscale; /* QP */
  113. int pict_type; /* I_TYPE, P_TYPE, B_TYPE, ... */
  114. int last_non_b_pict_type; /* used for mpeg4 gmc b-frames */
  115. int last_pict_type; /* used for bit rate stuff (needs that to update the right predictor) */
  116. int frame_rate_index;
  117. /* motion compensation */
  118. int unrestricted_mv;
  119. int h263_long_vectors; /* use horrible h263v1 long vector mode */
  120. int f_code; /* forward MV resolution */
  121. int b_code; /* backward MV resolution for B Frames (mpeg4) */
  122. INT16 (*motion_val)[2]; /* used for MV prediction (4MV per MB) */
  123. INT16 (*p_mv_table)[2]; /* MV table (1MV per MB) p-frame encoding */
  124. INT16 (*last_p_mv_table)[2]; /* MV table (1MV per MB) p-frame encoding */
  125. INT16 (*b_forw_mv_table)[2]; /* MV table (1MV per MB) forward mode b-frame encoding */
  126. INT16 (*b_back_mv_table)[2]; /* MV table (1MV per MB) backward mode b-frame encoding */
  127. INT16 (*b_bidir_forw_mv_table)[2]; /* MV table (1MV per MB) bidir mode b-frame encoding */
  128. INT16 (*b_bidir_back_mv_table)[2]; /* MV table (1MV per MB) bidir mode b-frame encoding */
  129. INT16 (*b_direct_forw_mv_table)[2];/* MV table (1MV per MB) direct mode b-frame encoding */
  130. INT16 (*b_direct_back_mv_table)[2];/* MV table (1MV per MB) direct mode b-frame encoding */
  131. INT16 (*b_direct_mv_table)[2]; /* MV table (1MV per MB) direct mode b-frame encoding */
  132. int me_method; /* ME algorithm */
  133. int mv_dir;
  134. #define MV_DIR_BACKWARD 1
  135. #define MV_DIR_FORWARD 2
  136. #define MV_DIRECT 4 // bidirectional mode where the difference equals the MV of the last P/S/I-Frame (mpeg4)
  137. int mv_type;
  138. #define MV_TYPE_16X16 0 /* 1 vector for the whole mb */
  139. #define MV_TYPE_8X8 1 /* 4 vectors (h263, mpeg4 4MV) */
  140. #define MV_TYPE_16X8 2 /* 2 vectors, one per 16x8 block */
  141. #define MV_TYPE_FIELD 3 /* 2 vectors, one per field */
  142. #define MV_TYPE_DMV 4 /* 2 vectors, special mpeg2 Dual Prime Vectors */
  143. /* motion vectors for a macroblock
  144. first coordinate : 0 = forward 1 = backward
  145. second " : depend on type
  146. third " : 0 = x, 1 = y
  147. */
  148. int mv[2][4][2];
  149. int field_select[2][2];
  150. int last_mv[2][2][2]; /* last MV, used for MV prediction in MPEG1 & B-frame MPEG4 */
  151. UINT16 (*mv_penalty)[MAX_MV*2+1]; /* amount of bits needed to encode a MV, used for ME */
  152. UINT8 *fcode_tab; /* smallest fcode needed for each MV */
  153. int has_b_frames;
  154. int no_rounding; /* apply no rounding to motion compensation (MPEG4, msmpeg4, ...) */
  155. /* macroblock layer */
  156. int mb_x, mb_y;
  157. int mb_incr;
  158. int mb_intra;
  159. UINT16 *mb_var; /* Table for MB variances */
  160. UINT8 *mb_type; /* Table for MB type */
  161. #define MB_TYPE_INTRA 0x01
  162. #define MB_TYPE_INTER 0x02
  163. #define MB_TYPE_INTER4V 0x04
  164. #define MB_TYPE_SKIPED 0x08
  165. #define MB_TYPE_DIRECT 0x10
  166. #define MB_TYPE_FORWARD 0x20
  167. #define MB_TYPE_BACKWARD 0x40
  168. #define MB_TYPE_BIDIR 0x80
  169. int block_index[6]; /* index to current MB in block based arrays with edges*/
  170. int block_wrap[6];
  171. /* matrix transmitted in the bitstream */
  172. UINT16 intra_matrix[64];
  173. UINT16 chroma_intra_matrix[64];
  174. UINT16 non_intra_matrix[64];
  175. UINT16 chroma_non_intra_matrix[64];
  176. /* precomputed matrix (combine qscale and DCT renorm) */
  177. int q_intra_matrix[64];
  178. int q_non_intra_matrix[64];
  179. /* identical to the above but for MMX & these are not permutated */
  180. UINT16 __align8 q_intra_matrix16[64] ;
  181. UINT16 __align8 q_non_intra_matrix16[64];
  182. int block_last_index[6]; /* last non zero coefficient in block */
  183. void *opaque; /* private data for the user */
  184. /* bit rate control */
  185. int I_frame_bits; //FIXME used in mpeg12 ...
  186. int avg_mb_var; /* average MB variance for current frame */
  187. int mc_mb_var; /* motion compensated MB variance for current frame */
  188. int last_mc_mb_var; /* motion compensated MB variance for last frame */
  189. INT64 wanted_bits;
  190. INT64 total_bits;
  191. int frame_bits; /* bits used for the current frame */
  192. int last_frame_bits; /* bits used for the last frame */
  193. Predictor i_pred;
  194. Predictor p_pred;
  195. double qsum; /* sum of qscales */
  196. double qcount; /* count of qscales */
  197. double short_term_qsum; /* sum of recent qscales */
  198. double short_term_qcount; /* count of recent qscales */
  199. /* statistics, used for 2-pass encoding */
  200. int mv_bits;
  201. int header_bits;
  202. int i_tex_bits;
  203. int p_tex_bits;
  204. int i_count;
  205. int p_count;
  206. int skip_count;
  207. int misc_bits; // cbp, mb_type
  208. int last_bits; //temp var used for calculating the above vars
  209. /* H.263 specific */
  210. int gob_number;
  211. int gob_index;
  212. int first_gob_line;
  213. /* H.263+ specific */
  214. int umvplus;
  215. int umvplus_dec;
  216. int h263_aic; /* Advanded INTRA Coding (AIC) */
  217. int h263_aic_dir; /* AIC direction: 0 = left, 1 = top */
  218. /* mpeg4 specific */
  219. int time_increment_resolution;
  220. int time_increment_bits; /* number of bits to represent the fractional part of time */
  221. int last_time_base;
  222. int time_base; /* time in seconds of last I,P,S Frame */
  223. int64_t time; /* time of current frame */
  224. int64_t last_non_b_time;
  225. uint16_t pp_time; /* time distance between the last 2 p,s,i frames */
  226. uint16_t bp_time; /* time distance between the last b and p,s,i frame */
  227. int shape;
  228. int vol_sprite_usage;
  229. int sprite_width;
  230. int sprite_height;
  231. int sprite_left;
  232. int sprite_top;
  233. int sprite_brightness_change;
  234. int num_sprite_warping_points;
  235. int real_sprite_warping_points;
  236. int sprite_offset[2][2];
  237. int sprite_delta[2][2][2];
  238. int sprite_shift[2][2];
  239. int mcsel;
  240. int quant_precision;
  241. int quarter_sample; /* 1->qpel, 0->half pel ME/MC */
  242. int scalability;
  243. int new_pred;
  244. int reduced_res_vop;
  245. int aspect_ratio_info;
  246. int sprite_warping_accuracy;
  247. int low_latency_sprite;
  248. int data_partioning;
  249. int resync_marker;
  250. int resync_x_pos;
  251. /* divx specific, used to workaround (many) bugs in divx5 */
  252. int divx_version;
  253. int divx_build;
  254. /* RV10 specific */
  255. int rv10_version; /* RV10 version: 0 or 3 */
  256. int rv10_first_dc_coded[3];
  257. /* MJPEG specific */
  258. struct MJpegContext *mjpeg_ctx;
  259. int mjpeg_vsample[3]; /* vertical sampling factors, default = {2, 1, 1} */
  260. int mjpeg_hsample[3]; /* horizontal sampling factors, default = {2, 1, 1} */
  261. int mjpeg_write_tables; /* do we want to have quantisation- and
  262. huffmantables in the jpeg file ? */
  263. /* MSMPEG4 specific */
  264. int mv_table_index;
  265. int rl_table_index;
  266. int rl_chroma_table_index;
  267. int dc_table_index;
  268. int use_skip_mb_code;
  269. int slice_height; /* in macroblocks */
  270. int first_slice_line; /* used in mpeg4 too to handle resync markers */
  271. int flipflop_rounding;
  272. int bitrate;
  273. int msmpeg4_version; /* 1=mp41, 2=mp42, 3=mp43/divx3 */
  274. /* decompression specific */
  275. GetBitContext gb;
  276. /* MPEG2 specific - I wish I had not to support this mess. */
  277. int progressive_sequence;
  278. int mpeg_f_code[2][2];
  279. int picture_structure;
  280. /* picture type */
  281. #define PICT_TOP_FIELD 1
  282. #define PICT_BOTTOM_FIELD 2
  283. #define PICT_FRAME 3
  284. int intra_dc_precision;
  285. int frame_pred_frame_dct;
  286. int top_field_first;
  287. int concealment_motion_vectors;
  288. int q_scale_type;
  289. int intra_vlc_format;
  290. int alternate_scan;
  291. int repeat_first_field;
  292. int chroma_420_type;
  293. int progressive_frame;
  294. int mpeg2;
  295. int full_pel[2];
  296. int interlaced_dct;
  297. int last_qscale;
  298. int first_slice;
  299. /* RTP specific */
  300. /* These are explained on avcodec.h */
  301. int rtp_mode;
  302. int rtp_payload_size;
  303. void (*rtp_callback)(void *data, int size, int packet_number);
  304. UINT8 *ptr_lastgob;
  305. UINT8 *ptr_last_mb_line;
  306. UINT32 mb_line_avgsize;
  307. DCTELEM (*block)[64]; /* points to one of the following blocks */
  308. DCTELEM intra_block[6][64] __align8;
  309. DCTELEM inter_block[6][64] __align8;
  310. DCTELEM inter4v_block[6][64] __align8;
  311. void (*dct_unquantize_mpeg)(struct MpegEncContext *s,
  312. DCTELEM *block, int n, int qscale);
  313. void (*dct_unquantize_h263)(struct MpegEncContext *s,
  314. DCTELEM *block, int n, int qscale);
  315. void (*dct_unquantize)(struct MpegEncContext *s, // unquantizer to use (mpeg4 can use both)
  316. DCTELEM *block, int n, int qscale);
  317. } MpegEncContext;
  318. int MPV_common_init(MpegEncContext *s);
  319. void MPV_common_end(MpegEncContext *s);
  320. void MPV_decode_mb(MpegEncContext *s, DCTELEM block[6][64]);
  321. void MPV_frame_start(MpegEncContext *s);
  322. void MPV_frame_end(MpegEncContext *s);
  323. #ifdef HAVE_MMX
  324. void MPV_common_init_mmx(MpegEncContext *s);
  325. #endif
  326. /* motion_est.c */
  327. void ff_estimate_p_frame_motion(MpegEncContext * s,
  328. int mb_x, int mb_y);
  329. void ff_estimate_b_frame_motion(MpegEncContext * s,
  330. int mb_x, int mb_y);
  331. int ff_get_best_fcode(MpegEncContext * s, int16_t (*mv_table)[2], int type);
  332. void ff_fix_long_p_mvs(MpegEncContext * s);
  333. void ff_fix_long_b_mvs(MpegEncContext * s, int16_t (*mv_table)[2], int f_code, int type);
  334. /* mpeg12.c */
  335. extern INT16 default_intra_matrix[64];
  336. extern INT16 default_non_intra_matrix[64];
  337. void mpeg1_encode_picture_header(MpegEncContext *s, int picture_number);
  338. void mpeg1_encode_mb(MpegEncContext *s,
  339. DCTELEM block[6][64],
  340. int motion_x, int motion_y);
  341. void mpeg1_encode_init(MpegEncContext *s);
  342. /* h263enc.c */
  343. /* run length table */
  344. #define MAX_RUN 64
  345. #define MAX_LEVEL 64
  346. typedef struct RLTable {
  347. int n; /* number of entries of table_vlc minus 1 */
  348. int last; /* number of values for last = 0 */
  349. const UINT16 (*table_vlc)[2];
  350. const INT8 *table_run;
  351. const INT8 *table_level;
  352. UINT8 *index_run[2]; /* encoding only */
  353. INT8 *max_level[2]; /* encoding & decoding */
  354. INT8 *max_run[2]; /* encoding & decoding */
  355. VLC vlc; /* decoding only */
  356. } RLTable;
  357. void init_rl(RLTable *rl);
  358. void init_vlc_rl(RLTable *rl);
  359. static inline int get_rl_index(const RLTable *rl, int last, int run, int level)
  360. {
  361. int index;
  362. index = rl->index_run[last][run];
  363. if (index >= rl->n)
  364. return rl->n;
  365. if (level > rl->max_level[last][run])
  366. return rl->n;
  367. return index + level - 1;
  368. }
  369. void h263_encode_mb(MpegEncContext *s,
  370. DCTELEM block[6][64],
  371. int motion_x, int motion_y);
  372. void mpeg4_encode_mb(MpegEncContext *s,
  373. DCTELEM block[6][64],
  374. int motion_x, int motion_y);
  375. void h263_encode_picture_header(MpegEncContext *s, int picture_number);
  376. int h263_encode_gob_header(MpegEncContext * s, int mb_line);
  377. void h263_dc_scale(MpegEncContext *s);
  378. INT16 *h263_pred_motion(MpegEncContext * s, int block,
  379. int *px, int *py);
  380. void mpeg4_pred_ac(MpegEncContext * s, INT16 *block, int n,
  381. int dir);
  382. void mpeg4_encode_picture_header(MpegEncContext *s, int picture_number);
  383. void h263_encode_init(MpegEncContext *s);
  384. void h263_decode_init_vlc(MpegEncContext *s);
  385. int h263_decode_picture_header(MpegEncContext *s);
  386. int h263_decode_gob_header(MpegEncContext *s);
  387. int mpeg4_decode_picture_header(MpegEncContext * s);
  388. int intel_h263_decode_picture_header(MpegEncContext *s);
  389. int h263_decode_mb(MpegEncContext *s,
  390. DCTELEM block[6][64]);
  391. int h263_get_picture_format(int width, int height);
  392. /* rv10.c */
  393. void rv10_encode_picture_header(MpegEncContext *s, int picture_number);
  394. int rv_decode_dc(MpegEncContext *s, int n);
  395. /* msmpeg4.c */
  396. void msmpeg4_encode_picture_header(MpegEncContext * s, int picture_number);
  397. void msmpeg4_encode_ext_header(MpegEncContext * s);
  398. void msmpeg4_encode_mb(MpegEncContext * s,
  399. DCTELEM block[6][64],
  400. int motion_x, int motion_y);
  401. void msmpeg4_dc_scale(MpegEncContext * s);
  402. int msmpeg4_decode_picture_header(MpegEncContext * s);
  403. int msmpeg4_decode_ext_header(MpegEncContext * s, int buf_size);
  404. int msmpeg4_decode_mb(MpegEncContext *s,
  405. DCTELEM block[6][64]);
  406. int msmpeg4_decode_init_vlc(MpegEncContext *s);
  407. /* mjpegenc.c */
  408. int mjpeg_init(MpegEncContext *s);
  409. void mjpeg_close(MpegEncContext *s);
  410. void mjpeg_encode_mb(MpegEncContext *s,
  411. DCTELEM block[6][64]);
  412. void mjpeg_picture_header(MpegEncContext *s);
  413. void mjpeg_picture_trailer(MpegEncContext *s);