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.

314 lines
10KB

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