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.

729 lines
30KB

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