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.

950 lines
36KB

  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 Libav.
  8. *
  9. * Libav 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. * Libav 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 Libav; 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. /** Do motion compensation over 1 macroblock
  33. * Mostly adapted hpel_motion and qpel_motion from mpegvideo.c
  34. */
  35. void ff_vc1_mc_1mv(VC1Context *v, int dir)
  36. {
  37. MpegEncContext *s = &v->s;
  38. H264ChromaContext *h264chroma = &v->h264chroma;
  39. uint8_t *srcY, *srcU, *srcV;
  40. int dxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
  41. int v_edge_pos = s->v_edge_pos >> v->field_mode;
  42. int i;
  43. uint8_t (*luty)[256], (*lutuv)[256];
  44. int use_ic;
  45. if ((!v->field_mode ||
  46. (v->ref_field_type[dir] == 1 && v->cur_field_type == 1)) &&
  47. !v->s.last_picture.f->data[0])
  48. return;
  49. mx = s->mv[dir][0][0];
  50. my = s->mv[dir][0][1];
  51. // store motion vectors for further use in B-frames
  52. if (s->pict_type == AV_PICTURE_TYPE_P) {
  53. for (i = 0; i < 4; i++) {
  54. s->current_picture.motion_val[1][s->block_index[i] + v->blocks_off][0] = mx;
  55. s->current_picture.motion_val[1][s->block_index[i] + v->blocks_off][1] = my;
  56. }
  57. }
  58. uvmx = (mx + ((mx & 3) == 3)) >> 1;
  59. uvmy = (my + ((my & 3) == 3)) >> 1;
  60. v->luma_mv[s->mb_x][0] = uvmx;
  61. v->luma_mv[s->mb_x][1] = uvmy;
  62. if (v->field_mode &&
  63. v->cur_field_type != v->ref_field_type[dir]) {
  64. my = my - 2 + 4 * v->cur_field_type;
  65. uvmy = uvmy - 2 + 4 * v->cur_field_type;
  66. }
  67. // fastuvmc shall be ignored for interlaced frame picture
  68. if (v->fastuvmc && (v->fcm != ILACE_FRAME)) {
  69. uvmx = uvmx + ((uvmx < 0) ? (uvmx & 1) : -(uvmx & 1));
  70. uvmy = uvmy + ((uvmy < 0) ? (uvmy & 1) : -(uvmy & 1));
  71. }
  72. if (!dir) {
  73. if (v->field_mode && (v->cur_field_type != v->ref_field_type[dir]) && v->second_field) {
  74. srcY = s->current_picture.f->data[0];
  75. srcU = s->current_picture.f->data[1];
  76. srcV = s->current_picture.f->data[2];
  77. luty = v->curr_luty;
  78. lutuv = v->curr_lutuv;
  79. use_ic = v->curr_use_ic;
  80. } else {
  81. srcY = s->last_picture.f->data[0];
  82. srcU = s->last_picture.f->data[1];
  83. srcV = s->last_picture.f->data[2];
  84. luty = v->last_luty;
  85. lutuv = v->last_lutuv;
  86. use_ic = v->last_use_ic;
  87. }
  88. } else {
  89. srcY = s->next_picture.f->data[0];
  90. srcU = s->next_picture.f->data[1];
  91. srcV = s->next_picture.f->data[2];
  92. luty = v->next_luty;
  93. lutuv = v->next_lutuv;
  94. use_ic = v->next_use_ic;
  95. }
  96. if (!srcY || !srcU) {
  97. av_log(v->s.avctx, AV_LOG_ERROR, "Referenced frame missing.\n");
  98. return;
  99. }
  100. src_x = s->mb_x * 16 + (mx >> 2);
  101. src_y = s->mb_y * 16 + (my >> 2);
  102. uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
  103. uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
  104. if (v->profile != PROFILE_ADVANCED) {
  105. src_x = av_clip( src_x, -16, s->mb_width * 16);
  106. src_y = av_clip( src_y, -16, s->mb_height * 16);
  107. uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
  108. uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
  109. } else {
  110. src_x = av_clip( src_x, -17, s->avctx->coded_width);
  111. src_y = av_clip( src_y, -18, s->avctx->coded_height + 1);
  112. uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
  113. uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
  114. }
  115. srcY += src_y * s->linesize + src_x;
  116. srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
  117. srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
  118. if (v->field_mode && v->ref_field_type[dir]) {
  119. srcY += s->current_picture_ptr->f->linesize[0];
  120. srcU += s->current_picture_ptr->f->linesize[1];
  121. srcV += s->current_picture_ptr->f->linesize[2];
  122. }
  123. /* for grayscale we should not try to read from unknown area */
  124. if (s->avctx->flags & AV_CODEC_FLAG_GRAY) {
  125. srcU = s->sc.edge_emu_buffer + 18 * s->linesize;
  126. srcV = s->sc.edge_emu_buffer + 18 * s->linesize;
  127. }
  128. if (v->rangeredfrm || use_ic
  129. || s->h_edge_pos < 22 || v_edge_pos < 22
  130. || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx&3) - 16 - s->mspel * 3
  131. || (unsigned)(src_y - 1) > v_edge_pos - (my&3) - 16 - 3) {
  132. uint8_t *uvbuf = s->sc.edge_emu_buffer + 19 * s->linesize;
  133. srcY -= s->mspel * (1 + s->linesize);
  134. s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer, srcY,
  135. s->linesize, s->linesize,
  136. 17 + s->mspel * 2, 17 + s->mspel * 2,
  137. src_x - s->mspel, src_y - s->mspel,
  138. s->h_edge_pos, v_edge_pos);
  139. srcY = s->sc.edge_emu_buffer;
  140. s->vdsp.emulated_edge_mc(uvbuf, srcU,
  141. s->uvlinesize, s->uvlinesize,
  142. 8 + 1, 8 + 1,
  143. uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
  144. s->vdsp.emulated_edge_mc(uvbuf + 16, srcV,
  145. s->uvlinesize, s->uvlinesize,
  146. 8 + 1, 8 + 1,
  147. uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
  148. srcU = uvbuf;
  149. srcV = uvbuf + 16;
  150. /* if we deal with range reduction we need to scale source blocks */
  151. if (v->rangeredfrm) {
  152. int i, j;
  153. uint8_t *src, *src2;
  154. src = srcY;
  155. for (j = 0; j < 17 + s->mspel * 2; j++) {
  156. for (i = 0; i < 17 + s->mspel * 2; i++)
  157. src[i] = ((src[i] - 128) >> 1) + 128;
  158. src += s->linesize;
  159. }
  160. src = srcU;
  161. src2 = srcV;
  162. for (j = 0; j < 9; j++) {
  163. for (i = 0; i < 9; i++) {
  164. src[i] = ((src[i] - 128) >> 1) + 128;
  165. src2[i] = ((src2[i] - 128) >> 1) + 128;
  166. }
  167. src += s->uvlinesize;
  168. src2 += s->uvlinesize;
  169. }
  170. }
  171. /* if we deal with intensity compensation we need to scale source blocks */
  172. if (use_ic) {
  173. int i, j;
  174. uint8_t *src, *src2;
  175. src = srcY;
  176. for (j = 0; j < 17 + s->mspel * 2; j++) {
  177. int f = v->field_mode ? v->ref_field_type[dir] : ((j + src_y - s->mspel) & 1) ;
  178. for (i = 0; i < 17 + s->mspel * 2; i++)
  179. src[i] = luty[f][src[i]];
  180. src += s->linesize;
  181. }
  182. src = srcU;
  183. src2 = srcV;
  184. for (j = 0; j < 9; j++) {
  185. int f = v->field_mode ? v->ref_field_type[dir] : ((j + uvsrc_y) & 1);
  186. for (i = 0; i < 9; i++) {
  187. src[i] = lutuv[f][src[i]];
  188. src2[i] = lutuv[f][src2[i]];
  189. }
  190. src += s->uvlinesize;
  191. src2 += s->uvlinesize;
  192. }
  193. }
  194. srcY += s->mspel * (1 + s->linesize);
  195. }
  196. if (s->mspel) {
  197. dxy = ((my & 3) << 2) | (mx & 3);
  198. v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] , srcY , s->linesize, v->rnd);
  199. v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8, srcY + 8, s->linesize, v->rnd);
  200. srcY += s->linesize * 8;
  201. v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8 * s->linesize , srcY , s->linesize, v->rnd);
  202. v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8 * s->linesize + 8, srcY + 8, s->linesize, v->rnd);
  203. } else { // hpel mc - always used for luma
  204. dxy = (my & 2) | ((mx & 2) >> 1);
  205. if (!v->rnd)
  206. s->hdsp.put_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
  207. else
  208. s->hdsp.put_no_rnd_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
  209. }
  210. if (s->avctx->flags & AV_CODEC_FLAG_GRAY)
  211. return;
  212. /* Chroma MC always uses qpel bilinear */
  213. uvmx = (uvmx & 3) << 1;
  214. uvmy = (uvmy & 3) << 1;
  215. if (!v->rnd) {
  216. h264chroma->put_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
  217. h264chroma->put_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
  218. } else {
  219. v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
  220. v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
  221. }
  222. }
  223. static inline int median4(int a, int b, int c, int d)
  224. {
  225. if (a < b) {
  226. if (c < d) return (FFMIN(b, d) + FFMAX(a, c)) / 2;
  227. else return (FFMIN(b, c) + FFMAX(a, d)) / 2;
  228. } else {
  229. if (c < d) return (FFMIN(a, d) + FFMAX(b, c)) / 2;
  230. else return (FFMIN(a, c) + FFMAX(b, d)) / 2;
  231. }
  232. }
  233. /** Do motion compensation for 4-MV macroblock - luminance block
  234. */
  235. void ff_vc1_mc_4mv_luma(VC1Context *v, int n, int dir, int avg)
  236. {
  237. MpegEncContext *s = &v->s;
  238. uint8_t *srcY;
  239. int dxy, mx, my, src_x, src_y;
  240. int off;
  241. int fieldmv = (v->fcm == ILACE_FRAME) ? v->blk_mv_type[s->block_index[n]] : 0;
  242. int v_edge_pos = s->v_edge_pos >> v->field_mode;
  243. uint8_t (*luty)[256];
  244. int use_ic;
  245. if ((!v->field_mode ||
  246. (v->ref_field_type[dir] == 1 && v->cur_field_type == 1)) &&
  247. !v->s.last_picture.f->data[0])
  248. return;
  249. mx = s->mv[dir][n][0];
  250. my = s->mv[dir][n][1];
  251. if (!dir) {
  252. if (v->field_mode && (v->cur_field_type != v->ref_field_type[dir]) && v->second_field) {
  253. srcY = s->current_picture.f->data[0];
  254. luty = v->curr_luty;
  255. use_ic = v->curr_use_ic;
  256. } else {
  257. srcY = s->last_picture.f->data[0];
  258. luty = v->last_luty;
  259. use_ic = v->last_use_ic;
  260. }
  261. } else {
  262. srcY = s->next_picture.f->data[0];
  263. luty = v->next_luty;
  264. use_ic = v->next_use_ic;
  265. }
  266. if (!srcY) {
  267. av_log(v->s.avctx, AV_LOG_ERROR, "Referenced frame missing.\n");
  268. return;
  269. }
  270. if (v->field_mode) {
  271. if (v->cur_field_type != v->ref_field_type[dir])
  272. my = my - 2 + 4 * v->cur_field_type;
  273. }
  274. if (s->pict_type == AV_PICTURE_TYPE_P && n == 3 && v->field_mode) {
  275. int same_count = 0, opp_count = 0, k;
  276. int chosen_mv[2][4][2], f;
  277. int tx = 0, ty = 0;
  278. for (k = 0; k < 4; k++) {
  279. f = v->mv_f[0][s->block_index[k] + v->blocks_off];
  280. chosen_mv[f][f ? opp_count : same_count][0] = s->mv[0][k][0];
  281. chosen_mv[f][f ? opp_count : same_count][1] = s->mv[0][k][1];
  282. opp_count += f;
  283. same_count += 1 - f;
  284. }
  285. f = opp_count > same_count;
  286. switch (f ? opp_count : same_count) {
  287. case 4:
  288. tx = median4(chosen_mv[f][0][0], chosen_mv[f][1][0],
  289. chosen_mv[f][2][0], chosen_mv[f][3][0]);
  290. ty = median4(chosen_mv[f][0][1], chosen_mv[f][1][1],
  291. chosen_mv[f][2][1], chosen_mv[f][3][1]);
  292. break;
  293. case 3:
  294. tx = mid_pred(chosen_mv[f][0][0], chosen_mv[f][1][0], chosen_mv[f][2][0]);
  295. ty = mid_pred(chosen_mv[f][0][1], chosen_mv[f][1][1], chosen_mv[f][2][1]);
  296. break;
  297. case 2:
  298. tx = (chosen_mv[f][0][0] + chosen_mv[f][1][0]) / 2;
  299. ty = (chosen_mv[f][0][1] + chosen_mv[f][1][1]) / 2;
  300. break;
  301. }
  302. s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = tx;
  303. s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = ty;
  304. for (k = 0; k < 4; k++)
  305. v->mv_f[1][s->block_index[k] + v->blocks_off] = f;
  306. }
  307. if (v->fcm == ILACE_FRAME) { // not sure if needed for other types of picture
  308. int qx, qy;
  309. int width = s->avctx->coded_width;
  310. int height = s->avctx->coded_height >> 1;
  311. if (s->pict_type == AV_PICTURE_TYPE_P) {
  312. s->current_picture.motion_val[1][s->block_index[n] + v->blocks_off][0] = mx;
  313. s->current_picture.motion_val[1][s->block_index[n] + v->blocks_off][1] = my;
  314. }
  315. qx = (s->mb_x * 16) + (mx >> 2);
  316. qy = (s->mb_y * 8) + (my >> 3);
  317. if (qx < -17)
  318. mx -= 4 * (qx + 17);
  319. else if (qx > width)
  320. mx -= 4 * (qx - width);
  321. if (qy < -18)
  322. my -= 8 * (qy + 18);
  323. else if (qy > height + 1)
  324. my -= 8 * (qy - height - 1);
  325. }
  326. if ((v->fcm == ILACE_FRAME) && fieldmv)
  327. off = ((n > 1) ? s->linesize : 0) + (n & 1) * 8;
  328. else
  329. off = s->linesize * 4 * (n & 2) + (n & 1) * 8;
  330. src_x = s->mb_x * 16 + (n & 1) * 8 + (mx >> 2);
  331. if (!fieldmv)
  332. src_y = s->mb_y * 16 + (n & 2) * 4 + (my >> 2);
  333. else
  334. src_y = s->mb_y * 16 + ((n > 1) ? 1 : 0) + (my >> 2);
  335. if (v->profile != PROFILE_ADVANCED) {
  336. src_x = av_clip(src_x, -16, s->mb_width * 16);
  337. src_y = av_clip(src_y, -16, s->mb_height * 16);
  338. } else {
  339. src_x = av_clip(src_x, -17, s->avctx->coded_width);
  340. if (v->fcm == ILACE_FRAME) {
  341. if (src_y & 1)
  342. src_y = av_clip(src_y, -17, s->avctx->coded_height + 1);
  343. else
  344. src_y = av_clip(src_y, -18, s->avctx->coded_height);
  345. } else {
  346. src_y = av_clip(src_y, -18, s->avctx->coded_height + 1);
  347. }
  348. }
  349. srcY += src_y * s->linesize + src_x;
  350. if (v->field_mode && v->ref_field_type[dir])
  351. srcY += s->current_picture_ptr->f->linesize[0];
  352. if (fieldmv && !(src_y & 1))
  353. v_edge_pos--;
  354. if (fieldmv && (src_y & 1) && src_y < 4)
  355. src_y--;
  356. if (v->rangeredfrm || use_ic
  357. || s->h_edge_pos < 13 || v_edge_pos < 23
  358. || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx & 3) - 8 - s->mspel * 2
  359. || (unsigned)(src_y - (s->mspel << fieldmv)) > v_edge_pos - (my & 3) - ((8 + s->mspel * 2) << fieldmv)) {
  360. srcY -= s->mspel * (1 + (s->linesize << fieldmv));
  361. /* check emulate edge stride and offset */
  362. s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer, srcY,
  363. s->linesize, s->linesize,
  364. 9 + s->mspel * 2, (9 + s->mspel * 2) << fieldmv,
  365. src_x - s->mspel, src_y - (s->mspel << fieldmv),
  366. s->h_edge_pos, v_edge_pos);
  367. srcY = s->sc.edge_emu_buffer;
  368. /* if we deal with range reduction we need to scale source blocks */
  369. if (v->rangeredfrm) {
  370. int i, j;
  371. uint8_t *src;
  372. src = srcY;
  373. for (j = 0; j < 9 + s->mspel * 2; j++) {
  374. for (i = 0; i < 9 + s->mspel * 2; i++)
  375. src[i] = ((src[i] - 128) >> 1) + 128;
  376. src += s->linesize << fieldmv;
  377. }
  378. }
  379. /* if we deal with intensity compensation we need to scale source blocks */
  380. if (use_ic) {
  381. int i, j;
  382. uint8_t *src;
  383. src = srcY;
  384. for (j = 0; j < 9 + s->mspel * 2; j++) {
  385. int f = v->field_mode ? v->ref_field_type[dir] : (((j<<fieldmv)+src_y - (s->mspel << fieldmv)) & 1);
  386. for (i = 0; i < 9 + s->mspel * 2; i++)
  387. src[i] = luty[f][src[i]];
  388. src += s->linesize << fieldmv;
  389. }
  390. }
  391. srcY += s->mspel * (1 + (s->linesize << fieldmv));
  392. }
  393. if (s->mspel) {
  394. dxy = ((my & 3) << 2) | (mx & 3);
  395. if (avg)
  396. v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off, srcY, s->linesize << fieldmv, v->rnd);
  397. else
  398. v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off, srcY, s->linesize << fieldmv, v->rnd);
  399. } else { // hpel mc - always used for luma
  400. dxy = (my & 2) | ((mx & 2) >> 1);
  401. if (!v->rnd)
  402. s->hdsp.put_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
  403. else
  404. s->hdsp.put_no_rnd_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
  405. }
  406. }
  407. static av_always_inline int get_chroma_mv(int *mvx, int *mvy, int *a, int flag, int *tx, int *ty)
  408. {
  409. int idx, i;
  410. static const int count[16] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4};
  411. idx = ((a[3] != flag) << 3)
  412. | ((a[2] != flag) << 2)
  413. | ((a[1] != flag) << 1)
  414. | (a[0] != flag);
  415. if (!idx) {
  416. *tx = median4(mvx[0], mvx[1], mvx[2], mvx[3]);
  417. *ty = median4(mvy[0], mvy[1], mvy[2], mvy[3]);
  418. return 4;
  419. } else if (count[idx] == 1) {
  420. switch (idx) {
  421. case 0x1:
  422. *tx = mid_pred(mvx[1], mvx[2], mvx[3]);
  423. *ty = mid_pred(mvy[1], mvy[2], mvy[3]);
  424. return 3;
  425. case 0x2:
  426. *tx = mid_pred(mvx[0], mvx[2], mvx[3]);
  427. *ty = mid_pred(mvy[0], mvy[2], mvy[3]);
  428. return 3;
  429. case 0x4:
  430. *tx = mid_pred(mvx[0], mvx[1], mvx[3]);
  431. *ty = mid_pred(mvy[0], mvy[1], mvy[3]);
  432. return 3;
  433. case 0x8:
  434. *tx = mid_pred(mvx[0], mvx[1], mvx[2]);
  435. *ty = mid_pred(mvy[0], mvy[1], mvy[2]);
  436. return 3;
  437. }
  438. } else if (count[idx] == 2) {
  439. int t1 = 0, t2 = 0;
  440. for (i = 0; i < 3; i++)
  441. if (!a[i]) {
  442. t1 = i;
  443. break;
  444. }
  445. for (i = t1 + 1; i < 4; i++)
  446. if (!a[i]) {
  447. t2 = i;
  448. break;
  449. }
  450. *tx = (mvx[t1] + mvx[t2]) / 2;
  451. *ty = (mvy[t1] + mvy[t2]) / 2;
  452. return 2;
  453. } else {
  454. return 0;
  455. }
  456. return -1;
  457. }
  458. /** Do motion compensation for 4-MV macroblock - both chroma blocks
  459. */
  460. void ff_vc1_mc_4mv_chroma(VC1Context *v, int dir)
  461. {
  462. MpegEncContext *s = &v->s;
  463. H264ChromaContext *h264chroma = &v->h264chroma;
  464. uint8_t *srcU, *srcV;
  465. int uvmx, uvmy, uvsrc_x, uvsrc_y;
  466. int k, tx = 0, ty = 0;
  467. int mvx[4], mvy[4], intra[4], mv_f[4];
  468. int valid_count;
  469. int chroma_ref_type = v->cur_field_type;
  470. int v_edge_pos = s->v_edge_pos >> v->field_mode;
  471. uint8_t (*lutuv)[256];
  472. int use_ic;
  473. if (!v->field_mode && !v->s.last_picture.f->data[0])
  474. return;
  475. if (s->avctx->flags & AV_CODEC_FLAG_GRAY)
  476. return;
  477. for (k = 0; k < 4; k++) {
  478. mvx[k] = s->mv[dir][k][0];
  479. mvy[k] = s->mv[dir][k][1];
  480. intra[k] = v->mb_type[0][s->block_index[k]];
  481. if (v->field_mode)
  482. mv_f[k] = v->mv_f[dir][s->block_index[k] + v->blocks_off];
  483. }
  484. /* calculate chroma MV vector from four luma MVs */
  485. if (!v->field_mode || (v->field_mode && !v->numref)) {
  486. valid_count = get_chroma_mv(mvx, mvy, intra, 0, &tx, &ty);
  487. chroma_ref_type = v->reffield;
  488. if (!valid_count) {
  489. s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
  490. s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
  491. v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
  492. return; //no need to do MC for intra blocks
  493. }
  494. } else {
  495. int dominant = 0;
  496. if (mv_f[0] + mv_f[1] + mv_f[2] + mv_f[3] > 2)
  497. dominant = 1;
  498. valid_count = get_chroma_mv(mvx, mvy, mv_f, dominant, &tx, &ty);
  499. if (dominant)
  500. chroma_ref_type = !v->cur_field_type;
  501. }
  502. if (v->field_mode && chroma_ref_type == 1 && v->cur_field_type == 1 && !v->s.last_picture.f->data[0])
  503. return;
  504. s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = tx;
  505. s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = ty;
  506. uvmx = (tx + ((tx & 3) == 3)) >> 1;
  507. uvmy = (ty + ((ty & 3) == 3)) >> 1;
  508. v->luma_mv[s->mb_x][0] = uvmx;
  509. v->luma_mv[s->mb_x][1] = uvmy;
  510. if (v->fastuvmc) {
  511. uvmx = uvmx + ((uvmx < 0) ? (uvmx & 1) : -(uvmx & 1));
  512. uvmy = uvmy + ((uvmy < 0) ? (uvmy & 1) : -(uvmy & 1));
  513. }
  514. // Field conversion bias
  515. if (v->cur_field_type != chroma_ref_type)
  516. uvmy += 2 - 4 * chroma_ref_type;
  517. uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
  518. uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
  519. if (v->profile != PROFILE_ADVANCED) {
  520. uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
  521. uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
  522. } else {
  523. uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
  524. uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
  525. }
  526. if (!dir) {
  527. if (v->field_mode && (v->cur_field_type != chroma_ref_type) && v->second_field) {
  528. srcU = s->current_picture.f->data[1];
  529. srcV = s->current_picture.f->data[2];
  530. lutuv = v->curr_lutuv;
  531. use_ic = v->curr_use_ic;
  532. } else {
  533. srcU = s->last_picture.f->data[1];
  534. srcV = s->last_picture.f->data[2];
  535. lutuv = v->last_lutuv;
  536. use_ic = v->last_use_ic;
  537. }
  538. } else {
  539. srcU = s->next_picture.f->data[1];
  540. srcV = s->next_picture.f->data[2];
  541. lutuv = v->next_lutuv;
  542. use_ic = v->next_use_ic;
  543. }
  544. if (!srcU) {
  545. av_log(v->s.avctx, AV_LOG_ERROR, "Referenced frame missing.\n");
  546. return;
  547. }
  548. srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
  549. srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
  550. if (v->field_mode) {
  551. if (chroma_ref_type) {
  552. srcU += s->current_picture_ptr->f->linesize[1];
  553. srcV += s->current_picture_ptr->f->linesize[2];
  554. }
  555. }
  556. if (v->rangeredfrm || use_ic
  557. || s->h_edge_pos < 18 || v_edge_pos < 18
  558. || (unsigned)uvsrc_x > (s->h_edge_pos >> 1) - 9
  559. || (unsigned)uvsrc_y > (v_edge_pos >> 1) - 9) {
  560. s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer, srcU,
  561. s->uvlinesize, s->uvlinesize,
  562. 8 + 1, 8 + 1, uvsrc_x, uvsrc_y,
  563. s->h_edge_pos >> 1, v_edge_pos >> 1);
  564. s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer + 16, srcV,
  565. s->uvlinesize, s->uvlinesize,
  566. 8 + 1, 8 + 1, uvsrc_x, uvsrc_y,
  567. s->h_edge_pos >> 1, v_edge_pos >> 1);
  568. srcU = s->sc.edge_emu_buffer;
  569. srcV = s->sc.edge_emu_buffer + 16;
  570. /* if we deal with range reduction we need to scale source blocks */
  571. if (v->rangeredfrm) {
  572. int i, j;
  573. uint8_t *src, *src2;
  574. src = srcU;
  575. src2 = srcV;
  576. for (j = 0; j < 9; j++) {
  577. for (i = 0; i < 9; i++) {
  578. src[i] = ((src[i] - 128) >> 1) + 128;
  579. src2[i] = ((src2[i] - 128) >> 1) + 128;
  580. }
  581. src += s->uvlinesize;
  582. src2 += s->uvlinesize;
  583. }
  584. }
  585. /* if we deal with intensity compensation we need to scale source blocks */
  586. if (use_ic) {
  587. int i, j;
  588. uint8_t *src, *src2;
  589. src = srcU;
  590. src2 = srcV;
  591. for (j = 0; j < 9; j++) {
  592. int f = v->field_mode ? chroma_ref_type : ((j + uvsrc_y) & 1);
  593. for (i = 0; i < 9; i++) {
  594. src[i] = lutuv[f][src[i]];
  595. src2[i] = lutuv[f][src2[i]];
  596. }
  597. src += s->uvlinesize;
  598. src2 += s->uvlinesize;
  599. }
  600. }
  601. }
  602. /* Chroma MC always uses qpel bilinear */
  603. uvmx = (uvmx & 3) << 1;
  604. uvmy = (uvmy & 3) << 1;
  605. if (!v->rnd) {
  606. h264chroma->put_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
  607. h264chroma->put_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
  608. } else {
  609. v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
  610. v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
  611. }
  612. }
  613. /** Do motion compensation for 4-MV interlaced frame chroma macroblock (both U and V)
  614. */
  615. void ff_vc1_mc_4mv_chroma4(VC1Context *v, int dir, int dir2, int avg)
  616. {
  617. MpegEncContext *s = &v->s;
  618. H264ChromaContext *h264chroma = &v->h264chroma;
  619. uint8_t *srcU, *srcV;
  620. int uvsrc_x, uvsrc_y;
  621. int uvmx_field[4], uvmy_field[4];
  622. int i, off, tx, ty;
  623. int fieldmv = v->blk_mv_type[s->block_index[0]];
  624. static const int s_rndtblfield[16] = { 0, 0, 1, 2, 4, 4, 5, 6, 2, 2, 3, 8, 6, 6, 7, 12 };
  625. int v_dist = fieldmv ? 1 : 4; // vertical offset for lower sub-blocks
  626. int v_edge_pos = s->v_edge_pos >> 1;
  627. int use_ic;
  628. uint8_t (*lutuv)[256];
  629. if (s->avctx->flags & AV_CODEC_FLAG_GRAY)
  630. return;
  631. for (i = 0; i < 4; i++) {
  632. int d = i < 2 ? dir: dir2;
  633. tx = s->mv[d][i][0];
  634. uvmx_field[i] = (tx + ((tx & 3) == 3)) >> 1;
  635. ty = s->mv[d][i][1];
  636. if (fieldmv)
  637. uvmy_field[i] = (ty >> 4) * 8 + s_rndtblfield[ty & 0xF];
  638. else
  639. uvmy_field[i] = (ty + ((ty & 3) == 3)) >> 1;
  640. }
  641. for (i = 0; i < 4; i++) {
  642. off = (i & 1) * 4 + ((i & 2) ? v_dist * s->uvlinesize : 0);
  643. uvsrc_x = s->mb_x * 8 + (i & 1) * 4 + (uvmx_field[i] >> 2);
  644. uvsrc_y = s->mb_y * 8 + ((i & 2) ? v_dist : 0) + (uvmy_field[i] >> 2);
  645. // FIXME: implement proper pull-back (see vc1cropmv.c, vc1CROPMV_ChromaPullBack())
  646. uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
  647. uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
  648. if (i < 2 ? dir : dir2) {
  649. srcU = s->next_picture.f->data[1] + uvsrc_y * s->uvlinesize + uvsrc_x;
  650. srcV = s->next_picture.f->data[2] + uvsrc_y * s->uvlinesize + uvsrc_x;
  651. lutuv = v->next_lutuv;
  652. use_ic = v->next_use_ic;
  653. } else {
  654. srcU = s->last_picture.f->data[1] + uvsrc_y * s->uvlinesize + uvsrc_x;
  655. srcV = s->last_picture.f->data[2] + uvsrc_y * s->uvlinesize + uvsrc_x;
  656. lutuv = v->last_lutuv;
  657. use_ic = v->last_use_ic;
  658. }
  659. uvmx_field[i] = (uvmx_field[i] & 3) << 1;
  660. uvmy_field[i] = (uvmy_field[i] & 3) << 1;
  661. if (fieldmv && !(uvsrc_y & 1))
  662. v_edge_pos--;
  663. if (fieldmv && (uvsrc_y & 1) && uvsrc_y < 2)
  664. uvsrc_y--;
  665. if (use_ic
  666. || s->h_edge_pos < 10 || v_edge_pos < (5 << fieldmv)
  667. || (unsigned)uvsrc_x > (s->h_edge_pos >> 1) - 5
  668. || (unsigned)uvsrc_y > v_edge_pos - (5 << fieldmv)) {
  669. s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer, srcU,
  670. s->uvlinesize, s->uvlinesize,
  671. 5, (5 << fieldmv), uvsrc_x, uvsrc_y,
  672. s->h_edge_pos >> 1, v_edge_pos);
  673. s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer + 16, srcV,
  674. s->uvlinesize, s->uvlinesize,
  675. 5, (5 << fieldmv), uvsrc_x, uvsrc_y,
  676. s->h_edge_pos >> 1, v_edge_pos);
  677. srcU = s->sc.edge_emu_buffer;
  678. srcV = s->sc.edge_emu_buffer + 16;
  679. /* if we deal with intensity compensation we need to scale source blocks */
  680. if (use_ic) {
  681. int i, j;
  682. uint8_t *src, *src2;
  683. src = srcU;
  684. src2 = srcV;
  685. for (j = 0; j < 5; j++) {
  686. int f = (uvsrc_y + (j << fieldmv)) & 1;
  687. for (i = 0; i < 5; i++) {
  688. src[i] = lutuv[f][src[i]];
  689. src2[i] = lutuv[f][src2[i]];
  690. }
  691. src += s->uvlinesize << fieldmv;
  692. src2 += s->uvlinesize << fieldmv;
  693. }
  694. }
  695. }
  696. if (avg) {
  697. if (!v->rnd) {
  698. h264chroma->avg_h264_chroma_pixels_tab[1](s->dest[1] + off, srcU, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
  699. h264chroma->avg_h264_chroma_pixels_tab[1](s->dest[2] + off, srcV, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
  700. } else {
  701. 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]);
  702. 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]);
  703. }
  704. } else {
  705. if (!v->rnd) {
  706. h264chroma->put_h264_chroma_pixels_tab[1](s->dest[1] + off, srcU, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
  707. h264chroma->put_h264_chroma_pixels_tab[1](s->dest[2] + off, srcV, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
  708. } else {
  709. 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]);
  710. 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]);
  711. }
  712. }
  713. }
  714. }
  715. /** Motion compensation for direct or interpolated blocks in B-frames
  716. */
  717. void ff_vc1_interp_mc(VC1Context *v)
  718. {
  719. MpegEncContext *s = &v->s;
  720. H264ChromaContext *h264chroma = &v->h264chroma;
  721. uint8_t *srcY, *srcU, *srcV;
  722. int dxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
  723. int off, off_uv;
  724. int v_edge_pos = s->v_edge_pos >> v->field_mode;
  725. int use_ic = v->next_use_ic;
  726. if (!v->field_mode && !v->s.next_picture.f->data[0])
  727. return;
  728. mx = s->mv[1][0][0];
  729. my = s->mv[1][0][1];
  730. uvmx = (mx + ((mx & 3) == 3)) >> 1;
  731. uvmy = (my + ((my & 3) == 3)) >> 1;
  732. if (v->field_mode && v->cur_field_type != v->ref_field_type[1]) {
  733. my = my - 2 + 4 * v->cur_field_type;
  734. uvmy = uvmy - 2 + 4 * v->cur_field_type;
  735. }
  736. if (v->fastuvmc) {
  737. uvmx = uvmx + ((uvmx < 0) ? -(uvmx & 1) : (uvmx & 1));
  738. uvmy = uvmy + ((uvmy < 0) ? -(uvmy & 1) : (uvmy & 1));
  739. }
  740. srcY = s->next_picture.f->data[0];
  741. srcU = s->next_picture.f->data[1];
  742. srcV = s->next_picture.f->data[2];
  743. src_x = s->mb_x * 16 + (mx >> 2);
  744. src_y = s->mb_y * 16 + (my >> 2);
  745. uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
  746. uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
  747. if (v->profile != PROFILE_ADVANCED) {
  748. src_x = av_clip( src_x, -16, s->mb_width * 16);
  749. src_y = av_clip( src_y, -16, s->mb_height * 16);
  750. uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
  751. uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
  752. } else {
  753. src_x = av_clip( src_x, -17, s->avctx->coded_width);
  754. src_y = av_clip( src_y, -18, s->avctx->coded_height + 1);
  755. uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
  756. uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
  757. }
  758. srcY += src_y * s->linesize + src_x;
  759. srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
  760. srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
  761. if (v->field_mode && v->ref_field_type[1]) {
  762. srcY += s->current_picture_ptr->f->linesize[0];
  763. srcU += s->current_picture_ptr->f->linesize[1];
  764. srcV += s->current_picture_ptr->f->linesize[2];
  765. }
  766. /* for grayscale we should not try to read from unknown area */
  767. if (s->avctx->flags & AV_CODEC_FLAG_GRAY) {
  768. srcU = s->sc.edge_emu_buffer + 18 * s->linesize;
  769. srcV = s->sc.edge_emu_buffer + 18 * s->linesize;
  770. }
  771. if (v->rangeredfrm || s->h_edge_pos < 22 || v_edge_pos < 22 || use_ic
  772. || (unsigned)(src_x - 1) > s->h_edge_pos - (mx & 3) - 16 - 3
  773. || (unsigned)(src_y - 1) > v_edge_pos - (my & 3) - 16 - 3) {
  774. uint8_t *uvbuf = s->sc.edge_emu_buffer + 19 * s->linesize;
  775. srcY -= s->mspel * (1 + s->linesize);
  776. s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer, srcY,
  777. s->linesize, s->linesize,
  778. 17 + s->mspel * 2, 17 + s->mspel * 2,
  779. src_x - s->mspel, src_y - s->mspel,
  780. s->h_edge_pos, v_edge_pos);
  781. srcY = s->sc.edge_emu_buffer;
  782. s->vdsp.emulated_edge_mc(uvbuf, srcU,
  783. s->uvlinesize, s->uvlinesize,
  784. 8 + 1, 8 + 1,
  785. uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
  786. s->vdsp.emulated_edge_mc(uvbuf + 16, srcV,
  787. s->uvlinesize, s->uvlinesize,
  788. 8 + 1, 8 + 1,
  789. uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
  790. srcU = uvbuf;
  791. srcV = uvbuf + 16;
  792. /* if we deal with range reduction we need to scale source blocks */
  793. if (v->rangeredfrm) {
  794. int i, j;
  795. uint8_t *src, *src2;
  796. src = srcY;
  797. for (j = 0; j < 17 + s->mspel * 2; j++) {
  798. for (i = 0; i < 17 + s->mspel * 2; i++)
  799. src[i] = ((src[i] - 128) >> 1) + 128;
  800. src += s->linesize;
  801. }
  802. src = srcU;
  803. src2 = srcV;
  804. for (j = 0; j < 9; j++) {
  805. for (i = 0; i < 9; i++) {
  806. src[i] = ((src[i] - 128) >> 1) + 128;
  807. src2[i] = ((src2[i] - 128) >> 1) + 128;
  808. }
  809. src += s->uvlinesize;
  810. src2 += s->uvlinesize;
  811. }
  812. }
  813. if (use_ic) {
  814. uint8_t (*luty )[256] = v->next_luty;
  815. uint8_t (*lutuv)[256] = v->next_lutuv;
  816. int i, j;
  817. uint8_t *src, *src2;
  818. src = srcY;
  819. for (j = 0; j < 17 + s->mspel * 2; j++) {
  820. int f = v->field_mode ? v->ref_field_type[1] : ((j+src_y - s->mspel) & 1);
  821. for (i = 0; i < 17 + s->mspel * 2; i++)
  822. src[i] = luty[f][src[i]];
  823. src += s->linesize;
  824. }
  825. src = srcU;
  826. src2 = srcV;
  827. for (j = 0; j < 9; j++) {
  828. int f = v->field_mode ? v->ref_field_type[1] : ((j+uvsrc_y) & 1);
  829. for (i = 0; i < 9; i++) {
  830. src[i] = lutuv[f][src[i]];
  831. src2[i] = lutuv[f][src2[i]];
  832. }
  833. src += s->uvlinesize;
  834. src2 += s->uvlinesize;
  835. }
  836. }
  837. srcY += s->mspel * (1 + s->linesize);
  838. }
  839. off = 0;
  840. off_uv = 0;
  841. if (s->mspel) {
  842. dxy = ((my & 3) << 2) | (mx & 3);
  843. v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off , srcY , s->linesize, v->rnd);
  844. v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8, srcY + 8, s->linesize, v->rnd);
  845. srcY += s->linesize * 8;
  846. v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8 * s->linesize , srcY , s->linesize, v->rnd);
  847. v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8 * s->linesize + 8, srcY + 8, s->linesize, v->rnd);
  848. } else { // hpel mc
  849. dxy = (my & 2) | ((mx & 2) >> 1);
  850. if (!v->rnd)
  851. s->hdsp.avg_pixels_tab[0][dxy](s->dest[0] + off, srcY, s->linesize, 16);
  852. else
  853. s->hdsp.avg_no_rnd_pixels_tab[dxy](s->dest[0] + off, srcY, s->linesize, 16);
  854. }
  855. if (s->avctx->flags & AV_CODEC_FLAG_GRAY)
  856. return;
  857. /* Chroma MC always uses qpel bilinear */
  858. uvmx = (uvmx & 3) << 1;
  859. uvmy = (uvmy & 3) << 1;
  860. if (!v->rnd) {
  861. h264chroma->avg_h264_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
  862. h264chroma->avg_h264_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
  863. } else {
  864. v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
  865. v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
  866. }
  867. }