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.

495 lines
19KB

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