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.

353 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 */
  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 */
  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 frame_rate_index;
  80. /* motion compensation */
  81. int unrestricted_mv;
  82. int h263_long_vectors; /* use horrible h263v1 long vector mode */
  83. int f_code; /* resolution */
  84. int b_code; /* resolution for B Frames*/
  85. INT16 *mv_table[2]; /* MV table */
  86. INT16 (*motion_val)[2]; /* used for MV prediction */
  87. int full_search;
  88. int mv_dir;
  89. #define MV_DIR_BACKWARD 1
  90. #define MV_DIR_FORWARD 2
  91. int mv_type;
  92. #define MV_TYPE_16X16 0 /* 1 vector for the whole mb */
  93. #define MV_TYPE_8X8 1 /* 4 vectors (h263) */
  94. #define MV_TYPE_16X8 2 /* 2 vectors, one per 16x8 block */
  95. #define MV_TYPE_FIELD 3 /* 2 vectors, one per field */
  96. #define MV_TYPE_DMV 4 /* 2 vectors, special mpeg2 Dual Prime Vectors */
  97. /* motion vectors for a macroblock
  98. first coordinate : 0 = forward 1 = backward
  99. second " : depend on type
  100. third " : 0 = x, 1 = y
  101. */
  102. int mv[2][4][2];
  103. int field_select[2][2];
  104. int last_mv[2][2][2];
  105. int has_b_frames;
  106. int no_rounding; /* apply no rounding to motion estimation (MPEG4) */
  107. /* macroblock layer */
  108. int mb_x, mb_y;
  109. int mb_incr;
  110. int mb_intra;
  111. INT16 *mb_var; /* Table for MB variances */
  112. char *mb_type; /* Table for MB type */
  113. /* matrix transmitted in the bitstream */
  114. UINT16 intra_matrix[64];
  115. UINT16 chroma_intra_matrix[64];
  116. UINT16 non_intra_matrix[64];
  117. UINT16 chroma_non_intra_matrix[64];
  118. /* precomputed matrix (combine qscale and DCT renorm) */
  119. int q_intra_matrix[64];
  120. int q_non_intra_matrix[64];
  121. /* identical to the above but for MMX & these are not permutated */
  122. UINT16 __align8 q_intra_matrix16[64] ;
  123. UINT16 __align8 q_non_intra_matrix16[64];
  124. int block_last_index[6]; /* last non zero coefficient in block */
  125. void *opaque; /* private data for the user */
  126. /* bit rate control */
  127. int I_frame_bits; /* wanted number of bits per I frame */
  128. int P_frame_bits; /* same for P frame */
  129. int avg_mb_var; /* average MB variance for current frame */
  130. INT64 wanted_bits;
  131. INT64 total_bits;
  132. /* H.263 specific */
  133. int gob_number;
  134. int gob_index;
  135. int first_gob_line;
  136. /* H.263+ specific */
  137. int umvplus;
  138. int umvplus_dec;
  139. int h263_aic; /* Advanded INTRA Coding (AIC) */
  140. int h263_aic_dir; /* AIC direction: 0 = left, 1 = top */
  141. /* mpeg4 specific */
  142. int time_increment_bits;
  143. int shape;
  144. int vol_sprite_usage;
  145. int sprite_width;
  146. int sprite_height;
  147. int sprite_left;
  148. int sprite_top;
  149. int sprite_brightness_change;
  150. int num_sprite_warping_points;
  151. int real_sprite_warping_points;
  152. int sprite_offset[2][2];
  153. int sprite_delta[2][2][2];
  154. int sprite_shift[2][2];
  155. int mcsel;
  156. int quant_precision;
  157. int quarter_sample;
  158. int scalability;
  159. int new_pred;
  160. int reduced_res_vop;
  161. int aspect_ratio_info;
  162. int sprite_warping_accuracy;
  163. int low_latency_sprite;
  164. int data_partioning;
  165. /* divx specific, used to workaround (many) bugs in divx5 */
  166. int divx_version;
  167. int divx_build;
  168. /* RV10 specific */
  169. int rv10_version; /* RV10 version: 0 or 3 */
  170. int rv10_first_dc_coded[3];
  171. /* MJPEG specific */
  172. struct MJpegContext *mjpeg_ctx;
  173. int mjpeg_vsample[3]; /* vertical sampling factors, default = {2, 1, 1} */
  174. int mjpeg_hsample[3]; /* horizontal sampling factors, default = {2, 1, 1} */
  175. int mjpeg_write_tables; /* do we want to have quantisation- and
  176. huffmantables in the jpeg file ? */
  177. /* MSMPEG4 specific */
  178. int mv_table_index;
  179. int rl_table_index;
  180. int rl_chroma_table_index;
  181. int dc_table_index;
  182. int use_skip_mb_code;
  183. int slice_height; /* in macroblocks */
  184. int first_slice_line;
  185. int flipflop_rounding;
  186. int bitrate;
  187. /* decompression specific */
  188. GetBitContext gb;
  189. /* MPEG2 specific - I wish I had not to support this mess. */
  190. int progressive_sequence;
  191. int mpeg_f_code[2][2];
  192. int picture_structure;
  193. /* picture type */
  194. #define PICT_TOP_FIELD 1
  195. #define PICT_BOTTOM_FIELD 2
  196. #define PICT_FRAME 3
  197. int intra_dc_precision;
  198. int frame_pred_frame_dct;
  199. int top_field_first;
  200. int concealment_motion_vectors;
  201. int q_scale_type;
  202. int intra_vlc_format;
  203. int alternate_scan;
  204. int repeat_first_field;
  205. int chroma_420_type;
  206. int progressive_frame;
  207. int mpeg2;
  208. int full_pel[2];
  209. int interlaced_dct;
  210. int last_qscale;
  211. int first_slice;
  212. /* RTP specific */
  213. /* These are explained on avcodec.h */
  214. int rtp_mode;
  215. int rtp_payload_size;
  216. void (*rtp_callback)(void *data, int size, int packet_number);
  217. UINT8 *ptr_lastgob;
  218. UINT8 *ptr_last_mb_line;
  219. UINT32 mb_line_avgsize;
  220. DCTELEM block[6][64] __align8;
  221. void (*dct_unquantize)(struct MpegEncContext *s,
  222. DCTELEM *block, int n, int qscale);
  223. } MpegEncContext;
  224. int MPV_common_init(MpegEncContext *s);
  225. void MPV_common_end(MpegEncContext *s);
  226. void MPV_decode_mb(MpegEncContext *s, DCTELEM block[6][64]);
  227. void MPV_frame_start(MpegEncContext *s);
  228. void MPV_frame_end(MpegEncContext *s);
  229. #ifdef HAVE_MMX
  230. void MPV_common_init_mmx(MpegEncContext *s);
  231. #endif
  232. /* motion_est.c */
  233. int estimate_motion(MpegEncContext *s,
  234. int mb_x, int mb_y,
  235. int *mx_ptr, int *my_ptr);
  236. /* mpeg12.c */
  237. extern INT16 default_intra_matrix[64];
  238. extern INT16 default_non_intra_matrix[64];
  239. void mpeg1_encode_picture_header(MpegEncContext *s, int picture_number);
  240. void mpeg1_encode_mb(MpegEncContext *s,
  241. DCTELEM block[6][64],
  242. int motion_x, int motion_y);
  243. /* h263enc.c */
  244. /* run length table */
  245. #define MAX_RUN 64
  246. #define MAX_LEVEL 64
  247. typedef struct RLTable {
  248. int n; /* number of entries of table_vlc minus 1 */
  249. int last; /* number of values for last = 0 */
  250. const UINT16 (*table_vlc)[2];
  251. const INT8 *table_run;
  252. const INT8 *table_level;
  253. UINT8 *index_run[2]; /* encoding only */
  254. INT8 *max_level[2]; /* encoding & decoding */
  255. INT8 *max_run[2]; /* encoding & decoding */
  256. VLC vlc; /* decoding only */
  257. } RLTable;
  258. void init_rl(RLTable *rl);
  259. void init_vlc_rl(RLTable *rl);
  260. static inline int get_rl_index(const RLTable *rl, int last, int run, int level)
  261. {
  262. int index;
  263. index = rl->index_run[last][run];
  264. if (index >= rl->n)
  265. return rl->n;
  266. if (level > rl->max_level[last][run])
  267. return rl->n;
  268. return index + level - 1;
  269. }
  270. void h263_encode_mb(MpegEncContext *s,
  271. DCTELEM block[6][64],
  272. int motion_x, int motion_y);
  273. void h263_encode_picture_header(MpegEncContext *s, int picture_number);
  274. int h263_encode_gob_header(MpegEncContext * s, int mb_line);
  275. void h263_dc_scale(MpegEncContext *s);
  276. INT16 *h263_pred_motion(MpegEncContext * s, int block,
  277. int *px, int *py);
  278. void mpeg4_pred_ac(MpegEncContext * s, INT16 *block, int n,
  279. int dir);
  280. void mpeg4_encode_picture_header(MpegEncContext *s, int picture_number);
  281. void h263_encode_init_vlc(MpegEncContext *s);
  282. void h263_decode_init_vlc(MpegEncContext *s);
  283. int h263_decode_picture_header(MpegEncContext *s);
  284. int h263_decode_gob_header(MpegEncContext *s);
  285. int mpeg4_decode_picture_header(MpegEncContext * s);
  286. int intel_h263_decode_picture_header(MpegEncContext *s);
  287. int h263_decode_mb(MpegEncContext *s,
  288. DCTELEM block[6][64]);
  289. int h263_get_picture_format(int width, int height);
  290. /* rv10.c */
  291. void rv10_encode_picture_header(MpegEncContext *s, int picture_number);
  292. int rv_decode_dc(MpegEncContext *s, int n);
  293. /* msmpeg4.c */
  294. void msmpeg4_encode_picture_header(MpegEncContext * s, int picture_number);
  295. void msmpeg4_encode_ext_header(MpegEncContext * s);
  296. void msmpeg4_encode_mb(MpegEncContext * s,
  297. DCTELEM block[6][64],
  298. int motion_x, int motion_y);
  299. void msmpeg4_dc_scale(MpegEncContext * s);
  300. int msmpeg4_decode_picture_header(MpegEncContext * s);
  301. int msmpeg4_decode_ext_header(MpegEncContext * s, int buf_size);
  302. int msmpeg4_decode_mb(MpegEncContext *s,
  303. DCTELEM block[6][64]);
  304. int msmpeg4_decode_init_vlc(MpegEncContext *s);
  305. /* mjpegenc.c */
  306. int mjpeg_init(MpegEncContext *s);
  307. void mjpeg_close(MpegEncContext *s);
  308. void mjpeg_encode_mb(MpegEncContext *s,
  309. DCTELEM block[6][64]);
  310. void mjpeg_picture_header(MpegEncContext *s);
  311. void mjpeg_picture_trailer(MpegEncContext *s);