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.

705 lines
29KB

  1. /*
  2. * H.26L/H.264/AVC/JVT/14496-10/... direct mb/block decoding
  3. * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * Libav is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * H.264 / AVC / MPEG4 part10 direct mb/block decoding.
  24. * @author Michael Niedermayer <michaelni@gmx.at>
  25. */
  26. #include "internal.h"
  27. #include "avcodec.h"
  28. #include "h264.h"
  29. #include "mpegutils.h"
  30. #include "rectangle.h"
  31. #include "thread.h"
  32. #include <assert.h>
  33. static int get_scale_factor(H264SliceContext *sl,
  34. int poc, int poc1, int i)
  35. {
  36. int poc0 = sl->ref_list[0][i].poc;
  37. int td = av_clip_int8(poc1 - poc0);
  38. if (td == 0 || sl->ref_list[0][i].long_ref) {
  39. return 256;
  40. } else {
  41. int tb = av_clip_int8(poc - poc0);
  42. int tx = (16384 + (FFABS(td) >> 1)) / td;
  43. return av_clip_intp2((tb * tx + 32) >> 6, 10);
  44. }
  45. }
  46. void ff_h264_direct_dist_scale_factor(const H264Context *const h,
  47. H264SliceContext *sl)
  48. {
  49. const int poc = FIELD_PICTURE(h) ? h->cur_pic_ptr->field_poc[h->picture_structure == PICT_BOTTOM_FIELD]
  50. : h->cur_pic_ptr->poc;
  51. const int poc1 = sl->ref_list[1][0].poc;
  52. int i, field;
  53. if (FRAME_MBAFF(h))
  54. for (field = 0; field < 2; field++) {
  55. const int poc = h->cur_pic_ptr->field_poc[field];
  56. const int poc1 = sl->ref_list[1][0].field_poc[field];
  57. for (i = 0; i < 2 * sl->ref_count[0]; i++)
  58. sl->dist_scale_factor_field[field][i ^ field] =
  59. get_scale_factor(sl, poc, poc1, i + 16);
  60. }
  61. for (i = 0; i < sl->ref_count[0]; i++)
  62. sl->dist_scale_factor[i] = get_scale_factor(sl, poc, poc1, i);
  63. }
  64. static void fill_colmap(const H264Context *h, H264SliceContext *sl,
  65. int map[2][16 + 32], int list,
  66. int field, int colfield, int mbafi)
  67. {
  68. H264Picture *const ref1 = &sl->ref_list[1][0];
  69. int j, old_ref, rfield;
  70. int start = mbafi ? 16 : 0;
  71. int end = mbafi ? 16 + 2 * sl->ref_count[0] : sl->ref_count[0];
  72. int interl = mbafi || h->picture_structure != PICT_FRAME;
  73. /* bogus; fills in for missing frames */
  74. memset(map[list], 0, sizeof(map[list]));
  75. for (rfield = 0; rfield < 2; rfield++) {
  76. for (old_ref = 0; old_ref < ref1->ref_count[colfield][list]; old_ref++) {
  77. int poc = ref1->ref_poc[colfield][list][old_ref];
  78. if (!interl)
  79. poc |= 3;
  80. // FIXME: store all MBAFF references so this is not needed
  81. else if (interl && (poc & 3) == 3)
  82. poc = (poc & ~3) + rfield + 1;
  83. for (j = start; j < end; j++) {
  84. if (4 * sl->ref_list[0][j].frame_num +
  85. (sl->ref_list[0][j].reference & 3) == poc) {
  86. int cur_ref = mbafi ? (j - 16) ^ field : j;
  87. if (ref1->mbaff)
  88. map[list][2 * old_ref + (rfield ^ field) + 16] = cur_ref;
  89. if (rfield == field || !interl)
  90. map[list][old_ref] = cur_ref;
  91. break;
  92. }
  93. }
  94. }
  95. }
  96. }
  97. void ff_h264_direct_ref_list_init(const H264Context *const h, H264SliceContext *sl)
  98. {
  99. H264Picture *const ref1 = &sl->ref_list[1][0];
  100. H264Picture *const cur = h->cur_pic_ptr;
  101. int list, j, field;
  102. int sidx = (h->picture_structure & 1) ^ 1;
  103. int ref1sidx = (ref1->reference & 1) ^ 1;
  104. for (list = 0; list < 2; list++) {
  105. cur->ref_count[sidx][list] = sl->ref_count[list];
  106. for (j = 0; j < sl->ref_count[list]; j++)
  107. cur->ref_poc[sidx][list][j] = 4 * sl->ref_list[list][j].frame_num +
  108. (sl->ref_list[list][j].reference & 3);
  109. }
  110. if (h->picture_structure == PICT_FRAME) {
  111. memcpy(cur->ref_count[1], cur->ref_count[0], sizeof(cur->ref_count[0]));
  112. memcpy(cur->ref_poc[1], cur->ref_poc[0], sizeof(cur->ref_poc[0]));
  113. }
  114. cur->mbaff = FRAME_MBAFF(h);
  115. sl->col_fieldoff = 0;
  116. if (h->picture_structure == PICT_FRAME) {
  117. int cur_poc = h->cur_pic_ptr->poc;
  118. int *col_poc = sl->ref_list[1]->field_poc;
  119. sl->col_parity = (FFABS(col_poc[0] - cur_poc) >=
  120. FFABS(col_poc[1] - cur_poc));
  121. ref1sidx =
  122. sidx = sl->col_parity;
  123. // FL -> FL & differ parity
  124. } else if (!(h->picture_structure & sl->ref_list[1][0].reference) &&
  125. !sl->ref_list[1][0].mbaff) {
  126. sl->col_fieldoff = 2 * sl->ref_list[1][0].reference - 3;
  127. }
  128. if (sl->slice_type_nos != AV_PICTURE_TYPE_B || sl->direct_spatial_mv_pred)
  129. return;
  130. for (list = 0; list < 2; list++) {
  131. fill_colmap(h, sl, sl->map_col_to_list0, list, sidx, ref1sidx, 0);
  132. if (FRAME_MBAFF(h))
  133. for (field = 0; field < 2; field++)
  134. fill_colmap(h, sl, sl->map_col_to_list0_field[field], list, field,
  135. field, 1);
  136. }
  137. }
  138. static void await_reference_mb_row(const H264Context *const h, H264Picture *ref,
  139. int mb_y)
  140. {
  141. int ref_field = ref->reference - 1;
  142. int ref_field_picture = ref->field_picture;
  143. int ref_height = 16 * h->mb_height >> ref_field_picture;
  144. if (!HAVE_THREADS || !(h->avctx->active_thread_type & FF_THREAD_FRAME))
  145. return;
  146. /* FIXME: It can be safe to access mb stuff
  147. * even if pixels aren't deblocked yet. */
  148. ff_thread_await_progress(&ref->tf,
  149. FFMIN(16 * mb_y >> ref_field_picture,
  150. ref_height - 1),
  151. ref_field_picture && ref_field);
  152. }
  153. static void pred_spatial_direct_motion(const H264Context *const h, H264SliceContext *sl,
  154. int *mb_type)
  155. {
  156. int b8_stride = 2;
  157. int b4_stride = h->b_stride;
  158. int mb_xy = sl->mb_xy, mb_y = sl->mb_y;
  159. int mb_type_col[2];
  160. const int16_t (*l1mv0)[2], (*l1mv1)[2];
  161. const int8_t *l1ref0, *l1ref1;
  162. const int is_b8x8 = IS_8X8(*mb_type);
  163. unsigned int sub_mb_type = MB_TYPE_L0L1;
  164. int i8, i4;
  165. int ref[2];
  166. int mv[2];
  167. int list;
  168. assert(sl->ref_list[1][0].reference & 3);
  169. await_reference_mb_row(h, &sl->ref_list[1][0],
  170. sl->mb_y + !!IS_INTERLACED(*mb_type));
  171. #define MB_TYPE_16x16_OR_INTRA (MB_TYPE_16x16 | MB_TYPE_INTRA4x4 | \
  172. MB_TYPE_INTRA16x16 | MB_TYPE_INTRA_PCM)
  173. /* ref = min(neighbors) */
  174. for (list = 0; list < 2; list++) {
  175. int left_ref = sl->ref_cache[list][scan8[0] - 1];
  176. int top_ref = sl->ref_cache[list][scan8[0] - 8];
  177. int refc = sl->ref_cache[list][scan8[0] - 8 + 4];
  178. const int16_t *C = sl->mv_cache[list][scan8[0] - 8 + 4];
  179. if (refc == PART_NOT_AVAILABLE) {
  180. refc = sl->ref_cache[list][scan8[0] - 8 - 1];
  181. C = sl->mv_cache[list][scan8[0] - 8 - 1];
  182. }
  183. ref[list] = FFMIN3((unsigned)left_ref,
  184. (unsigned)top_ref,
  185. (unsigned)refc);
  186. if (ref[list] >= 0) {
  187. /* This is just pred_motion() but with the cases removed that
  188. * cannot happen for direct blocks. */
  189. const int16_t *const A = sl->mv_cache[list][scan8[0] - 1];
  190. const int16_t *const B = sl->mv_cache[list][scan8[0] - 8];
  191. int match_count = (left_ref == ref[list]) +
  192. (top_ref == ref[list]) +
  193. (refc == ref[list]);
  194. if (match_count > 1) { // most common
  195. mv[list] = pack16to32(mid_pred(A[0], B[0], C[0]),
  196. mid_pred(A[1], B[1], C[1]));
  197. } else {
  198. assert(match_count == 1);
  199. if (left_ref == ref[list])
  200. mv[list] = AV_RN32A(A);
  201. else if (top_ref == ref[list])
  202. mv[list] = AV_RN32A(B);
  203. else
  204. mv[list] = AV_RN32A(C);
  205. }
  206. } else {
  207. int mask = ~(MB_TYPE_L0 << (2 * list));
  208. mv[list] = 0;
  209. ref[list] = -1;
  210. if (!is_b8x8)
  211. *mb_type &= mask;
  212. sub_mb_type &= mask;
  213. }
  214. }
  215. if (ref[0] < 0 && ref[1] < 0) {
  216. ref[0] = ref[1] = 0;
  217. if (!is_b8x8)
  218. *mb_type |= MB_TYPE_L0L1;
  219. sub_mb_type |= MB_TYPE_L0L1;
  220. }
  221. if (!(is_b8x8 | mv[0] | mv[1])) {
  222. fill_rectangle(&sl->ref_cache[0][scan8[0]], 4, 4, 8, (uint8_t)ref[0], 1);
  223. fill_rectangle(&sl->ref_cache[1][scan8[0]], 4, 4, 8, (uint8_t)ref[1], 1);
  224. fill_rectangle(&sl->mv_cache[0][scan8[0]], 4, 4, 8, 0, 4);
  225. fill_rectangle(&sl->mv_cache[1][scan8[0]], 4, 4, 8, 0, 4);
  226. *mb_type = (*mb_type & ~(MB_TYPE_8x8 | MB_TYPE_16x8 | MB_TYPE_8x16 |
  227. MB_TYPE_P1L0 | MB_TYPE_P1L1)) |
  228. MB_TYPE_16x16 | MB_TYPE_DIRECT2;
  229. return;
  230. }
  231. if (IS_INTERLACED(sl->ref_list[1][0].mb_type[mb_xy])) { // AFL/AFR/FR/FL -> AFL/FL
  232. if (!IS_INTERLACED(*mb_type)) { // AFR/FR -> AFL/FL
  233. mb_y = (sl->mb_y & ~1) + sl->col_parity;
  234. mb_xy = sl->mb_x +
  235. ((sl->mb_y & ~1) + sl->col_parity) * h->mb_stride;
  236. b8_stride = 0;
  237. } else {
  238. mb_y += sl->col_fieldoff;
  239. mb_xy += h->mb_stride * sl->col_fieldoff; // non-zero for FL -> FL & differ parity
  240. }
  241. goto single_col;
  242. } else { // AFL/AFR/FR/FL -> AFR/FR
  243. if (IS_INTERLACED(*mb_type)) { // AFL /FL -> AFR/FR
  244. mb_y = sl->mb_y & ~1;
  245. mb_xy = (sl->mb_y & ~1) * h->mb_stride + sl->mb_x;
  246. mb_type_col[0] = sl->ref_list[1][0].mb_type[mb_xy];
  247. mb_type_col[1] = sl->ref_list[1][0].mb_type[mb_xy + h->mb_stride];
  248. b8_stride = 2 + 4 * h->mb_stride;
  249. b4_stride *= 6;
  250. if (IS_INTERLACED(mb_type_col[0]) !=
  251. IS_INTERLACED(mb_type_col[1])) {
  252. mb_type_col[0] &= ~MB_TYPE_INTERLACED;
  253. mb_type_col[1] &= ~MB_TYPE_INTERLACED;
  254. }
  255. sub_mb_type |= MB_TYPE_16x16 | MB_TYPE_DIRECT2; /* B_SUB_8x8 */
  256. if ((mb_type_col[0] & MB_TYPE_16x16_OR_INTRA) &&
  257. (mb_type_col[1] & MB_TYPE_16x16_OR_INTRA) &&
  258. !is_b8x8) {
  259. *mb_type |= MB_TYPE_16x8 | MB_TYPE_DIRECT2; /* B_16x8 */
  260. } else {
  261. *mb_type |= MB_TYPE_8x8;
  262. }
  263. } else { // AFR/FR -> AFR/FR
  264. single_col:
  265. mb_type_col[0] =
  266. mb_type_col[1] = sl->ref_list[1][0].mb_type[mb_xy];
  267. sub_mb_type |= MB_TYPE_16x16 | MB_TYPE_DIRECT2; /* B_SUB_8x8 */
  268. if (!is_b8x8 && (mb_type_col[0] & MB_TYPE_16x16_OR_INTRA)) {
  269. *mb_type |= MB_TYPE_16x16 | MB_TYPE_DIRECT2; /* B_16x16 */
  270. } else if (!is_b8x8 &&
  271. (mb_type_col[0] & (MB_TYPE_16x8 | MB_TYPE_8x16))) {
  272. *mb_type |= MB_TYPE_DIRECT2 |
  273. (mb_type_col[0] & (MB_TYPE_16x8 | MB_TYPE_8x16));
  274. } else {
  275. if (!h->sps.direct_8x8_inference_flag) {
  276. /* FIXME: Save sub mb types from previous frames (or derive
  277. * from MVs) so we know exactly what block size to use. */
  278. sub_mb_type += (MB_TYPE_8x8 - MB_TYPE_16x16); /* B_SUB_4x4 */
  279. }
  280. *mb_type |= MB_TYPE_8x8;
  281. }
  282. }
  283. }
  284. await_reference_mb_row(h, &sl->ref_list[1][0], mb_y);
  285. l1mv0 = &sl->ref_list[1][0].motion_val[0][h->mb2b_xy[mb_xy]];
  286. l1mv1 = &sl->ref_list[1][0].motion_val[1][h->mb2b_xy[mb_xy]];
  287. l1ref0 = &sl->ref_list[1][0].ref_index[0][4 * mb_xy];
  288. l1ref1 = &sl->ref_list[1][0].ref_index[1][4 * mb_xy];
  289. if (!b8_stride) {
  290. if (sl->mb_y & 1) {
  291. l1ref0 += 2;
  292. l1ref1 += 2;
  293. l1mv0 += 2 * b4_stride;
  294. l1mv1 += 2 * b4_stride;
  295. }
  296. }
  297. if (IS_INTERLACED(*mb_type) != IS_INTERLACED(mb_type_col[0])) {
  298. int n = 0;
  299. for (i8 = 0; i8 < 4; i8++) {
  300. int x8 = i8 & 1;
  301. int y8 = i8 >> 1;
  302. int xy8 = x8 + y8 * b8_stride;
  303. int xy4 = x8 * 3 + y8 * b4_stride;
  304. int a, b;
  305. if (is_b8x8 && !IS_DIRECT(sl->sub_mb_type[i8]))
  306. continue;
  307. sl->sub_mb_type[i8] = sub_mb_type;
  308. fill_rectangle(&sl->ref_cache[0][scan8[i8 * 4]], 2, 2, 8,
  309. (uint8_t)ref[0], 1);
  310. fill_rectangle(&sl->ref_cache[1][scan8[i8 * 4]], 2, 2, 8,
  311. (uint8_t)ref[1], 1);
  312. if (!IS_INTRA(mb_type_col[y8]) && !sl->ref_list[1][0].long_ref &&
  313. ((l1ref0[xy8] == 0 &&
  314. FFABS(l1mv0[xy4][0]) <= 1 &&
  315. FFABS(l1mv0[xy4][1]) <= 1) ||
  316. (l1ref0[xy8] < 0 &&
  317. l1ref1[xy8] == 0 &&
  318. FFABS(l1mv1[xy4][0]) <= 1 &&
  319. FFABS(l1mv1[xy4][1]) <= 1))) {
  320. a =
  321. b = 0;
  322. if (ref[0] > 0)
  323. a = mv[0];
  324. if (ref[1] > 0)
  325. b = mv[1];
  326. n++;
  327. } else {
  328. a = mv[0];
  329. b = mv[1];
  330. }
  331. fill_rectangle(&sl->mv_cache[0][scan8[i8 * 4]], 2, 2, 8, a, 4);
  332. fill_rectangle(&sl->mv_cache[1][scan8[i8 * 4]], 2, 2, 8, b, 4);
  333. }
  334. if (!is_b8x8 && !(n & 3))
  335. *mb_type = (*mb_type & ~(MB_TYPE_8x8 | MB_TYPE_16x8 | MB_TYPE_8x16 |
  336. MB_TYPE_P1L0 | MB_TYPE_P1L1)) |
  337. MB_TYPE_16x16 | MB_TYPE_DIRECT2;
  338. } else if (IS_16X16(*mb_type)) {
  339. int a, b;
  340. fill_rectangle(&sl->ref_cache[0][scan8[0]], 4, 4, 8, (uint8_t)ref[0], 1);
  341. fill_rectangle(&sl->ref_cache[1][scan8[0]], 4, 4, 8, (uint8_t)ref[1], 1);
  342. if (!IS_INTRA(mb_type_col[0]) && !sl->ref_list[1][0].long_ref &&
  343. ((l1ref0[0] == 0 &&
  344. FFABS(l1mv0[0][0]) <= 1 &&
  345. FFABS(l1mv0[0][1]) <= 1) ||
  346. (l1ref0[0] < 0 && !l1ref1[0] &&
  347. FFABS(l1mv1[0][0]) <= 1 &&
  348. FFABS(l1mv1[0][1]) <= 1 &&
  349. h->x264_build > 33U))) {
  350. a = b = 0;
  351. if (ref[0] > 0)
  352. a = mv[0];
  353. if (ref[1] > 0)
  354. b = mv[1];
  355. } else {
  356. a = mv[0];
  357. b = mv[1];
  358. }
  359. fill_rectangle(&sl->mv_cache[0][scan8[0]], 4, 4, 8, a, 4);
  360. fill_rectangle(&sl->mv_cache[1][scan8[0]], 4, 4, 8, b, 4);
  361. } else {
  362. int n = 0;
  363. for (i8 = 0; i8 < 4; i8++) {
  364. const int x8 = i8 & 1;
  365. const int y8 = i8 >> 1;
  366. if (is_b8x8 && !IS_DIRECT(sl->sub_mb_type[i8]))
  367. continue;
  368. sl->sub_mb_type[i8] = sub_mb_type;
  369. fill_rectangle(&sl->mv_cache[0][scan8[i8 * 4]], 2, 2, 8, mv[0], 4);
  370. fill_rectangle(&sl->mv_cache[1][scan8[i8 * 4]], 2, 2, 8, mv[1], 4);
  371. fill_rectangle(&sl->ref_cache[0][scan8[i8 * 4]], 2, 2, 8,
  372. (uint8_t)ref[0], 1);
  373. fill_rectangle(&sl->ref_cache[1][scan8[i8 * 4]], 2, 2, 8,
  374. (uint8_t)ref[1], 1);
  375. assert(b8_stride == 2);
  376. /* col_zero_flag */
  377. if (!IS_INTRA(mb_type_col[0]) && !sl->ref_list[1][0].long_ref &&
  378. (l1ref0[i8] == 0 ||
  379. (l1ref0[i8] < 0 &&
  380. l1ref1[i8] == 0 &&
  381. h->x264_build > 33U))) {
  382. const int16_t (*l1mv)[2] = l1ref0[i8] == 0 ? l1mv0 : l1mv1;
  383. if (IS_SUB_8X8(sub_mb_type)) {
  384. const int16_t *mv_col = l1mv[x8 * 3 + y8 * 3 * b4_stride];
  385. if (FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1) {
  386. if (ref[0] == 0)
  387. fill_rectangle(&sl->mv_cache[0][scan8[i8 * 4]], 2, 2,
  388. 8, 0, 4);
  389. if (ref[1] == 0)
  390. fill_rectangle(&sl->mv_cache[1][scan8[i8 * 4]], 2, 2,
  391. 8, 0, 4);
  392. n += 4;
  393. }
  394. } else {
  395. int m = 0;
  396. for (i4 = 0; i4 < 4; i4++) {
  397. const int16_t *mv_col = l1mv[x8 * 2 + (i4 & 1) +
  398. (y8 * 2 + (i4 >> 1)) * b4_stride];
  399. if (FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1) {
  400. if (ref[0] == 0)
  401. AV_ZERO32(sl->mv_cache[0][scan8[i8 * 4 + i4]]);
  402. if (ref[1] == 0)
  403. AV_ZERO32(sl->mv_cache[1][scan8[i8 * 4 + i4]]);
  404. m++;
  405. }
  406. }
  407. if (!(m & 3))
  408. sl->sub_mb_type[i8] += MB_TYPE_16x16 - MB_TYPE_8x8;
  409. n += m;
  410. }
  411. }
  412. }
  413. if (!is_b8x8 && !(n & 15))
  414. *mb_type = (*mb_type & ~(MB_TYPE_8x8 | MB_TYPE_16x8 | MB_TYPE_8x16 |
  415. MB_TYPE_P1L0 | MB_TYPE_P1L1)) |
  416. MB_TYPE_16x16 | MB_TYPE_DIRECT2;
  417. }
  418. }
  419. static void pred_temp_direct_motion(const H264Context *const h, H264SliceContext *sl,
  420. int *mb_type)
  421. {
  422. int b8_stride = 2;
  423. int b4_stride = h->b_stride;
  424. int mb_xy = sl->mb_xy, mb_y = sl->mb_y;
  425. int mb_type_col[2];
  426. const int16_t (*l1mv0)[2], (*l1mv1)[2];
  427. const int8_t *l1ref0, *l1ref1;
  428. const int is_b8x8 = IS_8X8(*mb_type);
  429. unsigned int sub_mb_type;
  430. int i8, i4;
  431. assert(sl->ref_list[1][0].reference & 3);
  432. await_reference_mb_row(h, &sl->ref_list[1][0],
  433. sl->mb_y + !!IS_INTERLACED(*mb_type));
  434. if (IS_INTERLACED(sl->ref_list[1][0].mb_type[mb_xy])) { // AFL/AFR/FR/FL -> AFL/FL
  435. if (!IS_INTERLACED(*mb_type)) { // AFR/FR -> AFL/FL
  436. mb_y = (sl->mb_y & ~1) + sl->col_parity;
  437. mb_xy = sl->mb_x +
  438. ((sl->mb_y & ~1) + sl->col_parity) * h->mb_stride;
  439. b8_stride = 0;
  440. } else {
  441. mb_y += sl->col_fieldoff;
  442. mb_xy += h->mb_stride * sl->col_fieldoff; // non-zero for FL -> FL & differ parity
  443. }
  444. goto single_col;
  445. } else { // AFL/AFR/FR/FL -> AFR/FR
  446. if (IS_INTERLACED(*mb_type)) { // AFL /FL -> AFR/FR
  447. mb_y = sl->mb_y & ~1;
  448. mb_xy = sl->mb_x + (sl->mb_y & ~1) * h->mb_stride;
  449. mb_type_col[0] = sl->ref_list[1][0].mb_type[mb_xy];
  450. mb_type_col[1] = sl->ref_list[1][0].mb_type[mb_xy + h->mb_stride];
  451. b8_stride = 2 + 4 * h->mb_stride;
  452. b4_stride *= 6;
  453. if (IS_INTERLACED(mb_type_col[0]) !=
  454. IS_INTERLACED(mb_type_col[1])) {
  455. mb_type_col[0] &= ~MB_TYPE_INTERLACED;
  456. mb_type_col[1] &= ~MB_TYPE_INTERLACED;
  457. }
  458. sub_mb_type = MB_TYPE_16x16 | MB_TYPE_P0L0 | MB_TYPE_P0L1 |
  459. MB_TYPE_DIRECT2; /* B_SUB_8x8 */
  460. if ((mb_type_col[0] & MB_TYPE_16x16_OR_INTRA) &&
  461. (mb_type_col[1] & MB_TYPE_16x16_OR_INTRA) &&
  462. !is_b8x8) {
  463. *mb_type |= MB_TYPE_16x8 | MB_TYPE_L0L1 |
  464. MB_TYPE_DIRECT2; /* B_16x8 */
  465. } else {
  466. *mb_type |= MB_TYPE_8x8 | MB_TYPE_L0L1;
  467. }
  468. } else { // AFR/FR -> AFR/FR
  469. single_col:
  470. mb_type_col[0] =
  471. mb_type_col[1] = sl->ref_list[1][0].mb_type[mb_xy];
  472. sub_mb_type = MB_TYPE_16x16 | MB_TYPE_P0L0 | MB_TYPE_P0L1 |
  473. MB_TYPE_DIRECT2; /* B_SUB_8x8 */
  474. if (!is_b8x8 && (mb_type_col[0] & MB_TYPE_16x16_OR_INTRA)) {
  475. *mb_type |= MB_TYPE_16x16 | MB_TYPE_P0L0 | MB_TYPE_P0L1 |
  476. MB_TYPE_DIRECT2; /* B_16x16 */
  477. } else if (!is_b8x8 &&
  478. (mb_type_col[0] & (MB_TYPE_16x8 | MB_TYPE_8x16))) {
  479. *mb_type |= MB_TYPE_L0L1 | MB_TYPE_DIRECT2 |
  480. (mb_type_col[0] & (MB_TYPE_16x8 | MB_TYPE_8x16));
  481. } else {
  482. if (!h->sps.direct_8x8_inference_flag) {
  483. /* FIXME: save sub mb types from previous frames (or derive
  484. * from MVs) so we know exactly what block size to use */
  485. sub_mb_type = MB_TYPE_8x8 | MB_TYPE_P0L0 | MB_TYPE_P0L1 |
  486. MB_TYPE_DIRECT2; /* B_SUB_4x4 */
  487. }
  488. *mb_type |= MB_TYPE_8x8 | MB_TYPE_L0L1;
  489. }
  490. }
  491. }
  492. await_reference_mb_row(h, &sl->ref_list[1][0], mb_y);
  493. l1mv0 = &sl->ref_list[1][0].motion_val[0][h->mb2b_xy[mb_xy]];
  494. l1mv1 = &sl->ref_list[1][0].motion_val[1][h->mb2b_xy[mb_xy]];
  495. l1ref0 = &sl->ref_list[1][0].ref_index[0][4 * mb_xy];
  496. l1ref1 = &sl->ref_list[1][0].ref_index[1][4 * mb_xy];
  497. if (!b8_stride) {
  498. if (sl->mb_y & 1) {
  499. l1ref0 += 2;
  500. l1ref1 += 2;
  501. l1mv0 += 2 * b4_stride;
  502. l1mv1 += 2 * b4_stride;
  503. }
  504. }
  505. {
  506. const int *map_col_to_list0[2] = { sl->map_col_to_list0[0],
  507. sl->map_col_to_list0[1] };
  508. const int *dist_scale_factor = sl->dist_scale_factor;
  509. int ref_offset;
  510. if (FRAME_MBAFF(h) && IS_INTERLACED(*mb_type)) {
  511. map_col_to_list0[0] = sl->map_col_to_list0_field[sl->mb_y & 1][0];
  512. map_col_to_list0[1] = sl->map_col_to_list0_field[sl->mb_y & 1][1];
  513. dist_scale_factor = sl->dist_scale_factor_field[sl->mb_y & 1];
  514. }
  515. ref_offset = (sl->ref_list[1][0].mbaff << 4) & (mb_type_col[0] >> 3);
  516. if (IS_INTERLACED(*mb_type) != IS_INTERLACED(mb_type_col[0])) {
  517. int y_shift = 2 * !IS_INTERLACED(*mb_type);
  518. assert(h->sps.direct_8x8_inference_flag);
  519. for (i8 = 0; i8 < 4; i8++) {
  520. const int x8 = i8 & 1;
  521. const int y8 = i8 >> 1;
  522. int ref0, scale;
  523. const int16_t (*l1mv)[2] = l1mv0;
  524. if (is_b8x8 && !IS_DIRECT(sl->sub_mb_type[i8]))
  525. continue;
  526. sl->sub_mb_type[i8] = sub_mb_type;
  527. fill_rectangle(&sl->ref_cache[1][scan8[i8 * 4]], 2, 2, 8, 0, 1);
  528. if (IS_INTRA(mb_type_col[y8])) {
  529. fill_rectangle(&sl->ref_cache[0][scan8[i8 * 4]], 2, 2, 8, 0, 1);
  530. fill_rectangle(&sl->mv_cache[0][scan8[i8 * 4]], 2, 2, 8, 0, 4);
  531. fill_rectangle(&sl->mv_cache[1][scan8[i8 * 4]], 2, 2, 8, 0, 4);
  532. continue;
  533. }
  534. ref0 = l1ref0[x8 + y8 * b8_stride];
  535. if (ref0 >= 0)
  536. ref0 = map_col_to_list0[0][ref0 + ref_offset];
  537. else {
  538. ref0 = map_col_to_list0[1][l1ref1[x8 + y8 * b8_stride] +
  539. ref_offset];
  540. l1mv = l1mv1;
  541. }
  542. scale = dist_scale_factor[ref0];
  543. fill_rectangle(&sl->ref_cache[0][scan8[i8 * 4]], 2, 2, 8,
  544. ref0, 1);
  545. {
  546. const int16_t *mv_col = l1mv[x8 * 3 + y8 * b4_stride];
  547. int my_col = (mv_col[1] << y_shift) / 2;
  548. int mx = (scale * mv_col[0] + 128) >> 8;
  549. int my = (scale * my_col + 128) >> 8;
  550. fill_rectangle(&sl->mv_cache[0][scan8[i8 * 4]], 2, 2, 8,
  551. pack16to32(mx, my), 4);
  552. fill_rectangle(&sl->mv_cache[1][scan8[i8 * 4]], 2, 2, 8,
  553. pack16to32(mx - mv_col[0], my - my_col), 4);
  554. }
  555. }
  556. return;
  557. }
  558. /* one-to-one mv scaling */
  559. if (IS_16X16(*mb_type)) {
  560. int ref, mv0, mv1;
  561. fill_rectangle(&sl->ref_cache[1][scan8[0]], 4, 4, 8, 0, 1);
  562. if (IS_INTRA(mb_type_col[0])) {
  563. ref = mv0 = mv1 = 0;
  564. } else {
  565. const int ref0 = l1ref0[0] >= 0 ? map_col_to_list0[0][l1ref0[0] + ref_offset]
  566. : map_col_to_list0[1][l1ref1[0] + ref_offset];
  567. const int scale = dist_scale_factor[ref0];
  568. const int16_t *mv_col = l1ref0[0] >= 0 ? l1mv0[0] : l1mv1[0];
  569. int mv_l0[2];
  570. mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
  571. mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
  572. ref = ref0;
  573. mv0 = pack16to32(mv_l0[0], mv_l0[1]);
  574. mv1 = pack16to32(mv_l0[0] - mv_col[0], mv_l0[1] - mv_col[1]);
  575. }
  576. fill_rectangle(&sl->ref_cache[0][scan8[0]], 4, 4, 8, ref, 1);
  577. fill_rectangle(&sl->mv_cache[0][scan8[0]], 4, 4, 8, mv0, 4);
  578. fill_rectangle(&sl->mv_cache[1][scan8[0]], 4, 4, 8, mv1, 4);
  579. } else {
  580. for (i8 = 0; i8 < 4; i8++) {
  581. const int x8 = i8 & 1;
  582. const int y8 = i8 >> 1;
  583. int ref0, scale;
  584. const int16_t (*l1mv)[2] = l1mv0;
  585. if (is_b8x8 && !IS_DIRECT(sl->sub_mb_type[i8]))
  586. continue;
  587. sl->sub_mb_type[i8] = sub_mb_type;
  588. fill_rectangle(&sl->ref_cache[1][scan8[i8 * 4]], 2, 2, 8, 0, 1);
  589. if (IS_INTRA(mb_type_col[0])) {
  590. fill_rectangle(&sl->ref_cache[0][scan8[i8 * 4]], 2, 2, 8, 0, 1);
  591. fill_rectangle(&sl->mv_cache[0][scan8[i8 * 4]], 2, 2, 8, 0, 4);
  592. fill_rectangle(&sl->mv_cache[1][scan8[i8 * 4]], 2, 2, 8, 0, 4);
  593. continue;
  594. }
  595. assert(b8_stride == 2);
  596. ref0 = l1ref0[i8];
  597. if (ref0 >= 0)
  598. ref0 = map_col_to_list0[0][ref0 + ref_offset];
  599. else {
  600. ref0 = map_col_to_list0[1][l1ref1[i8] + ref_offset];
  601. l1mv = l1mv1;
  602. }
  603. scale = dist_scale_factor[ref0];
  604. fill_rectangle(&sl->ref_cache[0][scan8[i8 * 4]], 2, 2, 8,
  605. ref0, 1);
  606. if (IS_SUB_8X8(sub_mb_type)) {
  607. const int16_t *mv_col = l1mv[x8 * 3 + y8 * 3 * b4_stride];
  608. int mx = (scale * mv_col[0] + 128) >> 8;
  609. int my = (scale * mv_col[1] + 128) >> 8;
  610. fill_rectangle(&sl->mv_cache[0][scan8[i8 * 4]], 2, 2, 8,
  611. pack16to32(mx, my), 4);
  612. fill_rectangle(&sl->mv_cache[1][scan8[i8 * 4]], 2, 2, 8,
  613. pack16to32(mx - mv_col[0], my - mv_col[1]), 4);
  614. } else {
  615. for (i4 = 0; i4 < 4; i4++) {
  616. const int16_t *mv_col = l1mv[x8 * 2 + (i4 & 1) +
  617. (y8 * 2 + (i4 >> 1)) * b4_stride];
  618. int16_t *mv_l0 = sl->mv_cache[0][scan8[i8 * 4 + i4]];
  619. mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
  620. mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
  621. AV_WN32A(sl->mv_cache[1][scan8[i8 * 4 + i4]],
  622. pack16to32(mv_l0[0] - mv_col[0],
  623. mv_l0[1] - mv_col[1]));
  624. }
  625. }
  626. }
  627. }
  628. }
  629. }
  630. void ff_h264_pred_direct_motion(const H264Context *const h, H264SliceContext *sl,
  631. int *mb_type)
  632. {
  633. if (sl->direct_spatial_mv_pred)
  634. pred_spatial_direct_motion(h, sl, mb_type);
  635. else
  636. pred_temp_direct_motion(h, sl, mb_type);
  637. }