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.

285 lines
9.6KB

  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. /* mpeg4 specific */
  120. int time_increment_bits;
  121. int shape;
  122. int vol_sprite_usage;
  123. int quant_precision;
  124. /* RV10 specific */
  125. int rv10_version; /* RV10 version: 0 or 3 */
  126. int rv10_first_dc_coded[3];
  127. /* MJPEG specific */
  128. struct MJpegContext *mjpeg_ctx;
  129. /* MSMPEG4 specific */
  130. int mv_table_index;
  131. int rl_table_index;
  132. int rl_chroma_table_index;
  133. int dc_table_index;
  134. int use_skip_mb_code;
  135. int slice_height; /* in macroblocks */
  136. int first_slice_line;
  137. /* decompression specific */
  138. GetBitContext gb;
  139. /* MPEG2 specific - I wish I had not to support this mess. */
  140. int progressive_sequence;
  141. int mpeg_f_code[2][2];
  142. int picture_structure;
  143. /* picture type */
  144. #define PICT_TOP_FIELD 1
  145. #define PICT_BOTTOM_FIELD 2
  146. #define PICT_FRAME 3
  147. int intra_dc_precision;
  148. int frame_pred_frame_dct;
  149. int top_field_first;
  150. int concealment_motion_vectors;
  151. int q_scale_type;
  152. int intra_vlc_format;
  153. int alternate_scan;
  154. int repeat_first_field;
  155. int chroma_420_type;
  156. int progressive_frame;
  157. int mpeg2;
  158. int full_pel[2];
  159. int interlaced_dct;
  160. int last_qscale;
  161. int first_slice;
  162. DCTELEM block[6][64] __align8;
  163. void (*dct_unquantize)(struct MpegEncContext *s,
  164. DCTELEM *block, int n, int qscale);
  165. } MpegEncContext;
  166. int MPV_common_init(MpegEncContext *s);
  167. void MPV_common_end(MpegEncContext *s);
  168. void MPV_decode_mb(MpegEncContext *s, DCTELEM block[6][64]);
  169. void MPV_frame_start(MpegEncContext *s);
  170. void MPV_frame_end(MpegEncContext *s);
  171. #ifdef HAVE_MMX
  172. void MPV_common_init_mmx(MpegEncContext *s);
  173. #endif
  174. /* motion_est.c */
  175. int estimate_motion(MpegEncContext *s,
  176. int mb_x, int mb_y,
  177. int *mx_ptr, int *my_ptr);
  178. /* mpeg12.c */
  179. extern INT16 default_intra_matrix[64];
  180. extern INT16 default_non_intra_matrix[64];
  181. void mpeg1_encode_picture_header(MpegEncContext *s, int picture_number);
  182. void mpeg1_encode_mb(MpegEncContext *s,
  183. DCTELEM block[6][64],
  184. int motion_x, int motion_y);
  185. /* h263enc.c */
  186. /* run length table */
  187. #define MAX_RUN 64
  188. #define MAX_LEVEL 64
  189. typedef struct RLTable {
  190. int n; /* number of entries of table_vlc minus 1 */
  191. int last; /* number of values for last = 0 */
  192. const UINT16 (*table_vlc)[2];
  193. const INT8 *table_run;
  194. const INT8 *table_level;
  195. UINT8 *index_run[2]; /* encoding only */
  196. INT8 *max_level[2]; /* encoding & decoding */
  197. INT8 *max_run[2]; /* encoding & decoding */
  198. VLC vlc; /* decoding only */
  199. } RLTable;
  200. void init_rl(RLTable *rl);
  201. void init_vlc_rl(RLTable *rl);
  202. extern inline int get_rl_index(const RLTable *rl, int last, int run, int level)
  203. {
  204. int index;
  205. index = rl->index_run[last][run];
  206. if (index >= rl->n)
  207. return rl->n;
  208. if (level > rl->max_level[last][run])
  209. return rl->n;
  210. return index + level - 1;
  211. }
  212. void h263_encode_mb(MpegEncContext *s,
  213. DCTELEM block[6][64],
  214. int motion_x, int motion_y);
  215. void h263_encode_picture_header(MpegEncContext *s, int picture_number);
  216. void h263_dc_scale(MpegEncContext *s);
  217. INT16 *h263_pred_motion(MpegEncContext * s, int block,
  218. int *px, int *py);
  219. void mpeg4_pred_ac(MpegEncContext * s, INT16 *block, int n,
  220. int dir);
  221. void mpeg4_encode_picture_header(MpegEncContext *s, int picture_number);
  222. void h263_encode_init_vlc(MpegEncContext *s);
  223. void h263_decode_init_vlc(MpegEncContext *s);
  224. int h263_decode_picture_header(MpegEncContext *s);
  225. int mpeg4_decode_picture_header(MpegEncContext * s);
  226. int intel_h263_decode_picture_header(MpegEncContext *s);
  227. int h263_decode_mb(MpegEncContext *s,
  228. DCTELEM block[6][64]);
  229. int h263_get_picture_format(int width, int height);
  230. /* rv10.c */
  231. void rv10_encode_picture_header(MpegEncContext *s, int picture_number);
  232. int rv_decode_dc(MpegEncContext *s, int n);
  233. /* msmpeg4.c */
  234. void msmpeg4_encode_picture_header(MpegEncContext * s, int picture_number);
  235. void msmpeg4_encode_mb(MpegEncContext * s,
  236. DCTELEM block[6][64],
  237. int motion_x, int motion_y);
  238. void msmpeg4_dc_scale(MpegEncContext * s);
  239. int msmpeg4_decode_picture_header(MpegEncContext * s);
  240. int msmpeg4_decode_mb(MpegEncContext *s,
  241. DCTELEM block[6][64]);
  242. int msmpeg4_decode_init_vlc(MpegEncContext *s);
  243. /* mjpegenc.c */
  244. int mjpeg_init(MpegEncContext *s);
  245. void mjpeg_close(MpegEncContext *s);
  246. void mjpeg_encode_mb(MpegEncContext *s,
  247. DCTELEM block[6][64]);
  248. void mjpeg_picture_header(MpegEncContext *s);
  249. void mjpeg_picture_trailer(MpegEncContext *s);