FFmpeg  4.4.7
h264_direct.c
Go to the documentation of this file.
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 /**
23  * @file
24  * H.264 / AVC / MPEG-4 part10 direct mb/block decoding.
25  * @author Michael Niedermayer <michaelni@gmx.at>
26  */
27 
28 #include "internal.h"
29 #include "avcodec.h"
30 #include "h264dec.h"
31 #include "h264_ps.h"
32 #include "mpegutils.h"
33 #include "rectangle.h"
34 #include "thread.h"
35 
36 #include <assert.h>
37 
39  int poc, int poc1, int i)
40 {
41  int poc0 = sl->ref_list[0][i].poc;
42  int64_t pocdiff = poc1 - (int64_t)poc0;
43  int td = av_clip_int8(pocdiff);
44 
45  if (pocdiff != (int)pocdiff)
46  avpriv_request_sample(sl->h264->avctx, "pocdiff overflow");
47 
48  if (td == 0 || sl->ref_list[0][i].parent->long_ref) {
49  return 256;
50  } else {
51  int64_t pocdiff0 = poc - (int64_t)poc0;
52  int tb = av_clip_int8(pocdiff0);
53  int tx = (16384 + (FFABS(td) >> 1)) / td;
54 
55  if (pocdiff0 != (int)pocdiff0)
56  av_log(sl->h264->avctx, AV_LOG_DEBUG, "pocdiff0 overflow\n");
57 
58  return av_clip_intp2((tb * tx + 32) >> 6, 10);
59  }
60 }
61 
63  H264SliceContext *sl)
64 {
65  const int poc = FIELD_PICTURE(h) ? h->cur_pic_ptr->field_poc[h->picture_structure == PICT_BOTTOM_FIELD]
66  : h->cur_pic_ptr->poc;
67  const int poc1 = sl->ref_list[1][0].poc;
68  int i, field;
69 
70  if (FRAME_MBAFF(h))
71  for (field = 0; field < 2; field++) {
72  const int poc = h->cur_pic_ptr->field_poc[field];
73  const int poc1 = sl->ref_list[1][0].parent->field_poc[field];
74  for (i = 0; i < 2 * sl->ref_count[0]; i++)
75  sl->dist_scale_factor_field[field][i ^ field] =
76  get_scale_factor(sl, poc, poc1, i + 16);
77  }
78 
79  for (i = 0; i < sl->ref_count[0]; i++)
80  sl->dist_scale_factor[i] = get_scale_factor(sl, poc, poc1, i);
81 }
82 
83 static void fill_colmap(const H264Context *h, H264SliceContext *sl,
84  int map[2][16 + 32], int list,
85  int field, int colfield, int mbafi)
86 {
87  H264Picture *const ref1 = sl->ref_list[1][0].parent;
88  int j, old_ref, rfield;
89  int start = mbafi ? 16 : 0;
90  int end = mbafi ? 16 + 2 * sl->ref_count[0] : sl->ref_count[0];
91  int interl = mbafi || h->picture_structure != PICT_FRAME;
92 
93  /* bogus; fills in for missing frames */
94  memset(map[list], 0, sizeof(map[list]));
95 
96  for (rfield = 0; rfield < 2; rfield++) {
97  for (old_ref = 0; old_ref < ref1->ref_count[colfield][list]; old_ref++) {
98  int poc = ref1->ref_poc[colfield][list][old_ref];
99 
100  if (!interl)
101  poc |= 3;
102  // FIXME: store all MBAFF references so this is not needed
103  else if (interl && (poc & 3) == 3)
104  poc = (poc & ~3) + rfield + 1;
105 
106  for (j = start; j < end; j++) {
107  if (4 * sl->ref_list[0][j].parent->frame_num +
108  (sl->ref_list[0][j].reference & 3) == poc) {
109  int cur_ref = mbafi ? (j - 16) ^ field : j;
110  if (ref1->mbaff)
111  map[list][2 * old_ref + (rfield ^ field) + 16] = cur_ref;
112  if (rfield == field || !interl)
113  map[list][old_ref] = cur_ref;
114  break;
115  }
116  }
117  }
118  }
119 }
120 
122 {
123  H264Ref *const ref1 = &sl->ref_list[1][0];
124  H264Picture *const cur = h->cur_pic_ptr;
125  int list, field;
126  int sidx = (h->picture_structure & 1) ^ 1;
127  int ref1sidx = (ref1->reference & 1) ^ 1;
128 
129  /* Updates to cur_pic are not safe once ff_thread_finish_setup() has been
130  * called (other threads may already be reading these fields). */
131  if (!h->setup_finished) {
132  for (list = 0; list < sl->list_count; list++) {
133  cur->ref_count[sidx][list] = sl->ref_count[list];
134  for (int j = 0; j < sl->ref_count[list]; j++)
135  cur->ref_poc[sidx][list][j] = 4 * sl->ref_list[list][j].parent->frame_num +
136  (sl->ref_list[list][j].reference & 3);
137  }
138 
139  if (h->picture_structure == PICT_FRAME) {
140  memcpy(cur->ref_count[1], cur->ref_count[0], sizeof(cur->ref_count[0]));
141  memcpy(cur->ref_poc[1], cur->ref_poc[0], sizeof(cur->ref_poc[0]));
142  }
143 
144  if (h->current_slice == 0) {
145  cur->mbaff = FRAME_MBAFF(h);
146  } else {
147  av_assert0(cur->mbaff == FRAME_MBAFF(h));
148  }
149  }
150 
151  sl->col_fieldoff = 0;
152 
153  if (sl->list_count != 2 || !sl->ref_count[1])
154  return;
155 
156  if (h->picture_structure == PICT_FRAME) {
157  int cur_poc = h->cur_pic_ptr->poc;
158  int *col_poc = sl->ref_list[1][0].parent->field_poc;
159  if (col_poc[0] == INT_MAX && col_poc[1] == INT_MAX) {
160  av_log(h->avctx, AV_LOG_ERROR, "co located POCs unavailable\n");
161  sl->col_parity = 1;
162  } else
163  sl->col_parity = (FFABS(col_poc[0] - (int64_t)cur_poc) >=
164  FFABS(col_poc[1] - (int64_t)cur_poc));
165  ref1sidx =
166  sidx = sl->col_parity;
167  // FL -> FL & differ parity
168  } else if (!(h->picture_structure & sl->ref_list[1][0].reference) &&
169  !sl->ref_list[1][0].parent->mbaff) {
170  sl->col_fieldoff = 2 * sl->ref_list[1][0].reference - 3;
171  }
172 
174  return;
175 
176  for (list = 0; list < 2; list++) {
177  fill_colmap(h, sl, sl->map_col_to_list0, list, sidx, ref1sidx, 0);
178  if (FRAME_MBAFF(h))
179  for (field = 0; field < 2; field++)
180  fill_colmap(h, sl, sl->map_col_to_list0_field[field], list, field,
181  field, 1);
182  }
183 }
184 
185 static void await_reference_mb_row(const H264Context *const h, H264Ref *ref,
186  int mb_y)
187 {
188  int ref_field = ref->reference - 1;
189  int ref_field_picture = ref->parent->field_picture;
190  int ref_height = 16 * h->mb_height >> ref_field_picture;
191 
192  if (!HAVE_THREADS || !(h->avctx->active_thread_type & FF_THREAD_FRAME))
193  return;
194 
195  /* FIXME: It can be safe to access mb stuff
196  * even if pixels aren't deblocked yet. */
197 
198  ff_thread_await_progress(&ref->parent->tf,
199  FFMIN(16 * mb_y >> ref_field_picture,
200  ref_height - 1),
201  ref_field_picture && ref_field);
202 }
203 
205  int *mb_type)
206 {
207  int b8_stride = 2;
208  int b4_stride = h->b_stride;
209  int mb_xy = sl->mb_xy, mb_y = sl->mb_y;
210  int mb_type_col[2];
211  const int16_t (*l1mv0)[2], (*l1mv1)[2];
212  const int8_t *l1ref0, *l1ref1;
213  const int is_b8x8 = IS_8X8(*mb_type);
214  unsigned int sub_mb_type = MB_TYPE_L0L1;
215  int i8, i4;
216  int ref[2];
217  int mv[2];
218  int list;
219 
220  assert(sl->ref_list[1][0].reference & 3);
221 
222  await_reference_mb_row(h, &sl->ref_list[1][0],
223  sl->mb_y + !!IS_INTERLACED(*mb_type));
224 
225 #define MB_TYPE_16x16_OR_INTRA (MB_TYPE_16x16 | MB_TYPE_INTRA4x4 | \
226  MB_TYPE_INTRA16x16 | MB_TYPE_INTRA_PCM)
227 
228  /* ref = min(neighbors) */
229  for (list = 0; list < 2; list++) {
230  int left_ref = sl->ref_cache[list][scan8[0] - 1];
231  int top_ref = sl->ref_cache[list][scan8[0] - 8];
232  int refc = sl->ref_cache[list][scan8[0] - 8 + 4];
233  const int16_t *C = sl->mv_cache[list][scan8[0] - 8 + 4];
234  if (refc == PART_NOT_AVAILABLE) {
235  refc = sl->ref_cache[list][scan8[0] - 8 - 1];
236  C = sl->mv_cache[list][scan8[0] - 8 - 1];
237  }
238  ref[list] = FFMIN3((unsigned)left_ref,
239  (unsigned)top_ref,
240  (unsigned)refc);
241  if (ref[list] >= 0) {
242  /* This is just pred_motion() but with the cases removed that
243  * cannot happen for direct blocks. */
244  const int16_t *const A = sl->mv_cache[list][scan8[0] - 1];
245  const int16_t *const B = sl->mv_cache[list][scan8[0] - 8];
246 
247  int match_count = (left_ref == ref[list]) +
248  (top_ref == ref[list]) +
249  (refc == ref[list]);
250 
251  if (match_count > 1) { // most common
252  mv[list] = pack16to32(mid_pred(A[0], B[0], C[0]),
253  mid_pred(A[1], B[1], C[1]));
254  } else {
255  assert(match_count == 1);
256  if (left_ref == ref[list])
257  mv[list] = AV_RN32A(A);
258  else if (top_ref == ref[list])
259  mv[list] = AV_RN32A(B);
260  else
261  mv[list] = AV_RN32A(C);
262  }
263  av_assert2(ref[list] < (sl->ref_count[list] << !!FRAME_MBAFF(h)));
264  } else {
265  int mask = ~(MB_TYPE_L0 << (2 * list));
266  mv[list] = 0;
267  ref[list] = -1;
268  if (!is_b8x8)
269  *mb_type &= mask;
270  sub_mb_type &= mask;
271  }
272  }
273  if (ref[0] < 0 && ref[1] < 0) {
274  ref[0] = ref[1] = 0;
275  if (!is_b8x8)
276  *mb_type |= MB_TYPE_L0L1;
277  sub_mb_type |= MB_TYPE_L0L1;
278  }
279 
280  if (!(is_b8x8 | mv[0] | mv[1])) {
281  fill_rectangle(&sl->ref_cache[0][scan8[0]], 4, 4, 8, (uint8_t)ref[0], 1);
282  fill_rectangle(&sl->ref_cache[1][scan8[0]], 4, 4, 8, (uint8_t)ref[1], 1);
283  fill_rectangle(&sl->mv_cache[0][scan8[0]], 4, 4, 8, 0, 4);
284  fill_rectangle(&sl->mv_cache[1][scan8[0]], 4, 4, 8, 0, 4);
285  *mb_type = (*mb_type & ~(MB_TYPE_8x8 | MB_TYPE_16x8 | MB_TYPE_8x16 |
288  return;
289  }
290 
291  if (IS_INTERLACED(sl->ref_list[1][0].parent->mb_type[mb_xy])) { // AFL/AFR/FR/FL -> AFL/FL
292  if (!IS_INTERLACED(*mb_type)) { // AFR/FR -> AFL/FL
293  mb_y = (sl->mb_y & ~1) + sl->col_parity;
294  mb_xy = sl->mb_x +
295  ((sl->mb_y & ~1) + sl->col_parity) * h->mb_stride;
296  b8_stride = 0;
297  } else {
298  mb_y += sl->col_fieldoff;
299  mb_xy += h->mb_stride * sl->col_fieldoff; // non-zero for FL -> FL & differ parity
300  }
301  goto single_col;
302  } else { // AFL/AFR/FR/FL -> AFR/FR
303  if (IS_INTERLACED(*mb_type)) { // AFL /FL -> AFR/FR
304  mb_y = sl->mb_y & ~1;
305  mb_xy = (sl->mb_y & ~1) * h->mb_stride + sl->mb_x;
306  mb_type_col[0] = sl->ref_list[1][0].parent->mb_type[mb_xy];
307  mb_type_col[1] = sl->ref_list[1][0].parent->mb_type[mb_xy + h->mb_stride];
308  b8_stride = 2 + 4 * h->mb_stride;
309  b4_stride *= 6;
310  if (IS_INTERLACED(mb_type_col[0]) !=
311  IS_INTERLACED(mb_type_col[1])) {
312  mb_type_col[0] &= ~MB_TYPE_INTERLACED;
313  mb_type_col[1] &= ~MB_TYPE_INTERLACED;
314  }
315 
316  sub_mb_type |= MB_TYPE_16x16 | MB_TYPE_DIRECT2; /* B_SUB_8x8 */
317  if ((mb_type_col[0] & MB_TYPE_16x16_OR_INTRA) &&
318  (mb_type_col[1] & MB_TYPE_16x16_OR_INTRA) &&
319  !is_b8x8) {
320  *mb_type |= MB_TYPE_16x8 | MB_TYPE_DIRECT2; /* B_16x8 */
321  } else {
322  *mb_type |= MB_TYPE_8x8;
323  }
324  } else { // AFR/FR -> AFR/FR
325 single_col:
326  mb_type_col[0] =
327  mb_type_col[1] = sl->ref_list[1][0].parent->mb_type[mb_xy];
328 
329  sub_mb_type |= MB_TYPE_16x16 | MB_TYPE_DIRECT2; /* B_SUB_8x8 */
330  if (!is_b8x8 && (mb_type_col[0] & MB_TYPE_16x16_OR_INTRA)) {
331  *mb_type |= MB_TYPE_16x16 | MB_TYPE_DIRECT2; /* B_16x16 */
332  } else if (!is_b8x8 &&
333  (mb_type_col[0] & (MB_TYPE_16x8 | MB_TYPE_8x16))) {
334  *mb_type |= MB_TYPE_DIRECT2 |
335  (mb_type_col[0] & (MB_TYPE_16x8 | MB_TYPE_8x16));
336  } else {
337  if (!h->ps.sps->direct_8x8_inference_flag) {
338  /* FIXME: Save sub mb types from previous frames (or derive
339  * from MVs) so we know exactly what block size to use. */
340  sub_mb_type += (MB_TYPE_8x8 - MB_TYPE_16x16); /* B_SUB_4x4 */
341  }
342  *mb_type |= MB_TYPE_8x8;
343  }
344  }
345  }
346 
347  await_reference_mb_row(h, &sl->ref_list[1][0], mb_y);
348 
349  l1mv0 = (void*)&sl->ref_list[1][0].parent->motion_val[0][h->mb2b_xy[mb_xy]];
350  l1mv1 = (void*)&sl->ref_list[1][0].parent->motion_val[1][h->mb2b_xy[mb_xy]];
351  l1ref0 = &sl->ref_list[1][0].parent->ref_index[0][4 * mb_xy];
352  l1ref1 = &sl->ref_list[1][0].parent->ref_index[1][4 * mb_xy];
353  if (!b8_stride) {
354  if (sl->mb_y & 1) {
355  l1ref0 += 2;
356  l1ref1 += 2;
357  l1mv0 += 2 * b4_stride;
358  l1mv1 += 2 * b4_stride;
359  }
360  }
361 
362  if (IS_INTERLACED(*mb_type) != IS_INTERLACED(mb_type_col[0])) {
363  int n = 0;
364  for (i8 = 0; i8 < 4; i8++) {
365  int x8 = i8 & 1;
366  int y8 = i8 >> 1;
367  int xy8 = x8 + y8 * b8_stride;
368  int xy4 = x8 * 3 + y8 * b4_stride;
369  int a, b;
370 
371  if (is_b8x8 && !IS_DIRECT(sl->sub_mb_type[i8]))
372  continue;
373  sl->sub_mb_type[i8] = sub_mb_type;
374 
375  fill_rectangle(&sl->ref_cache[0][scan8[i8 * 4]], 2, 2, 8,
376  (uint8_t)ref[0], 1);
377  fill_rectangle(&sl->ref_cache[1][scan8[i8 * 4]], 2, 2, 8,
378  (uint8_t)ref[1], 1);
379  if (!IS_INTRA(mb_type_col[y8]) && !sl->ref_list[1][0].parent->long_ref &&
380  ((l1ref0[xy8] == 0 &&
381  FFABS(l1mv0[xy4][0]) <= 1 &&
382  FFABS(l1mv0[xy4][1]) <= 1) ||
383  (l1ref0[xy8] < 0 &&
384  l1ref1[xy8] == 0 &&
385  FFABS(l1mv1[xy4][0]) <= 1 &&
386  FFABS(l1mv1[xy4][1]) <= 1))) {
387  a =
388  b = 0;
389  if (ref[0] > 0)
390  a = mv[0];
391  if (ref[1] > 0)
392  b = mv[1];
393  n++;
394  } else {
395  a = mv[0];
396  b = mv[1];
397  }
398  fill_rectangle(&sl->mv_cache[0][scan8[i8 * 4]], 2, 2, 8, a, 4);
399  fill_rectangle(&sl->mv_cache[1][scan8[i8 * 4]], 2, 2, 8, b, 4);
400  }
401  if (!is_b8x8 && !(n & 3))
402  *mb_type = (*mb_type & ~(MB_TYPE_8x8 | MB_TYPE_16x8 | MB_TYPE_8x16 |
405  } else if (IS_16X16(*mb_type)) {
406  int a, b;
407 
408  fill_rectangle(&sl->ref_cache[0][scan8[0]], 4, 4, 8, (uint8_t)ref[0], 1);
409  fill_rectangle(&sl->ref_cache[1][scan8[0]], 4, 4, 8, (uint8_t)ref[1], 1);
410  if (!IS_INTRA(mb_type_col[0]) && !sl->ref_list[1][0].parent->long_ref &&
411  ((l1ref0[0] == 0 &&
412  FFABS(l1mv0[0][0]) <= 1 &&
413  FFABS(l1mv0[0][1]) <= 1) ||
414  (l1ref0[0] < 0 && !l1ref1[0] &&
415  FFABS(l1mv1[0][0]) <= 1 &&
416  FFABS(l1mv1[0][1]) <= 1 &&
417  h->x264_build > 33U))) {
418  a = b = 0;
419  if (ref[0] > 0)
420  a = mv[0];
421  if (ref[1] > 0)
422  b = mv[1];
423  } else {
424  a = mv[0];
425  b = mv[1];
426  }
427  fill_rectangle(&sl->mv_cache[0][scan8[0]], 4, 4, 8, a, 4);
428  fill_rectangle(&sl->mv_cache[1][scan8[0]], 4, 4, 8, b, 4);
429  } else {
430  int n = 0;
431  for (i8 = 0; i8 < 4; i8++) {
432  const int x8 = i8 & 1;
433  const int y8 = i8 >> 1;
434 
435  if (is_b8x8 && !IS_DIRECT(sl->sub_mb_type[i8]))
436  continue;
437  sl->sub_mb_type[i8] = sub_mb_type;
438 
439  fill_rectangle(&sl->mv_cache[0][scan8[i8 * 4]], 2, 2, 8, mv[0], 4);
440  fill_rectangle(&sl->mv_cache[1][scan8[i8 * 4]], 2, 2, 8, mv[1], 4);
441  fill_rectangle(&sl->ref_cache[0][scan8[i8 * 4]], 2, 2, 8,
442  (uint8_t)ref[0], 1);
443  fill_rectangle(&sl->ref_cache[1][scan8[i8 * 4]], 2, 2, 8,
444  (uint8_t)ref[1], 1);
445 
446  assert(b8_stride == 2);
447  /* col_zero_flag */
448  if (!IS_INTRA(mb_type_col[0]) && !sl->ref_list[1][0].parent->long_ref &&
449  (l1ref0[i8] == 0 ||
450  (l1ref0[i8] < 0 &&
451  l1ref1[i8] == 0 &&
452  h->x264_build > 33U))) {
453  const int16_t (*l1mv)[2] = l1ref0[i8] == 0 ? l1mv0 : l1mv1;
454  if (IS_SUB_8X8(sub_mb_type)) {
455  const int16_t *mv_col = l1mv[x8 * 3 + y8 * 3 * b4_stride];
456  if (FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1) {
457  if (ref[0] == 0)
458  fill_rectangle(&sl->mv_cache[0][scan8[i8 * 4]], 2, 2,
459  8, 0, 4);
460  if (ref[1] == 0)
461  fill_rectangle(&sl->mv_cache[1][scan8[i8 * 4]], 2, 2,
462  8, 0, 4);
463  n += 4;
464  }
465  } else {
466  int m = 0;
467  for (i4 = 0; i4 < 4; i4++) {
468  const int16_t *mv_col = l1mv[x8 * 2 + (i4 & 1) +
469  (y8 * 2 + (i4 >> 1)) * b4_stride];
470  if (FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1) {
471  if (ref[0] == 0)
472  AV_ZERO32(sl->mv_cache[0][scan8[i8 * 4 + i4]]);
473  if (ref[1] == 0)
474  AV_ZERO32(sl->mv_cache[1][scan8[i8 * 4 + i4]]);
475  m++;
476  }
477  }
478  if (!(m & 3))
480  n += m;
481  }
482  }
483  }
484  if (!is_b8x8 && !(n & 15))
485  *mb_type = (*mb_type & ~(MB_TYPE_8x8 | MB_TYPE_16x8 | MB_TYPE_8x16 |
488  }
489 }
490 
492  int *mb_type)
493 {
494  int b8_stride = 2;
495  int b4_stride = h->b_stride;
496  int mb_xy = sl->mb_xy, mb_y = sl->mb_y;
497  int mb_type_col[2];
498  const int16_t (*l1mv0)[2], (*l1mv1)[2];
499  const int8_t *l1ref0, *l1ref1;
500  const int is_b8x8 = IS_8X8(*mb_type);
501  unsigned int sub_mb_type;
502  int i8, i4;
503 
504  assert(sl->ref_list[1][0].reference & 3);
505 
506  await_reference_mb_row(h, &sl->ref_list[1][0],
507  sl->mb_y + !!IS_INTERLACED(*mb_type));
508 
509  if (IS_INTERLACED(sl->ref_list[1][0].parent->mb_type[mb_xy])) { // AFL/AFR/FR/FL -> AFL/FL
510  if (!IS_INTERLACED(*mb_type)) { // AFR/FR -> AFL/FL
511  mb_y = (sl->mb_y & ~1) + sl->col_parity;
512  mb_xy = sl->mb_x +
513  ((sl->mb_y & ~1) + sl->col_parity) * h->mb_stride;
514  b8_stride = 0;
515  } else {
516  mb_y += sl->col_fieldoff;
517  mb_xy += h->mb_stride * sl->col_fieldoff; // non-zero for FL -> FL & differ parity
518  }
519  goto single_col;
520  } else { // AFL/AFR/FR/FL -> AFR/FR
521  if (IS_INTERLACED(*mb_type)) { // AFL /FL -> AFR/FR
522  mb_y = sl->mb_y & ~1;
523  mb_xy = sl->mb_x + (sl->mb_y & ~1) * h->mb_stride;
524  mb_type_col[0] = sl->ref_list[1][0].parent->mb_type[mb_xy];
525  mb_type_col[1] = sl->ref_list[1][0].parent->mb_type[mb_xy + h->mb_stride];
526  b8_stride = 2 + 4 * h->mb_stride;
527  b4_stride *= 6;
528  if (IS_INTERLACED(mb_type_col[0]) !=
529  IS_INTERLACED(mb_type_col[1])) {
530  mb_type_col[0] &= ~MB_TYPE_INTERLACED;
531  mb_type_col[1] &= ~MB_TYPE_INTERLACED;
532  }
533 
534  sub_mb_type = MB_TYPE_16x16 | MB_TYPE_P0L0 | MB_TYPE_P0L1 |
535  MB_TYPE_DIRECT2; /* B_SUB_8x8 */
536 
537  if ((mb_type_col[0] & MB_TYPE_16x16_OR_INTRA) &&
538  (mb_type_col[1] & MB_TYPE_16x16_OR_INTRA) &&
539  !is_b8x8) {
540  *mb_type |= MB_TYPE_16x8 | MB_TYPE_L0L1 |
541  MB_TYPE_DIRECT2; /* B_16x8 */
542  } else {
543  *mb_type |= MB_TYPE_8x8 | MB_TYPE_L0L1;
544  }
545  } else { // AFR/FR -> AFR/FR
546 single_col:
547  mb_type_col[0] =
548  mb_type_col[1] = sl->ref_list[1][0].parent->mb_type[mb_xy];
549 
550  sub_mb_type = MB_TYPE_16x16 | MB_TYPE_P0L0 | MB_TYPE_P0L1 |
551  MB_TYPE_DIRECT2; /* B_SUB_8x8 */
552  if (!is_b8x8 && (mb_type_col[0] & MB_TYPE_16x16_OR_INTRA)) {
553  *mb_type |= MB_TYPE_16x16 | MB_TYPE_P0L0 | MB_TYPE_P0L1 |
554  MB_TYPE_DIRECT2; /* B_16x16 */
555  } else if (!is_b8x8 &&
556  (mb_type_col[0] & (MB_TYPE_16x8 | MB_TYPE_8x16))) {
557  *mb_type |= MB_TYPE_L0L1 | MB_TYPE_DIRECT2 |
558  (mb_type_col[0] & (MB_TYPE_16x8 | MB_TYPE_8x16));
559  } else {
560  if (!h->ps.sps->direct_8x8_inference_flag) {
561  /* FIXME: save sub mb types from previous frames (or derive
562  * from MVs) so we know exactly what block size to use */
563  sub_mb_type = MB_TYPE_8x8 | MB_TYPE_P0L0 | MB_TYPE_P0L1 |
564  MB_TYPE_DIRECT2; /* B_SUB_4x4 */
565  }
566  *mb_type |= MB_TYPE_8x8 | MB_TYPE_L0L1;
567  }
568  }
569  }
570 
571  await_reference_mb_row(h, &sl->ref_list[1][0], mb_y);
572 
573  l1mv0 = (void*)&sl->ref_list[1][0].parent->motion_val[0][h->mb2b_xy[mb_xy]];
574  l1mv1 = (void*)&sl->ref_list[1][0].parent->motion_val[1][h->mb2b_xy[mb_xy]];
575  l1ref0 = &sl->ref_list[1][0].parent->ref_index[0][4 * mb_xy];
576  l1ref1 = &sl->ref_list[1][0].parent->ref_index[1][4 * mb_xy];
577  if (!b8_stride) {
578  if (sl->mb_y & 1) {
579  l1ref0 += 2;
580  l1ref1 += 2;
581  l1mv0 += 2 * b4_stride;
582  l1mv1 += 2 * b4_stride;
583  }
584  }
585 
586  {
587  const int *map_col_to_list0[2] = { sl->map_col_to_list0[0],
588  sl->map_col_to_list0[1] };
589  const int *dist_scale_factor = sl->dist_scale_factor;
590  int ref_offset;
591 
592  if (FRAME_MBAFF(h) && IS_INTERLACED(*mb_type)) {
593  map_col_to_list0[0] = sl->map_col_to_list0_field[sl->mb_y & 1][0];
594  map_col_to_list0[1] = sl->map_col_to_list0_field[sl->mb_y & 1][1];
595  dist_scale_factor = sl->dist_scale_factor_field[sl->mb_y & 1];
596  }
597  ref_offset = (sl->ref_list[1][0].parent->mbaff << 4) & (mb_type_col[0] >> 3);
598 
599  if (IS_INTERLACED(*mb_type) != IS_INTERLACED(mb_type_col[0])) {
600  int y_shift = 2 * !IS_INTERLACED(*mb_type);
601  assert(h->ps.sps->direct_8x8_inference_flag);
602 
603  for (i8 = 0; i8 < 4; i8++) {
604  const int x8 = i8 & 1;
605  const int y8 = i8 >> 1;
606  int ref0, scale;
607  const int16_t (*l1mv)[2] = l1mv0;
608 
609  if (is_b8x8 && !IS_DIRECT(sl->sub_mb_type[i8]))
610  continue;
611  sl->sub_mb_type[i8] = sub_mb_type;
612 
613  fill_rectangle(&sl->ref_cache[1][scan8[i8 * 4]], 2, 2, 8, 0, 1);
614  if (IS_INTRA(mb_type_col[y8])) {
615  fill_rectangle(&sl->ref_cache[0][scan8[i8 * 4]], 2, 2, 8, 0, 1);
616  fill_rectangle(&sl->mv_cache[0][scan8[i8 * 4]], 2, 2, 8, 0, 4);
617  fill_rectangle(&sl->mv_cache[1][scan8[i8 * 4]], 2, 2, 8, 0, 4);
618  continue;
619  }
620 
621  ref0 = l1ref0[x8 + y8 * b8_stride];
622  if (ref0 >= 0)
623  ref0 = map_col_to_list0[0][ref0 + ref_offset];
624  else {
625  ref0 = map_col_to_list0[1][l1ref1[x8 + y8 * b8_stride] +
626  ref_offset];
627  l1mv = l1mv1;
628  }
629  scale = dist_scale_factor[ref0];
630  fill_rectangle(&sl->ref_cache[0][scan8[i8 * 4]], 2, 2, 8,
631  ref0, 1);
632 
633  {
634  const int16_t *mv_col = l1mv[x8 * 3 + y8 * b4_stride];
635  int my_col = (mv_col[1] * (1 << y_shift)) / 2;
636  int mx = (scale * mv_col[0] + 128) >> 8;
637  int my = (scale * my_col + 128) >> 8;
638  fill_rectangle(&sl->mv_cache[0][scan8[i8 * 4]], 2, 2, 8,
639  pack16to32(mx, my), 4);
640  fill_rectangle(&sl->mv_cache[1][scan8[i8 * 4]], 2, 2, 8,
641  pack16to32(mx - mv_col[0], my - my_col), 4);
642  }
643  }
644  return;
645  }
646 
647  /* one-to-one mv scaling */
648 
649  if (IS_16X16(*mb_type)) {
650  int ref, mv0, mv1;
651 
652  fill_rectangle(&sl->ref_cache[1][scan8[0]], 4, 4, 8, 0, 1);
653  if (IS_INTRA(mb_type_col[0])) {
654  ref = mv0 = mv1 = 0;
655  } else {
656  const int ref0 = l1ref0[0] >= 0 ? map_col_to_list0[0][l1ref0[0] + ref_offset]
657  : map_col_to_list0[1][l1ref1[0] + ref_offset];
658  const int scale = dist_scale_factor[ref0];
659  const int16_t *mv_col = l1ref0[0] >= 0 ? l1mv0[0] : l1mv1[0];
660  int mv_l0[2];
661  mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
662  mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
663  ref = ref0;
664  mv0 = pack16to32(mv_l0[0], mv_l0[1]);
665  mv1 = pack16to32(mv_l0[0] - mv_col[0], mv_l0[1] - mv_col[1]);
666  }
667  fill_rectangle(&sl->ref_cache[0][scan8[0]], 4, 4, 8, ref, 1);
668  fill_rectangle(&sl->mv_cache[0][scan8[0]], 4, 4, 8, mv0, 4);
669  fill_rectangle(&sl->mv_cache[1][scan8[0]], 4, 4, 8, mv1, 4);
670  } else {
671  for (i8 = 0; i8 < 4; i8++) {
672  const int x8 = i8 & 1;
673  const int y8 = i8 >> 1;
674  int ref0, scale;
675  const int16_t (*l1mv)[2] = l1mv0;
676 
677  if (is_b8x8 && !IS_DIRECT(sl->sub_mb_type[i8]))
678  continue;
679  sl->sub_mb_type[i8] = sub_mb_type;
680  fill_rectangle(&sl->ref_cache[1][scan8[i8 * 4]], 2, 2, 8, 0, 1);
681  if (IS_INTRA(mb_type_col[0])) {
682  fill_rectangle(&sl->ref_cache[0][scan8[i8 * 4]], 2, 2, 8, 0, 1);
683  fill_rectangle(&sl->mv_cache[0][scan8[i8 * 4]], 2, 2, 8, 0, 4);
684  fill_rectangle(&sl->mv_cache[1][scan8[i8 * 4]], 2, 2, 8, 0, 4);
685  continue;
686  }
687 
688  assert(b8_stride == 2);
689  ref0 = l1ref0[i8];
690  if (ref0 >= 0)
691  ref0 = map_col_to_list0[0][ref0 + ref_offset];
692  else {
693  ref0 = map_col_to_list0[1][l1ref1[i8] + ref_offset];
694  l1mv = l1mv1;
695  }
696  scale = dist_scale_factor[ref0];
697 
698  fill_rectangle(&sl->ref_cache[0][scan8[i8 * 4]], 2, 2, 8,
699  ref0, 1);
700  if (IS_SUB_8X8(sub_mb_type)) {
701  const int16_t *mv_col = l1mv[x8 * 3 + y8 * 3 * b4_stride];
702  int mx = (scale * mv_col[0] + 128) >> 8;
703  int my = (scale * mv_col[1] + 128) >> 8;
704  fill_rectangle(&sl->mv_cache[0][scan8[i8 * 4]], 2, 2, 8,
705  pack16to32(mx, my), 4);
706  fill_rectangle(&sl->mv_cache[1][scan8[i8 * 4]], 2, 2, 8,
707  pack16to32(mx - mv_col[0], my - mv_col[1]), 4);
708  } else {
709  for (i4 = 0; i4 < 4; i4++) {
710  const int16_t *mv_col = l1mv[x8 * 2 + (i4 & 1) +
711  (y8 * 2 + (i4 >> 1)) * b4_stride];
712  int16_t *mv_l0 = sl->mv_cache[0][scan8[i8 * 4 + i4]];
713  mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
714  mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
715  AV_WN32A(sl->mv_cache[1][scan8[i8 * 4 + i4]],
716  pack16to32(mv_l0[0] - mv_col[0],
717  mv_l0[1] - mv_col[1]));
718  }
719  }
720  }
721  }
722  }
723 }
724 
726  int *mb_type)
727 {
728  if (sl->direct_spatial_mv_pred)
729  pred_spatial_direct_motion(h, sl, mb_type);
730  else
731  pred_temp_direct_motion(h, sl, mb_type);
732 }
#define A(x)
Definition: vp56_arith.h:28
uint8_t
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition: avassert.h:64
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
Libavcodec external API header.
#define FF_THREAD_FRAME
Decode more than one frame at once.
Definition: avcodec.h:1788
#define av_clip_intp2
Definition: common.h:143
#define FFMIN(a, b)
Definition: common.h:105
#define av_clip_int8
Definition: common.h:131
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:72
#define FFMIN3(a, b, c)
Definition: common.h:106
#define HAVE_THREADS
Definition: config.h:278
long long int64_t
Definition: coverity.c:34
static void fill_rectangle(int x, int y, int w, int h)
Definition: ffplay.c:828
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:215
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
@ AV_PICTURE_TYPE_B
Bi-dir predicted.
Definition: avutil.h:276
void ff_h264_direct_ref_list_init(const H264Context *const h, H264SliceContext *sl)
Definition: h264_direct.c:121
static int get_scale_factor(H264SliceContext *sl, int poc, int poc1, int i)
Definition: h264_direct.c:38
static void pred_spatial_direct_motion(const H264Context *const h, H264SliceContext *sl, int *mb_type)
Definition: h264_direct.c:204
static void await_reference_mb_row(const H264Context *const h, H264Ref *ref, int mb_y)
Definition: h264_direct.c:185
static void fill_colmap(const H264Context *h, H264SliceContext *sl, int map[2][16+32], int list, int field, int colfield, int mbafi)
Definition: h264_direct.c:83
static void pred_temp_direct_motion(const H264Context *const h, H264SliceContext *sl, int *mb_type)
Definition: h264_direct.c:491
void ff_h264_direct_dist_scale_factor(const H264Context *const h, H264SliceContext *sl)
Definition: h264_direct.c:62
void ff_h264_pred_direct_motion(const H264Context *const h, H264SliceContext *sl, int *mb_type)
Definition: h264_direct.c:725
#define MB_TYPE_16x16_OR_INTRA
H.264 parameter set handling.
H.264 / AVC / MPEG-4 part10 codec.
static const uint8_t scan8[16 *3+3]
Definition: h264dec.h:651
#define FIELD_PICTURE(h)
Definition: h264dec.h:75
static av_always_inline uint32_t pack16to32(unsigned a, unsigned b)
Definition: h264dec.h:667
#define PART_NOT_AVAILABLE
Definition: h264dec.h:398
#define FRAME_MBAFF(h)
Definition: h264dec.h:74
#define IS_INTRA(x, y)
#define B
Definition: huffyuvdsp.h:32
const VDPAUPixFmtMap * map
int i
Definition: input.c:407
#define AV_ZERO32(d)
Definition: intreadwrite.h:629
#define AV_WN32A(p, v)
Definition: intreadwrite.h:538
#define AV_RN32A(p)
Definition: intreadwrite.h:526
static const int8_t mv[256][2]
Definition: 4xm.c:78
#define C
common internal API header
static const uint16_t mask[17]
Definition: lzw.c:38
#define mid_pred
Definition: mathops.h:97
#define MB_TYPE_P0L1
Definition: mpegutils.h:65
#define MB_TYPE_8x8
Definition: mpegutils.h:57
#define IS_SUB_8X8(a)
Definition: mpegutils.h:90
#define MB_TYPE_8x16
Definition: mpegutils.h:56
#define IS_INTERLACED(a)
Definition: mpegutils.h:83
#define IS_DIRECT(a)
Definition: mpegutils.h:84
#define MB_TYPE_L0L1
Definition: mpegutils.h:69
#define MB_TYPE_DIRECT2
Definition: mpegutils.h:59
#define MB_TYPE_16x8
Definition: mpegutils.h:55
#define IS_16X16(a)
Definition: mpegutils.h:86
#define IS_8X8(a)
Definition: mpegutils.h:89
#define MB_TYPE_INTERLACED
Definition: mpegutils.h:58
#define MB_TYPE_16x16
Definition: mpegutils.h:54
#define MB_TYPE_L0
Definition: mpegutils.h:67
#define MB_TYPE_P1L0
Definition: mpegutils.h:64
#define MB_TYPE_P0L0
Definition: mpegutils.h:63
#define PICT_BOTTOM_FIELD
Definition: mpegutils.h:38
#define MB_TYPE_P1L1
Definition: mpegutils.h:66
#define PICT_FRAME
Definition: mpegutils.h:39
void ff_thread_await_progress(ThreadFrame *f, int n, int field)
Wait for earlier decoding threads to finish reference pictures.
useful rectangle filling function
#define tb
Definition: regdef.h:68
#define td
Definition: regdef.h:70
H264Context.
Definition: h264dec.h:344
AVCodecContext * avctx
Definition: h264dec.h:346
int ref_count[2][2]
number of entries in ref_poc (FIXME need per slice)
Definition: h264dec.h:157
uint32_t * mb_type
Definition: h264dec.h:140
int16_t(*[2] motion_val)[2]
Definition: h264dec.h:137
int8_t * ref_index[2]
Definition: h264dec.h:146
int frame_num
frame_num (raw frame_num from slice header)
Definition: h264dec.h:150
int long_ref
1->long term reference 0->short term reference
Definition: h264dec.h:155
int ref_poc[2][2][32]
POCs of the frames/fields used as reference (FIXME need per slice)
Definition: h264dec.h:156
int field_poc[2]
top/bottom POC
Definition: h264dec.h:148
int mbaff
1 -> MBAFF frame 0-> not MBAFF
Definition: h264dec.h:158
H264Picture * parent
Definition: h264dec.h:181
int poc
Definition: h264dec.h:178
int reference
Definition: h264dec.h:177
unsigned int list_count
Definition: h264dec.h:275
int8_t ref_cache[2][5 *8]
Definition: h264dec.h:307
int dist_scale_factor_field[2][32]
Definition: h264dec.h:267
uint16_t sub_mb_type[4]
as a DCT coefficient is int32_t in high depth, we need to reserve twice the space.
Definition: h264dec.h:311
int dist_scale_factor[32]
Definition: h264dec.h:266
int16_t mv_cache[2][5 *8][2]
Motion vector cache.
Definition: h264dec.h:306
struct H264Context * h264
Definition: h264dec.h:185
int map_col_to_list0[2][16+32]
Definition: h264dec.h:268
int slice_type_nos
S free slice type (SI/SP are remapped to I/P)
Definition: h264dec.h:191
H264Ref ref_list[2][48]
0..15: frame refs, 16..47: mbaff field refs.
Definition: h264dec.h:276
int direct_spatial_mv_pred
Definition: h264dec.h:258
int map_col_to_list0_field[2][2][16+32]
Definition: h264dec.h:269
unsigned int ref_count[2]
num_ref_idx_l0/1_active_minus1 + 1
Definition: h264dec.h:274
#define avpriv_request_sample(...)
#define av_log(a,...)
static int ref[MAX_W *MAX_W]
Definition: jpeg2000dwt.c:107
const char * b
Definition: vf_curves.c:118
if(ret< 0)
Definition: vf_mcdeint.c:282