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.

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