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.

714 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 FFmpeg.
  6. *
  7. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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 / MPEG-4 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].parent->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].parent->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].parent;
  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].parent->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. H264Ref *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 < sl->list_count; 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].parent->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 (sl->list_count != 2 || !sl->ref_count[1])
  117. return;
  118. if (h->picture_structure == PICT_FRAME) {
  119. int cur_poc = h->cur_pic_ptr->poc;
  120. int *col_poc = sl->ref_list[1][0].parent->field_poc;
  121. if (col_poc[0] == INT_MAX && col_poc[1] == INT_MAX) {
  122. av_log(h->avctx, AV_LOG_ERROR, "co located POCs unavailable\n");
  123. sl->col_parity = 1;
  124. } else
  125. sl->col_parity = (FFABS(col_poc[0] - cur_poc) >=
  126. FFABS(col_poc[1] - cur_poc));
  127. ref1sidx =
  128. sidx = sl->col_parity;
  129. // FL -> FL & differ parity
  130. } else if (!(h->picture_structure & sl->ref_list[1][0].reference) &&
  131. !sl->ref_list[1][0].parent->mbaff) {
  132. sl->col_fieldoff = 2 * sl->ref_list[1][0].reference - 3;
  133. }
  134. if (sl->slice_type_nos != AV_PICTURE_TYPE_B || sl->direct_spatial_mv_pred)
  135. return;
  136. for (list = 0; list < 2; list++) {
  137. fill_colmap(h, sl, sl->map_col_to_list0, list, sidx, ref1sidx, 0);
  138. if (FRAME_MBAFF(h))
  139. for (field = 0; field < 2; field++)
  140. fill_colmap(h, sl, sl->map_col_to_list0_field[field], list, field,
  141. field, 1);
  142. }
  143. }
  144. static void await_reference_mb_row(const H264Context *const h, H264Ref *ref,
  145. int mb_y)
  146. {
  147. int ref_field = ref->reference - 1;
  148. int ref_field_picture = ref->parent->field_picture;
  149. int ref_height = 16 * h->mb_height >> ref_field_picture;
  150. if (!HAVE_THREADS || !(h->avctx->active_thread_type & FF_THREAD_FRAME))
  151. return;
  152. /* FIXME: It can be safe to access mb stuff
  153. * even if pixels aren't deblocked yet. */
  154. ff_thread_await_progress(&ref->parent->tf,
  155. FFMIN(16 * mb_y >> ref_field_picture,
  156. ref_height - 1),
  157. ref_field_picture && ref_field);
  158. }
  159. static void pred_spatial_direct_motion(const H264Context *const h, H264SliceContext *sl,
  160. int *mb_type)
  161. {
  162. int b8_stride = 2;
  163. int b4_stride = h->b_stride;
  164. int mb_xy = sl->mb_xy, mb_y = sl->mb_y;
  165. int mb_type_col[2];
  166. const int16_t (*l1mv0)[2], (*l1mv1)[2];
  167. const int8_t *l1ref0, *l1ref1;
  168. const int is_b8x8 = IS_8X8(*mb_type);
  169. unsigned int sub_mb_type = MB_TYPE_L0L1;
  170. int i8, i4;
  171. int ref[2];
  172. int mv[2];
  173. int list;
  174. assert(sl->ref_list[1][0].reference & 3);
  175. await_reference_mb_row(h, &sl->ref_list[1][0],
  176. sl->mb_y + !!IS_INTERLACED(*mb_type));
  177. #define MB_TYPE_16x16_OR_INTRA (MB_TYPE_16x16 | MB_TYPE_INTRA4x4 | \
  178. MB_TYPE_INTRA16x16 | MB_TYPE_INTRA_PCM)
  179. /* ref = min(neighbors) */
  180. for (list = 0; list < 2; list++) {
  181. int left_ref = sl->ref_cache[list][scan8[0] - 1];
  182. int top_ref = sl->ref_cache[list][scan8[0] - 8];
  183. int refc = sl->ref_cache[list][scan8[0] - 8 + 4];
  184. const int16_t *C = sl->mv_cache[list][scan8[0] - 8 + 4];
  185. if (refc == PART_NOT_AVAILABLE) {
  186. refc = sl->ref_cache[list][scan8[0] - 8 - 1];
  187. C = sl->mv_cache[list][scan8[0] - 8 - 1];
  188. }
  189. ref[list] = FFMIN3((unsigned)left_ref,
  190. (unsigned)top_ref,
  191. (unsigned)refc);
  192. if (ref[list] >= 0) {
  193. /* This is just pred_motion() but with the cases removed that
  194. * cannot happen for direct blocks. */
  195. const int16_t *const A = sl->mv_cache[list][scan8[0] - 1];
  196. const int16_t *const B = sl->mv_cache[list][scan8[0] - 8];
  197. int match_count = (left_ref == ref[list]) +
  198. (top_ref == ref[list]) +
  199. (refc == ref[list]);
  200. if (match_count > 1) { // most common
  201. mv[list] = pack16to32(mid_pred(A[0], B[0], C[0]),
  202. mid_pred(A[1], B[1], C[1]));
  203. } else {
  204. assert(match_count == 1);
  205. if (left_ref == ref[list])
  206. mv[list] = AV_RN32A(A);
  207. else if (top_ref == ref[list])
  208. mv[list] = AV_RN32A(B);
  209. else
  210. mv[list] = AV_RN32A(C);
  211. }
  212. av_assert2(ref[list] < (sl->ref_count[list] << !!FRAME_MBAFF(h)));
  213. } else {
  214. int mask = ~(MB_TYPE_L0 << (2 * list));
  215. mv[list] = 0;
  216. ref[list] = -1;
  217. if (!is_b8x8)
  218. *mb_type &= mask;
  219. sub_mb_type &= mask;
  220. }
  221. }
  222. if (ref[0] < 0 && ref[1] < 0) {
  223. ref[0] = ref[1] = 0;
  224. if (!is_b8x8)
  225. *mb_type |= MB_TYPE_L0L1;
  226. sub_mb_type |= MB_TYPE_L0L1;
  227. }
  228. if (!(is_b8x8 | mv[0] | mv[1])) {
  229. fill_rectangle(&sl->ref_cache[0][scan8[0]], 4, 4, 8, (uint8_t)ref[0], 1);
  230. fill_rectangle(&sl->ref_cache[1][scan8[0]], 4, 4, 8, (uint8_t)ref[1], 1);
  231. fill_rectangle(&sl->mv_cache[0][scan8[0]], 4, 4, 8, 0, 4);
  232. fill_rectangle(&sl->mv_cache[1][scan8[0]], 4, 4, 8, 0, 4);
  233. *mb_type = (*mb_type & ~(MB_TYPE_8x8 | MB_TYPE_16x8 | MB_TYPE_8x16 |
  234. MB_TYPE_P1L0 | MB_TYPE_P1L1)) |
  235. MB_TYPE_16x16 | MB_TYPE_DIRECT2;
  236. return;
  237. }
  238. if (IS_INTERLACED(sl->ref_list[1][0].parent->mb_type[mb_xy])) { // AFL/AFR/FR/FL -> AFL/FL
  239. if (!IS_INTERLACED(*mb_type)) { // AFR/FR -> AFL/FL
  240. mb_y = (sl->mb_y & ~1) + sl->col_parity;
  241. mb_xy = sl->mb_x +
  242. ((sl->mb_y & ~1) + sl->col_parity) * h->mb_stride;
  243. b8_stride = 0;
  244. } else {
  245. mb_y += sl->col_fieldoff;
  246. mb_xy += h->mb_stride * sl->col_fieldoff; // non-zero for FL -> FL & differ parity
  247. }
  248. goto single_col;
  249. } else { // AFL/AFR/FR/FL -> AFR/FR
  250. if (IS_INTERLACED(*mb_type)) { // AFL /FL -> AFR/FR
  251. mb_y = sl->mb_y & ~1;
  252. mb_xy = (sl->mb_y & ~1) * h->mb_stride + sl->mb_x;
  253. mb_type_col[0] = sl->ref_list[1][0].parent->mb_type[mb_xy];
  254. mb_type_col[1] = sl->ref_list[1][0].parent->mb_type[mb_xy + h->mb_stride];
  255. b8_stride = 2 + 4 * h->mb_stride;
  256. b4_stride *= 6;
  257. if (IS_INTERLACED(mb_type_col[0]) !=
  258. IS_INTERLACED(mb_type_col[1])) {
  259. mb_type_col[0] &= ~MB_TYPE_INTERLACED;
  260. mb_type_col[1] &= ~MB_TYPE_INTERLACED;
  261. }
  262. sub_mb_type |= MB_TYPE_16x16 | MB_TYPE_DIRECT2; /* B_SUB_8x8 */
  263. if ((mb_type_col[0] & MB_TYPE_16x16_OR_INTRA) &&
  264. (mb_type_col[1] & MB_TYPE_16x16_OR_INTRA) &&
  265. !is_b8x8) {
  266. *mb_type |= MB_TYPE_16x8 | MB_TYPE_DIRECT2; /* B_16x8 */
  267. } else {
  268. *mb_type |= MB_TYPE_8x8;
  269. }
  270. } else { // AFR/FR -> AFR/FR
  271. single_col:
  272. mb_type_col[0] =
  273. mb_type_col[1] = sl->ref_list[1][0].parent->mb_type[mb_xy];
  274. sub_mb_type |= MB_TYPE_16x16 | MB_TYPE_DIRECT2; /* B_SUB_8x8 */
  275. if (!is_b8x8 && (mb_type_col[0] & MB_TYPE_16x16_OR_INTRA)) {
  276. *mb_type |= MB_TYPE_16x16 | MB_TYPE_DIRECT2; /* B_16x16 */
  277. } else if (!is_b8x8 &&
  278. (mb_type_col[0] & (MB_TYPE_16x8 | MB_TYPE_8x16))) {
  279. *mb_type |= MB_TYPE_DIRECT2 |
  280. (mb_type_col[0] & (MB_TYPE_16x8 | MB_TYPE_8x16));
  281. } else {
  282. if (!h->ps.sps->direct_8x8_inference_flag) {
  283. /* FIXME: Save sub mb types from previous frames (or derive
  284. * from MVs) so we know exactly what block size to use. */
  285. sub_mb_type += (MB_TYPE_8x8 - MB_TYPE_16x16); /* B_SUB_4x4 */
  286. }
  287. *mb_type |= MB_TYPE_8x8;
  288. }
  289. }
  290. }
  291. await_reference_mb_row(h, &sl->ref_list[1][0], mb_y);
  292. l1mv0 = (void*)&sl->ref_list[1][0].parent->motion_val[0][h->mb2b_xy[mb_xy]];
  293. l1mv1 = (void*)&sl->ref_list[1][0].parent->motion_val[1][h->mb2b_xy[mb_xy]];
  294. l1ref0 = &sl->ref_list[1][0].parent->ref_index[0][4 * mb_xy];
  295. l1ref1 = &sl->ref_list[1][0].parent->ref_index[1][4 * mb_xy];
  296. if (!b8_stride) {
  297. if (sl->mb_y & 1) {
  298. l1ref0 += 2;
  299. l1ref1 += 2;
  300. l1mv0 += 2 * b4_stride;
  301. l1mv1 += 2 * b4_stride;
  302. }
  303. }
  304. if (IS_INTERLACED(*mb_type) != IS_INTERLACED(mb_type_col[0])) {
  305. int n = 0;
  306. for (i8 = 0; i8 < 4; i8++) {
  307. int x8 = i8 & 1;
  308. int y8 = i8 >> 1;
  309. int xy8 = x8 + y8 * b8_stride;
  310. int xy4 = x8 * 3 + y8 * b4_stride;
  311. int a, b;
  312. if (is_b8x8 && !IS_DIRECT(sl->sub_mb_type[i8]))
  313. continue;
  314. sl->sub_mb_type[i8] = sub_mb_type;
  315. fill_rectangle(&sl->ref_cache[0][scan8[i8 * 4]], 2, 2, 8,
  316. (uint8_t)ref[0], 1);
  317. fill_rectangle(&sl->ref_cache[1][scan8[i8 * 4]], 2, 2, 8,
  318. (uint8_t)ref[1], 1);
  319. if (!IS_INTRA(mb_type_col[y8]) && !sl->ref_list[1][0].parent->long_ref &&
  320. ((l1ref0[xy8] == 0 &&
  321. FFABS(l1mv0[xy4][0]) <= 1 &&
  322. FFABS(l1mv0[xy4][1]) <= 1) ||
  323. (l1ref0[xy8] < 0 &&
  324. l1ref1[xy8] == 0 &&
  325. FFABS(l1mv1[xy4][0]) <= 1 &&
  326. FFABS(l1mv1[xy4][1]) <= 1))) {
  327. a =
  328. b = 0;
  329. if (ref[0] > 0)
  330. a = mv[0];
  331. if (ref[1] > 0)
  332. b = mv[1];
  333. n++;
  334. } else {
  335. a = mv[0];
  336. b = mv[1];
  337. }
  338. fill_rectangle(&sl->mv_cache[0][scan8[i8 * 4]], 2, 2, 8, a, 4);
  339. fill_rectangle(&sl->mv_cache[1][scan8[i8 * 4]], 2, 2, 8, b, 4);
  340. }
  341. if (!is_b8x8 && !(n & 3))
  342. *mb_type = (*mb_type & ~(MB_TYPE_8x8 | MB_TYPE_16x8 | MB_TYPE_8x16 |
  343. MB_TYPE_P1L0 | MB_TYPE_P1L1)) |
  344. MB_TYPE_16x16 | MB_TYPE_DIRECT2;
  345. } else if (IS_16X16(*mb_type)) {
  346. int a, b;
  347. fill_rectangle(&sl->ref_cache[0][scan8[0]], 4, 4, 8, (uint8_t)ref[0], 1);
  348. fill_rectangle(&sl->ref_cache[1][scan8[0]], 4, 4, 8, (uint8_t)ref[1], 1);
  349. if (!IS_INTRA(mb_type_col[0]) && !sl->ref_list[1][0].parent->long_ref &&
  350. ((l1ref0[0] == 0 &&
  351. FFABS(l1mv0[0][0]) <= 1 &&
  352. FFABS(l1mv0[0][1]) <= 1) ||
  353. (l1ref0[0] < 0 && !l1ref1[0] &&
  354. FFABS(l1mv1[0][0]) <= 1 &&
  355. FFABS(l1mv1[0][1]) <= 1 &&
  356. h->sei.unregistered.x264_build > 33U))) {
  357. a = b = 0;
  358. if (ref[0] > 0)
  359. a = mv[0];
  360. if (ref[1] > 0)
  361. b = mv[1];
  362. } else {
  363. a = mv[0];
  364. b = mv[1];
  365. }
  366. fill_rectangle(&sl->mv_cache[0][scan8[0]], 4, 4, 8, a, 4);
  367. fill_rectangle(&sl->mv_cache[1][scan8[0]], 4, 4, 8, b, 4);
  368. } else {
  369. int n = 0;
  370. for (i8 = 0; i8 < 4; i8++) {
  371. const int x8 = i8 & 1;
  372. const int y8 = i8 >> 1;
  373. if (is_b8x8 && !IS_DIRECT(sl->sub_mb_type[i8]))
  374. continue;
  375. sl->sub_mb_type[i8] = sub_mb_type;
  376. fill_rectangle(&sl->mv_cache[0][scan8[i8 * 4]], 2, 2, 8, mv[0], 4);
  377. fill_rectangle(&sl->mv_cache[1][scan8[i8 * 4]], 2, 2, 8, mv[1], 4);
  378. fill_rectangle(&sl->ref_cache[0][scan8[i8 * 4]], 2, 2, 8,
  379. (uint8_t)ref[0], 1);
  380. fill_rectangle(&sl->ref_cache[1][scan8[i8 * 4]], 2, 2, 8,
  381. (uint8_t)ref[1], 1);
  382. assert(b8_stride == 2);
  383. /* col_zero_flag */
  384. if (!IS_INTRA(mb_type_col[0]) && !sl->ref_list[1][0].parent->long_ref &&
  385. (l1ref0[i8] == 0 ||
  386. (l1ref0[i8] < 0 &&
  387. l1ref1[i8] == 0 &&
  388. h->sei.unregistered.x264_build > 33U))) {
  389. const int16_t (*l1mv)[2] = l1ref0[i8] == 0 ? l1mv0 : l1mv1;
  390. if (IS_SUB_8X8(sub_mb_type)) {
  391. const int16_t *mv_col = l1mv[x8 * 3 + y8 * 3 * b4_stride];
  392. if (FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1) {
  393. if (ref[0] == 0)
  394. fill_rectangle(&sl->mv_cache[0][scan8[i8 * 4]], 2, 2,
  395. 8, 0, 4);
  396. if (ref[1] == 0)
  397. fill_rectangle(&sl->mv_cache[1][scan8[i8 * 4]], 2, 2,
  398. 8, 0, 4);
  399. n += 4;
  400. }
  401. } else {
  402. int m = 0;
  403. for (i4 = 0; i4 < 4; i4++) {
  404. const int16_t *mv_col = l1mv[x8 * 2 + (i4 & 1) +
  405. (y8 * 2 + (i4 >> 1)) * b4_stride];
  406. if (FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1) {
  407. if (ref[0] == 0)
  408. AV_ZERO32(sl->mv_cache[0][scan8[i8 * 4 + i4]]);
  409. if (ref[1] == 0)
  410. AV_ZERO32(sl->mv_cache[1][scan8[i8 * 4 + i4]]);
  411. m++;
  412. }
  413. }
  414. if (!(m & 3))
  415. sl->sub_mb_type[i8] += MB_TYPE_16x16 - MB_TYPE_8x8;
  416. n += m;
  417. }
  418. }
  419. }
  420. if (!is_b8x8 && !(n & 15))
  421. *mb_type = (*mb_type & ~(MB_TYPE_8x8 | MB_TYPE_16x8 | MB_TYPE_8x16 |
  422. MB_TYPE_P1L0 | MB_TYPE_P1L1)) |
  423. MB_TYPE_16x16 | MB_TYPE_DIRECT2;
  424. }
  425. }
  426. static void pred_temp_direct_motion(const H264Context *const h, H264SliceContext *sl,
  427. int *mb_type)
  428. {
  429. int b8_stride = 2;
  430. int b4_stride = h->b_stride;
  431. int mb_xy = sl->mb_xy, mb_y = sl->mb_y;
  432. int mb_type_col[2];
  433. const int16_t (*l1mv0)[2], (*l1mv1)[2];
  434. const int8_t *l1ref0, *l1ref1;
  435. const int is_b8x8 = IS_8X8(*mb_type);
  436. unsigned int sub_mb_type;
  437. int i8, i4;
  438. assert(sl->ref_list[1][0].reference & 3);
  439. await_reference_mb_row(h, &sl->ref_list[1][0],
  440. sl->mb_y + !!IS_INTERLACED(*mb_type));
  441. if (IS_INTERLACED(sl->ref_list[1][0].parent->mb_type[mb_xy])) { // AFL/AFR/FR/FL -> AFL/FL
  442. if (!IS_INTERLACED(*mb_type)) { // AFR/FR -> AFL/FL
  443. mb_y = (sl->mb_y & ~1) + sl->col_parity;
  444. mb_xy = sl->mb_x +
  445. ((sl->mb_y & ~1) + sl->col_parity) * h->mb_stride;
  446. b8_stride = 0;
  447. } else {
  448. mb_y += sl->col_fieldoff;
  449. mb_xy += h->mb_stride * sl->col_fieldoff; // non-zero for FL -> FL & differ parity
  450. }
  451. goto single_col;
  452. } else { // AFL/AFR/FR/FL -> AFR/FR
  453. if (IS_INTERLACED(*mb_type)) { // AFL /FL -> AFR/FR
  454. mb_y = sl->mb_y & ~1;
  455. mb_xy = sl->mb_x + (sl->mb_y & ~1) * h->mb_stride;
  456. mb_type_col[0] = sl->ref_list[1][0].parent->mb_type[mb_xy];
  457. mb_type_col[1] = sl->ref_list[1][0].parent->mb_type[mb_xy + h->mb_stride];
  458. b8_stride = 2 + 4 * h->mb_stride;
  459. b4_stride *= 6;
  460. if (IS_INTERLACED(mb_type_col[0]) !=
  461. IS_INTERLACED(mb_type_col[1])) {
  462. mb_type_col[0] &= ~MB_TYPE_INTERLACED;
  463. mb_type_col[1] &= ~MB_TYPE_INTERLACED;
  464. }
  465. sub_mb_type = MB_TYPE_16x16 | MB_TYPE_P0L0 | MB_TYPE_P0L1 |
  466. MB_TYPE_DIRECT2; /* B_SUB_8x8 */
  467. if ((mb_type_col[0] & MB_TYPE_16x16_OR_INTRA) &&
  468. (mb_type_col[1] & MB_TYPE_16x16_OR_INTRA) &&
  469. !is_b8x8) {
  470. *mb_type |= MB_TYPE_16x8 | MB_TYPE_L0L1 |
  471. MB_TYPE_DIRECT2; /* B_16x8 */
  472. } else {
  473. *mb_type |= MB_TYPE_8x8 | MB_TYPE_L0L1;
  474. }
  475. } else { // AFR/FR -> AFR/FR
  476. single_col:
  477. mb_type_col[0] =
  478. mb_type_col[1] = sl->ref_list[1][0].parent->mb_type[mb_xy];
  479. sub_mb_type = MB_TYPE_16x16 | MB_TYPE_P0L0 | MB_TYPE_P0L1 |
  480. MB_TYPE_DIRECT2; /* B_SUB_8x8 */
  481. if (!is_b8x8 && (mb_type_col[0] & MB_TYPE_16x16_OR_INTRA)) {
  482. *mb_type |= MB_TYPE_16x16 | MB_TYPE_P0L0 | MB_TYPE_P0L1 |
  483. MB_TYPE_DIRECT2; /* B_16x16 */
  484. } else if (!is_b8x8 &&
  485. (mb_type_col[0] & (MB_TYPE_16x8 | MB_TYPE_8x16))) {
  486. *mb_type |= MB_TYPE_L0L1 | MB_TYPE_DIRECT2 |
  487. (mb_type_col[0] & (MB_TYPE_16x8 | MB_TYPE_8x16));
  488. } else {
  489. if (!h->ps.sps->direct_8x8_inference_flag) {
  490. /* FIXME: save sub mb types from previous frames (or derive
  491. * from MVs) so we know exactly what block size to use */
  492. sub_mb_type = MB_TYPE_8x8 | MB_TYPE_P0L0 | MB_TYPE_P0L1 |
  493. MB_TYPE_DIRECT2; /* B_SUB_4x4 */
  494. }
  495. *mb_type |= MB_TYPE_8x8 | MB_TYPE_L0L1;
  496. }
  497. }
  498. }
  499. await_reference_mb_row(h, &sl->ref_list[1][0], mb_y);
  500. l1mv0 = (void*)&sl->ref_list[1][0].parent->motion_val[0][h->mb2b_xy[mb_xy]];
  501. l1mv1 = (void*)&sl->ref_list[1][0].parent->motion_val[1][h->mb2b_xy[mb_xy]];
  502. l1ref0 = &sl->ref_list[1][0].parent->ref_index[0][4 * mb_xy];
  503. l1ref1 = &sl->ref_list[1][0].parent->ref_index[1][4 * mb_xy];
  504. if (!b8_stride) {
  505. if (sl->mb_y & 1) {
  506. l1ref0 += 2;
  507. l1ref1 += 2;
  508. l1mv0 += 2 * b4_stride;
  509. l1mv1 += 2 * b4_stride;
  510. }
  511. }
  512. {
  513. const int *map_col_to_list0[2] = { sl->map_col_to_list0[0],
  514. sl->map_col_to_list0[1] };
  515. const int *dist_scale_factor = sl->dist_scale_factor;
  516. int ref_offset;
  517. if (FRAME_MBAFF(h) && IS_INTERLACED(*mb_type)) {
  518. map_col_to_list0[0] = sl->map_col_to_list0_field[sl->mb_y & 1][0];
  519. map_col_to_list0[1] = sl->map_col_to_list0_field[sl->mb_y & 1][1];
  520. dist_scale_factor = sl->dist_scale_factor_field[sl->mb_y & 1];
  521. }
  522. ref_offset = (sl->ref_list[1][0].parent->mbaff << 4) & (mb_type_col[0] >> 3);
  523. if (IS_INTERLACED(*mb_type) != IS_INTERLACED(mb_type_col[0])) {
  524. int y_shift = 2 * !IS_INTERLACED(*mb_type);
  525. assert(h->ps.sps->direct_8x8_inference_flag);
  526. for (i8 = 0; i8 < 4; i8++) {
  527. const int x8 = i8 & 1;
  528. const int y8 = i8 >> 1;
  529. int ref0, scale;
  530. const int16_t (*l1mv)[2] = l1mv0;
  531. if (is_b8x8 && !IS_DIRECT(sl->sub_mb_type[i8]))
  532. continue;
  533. sl->sub_mb_type[i8] = sub_mb_type;
  534. fill_rectangle(&sl->ref_cache[1][scan8[i8 * 4]], 2, 2, 8, 0, 1);
  535. if (IS_INTRA(mb_type_col[y8])) {
  536. fill_rectangle(&sl->ref_cache[0][scan8[i8 * 4]], 2, 2, 8, 0, 1);
  537. fill_rectangle(&sl->mv_cache[0][scan8[i8 * 4]], 2, 2, 8, 0, 4);
  538. fill_rectangle(&sl->mv_cache[1][scan8[i8 * 4]], 2, 2, 8, 0, 4);
  539. continue;
  540. }
  541. ref0 = l1ref0[x8 + y8 * b8_stride];
  542. if (ref0 >= 0)
  543. ref0 = map_col_to_list0[0][ref0 + ref_offset];
  544. else {
  545. ref0 = map_col_to_list0[1][l1ref1[x8 + y8 * b8_stride] +
  546. ref_offset];
  547. l1mv = l1mv1;
  548. }
  549. scale = dist_scale_factor[ref0];
  550. fill_rectangle(&sl->ref_cache[0][scan8[i8 * 4]], 2, 2, 8,
  551. ref0, 1);
  552. {
  553. const int16_t *mv_col = l1mv[x8 * 3 + y8 * b4_stride];
  554. int my_col = (mv_col[1] << y_shift) / 2;
  555. int mx = (scale * mv_col[0] + 128) >> 8;
  556. int my = (scale * my_col + 128) >> 8;
  557. fill_rectangle(&sl->mv_cache[0][scan8[i8 * 4]], 2, 2, 8,
  558. pack16to32(mx, my), 4);
  559. fill_rectangle(&sl->mv_cache[1][scan8[i8 * 4]], 2, 2, 8,
  560. pack16to32(mx - mv_col[0], my - my_col), 4);
  561. }
  562. }
  563. return;
  564. }
  565. /* one-to-one mv scaling */
  566. if (IS_16X16(*mb_type)) {
  567. int ref, mv0, mv1;
  568. fill_rectangle(&sl->ref_cache[1][scan8[0]], 4, 4, 8, 0, 1);
  569. if (IS_INTRA(mb_type_col[0])) {
  570. ref = mv0 = mv1 = 0;
  571. } else {
  572. const int ref0 = l1ref0[0] >= 0 ? map_col_to_list0[0][l1ref0[0] + ref_offset]
  573. : map_col_to_list0[1][l1ref1[0] + ref_offset];
  574. const int scale = dist_scale_factor[ref0];
  575. const int16_t *mv_col = l1ref0[0] >= 0 ? l1mv0[0] : l1mv1[0];
  576. int mv_l0[2];
  577. mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
  578. mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
  579. ref = ref0;
  580. mv0 = pack16to32(mv_l0[0], mv_l0[1]);
  581. mv1 = pack16to32(mv_l0[0] - mv_col[0], mv_l0[1] - mv_col[1]);
  582. }
  583. fill_rectangle(&sl->ref_cache[0][scan8[0]], 4, 4, 8, ref, 1);
  584. fill_rectangle(&sl->mv_cache[0][scan8[0]], 4, 4, 8, mv0, 4);
  585. fill_rectangle(&sl->mv_cache[1][scan8[0]], 4, 4, 8, mv1, 4);
  586. } else {
  587. for (i8 = 0; i8 < 4; i8++) {
  588. const int x8 = i8 & 1;
  589. const int y8 = i8 >> 1;
  590. int ref0, scale;
  591. const int16_t (*l1mv)[2] = l1mv0;
  592. if (is_b8x8 && !IS_DIRECT(sl->sub_mb_type[i8]))
  593. continue;
  594. sl->sub_mb_type[i8] = sub_mb_type;
  595. fill_rectangle(&sl->ref_cache[1][scan8[i8 * 4]], 2, 2, 8, 0, 1);
  596. if (IS_INTRA(mb_type_col[0])) {
  597. fill_rectangle(&sl->ref_cache[0][scan8[i8 * 4]], 2, 2, 8, 0, 1);
  598. fill_rectangle(&sl->mv_cache[0][scan8[i8 * 4]], 2, 2, 8, 0, 4);
  599. fill_rectangle(&sl->mv_cache[1][scan8[i8 * 4]], 2, 2, 8, 0, 4);
  600. continue;
  601. }
  602. assert(b8_stride == 2);
  603. ref0 = l1ref0[i8];
  604. if (ref0 >= 0)
  605. ref0 = map_col_to_list0[0][ref0 + ref_offset];
  606. else {
  607. ref0 = map_col_to_list0[1][l1ref1[i8] + ref_offset];
  608. l1mv = l1mv1;
  609. }
  610. scale = dist_scale_factor[ref0];
  611. fill_rectangle(&sl->ref_cache[0][scan8[i8 * 4]], 2, 2, 8,
  612. ref0, 1);
  613. if (IS_SUB_8X8(sub_mb_type)) {
  614. const int16_t *mv_col = l1mv[x8 * 3 + y8 * 3 * b4_stride];
  615. int mx = (scale * mv_col[0] + 128) >> 8;
  616. int my = (scale * mv_col[1] + 128) >> 8;
  617. fill_rectangle(&sl->mv_cache[0][scan8[i8 * 4]], 2, 2, 8,
  618. pack16to32(mx, my), 4);
  619. fill_rectangle(&sl->mv_cache[1][scan8[i8 * 4]], 2, 2, 8,
  620. pack16to32(mx - mv_col[0], my - mv_col[1]), 4);
  621. } else {
  622. for (i4 = 0; i4 < 4; i4++) {
  623. const int16_t *mv_col = l1mv[x8 * 2 + (i4 & 1) +
  624. (y8 * 2 + (i4 >> 1)) * b4_stride];
  625. int16_t *mv_l0 = sl->mv_cache[0][scan8[i8 * 4 + i4]];
  626. mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
  627. mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
  628. AV_WN32A(sl->mv_cache[1][scan8[i8 * 4 + i4]],
  629. pack16to32(mv_l0[0] - mv_col[0],
  630. mv_l0[1] - mv_col[1]));
  631. }
  632. }
  633. }
  634. }
  635. }
  636. }
  637. void ff_h264_pred_direct_motion(const H264Context *const h, H264SliceContext *sl,
  638. int *mb_type)
  639. {
  640. if (sl->direct_spatial_mv_pred)
  641. pred_spatial_direct_motion(h, sl, mb_type);
  642. else
  643. pred_temp_direct_motion(h, sl, mb_type);
  644. }