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.

274 lines
9.2KB

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