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.

304 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. /* decompression specific */
  146. GetBitContext gb;
  147. /* MPEG2 specific - I wish I had not to support this mess. */
  148. int progressive_sequence;
  149. int mpeg_f_code[2][2];
  150. int picture_structure;
  151. /* picture type */
  152. #define PICT_TOP_FIELD 1
  153. #define PICT_BOTTOM_FIELD 2
  154. #define PICT_FRAME 3
  155. int intra_dc_precision;
  156. int frame_pred_frame_dct;
  157. int top_field_first;
  158. int concealment_motion_vectors;
  159. int q_scale_type;
  160. int intra_vlc_format;
  161. int alternate_scan;
  162. int repeat_first_field;
  163. int chroma_420_type;
  164. int progressive_frame;
  165. int mpeg2;
  166. int full_pel[2];
  167. int interlaced_dct;
  168. int last_qscale;
  169. int first_slice;
  170. /* RTP specific */
  171. int rtp_mode;
  172. int rtp_payload_size;
  173. UINT8 *ptr_lastgob;
  174. UINT8 *ptr_last_mb_line;
  175. UINT32 mb_line_avgsize;
  176. DCTELEM block[6][64] __align8;
  177. void (*dct_unquantize)(struct MpegEncContext *s,
  178. DCTELEM *block, int n, int qscale);
  179. } MpegEncContext;
  180. int MPV_common_init(MpegEncContext *s);
  181. void MPV_common_end(MpegEncContext *s);
  182. void MPV_decode_mb(MpegEncContext *s, DCTELEM block[6][64]);
  183. void MPV_frame_start(MpegEncContext *s);
  184. void MPV_frame_end(MpegEncContext *s);
  185. #ifdef HAVE_MMX
  186. void MPV_common_init_mmx(MpegEncContext *s);
  187. #endif
  188. /* motion_est.c */
  189. int estimate_motion(MpegEncContext *s,
  190. int mb_x, int mb_y,
  191. int *mx_ptr, int *my_ptr);
  192. /* mpeg12.c */
  193. extern INT16 default_intra_matrix[64];
  194. extern INT16 default_non_intra_matrix[64];
  195. void mpeg1_encode_picture_header(MpegEncContext *s, int picture_number);
  196. void mpeg1_encode_mb(MpegEncContext *s,
  197. DCTELEM block[6][64],
  198. int motion_x, int motion_y);
  199. /* h263enc.c */
  200. /* run length table */
  201. #define MAX_RUN 64
  202. #define MAX_LEVEL 64
  203. typedef struct RLTable {
  204. int n; /* number of entries of table_vlc minus 1 */
  205. int last; /* number of values for last = 0 */
  206. const UINT16 (*table_vlc)[2];
  207. const INT8 *table_run;
  208. const INT8 *table_level;
  209. UINT8 *index_run[2]; /* encoding only */
  210. INT8 *max_level[2]; /* encoding & decoding */
  211. INT8 *max_run[2]; /* encoding & decoding */
  212. VLC vlc; /* decoding only */
  213. } RLTable;
  214. void init_rl(RLTable *rl);
  215. void init_vlc_rl(RLTable *rl);
  216. extern inline int get_rl_index(const RLTable *rl, int last, int run, int level)
  217. {
  218. int index;
  219. index = rl->index_run[last][run];
  220. if (index >= rl->n)
  221. return rl->n;
  222. if (level > rl->max_level[last][run])
  223. return rl->n;
  224. return index + level - 1;
  225. }
  226. void h263_encode_mb(MpegEncContext *s,
  227. DCTELEM block[6][64],
  228. int motion_x, int motion_y);
  229. void h263_encode_picture_header(MpegEncContext *s, int picture_number);
  230. int h263_encode_gob_header(MpegEncContext * s, int mb_line);
  231. void h263_dc_scale(MpegEncContext *s);
  232. INT16 *h263_pred_motion(MpegEncContext * s, int block,
  233. int *px, int *py);
  234. void mpeg4_pred_ac(MpegEncContext * s, INT16 *block, int n,
  235. int dir);
  236. void mpeg4_encode_picture_header(MpegEncContext *s, int picture_number);
  237. void h263_encode_init_vlc(MpegEncContext *s);
  238. void h263_decode_init_vlc(MpegEncContext *s);
  239. int h263_decode_picture_header(MpegEncContext *s);
  240. int h263_decode_gob_header(MpegEncContext *s);
  241. int mpeg4_decode_picture_header(MpegEncContext * s);
  242. int intel_h263_decode_picture_header(MpegEncContext *s);
  243. int h263_decode_mb(MpegEncContext *s,
  244. DCTELEM block[6][64]);
  245. int h263_get_picture_format(int width, int height);
  246. /* rv10.c */
  247. void rv10_encode_picture_header(MpegEncContext *s, int picture_number);
  248. int rv_decode_dc(MpegEncContext *s, int n);
  249. /* msmpeg4.c */
  250. void msmpeg4_encode_picture_header(MpegEncContext * s, int picture_number);
  251. void msmpeg4_encode_mb(MpegEncContext * s,
  252. DCTELEM block[6][64],
  253. int motion_x, int motion_y);
  254. void msmpeg4_dc_scale(MpegEncContext * s);
  255. int msmpeg4_decode_picture_header(MpegEncContext * s);
  256. int msmpeg4_decode_mb(MpegEncContext *s,
  257. DCTELEM block[6][64]);
  258. int msmpeg4_decode_init_vlc(MpegEncContext *s);
  259. /* mjpegenc.c */
  260. int mjpeg_init(MpegEncContext *s);
  261. void mjpeg_close(MpegEncContext *s);
  262. void mjpeg_encode_mb(MpegEncContext *s,
  263. DCTELEM block[6][64]);
  264. void mjpeg_picture_header(MpegEncContext *s);
  265. void mjpeg_picture_trailer(MpegEncContext *s);