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.

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