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