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.

2815 lines
96KB

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