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.

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