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.

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