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.

706 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 / 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(H264Context *const h, 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(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(h, 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(h, sl, poc, poc1, i);
  63. }
  64. static void fill_colmap(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(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(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(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 = h->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. h->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. av_assert2(ref[list] < (sl->ref_count[list] << !!FRAME_MBAFF(h)));
  207. } else {
  208. int mask = ~(MB_TYPE_L0 << (2 * list));
  209. mv[list] = 0;
  210. ref[list] = -1;
  211. if (!is_b8x8)
  212. *mb_type &= mask;
  213. sub_mb_type &= mask;
  214. }
  215. }
  216. if (ref[0] < 0 && ref[1] < 0) {
  217. ref[0] = ref[1] = 0;
  218. if (!is_b8x8)
  219. *mb_type |= MB_TYPE_L0L1;
  220. sub_mb_type |= MB_TYPE_L0L1;
  221. }
  222. if (!(is_b8x8 | mv[0] | mv[1])) {
  223. fill_rectangle(&sl->ref_cache[0][scan8[0]], 4, 4, 8, (uint8_t)ref[0], 1);
  224. fill_rectangle(&sl->ref_cache[1][scan8[0]], 4, 4, 8, (uint8_t)ref[1], 1);
  225. fill_rectangle(&sl->mv_cache[0][scan8[0]], 4, 4, 8, 0, 4);
  226. fill_rectangle(&sl->mv_cache[1][scan8[0]], 4, 4, 8, 0, 4);
  227. *mb_type = (*mb_type & ~(MB_TYPE_8x8 | MB_TYPE_16x8 | MB_TYPE_8x16 |
  228. MB_TYPE_P1L0 | MB_TYPE_P1L1)) |
  229. MB_TYPE_16x16 | MB_TYPE_DIRECT2;
  230. return;
  231. }
  232. if (IS_INTERLACED(sl->ref_list[1][0].mb_type[mb_xy])) { // AFL/AFR/FR/FL -> AFL/FL
  233. if (!IS_INTERLACED(*mb_type)) { // AFR/FR -> AFL/FL
  234. mb_y = (h->mb_y & ~1) + sl->col_parity;
  235. mb_xy = h->mb_x +
  236. ((h->mb_y & ~1) + sl->col_parity) * h->mb_stride;
  237. b8_stride = 0;
  238. } else {
  239. mb_y += sl->col_fieldoff;
  240. mb_xy += h->mb_stride * sl->col_fieldoff; // non-zero for FL -> FL & differ parity
  241. }
  242. goto single_col;
  243. } else { // AFL/AFR/FR/FL -> AFR/FR
  244. if (IS_INTERLACED(*mb_type)) { // AFL /FL -> AFR/FR
  245. mb_y = h->mb_y & ~1;
  246. mb_xy = (h->mb_y & ~1) * h->mb_stride + h->mb_x;
  247. mb_type_col[0] = sl->ref_list[1][0].mb_type[mb_xy];
  248. mb_type_col[1] = sl->ref_list[1][0].mb_type[mb_xy + h->mb_stride];
  249. b8_stride = 2 + 4 * h->mb_stride;
  250. b4_stride *= 6;
  251. if (IS_INTERLACED(mb_type_col[0]) !=
  252. IS_INTERLACED(mb_type_col[1])) {
  253. mb_type_col[0] &= ~MB_TYPE_INTERLACED;
  254. mb_type_col[1] &= ~MB_TYPE_INTERLACED;
  255. }
  256. sub_mb_type |= MB_TYPE_16x16 | MB_TYPE_DIRECT2; /* B_SUB_8x8 */
  257. if ((mb_type_col[0] & MB_TYPE_16x16_OR_INTRA) &&
  258. (mb_type_col[1] & MB_TYPE_16x16_OR_INTRA) &&
  259. !is_b8x8) {
  260. *mb_type |= MB_TYPE_16x8 | MB_TYPE_DIRECT2; /* B_16x8 */
  261. } else {
  262. *mb_type |= MB_TYPE_8x8;
  263. }
  264. } else { // AFR/FR -> AFR/FR
  265. single_col:
  266. mb_type_col[0] =
  267. mb_type_col[1] = sl->ref_list[1][0].mb_type[mb_xy];
  268. sub_mb_type |= MB_TYPE_16x16 | MB_TYPE_DIRECT2; /* B_SUB_8x8 */
  269. if (!is_b8x8 && (mb_type_col[0] & MB_TYPE_16x16_OR_INTRA)) {
  270. *mb_type |= MB_TYPE_16x16 | MB_TYPE_DIRECT2; /* B_16x16 */
  271. } else if (!is_b8x8 &&
  272. (mb_type_col[0] & (MB_TYPE_16x8 | MB_TYPE_8x16))) {
  273. *mb_type |= MB_TYPE_DIRECT2 |
  274. (mb_type_col[0] & (MB_TYPE_16x8 | MB_TYPE_8x16));
  275. } else {
  276. if (!h->sps.direct_8x8_inference_flag) {
  277. /* FIXME: Save sub mb types from previous frames (or derive
  278. * from MVs) so we know exactly what block size to use. */
  279. sub_mb_type += (MB_TYPE_8x8 - MB_TYPE_16x16); /* B_SUB_4x4 */
  280. }
  281. *mb_type |= MB_TYPE_8x8;
  282. }
  283. }
  284. }
  285. await_reference_mb_row(h, &sl->ref_list[1][0], mb_y);
  286. l1mv0 = (void*)&sl->ref_list[1][0].motion_val[0][h->mb2b_xy[mb_xy]];
  287. l1mv1 = (void*)&sl->ref_list[1][0].motion_val[1][h->mb2b_xy[mb_xy]];
  288. l1ref0 = &sl->ref_list[1][0].ref_index[0][4 * mb_xy];
  289. l1ref1 = &sl->ref_list[1][0].ref_index[1][4 * mb_xy];
  290. if (!b8_stride) {
  291. if (h->mb_y & 1) {
  292. l1ref0 += 2;
  293. l1ref1 += 2;
  294. l1mv0 += 2 * b4_stride;
  295. l1mv1 += 2 * b4_stride;
  296. }
  297. }
  298. if (IS_INTERLACED(*mb_type) != IS_INTERLACED(mb_type_col[0])) {
  299. int n = 0;
  300. for (i8 = 0; i8 < 4; i8++) {
  301. int x8 = i8 & 1;
  302. int y8 = i8 >> 1;
  303. int xy8 = x8 + y8 * b8_stride;
  304. int xy4 = x8 * 3 + y8 * b4_stride;
  305. int a, b;
  306. if (is_b8x8 && !IS_DIRECT(sl->sub_mb_type[i8]))
  307. continue;
  308. sl->sub_mb_type[i8] = sub_mb_type;
  309. fill_rectangle(&sl->ref_cache[0][scan8[i8 * 4]], 2, 2, 8,
  310. (uint8_t)ref[0], 1);
  311. fill_rectangle(&sl->ref_cache[1][scan8[i8 * 4]], 2, 2, 8,
  312. (uint8_t)ref[1], 1);
  313. if (!IS_INTRA(mb_type_col[y8]) && !sl->ref_list[1][0].long_ref &&
  314. ((l1ref0[xy8] == 0 &&
  315. FFABS(l1mv0[xy4][0]) <= 1 &&
  316. FFABS(l1mv0[xy4][1]) <= 1) ||
  317. (l1ref0[xy8] < 0 &&
  318. l1ref1[xy8] == 0 &&
  319. FFABS(l1mv1[xy4][0]) <= 1 &&
  320. FFABS(l1mv1[xy4][1]) <= 1))) {
  321. a =
  322. b = 0;
  323. if (ref[0] > 0)
  324. a = mv[0];
  325. if (ref[1] > 0)
  326. b = mv[1];
  327. n++;
  328. } else {
  329. a = mv[0];
  330. b = mv[1];
  331. }
  332. fill_rectangle(&sl->mv_cache[0][scan8[i8 * 4]], 2, 2, 8, a, 4);
  333. fill_rectangle(&sl->mv_cache[1][scan8[i8 * 4]], 2, 2, 8, b, 4);
  334. }
  335. if (!is_b8x8 && !(n & 3))
  336. *mb_type = (*mb_type & ~(MB_TYPE_8x8 | MB_TYPE_16x8 | MB_TYPE_8x16 |
  337. MB_TYPE_P1L0 | MB_TYPE_P1L1)) |
  338. MB_TYPE_16x16 | MB_TYPE_DIRECT2;
  339. } else if (IS_16X16(*mb_type)) {
  340. int a, b;
  341. fill_rectangle(&sl->ref_cache[0][scan8[0]], 4, 4, 8, (uint8_t)ref[0], 1);
  342. fill_rectangle(&sl->ref_cache[1][scan8[0]], 4, 4, 8, (uint8_t)ref[1], 1);
  343. if (!IS_INTRA(mb_type_col[0]) && !sl->ref_list[1][0].long_ref &&
  344. ((l1ref0[0] == 0 &&
  345. FFABS(l1mv0[0][0]) <= 1 &&
  346. FFABS(l1mv0[0][1]) <= 1) ||
  347. (l1ref0[0] < 0 && !l1ref1[0] &&
  348. FFABS(l1mv1[0][0]) <= 1 &&
  349. FFABS(l1mv1[0][1]) <= 1 &&
  350. h->x264_build > 33U))) {
  351. a = b = 0;
  352. if (ref[0] > 0)
  353. a = mv[0];
  354. if (ref[1] > 0)
  355. b = mv[1];
  356. } else {
  357. a = mv[0];
  358. b = mv[1];
  359. }
  360. fill_rectangle(&sl->mv_cache[0][scan8[0]], 4, 4, 8, a, 4);
  361. fill_rectangle(&sl->mv_cache[1][scan8[0]], 4, 4, 8, b, 4);
  362. } else {
  363. int n = 0;
  364. for (i8 = 0; i8 < 4; i8++) {
  365. const int x8 = i8 & 1;
  366. const int y8 = i8 >> 1;
  367. if (is_b8x8 && !IS_DIRECT(sl->sub_mb_type[i8]))
  368. continue;
  369. sl->sub_mb_type[i8] = sub_mb_type;
  370. fill_rectangle(&sl->mv_cache[0][scan8[i8 * 4]], 2, 2, 8, mv[0], 4);
  371. fill_rectangle(&sl->mv_cache[1][scan8[i8 * 4]], 2, 2, 8, mv[1], 4);
  372. fill_rectangle(&sl->ref_cache[0][scan8[i8 * 4]], 2, 2, 8,
  373. (uint8_t)ref[0], 1);
  374. fill_rectangle(&sl->ref_cache[1][scan8[i8 * 4]], 2, 2, 8,
  375. (uint8_t)ref[1], 1);
  376. assert(b8_stride == 2);
  377. /* col_zero_flag */
  378. if (!IS_INTRA(mb_type_col[0]) && !sl->ref_list[1][0].long_ref &&
  379. (l1ref0[i8] == 0 ||
  380. (l1ref0[i8] < 0 &&
  381. l1ref1[i8] == 0 &&
  382. h->x264_build > 33U))) {
  383. const int16_t (*l1mv)[2] = l1ref0[i8] == 0 ? l1mv0 : l1mv1;
  384. if (IS_SUB_8X8(sub_mb_type)) {
  385. const int16_t *mv_col = l1mv[x8 * 3 + y8 * 3 * b4_stride];
  386. if (FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1) {
  387. if (ref[0] == 0)
  388. fill_rectangle(&sl->mv_cache[0][scan8[i8 * 4]], 2, 2,
  389. 8, 0, 4);
  390. if (ref[1] == 0)
  391. fill_rectangle(&sl->mv_cache[1][scan8[i8 * 4]], 2, 2,
  392. 8, 0, 4);
  393. n += 4;
  394. }
  395. } else {
  396. int m = 0;
  397. for (i4 = 0; i4 < 4; i4++) {
  398. const int16_t *mv_col = l1mv[x8 * 2 + (i4 & 1) +
  399. (y8 * 2 + (i4 >> 1)) * b4_stride];
  400. if (FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1) {
  401. if (ref[0] == 0)
  402. AV_ZERO32(sl->mv_cache[0][scan8[i8 * 4 + i4]]);
  403. if (ref[1] == 0)
  404. AV_ZERO32(sl->mv_cache[1][scan8[i8 * 4 + i4]]);
  405. m++;
  406. }
  407. }
  408. if (!(m & 3))
  409. sl->sub_mb_type[i8] += MB_TYPE_16x16 - MB_TYPE_8x8;
  410. n += m;
  411. }
  412. }
  413. }
  414. if (!is_b8x8 && !(n & 15))
  415. *mb_type = (*mb_type & ~(MB_TYPE_8x8 | MB_TYPE_16x8 | MB_TYPE_8x16 |
  416. MB_TYPE_P1L0 | MB_TYPE_P1L1)) |
  417. MB_TYPE_16x16 | MB_TYPE_DIRECT2;
  418. }
  419. }
  420. static void pred_temp_direct_motion(H264Context *const h, H264SliceContext *sl,
  421. int *mb_type)
  422. {
  423. int b8_stride = 2;
  424. int b4_stride = h->b_stride;
  425. int mb_xy = sl->mb_xy, mb_y = h->mb_y;
  426. int mb_type_col[2];
  427. const int16_t (*l1mv0)[2], (*l1mv1)[2];
  428. const int8_t *l1ref0, *l1ref1;
  429. const int is_b8x8 = IS_8X8(*mb_type);
  430. unsigned int sub_mb_type;
  431. int i8, i4;
  432. assert(sl->ref_list[1][0].reference & 3);
  433. await_reference_mb_row(h, &sl->ref_list[1][0],
  434. h->mb_y + !!IS_INTERLACED(*mb_type));
  435. if (IS_INTERLACED(sl->ref_list[1][0].mb_type[mb_xy])) { // AFL/AFR/FR/FL -> AFL/FL
  436. if (!IS_INTERLACED(*mb_type)) { // AFR/FR -> AFL/FL
  437. mb_y = (h->mb_y & ~1) + sl->col_parity;
  438. mb_xy = h->mb_x +
  439. ((h->mb_y & ~1) + sl->col_parity) * h->mb_stride;
  440. b8_stride = 0;
  441. } else {
  442. mb_y += sl->col_fieldoff;
  443. mb_xy += h->mb_stride * sl->col_fieldoff; // non-zero for FL -> FL & differ parity
  444. }
  445. goto single_col;
  446. } else { // AFL/AFR/FR/FL -> AFR/FR
  447. if (IS_INTERLACED(*mb_type)) { // AFL /FL -> AFR/FR
  448. mb_y = h->mb_y & ~1;
  449. mb_xy = h->mb_x + (h->mb_y & ~1) * h->mb_stride;
  450. mb_type_col[0] = sl->ref_list[1][0].mb_type[mb_xy];
  451. mb_type_col[1] = sl->ref_list[1][0].mb_type[mb_xy + h->mb_stride];
  452. b8_stride = 2 + 4 * h->mb_stride;
  453. b4_stride *= 6;
  454. if (IS_INTERLACED(mb_type_col[0]) !=
  455. IS_INTERLACED(mb_type_col[1])) {
  456. mb_type_col[0] &= ~MB_TYPE_INTERLACED;
  457. mb_type_col[1] &= ~MB_TYPE_INTERLACED;
  458. }
  459. sub_mb_type = MB_TYPE_16x16 | MB_TYPE_P0L0 | MB_TYPE_P0L1 |
  460. MB_TYPE_DIRECT2; /* B_SUB_8x8 */
  461. if ((mb_type_col[0] & MB_TYPE_16x16_OR_INTRA) &&
  462. (mb_type_col[1] & MB_TYPE_16x16_OR_INTRA) &&
  463. !is_b8x8) {
  464. *mb_type |= MB_TYPE_16x8 | MB_TYPE_L0L1 |
  465. MB_TYPE_DIRECT2; /* B_16x8 */
  466. } else {
  467. *mb_type |= MB_TYPE_8x8 | MB_TYPE_L0L1;
  468. }
  469. } else { // AFR/FR -> AFR/FR
  470. single_col:
  471. mb_type_col[0] =
  472. mb_type_col[1] = sl->ref_list[1][0].mb_type[mb_xy];
  473. sub_mb_type = MB_TYPE_16x16 | MB_TYPE_P0L0 | MB_TYPE_P0L1 |
  474. MB_TYPE_DIRECT2; /* B_SUB_8x8 */
  475. if (!is_b8x8 && (mb_type_col[0] & MB_TYPE_16x16_OR_INTRA)) {
  476. *mb_type |= MB_TYPE_16x16 | MB_TYPE_P0L0 | MB_TYPE_P0L1 |
  477. MB_TYPE_DIRECT2; /* B_16x16 */
  478. } else if (!is_b8x8 &&
  479. (mb_type_col[0] & (MB_TYPE_16x8 | MB_TYPE_8x16))) {
  480. *mb_type |= MB_TYPE_L0L1 | MB_TYPE_DIRECT2 |
  481. (mb_type_col[0] & (MB_TYPE_16x8 | MB_TYPE_8x16));
  482. } else {
  483. if (!h->sps.direct_8x8_inference_flag) {
  484. /* FIXME: save sub mb types from previous frames (or derive
  485. * from MVs) so we know exactly what block size to use */
  486. sub_mb_type = MB_TYPE_8x8 | MB_TYPE_P0L0 | MB_TYPE_P0L1 |
  487. MB_TYPE_DIRECT2; /* B_SUB_4x4 */
  488. }
  489. *mb_type |= MB_TYPE_8x8 | MB_TYPE_L0L1;
  490. }
  491. }
  492. }
  493. await_reference_mb_row(h, &sl->ref_list[1][0], mb_y);
  494. l1mv0 = (void*)&sl->ref_list[1][0].motion_val[0][h->mb2b_xy[mb_xy]];
  495. l1mv1 = (void*)&sl->ref_list[1][0].motion_val[1][h->mb2b_xy[mb_xy]];
  496. l1ref0 = &sl->ref_list[1][0].ref_index[0][4 * mb_xy];
  497. l1ref1 = &sl->ref_list[1][0].ref_index[1][4 * mb_xy];
  498. if (!b8_stride) {
  499. if (h->mb_y & 1) {
  500. l1ref0 += 2;
  501. l1ref1 += 2;
  502. l1mv0 += 2 * b4_stride;
  503. l1mv1 += 2 * b4_stride;
  504. }
  505. }
  506. {
  507. const int *map_col_to_list0[2] = { sl->map_col_to_list0[0],
  508. sl->map_col_to_list0[1] };
  509. const int *dist_scale_factor = sl->dist_scale_factor;
  510. int ref_offset;
  511. if (FRAME_MBAFF(h) && IS_INTERLACED(*mb_type)) {
  512. map_col_to_list0[0] = sl->map_col_to_list0_field[h->mb_y & 1][0];
  513. map_col_to_list0[1] = sl->map_col_to_list0_field[h->mb_y & 1][1];
  514. dist_scale_factor = sl->dist_scale_factor_field[h->mb_y & 1];
  515. }
  516. ref_offset = (sl->ref_list[1][0].mbaff << 4) & (mb_type_col[0] >> 3);
  517. if (IS_INTERLACED(*mb_type) != IS_INTERLACED(mb_type_col[0])) {
  518. int y_shift = 2 * !IS_INTERLACED(*mb_type);
  519. assert(h->sps.direct_8x8_inference_flag);
  520. for (i8 = 0; i8 < 4; i8++) {
  521. const int x8 = i8 & 1;
  522. const int y8 = i8 >> 1;
  523. int ref0, scale;
  524. const int16_t (*l1mv)[2] = l1mv0;
  525. if (is_b8x8 && !IS_DIRECT(sl->sub_mb_type[i8]))
  526. continue;
  527. sl->sub_mb_type[i8] = sub_mb_type;
  528. fill_rectangle(&sl->ref_cache[1][scan8[i8 * 4]], 2, 2, 8, 0, 1);
  529. if (IS_INTRA(mb_type_col[y8])) {
  530. fill_rectangle(&sl->ref_cache[0][scan8[i8 * 4]], 2, 2, 8, 0, 1);
  531. fill_rectangle(&sl->mv_cache[0][scan8[i8 * 4]], 2, 2, 8, 0, 4);
  532. fill_rectangle(&sl->mv_cache[1][scan8[i8 * 4]], 2, 2, 8, 0, 4);
  533. continue;
  534. }
  535. ref0 = l1ref0[x8 + y8 * b8_stride];
  536. if (ref0 >= 0)
  537. ref0 = map_col_to_list0[0][ref0 + ref_offset];
  538. else {
  539. ref0 = map_col_to_list0[1][l1ref1[x8 + y8 * b8_stride] +
  540. ref_offset];
  541. l1mv = l1mv1;
  542. }
  543. scale = dist_scale_factor[ref0];
  544. fill_rectangle(&sl->ref_cache[0][scan8[i8 * 4]], 2, 2, 8,
  545. ref0, 1);
  546. {
  547. const int16_t *mv_col = l1mv[x8 * 3 + y8 * b4_stride];
  548. int my_col = (mv_col[1] << y_shift) / 2;
  549. int mx = (scale * mv_col[0] + 128) >> 8;
  550. int my = (scale * my_col + 128) >> 8;
  551. fill_rectangle(&sl->mv_cache[0][scan8[i8 * 4]], 2, 2, 8,
  552. pack16to32(mx, my), 4);
  553. fill_rectangle(&sl->mv_cache[1][scan8[i8 * 4]], 2, 2, 8,
  554. pack16to32(mx - mv_col[0], my - my_col), 4);
  555. }
  556. }
  557. return;
  558. }
  559. /* one-to-one mv scaling */
  560. if (IS_16X16(*mb_type)) {
  561. int ref, mv0, mv1;
  562. fill_rectangle(&sl->ref_cache[1][scan8[0]], 4, 4, 8, 0, 1);
  563. if (IS_INTRA(mb_type_col[0])) {
  564. ref = mv0 = mv1 = 0;
  565. } else {
  566. const int ref0 = l1ref0[0] >= 0 ? map_col_to_list0[0][l1ref0[0] + ref_offset]
  567. : map_col_to_list0[1][l1ref1[0] + ref_offset];
  568. const int scale = dist_scale_factor[ref0];
  569. const int16_t *mv_col = l1ref0[0] >= 0 ? l1mv0[0] : l1mv1[0];
  570. int mv_l0[2];
  571. mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
  572. mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
  573. ref = ref0;
  574. mv0 = pack16to32(mv_l0[0], mv_l0[1]);
  575. mv1 = pack16to32(mv_l0[0] - mv_col[0], mv_l0[1] - mv_col[1]);
  576. }
  577. fill_rectangle(&sl->ref_cache[0][scan8[0]], 4, 4, 8, ref, 1);
  578. fill_rectangle(&sl->mv_cache[0][scan8[0]], 4, 4, 8, mv0, 4);
  579. fill_rectangle(&sl->mv_cache[1][scan8[0]], 4, 4, 8, mv1, 4);
  580. } else {
  581. for (i8 = 0; i8 < 4; i8++) {
  582. const int x8 = i8 & 1;
  583. const int y8 = i8 >> 1;
  584. int ref0, scale;
  585. const int16_t (*l1mv)[2] = l1mv0;
  586. if (is_b8x8 && !IS_DIRECT(sl->sub_mb_type[i8]))
  587. continue;
  588. sl->sub_mb_type[i8] = sub_mb_type;
  589. fill_rectangle(&sl->ref_cache[1][scan8[i8 * 4]], 2, 2, 8, 0, 1);
  590. if (IS_INTRA(mb_type_col[0])) {
  591. fill_rectangle(&sl->ref_cache[0][scan8[i8 * 4]], 2, 2, 8, 0, 1);
  592. fill_rectangle(&sl->mv_cache[0][scan8[i8 * 4]], 2, 2, 8, 0, 4);
  593. fill_rectangle(&sl->mv_cache[1][scan8[i8 * 4]], 2, 2, 8, 0, 4);
  594. continue;
  595. }
  596. assert(b8_stride == 2);
  597. ref0 = l1ref0[i8];
  598. if (ref0 >= 0)
  599. ref0 = map_col_to_list0[0][ref0 + ref_offset];
  600. else {
  601. ref0 = map_col_to_list0[1][l1ref1[i8] + ref_offset];
  602. l1mv = l1mv1;
  603. }
  604. scale = dist_scale_factor[ref0];
  605. fill_rectangle(&sl->ref_cache[0][scan8[i8 * 4]], 2, 2, 8,
  606. ref0, 1);
  607. if (IS_SUB_8X8(sub_mb_type)) {
  608. const int16_t *mv_col = l1mv[x8 * 3 + y8 * 3 * b4_stride];
  609. int mx = (scale * mv_col[0] + 128) >> 8;
  610. int my = (scale * mv_col[1] + 128) >> 8;
  611. fill_rectangle(&sl->mv_cache[0][scan8[i8 * 4]], 2, 2, 8,
  612. pack16to32(mx, my), 4);
  613. fill_rectangle(&sl->mv_cache[1][scan8[i8 * 4]], 2, 2, 8,
  614. pack16to32(mx - mv_col[0], my - mv_col[1]), 4);
  615. } else {
  616. for (i4 = 0; i4 < 4; i4++) {
  617. const int16_t *mv_col = l1mv[x8 * 2 + (i4 & 1) +
  618. (y8 * 2 + (i4 >> 1)) * b4_stride];
  619. int16_t *mv_l0 = sl->mv_cache[0][scan8[i8 * 4 + i4]];
  620. mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
  621. mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
  622. AV_WN32A(sl->mv_cache[1][scan8[i8 * 4 + i4]],
  623. pack16to32(mv_l0[0] - mv_col[0],
  624. mv_l0[1] - mv_col[1]));
  625. }
  626. }
  627. }
  628. }
  629. }
  630. }
  631. void ff_h264_pred_direct_motion(H264Context *const h, H264SliceContext *sl,
  632. int *mb_type)
  633. {
  634. if (sl->direct_spatial_mv_pred)
  635. pred_spatial_direct_motion(h, sl, mb_type);
  636. else
  637. pred_temp_direct_motion(h, sl, mb_type);
  638. }