FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
h264_slice.c
Go to the documentation of this file.
1 /*
2  * H.26L/H.264/AVC/JVT/14496-10/... decoder
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 codec.
25  * @author Michael Niedermayer <michaelni@gmx.at>
26  */
27 
28 #include "libavutil/avassert.h"
29 #include "libavutil/imgutils.h"
30 #include "libavutil/timer.h"
31 #include "internal.h"
32 #include "cabac.h"
33 #include "cabac_functions.h"
34 #include "error_resilience.h"
35 #include "avcodec.h"
36 #include "h264.h"
37 #include "h264data.h"
38 #include "h264chroma.h"
39 #include "h264_mvpred.h"
40 #include "golomb.h"
41 #include "mathops.h"
42 #include "mpegutils.h"
43 #include "rectangle.h"
44 #include "thread.h"
45 
46 
47 static const uint8_t rem6[QP_MAX_NUM + 1] = {
48  0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2,
49  3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5,
50  0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2,
51  3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5,
52  0, 1, 2, 3,
53 };
54 
55 static const uint8_t div6[QP_MAX_NUM + 1] = {
56  0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3,
57  3, 3, 3, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6,
58  7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 10, 10, 10,
59  10,10,10,11,11,11,11,11,11,12,12,12,12,12,12,13,13,13, 13, 13, 13,
60  14,14,14,14,
61 };
62 
63 static const uint8_t field_scan[16+1] = {
64  0 + 0 * 4, 0 + 1 * 4, 1 + 0 * 4, 0 + 2 * 4,
65  0 + 3 * 4, 1 + 1 * 4, 1 + 2 * 4, 1 + 3 * 4,
66  2 + 0 * 4, 2 + 1 * 4, 2 + 2 * 4, 2 + 3 * 4,
67  3 + 0 * 4, 3 + 1 * 4, 3 + 2 * 4, 3 + 3 * 4,
68 };
69 
70 static const uint8_t field_scan8x8[64+1] = {
71  0 + 0 * 8, 0 + 1 * 8, 0 + 2 * 8, 1 + 0 * 8,
72  1 + 1 * 8, 0 + 3 * 8, 0 + 4 * 8, 1 + 2 * 8,
73  2 + 0 * 8, 1 + 3 * 8, 0 + 5 * 8, 0 + 6 * 8,
74  0 + 7 * 8, 1 + 4 * 8, 2 + 1 * 8, 3 + 0 * 8,
75  2 + 2 * 8, 1 + 5 * 8, 1 + 6 * 8, 1 + 7 * 8,
76  2 + 3 * 8, 3 + 1 * 8, 4 + 0 * 8, 3 + 2 * 8,
77  2 + 4 * 8, 2 + 5 * 8, 2 + 6 * 8, 2 + 7 * 8,
78  3 + 3 * 8, 4 + 1 * 8, 5 + 0 * 8, 4 + 2 * 8,
79  3 + 4 * 8, 3 + 5 * 8, 3 + 6 * 8, 3 + 7 * 8,
80  4 + 3 * 8, 5 + 1 * 8, 6 + 0 * 8, 5 + 2 * 8,
81  4 + 4 * 8, 4 + 5 * 8, 4 + 6 * 8, 4 + 7 * 8,
82  5 + 3 * 8, 6 + 1 * 8, 6 + 2 * 8, 5 + 4 * 8,
83  5 + 5 * 8, 5 + 6 * 8, 5 + 7 * 8, 6 + 3 * 8,
84  7 + 0 * 8, 7 + 1 * 8, 6 + 4 * 8, 6 + 5 * 8,
85  6 + 6 * 8, 6 + 7 * 8, 7 + 2 * 8, 7 + 3 * 8,
86  7 + 4 * 8, 7 + 5 * 8, 7 + 6 * 8, 7 + 7 * 8,
87 };
88 
89 static const uint8_t field_scan8x8_cavlc[64+1] = {
90  0 + 0 * 8, 1 + 1 * 8, 2 + 0 * 8, 0 + 7 * 8,
91  2 + 2 * 8, 2 + 3 * 8, 2 + 4 * 8, 3 + 3 * 8,
92  3 + 4 * 8, 4 + 3 * 8, 4 + 4 * 8, 5 + 3 * 8,
93  5 + 5 * 8, 7 + 0 * 8, 6 + 6 * 8, 7 + 4 * 8,
94  0 + 1 * 8, 0 + 3 * 8, 1 + 3 * 8, 1 + 4 * 8,
95  1 + 5 * 8, 3 + 1 * 8, 2 + 5 * 8, 4 + 1 * 8,
96  3 + 5 * 8, 5 + 1 * 8, 4 + 5 * 8, 6 + 1 * 8,
97  5 + 6 * 8, 7 + 1 * 8, 6 + 7 * 8, 7 + 5 * 8,
98  0 + 2 * 8, 0 + 4 * 8, 0 + 5 * 8, 2 + 1 * 8,
99  1 + 6 * 8, 4 + 0 * 8, 2 + 6 * 8, 5 + 0 * 8,
100  3 + 6 * 8, 6 + 0 * 8, 4 + 6 * 8, 6 + 2 * 8,
101  5 + 7 * 8, 6 + 4 * 8, 7 + 2 * 8, 7 + 6 * 8,
102  1 + 0 * 8, 1 + 2 * 8, 0 + 6 * 8, 3 + 0 * 8,
103  1 + 7 * 8, 3 + 2 * 8, 2 + 7 * 8, 4 + 2 * 8,
104  3 + 7 * 8, 5 + 2 * 8, 4 + 7 * 8, 5 + 4 * 8,
105  6 + 3 * 8, 6 + 5 * 8, 7 + 3 * 8, 7 + 7 * 8,
106 };
107 
108 // zigzag_scan8x8_cavlc[i] = zigzag_scan8x8[(i/4) + 16*(i%4)]
109 static const uint8_t zigzag_scan8x8_cavlc[64+1] = {
110  0 + 0 * 8, 1 + 1 * 8, 1 + 2 * 8, 2 + 2 * 8,
111  4 + 1 * 8, 0 + 5 * 8, 3 + 3 * 8, 7 + 0 * 8,
112  3 + 4 * 8, 1 + 7 * 8, 5 + 3 * 8, 6 + 3 * 8,
113  2 + 7 * 8, 6 + 4 * 8, 5 + 6 * 8, 7 + 5 * 8,
114  1 + 0 * 8, 2 + 0 * 8, 0 + 3 * 8, 3 + 1 * 8,
115  3 + 2 * 8, 0 + 6 * 8, 4 + 2 * 8, 6 + 1 * 8,
116  2 + 5 * 8, 2 + 6 * 8, 6 + 2 * 8, 5 + 4 * 8,
117  3 + 7 * 8, 7 + 3 * 8, 4 + 7 * 8, 7 + 6 * 8,
118  0 + 1 * 8, 3 + 0 * 8, 0 + 4 * 8, 4 + 0 * 8,
119  2 + 3 * 8, 1 + 5 * 8, 5 + 1 * 8, 5 + 2 * 8,
120  1 + 6 * 8, 3 + 5 * 8, 7 + 1 * 8, 4 + 5 * 8,
121  4 + 6 * 8, 7 + 4 * 8, 5 + 7 * 8, 6 + 7 * 8,
122  0 + 2 * 8, 2 + 1 * 8, 1 + 3 * 8, 5 + 0 * 8,
123  1 + 4 * 8, 2 + 4 * 8, 6 + 0 * 8, 4 + 3 * 8,
124  0 + 7 * 8, 4 + 4 * 8, 7 + 2 * 8, 3 + 6 * 8,
125  5 + 5 * 8, 6 + 5 * 8, 6 + 6 * 8, 7 + 7 * 8,
126 };
127 
128 static const uint8_t dequant4_coeff_init[6][3] = {
129  { 10, 13, 16 },
130  { 11, 14, 18 },
131  { 13, 16, 20 },
132  { 14, 18, 23 },
133  { 16, 20, 25 },
134  { 18, 23, 29 },
135 };
136 
137 static const uint8_t dequant8_coeff_init_scan[16] = {
138  0, 3, 4, 3, 3, 1, 5, 1, 4, 5, 2, 5, 3, 1, 5, 1
139 };
140 
141 static const uint8_t dequant8_coeff_init[6][6] = {
142  { 20, 18, 32, 19, 25, 24 },
143  { 22, 19, 35, 21, 28, 26 },
144  { 26, 23, 42, 24, 33, 31 },
145  { 28, 25, 45, 26, 35, 33 },
146  { 32, 28, 51, 30, 40, 38 },
147  { 36, 32, 58, 34, 46, 43 },
148 };
149 
150 
151 static void release_unused_pictures(H264Context *h, int remove_current)
152 {
153  int i;
154 
155  /* release non reference frames */
156  for (i = 0; i < H264_MAX_PICTURE_COUNT; i++) {
157  if (h->DPB[i].f->buf[0] && !h->DPB[i].reference &&
158  (remove_current || &h->DPB[i] != h->cur_pic_ptr)) {
159  ff_h264_unref_picture(h, &h->DPB[i]);
160  }
161  }
162 }
163 
164 static int alloc_scratch_buffers(H264SliceContext *sl, int linesize)
165 {
166  const H264Context *h = sl->h264;
167  int alloc_size = FFALIGN(FFABS(linesize) + 32, 32);
168 
169  av_fast_malloc(&sl->bipred_scratchpad, &sl->bipred_scratchpad_allocated, 16 * 6 * alloc_size);
170  // edge emu needs blocksize + filter length - 1
171  // (= 21x21 for h264)
172  av_fast_malloc(&sl->edge_emu_buffer, &sl->edge_emu_buffer_allocated, alloc_size * 2 * 21);
173 
175  h->mb_width * 16 * 3 * sizeof(uint8_t) * 2);
177  h->mb_width * 16 * 3 * sizeof(uint8_t) * 2);
178 
179  if (!sl->bipred_scratchpad || !sl->edge_emu_buffer ||
180  !sl->top_borders[0] || !sl->top_borders[1]) {
183  av_freep(&sl->top_borders[0]);
184  av_freep(&sl->top_borders[1]);
185 
188  sl->top_borders_allocated[0] = 0;
189  sl->top_borders_allocated[1] = 0;
190  return AVERROR(ENOMEM);
191  }
192 
193  return 0;
194 }
195 
197 {
198  const int big_mb_num = h->mb_stride * (h->mb_height + 1) + 1;
199  const int mb_array_size = h->mb_stride * h->mb_height;
200  const int b4_stride = h->mb_width * 4 + 1;
201  const int b4_array_size = b4_stride * h->mb_height * 4;
202 
203  h->qscale_table_pool = av_buffer_pool_init(big_mb_num + h->mb_stride,
205  h->mb_type_pool = av_buffer_pool_init((big_mb_num + h->mb_stride) *
206  sizeof(uint32_t), av_buffer_allocz);
207  h->motion_val_pool = av_buffer_pool_init(2 * (b4_array_size + 4) *
208  sizeof(int16_t), av_buffer_allocz);
209  h->ref_index_pool = av_buffer_pool_init(4 * mb_array_size, av_buffer_allocz);
210 
211  if (!h->qscale_table_pool || !h->mb_type_pool || !h->motion_val_pool ||
212  !h->ref_index_pool) {
217  return AVERROR(ENOMEM);
218  }
219 
220  return 0;
221 }
222 
224 {
225  int i, ret = 0;
226 
227  av_assert0(!pic->f->data[0]);
228 
229  pic->tf.f = pic->f;
230  ret = ff_thread_get_buffer(h->avctx, &pic->tf, pic->reference ?
232  if (ret < 0)
233  goto fail;
234 
235  pic->crop = h->sps.crop;
236  pic->crop_top = h->sps.crop_top;
237  pic->crop_left= h->sps.crop_left;
238 
239  if (h->avctx->hwaccel) {
240  const AVHWAccel *hwaccel = h->avctx->hwaccel;
242  if (hwaccel->frame_priv_data_size) {
244  if (!pic->hwaccel_priv_buf)
245  return AVERROR(ENOMEM);
247  }
248  }
249  if (CONFIG_GRAY && !h->avctx->hwaccel && h->flags & AV_CODEC_FLAG_GRAY && pic->f->data[2]) {
250  int h_chroma_shift, v_chroma_shift;
252  &h_chroma_shift, &v_chroma_shift);
253 
254  for(i=0; i<FF_CEIL_RSHIFT(pic->f->height, v_chroma_shift); i++) {
255  memset(pic->f->data[1] + pic->f->linesize[1]*i,
256  0x80, FF_CEIL_RSHIFT(pic->f->width, h_chroma_shift));
257  memset(pic->f->data[2] + pic->f->linesize[2]*i,
258  0x80, FF_CEIL_RSHIFT(pic->f->width, h_chroma_shift));
259  }
260  }
261 
262  if (!h->qscale_table_pool) {
263  ret = init_table_pools(h);
264  if (ret < 0)
265  goto fail;
266  }
267 
270  if (!pic->qscale_table_buf || !pic->mb_type_buf)
271  goto fail;
272 
273  pic->mb_type = (uint32_t*)pic->mb_type_buf->data + 2 * h->mb_stride + 1;
274  pic->qscale_table = pic->qscale_table_buf->data + 2 * h->mb_stride + 1;
275 
276  for (i = 0; i < 2; i++) {
279  if (!pic->motion_val_buf[i] || !pic->ref_index_buf[i])
280  goto fail;
281 
282  pic->motion_val[i] = (int16_t (*)[2])pic->motion_val_buf[i]->data + 4;
283  pic->ref_index[i] = pic->ref_index_buf[i]->data;
284  }
285 
286  return 0;
287 fail:
288  ff_h264_unref_picture(h, pic);
289  return (ret < 0) ? ret : AVERROR(ENOMEM);
290 }
291 
292 static inline int pic_is_unused(H264Context *h, H264Picture *pic)
293 {
294  if (!pic->f->buf[0])
295  return 1;
296  return 0;
297 }
298 
300 {
301  int i;
302 
303  for (i = 0; i < H264_MAX_PICTURE_COUNT; i++) {
304  if (pic_is_unused(h, &h->DPB[i]))
305  break;
306  }
307  if (i == H264_MAX_PICTURE_COUNT)
308  return AVERROR_INVALIDDATA;
309 
310  return i;
311 }
312 
313 
315 {
316  int i, j, q, x;
317  const int max_qp = 51 + 6 * (h->sps.bit_depth_luma - 8);
318 
319  for (i = 0; i < 6; i++) {
320  h->dequant8_coeff[i] = h->dequant8_buffer[i];
321  for (j = 0; j < i; j++)
322  if (!memcmp(h->pps.scaling_matrix8[j], h->pps.scaling_matrix8[i],
323  64 * sizeof(uint8_t))) {
324  h->dequant8_coeff[i] = h->dequant8_buffer[j];
325  break;
326  }
327  if (j < i)
328  continue;
329 
330  for (q = 0; q < max_qp + 1; q++) {
331  int shift = div6[q];
332  int idx = rem6[q];
333  for (x = 0; x < 64; x++)
334  h->dequant8_coeff[i][q][(x >> 3) | ((x & 7) << 3)] =
335  ((uint32_t)dequant8_coeff_init[idx][dequant8_coeff_init_scan[((x >> 1) & 12) | (x & 3)]] *
336  h->pps.scaling_matrix8[i][x]) << shift;
337  }
338  }
339 }
340 
342 {
343  int i, j, q, x;
344  const int max_qp = 51 + 6 * (h->sps.bit_depth_luma - 8);
345  for (i = 0; i < 6; i++) {
346  h->dequant4_coeff[i] = h->dequant4_buffer[i];
347  for (j = 0; j < i; j++)
348  if (!memcmp(h->pps.scaling_matrix4[j], h->pps.scaling_matrix4[i],
349  16 * sizeof(uint8_t))) {
350  h->dequant4_coeff[i] = h->dequant4_buffer[j];
351  break;
352  }
353  if (j < i)
354  continue;
355 
356  for (q = 0; q < max_qp + 1; q++) {
357  int shift = div6[q] + 2;
358  int idx = rem6[q];
359  for (x = 0; x < 16; x++)
360  h->dequant4_coeff[i][q][(x >> 2) | ((x << 2) & 0xF)] =
361  ((uint32_t)dequant4_coeff_init[idx][(x & 1) + ((x >> 2) & 1)] *
362  h->pps.scaling_matrix4[i][x]) << shift;
363  }
364  }
365 }
366 
368 {
369  int i, x;
371  memset(h->dequant8_coeff, 0, sizeof(h->dequant8_coeff));
372 
373  if (h->pps.transform_8x8_mode)
375  if (h->sps.transform_bypass) {
376  for (i = 0; i < 6; i++)
377  for (x = 0; x < 16; x++)
378  h->dequant4_coeff[i][0][x] = 1 << 6;
380  for (i = 0; i < 6; i++)
381  for (x = 0; x < 64; x++)
382  h->dequant8_coeff[i][0][x] = 1 << 6;
383  }
384 }
385 
386 #define IN_RANGE(a, b, size) (((void*)(a) >= (void*)(b)) && ((void*)(a) < (void*)((b) + (size))))
387 
388 #define REBASE_PICTURE(pic, new_ctx, old_ctx) \
389  (((pic) && (pic) >= (old_ctx)->DPB && \
390  (pic) < (old_ctx)->DPB + H264_MAX_PICTURE_COUNT) ? \
391  &(new_ctx)->DPB[(pic) - (old_ctx)->DPB] : NULL)
392 
394  H264Context *new_base,
395  H264Context *old_base)
396 {
397  int i;
398 
399  for (i = 0; i < count; i++) {
400  av_assert1(!from[i] ||
401  IN_RANGE(from[i], old_base, 1) ||
402  IN_RANGE(from[i], old_base->DPB, H264_MAX_PICTURE_COUNT));
403  to[i] = REBASE_PICTURE(from[i], new_base, old_base);
404  }
405 }
406 
407 static int copy_parameter_set(void **to, void **from, int count, int size)
408 {
409  int i;
410 
411  for (i = 0; i < count; i++) {
412  if (to[i] && !from[i]) {
413  av_freep(&to[i]);
414  } else if (from[i] && !to[i]) {
415  to[i] = av_malloc(size);
416  if (!to[i])
417  return AVERROR(ENOMEM);
418  }
419 
420  if (from[i])
421  memcpy(to[i], from[i], size);
422  }
423 
424  return 0;
425 }
426 
427 #define copy_fields(to, from, start_field, end_field) \
428  memcpy(&(to)->start_field, &(from)->start_field, \
429  (char *)&(to)->end_field - (char *)&(to)->start_field)
430 
432 
434  const AVCodecContext *src)
435 {
436  H264Context *h = dst->priv_data, *h1 = src->priv_data;
437  int inited = h->context_initialized, err = 0;
438  int need_reinit = 0;
439  int i, ret;
440 
441  if (dst == src)
442  return 0;
443 
444  if (inited &&
445  (h->width != h1->width ||
446  h->height != h1->height ||
447  h->mb_width != h1->mb_width ||
448  h->mb_height != h1->mb_height ||
449  h->sps.bit_depth_luma != h1->sps.bit_depth_luma ||
450  h->sps.chroma_format_idc != h1->sps.chroma_format_idc ||
451  h->sps.colorspace != h1->sps.colorspace)) {
452 
453  need_reinit = 1;
454  }
455 
456  /* copy block_offset since frame_start may not be called */
457  memcpy(h->block_offset, h1->block_offset, sizeof(h->block_offset));
458 
459  // SPS/PPS
460  if ((ret = copy_parameter_set((void **)h->sps_buffers,
461  (void **)h1->sps_buffers,
462  MAX_SPS_COUNT, sizeof(SPS))) < 0)
463  return ret;
464  h->sps = h1->sps;
465  if ((ret = copy_parameter_set((void **)h->pps_buffers,
466  (void **)h1->pps_buffers,
467  MAX_PPS_COUNT, sizeof(PPS))) < 0)
468  return ret;
469  h->pps = h1->pps;
470 
471  if (need_reinit || !inited) {
472  h->width = h1->width;
473  h->height = h1->height;
474  h->mb_height = h1->mb_height;
475  h->mb_width = h1->mb_width;
476  h->mb_num = h1->mb_num;
477  h->mb_stride = h1->mb_stride;
478  h->b_stride = h1->b_stride;
479 
480  if (h->context_initialized || h1->context_initialized) {
481  if ((err = h264_slice_header_init(h)) < 0) {
482  av_log(h->avctx, AV_LOG_ERROR, "h264_slice_header_init() failed");
483  return err;
484  }
485  }
486  /* copy block_offset since frame_start may not be called */
487  memcpy(h->block_offset, h1->block_offset, sizeof(h->block_offset));
488  }
489 
490  h->avctx->coded_height = h1->avctx->coded_height;
491  h->avctx->coded_width = h1->avctx->coded_width;
492  h->avctx->width = h1->avctx->width;
493  h->avctx->height = h1->avctx->height;
494  h->coded_picture_number = h1->coded_picture_number;
495  h->first_field = h1->first_field;
496  h->picture_structure = h1->picture_structure;
497  h->droppable = h1->droppable;
498  h->low_delay = h1->low_delay;
499  h->backup_width = h1->backup_width;
500  h->backup_height = h1->backup_height;
501  h->backup_pix_fmt = h1->backup_pix_fmt;
502 
503  for (i = 0; i < H264_MAX_PICTURE_COUNT; i++) {
504  ff_h264_unref_picture(h, &h->DPB[i]);
505  if (h1->DPB[i].f->buf[0] &&
506  (ret = ff_h264_ref_picture(h, &h->DPB[i], &h1->DPB[i])) < 0)
507  return ret;
508  }
509 
510  h->cur_pic_ptr = REBASE_PICTURE(h1->cur_pic_ptr, h, h1);
512  if (h1->cur_pic.f->buf[0]) {
513  ret = ff_h264_ref_picture(h, &h->cur_pic, &h1->cur_pic);
514  if (ret < 0)
515  return ret;
516  }
517 
518  h->enable_er = h1->enable_er;
519  h->workaround_bugs = h1->workaround_bugs;
520  h->low_delay = h1->low_delay;
521  h->droppable = h1->droppable;
522 
523  // extradata/NAL handling
524  h->is_avc = h1->is_avc;
525  h->nal_length_size = h1->nal_length_size;
526  h->x264_build = h1->x264_build;
527 
528  // Dequantization matrices
529  // FIXME these are big - can they be only copied when PPS changes?
530  copy_fields(h, h1, dequant4_buffer, dequant4_coeff);
531 
532  for (i = 0; i < 6; i++)
533  h->dequant4_coeff[i] = h->dequant4_buffer[0] +
534  (h1->dequant4_coeff[i] - h1->dequant4_buffer[0]);
535 
536  for (i = 0; i < 6; i++)
537  h->dequant8_coeff[i] = h->dequant8_buffer[0] +
538  (h1->dequant8_coeff[i] - h1->dequant8_buffer[0]);
539 
540  h->dequant_coeff_pps = h1->dequant_coeff_pps;
541 
542  // POC timing
543  copy_fields(h, h1, poc_lsb, default_ref_list);
544 
545  // reference lists
546  copy_fields(h, h1, short_ref, current_slice);
547 
548  copy_picture_range(h->short_ref, h1->short_ref, 32, h, h1);
549  copy_picture_range(h->long_ref, h1->long_ref, 32, h, h1);
550  copy_picture_range(h->delayed_pic, h1->delayed_pic,
551  MAX_DELAYED_PIC_COUNT + 2, h, h1);
552 
553  h->frame_recovered = h1->frame_recovered;
554 
555  if (!h->cur_pic_ptr)
556  return 0;
557 
558  if (!h->droppable) {
560  h->prev_poc_msb = h->poc_msb;
561  h->prev_poc_lsb = h->poc_lsb;
562  }
564  h->prev_frame_num = h->frame_num;
565 
566  h->recovery_frame = h1->recovery_frame;
567 
568  return err;
569 }
570 
572 {
573  H264Picture *pic;
574  int i, ret;
575  const int pixel_shift = h->pixel_shift;
576  int c[4] = {
577  1<<(h->sps.bit_depth_luma-1),
578  1<<(h->sps.bit_depth_chroma-1),
579  1<<(h->sps.bit_depth_chroma-1),
580  -1
581  };
582 
583  if (!ff_thread_can_start_frame(h->avctx)) {
584  av_log(h->avctx, AV_LOG_ERROR, "Attempt to start a frame outside SETUP state\n");
585  return -1;
586  }
587 
589  h->cur_pic_ptr = NULL;
590 
591  i = find_unused_picture(h);
592  if (i < 0) {
593  av_log(h->avctx, AV_LOG_ERROR, "no frame buffer available\n");
594  return i;
595  }
596  pic = &h->DPB[i];
597 
598  pic->reference = h->droppable ? 0 : h->picture_structure;
601 
602  /*
603  * Zero key_frame here; IDR markings per slice in frame or fields are ORed
604  * in later.
605  * See decode_nal_units().
606  */
607  pic->f->key_frame = 0;
608  pic->mmco_reset = 0;
609  pic->recovered = 0;
610  pic->invalid_gap = 0;
612 
613  if ((ret = alloc_picture(h, pic)) < 0)
614  return ret;
615  if(!h->frame_recovered && !h->avctx->hwaccel
618 #endif
619  )
620  avpriv_color_frame(pic->f, c);
621 
622  h->cur_pic_ptr = pic;
624  if (CONFIG_ERROR_RESILIENCE) {
626  }
627 
628  if ((ret = ff_h264_ref_picture(h, &h->cur_pic, h->cur_pic_ptr)) < 0)
629  return ret;
630 
631  for (i = 0; i < h->nb_slice_ctx; i++) {
632  h->slice_ctx[i].linesize = h->cur_pic_ptr->f->linesize[0];
633  h->slice_ctx[i].uvlinesize = h->cur_pic_ptr->f->linesize[1];
634  }
635 
636  if (CONFIG_ERROR_RESILIENCE && h->enable_er) {
640  }
641 
642  for (i = 0; i < 16; i++) {
643  h->block_offset[i] = (4 * ((scan8[i] - scan8[0]) & 7) << pixel_shift) + 4 * pic->f->linesize[0] * ((scan8[i] - scan8[0]) >> 3);
644  h->block_offset[48 + i] = (4 * ((scan8[i] - scan8[0]) & 7) << pixel_shift) + 8 * pic->f->linesize[0] * ((scan8[i] - scan8[0]) >> 3);
645  }
646  for (i = 0; i < 16; i++) {
647  h->block_offset[16 + i] =
648  h->block_offset[32 + i] = (4 * ((scan8[i] - scan8[0]) & 7) << pixel_shift) + 4 * pic->f->linesize[1] * ((scan8[i] - scan8[0]) >> 3);
649  h->block_offset[48 + 16 + i] =
650  h->block_offset[48 + 32 + i] = (4 * ((scan8[i] - scan8[0]) & 7) << pixel_shift) + 8 * pic->f->linesize[1] * ((scan8[i] - scan8[0]) >> 3);
651  }
652 
653  /* We mark the current picture as non-reference after allocating it, so
654  * that if we break out due to an error it can be released automatically
655  * in the next ff_mpv_frame_start().
656  */
657  h->cur_pic_ptr->reference = 0;
658 
659  h->cur_pic_ptr->field_poc[0] = h->cur_pic_ptr->field_poc[1] = INT_MAX;
660 
661  h->next_output_pic = NULL;
662 
663  assert(h->cur_pic_ptr->long_ref == 0);
664 
665  return 0;
666 }
667 
669  uint8_t *src_y,
670  uint8_t *src_cb, uint8_t *src_cr,
671  int linesize, int uvlinesize,
672  int simple)
673 {
674  uint8_t *top_border;
675  int top_idx = 1;
676  const int pixel_shift = h->pixel_shift;
677  int chroma444 = CHROMA444(h);
678  int chroma422 = CHROMA422(h);
679 
680  src_y -= linesize;
681  src_cb -= uvlinesize;
682  src_cr -= uvlinesize;
683 
684  if (!simple && FRAME_MBAFF(h)) {
685  if (sl->mb_y & 1) {
686  if (!MB_MBAFF(sl)) {
687  top_border = sl->top_borders[0][sl->mb_x];
688  AV_COPY128(top_border, src_y + 15 * linesize);
689  if (pixel_shift)
690  AV_COPY128(top_border + 16, src_y + 15 * linesize + 16);
691  if (simple || !CONFIG_GRAY || !(h->flags & AV_CODEC_FLAG_GRAY)) {
692  if (chroma444) {
693  if (pixel_shift) {
694  AV_COPY128(top_border + 32, src_cb + 15 * uvlinesize);
695  AV_COPY128(top_border + 48, src_cb + 15 * uvlinesize + 16);
696  AV_COPY128(top_border + 64, src_cr + 15 * uvlinesize);
697  AV_COPY128(top_border + 80, src_cr + 15 * uvlinesize + 16);
698  } else {
699  AV_COPY128(top_border + 16, src_cb + 15 * uvlinesize);
700  AV_COPY128(top_border + 32, src_cr + 15 * uvlinesize);
701  }
702  } else if (chroma422) {
703  if (pixel_shift) {
704  AV_COPY128(top_border + 32, src_cb + 15 * uvlinesize);
705  AV_COPY128(top_border + 48, src_cr + 15 * uvlinesize);
706  } else {
707  AV_COPY64(top_border + 16, src_cb + 15 * uvlinesize);
708  AV_COPY64(top_border + 24, src_cr + 15 * uvlinesize);
709  }
710  } else {
711  if (pixel_shift) {
712  AV_COPY128(top_border + 32, src_cb + 7 * uvlinesize);
713  AV_COPY128(top_border + 48, src_cr + 7 * uvlinesize);
714  } else {
715  AV_COPY64(top_border + 16, src_cb + 7 * uvlinesize);
716  AV_COPY64(top_border + 24, src_cr + 7 * uvlinesize);
717  }
718  }
719  }
720  }
721  } else if (MB_MBAFF(sl)) {
722  top_idx = 0;
723  } else
724  return;
725  }
726 
727  top_border = sl->top_borders[top_idx][sl->mb_x];
728  /* There are two lines saved, the line above the top macroblock
729  * of a pair, and the line above the bottom macroblock. */
730  AV_COPY128(top_border, src_y + 16 * linesize);
731  if (pixel_shift)
732  AV_COPY128(top_border + 16, src_y + 16 * linesize + 16);
733 
734  if (simple || !CONFIG_GRAY || !(h->flags & AV_CODEC_FLAG_GRAY)) {
735  if (chroma444) {
736  if (pixel_shift) {
737  AV_COPY128(top_border + 32, src_cb + 16 * linesize);
738  AV_COPY128(top_border + 48, src_cb + 16 * linesize + 16);
739  AV_COPY128(top_border + 64, src_cr + 16 * linesize);
740  AV_COPY128(top_border + 80, src_cr + 16 * linesize + 16);
741  } else {
742  AV_COPY128(top_border + 16, src_cb + 16 * linesize);
743  AV_COPY128(top_border + 32, src_cr + 16 * linesize);
744  }
745  } else if (chroma422) {
746  if (pixel_shift) {
747  AV_COPY128(top_border + 32, src_cb + 16 * uvlinesize);
748  AV_COPY128(top_border + 48, src_cr + 16 * uvlinesize);
749  } else {
750  AV_COPY64(top_border + 16, src_cb + 16 * uvlinesize);
751  AV_COPY64(top_border + 24, src_cr + 16 * uvlinesize);
752  }
753  } else {
754  if (pixel_shift) {
755  AV_COPY128(top_border + 32, src_cb + 8 * uvlinesize);
756  AV_COPY128(top_border + 48, src_cr + 8 * uvlinesize);
757  } else {
758  AV_COPY64(top_border + 16, src_cb + 8 * uvlinesize);
759  AV_COPY64(top_border + 24, src_cr + 8 * uvlinesize);
760  }
761  }
762  }
763 }
764 
765 /**
766  * Initialize implicit_weight table.
767  * @param field 0/1 initialize the weight for interlaced MBAFF
768  * -1 initializes the rest
769  */
770 static void implicit_weight_table(const H264Context *h, H264SliceContext *sl, int field)
771 {
772  int ref0, ref1, i, cur_poc, ref_start, ref_count0, ref_count1;
773 
774  for (i = 0; i < 2; i++) {
775  sl->luma_weight_flag[i] = 0;
776  sl->chroma_weight_flag[i] = 0;
777  }
778 
779  if (field < 0) {
780  if (h->picture_structure == PICT_FRAME) {
781  cur_poc = h->cur_pic_ptr->poc;
782  } else {
783  cur_poc = h->cur_pic_ptr->field_poc[h->picture_structure - 1];
784  }
785  if (sl->ref_count[0] == 1 && sl->ref_count[1] == 1 && !FRAME_MBAFF(h) &&
786  sl->ref_list[0][0].poc + sl->ref_list[1][0].poc == 2 * cur_poc) {
787  sl->use_weight = 0;
788  sl->use_weight_chroma = 0;
789  return;
790  }
791  ref_start = 0;
792  ref_count0 = sl->ref_count[0];
793  ref_count1 = sl->ref_count[1];
794  } else {
795  cur_poc = h->cur_pic_ptr->field_poc[field];
796  ref_start = 16;
797  ref_count0 = 16 + 2 * sl->ref_count[0];
798  ref_count1 = 16 + 2 * sl->ref_count[1];
799  }
800 
801  sl->use_weight = 2;
802  sl->use_weight_chroma = 2;
803  sl->luma_log2_weight_denom = 5;
804  sl->chroma_log2_weight_denom = 5;
805 
806  for (ref0 = ref_start; ref0 < ref_count0; ref0++) {
807  int poc0 = sl->ref_list[0][ref0].poc;
808  for (ref1 = ref_start; ref1 < ref_count1; ref1++) {
809  int w = 32;
810  if (!sl->ref_list[0][ref0].parent->long_ref && !sl->ref_list[1][ref1].parent->long_ref) {
811  int poc1 = sl->ref_list[1][ref1].poc;
812  int td = av_clip_int8(poc1 - poc0);
813  if (td) {
814  int tb = av_clip_int8(cur_poc - poc0);
815  int tx = (16384 + (FFABS(td) >> 1)) / td;
816  int dist_scale_factor = (tb * tx + 32) >> 8;
817  if (dist_scale_factor >= -64 && dist_scale_factor <= 128)
818  w = 64 - dist_scale_factor;
819  }
820  }
821  if (field < 0) {
822  sl->implicit_weight[ref0][ref1][0] =
823  sl->implicit_weight[ref0][ref1][1] = w;
824  } else {
825  sl->implicit_weight[ref0][ref1][field] = w;
826  }
827  }
828  }
829 }
830 
831 /**
832  * initialize scan tables
833  */
835 {
836  int i;
837  for (i = 0; i < 16; i++) {
838 #define TRANSPOSE(x) ((x) >> 2) | (((x) << 2) & 0xF)
839  h->zigzag_scan[i] = TRANSPOSE(zigzag_scan[i]);
840  h->field_scan[i] = TRANSPOSE(field_scan[i]);
841 #undef TRANSPOSE
842  }
843  for (i = 0; i < 64; i++) {
844 #define TRANSPOSE(x) ((x) >> 3) | (((x) & 7) << 3)
849 #undef TRANSPOSE
850  }
851  if (h->sps.transform_bypass) { // FIXME same ugly
852  memcpy(h->zigzag_scan_q0 , zigzag_scan , sizeof(h->zigzag_scan_q0 ));
853  memcpy(h->zigzag_scan8x8_q0 , ff_zigzag_direct , sizeof(h->zigzag_scan8x8_q0 ));
855  memcpy(h->field_scan_q0 , field_scan , sizeof(h->field_scan_q0 ));
856  memcpy(h->field_scan8x8_q0 , field_scan8x8 , sizeof(h->field_scan8x8_q0 ));
858  } else {
859  memcpy(h->zigzag_scan_q0 , h->zigzag_scan , sizeof(h->zigzag_scan_q0 ));
860  memcpy(h->zigzag_scan8x8_q0 , h->zigzag_scan8x8 , sizeof(h->zigzag_scan8x8_q0 ));
862  memcpy(h->field_scan_q0 , h->field_scan , sizeof(h->field_scan_q0 ));
863  memcpy(h->field_scan8x8_q0 , h->field_scan8x8 , sizeof(h->field_scan8x8_q0 ));
865  }
866 }
867 
868 static enum AVPixelFormat get_pixel_format(H264Context *h, int force_callback)
869 {
870 #define HWACCEL_MAX (CONFIG_H264_DXVA2_HWACCEL + \
871  CONFIG_H264_D3D11VA_HWACCEL + \
872  CONFIG_H264_VAAPI_HWACCEL + \
873  (CONFIG_H264_VDA_HWACCEL * 2) + \
874  CONFIG_H264_VIDEOTOOLBOX_HWACCEL + \
875  CONFIG_H264_VDPAU_HWACCEL)
877  const enum AVPixelFormat *choices = pix_fmts;
878  int i;
879 
880  switch (h->sps.bit_depth_luma) {
881  case 9:
882  if (CHROMA444(h)) {
883  if (h->avctx->colorspace == AVCOL_SPC_RGB) {
884  *fmt++ = AV_PIX_FMT_GBRP9;
885  } else
886  *fmt++ = AV_PIX_FMT_YUV444P9;
887  } else if (CHROMA422(h))
888  *fmt++ = AV_PIX_FMT_YUV422P9;
889  else
890  *fmt++ = AV_PIX_FMT_YUV420P9;
891  break;
892  case 10:
893  if (CHROMA444(h)) {
894  if (h->avctx->colorspace == AVCOL_SPC_RGB) {
895  *fmt++ = AV_PIX_FMT_GBRP10;
896  } else
897  *fmt++ = AV_PIX_FMT_YUV444P10;
898  } else if (CHROMA422(h))
899  *fmt++ = AV_PIX_FMT_YUV422P10;
900  else
901  *fmt++ = AV_PIX_FMT_YUV420P10;
902  break;
903  case 12:
904  if (CHROMA444(h)) {
905  if (h->avctx->colorspace == AVCOL_SPC_RGB) {
906  *fmt++ = AV_PIX_FMT_GBRP12;
907  } else
908  *fmt++ = AV_PIX_FMT_YUV444P12;
909  } else if (CHROMA422(h))
910  *fmt++ = AV_PIX_FMT_YUV422P12;
911  else
912  *fmt++ = AV_PIX_FMT_YUV420P12;
913  break;
914  case 14:
915  if (CHROMA444(h)) {
916  if (h->avctx->colorspace == AVCOL_SPC_RGB) {
917  *fmt++ = AV_PIX_FMT_GBRP14;
918  } else
919  *fmt++ = AV_PIX_FMT_YUV444P14;
920  } else if (CHROMA422(h))
921  *fmt++ = AV_PIX_FMT_YUV422P14;
922  else
923  *fmt++ = AV_PIX_FMT_YUV420P14;
924  break;
925  case 8:
926 #if CONFIG_H264_VDPAU_HWACCEL
927  *fmt++ = AV_PIX_FMT_VDPAU;
928 #endif
929  if (CHROMA444(h)) {
930  if (h->avctx->colorspace == AVCOL_SPC_RGB)
931  *fmt++ = AV_PIX_FMT_GBRP;
932  else if (h->avctx->color_range == AVCOL_RANGE_JPEG)
933  *fmt++ = AV_PIX_FMT_YUVJ444P;
934  else
935  *fmt++ = AV_PIX_FMT_YUV444P;
936  } else if (CHROMA422(h)) {
938  *fmt++ = AV_PIX_FMT_YUVJ422P;
939  else
940  *fmt++ = AV_PIX_FMT_YUV422P;
941  } else {
942 #if CONFIG_H264_DXVA2_HWACCEL
943  *fmt++ = AV_PIX_FMT_DXVA2_VLD;
944 #endif
945 #if CONFIG_H264_D3D11VA_HWACCEL
946  *fmt++ = AV_PIX_FMT_D3D11VA_VLD;
947 #endif
948 #if CONFIG_H264_VAAPI_HWACCEL
949  *fmt++ = AV_PIX_FMT_VAAPI;
950 #endif
951 #if CONFIG_H264_VDA_HWACCEL
952  *fmt++ = AV_PIX_FMT_VDA_VLD;
953  *fmt++ = AV_PIX_FMT_VDA;
954 #endif
955 #if CONFIG_H264_VIDEOTOOLBOX_HWACCEL
956  *fmt++ = AV_PIX_FMT_VIDEOTOOLBOX;
957 #endif
958  if (h->avctx->codec->pix_fmts)
959  choices = h->avctx->codec->pix_fmts;
960  else if (h->avctx->color_range == AVCOL_RANGE_JPEG)
961  *fmt++ = AV_PIX_FMT_YUVJ420P;
962  else
963  *fmt++ = AV_PIX_FMT_YUV420P;
964  }
965  break;
966  default:
968  "Unsupported bit depth %d\n", h->sps.bit_depth_luma);
969  return AVERROR_INVALIDDATA;
970  }
971 
972  *fmt = AV_PIX_FMT_NONE;
973 
974  for (i=0; choices[i] != AV_PIX_FMT_NONE; i++)
975  if (choices[i] == h->avctx->pix_fmt && !force_callback)
976  return choices[i];
977  return ff_thread_get_format(h->avctx, choices);
978 }
979 
980 /* export coded and cropped frame dimensions to AVCodecContext */
982 {
983  int width = h->width - (h->sps.crop_right + h->sps.crop_left);
984  int height = h->height - (h->sps.crop_top + h->sps.crop_bottom);
985  av_assert0(h->sps.crop_right + h->sps.crop_left < (unsigned)h->width);
986  av_assert0(h->sps.crop_top + h->sps.crop_bottom < (unsigned)h->height);
987 
988  /* handle container cropping */
989  if (FFALIGN(h->avctx->width, 16) == FFALIGN(width, 16) &&
990  FFALIGN(h->avctx->height, 16) == FFALIGN(height, 16) &&
991  h->avctx->width <= width &&
992  h->avctx->height <= height
993  ) {
994  width = h->avctx->width;
995  height = h->avctx->height;
996  }
997 
998  if (width <= 0 || height <= 0) {
999  av_log(h->avctx, AV_LOG_ERROR, "Invalid cropped dimensions: %dx%d.\n",
1000  width, height);
1002  return AVERROR_INVALIDDATA;
1003 
1004  av_log(h->avctx, AV_LOG_WARNING, "Ignoring cropping information.\n");
1005  h->sps.crop_bottom =
1006  h->sps.crop_top =
1007  h->sps.crop_right =
1008  h->sps.crop_left =
1009  h->sps.crop = 0;
1010 
1011  width = h->width;
1012  height = h->height;
1013  }
1014 
1015  h->avctx->coded_width = h->width;
1016  h->avctx->coded_height = h->height;
1017  h->avctx->width = width;
1018  h->avctx->height = height;
1019 
1020  return 0;
1021 }
1022 
1024 {
1025  int nb_slices = (HAVE_THREADS &&
1027  h->avctx->thread_count : 1;
1028  int i, ret;
1029 
1030  ff_set_sar(h->avctx, h->sps.sar);
1032  &h->chroma_x_shift, &h->chroma_y_shift);
1033 
1034  if (h->sps.timing_info_present_flag) {
1035  int64_t den = h->sps.time_scale;
1036  if (h->x264_build < 44U)
1037  den *= 2;
1039  h->sps.num_units_in_tick * h->avctx->ticks_per_frame, den, 1 << 30);
1040  }
1041 
1043 
1044  h->first_field = 0;
1045  h->prev_interlaced_frame = 1;
1046 
1047  init_scan_tables(h);
1048  ret = ff_h264_alloc_tables(h);
1049  if (ret < 0) {
1050  av_log(h->avctx, AV_LOG_ERROR, "Could not allocate memory\n");
1051  goto fail;
1052  }
1053 
1054 #if FF_API_CAP_VDPAU
1055  if (h->avctx->codec &&
1057  (h->sps.bit_depth_luma != 8 || h->sps.chroma_format_idc > 1)) {
1059  "VDPAU decoding does not support video colorspace.\n");
1060  ret = AVERROR_INVALIDDATA;
1061  goto fail;
1062  }
1063 #endif
1064 
1065  if (h->sps.bit_depth_luma < 8 || h->sps.bit_depth_luma > 14 ||
1066  h->sps.bit_depth_luma == 11 || h->sps.bit_depth_luma == 13
1067  ) {
1068  av_log(h->avctx, AV_LOG_ERROR, "Unsupported bit depth %d\n",
1069  h->sps.bit_depth_luma);
1070  ret = AVERROR_INVALIDDATA;
1071  goto fail;
1072  }
1073 
1074  h->cur_bit_depth_luma =
1077  h->pixel_shift = h->sps.bit_depth_luma > 8;
1080 
1082  h->sps.chroma_format_idc);
1086  h->sps.chroma_format_idc);
1088 
1089  if (nb_slices > H264_MAX_THREADS || (nb_slices > h->mb_height && h->mb_height)) {
1090  int max_slices;
1091  if (h->mb_height)
1092  max_slices = FFMIN(H264_MAX_THREADS, h->mb_height);
1093  else
1094  max_slices = H264_MAX_THREADS;
1095  av_log(h->avctx, AV_LOG_WARNING, "too many threads/slices %d,"
1096  " reducing to %d\n", nb_slices, max_slices);
1097  nb_slices = max_slices;
1098  }
1099  h->slice_context_count = nb_slices;
1100 
1101  if (!HAVE_THREADS || !(h->avctx->active_thread_type & FF_THREAD_SLICE)) {
1102  ret = ff_h264_slice_context_init(h, &h->slice_ctx[0]);
1103  if (ret < 0) {
1104  av_log(h->avctx, AV_LOG_ERROR, "context_init() failed.\n");
1105  goto fail;
1106  }
1107  } else {
1108  for (i = 0; i < h->slice_context_count; i++) {
1109  H264SliceContext *sl = &h->slice_ctx[i];
1110 
1111  sl->h264 = h;
1112  sl->intra4x4_pred_mode = h->intra4x4_pred_mode + i * 8 * 2 * h->mb_stride;
1113  sl->mvd_table[0] = h->mvd_table[0] + i * 8 * 2 * h->mb_stride;
1114  sl->mvd_table[1] = h->mvd_table[1] + i * 8 * 2 * h->mb_stride;
1115 
1116  if ((ret = ff_h264_slice_context_init(h, sl)) < 0) {
1117  av_log(h->avctx, AV_LOG_ERROR, "context_init() failed.\n");
1118  goto fail;
1119  }
1120  }
1121  }
1122 
1123  h->context_initialized = 1;
1124 
1125  return 0;
1126 fail:
1128  h->context_initialized = 0;
1129  return ret;
1130 }
1131 
1133 {
1134  switch (a) {
1138  default:
1139  return a;
1140  }
1141 }
1142 
1143 /**
1144  * Decode a slice header.
1145  * This will (re)intialize the decoder and call h264_frame_start() as needed.
1146  *
1147  * @param h h264context
1148  *
1149  * @return 0 if okay, <0 if an error occurred, 1 if decoding must not be multithreaded
1150  */
1152 {
1153  unsigned int first_mb_in_slice;
1154  unsigned int pps_id;
1155  int ret;
1156  unsigned int slice_type, tmp, i, j;
1157  int last_pic_structure, last_pic_droppable;
1158  int must_reinit;
1159  int needs_reinit = 0;
1160  int field_pic_flag, bottom_field_flag;
1161  int first_slice = sl == h->slice_ctx && !h->current_slice;
1162  int frame_num, droppable, picture_structure;
1163  int mb_aff_frame, last_mb_aff_frame;
1164  PPS *pps;
1165 
1166  if (first_slice)
1168 
1171 
1172  first_mb_in_slice = get_ue_golomb_long(&sl->gb);
1173 
1174  if (first_mb_in_slice == 0) { // FIXME better field boundary detection
1175  if (h->current_slice) {
1176  if (h->setup_finished) {
1177  av_log(h->avctx, AV_LOG_ERROR, "Too many fields\n");
1178  return AVERROR_INVALIDDATA;
1179  }
1180  if (h->cur_pic_ptr && FIELD_PICTURE(h) && h->first_field) {
1181  ret = ff_h264_field_end(h, h->slice_ctx, 1);
1182  h->current_slice = 0;
1183  if (ret < 0)
1184  return ret;
1185  } else if (h->cur_pic_ptr && !FIELD_PICTURE(h) && !h->first_field && h->nal_unit_type == NAL_IDR_SLICE) {
1186  av_log(h, AV_LOG_WARNING, "Broken frame packetizing\n");
1187  ret = ff_h264_field_end(h, h->slice_ctx, 1);
1188  h->current_slice = 0;
1189  ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX, 0);
1190  ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX, 1);
1191  h->cur_pic_ptr = NULL;
1192  if (ret < 0)
1193  return ret;
1194  } else
1195  return AVERROR_INVALIDDATA;
1196  }
1197 
1198  if (!h->first_field) {
1199  if (h->cur_pic_ptr && !h->droppable) {
1202  }
1203  h->cur_pic_ptr = NULL;
1204  }
1205  }
1206 
1207  slice_type = get_ue_golomb_31(&sl->gb);
1208  if (slice_type > 9) {
1210  "slice type %d too large at %d\n",
1211  slice_type, first_mb_in_slice);
1212  return AVERROR_INVALIDDATA;
1213  }
1214  if (slice_type > 4) {
1215  slice_type -= 5;
1216  sl->slice_type_fixed = 1;
1217  } else
1218  sl->slice_type_fixed = 0;
1219 
1220  slice_type = golomb_to_pict_type[slice_type];
1221 
1222  sl->slice_type = slice_type;
1223  sl->slice_type_nos = slice_type & 3;
1224 
1225  if (h->nal_unit_type == NAL_IDR_SLICE &&
1227  av_log(h->avctx, AV_LOG_ERROR, "A non-intra slice in an IDR NAL unit.\n");
1228  return AVERROR_INVALIDDATA;
1229  }
1230 
1231  if (h->current_slice == 0 && !h->first_field) {
1232  if (
1233  (h->avctx->skip_frame >= AVDISCARD_NONREF && !h->nal_ref_idc) ||
1237  h->avctx->skip_frame >= AVDISCARD_ALL) {
1238  return SLICE_SKIPED;
1239  }
1240  }
1241 
1242  // to make a few old functions happy, it's wrong though
1243  if (!h->setup_finished)
1244  h->pict_type = sl->slice_type;
1245 
1246  pps_id = get_ue_golomb(&sl->gb);
1247  if (pps_id >= MAX_PPS_COUNT) {
1248  av_log(h->avctx, AV_LOG_ERROR, "pps_id %u out of range\n", pps_id);
1249  return AVERROR_INVALIDDATA;
1250  }
1251  if (!h->pps_buffers[pps_id]) {
1253  "non-existing PPS %u referenced\n",
1254  pps_id);
1255  return AVERROR_INVALIDDATA;
1256  }
1257  if (h->au_pps_id >= 0 && pps_id != h->au_pps_id) {
1259  "PPS change from %d to %d forbidden\n",
1260  h->au_pps_id, pps_id);
1261  return AVERROR_INVALIDDATA;
1262  }
1263 
1264  pps = h->pps_buffers[pps_id];
1265 
1266  if (!h->sps_buffers[pps->sps_id]) {
1268  "non-existing SPS %u referenced\n",
1269  h->pps.sps_id);
1270  return AVERROR_INVALIDDATA;
1271  }
1272 
1273  if (first_slice) {
1274  h->pps = *h->pps_buffers[pps_id];
1275  } else if (h->setup_finished && h->dequant_coeff_pps != pps_id) {
1276  av_log(h->avctx, AV_LOG_ERROR, "PPS changed between slices\n");
1277  return AVERROR_INVALIDDATA;
1278  }
1279 
1280  if (pps->sps_id != h->sps.sps_id ||
1281  pps->sps_id != h->current_sps_id ||
1282  h->sps_buffers[pps->sps_id]->new) {
1283 
1284  if (!first_slice) {
1286  "SPS changed in the middle of the frame\n");
1287  return AVERROR_INVALIDDATA;
1288  }
1289 
1290  h->sps = *h->sps_buffers[h->pps.sps_id];
1291 
1292  if (h->mb_width != h->sps.mb_width ||
1293  h->mb_height != h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag) ||
1296  )
1297  needs_reinit = 1;
1298 
1299  if (h->bit_depth_luma != h->sps.bit_depth_luma ||
1301  needs_reinit = 1;
1302 
1303  if (h->flags & AV_CODEC_FLAG_LOW_DELAY ||
1305  !h->sps.num_reorder_frames)) {
1306  if (h->avctx->has_b_frames > 1 || h->delayed_pic[0])
1307  av_log(h->avctx, AV_LOG_WARNING, "Delayed frames seen. "
1308  "Reenabling low delay requires a codec flush.\n");
1309  else
1310  h->low_delay = 1;
1311  }
1312 
1313  if (h->avctx->has_b_frames < 2)
1314  h->avctx->has_b_frames = !h->low_delay;
1315 
1316  }
1317 
1318  must_reinit = (h->context_initialized &&
1319  ( 16*h->sps.mb_width != h->avctx->coded_width
1320  || 16*h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag) != h->avctx->coded_height
1323  || h->mb_width != h->sps.mb_width
1324  || h->mb_height != h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag)
1325  ));
1326  if (h->avctx->pix_fmt == AV_PIX_FMT_NONE
1328  must_reinit = 1;
1329 
1330  if (first_slice && av_cmp_q(h->sps.sar, h->avctx->sample_aspect_ratio))
1331  must_reinit = 1;
1332 
1333  if (!h->setup_finished) {
1334  h->avctx->profile = ff_h264_get_profile(&h->sps);
1335  h->avctx->level = h->sps.level_idc;
1336  h->avctx->refs = h->sps.ref_frame_count;
1337 
1338  h->mb_width = h->sps.mb_width;
1339  h->mb_height = h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag);
1340  h->mb_num = h->mb_width * h->mb_height;
1341  h->mb_stride = h->mb_width + 1;
1342 
1343  h->b_stride = h->mb_width * 4;
1344 
1345  h->chroma_y_shift = h->sps.chroma_format_idc <= 1; // 400 uses yuv420p
1346 
1347  h->width = 16 * h->mb_width;
1348  h->height = 16 * h->mb_height;
1349 
1350  ret = init_dimensions(h);
1351  if (ret < 0)
1352  return ret;
1353 
1356  : AVCOL_RANGE_MPEG;
1358  if (h->avctx->colorspace != h->sps.colorspace)
1359  needs_reinit = 1;
1361  h->avctx->color_trc = h->sps.color_trc;
1362  h->avctx->colorspace = h->sps.colorspace;
1363  }
1364  }
1365  }
1366 
1367  if (h->context_initialized &&
1368  (must_reinit || needs_reinit)) {
1369  h->context_initialized = 0;
1370  if (sl != h->slice_ctx) {
1372  "changing width %d -> %d / height %d -> %d on "
1373  "slice %d\n",
1374  h->width, h->avctx->coded_width,
1375  h->height, h->avctx->coded_height,
1376  h->current_slice + 1);
1377  return AVERROR_INVALIDDATA;
1378  }
1379 
1380  av_assert1(first_slice);
1381 
1383 
1384  if ((ret = get_pixel_format(h, 1)) < 0)
1385  return ret;
1386  h->avctx->pix_fmt = ret;
1387 
1388  av_log(h->avctx, AV_LOG_INFO, "Reinit context to %dx%d, "
1389  "pix_fmt: %s\n", h->width, h->height, av_get_pix_fmt_name(h->avctx->pix_fmt));
1390 
1391  if ((ret = h264_slice_header_init(h)) < 0) {
1393  "h264_slice_header_init() failed\n");
1394  return ret;
1395  }
1396  }
1397  if (!h->context_initialized) {
1398  if (sl != h->slice_ctx) {
1400  "Cannot (re-)initialize context during parallel decoding.\n");
1401  return AVERROR_PATCHWELCOME;
1402  }
1403 
1404  if ((ret = get_pixel_format(h, 1)) < 0)
1405  return ret;
1406  h->avctx->pix_fmt = ret;
1407 
1408  if ((ret = h264_slice_header_init(h)) < 0) {
1410  "h264_slice_header_init() failed\n");
1411  return ret;
1412  }
1413  }
1414 
1415  if (first_slice && h->dequant_coeff_pps != pps_id) {
1416  h->dequant_coeff_pps = pps_id;
1418  }
1419 
1420  frame_num = get_bits(&sl->gb, h->sps.log2_max_frame_num);
1421  if (!first_slice) {
1422  if (h->frame_num != frame_num) {
1423  av_log(h->avctx, AV_LOG_ERROR, "Frame num change from %d to %d\n",
1424  h->frame_num, frame_num);
1425  return AVERROR_INVALIDDATA;
1426  }
1427  }
1428 
1429  if (!h->setup_finished)
1430  h->frame_num = frame_num;
1431 
1432  sl->mb_mbaff = 0;
1433  mb_aff_frame = 0;
1434  last_mb_aff_frame = h->mb_aff_frame;
1435  last_pic_structure = h->picture_structure;
1436  last_pic_droppable = h->droppable;
1437 
1438  droppable = h->nal_ref_idc == 0;
1439  if (h->sps.frame_mbs_only_flag) {
1440  picture_structure = PICT_FRAME;
1441  } else {
1442  if (!h->sps.direct_8x8_inference_flag && slice_type == AV_PICTURE_TYPE_B) {
1443  av_log(h->avctx, AV_LOG_ERROR, "This stream was generated by a broken encoder, invalid 8x8 inference\n");
1444  return -1;
1445  }
1446  field_pic_flag = get_bits1(&sl->gb);
1447 
1448  if (field_pic_flag) {
1449  bottom_field_flag = get_bits1(&sl->gb);
1450  picture_structure = PICT_TOP_FIELD + bottom_field_flag;
1451  } else {
1452  picture_structure = PICT_FRAME;
1453  mb_aff_frame = h->sps.mb_aff;
1454  }
1455  }
1456 
1457  if (h->current_slice) {
1458  if (last_pic_structure != picture_structure ||
1459  last_pic_droppable != droppable ||
1460  last_mb_aff_frame != mb_aff_frame) {
1462  "Changing field mode (%d -> %d) between slices is not allowed\n",
1463  last_pic_structure, h->picture_structure);
1464  return AVERROR_INVALIDDATA;
1465  } else if (!h->cur_pic_ptr) {
1467  "unset cur_pic_ptr on slice %d\n",
1468  h->current_slice + 1);
1469  return AVERROR_INVALIDDATA;
1470  }
1471  }
1472 
1473  h->picture_structure = picture_structure;
1474  if (!h->setup_finished) {
1475  h->droppable = droppable;
1476  h->picture_structure = picture_structure;
1477  h->mb_aff_frame = mb_aff_frame;
1478  }
1479  sl->mb_field_decoding_flag = picture_structure != PICT_FRAME;
1480 
1481  if (h->current_slice == 0) {
1482  /* Shorten frame num gaps so we don't have to allocate reference
1483  * frames just to throw them away */
1484  if (h->frame_num != h->prev_frame_num) {
1485  int unwrap_prev_frame_num = h->prev_frame_num;
1486  int max_frame_num = 1 << h->sps.log2_max_frame_num;
1487 
1488  if (unwrap_prev_frame_num > h->frame_num)
1489  unwrap_prev_frame_num -= max_frame_num;
1490 
1491  if ((h->frame_num - unwrap_prev_frame_num) > h->sps.ref_frame_count) {
1492  unwrap_prev_frame_num = (h->frame_num - h->sps.ref_frame_count) - 1;
1493  if (unwrap_prev_frame_num < 0)
1494  unwrap_prev_frame_num += max_frame_num;
1495 
1496  h->prev_frame_num = unwrap_prev_frame_num;
1497  }
1498  }
1499 
1500  /* See if we have a decoded first field looking for a pair...
1501  * Here, we're using that to see if we should mark previously
1502  * decode frames as "finished".
1503  * We have to do that before the "dummy" in-between frame allocation,
1504  * since that can modify h->cur_pic_ptr. */
1505  if (h->first_field) {
1506  av_assert0(h->cur_pic_ptr);
1507  av_assert0(h->cur_pic_ptr->f->buf[0]);
1508  assert(h->cur_pic_ptr->reference != DELAYED_PIC_REF);
1509 
1510  /* Mark old field/frame as completed */
1511  if (h->cur_pic_ptr->tf.owner == h->avctx) {
1513  last_pic_structure == PICT_BOTTOM_FIELD);
1514  }
1515 
1516  /* figure out if we have a complementary field pair */
1517  if (!FIELD_PICTURE(h) || h->picture_structure == last_pic_structure) {
1518  /* Previous field is unmatched. Don't display it, but let it
1519  * remain for reference if marked as such. */
1520  if (last_pic_structure != PICT_FRAME) {
1522  last_pic_structure == PICT_TOP_FIELD);
1523  }
1524  } else {
1525  if (h->cur_pic_ptr->frame_num != h->frame_num) {
1526  /* This and previous field were reference, but had
1527  * different frame_nums. Consider this field first in
1528  * pair. Throw away previous field except for reference
1529  * purposes. */
1530  if (last_pic_structure != PICT_FRAME) {
1532  last_pic_structure == PICT_TOP_FIELD);
1533  }
1534  } else {
1535  /* Second field in complementary pair */
1536  if (!((last_pic_structure == PICT_TOP_FIELD &&
1538  (last_pic_structure == PICT_BOTTOM_FIELD &&
1541  "Invalid field mode combination %d/%d\n",
1542  last_pic_structure, h->picture_structure);
1543  h->picture_structure = last_pic_structure;
1544  h->droppable = last_pic_droppable;
1545  return AVERROR_INVALIDDATA;
1546  } else if (last_pic_droppable != h->droppable) {
1548  "Found reference and non-reference fields in the same frame, which");
1549  h->picture_structure = last_pic_structure;
1550  h->droppable = last_pic_droppable;
1551  return AVERROR_PATCHWELCOME;
1552  }
1553  }
1554  }
1555  }
1556 
1557  while (h->frame_num != h->prev_frame_num && !h->first_field &&
1558  h->frame_num != (h->prev_frame_num + 1) % (1 << h->sps.log2_max_frame_num)) {
1559  H264Picture *prev = h->short_ref_count ? h->short_ref[0] : NULL;
1560  av_log(h->avctx, AV_LOG_DEBUG, "Frame num gap %d %d\n",
1561  h->frame_num, h->prev_frame_num);
1563  for(i=0; i<FF_ARRAY_ELEMS(h->last_pocs); i++)
1564  h->last_pocs[i] = INT_MIN;
1565  ret = h264_frame_start(h);
1566  if (ret < 0) {
1567  h->first_field = 0;
1568  return ret;
1569  }
1570 
1571  h->prev_frame_num++;
1572  h->prev_frame_num %= 1 << h->sps.log2_max_frame_num;
1575  ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX, 0);
1576  ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX, 1);
1578  if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
1579  return ret;
1581  if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
1582  return ret;
1583  /* Error concealment: If a ref is missing, copy the previous ref
1584  * in its place.
1585  * FIXME: Avoiding a memcpy would be nice, but ref handling makes
1586  * many assumptions about there being no actual duplicates.
1587  * FIXME: This does not copy padding for out-of-frame motion
1588  * vectors. Given we are concealing a lost frame, this probably
1589  * is not noticeable by comparison, but it should be fixed. */
1590  if (h->short_ref_count) {
1591  if (prev &&
1592  h->short_ref[0]->f->width == prev->f->width &&
1593  h->short_ref[0]->f->height == prev->f->height &&
1594  h->short_ref[0]->f->format == prev->f->format) {
1595  av_image_copy(h->short_ref[0]->f->data,
1596  h->short_ref[0]->f->linesize,
1597  (const uint8_t **)prev->f->data,
1598  prev->f->linesize,
1599  prev->f->format,
1600  prev->f->width,
1601  prev->f->height);
1602  h->short_ref[0]->poc = prev->poc + 2;
1603  }
1604  h->short_ref[0]->frame_num = h->prev_frame_num;
1605  }
1606  }
1607 
1608  /* See if we have a decoded first field looking for a pair...
1609  * We're using that to see whether to continue decoding in that
1610  * frame, or to allocate a new one. */
1611  if (h->first_field) {
1612  av_assert0(h->cur_pic_ptr);
1613  av_assert0(h->cur_pic_ptr->f->buf[0]);
1614  assert(h->cur_pic_ptr->reference != DELAYED_PIC_REF);
1615 
1616  /* figure out if we have a complementary field pair */
1617  if (!FIELD_PICTURE(h) || h->picture_structure == last_pic_structure) {
1618  /* Previous field is unmatched. Don't display it, but let it
1619  * remain for reference if marked as such. */
1620  h->missing_fields ++;
1621  h->cur_pic_ptr = NULL;
1622  h->first_field = FIELD_PICTURE(h);
1623  } else {
1624  h->missing_fields = 0;
1625  if (h->cur_pic_ptr->frame_num != h->frame_num) {
1628  /* This and the previous field had different frame_nums.
1629  * Consider this field first in pair. Throw away previous
1630  * one except for reference purposes. */
1631  h->first_field = 1;
1632  h->cur_pic_ptr = NULL;
1633  } else {
1634  /* Second field in complementary pair */
1635  h->first_field = 0;
1636  }
1637  }
1638  } else {
1639  /* Frame or first field in a potentially complementary pair */
1640  h->first_field = FIELD_PICTURE(h);
1641  }
1642 
1643  if (!FIELD_PICTURE(h) || h->first_field) {
1644  if (h264_frame_start(h) < 0) {
1645  h->first_field = 0;
1646  return AVERROR_INVALIDDATA;
1647  }
1648  } else {
1650  }
1651  /* Some macroblocks can be accessed before they're available in case
1652  * of lost slices, MBAFF or threading. */
1653  if (FIELD_PICTURE(h)) {
1654  for(i = (h->picture_structure == PICT_BOTTOM_FIELD); i<h->mb_height; i++)
1655  memset(h->slice_table + i*h->mb_stride, -1, (h->mb_stride - (i+1==h->mb_height)) * sizeof(*h->slice_table));
1656  } else {
1657  memset(h->slice_table, -1,
1658  (h->mb_height * h->mb_stride - 1) * sizeof(*h->slice_table));
1659  }
1660  h->last_slice_type = -1;
1661  }
1662 
1663  if (!h->setup_finished)
1664  h->cur_pic_ptr->frame_num = h->frame_num; // FIXME frame_num cleanup
1665 
1666  av_assert1(h->mb_num == h->mb_width * h->mb_height);
1667  if (first_mb_in_slice << FIELD_OR_MBAFF_PICTURE(h) >= h->mb_num ||
1668  first_mb_in_slice >= h->mb_num) {
1669  av_log(h->avctx, AV_LOG_ERROR, "first_mb_in_slice overflow\n");
1670  return AVERROR_INVALIDDATA;
1671  }
1672  sl->resync_mb_x = sl->mb_x = first_mb_in_slice % h->mb_width;
1673  sl->resync_mb_y = sl->mb_y = (first_mb_in_slice / h->mb_width) <<
1676  sl->resync_mb_y = sl->mb_y = sl->mb_y + 1;
1677  av_assert1(sl->mb_y < h->mb_height);
1678 
1679  if (h->picture_structure == PICT_FRAME) {
1680  h->curr_pic_num = h->frame_num;
1681  h->max_pic_num = 1 << h->sps.log2_max_frame_num;
1682  } else {
1683  h->curr_pic_num = 2 * h->frame_num + 1;
1684  h->max_pic_num = 1 << (h->sps.log2_max_frame_num + 1);
1685  }
1686 
1687  if (h->nal_unit_type == NAL_IDR_SLICE)
1688  get_ue_golomb(&sl->gb); /* idr_pic_id */
1689 
1690  if (h->sps.poc_type == 0) {
1691  int poc_lsb = get_bits(&sl->gb, h->sps.log2_max_poc_lsb);
1692 
1693  if (!h->setup_finished)
1694  h->poc_lsb = poc_lsb;
1695 
1696  if (h->pps.pic_order_present == 1 && h->picture_structure == PICT_FRAME) {
1697  int delta_poc_bottom = get_se_golomb(&sl->gb);
1698  if (!h->setup_finished)
1699  h->delta_poc_bottom = delta_poc_bottom;
1700  }
1701  }
1702 
1703  if (h->sps.poc_type == 1 && !h->sps.delta_pic_order_always_zero_flag) {
1704  int delta_poc = get_se_golomb(&sl->gb);
1705 
1706  if (!h->setup_finished)
1707  h->delta_poc[0] = delta_poc;
1708 
1709  if (h->pps.pic_order_present == 1 && h->picture_structure == PICT_FRAME) {
1710  delta_poc = get_se_golomb(&sl->gb);
1711 
1712  if (!h->setup_finished)
1713  h->delta_poc[1] = delta_poc;
1714  }
1715  }
1716 
1717  if (!h->setup_finished)
1719 
1721  sl->redundant_pic_count = get_ue_golomb(&sl->gb);
1722 
1723  ret = ff_set_ref_count(h, sl);
1724  if (ret < 0)
1725  return ret;
1726 
1727  if (slice_type != AV_PICTURE_TYPE_I &&
1728  (h->current_slice == 0 ||
1729  slice_type != h->last_slice_type ||
1730  memcmp(h->last_ref_count, sl->ref_count, sizeof(sl->ref_count)))) {
1731 
1733  }
1734 
1735  if (sl->slice_type_nos != AV_PICTURE_TYPE_I) {
1737  if (ret < 0) {
1738  sl->ref_count[1] = sl->ref_count[0] = 0;
1739  return ret;
1740  }
1741  }
1742 
1743  if ((h->pps.weighted_pred && sl->slice_type_nos == AV_PICTURE_TYPE_P) ||
1744  (h->pps.weighted_bipred_idc == 1 &&
1746  ff_pred_weight_table(h, sl);
1747  else if (h->pps.weighted_bipred_idc == 2 &&
1749  implicit_weight_table(h, sl, -1);
1750  } else {
1751  sl->use_weight = 0;
1752  for (i = 0; i < 2; i++) {
1753  sl->luma_weight_flag[i] = 0;
1754  sl->chroma_weight_flag[i] = 0;
1755  }
1756  }
1757 
1758  // If frame-mt is enabled, only update mmco tables for the first slice
1759  // in a field. Subsequent slices can temporarily clobber h->mmco_index
1760  // or h->mmco, which will cause ref list mix-ups and decoding errors
1761  // further down the line. This may break decoding if the first slice is
1762  // corrupt, thus we only do this if frame-mt is enabled.
1763  if (h->nal_ref_idc) {
1764  ret = ff_h264_decode_ref_pic_marking(h, &sl->gb,
1766  h->current_slice == 0);
1767  if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
1768  return AVERROR_INVALIDDATA;
1769  }
1770 
1771  if (FRAME_MBAFF(h)) {
1773 
1774  if (h->pps.weighted_bipred_idc == 2 && sl->slice_type_nos == AV_PICTURE_TYPE_B) {
1775  implicit_weight_table(h, sl, 0);
1776  implicit_weight_table(h, sl, 1);
1777  }
1778  }
1779 
1783 
1784  if (sl->slice_type_nos != AV_PICTURE_TYPE_I && h->pps.cabac) {
1785  tmp = get_ue_golomb_31(&sl->gb);
1786  if (tmp > 2) {
1787  av_log(h->avctx, AV_LOG_ERROR, "cabac_init_idc %u overflow\n", tmp);
1788  return AVERROR_INVALIDDATA;
1789  }
1790  sl->cabac_init_idc = tmp;
1791  }
1792 
1793  sl->last_qscale_diff = 0;
1794  tmp = h->pps.init_qp + get_se_golomb(&sl->gb);
1795  if (tmp > 51 + 6 * (h->sps.bit_depth_luma - 8)) {
1796  av_log(h->avctx, AV_LOG_ERROR, "QP %u out of range\n", tmp);
1797  return AVERROR_INVALIDDATA;
1798  }
1799  sl->qscale = tmp;
1800  sl->chroma_qp[0] = get_chroma_qp(h, 0, sl->qscale);
1801  sl->chroma_qp[1] = get_chroma_qp(h, 1, sl->qscale);
1802  // FIXME qscale / qp ... stuff
1803  if (sl->slice_type == AV_PICTURE_TYPE_SP)
1804  get_bits1(&sl->gb); /* sp_for_switch_flag */
1805  if (sl->slice_type == AV_PICTURE_TYPE_SP ||
1807  get_se_golomb(&sl->gb); /* slice_qs_delta */
1808 
1809  sl->deblocking_filter = 1;
1810  sl->slice_alpha_c0_offset = 0;
1811  sl->slice_beta_offset = 0;
1813  tmp = get_ue_golomb_31(&sl->gb);
1814  if (tmp > 2) {
1816  "deblocking_filter_idc %u out of range\n", tmp);
1817  return AVERROR_INVALIDDATA;
1818  }
1819  sl->deblocking_filter = tmp;
1820  if (sl->deblocking_filter < 2)
1821  sl->deblocking_filter ^= 1; // 1<->0
1822 
1823  if (sl->deblocking_filter) {
1824  sl->slice_alpha_c0_offset = get_se_golomb(&sl->gb) * 2;
1825  sl->slice_beta_offset = get_se_golomb(&sl->gb) * 2;
1826  if (sl->slice_alpha_c0_offset > 12 ||
1827  sl->slice_alpha_c0_offset < -12 ||
1828  sl->slice_beta_offset > 12 ||
1829  sl->slice_beta_offset < -12) {
1831  "deblocking filter parameters %d %d out of range\n",
1833  return AVERROR_INVALIDDATA;
1834  }
1835  }
1836  }
1837 
1838  if (h->avctx->skip_loop_filter >= AVDISCARD_ALL ||
1840  h->nal_unit_type != NAL_IDR_SLICE) ||
1846  h->nal_ref_idc == 0))
1847  sl->deblocking_filter = 0;
1848 
1849  if (sl->deblocking_filter == 1 && h->max_contexts > 1) {
1850  if (h->avctx->flags2 & AV_CODEC_FLAG2_FAST) {
1851  /* Cheat slightly for speed:
1852  * Do not bother to deblock across slices. */
1853  sl->deblocking_filter = 2;
1854  } else {
1855  h->max_contexts = 1;
1856  if (!h->single_decode_warning) {
1857  av_log(h->avctx, AV_LOG_INFO,
1858  "Cannot parallelize slice decoding with deblocking filter type 1, decoding such frames in sequential order\n"
1859  "To parallelize slice decoding you need video encoded with disable_deblocking_filter_idc set to 2 (deblock only edges that do not cross slices).\n"
1860  "Setting the flags2 libavcodec option to +fast (-flags2 +fast) will disable deblocking across slices and enable parallel slice decoding "
1861  "but will generate non-standard-compliant output.\n");
1862  h->single_decode_warning = 1;
1863  }
1864  if (sl != h->slice_ctx) {
1866  "Deblocking switched inside frame.\n");
1867  return SLICE_SINGLETHREAD;
1868  }
1869  }
1870  }
1871  sl->qp_thresh = 15 -
1873  FFMAX3(0,
1875  h->pps.chroma_qp_index_offset[1]) +
1876  6 * (h->sps.bit_depth_luma - 8);
1877 
1878  h->last_slice_type = slice_type;
1879  memcpy(h->last_ref_count, sl->ref_count, sizeof(h->last_ref_count));
1880  sl->slice_num = ++h->current_slice;
1881 
1882  if (sl->slice_num)
1883  h->slice_row[(sl->slice_num-1)&(MAX_SLICES-1)]= sl->resync_mb_y;
1884  if ( h->slice_row[sl->slice_num&(MAX_SLICES-1)] + 3 >= sl->resync_mb_y
1885  && h->slice_row[sl->slice_num&(MAX_SLICES-1)] <= sl->resync_mb_y
1886  && sl->slice_num >= MAX_SLICES) {
1887  //in case of ASO this check needs to be updated depending on how we decide to assign slice numbers in this case
1888  av_log(h->avctx, AV_LOG_WARNING, "Possibly too many slices (%d >= %d), increase MAX_SLICES and recompile if there are artifacts\n", sl->slice_num, MAX_SLICES);
1889  }
1890 
1891  for (j = 0; j < 2; j++) {
1892  int id_list[16];
1893  int *ref2frm = sl->ref2frm[sl->slice_num & (MAX_SLICES - 1)][j];
1894  for (i = 0; i < 16; i++) {
1895  id_list[i] = 60;
1896  if (j < sl->list_count && i < sl->ref_count[j] &&
1897  sl->ref_list[j][i].parent->f->buf[0]) {
1898  int k;
1899  AVBuffer *buf = sl->ref_list[j][i].parent->f->buf[0]->buffer;
1900  for (k = 0; k < h->short_ref_count; k++)
1901  if (h->short_ref[k]->f->buf[0]->buffer == buf) {
1902  id_list[i] = k;
1903  break;
1904  }
1905  for (k = 0; k < h->long_ref_count; k++)
1906  if (h->long_ref[k] && h->long_ref[k]->f->buf[0]->buffer == buf) {
1907  id_list[i] = h->short_ref_count + k;
1908  break;
1909  }
1910  }
1911  }
1912 
1913  ref2frm[0] =
1914  ref2frm[1] = -1;
1915  for (i = 0; i < 16; i++)
1916  ref2frm[i + 2] = 4 * id_list[i] + (sl->ref_list[j][i].reference & 3);
1917  ref2frm[18 + 0] =
1918  ref2frm[18 + 1] = -1;
1919  for (i = 16; i < 48; i++)
1920  ref2frm[i + 4] = 4 * id_list[(i - 16) >> 1] +
1921  (sl->ref_list[j][i].reference & 3);
1922  }
1923 
1924  h->au_pps_id = pps_id;
1925  h->sps.new =
1926  h->sps_buffers[h->pps.sps_id]->new = 0;
1927  h->current_sps_id = h->pps.sps_id;
1928 
1929  if (h->avctx->debug & FF_DEBUG_PICT_INFO) {
1931  "slice:%d %s mb:%d %c%s%s pps:%u frame:%d poc:%d/%d ref:%d/%d qp:%d loop:%d:%d:%d weight:%d%s %s\n",
1932  sl->slice_num,
1933  (h->picture_structure == PICT_FRAME ? "F" : h->picture_structure == PICT_TOP_FIELD ? "T" : "B"),
1934  first_mb_in_slice,
1936  sl->slice_type_fixed ? " fix" : "",
1937  h->nal_unit_type == NAL_IDR_SLICE ? " IDR" : "",
1938  pps_id, h->frame_num,
1939  h->cur_pic_ptr->field_poc[0],
1940  h->cur_pic_ptr->field_poc[1],
1941  sl->ref_count[0], sl->ref_count[1],
1942  sl->qscale,
1943  sl->deblocking_filter,
1945  sl->use_weight,
1946  sl->use_weight == 1 && sl->use_weight_chroma ? "c" : "",
1947  sl->slice_type == AV_PICTURE_TYPE_B ? (sl->direct_spatial_mv_pred ? "SPAT" : "TEMP") : "");
1948  }
1949 
1950  return 0;
1951 }
1952 
1954 {
1955  switch (sl->slice_type) {
1956  case AV_PICTURE_TYPE_P:
1957  return 0;
1958  case AV_PICTURE_TYPE_B:
1959  return 1;
1960  case AV_PICTURE_TYPE_I:
1961  return 2;
1962  case AV_PICTURE_TYPE_SP:
1963  return 3;
1964  case AV_PICTURE_TYPE_SI:
1965  return 4;
1966  default:
1967  return AVERROR_INVALIDDATA;
1968  }
1969 }
1970 
1972  H264SliceContext *sl,
1973  int mb_type, int top_xy,
1974  int left_xy[LEFT_MBS],
1975  int top_type,
1976  int left_type[LEFT_MBS],
1977  int mb_xy, int list)
1978 {
1979  int b_stride = h->b_stride;
1980  int16_t(*mv_dst)[2] = &sl->mv_cache[list][scan8[0]];
1981  int8_t *ref_cache = &sl->ref_cache[list][scan8[0]];
1982  if (IS_INTER(mb_type) || IS_DIRECT(mb_type)) {
1983  if (USES_LIST(top_type, list)) {
1984  const int b_xy = h->mb2b_xy[top_xy] + 3 * b_stride;
1985  const int b8_xy = 4 * top_xy + 2;
1986  int (*ref2frm)[64] = (void*)(sl->ref2frm[h->slice_table[top_xy] & (MAX_SLICES - 1)][0] + (MB_MBAFF(sl) ? 20 : 2));
1987  AV_COPY128(mv_dst - 1 * 8, h->cur_pic.motion_val[list][b_xy + 0]);
1988  ref_cache[0 - 1 * 8] =
1989  ref_cache[1 - 1 * 8] = ref2frm[list][h->cur_pic.ref_index[list][b8_xy + 0]];
1990  ref_cache[2 - 1 * 8] =
1991  ref_cache[3 - 1 * 8] = ref2frm[list][h->cur_pic.ref_index[list][b8_xy + 1]];
1992  } else {
1993  AV_ZERO128(mv_dst - 1 * 8);
1994  AV_WN32A(&ref_cache[0 - 1 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
1995  }
1996 
1997  if (!IS_INTERLACED(mb_type ^ left_type[LTOP])) {
1998  if (USES_LIST(left_type[LTOP], list)) {
1999  const int b_xy = h->mb2b_xy[left_xy[LTOP]] + 3;
2000  const int b8_xy = 4 * left_xy[LTOP] + 1;
2001  int (*ref2frm)[64] =(void*)( sl->ref2frm[h->slice_table[left_xy[LTOP]] & (MAX_SLICES - 1)][0] + (MB_MBAFF(sl) ? 20 : 2));
2002  AV_COPY32(mv_dst - 1 + 0, h->cur_pic.motion_val[list][b_xy + b_stride * 0]);
2003  AV_COPY32(mv_dst - 1 + 8, h->cur_pic.motion_val[list][b_xy + b_stride * 1]);
2004  AV_COPY32(mv_dst - 1 + 16, h->cur_pic.motion_val[list][b_xy + b_stride * 2]);
2005  AV_COPY32(mv_dst - 1 + 24, h->cur_pic.motion_val[list][b_xy + b_stride * 3]);
2006  ref_cache[-1 + 0] =
2007  ref_cache[-1 + 8] = ref2frm[list][h->cur_pic.ref_index[list][b8_xy + 2 * 0]];
2008  ref_cache[-1 + 16] =
2009  ref_cache[-1 + 24] = ref2frm[list][h->cur_pic.ref_index[list][b8_xy + 2 * 1]];
2010  } else {
2011  AV_ZERO32(mv_dst - 1 + 0);
2012  AV_ZERO32(mv_dst - 1 + 8);
2013  AV_ZERO32(mv_dst - 1 + 16);
2014  AV_ZERO32(mv_dst - 1 + 24);
2015  ref_cache[-1 + 0] =
2016  ref_cache[-1 + 8] =
2017  ref_cache[-1 + 16] =
2018  ref_cache[-1 + 24] = LIST_NOT_USED;
2019  }
2020  }
2021  }
2022 
2023  if (!USES_LIST(mb_type, list)) {
2024  fill_rectangle(mv_dst, 4, 4, 8, pack16to32(0, 0), 4);
2025  AV_WN32A(&ref_cache[0 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
2026  AV_WN32A(&ref_cache[1 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
2027  AV_WN32A(&ref_cache[2 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
2028  AV_WN32A(&ref_cache[3 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
2029  return;
2030  }
2031 
2032  {
2033  int8_t *ref = &h->cur_pic.ref_index[list][4 * mb_xy];
2034  int (*ref2frm)[64] = (void*)(sl->ref2frm[sl->slice_num & (MAX_SLICES - 1)][0] + (MB_MBAFF(sl) ? 20 : 2));
2035  uint32_t ref01 = (pack16to32(ref2frm[list][ref[0]], ref2frm[list][ref[1]]) & 0x00FF00FF) * 0x0101;
2036  uint32_t ref23 = (pack16to32(ref2frm[list][ref[2]], ref2frm[list][ref[3]]) & 0x00FF00FF) * 0x0101;
2037  AV_WN32A(&ref_cache[0 * 8], ref01);
2038  AV_WN32A(&ref_cache[1 * 8], ref01);
2039  AV_WN32A(&ref_cache[2 * 8], ref23);
2040  AV_WN32A(&ref_cache[3 * 8], ref23);
2041  }
2042 
2043  {
2044  int16_t(*mv_src)[2] = &h->cur_pic.motion_val[list][4 * sl->mb_x + 4 * sl->mb_y * b_stride];
2045  AV_COPY128(mv_dst + 8 * 0, mv_src + 0 * b_stride);
2046  AV_COPY128(mv_dst + 8 * 1, mv_src + 1 * b_stride);
2047  AV_COPY128(mv_dst + 8 * 2, mv_src + 2 * b_stride);
2048  AV_COPY128(mv_dst + 8 * 3, mv_src + 3 * b_stride);
2049  }
2050 }
2051 
2052 /**
2053  *
2054  * @return non zero if the loop filter can be skipped
2055  */
2056 static int fill_filter_caches(const H264Context *h, H264SliceContext *sl, int mb_type)
2057 {
2058  const int mb_xy = sl->mb_xy;
2059  int top_xy, left_xy[LEFT_MBS];
2060  int top_type, left_type[LEFT_MBS];
2061  uint8_t *nnz;
2062  uint8_t *nnz_cache;
2063 
2064  top_xy = mb_xy - (h->mb_stride << MB_FIELD(sl));
2065 
2066  /* Wow, what a mess, why didn't they simplify the interlacing & intra
2067  * stuff, I can't imagine that these complex rules are worth it. */
2068 
2069  left_xy[LBOT] = left_xy[LTOP] = mb_xy - 1;
2070  if (FRAME_MBAFF(h)) {
2071  const int left_mb_field_flag = IS_INTERLACED(h->cur_pic.mb_type[mb_xy - 1]);
2072  const int curr_mb_field_flag = IS_INTERLACED(mb_type);
2073  if (sl->mb_y & 1) {
2074  if (left_mb_field_flag != curr_mb_field_flag)
2075  left_xy[LTOP] -= h->mb_stride;
2076  } else {
2077  if (curr_mb_field_flag)
2078  top_xy += h->mb_stride &
2079  (((h->cur_pic.mb_type[top_xy] >> 7) & 1) - 1);
2080  if (left_mb_field_flag != curr_mb_field_flag)
2081  left_xy[LBOT] += h->mb_stride;
2082  }
2083  }
2084 
2085  sl->top_mb_xy = top_xy;
2086  sl->left_mb_xy[LTOP] = left_xy[LTOP];
2087  sl->left_mb_xy[LBOT] = left_xy[LBOT];
2088  {
2089  /* For sufficiently low qp, filtering wouldn't do anything.
2090  * This is a conservative estimate: could also check beta_offset
2091  * and more accurate chroma_qp. */
2092  int qp_thresh = sl->qp_thresh; // FIXME strictly we should store qp_thresh for each mb of a slice
2093  int qp = h->cur_pic.qscale_table[mb_xy];
2094  if (qp <= qp_thresh &&
2095  (left_xy[LTOP] < 0 ||
2096  ((qp + h->cur_pic.qscale_table[left_xy[LTOP]] + 1) >> 1) <= qp_thresh) &&
2097  (top_xy < 0 ||
2098  ((qp + h->cur_pic.qscale_table[top_xy] + 1) >> 1) <= qp_thresh)) {
2099  if (!FRAME_MBAFF(h))
2100  return 1;
2101  if ((left_xy[LTOP] < 0 ||
2102  ((qp + h->cur_pic.qscale_table[left_xy[LBOT]] + 1) >> 1) <= qp_thresh) &&
2103  (top_xy < h->mb_stride ||
2104  ((qp + h->cur_pic.qscale_table[top_xy - h->mb_stride] + 1) >> 1) <= qp_thresh))
2105  return 1;
2106  }
2107  }
2108 
2109  top_type = h->cur_pic.mb_type[top_xy];
2110  left_type[LTOP] = h->cur_pic.mb_type[left_xy[LTOP]];
2111  left_type[LBOT] = h->cur_pic.mb_type[left_xy[LBOT]];
2112  if (sl->deblocking_filter == 2) {
2113  if (h->slice_table[top_xy] != sl->slice_num)
2114  top_type = 0;
2115  if (h->slice_table[left_xy[LBOT]] != sl->slice_num)
2116  left_type[LTOP] = left_type[LBOT] = 0;
2117  } else {
2118  if (h->slice_table[top_xy] == 0xFFFF)
2119  top_type = 0;
2120  if (h->slice_table[left_xy[LBOT]] == 0xFFFF)
2121  left_type[LTOP] = left_type[LBOT] = 0;
2122  }
2123  sl->top_type = top_type;
2124  sl->left_type[LTOP] = left_type[LTOP];
2125  sl->left_type[LBOT] = left_type[LBOT];
2126 
2127  if (IS_INTRA(mb_type))
2128  return 0;
2129 
2130  fill_filter_caches_inter(h, sl, mb_type, top_xy, left_xy,
2131  top_type, left_type, mb_xy, 0);
2132  if (sl->list_count == 2)
2133  fill_filter_caches_inter(h, sl, mb_type, top_xy, left_xy,
2134  top_type, left_type, mb_xy, 1);
2135 
2136  nnz = h->non_zero_count[mb_xy];
2137  nnz_cache = sl->non_zero_count_cache;
2138  AV_COPY32(&nnz_cache[4 + 8 * 1], &nnz[0]);
2139  AV_COPY32(&nnz_cache[4 + 8 * 2], &nnz[4]);
2140  AV_COPY32(&nnz_cache[4 + 8 * 3], &nnz[8]);
2141  AV_COPY32(&nnz_cache[4 + 8 * 4], &nnz[12]);
2142  sl->cbp = h->cbp_table[mb_xy];
2143 
2144  if (top_type) {
2145  nnz = h->non_zero_count[top_xy];
2146  AV_COPY32(&nnz_cache[4 + 8 * 0], &nnz[3 * 4]);
2147  }
2148 
2149  if (left_type[LTOP]) {
2150  nnz = h->non_zero_count[left_xy[LTOP]];
2151  nnz_cache[3 + 8 * 1] = nnz[3 + 0 * 4];
2152  nnz_cache[3 + 8 * 2] = nnz[3 + 1 * 4];
2153  nnz_cache[3 + 8 * 3] = nnz[3 + 2 * 4];
2154  nnz_cache[3 + 8 * 4] = nnz[3 + 3 * 4];
2155  }
2156 
2157  /* CAVLC 8x8dct requires NNZ values for residual decoding that differ
2158  * from what the loop filter needs */
2159  if (!CABAC(h) && h->pps.transform_8x8_mode) {
2160  if (IS_8x8DCT(top_type)) {
2161  nnz_cache[4 + 8 * 0] =
2162  nnz_cache[5 + 8 * 0] = (h->cbp_table[top_xy] & 0x4000) >> 12;
2163  nnz_cache[6 + 8 * 0] =
2164  nnz_cache[7 + 8 * 0] = (h->cbp_table[top_xy] & 0x8000) >> 12;
2165  }
2166  if (IS_8x8DCT(left_type[LTOP])) {
2167  nnz_cache[3 + 8 * 1] =
2168  nnz_cache[3 + 8 * 2] = (h->cbp_table[left_xy[LTOP]] & 0x2000) >> 12; // FIXME check MBAFF
2169  }
2170  if (IS_8x8DCT(left_type[LBOT])) {
2171  nnz_cache[3 + 8 * 3] =
2172  nnz_cache[3 + 8 * 4] = (h->cbp_table[left_xy[LBOT]] & 0x8000) >> 12; // FIXME check MBAFF
2173  }
2174 
2175  if (IS_8x8DCT(mb_type)) {
2176  nnz_cache[scan8[0]] =
2177  nnz_cache[scan8[1]] =
2178  nnz_cache[scan8[2]] =
2179  nnz_cache[scan8[3]] = (sl->cbp & 0x1000) >> 12;
2180 
2181  nnz_cache[scan8[0 + 4]] =
2182  nnz_cache[scan8[1 + 4]] =
2183  nnz_cache[scan8[2 + 4]] =
2184  nnz_cache[scan8[3 + 4]] = (sl->cbp & 0x2000) >> 12;
2185 
2186  nnz_cache[scan8[0 + 8]] =
2187  nnz_cache[scan8[1 + 8]] =
2188  nnz_cache[scan8[2 + 8]] =
2189  nnz_cache[scan8[3 + 8]] = (sl->cbp & 0x4000) >> 12;
2190 
2191  nnz_cache[scan8[0 + 12]] =
2192  nnz_cache[scan8[1 + 12]] =
2193  nnz_cache[scan8[2 + 12]] =
2194  nnz_cache[scan8[3 + 12]] = (sl->cbp & 0x8000) >> 12;
2195  }
2196  }
2197 
2198  return 0;
2199 }
2200 
2201 static void loop_filter(const H264Context *h, H264SliceContext *sl, int start_x, int end_x)
2202 {
2203  uint8_t *dest_y, *dest_cb, *dest_cr;
2204  int linesize, uvlinesize, mb_x, mb_y;
2205  const int end_mb_y = sl->mb_y + FRAME_MBAFF(h);
2206  const int old_slice_type = sl->slice_type;
2207  const int pixel_shift = h->pixel_shift;
2208  const int block_h = 16 >> h->chroma_y_shift;
2209 
2210  if (sl->deblocking_filter) {
2211  for (mb_x = start_x; mb_x < end_x; mb_x++)
2212  for (mb_y = end_mb_y - FRAME_MBAFF(h); mb_y <= end_mb_y; mb_y++) {
2213  int mb_xy, mb_type;
2214  mb_xy = sl->mb_xy = mb_x + mb_y * h->mb_stride;
2215  sl->slice_num = h->slice_table[mb_xy];
2216  mb_type = h->cur_pic.mb_type[mb_xy];
2217  sl->list_count = h->list_counts[mb_xy];
2218 
2219  if (FRAME_MBAFF(h))
2220  sl->mb_mbaff =
2221  sl->mb_field_decoding_flag = !!IS_INTERLACED(mb_type);
2222 
2223  sl->mb_x = mb_x;
2224  sl->mb_y = mb_y;
2225  dest_y = h->cur_pic.f->data[0] +
2226  ((mb_x << pixel_shift) + mb_y * sl->linesize) * 16;
2227  dest_cb = h->cur_pic.f->data[1] +
2228  (mb_x << pixel_shift) * (8 << CHROMA444(h)) +
2229  mb_y * sl->uvlinesize * block_h;
2230  dest_cr = h->cur_pic.f->data[2] +
2231  (mb_x << pixel_shift) * (8 << CHROMA444(h)) +
2232  mb_y * sl->uvlinesize * block_h;
2233  // FIXME simplify above
2234 
2235  if (MB_FIELD(sl)) {
2236  linesize = sl->mb_linesize = sl->linesize * 2;
2237  uvlinesize = sl->mb_uvlinesize = sl->uvlinesize * 2;
2238  if (mb_y & 1) { // FIXME move out of this function?
2239  dest_y -= sl->linesize * 15;
2240  dest_cb -= sl->uvlinesize * (block_h - 1);
2241  dest_cr -= sl->uvlinesize * (block_h - 1);
2242  }
2243  } else {
2244  linesize = sl->mb_linesize = sl->linesize;
2245  uvlinesize = sl->mb_uvlinesize = sl->uvlinesize;
2246  }
2247  backup_mb_border(h, sl, dest_y, dest_cb, dest_cr, linesize,
2248  uvlinesize, 0);
2249  if (fill_filter_caches(h, sl, mb_type))
2250  continue;
2251  sl->chroma_qp[0] = get_chroma_qp(h, 0, h->cur_pic.qscale_table[mb_xy]);
2252  sl->chroma_qp[1] = get_chroma_qp(h, 1, h->cur_pic.qscale_table[mb_xy]);
2253 
2254  if (FRAME_MBAFF(h)) {
2255  ff_h264_filter_mb(h, sl, mb_x, mb_y, dest_y, dest_cb, dest_cr,
2256  linesize, uvlinesize);
2257  } else {
2258  ff_h264_filter_mb_fast(h, sl, mb_x, mb_y, dest_y, dest_cb,
2259  dest_cr, linesize, uvlinesize);
2260  }
2261  }
2262  }
2263  sl->slice_type = old_slice_type;
2264  sl->mb_x = end_x;
2265  sl->mb_y = end_mb_y - FRAME_MBAFF(h);
2266  sl->chroma_qp[0] = get_chroma_qp(h, 0, sl->qscale);
2267  sl->chroma_qp[1] = get_chroma_qp(h, 1, sl->qscale);
2268 }
2269 
2271 {
2272  const int mb_xy = sl->mb_x + sl->mb_y * h->mb_stride;
2273  int mb_type = (h->slice_table[mb_xy - 1] == sl->slice_num) ?
2274  h->cur_pic.mb_type[mb_xy - 1] :
2275  (h->slice_table[mb_xy - h->mb_stride] == sl->slice_num) ?
2276  h->cur_pic.mb_type[mb_xy - h->mb_stride] : 0;
2277  sl->mb_mbaff = sl->mb_field_decoding_flag = IS_INTERLACED(mb_type) ? 1 : 0;
2278 }
2279 
2280 /**
2281  * Draw edges and report progress for the last MB row.
2282  */
2284 {
2285  int top = 16 * (sl->mb_y >> FIELD_PICTURE(h));
2286  int pic_height = 16 * h->mb_height >> FIELD_PICTURE(h);
2287  int height = 16 << FRAME_MBAFF(h);
2288  int deblock_border = (16 + 4) << FRAME_MBAFF(h);
2289 
2290  if (sl->deblocking_filter) {
2291  if ((top + height) >= pic_height)
2292  height += deblock_border;
2293  top -= deblock_border;
2294  }
2295 
2296  if (top >= pic_height || (top + height) < 0)
2297  return;
2298 
2299  height = FFMIN(height, pic_height - top);
2300  if (top < 0) {
2301  height = top + height;
2302  top = 0;
2303  }
2304 
2305  ff_h264_draw_horiz_band(h, sl, top, height);
2306 
2307  if (h->droppable || sl->h264->slice_ctx[0].er.error_occurred)
2308  return;
2309 
2310  ff_thread_report_progress(&h->cur_pic_ptr->tf, top + height - 1,
2312 }
2313 
2315  int startx, int starty,
2316  int endx, int endy, int status)
2317 {
2318  if (!sl->h264->enable_er)
2319  return;
2320 
2321  if (CONFIG_ERROR_RESILIENCE) {
2322  ERContext *er = &sl->h264->slice_ctx[0].er;
2323 
2324  ff_er_add_slice(er, startx, starty, endx, endy, status);
2325  }
2326 }
2327 
2328 static int decode_slice(struct AVCodecContext *avctx, void *arg)
2329 {
2330  H264SliceContext *sl = arg;
2331  const H264Context *h = sl->h264;
2332  int lf_x_start = sl->mb_x;
2333  int ret;
2334 
2335  sl->linesize = h->cur_pic_ptr->f->linesize[0];
2336  sl->uvlinesize = h->cur_pic_ptr->f->linesize[1];
2337 
2338  ret = alloc_scratch_buffers(sl, sl->linesize);
2339  if (ret < 0)
2340  return ret;
2341 
2342  sl->mb_skip_run = -1;
2343 
2344  av_assert0(h->block_offset[15] == (4 * ((scan8[15] - scan8[0]) & 7) << h->pixel_shift) + 4 * sl->linesize * ((scan8[15] - scan8[0]) >> 3));
2345 
2346  sl->is_complex = FRAME_MBAFF(h) || h->picture_structure != PICT_FRAME ||
2347  avctx->codec_id != AV_CODEC_ID_H264 ||
2348  (CONFIG_GRAY && (h->flags & AV_CODEC_FLAG_GRAY));
2349 
2351  const int start_i = av_clip(sl->resync_mb_x + sl->resync_mb_y * h->mb_width, 0, h->mb_num - 1);
2352  if (start_i) {
2353  int prev_status = h->slice_ctx[0].er.error_status_table[h->slice_ctx[0].er.mb_index2xy[start_i - 1]];
2354  prev_status &= ~ VP_START;
2355  if (prev_status != (ER_MV_END | ER_DC_END | ER_AC_END))
2356  h->slice_ctx[0].er.error_occurred = 1;
2357  }
2358  }
2359 
2360  if (h->pps.cabac) {
2361  /* realign */
2362  align_get_bits(&sl->gb);
2363 
2364  /* init cabac */
2366  sl->gb.buffer + get_bits_count(&sl->gb) / 8,
2367  (get_bits_left(&sl->gb) + 7) / 8);
2368 
2370 
2371  for (;;) {
2372  // START_TIMER
2373  int ret, eos;
2374  if (sl->mb_x + sl->mb_y * h->mb_width >= sl->next_slice_idx) {
2375  av_log(h->avctx, AV_LOG_ERROR, "Slice overlaps with next at %d\n",
2376  sl->next_slice_idx);
2377  er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y, sl->mb_x,
2378  sl->mb_y, ER_MB_ERROR);
2379  return AVERROR_INVALIDDATA;
2380  }
2381 
2382  ret = ff_h264_decode_mb_cabac(h, sl);
2383  // STOP_TIMER("decode_mb_cabac")
2384 
2385  if (ret >= 0)
2386  ff_h264_hl_decode_mb(h, sl);
2387 
2388  // FIXME optimal? or let mb_decode decode 16x32 ?
2389  if (ret >= 0 && FRAME_MBAFF(h)) {
2390  sl->mb_y++;
2391 
2392  ret = ff_h264_decode_mb_cabac(h, sl);
2393 
2394  if (ret >= 0)
2395  ff_h264_hl_decode_mb(h, sl);
2396  sl->mb_y--;
2397  }
2398  eos = get_cabac_terminate(&sl->cabac);
2399 
2400  if ((h->workaround_bugs & FF_BUG_TRUNCATED) &&
2401  sl->cabac.bytestream > sl->cabac.bytestream_end + 2) {
2402  er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y, sl->mb_x - 1,
2403  sl->mb_y, ER_MB_END);
2404  if (sl->mb_x >= lf_x_start)
2405  loop_filter(h, sl, lf_x_start, sl->mb_x + 1);
2406  return 0;
2407  }
2408  if (sl->cabac.bytestream > sl->cabac.bytestream_end + 2 )
2409  av_log(h->avctx, AV_LOG_DEBUG, "bytestream overread %"PTRDIFF_SPECIFIER"\n", sl->cabac.bytestream_end - sl->cabac.bytestream);
2410  if (ret < 0 || sl->cabac.bytestream > sl->cabac.bytestream_end + 4) {
2412  "error while decoding MB %d %d, bytestream %"PTRDIFF_SPECIFIER"\n",
2413  sl->mb_x, sl->mb_y,
2414  sl->cabac.bytestream_end - sl->cabac.bytestream);
2415  er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y, sl->mb_x,
2416  sl->mb_y, ER_MB_ERROR);
2417  return AVERROR_INVALIDDATA;
2418  }
2419 
2420  if (++sl->mb_x >= h->mb_width) {
2421  loop_filter(h, sl, lf_x_start, sl->mb_x);
2422  sl->mb_x = lf_x_start = 0;
2423  decode_finish_row(h, sl);
2424  ++sl->mb_y;
2425  if (FIELD_OR_MBAFF_PICTURE(h)) {
2426  ++sl->mb_y;
2427  if (FRAME_MBAFF(h) && sl->mb_y < h->mb_height)
2429  }
2430  }
2431 
2432  if (eos || sl->mb_y >= h->mb_height) {
2433  ff_tlog(h->avctx, "slice end %d %d\n",
2434  get_bits_count(&sl->gb), sl->gb.size_in_bits);
2435  er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y, sl->mb_x - 1,
2436  sl->mb_y, ER_MB_END);
2437  if (sl->mb_x > lf_x_start)
2438  loop_filter(h, sl, lf_x_start, sl->mb_x);
2439  return 0;
2440  }
2441  }
2442  } else {
2443  for (;;) {
2444  int ret;
2445 
2446  if (sl->mb_x + sl->mb_y * h->mb_width >= sl->next_slice_idx) {
2447  av_log(h->avctx, AV_LOG_ERROR, "Slice overlaps with next at %d\n",
2448  sl->next_slice_idx);
2449  er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y, sl->mb_x,
2450  sl->mb_y, ER_MB_ERROR);
2451  return AVERROR_INVALIDDATA;
2452  }
2453 
2454  ret = ff_h264_decode_mb_cavlc(h, sl);
2455 
2456  if (ret >= 0)
2457  ff_h264_hl_decode_mb(h, sl);
2458 
2459  // FIXME optimal? or let mb_decode decode 16x32 ?
2460  if (ret >= 0 && FRAME_MBAFF(h)) {
2461  sl->mb_y++;
2462  ret = ff_h264_decode_mb_cavlc(h, sl);
2463 
2464  if (ret >= 0)
2465  ff_h264_hl_decode_mb(h, sl);
2466  sl->mb_y--;
2467  }
2468 
2469  if (ret < 0) {
2471  "error while decoding MB %d %d\n", sl->mb_x, sl->mb_y);
2472  er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y, sl->mb_x,
2473  sl->mb_y, ER_MB_ERROR);
2474  return ret;
2475  }
2476 
2477  if (++sl->mb_x >= h->mb_width) {
2478  loop_filter(h, sl, lf_x_start, sl->mb_x);
2479  sl->mb_x = lf_x_start = 0;
2480  decode_finish_row(h, sl);
2481  ++sl->mb_y;
2482  if (FIELD_OR_MBAFF_PICTURE(h)) {
2483  ++sl->mb_y;
2484  if (FRAME_MBAFF(h) && sl->mb_y < h->mb_height)
2486  }
2487  if (sl->mb_y >= h->mb_height) {
2488  ff_tlog(h->avctx, "slice end %d %d\n",
2489  get_bits_count(&sl->gb), sl->gb.size_in_bits);
2490 
2491  if ( get_bits_left(&sl->gb) == 0
2492  || get_bits_left(&sl->gb) > 0 && !(h->avctx->err_recognition & AV_EF_AGGRESSIVE)) {
2493  er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y,
2494  sl->mb_x - 1, sl->mb_y, ER_MB_END);
2495 
2496  return 0;
2497  } else {
2498  er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y,
2499  sl->mb_x, sl->mb_y, ER_MB_END);
2500 
2501  return AVERROR_INVALIDDATA;
2502  }
2503  }
2504  }
2505 
2506  if (get_bits_left(&sl->gb) <= 0 && sl->mb_skip_run <= 0) {
2507  ff_tlog(h->avctx, "slice end %d %d\n",
2508  get_bits_count(&sl->gb), sl->gb.size_in_bits);
2509 
2510  if (get_bits_left(&sl->gb) == 0) {
2511  er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y,
2512  sl->mb_x - 1, sl->mb_y, ER_MB_END);
2513  if (sl->mb_x > lf_x_start)
2514  loop_filter(h, sl, lf_x_start, sl->mb_x);
2515 
2516  return 0;
2517  } else {
2518  er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y, sl->mb_x,
2519  sl->mb_y, ER_MB_ERROR);
2520 
2521  return AVERROR_INVALIDDATA;
2522  }
2523  }
2524  }
2525  }
2526 }
2527 
2528 /**
2529  * Call decode_slice() for each context.
2530  *
2531  * @param h h264 master context
2532  * @param context_count number of contexts to execute
2533  */
2534 int ff_h264_execute_decode_slices(H264Context *h, unsigned context_count)
2535 {
2536  AVCodecContext *const avctx = h->avctx;
2537  H264SliceContext *sl;
2538  int i, j;
2539 
2540  av_assert0(context_count && h->slice_ctx[context_count - 1].mb_y < h->mb_height);
2541 
2542  h->slice_ctx[0].next_slice_idx = INT_MAX;
2543 
2544  if (h->avctx->hwaccel
2545 #if FF_API_CAP_VDPAU
2547 #endif
2548  )
2549  return 0;
2550  if (context_count == 1) {
2551  int ret;
2552 
2553  h->slice_ctx[0].next_slice_idx = h->mb_width * h->mb_height;
2554 
2555  ret = decode_slice(avctx, &h->slice_ctx[0]);
2556  h->mb_y = h->slice_ctx[0].mb_y;
2557  return ret;
2558  } else {
2559  av_assert0(context_count > 0);
2560  for (i = 0; i < context_count; i++) {
2561  int next_slice_idx = h->mb_width * h->mb_height;
2562  int slice_idx;
2563 
2564  sl = &h->slice_ctx[i];
2565  if (CONFIG_ERROR_RESILIENCE) {
2566  sl->er.error_count = 0;
2567  }
2568 
2569  /* make sure none of those slices overlap */
2570  slice_idx = sl->mb_y * h->mb_width + sl->mb_x;
2571  for (j = 0; j < context_count; j++) {
2572  H264SliceContext *sl2 = &h->slice_ctx[j];
2573  int slice_idx2 = sl2->mb_y * h->mb_width + sl2->mb_x;
2574 
2575  if (i == j || slice_idx2 < slice_idx)
2576  continue;
2577  next_slice_idx = FFMIN(next_slice_idx, slice_idx2);
2578  }
2579  sl->next_slice_idx = next_slice_idx;
2580  }
2581 
2582  avctx->execute(avctx, decode_slice, h->slice_ctx,
2583  NULL, context_count, sizeof(h->slice_ctx[0]));
2584 
2585  /* pull back stuff from slices to master context */
2586  sl = &h->slice_ctx[context_count - 1];
2587  h->mb_y = sl->mb_y;
2588  if (CONFIG_ERROR_RESILIENCE) {
2589  for (i = 1; i < context_count; i++)
2591  }
2592  }
2593 
2594  return 0;
2595 }
int chroma_format_idc
Definition: h264.h:178
int video_signal_type_present_flag
Definition: h264.h:203
struct H264Context * h264
Definition: h264.h:363
int last_slice_type
Definition: h264.h:712
#define ff_tlog(ctx,...)
Definition: internal.h:54
void ff_h264_unref_picture(H264Context *h, H264Picture *pic)
Definition: h264_picture.c:47
#define NULL
Definition: coverity.c:32
int ff_thread_can_start_frame(AVCodecContext *avctx)
const struct AVCodec * codec
Definition: avcodec.h:1511
AVRational framerate
Definition: avcodec.h:3302
discard all frames except keyframes
Definition: avcodec.h:688
void ff_h264_flush_change(H264Context *h)
Definition: h264.c:1084
static const uint8_t dequant8_coeff_init[6][6]
Definition: h264_slice.c:141
int workaround_bugs
Definition: h264.h:554
int long_ref
1->long term reference 0->short term reference
Definition: h264.h:335
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
int single_decode_warning
1 if the single thread fallback warning has already been displayed, 0 otherwise.
Definition: h264.h:708
int sei_recovery_frame_cnt
Definition: h264.h:344
enum AVPixelFormat backup_pix_fmt
Definition: h264.h:546
static int shift(int a, int b)
Definition: sonic.c:82
int low_delay
Definition: h264.h:550
int mb_num
Definition: h264.h:621
int ff_h264_decode_ref_pic_list_reordering(H264Context *h, H264SliceContext *sl)
Definition: h264_refs.c:236
int mb_aff_frame
Definition: h264.h:589
int16_t mv_cache[2][5 *8][2]
Motion vector cache.
Definition: h264.h:486
int delta_poc[2]
Definition: h264.h:649
#define AV_PIX_FMT_YUV444P14
Definition: pixfmt.h:390
static int get_se_golomb(GetBitContext *gb)
read signed exp golomb code.
Definition: golomb.h:183
#define CHROMA444(h)
Definition: h264.h:99
#define LEFT_MBS
Definition: h264.h:75
int edge_emu_buffer_allocated
Definition: h264.h:474
int coded_width
Bitstream width / height, may be different from width/height e.g.
Definition: avcodec.h:1696
static void decode_finish_row(const H264Context *h, H264SliceContext *sl)
Draw edges and report progress for the last MB row.
Definition: h264_slice.c:2283
const char * fmt
Definition: avisynth_c.h:632
#define H264_MAX_PICTURE_COUNT
Definition: h264.h:46
int first_field
Definition: h264.h:591
int ff_h264_decode_mb_cavlc(const H264Context *h, H264SliceContext *sl)
Decode a macroblock.
Definition: h264_cavlc.c:709
uint8_t field_scan8x8_q0[64]
Definition: h264.h:613
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:68
misc image utilities
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:260
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
#define ER_MB_END
AVFrame * f
Definition: thread.h:36
int weighted_bipred_idc
Definition: h264.h:245
int left_mb_xy[LEFT_MBS]
Definition: h264.h:406
int chroma_qp_index_offset[2]
Definition: h264.h:248
const uint8_t * bytestream_end
Definition: cabac.h:54
AVBufferRef * buf[AV_NUM_DATA_POINTERS]
AVBuffer references backing the data for this frame.
Definition: frame.h:441
hardware decoding through Videotoolbox
Definition: pixfmt.h:326
H264ChromaContext h264chroma
Definition: h264.h:522
uint16_t * cbp_table
Definition: h264.h:596
MMCO mmco[MAX_MMCO_COUNT]
memory management control operations buffer.
Definition: h264.h:678
static void implicit_weight_table(const H264Context *h, H264SliceContext *sl, int field)
Initialize implicit_weight table.
Definition: h264_slice.c:770
#define MAX_PPS_COUNT
Definition: h264.h:50
Sequence parameter set.
Definition: h264.h:174
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: avcodec.h:2237
int mb_y
Definition: h264.h:618
int coded_picture_number
Definition: h264.h:549
int bitstream_restriction_flag
Definition: h264.h:214
planar GBR 4:4:4 24bpp
Definition: pixfmt.h:188
int num
numerator
Definition: rational.h:44
AVBufferRef * mb_type_buf
Definition: h264.h:319
int bipred_scratchpad_allocated
Definition: h264.h:473
#define DELAYED_PIC_REF
Value of Picture.reference when Picture is not a reference picture, but is held for delayed output...
Definition: diracdec.c:74
void ff_h264_direct_ref_list_init(const H264Context *const h, H264SliceContext *sl)
Definition: h264_direct.c:110
#define AV_PIX_FMT_GBRP10
Definition: pixfmt.h:396
#define VP_START
< current MB is the first after a resync marker
AVBufferPool * mb_type_pool
Definition: h264.h:831
int chroma_x_shift
Definition: h264.h:538
qpel_mc_func(* qpel_put)[16]
Definition: h264.h:836
void ff_h264_filter_mb_fast(const H264Context *h, H264SliceContext *sl, int mb_x, int mb_y, uint8_t *img_y, uint8_t *img_cb, uint8_t *img_cr, unsigned int linesize, unsigned int uvlinesize)
#define AV_EF_AGGRESSIVE
consider things that a sane encoder should not do as an error
Definition: avcodec.h:2906
const uint8_t * buffer
Definition: get_bits.h:55
Picture parameter set.
Definition: h264.h:237
int crop
Definition: h264.h:346
void ff_h264_draw_horiz_band(const H264Context *h, H264SliceContext *sl, int y, int height)
Definition: h264.c:98
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown) That is the width of a pixel divided by the height of the pixel...
Definition: avcodec.h:1902
int16_t(*[2] motion_val)[2]
Definition: h264.h:317
int flags
Definition: h264.h:553
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1722
int frame_mbs_only_flag
Definition: h264.h:191
int mb_height
Definition: h264.h:619
H264Picture * delayed_pic[MAX_DELAYED_PIC_COUNT+2]
Definition: h264.h:670
static int av_cmp_q(AVRational a, AVRational b)
Compare two rationals.
Definition: rational.h:66
int is_avc
Used to parse AVC variant of h264.
Definition: h264.h:632
int mmco_index
Definition: h264.h:679
av_cold void ff_h264_pred_init(H264PredContext *h, int codec_id, const int bit_depth, int chroma_format_idc)
Set the intra prediction function pointers.
Definition: h264pred.c:411
AVBufferPool * ref_index_pool
Definition: h264.h:833
static const uint8_t dequant4_coeff_init[6][3]
Definition: h264_slice.c:128
void ff_h264_free_tables(H264Context *h)
Definition: h264.c:355
uint8_t zigzag_scan8x8_cavlc[64]
Definition: h264.h:605
#define AV_PIX_FMT_YUV420P12
Definition: pixfmt.h:384
av_cold void ff_h264chroma_init(H264ChromaContext *c, int bit_depth)
Definition: h264chroma.c:41
ERPicture last_pic
int ff_h264_get_profile(SPS *sps)
Compute profile from profile_idc and constraint_set?_flags.
Definition: h264.c:1218
int next_slice_idx
Definition: h264.h:430
static const uint8_t zigzag_scan8x8_cavlc[64+1]
Definition: h264_slice.c:109
uint32_t dequant8_buffer[6][QP_MAX_NUM+1][64]
Definition: h264.h:582
H264Context.
Definition: h264.h:517
discard all non intra frames
Definition: avcodec.h:687
discard all
Definition: avcodec.h:689
AVFrame * f
Definition: h264.h:310
int prev_poc_msb
poc_msb of the last reference pic for POC type 0
Definition: h264.h:651
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:3003
uint32_t num_units_in_tick
Definition: h264.h:210
void ff_h264_filter_mb(const H264Context *h, H264SliceContext *sl, int mb_x, int mb_y, uint8_t *img_y, uint8_t *img_cb, uint8_t *img_cr, unsigned int linesize, unsigned int uvlinesize)
static const uint8_t field_scan[16+1]
Definition: h264_slice.c:63
H264Picture * long_ref[32]
Definition: h264.h:669
int profile
profile
Definition: avcodec.h:3115
int picture_structure
Definition: h264.h:590
order of coefficients is actually GBR, also IEC 61966-2-1 (sRGB)
Definition: pixfmt.h:518
#define AV_WN32A(p, v)
Definition: intreadwrite.h:538
#define AV_COPY32(d, s)
Definition: intreadwrite.h:586
int luma_weight_flag[2]
7.4.3.2 luma_weight_lX_flag
Definition: h264.h:387
unsigned current_sps_id
id of the current SPS
Definition: h264.h:575
unsigned int ref_count[2]
num_ref_idx_l0/1_active_minus1 + 1
Definition: h264.h:460
#define IN_RANGE(a, b, size)
Definition: h264_slice.c:386
int ff_set_ref_count(H264Context *h, H264SliceContext *sl)
Definition: h264.c:1238
#define FFALIGN(x, a)
Definition: common.h:86
#define REBASE_PICTURE(pic, new_ctx, old_ctx)
Definition: h264_slice.c:388
int ff_h264_update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
Definition: h264_slice.c:433
Switching Intra.
Definition: avutil.h:270
int setup_finished
Definition: h264.h:814
enum AVDiscard skip_frame
Skip decoding for selected frames.
Definition: avcodec.h:3226
static const uint8_t golomb_to_pict_type[5]
Definition: h264data.h:37
struct AVHWAccel * hwaccel
Hardware accelerator in use.
Definition: avcodec.h:2922
unsigned int crop_top
frame_cropping_rect_top_offset
Definition: h264.h:199
#define USES_LIST(a, list)
Definition: mpegutils.h:95
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
int is_complex
Definition: h264.h:432
#define IS_8x8DCT(a)
Definition: h264.h:106
uint8_t scaling_matrix4[6][16]
Definition: h264.h:253
const uint8_t * bytestream
Definition: cabac.h:53
int deblocking_filter_parameters_present
deblocking_filter_parameters_present_flag
Definition: h264.h:249
#define MB_FIELD(sl)
Definition: h264.h:72
void void avpriv_request_sample(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
static int copy_parameter_set(void **to, void **from, int count, int size)
Definition: h264_slice.c:407
uint32_t(*[6] dequant4_coeff)[16]
Definition: h264.h:583
static enum AVPixelFormat non_j_pixfmt(enum AVPixelFormat a)
Definition: h264_slice.c:1132
uint8_t
int prev_frame_num_offset
for POC type 2
Definition: h264.h:654
#define av_malloc(s)
int full_range
Definition: h264.h:204
unsigned int crop_left
frame_cropping_rect_left_offset
Definition: h264.h:197
int gaps_in_frame_num_allowed_flag
Definition: h264.h:188
int slice_alpha_c0_offset
Definition: h264.h:379
int poc
Definition: h264.h:356
int field_picture
whether or not picture was encoded in separate fields
Definition: h264.h:339
int bit_depth_chroma
bit_depth_chroma_minus8 + 8
Definition: h264.h:228
static void init_dequant4_coeff_table(H264Context *h)
Definition: h264_slice.c:341
enum AVColorPrimaries color_primaries
Definition: h264.h:206
int poc
frame POC
Definition: h264.h:329
void ff_h264_direct_dist_scale_factor(const H264Context *const h, H264SliceContext *sl)
Definition: h264_direct.c:51
int slice_type
Definition: h264.h:368
Multithreading support functions.
#define ER_MB_ERROR
int cabac
entropy_coding_mode_flag
Definition: h264.h:239
#define AV_CODEC_FLAG_LOW_DELAY
Force low delay.
Definition: avcodec.h:780
static const uint8_t dequant8_coeff_init_scan[16]
Definition: h264_slice.c:137
static void init_dequant8_coeff_table(H264Context *h)
Definition: h264_slice.c:314
const char * from
Definition: jacosubdec.c:65
unsigned int crop_right
frame_cropping_rect_right_offset
Definition: h264.h:198
unsigned int last_ref_count[2]
Definition: h264.h:713
#define AV_PIX_FMT_GBRP9
Definition: pixfmt.h:395
uint8_t(*[2] top_borders)[(16 *3)*2]
Definition: h264.h:472
int invalid_gap
Definition: h264.h:343
#define FF_API_CAP_VDPAU
Definition: version.h:95
ERPicture cur_pic
int frame_recovered
Initial frame has been completely recovered.
Definition: h264.h:804
int height
Definition: h264.h:537
#define PICT_BOTTOM_FIELD
Definition: mpegutils.h:34
int transform_bypass
qpprime_y_zero_transform_bypass_flag
Definition: h264.h:179
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:212
char av_get_picture_type_char(enum AVPictureType pict_type)
Return a single letter to describe the given picture type pict_type.
Definition: utils.c:91
#define AV_CODEC_CAP_HWACCEL_VDPAU
Codec can export data for HW decoding (VDPAU).
Definition: avcodec.h:893
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV422P and setting col...
Definition: pixfmt.h:76
#define ER_MV_END
int ff_set_sar(AVCodecContext *avctx, AVRational sar)
Check that the provided sample aspect ratio is valid and set it on the codec context.
Definition: utils.c:231
qpel_mc_func(* qpel_avg)[16]
Definition: h264.h:837
int redundant_pic_cnt_present
redundant_pic_cnt_present_flag
Definition: h264.h:251
int chroma_y_shift
Definition: h264.h:538
#define MAX_DELAYED_PIC_COUNT
Definition: h264.h:54
static void fill_rectangle(SDL_Surface *screen, int x, int y, int w, int h, int color, int update)
Definition: ffplay.c:785
ptrdiff_t size
Definition: opengl_enc.c:101
AVBufferRef * qscale_table_buf
Definition: h264.h:313
#define AV_PIX_FMT_YUV422P12
Definition: pixfmt.h:385
H264Picture * parent
Definition: h264.h:359
int ff_h264_decode_mb_cabac(const H264Context *h, H264SliceContext *sl)
Decode a CABAC coded macroblock.
Definition: h264_cabac.c:1911
#define AV_CODEC_FLAG_GRAY
Only decode/encode grayscale.
Definition: avcodec.h:763
high precision timer, useful to profile code
int recovered
picture at IDR or recovery point + recovery count
Definition: h264.h:342
#define AV_COPY64(d, s)
Definition: intreadwrite.h:590
int ff_h264_fill_default_ref_list(H264Context *h, H264SliceContext *sl)
Fill the default_ref_list.
Definition: h264_refs.c:125
int chroma_qp[2]
Definition: h264.h:373
qpel_mc_func avg_h264_qpel_pixels_tab[4][16]
Definition: h264qpel.h:29
#define av_log(a,...)
AVCodecContext * owner
Definition: thread.h:37
int resync_mb_x
Definition: h264.h:427
int last_pocs[MAX_DELAYED_PIC_COUNT]
Definition: h264.h:671
const char * to
Definition: webvttdec.c:34
int width
Definition: h264.h:537
static int h264_frame_start(H264Context *h)
Definition: h264_slice.c:571
H.264 / AVC / MPEG4 part10 codec.
int slice_num
Definition: h264.h:367
#define U(x)
Definition: vp56_arith.h:37
int frame_num
Definition: h264.h:650
#define HWACCEL_MAX
void ff_h264_hl_decode_mb(const H264Context *h, H264SliceContext *sl)
Definition: h264_mb.c:818
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:588
H264Picture DPB[H264_MAX_PICTURE_COUNT]
Definition: h264.h:526
int mb_aff
mb_adaptive_frame_field_flag
Definition: h264.h:192
enum AVColorTransferCharacteristic color_trc
Definition: h264.h:207
H264PredContext hpc
Definition: h264.h:557
int width
width and height of the video frame
Definition: frame.h:220
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
int has_b_frames
Size of the frame reordering buffer in the decoder.
Definition: avcodec.h:1812
#define td
Definition: regdef.h:70
static int get_ue_golomb(GetBitContext *gb)
read unsigned exp golomb code.
Definition: golomb.h:53
static int alloc_scratch_buffers(H264SliceContext *sl, int linesize)
Definition: h264_slice.c:164
int poc_type
pic_order_cnt_type
Definition: h264.h:181
void ff_er_add_slice(ERContext *s, int startx, int starty, int endx, int endy, int status)
Add a slice.
int context_initialized
Definition: h264.h:552
#define PTRDIFF_SPECIFIER
Definition: internal.h:249
#define AV_EF_EXPLODE
abort decoding on minor error detection
Definition: avcodec.h:2901
ERContext er
Definition: h264.h:365
int nal_unit_type
Definition: h264.h:627
int num_reorder_frames
Definition: h264.h:215
discard all bidirectional frames
Definition: avcodec.h:686
#define AVERROR(e)
Definition: error.h:43
void * hwaccel_picture_private
hardware accelerator private data
Definition: h264.h:323
int backup_height
Definition: h264.h:545
int av_pix_fmt_get_chroma_sub_sample(enum AVPixelFormat pix_fmt, int *h_shift, int *v_shift)
Utility function to access log2_chroma_w log2_chroma_h from the pixel format AVPixFmtDescriptor.
Definition: pixdesc.c:2157
int active_thread_type
Which multithreading methods are in use by the codec.
Definition: avcodec.h:3052
static const uint8_t field_scan8x8[64+1]
Definition: h264_slice.c:70
int ff_init_poc(H264Context *h, int pic_field_poc[2], int *pic_poc)
Definition: h264.c:1133
int backup_width
Backup frame properties: needed, because they can be different between returned frame and last decode...
Definition: h264.h:544
int capabilities
Codec capabilities.
Definition: avcodec.h:3491
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
int ff_pred_weight_table(H264Context *h, H264SliceContext *sl)
Definition: h264.c:996
PPS pps
current pps
Definition: h264.h:577
static int init_dimensions(H264Context *h)
Definition: h264_slice.c:981
#define FF_BUG_TRUNCATED
Definition: avcodec.h:2806
const char * arg
Definition: jacosubdec.c:66
int deblocking_filter
disable_deblocking_filter_idc with 1 <-> 0
Definition: h264.h:378
uint8_t(*[2] mvd_table)[2]
Definition: h264.h:600
int prev_interlaced_frame
Complement sei_pic_struct SEI_PIC_STRUCT_TOP_BOTTOM and SEI_PIC_STRUCT_BOTTOM_TOP indicate interlaced...
Definition: h264.h:727
static int fill_filter_caches(const H264Context *h, H264SliceContext *sl, int mb_type)
Definition: h264_slice.c:2056
ThreadFrame tf
Definition: h264.h:311
simple assert() macros that are a bit more flexible than ISO C assert().
int weighted_pred
weighted_pred_flag
Definition: h264.h:244
#define PICT_TOP_FIELD
Definition: mpegutils.h:33
H264QpelContext h264qpel
Definition: h264.h:523
#define AV_PIX_FMT_YUV444P10
Definition: pixfmt.h:383
int direct_spatial_mv_pred
Definition: h264.h:444
#define CABAC(h)
Definition: h264.h:94
HW acceleration through VDA, data[3] contains a CVPixelBufferRef.
Definition: pixfmt.h:244
int frame_num
frame_num (raw frame_num from slice header)
Definition: h264.h:330
static av_always_inline uint32_t pack16to32(unsigned a, unsigned b)
Definition: h264.h:1023
#define MAX_SLICES
Definition: dxva2_hevc.c:28
GLsizei count
Definition: opengl_enc.c:109
uint8_t * list_counts
Array of list_count per MB specifying the slice type.
Definition: h264.h:593
int ff_h264_get_slice_type(const H264SliceContext *sl)
Reconstruct bitstream slice_type.
Definition: h264_slice.c:1953
Libavcodec external API header.
#define fail()
Definition: checkasm.h:57
qpel_mc_func put_h264_qpel_pixels_tab[4][16]
Definition: h264qpel.h:28
int delta_pic_order_always_zero_flag
Definition: h264.h:183
void av_image_copy(uint8_t *dst_data[4], int dst_linesizes[4], const uint8_t *src_data[4], const int src_linesizes[4], enum AVPixelFormat pix_fmt, int width, int height)
Copy image in src_data to dst_data.
Definition: imgutils.c:288
int new
flag to keep track if the decoder context needs re-init due to changed SPS
Definition: h264.h:231
int * mb_index2xy
#define FIELD_OR_MBAFF_PICTURE(h)
Definition: h264.h:91
int slice_type_nos
S free slice type (SI/SP are remapped to I/P)
Definition: h264.h:369
int mb_skip_run
Definition: h264.h:431
uint8_t zigzag_scan8x8[64]
Definition: h264.h:604
AVBufferRef * hwaccel_priv_buf
Definition: h264.h:322
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:67
static const uint8_t scan8[16 *3+3]
Definition: h264.h:1007
int crop_left
Definition: h264.h:347
int crop
frame_cropping_flag
Definition: h264.h:194
uint8_t * error_status_table
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
Reduce a fraction.
Definition: rational.c:35
int use_weight
Definition: h264.h:383
uint8_t scaling_matrix8[6][64]
Definition: h264.h:254
int nal_length_size
Number of bytes used for nal length (1, 2 or 4)
Definition: h264.h:633
useful rectangle filling function
#define AV_PIX_FMT_YUV422P9
Definition: pixfmt.h:378
int refs
number of reference frames
Definition: avcodec.h:2178
av_cold void ff_videodsp_init(VideoDSPContext *ctx, int bpc)
Definition: videodsp.c:38
AVBufferRef * motion_val_buf[2]
Definition: h264.h:316
int ref_frame_count
num_ref_frames
Definition: h264.h:187
enum AVPixelFormat * pix_fmts
array of supported pixel formats, or NULL if unknown, array is terminated by -1
Definition: avcodec.h:3493
int frame_num_offset
for POC type 2
Definition: h264.h:653
int err_recognition
Error recognition; may misdetect some more or less valid parts as errors.
Definition: avcodec.h:2890
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:53
int x264_build
Definition: h264.h:616
#define FFMIN(a, b)
Definition: common.h:81
uint16_t * slice_table
slice_table_base + 2*mb_stride + 1
Definition: h264.h:586
static void copy_picture_range(H264Picture **to, H264Picture **from, int count, H264Context *new_base, H264Context *old_base)
Definition: h264_slice.c:393
av_cold void ff_h264dsp_init(H264DSPContext *c, const int bit_depth, const int chroma_format_idc)
Definition: h264dsp.c:67
uint8_t field_scan8x8_cavlc[64]
Definition: h264.h:608
#define H264_MAX_THREADS
Definition: h264.h:47
#define IS_DIRECT(a)
Definition: mpegutils.h:80
CABACContext cabac
Cabac.
Definition: h264.h:505
int colour_description_present_flag
Definition: h264.h:205
int reference
Definition: h264.h:341
static void er_add_slice(H264SliceContext *sl, int startx, int starty, int endx, int endy, int status)
Definition: h264_slice.c:2314
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV420P and setting col...
Definition: pixfmt.h:75
AVRational sar
Definition: h264.h:202
#define FIELD_PICTURE(h)
Definition: h264.h:74
int width
picture width / height.
Definition: avcodec.h:1681
int redundant_pic_count
Definition: h264.h:437
int nb_slice_ctx
Definition: h264.h:532
int long_ref_count
number of actual long term references
Definition: h264.h:682
#define ER_DC_END
#define FF_CEIL_RSHIFT(a, b)
Definition: common.h:57
uint32_t * mb_type
Definition: h264.h:320
int ff_h264_ref_picture(H264Context *h, H264Picture *dst, H264Picture *src)
Definition: h264_picture.c:68
int top_mb_xy
Definition: h264.h:404
void ff_thread_report_progress(ThreadFrame *f, int n, int field)
Notify later decoding threads when part of their reference picture is ready.
int size_in_bits
Definition: get_bits.h:57
SPS sps
current sps
Definition: h264.h:576
PPS * pps_buffers[MAX_PPS_COUNT]
Definition: h264.h:639
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
Definition: avcodec.h:2216
#define MAX_SPS_COUNT
Definition: h264.h:49
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:68
Context Adaptive Binary Arithmetic Coder inline functions.
int level
level
Definition: avcodec.h:3204
int init_qp
pic_init_qp_minus26 + 26
Definition: h264.h:246
H264SliceContext * slice_ctx
Definition: h264.h:531
int direct_8x8_inference_flag
Definition: h264.h:193
int poc_lsb
Definition: h264.h:646
int reference
Definition: h264.h:355
int ff_h264_decode_slice_header(H264Context *h, H264SliceContext *sl)
Decode a slice header.
Definition: h264_slice.c:1151
int max_pic_num
max_frame_num or 2 * max_frame_num for field pics.
Definition: h264.h:665
int ticks_per_frame
For some codecs, the time base is closer to the field rate than the frame rate.
Definition: avcodec.h:1640
int top_borders_allocated[2]
Definition: h264.h:475
int ff_generate_sliding_window_mmcos(H264Context *h, int first_slice)
Definition: h264_refs.c:584
static int pic_is_unused(H264Context *h, H264Picture *pic)
Definition: h264_slice.c:292
int curr_pic_num
frame_num for frames or 2 * frame_num + 1 for field pics.
Definition: h264.h:660
int chroma_log2_weight_denom
Definition: h264.h:386
static void init_scan_tables(H264Context *h)
initialize scan tables
Definition: h264_slice.c:834
static int av_unused get_cabac_terminate(CABACContext *c)
#define MB_MBAFF(h)
Definition: h264.h:71
#define AV_PIX_FMT_YUV444P9
Definition: pixfmt.h:379
#define AV_PIX_FMT_GBRP14
Definition: pixfmt.h:398
uint32_t dequant4_buffer[6][QP_MAX_NUM+1][16]
Definition: h264.h:581
void ff_h264_init_cabac_states(const H264Context *h, H264SliceContext *sl)
Definition: h264_cabac.c:1264
unsigned int sps_id
Definition: h264.h:175
#define FF_ARRAY_ELEMS(a)
int thread_count
thread count is used to decide how many independent tasks should be passed to execute() ...
Definition: avcodec.h:3033
the normal 2^n-1 "JPEG" YUV ranges
Definition: pixfmt.h:540
static int init_table_pools(H264Context *h)
Definition: h264_slice.c:196
uint8_t * edge_emu_buffer
Definition: h264.h:471
int top_type
Definition: h264.h:409
int dequant_coeff_pps
reinit tables when pps changes
Definition: h264.h:641
static unsigned get_ue_golomb_long(GetBitContext *gb)
Read an unsigned Exp-Golomb code in the range 0 to UINT32_MAX-1.
Definition: golomb.h:85
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
int pic_order_present
pic_order_present_flag
Definition: h264.h:240
SPS * sps_buffers[MAX_SPS_COUNT]
Definition: h264.h:638
uint8_t zigzag_scan_q0[16]
Definition: h264.h:609
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:232
int bit_depth_luma
luma bit depth from sps to detect changes
Definition: h264.h:635
int chroma_format_idc
chroma format from sps to detect changes
Definition: h264.h:636
VideoDSPContext vdsp
Definition: h264.h:520
int timing_info_present_flag
Definition: h264.h:209
int ff_h264_execute_ref_pic_marking(H264Context *h, MMCO *mmco, int mmco_count)
Execute the reference picture marking (memory management control operations).
Definition: h264_refs.c:616
int coded_picture_number
picture number in bitstream order
Definition: frame.h:274
int ff_h264_slice_context_init(H264Context *h, H264SliceContext *sl)
Init context Allocate buffers which are not shared amongst multiple threads.
Definition: h264.c:459
int mb_stride
Definition: h264.h:620
#define AV_LOG_INFO
Standard information.
Definition: log.h:187
#define IS_INTERLACED(a)
Definition: mpegutils.h:79
AVCodecContext * avctx
Definition: h264.h:519
uint8_t zigzag_scan8x8_q0[64]
Definition: h264.h:610
#define AV_PIX_FMT_YUV420P14
Definition: pixfmt.h:388
AVS_Value src
Definition: avisynth_c.h:482
H264 / AVC / MPEG4 part10 codec data table
int last_qscale_diff
Definition: h264.h:375
#define FF_THREAD_SLICE
Decode more than one part of a single frame at once.
Definition: avcodec.h:3045
enum AVCodecID codec_id
Definition: avcodec.h:1519
static int get_ue_golomb_31(GetBitContext *gb)
read unsigned exp golomb code, constraint to a max of 31.
Definition: golomb.h:100
static const uint8_t zigzag_scan[16+1]
Definition: h264data.h:54
int prev_frame_num
frame_num of the last pic for POC type 1/2
Definition: h264.h:655
ERPicture next_pic
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:199
H264Picture * short_ref[32]
Definition: h264.h:668
#define LTOP
Definition: h264.h:76
void avpriv_color_frame(AVFrame *frame, const int color[4])
Definition: utils.c:690
int poc_msb
Definition: h264.h:647
#define AV_CODEC_FLAG2_FAST
Definition: avcodec.h:803
int field_poc[2]
top/bottom POC
Definition: h264.h:328
int debug
debug
Definition: avcodec.h:2842
int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
Wrapper around get_buffer() for frame-multithreaded codecs.
int max_contexts
Max number of threads / contexts.
Definition: h264.h:700
int recovery_frame
recovery_frame is the frame_num at which the next frame should be fully constructed.
Definition: h264.h:791
main external API structure.
Definition: avcodec.h:1502
int qp_thresh
QP threshold to skip loopfilter.
Definition: h264.h:374
uint8_t * data
The data buffer.
Definition: buffer.h:89
int ff_h264_alloc_tables(H264Context *h)
Allocate tables.
Definition: h264.c:398
#define QP_MAX_NUM
Definition: h264.h:108
void * buf
Definition: avisynth_c.h:553
int resync_mb_y
Definition: h264.h:428
int8_t * qscale_table
Definition: h264.h:314
AVBufferRef * av_buffer_allocz(int size)
Same as av_buffer_alloc(), except the returned buffer will be initialized to zero.
Definition: buffer.c:82
AVBuffer * buffer
Definition: buffer.h:82
static const uint8_t field_scan8x8_cavlc[64+1]
Definition: h264_slice.c:89
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:304
BYTE int const BYTE int int int height
Definition: avisynth_c.h:676
#define AV_PIX_FMT_YUV420P10
Definition: pixfmt.h:380
#define FF_THREAD_FRAME
Decode more than one frame at once.
Definition: avcodec.h:3044
int coded_height
Definition: avcodec.h:1696
Switching Predicted.
Definition: avutil.h:271
uint8_t non_zero_count_cache[15 *8]
non zero coeff count cache.
Definition: h264.h:481
#define CHROMA422(h)
Definition: h264.h:98
uint32_t(*[6] dequant8_coeff)[64]
Definition: h264.h:584
void av_buffer_pool_uninit(AVBufferPool **ppool)
Mark the pool as being available for freeing.
Definition: buffer.c:250
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:2230
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
Definition: avcodec.h:2223
HW acceleration through VDPAU, Picture.data[3] contains a VdpVideoSurface.
Definition: pixfmt.h:223
int8_t * ref_index[2]
Definition: h264.h:326
int use_weight_chroma
Definition: h264.h:384
A reference counted buffer type.
int pixel_shift
0 for 8-bit H264, 1 for high-bit-depth H264
Definition: h264.h:534
int mmco_reset
MMCO_RESET set this 1.
Definition: h264.h:331
static const uint8_t rem6[QP_MAX_NUM+1]
Definition: h264_slice.c:47
int ff_h264_decode_ref_pic_marking(H264Context *h, GetBitContext *gb, int first_slice)
Definition: h264_refs.c:825
H264Picture * cur_pic_ptr
Definition: h264.h:527
const uint8_t ff_zigzag_direct[64]
Definition: mathtables.c:98
ptrdiff_t mb_uvlinesize
Definition: h264.h:423
static int h264_slice_header_init(H264Context *h)
Definition: h264_slice.c:1023
int mb_mbaff
mb_aff_frame && mb_field_decoding_flag
Definition: h264.h:435
enum AVPixelFormat ff_thread_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
Wrapper around get_format() for frame-multithreaded codecs.
int enable_er
Definition: h264.h:828
#define AV_PIX_FMT_YUV420P9
Definition: pixfmt.h:377
#define IS_INTER(a)
Definition: mpegutils.h:75
unsigned int sps_id
Definition: h264.h:238
#define TRANSPOSE(x)
int log2_max_poc_lsb
log2_max_pic_order_cnt_lsb_minus4
Definition: h264.h:182
ptrdiff_t mb_linesize
may be equal to s->linesize or s->linesize * 2, for mbaff
Definition: h264.h:422
HW decoding through DXVA2, Picture.data[3] contains a LPDIRECT3DSURFACE9 pointer. ...
Definition: pixfmt.h:149
int16_t slice_row[MAX_SLICES]
to detect when MAX_SLICES is too low
Definition: h264.h:822
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:209
static int alloc_picture(H264Context *h, H264Picture *pic)
Definition: h264_slice.c:223
ptrdiff_t linesize
Definition: h264.h:421
int block_offset[2 *(16 *3)]
block_offset[ 0..23] for frame macroblocks block_offset[24..47] for field macroblocks ...
Definition: h264.h:568
uint32_t time_scale
Definition: h264.h:211
#define AV_PIX_FMT_YUV422P14
Definition: pixfmt.h:389
#define AV_PIX_FMT_GBRP12
Definition: pixfmt.h:397
int transform_8x8_mode
transform_8x8_mode_flag
Definition: h264.h:252
ptrdiff_t uvlinesize
Definition: h264.h:421
#define AV_PIX_FMT_YUV422P10
Definition: pixfmt.h:381
unsigned int list_count
Definition: h264.h:461
uint8_t zigzag_scan[16]
Definition: h264.h:603
#define AV_PIX_FMT_YUV444P12
Definition: pixfmt.h:387
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:182
#define FF_DEBUG_PICT_INFO
Definition: avcodec.h:2843
static void release_unused_pictures(H264Context *h, int remove_current)
Definition: h264_slice.c:151
#define LBOT
Definition: h264.h:77
#define AV_ZERO128(d)
Definition: intreadwrite.h:622
the normal 219*2^(n-8) "MPEG" YUV ranges
Definition: pixfmt.h:539
int left_type[LEFT_MBS]
Definition: h264.h:411
#define copy_fields(to, from, start_field, end_field)
Definition: h264_slice.c:427
hardware decoding through VDA
Definition: pixfmt.h:180
discard all non reference
Definition: avcodec.h:685
AVBufferPool * qscale_table_pool
Definition: h264.h:830
H264Picture * next_output_pic
Definition: h264.h:672
volatile int error_count
int slice_context_count
Definition: h264.h:702
int mb_height
pic_height_in_map_units_minus1 + 1
Definition: h264.h:190
AVBufferPool * motion_val_pool
Definition: h264.h:832
void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
Allocate a buffer, reusing the given one if large enough.
Definition: mem.c:499
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:63
#define SLICE_SINGLETHREAD
Definition: h264.h:1222
common internal api header.
if(ret< 0)
Definition: vf_mcdeint.c:280
AVBufferPool * av_buffer_pool_init(int size, AVBufferRef *(*alloc)(int size))
Allocate and initialize a buffer pool.
Definition: buffer.c:217
#define AV_COPY128(d, s)
Definition: intreadwrite.h:594
static enum AVPixelFormat get_pixel_format(H264Context *h, int force_callback)
Definition: h264_slice.c:868
int log2_max_frame_num
log2_max_frame_num_minus4 + 4
Definition: h264.h:180
int missing_fields
Definition: h264.h:808
static double c[64]
H.264 / AVC / MPEG4 part10 motion vector predicion.
void ff_h264_fill_mbaff_ref_list(H264Context *h, H264SliceContext *sl)
Definition: h264_refs.c:367
Bi-dir predicted.
Definition: avutil.h:268
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV444P and setting col...
Definition: pixfmt.h:77
int implicit_weight[48][48][2]
Definition: h264.h:392
int cur_chroma_format_idc
Definition: h264.h:820
int8_t * intra4x4_pred_mode
Definition: h264.h:401
enum AVDiscard skip_loop_filter
Skip loop filtering for selected frames.
Definition: avcodec.h:3212
int den
denominator
Definition: rational.h:45
static void predict_field_decoding_flag(const H264Context *h, H264SliceContext *sl)
Definition: h264_slice.c:2270
int ref2frm[MAX_SLICES][2][64]
reference to frame number lists, used in the loop filter, the first 2 are for -2,-1 ...
Definition: h264.h:465
int bit_depth_luma
bit_depth_luma_minus8 + 8
Definition: h264.h:227
static av_always_inline int get_chroma_qp(const H264Context *h, int t, int qscale)
Get the chroma qp.
Definition: h264.h:1044
static const uint8_t div6[QP_MAX_NUM+1]
Definition: h264_slice.c:55
#define IS_INTRA(x, y)
void * priv_data
Definition: avcodec.h:1544
int ff_h264_field_end(H264Context *h, H264SliceContext *sl, int in_setup)
Definition: h264_picture.c:154
#define PICT_FRAME
Definition: mpegutils.h:35
int prev_poc_lsb
poc_lsb of the last reference pic for POC type 0
Definition: h264.h:652
int ff_h264_execute_decode_slices(H264Context *h, unsigned context_count)
Call decode_slice() for each context.
Definition: h264_slice.c:2534
static av_always_inline void backup_mb_border(const H264Context *h, H264SliceContext *sl, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr, int linesize, int uvlinesize, int simple)
Definition: h264_slice.c:668
void ff_h264_set_erpic(ERPicture *dst, H264Picture *src)
Definition: h264_picture.c:131
uint8_t zigzag_scan8x8_cavlc_q0[64]
Definition: h264.h:611
int8_t ref_cache[2][5 *8]
Definition: h264.h:487
void ff_init_cabac_decoder(CABACContext *c, const uint8_t *buf, int buf_size)
Definition: cabac.c:54
int(* execute)(struct AVCodecContext *c, int(*func)(struct AVCodecContext *c2, void *arg), void *arg2, int *ret, int count, int size)
The codec may call this to execute several independent things.
Definition: avcodec.h:3073
#define SLICE_SKIPED
Definition: h264.h:1223
#define FRAME_MBAFF(h)
Definition: h264.h:73
int frame_priv_data_size
Size of per-frame hardware accelerator private data.
Definition: avcodec.h:3674
int luma_log2_weight_denom
Definition: h264.h:385
H264Picture cur_pic
Definition: h264.h:528
void ff_h264_init_dequant_tables(H264Context *h)
Definition: h264_slice.c:367
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:237
#define AV_ZERO32(d)
Definition: intreadwrite.h:614
int mb_width
Definition: h264.h:619
enum AVPictureType pict_type
Definition: h264.h:710
static int find_unused_picture(H264Context *h)
Definition: h264_slice.c:299
int current_slice
current slice number, used to initialize slice_num of each thread/context
Definition: h264.h:692
static const uint8_t * align_get_bits(GetBitContext *s)
Definition: get_bits.h:449
int mb_width
pic_width_in_mbs_minus1 + 1
Definition: h264.h:189
int flags2
AV_CODEC_FLAG2_*.
Definition: avcodec.h:1604
uint32_t * mb2b_xy
Definition: h264.h:570
H264Ref ref_list[2][48]
0..15: frame refs, 16..47: mbaff field refs.
Definition: h264.h:462
uint8_t field_scan8x8_cavlc_q0[64]
Definition: h264.h:614
int cur_bit_depth_luma
Definition: h264.h:821
AVBufferRef * ref_index_buf[2]
Definition: h264.h:325
int delta_poc_bottom
Definition: h264.h:648
int au_pps_id
pps_id of current access unit
Definition: h264.h:579
H264DSPContext h264dsp
Definition: h264.h:521
void ff_er_frame_start(ERContext *s)
int height
Definition: frame.h:220
int crop_top
Definition: h264.h:348
uint8_t(*[2] mvd_table)[2]
Definition: h264.h:500
uint8_t field_scan8x8[64]
Definition: h264.h:607
int slice_type_fixed
Definition: h264.h:370
static av_always_inline void fill_filter_caches_inter(const H264Context *h, H264SliceContext *sl, int mb_type, int top_xy, int left_xy[LEFT_MBS], int top_type, int left_type[LEFT_MBS], int mb_xy, int list)
Definition: h264_slice.c:1971
#define av_freep(p)
#define av_always_inline
Definition: attributes.h:37
HW decoding through Direct3D11, Picture.data[3] contains a ID3D11VideoDecoderOutputView pointer...
Definition: pixfmt.h:268
int slice_beta_offset
Definition: h264.h:380
int8_t * intra4x4_pred_mode
Definition: h264.h:556
AVBufferRef * av_buffer_pool_get(AVBufferPool *pool)
Allocate a new AVBuffer, reusing an old buffer from the pool when available.
Definition: buffer.c:355
#define ER_AC_END
static int decode_slice(struct AVCodecContext *avctx, void *arg)
Definition: h264_slice.c:2328
const char * av_get_pix_fmt_name(enum AVPixelFormat pix_fmt)
Return the short name for a pixel format, NULL in case pix_fmt is unknown.
Definition: pixdesc.c:2050
uint8_t field_scan_q0[16]
Definition: h264.h:612
#define LIST_NOT_USED
Definition: h264.h:561
int mb_field_decoding_flag
Definition: h264.h:434
uint8_t(* non_zero_count)[48]
Definition: h264.h:559
static void loop_filter(const H264Context *h, H264SliceContext *sl, int start_x, int end_x)
Definition: h264_slice.c:2201
unsigned int crop_bottom
frame_cropping_rect_bottom_offset
Definition: h264.h:200
exp golomb vlc stuff
uint8_t * bipred_scratchpad
Definition: h264.h:470
AVPixelFormat
Pixel format.
Definition: pixfmt.h:61
av_cold void ff_h264qpel_init(H264QpelContext *c, int bit_depth)
Definition: h264qpel.c:49
int sei_recovery_frame_cnt
recovery_frame_cnt from SEI message
Definition: h264.h:776
#define AV_GET_BUFFER_FLAG_REF
The decoder will keep a reference to the frame and may reuse it later.
Definition: avcodec.h:1216
int droppable
Definition: h264.h:548
int level_idc
Definition: h264.h:177
int nal_ref_idc
Definition: h264.h:626
GetBitContext gb
Definition: h264.h:364
uint8_t field_scan[16]
Definition: h264.h:606
int cabac_init_idc
Definition: h264.h:507
for(j=16;j >0;--j)
#define FFMAX3(a, b, c)
Definition: common.h:80
int b_stride
Definition: h264.h:572
Predicted.
Definition: avutil.h:267
#define tb
Definition: regdef.h:68
Context Adaptive Binary Arithmetic Coder.
int chroma_weight_flag[2]
7.4.3.2 chroma_weight_lX_flag
Definition: h264.h:388
int short_ref_count
number of actual short term references
Definition: h264.h:683
static int width
enum AVColorSpace colorspace
Definition: h264.h:208