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.

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