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