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.

2854 lines
98KB

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