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.

389 lines
14KB

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