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.

289 lines
9.7KB

  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. int qscale;
  73. int pict_type;
  74. int frame_rate_index;
  75. /* motion compensation */
  76. int unrestricted_mv;
  77. int h263_long_vectors; /* use horrible h263v1 long vector mode */
  78. int f_code; /* resolution */
  79. INT16 (*motion_val)[2]; /* used for MV prediction */
  80. int full_search;
  81. int mv_dir;
  82. #define MV_DIR_BACKWARD 1
  83. #define MV_DIR_FORWARD 2
  84. int mv_type;
  85. #define MV_TYPE_16X16 0 /* 1 vector for the whole mb */
  86. #define MV_TYPE_8X8 1 /* 4 vectors (h263) */
  87. #define MV_TYPE_16X8 2 /* 2 vectors, one per 16x8 block */
  88. #define MV_TYPE_FIELD 3 /* 2 vectors, one per field */
  89. #define MV_TYPE_DMV 4 /* 2 vectors, special mpeg2 Dual Prime Vectors */
  90. /* motion vectors for a macroblock
  91. first coordinate : 0 = forward 1 = backward
  92. second " : depend on type
  93. third " : 0 = x, 1 = y
  94. */
  95. int mv[2][4][2];
  96. int field_select[2][2];
  97. int last_mv[2][2][2];
  98. int has_b_frames;
  99. int no_rounding; /* apply no rounding to motion estimation (MPEG4) */
  100. /* macroblock layer */
  101. int mb_x, mb_y;
  102. int mb_incr;
  103. int mb_intra;
  104. /* matrix transmitted in the bitstream */
  105. UINT16 intra_matrix[64];
  106. UINT16 chroma_intra_matrix[64];
  107. UINT16 non_intra_matrix[64];
  108. UINT16 chroma_non_intra_matrix[64];
  109. /* precomputed matrix (combine qscale and DCT renorm) */
  110. int q_intra_matrix[64];
  111. int q_non_intra_matrix[64];
  112. int block_last_index[6]; /* last non zero coefficient in block */
  113. void *opaque; /* private data for the user */
  114. /* bit rate control */
  115. int I_frame_bits; /* wanted number of bits per I frame */
  116. int P_frame_bits; /* same for P frame */
  117. INT64 wanted_bits;
  118. INT64 total_bits;
  119. /* H.263+ specific */
  120. int umvplus;
  121. int umvplus_dec;
  122. /* mpeg4 specific */
  123. int time_increment_bits;
  124. int shape;
  125. int vol_sprite_usage;
  126. int quant_precision;
  127. /* RV10 specific */
  128. int rv10_version; /* RV10 version: 0 or 3 */
  129. int rv10_first_dc_coded[3];
  130. /* MJPEG specific */
  131. struct MJpegContext *mjpeg_ctx;
  132. /* MSMPEG4 specific */
  133. int mv_table_index;
  134. int rl_table_index;
  135. int rl_chroma_table_index;
  136. int dc_table_index;
  137. int use_skip_mb_code;
  138. int slice_height; /* in macroblocks */
  139. int first_slice_line;
  140. /* decompression specific */
  141. GetBitContext gb;
  142. /* MPEG2 specific - I wish I had not to support this mess. */
  143. int progressive_sequence;
  144. int mpeg_f_code[2][2];
  145. int picture_structure;
  146. /* picture type */
  147. #define PICT_TOP_FIELD 1
  148. #define PICT_BOTTOM_FIELD 2
  149. #define PICT_FRAME 3
  150. int intra_dc_precision;
  151. int frame_pred_frame_dct;
  152. int top_field_first;
  153. int concealment_motion_vectors;
  154. int q_scale_type;
  155. int intra_vlc_format;
  156. int alternate_scan;
  157. int repeat_first_field;
  158. int chroma_420_type;
  159. int progressive_frame;
  160. int mpeg2;
  161. int full_pel[2];
  162. int interlaced_dct;
  163. int last_qscale;
  164. int first_slice;
  165. DCTELEM block[6][64] __align8;
  166. void (*dct_unquantize)(struct MpegEncContext *s,
  167. DCTELEM *block, int n, int qscale);
  168. } MpegEncContext;
  169. int MPV_common_init(MpegEncContext *s);
  170. void MPV_common_end(MpegEncContext *s);
  171. void MPV_decode_mb(MpegEncContext *s, DCTELEM block[6][64]);
  172. void MPV_frame_start(MpegEncContext *s);
  173. void MPV_frame_end(MpegEncContext *s);
  174. #ifdef HAVE_MMX
  175. void MPV_common_init_mmx(MpegEncContext *s);
  176. #endif
  177. /* motion_est.c */
  178. int estimate_motion(MpegEncContext *s,
  179. int mb_x, int mb_y,
  180. int *mx_ptr, int *my_ptr);
  181. /* mpeg12.c */
  182. extern INT16 default_intra_matrix[64];
  183. extern INT16 default_non_intra_matrix[64];
  184. void mpeg1_encode_picture_header(MpegEncContext *s, int picture_number);
  185. void mpeg1_encode_mb(MpegEncContext *s,
  186. DCTELEM block[6][64],
  187. int motion_x, int motion_y);
  188. /* h263enc.c */
  189. /* run length table */
  190. #define MAX_RUN 64
  191. #define MAX_LEVEL 64
  192. typedef struct RLTable {
  193. int n; /* number of entries of table_vlc minus 1 */
  194. int last; /* number of values for last = 0 */
  195. const UINT16 (*table_vlc)[2];
  196. const INT8 *table_run;
  197. const INT8 *table_level;
  198. UINT8 *index_run[2]; /* encoding only */
  199. INT8 *max_level[2]; /* encoding & decoding */
  200. INT8 *max_run[2]; /* encoding & decoding */
  201. VLC vlc; /* decoding only */
  202. } RLTable;
  203. void init_rl(RLTable *rl);
  204. void init_vlc_rl(RLTable *rl);
  205. extern inline int get_rl_index(const RLTable *rl, int last, int run, int level)
  206. {
  207. int index;
  208. index = rl->index_run[last][run];
  209. if (index >= rl->n)
  210. return rl->n;
  211. if (level > rl->max_level[last][run])
  212. return rl->n;
  213. return index + level - 1;
  214. }
  215. void h263_encode_mb(MpegEncContext *s,
  216. DCTELEM block[6][64],
  217. int motion_x, int motion_y);
  218. void h263_encode_picture_header(MpegEncContext *s, int picture_number);
  219. void h263_dc_scale(MpegEncContext *s);
  220. INT16 *h263_pred_motion(MpegEncContext * s, int block,
  221. int *px, int *py);
  222. void mpeg4_pred_ac(MpegEncContext * s, INT16 *block, int n,
  223. int dir);
  224. void mpeg4_encode_picture_header(MpegEncContext *s, int picture_number);
  225. void h263_encode_init_vlc(MpegEncContext *s);
  226. void h263_decode_init_vlc(MpegEncContext *s);
  227. int h263_decode_picture_header(MpegEncContext *s);
  228. int mpeg4_decode_picture_header(MpegEncContext * s);
  229. int intel_h263_decode_picture_header(MpegEncContext *s);
  230. int h263_decode_mb(MpegEncContext *s,
  231. DCTELEM block[6][64]);
  232. int h263_get_picture_format(int width, int height);
  233. /* rv10.c */
  234. void rv10_encode_picture_header(MpegEncContext *s, int picture_number);
  235. int rv_decode_dc(MpegEncContext *s, int n);
  236. /* msmpeg4.c */
  237. void msmpeg4_encode_picture_header(MpegEncContext * s, int picture_number);
  238. void msmpeg4_encode_mb(MpegEncContext * s,
  239. DCTELEM block[6][64],
  240. int motion_x, int motion_y);
  241. void msmpeg4_dc_scale(MpegEncContext * s);
  242. int msmpeg4_decode_picture_header(MpegEncContext * s);
  243. int msmpeg4_decode_mb(MpegEncContext *s,
  244. DCTELEM block[6][64]);
  245. int msmpeg4_decode_init_vlc(MpegEncContext *s);
  246. /* mjpegenc.c */
  247. int mjpeg_init(MpegEncContext *s);
  248. void mjpeg_close(MpegEncContext *s);
  249. void mjpeg_encode_mb(MpegEncContext *s,
  250. DCTELEM block[6][64]);
  251. void mjpeg_picture_header(MpegEncContext *s);
  252. void mjpeg_picture_trailer(MpegEncContext *s);