PageRenderTime 61ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 2ms

/lib/ffmpeg/libavcodec/vc1dec.c

http://github.com/xbmc/xbmc
C | 5908 lines | 5091 code | 462 blank | 355 comment | 1918 complexity | 268d2bc16967f1c51a395e9e3e5266dd MD5 | raw file
Possible License(s): GPL-3.0, CC-BY-SA-3.0, LGPL-2.0, 0BSD, Unlicense, GPL-2.0, AGPL-1.0, BSD-3-Clause, LGPL-2.1, LGPL-3.0

Large files files are truncated, but you can click here to view the full file

  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 decoder
  26. */
  27. #include "internal.h"
  28. #include "avcodec.h"
  29. #include "mpegvideo.h"
  30. #include "h263.h"
  31. #include "h264chroma.h"
  32. #include "vc1.h"
  33. #include "vc1data.h"
  34. #include "vc1acdata.h"
  35. #include "msmpeg4data.h"
  36. #include "unary.h"
  37. #include "mathops.h"
  38. #include "vdpau_internal.h"
  39. #include "libavutil/avassert.h"
  40. #undef NDEBUG
  41. #include <assert.h>
  42. #define MB_INTRA_VLC_BITS 9
  43. #define DC_VLC_BITS 9
  44. // offset tables for interlaced picture MVDATA decoding
  45. static const int offset_table1[9] = { 0, 1, 2, 4, 8, 16, 32, 64, 128 };
  46. static const int offset_table2[9] = { 0, 1, 3, 7, 15, 31, 63, 127, 255 };
  47. /***********************************************************************/
  48. /**
  49. * @name VC-1 Bitplane decoding
  50. * @see 8.7, p56
  51. * @{
  52. */
  53. /**
  54. * Imode types
  55. * @{
  56. */
  57. enum Imode {
  58. IMODE_RAW,
  59. IMODE_NORM2,
  60. IMODE_DIFF2,
  61. IMODE_NORM6,
  62. IMODE_DIFF6,
  63. IMODE_ROWSKIP,
  64. IMODE_COLSKIP
  65. };
  66. /** @} */ //imode defines
  67. static void init_block_index(VC1Context *v)
  68. {
  69. MpegEncContext *s = &v->s;
  70. ff_init_block_index(s);
  71. if (v->field_mode && v->second_field) {
  72. s->dest[0] += s->current_picture_ptr->f.linesize[0];
  73. s->dest[1] += s->current_picture_ptr->f.linesize[1];
  74. s->dest[2] += s->current_picture_ptr->f.linesize[2];
  75. }
  76. }
  77. /** @} */ //Bitplane group
  78. static void vc1_put_signed_blocks_clamped(VC1Context *v)
  79. {
  80. MpegEncContext *s = &v->s;
  81. int topleft_mb_pos, top_mb_pos;
  82. int stride_y, fieldtx = 0;
  83. int v_dist;
  84. /* The put pixels loop is always one MB row behind the decoding loop,
  85. * because we can only put pixels when overlap filtering is done, and
  86. * for filtering of the bottom edge of a MB, we need the next MB row
  87. * present as well.
  88. * Within the row, the put pixels loop is also one MB col behind the
  89. * decoding loop. The reason for this is again, because for filtering
  90. * of the right MB edge, we need the next MB present. */
  91. if (!s->first_slice_line) {
  92. if (s->mb_x) {
  93. topleft_mb_pos = (s->mb_y - 1) * s->mb_stride + s->mb_x - 1;
  94. if (v->fcm == ILACE_FRAME)
  95. fieldtx = v->fieldtx_plane[topleft_mb_pos];
  96. stride_y = s->linesize << fieldtx;
  97. v_dist = (16 - fieldtx) >> (fieldtx == 0);
  98. s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][0],
  99. s->dest[0] - 16 * s->linesize - 16,
  100. stride_y);
  101. s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][1],
  102. s->dest[0] - 16 * s->linesize - 8,
  103. stride_y);
  104. s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][2],
  105. s->dest[0] - v_dist * s->linesize - 16,
  106. stride_y);
  107. s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][3],
  108. s->dest[0] - v_dist * s->linesize - 8,
  109. stride_y);
  110. s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][4],
  111. s->dest[1] - 8 * s->uvlinesize - 8,
  112. s->uvlinesize);
  113. s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][5],
  114. s->dest[2] - 8 * s->uvlinesize - 8,
  115. s->uvlinesize);
  116. }
  117. if (s->mb_x == s->mb_width - 1) {
  118. top_mb_pos = (s->mb_y - 1) * s->mb_stride + s->mb_x;
  119. if (v->fcm == ILACE_FRAME)
  120. fieldtx = v->fieldtx_plane[top_mb_pos];
  121. stride_y = s->linesize << fieldtx;
  122. v_dist = fieldtx ? 15 : 8;
  123. s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][0],
  124. s->dest[0] - 16 * s->linesize,
  125. stride_y);
  126. s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][1],
  127. s->dest[0] - 16 * s->linesize + 8,
  128. stride_y);
  129. s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][2],
  130. s->dest[0] - v_dist * s->linesize,
  131. stride_y);
  132. s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][3],
  133. s->dest[0] - v_dist * s->linesize + 8,
  134. stride_y);
  135. s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][4],
  136. s->dest[1] - 8 * s->uvlinesize,
  137. s->uvlinesize);
  138. s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][5],
  139. s->dest[2] - 8 * s->uvlinesize,
  140. s->uvlinesize);
  141. }
  142. }
  143. #define inc_blk_idx(idx) do { \
  144. idx++; \
  145. if (idx >= v->n_allocated_blks) \
  146. idx = 0; \
  147. } while (0)
  148. inc_blk_idx(v->topleft_blk_idx);
  149. inc_blk_idx(v->top_blk_idx);
  150. inc_blk_idx(v->left_blk_idx);
  151. inc_blk_idx(v->cur_blk_idx);
  152. }
  153. static void vc1_loop_filter_iblk(VC1Context *v, int pq)
  154. {
  155. MpegEncContext *s = &v->s;
  156. int j;
  157. if (!s->first_slice_line) {
  158. v->vc1dsp.vc1_v_loop_filter16(s->dest[0], s->linesize, pq);
  159. if (s->mb_x)
  160. v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize, s->linesize, pq);
  161. v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize + 8, s->linesize, pq);
  162. for (j = 0; j < 2; j++) {
  163. v->vc1dsp.vc1_v_loop_filter8(s->dest[j + 1], s->uvlinesize, pq);
  164. if (s->mb_x)
  165. v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize, s->uvlinesize, pq);
  166. }
  167. }
  168. v->vc1dsp.vc1_v_loop_filter16(s->dest[0] + 8 * s->linesize, s->linesize, pq);
  169. if (s->mb_y == s->end_mb_y - 1) {
  170. if (s->mb_x) {
  171. v->vc1dsp.vc1_h_loop_filter16(s->dest[0], s->linesize, pq);
  172. v->vc1dsp.vc1_h_loop_filter8(s->dest[1], s->uvlinesize, pq);
  173. v->vc1dsp.vc1_h_loop_filter8(s->dest[2], s->uvlinesize, pq);
  174. }
  175. v->vc1dsp.vc1_h_loop_filter16(s->dest[0] + 8, s->linesize, pq);
  176. }
  177. }
  178. static void vc1_loop_filter_iblk_delayed(VC1Context *v, int pq)
  179. {
  180. MpegEncContext *s = &v->s;
  181. int j;
  182. /* The loopfilter runs 1 row and 1 column behind the overlap filter, which
  183. * means it runs two rows/cols behind the decoding loop. */
  184. if (!s->first_slice_line) {
  185. if (s->mb_x) {
  186. if (s->mb_y >= s->start_mb_y + 2) {
  187. v->vc1dsp.vc1_v_loop_filter16(s->dest[0] - 16 * s->linesize - 16, s->linesize, pq);
  188. if (s->mb_x >= 2)
  189. v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 32 * s->linesize - 16, s->linesize, pq);
  190. v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 32 * s->linesize - 8, s->linesize, pq);
  191. for (j = 0; j < 2; j++) {
  192. v->vc1dsp.vc1_v_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize - 8, s->uvlinesize, pq);
  193. if (s->mb_x >= 2) {
  194. v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 16 * s->uvlinesize - 8, s->uvlinesize, pq);
  195. }
  196. }
  197. }
  198. v->vc1dsp.vc1_v_loop_filter16(s->dest[0] - 8 * s->linesize - 16, s->linesize, pq);
  199. }
  200. if (s->mb_x == s->mb_width - 1) {
  201. if (s->mb_y >= s->start_mb_y + 2) {
  202. v->vc1dsp.vc1_v_loop_filter16(s->dest[0] - 16 * s->linesize, s->linesize, pq);
  203. if (s->mb_x)
  204. v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 32 * s->linesize, s->linesize, pq);
  205. v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 32 * s->linesize + 8, s->linesize, pq);
  206. for (j = 0; j < 2; j++) {
  207. v->vc1dsp.vc1_v_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize, s->uvlinesize, pq);
  208. if (s->mb_x >= 2) {
  209. v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 16 * s->uvlinesize, s->uvlinesize, pq);
  210. }
  211. }
  212. }
  213. v->vc1dsp.vc1_v_loop_filter16(s->dest[0] - 8 * s->linesize, s->linesize, pq);
  214. }
  215. if (s->mb_y == s->end_mb_y) {
  216. if (s->mb_x) {
  217. if (s->mb_x >= 2)
  218. v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize - 16, s->linesize, pq);
  219. v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize - 8, s->linesize, pq);
  220. if (s->mb_x >= 2) {
  221. for (j = 0; j < 2; j++) {
  222. v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize - 8, s->uvlinesize, pq);
  223. }
  224. }
  225. }
  226. if (s->mb_x == s->mb_width - 1) {
  227. if (s->mb_x)
  228. v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize, s->linesize, pq);
  229. v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize + 8, s->linesize, pq);
  230. if (s->mb_x) {
  231. for (j = 0; j < 2; j++) {
  232. v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize, s->uvlinesize, pq);
  233. }
  234. }
  235. }
  236. }
  237. }
  238. }
  239. static void vc1_smooth_overlap_filter_iblk(VC1Context *v)
  240. {
  241. MpegEncContext *s = &v->s;
  242. int mb_pos;
  243. if (v->condover == CONDOVER_NONE)
  244. return;
  245. mb_pos = s->mb_x + s->mb_y * s->mb_stride;
  246. /* Within a MB, the horizontal overlap always runs before the vertical.
  247. * To accomplish that, we run the H on left and internal borders of the
  248. * currently decoded MB. Then, we wait for the next overlap iteration
  249. * to do H overlap on the right edge of this MB, before moving over and
  250. * running the V overlap. Therefore, the V overlap makes us trail by one
  251. * MB col and the H overlap filter makes us trail by one MB row. This
  252. * is reflected in the time at which we run the put_pixels loop. */
  253. if (v->condover == CONDOVER_ALL || v->pq >= 9 || v->over_flags_plane[mb_pos]) {
  254. if (s->mb_x && (v->condover == CONDOVER_ALL || v->pq >= 9 ||
  255. v->over_flags_plane[mb_pos - 1])) {
  256. v->vc1dsp.vc1_h_s_overlap(v->block[v->left_blk_idx][1],
  257. v->block[v->cur_blk_idx][0]);
  258. v->vc1dsp.vc1_h_s_overlap(v->block[v->left_blk_idx][3],
  259. v->block[v->cur_blk_idx][2]);
  260. if (!(s->flags & CODEC_FLAG_GRAY)) {
  261. v->vc1dsp.vc1_h_s_overlap(v->block[v->left_blk_idx][4],
  262. v->block[v->cur_blk_idx][4]);
  263. v->vc1dsp.vc1_h_s_overlap(v->block[v->left_blk_idx][5],
  264. v->block[v->cur_blk_idx][5]);
  265. }
  266. }
  267. v->vc1dsp.vc1_h_s_overlap(v->block[v->cur_blk_idx][0],
  268. v->block[v->cur_blk_idx][1]);
  269. v->vc1dsp.vc1_h_s_overlap(v->block[v->cur_blk_idx][2],
  270. v->block[v->cur_blk_idx][3]);
  271. if (s->mb_x == s->mb_width - 1) {
  272. if (!s->first_slice_line && (v->condover == CONDOVER_ALL || v->pq >= 9 ||
  273. v->over_flags_plane[mb_pos - s->mb_stride])) {
  274. v->vc1dsp.vc1_v_s_overlap(v->block[v->top_blk_idx][2],
  275. v->block[v->cur_blk_idx][0]);
  276. v->vc1dsp.vc1_v_s_overlap(v->block[v->top_blk_idx][3],
  277. v->block[v->cur_blk_idx][1]);
  278. if (!(s->flags & CODEC_FLAG_GRAY)) {
  279. v->vc1dsp.vc1_v_s_overlap(v->block[v->top_blk_idx][4],
  280. v->block[v->cur_blk_idx][4]);
  281. v->vc1dsp.vc1_v_s_overlap(v->block[v->top_blk_idx][5],
  282. v->block[v->cur_blk_idx][5]);
  283. }
  284. }
  285. v->vc1dsp.vc1_v_s_overlap(v->block[v->cur_blk_idx][0],
  286. v->block[v->cur_blk_idx][2]);
  287. v->vc1dsp.vc1_v_s_overlap(v->block[v->cur_blk_idx][1],
  288. v->block[v->cur_blk_idx][3]);
  289. }
  290. }
  291. if (s->mb_x && (v->condover == CONDOVER_ALL || v->over_flags_plane[mb_pos - 1])) {
  292. if (!s->first_slice_line && (v->condover == CONDOVER_ALL || v->pq >= 9 ||
  293. v->over_flags_plane[mb_pos - s->mb_stride - 1])) {
  294. v->vc1dsp.vc1_v_s_overlap(v->block[v->topleft_blk_idx][2],
  295. v->block[v->left_blk_idx][0]);
  296. v->vc1dsp.vc1_v_s_overlap(v->block[v->topleft_blk_idx][3],
  297. v->block[v->left_blk_idx][1]);
  298. if (!(s->flags & CODEC_FLAG_GRAY)) {
  299. v->vc1dsp.vc1_v_s_overlap(v->block[v->topleft_blk_idx][4],
  300. v->block[v->left_blk_idx][4]);
  301. v->vc1dsp.vc1_v_s_overlap(v->block[v->topleft_blk_idx][5],
  302. v->block[v->left_blk_idx][5]);
  303. }
  304. }
  305. v->vc1dsp.vc1_v_s_overlap(v->block[v->left_blk_idx][0],
  306. v->block[v->left_blk_idx][2]);
  307. v->vc1dsp.vc1_v_s_overlap(v->block[v->left_blk_idx][1],
  308. v->block[v->left_blk_idx][3]);
  309. }
  310. }
  311. /** Do motion compensation over 1 macroblock
  312. * Mostly adapted hpel_motion and qpel_motion from mpegvideo.c
  313. */
  314. static void vc1_mc_1mv(VC1Context *v, int dir)
  315. {
  316. MpegEncContext *s = &v->s;
  317. DSPContext *dsp = &v->s.dsp;
  318. H264ChromaContext *h264chroma = &v->h264chroma;
  319. uint8_t *srcY, *srcU, *srcV;
  320. int dxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
  321. int off, off_uv;
  322. int v_edge_pos = s->v_edge_pos >> v->field_mode;
  323. if ((!v->field_mode ||
  324. (v->ref_field_type[dir] == 1 && v->cur_field_type == 1)) &&
  325. !v->s.last_picture.f.data[0])
  326. return;
  327. mx = s->mv[dir][0][0];
  328. my = s->mv[dir][0][1];
  329. // store motion vectors for further use in B frames
  330. if (s->pict_type == AV_PICTURE_TYPE_P) {
  331. s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0] = mx;
  332. s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1] = my;
  333. }
  334. uvmx = (mx + ((mx & 3) == 3)) >> 1;
  335. uvmy = (my + ((my & 3) == 3)) >> 1;
  336. v->luma_mv[s->mb_x][0] = uvmx;
  337. v->luma_mv[s->mb_x][1] = uvmy;
  338. if (v->field_mode &&
  339. v->cur_field_type != v->ref_field_type[dir]) {
  340. my = my - 2 + 4 * v->cur_field_type;
  341. uvmy = uvmy - 2 + 4 * v->cur_field_type;
  342. }
  343. // fastuvmc shall be ignored for interlaced frame picture
  344. if (v->fastuvmc && (v->fcm != ILACE_FRAME)) {
  345. uvmx = uvmx + ((uvmx < 0) ? (uvmx & 1) : -(uvmx & 1));
  346. uvmy = uvmy + ((uvmy < 0) ? (uvmy & 1) : -(uvmy & 1));
  347. }
  348. if (v->field_mode) { // interlaced field picture
  349. if (!dir) {
  350. if ((v->cur_field_type != v->ref_field_type[dir]) && v->second_field) {
  351. srcY = s->current_picture.f.data[0];
  352. srcU = s->current_picture.f.data[1];
  353. srcV = s->current_picture.f.data[2];
  354. } else {
  355. srcY = s->last_picture.f.data[0];
  356. srcU = s->last_picture.f.data[1];
  357. srcV = s->last_picture.f.data[2];
  358. }
  359. } else {
  360. srcY = s->next_picture.f.data[0];
  361. srcU = s->next_picture.f.data[1];
  362. srcV = s->next_picture.f.data[2];
  363. }
  364. } else {
  365. if (!dir) {
  366. srcY = s->last_picture.f.data[0];
  367. srcU = s->last_picture.f.data[1];
  368. srcV = s->last_picture.f.data[2];
  369. } else {
  370. srcY = s->next_picture.f.data[0];
  371. srcU = s->next_picture.f.data[1];
  372. srcV = s->next_picture.f.data[2];
  373. }
  374. }
  375. if(!srcY)
  376. return;
  377. src_x = s->mb_x * 16 + (mx >> 2);
  378. src_y = s->mb_y * 16 + (my >> 2);
  379. uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
  380. uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
  381. if (v->profile != PROFILE_ADVANCED) {
  382. src_x = av_clip( src_x, -16, s->mb_width * 16);
  383. src_y = av_clip( src_y, -16, s->mb_height * 16);
  384. uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
  385. uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
  386. } else {
  387. src_x = av_clip( src_x, -17, s->avctx->coded_width);
  388. src_y = av_clip( src_y, -18, s->avctx->coded_height + 1);
  389. uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
  390. uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
  391. }
  392. srcY += src_y * s->linesize + src_x;
  393. srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
  394. srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
  395. if (v->field_mode && v->ref_field_type[dir]) {
  396. srcY += s->current_picture_ptr->f.linesize[0];
  397. srcU += s->current_picture_ptr->f.linesize[1];
  398. srcV += s->current_picture_ptr->f.linesize[2];
  399. }
  400. /* for grayscale we should not try to read from unknown area */
  401. if (s->flags & CODEC_FLAG_GRAY) {
  402. srcU = s->edge_emu_buffer + 18 * s->linesize;
  403. srcV = s->edge_emu_buffer + 18 * s->linesize;
  404. }
  405. if (v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
  406. || s->h_edge_pos < 22 || v_edge_pos < 22
  407. || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx&3) - 16 - s->mspel * 3
  408. || (unsigned)(src_y - 1) > v_edge_pos - (my&3) - 16 - 3) {
  409. uint8_t *uvbuf = s->edge_emu_buffer + 19 * s->linesize;
  410. srcY -= s->mspel * (1 + s->linesize);
  411. s->vdsp.emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize,
  412. 17 + s->mspel * 2, 17 + s->mspel * 2,
  413. src_x - s->mspel, src_y - s->mspel,
  414. s->h_edge_pos, v_edge_pos);
  415. srcY = s->edge_emu_buffer;
  416. s->vdsp.emulated_edge_mc(uvbuf , srcU, s->uvlinesize, 8 + 1, 8 + 1,
  417. uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
  418. s->vdsp.emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, 8 + 1, 8 + 1,
  419. uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
  420. srcU = uvbuf;
  421. srcV = uvbuf + 16;
  422. /* if we deal with range reduction we need to scale source blocks */
  423. if (v->rangeredfrm) {
  424. int i, j;
  425. uint8_t *src, *src2;
  426. src = srcY;
  427. for (j = 0; j < 17 + s->mspel * 2; j++) {
  428. for (i = 0; i < 17 + s->mspel * 2; i++)
  429. src[i] = ((src[i] - 128) >> 1) + 128;
  430. src += s->linesize;
  431. }
  432. src = srcU;
  433. src2 = srcV;
  434. for (j = 0; j < 9; j++) {
  435. for (i = 0; i < 9; i++) {
  436. src[i] = ((src[i] - 128) >> 1) + 128;
  437. src2[i] = ((src2[i] - 128) >> 1) + 128;
  438. }
  439. src += s->uvlinesize;
  440. src2 += s->uvlinesize;
  441. }
  442. }
  443. /* if we deal with intensity compensation we need to scale source blocks */
  444. if (v->mv_mode == MV_PMODE_INTENSITY_COMP) {
  445. int i, j;
  446. uint8_t *src, *src2;
  447. src = srcY;
  448. for (j = 0; j < 17 + s->mspel * 2; j++) {
  449. for (i = 0; i < 17 + s->mspel * 2; i++)
  450. src[i] = v->luty[src[i]];
  451. src += s->linesize;
  452. }
  453. src = srcU;
  454. src2 = srcV;
  455. for (j = 0; j < 9; j++) {
  456. for (i = 0; i < 9; i++) {
  457. src[i] = v->lutuv[src[i]];
  458. src2[i] = v->lutuv[src2[i]];
  459. }
  460. src += s->uvlinesize;
  461. src2 += s->uvlinesize;
  462. }
  463. }
  464. srcY += s->mspel * (1 + s->linesize);
  465. }
  466. off = 0;
  467. off_uv = 0;
  468. if (s->mspel) {
  469. dxy = ((my & 3) << 2) | (mx & 3);
  470. v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off , srcY , s->linesize, v->rnd);
  471. v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8, srcY + 8, s->linesize, v->rnd);
  472. srcY += s->linesize * 8;
  473. v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8 * s->linesize , srcY , s->linesize, v->rnd);
  474. v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8 * s->linesize + 8, srcY + 8, s->linesize, v->rnd);
  475. } else { // hpel mc - always used for luma
  476. dxy = (my & 2) | ((mx & 2) >> 1);
  477. if (!v->rnd)
  478. dsp->put_pixels_tab[0][dxy](s->dest[0] + off, srcY, s->linesize, 16);
  479. else
  480. dsp->put_no_rnd_pixels_tab[0][dxy](s->dest[0] + off, srcY, s->linesize, 16);
  481. }
  482. if (s->flags & CODEC_FLAG_GRAY) return;
  483. /* Chroma MC always uses qpel bilinear */
  484. uvmx = (uvmx & 3) << 1;
  485. uvmy = (uvmy & 3) << 1;
  486. if (!v->rnd) {
  487. h264chroma->put_h264_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
  488. h264chroma->put_h264_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
  489. } else {
  490. v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
  491. v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
  492. }
  493. }
  494. static inline int median4(int a, int b, int c, int d)
  495. {
  496. if (a < b) {
  497. if (c < d) return (FFMIN(b, d) + FFMAX(a, c)) / 2;
  498. else return (FFMIN(b, c) + FFMAX(a, d)) / 2;
  499. } else {
  500. if (c < d) return (FFMIN(a, d) + FFMAX(b, c)) / 2;
  501. else return (FFMIN(a, c) + FFMAX(b, d)) / 2;
  502. }
  503. }
  504. /** Do motion compensation for 4-MV macroblock - luminance block
  505. */
  506. static void vc1_mc_4mv_luma(VC1Context *v, int n, int dir)
  507. {
  508. MpegEncContext *s = &v->s;
  509. DSPContext *dsp = &v->s.dsp;
  510. uint8_t *srcY;
  511. int dxy, mx, my, src_x, src_y;
  512. int off;
  513. int fieldmv = (v->fcm == ILACE_FRAME) ? v->blk_mv_type[s->block_index[n]] : 0;
  514. int v_edge_pos = s->v_edge_pos >> v->field_mode;
  515. if ((!v->field_mode ||
  516. (v->ref_field_type[dir] == 1 && v->cur_field_type == 1)) &&
  517. !v->s.last_picture.f.data[0])
  518. return;
  519. mx = s->mv[dir][n][0];
  520. my = s->mv[dir][n][1];
  521. if (!dir) {
  522. if (v->field_mode) {
  523. if ((v->cur_field_type != v->ref_field_type[dir]) && v->second_field)
  524. srcY = s->current_picture.f.data[0];
  525. else
  526. srcY = s->last_picture.f.data[0];
  527. } else
  528. srcY = s->last_picture.f.data[0];
  529. } else
  530. srcY = s->next_picture.f.data[0];
  531. if(!srcY)
  532. return;
  533. if (v->field_mode) {
  534. if (v->cur_field_type != v->ref_field_type[dir])
  535. my = my - 2 + 4 * v->cur_field_type;
  536. }
  537. if (s->pict_type == AV_PICTURE_TYPE_P && n == 3 && v->field_mode) {
  538. int same_count = 0, opp_count = 0, k;
  539. int chosen_mv[2][4][2], f;
  540. int tx, ty;
  541. for (k = 0; k < 4; k++) {
  542. f = v->mv_f[0][s->block_index[k] + v->blocks_off];
  543. chosen_mv[f][f ? opp_count : same_count][0] = s->mv[0][k][0];
  544. chosen_mv[f][f ? opp_count : same_count][1] = s->mv[0][k][1];
  545. opp_count += f;
  546. same_count += 1 - f;
  547. }
  548. f = opp_count > same_count;
  549. switch (f ? opp_count : same_count) {
  550. case 4:
  551. tx = median4(chosen_mv[f][0][0], chosen_mv[f][1][0],
  552. chosen_mv[f][2][0], chosen_mv[f][3][0]);
  553. ty = median4(chosen_mv[f][0][1], chosen_mv[f][1][1],
  554. chosen_mv[f][2][1], chosen_mv[f][3][1]);
  555. break;
  556. case 3:
  557. tx = mid_pred(chosen_mv[f][0][0], chosen_mv[f][1][0], chosen_mv[f][2][0]);
  558. ty = mid_pred(chosen_mv[f][0][1], chosen_mv[f][1][1], chosen_mv[f][2][1]);
  559. break;
  560. case 2:
  561. tx = (chosen_mv[f][0][0] + chosen_mv[f][1][0]) / 2;
  562. ty = (chosen_mv[f][0][1] + chosen_mv[f][1][1]) / 2;
  563. break;
  564. default:
  565. av_assert2(0);
  566. }
  567. s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0] = tx;
  568. s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1] = ty;
  569. for (k = 0; k < 4; k++)
  570. v->mv_f[1][s->block_index[k] + v->blocks_off] = f;
  571. }
  572. if (v->fcm == ILACE_FRAME) { // not sure if needed for other types of picture
  573. int qx, qy;
  574. int width = s->avctx->coded_width;
  575. int height = s->avctx->coded_height >> 1;
  576. qx = (s->mb_x * 16) + (mx >> 2);
  577. qy = (s->mb_y * 8) + (my >> 3);
  578. if (qx < -17)
  579. mx -= 4 * (qx + 17);
  580. else if (qx > width)
  581. mx -= 4 * (qx - width);
  582. if (qy < -18)
  583. my -= 8 * (qy + 18);
  584. else if (qy > height + 1)
  585. my -= 8 * (qy - height - 1);
  586. }
  587. if ((v->fcm == ILACE_FRAME) && fieldmv)
  588. off = ((n > 1) ? s->linesize : 0) + (n & 1) * 8;
  589. else
  590. off = s->linesize * 4 * (n & 2) + (n & 1) * 8;
  591. src_x = s->mb_x * 16 + (n & 1) * 8 + (mx >> 2);
  592. if (!fieldmv)
  593. src_y = s->mb_y * 16 + (n & 2) * 4 + (my >> 2);
  594. else
  595. src_y = s->mb_y * 16 + ((n > 1) ? 1 : 0) + (my >> 2);
  596. if (v->profile != PROFILE_ADVANCED) {
  597. src_x = av_clip(src_x, -16, s->mb_width * 16);
  598. src_y = av_clip(src_y, -16, s->mb_height * 16);
  599. } else {
  600. src_x = av_clip(src_x, -17, s->avctx->coded_width);
  601. if (v->fcm == ILACE_FRAME) {
  602. if (src_y & 1)
  603. src_y = av_clip(src_y, -17, s->avctx->coded_height + 1);
  604. else
  605. src_y = av_clip(src_y, -18, s->avctx->coded_height);
  606. } else {
  607. src_y = av_clip(src_y, -18, s->avctx->coded_height + 1);
  608. }
  609. }
  610. srcY += src_y * s->linesize + src_x;
  611. if (v->field_mode && v->ref_field_type[dir])
  612. srcY += s->current_picture_ptr->f.linesize[0];
  613. if (fieldmv && !(src_y & 1))
  614. v_edge_pos--;
  615. if (fieldmv && (src_y & 1) && src_y < 4)
  616. src_y--;
  617. if (v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
  618. || s->h_edge_pos < 13 || v_edge_pos < 23
  619. || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx & 3) - 8 - s->mspel * 2
  620. || (unsigned)(src_y - (s->mspel << fieldmv)) > v_edge_pos - (my & 3) - ((8 + s->mspel * 2) << fieldmv)) {
  621. srcY -= s->mspel * (1 + (s->linesize << fieldmv));
  622. /* check emulate edge stride and offset */
  623. s->vdsp.emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize,
  624. 9 + s->mspel * 2, (9 + s->mspel * 2) << fieldmv,
  625. src_x - s->mspel, src_y - (s->mspel << fieldmv),
  626. s->h_edge_pos, v_edge_pos);
  627. srcY = s->edge_emu_buffer;
  628. /* if we deal with range reduction we need to scale source blocks */
  629. if (v->rangeredfrm) {
  630. int i, j;
  631. uint8_t *src;
  632. src = srcY;
  633. for (j = 0; j < 9 + s->mspel * 2; j++) {
  634. for (i = 0; i < 9 + s->mspel * 2; i++)
  635. src[i] = ((src[i] - 128) >> 1) + 128;
  636. src += s->linesize << fieldmv;
  637. }
  638. }
  639. /* if we deal with intensity compensation we need to scale source blocks */
  640. if (v->mv_mode == MV_PMODE_INTENSITY_COMP) {
  641. int i, j;
  642. uint8_t *src;
  643. src = srcY;
  644. for (j = 0; j < 9 + s->mspel * 2; j++) {
  645. for (i = 0; i < 9 + s->mspel * 2; i++)
  646. src[i] = v->luty[src[i]];
  647. src += s->linesize << fieldmv;
  648. }
  649. }
  650. srcY += s->mspel * (1 + (s->linesize << fieldmv));
  651. }
  652. if (s->mspel) {
  653. dxy = ((my & 3) << 2) | (mx & 3);
  654. v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off, srcY, s->linesize << fieldmv, v->rnd);
  655. } else { // hpel mc - always used for luma
  656. dxy = (my & 2) | ((mx & 2) >> 1);
  657. if (!v->rnd)
  658. dsp->put_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
  659. else
  660. dsp->put_no_rnd_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
  661. }
  662. }
  663. static av_always_inline int get_chroma_mv(int *mvx, int *mvy, int *a, int flag, int *tx, int *ty)
  664. {
  665. int idx, i;
  666. static const int count[16] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4};
  667. idx = ((a[3] != flag) << 3)
  668. | ((a[2] != flag) << 2)
  669. | ((a[1] != flag) << 1)
  670. | (a[0] != flag);
  671. if (!idx) {
  672. *tx = median4(mvx[0], mvx[1], mvx[2], mvx[3]);
  673. *ty = median4(mvy[0], mvy[1], mvy[2], mvy[3]);
  674. return 4;
  675. } else if (count[idx] == 1) {
  676. switch (idx) {
  677. case 0x1:
  678. *tx = mid_pred(mvx[1], mvx[2], mvx[3]);
  679. *ty = mid_pred(mvy[1], mvy[2], mvy[3]);
  680. return 3;
  681. case 0x2:
  682. *tx = mid_pred(mvx[0], mvx[2], mvx[3]);
  683. *ty = mid_pred(mvy[0], mvy[2], mvy[3]);
  684. return 3;
  685. case 0x4:
  686. *tx = mid_pred(mvx[0], mvx[1], mvx[3]);
  687. *ty = mid_pred(mvy[0], mvy[1], mvy[3]);
  688. return 3;
  689. case 0x8:
  690. *tx = mid_pred(mvx[0], mvx[1], mvx[2]);
  691. *ty = mid_pred(mvy[0], mvy[1], mvy[2]);
  692. return 3;
  693. }
  694. } else if (count[idx] == 2) {
  695. int t1 = 0, t2 = 0;
  696. for (i = 0; i < 3; i++)
  697. if (!a[i]) {
  698. t1 = i;
  699. break;
  700. }
  701. for (i = t1 + 1; i < 4; i++)
  702. if (!a[i]) {
  703. t2 = i;
  704. break;
  705. }
  706. *tx = (mvx[t1] + mvx[t2]) / 2;
  707. *ty = (mvy[t1] + mvy[t2]) / 2;
  708. return 2;
  709. } else {
  710. return 0;
  711. }
  712. return -1;
  713. }
  714. /** Do motion compensation for 4-MV macroblock - both chroma blocks
  715. */
  716. static void vc1_mc_4mv_chroma(VC1Context *v, int dir)
  717. {
  718. MpegEncContext *s = &v->s;
  719. H264ChromaContext *h264chroma = &v->h264chroma;
  720. uint8_t *srcU, *srcV;
  721. int uvmx, uvmy, uvsrc_x, uvsrc_y;
  722. int k, tx = 0, ty = 0;
  723. int mvx[4], mvy[4], intra[4], mv_f[4];
  724. int valid_count;
  725. int chroma_ref_type = v->cur_field_type, off = 0;
  726. int v_edge_pos = s->v_edge_pos >> v->field_mode;
  727. if (!v->field_mode && !v->s.last_picture.f.data[0])
  728. return;
  729. if (s->flags & CODEC_FLAG_GRAY)
  730. return;
  731. for (k = 0; k < 4; k++) {
  732. mvx[k] = s->mv[dir][k][0];
  733. mvy[k] = s->mv[dir][k][1];
  734. intra[k] = v->mb_type[0][s->block_index[k]];
  735. if (v->field_mode)
  736. mv_f[k] = v->mv_f[dir][s->block_index[k] + v->blocks_off];
  737. }
  738. /* calculate chroma MV vector from four luma MVs */
  739. if (!v->field_mode || (v->field_mode && !v->numref)) {
  740. valid_count = get_chroma_mv(mvx, mvy, intra, 0, &tx, &ty);
  741. chroma_ref_type = v->reffield;
  742. if (!valid_count) {
  743. s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
  744. s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
  745. v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
  746. return; //no need to do MC for intra blocks
  747. }
  748. } else {
  749. int dominant = 0;
  750. if (mv_f[0] + mv_f[1] + mv_f[2] + mv_f[3] > 2)
  751. dominant = 1;
  752. valid_count = get_chroma_mv(mvx, mvy, mv_f, dominant, &tx, &ty);
  753. if (dominant)
  754. chroma_ref_type = !v->cur_field_type;
  755. }
  756. if (v->field_mode && chroma_ref_type == 1 && v->cur_field_type == 1 && !v->s.last_picture.f.data[0])
  757. return;
  758. s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0] = tx;
  759. s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1] = ty;
  760. uvmx = (tx + ((tx & 3) == 3)) >> 1;
  761. uvmy = (ty + ((ty & 3) == 3)) >> 1;
  762. v->luma_mv[s->mb_x][0] = uvmx;
  763. v->luma_mv[s->mb_x][1] = uvmy;
  764. if (v->fastuvmc) {
  765. uvmx = uvmx + ((uvmx < 0) ? (uvmx & 1) : -(uvmx & 1));
  766. uvmy = uvmy + ((uvmy < 0) ? (uvmy & 1) : -(uvmy & 1));
  767. }
  768. // Field conversion bias
  769. if (v->cur_field_type != chroma_ref_type)
  770. uvmy += 2 - 4 * chroma_ref_type;
  771. uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
  772. uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
  773. if (v->profile != PROFILE_ADVANCED) {
  774. uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
  775. uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
  776. } else {
  777. uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
  778. uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
  779. }
  780. if (!dir) {
  781. if (v->field_mode) {
  782. if ((v->cur_field_type != chroma_ref_type) && v->cur_field_type) {
  783. srcU = s->current_picture.f.data[1];
  784. srcV = s->current_picture.f.data[2];
  785. } else {
  786. srcU = s->last_picture.f.data[1];
  787. srcV = s->last_picture.f.data[2];
  788. }
  789. } else {
  790. srcU = s->last_picture.f.data[1];
  791. srcV = s->last_picture.f.data[2];
  792. }
  793. } else {
  794. srcU = s->next_picture.f.data[1];
  795. srcV = s->next_picture.f.data[2];
  796. }
  797. if(!srcU)
  798. return;
  799. srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
  800. srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
  801. if (v->field_mode) {
  802. if (chroma_ref_type) {
  803. srcU += s->current_picture_ptr->f.linesize[1];
  804. srcV += s->current_picture_ptr->f.linesize[2];
  805. }
  806. off = 0;
  807. }
  808. if (v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
  809. || s->h_edge_pos < 18 || v_edge_pos < 18
  810. || (unsigned)uvsrc_x > (s->h_edge_pos >> 1) - 9
  811. || (unsigned)uvsrc_y > (v_edge_pos >> 1) - 9) {
  812. s->vdsp.emulated_edge_mc(s->edge_emu_buffer , srcU, s->uvlinesize,
  813. 8 + 1, 8 + 1, uvsrc_x, uvsrc_y,
  814. s->h_edge_pos >> 1, v_edge_pos >> 1);
  815. s->vdsp.emulated_edge_mc(s->edge_emu_buffer + 16, srcV, s->uvlinesize,
  816. 8 + 1, 8 + 1, uvsrc_x, uvsrc_y,
  817. s->h_edge_pos >> 1, v_edge_pos >> 1);
  818. srcU = s->edge_emu_buffer;
  819. srcV = s->edge_emu_buffer + 16;
  820. /* if we deal with range reduction we need to scale source blocks */
  821. if (v->rangeredfrm) {
  822. int i, j;
  823. uint8_t *src, *src2;
  824. src = srcU;
  825. src2 = srcV;
  826. for (j = 0; j < 9; j++) {
  827. for (i = 0; i < 9; i++) {
  828. src[i] = ((src[i] - 128) >> 1) + 128;
  829. src2[i] = ((src2[i] - 128) >> 1) + 128;
  830. }
  831. src += s->uvlinesize;
  832. src2 += s->uvlinesize;
  833. }
  834. }
  835. /* if we deal with intensity compensation we need to scale source blocks */
  836. if (v->mv_mode == MV_PMODE_INTENSITY_COMP) {
  837. int i, j;
  838. uint8_t *src, *src2;
  839. src = srcU;
  840. src2 = srcV;
  841. for (j = 0; j < 9; j++) {
  842. for (i = 0; i < 9; i++) {
  843. src[i] = v->lutuv[src[i]];
  844. src2[i] = v->lutuv[src2[i]];
  845. }
  846. src += s->uvlinesize;
  847. src2 += s->uvlinesize;
  848. }
  849. }
  850. }
  851. /* Chroma MC always uses qpel bilinear */
  852. uvmx = (uvmx & 3) << 1;
  853. uvmy = (uvmy & 3) << 1;
  854. if (!v->rnd) {
  855. h264chroma->put_h264_chroma_pixels_tab[0](s->dest[1] + off, srcU, s->uvlinesize, 8, uvmx, uvmy);
  856. h264chroma->put_h264_chroma_pixels_tab[0](s->dest[2] + off, srcV, s->uvlinesize, 8, uvmx, uvmy);
  857. } else {
  858. v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1] + off, srcU, s->uvlinesize, 8, uvmx, uvmy);
  859. v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2] + off, srcV, s->uvlinesize, 8, uvmx, uvmy);
  860. }
  861. }
  862. /** Do motion compensation for 4-MV field chroma macroblock (both U and V)
  863. */
  864. static void vc1_mc_4mv_chroma4(VC1Context *v)
  865. {
  866. MpegEncContext *s = &v->s;
  867. H264ChromaContext *h264chroma = &v->h264chroma;
  868. uint8_t *srcU, *srcV;
  869. int uvsrc_x, uvsrc_y;
  870. int uvmx_field[4], uvmy_field[4];
  871. int i, off, tx, ty;
  872. int fieldmv = v->blk_mv_type[s->block_index[0]];
  873. static const int s_rndtblfield[16] = { 0, 0, 1, 2, 4, 4, 5, 6, 2, 2, 3, 8, 6, 6, 7, 12 };
  874. int v_dist = fieldmv ? 1 : 4; // vertical offset for lower sub-blocks
  875. int v_edge_pos = s->v_edge_pos >> 1;
  876. if (!v->s.last_picture.f.data[0])
  877. return;
  878. if (s->flags & CODEC_FLAG_GRAY)
  879. return;
  880. for (i = 0; i < 4; i++) {
  881. tx = s->mv[0][i][0];
  882. uvmx_field[i] = (tx + ((tx & 3) == 3)) >> 1;
  883. ty = s->mv[0][i][1];
  884. if (fieldmv)
  885. uvmy_field[i] = (ty >> 4) * 8 + s_rndtblfield[ty & 0xF];
  886. else
  887. uvmy_field[i] = (ty + ((ty & 3) == 3)) >> 1;
  888. }
  889. for (i = 0; i < 4; i++) {
  890. off = (i & 1) * 4 + ((i & 2) ? v_dist * s->uvlinesize : 0);
  891. uvsrc_x = s->mb_x * 8 + (i & 1) * 4 + (uvmx_field[i] >> 2);
  892. uvsrc_y = s->mb_y * 8 + ((i & 2) ? v_dist : 0) + (uvmy_field[i] >> 2);
  893. // FIXME: implement proper pull-back (see vc1cropmv.c, vc1CROPMV_ChromaPullBack())
  894. uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
  895. uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
  896. srcU = s->last_picture.f.data[1] + uvsrc_y * s->uvlinesize + uvsrc_x;
  897. srcV = s->last_picture.f.data[2] + uvsrc_y * s->uvlinesize + uvsrc_x;
  898. uvmx_field[i] = (uvmx_field[i] & 3) << 1;
  899. uvmy_field[i] = (uvmy_field[i] & 3) << 1;
  900. if (fieldmv && !(uvsrc_y & 1))
  901. v_edge_pos = (s->v_edge_pos >> 1) - 1;
  902. if (fieldmv && (uvsrc_y & 1) && uvsrc_y < 2)
  903. uvsrc_y--;
  904. if ((v->mv_mode == MV_PMODE_INTENSITY_COMP)
  905. || s->h_edge_pos < 10 || v_edge_pos < (5 << fieldmv)
  906. || (unsigned)uvsrc_x > (s->h_edge_pos >> 1) - 5
  907. || (unsigned)uvsrc_y > v_edge_pos - (5 << fieldmv)) {
  908. s->vdsp.emulated_edge_mc(s->edge_emu_buffer, srcU, s->uvlinesize,
  909. 5, (5 << fieldmv), uvsrc_x, uvsrc_y,
  910. s->h_edge_pos >> 1, v_edge_pos);
  911. s->vdsp.emulated_edge_mc(s->edge_emu_buffer + 16, srcV, s->uvlinesize,
  912. 5, (5 << fieldmv), uvsrc_x, uvsrc_y,
  913. s->h_edge_pos >> 1, v_edge_pos);
  914. srcU = s->edge_emu_buffer;
  915. srcV = s->edge_emu_buffer + 16;
  916. /* if we deal with intensity compensation we need to scale source blocks */
  917. if (v->mv_mode == MV_PMODE_INTENSITY_COMP) {
  918. int i, j;
  919. uint8_t *src, *src2;
  920. src = srcU;
  921. src2 = srcV;
  922. for (j = 0; j < 5; j++) {
  923. for (i = 0; i < 5; i++) {
  924. src[i] = v->lutuv[src[i]];
  925. src2[i] = v->lutuv[src2[i]];
  926. }
  927. src += s->uvlinesize << 1;
  928. src2 += s->uvlinesize << 1;
  929. }
  930. }
  931. }
  932. if (!v->rnd) {
  933. h264chroma->put_h264_chroma_pixels_tab[1](s->dest[1] + off, srcU, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
  934. h264chroma->put_h264_chroma_pixels_tab[1](s->dest[2] + off, srcV, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
  935. } else {
  936. 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]);
  937. 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]);
  938. }
  939. }
  940. }
  941. /***********************************************************************/
  942. /**
  943. * @name VC-1 Block-level functions
  944. * @see 7.1.4, p91 and 8.1.1.7, p(1)04
  945. * @{
  946. */
  947. /**
  948. * @def GET_MQUANT
  949. * @brief Get macroblock-level quantizer scale
  950. */
  951. #define GET_MQUANT() \
  952. if (v->dquantfrm) { \
  953. int edges = 0; \
  954. if (v->dqprofile == DQPROFILE_ALL_MBS) { \
  955. if (v->dqbilevel) { \
  956. mquant = (get_bits1(gb)) ? v->altpq : v->pq; \
  957. } else { \
  958. mqdiff = get_bits(gb, 3); \
  959. if (mqdiff != 7) \
  960. mquant = v->pq + mqdiff; \
  961. else \
  962. mquant = get_bits(gb, 5); \
  963. } \
  964. } \
  965. if (v->dqprofile == DQPROFILE_SINGLE_EDGE) \
  966. edges = 1 << v->dqsbedge; \
  967. else if (v->dqprofile == DQPROFILE_DOUBLE_EDGES) \
  968. edges = (3 << v->dqsbedge) % 15; \
  969. else if (v->dqprofile == DQPROFILE_FOUR_EDGES) \
  970. edges = 15; \
  971. if ((edges&1) && !s->mb_x) \
  972. mquant = v->altpq; \
  973. if ((edges&2) && s->first_slice_line) \
  974. mquant = v->altpq; \
  975. if ((edges&4) && s->mb_x == (s->mb_width - 1)) \
  976. mquant = v->altpq; \
  977. if ((edges&8) && s->mb_y == (s->mb_height - 1)) \
  978. mquant = v->altpq; \
  979. if (!mquant || mquant > 31) { \
  980. av_log(v->s.avctx, AV_LOG_ERROR, \
  981. "Overriding invalid mquant %d\n", mquant); \
  982. mquant = 1; \
  983. } \
  984. }
  985. /**
  986. * @def GET_MVDATA(_dmv_x, _dmv_y)
  987. * @brief Get MV differentials
  988. * @see MVDATA decoding from 8.3.5.2, p(1)20
  989. * @param _dmv_x Horizontal differential for decoded MV
  990. * @param _dmv_y Vertical differential for decoded MV
  991. */
  992. #define GET_MVDATA(_dmv_x, _dmv_y) \
  993. index = 1 + get_vlc2(gb, ff_vc1_mv_diff_vlc[s->mv_table_index].table, \
  994. VC1_MV_DIFF_VLC_BITS, 2); \
  995. if (index > 36) { \
  996. mb_has_coeffs = 1; \
  997. index -= 37; \
  998. } else \
  999. mb_has_coeffs = 0; \
  1000. s->mb_intra = 0; \
  1001. if (!index) { \
  1002. _dmv_x = _dmv_y = 0; \
  1003. } else if (index == 35) { \
  1004. _dmv_x = get_bits(gb, v->k_x - 1 + s->quarter_sample); \
  1005. _dmv_y = get_bits(gb, v->k_y - 1 + s->quarter_sample); \
  1006. } else if (index == 36) { \
  1007. _dmv_x = 0; \
  1008. _dmv_y = 0; \
  1009. s->mb_intra = 1; \
  1010. } else { \
  1011. index1 = index % 6; \
  1012. if (!s->quarter_sample && index1 == 5) val = 1; \
  1013. else val = 0; \
  1014. if (size_table[index1] - val > 0) \
  1015. val = get_bits(gb, size_table[index1] - val); \
  1016. else val = 0; \
  1017. sign = 0 - (val&1); \
  1018. _dmv_x = (sign ^ ((val>>1) + offset_table[index1])) - sign; \
  1019. \
  1020. index1 = index / 6; \
  1021. if (!s->quarter_sample && index1 == 5) val = 1; \
  1022. else val = 0; \
  1023. if (size_table[index1] - val > 0) \
  1024. val = get_bits(gb, size_table[index1] - val); \
  1025. else val = 0; \
  1026. sign = 0 - (val & 1); \
  1027. _dmv_y = (sign ^ ((val >> 1) + offset_table[index1])) - sign; \
  1028. }
  1029. static av_always_inline void get_mvdata_interlaced(VC1Context *v, int *dmv_x,
  1030. int *dmv_y, int *pred_flag)
  1031. {
  1032. int index, index1;
  1033. int extend_x = 0, extend_y = 0;
  1034. GetBitContext *gb = &v->s.gb;
  1035. int bits, esc;
  1036. int val, sign;
  1037. const int* offs_tab;
  1038. if (v->numref) {
  1039. bits = VC1_2REF_MVDATA_VLC_BITS;
  1040. esc = 125;
  1041. } else {
  1042. bits = VC1_1REF_MVDATA_VLC_BITS;
  1043. esc = 71;
  1044. }
  1045. switch (v->dmvrange) {
  1046. case 1:
  1047. extend_x = 1;
  1048. break;
  1049. case 2:
  1050. extend_y = 1;
  1051. break;
  1052. case 3:
  1053. extend_x = extend_y = 1;
  1054. break;
  1055. }
  1056. index = get_vlc2(gb, v->imv_vlc->table, bits, 3);
  1057. if (index == esc) {
  1058. *dmv_x = get_bits(gb, v->k_x);
  1059. *dmv_y = get_bits(gb, v->k_y);
  1060. if (v->numref) {
  1061. if (pred_flag) {
  1062. *pred_flag = *dmv_y & 1;
  1063. *dmv_y = (*dmv_y + *pred_flag) >> 1;
  1064. } else {
  1065. *dmv_y = (*dmv_y + (*dmv_y & 1)) >> 1;
  1066. }
  1067. }
  1068. }
  1069. else {
  1070. av_assert0(index < esc);
  1071. if (extend_x)
  1072. offs_tab = offset_table2;
  1073. else
  1074. offs_tab = offset_table1;
  1075. index1 = (index + 1) % 9;
  1076. if (index1 != 0) {
  1077. val = get_bits(gb, index1 + extend_x);
  1078. sign = 0 -(val & 1);
  1079. *dmv_x = (sign ^ ((val >> 1) + offs_tab[index1])) - sign;
  1080. } else
  1081. *dmv_x = 0;
  1082. if (extend_y)
  1083. offs_tab = offset_table2;
  1084. else
  1085. offs_tab = offset_table1;
  1086. index1 = (index + 1) / 9;
  1087. if (index1 > v->numref) {
  1088. val = get_bits(gb, (index1 + (extend_y << v->numref)) >> v->numref);
  1089. sign = 0 - (val & 1);
  1090. *dmv_y = (sign ^ ((val >> 1) + offs_tab[index1 >> v->numref])) - sign;
  1091. } else
  1092. *dmv_y = 0;
  1093. if (v->numref && pred_flag)
  1094. *pred_flag = index1 & 1;
  1095. }
  1096. }
  1097. static av_always_inline int scaleforsame_x(VC1Context *v, int n /* MV */, int dir)
  1098. {
  1099. int scaledvalue, refdist;
  1100. int scalesame1, scalesame2;
  1101. int scalezone1_x, zone1offset_x;
  1102. int table_index = dir ^ v->second_field;
  1103. if (v->s.pict_type != AV_PICTURE_TYPE_B)
  1104. refdist = v->refdist;
  1105. else
  1106. refdist = dir ? v->brfd : v->frfd;
  1107. if (refdist > 3)
  1108. refdist = 3;
  1109. scalesame1 = ff_vc1_field_mvpred_scales[table_index][1][refdist];
  1110. scalesame2 = ff_vc1_field_mvpred_scales[table_index][2][refdist];
  1111. scalezone1_x = ff_vc1_field_mvpred_scales[table_index][3][refdist];
  1112. zone1offset_x = ff_vc1_field_mvpred_scales[table_index][5][refdist];
  1113. if (FFABS(n) > 255)
  1114. scaledvalue = n;
  1115. else {
  1116. if (FFABS(n) < scalezone1_x)
  1117. scaledvalue = (n * scalesame1) >> 8;
  1118. else {
  1119. if (n < 0)
  1120. scaledvalue = ((n * scalesame2) >> 8) - zone1offset_x;
  1121. else
  1122. scaledvalue = ((n * scalesame2) >> 8) + zone1offset_x;
  1123. }
  1124. }
  1125. return av_clip(scaledvalue, -v->range_x, v->range_x - 1);
  1126. }
  1127. static av_always_inline int scaleforsame_y(VC1Context *v, int i, int n /* MV */, int dir)
  1128. {
  1129. int scaledvalue, refdist;
  1130. int scalesame1, scalesame2;
  1131. int scalezone1_y,

Large files files are truncated, but you can click here to view the full file