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.

2839 lines
97KB

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