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.

325 lines
11KB

  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. #define QMAT_SHIFT_MMX 19
  30. #define QMAT_SHIFT 25
  31. typedef struct MpegEncContext {
  32. struct AVCodecContext *avctx;
  33. /* the following parameters must be initialized before encoding */
  34. int width, height; /* picture size. must be a multiple of 16 */
  35. int gop_size;
  36. int frame_rate; /* number of frames per second */
  37. int intra_only; /* if true, only intra pictures are generated */
  38. int bit_rate; /* wanted bit rate */
  39. enum OutputFormat out_format; /* output format */
  40. int h263_plus; /* h263 plus headers */
  41. int h263_rv10; /* use RV10 variation for H263 */
  42. int h263_pred; /* use mpeg4/h263 ac/dc predictions */
  43. int h263_msmpeg4; /* generate MSMPEG4 compatible stream */
  44. int h263_intel; /* use I263 intel h263 header */
  45. int fixed_qscale; /* fixed qscale if non zero */
  46. int encoding; /* true if we are encoding (vs decoding) */
  47. /* the following fields are managed internally by the encoder */
  48. /* bit output */
  49. PutBitContext pb;
  50. /* sequence parameters */
  51. int context_initialized;
  52. int picture_number;
  53. int fake_picture_number; /* picture number at the bitstream frame rate */
  54. int gop_picture_number; /* index of the first picture of a GOP */
  55. int mb_width, mb_height;
  56. int mb_num; /* number of MBs of a picture */
  57. int linesize; /* line size, in bytes, may be different from width */
  58. UINT8 *new_picture[3]; /* picture to be compressed */
  59. UINT8 *last_picture[3]; /* previous picture */
  60. UINT8 *last_picture_base[3]; /* real start of the picture */
  61. UINT8 *next_picture[3]; /* previous picture (for bidir pred) */
  62. UINT8 *next_picture_base[3]; /* real start of the picture */
  63. UINT8 *aux_picture[3]; /* aux picture (for B frames only) */
  64. UINT8 *aux_picture_base[3]; /* real start of the picture */
  65. UINT8 *current_picture[3]; /* buffer to store the decompressed current picture */
  66. int last_dc[3]; /* last DC values for MPEG1 */
  67. INT16 *dc_val[3]; /* used for mpeg4 DC prediction */
  68. int y_dc_scale, c_dc_scale;
  69. UINT8 *coded_block; /* used for coded block pattern prediction */
  70. INT16 (*ac_val[3])[16]; /* used for for mpeg4 AC prediction */
  71. int ac_pred;
  72. int mb_skiped; /* MUST BE SET only during DECODING */
  73. UINT8 *mbskip_table; /* used to avoid copy if macroblock
  74. skipped (for black regions for example) */
  75. UINT8 *mbintra_table; /* used to kill a few memsets */
  76. int qscale;
  77. int pict_type;
  78. int frame_rate_index;
  79. /* motion compensation */
  80. int unrestricted_mv;
  81. int h263_long_vectors; /* use horrible h263v1 long vector mode */
  82. int f_code; /* resolution */
  83. INT16 *mv_table[2]; /* MV table */
  84. INT16 (*motion_val)[2]; /* used for MV prediction */
  85. int full_search;
  86. int mv_dir;
  87. #define MV_DIR_BACKWARD 1
  88. #define MV_DIR_FORWARD 2
  89. int mv_type;
  90. #define MV_TYPE_16X16 0 /* 1 vector for the whole mb */
  91. #define MV_TYPE_8X8 1 /* 4 vectors (h263) */
  92. #define MV_TYPE_16X8 2 /* 2 vectors, one per 16x8 block */
  93. #define MV_TYPE_FIELD 3 /* 2 vectors, one per field */
  94. #define MV_TYPE_DMV 4 /* 2 vectors, special mpeg2 Dual Prime Vectors */
  95. /* motion vectors for a macroblock
  96. first coordinate : 0 = forward 1 = backward
  97. second " : depend on type
  98. third " : 0 = x, 1 = y
  99. */
  100. int mv[2][4][2];
  101. int field_select[2][2];
  102. int last_mv[2][2][2];
  103. int has_b_frames;
  104. int no_rounding; /* apply no rounding to motion estimation (MPEG4) */
  105. /* macroblock layer */
  106. int mb_x, mb_y;
  107. int mb_incr;
  108. int mb_intra;
  109. char *mb_type; /* Table for MB type */
  110. /* matrix transmitted in the bitstream */
  111. UINT16 intra_matrix[64];
  112. UINT16 chroma_intra_matrix[64];
  113. UINT16 non_intra_matrix[64];
  114. UINT16 chroma_non_intra_matrix[64];
  115. /* precomputed matrix (combine qscale and DCT renorm) */
  116. int q_intra_matrix[64];
  117. int q_non_intra_matrix[64];
  118. /* identical to the above but for MMX & these are not permutated */
  119. UINT16 __align8 q_intra_matrix16[64] ;
  120. UINT16 __align8 q_non_intra_matrix16[64];
  121. int block_last_index[6]; /* last non zero coefficient in block */
  122. void *opaque; /* private data for the user */
  123. /* bit rate control */
  124. int I_frame_bits; /* wanted number of bits per I frame */
  125. int P_frame_bits; /* same for P frame */
  126. int avg_mb_var; /* average MB variance for current frame */
  127. INT64 wanted_bits;
  128. INT64 total_bits;
  129. /* H.263 specific */
  130. int gob_number;
  131. int gob_index;
  132. int first_gob_line;
  133. /* H.263+ specific */
  134. int umvplus;
  135. int umvplus_dec;
  136. /* mpeg4 specific */
  137. int time_increment_bits;
  138. int shape;
  139. int vol_sprite_usage;
  140. int quant_precision;
  141. /* RV10 specific */
  142. int rv10_version; /* RV10 version: 0 or 3 */
  143. int rv10_first_dc_coded[3];
  144. /* MJPEG specific */
  145. struct MJpegContext *mjpeg_ctx;
  146. int mjpeg_vsample[3]; /* vertical sampling factors, default = {2, 1, 1} */
  147. int mjpeg_hsample[3]; /* horizontal sampling factors, default = {2, 1, 1} */
  148. int mjpeg_write_tables; /* do we want to have quantisation- and
  149. huffmantables in the jpeg file ? */
  150. /* MSMPEG4 specific */
  151. int mv_table_index;
  152. int rl_table_index;
  153. int rl_chroma_table_index;
  154. int dc_table_index;
  155. int use_skip_mb_code;
  156. int slice_height; /* in macroblocks */
  157. int first_slice_line;
  158. int flipflop_rounding;
  159. int bitrate;
  160. /* decompression specific */
  161. GetBitContext gb;
  162. /* MPEG2 specific - I wish I had not to support this mess. */
  163. int progressive_sequence;
  164. int mpeg_f_code[2][2];
  165. int picture_structure;
  166. /* picture type */
  167. #define PICT_TOP_FIELD 1
  168. #define PICT_BOTTOM_FIELD 2
  169. #define PICT_FRAME 3
  170. int intra_dc_precision;
  171. int frame_pred_frame_dct;
  172. int top_field_first;
  173. int concealment_motion_vectors;
  174. int q_scale_type;
  175. int intra_vlc_format;
  176. int alternate_scan;
  177. int repeat_first_field;
  178. int chroma_420_type;
  179. int progressive_frame;
  180. int mpeg2;
  181. int full_pel[2];
  182. int interlaced_dct;
  183. int last_qscale;
  184. int first_slice;
  185. /* RTP specific */
  186. /* These are explained on avcodec.h */
  187. int rtp_mode;
  188. int rtp_payload_size;
  189. void (*rtp_callback)(void *data, int size, int packet_number);
  190. UINT8 *ptr_lastgob;
  191. UINT8 *ptr_last_mb_line;
  192. UINT32 mb_line_avgsize;
  193. DCTELEM block[6][64] __align8;
  194. void (*dct_unquantize)(struct MpegEncContext *s,
  195. DCTELEM *block, int n, int qscale);
  196. } MpegEncContext;
  197. int MPV_common_init(MpegEncContext *s);
  198. void MPV_common_end(MpegEncContext *s);
  199. void MPV_decode_mb(MpegEncContext *s, DCTELEM block[6][64]);
  200. void MPV_frame_start(MpegEncContext *s);
  201. void MPV_frame_end(MpegEncContext *s);
  202. #ifdef HAVE_MMX
  203. void MPV_common_init_mmx(MpegEncContext *s);
  204. #endif
  205. /* motion_est.c */
  206. int estimate_motion(MpegEncContext *s,
  207. int mb_x, int mb_y,
  208. int *mx_ptr, int *my_ptr);
  209. /* mpeg12.c */
  210. extern INT16 default_intra_matrix[64];
  211. extern INT16 default_non_intra_matrix[64];
  212. void mpeg1_encode_picture_header(MpegEncContext *s, int picture_number);
  213. void mpeg1_encode_mb(MpegEncContext *s,
  214. DCTELEM block[6][64],
  215. int motion_x, int motion_y);
  216. /* h263enc.c */
  217. /* run length table */
  218. #define MAX_RUN 64
  219. #define MAX_LEVEL 64
  220. typedef struct RLTable {
  221. int n; /* number of entries of table_vlc minus 1 */
  222. int last; /* number of values for last = 0 */
  223. const UINT16 (*table_vlc)[2];
  224. const INT8 *table_run;
  225. const INT8 *table_level;
  226. UINT8 *index_run[2]; /* encoding only */
  227. INT8 *max_level[2]; /* encoding & decoding */
  228. INT8 *max_run[2]; /* encoding & decoding */
  229. VLC vlc; /* decoding only */
  230. } RLTable;
  231. void init_rl(RLTable *rl);
  232. void init_vlc_rl(RLTable *rl);
  233. extern inline int get_rl_index(const RLTable *rl, int last, int run, int level)
  234. {
  235. int index;
  236. index = rl->index_run[last][run];
  237. if (index >= rl->n)
  238. return rl->n;
  239. if (level > rl->max_level[last][run])
  240. return rl->n;
  241. return index + level - 1;
  242. }
  243. void h263_encode_mb(MpegEncContext *s,
  244. DCTELEM block[6][64],
  245. int motion_x, int motion_y);
  246. void h263_encode_picture_header(MpegEncContext *s, int picture_number);
  247. int h263_encode_gob_header(MpegEncContext * s, int mb_line);
  248. void h263_dc_scale(MpegEncContext *s);
  249. INT16 *h263_pred_motion(MpegEncContext * s, int block,
  250. int *px, int *py);
  251. void mpeg4_pred_ac(MpegEncContext * s, INT16 *block, int n,
  252. int dir);
  253. void mpeg4_encode_picture_header(MpegEncContext *s, int picture_number);
  254. void h263_encode_init_vlc(MpegEncContext *s);
  255. void h263_decode_init_vlc(MpegEncContext *s);
  256. int h263_decode_picture_header(MpegEncContext *s);
  257. int h263_decode_gob_header(MpegEncContext *s);
  258. int mpeg4_decode_picture_header(MpegEncContext * s);
  259. int intel_h263_decode_picture_header(MpegEncContext *s);
  260. int h263_decode_mb(MpegEncContext *s,
  261. DCTELEM block[6][64]);
  262. int h263_get_picture_format(int width, int height);
  263. /* rv10.c */
  264. void rv10_encode_picture_header(MpegEncContext *s, int picture_number);
  265. int rv_decode_dc(MpegEncContext *s, int n);
  266. /* msmpeg4.c */
  267. void msmpeg4_encode_picture_header(MpegEncContext * s, int picture_number);
  268. void msmpeg4_encode_ext_header(MpegEncContext * s);
  269. void msmpeg4_encode_mb(MpegEncContext * s,
  270. DCTELEM block[6][64],
  271. int motion_x, int motion_y);
  272. void msmpeg4_dc_scale(MpegEncContext * s);
  273. int msmpeg4_decode_picture_header(MpegEncContext * s);
  274. int msmpeg4_decode_ext_header(MpegEncContext * s, int buf_size);
  275. int msmpeg4_decode_mb(MpegEncContext *s,
  276. DCTELEM block[6][64]);
  277. int msmpeg4_decode_init_vlc(MpegEncContext *s);
  278. /* mjpegenc.c */
  279. int mjpeg_init(MpegEncContext *s);
  280. void mjpeg_close(MpegEncContext *s);
  281. void mjpeg_encode_mb(MpegEncContext *s,
  282. DCTELEM block[6][64]);
  283. void mjpeg_picture_header(MpegEncContext *s);
  284. void mjpeg_picture_trailer(MpegEncContext *s);