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.

3220 lines
110KB

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