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.

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