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.

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