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.

702 lines
28KB

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