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.

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