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.

366 lines
12KB

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