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.

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