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.

3202 lines
110KB

  1. /*
  2. * The simplest mpeg encoder (well, it was the simplest!)
  3. * Copyright (c) 2000,2001 Fabrice Bellard.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library 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 GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. * 4MV & hq & b-frame encoding stuff by Michael Niedermayer <michaelni@gmx.at>
  20. */
  21. #include <ctype.h>
  22. #include "avcodec.h"
  23. #include "dsputil.h"
  24. #include "mpegvideo.h"
  25. #ifdef USE_FASTMEMCPY
  26. #include "fastmemcpy.h"
  27. #endif
  28. static void encode_picture(MpegEncContext *s, int picture_number);
  29. static void dct_unquantize_mpeg1_c(MpegEncContext *s,
  30. DCTELEM *block, int n, int qscale);
  31. static void dct_unquantize_mpeg2_c(MpegEncContext *s,
  32. DCTELEM *block, int n, int qscale);
  33. static void dct_unquantize_h263_c(MpegEncContext *s,
  34. DCTELEM *block, int n, int qscale);
  35. static void draw_edges_c(UINT8 *buf, int wrap, int width, int height, int w);
  36. static int dct_quantize_c(MpegEncContext *s, DCTELEM *block, int n, int qscale, int *overflow);
  37. void (*draw_edges)(UINT8 *buf, int wrap, int width, int height, int w)= draw_edges_c;
  38. static void emulated_edge_mc(MpegEncContext *s, UINT8 *src, int linesize, int block_w, int block_h,
  39. int src_x, int src_y, int w, int h);
  40. #define EDGE_WIDTH 16
  41. /* enable all paranoid tests for rounding, overflows, etc... */
  42. //#define PARANOID
  43. //#define DEBUG
  44. /* for jpeg fast DCT */
  45. #define CONST_BITS 14
  46. static const unsigned short aanscales[64] = {
  47. /* precomputed values scaled up by 14 bits */
  48. 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
  49. 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270,
  50. 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906,
  51. 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315,
  52. 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
  53. 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552,
  54. 8867, 12299, 11585, 10426, 8867, 6967, 4799, 2446,
  55. 4520, 6270, 5906, 5315, 4520, 3552, 2446, 1247
  56. };
  57. static UINT8 h263_chroma_roundtab[16] = {
  58. 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2,
  59. };
  60. static UINT16 default_mv_penalty[MAX_FCODE+1][MAX_MV*2+1];
  61. static UINT8 default_fcode_tab[MAX_MV*2+1];
  62. extern UINT8 zigzag_end[64];
  63. /* default motion estimation */
  64. int motion_estimation_method = ME_EPZS;
  65. static void convert_matrix(MpegEncContext *s, int (*qmat)[64], uint16_t (*qmat16)[64], uint16_t (*qmat16_bias)[64],
  66. const UINT16 *quant_matrix, int bias)
  67. {
  68. int qscale;
  69. for(qscale=1; qscale<32; qscale++){
  70. int i;
  71. if (s->fdct == ff_jpeg_fdct_islow) {
  72. for(i=0;i<64;i++) {
  73. const int j= block_permute_op(i);
  74. /* 16 <= qscale * quant_matrix[i] <= 7905 */
  75. /* 19952 <= aanscales[i] * qscale * quant_matrix[i] <= 249205026 */
  76. /* (1<<36)/19952 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= (1<<36)/249205026 */
  77. /* 3444240 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= 275 */
  78. qmat[qscale][j] = (int)((UINT64_C(1) << QMAT_SHIFT) /
  79. (qscale * quant_matrix[j]));
  80. }
  81. } else if (s->fdct == fdct_ifast) {
  82. for(i=0;i<64;i++) {
  83. const int j= block_permute_op(i);
  84. /* 16 <= qscale * quant_matrix[i] <= 7905 */
  85. /* 19952 <= aanscales[i] * qscale * quant_matrix[i] <= 249205026 */
  86. /* (1<<36)/19952 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= (1<<36)/249205026 */
  87. /* 3444240 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= 275 */
  88. qmat[qscale][j] = (int)((UINT64_C(1) << (QMAT_SHIFT + 14)) /
  89. (aanscales[i] * qscale * quant_matrix[j]));
  90. }
  91. } else {
  92. for(i=0;i<64;i++) {
  93. /* We can safely suppose that 16 <= quant_matrix[i] <= 255
  94. So 16 <= qscale * quant_matrix[i] <= 7905
  95. so (1<<19) / 16 >= (1<<19) / (qscale * quant_matrix[i]) >= (1<<19) / 7905
  96. so 32768 >= (1<<19) / (qscale * quant_matrix[i]) >= 67
  97. */
  98. qmat [qscale][i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[i]);
  99. qmat16[qscale][i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[block_permute_op(i)]);
  100. if(qmat16[qscale][i]==0 || qmat16[qscale][i]==128*256) qmat16[qscale][i]=128*256-1;
  101. qmat16_bias[qscale][i]= ROUNDED_DIV(bias<<(16-QUANT_BIAS_SHIFT), qmat16[qscale][i]);
  102. }
  103. }
  104. }
  105. }
  106. // move into common.c perhaps
  107. #define CHECKED_ALLOCZ(p, size)\
  108. {\
  109. p= av_mallocz(size);\
  110. if(p==NULL){\
  111. perror("malloc");\
  112. goto fail;\
  113. }\
  114. }
  115. /* init common structure for both encoder and decoder */
  116. int MPV_common_init(MpegEncContext *s)
  117. {
  118. int c_size, i;
  119. UINT8 *pict;
  120. s->dct_unquantize_h263 = dct_unquantize_h263_c;
  121. s->dct_unquantize_mpeg1 = dct_unquantize_mpeg1_c;
  122. s->dct_unquantize_mpeg2 = dct_unquantize_mpeg2_c;
  123. s->dct_quantize= dct_quantize_c;
  124. if(s->avctx->dct_algo==FF_DCT_FASTINT)
  125. s->fdct = fdct_ifast;
  126. else
  127. s->fdct = ff_jpeg_fdct_islow;
  128. #ifdef HAVE_MMX
  129. MPV_common_init_mmx(s);
  130. #endif
  131. #ifdef ARCH_ALPHA
  132. MPV_common_init_axp(s);
  133. #endif
  134. #ifdef HAVE_MLIB
  135. MPV_common_init_mlib(s);
  136. #endif
  137. s->mb_width = (s->width + 15) / 16;
  138. s->mb_height = (s->height + 15) / 16;
  139. /* set default edge pos, will be overriden in decode_header if needed */
  140. s->h_edge_pos= s->mb_width*16;
  141. s->v_edge_pos= s->mb_height*16;
  142. /* convert fourcc to upper case */
  143. s->avctx->fourcc= toupper( s->avctx->fourcc &0xFF)
  144. + (toupper((s->avctx->fourcc>>8 )&0xFF)<<8 )
  145. + (toupper((s->avctx->fourcc>>16)&0xFF)<<16)
  146. + (toupper((s->avctx->fourcc>>24)&0xFF)<<24);
  147. s->mb_num = s->mb_width * s->mb_height;
  148. if(!(s->flags&CODEC_FLAG_DR1)){
  149. s->linesize = s->mb_width * 16 + 2 * EDGE_WIDTH;
  150. s->uvlinesize = s->mb_width * 8 + EDGE_WIDTH;
  151. for(i=0;i<3;i++) {
  152. int w, h, shift, pict_start;
  153. w = s->linesize;
  154. h = s->mb_height * 16 + 2 * EDGE_WIDTH;
  155. shift = (i == 0) ? 0 : 1;
  156. c_size = (s->linesize>>shift) * (h >> shift);
  157. pict_start = (s->linesize>>shift) * (EDGE_WIDTH >> shift) + (EDGE_WIDTH >> shift);
  158. CHECKED_ALLOCZ(pict, c_size)
  159. s->last_picture_base[i] = pict;
  160. s->last_picture[i] = pict + pict_start;
  161. if(i>0) memset(s->last_picture_base[i], 128, c_size);
  162. CHECKED_ALLOCZ(pict, c_size)
  163. s->next_picture_base[i] = pict;
  164. s->next_picture[i] = pict + pict_start;
  165. if(i>0) memset(s->next_picture_base[i], 128, c_size);
  166. if (s->has_b_frames || s->codec_id==CODEC_ID_MPEG4) {
  167. /* Note the MPEG4 stuff is here cuz of buggy encoders which dont set the low_delay flag but
  168. do low-delay encoding, so we cant allways distinguish b-frame containing streams from low_delay streams */
  169. CHECKED_ALLOCZ(pict, c_size)
  170. s->aux_picture_base[i] = pict;
  171. s->aux_picture[i] = pict + pict_start;
  172. if(i>0) memset(s->aux_picture_base[i], 128, c_size);
  173. }
  174. }
  175. s->ip_buffer_count= 2;
  176. }
  177. CHECKED_ALLOCZ(s->edge_emu_buffer, (s->width+64)*2*17*2); //(width + edge + align)*interlaced*MBsize*tolerance
  178. if (s->encoding) {
  179. int j;
  180. int mv_table_size= (s->mb_width+2)*(s->mb_height+2);
  181. CHECKED_ALLOCZ(s->mb_var , s->mb_num * sizeof(INT16))
  182. CHECKED_ALLOCZ(s->mc_mb_var, s->mb_num * sizeof(INT16))
  183. CHECKED_ALLOCZ(s->mb_mean , s->mb_num * sizeof(INT8))
  184. /* Allocate MV tables */
  185. CHECKED_ALLOCZ(s->p_mv_table , mv_table_size * 2 * sizeof(INT16))
  186. CHECKED_ALLOCZ(s->b_forw_mv_table , mv_table_size * 2 * sizeof(INT16))
  187. CHECKED_ALLOCZ(s->b_back_mv_table , mv_table_size * 2 * sizeof(INT16))
  188. CHECKED_ALLOCZ(s->b_bidir_forw_mv_table , mv_table_size * 2 * sizeof(INT16))
  189. CHECKED_ALLOCZ(s->b_bidir_back_mv_table , mv_table_size * 2 * sizeof(INT16))
  190. CHECKED_ALLOCZ(s->b_direct_forw_mv_table, mv_table_size * 2 * sizeof(INT16))
  191. CHECKED_ALLOCZ(s->b_direct_back_mv_table, mv_table_size * 2 * sizeof(INT16))
  192. CHECKED_ALLOCZ(s->b_direct_mv_table , mv_table_size * 2 * sizeof(INT16))
  193. CHECKED_ALLOCZ(s->me_scratchpad, s->linesize*16*3*sizeof(uint8_t))
  194. CHECKED_ALLOCZ(s->me_map , ME_MAP_SIZE*sizeof(uint32_t))
  195. CHECKED_ALLOCZ(s->me_score_map, ME_MAP_SIZE*sizeof(uint16_t))
  196. if(s->max_b_frames){
  197. for(j=0; j<REORDER_BUFFER_SIZE; j++){
  198. int i;
  199. for(i=0;i<3;i++) {
  200. int w, h, shift;
  201. w = s->linesize;
  202. h = s->mb_height * 16;
  203. shift = (i == 0) ? 0 : 1;
  204. c_size = (w >> shift) * (h >> shift);
  205. CHECKED_ALLOCZ(pict, c_size);
  206. s->picture_buffer[j][i] = pict;
  207. }
  208. }
  209. }
  210. if(s->codec_id==CODEC_ID_MPEG4){
  211. CHECKED_ALLOCZ(s->tex_pb_buffer, PB_BUFFER_SIZE);
  212. CHECKED_ALLOCZ( s->pb2_buffer, PB_BUFFER_SIZE);
  213. }
  214. if(s->msmpeg4_version){
  215. CHECKED_ALLOCZ(s->ac_stats, 2*2*(MAX_LEVEL+1)*(MAX_RUN+1)*2*sizeof(int));
  216. }
  217. CHECKED_ALLOCZ(s->avctx->stats_out, 256);
  218. }
  219. if (s->out_format == FMT_H263 || s->encoding) {
  220. int size;
  221. /* Allocate MB type table */
  222. CHECKED_ALLOCZ(s->mb_type , s->mb_num * sizeof(UINT8))
  223. /* MV prediction */
  224. size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2);
  225. CHECKED_ALLOCZ(s->motion_val, size * 2 * sizeof(INT16));
  226. }
  227. if(s->codec_id==CODEC_ID_MPEG4){
  228. /* 4mv and interlaced direct mode decoding tables */
  229. CHECKED_ALLOCZ(s->co_located_type_table, s->mb_num * sizeof(UINT8))
  230. CHECKED_ALLOCZ(s->field_mv_table, s->mb_num*2*2 * sizeof(INT16))
  231. CHECKED_ALLOCZ(s->field_select_table, s->mb_num*2* sizeof(INT8))
  232. }
  233. if (s->h263_pred || s->h263_plus) {
  234. int y_size, c_size, i, size;
  235. /* dc values */
  236. y_size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2);
  237. c_size = (s->mb_width + 2) * (s->mb_height + 2);
  238. size = y_size + 2 * c_size;
  239. CHECKED_ALLOCZ(s->dc_val[0], size * sizeof(INT16));
  240. s->dc_val[1] = s->dc_val[0] + y_size;
  241. s->dc_val[2] = s->dc_val[1] + c_size;
  242. for(i=0;i<size;i++)
  243. s->dc_val[0][i] = 1024;
  244. /* ac values */
  245. CHECKED_ALLOCZ(s->ac_val[0], size * sizeof(INT16) * 16);
  246. s->ac_val[1] = s->ac_val[0] + y_size;
  247. s->ac_val[2] = s->ac_val[1] + c_size;
  248. /* cbp values */
  249. CHECKED_ALLOCZ(s->coded_block, y_size);
  250. /* divx501 bitstream reorder buffer */
  251. CHECKED_ALLOCZ(s->bitstream_buffer, BITSTREAM_BUFFER_SIZE);
  252. /* cbp, ac_pred, pred_dir */
  253. CHECKED_ALLOCZ(s->cbp_table , s->mb_num * sizeof(UINT8))
  254. CHECKED_ALLOCZ(s->pred_dir_table, s->mb_num * sizeof(UINT8))
  255. }
  256. CHECKED_ALLOCZ(s->qscale_table , s->mb_num * sizeof(UINT8))
  257. /* which mb is a intra block */
  258. CHECKED_ALLOCZ(s->mbintra_table, s->mb_num);
  259. memset(s->mbintra_table, 1, s->mb_num);
  260. /* default structure is frame */
  261. s->picture_structure = PICT_FRAME;
  262. /* init macroblock skip table */
  263. CHECKED_ALLOCZ(s->mbskip_table, s->mb_num);
  264. s->block= s->blocks[0];
  265. s->context_initialized = 1;
  266. return 0;
  267. fail:
  268. MPV_common_end(s);
  269. return -1;
  270. }
  271. //extern int sads;
  272. /* init common structure for both encoder and decoder */
  273. void MPV_common_end(MpegEncContext *s)
  274. {
  275. int i;
  276. av_freep(&s->mb_type);
  277. av_freep(&s->mb_var);
  278. av_freep(&s->mc_mb_var);
  279. av_freep(&s->mb_mean);
  280. av_freep(&s->p_mv_table);
  281. av_freep(&s->b_forw_mv_table);
  282. av_freep(&s->b_back_mv_table);
  283. av_freep(&s->b_bidir_forw_mv_table);
  284. av_freep(&s->b_bidir_back_mv_table);
  285. av_freep(&s->b_direct_forw_mv_table);
  286. av_freep(&s->b_direct_back_mv_table);
  287. av_freep(&s->b_direct_mv_table);
  288. av_freep(&s->motion_val);
  289. av_freep(&s->dc_val[0]);
  290. av_freep(&s->ac_val[0]);
  291. av_freep(&s->coded_block);
  292. av_freep(&s->mbintra_table);
  293. av_freep(&s->cbp_table);
  294. av_freep(&s->pred_dir_table);
  295. av_freep(&s->qscale_table);
  296. av_freep(&s->me_scratchpad);
  297. av_freep(&s->me_map);
  298. av_freep(&s->me_score_map);
  299. av_freep(&s->mbskip_table);
  300. av_freep(&s->bitstream_buffer);
  301. av_freep(&s->tex_pb_buffer);
  302. av_freep(&s->pb2_buffer);
  303. av_freep(&s->edge_emu_buffer);
  304. av_freep(&s->co_located_type_table);
  305. av_freep(&s->field_mv_table);
  306. av_freep(&s->field_select_table);
  307. av_freep(&s->avctx->stats_out);
  308. av_freep(&s->ac_stats);
  309. for(i=0;i<3;i++) {
  310. int j;
  311. if(!(s->flags&CODEC_FLAG_DR1)){
  312. av_freep(&s->last_picture_base[i]);
  313. av_freep(&s->next_picture_base[i]);
  314. av_freep(&s->aux_picture_base[i]);
  315. }
  316. s->last_picture_base[i]=
  317. s->next_picture_base[i]=
  318. s->aux_picture_base [i] = NULL;
  319. s->last_picture[i]=
  320. s->next_picture[i]=
  321. s->aux_picture [i] = NULL;
  322. for(j=0; j<REORDER_BUFFER_SIZE; j++){
  323. av_freep(&s->picture_buffer[j][i]);
  324. }
  325. }
  326. s->context_initialized = 0;
  327. }
  328. /* init video encoder */
  329. int MPV_encode_init(AVCodecContext *avctx)
  330. {
  331. MpegEncContext *s = avctx->priv_data;
  332. int i;
  333. avctx->pix_fmt = PIX_FMT_YUV420P;
  334. s->bit_rate = avctx->bit_rate;
  335. s->bit_rate_tolerance = avctx->bit_rate_tolerance;
  336. s->frame_rate = avctx->frame_rate;
  337. s->width = avctx->width;
  338. s->height = avctx->height;
  339. if(avctx->gop_size > 600){
  340. fprintf(stderr, "Warning keyframe interval too large! reducing it ...\n");
  341. avctx->gop_size=600;
  342. }
  343. s->gop_size = avctx->gop_size;
  344. s->rtp_mode = avctx->rtp_mode;
  345. s->rtp_payload_size = avctx->rtp_payload_size;
  346. if (avctx->rtp_callback)
  347. s->rtp_callback = avctx->rtp_callback;
  348. s->qmin= avctx->qmin;
  349. s->qmax= avctx->qmax;
  350. s->max_qdiff= avctx->max_qdiff;
  351. s->qcompress= avctx->qcompress;
  352. s->qblur= avctx->qblur;
  353. s->avctx = avctx;
  354. s->aspect_ratio_info= avctx->aspect_ratio_info;
  355. if (avctx->aspect_ratio_info == FF_ASPECT_EXTENDED)
  356. {
  357. s->aspected_width = avctx->aspected_width;
  358. s->aspected_height = avctx->aspected_height;
  359. }
  360. s->flags= avctx->flags;
  361. s->max_b_frames= avctx->max_b_frames;
  362. s->b_frame_strategy= avctx->b_frame_strategy;
  363. s->codec_id= avctx->codec->id;
  364. s->luma_elim_threshold = avctx->luma_elim_threshold;
  365. s->chroma_elim_threshold= avctx->chroma_elim_threshold;
  366. s->strict_std_compliance= avctx->strict_std_compliance;
  367. s->data_partitioning= avctx->flags & CODEC_FLAG_PART;
  368. s->mpeg_quant= avctx->mpeg_quant;
  369. if (s->gop_size <= 1) {
  370. s->intra_only = 1;
  371. s->gop_size = 12;
  372. } else {
  373. s->intra_only = 0;
  374. }
  375. /* ME algorithm */
  376. if (avctx->me_method == 0)
  377. /* For compatibility */
  378. s->me_method = motion_estimation_method;
  379. else
  380. s->me_method = avctx->me_method;
  381. /* Fixed QSCALE */
  382. s->fixed_qscale = (avctx->flags & CODEC_FLAG_QSCALE);
  383. s->adaptive_quant= ( s->avctx->lumi_masking
  384. || s->avctx->dark_masking
  385. || s->avctx->temporal_cplx_masking
  386. || s->avctx->spatial_cplx_masking
  387. || s->avctx->p_masking)
  388. && !s->fixed_qscale;
  389. s->progressive_sequence= !(avctx->flags & CODEC_FLAG_INTERLACED_DCT);
  390. switch(avctx->codec->id) {
  391. case CODEC_ID_MPEG1VIDEO:
  392. s->out_format = FMT_MPEG1;
  393. avctx->delay=0; //FIXME not sure, should check the spec
  394. break;
  395. case CODEC_ID_MJPEG:
  396. s->out_format = FMT_MJPEG;
  397. s->intra_only = 1; /* force intra only for jpeg */
  398. s->mjpeg_write_tables = 1; /* write all tables */
  399. s->mjpeg_data_only_frames = 0; /* write all the needed headers */
  400. s->mjpeg_vsample[0] = 2; /* set up default sampling factors */
  401. s->mjpeg_vsample[1] = 1; /* the only currently supported values */
  402. s->mjpeg_vsample[2] = 1;
  403. s->mjpeg_hsample[0] = 2;
  404. s->mjpeg_hsample[1] = 1;
  405. s->mjpeg_hsample[2] = 1;
  406. if (mjpeg_init(s) < 0)
  407. return -1;
  408. avctx->delay=0;
  409. break;
  410. case CODEC_ID_H263:
  411. if (h263_get_picture_format(s->width, s->height) == 7) {
  412. printf("Input picture size isn't suitable for h263 codec! try h263+\n");
  413. return -1;
  414. }
  415. s->out_format = FMT_H263;
  416. avctx->delay=0;
  417. break;
  418. case CODEC_ID_H263P:
  419. s->out_format = FMT_H263;
  420. s->rtp_mode = 1;
  421. s->rtp_payload_size = 1200;
  422. s->h263_plus = 1;
  423. s->unrestricted_mv = 1;
  424. s->h263_aic = 1;
  425. /* These are just to be sure */
  426. s->umvplus = 0;
  427. s->umvplus_dec = 0;
  428. avctx->delay=0;
  429. break;
  430. case CODEC_ID_RV10:
  431. s->out_format = FMT_H263;
  432. s->h263_rv10 = 1;
  433. avctx->delay=0;
  434. break;
  435. case CODEC_ID_MPEG4:
  436. s->out_format = FMT_H263;
  437. s->h263_pred = 1;
  438. s->unrestricted_mv = 1;
  439. s->has_b_frames= s->max_b_frames ? 1 : 0;
  440. s->low_delay=0;
  441. avctx->delay= s->low_delay ? 0 : (s->max_b_frames + 1);
  442. break;
  443. case CODEC_ID_MSMPEG4V1:
  444. s->out_format = FMT_H263;
  445. s->h263_msmpeg4 = 1;
  446. s->h263_pred = 1;
  447. s->unrestricted_mv = 1;
  448. s->msmpeg4_version= 1;
  449. avctx->delay=0;
  450. break;
  451. case CODEC_ID_MSMPEG4V2:
  452. s->out_format = FMT_H263;
  453. s->h263_msmpeg4 = 1;
  454. s->h263_pred = 1;
  455. s->unrestricted_mv = 1;
  456. s->msmpeg4_version= 2;
  457. avctx->delay=0;
  458. break;
  459. case CODEC_ID_MSMPEG4V3:
  460. s->out_format = FMT_H263;
  461. s->h263_msmpeg4 = 1;
  462. s->h263_pred = 1;
  463. s->unrestricted_mv = 1;
  464. s->msmpeg4_version= 3;
  465. avctx->delay=0;
  466. break;
  467. case CODEC_ID_WMV1:
  468. s->out_format = FMT_H263;
  469. s->h263_msmpeg4 = 1;
  470. s->h263_pred = 1;
  471. s->unrestricted_mv = 1;
  472. s->msmpeg4_version= 4;
  473. avctx->delay=0;
  474. break;
  475. case CODEC_ID_WMV2:
  476. s->out_format = FMT_H263;
  477. s->h263_msmpeg4 = 1;
  478. s->h263_pred = 1;
  479. s->unrestricted_mv = 1;
  480. s->msmpeg4_version= 5;
  481. avctx->delay=0;
  482. break;
  483. default:
  484. return -1;
  485. }
  486. { /* set up some save defaults, some codecs might override them later */
  487. static int done=0;
  488. if(!done){
  489. int i;
  490. done=1;
  491. memset(default_mv_penalty, 0, sizeof(UINT16)*(MAX_FCODE+1)*(2*MAX_MV+1));
  492. memset(default_fcode_tab , 0, sizeof(UINT8)*(2*MAX_MV+1));
  493. for(i=-16; i<16; i++){
  494. default_fcode_tab[i + MAX_MV]= 1;
  495. }
  496. }
  497. }
  498. s->mv_penalty= default_mv_penalty;
  499. s->fcode_tab= default_fcode_tab;
  500. s->y_dc_scale_table=
  501. s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
  502. if (s->out_format == FMT_H263)
  503. h263_encode_init(s);
  504. else if (s->out_format == FMT_MPEG1)
  505. ff_mpeg1_encode_init(s);
  506. if(s->msmpeg4_version)
  507. ff_msmpeg4_encode_init(s);
  508. /* dont use mv_penalty table for crap MV as it would be confused */
  509. if (s->me_method < ME_EPZS) s->mv_penalty = default_mv_penalty;
  510. s->encoding = 1;
  511. /* init */
  512. if (MPV_common_init(s) < 0)
  513. return -1;
  514. /* init default q matrix */
  515. for(i=0;i<64;i++) {
  516. if(s->codec_id==CODEC_ID_MPEG4 && s->mpeg_quant){
  517. s->intra_matrix[i] = ff_mpeg4_default_intra_matrix[i];
  518. s->inter_matrix[i] = ff_mpeg4_default_non_intra_matrix[i];
  519. }else if(s->out_format == FMT_H263){
  520. s->intra_matrix[i] =
  521. s->inter_matrix[i] = ff_mpeg1_default_non_intra_matrix[i];
  522. }else{ /* mpeg1 */
  523. s->intra_matrix[i] = ff_mpeg1_default_intra_matrix[i];
  524. s->inter_matrix[i] = ff_mpeg1_default_non_intra_matrix[i];
  525. }
  526. }
  527. /* precompute matrix */
  528. /* for mjpeg, we do include qscale in the matrix */
  529. if (s->out_format != FMT_MJPEG) {
  530. convert_matrix(s, s->q_intra_matrix, s->q_intra_matrix16, s->q_intra_matrix16_bias,
  531. s->intra_matrix, s->intra_quant_bias);
  532. convert_matrix(s, s->q_inter_matrix, s->q_inter_matrix16, s->q_inter_matrix16_bias,
  533. s->inter_matrix, s->inter_quant_bias);
  534. }
  535. if(ff_rate_control_init(s) < 0)
  536. return -1;
  537. s->picture_number = 0;
  538. s->picture_in_gop_number = 0;
  539. s->fake_picture_number = 0;
  540. /* motion detector init */
  541. s->f_code = 1;
  542. s->b_code = 1;
  543. return 0;
  544. }
  545. int MPV_encode_end(AVCodecContext *avctx)
  546. {
  547. MpegEncContext *s = avctx->priv_data;
  548. #ifdef STATS
  549. print_stats();
  550. #endif
  551. ff_rate_control_uninit(s);
  552. MPV_common_end(s);
  553. if (s->out_format == FMT_MJPEG)
  554. mjpeg_close(s);
  555. return 0;
  556. }
  557. /* draw the edges of width 'w' of an image of size width, height */
  558. //FIXME check that this is ok for mpeg4 interlaced
  559. static void draw_edges_c(UINT8 *buf, int wrap, int width, int height, int w)
  560. {
  561. UINT8 *ptr, *last_line;
  562. int i;
  563. last_line = buf + (height - 1) * wrap;
  564. for(i=0;i<w;i++) {
  565. /* top and bottom */
  566. memcpy(buf - (i + 1) * wrap, buf, width);
  567. memcpy(last_line + (i + 1) * wrap, last_line, width);
  568. }
  569. /* left and right */
  570. ptr = buf;
  571. for(i=0;i<height;i++) {
  572. memset(ptr - w, ptr[0], w);
  573. memset(ptr + width, ptr[width-1], w);
  574. ptr += wrap;
  575. }
  576. /* corners */
  577. for(i=0;i<w;i++) {
  578. memset(buf - (i + 1) * wrap - w, buf[0], w); /* top left */
  579. memset(buf - (i + 1) * wrap + width, buf[width-1], w); /* top right */
  580. memset(last_line + (i + 1) * wrap - w, last_line[0], w); /* top left */
  581. memset(last_line + (i + 1) * wrap + width, last_line[width-1], w); /* top right */
  582. }
  583. }
  584. /* generic function for encode/decode called before a frame is coded/decoded */
  585. void MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx)
  586. {
  587. int i;
  588. UINT8 *tmp;
  589. s->mb_skiped = 0;
  590. s->decoding_error=0;
  591. avctx->mbskip_table= s->mbskip_table;
  592. if(avctx->flags&CODEC_FLAG_DR1){
  593. avctx->get_buffer_callback(avctx, s->width, s->height, s->pict_type);
  594. s->linesize = avctx->dr_stride;
  595. s->uvlinesize= avctx->dr_uvstride;
  596. s->ip_buffer_count= avctx->dr_ip_buffer_count;
  597. }
  598. avctx->dr_ip_buffer_count= s->ip_buffer_count;
  599. if (s->pict_type == B_TYPE) {
  600. for(i=0;i<3;i++) {
  601. if(avctx->flags&CODEC_FLAG_DR1)
  602. s->aux_picture[i]= avctx->dr_buffer[i];
  603. //FIXME the following should never be needed, the decoder should drop b frames if no reference is available
  604. if(s->next_picture[i]==NULL)
  605. s->next_picture[i]= s->aux_picture[i];
  606. if(s->last_picture[i]==NULL)
  607. s->last_picture[i]= s->next_picture[i];
  608. s->current_picture[i] = s->aux_picture[i];
  609. }
  610. } else {
  611. for(i=0;i<3;i++) {
  612. /* swap next and last */
  613. if(avctx->flags&CODEC_FLAG_DR1)
  614. tmp= avctx->dr_buffer[i];
  615. else
  616. tmp = s->last_picture[i];
  617. s->last_picture[i] = s->next_picture[i];
  618. s->next_picture[i] = tmp;
  619. s->current_picture[i] = tmp;
  620. if(s->last_picture[i]==NULL)
  621. s->last_picture[i]= s->next_picture[i];
  622. s->last_dr_opaque= s->next_dr_opaque;
  623. s->next_dr_opaque= avctx->dr_opaque_frame;
  624. if(s->has_b_frames && s->last_dr_opaque && s->codec_id!=CODEC_ID_SVQ1)
  625. avctx->dr_opaque_frame= s->last_dr_opaque;
  626. else
  627. avctx->dr_opaque_frame= s->next_dr_opaque;
  628. }
  629. }
  630. /* set dequantizer, we cant do it during init as it might change for mpeg4
  631. and we cant do it in the header decode as init isnt called for mpeg4 there yet */
  632. if(s->out_format == FMT_H263){
  633. if(s->mpeg_quant)
  634. s->dct_unquantize = s->dct_unquantize_mpeg2;
  635. else
  636. s->dct_unquantize = s->dct_unquantize_h263;
  637. }else
  638. s->dct_unquantize = s->dct_unquantize_mpeg1;
  639. }
  640. /* generic function for encode/decode called after a frame has been coded/decoded */
  641. void MPV_frame_end(MpegEncContext *s)
  642. {
  643. s->avctx->key_frame = (s->pict_type == I_TYPE);
  644. s->avctx->pict_type = s->pict_type;
  645. /* draw edge for correct motion prediction if outside */
  646. if (s->pict_type != B_TYPE && !s->intra_only && !(s->flags&CODEC_FLAG_EMU_EDGE)) {
  647. draw_edges(s->current_picture[0], s->linesize , s->h_edge_pos , s->v_edge_pos , EDGE_WIDTH );
  648. draw_edges(s->current_picture[1], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2);
  649. draw_edges(s->current_picture[2], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2);
  650. }
  651. emms_c();
  652. s->last_pict_type = s->pict_type;
  653. if(s->pict_type!=B_TYPE){
  654. s->last_non_b_pict_type= s->pict_type;
  655. s->num_available_buffers++;
  656. if(s->num_available_buffers>2) s->num_available_buffers= 2;
  657. }
  658. }
  659. /* reorder input for encoding */
  660. void reorder_input(MpegEncContext *s, AVPicture *pict)
  661. {
  662. int i, j, index;
  663. if(s->max_b_frames > FF_MAX_B_FRAMES) s->max_b_frames= FF_MAX_B_FRAMES;
  664. // delay= s->max_b_frames+1; (or 0 if no b frames cuz decoder diff)
  665. for(j=0; j<REORDER_BUFFER_SIZE-1; j++){
  666. s->coded_order[j]= s->coded_order[j+1];
  667. }
  668. s->coded_order[j].picture[0]= s->coded_order[j].picture[1]= s->coded_order[j].picture[2]= NULL; //catch uninitalized buffers
  669. s->coded_order[j].pict_type=0;
  670. switch(s->input_pict_type){
  671. default:
  672. case I_TYPE:
  673. case S_TYPE:
  674. case P_TYPE:
  675. index= s->max_b_frames - s->b_frames_since_non_b;
  676. s->b_frames_since_non_b=0;
  677. break;
  678. case B_TYPE:
  679. index= s->max_b_frames + 1;
  680. s->b_frames_since_non_b++;
  681. break;
  682. }
  683. //printf("index:%d type:%d strides: %d %d\n", index, s->input_pict_type, pict->linesize[0], s->linesize);
  684. if( (index==0 || (s->flags&CODEC_FLAG_INPUT_PRESERVED))
  685. && pict->linesize[0] == s->linesize
  686. && pict->linesize[1] == s->uvlinesize
  687. && pict->linesize[2] == s->uvlinesize){
  688. //printf("ptr\n");
  689. for(i=0; i<3; i++){
  690. s->coded_order[index].picture[i]= pict->data[i];
  691. }
  692. }else{
  693. //printf("copy\n");
  694. for(i=0; i<3; i++){
  695. uint8_t *src = pict->data[i];
  696. uint8_t *dest;
  697. int src_wrap = pict->linesize[i];
  698. int dest_wrap = s->linesize;
  699. int w = s->width;
  700. int h = s->height;
  701. if(index==0) dest= s->last_picture[i]+16; //is current_picture indeed but the switch hapens after reordering
  702. else dest= s->picture_buffer[s->picture_buffer_index][i];
  703. if (i >= 1) {
  704. dest_wrap >>= 1;
  705. w >>= 1;
  706. h >>= 1;
  707. }
  708. s->coded_order[index].picture[i]= dest;
  709. for(j=0;j<h;j++) {
  710. memcpy(dest, src, w);
  711. dest += dest_wrap;
  712. src += src_wrap;
  713. }
  714. }
  715. if(index!=0){
  716. s->picture_buffer_index++;
  717. if(s->picture_buffer_index >= REORDER_BUFFER_SIZE) s->picture_buffer_index=0;
  718. }
  719. }
  720. s->coded_order[index].pict_type = s->input_pict_type;
  721. s->coded_order[index].qscale = s->input_qscale;
  722. s->coded_order[index].force_type= s->force_input_type;
  723. s->coded_order[index].picture_in_gop_number= s->input_picture_in_gop_number;
  724. s->coded_order[index].picture_number= s->input_picture_number;
  725. for(i=0; i<3; i++){
  726. s->new_picture[i]= s->coded_order[0].picture[i];
  727. }
  728. }
  729. int MPV_encode_picture(AVCodecContext *avctx,
  730. unsigned char *buf, int buf_size, void *data)
  731. {
  732. MpegEncContext *s = avctx->priv_data;
  733. AVPicture *pict = data;
  734. s->input_qscale = avctx->quality;
  735. init_put_bits(&s->pb, buf, buf_size, NULL, NULL);
  736. if(avctx->flags&CODEC_FLAG_TYPE){
  737. s->input_pict_type=
  738. s->force_input_type= avctx->key_frame ? I_TYPE : P_TYPE;
  739. }else if(s->flags&CODEC_FLAG_PASS2){
  740. s->input_pict_type=
  741. s->force_input_type= s->rc_context.entry[s->input_picture_number].new_pict_type;
  742. }else{
  743. s->force_input_type=0;
  744. if (!s->intra_only) {
  745. /* first picture of GOP is intra */
  746. if (s->input_picture_in_gop_number % s->gop_size==0){
  747. s->input_pict_type = I_TYPE;
  748. }else if(s->max_b_frames==0){
  749. s->input_pict_type = P_TYPE;
  750. }else{
  751. if(s->b_frames_since_non_b < s->max_b_frames) //FIXME more IQ
  752. s->input_pict_type = B_TYPE;
  753. else
  754. s->input_pict_type = P_TYPE;
  755. }
  756. } else {
  757. s->input_pict_type = I_TYPE;
  758. }
  759. }
  760. if(s->input_pict_type==I_TYPE)
  761. s->input_picture_in_gop_number=0;
  762. reorder_input(s, pict);
  763. /* output? */
  764. if(s->coded_order[0].picture[0]){
  765. s->pict_type= s->coded_order[0].pict_type;
  766. if (s->fixed_qscale) /* the ratecontrol needs the last qscale so we dont touch it for CBR */
  767. s->qscale= s->coded_order[0].qscale;
  768. s->force_type= s->coded_order[0].force_type;
  769. s->picture_in_gop_number= s->coded_order[0].picture_in_gop_number;
  770. s->picture_number= s->coded_order[0].picture_number;
  771. MPV_frame_start(s, avctx);
  772. encode_picture(s, s->picture_number);
  773. avctx->real_pict_num = s->picture_number;
  774. avctx->header_bits = s->header_bits;
  775. avctx->mv_bits = s->mv_bits;
  776. avctx->misc_bits = s->misc_bits;
  777. avctx->i_tex_bits = s->i_tex_bits;
  778. avctx->p_tex_bits = s->p_tex_bits;
  779. avctx->i_count = s->i_count;
  780. avctx->p_count = s->mb_num - s->i_count - s->skip_count; //FIXME f/b_count in avctx
  781. avctx->skip_count = s->skip_count;
  782. MPV_frame_end(s);
  783. if (s->out_format == FMT_MJPEG)
  784. mjpeg_picture_trailer(s);
  785. if(!s->fixed_qscale)
  786. avctx->quality = s->qscale;
  787. if(s->flags&CODEC_FLAG_PASS1)
  788. ff_write_pass1_stats(s);
  789. }
  790. s->input_picture_number++;
  791. s->input_picture_in_gop_number++;
  792. flush_put_bits(&s->pb);
  793. s->frame_bits = (pbBufPtr(&s->pb) - s->pb.buf) * 8;
  794. s->total_bits += s->frame_bits;
  795. avctx->frame_bits = s->frame_bits;
  796. //printf("fcode: %d, type: %d, head: %d, mv: %d, misc: %d, frame: %d, itex: %d, ptex: %d\n",
  797. //s->f_code, avctx->key_frame, s->header_bits, s->mv_bits, s->misc_bits, s->frame_bits, s->i_tex_bits, s->p_tex_bits);
  798. #if 0 //dump some stats to stats.txt for testing/debuging
  799. if(s->max_b_frames==0)
  800. {
  801. static FILE *f=NULL;
  802. if(!f) f= fopen("stats.txt", "wb");
  803. get_psnr(pict->data, s->current_picture,
  804. pict->linesize, s->linesize, avctx);
  805. fprintf(f, "%7d, %7d, %2.4f\n", pbBufPtr(&s->pb) - s->pb.buf, s->qscale, avctx->psnr_y);
  806. }
  807. #endif
  808. if (avctx->get_psnr) {
  809. /* At this point pict->data should have the original frame */
  810. /* an s->current_picture should have the coded/decoded frame */
  811. get_psnr(pict->data, s->current_picture,
  812. pict->linesize, s->linesize, avctx);
  813. // printf("%f\n", avctx->psnr_y);
  814. }
  815. return pbBufPtr(&s->pb) - s->pb.buf;
  816. }
  817. static inline void gmc1_motion(MpegEncContext *s,
  818. UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
  819. int dest_offset,
  820. UINT8 **ref_picture, int src_offset,
  821. int h)
  822. {
  823. UINT8 *ptr;
  824. int offset, src_x, src_y, linesize, uvlinesize;
  825. int motion_x, motion_y;
  826. int emu=0;
  827. if(s->real_sprite_warping_points>1) printf("more than 1 warp point isnt supported\n");
  828. motion_x= s->sprite_offset[0][0];
  829. motion_y= s->sprite_offset[0][1];
  830. src_x = s->mb_x * 16 + (motion_x >> (s->sprite_warping_accuracy+1));
  831. src_y = s->mb_y * 16 + (motion_y >> (s->sprite_warping_accuracy+1));
  832. motion_x<<=(3-s->sprite_warping_accuracy);
  833. motion_y<<=(3-s->sprite_warping_accuracy);
  834. src_x = clip(src_x, -16, s->width);
  835. if (src_x == s->width)
  836. motion_x =0;
  837. src_y = clip(src_y, -16, s->height);
  838. if (src_y == s->height)
  839. motion_y =0;
  840. linesize = s->linesize;
  841. uvlinesize = s->uvlinesize;
  842. ptr = ref_picture[0] + (src_y * linesize) + src_x + src_offset;
  843. dest_y+=dest_offset;
  844. if(s->flags&CODEC_FLAG_EMU_EDGE){
  845. if(src_x<0 || src_y<0 || src_x + (motion_x&15) + 16 > s->h_edge_pos
  846. || src_y + (motion_y&15) + h > s->v_edge_pos){
  847. emulated_edge_mc(s, ptr, linesize, 17, h+1, src_x, src_y, s->h_edge_pos, s->v_edge_pos);
  848. ptr= s->edge_emu_buffer;
  849. emu=1;
  850. }
  851. }
  852. gmc1(dest_y , ptr , linesize, h, motion_x&15, motion_y&15, s->no_rounding);
  853. gmc1(dest_y+8, ptr+8, linesize, h, motion_x&15, motion_y&15, s->no_rounding);
  854. motion_x= s->sprite_offset[1][0];
  855. motion_y= s->sprite_offset[1][1];
  856. src_x = s->mb_x * 8 + (motion_x >> (s->sprite_warping_accuracy+1));
  857. src_y = s->mb_y * 8 + (motion_y >> (s->sprite_warping_accuracy+1));
  858. motion_x<<=(3-s->sprite_warping_accuracy);
  859. motion_y<<=(3-s->sprite_warping_accuracy);
  860. src_x = clip(src_x, -8, s->width>>1);
  861. if (src_x == s->width>>1)
  862. motion_x =0;
  863. src_y = clip(src_y, -8, s->height>>1);
  864. if (src_y == s->height>>1)
  865. motion_y =0;
  866. offset = (src_y * uvlinesize) + src_x + (src_offset>>1);
  867. ptr = ref_picture[1] + offset;
  868. if(emu){
  869. emulated_edge_mc(s, ptr, uvlinesize, 9, (h>>1)+1, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
  870. ptr= s->edge_emu_buffer;
  871. }
  872. gmc1(dest_cb + (dest_offset>>1), ptr, uvlinesize, h>>1, motion_x&15, motion_y&15, s->no_rounding);
  873. ptr = ref_picture[2] + offset;
  874. if(emu){
  875. emulated_edge_mc(s, ptr, uvlinesize, 9, (h>>1)+1, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
  876. ptr= s->edge_emu_buffer;
  877. }
  878. gmc1(dest_cr + (dest_offset>>1), ptr, uvlinesize, h>>1, motion_x&15, motion_y&15, s->no_rounding);
  879. return;
  880. }
  881. static void emulated_edge_mc(MpegEncContext *s, UINT8 *src, int linesize, int block_w, int block_h,
  882. int src_x, int src_y, int w, int h){
  883. int x, y;
  884. int start_y, start_x, end_y, end_x;
  885. UINT8 *buf= s->edge_emu_buffer;
  886. if(src_y>= h){
  887. src+= (h-1-src_y)*linesize;
  888. src_y=h-1;
  889. }else if(src_y<=-block_h){
  890. src+= (1-block_h-src_y)*linesize;
  891. src_y=1-block_h;
  892. }
  893. if(src_x>= w){
  894. src+= (w-1-src_x);
  895. src_x=w-1;
  896. }else if(src_x<=-block_w){
  897. src+= (1-block_w-src_x);
  898. src_x=1-block_w;
  899. }
  900. start_y= MAX(0, -src_y);
  901. start_x= MAX(0, -src_x);
  902. end_y= MIN(block_h, h-src_y);
  903. end_x= MIN(block_w, w-src_x);
  904. // copy existing part
  905. for(y=start_y; y<end_y; y++){
  906. for(x=start_x; x<end_x; x++){
  907. buf[x + y*linesize]= src[x + y*linesize];
  908. }
  909. }
  910. //top
  911. for(y=0; y<start_y; y++){
  912. for(x=start_x; x<end_x; x++){
  913. buf[x + y*linesize]= buf[x + start_y*linesize];
  914. }
  915. }
  916. //bottom
  917. for(y=end_y; y<block_h; y++){
  918. for(x=start_x; x<end_x; x++){
  919. buf[x + y*linesize]= buf[x + (end_y-1)*linesize];
  920. }
  921. }
  922. for(y=0; y<block_h; y++){
  923. //left
  924. for(x=0; x<start_x; x++){
  925. buf[x + y*linesize]= buf[start_x + y*linesize];
  926. }
  927. //right
  928. for(x=end_x; x<block_w; x++){
  929. buf[x + y*linesize]= buf[end_x - 1 + y*linesize];
  930. }
  931. }
  932. }
  933. /* apply one mpeg motion vector to the three components */
  934. static inline void mpeg_motion(MpegEncContext *s,
  935. UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
  936. int dest_offset,
  937. UINT8 **ref_picture, int src_offset,
  938. int field_based, op_pixels_func (*pix_op)[4],
  939. int motion_x, int motion_y, int h)
  940. {
  941. UINT8 *ptr;
  942. int dxy, offset, mx, my, src_x, src_y, height, v_edge_pos, linesize, uvlinesize;
  943. int emu=0;
  944. #if 0
  945. if(s->quarter_sample)
  946. {
  947. motion_x>>=1;
  948. motion_y>>=1;
  949. }
  950. #endif
  951. dxy = ((motion_y & 1) << 1) | (motion_x & 1);
  952. src_x = s->mb_x * 16 + (motion_x >> 1);
  953. src_y = s->mb_y * (16 >> field_based) + (motion_y >> 1);
  954. /* WARNING: do no forget half pels */
  955. height = s->height >> field_based;
  956. v_edge_pos = s->v_edge_pos >> field_based;
  957. src_x = clip(src_x, -16, s->width);
  958. if (src_x == s->width)
  959. dxy &= ~1;
  960. src_y = clip(src_y, -16, height);
  961. if (src_y == height)
  962. dxy &= ~2;
  963. linesize = s->linesize << field_based;
  964. uvlinesize = s->uvlinesize << field_based;
  965. ptr = ref_picture[0] + (src_y * linesize) + (src_x) + src_offset;
  966. dest_y += dest_offset;
  967. if(s->flags&CODEC_FLAG_EMU_EDGE){
  968. if(src_x<0 || src_y<0 || src_x + (motion_x&1) + 16 > s->h_edge_pos
  969. || src_y + (motion_y&1) + h > v_edge_pos){
  970. emulated_edge_mc(s, ptr, linesize, 17, h+1, src_x, src_y, s->h_edge_pos, v_edge_pos);
  971. ptr= s->edge_emu_buffer;
  972. emu=1;
  973. }
  974. }
  975. pix_op[0][dxy](dest_y, ptr, linesize, h);
  976. if(s->flags&CODEC_FLAG_GRAY) return;
  977. if (s->out_format == FMT_H263) {
  978. dxy = 0;
  979. if ((motion_x & 3) != 0)
  980. dxy |= 1;
  981. if ((motion_y & 3) != 0)
  982. dxy |= 2;
  983. mx = motion_x >> 2;
  984. my = motion_y >> 2;
  985. } else {
  986. mx = motion_x / 2;
  987. my = motion_y / 2;
  988. dxy = ((my & 1) << 1) | (mx & 1);
  989. mx >>= 1;
  990. my >>= 1;
  991. }
  992. src_x = s->mb_x * 8 + mx;
  993. src_y = s->mb_y * (8 >> field_based) + my;
  994. src_x = clip(src_x, -8, s->width >> 1);
  995. if (src_x == (s->width >> 1))
  996. dxy &= ~1;
  997. src_y = clip(src_y, -8, height >> 1);
  998. if (src_y == (height >> 1))
  999. dxy &= ~2;
  1000. offset = (src_y * uvlinesize) + src_x + (src_offset >> 1);
  1001. ptr = ref_picture[1] + offset;
  1002. if(emu){
  1003. emulated_edge_mc(s, ptr, uvlinesize, 9, (h>>1)+1, src_x, src_y, s->h_edge_pos>>1, v_edge_pos>>1);
  1004. ptr= s->edge_emu_buffer;
  1005. }
  1006. pix_op[1][dxy](dest_cb + (dest_offset >> 1), ptr, uvlinesize, h >> 1);
  1007. ptr = ref_picture[2] + offset;
  1008. if(emu){
  1009. emulated_edge_mc(s, ptr, uvlinesize, 9, (h>>1)+1, src_x, src_y, s->h_edge_pos>>1, v_edge_pos>>1);
  1010. ptr= s->edge_emu_buffer;
  1011. }
  1012. pix_op[1][dxy](dest_cr + (dest_offset >> 1), ptr, uvlinesize, h >> 1);
  1013. }
  1014. static inline void qpel_motion(MpegEncContext *s,
  1015. UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
  1016. int dest_offset,
  1017. UINT8 **ref_picture, int src_offset,
  1018. int field_based, op_pixels_func (*pix_op)[4],
  1019. qpel_mc_func (*qpix_op)[16],
  1020. int motion_x, int motion_y, int h)
  1021. {
  1022. UINT8 *ptr;
  1023. int dxy, offset, mx, my, src_x, src_y, height, v_edge_pos, linesize, uvlinesize;
  1024. int emu=0;
  1025. dxy = ((motion_y & 3) << 2) | (motion_x & 3);
  1026. src_x = s->mb_x * 16 + (motion_x >> 2);
  1027. src_y = s->mb_y * (16 >> field_based) + (motion_y >> 2);
  1028. height = s->height >> field_based;
  1029. v_edge_pos = s->v_edge_pos >> field_based;
  1030. src_x = clip(src_x, -16, s->width);
  1031. if (src_x == s->width)
  1032. dxy &= ~3;
  1033. src_y = clip(src_y, -16, height);
  1034. if (src_y == height)
  1035. dxy &= ~12;
  1036. linesize = s->linesize << field_based;
  1037. uvlinesize = s->uvlinesize << field_based;
  1038. ptr = ref_picture[0] + (src_y * linesize) + src_x + src_offset;
  1039. dest_y += dest_offset;
  1040. //printf("%d %d %d\n", src_x, src_y, dxy);
  1041. if(s->flags&CODEC_FLAG_EMU_EDGE){
  1042. if(src_x<0 || src_y<0 || src_x + (motion_x&3) + 16 > s->h_edge_pos
  1043. || src_y + (motion_y&3) + h > v_edge_pos){
  1044. emulated_edge_mc(s, ptr, linesize, 17, h+1, src_x, src_y, s->h_edge_pos, v_edge_pos);
  1045. ptr= s->edge_emu_buffer;
  1046. emu=1;
  1047. }
  1048. }
  1049. if(!field_based)
  1050. qpix_op[0][dxy](dest_y, ptr, linesize);
  1051. else{
  1052. //damn interlaced mode
  1053. //FIXME boundary mirroring is not exactly correct here
  1054. qpix_op[1][dxy](dest_y , ptr , linesize);
  1055. qpix_op[1][dxy](dest_y+8, ptr+8, linesize);
  1056. }
  1057. if(s->flags&CODEC_FLAG_GRAY) return;
  1058. if(field_based){
  1059. mx= motion_x/2;
  1060. my= motion_y>>1;
  1061. }else if(s->divx_version){
  1062. mx= (motion_x>>1)|(motion_x&1);
  1063. my= (motion_y>>1)|(motion_y&1);
  1064. }else{
  1065. mx= motion_x/2;
  1066. my= motion_y/2;
  1067. }
  1068. mx= (mx>>1)|(mx&1);
  1069. my= (my>>1)|(my&1);
  1070. dxy= (mx&1) | ((my&1)<<1);
  1071. mx>>=1;
  1072. my>>=1;
  1073. src_x = s->mb_x * 8 + mx;
  1074. src_y = s->mb_y * (8 >> field_based) + my;
  1075. src_x = clip(src_x, -8, s->width >> 1);
  1076. if (src_x == (s->width >> 1))
  1077. dxy &= ~1;
  1078. src_y = clip(src_y, -8, height >> 1);
  1079. if (src_y == (height >> 1))
  1080. dxy &= ~2;
  1081. offset = (src_y * uvlinesize) + src_x + (src_offset >> 1);
  1082. ptr = ref_picture[1] + offset;
  1083. if(emu){
  1084. emulated_edge_mc(s, ptr, uvlinesize, 9, (h>>1)+1, src_x, src_y, s->h_edge_pos>>1, v_edge_pos>>1);
  1085. ptr= s->edge_emu_buffer;
  1086. }
  1087. pix_op[1][dxy](dest_cb + (dest_offset >> 1), ptr, uvlinesize, h >> 1);
  1088. ptr = ref_picture[2] + offset;
  1089. if(emu){
  1090. emulated_edge_mc(s, ptr, uvlinesize, 9, (h>>1)+1, src_x, src_y, s->h_edge_pos>>1, v_edge_pos>>1);
  1091. ptr= s->edge_emu_buffer;
  1092. }
  1093. pix_op[1][dxy](dest_cr + (dest_offset >> 1), ptr, uvlinesize, h >> 1);
  1094. }
  1095. static inline void MPV_motion(MpegEncContext *s,
  1096. UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
  1097. int dir, UINT8 **ref_picture,
  1098. op_pixels_func (*pix_op)[4], qpel_mc_func (*qpix_op)[16])
  1099. {
  1100. int dxy, offset, mx, my, src_x, src_y, motion_x, motion_y;
  1101. int mb_x, mb_y, i;
  1102. UINT8 *ptr, *dest;
  1103. int emu=0;
  1104. mb_x = s->mb_x;
  1105. mb_y = s->mb_y;
  1106. switch(s->mv_type) {
  1107. case MV_TYPE_16X16:
  1108. if(s->mcsel){
  1109. gmc1_motion(s, dest_y, dest_cb, dest_cr, 0,
  1110. ref_picture, 0,
  1111. 16);
  1112. }else if(s->quarter_sample){
  1113. qpel_motion(s, dest_y, dest_cb, dest_cr, 0,
  1114. ref_picture, 0,
  1115. 0, pix_op, qpix_op,
  1116. s->mv[dir][0][0], s->mv[dir][0][1], 16);
  1117. }else{
  1118. mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
  1119. ref_picture, 0,
  1120. 0, pix_op,
  1121. s->mv[dir][0][0], s->mv[dir][0][1], 16);
  1122. }
  1123. break;
  1124. case MV_TYPE_8X8:
  1125. mx = 0;
  1126. my = 0;
  1127. if(s->quarter_sample){
  1128. for(i=0;i<4;i++) {
  1129. motion_x = s->mv[dir][i][0];
  1130. motion_y = s->mv[dir][i][1];
  1131. dxy = ((motion_y & 3) << 2) | (motion_x & 3);
  1132. src_x = mb_x * 16 + (motion_x >> 2) + (i & 1) * 8;
  1133. src_y = mb_y * 16 + (motion_y >> 2) + (i >>1) * 8;
  1134. /* WARNING: do no forget half pels */
  1135. src_x = clip(src_x, -16, s->width);
  1136. if (src_x == s->width)
  1137. dxy &= ~3;
  1138. src_y = clip(src_y, -16, s->height);
  1139. if (src_y == s->height)
  1140. dxy &= ~12;
  1141. ptr = ref_picture[0] + (src_y * s->linesize) + (src_x);
  1142. if(s->flags&CODEC_FLAG_EMU_EDGE){
  1143. if(src_x<0 || src_y<0 || src_x + (motion_x&3) + 8 > s->h_edge_pos
  1144. || src_y + (motion_y&3) + 8 > s->v_edge_pos){
  1145. emulated_edge_mc(s, ptr, s->linesize, 9, 9, src_x, src_y, s->h_edge_pos, s->v_edge_pos);
  1146. ptr= s->edge_emu_buffer;
  1147. }
  1148. }
  1149. dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize;
  1150. qpix_op[1][dxy](dest, ptr, s->linesize);
  1151. mx += s->mv[dir][i][0]/2;
  1152. my += s->mv[dir][i][1]/2;
  1153. }
  1154. }else{
  1155. for(i=0;i<4;i++) {
  1156. motion_x = s->mv[dir][i][0];
  1157. motion_y = s->mv[dir][i][1];
  1158. dxy = ((motion_y & 1) << 1) | (motion_x & 1);
  1159. src_x = mb_x * 16 + (motion_x >> 1) + (i & 1) * 8;
  1160. src_y = mb_y * 16 + (motion_y >> 1) + (i >>1) * 8;
  1161. /* WARNING: do no forget half pels */
  1162. src_x = clip(src_x, -16, s->width);
  1163. if (src_x == s->width)
  1164. dxy &= ~1;
  1165. src_y = clip(src_y, -16, s->height);
  1166. if (src_y == s->height)
  1167. dxy &= ~2;
  1168. ptr = ref_picture[0] + (src_y * s->linesize) + (src_x);
  1169. if(s->flags&CODEC_FLAG_EMU_EDGE){
  1170. if(src_x<0 || src_y<0 || src_x + (motion_x&1) + 8 > s->h_edge_pos
  1171. || src_y + (motion_y&1) + 8 > s->v_edge_pos){
  1172. emulated_edge_mc(s, ptr, s->linesize, 9, 9, src_x, src_y, s->h_edge_pos, s->v_edge_pos);
  1173. ptr= s->edge_emu_buffer;
  1174. }
  1175. }
  1176. dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize;
  1177. pix_op[1][dxy](dest, ptr, s->linesize, 8);
  1178. mx += s->mv[dir][i][0];
  1179. my += s->mv[dir][i][1];
  1180. }
  1181. }
  1182. if(s->flags&CODEC_FLAG_GRAY) break;
  1183. /* In case of 8X8, we construct a single chroma motion vector
  1184. with a special rounding */
  1185. for(i=0;i<4;i++) {
  1186. }
  1187. if (mx >= 0)
  1188. mx = (h263_chroma_roundtab[mx & 0xf] + ((mx >> 3) & ~1));
  1189. else {
  1190. mx = -mx;
  1191. mx = -(h263_chroma_roundtab[mx & 0xf] + ((mx >> 3) & ~1));
  1192. }
  1193. if (my >= 0)
  1194. my = (h263_chroma_roundtab[my & 0xf] + ((my >> 3) & ~1));
  1195. else {
  1196. my = -my;
  1197. my = -(h263_chroma_roundtab[my & 0xf] + ((my >> 3) & ~1));
  1198. }
  1199. dxy = ((my & 1) << 1) | (mx & 1);
  1200. mx >>= 1;
  1201. my >>= 1;
  1202. src_x = mb_x * 8 + mx;
  1203. src_y = mb_y * 8 + my;
  1204. src_x = clip(src_x, -8, s->width/2);
  1205. if (src_x == s->width/2)
  1206. dxy &= ~1;
  1207. src_y = clip(src_y, -8, s->height/2);
  1208. if (src_y == s->height/2)
  1209. dxy &= ~2;
  1210. offset = (src_y * (s->uvlinesize)) + src_x;
  1211. ptr = ref_picture[1] + offset;
  1212. if(s->flags&CODEC_FLAG_EMU_EDGE){
  1213. if(src_x<0 || src_y<0 || src_x + (dxy &1) + 8 > s->h_edge_pos>>1
  1214. || src_y + (dxy>>1) + 8 > s->v_edge_pos>>1){
  1215. emulated_edge_mc(s, ptr, s->uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
  1216. ptr= s->edge_emu_buffer;
  1217. emu=1;
  1218. }
  1219. }
  1220. pix_op[1][dxy](dest_cb, ptr, s->uvlinesize, 8);
  1221. ptr = ref_picture[2] + offset;
  1222. if(emu){
  1223. emulated_edge_mc(s, ptr, s->uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
  1224. ptr= s->edge_emu_buffer;
  1225. }
  1226. pix_op[1][dxy](dest_cr, ptr, s->uvlinesize, 8);
  1227. break;
  1228. case MV_TYPE_FIELD:
  1229. if (s->picture_structure == PICT_FRAME) {
  1230. if(s->quarter_sample){
  1231. /* top field */
  1232. qpel_motion(s, dest_y, dest_cb, dest_cr, 0,
  1233. ref_picture, s->field_select[dir][0] ? s->linesize : 0,
  1234. 1, pix_op, qpix_op,
  1235. s->mv[dir][0][0], s->mv[dir][0][1], 8);
  1236. /* bottom field */
  1237. qpel_motion(s, dest_y, dest_cb, dest_cr, s->linesize,
  1238. ref_picture, s->field_select[dir][1] ? s->linesize : 0,
  1239. 1, pix_op, qpix_op,
  1240. s->mv[dir][1][0], s->mv[dir][1][1], 8);
  1241. }else{
  1242. /* top field */
  1243. mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
  1244. ref_picture, s->field_select[dir][0] ? s->linesize : 0,
  1245. 1, pix_op,
  1246. s->mv[dir][0][0], s->mv[dir][0][1], 8);
  1247. /* bottom field */
  1248. mpeg_motion(s, dest_y, dest_cb, dest_cr, s->linesize,
  1249. ref_picture, s->field_select[dir][1] ? s->linesize : 0,
  1250. 1, pix_op,
  1251. s->mv[dir][1][0], s->mv[dir][1][1], 8);
  1252. }
  1253. } else {
  1254. }
  1255. break;
  1256. }
  1257. }
  1258. /* put block[] to dest[] */
  1259. static inline void put_dct(MpegEncContext *s,
  1260. DCTELEM *block, int i, UINT8 *dest, int line_size)
  1261. {
  1262. if (!s->mpeg2)
  1263. s->dct_unquantize(s, block, i, s->qscale);
  1264. ff_idct_put (dest, line_size, block);
  1265. }
  1266. /* add block[] to dest[] */
  1267. static inline void add_dct(MpegEncContext *s,
  1268. DCTELEM *block, int i, UINT8 *dest, int line_size)
  1269. {
  1270. if (s->block_last_index[i] >= 0) {
  1271. ff_idct_add (dest, line_size, block);
  1272. }
  1273. }
  1274. static inline void add_dequant_dct(MpegEncContext *s,
  1275. DCTELEM *block, int i, UINT8 *dest, int line_size)
  1276. {
  1277. if (s->block_last_index[i] >= 0) {
  1278. s->dct_unquantize(s, block, i, s->qscale);
  1279. ff_idct_add (dest, line_size, block);
  1280. }
  1281. }
  1282. /**
  1283. * cleans dc, ac, coded_block for the current non intra MB
  1284. */
  1285. void ff_clean_intra_table_entries(MpegEncContext *s)
  1286. {
  1287. int wrap = s->block_wrap[0];
  1288. int xy = s->block_index[0];
  1289. s->dc_val[0][xy ] =
  1290. s->dc_val[0][xy + 1 ] =
  1291. s->dc_val[0][xy + wrap] =
  1292. s->dc_val[0][xy + 1 + wrap] = 1024;
  1293. /* ac pred */
  1294. memset(s->ac_val[0][xy ], 0, 32 * sizeof(INT16));
  1295. memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(INT16));
  1296. if (s->msmpeg4_version>=3) {
  1297. s->coded_block[xy ] =
  1298. s->coded_block[xy + 1 ] =
  1299. s->coded_block[xy + wrap] =
  1300. s->coded_block[xy + 1 + wrap] = 0;
  1301. }
  1302. /* chroma */
  1303. wrap = s->block_wrap[4];
  1304. xy = s->mb_x + 1 + (s->mb_y + 1) * wrap;
  1305. s->dc_val[1][xy] =
  1306. s->dc_val[2][xy] = 1024;
  1307. /* ac pred */
  1308. memset(s->ac_val[1][xy], 0, 16 * sizeof(INT16));
  1309. memset(s->ac_val[2][xy], 0, 16 * sizeof(INT16));
  1310. s->mbintra_table[s->mb_x + s->mb_y*s->mb_width]= 0;
  1311. }
  1312. /* generic function called after a macroblock has been parsed by the
  1313. decoder or after it has been encoded by the encoder.
  1314. Important variables used:
  1315. s->mb_intra : true if intra macroblock
  1316. s->mv_dir : motion vector direction
  1317. s->mv_type : motion vector type
  1318. s->mv : motion vector
  1319. s->interlaced_dct : true if interlaced dct used (mpeg2)
  1320. */
  1321. void MPV_decode_mb(MpegEncContext *s, DCTELEM block[6][64])
  1322. {
  1323. int mb_x, mb_y;
  1324. const int mb_xy = s->mb_y * s->mb_width + s->mb_x;
  1325. mb_x = s->mb_x;
  1326. mb_y = s->mb_y;
  1327. #ifdef FF_POSTPROCESS
  1328. /* Obsolete. Exists for compatibility with mplayer only. */
  1329. quant_store[mb_y][mb_x]=s->qscale;
  1330. //printf("[%02d][%02d] %d\n",mb_x,mb_y,s->qscale);
  1331. #else
  1332. /* even more obsolete, exists for mplayer xp only */
  1333. if(s->avctx->quant_store) s->avctx->quant_store[mb_y*s->avctx->qstride+mb_x] = s->qscale;
  1334. #endif
  1335. s->qscale_table[mb_xy]= s->qscale;
  1336. /* update DC predictors for P macroblocks */
  1337. if (!s->mb_intra) {
  1338. if (s->h263_pred || s->h263_aic) {
  1339. if(s->mbintra_table[mb_xy])
  1340. ff_clean_intra_table_entries(s);
  1341. } else {
  1342. s->last_dc[0] =
  1343. s->last_dc[1] =
  1344. s->last_dc[2] = 128 << s->intra_dc_precision;
  1345. }
  1346. }
  1347. else if (s->h263_pred || s->h263_aic)
  1348. s->mbintra_table[mb_xy]=1;
  1349. /* update motion predictor, not for B-frames as they need the motion_val from the last P/S-Frame */
  1350. if (s->out_format == FMT_H263 && s->pict_type!=B_TYPE) { //FIXME move into h263.c if possible, format specific stuff shouldnt be here
  1351. const int wrap = s->block_wrap[0];
  1352. const int xy = s->block_index[0];
  1353. const int mb_index= s->mb_x + s->mb_y*s->mb_width;
  1354. if(s->mv_type == MV_TYPE_8X8){
  1355. s->co_located_type_table[mb_index]= CO_LOCATED_TYPE_4MV;
  1356. } else {
  1357. int motion_x, motion_y;
  1358. if (s->mb_intra) {
  1359. motion_x = 0;
  1360. motion_y = 0;
  1361. if(s->co_located_type_table)
  1362. s->co_located_type_table[mb_index]= 0;
  1363. } else if (s->mv_type == MV_TYPE_16X16) {
  1364. motion_x = s->mv[0][0][0];
  1365. motion_y = s->mv[0][0][1];
  1366. if(s->co_located_type_table)
  1367. s->co_located_type_table[mb_index]= 0;
  1368. } else /*if (s->mv_type == MV_TYPE_FIELD)*/ {
  1369. int i;
  1370. motion_x = s->mv[0][0][0] + s->mv[0][1][0];
  1371. motion_y = s->mv[0][0][1] + s->mv[0][1][1];
  1372. motion_x = (motion_x>>1) | (motion_x&1);
  1373. for(i=0; i<2; i++){
  1374. s->field_mv_table[mb_index][i][0]= s->mv[0][i][0];
  1375. s->field_mv_table[mb_index][i][1]= s->mv[0][i][1];
  1376. s->field_select_table[mb_index][i]= s->field_select[0][i];
  1377. }
  1378. s->co_located_type_table[mb_index]= CO_LOCATED_TYPE_FIELDMV;
  1379. }
  1380. /* no update if 8X8 because it has been done during parsing */
  1381. s->motion_val[xy][0] = motion_x;
  1382. s->motion_val[xy][1] = motion_y;
  1383. s->motion_val[xy + 1][0] = motion_x;
  1384. s->motion_val[xy + 1][1] = motion_y;
  1385. s->motion_val[xy + wrap][0] = motion_x;
  1386. s->motion_val[xy + wrap][1] = motion_y;
  1387. s->motion_val[xy + 1 + wrap][0] = motion_x;
  1388. s->motion_val[xy + 1 + wrap][1] = motion_y;
  1389. }
  1390. }
  1391. if (!(s->encoding && (s->intra_only || s->pict_type==B_TYPE))) {
  1392. UINT8 *dest_y, *dest_cb, *dest_cr;
  1393. int dct_linesize, dct_offset;
  1394. op_pixels_func (*op_pix)[4];
  1395. qpel_mc_func (*op_qpix)[16];
  1396. /* avoid copy if macroblock skipped in last frame too
  1397. dont touch it for B-frames as they need the skip info from the next p-frame */
  1398. if (s->pict_type != B_TYPE) {
  1399. UINT8 *mbskip_ptr = &s->mbskip_table[mb_xy];
  1400. if (s->mb_skiped) {
  1401. s->mb_skiped = 0;
  1402. (*mbskip_ptr) ++; /* indicate that this time we skiped it */
  1403. if(*mbskip_ptr >99) *mbskip_ptr= 99;
  1404. /* if previous was skipped too, then nothing to do !
  1405. skip only during decoding as we might trash the buffers during encoding a bit */
  1406. if (*mbskip_ptr >= s->ip_buffer_count && !s->encoding)
  1407. goto the_end;
  1408. } else {
  1409. *mbskip_ptr = 0; /* not skipped */
  1410. }
  1411. }
  1412. dest_y = s->current_picture [0] + (mb_y * 16* s->linesize ) + mb_x * 16;
  1413. dest_cb = s->current_picture[1] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
  1414. dest_cr = s->current_picture[2] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
  1415. if (s->interlaced_dct) {
  1416. dct_linesize = s->linesize * 2;
  1417. dct_offset = s->linesize;
  1418. } else {
  1419. dct_linesize = s->linesize;
  1420. dct_offset = s->linesize * 8;
  1421. }
  1422. if (!s->mb_intra) {
  1423. /* motion handling */
  1424. /* decoding or more than one mb_type (MC was allready done otherwise) */
  1425. if((!s->encoding) || (s->mb_type[mb_xy]&(s->mb_type[mb_xy]-1))){
  1426. if ((!s->no_rounding) || s->pict_type==B_TYPE){
  1427. op_pix = put_pixels_tab;
  1428. op_qpix= put_qpel_pixels_tab;
  1429. }else{
  1430. op_pix = put_no_rnd_pixels_tab;
  1431. op_qpix= put_no_rnd_qpel_pixels_tab;
  1432. }
  1433. if (s->mv_dir & MV_DIR_FORWARD) {
  1434. MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture, op_pix, op_qpix);
  1435. op_pix = avg_pixels_tab;
  1436. op_qpix= avg_qpel_pixels_tab;
  1437. }
  1438. if (s->mv_dir & MV_DIR_BACKWARD) {
  1439. MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture, op_pix, op_qpix);
  1440. }
  1441. }
  1442. /* skip dequant / idct if we are really late ;) */
  1443. if(s->hurry_up>1) goto the_end;
  1444. /* add dct residue */
  1445. if(s->encoding || !(s->mpeg2 || s->h263_msmpeg4 || (s->codec_id==CODEC_ID_MPEG4 && !s->mpeg_quant))){
  1446. add_dequant_dct(s, block[0], 0, dest_y, dct_linesize);
  1447. add_dequant_dct(s, block[1], 1, dest_y + 8, dct_linesize);
  1448. add_dequant_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
  1449. add_dequant_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
  1450. if(!(s->flags&CODEC_FLAG_GRAY)){
  1451. add_dequant_dct(s, block[4], 4, dest_cb, s->uvlinesize);
  1452. add_dequant_dct(s, block[5], 5, dest_cr, s->uvlinesize);
  1453. }
  1454. } else {
  1455. add_dct(s, block[0], 0, dest_y, dct_linesize);
  1456. add_dct(s, block[1], 1, dest_y + 8, dct_linesize);
  1457. add_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
  1458. add_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
  1459. if(!(s->flags&CODEC_FLAG_GRAY)){
  1460. add_dct(s, block[4], 4, dest_cb, s->uvlinesize);
  1461. add_dct(s, block[5], 5, dest_cr, s->uvlinesize);
  1462. }
  1463. }
  1464. } else {
  1465. /* dct only in intra block */
  1466. put_dct(s, block[0], 0, dest_y, dct_linesize);
  1467. put_dct(s, block[1], 1, dest_y + 8, dct_linesize);
  1468. put_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
  1469. put_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
  1470. if(!(s->flags&CODEC_FLAG_GRAY)){
  1471. put_dct(s, block[4], 4, dest_cb, s->uvlinesize);
  1472. put_dct(s, block[5], 5, dest_cr, s->uvlinesize);
  1473. }
  1474. }
  1475. }
  1476. the_end:
  1477. emms_c(); //FIXME remove
  1478. }
  1479. static inline void dct_single_coeff_elimination(MpegEncContext *s, int n, int threshold)
  1480. {
  1481. static const char tab[64]=
  1482. {3,2,2,1,1,1,1,1,
  1483. 1,1,1,1,1,1,1,1,
  1484. 1,1,1,1,1,1,1,1,
  1485. 0,0,0,0,0,0,0,0,
  1486. 0,0,0,0,0,0,0,0,
  1487. 0,0,0,0,0,0,0,0,
  1488. 0,0,0,0,0,0,0,0,
  1489. 0,0,0,0,0,0,0,0};
  1490. int score=0;
  1491. int run=0;
  1492. int i;
  1493. DCTELEM *block= s->block[n];
  1494. const int last_index= s->block_last_index[n];
  1495. int skip_dc;
  1496. if(threshold<0){
  1497. skip_dc=0;
  1498. threshold= -threshold;
  1499. }else
  1500. skip_dc=1;
  1501. /* are all which we could set to zero are allready zero? */
  1502. if(last_index<=skip_dc - 1) return;
  1503. for(i=0; i<=last_index; i++){
  1504. const int j = zigzag_direct[i];
  1505. const int level = ABS(block[j]);
  1506. if(level==1){
  1507. if(skip_dc && i==0) continue;
  1508. score+= tab[run];
  1509. run=0;
  1510. }else if(level>1){
  1511. return;
  1512. }else{
  1513. run++;
  1514. }
  1515. }
  1516. if(score >= threshold) return;
  1517. for(i=skip_dc; i<=last_index; i++){
  1518. const int j = zigzag_direct[i];
  1519. block[j]=0;
  1520. }
  1521. if(block[0]) s->block_last_index[n]= 0;
  1522. else s->block_last_index[n]= -1;
  1523. }
  1524. static inline void clip_coeffs(MpegEncContext *s, DCTELEM *block, int last_index)
  1525. {
  1526. int i;
  1527. const int maxlevel= s->max_qcoeff;
  1528. const int minlevel= s->min_qcoeff;
  1529. for(i=0;i<=last_index; i++){
  1530. const int j = zigzag_direct[i];
  1531. int level = block[j];
  1532. if (level>maxlevel) level=maxlevel;
  1533. else if(level<minlevel) level=minlevel;
  1534. block[j]= level;
  1535. }
  1536. }
  1537. static inline void requantize_coeffs(MpegEncContext *s, DCTELEM block[64], int oldq, int newq, int n)
  1538. {
  1539. int i;
  1540. if(s->mb_intra){
  1541. //FIXME requantize, note (mpeg1/h263/h263p-aic dont need it,...)
  1542. i=1;
  1543. }else
  1544. i=0;
  1545. for(;i<=s->block_last_index[n]; i++){
  1546. const int j = zigzag_direct[i];
  1547. int level = block[j];
  1548. block[j]= ROUNDED_DIV(level*oldq, newq);
  1549. }
  1550. for(i=s->block_last_index[n]; i>=0; i--){
  1551. const int j = zigzag_direct[i]; //FIXME other scantabs
  1552. if(block[j]) break;
  1553. }
  1554. s->block_last_index[n]= i;
  1555. }
  1556. static inline void auto_requantize_coeffs(MpegEncContext *s, DCTELEM block[6][64])
  1557. {
  1558. int i,n, newq;
  1559. const int maxlevel= s->max_qcoeff;
  1560. const int minlevel= s->min_qcoeff;
  1561. int largest=0, smallest=0;
  1562. assert(s->adaptive_quant);
  1563. for(n=0; n<6; n++){
  1564. if(s->mb_intra) i=1;
  1565. else i=0;
  1566. for(;i<=s->block_last_index[n]; i++){
  1567. const int j = zigzag_direct[i]; //FIXME other scantabs
  1568. int level = block[n][j];
  1569. if(largest < level) largest = level;
  1570. if(smallest > level) smallest= level;
  1571. }
  1572. }
  1573. for(newq=s->qscale+1; newq<32; newq++){
  1574. if( ROUNDED_DIV(smallest*s->qscale, newq) >= minlevel
  1575. && ROUNDED_DIV(largest *s->qscale, newq) <= maxlevel)
  1576. break;
  1577. }
  1578. if(s->out_format==FMT_H263){
  1579. /* h263 like formats cannot change qscale by more than 2 easiely */
  1580. if(s->avctx->qmin + 2 < newq)
  1581. newq= s->avctx->qmin + 2;
  1582. }
  1583. for(n=0; n<6; n++){
  1584. requantize_coeffs(s, block[n], s->qscale, newq, n);
  1585. clip_coeffs(s, block[n], s->block_last_index[n]);
  1586. }
  1587. s->dquant+= newq - s->qscale;
  1588. s->qscale= newq;
  1589. }
  1590. #if 0
  1591. static int pix_vcmp16x8(UINT8 *s, int stride){ //FIXME move to dsputil & optimize
  1592. int score=0;
  1593. int x,y;
  1594. for(y=0; y<7; y++){
  1595. for(x=0; x<16; x+=4){
  1596. score+= ABS(s[x ] - s[x +stride]) + ABS(s[x+1] - s[x+1+stride])
  1597. +ABS(s[x+2] - s[x+2+stride]) + ABS(s[x+3] - s[x+3+stride]);
  1598. }
  1599. s+= stride;
  1600. }
  1601. return score;
  1602. }
  1603. static int pix_diff_vcmp16x8(UINT8 *s1, UINT8*s2, int stride){ //FIXME move to dsputil & optimize
  1604. int score=0;
  1605. int x,y;
  1606. for(y=0; y<7; y++){
  1607. for(x=0; x<16; x++){
  1608. score+= ABS(s1[x ] - s2[x ] - s1[x +stride] + s2[x +stride]);
  1609. }
  1610. s1+= stride;
  1611. s2+= stride;
  1612. }
  1613. return score;
  1614. }
  1615. #else
  1616. #define SQ(a) ((a)*(a))
  1617. static int pix_vcmp16x8(UINT8 *s, int stride){ //FIXME move to dsputil & optimize
  1618. int score=0;
  1619. int x,y;
  1620. for(y=0; y<7; y++){
  1621. for(x=0; x<16; x+=4){
  1622. score+= SQ(s[x ] - s[x +stride]) + SQ(s[x+1] - s[x+1+stride])
  1623. +SQ(s[x+2] - s[x+2+stride]) + SQ(s[x+3] - s[x+3+stride]);
  1624. }
  1625. s+= stride;
  1626. }
  1627. return score;
  1628. }
  1629. static int pix_diff_vcmp16x8(UINT8 *s1, UINT8*s2, int stride){ //FIXME move to dsputil & optimize
  1630. int score=0;
  1631. int x,y;
  1632. for(y=0; y<7; y++){
  1633. for(x=0; x<16; x++){
  1634. score+= SQ(s1[x ] - s2[x ] - s1[x +stride] + s2[x +stride]);
  1635. }
  1636. s1+= stride;
  1637. s2+= stride;
  1638. }
  1639. return score;
  1640. }
  1641. #endif
  1642. static void encode_mb(MpegEncContext *s, int motion_x, int motion_y)
  1643. {
  1644. const int mb_x= s->mb_x;
  1645. const int mb_y= s->mb_y;
  1646. int i;
  1647. int skip_dct[6];
  1648. int dct_offset = s->linesize*8; //default for progressive frames
  1649. for(i=0; i<6; i++) skip_dct[i]=0;
  1650. if(s->adaptive_quant){
  1651. s->dquant= s->qscale_table[mb_x + mb_y*s->mb_width] - s->qscale;
  1652. if(s->out_format==FMT_H263){
  1653. if (s->dquant> 2) s->dquant= 2;
  1654. else if(s->dquant<-2) s->dquant=-2;
  1655. }
  1656. if(s->codec_id==CODEC_ID_MPEG4){
  1657. if(!s->mb_intra){
  1658. assert(s->dquant==0 || s->mv_type!=MV_TYPE_8X8);
  1659. if(s->mv_dir&MV_DIRECT)
  1660. s->dquant=0;
  1661. }
  1662. }
  1663. s->qscale+= s->dquant;
  1664. s->y_dc_scale= s->y_dc_scale_table[ s->qscale ];
  1665. s->c_dc_scale= s->c_dc_scale_table[ s->qscale ];
  1666. }
  1667. if (s->mb_intra) {
  1668. UINT8 *ptr;
  1669. int wrap_y;
  1670. int emu=0;
  1671. wrap_y = s->linesize;
  1672. ptr = s->new_picture[0] + (mb_y * 16 * wrap_y) + mb_x * 16;
  1673. if(mb_x*16+16 > s->width || mb_y*16+16 > s->height){
  1674. emulated_edge_mc(s, ptr, wrap_y, 16, 16, mb_x*16, mb_y*16, s->width, s->height);
  1675. ptr= s->edge_emu_buffer;
  1676. emu=1;
  1677. }
  1678. if(s->flags&CODEC_FLAG_INTERLACED_DCT){
  1679. int progressive_score, interlaced_score;
  1680. progressive_score= pix_vcmp16x8(ptr, wrap_y ) + pix_vcmp16x8(ptr + wrap_y*8, wrap_y );
  1681. interlaced_score = pix_vcmp16x8(ptr, wrap_y*2) + pix_vcmp16x8(ptr + wrap_y , wrap_y*2);
  1682. if(progressive_score > interlaced_score + 100){
  1683. s->interlaced_dct=1;
  1684. dct_offset= wrap_y;
  1685. wrap_y<<=1;
  1686. }else
  1687. s->interlaced_dct=0;
  1688. }
  1689. get_pixels(s->block[0], ptr , wrap_y);
  1690. get_pixels(s->block[1], ptr + 8, wrap_y);
  1691. get_pixels(s->block[2], ptr + dct_offset , wrap_y);
  1692. get_pixels(s->block[3], ptr + dct_offset + 8, wrap_y);
  1693. if(s->flags&CODEC_FLAG_GRAY){
  1694. skip_dct[4]= 1;
  1695. skip_dct[5]= 1;
  1696. }else{
  1697. int wrap_c = s->uvlinesize;
  1698. ptr = s->new_picture[1] + (mb_y * 8 * wrap_c) + mb_x * 8;
  1699. if(emu){
  1700. emulated_edge_mc(s, ptr, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
  1701. ptr= s->edge_emu_buffer;
  1702. }
  1703. get_pixels(s->block[4], ptr, wrap_c);
  1704. ptr = s->new_picture[2] + (mb_y * 8 * wrap_c) + mb_x * 8;
  1705. if(emu){
  1706. emulated_edge_mc(s, ptr, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
  1707. ptr= s->edge_emu_buffer;
  1708. }
  1709. get_pixels(s->block[5], ptr, wrap_c);
  1710. }
  1711. }else{
  1712. op_pixels_func (*op_pix)[4];
  1713. qpel_mc_func (*op_qpix)[16];
  1714. UINT8 *dest_y, *dest_cb, *dest_cr;
  1715. UINT8 *ptr_y, *ptr_cb, *ptr_cr;
  1716. int wrap_y, wrap_c;
  1717. int emu=0;
  1718. dest_y = s->current_picture[0] + (mb_y * 16 * s->linesize ) + mb_x * 16;
  1719. dest_cb = s->current_picture[1] + (mb_y * 8 * (s->uvlinesize)) + mb_x * 8;
  1720. dest_cr = s->current_picture[2] + (mb_y * 8 * (s->uvlinesize)) + mb_x * 8;
  1721. wrap_y = s->linesize;
  1722. wrap_c = s->uvlinesize;
  1723. ptr_y = s->new_picture[0] + (mb_y * 16 * wrap_y) + mb_x * 16;
  1724. ptr_cb = s->new_picture[1] + (mb_y * 8 * wrap_c) + mb_x * 8;
  1725. ptr_cr = s->new_picture[2] + (mb_y * 8 * wrap_c) + mb_x * 8;
  1726. if ((!s->no_rounding) || s->pict_type==B_TYPE){
  1727. op_pix = put_pixels_tab;
  1728. op_qpix= put_qpel_pixels_tab;
  1729. }else{
  1730. op_pix = put_no_rnd_pixels_tab;
  1731. op_qpix= put_no_rnd_qpel_pixels_tab;
  1732. }
  1733. if (s->mv_dir & MV_DIR_FORWARD) {
  1734. MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture, op_pix, op_qpix);
  1735. op_pix = avg_pixels_tab;
  1736. op_qpix= avg_qpel_pixels_tab;
  1737. }
  1738. if (s->mv_dir & MV_DIR_BACKWARD) {
  1739. MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture, op_pix, op_qpix);
  1740. }
  1741. if(mb_x*16+16 > s->width || mb_y*16+16 > s->height){
  1742. emulated_edge_mc(s, ptr_y, wrap_y, 16, 16, mb_x*16, mb_y*16, s->width, s->height);
  1743. ptr_y= s->edge_emu_buffer;
  1744. emu=1;
  1745. }
  1746. if(s->flags&CODEC_FLAG_INTERLACED_DCT){
  1747. int progressive_score, interlaced_score;
  1748. progressive_score= pix_diff_vcmp16x8(ptr_y , dest_y , wrap_y )
  1749. + pix_diff_vcmp16x8(ptr_y + wrap_y*8, dest_y + wrap_y*8, wrap_y );
  1750. interlaced_score = pix_diff_vcmp16x8(ptr_y , dest_y , wrap_y*2)
  1751. + pix_diff_vcmp16x8(ptr_y + wrap_y , dest_y + wrap_y , wrap_y*2);
  1752. if(progressive_score > interlaced_score + 600){
  1753. s->interlaced_dct=1;
  1754. dct_offset= wrap_y;
  1755. wrap_y<<=1;
  1756. }else
  1757. s->interlaced_dct=0;
  1758. }
  1759. diff_pixels(s->block[0], ptr_y , dest_y , wrap_y);
  1760. diff_pixels(s->block[1], ptr_y + 8, dest_y + 8, wrap_y);
  1761. diff_pixels(s->block[2], ptr_y + dct_offset , dest_y + dct_offset , wrap_y);
  1762. diff_pixels(s->block[3], ptr_y + dct_offset + 8, dest_y + dct_offset + 8, wrap_y);
  1763. if(s->flags&CODEC_FLAG_GRAY){
  1764. skip_dct[4]= 1;
  1765. skip_dct[5]= 1;
  1766. }else{
  1767. if(emu){
  1768. emulated_edge_mc(s, ptr_cb, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
  1769. ptr_cb= s->edge_emu_buffer;
  1770. }
  1771. diff_pixels(s->block[4], ptr_cb, dest_cb, wrap_c);
  1772. if(emu){
  1773. emulated_edge_mc(s, ptr_cr, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
  1774. ptr_cr= s->edge_emu_buffer;
  1775. }
  1776. diff_pixels(s->block[5], ptr_cr, dest_cr, wrap_c);
  1777. }
  1778. /* pre quantization */
  1779. if(s->mc_mb_var[s->mb_width*mb_y+ mb_x]<2*s->qscale*s->qscale){
  1780. //FIXME optimize
  1781. if(pix_abs8x8(ptr_y , dest_y , wrap_y) < 20*s->qscale) skip_dct[0]= 1;
  1782. if(pix_abs8x8(ptr_y + 8, dest_y + 8, wrap_y) < 20*s->qscale) skip_dct[1]= 1;
  1783. if(pix_abs8x8(ptr_y +dct_offset , dest_y +dct_offset , wrap_y) < 20*s->qscale) skip_dct[2]= 1;
  1784. if(pix_abs8x8(ptr_y +dct_offset+ 8, dest_y +dct_offset+ 8, wrap_y) < 20*s->qscale) skip_dct[3]= 1;
  1785. if(pix_abs8x8(ptr_cb , dest_cb , wrap_y) < 20*s->qscale) skip_dct[4]= 1;
  1786. if(pix_abs8x8(ptr_cr , dest_cr , wrap_y) < 20*s->qscale) skip_dct[5]= 1;
  1787. #if 0
  1788. {
  1789. static int stat[7];
  1790. int num=0;
  1791. for(i=0; i<6; i++)
  1792. if(skip_dct[i]) num++;
  1793. stat[num]++;
  1794. if(s->mb_x==0 && s->mb_y==0){
  1795. for(i=0; i<7; i++){
  1796. printf("%6d %1d\n", stat[i], i);
  1797. }
  1798. }
  1799. }
  1800. #endif
  1801. }
  1802. }
  1803. #if 0
  1804. {
  1805. float adap_parm;
  1806. adap_parm = ((s->avg_mb_var << 1) + s->mb_var[s->mb_width*mb_y+mb_x] + 1.0) /
  1807. ((s->mb_var[s->mb_width*mb_y+mb_x] << 1) + s->avg_mb_var + 1.0);
  1808. printf("\ntype=%c qscale=%2d adap=%0.2f dquant=%4.2f var=%4d avgvar=%4d",
  1809. (s->mb_type[s->mb_width*mb_y+mb_x] > 0) ? 'I' : 'P',
  1810. s->qscale, adap_parm, s->qscale*adap_parm,
  1811. s->mb_var[s->mb_width*mb_y+mb_x], s->avg_mb_var);
  1812. }
  1813. #endif
  1814. /* DCT & quantize */
  1815. if(s->out_format==FMT_MJPEG){
  1816. for(i=0;i<6;i++) {
  1817. int overflow;
  1818. s->block_last_index[i] = s->dct_quantize(s, s->block[i], i, 8, &overflow);
  1819. if (overflow) clip_coeffs(s, s->block[i], s->block_last_index[i]);
  1820. }
  1821. }else{
  1822. for(i=0;i<6;i++) {
  1823. if(!skip_dct[i]){
  1824. int overflow;
  1825. s->block_last_index[i] = s->dct_quantize(s, s->block[i], i, s->qscale, &overflow);
  1826. // FIXME we could decide to change to quantizer instead of clipping
  1827. // JS: I don't think that would be a good idea it could lower quality instead
  1828. // of improve it. Just INTRADC clipping deserves changes in quantizer
  1829. if (overflow) clip_coeffs(s, s->block[i], s->block_last_index[i]);
  1830. }else
  1831. s->block_last_index[i]= -1;
  1832. }
  1833. if(s->luma_elim_threshold && !s->mb_intra)
  1834. for(i=0; i<4; i++)
  1835. dct_single_coeff_elimination(s, i, s->luma_elim_threshold);
  1836. if(s->chroma_elim_threshold && !s->mb_intra)
  1837. for(i=4; i<6; i++)
  1838. dct_single_coeff_elimination(s, i, s->chroma_elim_threshold);
  1839. }
  1840. if((s->flags&CODEC_FLAG_GRAY) && s->mb_intra){
  1841. s->block_last_index[4]=
  1842. s->block_last_index[5]= 0;
  1843. s->block[4][0]=
  1844. s->block[5][0]= 128;
  1845. }
  1846. /* huffman encode */
  1847. switch(s->out_format) {
  1848. case FMT_MPEG1:
  1849. mpeg1_encode_mb(s, s->block, motion_x, motion_y);
  1850. break;
  1851. case FMT_H263:
  1852. if (s->h263_msmpeg4)
  1853. msmpeg4_encode_mb(s, s->block, motion_x, motion_y);
  1854. else if(s->h263_pred)
  1855. mpeg4_encode_mb(s, s->block, motion_x, motion_y);
  1856. else
  1857. h263_encode_mb(s, s->block, motion_x, motion_y);
  1858. break;
  1859. case FMT_MJPEG:
  1860. mjpeg_encode_mb(s, s->block);
  1861. break;
  1862. }
  1863. }
  1864. void ff_copy_bits(PutBitContext *pb, UINT8 *src, int length)
  1865. {
  1866. int bytes= length>>4;
  1867. int bits= length&15;
  1868. int i;
  1869. if(length==0) return;
  1870. for(i=0; i<bytes; i++) put_bits(pb, 16, be2me_16(((uint16_t*)src)[i]));
  1871. put_bits(pb, bits, be2me_16(((uint16_t*)src)[i])>>(16-bits));
  1872. }
  1873. static inline void copy_context_before_encode(MpegEncContext *d, MpegEncContext *s, int type){
  1874. int i;
  1875. memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster then a loop?
  1876. /* mpeg1 */
  1877. d->mb_incr= s->mb_incr;
  1878. for(i=0; i<3; i++)
  1879. d->last_dc[i]= s->last_dc[i];
  1880. /* statistics */
  1881. d->mv_bits= s->mv_bits;
  1882. d->i_tex_bits= s->i_tex_bits;
  1883. d->p_tex_bits= s->p_tex_bits;
  1884. d->i_count= s->i_count;
  1885. d->f_count= s->f_count;
  1886. d->b_count= s->b_count;
  1887. d->skip_count= s->skip_count;
  1888. d->misc_bits= s->misc_bits;
  1889. d->last_bits= 0;
  1890. d->mb_skiped= s->mb_skiped;
  1891. }
  1892. static inline void copy_context_after_encode(MpegEncContext *d, MpegEncContext *s, int type){
  1893. int i;
  1894. memcpy(d->mv, s->mv, 2*4*2*sizeof(int));
  1895. memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster then a loop?
  1896. /* mpeg1 */
  1897. d->mb_incr= s->mb_incr;
  1898. for(i=0; i<3; i++)
  1899. d->last_dc[i]= s->last_dc[i];
  1900. /* statistics */
  1901. d->mv_bits= s->mv_bits;
  1902. d->i_tex_bits= s->i_tex_bits;
  1903. d->p_tex_bits= s->p_tex_bits;
  1904. d->i_count= s->i_count;
  1905. d->f_count= s->f_count;
  1906. d->b_count= s->b_count;
  1907. d->skip_count= s->skip_count;
  1908. d->misc_bits= s->misc_bits;
  1909. d->mb_intra= s->mb_intra;
  1910. d->mb_skiped= s->mb_skiped;
  1911. d->mv_type= s->mv_type;
  1912. d->mv_dir= s->mv_dir;
  1913. d->pb= s->pb;
  1914. if(s->data_partitioning){
  1915. d->pb2= s->pb2;
  1916. d->tex_pb= s->tex_pb;
  1917. }
  1918. d->block= s->block;
  1919. for(i=0; i<6; i++)
  1920. d->block_last_index[i]= s->block_last_index[i];
  1921. }
  1922. static inline void encode_mb_hq(MpegEncContext *s, MpegEncContext *backup, MpegEncContext *best, int type,
  1923. PutBitContext pb[2], PutBitContext pb2[2], PutBitContext tex_pb[2],
  1924. int *dmin, int *next_block, int motion_x, int motion_y)
  1925. {
  1926. int bits_count;
  1927. copy_context_before_encode(s, backup, type);
  1928. s->block= s->blocks[*next_block];
  1929. s->pb= pb[*next_block];
  1930. if(s->data_partitioning){
  1931. s->pb2 = pb2 [*next_block];
  1932. s->tex_pb= tex_pb[*next_block];
  1933. }
  1934. encode_mb(s, motion_x, motion_y);
  1935. bits_count= get_bit_count(&s->pb);
  1936. if(s->data_partitioning){
  1937. bits_count+= get_bit_count(&s->pb2);
  1938. bits_count+= get_bit_count(&s->tex_pb);
  1939. }
  1940. if(bits_count<*dmin){
  1941. *dmin= bits_count;
  1942. *next_block^=1;
  1943. copy_context_after_encode(best, s, type);
  1944. }
  1945. }
  1946. static void encode_picture(MpegEncContext *s, int picture_number)
  1947. {
  1948. int mb_x, mb_y, last_gob, pdif = 0;
  1949. int i;
  1950. int bits;
  1951. MpegEncContext best_s, backup_s;
  1952. UINT8 bit_buf[2][3000];
  1953. UINT8 bit_buf2[2][3000];
  1954. UINT8 bit_buf_tex[2][3000];
  1955. PutBitContext pb[2], pb2[2], tex_pb[2];
  1956. for(i=0; i<2; i++){
  1957. init_put_bits(&pb [i], bit_buf [i], 3000, NULL, NULL);
  1958. init_put_bits(&pb2 [i], bit_buf2 [i], 3000, NULL, NULL);
  1959. init_put_bits(&tex_pb[i], bit_buf_tex[i], 3000, NULL, NULL);
  1960. }
  1961. s->picture_number = picture_number;
  1962. s->block_wrap[0]=
  1963. s->block_wrap[1]=
  1964. s->block_wrap[2]=
  1965. s->block_wrap[3]= s->mb_width*2 + 2;
  1966. s->block_wrap[4]=
  1967. s->block_wrap[5]= s->mb_width + 2;
  1968. /* Reset the average MB variance */
  1969. s->mb_var_sum = 0;
  1970. s->mc_mb_var_sum = 0;
  1971. /* we need to initialize some time vars before we can encode b-frames */
  1972. if (s->h263_pred && !s->h263_msmpeg4)
  1973. ff_set_mpeg4_time(s, s->picture_number);
  1974. s->scene_change_score=0;
  1975. s->qscale= (int)(s->frame_qscale + 0.5); //FIXME qscale / ... stuff for ME ratedistoration
  1976. /* Estimate motion for every MB */
  1977. if(s->pict_type != I_TYPE){
  1978. for(mb_y=0; mb_y < s->mb_height; mb_y++) {
  1979. s->block_index[0]= s->block_wrap[0]*(mb_y*2 + 1) - 1;
  1980. s->block_index[1]= s->block_wrap[0]*(mb_y*2 + 1);
  1981. s->block_index[2]= s->block_wrap[0]*(mb_y*2 + 2) - 1;
  1982. s->block_index[3]= s->block_wrap[0]*(mb_y*2 + 2);
  1983. for(mb_x=0; mb_x < s->mb_width; mb_x++) {
  1984. s->mb_x = mb_x;
  1985. s->mb_y = mb_y;
  1986. s->block_index[0]+=2;
  1987. s->block_index[1]+=2;
  1988. s->block_index[2]+=2;
  1989. s->block_index[3]+=2;
  1990. /* compute motion vector & mb_type and store in context */
  1991. if(s->pict_type==B_TYPE)
  1992. ff_estimate_b_frame_motion(s, mb_x, mb_y);
  1993. else
  1994. ff_estimate_p_frame_motion(s, mb_x, mb_y);
  1995. // s->mb_type[mb_y*s->mb_width + mb_x]=MB_TYPE_INTER;
  1996. }
  1997. }
  1998. emms_c();
  1999. }else /* if(s->pict_type == I_TYPE) */{
  2000. /* I-Frame */
  2001. //FIXME do we need to zero them?
  2002. memset(s->motion_val[0], 0, sizeof(INT16)*(s->mb_width*2 + 2)*(s->mb_height*2 + 2)*2);
  2003. memset(s->p_mv_table , 0, sizeof(INT16)*(s->mb_width+2)*(s->mb_height+2)*2);
  2004. memset(s->mb_type , MB_TYPE_INTRA, sizeof(UINT8)*s->mb_width*s->mb_height);
  2005. if(!s->fixed_qscale){
  2006. /* finding spatial complexity for I-frame rate control */
  2007. for(mb_y=0; mb_y < s->mb_height; mb_y++) {
  2008. for(mb_x=0; mb_x < s->mb_width; mb_x++) {
  2009. int xx = mb_x * 16;
  2010. int yy = mb_y * 16;
  2011. uint8_t *pix = s->new_picture[0] + (yy * s->linesize) + xx;
  2012. int varc;
  2013. int sum = pix_sum(pix, s->linesize);
  2014. sum= (sum+8)>>4;
  2015. varc = (pix_norm1(pix, s->linesize) - sum*sum + 500 + 128)>>8;
  2016. s->mb_var [s->mb_width * mb_y + mb_x] = varc;
  2017. s->mb_mean[s->mb_width * mb_y + mb_x] = (sum+7)>>4;
  2018. s->mb_var_sum += varc;
  2019. }
  2020. }
  2021. }
  2022. }
  2023. if(s->scene_change_score > 0 && s->pict_type == P_TYPE){
  2024. s->pict_type= I_TYPE;
  2025. memset(s->mb_type , MB_TYPE_INTRA, sizeof(UINT8)*s->mb_width*s->mb_height);
  2026. if(s->max_b_frames==0){
  2027. s->input_pict_type= I_TYPE;
  2028. s->input_picture_in_gop_number=0;
  2029. }
  2030. //printf("Scene change detected, encoding as I Frame %d %d\n", s->mb_var_sum, s->mc_mb_var_sum);
  2031. }
  2032. if(s->pict_type==P_TYPE || s->pict_type==S_TYPE)
  2033. s->f_code= ff_get_best_fcode(s, s->p_mv_table, MB_TYPE_INTER);
  2034. ff_fix_long_p_mvs(s);
  2035. if(s->pict_type==B_TYPE){
  2036. s->f_code= ff_get_best_fcode(s, s->b_forw_mv_table, MB_TYPE_FORWARD);
  2037. s->b_code= ff_get_best_fcode(s, s->b_back_mv_table, MB_TYPE_BACKWARD);
  2038. ff_fix_long_b_mvs(s, s->b_forw_mv_table, s->f_code, MB_TYPE_FORWARD);
  2039. ff_fix_long_b_mvs(s, s->b_back_mv_table, s->b_code, MB_TYPE_BACKWARD);
  2040. ff_fix_long_b_mvs(s, s->b_bidir_forw_mv_table, s->f_code, MB_TYPE_BIDIR);
  2041. ff_fix_long_b_mvs(s, s->b_bidir_back_mv_table, s->b_code, MB_TYPE_BIDIR);
  2042. }
  2043. if (s->fixed_qscale)
  2044. s->frame_qscale = s->avctx->quality;
  2045. else
  2046. s->frame_qscale = ff_rate_estimate_qscale(s);
  2047. if(s->adaptive_quant){
  2048. switch(s->codec_id){
  2049. case CODEC_ID_MPEG4:
  2050. ff_clean_mpeg4_qscales(s);
  2051. break;
  2052. case CODEC_ID_H263:
  2053. case CODEC_ID_H263P:
  2054. ff_clean_h263_qscales(s);
  2055. break;
  2056. }
  2057. s->qscale= s->qscale_table[0];
  2058. }else
  2059. s->qscale= (int)(s->frame_qscale + 0.5);
  2060. if (s->out_format == FMT_MJPEG) {
  2061. /* for mjpeg, we do include qscale in the matrix */
  2062. s->intra_matrix[0] = ff_mpeg1_default_intra_matrix[0];
  2063. for(i=1;i<64;i++)
  2064. s->intra_matrix[i] = CLAMP_TO_8BIT((ff_mpeg1_default_intra_matrix[i] * s->qscale) >> 3);
  2065. convert_matrix(s, s->q_intra_matrix, s->q_intra_matrix16,
  2066. s->q_intra_matrix16_bias, s->intra_matrix, s->intra_quant_bias);
  2067. }
  2068. s->last_bits= get_bit_count(&s->pb);
  2069. switch(s->out_format) {
  2070. case FMT_MJPEG:
  2071. mjpeg_picture_header(s);
  2072. break;
  2073. case FMT_H263:
  2074. if (s->h263_msmpeg4)
  2075. msmpeg4_encode_picture_header(s, picture_number);
  2076. else if (s->h263_pred)
  2077. mpeg4_encode_picture_header(s, picture_number);
  2078. else if (s->h263_rv10)
  2079. rv10_encode_picture_header(s, picture_number);
  2080. else
  2081. h263_encode_picture_header(s, picture_number);
  2082. break;
  2083. case FMT_MPEG1:
  2084. mpeg1_encode_picture_header(s, picture_number);
  2085. break;
  2086. }
  2087. bits= get_bit_count(&s->pb);
  2088. s->header_bits= bits - s->last_bits;
  2089. s->last_bits= bits;
  2090. s->mv_bits=0;
  2091. s->misc_bits=0;
  2092. s->i_tex_bits=0;
  2093. s->p_tex_bits=0;
  2094. s->i_count=0;
  2095. s->f_count=0;
  2096. s->b_count=0;
  2097. s->skip_count=0;
  2098. /* init last dc values */
  2099. /* note: quant matrix value (8) is implied here */
  2100. s->last_dc[0] = 128;
  2101. s->last_dc[1] = 128;
  2102. s->last_dc[2] = 128;
  2103. s->mb_incr = 1;
  2104. s->last_mv[0][0][0] = 0;
  2105. s->last_mv[0][0][1] = 0;
  2106. /* Get the GOB height based on picture height */
  2107. if (s->out_format == FMT_H263 && !s->h263_pred && !s->h263_msmpeg4) {
  2108. if (s->height <= 400)
  2109. s->gob_index = 1;
  2110. else if (s->height <= 800)
  2111. s->gob_index = 2;
  2112. else
  2113. s->gob_index = 4;
  2114. }else if(s->codec_id==CODEC_ID_MPEG4){
  2115. s->gob_index = 1;
  2116. }
  2117. if(s->codec_id==CODEC_ID_MPEG4 && s->data_partitioning && s->pict_type!=B_TYPE)
  2118. ff_mpeg4_init_partitions(s);
  2119. s->resync_mb_x=0;
  2120. s->resync_mb_y=0;
  2121. for(mb_y=0; mb_y < s->mb_height; mb_y++) {
  2122. /* Put GOB header based on RTP MTU for formats which support it per line (H263*)*/
  2123. /* TODO: Put all this stuff in a separate generic function */
  2124. if (s->rtp_mode) {
  2125. if (!mb_y) {
  2126. s->ptr_lastgob = s->pb.buf;
  2127. s->ptr_last_mb_line = s->pb.buf;
  2128. } else if (s->out_format == FMT_H263 && !s->h263_pred && !s->h263_msmpeg4 && !(mb_y % s->gob_index)) {
  2129. // MN: we could move the space check from h263 -> here, as its not h263 specific
  2130. last_gob = h263_encode_gob_header(s, mb_y);
  2131. if (last_gob) {
  2132. s->first_slice_line = 1;
  2133. }else{
  2134. /*MN: we reset it here instead at the end of each line cuz mpeg4 can have
  2135. slice lines starting & ending in the middle*/
  2136. s->first_slice_line = 0;
  2137. }
  2138. }
  2139. }
  2140. s->y_dc_scale= s->y_dc_scale_table[ s->qscale ];
  2141. s->c_dc_scale= s->c_dc_scale_table[ s->qscale ];
  2142. s->block_index[0]= s->block_wrap[0]*(mb_y*2 + 1) - 1;
  2143. s->block_index[1]= s->block_wrap[0]*(mb_y*2 + 1);
  2144. s->block_index[2]= s->block_wrap[0]*(mb_y*2 + 2) - 1;
  2145. s->block_index[3]= s->block_wrap[0]*(mb_y*2 + 2);
  2146. s->block_index[4]= s->block_wrap[4]*(mb_y + 1) + s->block_wrap[0]*(s->mb_height*2 + 2);
  2147. s->block_index[5]= s->block_wrap[4]*(mb_y + 1 + s->mb_height + 2) + s->block_wrap[0]*(s->mb_height*2 + 2);
  2148. for(mb_x=0; mb_x < s->mb_width; mb_x++) {
  2149. const int mb_type= s->mb_type[mb_y * s->mb_width + mb_x];
  2150. const int xy= (mb_y+1) * (s->mb_width+2) + mb_x + 1;
  2151. // int d;
  2152. int dmin=10000000;
  2153. s->mb_x = mb_x;
  2154. s->mb_y = mb_y;
  2155. s->block_index[0]+=2;
  2156. s->block_index[1]+=2;
  2157. s->block_index[2]+=2;
  2158. s->block_index[3]+=2;
  2159. s->block_index[4]++;
  2160. s->block_index[5]++;
  2161. /* write gob / video packet header for formats which support it at any MB (MPEG4) */
  2162. if(s->rtp_mode && s->mb_y>0 && s->codec_id==CODEC_ID_MPEG4){
  2163. int pdif= pbBufPtr(&s->pb) - s->ptr_lastgob;
  2164. //the *2 is there so we stay below the requested size
  2165. if(pdif + s->mb_line_avgsize/s->mb_width >= s->rtp_payload_size){
  2166. if(s->codec_id==CODEC_ID_MPEG4){
  2167. if(s->data_partitioning && s->pict_type!=B_TYPE){
  2168. ff_mpeg4_merge_partitions(s);
  2169. ff_mpeg4_init_partitions(s);
  2170. }
  2171. ff_mpeg4_encode_video_packet_header(s);
  2172. if(s->flags&CODEC_FLAG_PASS1){
  2173. int bits= get_bit_count(&s->pb);
  2174. s->misc_bits+= bits - s->last_bits;
  2175. s->last_bits= bits;
  2176. }
  2177. ff_mpeg4_clean_buffers(s);
  2178. }
  2179. s->ptr_lastgob = pbBufPtr(&s->pb);
  2180. s->first_slice_line=1;
  2181. s->resync_mb_x=mb_x;
  2182. s->resync_mb_y=mb_y;
  2183. }
  2184. if( (s->resync_mb_x == s->mb_x)
  2185. && s->resync_mb_y+1 == s->mb_y){
  2186. s->first_slice_line=0;
  2187. }
  2188. }
  2189. if(mb_type & (mb_type-1)){ // more than 1 MB type possible
  2190. int next_block=0;
  2191. int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
  2192. copy_context_before_encode(&backup_s, s, -1);
  2193. backup_s.pb= s->pb;
  2194. best_s.data_partitioning= s->data_partitioning;
  2195. if(s->data_partitioning){
  2196. backup_s.pb2= s->pb2;
  2197. backup_s.tex_pb= s->tex_pb;
  2198. }
  2199. if(mb_type&MB_TYPE_INTER){
  2200. s->mv_dir = MV_DIR_FORWARD;
  2201. s->mv_type = MV_TYPE_16X16;
  2202. s->mb_intra= 0;
  2203. s->mv[0][0][0] = s->p_mv_table[xy][0];
  2204. s->mv[0][0][1] = s->p_mv_table[xy][1];
  2205. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTER, pb, pb2, tex_pb,
  2206. &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
  2207. }
  2208. if(mb_type&MB_TYPE_INTER4V){
  2209. s->mv_dir = MV_DIR_FORWARD;
  2210. s->mv_type = MV_TYPE_8X8;
  2211. s->mb_intra= 0;
  2212. for(i=0; i<4; i++){
  2213. s->mv[0][i][0] = s->motion_val[s->block_index[i]][0];
  2214. s->mv[0][i][1] = s->motion_val[s->block_index[i]][1];
  2215. }
  2216. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTER4V, pb, pb2, tex_pb,
  2217. &dmin, &next_block, 0, 0);
  2218. }
  2219. if(mb_type&MB_TYPE_FORWARD){
  2220. s->mv_dir = MV_DIR_FORWARD;
  2221. s->mv_type = MV_TYPE_16X16;
  2222. s->mb_intra= 0;
  2223. s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
  2224. s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
  2225. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_FORWARD, pb, pb2, tex_pb,
  2226. &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
  2227. }
  2228. if(mb_type&MB_TYPE_BACKWARD){
  2229. s->mv_dir = MV_DIR_BACKWARD;
  2230. s->mv_type = MV_TYPE_16X16;
  2231. s->mb_intra= 0;
  2232. s->mv[1][0][0] = s->b_back_mv_table[xy][0];
  2233. s->mv[1][0][1] = s->b_back_mv_table[xy][1];
  2234. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_BACKWARD, pb, pb2, tex_pb,
  2235. &dmin, &next_block, s->mv[1][0][0], s->mv[1][0][1]);
  2236. }
  2237. if(mb_type&MB_TYPE_BIDIR){
  2238. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
  2239. s->mv_type = MV_TYPE_16X16;
  2240. s->mb_intra= 0;
  2241. s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
  2242. s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
  2243. s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
  2244. s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
  2245. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_BIDIR, pb, pb2, tex_pb,
  2246. &dmin, &next_block, 0, 0);
  2247. }
  2248. if(mb_type&MB_TYPE_DIRECT){
  2249. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
  2250. s->mv_type = MV_TYPE_16X16; //FIXME
  2251. s->mb_intra= 0;
  2252. s->mv[0][0][0] = s->b_direct_forw_mv_table[xy][0];
  2253. s->mv[0][0][1] = s->b_direct_forw_mv_table[xy][1];
  2254. s->mv[1][0][0] = s->b_direct_back_mv_table[xy][0];
  2255. s->mv[1][0][1] = s->b_direct_back_mv_table[xy][1];
  2256. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_DIRECT, pb, pb2, tex_pb,
  2257. &dmin, &next_block, s->b_direct_mv_table[xy][0], s->b_direct_mv_table[xy][1]);
  2258. }
  2259. if(mb_type&MB_TYPE_INTRA){
  2260. s->mv_dir = MV_DIR_FORWARD;
  2261. s->mv_type = MV_TYPE_16X16;
  2262. s->mb_intra= 1;
  2263. s->mv[0][0][0] = 0;
  2264. s->mv[0][0][1] = 0;
  2265. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTRA, pb, pb2, tex_pb,
  2266. &dmin, &next_block, 0, 0);
  2267. /* force cleaning of ac/dc pred stuff if needed ... */
  2268. if(s->h263_pred || s->h263_aic)
  2269. s->mbintra_table[mb_x + mb_y*s->mb_width]=1;
  2270. }
  2271. copy_context_after_encode(s, &best_s, -1);
  2272. pb_bits_count= get_bit_count(&s->pb);
  2273. flush_put_bits(&s->pb);
  2274. ff_copy_bits(&backup_s.pb, bit_buf[next_block^1], pb_bits_count);
  2275. s->pb= backup_s.pb;
  2276. if(s->data_partitioning){
  2277. pb2_bits_count= get_bit_count(&s->pb2);
  2278. flush_put_bits(&s->pb2);
  2279. ff_copy_bits(&backup_s.pb2, bit_buf2[next_block^1], pb2_bits_count);
  2280. s->pb2= backup_s.pb2;
  2281. tex_pb_bits_count= get_bit_count(&s->tex_pb);
  2282. flush_put_bits(&s->tex_pb);
  2283. ff_copy_bits(&backup_s.tex_pb, bit_buf_tex[next_block^1], tex_pb_bits_count);
  2284. s->tex_pb= backup_s.tex_pb;
  2285. }
  2286. s->last_bits= get_bit_count(&s->pb);
  2287. } else {
  2288. int motion_x, motion_y;
  2289. s->mv_type=MV_TYPE_16X16;
  2290. // only one MB-Type possible
  2291. switch(mb_type){
  2292. case MB_TYPE_INTRA:
  2293. s->mv_dir = MV_DIR_FORWARD;
  2294. s->mb_intra= 1;
  2295. motion_x= s->mv[0][0][0] = 0;
  2296. motion_y= s->mv[0][0][1] = 0;
  2297. break;
  2298. case MB_TYPE_INTER:
  2299. s->mv_dir = MV_DIR_FORWARD;
  2300. s->mb_intra= 0;
  2301. motion_x= s->mv[0][0][0] = s->p_mv_table[xy][0];
  2302. motion_y= s->mv[0][0][1] = s->p_mv_table[xy][1];
  2303. break;
  2304. case MB_TYPE_INTER4V:
  2305. s->mv_dir = MV_DIR_FORWARD;
  2306. s->mv_type = MV_TYPE_8X8;
  2307. s->mb_intra= 0;
  2308. for(i=0; i<4; i++){
  2309. s->mv[0][i][0] = s->motion_val[s->block_index[i]][0];
  2310. s->mv[0][i][1] = s->motion_val[s->block_index[i]][1];
  2311. }
  2312. motion_x= motion_y= 0;
  2313. break;
  2314. case MB_TYPE_DIRECT:
  2315. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
  2316. s->mb_intra= 0;
  2317. motion_x=s->b_direct_mv_table[xy][0];
  2318. motion_y=s->b_direct_mv_table[xy][1];
  2319. s->mv[0][0][0] = s->b_direct_forw_mv_table[xy][0];
  2320. s->mv[0][0][1] = s->b_direct_forw_mv_table[xy][1];
  2321. s->mv[1][0][0] = s->b_direct_back_mv_table[xy][0];
  2322. s->mv[1][0][1] = s->b_direct_back_mv_table[xy][1];
  2323. break;
  2324. case MB_TYPE_BIDIR:
  2325. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
  2326. s->mb_intra= 0;
  2327. motion_x=0;
  2328. motion_y=0;
  2329. s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
  2330. s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
  2331. s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
  2332. s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
  2333. break;
  2334. case MB_TYPE_BACKWARD:
  2335. s->mv_dir = MV_DIR_BACKWARD;
  2336. s->mb_intra= 0;
  2337. motion_x= s->mv[1][0][0] = s->b_back_mv_table[xy][0];
  2338. motion_y= s->mv[1][0][1] = s->b_back_mv_table[xy][1];
  2339. break;
  2340. case MB_TYPE_FORWARD:
  2341. s->mv_dir = MV_DIR_FORWARD;
  2342. s->mb_intra= 0;
  2343. motion_x= s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
  2344. motion_y= s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
  2345. // printf(" %d %d ", motion_x, motion_y);
  2346. break;
  2347. default:
  2348. motion_x=motion_y=0; //gcc warning fix
  2349. printf("illegal MB type\n");
  2350. }
  2351. encode_mb(s, motion_x, motion_y);
  2352. }
  2353. /* clean the MV table in IPS frames for direct mode in B frames */
  2354. if(s->mb_intra /* && I,P,S_TYPE */){
  2355. s->p_mv_table[xy][0]=0;
  2356. s->p_mv_table[xy][1]=0;
  2357. }
  2358. MPV_decode_mb(s, s->block);
  2359. //printf("MB %d %d bits\n", s->mb_x+s->mb_y*s->mb_width, get_bit_count(&s->pb));
  2360. }
  2361. /* Obtain average GOB size for RTP */
  2362. if (s->rtp_mode) {
  2363. if (!mb_y)
  2364. s->mb_line_avgsize = pbBufPtr(&s->pb) - s->ptr_last_mb_line;
  2365. else if (!(mb_y % s->gob_index)) {
  2366. s->mb_line_avgsize = (s->mb_line_avgsize + pbBufPtr(&s->pb) - s->ptr_last_mb_line) >> 1;
  2367. s->ptr_last_mb_line = pbBufPtr(&s->pb);
  2368. }
  2369. //fprintf(stderr, "\nMB line: %d\tSize: %u\tAvg. Size: %u", s->mb_y,
  2370. // (s->pb.buf_ptr - s->ptr_last_mb_line), s->mb_line_avgsize);
  2371. if(s->codec_id!=CODEC_ID_MPEG4) s->first_slice_line = 0; //FIXME clean
  2372. }
  2373. }
  2374. emms_c();
  2375. if(s->codec_id==CODEC_ID_MPEG4 && s->data_partitioning && s->pict_type!=B_TYPE)
  2376. ff_mpeg4_merge_partitions(s);
  2377. if (s->msmpeg4_version && s->msmpeg4_version<4 && s->pict_type == I_TYPE)
  2378. msmpeg4_encode_ext_header(s);
  2379. if(s->codec_id==CODEC_ID_MPEG4)
  2380. ff_mpeg4_stuffing(&s->pb);
  2381. //if (s->gob_number)
  2382. // fprintf(stderr,"\nNumber of GOB: %d", s->gob_number);
  2383. /* Send the last GOB if RTP */
  2384. if (s->rtp_mode) {
  2385. flush_put_bits(&s->pb);
  2386. pdif = pbBufPtr(&s->pb) - s->ptr_lastgob;
  2387. /* Call the RTP callback to send the last GOB */
  2388. if (s->rtp_callback)
  2389. s->rtp_callback(s->ptr_lastgob, pdif, s->gob_number);
  2390. s->ptr_lastgob = pbBufPtr(&s->pb);
  2391. //fprintf(stderr,"\nGOB: %2d size: %d (last)", s->gob_number, pdif);
  2392. }
  2393. }
  2394. static int dct_quantize_c(MpegEncContext *s,
  2395. DCTELEM *block, int n,
  2396. int qscale, int *overflow)
  2397. {
  2398. int i, j, level, last_non_zero, q;
  2399. const int *qmat;
  2400. int bias;
  2401. int max=0;
  2402. unsigned int threshold1, threshold2;
  2403. s->fdct (block);
  2404. #ifndef ARCH_ALPHA /* Alpha uses unpermuted matrix */
  2405. /* we need this permutation so that we correct the IDCT
  2406. permutation. will be moved into DCT code */
  2407. block_permute(block);
  2408. #endif
  2409. if (s->mb_intra) {
  2410. if (!s->h263_aic) {
  2411. if (n < 4)
  2412. q = s->y_dc_scale;
  2413. else
  2414. q = s->c_dc_scale;
  2415. q = q << 3;
  2416. } else
  2417. /* For AIC we skip quant/dequant of INTRADC */
  2418. q = 1 << 3;
  2419. /* note: block[0] is assumed to be positive */
  2420. block[0] = (block[0] + (q >> 1)) / q;
  2421. i = 1;
  2422. last_non_zero = 0;
  2423. qmat = s->q_intra_matrix[qscale];
  2424. bias= s->intra_quant_bias<<(QMAT_SHIFT - QUANT_BIAS_SHIFT);
  2425. } else {
  2426. i = 0;
  2427. last_non_zero = -1;
  2428. qmat = s->q_inter_matrix[qscale];
  2429. bias= s->inter_quant_bias<<(QMAT_SHIFT - QUANT_BIAS_SHIFT);
  2430. }
  2431. threshold1= (1<<QMAT_SHIFT) - bias - 1;
  2432. threshold2= (threshold1<<1);
  2433. for(;i<64;i++) {
  2434. j = zigzag_direct[i];
  2435. level = block[j];
  2436. level = level * qmat[j];
  2437. // if( bias+level >= (1<<(QMAT_SHIFT - 3))
  2438. // || bias-level >= (1<<(QMAT_SHIFT - 3))){
  2439. if(((unsigned)(level+threshold1))>threshold2){
  2440. if(level>0){
  2441. level= (bias + level)>>QMAT_SHIFT;
  2442. block[j]= level;
  2443. }else{
  2444. level= (bias - level)>>QMAT_SHIFT;
  2445. block[j]= -level;
  2446. }
  2447. max |=level;
  2448. last_non_zero = i;
  2449. }else{
  2450. block[j]=0;
  2451. }
  2452. }
  2453. *overflow= s->max_qcoeff < max; //overflow might have happend
  2454. return last_non_zero;
  2455. }
  2456. static void dct_unquantize_mpeg1_c(MpegEncContext *s,
  2457. DCTELEM *block, int n, int qscale)
  2458. {
  2459. int i, level, nCoeffs;
  2460. const UINT16 *quant_matrix;
  2461. if(s->alternate_scan) nCoeffs= 64;
  2462. else nCoeffs= s->block_last_index[n]+1;
  2463. if (s->mb_intra) {
  2464. if (n < 4)
  2465. block[0] = block[0] * s->y_dc_scale;
  2466. else
  2467. block[0] = block[0] * s->c_dc_scale;
  2468. /* XXX: only mpeg1 */
  2469. quant_matrix = s->intra_matrix;
  2470. for(i=1;i<nCoeffs;i++) {
  2471. int j= zigzag_direct[i];
  2472. level = block[j];
  2473. if (level) {
  2474. if (level < 0) {
  2475. level = -level;
  2476. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2477. level = (level - 1) | 1;
  2478. level = -level;
  2479. } else {
  2480. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2481. level = (level - 1) | 1;
  2482. }
  2483. #ifdef PARANOID
  2484. if (level < -2048 || level > 2047)
  2485. fprintf(stderr, "unquant error %d %d\n", i, level);
  2486. #endif
  2487. block[j] = level;
  2488. }
  2489. }
  2490. } else {
  2491. i = 0;
  2492. quant_matrix = s->inter_matrix;
  2493. for(;i<nCoeffs;i++) {
  2494. int j= zigzag_direct[i];
  2495. level = block[j];
  2496. if (level) {
  2497. if (level < 0) {
  2498. level = -level;
  2499. level = (((level << 1) + 1) * qscale *
  2500. ((int) (quant_matrix[j]))) >> 4;
  2501. level = (level - 1) | 1;
  2502. level = -level;
  2503. } else {
  2504. level = (((level << 1) + 1) * qscale *
  2505. ((int) (quant_matrix[j]))) >> 4;
  2506. level = (level - 1) | 1;
  2507. }
  2508. #ifdef PARANOID
  2509. if (level < -2048 || level > 2047)
  2510. fprintf(stderr, "unquant error %d %d\n", i, level);
  2511. #endif
  2512. block[j] = level;
  2513. }
  2514. }
  2515. }
  2516. }
  2517. static void dct_unquantize_mpeg2_c(MpegEncContext *s,
  2518. DCTELEM *block, int n, int qscale)
  2519. {
  2520. int i, level, nCoeffs;
  2521. const UINT16 *quant_matrix;
  2522. if(s->alternate_scan) nCoeffs= 64;
  2523. else nCoeffs= s->block_last_index[n]+1;
  2524. if (s->mb_intra) {
  2525. if (n < 4)
  2526. block[0] = block[0] * s->y_dc_scale;
  2527. else
  2528. block[0] = block[0] * s->c_dc_scale;
  2529. quant_matrix = s->intra_matrix;
  2530. for(i=1;i<nCoeffs;i++) {
  2531. int j= zigzag_direct[i];
  2532. level = block[j];
  2533. if (level) {
  2534. if (level < 0) {
  2535. level = -level;
  2536. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2537. level = -level;
  2538. } else {
  2539. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2540. }
  2541. #ifdef PARANOID
  2542. if (level < -2048 || level > 2047)
  2543. fprintf(stderr, "unquant error %d %d\n", i, level);
  2544. #endif
  2545. block[j] = level;
  2546. }
  2547. }
  2548. } else {
  2549. int sum=-1;
  2550. i = 0;
  2551. quant_matrix = s->inter_matrix;
  2552. for(;i<nCoeffs;i++) {
  2553. int j= zigzag_direct[i];
  2554. level = block[j];
  2555. if (level) {
  2556. if (level < 0) {
  2557. level = -level;
  2558. level = (((level << 1) + 1) * qscale *
  2559. ((int) (quant_matrix[j]))) >> 4;
  2560. level = -level;
  2561. } else {
  2562. level = (((level << 1) + 1) * qscale *
  2563. ((int) (quant_matrix[j]))) >> 4;
  2564. }
  2565. #ifdef PARANOID
  2566. if (level < -2048 || level > 2047)
  2567. fprintf(stderr, "unquant error %d %d\n", i, level);
  2568. #endif
  2569. block[j] = level;
  2570. sum+=level;
  2571. }
  2572. }
  2573. block[63]^=sum&1;
  2574. }
  2575. }
  2576. static void dct_unquantize_h263_c(MpegEncContext *s,
  2577. DCTELEM *block, int n, int qscale)
  2578. {
  2579. int i, level, qmul, qadd;
  2580. int nCoeffs;
  2581. if (s->mb_intra) {
  2582. if (!s->h263_aic) {
  2583. if (n < 4)
  2584. block[0] = block[0] * s->y_dc_scale;
  2585. else
  2586. block[0] = block[0] * s->c_dc_scale;
  2587. }
  2588. i = 1;
  2589. nCoeffs= 64; //does not allways use zigzag table
  2590. } else {
  2591. i = 0;
  2592. nCoeffs= zigzag_end[ s->block_last_index[n] ];
  2593. }
  2594. qmul = s->qscale << 1;
  2595. if (s->h263_aic && s->mb_intra)
  2596. qadd = 0;
  2597. else
  2598. qadd = (s->qscale - 1) | 1;
  2599. for(;i<nCoeffs;i++) {
  2600. level = block[i];
  2601. if (level) {
  2602. if (level < 0) {
  2603. level = level * qmul - qadd;
  2604. } else {
  2605. level = level * qmul + qadd;
  2606. }
  2607. #ifdef PARANOID
  2608. if (level < -2048 || level > 2047)
  2609. fprintf(stderr, "unquant error %d %d\n", i, level);
  2610. #endif
  2611. block[i] = level;
  2612. }
  2613. }
  2614. }
  2615. static void remove_ac(MpegEncContext *s, uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr, int mb_x, int mb_y)
  2616. {
  2617. int dc, dcb, dcr, y, i;
  2618. for(i=0; i<4; i++){
  2619. dc= s->dc_val[0][mb_x*2+1 + (i&1) + (mb_y*2+1 + (i>>1))*(s->mb_width*2+2)];
  2620. for(y=0; y<8; y++){
  2621. int x;
  2622. for(x=0; x<8; x++){
  2623. dest_y[x + (i&1)*8 + (y + (i>>1)*8)*s->linesize]= dc/8;
  2624. }
  2625. }
  2626. }
  2627. dcb = s->dc_val[1][mb_x+1 + (mb_y+1)*(s->mb_width+2)];
  2628. dcr= s->dc_val[2][mb_x+1 + (mb_y+1)*(s->mb_width+2)];
  2629. for(y=0; y<8; y++){
  2630. int x;
  2631. for(x=0; x<8; x++){
  2632. dest_cb[x + y*(s->uvlinesize)]= dcb/8;
  2633. dest_cr[x + y*(s->uvlinesize)]= dcr/8;
  2634. }
  2635. }
  2636. }
  2637. /**
  2638. * will conceal past errors, and allso drop b frames if needed
  2639. *
  2640. */
  2641. void ff_conceal_past_errors(MpegEncContext *s, int unknown_pos)
  2642. {
  2643. int mb_x= s->mb_x;
  2644. int mb_y= s->mb_y;
  2645. int mb_dist=0;
  2646. int i, intra_count=0, inter_count=0;
  2647. int intra_conceal= s->msmpeg4_version ? 50 : 50; //FIXME finetune
  2648. int inter_conceal= s->msmpeg4_version ? 50 : 50;
  2649. // for last block
  2650. if(mb_x>=s->mb_width) mb_x= s->mb_width -1;
  2651. if(mb_y>=s->mb_height) mb_y= s->mb_height-1;
  2652. if(s->decoding_error==0 && unknown_pos){
  2653. if(s->data_partitioning && s->pict_type!=B_TYPE)
  2654. s->decoding_error= DECODING_AC_LOST;
  2655. else
  2656. s->decoding_error= DECODING_DESYNC;
  2657. }
  2658. if(s->decoding_error==DECODING_DESYNC && s->pict_type!=B_TYPE) s->next_p_frame_damaged=1;
  2659. for(i=mb_x + mb_y*s->mb_width; i>=0; i--){
  2660. if(s->mbintra_table[i]) intra_count++;
  2661. else inter_count++;
  2662. }
  2663. if(s->decoding_error==DECODING_AC_LOST){
  2664. intra_conceal*=2;
  2665. inter_conceal*=2;
  2666. }else if(s->decoding_error==DECODING_ACDC_LOST){
  2667. intra_conceal*=2;
  2668. inter_conceal*=2;
  2669. }
  2670. if(unknown_pos && (intra_count<inter_count)){
  2671. intra_conceal= inter_conceal= s->mb_num;
  2672. // printf("%d %d\n",intra_count, inter_count);
  2673. }
  2674. fprintf(stderr, "concealing errors\n");
  2675. /* for all MBs from the current one back until the last resync marker */
  2676. for(; mb_y>=0 && mb_y>=s->resync_mb_y; mb_y--){
  2677. for(; mb_x>=0; mb_x--){
  2678. uint8_t *dest_y = s->current_picture[0] + (mb_y * 16* s->linesize ) + mb_x * 16;
  2679. uint8_t *dest_cb = s->current_picture[1] + (mb_y * 8 * (s->uvlinesize)) + mb_x * 8;
  2680. uint8_t *dest_cr = s->current_picture[2] + (mb_y * 8 * (s->uvlinesize)) + mb_x * 8;
  2681. int mb_x_backup= s->mb_x; //FIXME pass xy to mpeg_motion
  2682. int mb_y_backup= s->mb_y;
  2683. s->mb_x=mb_x;
  2684. s->mb_y=mb_y;
  2685. if(s->mbintra_table[mb_y*s->mb_width + mb_x] && mb_dist<intra_conceal){
  2686. if(s->decoding_error==DECODING_AC_LOST){
  2687. remove_ac(s, dest_y, dest_cb, dest_cr, mb_x, mb_y);
  2688. // printf("remove ac to %d %d\n", mb_x, mb_y);
  2689. }else{
  2690. mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
  2691. s->last_picture, 0, 0, put_pixels_tab,
  2692. 0/*mx*/, 0/*my*/, 16);
  2693. }
  2694. }
  2695. else if(!s->mbintra_table[mb_y*s->mb_width + mb_x] && mb_dist<inter_conceal){
  2696. int mx=0;
  2697. int my=0;
  2698. if(s->decoding_error!=DECODING_DESYNC){
  2699. int xy= mb_x*2+1 + (mb_y*2+1)*(s->mb_width*2+2);
  2700. mx= s->motion_val[ xy ][0];
  2701. my= s->motion_val[ xy ][1];
  2702. }
  2703. mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
  2704. s->last_picture, 0, 0, put_pixels_tab,
  2705. mx, my, 16);
  2706. }
  2707. s->mb_x= mb_x_backup;
  2708. s->mb_y= mb_y_backup;
  2709. if(mb_x== s->resync_mb_x && mb_y== s->resync_mb_y) return;
  2710. if(!s->mbskip_table[mb_x + mb_y*s->mb_width]) mb_dist++;
  2711. }
  2712. mb_x=s->mb_width-1;
  2713. }
  2714. }
  2715. AVCodec mpeg1video_encoder = {
  2716. "mpeg1video",
  2717. CODEC_TYPE_VIDEO,
  2718. CODEC_ID_MPEG1VIDEO,
  2719. sizeof(MpegEncContext),
  2720. MPV_encode_init,
  2721. MPV_encode_picture,
  2722. MPV_encode_end,
  2723. };
  2724. AVCodec h263_encoder = {
  2725. "h263",
  2726. CODEC_TYPE_VIDEO,
  2727. CODEC_ID_H263,
  2728. sizeof(MpegEncContext),
  2729. MPV_encode_init,
  2730. MPV_encode_picture,
  2731. MPV_encode_end,
  2732. };
  2733. AVCodec h263p_encoder = {
  2734. "h263p",
  2735. CODEC_TYPE_VIDEO,
  2736. CODEC_ID_H263P,
  2737. sizeof(MpegEncContext),
  2738. MPV_encode_init,
  2739. MPV_encode_picture,
  2740. MPV_encode_end,
  2741. };
  2742. AVCodec rv10_encoder = {
  2743. "rv10",
  2744. CODEC_TYPE_VIDEO,
  2745. CODEC_ID_RV10,
  2746. sizeof(MpegEncContext),
  2747. MPV_encode_init,
  2748. MPV_encode_picture,
  2749. MPV_encode_end,
  2750. };
  2751. AVCodec mjpeg_encoder = {
  2752. "mjpeg",
  2753. CODEC_TYPE_VIDEO,
  2754. CODEC_ID_MJPEG,
  2755. sizeof(MpegEncContext),
  2756. MPV_encode_init,
  2757. MPV_encode_picture,
  2758. MPV_encode_end,
  2759. };
  2760. AVCodec mpeg4_encoder = {
  2761. "mpeg4",
  2762. CODEC_TYPE_VIDEO,
  2763. CODEC_ID_MPEG4,
  2764. sizeof(MpegEncContext),
  2765. MPV_encode_init,
  2766. MPV_encode_picture,
  2767. MPV_encode_end,
  2768. };
  2769. AVCodec msmpeg4v1_encoder = {
  2770. "msmpeg4v1",
  2771. CODEC_TYPE_VIDEO,
  2772. CODEC_ID_MSMPEG4V1,
  2773. sizeof(MpegEncContext),
  2774. MPV_encode_init,
  2775. MPV_encode_picture,
  2776. MPV_encode_end,
  2777. };
  2778. AVCodec msmpeg4v2_encoder = {
  2779. "msmpeg4v2",
  2780. CODEC_TYPE_VIDEO,
  2781. CODEC_ID_MSMPEG4V2,
  2782. sizeof(MpegEncContext),
  2783. MPV_encode_init,
  2784. MPV_encode_picture,
  2785. MPV_encode_end,
  2786. };
  2787. AVCodec msmpeg4v3_encoder = {
  2788. "msmpeg4",
  2789. CODEC_TYPE_VIDEO,
  2790. CODEC_ID_MSMPEG4V3,
  2791. sizeof(MpegEncContext),
  2792. MPV_encode_init,
  2793. MPV_encode_picture,
  2794. MPV_encode_end,
  2795. };
  2796. AVCodec wmv1_encoder = {
  2797. "wmv1",
  2798. CODEC_TYPE_VIDEO,
  2799. CODEC_ID_WMV1,
  2800. sizeof(MpegEncContext),
  2801. MPV_encode_init,
  2802. MPV_encode_picture,
  2803. MPV_encode_end,
  2804. };
  2805. AVCodec wmv2_encoder = {
  2806. "wmv2",
  2807. CODEC_TYPE_VIDEO,
  2808. CODEC_ID_WMV2,
  2809. sizeof(MpegEncContext),
  2810. MPV_encode_init,
  2811. MPV_encode_picture,
  2812. MPV_encode_end,
  2813. };