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.

407 lines
14KB

  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. #define S_TYPE 4 //S(GMC)-VOP MPEG4
  24. enum OutputFormat {
  25. FMT_MPEG1,
  26. FMT_H263,
  27. FMT_MJPEG,
  28. };
  29. #define MPEG_BUF_SIZE (16 * 1024)
  30. #define QMAT_SHIFT_MMX 19
  31. #define QMAT_SHIFT 25
  32. #define MAX_FCODE 7
  33. #define MAX_MV 2048
  34. typedef struct Predictor{
  35. double coeff;
  36. double count;
  37. double decay;
  38. } Predictor;
  39. typedef struct MpegEncContext {
  40. struct AVCodecContext *avctx;
  41. /* the following parameters must be initialized before encoding */
  42. int width, height; /* picture size. must be a multiple of 16 */
  43. int gop_size;
  44. int frame_rate; /* number of frames per second */
  45. int intra_only; /* if true, only intra pictures are generated */
  46. int bit_rate; /* wanted bit rate */
  47. int bit_rate_tolerance; /* amount of +- bits (>0)*/
  48. enum OutputFormat out_format; /* output format */
  49. int h263_plus; /* h263 plus headers */
  50. int h263_rv10; /* use RV10 variation for H263 */
  51. int h263_pred; /* use mpeg4/h263 ac/dc predictions */
  52. int h263_msmpeg4; /* generate MSMPEG4 compatible stream */
  53. int h263_intel; /* use I263 intel h263 header */
  54. int fixed_qscale; /* fixed qscale if non zero */
  55. float qcompress; /* amount of qscale change between easy & hard scenes (0.0-1.0) */
  56. float qblur; /* amount of qscale smoothing over time (0.0-1.0) */
  57. int qmin; /* min qscale */
  58. int qmax; /* max qscale */
  59. int max_qdiff; /* max qscale difference between frames */
  60. int encoding; /* true if we are encoding (vs decoding) */
  61. /* the following fields are managed internally by the encoder */
  62. /* bit output */
  63. PutBitContext pb;
  64. /* sequence parameters */
  65. int context_initialized;
  66. int picture_number;
  67. int fake_picture_number; /* picture number at the bitstream frame rate */
  68. int gop_picture_number; /* index of the first picture of a GOP based on fake_pic_num & mpeg1 specific */
  69. int picture_in_gop_number; /* 0-> first pic in gop, ... */
  70. int mb_width, mb_height;
  71. int mb_num; /* number of MBs of a picture */
  72. int linesize; /* line size, in bytes, may be different from width */
  73. UINT8 *new_picture[3]; /* picture to be compressed */
  74. UINT8 *last_picture[3]; /* previous picture */
  75. UINT8 *last_picture_base[3]; /* real start of the picture */
  76. UINT8 *next_picture[3]; /* previous picture (for bidir pred) */
  77. UINT8 *next_picture_base[3]; /* real start of the picture */
  78. UINT8 *aux_picture[3]; /* aux picture (for B frames only) */
  79. UINT8 *aux_picture_base[3]; /* real start of the picture */
  80. UINT8 *current_picture[3]; /* buffer to store the decompressed current picture */
  81. int last_dc[3]; /* last DC values for MPEG1 */
  82. INT16 *dc_val[3]; /* used for mpeg4 DC prediction, all 3 arrays must be continuous */
  83. int y_dc_scale, c_dc_scale;
  84. UINT8 *coded_block; /* used for coded block pattern prediction */
  85. INT16 (*ac_val[3])[16]; /* used for for mpeg4 AC prediction, all 3 arrays must be continuous */
  86. int ac_pred;
  87. int mb_skiped; /* MUST BE SET only during DECODING */
  88. UINT8 *mbskip_table; /* used to avoid copy if macroblock
  89. skipped (for black regions for example) */
  90. UINT8 *mbintra_table; /* used to kill a few memsets */
  91. int qscale;
  92. int pict_type;
  93. int last_non_b_pict_type; /* used for mpeg4 gmc b-frames */
  94. int last_pict_type; /* used for bit rate stuff (needs that to update the right predictor) */
  95. int frame_rate_index;
  96. /* motion compensation */
  97. int unrestricted_mv;
  98. int h263_long_vectors; /* use horrible h263v1 long vector mode */
  99. int f_code; /* resolution */
  100. int b_code; /* backward resolution for B Frames (mpeg4) */
  101. INT16 *mv_table[2]; /* MV table (1MV per MB)*/
  102. INT16 (*motion_val)[2]; /* used for MV prediction (4MV per MB)*/
  103. int full_search;
  104. int mv_dir;
  105. #define MV_DIR_BACKWARD 1
  106. #define MV_DIR_FORWARD 2
  107. #define MV_DIRECT 4 // bidirectional mode where the difference equals the MV of the last P/S/I-Frame (mpeg4)
  108. int mv_type;
  109. #define MV_TYPE_16X16 0 /* 1 vector for the whole mb */
  110. #define MV_TYPE_8X8 1 /* 4 vectors (h263, mpeg4 4MV) */
  111. #define MV_TYPE_16X8 2 /* 2 vectors, one per 16x8 block */
  112. #define MV_TYPE_FIELD 3 /* 2 vectors, one per field */
  113. #define MV_TYPE_DMV 4 /* 2 vectors, special mpeg2 Dual Prime Vectors */
  114. /* motion vectors for a macroblock
  115. first coordinate : 0 = forward 1 = backward
  116. second " : depend on type
  117. third " : 0 = x, 1 = y
  118. */
  119. int mv[2][4][2];
  120. int field_select[2][2];
  121. int last_mv[2][2][2];
  122. UINT16 (*mv_penalty)[MAX_MV*2+1]; /* amount of bits needed to encode a MV, used for ME */
  123. UINT8 *fcode_tab; /* smallest fcode needed for each MV */
  124. int has_b_frames;
  125. int no_rounding; /* apply no rounding to motion estimation (MPEG4) */
  126. /* macroblock layer */
  127. int mb_x, mb_y;
  128. int mb_incr;
  129. int mb_intra;
  130. INT16 *mb_var; /* Table for MB variances */
  131. char *mb_type; /* Table for MB type */
  132. int block_index[6];
  133. int block_wrap[6];
  134. /* matrix transmitted in the bitstream */
  135. UINT16 intra_matrix[64];
  136. UINT16 chroma_intra_matrix[64];
  137. UINT16 non_intra_matrix[64];
  138. UINT16 chroma_non_intra_matrix[64];
  139. /* precomputed matrix (combine qscale and DCT renorm) */
  140. int q_intra_matrix[64];
  141. int q_non_intra_matrix[64];
  142. /* identical to the above but for MMX & these are not permutated */
  143. UINT16 __align8 q_intra_matrix16[64] ;
  144. UINT16 __align8 q_non_intra_matrix16[64];
  145. int block_last_index[6]; /* last non zero coefficient in block */
  146. void *opaque; /* private data for the user */
  147. /* bit rate control */
  148. int I_frame_bits; /* wanted number of bits per I frame */
  149. int P_frame_bits; /* same for P frame */
  150. int avg_mb_var; /* average MB variance for current frame */
  151. int mc_mb_var; /* motion compensated MB variance for current frame */
  152. int last_mc_mb_var; /* motion compensated MB variance for last frame */
  153. INT64 wanted_bits;
  154. INT64 total_bits;
  155. int frame_bits; /* bits used for the current frame */
  156. int last_frame_bits; /* bits used for the last frame */
  157. Predictor i_pred;
  158. Predictor p_pred;
  159. double qsum; /* sum of qscales */
  160. double qcount; /* count of qscales */
  161. double short_term_qsum; /* sum of recent qscales */
  162. double short_term_qcount; /* count of recent qscales */
  163. /* statistics, used for 2-pass encoding */
  164. int mv_bits;
  165. int header_bits;
  166. int i_tex_bits;
  167. int p_tex_bits;
  168. int i_count;
  169. int p_count;
  170. int skip_count;
  171. int misc_bits; // cbp, mb_type
  172. int last_bits; //temp var used for calculating the above vars
  173. /* H.263 specific */
  174. int gob_number;
  175. int gob_index;
  176. int first_gob_line;
  177. /* H.263+ specific */
  178. int umvplus;
  179. int umvplus_dec;
  180. int h263_aic; /* Advanded INTRA Coding (AIC) */
  181. int h263_aic_dir; /* AIC direction: 0 = left, 1 = top */
  182. /* mpeg4 specific */
  183. int time_increment_resolution;
  184. int time_increment_bits;
  185. int time_increment;
  186. int time_base;
  187. int time;
  188. int last_non_b_time[2];
  189. int shape;
  190. int vol_sprite_usage;
  191. int sprite_width;
  192. int sprite_height;
  193. int sprite_left;
  194. int sprite_top;
  195. int sprite_brightness_change;
  196. int num_sprite_warping_points;
  197. int real_sprite_warping_points;
  198. int sprite_offset[2][2];
  199. int sprite_delta[2][2][2];
  200. int sprite_shift[2][2];
  201. int mcsel;
  202. int quant_precision;
  203. int quarter_sample;
  204. int scalability;
  205. int new_pred;
  206. int reduced_res_vop;
  207. int aspect_ratio_info;
  208. int sprite_warping_accuracy;
  209. int low_latency_sprite;
  210. int data_partioning;
  211. /* divx specific, used to workaround (many) bugs in divx5 */
  212. int divx_version;
  213. int divx_build;
  214. /* RV10 specific */
  215. int rv10_version; /* RV10 version: 0 or 3 */
  216. int rv10_first_dc_coded[3];
  217. /* MJPEG specific */
  218. struct MJpegContext *mjpeg_ctx;
  219. int mjpeg_vsample[3]; /* vertical sampling factors, default = {2, 1, 1} */
  220. int mjpeg_hsample[3]; /* horizontal sampling factors, default = {2, 1, 1} */
  221. int mjpeg_write_tables; /* do we want to have quantisation- and
  222. huffmantables in the jpeg file ? */
  223. /* MSMPEG4 specific */
  224. int mv_table_index;
  225. int rl_table_index;
  226. int rl_chroma_table_index;
  227. int dc_table_index;
  228. int use_skip_mb_code;
  229. int slice_height; /* in macroblocks */
  230. int first_slice_line;
  231. int flipflop_rounding;
  232. int bitrate;
  233. /* decompression specific */
  234. GetBitContext gb;
  235. /* MPEG2 specific - I wish I had not to support this mess. */
  236. int progressive_sequence;
  237. int mpeg_f_code[2][2];
  238. int picture_structure;
  239. /* picture type */
  240. #define PICT_TOP_FIELD 1
  241. #define PICT_BOTTOM_FIELD 2
  242. #define PICT_FRAME 3
  243. int intra_dc_precision;
  244. int frame_pred_frame_dct;
  245. int top_field_first;
  246. int concealment_motion_vectors;
  247. int q_scale_type;
  248. int intra_vlc_format;
  249. int alternate_scan;
  250. int repeat_first_field;
  251. int chroma_420_type;
  252. int progressive_frame;
  253. int mpeg2;
  254. int full_pel[2];
  255. int interlaced_dct;
  256. int last_qscale;
  257. int first_slice;
  258. /* RTP specific */
  259. /* These are explained on avcodec.h */
  260. int rtp_mode;
  261. int rtp_payload_size;
  262. void (*rtp_callback)(void *data, int size, int packet_number);
  263. UINT8 *ptr_lastgob;
  264. UINT8 *ptr_last_mb_line;
  265. UINT32 mb_line_avgsize;
  266. DCTELEM block[6][64] __align8;
  267. void (*dct_unquantize)(struct MpegEncContext *s,
  268. DCTELEM *block, int n, int qscale);
  269. } MpegEncContext;
  270. int MPV_common_init(MpegEncContext *s);
  271. void MPV_common_end(MpegEncContext *s);
  272. void MPV_decode_mb(MpegEncContext *s, DCTELEM block[6][64]);
  273. void MPV_frame_start(MpegEncContext *s);
  274. void MPV_frame_end(MpegEncContext *s);
  275. #ifdef HAVE_MMX
  276. void MPV_common_init_mmx(MpegEncContext *s);
  277. #endif
  278. /* motion_est.c */
  279. int estimate_motion(MpegEncContext *s,
  280. int mb_x, int mb_y,
  281. int *mx_ptr, int *my_ptr);
  282. /* mpeg12.c */
  283. extern INT16 default_intra_matrix[64];
  284. extern INT16 default_non_intra_matrix[64];
  285. void mpeg1_encode_picture_header(MpegEncContext *s, int picture_number);
  286. void mpeg1_encode_mb(MpegEncContext *s,
  287. DCTELEM block[6][64],
  288. int motion_x, int motion_y);
  289. void mpeg1_encode_init(MpegEncContext *s);
  290. /* h263enc.c */
  291. /* run length table */
  292. #define MAX_RUN 64
  293. #define MAX_LEVEL 64
  294. typedef struct RLTable {
  295. int n; /* number of entries of table_vlc minus 1 */
  296. int last; /* number of values for last = 0 */
  297. const UINT16 (*table_vlc)[2];
  298. const INT8 *table_run;
  299. const INT8 *table_level;
  300. UINT8 *index_run[2]; /* encoding only */
  301. INT8 *max_level[2]; /* encoding & decoding */
  302. INT8 *max_run[2]; /* encoding & decoding */
  303. VLC vlc; /* decoding only */
  304. } RLTable;
  305. void init_rl(RLTable *rl);
  306. void init_vlc_rl(RLTable *rl);
  307. static inline int get_rl_index(const RLTable *rl, int last, int run, int level)
  308. {
  309. int index;
  310. index = rl->index_run[last][run];
  311. if (index >= rl->n)
  312. return rl->n;
  313. if (level > rl->max_level[last][run])
  314. return rl->n;
  315. return index + level - 1;
  316. }
  317. void h263_encode_mb(MpegEncContext *s,
  318. DCTELEM block[6][64],
  319. int motion_x, int motion_y);
  320. void mpeg4_encode_mb(MpegEncContext *s,
  321. DCTELEM block[6][64],
  322. int motion_x, int motion_y);
  323. void h263_encode_picture_header(MpegEncContext *s, int picture_number);
  324. int h263_encode_gob_header(MpegEncContext * s, int mb_line);
  325. void h263_dc_scale(MpegEncContext *s);
  326. INT16 *h263_pred_motion(MpegEncContext * s, int block,
  327. int *px, int *py);
  328. void mpeg4_pred_ac(MpegEncContext * s, INT16 *block, int n,
  329. int dir);
  330. void mpeg4_encode_picture_header(MpegEncContext *s, int picture_number);
  331. void h263_encode_init(MpegEncContext *s);
  332. void h263_decode_init_vlc(MpegEncContext *s);
  333. int h263_decode_picture_header(MpegEncContext *s);
  334. int h263_decode_gob_header(MpegEncContext *s);
  335. int mpeg4_decode_picture_header(MpegEncContext * s);
  336. int intel_h263_decode_picture_header(MpegEncContext *s);
  337. int h263_decode_mb(MpegEncContext *s,
  338. DCTELEM block[6][64]);
  339. int h263_get_picture_format(int width, int height);
  340. /* rv10.c */
  341. void rv10_encode_picture_header(MpegEncContext *s, int picture_number);
  342. int rv_decode_dc(MpegEncContext *s, int n);
  343. /* msmpeg4.c */
  344. void msmpeg4_encode_picture_header(MpegEncContext * s, int picture_number);
  345. void msmpeg4_encode_ext_header(MpegEncContext * s);
  346. void msmpeg4_encode_mb(MpegEncContext * s,
  347. DCTELEM block[6][64],
  348. int motion_x, int motion_y);
  349. void msmpeg4_dc_scale(MpegEncContext * s);
  350. int msmpeg4_decode_picture_header(MpegEncContext * s);
  351. int msmpeg4_decode_ext_header(MpegEncContext * s, int buf_size);
  352. int msmpeg4_decode_mb(MpegEncContext *s,
  353. DCTELEM block[6][64]);
  354. int msmpeg4_decode_init_vlc(MpegEncContext *s);
  355. /* mjpegenc.c */
  356. int mjpeg_init(MpegEncContext *s);
  357. void mjpeg_close(MpegEncContext *s);
  358. void mjpeg_encode_mb(MpegEncContext *s,
  359. DCTELEM block[6][64]);
  360. void mjpeg_picture_header(MpegEncContext *s);
  361. void mjpeg_picture_trailer(MpegEncContext *s);