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.

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