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.

6379 lines
249KB

  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 "error_resilience.h"
  30. #include "mpeg_er.h"
  31. #include "mpegutils.h"
  32. #include "mpegvideo.h"
  33. #include "h263.h"
  34. #include "h264chroma.h"
  35. #include "qpeldsp.h"
  36. #include "vc1.h"
  37. #include "vc1data.h"
  38. #include "vc1acdata.h"
  39. #include "msmpeg4data.h"
  40. #include "unary.h"
  41. #include "mathops.h"
  42. #include "vdpau_internal.h"
  43. #include "libavutil/avassert.h"
  44. #undef NDEBUG
  45. #include <assert.h>
  46. #define MB_INTRA_VLC_BITS 9
  47. #define DC_VLC_BITS 9
  48. // offset tables for interlaced picture MVDATA decoding
  49. static const int offset_table1[9] = { 0, 1, 2, 4, 8, 16, 32, 64, 128 };
  50. static const int offset_table2[9] = { 0, 1, 3, 7, 15, 31, 63, 127, 255 };
  51. /***********************************************************************/
  52. /**
  53. * @name VC-1 Bitplane decoding
  54. * @see 8.7, p56
  55. * @{
  56. */
  57. static void init_block_index(VC1Context *v)
  58. {
  59. MpegEncContext *s = &v->s;
  60. ff_init_block_index(s);
  61. if (v->field_mode && !(v->second_field ^ v->tff)) {
  62. s->dest[0] += s->current_picture_ptr->f->linesize[0];
  63. s->dest[1] += s->current_picture_ptr->f->linesize[1];
  64. s->dest[2] += s->current_picture_ptr->f->linesize[2];
  65. }
  66. }
  67. /** @} */ //Bitplane group
  68. static void vc1_put_signed_blocks_clamped(VC1Context *v)
  69. {
  70. MpegEncContext *s = &v->s;
  71. int topleft_mb_pos, top_mb_pos;
  72. int stride_y, fieldtx = 0;
  73. int v_dist;
  74. /* The put pixels loop is always one MB row behind the decoding loop,
  75. * because we can only put pixels when overlap filtering is done, and
  76. * for filtering of the bottom edge of a MB, we need the next MB row
  77. * present as well.
  78. * Within the row, the put pixels loop is also one MB col behind the
  79. * decoding loop. The reason for this is again, because for filtering
  80. * of the right MB edge, we need the next MB present. */
  81. if (!s->first_slice_line) {
  82. if (s->mb_x) {
  83. topleft_mb_pos = (s->mb_y - 1) * s->mb_stride + s->mb_x - 1;
  84. if (v->fcm == ILACE_FRAME)
  85. fieldtx = v->fieldtx_plane[topleft_mb_pos];
  86. stride_y = s->linesize << fieldtx;
  87. v_dist = (16 - fieldtx) >> (fieldtx == 0);
  88. s->idsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][0],
  89. s->dest[0] - 16 * s->linesize - 16,
  90. stride_y);
  91. s->idsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][1],
  92. s->dest[0] - 16 * s->linesize - 8,
  93. stride_y);
  94. s->idsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][2],
  95. s->dest[0] - v_dist * s->linesize - 16,
  96. stride_y);
  97. s->idsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][3],
  98. s->dest[0] - v_dist * s->linesize - 8,
  99. stride_y);
  100. s->idsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][4],
  101. s->dest[1] - 8 * s->uvlinesize - 8,
  102. s->uvlinesize);
  103. s->idsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][5],
  104. s->dest[2] - 8 * s->uvlinesize - 8,
  105. s->uvlinesize);
  106. }
  107. if (s->mb_x == s->mb_width - 1) {
  108. top_mb_pos = (s->mb_y - 1) * s->mb_stride + s->mb_x;
  109. if (v->fcm == ILACE_FRAME)
  110. fieldtx = v->fieldtx_plane[top_mb_pos];
  111. stride_y = s->linesize << fieldtx;
  112. v_dist = fieldtx ? 15 : 8;
  113. s->idsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][0],
  114. s->dest[0] - 16 * s->linesize,
  115. stride_y);
  116. s->idsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][1],
  117. s->dest[0] - 16 * s->linesize + 8,
  118. stride_y);
  119. s->idsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][2],
  120. s->dest[0] - v_dist * s->linesize,
  121. stride_y);
  122. s->idsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][3],
  123. s->dest[0] - v_dist * s->linesize + 8,
  124. stride_y);
  125. s->idsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][4],
  126. s->dest[1] - 8 * s->uvlinesize,
  127. s->uvlinesize);
  128. s->idsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][5],
  129. s->dest[2] - 8 * s->uvlinesize,
  130. s->uvlinesize);
  131. }
  132. }
  133. #define inc_blk_idx(idx) do { \
  134. idx++; \
  135. if (idx >= v->n_allocated_blks) \
  136. idx = 0; \
  137. } while (0)
  138. inc_blk_idx(v->topleft_blk_idx);
  139. inc_blk_idx(v->top_blk_idx);
  140. inc_blk_idx(v->left_blk_idx);
  141. inc_blk_idx(v->cur_blk_idx);
  142. }
  143. static void vc1_loop_filter_iblk(VC1Context *v, int pq)
  144. {
  145. MpegEncContext *s = &v->s;
  146. int j;
  147. if (!s->first_slice_line) {
  148. v->vc1dsp.vc1_v_loop_filter16(s->dest[0], s->linesize, pq);
  149. if (s->mb_x)
  150. v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize, s->linesize, pq);
  151. v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize + 8, s->linesize, pq);
  152. for (j = 0; j < 2; j++) {
  153. v->vc1dsp.vc1_v_loop_filter8(s->dest[j + 1], s->uvlinesize, pq);
  154. if (s->mb_x)
  155. v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize, s->uvlinesize, pq);
  156. }
  157. }
  158. v->vc1dsp.vc1_v_loop_filter16(s->dest[0] + 8 * s->linesize, s->linesize, pq);
  159. if (s->mb_y == s->end_mb_y - 1) {
  160. if (s->mb_x) {
  161. v->vc1dsp.vc1_h_loop_filter16(s->dest[0], s->linesize, pq);
  162. v->vc1dsp.vc1_h_loop_filter8(s->dest[1], s->uvlinesize, pq);
  163. v->vc1dsp.vc1_h_loop_filter8(s->dest[2], s->uvlinesize, pq);
  164. }
  165. v->vc1dsp.vc1_h_loop_filter16(s->dest[0] + 8, s->linesize, pq);
  166. }
  167. }
  168. static void vc1_loop_filter_iblk_delayed(VC1Context *v, int pq)
  169. {
  170. MpegEncContext *s = &v->s;
  171. int j;
  172. /* The loopfilter runs 1 row and 1 column behind the overlap filter, which
  173. * means it runs two rows/cols behind the decoding loop. */
  174. if (!s->first_slice_line) {
  175. if (s->mb_x) {
  176. if (s->mb_y >= s->start_mb_y + 2) {
  177. v->vc1dsp.vc1_v_loop_filter16(s->dest[0] - 16 * s->linesize - 16, s->linesize, pq);
  178. if (s->mb_x >= 2)
  179. v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 32 * s->linesize - 16, s->linesize, pq);
  180. v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 32 * s->linesize - 8, s->linesize, pq);
  181. for (j = 0; j < 2; j++) {
  182. v->vc1dsp.vc1_v_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize - 8, s->uvlinesize, pq);
  183. if (s->mb_x >= 2) {
  184. v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 16 * s->uvlinesize - 8, s->uvlinesize, pq);
  185. }
  186. }
  187. }
  188. v->vc1dsp.vc1_v_loop_filter16(s->dest[0] - 8 * s->linesize - 16, s->linesize, pq);
  189. }
  190. if (s->mb_x == s->mb_width - 1) {
  191. if (s->mb_y >= s->start_mb_y + 2) {
  192. v->vc1dsp.vc1_v_loop_filter16(s->dest[0] - 16 * s->linesize, s->linesize, pq);
  193. if (s->mb_x)
  194. v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 32 * s->linesize, s->linesize, pq);
  195. v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 32 * s->linesize + 8, s->linesize, pq);
  196. for (j = 0; j < 2; j++) {
  197. v->vc1dsp.vc1_v_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize, s->uvlinesize, pq);
  198. if (s->mb_x >= 2) {
  199. v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 16 * s->uvlinesize, s->uvlinesize, pq);
  200. }
  201. }
  202. }
  203. v->vc1dsp.vc1_v_loop_filter16(s->dest[0] - 8 * s->linesize, s->linesize, pq);
  204. }
  205. if (s->mb_y == s->end_mb_y) {
  206. if (s->mb_x) {
  207. if (s->mb_x >= 2)
  208. v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize - 16, s->linesize, pq);
  209. v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize - 8, s->linesize, pq);
  210. if (s->mb_x >= 2) {
  211. for (j = 0; j < 2; j++) {
  212. v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize - 8, s->uvlinesize, pq);
  213. }
  214. }
  215. }
  216. if (s->mb_x == s->mb_width - 1) {
  217. if (s->mb_x)
  218. v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize, 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) {
  221. for (j = 0; j < 2; j++) {
  222. v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize, s->uvlinesize, pq);
  223. }
  224. }
  225. }
  226. }
  227. }
  228. }
  229. static void vc1_smooth_overlap_filter_iblk(VC1Context *v)
  230. {
  231. MpegEncContext *s = &v->s;
  232. int mb_pos;
  233. if (v->condover == CONDOVER_NONE)
  234. return;
  235. mb_pos = s->mb_x + s->mb_y * s->mb_stride;
  236. /* Within a MB, the horizontal overlap always runs before the vertical.
  237. * To accomplish that, we run the H on left and internal borders of the
  238. * currently decoded MB. Then, we wait for the next overlap iteration
  239. * to do H overlap on the right edge of this MB, before moving over and
  240. * running the V overlap. Therefore, the V overlap makes us trail by one
  241. * MB col and the H overlap filter makes us trail by one MB row. This
  242. * is reflected in the time at which we run the put_pixels loop. */
  243. if (v->condover == CONDOVER_ALL || v->pq >= 9 || v->over_flags_plane[mb_pos]) {
  244. if (s->mb_x && (v->condover == CONDOVER_ALL || v->pq >= 9 ||
  245. v->over_flags_plane[mb_pos - 1])) {
  246. v->vc1dsp.vc1_h_s_overlap(v->block[v->left_blk_idx][1],
  247. v->block[v->cur_blk_idx][0]);
  248. v->vc1dsp.vc1_h_s_overlap(v->block[v->left_blk_idx][3],
  249. v->block[v->cur_blk_idx][2]);
  250. if (!(s->flags & CODEC_FLAG_GRAY)) {
  251. v->vc1dsp.vc1_h_s_overlap(v->block[v->left_blk_idx][4],
  252. v->block[v->cur_blk_idx][4]);
  253. v->vc1dsp.vc1_h_s_overlap(v->block[v->left_blk_idx][5],
  254. v->block[v->cur_blk_idx][5]);
  255. }
  256. }
  257. v->vc1dsp.vc1_h_s_overlap(v->block[v->cur_blk_idx][0],
  258. v->block[v->cur_blk_idx][1]);
  259. v->vc1dsp.vc1_h_s_overlap(v->block[v->cur_blk_idx][2],
  260. v->block[v->cur_blk_idx][3]);
  261. if (s->mb_x == s->mb_width - 1) {
  262. if (!s->first_slice_line && (v->condover == CONDOVER_ALL || v->pq >= 9 ||
  263. v->over_flags_plane[mb_pos - s->mb_stride])) {
  264. v->vc1dsp.vc1_v_s_overlap(v->block[v->top_blk_idx][2],
  265. v->block[v->cur_blk_idx][0]);
  266. v->vc1dsp.vc1_v_s_overlap(v->block[v->top_blk_idx][3],
  267. v->block[v->cur_blk_idx][1]);
  268. if (!(s->flags & CODEC_FLAG_GRAY)) {
  269. v->vc1dsp.vc1_v_s_overlap(v->block[v->top_blk_idx][4],
  270. v->block[v->cur_blk_idx][4]);
  271. v->vc1dsp.vc1_v_s_overlap(v->block[v->top_blk_idx][5],
  272. v->block[v->cur_blk_idx][5]);
  273. }
  274. }
  275. v->vc1dsp.vc1_v_s_overlap(v->block[v->cur_blk_idx][0],
  276. v->block[v->cur_blk_idx][2]);
  277. v->vc1dsp.vc1_v_s_overlap(v->block[v->cur_blk_idx][1],
  278. v->block[v->cur_blk_idx][3]);
  279. }
  280. }
  281. if (s->mb_x && (v->condover == CONDOVER_ALL || v->over_flags_plane[mb_pos - 1])) {
  282. if (!s->first_slice_line && (v->condover == CONDOVER_ALL || v->pq >= 9 ||
  283. v->over_flags_plane[mb_pos - s->mb_stride - 1])) {
  284. v->vc1dsp.vc1_v_s_overlap(v->block[v->topleft_blk_idx][2],
  285. v->block[v->left_blk_idx][0]);
  286. v->vc1dsp.vc1_v_s_overlap(v->block[v->topleft_blk_idx][3],
  287. v->block[v->left_blk_idx][1]);
  288. if (!(s->flags & CODEC_FLAG_GRAY)) {
  289. v->vc1dsp.vc1_v_s_overlap(v->block[v->topleft_blk_idx][4],
  290. v->block[v->left_blk_idx][4]);
  291. v->vc1dsp.vc1_v_s_overlap(v->block[v->topleft_blk_idx][5],
  292. v->block[v->left_blk_idx][5]);
  293. }
  294. }
  295. v->vc1dsp.vc1_v_s_overlap(v->block[v->left_blk_idx][0],
  296. v->block[v->left_blk_idx][2]);
  297. v->vc1dsp.vc1_v_s_overlap(v->block[v->left_blk_idx][1],
  298. v->block[v->left_blk_idx][3]);
  299. }
  300. }
  301. /** Do motion compensation over 1 macroblock
  302. * Mostly adapted hpel_motion and qpel_motion from mpegvideo.c
  303. */
  304. static void vc1_mc_1mv(VC1Context *v, int dir)
  305. {
  306. MpegEncContext *s = &v->s;
  307. H264ChromaContext *h264chroma = &v->h264chroma;
  308. uint8_t *srcY, *srcU, *srcV;
  309. int dxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
  310. int v_edge_pos = s->v_edge_pos >> v->field_mode;
  311. int i;
  312. uint8_t (*luty)[256], (*lutuv)[256];
  313. int use_ic;
  314. if ((!v->field_mode ||
  315. (v->ref_field_type[dir] == 1 && v->cur_field_type == 1)) &&
  316. !v->s.last_picture.f->data[0])
  317. return;
  318. mx = s->mv[dir][0][0];
  319. my = s->mv[dir][0][1];
  320. // store motion vectors for further use in B frames
  321. if (s->pict_type == AV_PICTURE_TYPE_P) {
  322. for (i = 0; i < 4; i++) {
  323. s->current_picture.motion_val[1][s->block_index[i] + v->blocks_off][0] = mx;
  324. s->current_picture.motion_val[1][s->block_index[i] + v->blocks_off][1] = my;
  325. }
  326. }
  327. uvmx = (mx + ((mx & 3) == 3)) >> 1;
  328. uvmy = (my + ((my & 3) == 3)) >> 1;
  329. v->luma_mv[s->mb_x][0] = uvmx;
  330. v->luma_mv[s->mb_x][1] = uvmy;
  331. if (v->field_mode &&
  332. v->cur_field_type != v->ref_field_type[dir]) {
  333. my = my - 2 + 4 * v->cur_field_type;
  334. uvmy = uvmy - 2 + 4 * v->cur_field_type;
  335. }
  336. // fastuvmc shall be ignored for interlaced frame picture
  337. if (v->fastuvmc && (v->fcm != ILACE_FRAME)) {
  338. uvmx = uvmx + ((uvmx < 0) ? (uvmx & 1) : -(uvmx & 1));
  339. uvmy = uvmy + ((uvmy < 0) ? (uvmy & 1) : -(uvmy & 1));
  340. }
  341. if (!dir) {
  342. if (v->field_mode && (v->cur_field_type != v->ref_field_type[dir]) && v->second_field) {
  343. srcY = s->current_picture.f->data[0];
  344. srcU = s->current_picture.f->data[1];
  345. srcV = s->current_picture.f->data[2];
  346. luty = v->curr_luty;
  347. lutuv = v->curr_lutuv;
  348. use_ic = *v->curr_use_ic;
  349. } else {
  350. srcY = s->last_picture.f->data[0];
  351. srcU = s->last_picture.f->data[1];
  352. srcV = s->last_picture.f->data[2];
  353. luty = v->last_luty;
  354. lutuv = v->last_lutuv;
  355. use_ic = v->last_use_ic;
  356. }
  357. } else {
  358. srcY = s->next_picture.f->data[0];
  359. srcU = s->next_picture.f->data[1];
  360. srcV = s->next_picture.f->data[2];
  361. luty = v->next_luty;
  362. lutuv = v->next_lutuv;
  363. use_ic = v->next_use_ic;
  364. }
  365. if (!srcY || !srcU) {
  366. av_log(v->s.avctx, AV_LOG_ERROR, "Referenced frame missing.\n");
  367. return;
  368. }
  369. src_x = s->mb_x * 16 + (mx >> 2);
  370. src_y = s->mb_y * 16 + (my >> 2);
  371. uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
  372. uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
  373. if (v->profile != PROFILE_ADVANCED) {
  374. src_x = av_clip( src_x, -16, s->mb_width * 16);
  375. src_y = av_clip( src_y, -16, s->mb_height * 16);
  376. uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
  377. uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
  378. } else {
  379. src_x = av_clip( src_x, -17, s->avctx->coded_width);
  380. src_y = av_clip( src_y, -18, s->avctx->coded_height + 1);
  381. uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
  382. uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
  383. }
  384. srcY += src_y * s->linesize + src_x;
  385. srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
  386. srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
  387. if (v->field_mode && v->ref_field_type[dir]) {
  388. srcY += s->current_picture_ptr->f->linesize[0];
  389. srcU += s->current_picture_ptr->f->linesize[1];
  390. srcV += s->current_picture_ptr->f->linesize[2];
  391. }
  392. /* for grayscale we should not try to read from unknown area */
  393. if (s->flags & CODEC_FLAG_GRAY) {
  394. srcU = s->edge_emu_buffer + 18 * s->linesize;
  395. srcV = s->edge_emu_buffer + 18 * s->linesize;
  396. }
  397. if (v->rangeredfrm || use_ic
  398. || s->h_edge_pos < 22 || v_edge_pos < 22
  399. || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx&3) - 16 - s->mspel * 3
  400. || (unsigned)(src_y - 1) > v_edge_pos - (my&3) - 16 - 3) {
  401. uint8_t *ubuf = s->edge_emu_buffer + 19 * s->linesize;
  402. uint8_t *vbuf = ubuf + 9 * s->uvlinesize;
  403. srcY -= s->mspel * (1 + s->linesize);
  404. s->vdsp.emulated_edge_mc(s->edge_emu_buffer, srcY,
  405. s->linesize, s->linesize,
  406. 17 + s->mspel * 2, 17 + s->mspel * 2,
  407. src_x - s->mspel, src_y - s->mspel,
  408. s->h_edge_pos, v_edge_pos);
  409. srcY = s->edge_emu_buffer;
  410. s->vdsp.emulated_edge_mc(ubuf, srcU,
  411. s->uvlinesize, s->uvlinesize,
  412. 8 + 1, 8 + 1,
  413. uvsrc_x, uvsrc_y,
  414. s->h_edge_pos >> 1, v_edge_pos >> 1);
  415. s->vdsp.emulated_edge_mc(vbuf, srcV,
  416. s->uvlinesize, s->uvlinesize,
  417. 8 + 1, 8 + 1,
  418. uvsrc_x, uvsrc_y,
  419. s->h_edge_pos >> 1, v_edge_pos >> 1);
  420. srcU = ubuf;
  421. srcV = vbuf;
  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 (use_ic) {
  445. int i, j;
  446. uint8_t *src, *src2;
  447. src = srcY;
  448. for (j = 0; j < 17 + s->mspel * 2; j++) {
  449. int f = v->field_mode ? v->ref_field_type[dir] : ((j + src_y - s->mspel) & 1) ;
  450. for (i = 0; i < 17 + s->mspel * 2; i++)
  451. src[i] = luty[f][src[i]];
  452. src += s->linesize;
  453. }
  454. src = srcU;
  455. src2 = srcV;
  456. for (j = 0; j < 9; j++) {
  457. int f = v->field_mode ? v->ref_field_type[dir] : ((j + uvsrc_y) & 1);
  458. for (i = 0; i < 9; i++) {
  459. src[i] = lutuv[f][src[i]];
  460. src2[i] = lutuv[f][src2[i]];
  461. }
  462. src += s->uvlinesize;
  463. src2 += s->uvlinesize;
  464. }
  465. }
  466. srcY += s->mspel * (1 + s->linesize);
  467. }
  468. if (s->mspel) {
  469. dxy = ((my & 3) << 2) | (mx & 3);
  470. v->vc1dsp.put_vc1_mspel_pixels_tab[0][dxy](s->dest[0] , srcY , s->linesize, v->rnd);
  471. } else { // hpel mc - always used for luma
  472. dxy = (my & 2) | ((mx & 2) >> 1);
  473. if (!v->rnd)
  474. s->hdsp.put_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
  475. else
  476. s->hdsp.put_no_rnd_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
  477. }
  478. if (s->flags & CODEC_FLAG_GRAY) return;
  479. /* Chroma MC always uses qpel bilinear */
  480. uvmx = (uvmx & 3) << 1;
  481. uvmy = (uvmy & 3) << 1;
  482. if (!v->rnd) {
  483. h264chroma->put_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
  484. h264chroma->put_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
  485. } else {
  486. v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
  487. v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
  488. }
  489. }
  490. static inline int median4(int a, int b, int c, int d)
  491. {
  492. if (a < b) {
  493. if (c < d) return (FFMIN(b, d) + FFMAX(a, c)) / 2;
  494. else return (FFMIN(b, c) + FFMAX(a, d)) / 2;
  495. } else {
  496. if (c < d) return (FFMIN(a, d) + FFMAX(b, c)) / 2;
  497. else return (FFMIN(a, c) + FFMAX(b, d)) / 2;
  498. }
  499. }
  500. /** Do motion compensation for 4-MV macroblock - luminance block
  501. */
  502. static void vc1_mc_4mv_luma(VC1Context *v, int n, int dir, int avg)
  503. {
  504. MpegEncContext *s = &v->s;
  505. uint8_t *srcY;
  506. int dxy, mx, my, src_x, src_y;
  507. int off;
  508. int fieldmv = (v->fcm == ILACE_FRAME) ? v->blk_mv_type[s->block_index[n]] : 0;
  509. int v_edge_pos = s->v_edge_pos >> v->field_mode;
  510. uint8_t (*luty)[256];
  511. int use_ic;
  512. if ((!v->field_mode ||
  513. (v->ref_field_type[dir] == 1 && v->cur_field_type == 1)) &&
  514. !v->s.last_picture.f->data[0])
  515. return;
  516. mx = s->mv[dir][n][0];
  517. my = s->mv[dir][n][1];
  518. if (!dir) {
  519. if (v->field_mode && (v->cur_field_type != v->ref_field_type[dir]) && v->second_field) {
  520. srcY = s->current_picture.f->data[0];
  521. luty = v->curr_luty;
  522. use_ic = *v->curr_use_ic;
  523. } else {
  524. srcY = s->last_picture.f->data[0];
  525. luty = v->last_luty;
  526. use_ic = v->last_use_ic;
  527. }
  528. } else {
  529. srcY = s->next_picture.f->data[0];
  530. luty = v->next_luty;
  531. use_ic = v->next_use_ic;
  532. }
  533. if (!srcY) {
  534. av_log(v->s.avctx, AV_LOG_ERROR, "Referenced frame missing.\n");
  535. return;
  536. }
  537. if (v->field_mode) {
  538. if (v->cur_field_type != v->ref_field_type[dir])
  539. my = my - 2 + 4 * v->cur_field_type;
  540. }
  541. if (s->pict_type == AV_PICTURE_TYPE_P && n == 3 && v->field_mode) {
  542. int same_count = 0, opp_count = 0, k;
  543. int chosen_mv[2][4][2], f;
  544. int tx, ty;
  545. for (k = 0; k < 4; k++) {
  546. f = v->mv_f[0][s->block_index[k] + v->blocks_off];
  547. chosen_mv[f][f ? opp_count : same_count][0] = s->mv[0][k][0];
  548. chosen_mv[f][f ? opp_count : same_count][1] = s->mv[0][k][1];
  549. opp_count += f;
  550. same_count += 1 - f;
  551. }
  552. f = opp_count > same_count;
  553. switch (f ? opp_count : same_count) {
  554. case 4:
  555. tx = median4(chosen_mv[f][0][0], chosen_mv[f][1][0],
  556. chosen_mv[f][2][0], chosen_mv[f][3][0]);
  557. ty = median4(chosen_mv[f][0][1], chosen_mv[f][1][1],
  558. chosen_mv[f][2][1], chosen_mv[f][3][1]);
  559. break;
  560. case 3:
  561. tx = mid_pred(chosen_mv[f][0][0], chosen_mv[f][1][0], chosen_mv[f][2][0]);
  562. ty = mid_pred(chosen_mv[f][0][1], chosen_mv[f][1][1], chosen_mv[f][2][1]);
  563. break;
  564. case 2:
  565. tx = (chosen_mv[f][0][0] + chosen_mv[f][1][0]) / 2;
  566. ty = (chosen_mv[f][0][1] + chosen_mv[f][1][1]) / 2;
  567. break;
  568. default:
  569. av_assert0(0);
  570. }
  571. s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = tx;
  572. s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = ty;
  573. for (k = 0; k < 4; k++)
  574. v->mv_f[1][s->block_index[k] + v->blocks_off] = f;
  575. }
  576. if (v->fcm == ILACE_FRAME) { // not sure if needed for other types of picture
  577. int qx, qy;
  578. int width = s->avctx->coded_width;
  579. int height = s->avctx->coded_height >> 1;
  580. if (s->pict_type == AV_PICTURE_TYPE_P) {
  581. s->current_picture.motion_val[1][s->block_index[n] + v->blocks_off][0] = mx;
  582. s->current_picture.motion_val[1][s->block_index[n] + v->blocks_off][1] = my;
  583. }
  584. qx = (s->mb_x * 16) + (mx >> 2);
  585. qy = (s->mb_y * 8) + (my >> 3);
  586. if (qx < -17)
  587. mx -= 4 * (qx + 17);
  588. else if (qx > width)
  589. mx -= 4 * (qx - width);
  590. if (qy < -18)
  591. my -= 8 * (qy + 18);
  592. else if (qy > height + 1)
  593. my -= 8 * (qy - height - 1);
  594. }
  595. if ((v->fcm == ILACE_FRAME) && fieldmv)
  596. off = ((n > 1) ? s->linesize : 0) + (n & 1) * 8;
  597. else
  598. off = s->linesize * 4 * (n & 2) + (n & 1) * 8;
  599. src_x = s->mb_x * 16 + (n & 1) * 8 + (mx >> 2);
  600. if (!fieldmv)
  601. src_y = s->mb_y * 16 + (n & 2) * 4 + (my >> 2);
  602. else
  603. src_y = s->mb_y * 16 + ((n > 1) ? 1 : 0) + (my >> 2);
  604. if (v->profile != PROFILE_ADVANCED) {
  605. src_x = av_clip(src_x, -16, s->mb_width * 16);
  606. src_y = av_clip(src_y, -16, s->mb_height * 16);
  607. } else {
  608. src_x = av_clip(src_x, -17, s->avctx->coded_width);
  609. if (v->fcm == ILACE_FRAME) {
  610. if (src_y & 1)
  611. src_y = av_clip(src_y, -17, s->avctx->coded_height + 1);
  612. else
  613. src_y = av_clip(src_y, -18, s->avctx->coded_height);
  614. } else {
  615. src_y = av_clip(src_y, -18, s->avctx->coded_height + 1);
  616. }
  617. }
  618. srcY += src_y * s->linesize + src_x;
  619. if (v->field_mode && v->ref_field_type[dir])
  620. srcY += s->current_picture_ptr->f->linesize[0];
  621. if (fieldmv && !(src_y & 1))
  622. v_edge_pos--;
  623. if (fieldmv && (src_y & 1) && src_y < 4)
  624. src_y--;
  625. if (v->rangeredfrm || use_ic
  626. || s->h_edge_pos < 13 || v_edge_pos < 23
  627. || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx & 3) - 8 - s->mspel * 2
  628. || (unsigned)(src_y - (s->mspel << fieldmv)) > v_edge_pos - (my & 3) - ((8 + s->mspel * 2) << fieldmv)) {
  629. srcY -= s->mspel * (1 + (s->linesize << fieldmv));
  630. /* check emulate edge stride and offset */
  631. s->vdsp.emulated_edge_mc(s->edge_emu_buffer, srcY,
  632. s->linesize, s->linesize,
  633. 9 + s->mspel * 2, (9 + s->mspel * 2) << fieldmv,
  634. src_x - s->mspel, src_y - (s->mspel << fieldmv),
  635. s->h_edge_pos, v_edge_pos);
  636. srcY = s->edge_emu_buffer;
  637. /* if we deal with range reduction we need to scale source blocks */
  638. if (v->rangeredfrm) {
  639. int i, j;
  640. uint8_t *src;
  641. src = srcY;
  642. for (j = 0; j < 9 + s->mspel * 2; j++) {
  643. for (i = 0; i < 9 + s->mspel * 2; i++)
  644. src[i] = ((src[i] - 128) >> 1) + 128;
  645. src += s->linesize << fieldmv;
  646. }
  647. }
  648. /* if we deal with intensity compensation we need to scale source blocks */
  649. if (use_ic) {
  650. int i, j;
  651. uint8_t *src;
  652. src = srcY;
  653. for (j = 0; j < 9 + s->mspel * 2; j++) {
  654. int f = v->field_mode ? v->ref_field_type[dir] : (((j<<fieldmv)+src_y - (s->mspel << fieldmv)) & 1);
  655. for (i = 0; i < 9 + s->mspel * 2; i++)
  656. src[i] = luty[f][src[i]];
  657. src += s->linesize << fieldmv;
  658. }
  659. }
  660. srcY += s->mspel * (1 + (s->linesize << fieldmv));
  661. }
  662. if (s->mspel) {
  663. dxy = ((my & 3) << 2) | (mx & 3);
  664. if (avg)
  665. v->vc1dsp.avg_vc1_mspel_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize << fieldmv, v->rnd);
  666. else
  667. v->vc1dsp.put_vc1_mspel_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize << fieldmv, v->rnd);
  668. } else { // hpel mc - always used for luma
  669. dxy = (my & 2) | ((mx & 2) >> 1);
  670. if (!v->rnd)
  671. s->hdsp.put_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
  672. else
  673. s->hdsp.put_no_rnd_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
  674. }
  675. }
  676. static av_always_inline int get_chroma_mv(int *mvx, int *mvy, int *a, int flag, int *tx, int *ty)
  677. {
  678. int idx, i;
  679. static const int count[16] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4};
  680. idx = ((a[3] != flag) << 3)
  681. | ((a[2] != flag) << 2)
  682. | ((a[1] != flag) << 1)
  683. | (a[0] != flag);
  684. if (!idx) {
  685. *tx = median4(mvx[0], mvx[1], mvx[2], mvx[3]);
  686. *ty = median4(mvy[0], mvy[1], mvy[2], mvy[3]);
  687. return 4;
  688. } else if (count[idx] == 1) {
  689. switch (idx) {
  690. case 0x1:
  691. *tx = mid_pred(mvx[1], mvx[2], mvx[3]);
  692. *ty = mid_pred(mvy[1], mvy[2], mvy[3]);
  693. return 3;
  694. case 0x2:
  695. *tx = mid_pred(mvx[0], mvx[2], mvx[3]);
  696. *ty = mid_pred(mvy[0], mvy[2], mvy[3]);
  697. return 3;
  698. case 0x4:
  699. *tx = mid_pred(mvx[0], mvx[1], mvx[3]);
  700. *ty = mid_pred(mvy[0], mvy[1], mvy[3]);
  701. return 3;
  702. case 0x8:
  703. *tx = mid_pred(mvx[0], mvx[1], mvx[2]);
  704. *ty = mid_pred(mvy[0], mvy[1], mvy[2]);
  705. return 3;
  706. }
  707. } else if (count[idx] == 2) {
  708. int t1 = 0, t2 = 0;
  709. for (i = 0; i < 3; i++)
  710. if (!a[i]) {
  711. t1 = i;
  712. break;
  713. }
  714. for (i = t1 + 1; i < 4; i++)
  715. if (!a[i]) {
  716. t2 = i;
  717. break;
  718. }
  719. *tx = (mvx[t1] + mvx[t2]) / 2;
  720. *ty = (mvy[t1] + mvy[t2]) / 2;
  721. return 2;
  722. } else {
  723. return 0;
  724. }
  725. return -1;
  726. }
  727. /** Do motion compensation for 4-MV macroblock - both chroma blocks
  728. */
  729. static void vc1_mc_4mv_chroma(VC1Context *v, int dir)
  730. {
  731. MpegEncContext *s = &v->s;
  732. H264ChromaContext *h264chroma = &v->h264chroma;
  733. uint8_t *srcU, *srcV;
  734. int uvmx, uvmy, uvsrc_x, uvsrc_y;
  735. int k, tx = 0, ty = 0;
  736. int mvx[4], mvy[4], intra[4], mv_f[4];
  737. int valid_count;
  738. int chroma_ref_type = v->cur_field_type;
  739. int v_edge_pos = s->v_edge_pos >> v->field_mode;
  740. uint8_t (*lutuv)[256];
  741. int use_ic;
  742. if (!v->field_mode && !v->s.last_picture.f->data[0])
  743. return;
  744. if (s->flags & CODEC_FLAG_GRAY)
  745. return;
  746. for (k = 0; k < 4; k++) {
  747. mvx[k] = s->mv[dir][k][0];
  748. mvy[k] = s->mv[dir][k][1];
  749. intra[k] = v->mb_type[0][s->block_index[k]];
  750. if (v->field_mode)
  751. mv_f[k] = v->mv_f[dir][s->block_index[k] + v->blocks_off];
  752. }
  753. /* calculate chroma MV vector from four luma MVs */
  754. if (!v->field_mode || (v->field_mode && !v->numref)) {
  755. valid_count = get_chroma_mv(mvx, mvy, intra, 0, &tx, &ty);
  756. chroma_ref_type = v->reffield;
  757. if (!valid_count) {
  758. s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
  759. s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
  760. v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
  761. return; //no need to do MC for intra blocks
  762. }
  763. } else {
  764. int dominant = 0;
  765. if (mv_f[0] + mv_f[1] + mv_f[2] + mv_f[3] > 2)
  766. dominant = 1;
  767. valid_count = get_chroma_mv(mvx, mvy, mv_f, dominant, &tx, &ty);
  768. if (dominant)
  769. chroma_ref_type = !v->cur_field_type;
  770. }
  771. if (v->field_mode && chroma_ref_type == 1 && v->cur_field_type == 1 && !v->s.last_picture.f->data[0])
  772. return;
  773. s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = tx;
  774. s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = ty;
  775. uvmx = (tx + ((tx & 3) == 3)) >> 1;
  776. uvmy = (ty + ((ty & 3) == 3)) >> 1;
  777. v->luma_mv[s->mb_x][0] = uvmx;
  778. v->luma_mv[s->mb_x][1] = uvmy;
  779. if (v->fastuvmc) {
  780. uvmx = uvmx + ((uvmx < 0) ? (uvmx & 1) : -(uvmx & 1));
  781. uvmy = uvmy + ((uvmy < 0) ? (uvmy & 1) : -(uvmy & 1));
  782. }
  783. // Field conversion bias
  784. if (v->cur_field_type != chroma_ref_type)
  785. uvmy += 2 - 4 * chroma_ref_type;
  786. uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
  787. uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
  788. if (v->profile != PROFILE_ADVANCED) {
  789. uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
  790. uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
  791. } else {
  792. uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
  793. uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
  794. }
  795. if (!dir) {
  796. if (v->field_mode && (v->cur_field_type != chroma_ref_type) && v->second_field) {
  797. srcU = s->current_picture.f->data[1];
  798. srcV = s->current_picture.f->data[2];
  799. lutuv = v->curr_lutuv;
  800. use_ic = *v->curr_use_ic;
  801. } else {
  802. srcU = s->last_picture.f->data[1];
  803. srcV = s->last_picture.f->data[2];
  804. lutuv = v->last_lutuv;
  805. use_ic = v->last_use_ic;
  806. }
  807. } else {
  808. srcU = s->next_picture.f->data[1];
  809. srcV = s->next_picture.f->data[2];
  810. lutuv = v->next_lutuv;
  811. use_ic = v->next_use_ic;
  812. }
  813. if (!srcU) {
  814. av_log(v->s.avctx, AV_LOG_ERROR, "Referenced frame missing.\n");
  815. return;
  816. }
  817. srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
  818. srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
  819. if (v->field_mode) {
  820. if (chroma_ref_type) {
  821. srcU += s->current_picture_ptr->f->linesize[1];
  822. srcV += s->current_picture_ptr->f->linesize[2];
  823. }
  824. }
  825. if (v->rangeredfrm || use_ic
  826. || s->h_edge_pos < 18 || v_edge_pos < 18
  827. || (unsigned)uvsrc_x > (s->h_edge_pos >> 1) - 9
  828. || (unsigned)uvsrc_y > (v_edge_pos >> 1) - 9) {
  829. s->vdsp.emulated_edge_mc(s->edge_emu_buffer, srcU,
  830. s->uvlinesize, s->uvlinesize,
  831. 8 + 1, 8 + 1, uvsrc_x, uvsrc_y,
  832. s->h_edge_pos >> 1, v_edge_pos >> 1);
  833. s->vdsp.emulated_edge_mc(s->edge_emu_buffer + 16, srcV,
  834. s->uvlinesize, s->uvlinesize,
  835. 8 + 1, 8 + 1, uvsrc_x, uvsrc_y,
  836. s->h_edge_pos >> 1, v_edge_pos >> 1);
  837. srcU = s->edge_emu_buffer;
  838. srcV = s->edge_emu_buffer + 16;
  839. /* if we deal with range reduction we need to scale source blocks */
  840. if (v->rangeredfrm) {
  841. int i, j;
  842. uint8_t *src, *src2;
  843. src = srcU;
  844. src2 = srcV;
  845. for (j = 0; j < 9; j++) {
  846. for (i = 0; i < 9; i++) {
  847. src[i] = ((src[i] - 128) >> 1) + 128;
  848. src2[i] = ((src2[i] - 128) >> 1) + 128;
  849. }
  850. src += s->uvlinesize;
  851. src2 += s->uvlinesize;
  852. }
  853. }
  854. /* if we deal with intensity compensation we need to scale source blocks */
  855. if (use_ic) {
  856. int i, j;
  857. uint8_t *src, *src2;
  858. src = srcU;
  859. src2 = srcV;
  860. for (j = 0; j < 9; j++) {
  861. int f = v->field_mode ? chroma_ref_type : ((j + uvsrc_y) & 1);
  862. for (i = 0; i < 9; i++) {
  863. src[i] = lutuv[f][src[i]];
  864. src2[i] = lutuv[f][src2[i]];
  865. }
  866. src += s->uvlinesize;
  867. src2 += s->uvlinesize;
  868. }
  869. }
  870. }
  871. /* Chroma MC always uses qpel bilinear */
  872. uvmx = (uvmx & 3) << 1;
  873. uvmy = (uvmy & 3) << 1;
  874. if (!v->rnd) {
  875. h264chroma->put_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
  876. h264chroma->put_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
  877. } else {
  878. v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
  879. v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
  880. }
  881. }
  882. /** Do motion compensation for 4-MV interlaced frame chroma macroblock (both U and V)
  883. */
  884. static void vc1_mc_4mv_chroma4(VC1Context *v, int dir, int dir2, int avg)
  885. {
  886. MpegEncContext *s = &v->s;
  887. H264ChromaContext *h264chroma = &v->h264chroma;
  888. uint8_t *srcU, *srcV;
  889. int uvsrc_x, uvsrc_y;
  890. int uvmx_field[4], uvmy_field[4];
  891. int i, off, tx, ty;
  892. int fieldmv = v->blk_mv_type[s->block_index[0]];
  893. static const int s_rndtblfield[16] = { 0, 0, 1, 2, 4, 4, 5, 6, 2, 2, 3, 8, 6, 6, 7, 12 };
  894. int v_dist = fieldmv ? 1 : 4; // vertical offset for lower sub-blocks
  895. int v_edge_pos = s->v_edge_pos >> 1;
  896. int use_ic;
  897. uint8_t (*lutuv)[256];
  898. if (s->flags & CODEC_FLAG_GRAY)
  899. return;
  900. for (i = 0; i < 4; i++) {
  901. int d = i < 2 ? dir: dir2;
  902. tx = s->mv[d][i][0];
  903. uvmx_field[i] = (tx + ((tx & 3) == 3)) >> 1;
  904. ty = s->mv[d][i][1];
  905. if (fieldmv)
  906. uvmy_field[i] = (ty >> 4) * 8 + s_rndtblfield[ty & 0xF];
  907. else
  908. uvmy_field[i] = (ty + ((ty & 3) == 3)) >> 1;
  909. }
  910. for (i = 0; i < 4; i++) {
  911. off = (i & 1) * 4 + ((i & 2) ? v_dist * s->uvlinesize : 0);
  912. uvsrc_x = s->mb_x * 8 + (i & 1) * 4 + (uvmx_field[i] >> 2);
  913. uvsrc_y = s->mb_y * 8 + ((i & 2) ? v_dist : 0) + (uvmy_field[i] >> 2);
  914. // FIXME: implement proper pull-back (see vc1cropmv.c, vc1CROPMV_ChromaPullBack())
  915. uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
  916. uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
  917. if (i < 2 ? dir : dir2) {
  918. srcU = s->next_picture.f->data[1];
  919. srcV = s->next_picture.f->data[2];
  920. lutuv = v->next_lutuv;
  921. use_ic = v->next_use_ic;
  922. } else {
  923. srcU = s->last_picture.f->data[1];
  924. srcV = s->last_picture.f->data[2];
  925. lutuv = v->last_lutuv;
  926. use_ic = v->last_use_ic;
  927. }
  928. if (!srcU)
  929. return;
  930. srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
  931. srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
  932. uvmx_field[i] = (uvmx_field[i] & 3) << 1;
  933. uvmy_field[i] = (uvmy_field[i] & 3) << 1;
  934. if (fieldmv && !(uvsrc_y & 1))
  935. v_edge_pos = (s->v_edge_pos >> 1) - 1;
  936. if (fieldmv && (uvsrc_y & 1) && uvsrc_y < 2)
  937. uvsrc_y--;
  938. if (use_ic
  939. || s->h_edge_pos < 10 || v_edge_pos < (5 << fieldmv)
  940. || (unsigned)uvsrc_x > (s->h_edge_pos >> 1) - 5
  941. || (unsigned)uvsrc_y > v_edge_pos - (5 << fieldmv)) {
  942. s->vdsp.emulated_edge_mc(s->edge_emu_buffer, srcU,
  943. s->uvlinesize, s->uvlinesize,
  944. 5, (5 << fieldmv), uvsrc_x, uvsrc_y,
  945. s->h_edge_pos >> 1, v_edge_pos);
  946. s->vdsp.emulated_edge_mc(s->edge_emu_buffer + 16, srcV,
  947. s->uvlinesize, s->uvlinesize,
  948. 5, (5 << fieldmv), uvsrc_x, uvsrc_y,
  949. s->h_edge_pos >> 1, v_edge_pos);
  950. srcU = s->edge_emu_buffer;
  951. srcV = s->edge_emu_buffer + 16;
  952. /* if we deal with intensity compensation we need to scale source blocks */
  953. if (use_ic) {
  954. int i, j;
  955. uint8_t *src, *src2;
  956. src = srcU;
  957. src2 = srcV;
  958. for (j = 0; j < 5; j++) {
  959. int f = (uvsrc_y + (j << fieldmv)) & 1;
  960. for (i = 0; i < 5; i++) {
  961. src[i] = lutuv[f][src[i]];
  962. src2[i] = lutuv[f][src2[i]];
  963. }
  964. src += s->uvlinesize << fieldmv;
  965. src2 += s->uvlinesize << fieldmv;
  966. }
  967. }
  968. }
  969. if (avg) {
  970. if (!v->rnd) {
  971. h264chroma->avg_h264_chroma_pixels_tab[1](s->dest[1] + off, srcU, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
  972. h264chroma->avg_h264_chroma_pixels_tab[1](s->dest[2] + off, srcV, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
  973. } else {
  974. 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]);
  975. 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]);
  976. }
  977. } else {
  978. if (!v->rnd) {
  979. h264chroma->put_h264_chroma_pixels_tab[1](s->dest[1] + off, srcU, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
  980. h264chroma->put_h264_chroma_pixels_tab[1](s->dest[2] + off, srcV, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
  981. } else {
  982. 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]);
  983. 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]);
  984. }
  985. }
  986. }
  987. }
  988. /***********************************************************************/
  989. /**
  990. * @name VC-1 Block-level functions
  991. * @see 7.1.4, p91 and 8.1.1.7, p(1)04
  992. * @{
  993. */
  994. /**
  995. * @def GET_MQUANT
  996. * @brief Get macroblock-level quantizer scale
  997. */
  998. #define GET_MQUANT() \
  999. if (v->dquantfrm) { \
  1000. int edges = 0; \
  1001. if (v->dqprofile == DQPROFILE_ALL_MBS) { \
  1002. if (v->dqbilevel) { \
  1003. mquant = (get_bits1(gb)) ? v->altpq : v->pq; \
  1004. } else { \
  1005. mqdiff = get_bits(gb, 3); \
  1006. if (mqdiff != 7) \
  1007. mquant = v->pq + mqdiff; \
  1008. else \
  1009. mquant = get_bits(gb, 5); \
  1010. } \
  1011. } \
  1012. if (v->dqprofile == DQPROFILE_SINGLE_EDGE) \
  1013. edges = 1 << v->dqsbedge; \
  1014. else if (v->dqprofile == DQPROFILE_DOUBLE_EDGES) \
  1015. edges = (3 << v->dqsbedge) % 15; \
  1016. else if (v->dqprofile == DQPROFILE_FOUR_EDGES) \
  1017. edges = 15; \
  1018. if ((edges&1) && !s->mb_x) \
  1019. mquant = v->altpq; \
  1020. if ((edges&2) && s->first_slice_line) \
  1021. mquant = v->altpq; \
  1022. if ((edges&4) && s->mb_x == (s->mb_width - 1)) \
  1023. mquant = v->altpq; \
  1024. if ((edges&8) && s->mb_y == (s->mb_height - 1)) \
  1025. mquant = v->altpq; \
  1026. if (!mquant || mquant > 31) { \
  1027. av_log(v->s.avctx, AV_LOG_ERROR, \
  1028. "Overriding invalid mquant %d\n", mquant); \
  1029. mquant = 1; \
  1030. } \
  1031. }
  1032. /**
  1033. * @def GET_MVDATA(_dmv_x, _dmv_y)
  1034. * @brief Get MV differentials
  1035. * @see MVDATA decoding from 8.3.5.2, p(1)20
  1036. * @param _dmv_x Horizontal differential for decoded MV
  1037. * @param _dmv_y Vertical differential for decoded MV
  1038. */
  1039. #define GET_MVDATA(_dmv_x, _dmv_y) \
  1040. index = 1 + get_vlc2(gb, ff_vc1_mv_diff_vlc[s->mv_table_index].table, \
  1041. VC1_MV_DIFF_VLC_BITS, 2); \
  1042. if (index > 36) { \
  1043. mb_has_coeffs = 1; \
  1044. index -= 37; \
  1045. } else \
  1046. mb_has_coeffs = 0; \
  1047. s->mb_intra = 0; \
  1048. if (!index) { \
  1049. _dmv_x = _dmv_y = 0; \
  1050. } else if (index == 35) { \
  1051. _dmv_x = get_bits(gb, v->k_x - 1 + s->quarter_sample); \
  1052. _dmv_y = get_bits(gb, v->k_y - 1 + s->quarter_sample); \
  1053. } else if (index == 36) { \
  1054. _dmv_x = 0; \
  1055. _dmv_y = 0; \
  1056. s->mb_intra = 1; \
  1057. } else { \
  1058. index1 = index % 6; \
  1059. if (!s->quarter_sample && index1 == 5) val = 1; \
  1060. else val = 0; \
  1061. if (size_table[index1] - val > 0) \
  1062. val = get_bits(gb, size_table[index1] - val); \
  1063. else val = 0; \
  1064. sign = 0 - (val&1); \
  1065. _dmv_x = (sign ^ ((val>>1) + offset_table[index1])) - sign; \
  1066. \
  1067. index1 = index / 6; \
  1068. if (!s->quarter_sample && index1 == 5) val = 1; \
  1069. else val = 0; \
  1070. if (size_table[index1] - val > 0) \
  1071. val = get_bits(gb, size_table[index1] - val); \
  1072. else val = 0; \
  1073. sign = 0 - (val & 1); \
  1074. _dmv_y = (sign ^ ((val >> 1) + offset_table[index1])) - sign; \
  1075. }
  1076. static av_always_inline void get_mvdata_interlaced(VC1Context *v, int *dmv_x,
  1077. int *dmv_y, int *pred_flag)
  1078. {
  1079. int index, index1;
  1080. int extend_x = 0, extend_y = 0;
  1081. GetBitContext *gb = &v->s.gb;
  1082. int bits, esc;
  1083. int val, sign;
  1084. const int* offs_tab;
  1085. if (v->numref) {
  1086. bits = VC1_2REF_MVDATA_VLC_BITS;
  1087. esc = 125;
  1088. } else {
  1089. bits = VC1_1REF_MVDATA_VLC_BITS;
  1090. esc = 71;
  1091. }
  1092. switch (v->dmvrange) {
  1093. case 1:
  1094. extend_x = 1;
  1095. break;
  1096. case 2:
  1097. extend_y = 1;
  1098. break;
  1099. case 3:
  1100. extend_x = extend_y = 1;
  1101. break;
  1102. }
  1103. index = get_vlc2(gb, v->imv_vlc->table, bits, 3);
  1104. if (index == esc) {
  1105. *dmv_x = get_bits(gb, v->k_x);
  1106. *dmv_y = get_bits(gb, v->k_y);
  1107. if (v->numref) {
  1108. if (pred_flag) {
  1109. *pred_flag = *dmv_y & 1;
  1110. *dmv_y = (*dmv_y + *pred_flag) >> 1;
  1111. } else {
  1112. *dmv_y = (*dmv_y + (*dmv_y & 1)) >> 1;
  1113. }
  1114. }
  1115. }
  1116. else {
  1117. av_assert0(index < esc);
  1118. if (extend_x)
  1119. offs_tab = offset_table2;
  1120. else
  1121. offs_tab = offset_table1;
  1122. index1 = (index + 1) % 9;
  1123. if (index1 != 0) {
  1124. val = get_bits(gb, index1 + extend_x);
  1125. sign = 0 -(val & 1);
  1126. *dmv_x = (sign ^ ((val >> 1) + offs_tab[index1])) - sign;
  1127. } else
  1128. *dmv_x = 0;
  1129. if (extend_y)
  1130. offs_tab = offset_table2;
  1131. else
  1132. offs_tab = offset_table1;
  1133. index1 = (index + 1) / 9;
  1134. if (index1 > v->numref) {
  1135. val = get_bits(gb, (index1 + (extend_y << v->numref)) >> v->numref);
  1136. sign = 0 - (val & 1);
  1137. *dmv_y = (sign ^ ((val >> 1) + offs_tab[index1 >> v->numref])) - sign;
  1138. } else
  1139. *dmv_y = 0;
  1140. if (v->numref && pred_flag)
  1141. *pred_flag = index1 & 1;
  1142. }
  1143. }
  1144. static av_always_inline int scaleforsame_x(VC1Context *v, int n /* MV */, int dir)
  1145. {
  1146. int scaledvalue, refdist;
  1147. int scalesame1, scalesame2;
  1148. int scalezone1_x, zone1offset_x;
  1149. int table_index = dir ^ v->second_field;
  1150. if (v->s.pict_type != AV_PICTURE_TYPE_B)
  1151. refdist = v->refdist;
  1152. else
  1153. refdist = dir ? v->brfd : v->frfd;
  1154. if (refdist > 3)
  1155. refdist = 3;
  1156. scalesame1 = ff_vc1_field_mvpred_scales[table_index][1][refdist];
  1157. scalesame2 = ff_vc1_field_mvpred_scales[table_index][2][refdist];
  1158. scalezone1_x = ff_vc1_field_mvpred_scales[table_index][3][refdist];
  1159. zone1offset_x = ff_vc1_field_mvpred_scales[table_index][5][refdist];
  1160. if (FFABS(n) > 255)
  1161. scaledvalue = n;
  1162. else {
  1163. if (FFABS(n) < scalezone1_x)
  1164. scaledvalue = (n * scalesame1) >> 8;
  1165. else {
  1166. if (n < 0)
  1167. scaledvalue = ((n * scalesame2) >> 8) - zone1offset_x;
  1168. else
  1169. scaledvalue = ((n * scalesame2) >> 8) + zone1offset_x;
  1170. }
  1171. }
  1172. return av_clip(scaledvalue, -v->range_x, v->range_x - 1);
  1173. }
  1174. static av_always_inline int scaleforsame_y(VC1Context *v, int i, int n /* MV */, int dir)
  1175. {
  1176. int scaledvalue, refdist;
  1177. int scalesame1, scalesame2;
  1178. int scalezone1_y, zone1offset_y;
  1179. int table_index = dir ^ v->second_field;
  1180. if (v->s.pict_type != AV_PICTURE_TYPE_B)
  1181. refdist = v->refdist;
  1182. else
  1183. refdist = dir ? v->brfd : v->frfd;
  1184. if (refdist > 3)
  1185. refdist = 3;
  1186. scalesame1 = ff_vc1_field_mvpred_scales[table_index][1][refdist];
  1187. scalesame2 = ff_vc1_field_mvpred_scales[table_index][2][refdist];
  1188. scalezone1_y = ff_vc1_field_mvpred_scales[table_index][4][refdist];
  1189. zone1offset_y = ff_vc1_field_mvpred_scales[table_index][6][refdist];
  1190. if (FFABS(n) > 63)
  1191. scaledvalue = n;
  1192. else {
  1193. if (FFABS(n) < scalezone1_y)
  1194. scaledvalue = (n * scalesame1) >> 8;
  1195. else {
  1196. if (n < 0)
  1197. scaledvalue = ((n * scalesame2) >> 8) - zone1offset_y;
  1198. else
  1199. scaledvalue = ((n * scalesame2) >> 8) + zone1offset_y;
  1200. }
  1201. }
  1202. if (v->cur_field_type && !v->ref_field_type[dir])
  1203. return av_clip(scaledvalue, -v->range_y / 2 + 1, v->range_y / 2);
  1204. else
  1205. return av_clip(scaledvalue, -v->range_y / 2, v->range_y / 2 - 1);
  1206. }
  1207. static av_always_inline int scaleforopp_x(VC1Context *v, int n /* MV */)
  1208. {
  1209. int scalezone1_x, zone1offset_x;
  1210. int scaleopp1, scaleopp2, brfd;
  1211. int scaledvalue;
  1212. brfd = FFMIN(v->brfd, 3);
  1213. scalezone1_x = ff_vc1_b_field_mvpred_scales[3][brfd];
  1214. zone1offset_x = ff_vc1_b_field_mvpred_scales[5][brfd];
  1215. scaleopp1 = ff_vc1_b_field_mvpred_scales[1][brfd];
  1216. scaleopp2 = ff_vc1_b_field_mvpred_scales[2][brfd];
  1217. if (FFABS(n) > 255)
  1218. scaledvalue = n;
  1219. else {
  1220. if (FFABS(n) < scalezone1_x)
  1221. scaledvalue = (n * scaleopp1) >> 8;
  1222. else {
  1223. if (n < 0)
  1224. scaledvalue = ((n * scaleopp2) >> 8) - zone1offset_x;
  1225. else
  1226. scaledvalue = ((n * scaleopp2) >> 8) + zone1offset_x;
  1227. }
  1228. }
  1229. return av_clip(scaledvalue, -v->range_x, v->range_x - 1);
  1230. }
  1231. static av_always_inline int scaleforopp_y(VC1Context *v, int n /* MV */, int dir)
  1232. {
  1233. int scalezone1_y, zone1offset_y;
  1234. int scaleopp1, scaleopp2, brfd;
  1235. int scaledvalue;
  1236. brfd = FFMIN(v->brfd, 3);
  1237. scalezone1_y = ff_vc1_b_field_mvpred_scales[4][brfd];
  1238. zone1offset_y = ff_vc1_b_field_mvpred_scales[6][brfd];
  1239. scaleopp1 = ff_vc1_b_field_mvpred_scales[1][brfd];
  1240. scaleopp2 = ff_vc1_b_field_mvpred_scales[2][brfd];
  1241. if (FFABS(n) > 63)
  1242. scaledvalue = n;
  1243. else {
  1244. if (FFABS(n) < scalezone1_y)
  1245. scaledvalue = (n * scaleopp1) >> 8;
  1246. else {
  1247. if (n < 0)
  1248. scaledvalue = ((n * scaleopp2) >> 8) - zone1offset_y;
  1249. else
  1250. scaledvalue = ((n * scaleopp2) >> 8) + zone1offset_y;
  1251. }
  1252. }
  1253. if (v->cur_field_type && !v->ref_field_type[dir]) {
  1254. return av_clip(scaledvalue, -v->range_y / 2 + 1, v->range_y / 2);
  1255. } else {
  1256. return av_clip(scaledvalue, -v->range_y / 2, v->range_y / 2 - 1);
  1257. }
  1258. }
  1259. static av_always_inline int scaleforsame(VC1Context *v, int i, int n /* MV */,
  1260. int dim, int dir)
  1261. {
  1262. int brfd, scalesame;
  1263. int hpel = 1 - v->s.quarter_sample;
  1264. n >>= hpel;
  1265. if (v->s.pict_type != AV_PICTURE_TYPE_B || v->second_field || !dir) {
  1266. if (dim)
  1267. n = scaleforsame_y(v, i, n, dir) << hpel;
  1268. else
  1269. n = scaleforsame_x(v, n, dir) << hpel;
  1270. return n;
  1271. }
  1272. brfd = FFMIN(v->brfd, 3);
  1273. scalesame = ff_vc1_b_field_mvpred_scales[0][brfd];
  1274. n = (n * scalesame >> 8) << hpel;
  1275. return n;
  1276. }
  1277. static av_always_inline int scaleforopp(VC1Context *v, int n /* MV */,
  1278. int dim, int dir)
  1279. {
  1280. int refdist, scaleopp;
  1281. int hpel = 1 - v->s.quarter_sample;
  1282. n >>= hpel;
  1283. if (v->s.pict_type == AV_PICTURE_TYPE_B && !v->second_field && dir == 1) {
  1284. if (dim)
  1285. n = scaleforopp_y(v, n, dir) << hpel;
  1286. else
  1287. n = scaleforopp_x(v, n) << hpel;
  1288. return n;
  1289. }
  1290. if (v->s.pict_type != AV_PICTURE_TYPE_B)
  1291. refdist = FFMIN(v->refdist, 3);
  1292. else
  1293. refdist = dir ? v->brfd : v->frfd;
  1294. scaleopp = ff_vc1_field_mvpred_scales[dir ^ v->second_field][0][refdist];
  1295. n = (n * scaleopp >> 8) << hpel;
  1296. return n;
  1297. }
  1298. /** Predict and set motion vector
  1299. */
  1300. static inline void vc1_pred_mv(VC1Context *v, int n, int dmv_x, int dmv_y,
  1301. int mv1, int r_x, int r_y, uint8_t* is_intra,
  1302. int pred_flag, int dir)
  1303. {
  1304. MpegEncContext *s = &v->s;
  1305. int xy, wrap, off = 0;
  1306. int16_t *A, *B, *C;
  1307. int px, py;
  1308. int sum;
  1309. int mixedmv_pic, num_samefield = 0, num_oppfield = 0;
  1310. int opposite, a_f, b_f, c_f;
  1311. int16_t field_predA[2];
  1312. int16_t field_predB[2];
  1313. int16_t field_predC[2];
  1314. int a_valid, b_valid, c_valid;
  1315. int hybridmv_thresh, y_bias = 0;
  1316. if (v->mv_mode == MV_PMODE_MIXED_MV ||
  1317. ((v->mv_mode == MV_PMODE_INTENSITY_COMP) && (v->mv_mode2 == MV_PMODE_MIXED_MV)))
  1318. mixedmv_pic = 1;
  1319. else
  1320. mixedmv_pic = 0;
  1321. /* scale MV difference to be quad-pel */
  1322. dmv_x <<= 1 - s->quarter_sample;
  1323. dmv_y <<= 1 - s->quarter_sample;
  1324. wrap = s->b8_stride;
  1325. xy = s->block_index[n];
  1326. if (s->mb_intra) {
  1327. s->mv[0][n][0] = s->current_picture.motion_val[0][xy + v->blocks_off][0] = 0;
  1328. s->mv[0][n][1] = s->current_picture.motion_val[0][xy + v->blocks_off][1] = 0;
  1329. s->current_picture.motion_val[1][xy + v->blocks_off][0] = 0;
  1330. s->current_picture.motion_val[1][xy + v->blocks_off][1] = 0;
  1331. if (mv1) { /* duplicate motion data for 1-MV block */
  1332. s->current_picture.motion_val[0][xy + 1 + v->blocks_off][0] = 0;
  1333. s->current_picture.motion_val[0][xy + 1 + v->blocks_off][1] = 0;
  1334. s->current_picture.motion_val[0][xy + wrap + v->blocks_off][0] = 0;
  1335. s->current_picture.motion_val[0][xy + wrap + v->blocks_off][1] = 0;
  1336. s->current_picture.motion_val[0][xy + wrap + 1 + v->blocks_off][0] = 0;
  1337. s->current_picture.motion_val[0][xy + wrap + 1 + v->blocks_off][1] = 0;
  1338. v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
  1339. s->current_picture.motion_val[1][xy + 1 + v->blocks_off][0] = 0;
  1340. s->current_picture.motion_val[1][xy + 1 + v->blocks_off][1] = 0;
  1341. s->current_picture.motion_val[1][xy + wrap][0] = 0;
  1342. s->current_picture.motion_val[1][xy + wrap + v->blocks_off][1] = 0;
  1343. s->current_picture.motion_val[1][xy + wrap + 1 + v->blocks_off][0] = 0;
  1344. s->current_picture.motion_val[1][xy + wrap + 1 + v->blocks_off][1] = 0;
  1345. }
  1346. return;
  1347. }
  1348. C = s->current_picture.motion_val[dir][xy - 1 + v->blocks_off];
  1349. A = s->current_picture.motion_val[dir][xy - wrap + v->blocks_off];
  1350. if (mv1) {
  1351. if (v->field_mode && mixedmv_pic)
  1352. off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
  1353. else
  1354. off = (s->mb_x == (s->mb_width - 1)) ? -1 : 2;
  1355. } else {
  1356. //in 4-MV mode different blocks have different B predictor position
  1357. switch (n) {
  1358. case 0:
  1359. off = (s->mb_x > 0) ? -1 : 1;
  1360. break;
  1361. case 1:
  1362. off = (s->mb_x == (s->mb_width - 1)) ? -1 : 1;
  1363. break;
  1364. case 2:
  1365. off = 1;
  1366. break;
  1367. case 3:
  1368. off = -1;
  1369. }
  1370. }
  1371. B = s->current_picture.motion_val[dir][xy - wrap + off + v->blocks_off];
  1372. a_valid = !s->first_slice_line || (n == 2 || n == 3);
  1373. b_valid = a_valid && (s->mb_width > 1);
  1374. c_valid = s->mb_x || (n == 1 || n == 3);
  1375. if (v->field_mode) {
  1376. a_valid = a_valid && !is_intra[xy - wrap];
  1377. b_valid = b_valid && !is_intra[xy - wrap + off];
  1378. c_valid = c_valid && !is_intra[xy - 1];
  1379. }
  1380. if (a_valid) {
  1381. a_f = v->mv_f[dir][xy - wrap + v->blocks_off];
  1382. num_oppfield += a_f;
  1383. num_samefield += 1 - a_f;
  1384. field_predA[0] = A[0];
  1385. field_predA[1] = A[1];
  1386. } else {
  1387. field_predA[0] = field_predA[1] = 0;
  1388. a_f = 0;
  1389. }
  1390. if (b_valid) {
  1391. b_f = v->mv_f[dir][xy - wrap + off + v->blocks_off];
  1392. num_oppfield += b_f;
  1393. num_samefield += 1 - b_f;
  1394. field_predB[0] = B[0];
  1395. field_predB[1] = B[1];
  1396. } else {
  1397. field_predB[0] = field_predB[1] = 0;
  1398. b_f = 0;
  1399. }
  1400. if (c_valid) {
  1401. c_f = v->mv_f[dir][xy - 1 + v->blocks_off];
  1402. num_oppfield += c_f;
  1403. num_samefield += 1 - c_f;
  1404. field_predC[0] = C[0];
  1405. field_predC[1] = C[1];
  1406. } else {
  1407. field_predC[0] = field_predC[1] = 0;
  1408. c_f = 0;
  1409. }
  1410. if (v->field_mode) {
  1411. if (!v->numref)
  1412. // REFFIELD determines if the last field or the second-last field is
  1413. // to be used as reference
  1414. opposite = 1 - v->reffield;
  1415. else {
  1416. if (num_samefield <= num_oppfield)
  1417. opposite = 1 - pred_flag;
  1418. else
  1419. opposite = pred_flag;
  1420. }
  1421. } else
  1422. opposite = 0;
  1423. if (opposite) {
  1424. if (a_valid && !a_f) {
  1425. field_predA[0] = scaleforopp(v, field_predA[0], 0, dir);
  1426. field_predA[1] = scaleforopp(v, field_predA[1], 1, dir);
  1427. }
  1428. if (b_valid && !b_f) {
  1429. field_predB[0] = scaleforopp(v, field_predB[0], 0, dir);
  1430. field_predB[1] = scaleforopp(v, field_predB[1], 1, dir);
  1431. }
  1432. if (c_valid && !c_f) {
  1433. field_predC[0] = scaleforopp(v, field_predC[0], 0, dir);
  1434. field_predC[1] = scaleforopp(v, field_predC[1], 1, dir);
  1435. }
  1436. v->mv_f[dir][xy + v->blocks_off] = 1;
  1437. v->ref_field_type[dir] = !v->cur_field_type;
  1438. } else {
  1439. if (a_valid && a_f) {
  1440. field_predA[0] = scaleforsame(v, n, field_predA[0], 0, dir);
  1441. field_predA[1] = scaleforsame(v, n, field_predA[1], 1, dir);
  1442. }
  1443. if (b_valid && b_f) {
  1444. field_predB[0] = scaleforsame(v, n, field_predB[0], 0, dir);
  1445. field_predB[1] = scaleforsame(v, n, field_predB[1], 1, dir);
  1446. }
  1447. if (c_valid && c_f) {
  1448. field_predC[0] = scaleforsame(v, n, field_predC[0], 0, dir);
  1449. field_predC[1] = scaleforsame(v, n, field_predC[1], 1, dir);
  1450. }
  1451. v->mv_f[dir][xy + v->blocks_off] = 0;
  1452. v->ref_field_type[dir] = v->cur_field_type;
  1453. }
  1454. if (a_valid) {
  1455. px = field_predA[0];
  1456. py = field_predA[1];
  1457. } else if (c_valid) {
  1458. px = field_predC[0];
  1459. py = field_predC[1];
  1460. } else if (b_valid) {
  1461. px = field_predB[0];
  1462. py = field_predB[1];
  1463. } else {
  1464. px = 0;
  1465. py = 0;
  1466. }
  1467. if (num_samefield + num_oppfield > 1) {
  1468. px = mid_pred(field_predA[0], field_predB[0], field_predC[0]);
  1469. py = mid_pred(field_predA[1], field_predB[1], field_predC[1]);
  1470. }
  1471. /* Pullback MV as specified in 8.3.5.3.4 */
  1472. if (!v->field_mode) {
  1473. int qx, qy, X, Y;
  1474. qx = (s->mb_x << 6) + ((n == 1 || n == 3) ? 32 : 0);
  1475. qy = (s->mb_y << 6) + ((n == 2 || n == 3) ? 32 : 0);
  1476. X = (s->mb_width << 6) - 4;
  1477. Y = (s->mb_height << 6) - 4;
  1478. if (mv1) {
  1479. if (qx + px < -60) px = -60 - qx;
  1480. if (qy + py < -60) py = -60 - qy;
  1481. } else {
  1482. if (qx + px < -28) px = -28 - qx;
  1483. if (qy + py < -28) py = -28 - qy;
  1484. }
  1485. if (qx + px > X) px = X - qx;
  1486. if (qy + py > Y) py = Y - qy;
  1487. }
  1488. if (!v->field_mode || s->pict_type != AV_PICTURE_TYPE_B) {
  1489. /* Calculate hybrid prediction as specified in 8.3.5.3.5 (also 10.3.5.4.3.5) */
  1490. hybridmv_thresh = 32;
  1491. if (a_valid && c_valid) {
  1492. if (is_intra[xy - wrap])
  1493. sum = FFABS(px) + FFABS(py);
  1494. else
  1495. sum = FFABS(px - field_predA[0]) + FFABS(py - field_predA[1]);
  1496. if (sum > hybridmv_thresh) {
  1497. if (get_bits1(&s->gb)) { // read HYBRIDPRED bit
  1498. px = field_predA[0];
  1499. py = field_predA[1];
  1500. } else {
  1501. px = field_predC[0];
  1502. py = field_predC[1];
  1503. }
  1504. } else {
  1505. if (is_intra[xy - 1])
  1506. sum = FFABS(px) + FFABS(py);
  1507. else
  1508. sum = FFABS(px - field_predC[0]) + FFABS(py - field_predC[1]);
  1509. if (sum > hybridmv_thresh) {
  1510. if (get_bits1(&s->gb)) {
  1511. px = field_predA[0];
  1512. py = field_predA[1];
  1513. } else {
  1514. px = field_predC[0];
  1515. py = field_predC[1];
  1516. }
  1517. }
  1518. }
  1519. }
  1520. }
  1521. if (v->field_mode && v->numref)
  1522. r_y >>= 1;
  1523. if (v->field_mode && v->cur_field_type && v->ref_field_type[dir] == 0)
  1524. y_bias = 1;
  1525. /* store MV using signed modulus of MV range defined in 4.11 */
  1526. s->mv[dir][n][0] = s->current_picture.motion_val[dir][xy + v->blocks_off][0] = ((px + dmv_x + r_x) & ((r_x << 1) - 1)) - r_x;
  1527. s->mv[dir][n][1] = s->current_picture.motion_val[dir][xy + v->blocks_off][1] = ((py + dmv_y + r_y - y_bias) & ((r_y << 1) - 1)) - r_y + y_bias;
  1528. if (mv1) { /* duplicate motion data for 1-MV block */
  1529. s->current_picture.motion_val[dir][xy + 1 + v->blocks_off][0] = s->current_picture.motion_val[dir][xy + v->blocks_off][0];
  1530. s->current_picture.motion_val[dir][xy + 1 + v->blocks_off][1] = s->current_picture.motion_val[dir][xy + v->blocks_off][1];
  1531. s->current_picture.motion_val[dir][xy + wrap + v->blocks_off][0] = s->current_picture.motion_val[dir][xy + v->blocks_off][0];
  1532. s->current_picture.motion_val[dir][xy + wrap + v->blocks_off][1] = s->current_picture.motion_val[dir][xy + v->blocks_off][1];
  1533. s->current_picture.motion_val[dir][xy + wrap + 1 + v->blocks_off][0] = s->current_picture.motion_val[dir][xy + v->blocks_off][0];
  1534. s->current_picture.motion_val[dir][xy + wrap + 1 + v->blocks_off][1] = s->current_picture.motion_val[dir][xy + v->blocks_off][1];
  1535. v->mv_f[dir][xy + 1 + v->blocks_off] = v->mv_f[dir][xy + v->blocks_off];
  1536. v->mv_f[dir][xy + wrap + v->blocks_off] = v->mv_f[dir][xy + wrap + 1 + v->blocks_off] = v->mv_f[dir][xy + v->blocks_off];
  1537. }
  1538. }
  1539. /** Predict and set motion vector for interlaced frame picture MBs
  1540. */
  1541. static inline void vc1_pred_mv_intfr(VC1Context *v, int n, int dmv_x, int dmv_y,
  1542. int mvn, int r_x, int r_y, uint8_t* is_intra, int dir)
  1543. {
  1544. MpegEncContext *s = &v->s;
  1545. int xy, wrap, off = 0;
  1546. int A[2], B[2], C[2];
  1547. int px = 0, py = 0;
  1548. int a_valid = 0, b_valid = 0, c_valid = 0;
  1549. int field_a, field_b, field_c; // 0: same, 1: opposit
  1550. int total_valid, num_samefield, num_oppfield;
  1551. int pos_c, pos_b, n_adj;
  1552. wrap = s->b8_stride;
  1553. xy = s->block_index[n];
  1554. if (s->mb_intra) {
  1555. s->mv[0][n][0] = s->current_picture.motion_val[0][xy][0] = 0;
  1556. s->mv[0][n][1] = s->current_picture.motion_val[0][xy][1] = 0;
  1557. s->current_picture.motion_val[1][xy][0] = 0;
  1558. s->current_picture.motion_val[1][xy][1] = 0;
  1559. if (mvn == 1) { /* duplicate motion data for 1-MV block */
  1560. s->current_picture.motion_val[0][xy + 1][0] = 0;
  1561. s->current_picture.motion_val[0][xy + 1][1] = 0;
  1562. s->current_picture.motion_val[0][xy + wrap][0] = 0;
  1563. s->current_picture.motion_val[0][xy + wrap][1] = 0;
  1564. s->current_picture.motion_val[0][xy + wrap + 1][0] = 0;
  1565. s->current_picture.motion_val[0][xy + wrap + 1][1] = 0;
  1566. v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
  1567. s->current_picture.motion_val[1][xy + 1][0] = 0;
  1568. s->current_picture.motion_val[1][xy + 1][1] = 0;
  1569. s->current_picture.motion_val[1][xy + wrap][0] = 0;
  1570. s->current_picture.motion_val[1][xy + wrap][1] = 0;
  1571. s->current_picture.motion_val[1][xy + wrap + 1][0] = 0;
  1572. s->current_picture.motion_val[1][xy + wrap + 1][1] = 0;
  1573. }
  1574. return;
  1575. }
  1576. off = ((n == 0) || (n == 1)) ? 1 : -1;
  1577. /* predict A */
  1578. if (s->mb_x || (n == 1) || (n == 3)) {
  1579. if ((v->blk_mv_type[xy]) // current block (MB) has a field MV
  1580. || (!v->blk_mv_type[xy] && !v->blk_mv_type[xy - 1])) { // or both have frame MV
  1581. A[0] = s->current_picture.motion_val[dir][xy - 1][0];
  1582. A[1] = s->current_picture.motion_val[dir][xy - 1][1];
  1583. a_valid = 1;
  1584. } else { // current block has frame mv and cand. has field MV (so average)
  1585. A[0] = (s->current_picture.motion_val[dir][xy - 1][0]
  1586. + s->current_picture.motion_val[dir][xy - 1 + off * wrap][0] + 1) >> 1;
  1587. A[1] = (s->current_picture.motion_val[dir][xy - 1][1]
  1588. + s->current_picture.motion_val[dir][xy - 1 + off * wrap][1] + 1) >> 1;
  1589. a_valid = 1;
  1590. }
  1591. if (!(n & 1) && v->is_intra[s->mb_x - 1]) {
  1592. a_valid = 0;
  1593. A[0] = A[1] = 0;
  1594. }
  1595. } else
  1596. A[0] = A[1] = 0;
  1597. /* Predict B and C */
  1598. B[0] = B[1] = C[0] = C[1] = 0;
  1599. if (n == 0 || n == 1 || v->blk_mv_type[xy]) {
  1600. if (!s->first_slice_line) {
  1601. if (!v->is_intra[s->mb_x - s->mb_stride]) {
  1602. b_valid = 1;
  1603. n_adj = n | 2;
  1604. pos_b = s->block_index[n_adj] - 2 * wrap;
  1605. if (v->blk_mv_type[pos_b] && v->blk_mv_type[xy]) {
  1606. n_adj = (n & 2) | (n & 1);
  1607. }
  1608. B[0] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap][0];
  1609. B[1] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap][1];
  1610. if (v->blk_mv_type[pos_b] && !v->blk_mv_type[xy]) {
  1611. B[0] = (B[0] + s->current_picture.motion_val[dir][s->block_index[n_adj ^ 2] - 2 * wrap][0] + 1) >> 1;
  1612. B[1] = (B[1] + s->current_picture.motion_val[dir][s->block_index[n_adj ^ 2] - 2 * wrap][1] + 1) >> 1;
  1613. }
  1614. }
  1615. if (s->mb_width > 1) {
  1616. if (!v->is_intra[s->mb_x - s->mb_stride + 1]) {
  1617. c_valid = 1;
  1618. n_adj = 2;
  1619. pos_c = s->block_index[2] - 2 * wrap + 2;
  1620. if (v->blk_mv_type[pos_c] && v->blk_mv_type[xy]) {
  1621. n_adj = n & 2;
  1622. }
  1623. C[0] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap + 2][0];
  1624. C[1] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap + 2][1];
  1625. if (v->blk_mv_type[pos_c] && !v->blk_mv_type[xy]) {
  1626. C[0] = (1 + C[0] + (s->current_picture.motion_val[dir][s->block_index[n_adj ^ 2] - 2 * wrap + 2][0])) >> 1;
  1627. C[1] = (1 + C[1] + (s->current_picture.motion_val[dir][s->block_index[n_adj ^ 2] - 2 * wrap + 2][1])) >> 1;
  1628. }
  1629. if (s->mb_x == s->mb_width - 1) {
  1630. if (!v->is_intra[s->mb_x - s->mb_stride - 1]) {
  1631. c_valid = 1;
  1632. n_adj = 3;
  1633. pos_c = s->block_index[3] - 2 * wrap - 2;
  1634. if (v->blk_mv_type[pos_c] && v->blk_mv_type[xy]) {
  1635. n_adj = n | 1;
  1636. }
  1637. C[0] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap - 2][0];
  1638. C[1] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap - 2][1];
  1639. if (v->blk_mv_type[pos_c] && !v->blk_mv_type[xy]) {
  1640. C[0] = (1 + C[0] + s->current_picture.motion_val[dir][s->block_index[1] - 2 * wrap - 2][0]) >> 1;
  1641. C[1] = (1 + C[1] + s->current_picture.motion_val[dir][s->block_index[1] - 2 * wrap - 2][1]) >> 1;
  1642. }
  1643. } else
  1644. c_valid = 0;
  1645. }
  1646. }
  1647. }
  1648. }
  1649. } else {
  1650. pos_b = s->block_index[1];
  1651. b_valid = 1;
  1652. B[0] = s->current_picture.motion_val[dir][pos_b][0];
  1653. B[1] = s->current_picture.motion_val[dir][pos_b][1];
  1654. pos_c = s->block_index[0];
  1655. c_valid = 1;
  1656. C[0] = s->current_picture.motion_val[dir][pos_c][0];
  1657. C[1] = s->current_picture.motion_val[dir][pos_c][1];
  1658. }
  1659. total_valid = a_valid + b_valid + c_valid;
  1660. // check if predictor A is out of bounds
  1661. if (!s->mb_x && !(n == 1 || n == 3)) {
  1662. A[0] = A[1] = 0;
  1663. }
  1664. // check if predictor B is out of bounds
  1665. if ((s->first_slice_line && v->blk_mv_type[xy]) || (s->first_slice_line && !(n & 2))) {
  1666. B[0] = B[1] = C[0] = C[1] = 0;
  1667. }
  1668. if (!v->blk_mv_type[xy]) {
  1669. if (s->mb_width == 1) {
  1670. px = B[0];
  1671. py = B[1];
  1672. } else {
  1673. if (total_valid >= 2) {
  1674. px = mid_pred(A[0], B[0], C[0]);
  1675. py = mid_pred(A[1], B[1], C[1]);
  1676. } else if (total_valid) {
  1677. if (a_valid) { px = A[0]; py = A[1]; }
  1678. else if (b_valid) { px = B[0]; py = B[1]; }
  1679. else { px = C[0]; py = C[1]; }
  1680. }
  1681. }
  1682. } else {
  1683. if (a_valid)
  1684. field_a = (A[1] & 4) ? 1 : 0;
  1685. else
  1686. field_a = 0;
  1687. if (b_valid)
  1688. field_b = (B[1] & 4) ? 1 : 0;
  1689. else
  1690. field_b = 0;
  1691. if (c_valid)
  1692. field_c = (C[1] & 4) ? 1 : 0;
  1693. else
  1694. field_c = 0;
  1695. num_oppfield = field_a + field_b + field_c;
  1696. num_samefield = total_valid - num_oppfield;
  1697. if (total_valid == 3) {
  1698. if ((num_samefield == 3) || (num_oppfield == 3)) {
  1699. px = mid_pred(A[0], B[0], C[0]);
  1700. py = mid_pred(A[1], B[1], C[1]);
  1701. } else if (num_samefield >= num_oppfield) {
  1702. /* take one MV from same field set depending on priority
  1703. the check for B may not be necessary */
  1704. px = !field_a ? A[0] : B[0];
  1705. py = !field_a ? A[1] : B[1];
  1706. } else {
  1707. px = field_a ? A[0] : B[0];
  1708. py = field_a ? A[1] : B[1];
  1709. }
  1710. } else if (total_valid == 2) {
  1711. if (num_samefield >= num_oppfield) {
  1712. if (!field_a && a_valid) {
  1713. px = A[0];
  1714. py = A[1];
  1715. } else if (!field_b && b_valid) {
  1716. px = B[0];
  1717. py = B[1];
  1718. } else /*if (c_valid)*/ {
  1719. av_assert1(c_valid);
  1720. px = C[0];
  1721. py = C[1];
  1722. } /*else px = py = 0;*/
  1723. } else {
  1724. if (field_a && a_valid) {
  1725. px = A[0];
  1726. py = A[1];
  1727. } else /*if (field_b && b_valid)*/ {
  1728. av_assert1(field_b && b_valid);
  1729. px = B[0];
  1730. py = B[1];
  1731. } /*else if (c_valid) {
  1732. px = C[0];
  1733. py = C[1];
  1734. }*/
  1735. }
  1736. } else if (total_valid == 1) {
  1737. px = (a_valid) ? A[0] : ((b_valid) ? B[0] : C[0]);
  1738. py = (a_valid) ? A[1] : ((b_valid) ? B[1] : C[1]);
  1739. }
  1740. }
  1741. /* store MV using signed modulus of MV range defined in 4.11 */
  1742. s->mv[dir][n][0] = s->current_picture.motion_val[dir][xy][0] = ((px + dmv_x + r_x) & ((r_x << 1) - 1)) - r_x;
  1743. s->mv[dir][n][1] = s->current_picture.motion_val[dir][xy][1] = ((py + dmv_y + r_y) & ((r_y << 1) - 1)) - r_y;
  1744. if (mvn == 1) { /* duplicate motion data for 1-MV block */
  1745. s->current_picture.motion_val[dir][xy + 1 ][0] = s->current_picture.motion_val[dir][xy][0];
  1746. s->current_picture.motion_val[dir][xy + 1 ][1] = s->current_picture.motion_val[dir][xy][1];
  1747. s->current_picture.motion_val[dir][xy + wrap ][0] = s->current_picture.motion_val[dir][xy][0];
  1748. s->current_picture.motion_val[dir][xy + wrap ][1] = s->current_picture.motion_val[dir][xy][1];
  1749. s->current_picture.motion_val[dir][xy + wrap + 1][0] = s->current_picture.motion_val[dir][xy][0];
  1750. s->current_picture.motion_val[dir][xy + wrap + 1][1] = s->current_picture.motion_val[dir][xy][1];
  1751. } else if (mvn == 2) { /* duplicate motion data for 2-Field MV block */
  1752. s->current_picture.motion_val[dir][xy + 1][0] = s->current_picture.motion_val[dir][xy][0];
  1753. s->current_picture.motion_val[dir][xy + 1][1] = s->current_picture.motion_val[dir][xy][1];
  1754. s->mv[dir][n + 1][0] = s->mv[dir][n][0];
  1755. s->mv[dir][n + 1][1] = s->mv[dir][n][1];
  1756. }
  1757. }
  1758. /** Motion compensation for direct or interpolated blocks in B-frames
  1759. */
  1760. static void vc1_interp_mc(VC1Context *v)
  1761. {
  1762. MpegEncContext *s = &v->s;
  1763. H264ChromaContext *h264chroma = &v->h264chroma;
  1764. uint8_t *srcY, *srcU, *srcV;
  1765. int dxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
  1766. int off, off_uv;
  1767. int v_edge_pos = s->v_edge_pos >> v->field_mode;
  1768. int use_ic = v->next_use_ic;
  1769. if (!v->field_mode && !v->s.next_picture.f->data[0])
  1770. return;
  1771. mx = s->mv[1][0][0];
  1772. my = s->mv[1][0][1];
  1773. uvmx = (mx + ((mx & 3) == 3)) >> 1;
  1774. uvmy = (my + ((my & 3) == 3)) >> 1;
  1775. if (v->field_mode) {
  1776. if (v->cur_field_type != v->ref_field_type[1]) {
  1777. my = my - 2 + 4 * v->cur_field_type;
  1778. uvmy = uvmy - 2 + 4 * v->cur_field_type;
  1779. }
  1780. }
  1781. if (v->fastuvmc) {
  1782. uvmx = uvmx + ((uvmx < 0) ? -(uvmx & 1) : (uvmx & 1));
  1783. uvmy = uvmy + ((uvmy < 0) ? -(uvmy & 1) : (uvmy & 1));
  1784. }
  1785. srcY = s->next_picture.f->data[0];
  1786. srcU = s->next_picture.f->data[1];
  1787. srcV = s->next_picture.f->data[2];
  1788. src_x = s->mb_x * 16 + (mx >> 2);
  1789. src_y = s->mb_y * 16 + (my >> 2);
  1790. uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
  1791. uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
  1792. if (v->profile != PROFILE_ADVANCED) {
  1793. src_x = av_clip( src_x, -16, s->mb_width * 16);
  1794. src_y = av_clip( src_y, -16, s->mb_height * 16);
  1795. uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
  1796. uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
  1797. } else {
  1798. src_x = av_clip( src_x, -17, s->avctx->coded_width);
  1799. src_y = av_clip( src_y, -18, s->avctx->coded_height + 1);
  1800. uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
  1801. uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
  1802. }
  1803. srcY += src_y * s->linesize + src_x;
  1804. srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
  1805. srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
  1806. if (v->field_mode && v->ref_field_type[1]) {
  1807. srcY += s->current_picture_ptr->f->linesize[0];
  1808. srcU += s->current_picture_ptr->f->linesize[1];
  1809. srcV += s->current_picture_ptr->f->linesize[2];
  1810. }
  1811. /* for grayscale we should not try to read from unknown area */
  1812. if (s->flags & CODEC_FLAG_GRAY) {
  1813. srcU = s->edge_emu_buffer + 18 * s->linesize;
  1814. srcV = s->edge_emu_buffer + 18 * s->linesize;
  1815. }
  1816. if (v->rangeredfrm || s->h_edge_pos < 22 || v_edge_pos < 22 || use_ic
  1817. || (unsigned)(src_x - 1) > s->h_edge_pos - (mx & 3) - 16 - 3
  1818. || (unsigned)(src_y - 1) > v_edge_pos - (my & 3) - 16 - 3) {
  1819. uint8_t *ubuf = s->edge_emu_buffer + 19 * s->linesize;
  1820. uint8_t *vbuf = ubuf + 9 * s->uvlinesize;
  1821. srcY -= s->mspel * (1 + s->linesize);
  1822. s->vdsp.emulated_edge_mc(s->edge_emu_buffer, srcY,
  1823. s->linesize, s->linesize,
  1824. 17 + s->mspel * 2, 17 + s->mspel * 2,
  1825. src_x - s->mspel, src_y - s->mspel,
  1826. s->h_edge_pos, v_edge_pos);
  1827. srcY = s->edge_emu_buffer;
  1828. s->vdsp.emulated_edge_mc(ubuf, srcU,
  1829. s->uvlinesize, s->uvlinesize,
  1830. 8 + 1, 8 + 1,
  1831. uvsrc_x, uvsrc_y,
  1832. s->h_edge_pos >> 1, v_edge_pos >> 1);
  1833. s->vdsp.emulated_edge_mc(vbuf, srcV,
  1834. s->uvlinesize, s->uvlinesize,
  1835. 8 + 1, 8 + 1,
  1836. uvsrc_x, uvsrc_y,
  1837. s->h_edge_pos >> 1, v_edge_pos >> 1);
  1838. srcU = ubuf;
  1839. srcV = vbuf;
  1840. /* if we deal with range reduction we need to scale source blocks */
  1841. if (v->rangeredfrm) {
  1842. int i, j;
  1843. uint8_t *src, *src2;
  1844. src = srcY;
  1845. for (j = 0; j < 17 + s->mspel * 2; j++) {
  1846. for (i = 0; i < 17 + s->mspel * 2; i++)
  1847. src[i] = ((src[i] - 128) >> 1) + 128;
  1848. src += s->linesize;
  1849. }
  1850. src = srcU;
  1851. src2 = srcV;
  1852. for (j = 0; j < 9; j++) {
  1853. for (i = 0; i < 9; i++) {
  1854. src[i] = ((src[i] - 128) >> 1) + 128;
  1855. src2[i] = ((src2[i] - 128) >> 1) + 128;
  1856. }
  1857. src += s->uvlinesize;
  1858. src2 += s->uvlinesize;
  1859. }
  1860. }
  1861. if (use_ic) {
  1862. uint8_t (*luty )[256] = v->next_luty;
  1863. uint8_t (*lutuv)[256] = v->next_lutuv;
  1864. int i, j;
  1865. uint8_t *src, *src2;
  1866. src = srcY;
  1867. for (j = 0; j < 17 + s->mspel * 2; j++) {
  1868. int f = v->field_mode ? v->ref_field_type[1] : ((j+src_y - s->mspel) & 1);
  1869. for (i = 0; i < 17 + s->mspel * 2; i++)
  1870. src[i] = luty[f][src[i]];
  1871. src += s->linesize;
  1872. }
  1873. src = srcU;
  1874. src2 = srcV;
  1875. for (j = 0; j < 9; j++) {
  1876. int f = v->field_mode ? v->ref_field_type[1] : ((j+uvsrc_y) & 1);
  1877. for (i = 0; i < 9; i++) {
  1878. src[i] = lutuv[f][src[i]];
  1879. src2[i] = lutuv[f][src2[i]];
  1880. }
  1881. src += s->uvlinesize;
  1882. src2 += s->uvlinesize;
  1883. }
  1884. }
  1885. srcY += s->mspel * (1 + s->linesize);
  1886. }
  1887. off = 0;
  1888. off_uv = 0;
  1889. if (s->mspel) {
  1890. dxy = ((my & 3) << 2) | (mx & 3);
  1891. v->vc1dsp.avg_vc1_mspel_pixels_tab[0][dxy](s->dest[0] + off , srcY , s->linesize, v->rnd);
  1892. } else { // hpel mc
  1893. dxy = (my & 2) | ((mx & 2) >> 1);
  1894. if (!v->rnd)
  1895. s->hdsp.avg_pixels_tab[0][dxy](s->dest[0] + off, srcY, s->linesize, 16);
  1896. else
  1897. s->hdsp.avg_no_rnd_pixels_tab[dxy](s->dest[0] + off, srcY, s->linesize, 16);
  1898. }
  1899. if (s->flags & CODEC_FLAG_GRAY) return;
  1900. /* Chroma MC always uses qpel blilinear */
  1901. uvmx = (uvmx & 3) << 1;
  1902. uvmy = (uvmy & 3) << 1;
  1903. if (!v->rnd) {
  1904. h264chroma->avg_h264_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
  1905. h264chroma->avg_h264_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
  1906. } else {
  1907. v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
  1908. v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
  1909. }
  1910. }
  1911. static av_always_inline int scale_mv(int value, int bfrac, int inv, int qs)
  1912. {
  1913. int n = bfrac;
  1914. #if B_FRACTION_DEN==256
  1915. if (inv)
  1916. n -= 256;
  1917. if (!qs)
  1918. return 2 * ((value * n + 255) >> 9);
  1919. return (value * n + 128) >> 8;
  1920. #else
  1921. if (inv)
  1922. n -= B_FRACTION_DEN;
  1923. if (!qs)
  1924. return 2 * ((value * n + B_FRACTION_DEN - 1) / (2 * B_FRACTION_DEN));
  1925. return (value * n + B_FRACTION_DEN/2) / B_FRACTION_DEN;
  1926. #endif
  1927. }
  1928. /** Reconstruct motion vector for B-frame and do motion compensation
  1929. */
  1930. static inline void vc1_b_mc(VC1Context *v, int dmv_x[2], int dmv_y[2],
  1931. int direct, int mode)
  1932. {
  1933. if (direct) {
  1934. vc1_mc_1mv(v, 0);
  1935. vc1_interp_mc(v);
  1936. return;
  1937. }
  1938. if (mode == BMV_TYPE_INTERPOLATED) {
  1939. vc1_mc_1mv(v, 0);
  1940. vc1_interp_mc(v);
  1941. return;
  1942. }
  1943. vc1_mc_1mv(v, (mode == BMV_TYPE_BACKWARD));
  1944. }
  1945. static inline void vc1_pred_b_mv(VC1Context *v, int dmv_x[2], int dmv_y[2],
  1946. int direct, int mvtype)
  1947. {
  1948. MpegEncContext *s = &v->s;
  1949. int xy, wrap, off = 0;
  1950. int16_t *A, *B, *C;
  1951. int px, py;
  1952. int sum;
  1953. int r_x, r_y;
  1954. const uint8_t *is_intra = v->mb_type[0];
  1955. av_assert0(!v->field_mode);
  1956. r_x = v->range_x;
  1957. r_y = v->range_y;
  1958. /* scale MV difference to be quad-pel */
  1959. dmv_x[0] <<= 1 - s->quarter_sample;
  1960. dmv_y[0] <<= 1 - s->quarter_sample;
  1961. dmv_x[1] <<= 1 - s->quarter_sample;
  1962. dmv_y[1] <<= 1 - s->quarter_sample;
  1963. wrap = s->b8_stride;
  1964. xy = s->block_index[0];
  1965. if (s->mb_intra) {
  1966. s->current_picture.motion_val[0][xy][0] =
  1967. s->current_picture.motion_val[0][xy][1] =
  1968. s->current_picture.motion_val[1][xy][0] =
  1969. s->current_picture.motion_val[1][xy][1] = 0;
  1970. return;
  1971. }
  1972. if (direct && s->next_picture_ptr->field_picture)
  1973. av_log(s->avctx, AV_LOG_WARNING, "Mixed frame/field direct mode not supported\n");
  1974. s->mv[0][0][0] = scale_mv(s->next_picture.motion_val[1][xy][0], v->bfraction, 0, s->quarter_sample);
  1975. s->mv[0][0][1] = scale_mv(s->next_picture.motion_val[1][xy][1], v->bfraction, 0, s->quarter_sample);
  1976. s->mv[1][0][0] = scale_mv(s->next_picture.motion_val[1][xy][0], v->bfraction, 1, s->quarter_sample);
  1977. s->mv[1][0][1] = scale_mv(s->next_picture.motion_val[1][xy][1], v->bfraction, 1, s->quarter_sample);
  1978. /* Pullback predicted motion vectors as specified in 8.4.5.4 */
  1979. s->mv[0][0][0] = av_clip(s->mv[0][0][0], -60 - (s->mb_x << 6), (s->mb_width << 6) - 4 - (s->mb_x << 6));
  1980. s->mv[0][0][1] = av_clip(s->mv[0][0][1], -60 - (s->mb_y << 6), (s->mb_height << 6) - 4 - (s->mb_y << 6));
  1981. s->mv[1][0][0] = av_clip(s->mv[1][0][0], -60 - (s->mb_x << 6), (s->mb_width << 6) - 4 - (s->mb_x << 6));
  1982. s->mv[1][0][1] = av_clip(s->mv[1][0][1], -60 - (s->mb_y << 6), (s->mb_height << 6) - 4 - (s->mb_y << 6));
  1983. if (direct) {
  1984. s->current_picture.motion_val[0][xy][0] = s->mv[0][0][0];
  1985. s->current_picture.motion_val[0][xy][1] = s->mv[0][0][1];
  1986. s->current_picture.motion_val[1][xy][0] = s->mv[1][0][0];
  1987. s->current_picture.motion_val[1][xy][1] = s->mv[1][0][1];
  1988. return;
  1989. }
  1990. if ((mvtype == BMV_TYPE_FORWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
  1991. C = s->current_picture.motion_val[0][xy - 2];
  1992. A = s->current_picture.motion_val[0][xy - wrap * 2];
  1993. off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
  1994. B = s->current_picture.motion_val[0][xy - wrap * 2 + off];
  1995. if (!s->mb_x) C[0] = C[1] = 0;
  1996. if (!s->first_slice_line) { // predictor A is not out of bounds
  1997. if (s->mb_width == 1) {
  1998. px = A[0];
  1999. py = A[1];
  2000. } else {
  2001. px = mid_pred(A[0], B[0], C[0]);
  2002. py = mid_pred(A[1], B[1], C[1]);
  2003. }
  2004. } else if (s->mb_x) { // predictor C is not out of bounds
  2005. px = C[0];
  2006. py = C[1];
  2007. } else {
  2008. px = py = 0;
  2009. }
  2010. /* Pullback MV as specified in 8.3.5.3.4 */
  2011. {
  2012. int qx, qy, X, Y;
  2013. if (v->profile < PROFILE_ADVANCED) {
  2014. qx = (s->mb_x << 5);
  2015. qy = (s->mb_y << 5);
  2016. X = (s->mb_width << 5) - 4;
  2017. Y = (s->mb_height << 5) - 4;
  2018. if (qx + px < -28) px = -28 - qx;
  2019. if (qy + py < -28) py = -28 - qy;
  2020. if (qx + px > X) px = X - qx;
  2021. if (qy + py > Y) py = Y - qy;
  2022. } else {
  2023. qx = (s->mb_x << 6);
  2024. qy = (s->mb_y << 6);
  2025. X = (s->mb_width << 6) - 4;
  2026. Y = (s->mb_height << 6) - 4;
  2027. if (qx + px < -60) px = -60 - qx;
  2028. if (qy + py < -60) py = -60 - qy;
  2029. if (qx + px > X) px = X - qx;
  2030. if (qy + py > Y) py = Y - qy;
  2031. }
  2032. }
  2033. /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
  2034. if (0 && !s->first_slice_line && s->mb_x) {
  2035. if (is_intra[xy - wrap])
  2036. sum = FFABS(px) + FFABS(py);
  2037. else
  2038. sum = FFABS(px - A[0]) + FFABS(py - A[1]);
  2039. if (sum > 32) {
  2040. if (get_bits1(&s->gb)) {
  2041. px = A[0];
  2042. py = A[1];
  2043. } else {
  2044. px = C[0];
  2045. py = C[1];
  2046. }
  2047. } else {
  2048. if (is_intra[xy - 2])
  2049. sum = FFABS(px) + FFABS(py);
  2050. else
  2051. sum = FFABS(px - C[0]) + FFABS(py - C[1]);
  2052. if (sum > 32) {
  2053. if (get_bits1(&s->gb)) {
  2054. px = A[0];
  2055. py = A[1];
  2056. } else {
  2057. px = C[0];
  2058. py = C[1];
  2059. }
  2060. }
  2061. }
  2062. }
  2063. /* store MV using signed modulus of MV range defined in 4.11 */
  2064. s->mv[0][0][0] = ((px + dmv_x[0] + r_x) & ((r_x << 1) - 1)) - r_x;
  2065. s->mv[0][0][1] = ((py + dmv_y[0] + r_y) & ((r_y << 1) - 1)) - r_y;
  2066. }
  2067. if ((mvtype == BMV_TYPE_BACKWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
  2068. C = s->current_picture.motion_val[1][xy - 2];
  2069. A = s->current_picture.motion_val[1][xy - wrap * 2];
  2070. off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
  2071. B = s->current_picture.motion_val[1][xy - wrap * 2 + off];
  2072. if (!s->mb_x)
  2073. C[0] = C[1] = 0;
  2074. if (!s->first_slice_line) { // predictor A is not out of bounds
  2075. if (s->mb_width == 1) {
  2076. px = A[0];
  2077. py = A[1];
  2078. } else {
  2079. px = mid_pred(A[0], B[0], C[0]);
  2080. py = mid_pred(A[1], B[1], C[1]);
  2081. }
  2082. } else if (s->mb_x) { // predictor C is not out of bounds
  2083. px = C[0];
  2084. py = C[1];
  2085. } else {
  2086. px = py = 0;
  2087. }
  2088. /* Pullback MV as specified in 8.3.5.3.4 */
  2089. {
  2090. int qx, qy, X, Y;
  2091. if (v->profile < PROFILE_ADVANCED) {
  2092. qx = (s->mb_x << 5);
  2093. qy = (s->mb_y << 5);
  2094. X = (s->mb_width << 5) - 4;
  2095. Y = (s->mb_height << 5) - 4;
  2096. if (qx + px < -28) px = -28 - qx;
  2097. if (qy + py < -28) py = -28 - qy;
  2098. if (qx + px > X) px = X - qx;
  2099. if (qy + py > Y) py = Y - qy;
  2100. } else {
  2101. qx = (s->mb_x << 6);
  2102. qy = (s->mb_y << 6);
  2103. X = (s->mb_width << 6) - 4;
  2104. Y = (s->mb_height << 6) - 4;
  2105. if (qx + px < -60) px = -60 - qx;
  2106. if (qy + py < -60) py = -60 - qy;
  2107. if (qx + px > X) px = X - qx;
  2108. if (qy + py > Y) py = Y - qy;
  2109. }
  2110. }
  2111. /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
  2112. if (0 && !s->first_slice_line && s->mb_x) {
  2113. if (is_intra[xy - wrap])
  2114. sum = FFABS(px) + FFABS(py);
  2115. else
  2116. sum = FFABS(px - A[0]) + FFABS(py - A[1]);
  2117. if (sum > 32) {
  2118. if (get_bits1(&s->gb)) {
  2119. px = A[0];
  2120. py = A[1];
  2121. } else {
  2122. px = C[0];
  2123. py = C[1];
  2124. }
  2125. } else {
  2126. if (is_intra[xy - 2])
  2127. sum = FFABS(px) + FFABS(py);
  2128. else
  2129. sum = FFABS(px - C[0]) + FFABS(py - C[1]);
  2130. if (sum > 32) {
  2131. if (get_bits1(&s->gb)) {
  2132. px = A[0];
  2133. py = A[1];
  2134. } else {
  2135. px = C[0];
  2136. py = C[1];
  2137. }
  2138. }
  2139. }
  2140. }
  2141. /* store MV using signed modulus of MV range defined in 4.11 */
  2142. s->mv[1][0][0] = ((px + dmv_x[1] + r_x) & ((r_x << 1) - 1)) - r_x;
  2143. s->mv[1][0][1] = ((py + dmv_y[1] + r_y) & ((r_y << 1) - 1)) - r_y;
  2144. }
  2145. s->current_picture.motion_val[0][xy][0] = s->mv[0][0][0];
  2146. s->current_picture.motion_val[0][xy][1] = s->mv[0][0][1];
  2147. s->current_picture.motion_val[1][xy][0] = s->mv[1][0][0];
  2148. s->current_picture.motion_val[1][xy][1] = s->mv[1][0][1];
  2149. }
  2150. static inline void vc1_pred_b_mv_intfi(VC1Context *v, int n, int *dmv_x, int *dmv_y, int mv1, int *pred_flag)
  2151. {
  2152. int dir = (v->bmvtype == BMV_TYPE_BACKWARD) ? 1 : 0;
  2153. MpegEncContext *s = &v->s;
  2154. int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
  2155. if (v->bmvtype == BMV_TYPE_DIRECT) {
  2156. int total_opp, k, f;
  2157. if (s->next_picture.mb_type[mb_pos + v->mb_off] != MB_TYPE_INTRA) {
  2158. s->mv[0][0][0] = scale_mv(s->next_picture.motion_val[1][s->block_index[0] + v->blocks_off][0],
  2159. v->bfraction, 0, s->quarter_sample);
  2160. s->mv[0][0][1] = scale_mv(s->next_picture.motion_val[1][s->block_index[0] + v->blocks_off][1],
  2161. v->bfraction, 0, s->quarter_sample);
  2162. s->mv[1][0][0] = scale_mv(s->next_picture.motion_val[1][s->block_index[0] + v->blocks_off][0],
  2163. v->bfraction, 1, s->quarter_sample);
  2164. s->mv[1][0][1] = scale_mv(s->next_picture.motion_val[1][s->block_index[0] + v->blocks_off][1],
  2165. v->bfraction, 1, s->quarter_sample);
  2166. total_opp = v->mv_f_next[0][s->block_index[0] + v->blocks_off]
  2167. + v->mv_f_next[0][s->block_index[1] + v->blocks_off]
  2168. + v->mv_f_next[0][s->block_index[2] + v->blocks_off]
  2169. + v->mv_f_next[0][s->block_index[3] + v->blocks_off];
  2170. f = (total_opp > 2) ? 1 : 0;
  2171. } else {
  2172. s->mv[0][0][0] = s->mv[0][0][1] = 0;
  2173. s->mv[1][0][0] = s->mv[1][0][1] = 0;
  2174. f = 0;
  2175. }
  2176. v->ref_field_type[0] = v->ref_field_type[1] = v->cur_field_type ^ f;
  2177. for (k = 0; k < 4; k++) {
  2178. s->current_picture.motion_val[0][s->block_index[k] + v->blocks_off][0] = s->mv[0][0][0];
  2179. s->current_picture.motion_val[0][s->block_index[k] + v->blocks_off][1] = s->mv[0][0][1];
  2180. s->current_picture.motion_val[1][s->block_index[k] + v->blocks_off][0] = s->mv[1][0][0];
  2181. s->current_picture.motion_val[1][s->block_index[k] + v->blocks_off][1] = s->mv[1][0][1];
  2182. v->mv_f[0][s->block_index[k] + v->blocks_off] = f;
  2183. v->mv_f[1][s->block_index[k] + v->blocks_off] = f;
  2184. }
  2185. return;
  2186. }
  2187. if (v->bmvtype == BMV_TYPE_INTERPOLATED) {
  2188. vc1_pred_mv(v, 0, dmv_x[0], dmv_y[0], 1, v->range_x, v->range_y, v->mb_type[0], pred_flag[0], 0);
  2189. vc1_pred_mv(v, 0, dmv_x[1], dmv_y[1], 1, v->range_x, v->range_y, v->mb_type[0], pred_flag[1], 1);
  2190. return;
  2191. }
  2192. if (dir) { // backward
  2193. vc1_pred_mv(v, n, dmv_x[1], dmv_y[1], mv1, v->range_x, v->range_y, v->mb_type[0], pred_flag[1], 1);
  2194. if (n == 3 || mv1) {
  2195. vc1_pred_mv(v, 0, dmv_x[0], dmv_y[0], 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
  2196. }
  2197. } else { // forward
  2198. vc1_pred_mv(v, n, dmv_x[0], dmv_y[0], mv1, v->range_x, v->range_y, v->mb_type[0], pred_flag[0], 0);
  2199. if (n == 3 || mv1) {
  2200. vc1_pred_mv(v, 0, dmv_x[1], dmv_y[1], 1, v->range_x, v->range_y, v->mb_type[0], 0, 1);
  2201. }
  2202. }
  2203. }
  2204. /** Get predicted DC value for I-frames only
  2205. * prediction dir: left=0, top=1
  2206. * @param s MpegEncContext
  2207. * @param overlap flag indicating that overlap filtering is used
  2208. * @param pq integer part of picture quantizer
  2209. * @param[in] n block index in the current MB
  2210. * @param dc_val_ptr Pointer to DC predictor
  2211. * @param dir_ptr Prediction direction for use in AC prediction
  2212. */
  2213. static inline int vc1_i_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
  2214. int16_t **dc_val_ptr, int *dir_ptr)
  2215. {
  2216. int a, b, c, wrap, pred, scale;
  2217. int16_t *dc_val;
  2218. static const uint16_t dcpred[32] = {
  2219. -1, 1024, 512, 341, 256, 205, 171, 146, 128,
  2220. 114, 102, 93, 85, 79, 73, 68, 64,
  2221. 60, 57, 54, 51, 49, 47, 45, 43,
  2222. 41, 39, 38, 37, 35, 34, 33
  2223. };
  2224. /* find prediction - wmv3_dc_scale always used here in fact */
  2225. if (n < 4) scale = s->y_dc_scale;
  2226. else scale = s->c_dc_scale;
  2227. wrap = s->block_wrap[n];
  2228. dc_val = s->dc_val[0] + s->block_index[n];
  2229. /* B A
  2230. * C X
  2231. */
  2232. c = dc_val[ - 1];
  2233. b = dc_val[ - 1 - wrap];
  2234. a = dc_val[ - wrap];
  2235. if (pq < 9 || !overlap) {
  2236. /* Set outer values */
  2237. if (s->first_slice_line && (n != 2 && n != 3))
  2238. b = a = dcpred[scale];
  2239. if (s->mb_x == 0 && (n != 1 && n != 3))
  2240. b = c = dcpred[scale];
  2241. } else {
  2242. /* Set outer values */
  2243. if (s->first_slice_line && (n != 2 && n != 3))
  2244. b = a = 0;
  2245. if (s->mb_x == 0 && (n != 1 && n != 3))
  2246. b = c = 0;
  2247. }
  2248. if (abs(a - b) <= abs(b - c)) {
  2249. pred = c;
  2250. *dir_ptr = 1; // left
  2251. } else {
  2252. pred = a;
  2253. *dir_ptr = 0; // top
  2254. }
  2255. /* update predictor */
  2256. *dc_val_ptr = &dc_val[0];
  2257. return pred;
  2258. }
  2259. /** Get predicted DC value
  2260. * prediction dir: left=0, top=1
  2261. * @param s MpegEncContext
  2262. * @param overlap flag indicating that overlap filtering is used
  2263. * @param pq integer part of picture quantizer
  2264. * @param[in] n block index in the current MB
  2265. * @param a_avail flag indicating top block availability
  2266. * @param c_avail flag indicating left block availability
  2267. * @param dc_val_ptr Pointer to DC predictor
  2268. * @param dir_ptr Prediction direction for use in AC prediction
  2269. */
  2270. static inline int vc1_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
  2271. int a_avail, int c_avail,
  2272. int16_t **dc_val_ptr, int *dir_ptr)
  2273. {
  2274. int a, b, c, wrap, pred;
  2275. int16_t *dc_val;
  2276. int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
  2277. int q1, q2 = 0;
  2278. int dqscale_index;
  2279. wrap = s->block_wrap[n];
  2280. dc_val = s->dc_val[0] + s->block_index[n];
  2281. /* B A
  2282. * C X
  2283. */
  2284. c = dc_val[ - 1];
  2285. b = dc_val[ - 1 - wrap];
  2286. a = dc_val[ - wrap];
  2287. /* scale predictors if needed */
  2288. q1 = s->current_picture.qscale_table[mb_pos];
  2289. dqscale_index = s->y_dc_scale_table[q1] - 1;
  2290. if (dqscale_index < 0)
  2291. return 0;
  2292. if (c_avail && (n != 1 && n != 3)) {
  2293. q2 = s->current_picture.qscale_table[mb_pos - 1];
  2294. if (q2 && q2 != q1)
  2295. c = (c * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
  2296. }
  2297. if (a_avail && (n != 2 && n != 3)) {
  2298. q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
  2299. if (q2 && q2 != q1)
  2300. a = (a * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
  2301. }
  2302. if (a_avail && c_avail && (n != 3)) {
  2303. int off = mb_pos;
  2304. if (n != 1)
  2305. off--;
  2306. if (n != 2)
  2307. off -= s->mb_stride;
  2308. q2 = s->current_picture.qscale_table[off];
  2309. if (q2 && q2 != q1)
  2310. b = (b * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
  2311. }
  2312. if (a_avail && c_avail) {
  2313. if (abs(a - b) <= abs(b - c)) {
  2314. pred = c;
  2315. *dir_ptr = 1; // left
  2316. } else {
  2317. pred = a;
  2318. *dir_ptr = 0; // top
  2319. }
  2320. } else if (a_avail) {
  2321. pred = a;
  2322. *dir_ptr = 0; // top
  2323. } else if (c_avail) {
  2324. pred = c;
  2325. *dir_ptr = 1; // left
  2326. } else {
  2327. pred = 0;
  2328. *dir_ptr = 1; // left
  2329. }
  2330. /* update predictor */
  2331. *dc_val_ptr = &dc_val[0];
  2332. return pred;
  2333. }
  2334. /** @} */ // Block group
  2335. /**
  2336. * @name VC1 Macroblock-level functions in Simple/Main Profiles
  2337. * @see 7.1.4, p91 and 8.1.1.7, p(1)04
  2338. * @{
  2339. */
  2340. static inline int vc1_coded_block_pred(MpegEncContext * s, int n,
  2341. uint8_t **coded_block_ptr)
  2342. {
  2343. int xy, wrap, pred, a, b, c;
  2344. xy = s->block_index[n];
  2345. wrap = s->b8_stride;
  2346. /* B C
  2347. * A X
  2348. */
  2349. a = s->coded_block[xy - 1 ];
  2350. b = s->coded_block[xy - 1 - wrap];
  2351. c = s->coded_block[xy - wrap];
  2352. if (b == c) {
  2353. pred = a;
  2354. } else {
  2355. pred = c;
  2356. }
  2357. /* store value */
  2358. *coded_block_ptr = &s->coded_block[xy];
  2359. return pred;
  2360. }
  2361. /**
  2362. * Decode one AC coefficient
  2363. * @param v The VC1 context
  2364. * @param last Last coefficient
  2365. * @param skip How much zero coefficients to skip
  2366. * @param value Decoded AC coefficient value
  2367. * @param codingset set of VLC to decode data
  2368. * @see 8.1.3.4
  2369. */
  2370. static void vc1_decode_ac_coeff(VC1Context *v, int *last, int *skip,
  2371. int *value, int codingset)
  2372. {
  2373. GetBitContext *gb = &v->s.gb;
  2374. int index, escape, run = 0, level = 0, lst = 0;
  2375. index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
  2376. if (index != ff_vc1_ac_sizes[codingset] - 1) {
  2377. run = vc1_index_decode_table[codingset][index][0];
  2378. level = vc1_index_decode_table[codingset][index][1];
  2379. lst = index >= vc1_last_decode_table[codingset] || get_bits_left(gb) < 0;
  2380. if (get_bits1(gb))
  2381. level = -level;
  2382. } else {
  2383. escape = decode210(gb);
  2384. if (escape != 2) {
  2385. index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
  2386. run = vc1_index_decode_table[codingset][index][0];
  2387. level = vc1_index_decode_table[codingset][index][1];
  2388. lst = index >= vc1_last_decode_table[codingset];
  2389. if (escape == 0) {
  2390. if (lst)
  2391. level += vc1_last_delta_level_table[codingset][run];
  2392. else
  2393. level += vc1_delta_level_table[codingset][run];
  2394. } else {
  2395. if (lst)
  2396. run += vc1_last_delta_run_table[codingset][level] + 1;
  2397. else
  2398. run += vc1_delta_run_table[codingset][level] + 1;
  2399. }
  2400. if (get_bits1(gb))
  2401. level = -level;
  2402. } else {
  2403. int sign;
  2404. lst = get_bits1(gb);
  2405. if (v->s.esc3_level_length == 0) {
  2406. if (v->pq < 8 || v->dquantfrm) { // table 59
  2407. v->s.esc3_level_length = get_bits(gb, 3);
  2408. if (!v->s.esc3_level_length)
  2409. v->s.esc3_level_length = get_bits(gb, 2) + 8;
  2410. } else { // table 60
  2411. v->s.esc3_level_length = get_unary(gb, 1, 6) + 2;
  2412. }
  2413. v->s.esc3_run_length = 3 + get_bits(gb, 2);
  2414. }
  2415. run = get_bits(gb, v->s.esc3_run_length);
  2416. sign = get_bits1(gb);
  2417. level = get_bits(gb, v->s.esc3_level_length);
  2418. if (sign)
  2419. level = -level;
  2420. }
  2421. }
  2422. *last = lst;
  2423. *skip = run;
  2424. *value = level;
  2425. }
  2426. /** Decode intra block in intra frames - should be faster than decode_intra_block
  2427. * @param v VC1Context
  2428. * @param block block to decode
  2429. * @param[in] n subblock index
  2430. * @param coded are AC coeffs present or not
  2431. * @param codingset set of VLC to decode data
  2432. */
  2433. static int vc1_decode_i_block(VC1Context *v, int16_t block[64], int n,
  2434. int coded, int codingset)
  2435. {
  2436. GetBitContext *gb = &v->s.gb;
  2437. MpegEncContext *s = &v->s;
  2438. int dc_pred_dir = 0; /* Direction of the DC prediction used */
  2439. int i;
  2440. int16_t *dc_val;
  2441. int16_t *ac_val, *ac_val2;
  2442. int dcdiff;
  2443. /* Get DC differential */
  2444. if (n < 4) {
  2445. dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
  2446. } else {
  2447. dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
  2448. }
  2449. if (dcdiff < 0) {
  2450. av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
  2451. return -1;
  2452. }
  2453. if (dcdiff) {
  2454. if (dcdiff == 119 /* ESC index value */) {
  2455. /* TODO: Optimize */
  2456. if (v->pq == 1) dcdiff = get_bits(gb, 10);
  2457. else if (v->pq == 2) dcdiff = get_bits(gb, 9);
  2458. else dcdiff = get_bits(gb, 8);
  2459. } else {
  2460. if (v->pq == 1)
  2461. dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
  2462. else if (v->pq == 2)
  2463. dcdiff = (dcdiff << 1) + get_bits1(gb) - 1;
  2464. }
  2465. if (get_bits1(gb))
  2466. dcdiff = -dcdiff;
  2467. }
  2468. /* Prediction */
  2469. dcdiff += vc1_i_pred_dc(&v->s, v->overlap, v->pq, n, &dc_val, &dc_pred_dir);
  2470. *dc_val = dcdiff;
  2471. /* Store the quantized DC coeff, used for prediction */
  2472. if (n < 4) {
  2473. block[0] = dcdiff * s->y_dc_scale;
  2474. } else {
  2475. block[0] = dcdiff * s->c_dc_scale;
  2476. }
  2477. /* Skip ? */
  2478. if (!coded) {
  2479. goto not_coded;
  2480. }
  2481. // AC Decoding
  2482. i = 1;
  2483. {
  2484. int last = 0, skip, value;
  2485. const uint8_t *zz_table;
  2486. int scale;
  2487. int k;
  2488. scale = v->pq * 2 + v->halfpq;
  2489. if (v->s.ac_pred) {
  2490. if (!dc_pred_dir)
  2491. zz_table = v->zz_8x8[2];
  2492. else
  2493. zz_table = v->zz_8x8[3];
  2494. } else
  2495. zz_table = v->zz_8x8[1];
  2496. ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
  2497. ac_val2 = ac_val;
  2498. if (dc_pred_dir) // left
  2499. ac_val -= 16;
  2500. else // top
  2501. ac_val -= 16 * s->block_wrap[n];
  2502. while (!last) {
  2503. vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
  2504. i += skip;
  2505. if (i > 63)
  2506. break;
  2507. block[zz_table[i++]] = value;
  2508. }
  2509. /* apply AC prediction if needed */
  2510. if (s->ac_pred) {
  2511. if (dc_pred_dir) { // left
  2512. for (k = 1; k < 8; k++)
  2513. block[k << v->left_blk_sh] += ac_val[k];
  2514. } else { // top
  2515. for (k = 1; k < 8; k++)
  2516. block[k << v->top_blk_sh] += ac_val[k + 8];
  2517. }
  2518. }
  2519. /* save AC coeffs for further prediction */
  2520. for (k = 1; k < 8; k++) {
  2521. ac_val2[k] = block[k << v->left_blk_sh];
  2522. ac_val2[k + 8] = block[k << v->top_blk_sh];
  2523. }
  2524. /* scale AC coeffs */
  2525. for (k = 1; k < 64; k++)
  2526. if (block[k]) {
  2527. block[k] *= scale;
  2528. if (!v->pquantizer)
  2529. block[k] += (block[k] < 0) ? -v->pq : v->pq;
  2530. }
  2531. if (s->ac_pred) i = 63;
  2532. }
  2533. not_coded:
  2534. if (!coded) {
  2535. int k, scale;
  2536. ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
  2537. ac_val2 = ac_val;
  2538. i = 0;
  2539. scale = v->pq * 2 + v->halfpq;
  2540. memset(ac_val2, 0, 16 * 2);
  2541. if (dc_pred_dir) { // left
  2542. ac_val -= 16;
  2543. if (s->ac_pred)
  2544. memcpy(ac_val2, ac_val, 8 * 2);
  2545. } else { // top
  2546. ac_val -= 16 * s->block_wrap[n];
  2547. if (s->ac_pred)
  2548. memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
  2549. }
  2550. /* apply AC prediction if needed */
  2551. if (s->ac_pred) {
  2552. if (dc_pred_dir) { //left
  2553. for (k = 1; k < 8; k++) {
  2554. block[k << v->left_blk_sh] = ac_val[k] * scale;
  2555. if (!v->pquantizer && block[k << v->left_blk_sh])
  2556. block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -v->pq : v->pq;
  2557. }
  2558. } else { // top
  2559. for (k = 1; k < 8; k++) {
  2560. block[k << v->top_blk_sh] = ac_val[k + 8] * scale;
  2561. if (!v->pquantizer && block[k << v->top_blk_sh])
  2562. block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -v->pq : v->pq;
  2563. }
  2564. }
  2565. i = 63;
  2566. }
  2567. }
  2568. s->block_last_index[n] = i;
  2569. return 0;
  2570. }
  2571. /** Decode intra block in intra frames - should be faster than decode_intra_block
  2572. * @param v VC1Context
  2573. * @param block block to decode
  2574. * @param[in] n subblock number
  2575. * @param coded are AC coeffs present or not
  2576. * @param codingset set of VLC to decode data
  2577. * @param mquant quantizer value for this macroblock
  2578. */
  2579. static int vc1_decode_i_block_adv(VC1Context *v, int16_t block[64], int n,
  2580. int coded, int codingset, int mquant)
  2581. {
  2582. GetBitContext *gb = &v->s.gb;
  2583. MpegEncContext *s = &v->s;
  2584. int dc_pred_dir = 0; /* Direction of the DC prediction used */
  2585. int i;
  2586. int16_t *dc_val = NULL;
  2587. int16_t *ac_val, *ac_val2;
  2588. int dcdiff;
  2589. int a_avail = v->a_avail, c_avail = v->c_avail;
  2590. int use_pred = s->ac_pred;
  2591. int scale;
  2592. int q1, q2 = 0;
  2593. int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
  2594. /* Get DC differential */
  2595. if (n < 4) {
  2596. dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
  2597. } else {
  2598. dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
  2599. }
  2600. if (dcdiff < 0) {
  2601. av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
  2602. return -1;
  2603. }
  2604. if (dcdiff) {
  2605. if (dcdiff == 119 /* ESC index value */) {
  2606. /* TODO: Optimize */
  2607. if (mquant == 1) dcdiff = get_bits(gb, 10);
  2608. else if (mquant == 2) dcdiff = get_bits(gb, 9);
  2609. else dcdiff = get_bits(gb, 8);
  2610. } else {
  2611. if (mquant == 1)
  2612. dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
  2613. else if (mquant == 2)
  2614. dcdiff = (dcdiff << 1) + get_bits1(gb) - 1;
  2615. }
  2616. if (get_bits1(gb))
  2617. dcdiff = -dcdiff;
  2618. }
  2619. /* Prediction */
  2620. dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, v->a_avail, v->c_avail, &dc_val, &dc_pred_dir);
  2621. *dc_val = dcdiff;
  2622. /* Store the quantized DC coeff, used for prediction */
  2623. if (n < 4) {
  2624. block[0] = dcdiff * s->y_dc_scale;
  2625. } else {
  2626. block[0] = dcdiff * s->c_dc_scale;
  2627. }
  2628. //AC Decoding
  2629. i = 1;
  2630. /* check if AC is needed at all */
  2631. if (!a_avail && !c_avail)
  2632. use_pred = 0;
  2633. ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
  2634. ac_val2 = ac_val;
  2635. scale = mquant * 2 + ((mquant == v->pq) ? v->halfpq : 0);
  2636. if (dc_pred_dir) // left
  2637. ac_val -= 16;
  2638. else // top
  2639. ac_val -= 16 * s->block_wrap[n];
  2640. q1 = s->current_picture.qscale_table[mb_pos];
  2641. if ( dc_pred_dir && c_avail && mb_pos)
  2642. q2 = s->current_picture.qscale_table[mb_pos - 1];
  2643. if (!dc_pred_dir && a_avail && mb_pos >= s->mb_stride)
  2644. q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
  2645. if ( dc_pred_dir && n == 1)
  2646. q2 = q1;
  2647. if (!dc_pred_dir && n == 2)
  2648. q2 = q1;
  2649. if (n == 3)
  2650. q2 = q1;
  2651. if (coded) {
  2652. int last = 0, skip, value;
  2653. const uint8_t *zz_table;
  2654. int k;
  2655. if (v->s.ac_pred) {
  2656. if (!use_pred && v->fcm == ILACE_FRAME) {
  2657. zz_table = v->zzi_8x8;
  2658. } else {
  2659. if (!dc_pred_dir) // top
  2660. zz_table = v->zz_8x8[2];
  2661. else // left
  2662. zz_table = v->zz_8x8[3];
  2663. }
  2664. } else {
  2665. if (v->fcm != ILACE_FRAME)
  2666. zz_table = v->zz_8x8[1];
  2667. else
  2668. zz_table = v->zzi_8x8;
  2669. }
  2670. while (!last) {
  2671. vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
  2672. i += skip;
  2673. if (i > 63)
  2674. break;
  2675. block[zz_table[i++]] = value;
  2676. }
  2677. /* apply AC prediction if needed */
  2678. if (use_pred) {
  2679. /* scale predictors if needed*/
  2680. if (q2 && q1 != q2) {
  2681. q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
  2682. q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
  2683. if (q1 < 1)
  2684. return AVERROR_INVALIDDATA;
  2685. if (dc_pred_dir) { // left
  2686. for (k = 1; k < 8; k++)
  2687. block[k << v->left_blk_sh] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
  2688. } else { // top
  2689. for (k = 1; k < 8; k++)
  2690. block[k << v->top_blk_sh] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
  2691. }
  2692. } else {
  2693. if (dc_pred_dir) { //left
  2694. for (k = 1; k < 8; k++)
  2695. block[k << v->left_blk_sh] += ac_val[k];
  2696. } else { //top
  2697. for (k = 1; k < 8; k++)
  2698. block[k << v->top_blk_sh] += ac_val[k + 8];
  2699. }
  2700. }
  2701. }
  2702. /* save AC coeffs for further prediction */
  2703. for (k = 1; k < 8; k++) {
  2704. ac_val2[k ] = block[k << v->left_blk_sh];
  2705. ac_val2[k + 8] = block[k << v->top_blk_sh];
  2706. }
  2707. /* scale AC coeffs */
  2708. for (k = 1; k < 64; k++)
  2709. if (block[k]) {
  2710. block[k] *= scale;
  2711. if (!v->pquantizer)
  2712. block[k] += (block[k] < 0) ? -mquant : mquant;
  2713. }
  2714. if (use_pred) i = 63;
  2715. } else { // no AC coeffs
  2716. int k;
  2717. memset(ac_val2, 0, 16 * 2);
  2718. if (dc_pred_dir) { // left
  2719. if (use_pred) {
  2720. memcpy(ac_val2, ac_val, 8 * 2);
  2721. if (q2 && q1 != q2) {
  2722. q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
  2723. q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
  2724. if (q1 < 1)
  2725. return AVERROR_INVALIDDATA;
  2726. for (k = 1; k < 8; k++)
  2727. ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
  2728. }
  2729. }
  2730. } else { // top
  2731. if (use_pred) {
  2732. memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
  2733. if (q2 && q1 != q2) {
  2734. q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
  2735. q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
  2736. if (q1 < 1)
  2737. return AVERROR_INVALIDDATA;
  2738. for (k = 1; k < 8; k++)
  2739. ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
  2740. }
  2741. }
  2742. }
  2743. /* apply AC prediction if needed */
  2744. if (use_pred) {
  2745. if (dc_pred_dir) { // left
  2746. for (k = 1; k < 8; k++) {
  2747. block[k << v->left_blk_sh] = ac_val2[k] * scale;
  2748. if (!v->pquantizer && block[k << v->left_blk_sh])
  2749. block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -mquant : mquant;
  2750. }
  2751. } else { // top
  2752. for (k = 1; k < 8; k++) {
  2753. block[k << v->top_blk_sh] = ac_val2[k + 8] * scale;
  2754. if (!v->pquantizer && block[k << v->top_blk_sh])
  2755. block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -mquant : mquant;
  2756. }
  2757. }
  2758. i = 63;
  2759. }
  2760. }
  2761. s->block_last_index[n] = i;
  2762. return 0;
  2763. }
  2764. /** Decode intra block in inter frames - more generic version than vc1_decode_i_block
  2765. * @param v VC1Context
  2766. * @param block block to decode
  2767. * @param[in] n subblock index
  2768. * @param coded are AC coeffs present or not
  2769. * @param mquant block quantizer
  2770. * @param codingset set of VLC to decode data
  2771. */
  2772. static int vc1_decode_intra_block(VC1Context *v, int16_t block[64], int n,
  2773. int coded, int mquant, int codingset)
  2774. {
  2775. GetBitContext *gb = &v->s.gb;
  2776. MpegEncContext *s = &v->s;
  2777. int dc_pred_dir = 0; /* Direction of the DC prediction used */
  2778. int i;
  2779. int16_t *dc_val = NULL;
  2780. int16_t *ac_val, *ac_val2;
  2781. int dcdiff;
  2782. int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
  2783. int a_avail = v->a_avail, c_avail = v->c_avail;
  2784. int use_pred = s->ac_pred;
  2785. int scale;
  2786. int q1, q2 = 0;
  2787. s->bdsp.clear_block(block);
  2788. /* XXX: Guard against dumb values of mquant */
  2789. mquant = (mquant < 1) ? 0 : ((mquant > 31) ? 31 : mquant);
  2790. /* Set DC scale - y and c use the same */
  2791. s->y_dc_scale = s->y_dc_scale_table[mquant];
  2792. s->c_dc_scale = s->c_dc_scale_table[mquant];
  2793. /* Get DC differential */
  2794. if (n < 4) {
  2795. dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
  2796. } else {
  2797. dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
  2798. }
  2799. if (dcdiff < 0) {
  2800. av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
  2801. return -1;
  2802. }
  2803. if (dcdiff) {
  2804. if (dcdiff == 119 /* ESC index value */) {
  2805. /* TODO: Optimize */
  2806. if (mquant == 1) dcdiff = get_bits(gb, 10);
  2807. else if (mquant == 2) dcdiff = get_bits(gb, 9);
  2808. else dcdiff = get_bits(gb, 8);
  2809. } else {
  2810. if (mquant == 1)
  2811. dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
  2812. else if (mquant == 2)
  2813. dcdiff = (dcdiff << 1) + get_bits1(gb) - 1;
  2814. }
  2815. if (get_bits1(gb))
  2816. dcdiff = -dcdiff;
  2817. }
  2818. /* Prediction */
  2819. dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, a_avail, c_avail, &dc_val, &dc_pred_dir);
  2820. *dc_val = dcdiff;
  2821. /* Store the quantized DC coeff, used for prediction */
  2822. if (n < 4) {
  2823. block[0] = dcdiff * s->y_dc_scale;
  2824. } else {
  2825. block[0] = dcdiff * s->c_dc_scale;
  2826. }
  2827. //AC Decoding
  2828. i = 1;
  2829. /* check if AC is needed at all and adjust direction if needed */
  2830. if (!a_avail) dc_pred_dir = 1;
  2831. if (!c_avail) dc_pred_dir = 0;
  2832. if (!a_avail && !c_avail) use_pred = 0;
  2833. ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
  2834. ac_val2 = ac_val;
  2835. scale = mquant * 2 + v->halfpq;
  2836. if (dc_pred_dir) //left
  2837. ac_val -= 16;
  2838. else //top
  2839. ac_val -= 16 * s->block_wrap[n];
  2840. q1 = s->current_picture.qscale_table[mb_pos];
  2841. if (dc_pred_dir && c_avail && mb_pos)
  2842. q2 = s->current_picture.qscale_table[mb_pos - 1];
  2843. if (!dc_pred_dir && a_avail && mb_pos >= s->mb_stride)
  2844. q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
  2845. if ( dc_pred_dir && n == 1)
  2846. q2 = q1;
  2847. if (!dc_pred_dir && n == 2)
  2848. q2 = q1;
  2849. if (n == 3) q2 = q1;
  2850. if (coded) {
  2851. int last = 0, skip, value;
  2852. int k;
  2853. while (!last) {
  2854. vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
  2855. i += skip;
  2856. if (i > 63)
  2857. break;
  2858. if (v->fcm == PROGRESSIVE)
  2859. block[v->zz_8x8[0][i++]] = value;
  2860. else {
  2861. if (use_pred && (v->fcm == ILACE_FRAME)) {
  2862. if (!dc_pred_dir) // top
  2863. block[v->zz_8x8[2][i++]] = value;
  2864. else // left
  2865. block[v->zz_8x8[3][i++]] = value;
  2866. } else {
  2867. block[v->zzi_8x8[i++]] = value;
  2868. }
  2869. }
  2870. }
  2871. /* apply AC prediction if needed */
  2872. if (use_pred) {
  2873. /* scale predictors if needed*/
  2874. if (q2 && q1 != q2) {
  2875. q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
  2876. q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
  2877. if (q1 < 1)
  2878. return AVERROR_INVALIDDATA;
  2879. if (dc_pred_dir) { // left
  2880. for (k = 1; k < 8; k++)
  2881. block[k << v->left_blk_sh] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
  2882. } else { //top
  2883. for (k = 1; k < 8; k++)
  2884. block[k << v->top_blk_sh] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
  2885. }
  2886. } else {
  2887. if (dc_pred_dir) { // left
  2888. for (k = 1; k < 8; k++)
  2889. block[k << v->left_blk_sh] += ac_val[k];
  2890. } else { // top
  2891. for (k = 1; k < 8; k++)
  2892. block[k << v->top_blk_sh] += ac_val[k + 8];
  2893. }
  2894. }
  2895. }
  2896. /* save AC coeffs for further prediction */
  2897. for (k = 1; k < 8; k++) {
  2898. ac_val2[k ] = block[k << v->left_blk_sh];
  2899. ac_val2[k + 8] = block[k << v->top_blk_sh];
  2900. }
  2901. /* scale AC coeffs */
  2902. for (k = 1; k < 64; k++)
  2903. if (block[k]) {
  2904. block[k] *= scale;
  2905. if (!v->pquantizer)
  2906. block[k] += (block[k] < 0) ? -mquant : mquant;
  2907. }
  2908. if (use_pred) i = 63;
  2909. } else { // no AC coeffs
  2910. int k;
  2911. memset(ac_val2, 0, 16 * 2);
  2912. if (dc_pred_dir) { // left
  2913. if (use_pred) {
  2914. memcpy(ac_val2, ac_val, 8 * 2);
  2915. if (q2 && q1 != q2) {
  2916. q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
  2917. q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
  2918. if (q1 < 1)
  2919. return AVERROR_INVALIDDATA;
  2920. for (k = 1; k < 8; k++)
  2921. ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
  2922. }
  2923. }
  2924. } else { // top
  2925. if (use_pred) {
  2926. memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
  2927. if (q2 && q1 != q2) {
  2928. q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
  2929. q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
  2930. if (q1 < 1)
  2931. return AVERROR_INVALIDDATA;
  2932. for (k = 1; k < 8; k++)
  2933. ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
  2934. }
  2935. }
  2936. }
  2937. /* apply AC prediction if needed */
  2938. if (use_pred) {
  2939. if (dc_pred_dir) { // left
  2940. for (k = 1; k < 8; k++) {
  2941. block[k << v->left_blk_sh] = ac_val2[k] * scale;
  2942. if (!v->pquantizer && block[k << v->left_blk_sh])
  2943. block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -mquant : mquant;
  2944. }
  2945. } else { // top
  2946. for (k = 1; k < 8; k++) {
  2947. block[k << v->top_blk_sh] = ac_val2[k + 8] * scale;
  2948. if (!v->pquantizer && block[k << v->top_blk_sh])
  2949. block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -mquant : mquant;
  2950. }
  2951. }
  2952. i = 63;
  2953. }
  2954. }
  2955. s->block_last_index[n] = i;
  2956. return 0;
  2957. }
  2958. /** Decode P block
  2959. */
  2960. static int vc1_decode_p_block(VC1Context *v, int16_t block[64], int n,
  2961. int mquant, int ttmb, int first_block,
  2962. uint8_t *dst, int linesize, int skip_block,
  2963. int *ttmb_out)
  2964. {
  2965. MpegEncContext *s = &v->s;
  2966. GetBitContext *gb = &s->gb;
  2967. int i, j;
  2968. int subblkpat = 0;
  2969. int scale, off, idx, last, skip, value;
  2970. int ttblk = ttmb & 7;
  2971. int pat = 0;
  2972. s->bdsp.clear_block(block);
  2973. if (ttmb == -1) {
  2974. ttblk = ff_vc1_ttblk_to_tt[v->tt_index][get_vlc2(gb, ff_vc1_ttblk_vlc[v->tt_index].table, VC1_TTBLK_VLC_BITS, 1)];
  2975. }
  2976. if (ttblk == TT_4X4) {
  2977. subblkpat = ~(get_vlc2(gb, ff_vc1_subblkpat_vlc[v->tt_index].table, VC1_SUBBLKPAT_VLC_BITS, 1) + 1);
  2978. }
  2979. if ((ttblk != TT_8X8 && ttblk != TT_4X4)
  2980. && ((v->ttmbf || (ttmb != -1 && (ttmb & 8) && !first_block))
  2981. || (!v->res_rtm_flag && !first_block))) {
  2982. subblkpat = decode012(gb);
  2983. if (subblkpat)
  2984. subblkpat ^= 3; // swap decoded pattern bits
  2985. if (ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM)
  2986. ttblk = TT_8X4;
  2987. if (ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT)
  2988. ttblk = TT_4X8;
  2989. }
  2990. scale = 2 * mquant + ((v->pq == mquant) ? v->halfpq : 0);
  2991. // convert transforms like 8X4_TOP to generic TT and SUBBLKPAT
  2992. if (ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM) {
  2993. subblkpat = 2 - (ttblk == TT_8X4_TOP);
  2994. ttblk = TT_8X4;
  2995. }
  2996. if (ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT) {
  2997. subblkpat = 2 - (ttblk == TT_4X8_LEFT);
  2998. ttblk = TT_4X8;
  2999. }
  3000. switch (ttblk) {
  3001. case TT_8X8:
  3002. pat = 0xF;
  3003. i = 0;
  3004. last = 0;
  3005. while (!last) {
  3006. vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
  3007. i += skip;
  3008. if (i > 63)
  3009. break;
  3010. if (!v->fcm)
  3011. idx = v->zz_8x8[0][i++];
  3012. else
  3013. idx = v->zzi_8x8[i++];
  3014. block[idx] = value * scale;
  3015. if (!v->pquantizer)
  3016. block[idx] += (block[idx] < 0) ? -mquant : mquant;
  3017. }
  3018. if (!skip_block) {
  3019. if (i == 1)
  3020. v->vc1dsp.vc1_inv_trans_8x8_dc(dst, linesize, block);
  3021. else {
  3022. v->vc1dsp.vc1_inv_trans_8x8(block);
  3023. s->idsp.add_pixels_clamped(block, dst, linesize);
  3024. }
  3025. }
  3026. break;
  3027. case TT_4X4:
  3028. pat = ~subblkpat & 0xF;
  3029. for (j = 0; j < 4; j++) {
  3030. last = subblkpat & (1 << (3 - j));
  3031. i = 0;
  3032. off = (j & 1) * 4 + (j & 2) * 16;
  3033. while (!last) {
  3034. vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
  3035. i += skip;
  3036. if (i > 15)
  3037. break;
  3038. if (!v->fcm)
  3039. idx = ff_vc1_simple_progressive_4x4_zz[i++];
  3040. else
  3041. idx = ff_vc1_adv_interlaced_4x4_zz[i++];
  3042. block[idx + off] = value * scale;
  3043. if (!v->pquantizer)
  3044. block[idx + off] += (block[idx + off] < 0) ? -mquant : mquant;
  3045. }
  3046. if (!(subblkpat & (1 << (3 - j))) && !skip_block) {
  3047. if (i == 1)
  3048. v->vc1dsp.vc1_inv_trans_4x4_dc(dst + (j & 1) * 4 + (j & 2) * 2 * linesize, linesize, block + off);
  3049. else
  3050. v->vc1dsp.vc1_inv_trans_4x4(dst + (j & 1) * 4 + (j & 2) * 2 * linesize, linesize, block + off);
  3051. }
  3052. }
  3053. break;
  3054. case TT_8X4:
  3055. pat = ~((subblkpat & 2) * 6 + (subblkpat & 1) * 3) & 0xF;
  3056. for (j = 0; j < 2; j++) {
  3057. last = subblkpat & (1 << (1 - j));
  3058. i = 0;
  3059. off = j * 32;
  3060. while (!last) {
  3061. vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
  3062. i += skip;
  3063. if (i > 31)
  3064. break;
  3065. if (!v->fcm)
  3066. idx = v->zz_8x4[i++] + off;
  3067. else
  3068. idx = ff_vc1_adv_interlaced_8x4_zz[i++] + off;
  3069. block[idx] = value * scale;
  3070. if (!v->pquantizer)
  3071. block[idx] += (block[idx] < 0) ? -mquant : mquant;
  3072. }
  3073. if (!(subblkpat & (1 << (1 - j))) && !skip_block) {
  3074. if (i == 1)
  3075. v->vc1dsp.vc1_inv_trans_8x4_dc(dst + j * 4 * linesize, linesize, block + off);
  3076. else
  3077. v->vc1dsp.vc1_inv_trans_8x4(dst + j * 4 * linesize, linesize, block + off);
  3078. }
  3079. }
  3080. break;
  3081. case TT_4X8:
  3082. pat = ~(subblkpat * 5) & 0xF;
  3083. for (j = 0; j < 2; j++) {
  3084. last = subblkpat & (1 << (1 - j));
  3085. i = 0;
  3086. off = j * 4;
  3087. while (!last) {
  3088. vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
  3089. i += skip;
  3090. if (i > 31)
  3091. break;
  3092. if (!v->fcm)
  3093. idx = v->zz_4x8[i++] + off;
  3094. else
  3095. idx = ff_vc1_adv_interlaced_4x8_zz[i++] + off;
  3096. block[idx] = value * scale;
  3097. if (!v->pquantizer)
  3098. block[idx] += (block[idx] < 0) ? -mquant : mquant;
  3099. }
  3100. if (!(subblkpat & (1 << (1 - j))) && !skip_block) {
  3101. if (i == 1)
  3102. v->vc1dsp.vc1_inv_trans_4x8_dc(dst + j * 4, linesize, block + off);
  3103. else
  3104. v->vc1dsp.vc1_inv_trans_4x8(dst + j*4, linesize, block + off);
  3105. }
  3106. }
  3107. break;
  3108. }
  3109. if (ttmb_out)
  3110. *ttmb_out |= ttblk << (n * 4);
  3111. return pat;
  3112. }
  3113. /** @} */ // Macroblock group
  3114. static const int size_table [6] = { 0, 2, 3, 4, 5, 8 };
  3115. static const int offset_table[6] = { 0, 1, 3, 7, 15, 31 };
  3116. static av_always_inline void vc1_apply_p_v_loop_filter(VC1Context *v, int block_num)
  3117. {
  3118. MpegEncContext *s = &v->s;
  3119. int mb_cbp = v->cbp[s->mb_x - s->mb_stride],
  3120. block_cbp = mb_cbp >> (block_num * 4), bottom_cbp,
  3121. mb_is_intra = v->is_intra[s->mb_x - s->mb_stride],
  3122. block_is_intra = mb_is_intra >> (block_num * 4), bottom_is_intra;
  3123. int idx, linesize = block_num > 3 ? s->uvlinesize : s->linesize, ttblk;
  3124. uint8_t *dst;
  3125. if (block_num > 3) {
  3126. dst = s->dest[block_num - 3];
  3127. } else {
  3128. dst = s->dest[0] + (block_num & 1) * 8 + ((block_num & 2) * 4 - 8) * linesize;
  3129. }
  3130. if (s->mb_y != s->end_mb_y || block_num < 2) {
  3131. int16_t (*mv)[2];
  3132. int mv_stride;
  3133. if (block_num > 3) {
  3134. bottom_cbp = v->cbp[s->mb_x] >> (block_num * 4);
  3135. bottom_is_intra = v->is_intra[s->mb_x] >> (block_num * 4);
  3136. mv = &v->luma_mv[s->mb_x - s->mb_stride];
  3137. mv_stride = s->mb_stride;
  3138. } else {
  3139. bottom_cbp = (block_num < 2) ? (mb_cbp >> ((block_num + 2) * 4))
  3140. : (v->cbp[s->mb_x] >> ((block_num - 2) * 4));
  3141. bottom_is_intra = (block_num < 2) ? (mb_is_intra >> ((block_num + 2) * 4))
  3142. : (v->is_intra[s->mb_x] >> ((block_num - 2) * 4));
  3143. mv_stride = s->b8_stride;
  3144. mv = &s->current_picture.motion_val[0][s->block_index[block_num] - 2 * mv_stride];
  3145. }
  3146. if (bottom_is_intra & 1 || block_is_intra & 1 ||
  3147. mv[0][0] != mv[mv_stride][0] || mv[0][1] != mv[mv_stride][1]) {
  3148. v->vc1dsp.vc1_v_loop_filter8(dst, linesize, v->pq);
  3149. } else {
  3150. idx = ((bottom_cbp >> 2) | block_cbp) & 3;
  3151. if (idx == 3) {
  3152. v->vc1dsp.vc1_v_loop_filter8(dst, linesize, v->pq);
  3153. } else if (idx) {
  3154. if (idx == 1)
  3155. v->vc1dsp.vc1_v_loop_filter4(dst + 4, linesize, v->pq);
  3156. else
  3157. v->vc1dsp.vc1_v_loop_filter4(dst, linesize, v->pq);
  3158. }
  3159. }
  3160. }
  3161. dst -= 4 * linesize;
  3162. ttblk = (v->ttblk[s->mb_x - s->mb_stride] >> (block_num * 4)) & 0xF;
  3163. if (ttblk == TT_4X4 || ttblk == TT_8X4) {
  3164. idx = (block_cbp | (block_cbp >> 2)) & 3;
  3165. if (idx == 3) {
  3166. v->vc1dsp.vc1_v_loop_filter8(dst, linesize, v->pq);
  3167. } else if (idx) {
  3168. if (idx == 1)
  3169. v->vc1dsp.vc1_v_loop_filter4(dst + 4, linesize, v->pq);
  3170. else
  3171. v->vc1dsp.vc1_v_loop_filter4(dst, linesize, v->pq);
  3172. }
  3173. }
  3174. }
  3175. static av_always_inline void vc1_apply_p_h_loop_filter(VC1Context *v, int block_num)
  3176. {
  3177. MpegEncContext *s = &v->s;
  3178. int mb_cbp = v->cbp[s->mb_x - 1 - s->mb_stride],
  3179. block_cbp = mb_cbp >> (block_num * 4), right_cbp,
  3180. mb_is_intra = v->is_intra[s->mb_x - 1 - s->mb_stride],
  3181. block_is_intra = mb_is_intra >> (block_num * 4), right_is_intra;
  3182. int idx, linesize = block_num > 3 ? s->uvlinesize : s->linesize, ttblk;
  3183. uint8_t *dst;
  3184. if (block_num > 3) {
  3185. dst = s->dest[block_num - 3] - 8 * linesize;
  3186. } else {
  3187. dst = s->dest[0] + (block_num & 1) * 8 + ((block_num & 2) * 4 - 16) * linesize - 8;
  3188. }
  3189. if (s->mb_x != s->mb_width || !(block_num & 5)) {
  3190. int16_t (*mv)[2];
  3191. if (block_num > 3) {
  3192. right_cbp = v->cbp[s->mb_x - s->mb_stride] >> (block_num * 4);
  3193. right_is_intra = v->is_intra[s->mb_x - s->mb_stride] >> (block_num * 4);
  3194. mv = &v->luma_mv[s->mb_x - s->mb_stride - 1];
  3195. } else {
  3196. right_cbp = (block_num & 1) ? (v->cbp[s->mb_x - s->mb_stride] >> ((block_num - 1) * 4))
  3197. : (mb_cbp >> ((block_num + 1) * 4));
  3198. right_is_intra = (block_num & 1) ? (v->is_intra[s->mb_x - s->mb_stride] >> ((block_num - 1) * 4))
  3199. : (mb_is_intra >> ((block_num + 1) * 4));
  3200. mv = &s->current_picture.motion_val[0][s->block_index[block_num] - s->b8_stride * 2 - 2];
  3201. }
  3202. if (block_is_intra & 1 || right_is_intra & 1 || mv[0][0] != mv[1][0] || mv[0][1] != mv[1][1]) {
  3203. v->vc1dsp.vc1_h_loop_filter8(dst, linesize, v->pq);
  3204. } else {
  3205. idx = ((right_cbp >> 1) | block_cbp) & 5; // FIXME check
  3206. if (idx == 5) {
  3207. v->vc1dsp.vc1_h_loop_filter8(dst, linesize, v->pq);
  3208. } else if (idx) {
  3209. if (idx == 1)
  3210. v->vc1dsp.vc1_h_loop_filter4(dst + 4 * linesize, linesize, v->pq);
  3211. else
  3212. v->vc1dsp.vc1_h_loop_filter4(dst, linesize, v->pq);
  3213. }
  3214. }
  3215. }
  3216. dst -= 4;
  3217. ttblk = (v->ttblk[s->mb_x - s->mb_stride - 1] >> (block_num * 4)) & 0xf;
  3218. if (ttblk == TT_4X4 || ttblk == TT_4X8) {
  3219. idx = (block_cbp | (block_cbp >> 1)) & 5;
  3220. if (idx == 5) {
  3221. v->vc1dsp.vc1_h_loop_filter8(dst, linesize, v->pq);
  3222. } else if (idx) {
  3223. if (idx == 1)
  3224. v->vc1dsp.vc1_h_loop_filter4(dst + linesize * 4, linesize, v->pq);
  3225. else
  3226. v->vc1dsp.vc1_h_loop_filter4(dst, linesize, v->pq);
  3227. }
  3228. }
  3229. }
  3230. static void vc1_apply_p_loop_filter(VC1Context *v)
  3231. {
  3232. MpegEncContext *s = &v->s;
  3233. int i;
  3234. for (i = 0; i < 6; i++) {
  3235. vc1_apply_p_v_loop_filter(v, i);
  3236. }
  3237. /* V always precedes H, therefore we run H one MB before V;
  3238. * at the end of a row, we catch up to complete the row */
  3239. if (s->mb_x) {
  3240. for (i = 0; i < 6; i++) {
  3241. vc1_apply_p_h_loop_filter(v, i);
  3242. }
  3243. if (s->mb_x == s->mb_width - 1) {
  3244. s->mb_x++;
  3245. ff_update_block_index(s);
  3246. for (i = 0; i < 6; i++) {
  3247. vc1_apply_p_h_loop_filter(v, i);
  3248. }
  3249. }
  3250. }
  3251. }
  3252. /** Decode one P-frame MB
  3253. */
  3254. static int vc1_decode_p_mb(VC1Context *v)
  3255. {
  3256. MpegEncContext *s = &v->s;
  3257. GetBitContext *gb = &s->gb;
  3258. int i, j;
  3259. int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
  3260. int cbp; /* cbp decoding stuff */
  3261. int mqdiff, mquant; /* MB quantization */
  3262. int ttmb = v->ttfrm; /* MB Transform type */
  3263. int mb_has_coeffs = 1; /* last_flag */
  3264. int dmv_x, dmv_y; /* Differential MV components */
  3265. int index, index1; /* LUT indexes */
  3266. int val, sign; /* temp values */
  3267. int first_block = 1;
  3268. int dst_idx, off;
  3269. int skipped, fourmv;
  3270. int block_cbp = 0, pat, block_tt = 0, block_intra = 0;
  3271. mquant = v->pq; /* lossy initialization */
  3272. if (v->mv_type_is_raw)
  3273. fourmv = get_bits1(gb);
  3274. else
  3275. fourmv = v->mv_type_mb_plane[mb_pos];
  3276. if (v->skip_is_raw)
  3277. skipped = get_bits1(gb);
  3278. else
  3279. skipped = v->s.mbskip_table[mb_pos];
  3280. if (!fourmv) { /* 1MV mode */
  3281. if (!skipped) {
  3282. GET_MVDATA(dmv_x, dmv_y);
  3283. if (s->mb_intra) {
  3284. s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
  3285. s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
  3286. }
  3287. s->current_picture.mb_type[mb_pos] = s->mb_intra ? MB_TYPE_INTRA : MB_TYPE_16x16;
  3288. vc1_pred_mv(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
  3289. /* FIXME Set DC val for inter block ? */
  3290. if (s->mb_intra && !mb_has_coeffs) {
  3291. GET_MQUANT();
  3292. s->ac_pred = get_bits1(gb);
  3293. cbp = 0;
  3294. } else if (mb_has_coeffs) {
  3295. if (s->mb_intra)
  3296. s->ac_pred = get_bits1(gb);
  3297. cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
  3298. GET_MQUANT();
  3299. } else {
  3300. mquant = v->pq;
  3301. cbp = 0;
  3302. }
  3303. s->current_picture.qscale_table[mb_pos] = mquant;
  3304. if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
  3305. ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table,
  3306. VC1_TTMB_VLC_BITS, 2);
  3307. if (!s->mb_intra) vc1_mc_1mv(v, 0);
  3308. dst_idx = 0;
  3309. for (i = 0; i < 6; i++) {
  3310. s->dc_val[0][s->block_index[i]] = 0;
  3311. dst_idx += i >> 2;
  3312. val = ((cbp >> (5 - i)) & 1);
  3313. off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
  3314. v->mb_type[0][s->block_index[i]] = s->mb_intra;
  3315. if (s->mb_intra) {
  3316. /* check if prediction blocks A and C are available */
  3317. v->a_avail = v->c_avail = 0;
  3318. if (i == 2 || i == 3 || !s->first_slice_line)
  3319. v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
  3320. if (i == 1 || i == 3 || s->mb_x)
  3321. v->c_avail = v->mb_type[0][s->block_index[i] - 1];
  3322. vc1_decode_intra_block(v, s->block[i], i, val, mquant,
  3323. (i & 4) ? v->codingset2 : v->codingset);
  3324. if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
  3325. continue;
  3326. v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
  3327. if (v->rangeredfrm)
  3328. for (j = 0; j < 64; j++)
  3329. s->block[i][j] <<= 1;
  3330. s->idsp.put_signed_pixels_clamped(s->block[i],
  3331. s->dest[dst_idx] + off,
  3332. i & 4 ? s->uvlinesize
  3333. : s->linesize);
  3334. if (v->pq >= 9 && v->overlap) {
  3335. if (v->c_avail)
  3336. v->vc1dsp.vc1_h_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
  3337. if (v->a_avail)
  3338. v->vc1dsp.vc1_v_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
  3339. }
  3340. block_cbp |= 0xF << (i << 2);
  3341. block_intra |= 1 << i;
  3342. } else if (val) {
  3343. pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb, first_block,
  3344. s->dest[dst_idx] + off, (i & 4) ? s->uvlinesize : s->linesize,
  3345. (i & 4) && (s->flags & CODEC_FLAG_GRAY), &block_tt);
  3346. block_cbp |= pat << (i << 2);
  3347. if (!v->ttmbf && ttmb < 8)
  3348. ttmb = -1;
  3349. first_block = 0;
  3350. }
  3351. }
  3352. } else { // skipped
  3353. s->mb_intra = 0;
  3354. for (i = 0; i < 6; i++) {
  3355. v->mb_type[0][s->block_index[i]] = 0;
  3356. s->dc_val[0][s->block_index[i]] = 0;
  3357. }
  3358. s->current_picture.mb_type[mb_pos] = MB_TYPE_SKIP;
  3359. s->current_picture.qscale_table[mb_pos] = 0;
  3360. vc1_pred_mv(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
  3361. vc1_mc_1mv(v, 0);
  3362. }
  3363. } else { // 4MV mode
  3364. if (!skipped /* unskipped MB */) {
  3365. int intra_count = 0, coded_inter = 0;
  3366. int is_intra[6], is_coded[6];
  3367. /* Get CBPCY */
  3368. cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
  3369. for (i = 0; i < 6; i++) {
  3370. val = ((cbp >> (5 - i)) & 1);
  3371. s->dc_val[0][s->block_index[i]] = 0;
  3372. s->mb_intra = 0;
  3373. if (i < 4) {
  3374. dmv_x = dmv_y = 0;
  3375. s->mb_intra = 0;
  3376. mb_has_coeffs = 0;
  3377. if (val) {
  3378. GET_MVDATA(dmv_x, dmv_y);
  3379. }
  3380. vc1_pred_mv(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], 0, 0);
  3381. if (!s->mb_intra)
  3382. vc1_mc_4mv_luma(v, i, 0, 0);
  3383. intra_count += s->mb_intra;
  3384. is_intra[i] = s->mb_intra;
  3385. is_coded[i] = mb_has_coeffs;
  3386. }
  3387. if (i & 4) {
  3388. is_intra[i] = (intra_count >= 3);
  3389. is_coded[i] = val;
  3390. }
  3391. if (i == 4)
  3392. vc1_mc_4mv_chroma(v, 0);
  3393. v->mb_type[0][s->block_index[i]] = is_intra[i];
  3394. if (!coded_inter)
  3395. coded_inter = !is_intra[i] & is_coded[i];
  3396. }
  3397. // if there are no coded blocks then don't do anything more
  3398. dst_idx = 0;
  3399. if (!intra_count && !coded_inter)
  3400. goto end;
  3401. GET_MQUANT();
  3402. s->current_picture.qscale_table[mb_pos] = mquant;
  3403. /* test if block is intra and has pred */
  3404. {
  3405. int intrapred = 0;
  3406. for (i = 0; i < 6; i++)
  3407. if (is_intra[i]) {
  3408. if (((!s->first_slice_line || (i == 2 || i == 3)) && v->mb_type[0][s->block_index[i] - s->block_wrap[i]])
  3409. || ((s->mb_x || (i == 1 || i == 3)) && v->mb_type[0][s->block_index[i] - 1])) {
  3410. intrapred = 1;
  3411. break;
  3412. }
  3413. }
  3414. if (intrapred)
  3415. s->ac_pred = get_bits1(gb);
  3416. else
  3417. s->ac_pred = 0;
  3418. }
  3419. if (!v->ttmbf && coded_inter)
  3420. ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
  3421. for (i = 0; i < 6; i++) {
  3422. dst_idx += i >> 2;
  3423. off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
  3424. s->mb_intra = is_intra[i];
  3425. if (is_intra[i]) {
  3426. /* check if prediction blocks A and C are available */
  3427. v->a_avail = v->c_avail = 0;
  3428. if (i == 2 || i == 3 || !s->first_slice_line)
  3429. v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
  3430. if (i == 1 || i == 3 || s->mb_x)
  3431. v->c_avail = v->mb_type[0][s->block_index[i] - 1];
  3432. vc1_decode_intra_block(v, s->block[i], i, is_coded[i], mquant,
  3433. (i & 4) ? v->codingset2 : v->codingset);
  3434. if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
  3435. continue;
  3436. v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
  3437. if (v->rangeredfrm)
  3438. for (j = 0; j < 64; j++)
  3439. s->block[i][j] <<= 1;
  3440. s->idsp.put_signed_pixels_clamped(s->block[i],
  3441. s->dest[dst_idx] + off,
  3442. (i & 4) ? s->uvlinesize
  3443. : s->linesize);
  3444. if (v->pq >= 9 && v->overlap) {
  3445. if (v->c_avail)
  3446. v->vc1dsp.vc1_h_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
  3447. if (v->a_avail)
  3448. v->vc1dsp.vc1_v_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
  3449. }
  3450. block_cbp |= 0xF << (i << 2);
  3451. block_intra |= 1 << i;
  3452. } else if (is_coded[i]) {
  3453. pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
  3454. first_block, s->dest[dst_idx] + off,
  3455. (i & 4) ? s->uvlinesize : s->linesize,
  3456. (i & 4) && (s->flags & CODEC_FLAG_GRAY),
  3457. &block_tt);
  3458. block_cbp |= pat << (i << 2);
  3459. if (!v->ttmbf && ttmb < 8)
  3460. ttmb = -1;
  3461. first_block = 0;
  3462. }
  3463. }
  3464. } else { // skipped MB
  3465. s->mb_intra = 0;
  3466. s->current_picture.qscale_table[mb_pos] = 0;
  3467. for (i = 0; i < 6; i++) {
  3468. v->mb_type[0][s->block_index[i]] = 0;
  3469. s->dc_val[0][s->block_index[i]] = 0;
  3470. }
  3471. for (i = 0; i < 4; i++) {
  3472. vc1_pred_mv(v, i, 0, 0, 0, v->range_x, v->range_y, v->mb_type[0], 0, 0);
  3473. vc1_mc_4mv_luma(v, i, 0, 0);
  3474. }
  3475. vc1_mc_4mv_chroma(v, 0);
  3476. s->current_picture.qscale_table[mb_pos] = 0;
  3477. }
  3478. }
  3479. end:
  3480. v->cbp[s->mb_x] = block_cbp;
  3481. v->ttblk[s->mb_x] = block_tt;
  3482. v->is_intra[s->mb_x] = block_intra;
  3483. return 0;
  3484. }
  3485. /* Decode one macroblock in an interlaced frame p picture */
  3486. static int vc1_decode_p_mb_intfr(VC1Context *v)
  3487. {
  3488. MpegEncContext *s = &v->s;
  3489. GetBitContext *gb = &s->gb;
  3490. int i;
  3491. int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
  3492. int cbp = 0; /* cbp decoding stuff */
  3493. int mqdiff, mquant; /* MB quantization */
  3494. int ttmb = v->ttfrm; /* MB Transform type */
  3495. int mb_has_coeffs = 1; /* last_flag */
  3496. int dmv_x, dmv_y; /* Differential MV components */
  3497. int val; /* temp value */
  3498. int first_block = 1;
  3499. int dst_idx, off;
  3500. int skipped, fourmv = 0, twomv = 0;
  3501. int block_cbp = 0, pat, block_tt = 0;
  3502. int idx_mbmode = 0, mvbp;
  3503. int stride_y, fieldtx;
  3504. mquant = v->pq; /* Lossy initialization */
  3505. if (v->skip_is_raw)
  3506. skipped = get_bits1(gb);
  3507. else
  3508. skipped = v->s.mbskip_table[mb_pos];
  3509. if (!skipped) {
  3510. if (v->fourmvswitch)
  3511. idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_4MV_MBMODE_VLC_BITS, 2); // try getting this done
  3512. else
  3513. idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_NON4MV_MBMODE_VLC_BITS, 2); // in a single line
  3514. switch (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0]) {
  3515. /* store the motion vector type in a flag (useful later) */
  3516. case MV_PMODE_INTFR_4MV:
  3517. fourmv = 1;
  3518. v->blk_mv_type[s->block_index[0]] = 0;
  3519. v->blk_mv_type[s->block_index[1]] = 0;
  3520. v->blk_mv_type[s->block_index[2]] = 0;
  3521. v->blk_mv_type[s->block_index[3]] = 0;
  3522. break;
  3523. case MV_PMODE_INTFR_4MV_FIELD:
  3524. fourmv = 1;
  3525. v->blk_mv_type[s->block_index[0]] = 1;
  3526. v->blk_mv_type[s->block_index[1]] = 1;
  3527. v->blk_mv_type[s->block_index[2]] = 1;
  3528. v->blk_mv_type[s->block_index[3]] = 1;
  3529. break;
  3530. case MV_PMODE_INTFR_2MV_FIELD:
  3531. twomv = 1;
  3532. v->blk_mv_type[s->block_index[0]] = 1;
  3533. v->blk_mv_type[s->block_index[1]] = 1;
  3534. v->blk_mv_type[s->block_index[2]] = 1;
  3535. v->blk_mv_type[s->block_index[3]] = 1;
  3536. break;
  3537. case MV_PMODE_INTFR_1MV:
  3538. v->blk_mv_type[s->block_index[0]] = 0;
  3539. v->blk_mv_type[s->block_index[1]] = 0;
  3540. v->blk_mv_type[s->block_index[2]] = 0;
  3541. v->blk_mv_type[s->block_index[3]] = 0;
  3542. break;
  3543. }
  3544. if (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_INTRA) { // intra MB
  3545. for (i = 0; i < 4; i++) {
  3546. s->current_picture.motion_val[1][s->block_index[i]][0] = 0;
  3547. s->current_picture.motion_val[1][s->block_index[i]][1] = 0;
  3548. }
  3549. s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
  3550. s->mb_intra = v->is_intra[s->mb_x] = 1;
  3551. for (i = 0; i < 6; i++)
  3552. v->mb_type[0][s->block_index[i]] = 1;
  3553. fieldtx = v->fieldtx_plane[mb_pos] = get_bits1(gb);
  3554. mb_has_coeffs = get_bits1(gb);
  3555. if (mb_has_coeffs)
  3556. cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
  3557. v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
  3558. GET_MQUANT();
  3559. s->current_picture.qscale_table[mb_pos] = mquant;
  3560. /* Set DC scale - y and c use the same (not sure if necessary here) */
  3561. s->y_dc_scale = s->y_dc_scale_table[mquant];
  3562. s->c_dc_scale = s->c_dc_scale_table[mquant];
  3563. dst_idx = 0;
  3564. for (i = 0; i < 6; i++) {
  3565. s->dc_val[0][s->block_index[i]] = 0;
  3566. dst_idx += i >> 2;
  3567. val = ((cbp >> (5 - i)) & 1);
  3568. v->mb_type[0][s->block_index[i]] = s->mb_intra;
  3569. v->a_avail = v->c_avail = 0;
  3570. if (i == 2 || i == 3 || !s->first_slice_line)
  3571. v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
  3572. if (i == 1 || i == 3 || s->mb_x)
  3573. v->c_avail = v->mb_type[0][s->block_index[i] - 1];
  3574. vc1_decode_intra_block(v, s->block[i], i, val, mquant,
  3575. (i & 4) ? v->codingset2 : v->codingset);
  3576. if ((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue;
  3577. v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
  3578. if (i < 4) {
  3579. stride_y = s->linesize << fieldtx;
  3580. off = (fieldtx) ? ((i & 1) * 8) + ((i & 2) >> 1) * s->linesize : (i & 1) * 8 + 4 * (i & 2) * s->linesize;
  3581. } else {
  3582. stride_y = s->uvlinesize;
  3583. off = 0;
  3584. }
  3585. s->idsp.put_signed_pixels_clamped(s->block[i],
  3586. s->dest[dst_idx] + off,
  3587. stride_y);
  3588. //TODO: loop filter
  3589. }
  3590. } else { // inter MB
  3591. mb_has_coeffs = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][3];
  3592. if (mb_has_coeffs)
  3593. cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
  3594. if (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_2MV_FIELD) {
  3595. v->twomvbp = get_vlc2(gb, v->twomvbp_vlc->table, VC1_2MV_BLOCK_PATTERN_VLC_BITS, 1);
  3596. } else {
  3597. if ((ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_4MV)
  3598. || (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_4MV_FIELD)) {
  3599. v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
  3600. }
  3601. }
  3602. s->mb_intra = v->is_intra[s->mb_x] = 0;
  3603. for (i = 0; i < 6; i++)
  3604. v->mb_type[0][s->block_index[i]] = 0;
  3605. fieldtx = v->fieldtx_plane[mb_pos] = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][1];
  3606. /* for all motion vector read MVDATA and motion compensate each block */
  3607. dst_idx = 0;
  3608. if (fourmv) {
  3609. mvbp = v->fourmvbp;
  3610. for (i = 0; i < 6; i++) {
  3611. if (i < 4) {
  3612. dmv_x = dmv_y = 0;
  3613. val = ((mvbp >> (3 - i)) & 1);
  3614. if (val) {
  3615. get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
  3616. }
  3617. vc1_pred_mv_intfr(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], 0);
  3618. vc1_mc_4mv_luma(v, i, 0, 0);
  3619. } else if (i == 4) {
  3620. vc1_mc_4mv_chroma4(v, 0, 0, 0);
  3621. }
  3622. }
  3623. } else if (twomv) {
  3624. mvbp = v->twomvbp;
  3625. dmv_x = dmv_y = 0;
  3626. if (mvbp & 2) {
  3627. get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
  3628. }
  3629. vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], 0);
  3630. vc1_mc_4mv_luma(v, 0, 0, 0);
  3631. vc1_mc_4mv_luma(v, 1, 0, 0);
  3632. dmv_x = dmv_y = 0;
  3633. if (mvbp & 1) {
  3634. get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
  3635. }
  3636. vc1_pred_mv_intfr(v, 2, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], 0);
  3637. vc1_mc_4mv_luma(v, 2, 0, 0);
  3638. vc1_mc_4mv_luma(v, 3, 0, 0);
  3639. vc1_mc_4mv_chroma4(v, 0, 0, 0);
  3640. } else {
  3641. mvbp = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][2];
  3642. dmv_x = dmv_y = 0;
  3643. if (mvbp) {
  3644. get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
  3645. }
  3646. vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 0);
  3647. vc1_mc_1mv(v, 0);
  3648. }
  3649. if (cbp)
  3650. GET_MQUANT(); // p. 227
  3651. s->current_picture.qscale_table[mb_pos] = mquant;
  3652. if (!v->ttmbf && cbp)
  3653. ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
  3654. for (i = 0; i < 6; i++) {
  3655. s->dc_val[0][s->block_index[i]] = 0;
  3656. dst_idx += i >> 2;
  3657. val = ((cbp >> (5 - i)) & 1);
  3658. if (!fieldtx)
  3659. off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
  3660. else
  3661. off = (i & 4) ? 0 : ((i & 1) * 8 + ((i > 1) * s->linesize));
  3662. if (val) {
  3663. pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
  3664. first_block, s->dest[dst_idx] + off,
  3665. (i & 4) ? s->uvlinesize : (s->linesize << fieldtx),
  3666. (i & 4) && (s->flags & CODEC_FLAG_GRAY), &block_tt);
  3667. block_cbp |= pat << (i << 2);
  3668. if (!v->ttmbf && ttmb < 8)
  3669. ttmb = -1;
  3670. first_block = 0;
  3671. }
  3672. }
  3673. }
  3674. } else { // skipped
  3675. s->mb_intra = v->is_intra[s->mb_x] = 0;
  3676. for (i = 0; i < 6; i++) {
  3677. v->mb_type[0][s->block_index[i]] = 0;
  3678. s->dc_val[0][s->block_index[i]] = 0;
  3679. }
  3680. s->current_picture.mb_type[mb_pos] = MB_TYPE_SKIP;
  3681. s->current_picture.qscale_table[mb_pos] = 0;
  3682. v->blk_mv_type[s->block_index[0]] = 0;
  3683. v->blk_mv_type[s->block_index[1]] = 0;
  3684. v->blk_mv_type[s->block_index[2]] = 0;
  3685. v->blk_mv_type[s->block_index[3]] = 0;
  3686. vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 0);
  3687. vc1_mc_1mv(v, 0);
  3688. }
  3689. if (s->mb_x == s->mb_width - 1)
  3690. memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0])*s->mb_stride);
  3691. return 0;
  3692. }
  3693. static int vc1_decode_p_mb_intfi(VC1Context *v)
  3694. {
  3695. MpegEncContext *s = &v->s;
  3696. GetBitContext *gb = &s->gb;
  3697. int i;
  3698. int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
  3699. int cbp = 0; /* cbp decoding stuff */
  3700. int mqdiff, mquant; /* MB quantization */
  3701. int ttmb = v->ttfrm; /* MB Transform type */
  3702. int mb_has_coeffs = 1; /* last_flag */
  3703. int dmv_x, dmv_y; /* Differential MV components */
  3704. int val; /* temp values */
  3705. int first_block = 1;
  3706. int dst_idx, off;
  3707. int pred_flag = 0;
  3708. int block_cbp = 0, pat, block_tt = 0;
  3709. int idx_mbmode = 0;
  3710. mquant = v->pq; /* Lossy initialization */
  3711. idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_IF_MBMODE_VLC_BITS, 2);
  3712. if (idx_mbmode <= 1) { // intra MB
  3713. s->mb_intra = v->is_intra[s->mb_x] = 1;
  3714. s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
  3715. s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
  3716. s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
  3717. GET_MQUANT();
  3718. s->current_picture.qscale_table[mb_pos] = mquant;
  3719. /* Set DC scale - y and c use the same (not sure if necessary here) */
  3720. s->y_dc_scale = s->y_dc_scale_table[mquant];
  3721. s->c_dc_scale = s->c_dc_scale_table[mquant];
  3722. v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
  3723. mb_has_coeffs = idx_mbmode & 1;
  3724. if (mb_has_coeffs)
  3725. cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_ICBPCY_VLC_BITS, 2);
  3726. dst_idx = 0;
  3727. for (i = 0; i < 6; i++) {
  3728. s->dc_val[0][s->block_index[i]] = 0;
  3729. v->mb_type[0][s->block_index[i]] = 1;
  3730. dst_idx += i >> 2;
  3731. val = ((cbp >> (5 - i)) & 1);
  3732. v->a_avail = v->c_avail = 0;
  3733. if (i == 2 || i == 3 || !s->first_slice_line)
  3734. v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
  3735. if (i == 1 || i == 3 || s->mb_x)
  3736. v->c_avail = v->mb_type[0][s->block_index[i] - 1];
  3737. vc1_decode_intra_block(v, s->block[i], i, val, mquant,
  3738. (i & 4) ? v->codingset2 : v->codingset);
  3739. if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
  3740. continue;
  3741. v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
  3742. off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
  3743. s->idsp.put_signed_pixels_clamped(s->block[i],
  3744. s->dest[dst_idx] + off,
  3745. (i & 4) ? s->uvlinesize
  3746. : s->linesize);
  3747. // TODO: loop filter
  3748. }
  3749. } else {
  3750. s->mb_intra = v->is_intra[s->mb_x] = 0;
  3751. s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_16x16;
  3752. for (i = 0; i < 6; i++) v->mb_type[0][s->block_index[i]] = 0;
  3753. if (idx_mbmode <= 5) { // 1-MV
  3754. dmv_x = dmv_y = pred_flag = 0;
  3755. if (idx_mbmode & 1) {
  3756. get_mvdata_interlaced(v, &dmv_x, &dmv_y, &pred_flag);
  3757. }
  3758. vc1_pred_mv(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], pred_flag, 0);
  3759. vc1_mc_1mv(v, 0);
  3760. mb_has_coeffs = !(idx_mbmode & 2);
  3761. } else { // 4-MV
  3762. v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
  3763. for (i = 0; i < 6; i++) {
  3764. if (i < 4) {
  3765. dmv_x = dmv_y = pred_flag = 0;
  3766. val = ((v->fourmvbp >> (3 - i)) & 1);
  3767. if (val) {
  3768. get_mvdata_interlaced(v, &dmv_x, &dmv_y, &pred_flag);
  3769. }
  3770. vc1_pred_mv(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], pred_flag, 0);
  3771. vc1_mc_4mv_luma(v, i, 0, 0);
  3772. } else if (i == 4)
  3773. vc1_mc_4mv_chroma(v, 0);
  3774. }
  3775. mb_has_coeffs = idx_mbmode & 1;
  3776. }
  3777. if (mb_has_coeffs)
  3778. cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
  3779. if (cbp) {
  3780. GET_MQUANT();
  3781. }
  3782. s->current_picture.qscale_table[mb_pos] = mquant;
  3783. if (!v->ttmbf && cbp) {
  3784. ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
  3785. }
  3786. dst_idx = 0;
  3787. for (i = 0; i < 6; i++) {
  3788. s->dc_val[0][s->block_index[i]] = 0;
  3789. dst_idx += i >> 2;
  3790. val = ((cbp >> (5 - i)) & 1);
  3791. off = (i & 4) ? 0 : (i & 1) * 8 + (i & 2) * 4 * s->linesize;
  3792. if (val) {
  3793. pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
  3794. first_block, s->dest[dst_idx] + off,
  3795. (i & 4) ? s->uvlinesize : s->linesize,
  3796. (i & 4) && (s->flags & CODEC_FLAG_GRAY),
  3797. &block_tt);
  3798. block_cbp |= pat << (i << 2);
  3799. if (!v->ttmbf && ttmb < 8) ttmb = -1;
  3800. first_block = 0;
  3801. }
  3802. }
  3803. }
  3804. if (s->mb_x == s->mb_width - 1)
  3805. memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0]) * s->mb_stride);
  3806. return 0;
  3807. }
  3808. /** Decode one B-frame MB (in Main profile)
  3809. */
  3810. static void vc1_decode_b_mb(VC1Context *v)
  3811. {
  3812. MpegEncContext *s = &v->s;
  3813. GetBitContext *gb = &s->gb;
  3814. int i, j;
  3815. int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
  3816. int cbp = 0; /* cbp decoding stuff */
  3817. int mqdiff, mquant; /* MB quantization */
  3818. int ttmb = v->ttfrm; /* MB Transform type */
  3819. int mb_has_coeffs = 0; /* last_flag */
  3820. int index, index1; /* LUT indexes */
  3821. int val, sign; /* temp values */
  3822. int first_block = 1;
  3823. int dst_idx, off;
  3824. int skipped, direct;
  3825. int dmv_x[2], dmv_y[2];
  3826. int bmvtype = BMV_TYPE_BACKWARD;
  3827. mquant = v->pq; /* lossy initialization */
  3828. s->mb_intra = 0;
  3829. if (v->dmb_is_raw)
  3830. direct = get_bits1(gb);
  3831. else
  3832. direct = v->direct_mb_plane[mb_pos];
  3833. if (v->skip_is_raw)
  3834. skipped = get_bits1(gb);
  3835. else
  3836. skipped = v->s.mbskip_table[mb_pos];
  3837. dmv_x[0] = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0;
  3838. for (i = 0; i < 6; i++) {
  3839. v->mb_type[0][s->block_index[i]] = 0;
  3840. s->dc_val[0][s->block_index[i]] = 0;
  3841. }
  3842. s->current_picture.qscale_table[mb_pos] = 0;
  3843. if (!direct) {
  3844. if (!skipped) {
  3845. GET_MVDATA(dmv_x[0], dmv_y[0]);
  3846. dmv_x[1] = dmv_x[0];
  3847. dmv_y[1] = dmv_y[0];
  3848. }
  3849. if (skipped || !s->mb_intra) {
  3850. bmvtype = decode012(gb);
  3851. switch (bmvtype) {
  3852. case 0:
  3853. bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_BACKWARD : BMV_TYPE_FORWARD;
  3854. break;
  3855. case 1:
  3856. bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_FORWARD : BMV_TYPE_BACKWARD;
  3857. break;
  3858. case 2:
  3859. bmvtype = BMV_TYPE_INTERPOLATED;
  3860. dmv_x[0] = dmv_y[0] = 0;
  3861. }
  3862. }
  3863. }
  3864. for (i = 0; i < 6; i++)
  3865. v->mb_type[0][s->block_index[i]] = s->mb_intra;
  3866. if (skipped) {
  3867. if (direct)
  3868. bmvtype = BMV_TYPE_INTERPOLATED;
  3869. vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
  3870. vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
  3871. return;
  3872. }
  3873. if (direct) {
  3874. cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
  3875. GET_MQUANT();
  3876. s->mb_intra = 0;
  3877. s->current_picture.qscale_table[mb_pos] = mquant;
  3878. if (!v->ttmbf)
  3879. ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
  3880. dmv_x[0] = dmv_y[0] = dmv_x[1] = dmv_y[1] = 0;
  3881. vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
  3882. vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
  3883. } else {
  3884. if (!mb_has_coeffs && !s->mb_intra) {
  3885. /* no coded blocks - effectively skipped */
  3886. vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
  3887. vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
  3888. return;
  3889. }
  3890. if (s->mb_intra && !mb_has_coeffs) {
  3891. GET_MQUANT();
  3892. s->current_picture.qscale_table[mb_pos] = mquant;
  3893. s->ac_pred = get_bits1(gb);
  3894. cbp = 0;
  3895. vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
  3896. } else {
  3897. if (bmvtype == BMV_TYPE_INTERPOLATED) {
  3898. GET_MVDATA(dmv_x[0], dmv_y[0]);
  3899. if (!mb_has_coeffs) {
  3900. /* interpolated skipped block */
  3901. vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
  3902. vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
  3903. return;
  3904. }
  3905. }
  3906. vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
  3907. if (!s->mb_intra) {
  3908. vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
  3909. }
  3910. if (s->mb_intra)
  3911. s->ac_pred = get_bits1(gb);
  3912. cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
  3913. GET_MQUANT();
  3914. s->current_picture.qscale_table[mb_pos] = mquant;
  3915. if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
  3916. ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
  3917. }
  3918. }
  3919. dst_idx = 0;
  3920. for (i = 0; i < 6; i++) {
  3921. s->dc_val[0][s->block_index[i]] = 0;
  3922. dst_idx += i >> 2;
  3923. val = ((cbp >> (5 - i)) & 1);
  3924. off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
  3925. v->mb_type[0][s->block_index[i]] = s->mb_intra;
  3926. if (s->mb_intra) {
  3927. /* check if prediction blocks A and C are available */
  3928. v->a_avail = v->c_avail = 0;
  3929. if (i == 2 || i == 3 || !s->first_slice_line)
  3930. v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
  3931. if (i == 1 || i == 3 || s->mb_x)
  3932. v->c_avail = v->mb_type[0][s->block_index[i] - 1];
  3933. vc1_decode_intra_block(v, s->block[i], i, val, mquant,
  3934. (i & 4) ? v->codingset2 : v->codingset);
  3935. if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
  3936. continue;
  3937. v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
  3938. if (v->rangeredfrm)
  3939. for (j = 0; j < 64; j++)
  3940. s->block[i][j] <<= 1;
  3941. s->idsp.put_signed_pixels_clamped(s->block[i],
  3942. s->dest[dst_idx] + off,
  3943. i & 4 ? s->uvlinesize
  3944. : s->linesize);
  3945. } else if (val) {
  3946. vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
  3947. first_block, s->dest[dst_idx] + off,
  3948. (i & 4) ? s->uvlinesize : s->linesize,
  3949. (i & 4) && (s->flags & CODEC_FLAG_GRAY), NULL);
  3950. if (!v->ttmbf && ttmb < 8)
  3951. ttmb = -1;
  3952. first_block = 0;
  3953. }
  3954. }
  3955. }
  3956. /** Decode one B-frame MB (in interlaced field B picture)
  3957. */
  3958. static void vc1_decode_b_mb_intfi(VC1Context *v)
  3959. {
  3960. MpegEncContext *s = &v->s;
  3961. GetBitContext *gb = &s->gb;
  3962. int i, j;
  3963. int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
  3964. int cbp = 0; /* cbp decoding stuff */
  3965. int mqdiff, mquant; /* MB quantization */
  3966. int ttmb = v->ttfrm; /* MB Transform type */
  3967. int mb_has_coeffs = 0; /* last_flag */
  3968. int val; /* temp value */
  3969. int first_block = 1;
  3970. int dst_idx, off;
  3971. int fwd;
  3972. int dmv_x[2], dmv_y[2], pred_flag[2];
  3973. int bmvtype = BMV_TYPE_BACKWARD;
  3974. int idx_mbmode;
  3975. mquant = v->pq; /* Lossy initialization */
  3976. s->mb_intra = 0;
  3977. idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_IF_MBMODE_VLC_BITS, 2);
  3978. if (idx_mbmode <= 1) { // intra MB
  3979. s->mb_intra = v->is_intra[s->mb_x] = 1;
  3980. s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
  3981. s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
  3982. s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
  3983. GET_MQUANT();
  3984. s->current_picture.qscale_table[mb_pos] = mquant;
  3985. /* Set DC scale - y and c use the same (not sure if necessary here) */
  3986. s->y_dc_scale = s->y_dc_scale_table[mquant];
  3987. s->c_dc_scale = s->c_dc_scale_table[mquant];
  3988. v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
  3989. mb_has_coeffs = idx_mbmode & 1;
  3990. if (mb_has_coeffs)
  3991. cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_ICBPCY_VLC_BITS, 2);
  3992. dst_idx = 0;
  3993. for (i = 0; i < 6; i++) {
  3994. s->dc_val[0][s->block_index[i]] = 0;
  3995. dst_idx += i >> 2;
  3996. val = ((cbp >> (5 - i)) & 1);
  3997. v->mb_type[0][s->block_index[i]] = s->mb_intra;
  3998. v->a_avail = v->c_avail = 0;
  3999. if (i == 2 || i == 3 || !s->first_slice_line)
  4000. v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
  4001. if (i == 1 || i == 3 || s->mb_x)
  4002. v->c_avail = v->mb_type[0][s->block_index[i] - 1];
  4003. vc1_decode_intra_block(v, s->block[i], i, val, mquant,
  4004. (i & 4) ? v->codingset2 : v->codingset);
  4005. if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
  4006. continue;
  4007. v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
  4008. if (v->rangeredfrm)
  4009. for (j = 0; j < 64; j++)
  4010. s->block[i][j] <<= 1;
  4011. off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
  4012. s->idsp.put_signed_pixels_clamped(s->block[i],
  4013. s->dest[dst_idx] + off,
  4014. (i & 4) ? s->uvlinesize
  4015. : s->linesize);
  4016. // TODO: yet to perform loop filter
  4017. }
  4018. } else {
  4019. s->mb_intra = v->is_intra[s->mb_x] = 0;
  4020. s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_16x16;
  4021. for (i = 0; i < 6; i++) v->mb_type[0][s->block_index[i]] = 0;
  4022. if (v->fmb_is_raw)
  4023. fwd = v->forward_mb_plane[mb_pos] = get_bits1(gb);
  4024. else
  4025. fwd = v->forward_mb_plane[mb_pos];
  4026. if (idx_mbmode <= 5) { // 1-MV
  4027. int interpmvp = 0;
  4028. dmv_x[0] = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0;
  4029. pred_flag[0] = pred_flag[1] = 0;
  4030. if (fwd)
  4031. bmvtype = BMV_TYPE_FORWARD;
  4032. else {
  4033. bmvtype = decode012(gb);
  4034. switch (bmvtype) {
  4035. case 0:
  4036. bmvtype = BMV_TYPE_BACKWARD;
  4037. break;
  4038. case 1:
  4039. bmvtype = BMV_TYPE_DIRECT;
  4040. break;
  4041. case 2:
  4042. bmvtype = BMV_TYPE_INTERPOLATED;
  4043. interpmvp = get_bits1(gb);
  4044. }
  4045. }
  4046. v->bmvtype = bmvtype;
  4047. if (bmvtype != BMV_TYPE_DIRECT && idx_mbmode & 1) {
  4048. get_mvdata_interlaced(v, &dmv_x[bmvtype == BMV_TYPE_BACKWARD], &dmv_y[bmvtype == BMV_TYPE_BACKWARD], &pred_flag[bmvtype == BMV_TYPE_BACKWARD]);
  4049. }
  4050. if (interpmvp) {
  4051. get_mvdata_interlaced(v, &dmv_x[1], &dmv_y[1], &pred_flag[1]);
  4052. }
  4053. if (bmvtype == BMV_TYPE_DIRECT) {
  4054. dmv_x[0] = dmv_y[0] = pred_flag[0] = 0;
  4055. dmv_x[1] = dmv_y[1] = pred_flag[0] = 0;
  4056. if (!s->next_picture_ptr->field_picture) {
  4057. av_log(s->avctx, AV_LOG_ERROR, "Mixed field/frame direct mode not supported\n");
  4058. return;
  4059. }
  4060. }
  4061. vc1_pred_b_mv_intfi(v, 0, dmv_x, dmv_y, 1, pred_flag);
  4062. vc1_b_mc(v, dmv_x, dmv_y, (bmvtype == BMV_TYPE_DIRECT), bmvtype);
  4063. mb_has_coeffs = !(idx_mbmode & 2);
  4064. } else { // 4-MV
  4065. if (fwd)
  4066. bmvtype = BMV_TYPE_FORWARD;
  4067. v->bmvtype = bmvtype;
  4068. v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
  4069. for (i = 0; i < 6; i++) {
  4070. if (i < 4) {
  4071. dmv_x[0] = dmv_y[0] = pred_flag[0] = 0;
  4072. dmv_x[1] = dmv_y[1] = pred_flag[1] = 0;
  4073. val = ((v->fourmvbp >> (3 - i)) & 1);
  4074. if (val) {
  4075. get_mvdata_interlaced(v, &dmv_x[bmvtype == BMV_TYPE_BACKWARD],
  4076. &dmv_y[bmvtype == BMV_TYPE_BACKWARD],
  4077. &pred_flag[bmvtype == BMV_TYPE_BACKWARD]);
  4078. }
  4079. vc1_pred_b_mv_intfi(v, i, dmv_x, dmv_y, 0, pred_flag);
  4080. vc1_mc_4mv_luma(v, i, bmvtype == BMV_TYPE_BACKWARD, 0);
  4081. } else if (i == 4)
  4082. vc1_mc_4mv_chroma(v, bmvtype == BMV_TYPE_BACKWARD);
  4083. }
  4084. mb_has_coeffs = idx_mbmode & 1;
  4085. }
  4086. if (mb_has_coeffs)
  4087. cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
  4088. if (cbp) {
  4089. GET_MQUANT();
  4090. }
  4091. s->current_picture.qscale_table[mb_pos] = mquant;
  4092. if (!v->ttmbf && cbp) {
  4093. ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
  4094. }
  4095. dst_idx = 0;
  4096. for (i = 0; i < 6; i++) {
  4097. s->dc_val[0][s->block_index[i]] = 0;
  4098. dst_idx += i >> 2;
  4099. val = ((cbp >> (5 - i)) & 1);
  4100. off = (i & 4) ? 0 : (i & 1) * 8 + (i & 2) * 4 * s->linesize;
  4101. if (val) {
  4102. vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
  4103. first_block, s->dest[dst_idx] + off,
  4104. (i & 4) ? s->uvlinesize : s->linesize,
  4105. (i & 4) && (s->flags & CODEC_FLAG_GRAY), NULL);
  4106. if (!v->ttmbf && ttmb < 8)
  4107. ttmb = -1;
  4108. first_block = 0;
  4109. }
  4110. }
  4111. }
  4112. }
  4113. /** Decode one B-frame MB (in interlaced frame B picture)
  4114. */
  4115. static int vc1_decode_b_mb_intfr(VC1Context *v)
  4116. {
  4117. MpegEncContext *s = &v->s;
  4118. GetBitContext *gb = &s->gb;
  4119. int i, j;
  4120. int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
  4121. int cbp = 0; /* cbp decoding stuff */
  4122. int mqdiff, mquant; /* MB quantization */
  4123. int ttmb = v->ttfrm; /* MB Transform type */
  4124. int mvsw = 0; /* motion vector switch */
  4125. int mb_has_coeffs = 1; /* last_flag */
  4126. int dmv_x, dmv_y; /* Differential MV components */
  4127. int val; /* temp value */
  4128. int first_block = 1;
  4129. int dst_idx, off;
  4130. int skipped, direct, twomv = 0;
  4131. int block_cbp = 0, pat, block_tt = 0;
  4132. int idx_mbmode = 0, mvbp;
  4133. int stride_y, fieldtx;
  4134. int bmvtype = BMV_TYPE_BACKWARD;
  4135. int dir, dir2;
  4136. mquant = v->pq; /* Lossy initialization */
  4137. s->mb_intra = 0;
  4138. if (v->skip_is_raw)
  4139. skipped = get_bits1(gb);
  4140. else
  4141. skipped = v->s.mbskip_table[mb_pos];
  4142. if (!skipped) {
  4143. idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_NON4MV_MBMODE_VLC_BITS, 2);
  4144. if (ff_vc1_mbmode_intfrp[0][idx_mbmode][0] == MV_PMODE_INTFR_2MV_FIELD) {
  4145. twomv = 1;
  4146. v->blk_mv_type[s->block_index[0]] = 1;
  4147. v->blk_mv_type[s->block_index[1]] = 1;
  4148. v->blk_mv_type[s->block_index[2]] = 1;
  4149. v->blk_mv_type[s->block_index[3]] = 1;
  4150. } else {
  4151. v->blk_mv_type[s->block_index[0]] = 0;
  4152. v->blk_mv_type[s->block_index[1]] = 0;
  4153. v->blk_mv_type[s->block_index[2]] = 0;
  4154. v->blk_mv_type[s->block_index[3]] = 0;
  4155. }
  4156. }
  4157. if (v->dmb_is_raw)
  4158. direct = get_bits1(gb);
  4159. else
  4160. direct = v->direct_mb_plane[mb_pos];
  4161. if (direct) {
  4162. if (s->next_picture_ptr->field_picture)
  4163. av_log(s->avctx, AV_LOG_WARNING, "Mixed frame/field direct mode not supported\n");
  4164. s->mv[0][0][0] = s->current_picture.motion_val[0][s->block_index[0]][0] = scale_mv(s->next_picture.motion_val[1][s->block_index[0]][0], v->bfraction, 0, s->quarter_sample);
  4165. s->mv[0][0][1] = s->current_picture.motion_val[0][s->block_index[0]][1] = scale_mv(s->next_picture.motion_val[1][s->block_index[0]][1], v->bfraction, 0, s->quarter_sample);
  4166. s->mv[1][0][0] = s->current_picture.motion_val[1][s->block_index[0]][0] = scale_mv(s->next_picture.motion_val[1][s->block_index[0]][0], v->bfraction, 1, s->quarter_sample);
  4167. s->mv[1][0][1] = s->current_picture.motion_val[1][s->block_index[0]][1] = scale_mv(s->next_picture.motion_val[1][s->block_index[0]][1], v->bfraction, 1, s->quarter_sample);
  4168. if (twomv) {
  4169. s->mv[0][2][0] = s->current_picture.motion_val[0][s->block_index[2]][0] = scale_mv(s->next_picture.motion_val[1][s->block_index[2]][0], v->bfraction, 0, s->quarter_sample);
  4170. s->mv[0][2][1] = s->current_picture.motion_val[0][s->block_index[2]][1] = scale_mv(s->next_picture.motion_val[1][s->block_index[2]][1], v->bfraction, 0, s->quarter_sample);
  4171. s->mv[1][2][0] = s->current_picture.motion_val[1][s->block_index[2]][0] = scale_mv(s->next_picture.motion_val[1][s->block_index[2]][0], v->bfraction, 1, s->quarter_sample);
  4172. s->mv[1][2][1] = s->current_picture.motion_val[1][s->block_index[2]][1] = scale_mv(s->next_picture.motion_val[1][s->block_index[2]][1], v->bfraction, 1, s->quarter_sample);
  4173. for (i = 1; i < 4; i += 2) {
  4174. s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0] = s->mv[0][i-1][0];
  4175. s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1] = s->mv[0][i-1][1];
  4176. s->mv[1][i][0] = s->current_picture.motion_val[1][s->block_index[i]][0] = s->mv[1][i-1][0];
  4177. s->mv[1][i][1] = s->current_picture.motion_val[1][s->block_index[i]][1] = s->mv[1][i-1][1];
  4178. }
  4179. } else {
  4180. for (i = 1; i < 4; i++) {
  4181. s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0] = s->mv[0][0][0];
  4182. s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1] = s->mv[0][0][1];
  4183. s->mv[1][i][0] = s->current_picture.motion_val[1][s->block_index[i]][0] = s->mv[1][0][0];
  4184. s->mv[1][i][1] = s->current_picture.motion_val[1][s->block_index[i]][1] = s->mv[1][0][1];
  4185. }
  4186. }
  4187. }
  4188. if (ff_vc1_mbmode_intfrp[0][idx_mbmode][0] == MV_PMODE_INTFR_INTRA) { // intra MB
  4189. for (i = 0; i < 4; i++) {
  4190. s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0] = 0;
  4191. s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1] = 0;
  4192. s->mv[1][i][0] = s->current_picture.motion_val[1][s->block_index[i]][0] = 0;
  4193. s->mv[1][i][1] = s->current_picture.motion_val[1][s->block_index[i]][1] = 0;
  4194. }
  4195. s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
  4196. s->mb_intra = v->is_intra[s->mb_x] = 1;
  4197. for (i = 0; i < 6; i++)
  4198. v->mb_type[0][s->block_index[i]] = 1;
  4199. fieldtx = v->fieldtx_plane[mb_pos] = get_bits1(gb);
  4200. mb_has_coeffs = get_bits1(gb);
  4201. if (mb_has_coeffs)
  4202. cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
  4203. v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
  4204. GET_MQUANT();
  4205. s->current_picture.qscale_table[mb_pos] = mquant;
  4206. /* Set DC scale - y and c use the same (not sure if necessary here) */
  4207. s->y_dc_scale = s->y_dc_scale_table[mquant];
  4208. s->c_dc_scale = s->c_dc_scale_table[mquant];
  4209. dst_idx = 0;
  4210. for (i = 0; i < 6; i++) {
  4211. s->dc_val[0][s->block_index[i]] = 0;
  4212. dst_idx += i >> 2;
  4213. val = ((cbp >> (5 - i)) & 1);
  4214. v->mb_type[0][s->block_index[i]] = s->mb_intra;
  4215. v->a_avail = v->c_avail = 0;
  4216. if (i == 2 || i == 3 || !s->first_slice_line)
  4217. v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
  4218. if (i == 1 || i == 3 || s->mb_x)
  4219. v->c_avail = v->mb_type[0][s->block_index[i] - 1];
  4220. vc1_decode_intra_block(v, s->block[i], i, val, mquant,
  4221. (i & 4) ? v->codingset2 : v->codingset);
  4222. if (i > 3 && (s->flags & CODEC_FLAG_GRAY))
  4223. continue;
  4224. v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
  4225. if (i < 4) {
  4226. stride_y = s->linesize << fieldtx;
  4227. off = (fieldtx) ? ((i & 1) * 8) + ((i & 2) >> 1) * s->linesize : (i & 1) * 8 + 4 * (i & 2) * s->linesize;
  4228. } else {
  4229. stride_y = s->uvlinesize;
  4230. off = 0;
  4231. }
  4232. s->idsp.put_signed_pixels_clamped(s->block[i],
  4233. s->dest[dst_idx] + off,
  4234. stride_y);
  4235. }
  4236. } else {
  4237. s->mb_intra = v->is_intra[s->mb_x] = 0;
  4238. if (!direct) {
  4239. if (skipped || !s->mb_intra) {
  4240. bmvtype = decode012(gb);
  4241. switch (bmvtype) {
  4242. case 0:
  4243. bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_BACKWARD : BMV_TYPE_FORWARD;
  4244. break;
  4245. case 1:
  4246. bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_FORWARD : BMV_TYPE_BACKWARD;
  4247. break;
  4248. case 2:
  4249. bmvtype = BMV_TYPE_INTERPOLATED;
  4250. }
  4251. }
  4252. if (twomv && bmvtype != BMV_TYPE_INTERPOLATED)
  4253. mvsw = get_bits1(gb);
  4254. }
  4255. if (!skipped) { // inter MB
  4256. mb_has_coeffs = ff_vc1_mbmode_intfrp[0][idx_mbmode][3];
  4257. if (mb_has_coeffs)
  4258. cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
  4259. if (!direct) {
  4260. if (bmvtype == BMV_TYPE_INTERPOLATED && twomv) {
  4261. v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
  4262. } else if (bmvtype == BMV_TYPE_INTERPOLATED || twomv) {
  4263. v->twomvbp = get_vlc2(gb, v->twomvbp_vlc->table, VC1_2MV_BLOCK_PATTERN_VLC_BITS, 1);
  4264. }
  4265. }
  4266. for (i = 0; i < 6; i++)
  4267. v->mb_type[0][s->block_index[i]] = 0;
  4268. fieldtx = v->fieldtx_plane[mb_pos] = ff_vc1_mbmode_intfrp[0][idx_mbmode][1];
  4269. /* for all motion vector read MVDATA and motion compensate each block */
  4270. dst_idx = 0;
  4271. if (direct) {
  4272. if (twomv) {
  4273. for (i = 0; i < 4; i++) {
  4274. vc1_mc_4mv_luma(v, i, 0, 0);
  4275. vc1_mc_4mv_luma(v, i, 1, 1);
  4276. }
  4277. vc1_mc_4mv_chroma4(v, 0, 0, 0);
  4278. vc1_mc_4mv_chroma4(v, 1, 1, 1);
  4279. } else {
  4280. vc1_mc_1mv(v, 0);
  4281. vc1_interp_mc(v);
  4282. }
  4283. } else if (twomv && bmvtype == BMV_TYPE_INTERPOLATED) {
  4284. mvbp = v->fourmvbp;
  4285. for (i = 0; i < 4; i++) {
  4286. dir = i==1 || i==3;
  4287. dmv_x = dmv_y = 0;
  4288. val = ((mvbp >> (3 - i)) & 1);
  4289. if (val)
  4290. get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
  4291. j = i > 1 ? 2 : 0;
  4292. vc1_pred_mv_intfr(v, j, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], dir);
  4293. vc1_mc_4mv_luma(v, j, dir, dir);
  4294. vc1_mc_4mv_luma(v, j+1, dir, dir);
  4295. }
  4296. vc1_mc_4mv_chroma4(v, 0, 0, 0);
  4297. vc1_mc_4mv_chroma4(v, 1, 1, 1);
  4298. } else if (bmvtype == BMV_TYPE_INTERPOLATED) {
  4299. mvbp = v->twomvbp;
  4300. dmv_x = dmv_y = 0;
  4301. if (mvbp & 2)
  4302. get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
  4303. vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 0);
  4304. vc1_mc_1mv(v, 0);
  4305. dmv_x = dmv_y = 0;
  4306. if (mvbp & 1)
  4307. get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
  4308. vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 1);
  4309. vc1_interp_mc(v);
  4310. } else if (twomv) {
  4311. dir = bmvtype == BMV_TYPE_BACKWARD;
  4312. dir2 = dir;
  4313. if (mvsw)
  4314. dir2 = !dir;
  4315. mvbp = v->twomvbp;
  4316. dmv_x = dmv_y = 0;
  4317. if (mvbp & 2)
  4318. get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
  4319. vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], dir);
  4320. dmv_x = dmv_y = 0;
  4321. if (mvbp & 1)
  4322. get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
  4323. vc1_pred_mv_intfr(v, 2, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], dir2);
  4324. if (mvsw) {
  4325. for (i = 0; i < 2; i++) {
  4326. s->mv[dir][i+2][0] = s->mv[dir][i][0] = s->current_picture.motion_val[dir][s->block_index[i+2]][0] = s->current_picture.motion_val[dir][s->block_index[i]][0];
  4327. s->mv[dir][i+2][1] = s->mv[dir][i][1] = s->current_picture.motion_val[dir][s->block_index[i+2]][1] = s->current_picture.motion_val[dir][s->block_index[i]][1];
  4328. s->mv[dir2][i+2][0] = s->mv[dir2][i][0] = s->current_picture.motion_val[dir2][s->block_index[i]][0] = s->current_picture.motion_val[dir2][s->block_index[i+2]][0];
  4329. s->mv[dir2][i+2][1] = s->mv[dir2][i][1] = s->current_picture.motion_val[dir2][s->block_index[i]][1] = s->current_picture.motion_val[dir2][s->block_index[i+2]][1];
  4330. }
  4331. } else {
  4332. vc1_pred_mv_intfr(v, 0, 0, 0, 2, v->range_x, v->range_y, v->mb_type[0], !dir);
  4333. vc1_pred_mv_intfr(v, 2, 0, 0, 2, v->range_x, v->range_y, v->mb_type[0], !dir);
  4334. }
  4335. vc1_mc_4mv_luma(v, 0, dir, 0);
  4336. vc1_mc_4mv_luma(v, 1, dir, 0);
  4337. vc1_mc_4mv_luma(v, 2, dir2, 0);
  4338. vc1_mc_4mv_luma(v, 3, dir2, 0);
  4339. vc1_mc_4mv_chroma4(v, dir, dir2, 0);
  4340. } else {
  4341. dir = bmvtype == BMV_TYPE_BACKWARD;
  4342. mvbp = ff_vc1_mbmode_intfrp[0][idx_mbmode][2];
  4343. dmv_x = dmv_y = 0;
  4344. if (mvbp)
  4345. get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
  4346. vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], dir);
  4347. v->blk_mv_type[s->block_index[0]] = 1;
  4348. v->blk_mv_type[s->block_index[1]] = 1;
  4349. v->blk_mv_type[s->block_index[2]] = 1;
  4350. v->blk_mv_type[s->block_index[3]] = 1;
  4351. vc1_pred_mv_intfr(v, 0, 0, 0, 2, v->range_x, v->range_y, 0, !dir);
  4352. for (i = 0; i < 2; i++) {
  4353. s->mv[!dir][i+2][0] = s->mv[!dir][i][0] = s->current_picture.motion_val[!dir][s->block_index[i+2]][0] = s->current_picture.motion_val[!dir][s->block_index[i]][0];
  4354. s->mv[!dir][i+2][1] = s->mv[!dir][i][1] = s->current_picture.motion_val[!dir][s->block_index[i+2]][1] = s->current_picture.motion_val[!dir][s->block_index[i]][1];
  4355. }
  4356. vc1_mc_1mv(v, dir);
  4357. }
  4358. if (cbp)
  4359. GET_MQUANT(); // p. 227
  4360. s->current_picture.qscale_table[mb_pos] = mquant;
  4361. if (!v->ttmbf && cbp)
  4362. ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
  4363. for (i = 0; i < 6; i++) {
  4364. s->dc_val[0][s->block_index[i]] = 0;
  4365. dst_idx += i >> 2;
  4366. val = ((cbp >> (5 - i)) & 1);
  4367. if (!fieldtx)
  4368. off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
  4369. else
  4370. off = (i & 4) ? 0 : ((i & 1) * 8 + ((i > 1) * s->linesize));
  4371. if (val) {
  4372. pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
  4373. first_block, s->dest[dst_idx] + off,
  4374. (i & 4) ? s->uvlinesize : (s->linesize << fieldtx),
  4375. (i & 4) && (s->flags & CODEC_FLAG_GRAY), &block_tt);
  4376. block_cbp |= pat << (i << 2);
  4377. if (!v->ttmbf && ttmb < 8)
  4378. ttmb = -1;
  4379. first_block = 0;
  4380. }
  4381. }
  4382. } else { // skipped
  4383. dir = 0;
  4384. for (i = 0; i < 6; i++) {
  4385. v->mb_type[0][s->block_index[i]] = 0;
  4386. s->dc_val[0][s->block_index[i]] = 0;
  4387. }
  4388. s->current_picture.mb_type[mb_pos] = MB_TYPE_SKIP;
  4389. s->current_picture.qscale_table[mb_pos] = 0;
  4390. v->blk_mv_type[s->block_index[0]] = 0;
  4391. v->blk_mv_type[s->block_index[1]] = 0;
  4392. v->blk_mv_type[s->block_index[2]] = 0;
  4393. v->blk_mv_type[s->block_index[3]] = 0;
  4394. if (!direct) {
  4395. if (bmvtype == BMV_TYPE_INTERPOLATED) {
  4396. vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 0);
  4397. vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 1);
  4398. } else {
  4399. dir = bmvtype == BMV_TYPE_BACKWARD;
  4400. vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], dir);
  4401. if (mvsw) {
  4402. int dir2 = dir;
  4403. if (mvsw)
  4404. dir2 = !dir;
  4405. for (i = 0; i < 2; i++) {
  4406. s->mv[dir][i+2][0] = s->mv[dir][i][0] = s->current_picture.motion_val[dir][s->block_index[i+2]][0] = s->current_picture.motion_val[dir][s->block_index[i]][0];
  4407. s->mv[dir][i+2][1] = s->mv[dir][i][1] = s->current_picture.motion_val[dir][s->block_index[i+2]][1] = s->current_picture.motion_val[dir][s->block_index[i]][1];
  4408. s->mv[dir2][i+2][0] = s->mv[dir2][i][0] = s->current_picture.motion_val[dir2][s->block_index[i]][0] = s->current_picture.motion_val[dir2][s->block_index[i+2]][0];
  4409. s->mv[dir2][i+2][1] = s->mv[dir2][i][1] = s->current_picture.motion_val[dir2][s->block_index[i]][1] = s->current_picture.motion_val[dir2][s->block_index[i+2]][1];
  4410. }
  4411. } else {
  4412. v->blk_mv_type[s->block_index[0]] = 1;
  4413. v->blk_mv_type[s->block_index[1]] = 1;
  4414. v->blk_mv_type[s->block_index[2]] = 1;
  4415. v->blk_mv_type[s->block_index[3]] = 1;
  4416. vc1_pred_mv_intfr(v, 0, 0, 0, 2, v->range_x, v->range_y, 0, !dir);
  4417. for (i = 0; i < 2; i++) {
  4418. s->mv[!dir][i+2][0] = s->mv[!dir][i][0] = s->current_picture.motion_val[!dir][s->block_index[i+2]][0] = s->current_picture.motion_val[!dir][s->block_index[i]][0];
  4419. s->mv[!dir][i+2][1] = s->mv[!dir][i][1] = s->current_picture.motion_val[!dir][s->block_index[i+2]][1] = s->current_picture.motion_val[!dir][s->block_index[i]][1];
  4420. }
  4421. }
  4422. }
  4423. }
  4424. vc1_mc_1mv(v, dir);
  4425. if (direct || bmvtype == BMV_TYPE_INTERPOLATED) {
  4426. vc1_interp_mc(v);
  4427. }
  4428. }
  4429. }
  4430. if (s->mb_x == s->mb_width - 1)
  4431. memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0]) * s->mb_stride);
  4432. v->cbp[s->mb_x] = block_cbp;
  4433. v->ttblk[s->mb_x] = block_tt;
  4434. return 0;
  4435. }
  4436. /** Decode blocks of I-frame
  4437. */
  4438. static void vc1_decode_i_blocks(VC1Context *v)
  4439. {
  4440. int k, j;
  4441. MpegEncContext *s = &v->s;
  4442. int cbp, val;
  4443. uint8_t *coded_val;
  4444. int mb_pos;
  4445. /* select codingmode used for VLC tables selection */
  4446. switch (v->y_ac_table_index) {
  4447. case 0:
  4448. v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
  4449. break;
  4450. case 1:
  4451. v->codingset = CS_HIGH_MOT_INTRA;
  4452. break;
  4453. case 2:
  4454. v->codingset = CS_MID_RATE_INTRA;
  4455. break;
  4456. }
  4457. switch (v->c_ac_table_index) {
  4458. case 0:
  4459. v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
  4460. break;
  4461. case 1:
  4462. v->codingset2 = CS_HIGH_MOT_INTER;
  4463. break;
  4464. case 2:
  4465. v->codingset2 = CS_MID_RATE_INTER;
  4466. break;
  4467. }
  4468. /* Set DC scale - y and c use the same */
  4469. s->y_dc_scale = s->y_dc_scale_table[v->pq];
  4470. s->c_dc_scale = s->c_dc_scale_table[v->pq];
  4471. //do frame decode
  4472. s->mb_x = s->mb_y = 0;
  4473. s->mb_intra = 1;
  4474. s->first_slice_line = 1;
  4475. for (s->mb_y = 0; s->mb_y < s->end_mb_y; s->mb_y++) {
  4476. s->mb_x = 0;
  4477. init_block_index(v);
  4478. for (; s->mb_x < v->end_mb_x; s->mb_x++) {
  4479. uint8_t *dst[6];
  4480. ff_update_block_index(s);
  4481. dst[0] = s->dest[0];
  4482. dst[1] = dst[0] + 8;
  4483. dst[2] = s->dest[0] + s->linesize * 8;
  4484. dst[3] = dst[2] + 8;
  4485. dst[4] = s->dest[1];
  4486. dst[5] = s->dest[2];
  4487. s->bdsp.clear_blocks(s->block[0]);
  4488. mb_pos = s->mb_x + s->mb_y * s->mb_width;
  4489. s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
  4490. s->current_picture.qscale_table[mb_pos] = v->pq;
  4491. s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
  4492. s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
  4493. // do actual MB decoding and displaying
  4494. cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
  4495. v->s.ac_pred = get_bits1(&v->s.gb);
  4496. for (k = 0; k < 6; k++) {
  4497. val = ((cbp >> (5 - k)) & 1);
  4498. if (k < 4) {
  4499. int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
  4500. val = val ^ pred;
  4501. *coded_val = val;
  4502. }
  4503. cbp |= val << (5 - k);
  4504. vc1_decode_i_block(v, s->block[k], k, val, (k < 4) ? v->codingset : v->codingset2);
  4505. if (k > 3 && (s->flags & CODEC_FLAG_GRAY))
  4506. continue;
  4507. v->vc1dsp.vc1_inv_trans_8x8(s->block[k]);
  4508. if (v->pq >= 9 && v->overlap) {
  4509. if (v->rangeredfrm)
  4510. for (j = 0; j < 64; j++)
  4511. s->block[k][j] <<= 1;
  4512. s->idsp.put_signed_pixels_clamped(s->block[k], dst[k],
  4513. k & 4 ? s->uvlinesize
  4514. : s->linesize);
  4515. } else {
  4516. if (v->rangeredfrm)
  4517. for (j = 0; j < 64; j++)
  4518. s->block[k][j] = (s->block[k][j] - 64) << 1;
  4519. s->idsp.put_pixels_clamped(s->block[k], dst[k],
  4520. k & 4 ? s->uvlinesize
  4521. : s->linesize);
  4522. }
  4523. }
  4524. if (v->pq >= 9 && v->overlap) {
  4525. if (s->mb_x) {
  4526. v->vc1dsp.vc1_h_overlap(s->dest[0], s->linesize);
  4527. v->vc1dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
  4528. if (!(s->flags & CODEC_FLAG_GRAY)) {
  4529. v->vc1dsp.vc1_h_overlap(s->dest[1], s->uvlinesize);
  4530. v->vc1dsp.vc1_h_overlap(s->dest[2], s->uvlinesize);
  4531. }
  4532. }
  4533. v->vc1dsp.vc1_h_overlap(s->dest[0] + 8, s->linesize);
  4534. v->vc1dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
  4535. if (!s->first_slice_line) {
  4536. v->vc1dsp.vc1_v_overlap(s->dest[0], s->linesize);
  4537. v->vc1dsp.vc1_v_overlap(s->dest[0] + 8, s->linesize);
  4538. if (!(s->flags & CODEC_FLAG_GRAY)) {
  4539. v->vc1dsp.vc1_v_overlap(s->dest[1], s->uvlinesize);
  4540. v->vc1dsp.vc1_v_overlap(s->dest[2], s->uvlinesize);
  4541. }
  4542. }
  4543. v->vc1dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
  4544. v->vc1dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
  4545. }
  4546. if (v->s.loop_filter) vc1_loop_filter_iblk(v, v->pq);
  4547. if (get_bits_count(&s->gb) > v->bits) {
  4548. ff_er_add_slice(&s->er, 0, 0, s->mb_x, s->mb_y, ER_MB_ERROR);
  4549. av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n",
  4550. get_bits_count(&s->gb), v->bits);
  4551. return;
  4552. }
  4553. }
  4554. if (!v->s.loop_filter)
  4555. ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
  4556. else if (s->mb_y)
  4557. ff_mpeg_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
  4558. s->first_slice_line = 0;
  4559. }
  4560. if (v->s.loop_filter)
  4561. ff_mpeg_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
  4562. /* This is intentionally mb_height and not end_mb_y - unlike in advanced
  4563. * profile, these only differ are when decoding MSS2 rectangles. */
  4564. ff_er_add_slice(&s->er, 0, 0, s->mb_width - 1, s->mb_height - 1, ER_MB_END);
  4565. }
  4566. /** Decode blocks of I-frame for advanced profile
  4567. */
  4568. static void vc1_decode_i_blocks_adv(VC1Context *v)
  4569. {
  4570. int k;
  4571. MpegEncContext *s = &v->s;
  4572. int cbp, val;
  4573. uint8_t *coded_val;
  4574. int mb_pos;
  4575. int mquant = v->pq;
  4576. int mqdiff;
  4577. GetBitContext *gb = &s->gb;
  4578. /* select codingmode used for VLC tables selection */
  4579. switch (v->y_ac_table_index) {
  4580. case 0:
  4581. v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
  4582. break;
  4583. case 1:
  4584. v->codingset = CS_HIGH_MOT_INTRA;
  4585. break;
  4586. case 2:
  4587. v->codingset = CS_MID_RATE_INTRA;
  4588. break;
  4589. }
  4590. switch (v->c_ac_table_index) {
  4591. case 0:
  4592. v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
  4593. break;
  4594. case 1:
  4595. v->codingset2 = CS_HIGH_MOT_INTER;
  4596. break;
  4597. case 2:
  4598. v->codingset2 = CS_MID_RATE_INTER;
  4599. break;
  4600. }
  4601. // do frame decode
  4602. s->mb_x = s->mb_y = 0;
  4603. s->mb_intra = 1;
  4604. s->first_slice_line = 1;
  4605. s->mb_y = s->start_mb_y;
  4606. if (s->start_mb_y) {
  4607. s->mb_x = 0;
  4608. init_block_index(v);
  4609. memset(&s->coded_block[s->block_index[0] - s->b8_stride], 0,
  4610. (1 + s->b8_stride) * sizeof(*s->coded_block));
  4611. }
  4612. for (; s->mb_y < s->end_mb_y; s->mb_y++) {
  4613. s->mb_x = 0;
  4614. init_block_index(v);
  4615. for (;s->mb_x < s->mb_width; s->mb_x++) {
  4616. int16_t (*block)[64] = v->block[v->cur_blk_idx];
  4617. ff_update_block_index(s);
  4618. s->bdsp.clear_blocks(block[0]);
  4619. mb_pos = s->mb_x + s->mb_y * s->mb_stride;
  4620. s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
  4621. s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
  4622. s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
  4623. // do actual MB decoding and displaying
  4624. if (v->fieldtx_is_raw)
  4625. v->fieldtx_plane[mb_pos] = get_bits1(&v->s.gb);
  4626. cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
  4627. if ( v->acpred_is_raw)
  4628. v->s.ac_pred = get_bits1(&v->s.gb);
  4629. else
  4630. v->s.ac_pred = v->acpred_plane[mb_pos];
  4631. if (v->condover == CONDOVER_SELECT && v->overflg_is_raw)
  4632. v->over_flags_plane[mb_pos] = get_bits1(&v->s.gb);
  4633. GET_MQUANT();
  4634. s->current_picture.qscale_table[mb_pos] = mquant;
  4635. /* Set DC scale - y and c use the same */
  4636. s->y_dc_scale = s->y_dc_scale_table[mquant];
  4637. s->c_dc_scale = s->c_dc_scale_table[mquant];
  4638. for (k = 0; k < 6; k++) {
  4639. val = ((cbp >> (5 - k)) & 1);
  4640. if (k < 4) {
  4641. int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
  4642. val = val ^ pred;
  4643. *coded_val = val;
  4644. }
  4645. cbp |= val << (5 - k);
  4646. v->a_avail = !s->first_slice_line || (k == 2 || k == 3);
  4647. v->c_avail = !!s->mb_x || (k == 1 || k == 3);
  4648. vc1_decode_i_block_adv(v, block[k], k, val,
  4649. (k < 4) ? v->codingset : v->codingset2, mquant);
  4650. if (k > 3 && (s->flags & CODEC_FLAG_GRAY))
  4651. continue;
  4652. v->vc1dsp.vc1_inv_trans_8x8(block[k]);
  4653. }
  4654. vc1_smooth_overlap_filter_iblk(v);
  4655. vc1_put_signed_blocks_clamped(v);
  4656. if (v->s.loop_filter) vc1_loop_filter_iblk_delayed(v, v->pq);
  4657. if (get_bits_count(&s->gb) > v->bits) {
  4658. // TODO: may need modification to handle slice coding
  4659. ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
  4660. av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n",
  4661. get_bits_count(&s->gb), v->bits);
  4662. return;
  4663. }
  4664. }
  4665. if (!v->s.loop_filter)
  4666. ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
  4667. else if (s->mb_y)
  4668. ff_mpeg_draw_horiz_band(s, (s->mb_y-1) * 16, 16);
  4669. s->first_slice_line = 0;
  4670. }
  4671. /* raw bottom MB row */
  4672. s->mb_x = 0;
  4673. init_block_index(v);
  4674. for (;s->mb_x < s->mb_width; s->mb_x++) {
  4675. ff_update_block_index(s);
  4676. vc1_put_signed_blocks_clamped(v);
  4677. if (v->s.loop_filter)
  4678. vc1_loop_filter_iblk_delayed(v, v->pq);
  4679. }
  4680. if (v->s.loop_filter)
  4681. ff_mpeg_draw_horiz_band(s, (s->end_mb_y-1)*16, 16);
  4682. ff_er_add_slice(&s->er, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
  4683. (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
  4684. }
  4685. static void vc1_decode_p_blocks(VC1Context *v)
  4686. {
  4687. MpegEncContext *s = &v->s;
  4688. int apply_loop_filter;
  4689. /* select codingmode used for VLC tables selection */
  4690. switch (v->c_ac_table_index) {
  4691. case 0:
  4692. v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
  4693. break;
  4694. case 1:
  4695. v->codingset = CS_HIGH_MOT_INTRA;
  4696. break;
  4697. case 2:
  4698. v->codingset = CS_MID_RATE_INTRA;
  4699. break;
  4700. }
  4701. switch (v->c_ac_table_index) {
  4702. case 0:
  4703. v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
  4704. break;
  4705. case 1:
  4706. v->codingset2 = CS_HIGH_MOT_INTER;
  4707. break;
  4708. case 2:
  4709. v->codingset2 = CS_MID_RATE_INTER;
  4710. break;
  4711. }
  4712. apply_loop_filter = s->loop_filter && !(s->avctx->skip_loop_filter >= AVDISCARD_NONKEY) &&
  4713. v->fcm == PROGRESSIVE;
  4714. s->first_slice_line = 1;
  4715. memset(v->cbp_base, 0, sizeof(v->cbp_base[0])*2*s->mb_stride);
  4716. for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
  4717. s->mb_x = 0;
  4718. init_block_index(v);
  4719. for (; s->mb_x < s->mb_width; s->mb_x++) {
  4720. ff_update_block_index(s);
  4721. if (v->fcm == ILACE_FIELD)
  4722. vc1_decode_p_mb_intfi(v);
  4723. else if (v->fcm == ILACE_FRAME)
  4724. vc1_decode_p_mb_intfr(v);
  4725. else vc1_decode_p_mb(v);
  4726. if (s->mb_y != s->start_mb_y && apply_loop_filter)
  4727. vc1_apply_p_loop_filter(v);
  4728. if (get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
  4729. // TODO: may need modification to handle slice coding
  4730. ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
  4731. av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i at %ix%i\n",
  4732. get_bits_count(&s->gb), v->bits, s->mb_x, s->mb_y);
  4733. return;
  4734. }
  4735. }
  4736. memmove(v->cbp_base, v->cbp, sizeof(v->cbp_base[0]) * s->mb_stride);
  4737. memmove(v->ttblk_base, v->ttblk, sizeof(v->ttblk_base[0]) * s->mb_stride);
  4738. memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0]) * s->mb_stride);
  4739. memmove(v->luma_mv_base, v->luma_mv, sizeof(v->luma_mv_base[0]) * s->mb_stride);
  4740. if (s->mb_y != s->start_mb_y) ff_mpeg_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
  4741. s->first_slice_line = 0;
  4742. }
  4743. if (apply_loop_filter) {
  4744. s->mb_x = 0;
  4745. init_block_index(v);
  4746. for (; s->mb_x < s->mb_width; s->mb_x++) {
  4747. ff_update_block_index(s);
  4748. vc1_apply_p_loop_filter(v);
  4749. }
  4750. }
  4751. if (s->end_mb_y >= s->start_mb_y)
  4752. ff_mpeg_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
  4753. ff_er_add_slice(&s->er, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
  4754. (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
  4755. }
  4756. static void vc1_decode_b_blocks(VC1Context *v)
  4757. {
  4758. MpegEncContext *s = &v->s;
  4759. /* select codingmode used for VLC tables selection */
  4760. switch (v->c_ac_table_index) {
  4761. case 0:
  4762. v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
  4763. break;
  4764. case 1:
  4765. v->codingset = CS_HIGH_MOT_INTRA;
  4766. break;
  4767. case 2:
  4768. v->codingset = CS_MID_RATE_INTRA;
  4769. break;
  4770. }
  4771. switch (v->c_ac_table_index) {
  4772. case 0:
  4773. v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
  4774. break;
  4775. case 1:
  4776. v->codingset2 = CS_HIGH_MOT_INTER;
  4777. break;
  4778. case 2:
  4779. v->codingset2 = CS_MID_RATE_INTER;
  4780. break;
  4781. }
  4782. s->first_slice_line = 1;
  4783. for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
  4784. s->mb_x = 0;
  4785. init_block_index(v);
  4786. for (; s->mb_x < s->mb_width; s->mb_x++) {
  4787. ff_update_block_index(s);
  4788. if (v->fcm == ILACE_FIELD)
  4789. vc1_decode_b_mb_intfi(v);
  4790. else if (v->fcm == ILACE_FRAME)
  4791. vc1_decode_b_mb_intfr(v);
  4792. else
  4793. vc1_decode_b_mb(v);
  4794. if (get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
  4795. // TODO: may need modification to handle slice coding
  4796. ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
  4797. av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i at %ix%i\n",
  4798. get_bits_count(&s->gb), v->bits, s->mb_x, s->mb_y);
  4799. return;
  4800. }
  4801. if (v->s.loop_filter) vc1_loop_filter_iblk(v, v->pq);
  4802. }
  4803. if (!v->s.loop_filter)
  4804. ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
  4805. else if (s->mb_y)
  4806. ff_mpeg_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
  4807. s->first_slice_line = 0;
  4808. }
  4809. if (v->s.loop_filter)
  4810. ff_mpeg_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
  4811. ff_er_add_slice(&s->er, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
  4812. (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
  4813. }
  4814. static void vc1_decode_skip_blocks(VC1Context *v)
  4815. {
  4816. MpegEncContext *s = &v->s;
  4817. if (!v->s.last_picture.f->data[0])
  4818. return;
  4819. ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_width - 1, s->end_mb_y - 1, ER_MB_END);
  4820. s->first_slice_line = 1;
  4821. for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
  4822. s->mb_x = 0;
  4823. init_block_index(v);
  4824. ff_update_block_index(s);
  4825. memcpy(s->dest[0], s->last_picture.f->data[0] + s->mb_y * 16 * s->linesize, s->linesize * 16);
  4826. memcpy(s->dest[1], s->last_picture.f->data[1] + s->mb_y * 8 * s->uvlinesize, s->uvlinesize * 8);
  4827. memcpy(s->dest[2], s->last_picture.f->data[2] + s->mb_y * 8 * s->uvlinesize, s->uvlinesize * 8);
  4828. ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
  4829. s->first_slice_line = 0;
  4830. }
  4831. s->pict_type = AV_PICTURE_TYPE_P;
  4832. }
  4833. void ff_vc1_decode_blocks(VC1Context *v)
  4834. {
  4835. v->s.esc3_level_length = 0;
  4836. if (v->x8_type) {
  4837. ff_intrax8_decode_picture(&v->x8, 2*v->pq + v->halfpq, v->pq * !v->pquantizer);
  4838. } else {
  4839. v->cur_blk_idx = 0;
  4840. v->left_blk_idx = -1;
  4841. v->topleft_blk_idx = 1;
  4842. v->top_blk_idx = 2;
  4843. switch (v->s.pict_type) {
  4844. case AV_PICTURE_TYPE_I:
  4845. if (v->profile == PROFILE_ADVANCED)
  4846. vc1_decode_i_blocks_adv(v);
  4847. else
  4848. vc1_decode_i_blocks(v);
  4849. break;
  4850. case AV_PICTURE_TYPE_P:
  4851. if (v->p_frame_skipped)
  4852. vc1_decode_skip_blocks(v);
  4853. else
  4854. vc1_decode_p_blocks(v);
  4855. break;
  4856. case AV_PICTURE_TYPE_B:
  4857. if (v->bi_type) {
  4858. if (v->profile == PROFILE_ADVANCED)
  4859. vc1_decode_i_blocks_adv(v);
  4860. else
  4861. vc1_decode_i_blocks(v);
  4862. } else
  4863. vc1_decode_b_blocks(v);
  4864. break;
  4865. }
  4866. }
  4867. }
  4868. #if CONFIG_WMV3IMAGE_DECODER || CONFIG_VC1IMAGE_DECODER
  4869. typedef struct {
  4870. /**
  4871. * Transform coefficients for both sprites in 16.16 fixed point format,
  4872. * in the order they appear in the bitstream:
  4873. * x scale
  4874. * rotation 1 (unused)
  4875. * x offset
  4876. * rotation 2 (unused)
  4877. * y scale
  4878. * y offset
  4879. * alpha
  4880. */
  4881. int coefs[2][7];
  4882. int effect_type, effect_flag;
  4883. int effect_pcount1, effect_pcount2; ///< amount of effect parameters stored in effect_params
  4884. int effect_params1[15], effect_params2[10]; ///< effect parameters in 16.16 fixed point format
  4885. } SpriteData;
  4886. static inline int get_fp_val(GetBitContext* gb)
  4887. {
  4888. return (get_bits_long(gb, 30) - (1 << 29)) << 1;
  4889. }
  4890. static void vc1_sprite_parse_transform(GetBitContext* gb, int c[7])
  4891. {
  4892. c[1] = c[3] = 0;
  4893. switch (get_bits(gb, 2)) {
  4894. case 0:
  4895. c[0] = 1 << 16;
  4896. c[2] = get_fp_val(gb);
  4897. c[4] = 1 << 16;
  4898. break;
  4899. case 1:
  4900. c[0] = c[4] = get_fp_val(gb);
  4901. c[2] = get_fp_val(gb);
  4902. break;
  4903. case 2:
  4904. c[0] = get_fp_val(gb);
  4905. c[2] = get_fp_val(gb);
  4906. c[4] = get_fp_val(gb);
  4907. break;
  4908. case 3:
  4909. c[0] = get_fp_val(gb);
  4910. c[1] = get_fp_val(gb);
  4911. c[2] = get_fp_val(gb);
  4912. c[3] = get_fp_val(gb);
  4913. c[4] = get_fp_val(gb);
  4914. break;
  4915. }
  4916. c[5] = get_fp_val(gb);
  4917. if (get_bits1(gb))
  4918. c[6] = get_fp_val(gb);
  4919. else
  4920. c[6] = 1 << 16;
  4921. }
  4922. static int vc1_parse_sprites(VC1Context *v, GetBitContext* gb, SpriteData* sd)
  4923. {
  4924. AVCodecContext *avctx = v->s.avctx;
  4925. int sprite, i;
  4926. for (sprite = 0; sprite <= v->two_sprites; sprite++) {
  4927. vc1_sprite_parse_transform(gb, sd->coefs[sprite]);
  4928. if (sd->coefs[sprite][1] || sd->coefs[sprite][3])
  4929. avpriv_request_sample(avctx, "Non-zero rotation coefficients");
  4930. av_log(avctx, AV_LOG_DEBUG, sprite ? "S2:" : "S1:");
  4931. for (i = 0; i < 7; i++)
  4932. av_log(avctx, AV_LOG_DEBUG, " %d.%.3d",
  4933. sd->coefs[sprite][i] / (1<<16),
  4934. (abs(sd->coefs[sprite][i]) & 0xFFFF) * 1000 / (1 << 16));
  4935. av_log(avctx, AV_LOG_DEBUG, "\n");
  4936. }
  4937. skip_bits(gb, 2);
  4938. if (sd->effect_type = get_bits_long(gb, 30)) {
  4939. switch (sd->effect_pcount1 = get_bits(gb, 4)) {
  4940. case 7:
  4941. vc1_sprite_parse_transform(gb, sd->effect_params1);
  4942. break;
  4943. case 14:
  4944. vc1_sprite_parse_transform(gb, sd->effect_params1);
  4945. vc1_sprite_parse_transform(gb, sd->effect_params1 + 7);
  4946. break;
  4947. default:
  4948. for (i = 0; i < sd->effect_pcount1; i++)
  4949. sd->effect_params1[i] = get_fp_val(gb);
  4950. }
  4951. if (sd->effect_type != 13 || sd->effect_params1[0] != sd->coefs[0][6]) {
  4952. // effect 13 is simple alpha blending and matches the opacity above
  4953. av_log(avctx, AV_LOG_DEBUG, "Effect: %d; params: ", sd->effect_type);
  4954. for (i = 0; i < sd->effect_pcount1; i++)
  4955. av_log(avctx, AV_LOG_DEBUG, " %d.%.2d",
  4956. sd->effect_params1[i] / (1 << 16),
  4957. (abs(sd->effect_params1[i]) & 0xFFFF) * 1000 / (1 << 16));
  4958. av_log(avctx, AV_LOG_DEBUG, "\n");
  4959. }
  4960. sd->effect_pcount2 = get_bits(gb, 16);
  4961. if (sd->effect_pcount2 > 10) {
  4962. av_log(avctx, AV_LOG_ERROR, "Too many effect parameters\n");
  4963. return AVERROR_INVALIDDATA;
  4964. } else if (sd->effect_pcount2) {
  4965. i = -1;
  4966. av_log(avctx, AV_LOG_DEBUG, "Effect params 2: ");
  4967. while (++i < sd->effect_pcount2) {
  4968. sd->effect_params2[i] = get_fp_val(gb);
  4969. av_log(avctx, AV_LOG_DEBUG, " %d.%.2d",
  4970. sd->effect_params2[i] / (1 << 16),
  4971. (abs(sd->effect_params2[i]) & 0xFFFF) * 1000 / (1 << 16));
  4972. }
  4973. av_log(avctx, AV_LOG_DEBUG, "\n");
  4974. }
  4975. }
  4976. if (sd->effect_flag = get_bits1(gb))
  4977. av_log(avctx, AV_LOG_DEBUG, "Effect flag set\n");
  4978. if (get_bits_count(gb) >= gb->size_in_bits +
  4979. (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE ? 64 : 0)) {
  4980. av_log(avctx, AV_LOG_ERROR, "Buffer overrun\n");
  4981. return AVERROR_INVALIDDATA;
  4982. }
  4983. if (get_bits_count(gb) < gb->size_in_bits - 8)
  4984. av_log(avctx, AV_LOG_WARNING, "Buffer not fully read\n");
  4985. return 0;
  4986. }
  4987. static void vc1_draw_sprites(VC1Context *v, SpriteData* sd)
  4988. {
  4989. int i, plane, row, sprite;
  4990. int sr_cache[2][2] = { { -1, -1 }, { -1, -1 } };
  4991. uint8_t* src_h[2][2];
  4992. int xoff[2], xadv[2], yoff[2], yadv[2], alpha;
  4993. int ysub[2];
  4994. MpegEncContext *s = &v->s;
  4995. for (i = 0; i <= v->two_sprites; i++) {
  4996. xoff[i] = av_clip(sd->coefs[i][2], 0, v->sprite_width-1 << 16);
  4997. xadv[i] = sd->coefs[i][0];
  4998. if (xadv[i] != 1<<16 || (v->sprite_width << 16) - (v->output_width << 16) - xoff[i])
  4999. xadv[i] = av_clip(xadv[i], 0, ((v->sprite_width<<16) - xoff[i] - 1) / v->output_width);
  5000. yoff[i] = av_clip(sd->coefs[i][5], 0, v->sprite_height-1 << 16);
  5001. yadv[i] = av_clip(sd->coefs[i][4], 0, ((v->sprite_height << 16) - yoff[i]) / v->output_height);
  5002. }
  5003. alpha = av_clip(sd->coefs[1][6], 0, (1<<16) - 1);
  5004. for (plane = 0; plane < (s->flags&CODEC_FLAG_GRAY ? 1 : 3); plane++) {
  5005. int width = v->output_width>>!!plane;
  5006. for (row = 0; row < v->output_height>>!!plane; row++) {
  5007. uint8_t *dst = v->sprite_output_frame->data[plane] +
  5008. v->sprite_output_frame->linesize[plane] * row;
  5009. for (sprite = 0; sprite <= v->two_sprites; sprite++) {
  5010. uint8_t *iplane = s->current_picture.f->data[plane];
  5011. int iline = s->current_picture.f->linesize[plane];
  5012. int ycoord = yoff[sprite] + yadv[sprite] * row;
  5013. int yline = ycoord >> 16;
  5014. int next_line;
  5015. ysub[sprite] = ycoord & 0xFFFF;
  5016. if (sprite) {
  5017. iplane = s->last_picture.f->data[plane];
  5018. iline = s->last_picture.f->linesize[plane];
  5019. }
  5020. next_line = FFMIN(yline + 1, (v->sprite_height >> !!plane) - 1) * iline;
  5021. if (!(xoff[sprite] & 0xFFFF) && xadv[sprite] == 1 << 16) {
  5022. src_h[sprite][0] = iplane + (xoff[sprite] >> 16) + yline * iline;
  5023. if (ysub[sprite])
  5024. src_h[sprite][1] = iplane + (xoff[sprite] >> 16) + next_line;
  5025. } else {
  5026. if (sr_cache[sprite][0] != yline) {
  5027. if (sr_cache[sprite][1] == yline) {
  5028. FFSWAP(uint8_t*, v->sr_rows[sprite][0], v->sr_rows[sprite][1]);
  5029. FFSWAP(int, sr_cache[sprite][0], sr_cache[sprite][1]);
  5030. } else {
  5031. v->vc1dsp.sprite_h(v->sr_rows[sprite][0], iplane + yline * iline, xoff[sprite], xadv[sprite], width);
  5032. sr_cache[sprite][0] = yline;
  5033. }
  5034. }
  5035. if (ysub[sprite] && sr_cache[sprite][1] != yline + 1) {
  5036. v->vc1dsp.sprite_h(v->sr_rows[sprite][1],
  5037. iplane + next_line, xoff[sprite],
  5038. xadv[sprite], width);
  5039. sr_cache[sprite][1] = yline + 1;
  5040. }
  5041. src_h[sprite][0] = v->sr_rows[sprite][0];
  5042. src_h[sprite][1] = v->sr_rows[sprite][1];
  5043. }
  5044. }
  5045. if (!v->two_sprites) {
  5046. if (ysub[0]) {
  5047. v->vc1dsp.sprite_v_single(dst, src_h[0][0], src_h[0][1], ysub[0], width);
  5048. } else {
  5049. memcpy(dst, src_h[0][0], width);
  5050. }
  5051. } else {
  5052. if (ysub[0] && ysub[1]) {
  5053. v->vc1dsp.sprite_v_double_twoscale(dst, src_h[0][0], src_h[0][1], ysub[0],
  5054. src_h[1][0], src_h[1][1], ysub[1], alpha, width);
  5055. } else if (ysub[0]) {
  5056. v->vc1dsp.sprite_v_double_onescale(dst, src_h[0][0], src_h[0][1], ysub[0],
  5057. src_h[1][0], alpha, width);
  5058. } else if (ysub[1]) {
  5059. v->vc1dsp.sprite_v_double_onescale(dst, src_h[1][0], src_h[1][1], ysub[1],
  5060. src_h[0][0], (1<<16)-1-alpha, width);
  5061. } else {
  5062. v->vc1dsp.sprite_v_double_noscale(dst, src_h[0][0], src_h[1][0], alpha, width);
  5063. }
  5064. }
  5065. }
  5066. if (!plane) {
  5067. for (i = 0; i <= v->two_sprites; i++) {
  5068. xoff[i] >>= 1;
  5069. yoff[i] >>= 1;
  5070. }
  5071. }
  5072. }
  5073. }
  5074. static int vc1_decode_sprites(VC1Context *v, GetBitContext* gb)
  5075. {
  5076. int ret;
  5077. MpegEncContext *s = &v->s;
  5078. AVCodecContext *avctx = s->avctx;
  5079. SpriteData sd;
  5080. memset(&sd, 0, sizeof(sd));
  5081. ret = vc1_parse_sprites(v, gb, &sd);
  5082. if (ret < 0)
  5083. return ret;
  5084. if (!s->current_picture.f->data[0]) {
  5085. av_log(avctx, AV_LOG_ERROR, "Got no sprites\n");
  5086. return -1;
  5087. }
  5088. if (v->two_sprites && (!s->last_picture_ptr || !s->last_picture.f->data[0])) {
  5089. av_log(avctx, AV_LOG_WARNING, "Need two sprites, only got one\n");
  5090. v->two_sprites = 0;
  5091. }
  5092. av_frame_unref(v->sprite_output_frame);
  5093. if ((ret = ff_get_buffer(avctx, v->sprite_output_frame, 0)) < 0)
  5094. return ret;
  5095. vc1_draw_sprites(v, &sd);
  5096. return 0;
  5097. }
  5098. static void vc1_sprite_flush(AVCodecContext *avctx)
  5099. {
  5100. VC1Context *v = avctx->priv_data;
  5101. MpegEncContext *s = &v->s;
  5102. AVFrame *f = s->current_picture.f;
  5103. int plane, i;
  5104. /* Windows Media Image codecs have a convergence interval of two keyframes.
  5105. Since we can't enforce it, clear to black the missing sprite. This is
  5106. wrong but it looks better than doing nothing. */
  5107. if (f && f->data[0])
  5108. for (plane = 0; plane < (s->flags&CODEC_FLAG_GRAY ? 1 : 3); plane++)
  5109. for (i = 0; i < v->sprite_height>>!!plane; i++)
  5110. memset(f->data[plane] + i * f->linesize[plane],
  5111. plane ? 128 : 0, f->linesize[plane]);
  5112. }
  5113. #endif
  5114. av_cold int ff_vc1_decode_init_alloc_tables(VC1Context *v)
  5115. {
  5116. MpegEncContext *s = &v->s;
  5117. int i;
  5118. int mb_height = FFALIGN(s->mb_height, 2);
  5119. /* Allocate mb bitplanes */
  5120. v->mv_type_mb_plane = av_malloc (s->mb_stride * mb_height);
  5121. v->direct_mb_plane = av_malloc (s->mb_stride * mb_height);
  5122. v->forward_mb_plane = av_malloc (s->mb_stride * mb_height);
  5123. v->fieldtx_plane = av_mallocz(s->mb_stride * mb_height);
  5124. v->acpred_plane = av_malloc (s->mb_stride * mb_height);
  5125. v->over_flags_plane = av_malloc (s->mb_stride * mb_height);
  5126. v->n_allocated_blks = s->mb_width + 2;
  5127. v->block = av_malloc(sizeof(*v->block) * v->n_allocated_blks);
  5128. v->cbp_base = av_malloc(sizeof(v->cbp_base[0]) * 2 * s->mb_stride);
  5129. v->cbp = v->cbp_base + s->mb_stride;
  5130. v->ttblk_base = av_malloc(sizeof(v->ttblk_base[0]) * 2 * s->mb_stride);
  5131. v->ttblk = v->ttblk_base + s->mb_stride;
  5132. v->is_intra_base = av_mallocz(sizeof(v->is_intra_base[0]) * 2 * s->mb_stride);
  5133. v->is_intra = v->is_intra_base + s->mb_stride;
  5134. v->luma_mv_base = av_mallocz(sizeof(v->luma_mv_base[0]) * 2 * s->mb_stride);
  5135. v->luma_mv = v->luma_mv_base + s->mb_stride;
  5136. /* allocate block type info in that way so it could be used with s->block_index[] */
  5137. v->mb_type_base = av_malloc(s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2);
  5138. v->mb_type[0] = v->mb_type_base + s->b8_stride + 1;
  5139. v->mb_type[1] = v->mb_type_base + s->b8_stride * (mb_height * 2 + 1) + s->mb_stride + 1;
  5140. v->mb_type[2] = v->mb_type[1] + s->mb_stride * (mb_height + 1);
  5141. /* allocate memory to store block level MV info */
  5142. v->blk_mv_type_base = av_mallocz( s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2);
  5143. v->blk_mv_type = v->blk_mv_type_base + s->b8_stride + 1;
  5144. v->mv_f_base = av_mallocz(2 * (s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2));
  5145. v->mv_f[0] = v->mv_f_base + s->b8_stride + 1;
  5146. v->mv_f[1] = v->mv_f[0] + (s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2);
  5147. v->mv_f_next_base = av_mallocz(2 * (s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2));
  5148. v->mv_f_next[0] = v->mv_f_next_base + s->b8_stride + 1;
  5149. v->mv_f_next[1] = v->mv_f_next[0] + (s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2);
  5150. /* Init coded blocks info */
  5151. if (v->profile == PROFILE_ADVANCED) {
  5152. // if (alloc_bitplane(&v->over_flags_plane, s->mb_width, s->mb_height) < 0)
  5153. // return -1;
  5154. // if (alloc_bitplane(&v->ac_pred_plane, s->mb_width, s->mb_height) < 0)
  5155. // return -1;
  5156. }
  5157. ff_intrax8_common_init(&v->x8,s);
  5158. if (s->avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || s->avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
  5159. for (i = 0; i < 4; i++)
  5160. if (!(v->sr_rows[i >> 1][i & 1] = av_malloc(v->output_width)))
  5161. return AVERROR(ENOMEM);
  5162. }
  5163. if (!v->mv_type_mb_plane || !v->direct_mb_plane || !v->acpred_plane || !v->over_flags_plane ||
  5164. !v->block || !v->cbp_base || !v->ttblk_base || !v->is_intra_base || !v->luma_mv_base ||
  5165. !v->mb_type_base) {
  5166. av_freep(&v->mv_type_mb_plane);
  5167. av_freep(&v->direct_mb_plane);
  5168. av_freep(&v->acpred_plane);
  5169. av_freep(&v->over_flags_plane);
  5170. av_freep(&v->block);
  5171. av_freep(&v->cbp_base);
  5172. av_freep(&v->ttblk_base);
  5173. av_freep(&v->is_intra_base);
  5174. av_freep(&v->luma_mv_base);
  5175. av_freep(&v->mb_type_base);
  5176. return AVERROR(ENOMEM);
  5177. }
  5178. return 0;
  5179. }
  5180. av_cold void ff_vc1_init_transposed_scantables(VC1Context *v)
  5181. {
  5182. int i;
  5183. for (i = 0; i < 64; i++) {
  5184. #define transpose(x) (((x) >> 3) | (((x) & 7) << 3))
  5185. v->zz_8x8[0][i] = transpose(ff_wmv1_scantable[0][i]);
  5186. v->zz_8x8[1][i] = transpose(ff_wmv1_scantable[1][i]);
  5187. v->zz_8x8[2][i] = transpose(ff_wmv1_scantable[2][i]);
  5188. v->zz_8x8[3][i] = transpose(ff_wmv1_scantable[3][i]);
  5189. v->zzi_8x8[i] = transpose(ff_vc1_adv_interlaced_8x8_zz[i]);
  5190. }
  5191. v->left_blk_sh = 0;
  5192. v->top_blk_sh = 3;
  5193. }
  5194. /** Initialize a VC1/WMV3 decoder
  5195. * @todo TODO: Handle VC-1 IDUs (Transport level?)
  5196. * @todo TODO: Decypher remaining bits in extra_data
  5197. */
  5198. static av_cold int vc1_decode_init(AVCodecContext *avctx)
  5199. {
  5200. VC1Context *v = avctx->priv_data;
  5201. MpegEncContext *s = &v->s;
  5202. GetBitContext gb;
  5203. int ret;
  5204. /* save the container output size for WMImage */
  5205. v->output_width = avctx->width;
  5206. v->output_height = avctx->height;
  5207. if (!avctx->extradata_size || !avctx->extradata)
  5208. return -1;
  5209. if (!(avctx->flags & CODEC_FLAG_GRAY))
  5210. avctx->pix_fmt = ff_get_format(avctx, avctx->codec->pix_fmts);
  5211. else
  5212. avctx->pix_fmt = AV_PIX_FMT_GRAY8;
  5213. v->s.avctx = avctx;
  5214. if ((ret = ff_vc1_init_common(v)) < 0)
  5215. return ret;
  5216. // ensure static VLC tables are initialized
  5217. if ((ret = ff_msmpeg4_decode_init(avctx)) < 0)
  5218. return ret;
  5219. if ((ret = ff_vc1_decode_init_alloc_tables(v)) < 0)
  5220. return ret;
  5221. // Hack to ensure the above functions will be called
  5222. // again once we know all necessary settings.
  5223. // That this is necessary might indicate a bug.
  5224. ff_vc1_decode_end(avctx);
  5225. ff_blockdsp_init(&s->bdsp, avctx);
  5226. ff_h264chroma_init(&v->h264chroma, 8);
  5227. ff_qpeldsp_init(&s->qdsp);
  5228. if (avctx->codec_id == AV_CODEC_ID_WMV3 || avctx->codec_id == AV_CODEC_ID_WMV3IMAGE) {
  5229. int count = 0;
  5230. // looks like WMV3 has a sequence header stored in the extradata
  5231. // advanced sequence header may be before the first frame
  5232. // the last byte of the extradata is a version number, 1 for the
  5233. // samples we can decode
  5234. init_get_bits(&gb, avctx->extradata, avctx->extradata_size*8);
  5235. if ((ret = ff_vc1_decode_sequence_header(avctx, v, &gb)) < 0)
  5236. return ret;
  5237. count = avctx->extradata_size*8 - get_bits_count(&gb);
  5238. if (count > 0) {
  5239. av_log(avctx, AV_LOG_INFO, "Extra data: %i bits left, value: %X\n",
  5240. count, get_bits(&gb, count));
  5241. } else if (count < 0) {
  5242. av_log(avctx, AV_LOG_INFO, "Read %i bits in overflow\n", -count);
  5243. }
  5244. } else { // VC1/WVC1/WVP2
  5245. const uint8_t *start = avctx->extradata;
  5246. uint8_t *end = avctx->extradata + avctx->extradata_size;
  5247. const uint8_t *next;
  5248. int size, buf2_size;
  5249. uint8_t *buf2 = NULL;
  5250. int seq_initialized = 0, ep_initialized = 0;
  5251. if (avctx->extradata_size < 16) {
  5252. av_log(avctx, AV_LOG_ERROR, "Extradata size too small: %i\n", avctx->extradata_size);
  5253. return -1;
  5254. }
  5255. buf2 = av_mallocz(avctx->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
  5256. start = find_next_marker(start, end); // in WVC1 extradata first byte is its size, but can be 0 in mkv
  5257. next = start;
  5258. for (; next < end; start = next) {
  5259. next = find_next_marker(start + 4, end);
  5260. size = next - start - 4;
  5261. if (size <= 0)
  5262. continue;
  5263. buf2_size = vc1_unescape_buffer(start + 4, size, buf2);
  5264. init_get_bits(&gb, buf2, buf2_size * 8);
  5265. switch (AV_RB32(start)) {
  5266. case VC1_CODE_SEQHDR:
  5267. if ((ret = ff_vc1_decode_sequence_header(avctx, v, &gb)) < 0) {
  5268. av_free(buf2);
  5269. return ret;
  5270. }
  5271. seq_initialized = 1;
  5272. break;
  5273. case VC1_CODE_ENTRYPOINT:
  5274. if ((ret = ff_vc1_decode_entry_point(avctx, v, &gb)) < 0) {
  5275. av_free(buf2);
  5276. return ret;
  5277. }
  5278. ep_initialized = 1;
  5279. break;
  5280. }
  5281. }
  5282. av_free(buf2);
  5283. if (!seq_initialized || !ep_initialized) {
  5284. av_log(avctx, AV_LOG_ERROR, "Incomplete extradata\n");
  5285. return -1;
  5286. }
  5287. v->res_sprite = (avctx->codec_id == AV_CODEC_ID_VC1IMAGE);
  5288. }
  5289. v->sprite_output_frame = av_frame_alloc();
  5290. if (!v->sprite_output_frame)
  5291. return AVERROR(ENOMEM);
  5292. avctx->profile = v->profile;
  5293. if (v->profile == PROFILE_ADVANCED)
  5294. avctx->level = v->level;
  5295. avctx->has_b_frames = !!avctx->max_b_frames;
  5296. s->mb_width = (avctx->coded_width + 15) >> 4;
  5297. s->mb_height = (avctx->coded_height + 15) >> 4;
  5298. if (v->profile == PROFILE_ADVANCED || v->res_fasttx) {
  5299. ff_vc1_init_transposed_scantables(v);
  5300. } else {
  5301. memcpy(v->zz_8x8, ff_wmv1_scantable, 4*64);
  5302. v->left_blk_sh = 3;
  5303. v->top_blk_sh = 0;
  5304. }
  5305. if (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
  5306. v->sprite_width = avctx->coded_width;
  5307. v->sprite_height = avctx->coded_height;
  5308. avctx->coded_width = avctx->width = v->output_width;
  5309. avctx->coded_height = avctx->height = v->output_height;
  5310. // prevent 16.16 overflows
  5311. if (v->sprite_width > 1 << 14 ||
  5312. v->sprite_height > 1 << 14 ||
  5313. v->output_width > 1 << 14 ||
  5314. v->output_height > 1 << 14) return -1;
  5315. if ((v->sprite_width&1) || (v->sprite_height&1)) {
  5316. avpriv_request_sample(avctx, "odd sprites support");
  5317. return AVERROR_PATCHWELCOME;
  5318. }
  5319. }
  5320. return 0;
  5321. }
  5322. /** Close a VC1/WMV3 decoder
  5323. * @warning Initial try at using MpegEncContext stuff
  5324. */
  5325. av_cold int ff_vc1_decode_end(AVCodecContext *avctx)
  5326. {
  5327. VC1Context *v = avctx->priv_data;
  5328. int i;
  5329. av_frame_free(&v->sprite_output_frame);
  5330. for (i = 0; i < 4; i++)
  5331. av_freep(&v->sr_rows[i >> 1][i & 1]);
  5332. av_freep(&v->hrd_rate);
  5333. av_freep(&v->hrd_buffer);
  5334. ff_mpv_common_end(&v->s);
  5335. av_freep(&v->mv_type_mb_plane);
  5336. av_freep(&v->direct_mb_plane);
  5337. av_freep(&v->forward_mb_plane);
  5338. av_freep(&v->fieldtx_plane);
  5339. av_freep(&v->acpred_plane);
  5340. av_freep(&v->over_flags_plane);
  5341. av_freep(&v->mb_type_base);
  5342. av_freep(&v->blk_mv_type_base);
  5343. av_freep(&v->mv_f_base);
  5344. av_freep(&v->mv_f_next_base);
  5345. av_freep(&v->block);
  5346. av_freep(&v->cbp_base);
  5347. av_freep(&v->ttblk_base);
  5348. av_freep(&v->is_intra_base); // FIXME use v->mb_type[]
  5349. av_freep(&v->luma_mv_base);
  5350. ff_intrax8_common_end(&v->x8);
  5351. return 0;
  5352. }
  5353. /** Decode a VC1/WMV3 frame
  5354. * @todo TODO: Handle VC-1 IDUs (Transport level?)
  5355. */
  5356. static int vc1_decode_frame(AVCodecContext *avctx, void *data,
  5357. int *got_frame, AVPacket *avpkt)
  5358. {
  5359. const uint8_t *buf = avpkt->data;
  5360. int buf_size = avpkt->size, n_slices = 0, i, ret;
  5361. VC1Context *v = avctx->priv_data;
  5362. MpegEncContext *s = &v->s;
  5363. AVFrame *pict = data;
  5364. uint8_t *buf2 = NULL;
  5365. const uint8_t *buf_start = buf, *buf_start_second_field = NULL;
  5366. int mb_height, n_slices1=-1;
  5367. struct {
  5368. uint8_t *buf;
  5369. GetBitContext gb;
  5370. int mby_start;
  5371. } *slices = NULL, *tmp;
  5372. v->second_field = 0;
  5373. if(s->flags & CODEC_FLAG_LOW_DELAY)
  5374. s->low_delay = 1;
  5375. /* no supplementary picture */
  5376. if (buf_size == 0 || (buf_size == 4 && AV_RB32(buf) == VC1_CODE_ENDOFSEQ)) {
  5377. /* special case for last picture */
  5378. if (s->low_delay == 0 && s->next_picture_ptr) {
  5379. if ((ret = av_frame_ref(pict, s->next_picture_ptr->f)) < 0)
  5380. return ret;
  5381. s->next_picture_ptr = NULL;
  5382. *got_frame = 1;
  5383. }
  5384. return buf_size;
  5385. }
  5386. if (s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU) {
  5387. if (v->profile < PROFILE_ADVANCED)
  5388. avctx->pix_fmt = AV_PIX_FMT_VDPAU_WMV3;
  5389. else
  5390. avctx->pix_fmt = AV_PIX_FMT_VDPAU_VC1;
  5391. }
  5392. //for advanced profile we may need to parse and unescape data
  5393. if (avctx->codec_id == AV_CODEC_ID_VC1 || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
  5394. int buf_size2 = 0;
  5395. buf2 = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
  5396. if (!buf2)
  5397. return AVERROR(ENOMEM);
  5398. if (IS_MARKER(AV_RB32(buf))) { /* frame starts with marker and needs to be parsed */
  5399. const uint8_t *start, *end, *next;
  5400. int size;
  5401. next = buf;
  5402. for (start = buf, end = buf + buf_size; next < end; start = next) {
  5403. next = find_next_marker(start + 4, end);
  5404. size = next - start - 4;
  5405. if (size <= 0) continue;
  5406. switch (AV_RB32(start)) {
  5407. case VC1_CODE_FRAME:
  5408. if (avctx->hwaccel ||
  5409. s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
  5410. buf_start = start;
  5411. buf_size2 = vc1_unescape_buffer(start + 4, size, buf2);
  5412. break;
  5413. case VC1_CODE_FIELD: {
  5414. int buf_size3;
  5415. if (avctx->hwaccel ||
  5416. s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
  5417. buf_start_second_field = start;
  5418. tmp = av_realloc_array(slices, sizeof(*slices), (n_slices+1));
  5419. if (!tmp)
  5420. goto err;
  5421. slices = tmp;
  5422. slices[n_slices].buf = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
  5423. if (!slices[n_slices].buf)
  5424. goto err;
  5425. buf_size3 = vc1_unescape_buffer(start + 4, size,
  5426. slices[n_slices].buf);
  5427. init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,
  5428. buf_size3 << 3);
  5429. /* assuming that the field marker is at the exact middle,
  5430. hope it's correct */
  5431. slices[n_slices].mby_start = s->mb_height + 1 >> 1;
  5432. n_slices1 = n_slices - 1; // index of the last slice of the first field
  5433. n_slices++;
  5434. break;
  5435. }
  5436. case VC1_CODE_ENTRYPOINT: /* it should be before frame data */
  5437. buf_size2 = vc1_unescape_buffer(start + 4, size, buf2);
  5438. init_get_bits(&s->gb, buf2, buf_size2 * 8);
  5439. ff_vc1_decode_entry_point(avctx, v, &s->gb);
  5440. break;
  5441. case VC1_CODE_SLICE: {
  5442. int buf_size3;
  5443. tmp = av_realloc_array(slices, sizeof(*slices), (n_slices+1));
  5444. if (!tmp)
  5445. goto err;
  5446. slices = tmp;
  5447. slices[n_slices].buf = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
  5448. if (!slices[n_slices].buf)
  5449. goto err;
  5450. buf_size3 = vc1_unescape_buffer(start + 4, size,
  5451. slices[n_slices].buf);
  5452. init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,
  5453. buf_size3 << 3);
  5454. slices[n_slices].mby_start = get_bits(&slices[n_slices].gb, 9);
  5455. n_slices++;
  5456. break;
  5457. }
  5458. }
  5459. }
  5460. } else if (v->interlace && ((buf[0] & 0xC0) == 0xC0)) { /* WVC1 interlaced stores both fields divided by marker */
  5461. const uint8_t *divider;
  5462. int buf_size3;
  5463. divider = find_next_marker(buf, buf + buf_size);
  5464. if ((divider == (buf + buf_size)) || AV_RB32(divider) != VC1_CODE_FIELD) {
  5465. av_log(avctx, AV_LOG_ERROR, "Error in WVC1 interlaced frame\n");
  5466. goto err;
  5467. } else { // found field marker, unescape second field
  5468. if (avctx->hwaccel ||
  5469. s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
  5470. buf_start_second_field = divider;
  5471. tmp = av_realloc_array(slices, sizeof(*slices), (n_slices+1));
  5472. if (!tmp)
  5473. goto err;
  5474. slices = tmp;
  5475. slices[n_slices].buf = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
  5476. if (!slices[n_slices].buf)
  5477. goto err;
  5478. buf_size3 = vc1_unescape_buffer(divider + 4, buf + buf_size - divider - 4, slices[n_slices].buf);
  5479. init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,
  5480. buf_size3 << 3);
  5481. slices[n_slices].mby_start = s->mb_height + 1 >> 1;
  5482. n_slices1 = n_slices - 1;
  5483. n_slices++;
  5484. }
  5485. buf_size2 = vc1_unescape_buffer(buf, divider - buf, buf2);
  5486. } else {
  5487. buf_size2 = vc1_unescape_buffer(buf, buf_size, buf2);
  5488. }
  5489. init_get_bits(&s->gb, buf2, buf_size2*8);
  5490. } else
  5491. init_get_bits(&s->gb, buf, buf_size*8);
  5492. if (v->res_sprite) {
  5493. v->new_sprite = !get_bits1(&s->gb);
  5494. v->two_sprites = get_bits1(&s->gb);
  5495. /* res_sprite means a Windows Media Image stream, AV_CODEC_ID_*IMAGE means
  5496. we're using the sprite compositor. These are intentionally kept separate
  5497. so you can get the raw sprites by using the wmv3 decoder for WMVP or
  5498. the vc1 one for WVP2 */
  5499. if (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
  5500. if (v->new_sprite) {
  5501. // switch AVCodecContext parameters to those of the sprites
  5502. avctx->width = avctx->coded_width = v->sprite_width;
  5503. avctx->height = avctx->coded_height = v->sprite_height;
  5504. } else {
  5505. goto image;
  5506. }
  5507. }
  5508. }
  5509. if (s->context_initialized &&
  5510. (s->width != avctx->coded_width ||
  5511. s->height != avctx->coded_height)) {
  5512. ff_vc1_decode_end(avctx);
  5513. }
  5514. if (!s->context_initialized) {
  5515. if (ff_msmpeg4_decode_init(avctx) < 0)
  5516. goto err;
  5517. if (ff_vc1_decode_init_alloc_tables(v) < 0) {
  5518. ff_mpv_common_end(s);
  5519. goto err;
  5520. }
  5521. s->low_delay = !avctx->has_b_frames || v->res_sprite;
  5522. if (v->profile == PROFILE_ADVANCED) {
  5523. if(avctx->coded_width<=1 || avctx->coded_height<=1)
  5524. goto err;
  5525. s->h_edge_pos = avctx->coded_width;
  5526. s->v_edge_pos = avctx->coded_height;
  5527. }
  5528. }
  5529. // do parse frame header
  5530. v->pic_header_flag = 0;
  5531. v->first_pic_header_flag = 1;
  5532. if (v->profile < PROFILE_ADVANCED) {
  5533. if (ff_vc1_parse_frame_header(v, &s->gb) < 0) {
  5534. goto err;
  5535. }
  5536. } else {
  5537. if (ff_vc1_parse_frame_header_adv(v, &s->gb) < 0) {
  5538. goto err;
  5539. }
  5540. }
  5541. v->first_pic_header_flag = 0;
  5542. if (avctx->debug & FF_DEBUG_PICT_INFO)
  5543. av_log(v->s.avctx, AV_LOG_DEBUG, "pict_type: %c\n", av_get_picture_type_char(s->pict_type));
  5544. if ((avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE)
  5545. && s->pict_type != AV_PICTURE_TYPE_I) {
  5546. av_log(v->s.avctx, AV_LOG_ERROR, "Sprite decoder: expected I-frame\n");
  5547. goto err;
  5548. }
  5549. if ((s->mb_height >> v->field_mode) == 0) {
  5550. av_log(v->s.avctx, AV_LOG_ERROR, "image too short\n");
  5551. goto err;
  5552. }
  5553. // for skipping the frame
  5554. s->current_picture.f->pict_type = s->pict_type;
  5555. s->current_picture.f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
  5556. /* skip B-frames if we don't have reference frames */
  5557. if (s->last_picture_ptr == NULL && (s->pict_type == AV_PICTURE_TYPE_B || s->droppable)) {
  5558. av_log(v->s.avctx, AV_LOG_DEBUG, "Skipping B frame without reference frames\n");
  5559. goto end;
  5560. }
  5561. if ((avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B) ||
  5562. (avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I) ||
  5563. avctx->skip_frame >= AVDISCARD_ALL) {
  5564. goto end;
  5565. }
  5566. if (s->next_p_frame_damaged) {
  5567. if (s->pict_type == AV_PICTURE_TYPE_B)
  5568. goto end;
  5569. else
  5570. s->next_p_frame_damaged = 0;
  5571. }
  5572. if (ff_mpv_frame_start(s, avctx) < 0) {
  5573. goto err;
  5574. }
  5575. v->s.current_picture_ptr->field_picture = v->field_mode;
  5576. v->s.current_picture_ptr->f->interlaced_frame = (v->fcm != PROGRESSIVE);
  5577. v->s.current_picture_ptr->f->top_field_first = v->tff;
  5578. // process pulldown flags
  5579. s->current_picture_ptr->f->repeat_pict = 0;
  5580. // Pulldown flags are only valid when 'broadcast' has been set.
  5581. // So ticks_per_frame will be 2
  5582. if (v->rff) {
  5583. // repeat field
  5584. s->current_picture_ptr->f->repeat_pict = 1;
  5585. } else if (v->rptfrm) {
  5586. // repeat frames
  5587. s->current_picture_ptr->f->repeat_pict = v->rptfrm * 2;
  5588. }
  5589. s->me.qpel_put = s->qdsp.put_qpel_pixels_tab;
  5590. s->me.qpel_avg = s->qdsp.avg_qpel_pixels_tab;
  5591. if ((CONFIG_VC1_VDPAU_DECODER)
  5592. &&s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU) {
  5593. if (v->field_mode && buf_start_second_field) {
  5594. ff_vdpau_vc1_decode_picture(s, buf_start, buf_start_second_field - buf_start);
  5595. ff_vdpau_vc1_decode_picture(s, buf_start_second_field, (buf + buf_size) - buf_start_second_field);
  5596. } else {
  5597. ff_vdpau_vc1_decode_picture(s, buf_start, (buf + buf_size) - buf_start);
  5598. }
  5599. } else if (avctx->hwaccel) {
  5600. if (v->field_mode && buf_start_second_field) {
  5601. // decode first field
  5602. s->picture_structure = PICT_BOTTOM_FIELD - v->tff;
  5603. if (avctx->hwaccel->start_frame(avctx, buf_start, buf_start_second_field - buf_start) < 0)
  5604. goto err;
  5605. if (avctx->hwaccel->decode_slice(avctx, buf_start, buf_start_second_field - buf_start) < 0)
  5606. goto err;
  5607. if (avctx->hwaccel->end_frame(avctx) < 0)
  5608. goto err;
  5609. // decode second field
  5610. s->gb = slices[n_slices1 + 1].gb;
  5611. s->picture_structure = PICT_TOP_FIELD + v->tff;
  5612. v->second_field = 1;
  5613. v->pic_header_flag = 0;
  5614. if (ff_vc1_parse_frame_header_adv(v, &s->gb) < 0) {
  5615. av_log(avctx, AV_LOG_ERROR, "parsing header for second field failed");
  5616. goto err;
  5617. }
  5618. v->s.current_picture_ptr->f->pict_type = v->s.pict_type;
  5619. if (avctx->hwaccel->start_frame(avctx, buf_start_second_field, (buf + buf_size) - buf_start_second_field) < 0)
  5620. goto err;
  5621. if (avctx->hwaccel->decode_slice(avctx, buf_start_second_field, (buf + buf_size) - buf_start_second_field) < 0)
  5622. goto err;
  5623. if (avctx->hwaccel->end_frame(avctx) < 0)
  5624. goto err;
  5625. } else {
  5626. s->picture_structure = PICT_FRAME;
  5627. if (avctx->hwaccel->start_frame(avctx, buf_start, (buf + buf_size) - buf_start) < 0)
  5628. goto err;
  5629. if (avctx->hwaccel->decode_slice(avctx, buf_start, (buf + buf_size) - buf_start) < 0)
  5630. goto err;
  5631. if (avctx->hwaccel->end_frame(avctx) < 0)
  5632. goto err;
  5633. }
  5634. } else {
  5635. int header_ret = 0;
  5636. ff_mpeg_er_frame_start(s);
  5637. v->bits = buf_size * 8;
  5638. v->end_mb_x = s->mb_width;
  5639. if (v->field_mode) {
  5640. s->current_picture.f->linesize[0] <<= 1;
  5641. s->current_picture.f->linesize[1] <<= 1;
  5642. s->current_picture.f->linesize[2] <<= 1;
  5643. s->linesize <<= 1;
  5644. s->uvlinesize <<= 1;
  5645. }
  5646. mb_height = s->mb_height >> v->field_mode;
  5647. av_assert0 (mb_height > 0);
  5648. for (i = 0; i <= n_slices; i++) {
  5649. if (i > 0 && slices[i - 1].mby_start >= mb_height) {
  5650. if (v->field_mode <= 0) {
  5651. av_log(v->s.avctx, AV_LOG_ERROR, "Slice %d starts beyond "
  5652. "picture boundary (%d >= %d)\n", i,
  5653. slices[i - 1].mby_start, mb_height);
  5654. continue;
  5655. }
  5656. v->second_field = 1;
  5657. av_assert0((s->mb_height & 1) == 0);
  5658. v->blocks_off = s->b8_stride * (s->mb_height&~1);
  5659. v->mb_off = s->mb_stride * s->mb_height >> 1;
  5660. } else {
  5661. v->second_field = 0;
  5662. v->blocks_off = 0;
  5663. v->mb_off = 0;
  5664. }
  5665. if (i) {
  5666. v->pic_header_flag = 0;
  5667. if (v->field_mode && i == n_slices1 + 2) {
  5668. if ((header_ret = ff_vc1_parse_frame_header_adv(v, &s->gb)) < 0) {
  5669. av_log(v->s.avctx, AV_LOG_ERROR, "Field header damaged\n");
  5670. if (avctx->err_recognition & AV_EF_EXPLODE)
  5671. goto err;
  5672. continue;
  5673. }
  5674. } else if (get_bits1(&s->gb)) {
  5675. v->pic_header_flag = 1;
  5676. if ((header_ret = ff_vc1_parse_frame_header_adv(v, &s->gb)) < 0) {
  5677. av_log(v->s.avctx, AV_LOG_ERROR, "Slice header damaged\n");
  5678. if (avctx->err_recognition & AV_EF_EXPLODE)
  5679. goto err;
  5680. continue;
  5681. }
  5682. }
  5683. }
  5684. if (header_ret < 0)
  5685. continue;
  5686. s->start_mb_y = (i == 0) ? 0 : FFMAX(0, slices[i-1].mby_start % mb_height);
  5687. if (!v->field_mode || v->second_field)
  5688. s->end_mb_y = (i == n_slices ) ? mb_height : FFMIN(mb_height, slices[i].mby_start % mb_height);
  5689. else {
  5690. if (i >= n_slices) {
  5691. av_log(v->s.avctx, AV_LOG_ERROR, "first field slice count too large\n");
  5692. continue;
  5693. }
  5694. s->end_mb_y = (i <= n_slices1 + 1) ? mb_height : FFMIN(mb_height, slices[i].mby_start % mb_height);
  5695. }
  5696. if (s->end_mb_y <= s->start_mb_y) {
  5697. av_log(v->s.avctx, AV_LOG_ERROR, "end mb y %d %d invalid\n", s->end_mb_y, s->start_mb_y);
  5698. continue;
  5699. }
  5700. if (!v->p_frame_skipped && s->pict_type != AV_PICTURE_TYPE_I && !v->cbpcy_vlc) {
  5701. av_log(v->s.avctx, AV_LOG_ERROR, "missing cbpcy_vlc\n");
  5702. continue;
  5703. }
  5704. ff_vc1_decode_blocks(v);
  5705. if (i != n_slices)
  5706. s->gb = slices[i].gb;
  5707. }
  5708. if (v->field_mode) {
  5709. v->second_field = 0;
  5710. s->current_picture.f->linesize[0] >>= 1;
  5711. s->current_picture.f->linesize[1] >>= 1;
  5712. s->current_picture.f->linesize[2] >>= 1;
  5713. s->linesize >>= 1;
  5714. s->uvlinesize >>= 1;
  5715. if (v->s.pict_type != AV_PICTURE_TYPE_BI && v->s.pict_type != AV_PICTURE_TYPE_B) {
  5716. FFSWAP(uint8_t *, v->mv_f_next[0], v->mv_f[0]);
  5717. FFSWAP(uint8_t *, v->mv_f_next[1], v->mv_f[1]);
  5718. }
  5719. }
  5720. av_dlog(s->avctx, "Consumed %i/%i bits\n",
  5721. get_bits_count(&s->gb), s->gb.size_in_bits);
  5722. // if (get_bits_count(&s->gb) > buf_size * 8)
  5723. // return -1;
  5724. if(s->er.error_occurred && s->pict_type == AV_PICTURE_TYPE_B)
  5725. goto err;
  5726. if (!v->field_mode)
  5727. ff_er_frame_end(&s->er);
  5728. }
  5729. ff_mpv_frame_end(s);
  5730. if (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
  5731. image:
  5732. avctx->width = avctx->coded_width = v->output_width;
  5733. avctx->height = avctx->coded_height = v->output_height;
  5734. if (avctx->skip_frame >= AVDISCARD_NONREF)
  5735. goto end;
  5736. #if CONFIG_WMV3IMAGE_DECODER || CONFIG_VC1IMAGE_DECODER
  5737. if (vc1_decode_sprites(v, &s->gb))
  5738. goto err;
  5739. #endif
  5740. if ((ret = av_frame_ref(pict, v->sprite_output_frame)) < 0)
  5741. goto err;
  5742. *got_frame = 1;
  5743. } else {
  5744. if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
  5745. if ((ret = av_frame_ref(pict, s->current_picture_ptr->f)) < 0)
  5746. goto err;
  5747. ff_print_debug_info(s, s->current_picture_ptr, pict);
  5748. *got_frame = 1;
  5749. } else if (s->last_picture_ptr != NULL) {
  5750. if ((ret = av_frame_ref(pict, s->last_picture_ptr->f)) < 0)
  5751. goto err;
  5752. ff_print_debug_info(s, s->last_picture_ptr, pict);
  5753. *got_frame = 1;
  5754. }
  5755. }
  5756. end:
  5757. av_free(buf2);
  5758. for (i = 0; i < n_slices; i++)
  5759. av_free(slices[i].buf);
  5760. av_free(slices);
  5761. return buf_size;
  5762. err:
  5763. av_free(buf2);
  5764. for (i = 0; i < n_slices; i++)
  5765. av_free(slices[i].buf);
  5766. av_free(slices);
  5767. return -1;
  5768. }
  5769. static const AVProfile profiles[] = {
  5770. { FF_PROFILE_VC1_SIMPLE, "Simple" },
  5771. { FF_PROFILE_VC1_MAIN, "Main" },
  5772. { FF_PROFILE_VC1_COMPLEX, "Complex" },
  5773. { FF_PROFILE_VC1_ADVANCED, "Advanced" },
  5774. { FF_PROFILE_UNKNOWN },
  5775. };
  5776. static const enum AVPixelFormat vc1_hwaccel_pixfmt_list_420[] = {
  5777. #if CONFIG_VC1_DXVA2_HWACCEL
  5778. AV_PIX_FMT_DXVA2_VLD,
  5779. #endif
  5780. #if CONFIG_VC1_VAAPI_HWACCEL
  5781. AV_PIX_FMT_VAAPI_VLD,
  5782. #endif
  5783. #if CONFIG_VC1_VDPAU_HWACCEL
  5784. AV_PIX_FMT_VDPAU,
  5785. #endif
  5786. AV_PIX_FMT_YUV420P,
  5787. AV_PIX_FMT_NONE
  5788. };
  5789. AVCodec ff_vc1_decoder = {
  5790. .name = "vc1",
  5791. .long_name = NULL_IF_CONFIG_SMALL("SMPTE VC-1"),
  5792. .type = AVMEDIA_TYPE_VIDEO,
  5793. .id = AV_CODEC_ID_VC1,
  5794. .priv_data_size = sizeof(VC1Context),
  5795. .init = vc1_decode_init,
  5796. .close = ff_vc1_decode_end,
  5797. .decode = vc1_decode_frame,
  5798. .flush = ff_mpeg_flush,
  5799. .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY,
  5800. .pix_fmts = vc1_hwaccel_pixfmt_list_420,
  5801. .profiles = NULL_IF_CONFIG_SMALL(profiles)
  5802. };
  5803. #if CONFIG_WMV3_DECODER
  5804. AVCodec ff_wmv3_decoder = {
  5805. .name = "wmv3",
  5806. .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9"),
  5807. .type = AVMEDIA_TYPE_VIDEO,
  5808. .id = AV_CODEC_ID_WMV3,
  5809. .priv_data_size = sizeof(VC1Context),
  5810. .init = vc1_decode_init,
  5811. .close = ff_vc1_decode_end,
  5812. .decode = vc1_decode_frame,
  5813. .flush = ff_mpeg_flush,
  5814. .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY,
  5815. .pix_fmts = vc1_hwaccel_pixfmt_list_420,
  5816. .profiles = NULL_IF_CONFIG_SMALL(profiles)
  5817. };
  5818. #endif
  5819. #if CONFIG_WMV3_VDPAU_DECODER
  5820. AVCodec ff_wmv3_vdpau_decoder = {
  5821. .name = "wmv3_vdpau",
  5822. .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9 VDPAU"),
  5823. .type = AVMEDIA_TYPE_VIDEO,
  5824. .id = AV_CODEC_ID_WMV3,
  5825. .priv_data_size = sizeof(VC1Context),
  5826. .init = vc1_decode_init,
  5827. .close = ff_vc1_decode_end,
  5828. .decode = vc1_decode_frame,
  5829. .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_HWACCEL_VDPAU,
  5830. .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_VDPAU_WMV3, AV_PIX_FMT_NONE },
  5831. .profiles = NULL_IF_CONFIG_SMALL(profiles)
  5832. };
  5833. #endif
  5834. #if CONFIG_VC1_VDPAU_DECODER
  5835. AVCodec ff_vc1_vdpau_decoder = {
  5836. .name = "vc1_vdpau",
  5837. .long_name = NULL_IF_CONFIG_SMALL("SMPTE VC-1 VDPAU"),
  5838. .type = AVMEDIA_TYPE_VIDEO,
  5839. .id = AV_CODEC_ID_VC1,
  5840. .priv_data_size = sizeof(VC1Context),
  5841. .init = vc1_decode_init,
  5842. .close = ff_vc1_decode_end,
  5843. .decode = vc1_decode_frame,
  5844. .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_HWACCEL_VDPAU,
  5845. .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_VDPAU_VC1, AV_PIX_FMT_NONE },
  5846. .profiles = NULL_IF_CONFIG_SMALL(profiles)
  5847. };
  5848. #endif
  5849. #if CONFIG_WMV3IMAGE_DECODER
  5850. AVCodec ff_wmv3image_decoder = {
  5851. .name = "wmv3image",
  5852. .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9 Image"),
  5853. .type = AVMEDIA_TYPE_VIDEO,
  5854. .id = AV_CODEC_ID_WMV3IMAGE,
  5855. .priv_data_size = sizeof(VC1Context),
  5856. .init = vc1_decode_init,
  5857. .close = ff_vc1_decode_end,
  5858. .decode = vc1_decode_frame,
  5859. .capabilities = CODEC_CAP_DR1,
  5860. .flush = vc1_sprite_flush,
  5861. .pix_fmts = (const enum AVPixelFormat[]) {
  5862. AV_PIX_FMT_YUV420P,
  5863. AV_PIX_FMT_NONE
  5864. },
  5865. };
  5866. #endif
  5867. #if CONFIG_VC1IMAGE_DECODER
  5868. AVCodec ff_vc1image_decoder = {
  5869. .name = "vc1image",
  5870. .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9 Image v2"),
  5871. .type = AVMEDIA_TYPE_VIDEO,
  5872. .id = AV_CODEC_ID_VC1IMAGE,
  5873. .priv_data_size = sizeof(VC1Context),
  5874. .init = vc1_decode_init,
  5875. .close = ff_vc1_decode_end,
  5876. .decode = vc1_decode_frame,
  5877. .capabilities = CODEC_CAP_DR1,
  5878. .flush = vc1_sprite_flush,
  5879. .pix_fmts = (const enum AVPixelFormat[]) {
  5880. AV_PIX_FMT_YUV420P,
  5881. AV_PIX_FMT_NONE
  5882. },
  5883. };
  5884. #endif