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.

328 lines
11KB

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