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