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.

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