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.

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