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.

2851 lines
98KB

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