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.

703 lines
29KB

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