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