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.

941 lines
37KB

  1. /*
  2. * VC-1 and WMV3 decoder
  3. * Copyright (c) 2011 Mashiat Sarker Shakkhar
  4. * Copyright (c) 2006-2007 Konstantin Shishkov
  5. * Partly based on vc9.c (c) 2005 Anonymous, Alex Beregszaszi, Michael Niedermayer
  6. *
  7. * This file is part of FFmpeg.
  8. *
  9. * FFmpeg is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * FFmpeg is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with FFmpeg; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. /**
  24. * @file
  25. * VC-1 and WMV3 block decoding routines
  26. */
  27. #include "mathops.h"
  28. #include "mpegutils.h"
  29. #include "mpegvideo.h"
  30. #include "vc1.h"
  31. #include "vc1_pred.h"
  32. #include "vc1data.h"
  33. static av_always_inline int scaleforsame_x(VC1Context *v, int n /* MV */, int dir)
  34. {
  35. int scaledvalue, refdist;
  36. int scalesame1, scalesame2;
  37. int scalezone1_x, zone1offset_x;
  38. int table_index = dir ^ v->second_field;
  39. if (v->s.pict_type != AV_PICTURE_TYPE_B)
  40. refdist = v->refdist;
  41. else
  42. refdist = dir ? v->brfd : v->frfd;
  43. if (refdist > 3)
  44. refdist = 3;
  45. scalesame1 = ff_vc1_field_mvpred_scales[table_index][1][refdist];
  46. scalesame2 = ff_vc1_field_mvpred_scales[table_index][2][refdist];
  47. scalezone1_x = ff_vc1_field_mvpred_scales[table_index][3][refdist];
  48. zone1offset_x = ff_vc1_field_mvpred_scales[table_index][5][refdist];
  49. if (FFABS(n) > 255)
  50. scaledvalue = n;
  51. else {
  52. if (FFABS(n) < scalezone1_x)
  53. scaledvalue = (n * scalesame1) >> 8;
  54. else {
  55. if (n < 0)
  56. scaledvalue = ((n * scalesame2) >> 8) - zone1offset_x;
  57. else
  58. scaledvalue = ((n * scalesame2) >> 8) + zone1offset_x;
  59. }
  60. }
  61. return av_clip(scaledvalue, -v->range_x, v->range_x - 1);
  62. }
  63. static av_always_inline int scaleforsame_y(VC1Context *v, int i, int n /* MV */, int dir)
  64. {
  65. int scaledvalue, refdist;
  66. int scalesame1, scalesame2;
  67. int scalezone1_y, zone1offset_y;
  68. int table_index = dir ^ v->second_field;
  69. if (v->s.pict_type != AV_PICTURE_TYPE_B)
  70. refdist = v->refdist;
  71. else
  72. refdist = dir ? v->brfd : v->frfd;
  73. if (refdist > 3)
  74. refdist = 3;
  75. scalesame1 = ff_vc1_field_mvpred_scales[table_index][1][refdist];
  76. scalesame2 = ff_vc1_field_mvpred_scales[table_index][2][refdist];
  77. scalezone1_y = ff_vc1_field_mvpred_scales[table_index][4][refdist];
  78. zone1offset_y = ff_vc1_field_mvpred_scales[table_index][6][refdist];
  79. if (FFABS(n) > 63)
  80. scaledvalue = n;
  81. else {
  82. if (FFABS(n) < scalezone1_y)
  83. scaledvalue = (n * scalesame1) >> 8;
  84. else {
  85. if (n < 0)
  86. scaledvalue = ((n * scalesame2) >> 8) - zone1offset_y;
  87. else
  88. scaledvalue = ((n * scalesame2) >> 8) + zone1offset_y;
  89. }
  90. }
  91. if (v->cur_field_type && !v->ref_field_type[dir])
  92. return av_clip(scaledvalue, -v->range_y / 2 + 1, v->range_y / 2);
  93. else
  94. return av_clip(scaledvalue, -v->range_y / 2, v->range_y / 2 - 1);
  95. }
  96. static av_always_inline int scaleforopp_x(VC1Context *v, int n /* MV */)
  97. {
  98. int scalezone1_x, zone1offset_x;
  99. int scaleopp1, scaleopp2, brfd;
  100. int scaledvalue;
  101. brfd = FFMIN(v->brfd, 3);
  102. scalezone1_x = ff_vc1_b_field_mvpred_scales[3][brfd];
  103. zone1offset_x = ff_vc1_b_field_mvpred_scales[5][brfd];
  104. scaleopp1 = ff_vc1_b_field_mvpred_scales[1][brfd];
  105. scaleopp2 = ff_vc1_b_field_mvpred_scales[2][brfd];
  106. if (FFABS(n) > 255)
  107. scaledvalue = n;
  108. else {
  109. if (FFABS(n) < scalezone1_x)
  110. scaledvalue = (n * scaleopp1) >> 8;
  111. else {
  112. if (n < 0)
  113. scaledvalue = ((n * scaleopp2) >> 8) - zone1offset_x;
  114. else
  115. scaledvalue = ((n * scaleopp2) >> 8) + zone1offset_x;
  116. }
  117. }
  118. return av_clip(scaledvalue, -v->range_x, v->range_x - 1);
  119. }
  120. static av_always_inline int scaleforopp_y(VC1Context *v, int n /* MV */, int dir)
  121. {
  122. int scalezone1_y, zone1offset_y;
  123. int scaleopp1, scaleopp2, brfd;
  124. int scaledvalue;
  125. brfd = FFMIN(v->brfd, 3);
  126. scalezone1_y = ff_vc1_b_field_mvpred_scales[4][brfd];
  127. zone1offset_y = ff_vc1_b_field_mvpred_scales[6][brfd];
  128. scaleopp1 = ff_vc1_b_field_mvpred_scales[1][brfd];
  129. scaleopp2 = ff_vc1_b_field_mvpred_scales[2][brfd];
  130. if (FFABS(n) > 63)
  131. scaledvalue = n;
  132. else {
  133. if (FFABS(n) < scalezone1_y)
  134. scaledvalue = (n * scaleopp1) >> 8;
  135. else {
  136. if (n < 0)
  137. scaledvalue = ((n * scaleopp2) >> 8) - zone1offset_y;
  138. else
  139. scaledvalue = ((n * scaleopp2) >> 8) + zone1offset_y;
  140. }
  141. }
  142. if (v->cur_field_type && !v->ref_field_type[dir]) {
  143. return av_clip(scaledvalue, -v->range_y / 2 + 1, v->range_y / 2);
  144. } else {
  145. return av_clip(scaledvalue, -v->range_y / 2, v->range_y / 2 - 1);
  146. }
  147. }
  148. static av_always_inline int scaleforsame(VC1Context *v, int i, int n /* MV */,
  149. int dim, int dir)
  150. {
  151. int brfd, scalesame;
  152. int hpel = 1 - v->s.quarter_sample;
  153. n >>= hpel;
  154. if (v->s.pict_type != AV_PICTURE_TYPE_B || v->second_field || !dir) {
  155. if (dim)
  156. n = scaleforsame_y(v, i, n, dir) * (1 << hpel);
  157. else
  158. n = scaleforsame_x(v, n, dir) * (1 << hpel);
  159. return n;
  160. }
  161. brfd = FFMIN(v->brfd, 3);
  162. scalesame = ff_vc1_b_field_mvpred_scales[0][brfd];
  163. n = (n * scalesame >> 8) << hpel;
  164. return n;
  165. }
  166. static av_always_inline int scaleforopp(VC1Context *v, int n /* MV */,
  167. int dim, int dir)
  168. {
  169. int refdist, scaleopp;
  170. int hpel = 1 - v->s.quarter_sample;
  171. n >>= hpel;
  172. if (v->s.pict_type == AV_PICTURE_TYPE_B && !v->second_field && dir == 1) {
  173. if (dim)
  174. n = scaleforopp_y(v, n, dir) << hpel;
  175. else
  176. n = scaleforopp_x(v, n) << hpel;
  177. return n;
  178. }
  179. if (v->s.pict_type != AV_PICTURE_TYPE_B)
  180. refdist = FFMIN(v->refdist, 3);
  181. else
  182. refdist = dir ? v->brfd : v->frfd;
  183. scaleopp = ff_vc1_field_mvpred_scales[dir ^ v->second_field][0][refdist];
  184. n = (n * scaleopp >> 8) * (1 << hpel);
  185. return n;
  186. }
  187. /** Predict and set motion vector
  188. */
  189. void ff_vc1_pred_mv(VC1Context *v, int n, int dmv_x, int dmv_y,
  190. int mv1, int r_x, int r_y, uint8_t* is_intra,
  191. int pred_flag, int dir)
  192. {
  193. MpegEncContext *s = &v->s;
  194. int xy, wrap, off = 0;
  195. int16_t *A, *B, *C;
  196. int px, py;
  197. int sum;
  198. int mixedmv_pic, num_samefield = 0, num_oppfield = 0;
  199. int opposite, a_f, b_f, c_f;
  200. int16_t field_predA[2];
  201. int16_t field_predB[2];
  202. int16_t field_predC[2];
  203. int a_valid, b_valid, c_valid;
  204. int hybridmv_thresh, y_bias = 0;
  205. if (v->mv_mode == MV_PMODE_MIXED_MV ||
  206. ((v->mv_mode == MV_PMODE_INTENSITY_COMP) && (v->mv_mode2 == MV_PMODE_MIXED_MV)))
  207. mixedmv_pic = 1;
  208. else
  209. mixedmv_pic = 0;
  210. /* scale MV difference to be quad-pel */
  211. if (!s->quarter_sample) {
  212. dmv_x *= 2;
  213. dmv_y *= 2;
  214. }
  215. wrap = s->b8_stride;
  216. xy = s->block_index[n];
  217. if (s->mb_intra) {
  218. s->mv[0][n][0] = s->current_picture.motion_val[0][xy + v->blocks_off][0] = 0;
  219. s->mv[0][n][1] = s->current_picture.motion_val[0][xy + v->blocks_off][1] = 0;
  220. s->current_picture.motion_val[1][xy + v->blocks_off][0] = 0;
  221. s->current_picture.motion_val[1][xy + v->blocks_off][1] = 0;
  222. if (mv1) { /* duplicate motion data for 1-MV block */
  223. s->current_picture.motion_val[0][xy + 1 + v->blocks_off][0] = 0;
  224. s->current_picture.motion_val[0][xy + 1 + v->blocks_off][1] = 0;
  225. s->current_picture.motion_val[0][xy + wrap + v->blocks_off][0] = 0;
  226. s->current_picture.motion_val[0][xy + wrap + v->blocks_off][1] = 0;
  227. s->current_picture.motion_val[0][xy + wrap + 1 + v->blocks_off][0] = 0;
  228. s->current_picture.motion_val[0][xy + wrap + 1 + v->blocks_off][1] = 0;
  229. v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
  230. s->current_picture.motion_val[1][xy + 1 + v->blocks_off][0] = 0;
  231. s->current_picture.motion_val[1][xy + 1 + v->blocks_off][1] = 0;
  232. s->current_picture.motion_val[1][xy + wrap][0] = 0;
  233. s->current_picture.motion_val[1][xy + wrap + v->blocks_off][1] = 0;
  234. s->current_picture.motion_val[1][xy + wrap + 1 + v->blocks_off][0] = 0;
  235. s->current_picture.motion_val[1][xy + wrap + 1 + v->blocks_off][1] = 0;
  236. }
  237. return;
  238. }
  239. C = s->current_picture.motion_val[dir][xy - 1 + v->blocks_off];
  240. A = s->current_picture.motion_val[dir][xy - wrap + v->blocks_off];
  241. if (mv1) {
  242. if (v->field_mode && mixedmv_pic)
  243. off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
  244. else
  245. off = (s->mb_x == (s->mb_width - 1)) ? -1 : 2;
  246. } else {
  247. //in 4-MV mode different blocks have different B predictor position
  248. switch (n) {
  249. case 0:
  250. off = (s->mb_x > 0) ? -1 : 1;
  251. break;
  252. case 1:
  253. off = (s->mb_x == (s->mb_width - 1)) ? -1 : 1;
  254. break;
  255. case 2:
  256. off = 1;
  257. break;
  258. case 3:
  259. off = -1;
  260. }
  261. }
  262. B = s->current_picture.motion_val[dir][xy - wrap + off + v->blocks_off];
  263. a_valid = !s->first_slice_line || (n == 2 || n == 3);
  264. b_valid = a_valid && (s->mb_width > 1);
  265. c_valid = s->mb_x || (n == 1 || n == 3);
  266. if (v->field_mode) {
  267. a_valid = a_valid && !is_intra[xy - wrap];
  268. b_valid = b_valid && !is_intra[xy - wrap + off];
  269. c_valid = c_valid && !is_intra[xy - 1];
  270. }
  271. if (a_valid) {
  272. a_f = v->mv_f[dir][xy - wrap + v->blocks_off];
  273. num_oppfield += a_f;
  274. num_samefield += 1 - a_f;
  275. field_predA[0] = A[0];
  276. field_predA[1] = A[1];
  277. } else {
  278. field_predA[0] = field_predA[1] = 0;
  279. a_f = 0;
  280. }
  281. if (b_valid) {
  282. b_f = v->mv_f[dir][xy - wrap + off + v->blocks_off];
  283. num_oppfield += b_f;
  284. num_samefield += 1 - b_f;
  285. field_predB[0] = B[0];
  286. field_predB[1] = B[1];
  287. } else {
  288. field_predB[0] = field_predB[1] = 0;
  289. b_f = 0;
  290. }
  291. if (c_valid) {
  292. c_f = v->mv_f[dir][xy - 1 + v->blocks_off];
  293. num_oppfield += c_f;
  294. num_samefield += 1 - c_f;
  295. field_predC[0] = C[0];
  296. field_predC[1] = C[1];
  297. } else {
  298. field_predC[0] = field_predC[1] = 0;
  299. c_f = 0;
  300. }
  301. if (v->field_mode) {
  302. if (!v->numref)
  303. // REFFIELD determines if the last field or the second-last field is
  304. // to be used as reference
  305. opposite = 1 - v->reffield;
  306. else {
  307. if (num_samefield <= num_oppfield)
  308. opposite = 1 - pred_flag;
  309. else
  310. opposite = pred_flag;
  311. }
  312. } else
  313. opposite = 0;
  314. if (opposite) {
  315. if (a_valid && !a_f) {
  316. field_predA[0] = scaleforopp(v, field_predA[0], 0, dir);
  317. field_predA[1] = scaleforopp(v, field_predA[1], 1, dir);
  318. }
  319. if (b_valid && !b_f) {
  320. field_predB[0] = scaleforopp(v, field_predB[0], 0, dir);
  321. field_predB[1] = scaleforopp(v, field_predB[1], 1, dir);
  322. }
  323. if (c_valid && !c_f) {
  324. field_predC[0] = scaleforopp(v, field_predC[0], 0, dir);
  325. field_predC[1] = scaleforopp(v, field_predC[1], 1, dir);
  326. }
  327. v->mv_f[dir][xy + v->blocks_off] = 1;
  328. v->ref_field_type[dir] = !v->cur_field_type;
  329. } else {
  330. if (a_valid && a_f) {
  331. field_predA[0] = scaleforsame(v, n, field_predA[0], 0, dir);
  332. field_predA[1] = scaleforsame(v, n, field_predA[1], 1, dir);
  333. }
  334. if (b_valid && b_f) {
  335. field_predB[0] = scaleforsame(v, n, field_predB[0], 0, dir);
  336. field_predB[1] = scaleforsame(v, n, field_predB[1], 1, dir);
  337. }
  338. if (c_valid && c_f) {
  339. field_predC[0] = scaleforsame(v, n, field_predC[0], 0, dir);
  340. field_predC[1] = scaleforsame(v, n, field_predC[1], 1, dir);
  341. }
  342. v->mv_f[dir][xy + v->blocks_off] = 0;
  343. v->ref_field_type[dir] = v->cur_field_type;
  344. }
  345. if (a_valid) {
  346. px = field_predA[0];
  347. py = field_predA[1];
  348. } else if (c_valid) {
  349. px = field_predC[0];
  350. py = field_predC[1];
  351. } else if (b_valid) {
  352. px = field_predB[0];
  353. py = field_predB[1];
  354. } else {
  355. px = 0;
  356. py = 0;
  357. }
  358. if (num_samefield + num_oppfield > 1) {
  359. px = mid_pred(field_predA[0], field_predB[0], field_predC[0]);
  360. py = mid_pred(field_predA[1], field_predB[1], field_predC[1]);
  361. }
  362. /* Pullback MV as specified in 8.3.5.3.4 */
  363. if (!v->field_mode) {
  364. int qx, qy, X, Y;
  365. int MV = mv1 ? -60 : -28;
  366. qx = (s->mb_x << 6) + ((n == 1 || n == 3) ? 32 : 0);
  367. qy = (s->mb_y << 6) + ((n == 2 || n == 3) ? 32 : 0);
  368. X = (s->mb_width << 6) - 4;
  369. Y = (s->mb_height << 6) - 4;
  370. if (qx + px < MV) px = MV - qx;
  371. if (qy + py < MV) py = MV - qy;
  372. if (qx + px > X) px = X - qx;
  373. if (qy + py > Y) py = Y - qy;
  374. }
  375. if (!v->field_mode || s->pict_type != AV_PICTURE_TYPE_B) {
  376. /* Calculate hybrid prediction as specified in 8.3.5.3.5 (also 10.3.5.4.3.5) */
  377. hybridmv_thresh = 32;
  378. if (a_valid && c_valid) {
  379. if (is_intra[xy - wrap])
  380. sum = FFABS(px) + FFABS(py);
  381. else
  382. sum = FFABS(px - field_predA[0]) + FFABS(py - field_predA[1]);
  383. if (sum > hybridmv_thresh) {
  384. if (get_bits1(&s->gb)) { // read HYBRIDPRED bit
  385. px = field_predA[0];
  386. py = field_predA[1];
  387. } else {
  388. px = field_predC[0];
  389. py = field_predC[1];
  390. }
  391. } else {
  392. if (is_intra[xy - 1])
  393. sum = FFABS(px) + FFABS(py);
  394. else
  395. sum = FFABS(px - field_predC[0]) + FFABS(py - field_predC[1]);
  396. if (sum > hybridmv_thresh) {
  397. if (get_bits1(&s->gb)) {
  398. px = field_predA[0];
  399. py = field_predA[1];
  400. } else {
  401. px = field_predC[0];
  402. py = field_predC[1];
  403. }
  404. }
  405. }
  406. }
  407. }
  408. if (v->field_mode && v->numref)
  409. r_y >>= 1;
  410. if (v->field_mode && v->cur_field_type && v->ref_field_type[dir] == 0)
  411. y_bias = 1;
  412. /* store MV using signed modulus of MV range defined in 4.11 */
  413. 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;
  414. 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;
  415. if (mv1) { /* duplicate motion data for 1-MV block */
  416. s->current_picture.motion_val[dir][xy + 1 + v->blocks_off][0] = s->current_picture.motion_val[dir][xy + v->blocks_off][0];
  417. s->current_picture.motion_val[dir][xy + 1 + v->blocks_off][1] = s->current_picture.motion_val[dir][xy + v->blocks_off][1];
  418. s->current_picture.motion_val[dir][xy + wrap + v->blocks_off][0] = s->current_picture.motion_val[dir][xy + v->blocks_off][0];
  419. s->current_picture.motion_val[dir][xy + wrap + v->blocks_off][1] = s->current_picture.motion_val[dir][xy + v->blocks_off][1];
  420. s->current_picture.motion_val[dir][xy + wrap + 1 + v->blocks_off][0] = s->current_picture.motion_val[dir][xy + v->blocks_off][0];
  421. s->current_picture.motion_val[dir][xy + wrap + 1 + v->blocks_off][1] = s->current_picture.motion_val[dir][xy + v->blocks_off][1];
  422. v->mv_f[dir][xy + 1 + v->blocks_off] = v->mv_f[dir][xy + v->blocks_off];
  423. 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];
  424. }
  425. }
  426. /** Predict and set motion vector for interlaced frame picture MBs
  427. */
  428. void ff_vc1_pred_mv_intfr(VC1Context *v, int n, int dmv_x, int dmv_y,
  429. int mvn, int r_x, int r_y, uint8_t* is_intra, int dir)
  430. {
  431. MpegEncContext *s = &v->s;
  432. int xy, wrap, off = 0;
  433. int A[2], B[2], C[2];
  434. int px = 0, py = 0;
  435. int a_valid = 0, b_valid = 0, c_valid = 0;
  436. int field_a, field_b, field_c; // 0: same, 1: opposit
  437. int total_valid, num_samefield, num_oppfield;
  438. int pos_c, pos_b, n_adj;
  439. wrap = s->b8_stride;
  440. xy = s->block_index[n];
  441. if (s->mb_intra) {
  442. s->mv[0][n][0] = s->current_picture.motion_val[0][xy][0] = 0;
  443. s->mv[0][n][1] = s->current_picture.motion_val[0][xy][1] = 0;
  444. s->current_picture.motion_val[1][xy][0] = 0;
  445. s->current_picture.motion_val[1][xy][1] = 0;
  446. if (mvn == 1) { /* duplicate motion data for 1-MV block */
  447. s->current_picture.motion_val[0][xy + 1][0] = 0;
  448. s->current_picture.motion_val[0][xy + 1][1] = 0;
  449. s->current_picture.motion_val[0][xy + wrap][0] = 0;
  450. s->current_picture.motion_val[0][xy + wrap][1] = 0;
  451. s->current_picture.motion_val[0][xy + wrap + 1][0] = 0;
  452. s->current_picture.motion_val[0][xy + wrap + 1][1] = 0;
  453. v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
  454. s->current_picture.motion_val[1][xy + 1][0] = 0;
  455. s->current_picture.motion_val[1][xy + 1][1] = 0;
  456. s->current_picture.motion_val[1][xy + wrap][0] = 0;
  457. s->current_picture.motion_val[1][xy + wrap][1] = 0;
  458. s->current_picture.motion_val[1][xy + wrap + 1][0] = 0;
  459. s->current_picture.motion_val[1][xy + wrap + 1][1] = 0;
  460. }
  461. return;
  462. }
  463. off = ((n == 0) || (n == 1)) ? 1 : -1;
  464. /* predict A */
  465. if (s->mb_x || (n == 1) || (n == 3)) {
  466. if ((v->blk_mv_type[xy]) // current block (MB) has a field MV
  467. || (!v->blk_mv_type[xy] && !v->blk_mv_type[xy - 1])) { // or both have frame MV
  468. A[0] = s->current_picture.motion_val[dir][xy - 1][0];
  469. A[1] = s->current_picture.motion_val[dir][xy - 1][1];
  470. a_valid = 1;
  471. } else { // current block has frame mv and cand. has field MV (so average)
  472. A[0] = (s->current_picture.motion_val[dir][xy - 1][0]
  473. + s->current_picture.motion_val[dir][xy - 1 + off * wrap][0] + 1) >> 1;
  474. A[1] = (s->current_picture.motion_val[dir][xy - 1][1]
  475. + s->current_picture.motion_val[dir][xy - 1 + off * wrap][1] + 1) >> 1;
  476. a_valid = 1;
  477. }
  478. if (!(n & 1) && v->is_intra[s->mb_x - 1]) {
  479. a_valid = 0;
  480. A[0] = A[1] = 0;
  481. }
  482. } else
  483. A[0] = A[1] = 0;
  484. /* Predict B and C */
  485. B[0] = B[1] = C[0] = C[1] = 0;
  486. if (n == 0 || n == 1 || v->blk_mv_type[xy]) {
  487. if (!s->first_slice_line) {
  488. if (!v->is_intra[s->mb_x - s->mb_stride]) {
  489. b_valid = 1;
  490. n_adj = n | 2;
  491. pos_b = s->block_index[n_adj] - 2 * wrap;
  492. if (v->blk_mv_type[pos_b] && v->blk_mv_type[xy]) {
  493. n_adj = (n & 2) | (n & 1);
  494. }
  495. B[0] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap][0];
  496. B[1] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap][1];
  497. if (v->blk_mv_type[pos_b] && !v->blk_mv_type[xy]) {
  498. B[0] = (B[0] + s->current_picture.motion_val[dir][s->block_index[n_adj ^ 2] - 2 * wrap][0] + 1) >> 1;
  499. B[1] = (B[1] + s->current_picture.motion_val[dir][s->block_index[n_adj ^ 2] - 2 * wrap][1] + 1) >> 1;
  500. }
  501. }
  502. if (s->mb_width > 1) {
  503. if (!v->is_intra[s->mb_x - s->mb_stride + 1]) {
  504. c_valid = 1;
  505. n_adj = 2;
  506. pos_c = s->block_index[2] - 2 * wrap + 2;
  507. if (v->blk_mv_type[pos_c] && v->blk_mv_type[xy]) {
  508. n_adj = n & 2;
  509. }
  510. C[0] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap + 2][0];
  511. C[1] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap + 2][1];
  512. if (v->blk_mv_type[pos_c] && !v->blk_mv_type[xy]) {
  513. C[0] = (1 + C[0] + (s->current_picture.motion_val[dir][s->block_index[n_adj ^ 2] - 2 * wrap + 2][0])) >> 1;
  514. C[1] = (1 + C[1] + (s->current_picture.motion_val[dir][s->block_index[n_adj ^ 2] - 2 * wrap + 2][1])) >> 1;
  515. }
  516. if (s->mb_x == s->mb_width - 1) {
  517. if (!v->is_intra[s->mb_x - s->mb_stride - 1]) {
  518. c_valid = 1;
  519. n_adj = 3;
  520. pos_c = s->block_index[3] - 2 * wrap - 2;
  521. if (v->blk_mv_type[pos_c] && v->blk_mv_type[xy]) {
  522. n_adj = n | 1;
  523. }
  524. C[0] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap - 2][0];
  525. C[1] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap - 2][1];
  526. if (v->blk_mv_type[pos_c] && !v->blk_mv_type[xy]) {
  527. C[0] = (1 + C[0] + s->current_picture.motion_val[dir][s->block_index[1] - 2 * wrap - 2][0]) >> 1;
  528. C[1] = (1 + C[1] + s->current_picture.motion_val[dir][s->block_index[1] - 2 * wrap - 2][1]) >> 1;
  529. }
  530. } else
  531. c_valid = 0;
  532. }
  533. }
  534. }
  535. }
  536. } else {
  537. pos_b = s->block_index[1];
  538. b_valid = 1;
  539. B[0] = s->current_picture.motion_val[dir][pos_b][0];
  540. B[1] = s->current_picture.motion_val[dir][pos_b][1];
  541. pos_c = s->block_index[0];
  542. c_valid = 1;
  543. C[0] = s->current_picture.motion_val[dir][pos_c][0];
  544. C[1] = s->current_picture.motion_val[dir][pos_c][1];
  545. }
  546. total_valid = a_valid + b_valid + c_valid;
  547. // check if predictor A is out of bounds
  548. if (!s->mb_x && !(n == 1 || n == 3)) {
  549. A[0] = A[1] = 0;
  550. }
  551. // check if predictor B is out of bounds
  552. if ((s->first_slice_line && v->blk_mv_type[xy]) || (s->first_slice_line && !(n & 2))) {
  553. B[0] = B[1] = C[0] = C[1] = 0;
  554. }
  555. if (!v->blk_mv_type[xy]) {
  556. if (s->mb_width == 1) {
  557. px = B[0];
  558. py = B[1];
  559. } else {
  560. if (total_valid >= 2) {
  561. px = mid_pred(A[0], B[0], C[0]);
  562. py = mid_pred(A[1], B[1], C[1]);
  563. } else if (total_valid) {
  564. if (a_valid) { px = A[0]; py = A[1]; }
  565. else if (b_valid) { px = B[0]; py = B[1]; }
  566. else { px = C[0]; py = C[1]; }
  567. }
  568. }
  569. } else {
  570. if (a_valid)
  571. field_a = (A[1] & 4) ? 1 : 0;
  572. else
  573. field_a = 0;
  574. if (b_valid)
  575. field_b = (B[1] & 4) ? 1 : 0;
  576. else
  577. field_b = 0;
  578. if (c_valid)
  579. field_c = (C[1] & 4) ? 1 : 0;
  580. else
  581. field_c = 0;
  582. num_oppfield = field_a + field_b + field_c;
  583. num_samefield = total_valid - num_oppfield;
  584. if (total_valid == 3) {
  585. if ((num_samefield == 3) || (num_oppfield == 3)) {
  586. px = mid_pred(A[0], B[0], C[0]);
  587. py = mid_pred(A[1], B[1], C[1]);
  588. } else if (num_samefield >= num_oppfield) {
  589. /* take one MV from same field set depending on priority
  590. the check for B may not be necessary */
  591. px = !field_a ? A[0] : B[0];
  592. py = !field_a ? A[1] : B[1];
  593. } else {
  594. px = field_a ? A[0] : B[0];
  595. py = field_a ? A[1] : B[1];
  596. }
  597. } else if (total_valid == 2) {
  598. if (num_samefield >= num_oppfield) {
  599. if (!field_a && a_valid) {
  600. px = A[0];
  601. py = A[1];
  602. } else if (!field_b && b_valid) {
  603. px = B[0];
  604. py = B[1];
  605. } else /*if (c_valid)*/ {
  606. av_assert1(c_valid);
  607. px = C[0];
  608. py = C[1];
  609. }
  610. } else {
  611. if (field_a && a_valid) {
  612. px = A[0];
  613. py = A[1];
  614. } else /*if (field_b && b_valid)*/ {
  615. av_assert1(field_b && b_valid);
  616. px = B[0];
  617. py = B[1];
  618. }
  619. }
  620. } else if (total_valid == 1) {
  621. px = (a_valid) ? A[0] : ((b_valid) ? B[0] : C[0]);
  622. py = (a_valid) ? A[1] : ((b_valid) ? B[1] : C[1]);
  623. }
  624. }
  625. /* store MV using signed modulus of MV range defined in 4.11 */
  626. s->mv[dir][n][0] = s->current_picture.motion_val[dir][xy][0] = ((px + dmv_x + r_x) & ((r_x << 1) - 1)) - r_x;
  627. s->mv[dir][n][1] = s->current_picture.motion_val[dir][xy][1] = ((py + dmv_y + r_y) & ((r_y << 1) - 1)) - r_y;
  628. if (mvn == 1) { /* duplicate motion data for 1-MV block */
  629. s->current_picture.motion_val[dir][xy + 1 ][0] = s->current_picture.motion_val[dir][xy][0];
  630. s->current_picture.motion_val[dir][xy + 1 ][1] = s->current_picture.motion_val[dir][xy][1];
  631. s->current_picture.motion_val[dir][xy + wrap ][0] = s->current_picture.motion_val[dir][xy][0];
  632. s->current_picture.motion_val[dir][xy + wrap ][1] = s->current_picture.motion_val[dir][xy][1];
  633. s->current_picture.motion_val[dir][xy + wrap + 1][0] = s->current_picture.motion_val[dir][xy][0];
  634. s->current_picture.motion_val[dir][xy + wrap + 1][1] = s->current_picture.motion_val[dir][xy][1];
  635. } else if (mvn == 2) { /* duplicate motion data for 2-Field MV block */
  636. s->current_picture.motion_val[dir][xy + 1][0] = s->current_picture.motion_val[dir][xy][0];
  637. s->current_picture.motion_val[dir][xy + 1][1] = s->current_picture.motion_val[dir][xy][1];
  638. s->mv[dir][n + 1][0] = s->mv[dir][n][0];
  639. s->mv[dir][n + 1][1] = s->mv[dir][n][1];
  640. }
  641. }
  642. void ff_vc1_pred_b_mv(VC1Context *v, int dmv_x[2], int dmv_y[2],
  643. int direct, int mvtype)
  644. {
  645. MpegEncContext *s = &v->s;
  646. int xy, wrap, off = 0;
  647. int16_t *A, *B, *C;
  648. int px, py;
  649. int sum;
  650. int r_x, r_y;
  651. const uint8_t *is_intra = v->mb_type[0];
  652. av_assert0(!v->field_mode);
  653. r_x = v->range_x;
  654. r_y = v->range_y;
  655. /* scale MV difference to be quad-pel */
  656. if (!s->quarter_sample) {
  657. dmv_x[0] *= 2;
  658. dmv_y[0] *= 2;
  659. dmv_x[1] *= 2;
  660. dmv_y[1] *= 2;
  661. }
  662. wrap = s->b8_stride;
  663. xy = s->block_index[0];
  664. if (s->mb_intra) {
  665. s->current_picture.motion_val[0][xy][0] =
  666. s->current_picture.motion_val[0][xy][1] =
  667. s->current_picture.motion_val[1][xy][0] =
  668. s->current_picture.motion_val[1][xy][1] = 0;
  669. return;
  670. }
  671. if (direct && s->next_picture_ptr->field_picture)
  672. av_log(s->avctx, AV_LOG_WARNING, "Mixed frame/field direct mode not supported\n");
  673. s->mv[0][0][0] = scale_mv(s->next_picture.motion_val[1][xy][0], v->bfraction, 0, s->quarter_sample);
  674. s->mv[0][0][1] = scale_mv(s->next_picture.motion_val[1][xy][1], v->bfraction, 0, s->quarter_sample);
  675. s->mv[1][0][0] = scale_mv(s->next_picture.motion_val[1][xy][0], v->bfraction, 1, s->quarter_sample);
  676. s->mv[1][0][1] = scale_mv(s->next_picture.motion_val[1][xy][1], v->bfraction, 1, s->quarter_sample);
  677. /* Pullback predicted motion vectors as specified in 8.4.5.4 */
  678. 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));
  679. 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));
  680. 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));
  681. 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));
  682. if (direct) {
  683. s->current_picture.motion_val[0][xy][0] = s->mv[0][0][0];
  684. s->current_picture.motion_val[0][xy][1] = s->mv[0][0][1];
  685. s->current_picture.motion_val[1][xy][0] = s->mv[1][0][0];
  686. s->current_picture.motion_val[1][xy][1] = s->mv[1][0][1];
  687. return;
  688. }
  689. if ((mvtype == BMV_TYPE_FORWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
  690. C = s->current_picture.motion_val[0][xy - 2];
  691. A = s->current_picture.motion_val[0][xy - wrap * 2];
  692. off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
  693. B = s->current_picture.motion_val[0][xy - wrap * 2 + off];
  694. if (!s->mb_x) C[0] = C[1] = 0;
  695. if (!s->first_slice_line) { // predictor A is not out of bounds
  696. if (s->mb_width == 1) {
  697. px = A[0];
  698. py = A[1];
  699. } else {
  700. px = mid_pred(A[0], B[0], C[0]);
  701. py = mid_pred(A[1], B[1], C[1]);
  702. }
  703. } else if (s->mb_x) { // predictor C is not out of bounds
  704. px = C[0];
  705. py = C[1];
  706. } else {
  707. px = py = 0;
  708. }
  709. /* Pullback MV as specified in 8.3.5.3.4 */
  710. {
  711. int qx, qy, X, Y;
  712. int sh = v->profile < PROFILE_ADVANCED ? 5 : 6;
  713. int MV = 4 - (1 << sh);
  714. qx = (s->mb_x << sh);
  715. qy = (s->mb_y << sh);
  716. X = (s->mb_width << sh) - 4;
  717. Y = (s->mb_height << sh) - 4;
  718. if (qx + px < MV) px = MV - qx;
  719. if (qy + py < MV) py = MV - qy;
  720. if (qx + px > X) px = X - qx;
  721. if (qy + py > Y) py = Y - qy;
  722. }
  723. /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
  724. if (0 && !s->first_slice_line && s->mb_x) {
  725. if (is_intra[xy - wrap])
  726. sum = FFABS(px) + FFABS(py);
  727. else
  728. sum = FFABS(px - A[0]) + FFABS(py - A[1]);
  729. if (sum > 32) {
  730. if (get_bits1(&s->gb)) {
  731. px = A[0];
  732. py = A[1];
  733. } else {
  734. px = C[0];
  735. py = C[1];
  736. }
  737. } else {
  738. if (is_intra[xy - 2])
  739. sum = FFABS(px) + FFABS(py);
  740. else
  741. sum = FFABS(px - C[0]) + FFABS(py - C[1]);
  742. if (sum > 32) {
  743. if (get_bits1(&s->gb)) {
  744. px = A[0];
  745. py = A[1];
  746. } else {
  747. px = C[0];
  748. py = C[1];
  749. }
  750. }
  751. }
  752. }
  753. /* store MV using signed modulus of MV range defined in 4.11 */
  754. s->mv[0][0][0] = ((px + dmv_x[0] + r_x) & ((r_x << 1) - 1)) - r_x;
  755. s->mv[0][0][1] = ((py + dmv_y[0] + r_y) & ((r_y << 1) - 1)) - r_y;
  756. }
  757. if ((mvtype == BMV_TYPE_BACKWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
  758. C = s->current_picture.motion_val[1][xy - 2];
  759. A = s->current_picture.motion_val[1][xy - wrap * 2];
  760. off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
  761. B = s->current_picture.motion_val[1][xy - wrap * 2 + off];
  762. if (!s->mb_x)
  763. C[0] = C[1] = 0;
  764. if (!s->first_slice_line) { // predictor A is not out of bounds
  765. if (s->mb_width == 1) {
  766. px = A[0];
  767. py = A[1];
  768. } else {
  769. px = mid_pred(A[0], B[0], C[0]);
  770. py = mid_pred(A[1], B[1], C[1]);
  771. }
  772. } else if (s->mb_x) { // predictor C is not out of bounds
  773. px = C[0];
  774. py = C[1];
  775. } else {
  776. px = py = 0;
  777. }
  778. /* Pullback MV as specified in 8.3.5.3.4 */
  779. {
  780. int qx, qy, X, Y;
  781. int sh = v->profile < PROFILE_ADVANCED ? 5 : 6;
  782. int MV = 4 - (1 << sh);
  783. qx = (s->mb_x << sh);
  784. qy = (s->mb_y << sh);
  785. X = (s->mb_width << sh) - 4;
  786. Y = (s->mb_height << sh) - 4;
  787. if (qx + px < MV) px = MV - qx;
  788. if (qy + py < MV) py = MV - qy;
  789. if (qx + px > X) px = X - qx;
  790. if (qy + py > Y) py = Y - qy;
  791. }
  792. /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
  793. if (0 && !s->first_slice_line && s->mb_x) {
  794. if (is_intra[xy - wrap])
  795. sum = FFABS(px) + FFABS(py);
  796. else
  797. sum = FFABS(px - A[0]) + FFABS(py - A[1]);
  798. if (sum > 32) {
  799. if (get_bits1(&s->gb)) {
  800. px = A[0];
  801. py = A[1];
  802. } else {
  803. px = C[0];
  804. py = C[1];
  805. }
  806. } else {
  807. if (is_intra[xy - 2])
  808. sum = FFABS(px) + FFABS(py);
  809. else
  810. sum = FFABS(px - C[0]) + FFABS(py - C[1]);
  811. if (sum > 32) {
  812. if (get_bits1(&s->gb)) {
  813. px = A[0];
  814. py = A[1];
  815. } else {
  816. px = C[0];
  817. py = C[1];
  818. }
  819. }
  820. }
  821. }
  822. /* store MV using signed modulus of MV range defined in 4.11 */
  823. s->mv[1][0][0] = ((px + dmv_x[1] + r_x) & ((r_x << 1) - 1)) - r_x;
  824. s->mv[1][0][1] = ((py + dmv_y[1] + r_y) & ((r_y << 1) - 1)) - r_y;
  825. }
  826. s->current_picture.motion_val[0][xy][0] = s->mv[0][0][0];
  827. s->current_picture.motion_val[0][xy][1] = s->mv[0][0][1];
  828. s->current_picture.motion_val[1][xy][0] = s->mv[1][0][0];
  829. s->current_picture.motion_val[1][xy][1] = s->mv[1][0][1];
  830. }
  831. void ff_vc1_pred_b_mv_intfi(VC1Context *v, int n, int *dmv_x, int *dmv_y,
  832. int mv1, int *pred_flag)
  833. {
  834. int dir = (v->bmvtype == BMV_TYPE_BACKWARD) ? 1 : 0;
  835. MpegEncContext *s = &v->s;
  836. int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
  837. if (v->bmvtype == BMV_TYPE_DIRECT) {
  838. int total_opp, k, f;
  839. if (s->next_picture.mb_type[mb_pos + v->mb_off] != MB_TYPE_INTRA) {
  840. s->mv[0][0][0] = scale_mv(s->next_picture.motion_val[1][s->block_index[0] + v->blocks_off][0],
  841. v->bfraction, 0, s->quarter_sample);
  842. s->mv[0][0][1] = scale_mv(s->next_picture.motion_val[1][s->block_index[0] + v->blocks_off][1],
  843. v->bfraction, 0, s->quarter_sample);
  844. s->mv[1][0][0] = scale_mv(s->next_picture.motion_val[1][s->block_index[0] + v->blocks_off][0],
  845. v->bfraction, 1, s->quarter_sample);
  846. s->mv[1][0][1] = scale_mv(s->next_picture.motion_val[1][s->block_index[0] + v->blocks_off][1],
  847. v->bfraction, 1, s->quarter_sample);
  848. total_opp = v->mv_f_next[0][s->block_index[0] + v->blocks_off]
  849. + v->mv_f_next[0][s->block_index[1] + v->blocks_off]
  850. + v->mv_f_next[0][s->block_index[2] + v->blocks_off]
  851. + v->mv_f_next[0][s->block_index[3] + v->blocks_off];
  852. f = (total_opp > 2) ? 1 : 0;
  853. } else {
  854. s->mv[0][0][0] = s->mv[0][0][1] = 0;
  855. s->mv[1][0][0] = s->mv[1][0][1] = 0;
  856. f = 0;
  857. }
  858. v->ref_field_type[0] = v->ref_field_type[1] = v->cur_field_type ^ f;
  859. for (k = 0; k < 4; k++) {
  860. s->current_picture.motion_val[0][s->block_index[k] + v->blocks_off][0] = s->mv[0][0][0];
  861. s->current_picture.motion_val[0][s->block_index[k] + v->blocks_off][1] = s->mv[0][0][1];
  862. s->current_picture.motion_val[1][s->block_index[k] + v->blocks_off][0] = s->mv[1][0][0];
  863. s->current_picture.motion_val[1][s->block_index[k] + v->blocks_off][1] = s->mv[1][0][1];
  864. v->mv_f[0][s->block_index[k] + v->blocks_off] = f;
  865. v->mv_f[1][s->block_index[k] + v->blocks_off] = f;
  866. }
  867. return;
  868. }
  869. if (v->bmvtype == BMV_TYPE_INTERPOLATED) {
  870. ff_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);
  871. ff_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);
  872. return;
  873. }
  874. if (dir) { // backward
  875. ff_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);
  876. if (n == 3 || mv1) {
  877. ff_vc1_pred_mv(v, 0, dmv_x[0], dmv_y[0], 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
  878. }
  879. } else { // forward
  880. ff_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);
  881. if (n == 3 || mv1) {
  882. ff_vc1_pred_mv(v, 0, dmv_x[1], dmv_y[1], 1, v->range_x, v->range_y, v->mb_type[0], 0, 1);
  883. }
  884. }
  885. }