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