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