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.

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