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.

948 lines
38KB

  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) * (1 << 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) * (1 << hpel);
  175. else
  176. n = scaleforopp_x(v, n) * (1 << hpel);
  177. return n;
  178. }
  179. if (v->s.pict_type != AV_PICTURE_TYPE_B)
  180. refdist = v->refdist;
  181. else
  182. refdist = dir ? v->brfd : v->frfd;
  183. refdist = FFMIN(refdist, 3);
  184. scaleopp = ff_vc1_field_mvpred_scales[dir ^ v->second_field][0][refdist];
  185. n = (n * scaleopp >> 8) * (1 << hpel);
  186. return n;
  187. }
  188. /** Predict and set motion vector
  189. */
  190. void ff_vc1_pred_mv(VC1Context *v, int n, int dmv_x, int dmv_y,
  191. int mv1, int r_x, int r_y, uint8_t* is_intra,
  192. int pred_flag, int dir)
  193. {
  194. MpegEncContext *s = &v->s;
  195. int xy, wrap, off = 0;
  196. int16_t *A, *B, *C;
  197. int px, py;
  198. int sum;
  199. int mixedmv_pic, num_samefield = 0, num_oppfield = 0;
  200. int opposite, a_f, b_f, c_f;
  201. int16_t field_predA[2];
  202. int16_t field_predB[2];
  203. int16_t field_predC[2];
  204. int a_valid, b_valid, c_valid;
  205. int hybridmv_thresh, y_bias = 0;
  206. if (v->mv_mode == MV_PMODE_MIXED_MV ||
  207. ((v->mv_mode == MV_PMODE_INTENSITY_COMP) && (v->mv_mode2 == MV_PMODE_MIXED_MV)))
  208. mixedmv_pic = 1;
  209. else
  210. mixedmv_pic = 0;
  211. /* scale MV difference to be quad-pel */
  212. if (!s->quarter_sample) {
  213. dmv_x *= 2;
  214. dmv_y *= 2;
  215. }
  216. wrap = s->b8_stride;
  217. xy = s->block_index[n];
  218. if (s->mb_intra) {
  219. s->mv[0][n][0] = s->current_picture.motion_val[0][xy + v->blocks_off][0] = 0;
  220. s->mv[0][n][1] = s->current_picture.motion_val[0][xy + v->blocks_off][1] = 0;
  221. s->current_picture.motion_val[1][xy + v->blocks_off][0] = 0;
  222. s->current_picture.motion_val[1][xy + v->blocks_off][1] = 0;
  223. if (mv1) { /* duplicate motion data for 1-MV block */
  224. s->current_picture.motion_val[0][xy + 1 + v->blocks_off][0] = 0;
  225. s->current_picture.motion_val[0][xy + 1 + v->blocks_off][1] = 0;
  226. s->current_picture.motion_val[0][xy + wrap + v->blocks_off][0] = 0;
  227. s->current_picture.motion_val[0][xy + wrap + v->blocks_off][1] = 0;
  228. s->current_picture.motion_val[0][xy + wrap + 1 + v->blocks_off][0] = 0;
  229. s->current_picture.motion_val[0][xy + wrap + 1 + v->blocks_off][1] = 0;
  230. v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
  231. s->current_picture.motion_val[1][xy + 1 + v->blocks_off][0] = 0;
  232. s->current_picture.motion_val[1][xy + 1 + v->blocks_off][1] = 0;
  233. s->current_picture.motion_val[1][xy + wrap + v->blocks_off][0] = 0;
  234. s->current_picture.motion_val[1][xy + wrap + v->blocks_off][1] = 0;
  235. s->current_picture.motion_val[1][xy + wrap + 1 + v->blocks_off][0] = 0;
  236. s->current_picture.motion_val[1][xy + wrap + 1 + v->blocks_off][1] = 0;
  237. }
  238. return;
  239. }
  240. a_valid = !s->first_slice_line || (n == 2 || n == 3);
  241. b_valid = a_valid;
  242. c_valid = s->mb_x || (n == 1 || n == 3);
  243. if (mv1) {
  244. if (v->field_mode && mixedmv_pic)
  245. off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
  246. else
  247. off = (s->mb_x == (s->mb_width - 1)) ? -1 : 2;
  248. b_valid = b_valid && s->mb_width > 1;
  249. } else {
  250. //in 4-MV mode different blocks have different B predictor position
  251. switch (n) {
  252. case 0:
  253. if (v->res_rtm_flag)
  254. off = s->mb_x ? -1 : 1;
  255. else
  256. off = s->mb_x ? -1 : 2 * s->mb_width - wrap - 1;
  257. break;
  258. case 1:
  259. off = (s->mb_x == (s->mb_width - 1)) ? -1 : 1;
  260. break;
  261. case 2:
  262. off = 1;
  263. break;
  264. case 3:
  265. off = -1;
  266. }
  267. if (v->field_mode && s->mb_width == 1)
  268. b_valid = b_valid && c_valid;
  269. }
  270. if (v->field_mode) {
  271. a_valid = a_valid && !is_intra[xy - wrap];
  272. b_valid = b_valid && !is_intra[xy - wrap + off];
  273. c_valid = c_valid && !is_intra[xy - 1];
  274. }
  275. if (a_valid) {
  276. A = s->current_picture.motion_val[dir][xy - wrap + v->blocks_off];
  277. a_f = v->mv_f[dir][xy - wrap + v->blocks_off];
  278. num_oppfield += a_f;
  279. num_samefield += 1 - a_f;
  280. field_predA[0] = A[0];
  281. field_predA[1] = A[1];
  282. } else {
  283. field_predA[0] = field_predA[1] = 0;
  284. a_f = 0;
  285. }
  286. if (b_valid) {
  287. B = s->current_picture.motion_val[dir][xy - wrap + off + v->blocks_off];
  288. b_f = v->mv_f[dir][xy - wrap + off + v->blocks_off];
  289. num_oppfield += b_f;
  290. num_samefield += 1 - b_f;
  291. field_predB[0] = B[0];
  292. field_predB[1] = B[1];
  293. } else {
  294. field_predB[0] = field_predB[1] = 0;
  295. b_f = 0;
  296. }
  297. if (c_valid) {
  298. C = s->current_picture.motion_val[dir][xy - 1 + v->blocks_off];
  299. c_f = v->mv_f[dir][xy - 1 + v->blocks_off];
  300. num_oppfield += c_f;
  301. num_samefield += 1 - c_f;
  302. field_predC[0] = C[0];
  303. field_predC[1] = C[1];
  304. } else {
  305. field_predC[0] = field_predC[1] = 0;
  306. c_f = 0;
  307. }
  308. if (v->field_mode) {
  309. if (!v->numref)
  310. // REFFIELD determines if the last field or the second-last field is
  311. // to be used as reference
  312. opposite = 1 - v->reffield;
  313. else {
  314. if (num_samefield <= num_oppfield)
  315. opposite = 1 - pred_flag;
  316. else
  317. opposite = pred_flag;
  318. }
  319. } else
  320. opposite = 0;
  321. if (opposite) {
  322. v->mv_f[dir][xy + v->blocks_off] = 1;
  323. v->ref_field_type[dir] = !v->cur_field_type;
  324. if (a_valid && !a_f) {
  325. field_predA[0] = scaleforopp(v, field_predA[0], 0, dir);
  326. field_predA[1] = scaleforopp(v, field_predA[1], 1, dir);
  327. }
  328. if (b_valid && !b_f) {
  329. field_predB[0] = scaleforopp(v, field_predB[0], 0, dir);
  330. field_predB[1] = scaleforopp(v, field_predB[1], 1, dir);
  331. }
  332. if (c_valid && !c_f) {
  333. field_predC[0] = scaleforopp(v, field_predC[0], 0, dir);
  334. field_predC[1] = scaleforopp(v, field_predC[1], 1, dir);
  335. }
  336. } else {
  337. v->mv_f[dir][xy + v->blocks_off] = 0;
  338. v->ref_field_type[dir] = v->cur_field_type;
  339. if (a_valid && a_f) {
  340. field_predA[0] = scaleforsame(v, n, field_predA[0], 0, dir);
  341. field_predA[1] = scaleforsame(v, n, field_predA[1], 1, dir);
  342. }
  343. if (b_valid && b_f) {
  344. field_predB[0] = scaleforsame(v, n, field_predB[0], 0, dir);
  345. field_predB[1] = scaleforsame(v, n, field_predB[1], 1, dir);
  346. }
  347. if (c_valid && c_f) {
  348. field_predC[0] = scaleforsame(v, n, field_predC[0], 0, dir);
  349. field_predC[1] = scaleforsame(v, n, field_predC[1], 1, dir);
  350. }
  351. }
  352. if (a_valid) {
  353. px = field_predA[0];
  354. py = field_predA[1];
  355. } else if (c_valid) {
  356. px = field_predC[0];
  357. py = field_predC[1];
  358. } else if (b_valid) {
  359. px = field_predB[0];
  360. py = field_predB[1];
  361. } else {
  362. px = 0;
  363. py = 0;
  364. }
  365. if (num_samefield + num_oppfield > 1) {
  366. px = mid_pred(field_predA[0], field_predB[0], field_predC[0]);
  367. py = mid_pred(field_predA[1], field_predB[1], field_predC[1]);
  368. }
  369. /* Pullback MV as specified in 8.3.5.3.4 */
  370. if (!v->field_mode) {
  371. int qx, qy, X, Y;
  372. int MV = mv1 ? -60 : -28;
  373. qx = (s->mb_x << 6) + ((n == 1 || n == 3) ? 32 : 0);
  374. qy = (s->mb_y << 6) + ((n == 2 || n == 3) ? 32 : 0);
  375. X = (s->mb_width << 6) - 4;
  376. Y = (s->mb_height << 6) - 4;
  377. if (qx + px < MV) px = MV - qx;
  378. if (qy + py < MV) py = MV - qy;
  379. if (qx + px > X) px = X - qx;
  380. if (qy + py > Y) py = Y - qy;
  381. }
  382. if (!v->field_mode || s->pict_type != AV_PICTURE_TYPE_B) {
  383. /* Calculate hybrid prediction as specified in 8.3.5.3.5 (also 10.3.5.4.3.5) */
  384. hybridmv_thresh = 32;
  385. if (a_valid && c_valid) {
  386. if (is_intra[xy - wrap])
  387. sum = FFABS(px) + FFABS(py);
  388. else
  389. sum = FFABS(px - field_predA[0]) + FFABS(py - field_predA[1]);
  390. if (sum > hybridmv_thresh) {
  391. if (get_bits1(&s->gb)) { // read HYBRIDPRED bit
  392. px = field_predA[0];
  393. py = field_predA[1];
  394. } else {
  395. px = field_predC[0];
  396. py = field_predC[1];
  397. }
  398. } else {
  399. if (is_intra[xy - 1])
  400. sum = FFABS(px) + FFABS(py);
  401. else
  402. sum = FFABS(px - field_predC[0]) + FFABS(py - field_predC[1]);
  403. if (sum > hybridmv_thresh) {
  404. if (get_bits1(&s->gb)) {
  405. px = field_predA[0];
  406. py = field_predA[1];
  407. } else {
  408. px = field_predC[0];
  409. py = field_predC[1];
  410. }
  411. }
  412. }
  413. }
  414. }
  415. if (v->field_mode && v->numref)
  416. r_y >>= 1;
  417. if (v->field_mode && v->cur_field_type && v->ref_field_type[dir] == 0)
  418. y_bias = 1;
  419. /* store MV using signed modulus of MV range defined in 4.11 */
  420. 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;
  421. 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;
  422. if (mv1) { /* duplicate motion data for 1-MV block */
  423. s->current_picture.motion_val[dir][xy + 1 + v->blocks_off][0] = s->current_picture.motion_val[dir][xy + v->blocks_off][0];
  424. s->current_picture.motion_val[dir][xy + 1 + v->blocks_off][1] = s->current_picture.motion_val[dir][xy + v->blocks_off][1];
  425. s->current_picture.motion_val[dir][xy + wrap + v->blocks_off][0] = s->current_picture.motion_val[dir][xy + v->blocks_off][0];
  426. s->current_picture.motion_val[dir][xy + wrap + v->blocks_off][1] = s->current_picture.motion_val[dir][xy + v->blocks_off][1];
  427. s->current_picture.motion_val[dir][xy + wrap + 1 + v->blocks_off][0] = s->current_picture.motion_val[dir][xy + v->blocks_off][0];
  428. s->current_picture.motion_val[dir][xy + wrap + 1 + v->blocks_off][1] = s->current_picture.motion_val[dir][xy + v->blocks_off][1];
  429. v->mv_f[dir][xy + 1 + v->blocks_off] = v->mv_f[dir][xy + v->blocks_off];
  430. 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];
  431. }
  432. }
  433. /** Predict and set motion vector for interlaced frame picture MBs
  434. */
  435. void ff_vc1_pred_mv_intfr(VC1Context *v, int n, int dmv_x, int dmv_y,
  436. int mvn, int r_x, int r_y, uint8_t* is_intra, int dir)
  437. {
  438. MpegEncContext *s = &v->s;
  439. int xy, wrap, off = 0;
  440. int A[2], B[2], C[2];
  441. int px = 0, py = 0;
  442. int a_valid = 0, b_valid = 0, c_valid = 0;
  443. int field_a, field_b, field_c; // 0: same, 1: opposite
  444. int total_valid, num_samefield, num_oppfield;
  445. int pos_c, pos_b, n_adj;
  446. wrap = s->b8_stride;
  447. xy = s->block_index[n];
  448. if (s->mb_intra) {
  449. s->mv[0][n][0] = s->current_picture.motion_val[0][xy][0] = 0;
  450. s->mv[0][n][1] = s->current_picture.motion_val[0][xy][1] = 0;
  451. s->current_picture.motion_val[1][xy][0] = 0;
  452. s->current_picture.motion_val[1][xy][1] = 0;
  453. if (mvn == 1) { /* duplicate motion data for 1-MV block */
  454. s->current_picture.motion_val[0][xy + 1][0] = 0;
  455. s->current_picture.motion_val[0][xy + 1][1] = 0;
  456. s->current_picture.motion_val[0][xy + wrap][0] = 0;
  457. s->current_picture.motion_val[0][xy + wrap][1] = 0;
  458. s->current_picture.motion_val[0][xy + wrap + 1][0] = 0;
  459. s->current_picture.motion_val[0][xy + wrap + 1][1] = 0;
  460. v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
  461. s->current_picture.motion_val[1][xy + 1][0] = 0;
  462. s->current_picture.motion_val[1][xy + 1][1] = 0;
  463. s->current_picture.motion_val[1][xy + wrap][0] = 0;
  464. s->current_picture.motion_val[1][xy + wrap][1] = 0;
  465. s->current_picture.motion_val[1][xy + wrap + 1][0] = 0;
  466. s->current_picture.motion_val[1][xy + wrap + 1][1] = 0;
  467. }
  468. return;
  469. }
  470. off = ((n == 0) || (n == 1)) ? 1 : -1;
  471. /* predict A */
  472. if (s->mb_x || (n == 1) || (n == 3)) {
  473. if ((v->blk_mv_type[xy]) // current block (MB) has a field MV
  474. || (!v->blk_mv_type[xy] && !v->blk_mv_type[xy - 1])) { // or both have frame MV
  475. A[0] = s->current_picture.motion_val[dir][xy - 1][0];
  476. A[1] = s->current_picture.motion_val[dir][xy - 1][1];
  477. a_valid = 1;
  478. } else { // current block has frame mv and cand. has field MV (so average)
  479. A[0] = (s->current_picture.motion_val[dir][xy - 1][0]
  480. + s->current_picture.motion_val[dir][xy - 1 + off * wrap][0] + 1) >> 1;
  481. A[1] = (s->current_picture.motion_val[dir][xy - 1][1]
  482. + s->current_picture.motion_val[dir][xy - 1 + off * wrap][1] + 1) >> 1;
  483. a_valid = 1;
  484. }
  485. if (!(n & 1) && v->is_intra[s->mb_x - 1]) {
  486. a_valid = 0;
  487. A[0] = A[1] = 0;
  488. }
  489. } else
  490. A[0] = A[1] = 0;
  491. /* Predict B and C */
  492. B[0] = B[1] = C[0] = C[1] = 0;
  493. if (n == 0 || n == 1 || v->blk_mv_type[xy]) {
  494. if (!s->first_slice_line) {
  495. if (!v->is_intra[s->mb_x - s->mb_stride]) {
  496. b_valid = 1;
  497. n_adj = n | 2;
  498. pos_b = s->block_index[n_adj] - 2 * wrap;
  499. if (v->blk_mv_type[pos_b] && v->blk_mv_type[xy]) {
  500. n_adj = (n & 2) | (n & 1);
  501. }
  502. B[0] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap][0];
  503. B[1] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap][1];
  504. if (v->blk_mv_type[pos_b] && !v->blk_mv_type[xy]) {
  505. B[0] = (B[0] + s->current_picture.motion_val[dir][s->block_index[n_adj ^ 2] - 2 * wrap][0] + 1) >> 1;
  506. B[1] = (B[1] + s->current_picture.motion_val[dir][s->block_index[n_adj ^ 2] - 2 * wrap][1] + 1) >> 1;
  507. }
  508. }
  509. if (s->mb_width > 1) {
  510. if (!v->is_intra[s->mb_x - s->mb_stride + 1]) {
  511. c_valid = 1;
  512. n_adj = 2;
  513. pos_c = s->block_index[2] - 2 * wrap + 2;
  514. if (v->blk_mv_type[pos_c] && v->blk_mv_type[xy]) {
  515. n_adj = n & 2;
  516. }
  517. C[0] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap + 2][0];
  518. C[1] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap + 2][1];
  519. if (v->blk_mv_type[pos_c] && !v->blk_mv_type[xy]) {
  520. C[0] = (1 + C[0] + (s->current_picture.motion_val[dir][s->block_index[n_adj ^ 2] - 2 * wrap + 2][0])) >> 1;
  521. C[1] = (1 + C[1] + (s->current_picture.motion_val[dir][s->block_index[n_adj ^ 2] - 2 * wrap + 2][1])) >> 1;
  522. }
  523. if (s->mb_x == s->mb_width - 1) {
  524. if (!v->is_intra[s->mb_x - s->mb_stride - 1]) {
  525. c_valid = 1;
  526. n_adj = 3;
  527. pos_c = s->block_index[3] - 2 * wrap - 2;
  528. if (v->blk_mv_type[pos_c] && v->blk_mv_type[xy]) {
  529. n_adj = n | 1;
  530. }
  531. C[0] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap - 2][0];
  532. C[1] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap - 2][1];
  533. if (v->blk_mv_type[pos_c] && !v->blk_mv_type[xy]) {
  534. C[0] = (1 + C[0] + s->current_picture.motion_val[dir][s->block_index[1] - 2 * wrap - 2][0]) >> 1;
  535. C[1] = (1 + C[1] + s->current_picture.motion_val[dir][s->block_index[1] - 2 * wrap - 2][1]) >> 1;
  536. }
  537. } else
  538. c_valid = 0;
  539. }
  540. }
  541. }
  542. }
  543. } else {
  544. pos_b = s->block_index[1];
  545. b_valid = 1;
  546. B[0] = s->current_picture.motion_val[dir][pos_b][0];
  547. B[1] = s->current_picture.motion_val[dir][pos_b][1];
  548. pos_c = s->block_index[0];
  549. c_valid = 1;
  550. C[0] = s->current_picture.motion_val[dir][pos_c][0];
  551. C[1] = s->current_picture.motion_val[dir][pos_c][1];
  552. }
  553. total_valid = a_valid + b_valid + c_valid;
  554. // check if predictor A is out of bounds
  555. if (!s->mb_x && !(n == 1 || n == 3)) {
  556. A[0] = A[1] = 0;
  557. }
  558. // check if predictor B is out of bounds
  559. if ((s->first_slice_line && v->blk_mv_type[xy]) || (s->first_slice_line && !(n & 2))) {
  560. B[0] = B[1] = C[0] = C[1] = 0;
  561. }
  562. if (!v->blk_mv_type[xy]) {
  563. if (s->mb_width == 1) {
  564. px = B[0];
  565. py = B[1];
  566. } else {
  567. if (total_valid >= 2) {
  568. px = mid_pred(A[0], B[0], C[0]);
  569. py = mid_pred(A[1], B[1], C[1]);
  570. } else if (total_valid) {
  571. if (a_valid) { px = A[0]; py = A[1]; }
  572. else if (b_valid) { px = B[0]; py = B[1]; }
  573. else { px = C[0]; py = C[1]; }
  574. }
  575. }
  576. } else {
  577. if (a_valid)
  578. field_a = (A[1] & 4) ? 1 : 0;
  579. else
  580. field_a = 0;
  581. if (b_valid)
  582. field_b = (B[1] & 4) ? 1 : 0;
  583. else
  584. field_b = 0;
  585. if (c_valid)
  586. field_c = (C[1] & 4) ? 1 : 0;
  587. else
  588. field_c = 0;
  589. num_oppfield = field_a + field_b + field_c;
  590. num_samefield = total_valid - num_oppfield;
  591. if (total_valid == 3) {
  592. if ((num_samefield == 3) || (num_oppfield == 3)) {
  593. px = mid_pred(A[0], B[0], C[0]);
  594. py = mid_pred(A[1], B[1], C[1]);
  595. } else if (num_samefield >= num_oppfield) {
  596. /* take one MV from same field set depending on priority
  597. the check for B may not be necessary */
  598. px = !field_a ? A[0] : B[0];
  599. py = !field_a ? A[1] : B[1];
  600. } else {
  601. px = field_a ? A[0] : B[0];
  602. py = field_a ? A[1] : B[1];
  603. }
  604. } else if (total_valid == 2) {
  605. if (num_samefield >= num_oppfield) {
  606. if (!field_a && a_valid) {
  607. px = A[0];
  608. py = A[1];
  609. } else if (!field_b && b_valid) {
  610. px = B[0];
  611. py = B[1];
  612. } else /*if (c_valid)*/ {
  613. av_assert1(c_valid);
  614. px = C[0];
  615. py = C[1];
  616. }
  617. } else {
  618. if (field_a && a_valid) {
  619. px = A[0];
  620. py = A[1];
  621. } else /*if (field_b && b_valid)*/ {
  622. av_assert1(field_b && b_valid);
  623. px = B[0];
  624. py = B[1];
  625. }
  626. }
  627. } else if (total_valid == 1) {
  628. px = (a_valid) ? A[0] : ((b_valid) ? B[0] : C[0]);
  629. py = (a_valid) ? A[1] : ((b_valid) ? B[1] : C[1]);
  630. }
  631. }
  632. /* store MV using signed modulus of MV range defined in 4.11 */
  633. s->mv[dir][n][0] = s->current_picture.motion_val[dir][xy][0] = ((px + dmv_x + r_x) & ((r_x << 1) - 1)) - r_x;
  634. s->mv[dir][n][1] = s->current_picture.motion_val[dir][xy][1] = ((py + dmv_y + r_y) & ((r_y << 1) - 1)) - r_y;
  635. if (mvn == 1) { /* duplicate motion data for 1-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->current_picture.motion_val[dir][xy + wrap ][0] = s->current_picture.motion_val[dir][xy][0];
  639. s->current_picture.motion_val[dir][xy + wrap ][1] = s->current_picture.motion_val[dir][xy][1];
  640. s->current_picture.motion_val[dir][xy + wrap + 1][0] = s->current_picture.motion_val[dir][xy][0];
  641. s->current_picture.motion_val[dir][xy + wrap + 1][1] = s->current_picture.motion_val[dir][xy][1];
  642. } else if (mvn == 2) { /* duplicate motion data for 2-Field MV block */
  643. s->current_picture.motion_val[dir][xy + 1][0] = s->current_picture.motion_val[dir][xy][0];
  644. s->current_picture.motion_val[dir][xy + 1][1] = s->current_picture.motion_val[dir][xy][1];
  645. s->mv[dir][n + 1][0] = s->mv[dir][n][0];
  646. s->mv[dir][n + 1][1] = s->mv[dir][n][1];
  647. }
  648. }
  649. void ff_vc1_pred_b_mv(VC1Context *v, int dmv_x[2], int dmv_y[2],
  650. int direct, int mvtype)
  651. {
  652. MpegEncContext *s = &v->s;
  653. int xy, wrap, off = 0;
  654. int16_t *A, *B, *C;
  655. int px, py;
  656. int sum;
  657. int r_x, r_y;
  658. const uint8_t *is_intra = v->mb_type[0];
  659. av_assert0(!v->field_mode);
  660. r_x = v->range_x;
  661. r_y = v->range_y;
  662. /* scale MV difference to be quad-pel */
  663. if (!s->quarter_sample) {
  664. dmv_x[0] *= 2;
  665. dmv_y[0] *= 2;
  666. dmv_x[1] *= 2;
  667. dmv_y[1] *= 2;
  668. }
  669. wrap = s->b8_stride;
  670. xy = s->block_index[0];
  671. if (s->mb_intra) {
  672. s->current_picture.motion_val[0][xy][0] =
  673. s->current_picture.motion_val[0][xy][1] =
  674. s->current_picture.motion_val[1][xy][0] =
  675. s->current_picture.motion_val[1][xy][1] = 0;
  676. return;
  677. }
  678. if (direct && s->next_picture_ptr->field_picture)
  679. av_log(s->avctx, AV_LOG_WARNING, "Mixed frame/field direct mode not supported\n");
  680. s->mv[0][0][0] = scale_mv(s->next_picture.motion_val[1][xy][0], v->bfraction, 0, s->quarter_sample);
  681. s->mv[0][0][1] = scale_mv(s->next_picture.motion_val[1][xy][1], v->bfraction, 0, s->quarter_sample);
  682. s->mv[1][0][0] = scale_mv(s->next_picture.motion_val[1][xy][0], v->bfraction, 1, s->quarter_sample);
  683. s->mv[1][0][1] = scale_mv(s->next_picture.motion_val[1][xy][1], v->bfraction, 1, s->quarter_sample);
  684. /* Pullback predicted motion vectors as specified in 8.4.5.4 */
  685. 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));
  686. 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));
  687. 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));
  688. 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));
  689. if (direct) {
  690. s->current_picture.motion_val[0][xy][0] = s->mv[0][0][0];
  691. s->current_picture.motion_val[0][xy][1] = s->mv[0][0][1];
  692. s->current_picture.motion_val[1][xy][0] = s->mv[1][0][0];
  693. s->current_picture.motion_val[1][xy][1] = s->mv[1][0][1];
  694. return;
  695. }
  696. if ((mvtype == BMV_TYPE_FORWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
  697. C = s->current_picture.motion_val[0][xy - 2];
  698. A = s->current_picture.motion_val[0][xy - wrap * 2];
  699. off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
  700. B = s->current_picture.motion_val[0][xy - wrap * 2 + off];
  701. if (!s->mb_x) C[0] = C[1] = 0;
  702. if (!s->first_slice_line) { // predictor A is not out of bounds
  703. if (s->mb_width == 1) {
  704. px = A[0];
  705. py = A[1];
  706. } else {
  707. px = mid_pred(A[0], B[0], C[0]);
  708. py = mid_pred(A[1], B[1], C[1]);
  709. }
  710. } else if (s->mb_x) { // predictor C is not out of bounds
  711. px = C[0];
  712. py = C[1];
  713. } else {
  714. px = py = 0;
  715. }
  716. /* Pullback MV as specified in 8.3.5.3.4 */
  717. {
  718. int qx, qy, X, Y;
  719. int sh = v->profile < PROFILE_ADVANCED ? 5 : 6;
  720. int MV = 4 - (1 << sh);
  721. qx = (s->mb_x << sh);
  722. qy = (s->mb_y << sh);
  723. X = (s->mb_width << sh) - 4;
  724. Y = (s->mb_height << sh) - 4;
  725. if (qx + px < MV) px = MV - qx;
  726. if (qy + py < MV) py = MV - qy;
  727. if (qx + px > X) px = X - qx;
  728. if (qy + py > Y) py = Y - qy;
  729. }
  730. /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
  731. if (0 && !s->first_slice_line && s->mb_x) {
  732. if (is_intra[xy - wrap])
  733. sum = FFABS(px) + FFABS(py);
  734. else
  735. sum = FFABS(px - A[0]) + FFABS(py - A[1]);
  736. if (sum > 32) {
  737. if (get_bits1(&s->gb)) {
  738. px = A[0];
  739. py = A[1];
  740. } else {
  741. px = C[0];
  742. py = C[1];
  743. }
  744. } else {
  745. if (is_intra[xy - 2])
  746. sum = FFABS(px) + FFABS(py);
  747. else
  748. sum = FFABS(px - C[0]) + FFABS(py - C[1]);
  749. if (sum > 32) {
  750. if (get_bits1(&s->gb)) {
  751. px = A[0];
  752. py = A[1];
  753. } else {
  754. px = C[0];
  755. py = C[1];
  756. }
  757. }
  758. }
  759. }
  760. /* store MV using signed modulus of MV range defined in 4.11 */
  761. s->mv[0][0][0] = ((px + dmv_x[0] + r_x) & ((r_x << 1) - 1)) - r_x;
  762. s->mv[0][0][1] = ((py + dmv_y[0] + r_y) & ((r_y << 1) - 1)) - r_y;
  763. }
  764. if ((mvtype == BMV_TYPE_BACKWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
  765. C = s->current_picture.motion_val[1][xy - 2];
  766. A = s->current_picture.motion_val[1][xy - wrap * 2];
  767. off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
  768. B = s->current_picture.motion_val[1][xy - wrap * 2 + off];
  769. if (!s->mb_x)
  770. C[0] = C[1] = 0;
  771. if (!s->first_slice_line) { // predictor A is not out of bounds
  772. if (s->mb_width == 1) {
  773. px = A[0];
  774. py = A[1];
  775. } else {
  776. px = mid_pred(A[0], B[0], C[0]);
  777. py = mid_pred(A[1], B[1], C[1]);
  778. }
  779. } else if (s->mb_x) { // predictor C is not out of bounds
  780. px = C[0];
  781. py = C[1];
  782. } else {
  783. px = py = 0;
  784. }
  785. /* Pullback MV as specified in 8.3.5.3.4 */
  786. {
  787. int qx, qy, X, Y;
  788. int sh = v->profile < PROFILE_ADVANCED ? 5 : 6;
  789. int MV = 4 - (1 << sh);
  790. qx = (s->mb_x << sh);
  791. qy = (s->mb_y << sh);
  792. X = (s->mb_width << sh) - 4;
  793. Y = (s->mb_height << sh) - 4;
  794. if (qx + px < MV) px = MV - qx;
  795. if (qy + py < MV) py = MV - qy;
  796. if (qx + px > X) px = X - qx;
  797. if (qy + py > Y) py = Y - qy;
  798. }
  799. /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
  800. if (0 && !s->first_slice_line && s->mb_x) {
  801. if (is_intra[xy - wrap])
  802. sum = FFABS(px) + FFABS(py);
  803. else
  804. sum = FFABS(px - A[0]) + FFABS(py - A[1]);
  805. if (sum > 32) {
  806. if (get_bits1(&s->gb)) {
  807. px = A[0];
  808. py = A[1];
  809. } else {
  810. px = C[0];
  811. py = C[1];
  812. }
  813. } else {
  814. if (is_intra[xy - 2])
  815. sum = FFABS(px) + FFABS(py);
  816. else
  817. sum = FFABS(px - C[0]) + FFABS(py - C[1]);
  818. if (sum > 32) {
  819. if (get_bits1(&s->gb)) {
  820. px = A[0];
  821. py = A[1];
  822. } else {
  823. px = C[0];
  824. py = C[1];
  825. }
  826. }
  827. }
  828. }
  829. /* store MV using signed modulus of MV range defined in 4.11 */
  830. s->mv[1][0][0] = ((px + dmv_x[1] + r_x) & ((r_x << 1) - 1)) - r_x;
  831. s->mv[1][0][1] = ((py + dmv_y[1] + r_y) & ((r_y << 1) - 1)) - r_y;
  832. }
  833. s->current_picture.motion_val[0][xy][0] = s->mv[0][0][0];
  834. s->current_picture.motion_val[0][xy][1] = s->mv[0][0][1];
  835. s->current_picture.motion_val[1][xy][0] = s->mv[1][0][0];
  836. s->current_picture.motion_val[1][xy][1] = s->mv[1][0][1];
  837. }
  838. void ff_vc1_pred_b_mv_intfi(VC1Context *v, int n, int *dmv_x, int *dmv_y,
  839. int mv1, int *pred_flag)
  840. {
  841. int dir = (v->bmvtype == BMV_TYPE_BACKWARD) ? 1 : 0;
  842. MpegEncContext *s = &v->s;
  843. int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
  844. if (v->bmvtype == BMV_TYPE_DIRECT) {
  845. int total_opp, k, f;
  846. if (s->next_picture.mb_type[mb_pos + v->mb_off] != MB_TYPE_INTRA) {
  847. s->mv[0][0][0] = scale_mv(s->next_picture.motion_val[1][s->block_index[0] + v->blocks_off][0],
  848. v->bfraction, 0, s->quarter_sample);
  849. s->mv[0][0][1] = scale_mv(s->next_picture.motion_val[1][s->block_index[0] + v->blocks_off][1],
  850. v->bfraction, 0, s->quarter_sample);
  851. s->mv[1][0][0] = scale_mv(s->next_picture.motion_val[1][s->block_index[0] + v->blocks_off][0],
  852. v->bfraction, 1, s->quarter_sample);
  853. s->mv[1][0][1] = scale_mv(s->next_picture.motion_val[1][s->block_index[0] + v->blocks_off][1],
  854. v->bfraction, 1, s->quarter_sample);
  855. total_opp = v->mv_f_next[0][s->block_index[0] + v->blocks_off]
  856. + v->mv_f_next[0][s->block_index[1] + v->blocks_off]
  857. + v->mv_f_next[0][s->block_index[2] + v->blocks_off]
  858. + v->mv_f_next[0][s->block_index[3] + v->blocks_off];
  859. f = (total_opp > 2) ? 1 : 0;
  860. } else {
  861. s->mv[0][0][0] = s->mv[0][0][1] = 0;
  862. s->mv[1][0][0] = s->mv[1][0][1] = 0;
  863. f = 0;
  864. }
  865. v->ref_field_type[0] = v->ref_field_type[1] = v->cur_field_type ^ f;
  866. for (k = 0; k < 4; k++) {
  867. s->current_picture.motion_val[0][s->block_index[k] + v->blocks_off][0] = s->mv[0][0][0];
  868. s->current_picture.motion_val[0][s->block_index[k] + v->blocks_off][1] = s->mv[0][0][1];
  869. s->current_picture.motion_val[1][s->block_index[k] + v->blocks_off][0] = s->mv[1][0][0];
  870. s->current_picture.motion_val[1][s->block_index[k] + v->blocks_off][1] = s->mv[1][0][1];
  871. v->mv_f[0][s->block_index[k] + v->blocks_off] = f;
  872. v->mv_f[1][s->block_index[k] + v->blocks_off] = f;
  873. }
  874. return;
  875. }
  876. if (v->bmvtype == BMV_TYPE_INTERPOLATED) {
  877. 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);
  878. 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);
  879. return;
  880. }
  881. if (dir) { // backward
  882. 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);
  883. if (n == 3 || mv1) {
  884. 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);
  885. }
  886. } else { // forward
  887. 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);
  888. if (n == 3 || mv1) {
  889. 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);
  890. }
  891. }
  892. }