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.

1233 lines
52KB

  1. /*
  2. * VC-1 and WMV3 decoder
  3. * Copyright (c) 2011 Mashiat Sarker Shakkhar
  4. * Copyright (c) 2006-2007 Konstantin Shishkov
  5. * Partly based on vc9.c (c) 2005 Anonymous, Alex Beregszaszi, Michael Niedermayer
  6. *
  7. * This file is part of FFmpeg.
  8. *
  9. * FFmpeg is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * FFmpeg is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with FFmpeg; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. /**
  24. * @file
  25. * VC-1 and WMV3 block decoding routines
  26. */
  27. #include "avcodec.h"
  28. #include "h264chroma.h"
  29. #include "mathops.h"
  30. #include "mpegvideo.h"
  31. #include "vc1.h"
  32. static av_always_inline void vc1_scale_luma(uint8_t *srcY,
  33. int k, int linesize)
  34. {
  35. int i, j;
  36. for (j = 0; j < k; j++) {
  37. for (i = 0; i < k; i++)
  38. srcY[i] = ((srcY[i] - 128) >> 1) + 128;
  39. srcY += linesize;
  40. }
  41. }
  42. static av_always_inline void vc1_scale_chroma(uint8_t *srcU, uint8_t *srcV,
  43. int k, int uvlinesize)
  44. {
  45. int i, j;
  46. for (j = 0; j < k; j++) {
  47. for (i = 0; i < k; i++) {
  48. srcU[i] = ((srcU[i] - 128) >> 1) + 128;
  49. srcV[i] = ((srcV[i] - 128) >> 1) + 128;
  50. }
  51. srcU += uvlinesize;
  52. srcV += uvlinesize;
  53. }
  54. }
  55. static av_always_inline void vc1_lut_scale_luma(uint8_t *srcY,
  56. uint8_t *lut1, uint8_t *lut2,
  57. int k, int linesize)
  58. {
  59. int i, j;
  60. for (j = 0; j < k; j += 2) {
  61. for (i = 0; i < k; i++)
  62. srcY[i] = lut1[srcY[i]];
  63. srcY += linesize;
  64. if (j + 1 == k)
  65. break;
  66. for (i = 0; i < k; i++)
  67. srcY[i] = lut2[srcY[i]];
  68. srcY += linesize;
  69. }
  70. }
  71. static av_always_inline void vc1_lut_scale_chroma(uint8_t *srcU, uint8_t *srcV,
  72. uint8_t *lut1, uint8_t *lut2,
  73. int k, int uvlinesize)
  74. {
  75. int i, j;
  76. for (j = 0; j < k; j += 2) {
  77. for (i = 0; i < k; i++) {
  78. srcU[i] = lut1[srcU[i]];
  79. srcV[i] = lut1[srcV[i]];
  80. }
  81. srcU += uvlinesize;
  82. srcV += uvlinesize;
  83. if (j + 1 == k)
  84. break;
  85. for (i = 0; i < k; i++) {
  86. srcU[i] = lut2[srcU[i]];
  87. srcV[i] = lut2[srcV[i]];
  88. }
  89. srcU += uvlinesize;
  90. srcV += uvlinesize;
  91. }
  92. }
  93. static const uint8_t popcount4[16] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4 };
  94. static av_always_inline int get_luma_mv(VC1Context *v, int dir, int16_t *tx, int16_t *ty)
  95. {
  96. MpegEncContext *s = &v->s;
  97. int idx = v->mv_f[dir][s->block_index[0] + v->blocks_off] |
  98. (v->mv_f[dir][s->block_index[1] + v->blocks_off] << 1) |
  99. (v->mv_f[dir][s->block_index[2] + v->blocks_off] << 2) |
  100. (v->mv_f[dir][s->block_index[3] + v->blocks_off] << 3);
  101. static const uint8_t index2[16] = { 0, 0, 0, 0x23, 0, 0x13, 0x03, 0, 0, 0x12, 0x02, 0, 0x01, 0, 0, 0 };
  102. int opp_count = popcount4[idx];
  103. switch (opp_count) {
  104. case 0:
  105. case 4:
  106. *tx = median4(s->mv[dir][0][0], s->mv[dir][1][0], s->mv[dir][2][0], s->mv[dir][3][0]);
  107. *ty = median4(s->mv[dir][0][1], s->mv[dir][1][1], s->mv[dir][2][1], s->mv[dir][3][1]);
  108. break;
  109. case 1:
  110. *tx = mid_pred(s->mv[dir][idx < 2][0], s->mv[dir][1 + (idx < 4)][0], s->mv[dir][2 + (idx < 8)][0]);
  111. *ty = mid_pred(s->mv[dir][idx < 2][1], s->mv[dir][1 + (idx < 4)][1], s->mv[dir][2 + (idx < 8)][1]);
  112. break;
  113. case 3:
  114. *tx = mid_pred(s->mv[dir][idx > 0xd][0], s->mv[dir][1 + (idx > 0xb)][0], s->mv[dir][2 + (idx > 0x7)][0]);
  115. *ty = mid_pred(s->mv[dir][idx > 0xd][1], s->mv[dir][1 + (idx > 0xb)][1], s->mv[dir][2 + (idx > 0x7)][1]);
  116. break;
  117. case 2:
  118. *tx = (s->mv[dir][index2[idx] >> 4][0] + s->mv[dir][index2[idx] & 0xf][0]) / 2;
  119. *ty = (s->mv[dir][index2[idx] >> 4][1] + s->mv[dir][index2[idx] & 0xf][1]) / 2;
  120. break;
  121. }
  122. return opp_count;
  123. }
  124. static av_always_inline int get_chroma_mv(VC1Context *v, int dir, int16_t *tx, int16_t *ty)
  125. {
  126. MpegEncContext *s = &v->s;
  127. int idx = !v->mb_type[0][s->block_index[0]] |
  128. (!v->mb_type[0][s->block_index[1]] << 1) |
  129. (!v->mb_type[0][s->block_index[2]] << 2) |
  130. (!v->mb_type[0][s->block_index[3]] << 3);
  131. static const uint8_t index2[16] = { 0, 0, 0, 0x01, 0, 0x02, 0x12, 0, 0, 0x03, 0x13, 0, 0x23, 0, 0, 0 };
  132. int valid_count = popcount4[idx];
  133. switch (valid_count) {
  134. case 4:
  135. *tx = median4(s->mv[dir][0][0], s->mv[dir][1][0], s->mv[dir][2][0], s->mv[dir][3][0]);
  136. *ty = median4(s->mv[dir][0][1], s->mv[dir][1][1], s->mv[dir][2][1], s->mv[dir][3][1]);
  137. break;
  138. case 3:
  139. *tx = mid_pred(s->mv[dir][idx > 0xd][0], s->mv[dir][1 + (idx > 0xb)][0], s->mv[dir][2 + (idx > 0x7)][0]);
  140. *ty = mid_pred(s->mv[dir][idx > 0xd][1], s->mv[dir][1 + (idx > 0xb)][1], s->mv[dir][2 + (idx > 0x7)][1]);
  141. break;
  142. case 2:
  143. *tx = (s->mv[dir][index2[idx] >> 4][0] + s->mv[dir][index2[idx] & 0xf][0]) / 2;
  144. *ty = (s->mv[dir][index2[idx] >> 4][1] + s->mv[dir][index2[idx] & 0xf][1]) / 2;
  145. break;
  146. default:
  147. return 0;
  148. }
  149. return valid_count;
  150. }
  151. /** Do motion compensation over 1 macroblock
  152. * Mostly adapted hpel_motion and qpel_motion from mpegvideo.c
  153. */
  154. void ff_vc1_mc_1mv(VC1Context *v, int dir)
  155. {
  156. MpegEncContext *s = &v->s;
  157. H264ChromaContext *h264chroma = &v->h264chroma;
  158. uint8_t *srcY, *srcU, *srcV;
  159. int dxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
  160. int v_edge_pos = s->v_edge_pos >> v->field_mode;
  161. int i;
  162. uint8_t (*luty)[256], (*lutuv)[256];
  163. int use_ic;
  164. int interlace;
  165. int linesize, uvlinesize;
  166. if ((!v->field_mode ||
  167. (v->ref_field_type[dir] == 1 && v->cur_field_type == 1)) &&
  168. !v->s.last_picture.f->data[0])
  169. return;
  170. linesize = s->current_picture_ptr->f->linesize[0];
  171. uvlinesize = s->current_picture_ptr->f->linesize[1];
  172. mx = s->mv[dir][0][0];
  173. my = s->mv[dir][0][1];
  174. // store motion vectors for further use in B-frames
  175. if (s->pict_type == AV_PICTURE_TYPE_P) {
  176. for (i = 0; i < 4; i++) {
  177. s->current_picture.motion_val[1][s->block_index[i] + v->blocks_off][0] = mx;
  178. s->current_picture.motion_val[1][s->block_index[i] + v->blocks_off][1] = my;
  179. }
  180. }
  181. uvmx = (mx + ((mx & 3) == 3)) >> 1;
  182. uvmy = (my + ((my & 3) == 3)) >> 1;
  183. v->luma_mv[s->mb_x][0] = uvmx;
  184. v->luma_mv[s->mb_x][1] = uvmy;
  185. if (v->field_mode &&
  186. v->cur_field_type != v->ref_field_type[dir]) {
  187. my = my - 2 + 4 * v->cur_field_type;
  188. uvmy = uvmy - 2 + 4 * v->cur_field_type;
  189. }
  190. // fastuvmc shall be ignored for interlaced frame picture
  191. if (v->fastuvmc && (v->fcm != ILACE_FRAME)) {
  192. uvmx = uvmx + ((uvmx < 0) ? (uvmx & 1) : -(uvmx & 1));
  193. uvmy = uvmy + ((uvmy < 0) ? (uvmy & 1) : -(uvmy & 1));
  194. }
  195. if (!dir) {
  196. if (v->field_mode && (v->cur_field_type != v->ref_field_type[dir]) && v->second_field) {
  197. srcY = s->current_picture.f->data[0];
  198. srcU = s->current_picture.f->data[1];
  199. srcV = s->current_picture.f->data[2];
  200. luty = v->curr_luty;
  201. lutuv = v->curr_lutuv;
  202. use_ic = *v->curr_use_ic;
  203. interlace = 1;
  204. } else {
  205. srcY = s->last_picture.f->data[0];
  206. srcU = s->last_picture.f->data[1];
  207. srcV = s->last_picture.f->data[2];
  208. luty = v->last_luty;
  209. lutuv = v->last_lutuv;
  210. use_ic = v->last_use_ic;
  211. interlace = s->last_picture.f->interlaced_frame;
  212. }
  213. } else {
  214. srcY = s->next_picture.f->data[0];
  215. srcU = s->next_picture.f->data[1];
  216. srcV = s->next_picture.f->data[2];
  217. luty = v->next_luty;
  218. lutuv = v->next_lutuv;
  219. use_ic = v->next_use_ic;
  220. interlace = s->next_picture.f->interlaced_frame;
  221. }
  222. if (!srcY || !srcU) {
  223. av_log(v->s.avctx, AV_LOG_ERROR, "Referenced frame missing.\n");
  224. return;
  225. }
  226. src_x = s->mb_x * 16 + (mx >> 2);
  227. src_y = s->mb_y * 16 + (my >> 2);
  228. uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
  229. uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
  230. if (v->profile != PROFILE_ADVANCED) {
  231. src_x = av_clip( src_x, -16, s->mb_width * 16);
  232. src_y = av_clip( src_y, -16, s->mb_height * 16);
  233. uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
  234. uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
  235. } else {
  236. src_x = av_clip( src_x, -17, s->avctx->coded_width);
  237. uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
  238. if (v->fcm == ILACE_FRAME) {
  239. src_y = av_clip(src_y, -18 + (src_y & 1), s->avctx->coded_height + (src_y & 1));
  240. uvsrc_y = av_clip(uvsrc_y, -8 + (uvsrc_y & 1), (s->avctx->coded_height >> 1) + (uvsrc_y & 1));
  241. } else {
  242. src_y = av_clip(src_y, -18, s->avctx->coded_height + 1);
  243. uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
  244. }
  245. }
  246. srcY += src_y * s->linesize + src_x;
  247. srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
  248. srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
  249. if (v->field_mode && v->ref_field_type[dir]) {
  250. srcY += linesize;
  251. srcU += uvlinesize;
  252. srcV += uvlinesize;
  253. }
  254. /* for grayscale we should not try to read from unknown area */
  255. if (CONFIG_GRAY && s->avctx->flags & AV_CODEC_FLAG_GRAY) {
  256. srcU = s->sc.edge_emu_buffer + 18 * s->linesize;
  257. srcV = s->sc.edge_emu_buffer + 18 * s->linesize;
  258. }
  259. if (v->rangeredfrm || use_ic
  260. || s->h_edge_pos < 22 || v_edge_pos < 22
  261. || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx&3) - 16 - s->mspel * 3
  262. || (unsigned)(src_y - 1) > v_edge_pos - (my&3) - 16 - 3) {
  263. uint8_t *ubuf = s->sc.edge_emu_buffer + 19 * s->linesize;
  264. uint8_t *vbuf = ubuf + 9 * s->uvlinesize;
  265. const int k = 17 + s->mspel * 2;
  266. srcY -= s->mspel * (1 + s->linesize);
  267. if (interlace) {
  268. s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer,
  269. srcY,
  270. linesize << 1,
  271. linesize << 1,
  272. k,
  273. v->field_mode ? k : k + 1 >> 1,
  274. src_x - s->mspel,
  275. src_y - s->mspel >> !v->field_mode,
  276. s->h_edge_pos,
  277. s->v_edge_pos >> 1);
  278. if (!v->field_mode)
  279. s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer + linesize,
  280. srcY + linesize,
  281. linesize << 1,
  282. linesize << 1,
  283. k,
  284. k >> 1,
  285. src_x - s->mspel,
  286. src_y - s->mspel + 1 >> 1,
  287. s->h_edge_pos,
  288. s->v_edge_pos >> 1);
  289. } else
  290. s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer,
  291. srcY,
  292. linesize,
  293. linesize,
  294. k,
  295. v->field_mode ? (k << 1) - 1 : k,
  296. src_x - s->mspel,
  297. v->field_mode ? 2 * (src_y - s->mspel) + v->ref_field_type[dir] :
  298. src_y - s->mspel,
  299. s->h_edge_pos,
  300. s->v_edge_pos);
  301. srcY = s->sc.edge_emu_buffer;
  302. if (interlace) {
  303. s->vdsp.emulated_edge_mc(ubuf,
  304. srcU,
  305. uvlinesize << 1,
  306. uvlinesize << 1,
  307. 9,
  308. v->field_mode ? 9 : 5,
  309. uvsrc_x,
  310. uvsrc_y >> !v->field_mode,
  311. s->h_edge_pos >> 1,
  312. s->v_edge_pos >> 2);
  313. s->vdsp.emulated_edge_mc(vbuf,
  314. srcV,
  315. uvlinesize << 1,
  316. uvlinesize << 1,
  317. 9,
  318. v->field_mode ? 9 : 5,
  319. uvsrc_x,
  320. uvsrc_y >> !v->field_mode,
  321. s->h_edge_pos >> 1,
  322. s->v_edge_pos >> 2);
  323. if (!v->field_mode) {
  324. s->vdsp.emulated_edge_mc(ubuf + uvlinesize,
  325. srcU + uvlinesize,
  326. uvlinesize << 1,
  327. uvlinesize << 1,
  328. 9,
  329. 4,
  330. uvsrc_x,
  331. uvsrc_y + 1 >> 1,
  332. s->h_edge_pos >> 1,
  333. s->v_edge_pos >> 2);
  334. s->vdsp.emulated_edge_mc(vbuf + uvlinesize,
  335. srcV + uvlinesize,
  336. uvlinesize << 1,
  337. uvlinesize << 1,
  338. 9,
  339. 4,
  340. uvsrc_x,
  341. uvsrc_y + 1 >> 1,
  342. s->h_edge_pos >> 1,
  343. s->v_edge_pos >> 2);
  344. }
  345. } else {
  346. s->vdsp.emulated_edge_mc(ubuf,
  347. srcU,
  348. uvlinesize,
  349. uvlinesize,
  350. 9,
  351. v->field_mode ? 17 : 9,
  352. uvsrc_x,
  353. v->field_mode ? 2 * uvsrc_y + v->ref_field_type[dir] : uvsrc_y,
  354. s->h_edge_pos >> 1,
  355. s->v_edge_pos >> 1);
  356. s->vdsp.emulated_edge_mc(vbuf,
  357. srcV,
  358. uvlinesize,
  359. uvlinesize,
  360. 9,
  361. v->field_mode ? 17 : 9,
  362. uvsrc_x,
  363. v->field_mode ? 2 * uvsrc_y + v->ref_field_type[dir] : uvsrc_y,
  364. s->h_edge_pos >> 1,
  365. s->v_edge_pos >> 1);
  366. }
  367. srcU = ubuf;
  368. srcV = vbuf;
  369. /* if we deal with range reduction we need to scale source blocks */
  370. if (v->rangeredfrm) {
  371. vc1_scale_luma(srcY, k, s->linesize);
  372. vc1_scale_chroma(srcU, srcV, 9, s->uvlinesize);
  373. }
  374. /* if we deal with intensity compensation we need to scale source blocks */
  375. if (use_ic) {
  376. vc1_lut_scale_luma(srcY,
  377. luty[v->field_mode ? v->ref_field_type[dir] : ((0 + src_y - s->mspel) & 1)],
  378. luty[v->field_mode ? v->ref_field_type[dir] : ((1 + src_y - s->mspel) & 1)],
  379. k, s->linesize);
  380. vc1_lut_scale_chroma(srcU, srcV,
  381. lutuv[v->field_mode ? v->ref_field_type[dir] : ((0 + uvsrc_y) & 1)],
  382. lutuv[v->field_mode ? v->ref_field_type[dir] : ((1 + uvsrc_y) & 1)],
  383. 9, s->uvlinesize);
  384. }
  385. srcY += s->mspel * (1 + s->linesize);
  386. }
  387. if (s->mspel) {
  388. dxy = ((my & 3) << 2) | (mx & 3);
  389. v->vc1dsp.put_vc1_mspel_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, v->rnd);
  390. } else { // hpel mc - always used for luma
  391. dxy = (my & 2) | ((mx & 2) >> 1);
  392. if (!v->rnd)
  393. s->hdsp.put_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
  394. else
  395. s->hdsp.put_no_rnd_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
  396. }
  397. if (CONFIG_GRAY && s->avctx->flags & AV_CODEC_FLAG_GRAY)
  398. return;
  399. /* Chroma MC always uses qpel bilinear */
  400. uvmx = (uvmx & 3) << 1;
  401. uvmy = (uvmy & 3) << 1;
  402. if (!v->rnd) {
  403. h264chroma->put_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
  404. h264chroma->put_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
  405. } else {
  406. v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
  407. v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
  408. }
  409. if (v->field_mode) {
  410. v->mv_f[dir][s->block_index[4] + v->mb_off] = v->cur_field_type != v->ref_field_type[dir];
  411. v->mv_f[dir][s->block_index[5] + v->mb_off] = v->cur_field_type != v->ref_field_type[dir];
  412. }
  413. }
  414. /** Do motion compensation for 4-MV macroblock - luminance block
  415. */
  416. void ff_vc1_mc_4mv_luma(VC1Context *v, int n, int dir, int avg)
  417. {
  418. MpegEncContext *s = &v->s;
  419. uint8_t *srcY;
  420. int dxy, mx, my, src_x, src_y;
  421. int off;
  422. int fieldmv = (v->fcm == ILACE_FRAME) ? v->blk_mv_type[s->block_index[n]] : 0;
  423. int v_edge_pos = s->v_edge_pos >> v->field_mode;
  424. uint8_t (*luty)[256];
  425. int use_ic;
  426. int interlace;
  427. int linesize;
  428. if ((!v->field_mode ||
  429. (v->ref_field_type[dir] == 1 && v->cur_field_type == 1)) &&
  430. !v->s.last_picture.f->data[0])
  431. return;
  432. linesize = s->current_picture_ptr->f->linesize[0];
  433. mx = s->mv[dir][n][0];
  434. my = s->mv[dir][n][1];
  435. if (!dir) {
  436. if (v->field_mode && (v->cur_field_type != v->ref_field_type[dir]) && v->second_field) {
  437. srcY = s->current_picture.f->data[0];
  438. luty = v->curr_luty;
  439. use_ic = *v->curr_use_ic;
  440. interlace = 1;
  441. } else {
  442. srcY = s->last_picture.f->data[0];
  443. luty = v->last_luty;
  444. use_ic = v->last_use_ic;
  445. interlace = s->last_picture.f->interlaced_frame;
  446. }
  447. } else {
  448. srcY = s->next_picture.f->data[0];
  449. luty = v->next_luty;
  450. use_ic = v->next_use_ic;
  451. interlace = s->next_picture.f->interlaced_frame;
  452. }
  453. if (!srcY) {
  454. av_log(v->s.avctx, AV_LOG_ERROR, "Referenced frame missing.\n");
  455. return;
  456. }
  457. if (v->field_mode) {
  458. if (v->cur_field_type != v->ref_field_type[dir])
  459. my = my - 2 + 4 * v->cur_field_type;
  460. }
  461. if (s->pict_type == AV_PICTURE_TYPE_P && n == 3 && v->field_mode) {
  462. int opp_count = get_luma_mv(v, 0,
  463. &s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0],
  464. &s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1]);
  465. int k, f = opp_count > 2;
  466. for (k = 0; k < 4; k++)
  467. v->mv_f[1][s->block_index[k] + v->blocks_off] = f;
  468. }
  469. if (v->fcm == ILACE_FRAME) { // not sure if needed for other types of picture
  470. int qx, qy;
  471. int width = s->avctx->coded_width;
  472. int height = s->avctx->coded_height >> 1;
  473. if (s->pict_type == AV_PICTURE_TYPE_P) {
  474. s->current_picture.motion_val[1][s->block_index[n] + v->blocks_off][0] = mx;
  475. s->current_picture.motion_val[1][s->block_index[n] + v->blocks_off][1] = my;
  476. }
  477. qx = (s->mb_x * 16) + (mx >> 2);
  478. qy = (s->mb_y * 8) + (my >> 3);
  479. if (qx < -17)
  480. mx -= 4 * (qx + 17);
  481. else if (qx > width)
  482. mx -= 4 * (qx - width);
  483. if (qy < -18)
  484. my -= 8 * (qy + 18);
  485. else if (qy > height + 1)
  486. my -= 8 * (qy - height - 1);
  487. }
  488. if ((v->fcm == ILACE_FRAME) && fieldmv)
  489. off = ((n > 1) ? s->linesize : 0) + (n & 1) * 8;
  490. else
  491. off = s->linesize * 4 * (n & 2) + (n & 1) * 8;
  492. src_x = s->mb_x * 16 + (n & 1) * 8 + (mx >> 2);
  493. if (!fieldmv)
  494. src_y = s->mb_y * 16 + (n & 2) * 4 + (my >> 2);
  495. else
  496. src_y = s->mb_y * 16 + ((n > 1) ? 1 : 0) + (my >> 2);
  497. if (v->profile != PROFILE_ADVANCED) {
  498. src_x = av_clip(src_x, -16, s->mb_width * 16);
  499. src_y = av_clip(src_y, -16, s->mb_height * 16);
  500. } else {
  501. src_x = av_clip(src_x, -17, s->avctx->coded_width);
  502. if (v->fcm == ILACE_FRAME)
  503. src_y = av_clip(src_y, -18 + (src_y & 1), s->avctx->coded_height + (src_y & 1));
  504. else
  505. src_y = av_clip(src_y, -18, s->avctx->coded_height + 1);
  506. }
  507. srcY += src_y * s->linesize + src_x;
  508. if (v->field_mode && v->ref_field_type[dir])
  509. srcY += linesize;
  510. if (v->rangeredfrm || use_ic
  511. || s->h_edge_pos < 13 || v_edge_pos < 23
  512. || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx & 3) - 8 - s->mspel * 2
  513. || (unsigned)(src_y - (s->mspel << fieldmv)) > v_edge_pos - (my & 3) - ((8 + s->mspel * 2) << fieldmv)) {
  514. const int k = 9 + s->mspel * 2;
  515. srcY -= s->mspel * (1 + (s->linesize << fieldmv));
  516. /* check emulate edge stride and offset */
  517. if (interlace) {
  518. s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer,
  519. srcY,
  520. linesize << 1,
  521. linesize << 1,
  522. k,
  523. v->field_mode ? k : (k << fieldmv) + 1 >> 1,
  524. src_x - s->mspel,
  525. src_y - (s->mspel << fieldmv) >> !v->field_mode,
  526. s->h_edge_pos,
  527. s->v_edge_pos >> 1);
  528. if (!v->field_mode && !fieldmv)
  529. s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer + linesize,
  530. srcY + linesize,
  531. linesize << 1,
  532. linesize << 1,
  533. k,
  534. k >> 1,
  535. src_x - s->mspel,
  536. src_y - s->mspel + 1 >> 1,
  537. s->h_edge_pos,
  538. s->v_edge_pos >> 1);
  539. } else
  540. s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer,
  541. srcY,
  542. linesize,
  543. linesize,
  544. k,
  545. v->field_mode ? (k << 1) - 1 : k << fieldmv,
  546. src_x - s->mspel,
  547. v->field_mode ? 2 * (src_y - s->mspel) + v->ref_field_type[dir] :
  548. src_y - (s->mspel << fieldmv),
  549. s->h_edge_pos,
  550. s->v_edge_pos);
  551. srcY = s->sc.edge_emu_buffer;
  552. /* if we deal with range reduction we need to scale source blocks */
  553. if (v->rangeredfrm) {
  554. vc1_scale_luma(srcY, k, s->linesize << fieldmv);
  555. }
  556. /* if we deal with intensity compensation we need to scale source blocks */
  557. if (use_ic) {
  558. vc1_lut_scale_luma(srcY,
  559. luty[v->field_mode ? v->ref_field_type[dir] : (((0<<fieldmv)+src_y - (s->mspel << fieldmv)) & 1)],
  560. luty[v->field_mode ? v->ref_field_type[dir] : (((1<<fieldmv)+src_y - (s->mspel << fieldmv)) & 1)],
  561. k, s->linesize << fieldmv);
  562. }
  563. srcY += s->mspel * (1 + (s->linesize << fieldmv));
  564. }
  565. if (s->mspel) {
  566. dxy = ((my & 3) << 2) | (mx & 3);
  567. if (avg)
  568. v->vc1dsp.avg_vc1_mspel_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize << fieldmv, v->rnd);
  569. else
  570. v->vc1dsp.put_vc1_mspel_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize << fieldmv, v->rnd);
  571. } else { // hpel mc - always used for luma
  572. dxy = (my & 2) | ((mx & 2) >> 1);
  573. if (!v->rnd)
  574. s->hdsp.put_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
  575. else
  576. s->hdsp.put_no_rnd_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
  577. }
  578. }
  579. /** Do motion compensation for 4-MV macroblock - both chroma blocks
  580. */
  581. void ff_vc1_mc_4mv_chroma(VC1Context *v, int dir)
  582. {
  583. MpegEncContext *s = &v->s;
  584. H264ChromaContext *h264chroma = &v->h264chroma;
  585. uint8_t *srcU, *srcV;
  586. int uvmx, uvmy, uvsrc_x, uvsrc_y;
  587. int16_t tx, ty;
  588. int chroma_ref_type;
  589. int v_edge_pos = s->v_edge_pos >> v->field_mode;
  590. uint8_t (*lutuv)[256];
  591. int use_ic;
  592. int interlace;
  593. int uvlinesize;
  594. if (!v->field_mode && !v->s.last_picture.f->data[0])
  595. return;
  596. if (CONFIG_GRAY && s->avctx->flags & AV_CODEC_FLAG_GRAY)
  597. return;
  598. /* calculate chroma MV vector from four luma MVs */
  599. if (!v->field_mode || !v->numref) {
  600. int valid_count = get_chroma_mv(v, dir, &tx, &ty);
  601. if (!valid_count) {
  602. s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
  603. s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
  604. v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
  605. return; //no need to do MC for intra blocks
  606. }
  607. chroma_ref_type = v->ref_field_type[dir];
  608. } else {
  609. int opp_count = get_luma_mv(v, dir, &tx, &ty);
  610. chroma_ref_type = v->cur_field_type ^ (opp_count > 2);
  611. }
  612. if (v->field_mode && chroma_ref_type == 1 && v->cur_field_type == 1 && !v->s.last_picture.f->data[0])
  613. return;
  614. s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = tx;
  615. s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = ty;
  616. uvlinesize = s->current_picture_ptr->f->linesize[1];
  617. uvmx = (tx + ((tx & 3) == 3)) >> 1;
  618. uvmy = (ty + ((ty & 3) == 3)) >> 1;
  619. v->luma_mv[s->mb_x][0] = uvmx;
  620. v->luma_mv[s->mb_x][1] = uvmy;
  621. if (v->fastuvmc) {
  622. uvmx = uvmx + ((uvmx < 0) ? (uvmx & 1) : -(uvmx & 1));
  623. uvmy = uvmy + ((uvmy < 0) ? (uvmy & 1) : -(uvmy & 1));
  624. }
  625. // Field conversion bias
  626. if (v->cur_field_type != chroma_ref_type)
  627. uvmy += 2 - 4 * chroma_ref_type;
  628. uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
  629. uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
  630. if (v->profile != PROFILE_ADVANCED) {
  631. uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
  632. uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
  633. } else {
  634. uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
  635. uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
  636. }
  637. if (!dir) {
  638. if (v->field_mode && (v->cur_field_type != chroma_ref_type) && v->second_field) {
  639. srcU = s->current_picture.f->data[1];
  640. srcV = s->current_picture.f->data[2];
  641. lutuv = v->curr_lutuv;
  642. use_ic = *v->curr_use_ic;
  643. interlace = 1;
  644. } else {
  645. srcU = s->last_picture.f->data[1];
  646. srcV = s->last_picture.f->data[2];
  647. lutuv = v->last_lutuv;
  648. use_ic = v->last_use_ic;
  649. interlace = s->last_picture.f->interlaced_frame;
  650. }
  651. } else {
  652. srcU = s->next_picture.f->data[1];
  653. srcV = s->next_picture.f->data[2];
  654. lutuv = v->next_lutuv;
  655. use_ic = v->next_use_ic;
  656. interlace = s->next_picture.f->interlaced_frame;
  657. }
  658. if (!srcU) {
  659. av_log(v->s.avctx, AV_LOG_ERROR, "Referenced frame missing.\n");
  660. return;
  661. }
  662. srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
  663. srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
  664. if (v->field_mode) {
  665. if (chroma_ref_type) {
  666. srcU += uvlinesize;
  667. srcV += uvlinesize;
  668. }
  669. }
  670. if (v->rangeredfrm || use_ic
  671. || s->h_edge_pos < 18 || v_edge_pos < 18
  672. || (unsigned)uvsrc_x > (s->h_edge_pos >> 1) - 9
  673. || (unsigned)uvsrc_y > (v_edge_pos >> 1) - 9) {
  674. if (interlace) {
  675. s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer,
  676. srcU,
  677. uvlinesize << 1,
  678. uvlinesize << 1,
  679. 9,
  680. v->field_mode ? 9 : 5,
  681. uvsrc_x,
  682. uvsrc_y >> !v->field_mode,
  683. s->h_edge_pos >> 1,
  684. s->v_edge_pos >> 2);
  685. s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer + 16,
  686. srcV,
  687. uvlinesize << 1,
  688. uvlinesize << 1,
  689. 9,
  690. v->field_mode ? 9 : 5,
  691. uvsrc_x,
  692. uvsrc_y >> !v->field_mode,
  693. s->h_edge_pos >> 1,
  694. s->v_edge_pos >> 2);
  695. if (!v->field_mode) {
  696. s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer + uvlinesize,
  697. srcU + uvlinesize,
  698. uvlinesize << 1,
  699. uvlinesize << 1,
  700. 9,
  701. 4,
  702. uvsrc_x,
  703. uvsrc_y + 1 >> 1,
  704. s->h_edge_pos >> 1,
  705. s->v_edge_pos >> 2);
  706. s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer + 16 + uvlinesize,
  707. srcV + uvlinesize,
  708. uvlinesize << 1,
  709. uvlinesize << 1,
  710. 9,
  711. 4,
  712. uvsrc_x,
  713. uvsrc_y + 1 >> 1,
  714. s->h_edge_pos >> 1,
  715. s->v_edge_pos >> 2);
  716. }
  717. } else {
  718. s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer,
  719. srcU,
  720. uvlinesize,
  721. uvlinesize,
  722. 9,
  723. v->field_mode ? 17 : 9,
  724. uvsrc_x,
  725. v->field_mode ? 2 * uvsrc_y + chroma_ref_type : uvsrc_y,
  726. s->h_edge_pos >> 1,
  727. s->v_edge_pos >> 1);
  728. s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer + 16,
  729. srcV,
  730. uvlinesize,
  731. uvlinesize,
  732. 9,
  733. v->field_mode ? 17 : 9,
  734. uvsrc_x,
  735. v->field_mode ? 2 * uvsrc_y + chroma_ref_type : uvsrc_y,
  736. s->h_edge_pos >> 1,
  737. s->v_edge_pos >> 1);
  738. }
  739. srcU = s->sc.edge_emu_buffer;
  740. srcV = s->sc.edge_emu_buffer + 16;
  741. /* if we deal with range reduction we need to scale source blocks */
  742. if (v->rangeredfrm) {
  743. vc1_scale_chroma(srcU, srcV, 9, s->uvlinesize);
  744. }
  745. /* if we deal with intensity compensation we need to scale source blocks */
  746. if (use_ic) {
  747. vc1_lut_scale_chroma(srcU, srcV,
  748. lutuv[v->field_mode ? chroma_ref_type : ((0 + uvsrc_y) & 1)],
  749. lutuv[v->field_mode ? chroma_ref_type : ((1 + uvsrc_y) & 1)],
  750. 9, s->uvlinesize);
  751. }
  752. }
  753. /* Chroma MC always uses qpel bilinear */
  754. uvmx = (uvmx & 3) << 1;
  755. uvmy = (uvmy & 3) << 1;
  756. if (!v->rnd) {
  757. h264chroma->put_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
  758. h264chroma->put_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
  759. } else {
  760. v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
  761. v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
  762. }
  763. if (v->field_mode) {
  764. v->mv_f[dir][s->block_index[4] + v->mb_off] = v->cur_field_type != chroma_ref_type;
  765. v->mv_f[dir][s->block_index[5] + v->mb_off] = v->cur_field_type != chroma_ref_type;
  766. }
  767. }
  768. /** Do motion compensation for 4-MV interlaced frame chroma macroblock (both U and V)
  769. */
  770. void ff_vc1_mc_4mv_chroma4(VC1Context *v, int dir, int dir2, int avg)
  771. {
  772. MpegEncContext *s = &v->s;
  773. H264ChromaContext *h264chroma = &v->h264chroma;
  774. uint8_t *srcU, *srcV;
  775. int uvsrc_x, uvsrc_y;
  776. int uvmx_field[4], uvmy_field[4];
  777. int i, off, tx, ty;
  778. int fieldmv = v->blk_mv_type[s->block_index[0]];
  779. static const uint8_t s_rndtblfield[16] = { 0, 0, 1, 2, 4, 4, 5, 6, 2, 2, 3, 8, 6, 6, 7, 12 };
  780. int v_dist = fieldmv ? 1 : 4; // vertical offset for lower sub-blocks
  781. int v_edge_pos = s->v_edge_pos >> 1;
  782. int use_ic;
  783. int interlace;
  784. int uvlinesize;
  785. uint8_t (*lutuv)[256];
  786. if (CONFIG_GRAY && s->avctx->flags & AV_CODEC_FLAG_GRAY)
  787. return;
  788. uvlinesize = s->current_picture_ptr->f->linesize[1];
  789. for (i = 0; i < 4; i++) {
  790. int d = i < 2 ? dir: dir2;
  791. tx = s->mv[d][i][0];
  792. uvmx_field[i] = (tx + ((tx & 3) == 3)) >> 1;
  793. ty = s->mv[d][i][1];
  794. if (fieldmv)
  795. uvmy_field[i] = (ty >> 4) * 8 + s_rndtblfield[ty & 0xF];
  796. else
  797. uvmy_field[i] = (ty + ((ty & 3) == 3)) >> 1;
  798. }
  799. for (i = 0; i < 4; i++) {
  800. off = (i & 1) * 4 + ((i & 2) ? v_dist * s->uvlinesize : 0);
  801. uvsrc_x = s->mb_x * 8 + (i & 1) * 4 + (uvmx_field[i] >> 2);
  802. uvsrc_y = s->mb_y * 8 + ((i & 2) ? v_dist : 0) + (uvmy_field[i] >> 2);
  803. // FIXME: implement proper pull-back (see vc1cropmv.c, vc1CROPMV_ChromaPullBack())
  804. uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
  805. if (v->fcm == ILACE_FRAME)
  806. uvsrc_y = av_clip(uvsrc_y, -8 + (uvsrc_y & 1), (s->avctx->coded_height >> 1) + (uvsrc_y & 1));
  807. else
  808. uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
  809. if (i < 2 ? dir : dir2) {
  810. srcU = s->next_picture.f->data[1];
  811. srcV = s->next_picture.f->data[2];
  812. lutuv = v->next_lutuv;
  813. use_ic = v->next_use_ic;
  814. interlace = s->next_picture.f->interlaced_frame;
  815. } else {
  816. srcU = s->last_picture.f->data[1];
  817. srcV = s->last_picture.f->data[2];
  818. lutuv = v->last_lutuv;
  819. use_ic = v->last_use_ic;
  820. interlace = s->last_picture.f->interlaced_frame;
  821. }
  822. if (!srcU)
  823. return;
  824. srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
  825. srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
  826. uvmx_field[i] = (uvmx_field[i] & 3) << 1;
  827. uvmy_field[i] = (uvmy_field[i] & 3) << 1;
  828. if (use_ic
  829. || s->h_edge_pos < 10 || v_edge_pos < (5 << fieldmv)
  830. || (unsigned)uvsrc_x > (s->h_edge_pos >> 1) - 5
  831. || (unsigned)uvsrc_y > v_edge_pos - (5 << fieldmv)) {
  832. if (interlace) {
  833. s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer,
  834. srcU,
  835. uvlinesize << 1,
  836. uvlinesize << 1,
  837. 5,
  838. (5 << fieldmv) + 1 >> 1,
  839. uvsrc_x,
  840. uvsrc_y >> 1,
  841. s->h_edge_pos >> 1,
  842. s->v_edge_pos >> 2);
  843. s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer + 16,
  844. srcV,
  845. uvlinesize << 1,
  846. uvlinesize << 1,
  847. 5,
  848. (5 << fieldmv) + 1 >> 1,
  849. uvsrc_x,
  850. uvsrc_y >> 1,
  851. s->h_edge_pos >> 1,
  852. s->v_edge_pos >> 2);
  853. if (!fieldmv) {
  854. s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer + uvlinesize,
  855. srcU + uvlinesize,
  856. uvlinesize << 1,
  857. uvlinesize << 1,
  858. 5,
  859. 2,
  860. uvsrc_x,
  861. uvsrc_y + 1 >> 1,
  862. s->h_edge_pos >> 1,
  863. s->v_edge_pos >> 2);
  864. s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer + 16 + uvlinesize,
  865. srcV + uvlinesize,
  866. uvlinesize << 1,
  867. uvlinesize << 1,
  868. 5,
  869. 2,
  870. uvsrc_x,
  871. uvsrc_y + 1 >> 1,
  872. s->h_edge_pos >> 1,
  873. s->v_edge_pos >> 2);
  874. }
  875. } else {
  876. s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer,
  877. srcU,
  878. uvlinesize,
  879. uvlinesize,
  880. 5,
  881. 5 << fieldmv,
  882. uvsrc_x,
  883. uvsrc_y,
  884. s->h_edge_pos >> 1,
  885. s->v_edge_pos >> 1);
  886. s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer + 16,
  887. srcV,
  888. uvlinesize,
  889. uvlinesize,
  890. 5,
  891. 5 << fieldmv,
  892. uvsrc_x,
  893. uvsrc_y,
  894. s->h_edge_pos >> 1,
  895. s->v_edge_pos >> 1);
  896. }
  897. srcU = s->sc.edge_emu_buffer;
  898. srcV = s->sc.edge_emu_buffer + 16;
  899. /* if we deal with intensity compensation we need to scale source blocks */
  900. if (use_ic) {
  901. vc1_lut_scale_chroma(srcU, srcV,
  902. lutuv[(uvsrc_y + (0 << fieldmv)) & 1],
  903. lutuv[(uvsrc_y + (1 << fieldmv)) & 1],
  904. 5, s->uvlinesize << fieldmv);
  905. }
  906. }
  907. if (avg) {
  908. if (!v->rnd) {
  909. h264chroma->avg_h264_chroma_pixels_tab[1](s->dest[1] + off, srcU, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
  910. h264chroma->avg_h264_chroma_pixels_tab[1](s->dest[2] + off, srcV, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
  911. } else {
  912. v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[1](s->dest[1] + off, srcU, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
  913. v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[1](s->dest[2] + off, srcV, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
  914. }
  915. } else {
  916. if (!v->rnd) {
  917. h264chroma->put_h264_chroma_pixels_tab[1](s->dest[1] + off, srcU, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
  918. h264chroma->put_h264_chroma_pixels_tab[1](s->dest[2] + off, srcV, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
  919. } else {
  920. v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[1](s->dest[1] + off, srcU, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
  921. v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[1](s->dest[2] + off, srcV, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
  922. }
  923. }
  924. }
  925. }
  926. /** Motion compensation for direct or interpolated blocks in B-frames
  927. */
  928. void ff_vc1_interp_mc(VC1Context *v)
  929. {
  930. MpegEncContext *s = &v->s;
  931. H264ChromaContext *h264chroma = &v->h264chroma;
  932. uint8_t *srcY, *srcU, *srcV;
  933. int dxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
  934. int v_edge_pos = s->v_edge_pos >> v->field_mode;
  935. int use_ic = v->next_use_ic;
  936. int interlace;
  937. int linesize, uvlinesize;
  938. if (!v->field_mode && !v->s.next_picture.f->data[0])
  939. return;
  940. linesize = s->current_picture_ptr->f->linesize[0];
  941. uvlinesize = s->current_picture_ptr->f->linesize[1];
  942. mx = s->mv[1][0][0];
  943. my = s->mv[1][0][1];
  944. uvmx = (mx + ((mx & 3) == 3)) >> 1;
  945. uvmy = (my + ((my & 3) == 3)) >> 1;
  946. if (v->field_mode && v->cur_field_type != v->ref_field_type[1]) {
  947. my = my - 2 + 4 * v->cur_field_type;
  948. uvmy = uvmy - 2 + 4 * v->cur_field_type;
  949. }
  950. if (v->fastuvmc) {
  951. uvmx = uvmx + ((uvmx < 0) ? -(uvmx & 1) : (uvmx & 1));
  952. uvmy = uvmy + ((uvmy < 0) ? -(uvmy & 1) : (uvmy & 1));
  953. }
  954. srcY = s->next_picture.f->data[0];
  955. srcU = s->next_picture.f->data[1];
  956. srcV = s->next_picture.f->data[2];
  957. interlace = s->next_picture.f->interlaced_frame;
  958. src_x = s->mb_x * 16 + (mx >> 2);
  959. src_y = s->mb_y * 16 + (my >> 2);
  960. uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
  961. uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
  962. if (v->profile != PROFILE_ADVANCED) {
  963. src_x = av_clip( src_x, -16, s->mb_width * 16);
  964. src_y = av_clip( src_y, -16, s->mb_height * 16);
  965. uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
  966. uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
  967. } else {
  968. src_x = av_clip( src_x, -17, s->avctx->coded_width);
  969. uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
  970. if (v->fcm == ILACE_FRAME) {
  971. src_y = av_clip(src_y, -18 + (src_y & 1), s->avctx->coded_height + (src_y & 1));
  972. uvsrc_y = av_clip(uvsrc_y, -8 + (uvsrc_y & 1), (s->avctx->coded_height >> 1) + (uvsrc_y & 1));
  973. } else {
  974. src_y = av_clip(src_y, -18, s->avctx->coded_height + 1);
  975. uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
  976. }
  977. }
  978. srcY += src_y * s->linesize + src_x;
  979. srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
  980. srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
  981. if (v->field_mode && v->ref_field_type[1]) {
  982. srcY += linesize;
  983. srcU += uvlinesize;
  984. srcV += uvlinesize;
  985. }
  986. /* for grayscale we should not try to read from unknown area */
  987. if (CONFIG_GRAY && s->avctx->flags & AV_CODEC_FLAG_GRAY) {
  988. srcU = s->sc.edge_emu_buffer + 18 * s->linesize;
  989. srcV = s->sc.edge_emu_buffer + 18 * s->linesize;
  990. }
  991. if (v->rangeredfrm || s->h_edge_pos < 22 || v_edge_pos < 22 || use_ic
  992. || (unsigned)(src_x - 1) > s->h_edge_pos - (mx & 3) - 16 - 3
  993. || (unsigned)(src_y - 1) > v_edge_pos - (my & 3) - 16 - 3) {
  994. uint8_t *ubuf = s->sc.edge_emu_buffer + 19 * s->linesize;
  995. uint8_t *vbuf = ubuf + 9 * s->uvlinesize;
  996. const int k = 17 + s->mspel * 2;
  997. srcY -= s->mspel * (1 + s->linesize);
  998. if (interlace) {
  999. s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer,
  1000. srcY,
  1001. linesize << 1,
  1002. linesize << 1,
  1003. k,
  1004. v->field_mode ? k : (k + 1 >> 1),
  1005. src_x - s->mspel,
  1006. src_y - s->mspel >> !v->field_mode,
  1007. s->h_edge_pos,
  1008. s->v_edge_pos >> 1);
  1009. if (!v->field_mode)
  1010. s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer + linesize,
  1011. srcY + linesize,
  1012. linesize << 1,
  1013. linesize << 1,
  1014. k,
  1015. k >> 1,
  1016. src_x - s->mspel,
  1017. src_y - s->mspel + 1 >> 1,
  1018. s->h_edge_pos,
  1019. s->v_edge_pos >> 1);
  1020. } else
  1021. s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer,
  1022. srcY,
  1023. linesize,
  1024. linesize,
  1025. k,
  1026. v->field_mode ? (k << 1) - 1 : k,
  1027. src_x - s->mspel,
  1028. v->field_mode ? 2 * (src_y - s->mspel) + v->ref_field_type[1] :
  1029. src_y - s->mspel,
  1030. s->h_edge_pos,
  1031. s->v_edge_pos);
  1032. srcY = s->sc.edge_emu_buffer;
  1033. if (interlace) {
  1034. s->vdsp.emulated_edge_mc(ubuf,
  1035. srcU,
  1036. uvlinesize << 1,
  1037. uvlinesize << 1,
  1038. 9,
  1039. v->field_mode ? 9 : 5,
  1040. uvsrc_x,
  1041. uvsrc_y >> !v->field_mode,
  1042. s->h_edge_pos >> 1,
  1043. s->v_edge_pos >> 2);
  1044. s->vdsp.emulated_edge_mc(vbuf,
  1045. srcV,
  1046. uvlinesize << 1,
  1047. uvlinesize << 1,
  1048. 9,
  1049. v->field_mode ? 9 : 5,
  1050. uvsrc_x,
  1051. uvsrc_y >> !v->field_mode,
  1052. s->h_edge_pos >> 1,
  1053. s->v_edge_pos >> 2);
  1054. if (!v->field_mode) {
  1055. s->vdsp.emulated_edge_mc(ubuf + uvlinesize,
  1056. srcU + uvlinesize,
  1057. uvlinesize << 1,
  1058. uvlinesize << 1,
  1059. 9,
  1060. 4,
  1061. uvsrc_x,
  1062. uvsrc_y + 1 >> 1,
  1063. s->h_edge_pos >> 1,
  1064. s->v_edge_pos >> 2);
  1065. s->vdsp.emulated_edge_mc(vbuf + uvlinesize,
  1066. srcV + uvlinesize,
  1067. uvlinesize << 1,
  1068. uvlinesize << 1,
  1069. 9,
  1070. 4,
  1071. uvsrc_x,
  1072. uvsrc_y + 1 >> 1,
  1073. s->h_edge_pos >> 1,
  1074. s->v_edge_pos >> 2);
  1075. }
  1076. } else {
  1077. s->vdsp.emulated_edge_mc(ubuf,
  1078. srcU,
  1079. uvlinesize,
  1080. uvlinesize,
  1081. 9,
  1082. v->field_mode ? 17 : 9,
  1083. uvsrc_x,
  1084. v->field_mode ? 2 * uvsrc_y + v->ref_field_type[1] : uvsrc_y,
  1085. s->h_edge_pos >> 1,
  1086. s->v_edge_pos >> 1);
  1087. s->vdsp.emulated_edge_mc(vbuf,
  1088. srcV,
  1089. uvlinesize,
  1090. uvlinesize,
  1091. 9,
  1092. v->field_mode ? 17 : 9,
  1093. uvsrc_x,
  1094. v->field_mode ? 2 * uvsrc_y + v->ref_field_type[1] : uvsrc_y,
  1095. s->h_edge_pos >> 1,
  1096. s->v_edge_pos >> 1);
  1097. }
  1098. srcU = ubuf;
  1099. srcV = vbuf;
  1100. /* if we deal with range reduction we need to scale source blocks */
  1101. if (v->rangeredfrm) {
  1102. vc1_scale_luma(srcY, k, s->linesize);
  1103. vc1_scale_chroma(srcU, srcV, 9, s->uvlinesize);
  1104. }
  1105. if (use_ic) {
  1106. uint8_t (*luty )[256] = v->next_luty;
  1107. uint8_t (*lutuv)[256] = v->next_lutuv;
  1108. vc1_lut_scale_luma(srcY,
  1109. luty[v->field_mode ? v->ref_field_type[1] : ((0+src_y - s->mspel) & 1)],
  1110. luty[v->field_mode ? v->ref_field_type[1] : ((1+src_y - s->mspel) & 1)],
  1111. k, s->linesize);
  1112. vc1_lut_scale_chroma(srcU, srcV,
  1113. lutuv[v->field_mode ? v->ref_field_type[1] : ((0+uvsrc_y) & 1)],
  1114. lutuv[v->field_mode ? v->ref_field_type[1] : ((1+uvsrc_y) & 1)],
  1115. 9, s->uvlinesize);
  1116. }
  1117. srcY += s->mspel * (1 + s->linesize);
  1118. }
  1119. if (s->mspel) {
  1120. dxy = ((my & 3) << 2) | (mx & 3);
  1121. v->vc1dsp.avg_vc1_mspel_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, v->rnd);
  1122. } else { // hpel mc
  1123. dxy = (my & 2) | ((mx & 2) >> 1);
  1124. if (!v->rnd)
  1125. s->hdsp.avg_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
  1126. else
  1127. s->hdsp.avg_no_rnd_pixels_tab[dxy](s->dest[0], srcY, s->linesize, 16);
  1128. }
  1129. if (CONFIG_GRAY && s->avctx->flags & AV_CODEC_FLAG_GRAY)
  1130. return;
  1131. /* Chroma MC always uses qpel bilinear */
  1132. uvmx = (uvmx & 3) << 1;
  1133. uvmy = (uvmy & 3) << 1;
  1134. if (!v->rnd) {
  1135. h264chroma->avg_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
  1136. h264chroma->avg_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
  1137. } else {
  1138. v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
  1139. v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
  1140. }
  1141. }