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.

426 lines
15KB

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