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.

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