FFmpeg  4.4.9
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  if (!HAVE_THREADS || !(h->avctx->active_thread_type & FF_THREAD_FRAME))
189  return;
190 
191  int ref_field = ref->reference - 1;
192  int ref_field_picture = ref->parent->field_picture;
193  int ref_height = 16 * h->mb_height >> ref_field_picture;
194  int row = FFMIN(16 * mb_y >> ref_field_picture, ref_height - 1);
195 
196  /* FIXME: It can be safe to access mb stuff
197  * even if pixels aren't deblocked yet. */
198 
199  ff_thread_await_progress(&ref->parent->tf, row,
200  ref_field_picture && ref_field);
201 
202  /* A frame references a field pair as a whole, so the wait above covers
203  * its bottom field only, while the colocated data is read from the field
204  * selected by col_parity. The two are decoded by different threads. */
205  if (ref_field_picture && !FIELD_PICTURE(h))
206  ff_thread_await_progress(&ref->parent->tf, row, 0);
207 }
208 
210  int *mb_type)
211 {
212  int b8_stride = 2;
213  int b4_stride = h->b_stride;
214  int mb_xy = sl->mb_xy, mb_y = sl->mb_y;
215  int mb_type_col[2];
216  int col_field = 0;
217  const int16_t (*l1mv0)[2], (*l1mv1)[2];
218  const int8_t *l1ref0, *l1ref1;
219  const int is_b8x8 = IS_8X8(*mb_type);
220  unsigned int sub_mb_type = MB_TYPE_L0L1;
221  int i8, i4;
222  int ref[2];
223  int mv[2];
224  int list;
225 
226  assert(sl->ref_list[1][0].reference & 3);
227 
228  await_reference_mb_row(h, &sl->ref_list[1][0],
229  sl->mb_y + !!IS_INTERLACED(*mb_type));
230 
231 #define MB_TYPE_16x16_OR_INTRA (MB_TYPE_16x16 | MB_TYPE_INTRA4x4 | \
232  MB_TYPE_INTRA16x16 | MB_TYPE_INTRA_PCM)
233 
234  /* ref = min(neighbors) */
235  for (list = 0; list < 2; list++) {
236  int left_ref = sl->ref_cache[list][scan8[0] - 1];
237  int top_ref = sl->ref_cache[list][scan8[0] - 8];
238  int refc = sl->ref_cache[list][scan8[0] - 8 + 4];
239  const int16_t *C = sl->mv_cache[list][scan8[0] - 8 + 4];
240  if (refc == PART_NOT_AVAILABLE) {
241  refc = sl->ref_cache[list][scan8[0] - 8 - 1];
242  C = sl->mv_cache[list][scan8[0] - 8 - 1];
243  }
244  ref[list] = FFMIN3((unsigned)left_ref,
245  (unsigned)top_ref,
246  (unsigned)refc);
247  if (ref[list] >= 0) {
248  /* This is just pred_motion() but with the cases removed that
249  * cannot happen for direct blocks. */
250  const int16_t *const A = sl->mv_cache[list][scan8[0] - 1];
251  const int16_t *const B = sl->mv_cache[list][scan8[0] - 8];
252 
253  int match_count = (left_ref == ref[list]) +
254  (top_ref == ref[list]) +
255  (refc == ref[list]);
256 
257  if (match_count > 1) { // most common
258  mv[list] = pack16to32(mid_pred(A[0], B[0], C[0]),
259  mid_pred(A[1], B[1], C[1]));
260  } else {
261  assert(match_count == 1);
262  if (left_ref == ref[list])
263  mv[list] = AV_RN32A(A);
264  else if (top_ref == ref[list])
265  mv[list] = AV_RN32A(B);
266  else
267  mv[list] = AV_RN32A(C);
268  }
269  av_assert2(ref[list] < (sl->ref_count[list] << !!FRAME_MBAFF(h)));
270  } else {
271  int mask = ~(MB_TYPE_L0 << (2 * list));
272  mv[list] = 0;
273  ref[list] = -1;
274  if (!is_b8x8)
275  *mb_type &= mask;
276  sub_mb_type &= mask;
277  }
278  }
279  if (ref[0] < 0 && ref[1] < 0) {
280  ref[0] = ref[1] = 0;
281  if (!is_b8x8)
282  *mb_type |= MB_TYPE_L0L1;
283  sub_mb_type |= MB_TYPE_L0L1;
284  }
285 
286  if (!(is_b8x8 | mv[0] | mv[1])) {
287  fill_rectangle(&sl->ref_cache[0][scan8[0]], 4, 4, 8, (uint8_t)ref[0], 1);
288  fill_rectangle(&sl->ref_cache[1][scan8[0]], 4, 4, 8, (uint8_t)ref[1], 1);
289  fill_rectangle(&sl->mv_cache[0][scan8[0]], 4, 4, 8, 0, 4);
290  fill_rectangle(&sl->mv_cache[1][scan8[0]], 4, 4, 8, 0, 4);
291  *mb_type = (*mb_type & ~(MB_TYPE_8x8 | MB_TYPE_16x8 | MB_TYPE_8x16 |
294  return;
295  }
296 
297  if (sl->ref_list[1][0].parent->field_picture ||
298  IS_INTERLACED(sl->ref_list[1][0].parent->mb_type[mb_xy])) { // AFL/AFR/FR/FL -> AFL/FL
299  if (!IS_INTERLACED(*mb_type)) { // AFR/FR -> AFL/FL
300  mb_y = (sl->mb_y & ~1) + sl->col_parity;
301  mb_xy = sl->mb_x +
302  ((sl->mb_y & ~1) + sl->col_parity) * h->mb_stride;
303  b8_stride = 0;
304  } else {
305  mb_y += sl->col_fieldoff;
306  mb_xy += h->mb_stride * sl->col_fieldoff; // non-zero for FL -> FL & differ parity
307  }
308  // Undecoded macroblocks of a broken field picture lack the flag, the
309  // colocated type has to agree with the branch taken here.
310  col_field = MB_TYPE_INTERLACED;
311  goto single_col;
312  } else { // AFL/AFR/FR/FL -> AFR/FR
313  if (IS_INTERLACED(*mb_type)) { // AFL /FL -> AFR/FR
314  mb_y = sl->mb_y & ~1;
315  mb_xy = (sl->mb_y & ~1) * h->mb_stride + sl->mb_x;
316  mb_type_col[0] = sl->ref_list[1][0].parent->mb_type[mb_xy];
317  mb_type_col[1] = sl->ref_list[1][0].parent->mb_type[mb_xy + h->mb_stride];
318  b8_stride = 2 + 4 * h->mb_stride;
319  b4_stride *= 6;
320  if (IS_INTERLACED(mb_type_col[0]) !=
321  IS_INTERLACED(mb_type_col[1])) {
322  mb_type_col[0] &= ~MB_TYPE_INTERLACED;
323  mb_type_col[1] &= ~MB_TYPE_INTERLACED;
324  }
325 
326  sub_mb_type |= MB_TYPE_16x16 | MB_TYPE_DIRECT2; /* B_SUB_8x8 */
327  if ((mb_type_col[0] & MB_TYPE_16x16_OR_INTRA) &&
328  (mb_type_col[1] & MB_TYPE_16x16_OR_INTRA) &&
329  !is_b8x8) {
330  *mb_type |= MB_TYPE_16x8 | MB_TYPE_DIRECT2; /* B_16x8 */
331  } else {
332  *mb_type |= MB_TYPE_8x8;
333  }
334  } else { // AFR/FR -> AFR/FR
335 single_col:
336  mb_type_col[0] =
337  mb_type_col[1] = sl->ref_list[1][0].parent->mb_type[mb_xy] | col_field;
338 
339  sub_mb_type |= MB_TYPE_16x16 | MB_TYPE_DIRECT2; /* B_SUB_8x8 */
340  if (!is_b8x8 && (mb_type_col[0] & MB_TYPE_16x16_OR_INTRA)) {
341  *mb_type |= MB_TYPE_16x16 | MB_TYPE_DIRECT2; /* B_16x16 */
342  } else if (!is_b8x8 &&
343  (mb_type_col[0] & (MB_TYPE_16x8 | MB_TYPE_8x16))) {
344  *mb_type |= MB_TYPE_DIRECT2 |
345  (mb_type_col[0] & (MB_TYPE_16x8 | MB_TYPE_8x16));
346  } else {
347  if (!h->ps.sps->direct_8x8_inference_flag) {
348  /* FIXME: Save sub mb types from previous frames (or derive
349  * from MVs) so we know exactly what block size to use. */
350  sub_mb_type += (MB_TYPE_8x8 - MB_TYPE_16x16); /* B_SUB_4x4 */
351  }
352  *mb_type |= MB_TYPE_8x8;
353  }
354  }
355  }
356 
357  await_reference_mb_row(h, &sl->ref_list[1][0], mb_y);
358 
359  l1mv0 = (void*)&sl->ref_list[1][0].parent->motion_val[0][h->mb2b_xy[mb_xy]];
360  l1mv1 = (void*)&sl->ref_list[1][0].parent->motion_val[1][h->mb2b_xy[mb_xy]];
361  l1ref0 = &sl->ref_list[1][0].parent->ref_index[0][4 * mb_xy];
362  l1ref1 = &sl->ref_list[1][0].parent->ref_index[1][4 * mb_xy];
363  if (!b8_stride) {
364  if (sl->mb_y & 1) {
365  l1ref0 += 2;
366  l1ref1 += 2;
367  l1mv0 += 2 * b4_stride;
368  l1mv1 += 2 * b4_stride;
369  }
370  }
371 
372  if (IS_INTERLACED(*mb_type) != IS_INTERLACED(mb_type_col[0])) {
373  int n = 0;
374  for (i8 = 0; i8 < 4; i8++) {
375  int x8 = i8 & 1;
376  int y8 = i8 >> 1;
377  int xy8 = x8 + y8 * b8_stride;
378  int xy4 = x8 * 3 + y8 * b4_stride;
379  int a, b;
380 
381  if (is_b8x8 && !IS_DIRECT(sl->sub_mb_type[i8]))
382  continue;
383  sl->sub_mb_type[i8] = sub_mb_type;
384 
385  fill_rectangle(&sl->ref_cache[0][scan8[i8 * 4]], 2, 2, 8,
386  (uint8_t)ref[0], 1);
387  fill_rectangle(&sl->ref_cache[1][scan8[i8 * 4]], 2, 2, 8,
388  (uint8_t)ref[1], 1);
389  if (!IS_INTRA(mb_type_col[y8]) && !sl->ref_list[1][0].parent->long_ref &&
390  ((l1ref0[xy8] == 0 &&
391  FFABS(l1mv0[xy4][0]) <= 1 &&
392  FFABS(l1mv0[xy4][1]) <= 1) ||
393  (l1ref0[xy8] < 0 &&
394  l1ref1[xy8] == 0 &&
395  FFABS(l1mv1[xy4][0]) <= 1 &&
396  FFABS(l1mv1[xy4][1]) <= 1))) {
397  a =
398  b = 0;
399  if (ref[0] > 0)
400  a = mv[0];
401  if (ref[1] > 0)
402  b = mv[1];
403  n++;
404  } else {
405  a = mv[0];
406  b = mv[1];
407  }
408  fill_rectangle(&sl->mv_cache[0][scan8[i8 * 4]], 2, 2, 8, a, 4);
409  fill_rectangle(&sl->mv_cache[1][scan8[i8 * 4]], 2, 2, 8, b, 4);
410  }
411  if (!is_b8x8 && !(n & 3))
412  *mb_type = (*mb_type & ~(MB_TYPE_8x8 | MB_TYPE_16x8 | MB_TYPE_8x16 |
415  } else if (IS_16X16(*mb_type)) {
416  int a, b;
417 
418  fill_rectangle(&sl->ref_cache[0][scan8[0]], 4, 4, 8, (uint8_t)ref[0], 1);
419  fill_rectangle(&sl->ref_cache[1][scan8[0]], 4, 4, 8, (uint8_t)ref[1], 1);
420  if (!IS_INTRA(mb_type_col[0]) && !sl->ref_list[1][0].parent->long_ref &&
421  ((l1ref0[0] == 0 &&
422  FFABS(l1mv0[0][0]) <= 1 &&
423  FFABS(l1mv0[0][1]) <= 1) ||
424  (l1ref0[0] < 0 && !l1ref1[0] &&
425  FFABS(l1mv1[0][0]) <= 1 &&
426  FFABS(l1mv1[0][1]) <= 1 &&
427  h->x264_build > 33U))) {
428  a = b = 0;
429  if (ref[0] > 0)
430  a = mv[0];
431  if (ref[1] > 0)
432  b = mv[1];
433  } else {
434  a = mv[0];
435  b = mv[1];
436  }
437  fill_rectangle(&sl->mv_cache[0][scan8[0]], 4, 4, 8, a, 4);
438  fill_rectangle(&sl->mv_cache[1][scan8[0]], 4, 4, 8, b, 4);
439  } else {
440  int n = 0;
441  for (i8 = 0; i8 < 4; i8++) {
442  const int x8 = i8 & 1;
443  const int y8 = i8 >> 1;
444 
445  if (is_b8x8 && !IS_DIRECT(sl->sub_mb_type[i8]))
446  continue;
447  sl->sub_mb_type[i8] = sub_mb_type;
448 
449  fill_rectangle(&sl->mv_cache[0][scan8[i8 * 4]], 2, 2, 8, mv[0], 4);
450  fill_rectangle(&sl->mv_cache[1][scan8[i8 * 4]], 2, 2, 8, mv[1], 4);
451  fill_rectangle(&sl->ref_cache[0][scan8[i8 * 4]], 2, 2, 8,
452  (uint8_t)ref[0], 1);
453  fill_rectangle(&sl->ref_cache[1][scan8[i8 * 4]], 2, 2, 8,
454  (uint8_t)ref[1], 1);
455 
456  assert(b8_stride == 2);
457  /* col_zero_flag */
458  if (!IS_INTRA(mb_type_col[0]) && !sl->ref_list[1][0].parent->long_ref &&
459  (l1ref0[i8] == 0 ||
460  (l1ref0[i8] < 0 &&
461  l1ref1[i8] == 0 &&
462  h->x264_build > 33U))) {
463  const int16_t (*l1mv)[2] = l1ref0[i8] == 0 ? l1mv0 : l1mv1;
464  if (IS_SUB_8X8(sub_mb_type)) {
465  const int16_t *mv_col = l1mv[x8 * 3 + y8 * 3 * b4_stride];
466  if (FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1) {
467  if (ref[0] == 0)
468  fill_rectangle(&sl->mv_cache[0][scan8[i8 * 4]], 2, 2,
469  8, 0, 4);
470  if (ref[1] == 0)
471  fill_rectangle(&sl->mv_cache[1][scan8[i8 * 4]], 2, 2,
472  8, 0, 4);
473  n += 4;
474  }
475  } else {
476  int m = 0;
477  for (i4 = 0; i4 < 4; i4++) {
478  const int16_t *mv_col = l1mv[x8 * 2 + (i4 & 1) +
479  (y8 * 2 + (i4 >> 1)) * b4_stride];
480  if (FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1) {
481  if (ref[0] == 0)
482  AV_ZERO32(sl->mv_cache[0][scan8[i8 * 4 + i4]]);
483  if (ref[1] == 0)
484  AV_ZERO32(sl->mv_cache[1][scan8[i8 * 4 + i4]]);
485  m++;
486  }
487  }
488  if (!(m & 3))
490  n += m;
491  }
492  }
493  }
494  if (!is_b8x8 && !(n & 15))
495  *mb_type = (*mb_type & ~(MB_TYPE_8x8 | MB_TYPE_16x8 | MB_TYPE_8x16 |
498  }
499 }
500 
502  int *mb_type)
503 {
504  int b8_stride = 2;
505  int b4_stride = h->b_stride;
506  int mb_xy = sl->mb_xy, mb_y = sl->mb_y;
507  int mb_type_col[2];
508  int col_field = 0;
509  const int16_t (*l1mv0)[2], (*l1mv1)[2];
510  const int8_t *l1ref0, *l1ref1;
511  const int is_b8x8 = IS_8X8(*mb_type);
512  unsigned int sub_mb_type;
513  int i8, i4;
514 
515  assert(sl->ref_list[1][0].reference & 3);
516 
517  await_reference_mb_row(h, &sl->ref_list[1][0],
518  sl->mb_y + !!IS_INTERLACED(*mb_type));
519 
520  if (sl->ref_list[1][0].parent->field_picture ||
521  IS_INTERLACED(sl->ref_list[1][0].parent->mb_type[mb_xy])) { // AFL/AFR/FR/FL -> AFL/FL
522  if (!IS_INTERLACED(*mb_type)) { // AFR/FR -> AFL/FL
523  mb_y = (sl->mb_y & ~1) + sl->col_parity;
524  mb_xy = sl->mb_x +
525  ((sl->mb_y & ~1) + sl->col_parity) * h->mb_stride;
526  b8_stride = 0;
527  } else {
528  mb_y += sl->col_fieldoff;
529  mb_xy += h->mb_stride * sl->col_fieldoff; // non-zero for FL -> FL & differ parity
530  }
531  // Undecoded macroblocks of a broken field picture lack the flag, the
532  // colocated type has to agree with the branch taken here.
533  col_field = MB_TYPE_INTERLACED;
534  goto single_col;
535  } else { // AFL/AFR/FR/FL -> AFR/FR
536  if (IS_INTERLACED(*mb_type)) { // AFL /FL -> AFR/FR
537  mb_y = sl->mb_y & ~1;
538  mb_xy = sl->mb_x + (sl->mb_y & ~1) * h->mb_stride;
539  mb_type_col[0] = sl->ref_list[1][0].parent->mb_type[mb_xy];
540  mb_type_col[1] = sl->ref_list[1][0].parent->mb_type[mb_xy + h->mb_stride];
541  b8_stride = 2 + 4 * h->mb_stride;
542  b4_stride *= 6;
543  if (IS_INTERLACED(mb_type_col[0]) !=
544  IS_INTERLACED(mb_type_col[1])) {
545  mb_type_col[0] &= ~MB_TYPE_INTERLACED;
546  mb_type_col[1] &= ~MB_TYPE_INTERLACED;
547  }
548 
549  sub_mb_type = MB_TYPE_16x16 | MB_TYPE_P0L0 | MB_TYPE_P0L1 |
550  MB_TYPE_DIRECT2; /* B_SUB_8x8 */
551 
552  if ((mb_type_col[0] & MB_TYPE_16x16_OR_INTRA) &&
553  (mb_type_col[1] & MB_TYPE_16x16_OR_INTRA) &&
554  !is_b8x8) {
555  *mb_type |= MB_TYPE_16x8 | MB_TYPE_L0L1 |
556  MB_TYPE_DIRECT2; /* B_16x8 */
557  } else {
558  *mb_type |= MB_TYPE_8x8 | MB_TYPE_L0L1;
559  }
560  } else { // AFR/FR -> AFR/FR
561 single_col:
562  mb_type_col[0] =
563  mb_type_col[1] = sl->ref_list[1][0].parent->mb_type[mb_xy] | col_field;
564 
565  sub_mb_type = MB_TYPE_16x16 | MB_TYPE_P0L0 | MB_TYPE_P0L1 |
566  MB_TYPE_DIRECT2; /* B_SUB_8x8 */
567  if (!is_b8x8 && (mb_type_col[0] & MB_TYPE_16x16_OR_INTRA)) {
568  *mb_type |= MB_TYPE_16x16 | MB_TYPE_P0L0 | MB_TYPE_P0L1 |
569  MB_TYPE_DIRECT2; /* B_16x16 */
570  } else if (!is_b8x8 &&
571  (mb_type_col[0] & (MB_TYPE_16x8 | MB_TYPE_8x16))) {
572  *mb_type |= MB_TYPE_L0L1 | MB_TYPE_DIRECT2 |
573  (mb_type_col[0] & (MB_TYPE_16x8 | MB_TYPE_8x16));
574  } else {
575  if (!h->ps.sps->direct_8x8_inference_flag) {
576  /* FIXME: save sub mb types from previous frames (or derive
577  * from MVs) so we know exactly what block size to use */
578  sub_mb_type = MB_TYPE_8x8 | MB_TYPE_P0L0 | MB_TYPE_P0L1 |
579  MB_TYPE_DIRECT2; /* B_SUB_4x4 */
580  }
581  *mb_type |= MB_TYPE_8x8 | MB_TYPE_L0L1;
582  }
583  }
584  }
585 
586  await_reference_mb_row(h, &sl->ref_list[1][0], mb_y);
587 
588  l1mv0 = (void*)&sl->ref_list[1][0].parent->motion_val[0][h->mb2b_xy[mb_xy]];
589  l1mv1 = (void*)&sl->ref_list[1][0].parent->motion_val[1][h->mb2b_xy[mb_xy]];
590  l1ref0 = &sl->ref_list[1][0].parent->ref_index[0][4 * mb_xy];
591  l1ref1 = &sl->ref_list[1][0].parent->ref_index[1][4 * mb_xy];
592  if (!b8_stride) {
593  if (sl->mb_y & 1) {
594  l1ref0 += 2;
595  l1ref1 += 2;
596  l1mv0 += 2 * b4_stride;
597  l1mv1 += 2 * b4_stride;
598  }
599  }
600 
601  {
602  const int *map_col_to_list0[2] = { sl->map_col_to_list0[0],
603  sl->map_col_to_list0[1] };
604  const int *dist_scale_factor = sl->dist_scale_factor;
605  int ref_offset;
606 
607  if (FRAME_MBAFF(h) && IS_INTERLACED(*mb_type)) {
608  map_col_to_list0[0] = sl->map_col_to_list0_field[sl->mb_y & 1][0];
609  map_col_to_list0[1] = sl->map_col_to_list0_field[sl->mb_y & 1][1];
610  dist_scale_factor = sl->dist_scale_factor_field[sl->mb_y & 1];
611  }
612  ref_offset = (sl->ref_list[1][0].parent->mbaff << 4) & (mb_type_col[0] >> 3);
613 
614  if (IS_INTERLACED(*mb_type) != IS_INTERLACED(mb_type_col[0])) {
615  int y_shift = 2 * !IS_INTERLACED(*mb_type);
616  assert(h->ps.sps->direct_8x8_inference_flag);
617 
618  for (i8 = 0; i8 < 4; i8++) {
619  const int x8 = i8 & 1;
620  const int y8 = i8 >> 1;
621  int ref0, scale;
622  const int16_t (*l1mv)[2] = l1mv0;
623 
624  if (is_b8x8 && !IS_DIRECT(sl->sub_mb_type[i8]))
625  continue;
626  sl->sub_mb_type[i8] = sub_mb_type;
627 
628  fill_rectangle(&sl->ref_cache[1][scan8[i8 * 4]], 2, 2, 8, 0, 1);
629  if (IS_INTRA(mb_type_col[y8])) {
630  fill_rectangle(&sl->ref_cache[0][scan8[i8 * 4]], 2, 2, 8, 0, 1);
631  fill_rectangle(&sl->mv_cache[0][scan8[i8 * 4]], 2, 2, 8, 0, 4);
632  fill_rectangle(&sl->mv_cache[1][scan8[i8 * 4]], 2, 2, 8, 0, 4);
633  continue;
634  }
635 
636  ref0 = l1ref0[x8 + y8 * b8_stride];
637  if (ref0 >= 0)
638  ref0 = map_col_to_list0[0][ref0 + ref_offset];
639  else {
640  ref0 = map_col_to_list0[1][l1ref1[x8 + y8 * b8_stride] +
641  ref_offset];
642  l1mv = l1mv1;
643  }
644  scale = dist_scale_factor[ref0];
645  fill_rectangle(&sl->ref_cache[0][scan8[i8 * 4]], 2, 2, 8,
646  ref0, 1);
647 
648  {
649  const int16_t *mv_col = l1mv[x8 * 3 + y8 * b4_stride];
650  int my_col = (mv_col[1] * (1 << y_shift)) / 2;
651  int mx = (scale * mv_col[0] + 128) >> 8;
652  int my = (scale * my_col + 128) >> 8;
653  fill_rectangle(&sl->mv_cache[0][scan8[i8 * 4]], 2, 2, 8,
654  pack16to32(mx, my), 4);
655  fill_rectangle(&sl->mv_cache[1][scan8[i8 * 4]], 2, 2, 8,
656  pack16to32(mx - mv_col[0], my - my_col), 4);
657  }
658  }
659  return;
660  }
661 
662  /* one-to-one mv scaling */
663 
664  if (IS_16X16(*mb_type)) {
665  int ref, mv0, mv1;
666 
667  fill_rectangle(&sl->ref_cache[1][scan8[0]], 4, 4, 8, 0, 1);
668  if (IS_INTRA(mb_type_col[0])) {
669  ref = mv0 = mv1 = 0;
670  } else {
671  const int ref0 = l1ref0[0] >= 0 ? map_col_to_list0[0][l1ref0[0] + ref_offset]
672  : map_col_to_list0[1][l1ref1[0] + ref_offset];
673  const int scale = dist_scale_factor[ref0];
674  const int16_t *mv_col = l1ref0[0] >= 0 ? l1mv0[0] : l1mv1[0];
675  int mv_l0[2];
676  mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
677  mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
678  ref = ref0;
679  mv0 = pack16to32(mv_l0[0], mv_l0[1]);
680  mv1 = pack16to32(mv_l0[0] - mv_col[0], mv_l0[1] - mv_col[1]);
681  }
682  fill_rectangle(&sl->ref_cache[0][scan8[0]], 4, 4, 8, ref, 1);
683  fill_rectangle(&sl->mv_cache[0][scan8[0]], 4, 4, 8, mv0, 4);
684  fill_rectangle(&sl->mv_cache[1][scan8[0]], 4, 4, 8, mv1, 4);
685  } else {
686  for (i8 = 0; i8 < 4; i8++) {
687  const int x8 = i8 & 1;
688  const int y8 = i8 >> 1;
689  int ref0, scale;
690  const int16_t (*l1mv)[2] = l1mv0;
691 
692  if (is_b8x8 && !IS_DIRECT(sl->sub_mb_type[i8]))
693  continue;
694  sl->sub_mb_type[i8] = sub_mb_type;
695  fill_rectangle(&sl->ref_cache[1][scan8[i8 * 4]], 2, 2, 8, 0, 1);
696  if (IS_INTRA(mb_type_col[0])) {
697  fill_rectangle(&sl->ref_cache[0][scan8[i8 * 4]], 2, 2, 8, 0, 1);
698  fill_rectangle(&sl->mv_cache[0][scan8[i8 * 4]], 2, 2, 8, 0, 4);
699  fill_rectangle(&sl->mv_cache[1][scan8[i8 * 4]], 2, 2, 8, 0, 4);
700  continue;
701  }
702 
703  assert(b8_stride == 2);
704  ref0 = l1ref0[i8];
705  if (ref0 >= 0)
706  ref0 = map_col_to_list0[0][ref0 + ref_offset];
707  else {
708  ref0 = map_col_to_list0[1][l1ref1[i8] + ref_offset];
709  l1mv = l1mv1;
710  }
711  scale = dist_scale_factor[ref0];
712 
713  fill_rectangle(&sl->ref_cache[0][scan8[i8 * 4]], 2, 2, 8,
714  ref0, 1);
715  if (IS_SUB_8X8(sub_mb_type)) {
716  const int16_t *mv_col = l1mv[x8 * 3 + y8 * 3 * b4_stride];
717  int mx = (scale * mv_col[0] + 128) >> 8;
718  int my = (scale * mv_col[1] + 128) >> 8;
719  fill_rectangle(&sl->mv_cache[0][scan8[i8 * 4]], 2, 2, 8,
720  pack16to32(mx, my), 4);
721  fill_rectangle(&sl->mv_cache[1][scan8[i8 * 4]], 2, 2, 8,
722  pack16to32(mx - mv_col[0], my - mv_col[1]), 4);
723  } else {
724  for (i4 = 0; i4 < 4; i4++) {
725  const int16_t *mv_col = l1mv[x8 * 2 + (i4 & 1) +
726  (y8 * 2 + (i4 >> 1)) * b4_stride];
727  int16_t *mv_l0 = sl->mv_cache[0][scan8[i8 * 4 + i4]];
728  mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
729  mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
730  AV_WN32A(sl->mv_cache[1][scan8[i8 * 4 + i4]],
731  pack16to32(mv_l0[0] - mv_col[0],
732  mv_l0[1] - mv_col[1]));
733  }
734  }
735  }
736  }
737  }
738 }
739 
741  int *mb_type)
742 {
743  if (sl->direct_spatial_mv_pred)
744  pred_spatial_direct_motion(h, sl, mb_type);
745  else
746  pred_temp_direct_motion(h, sl, mb_type);
747 }
#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:277
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:209
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:501
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:740
#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 field_picture
whether or not picture was encoded in separate fields
Definition: h264dec.h:159
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:119
if(ret< 0)
Definition: vf_mcdeint.c:282