Libav
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 Libav.
6  *
7  * Libav is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * Libav is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
28 #include "libavutil/avassert.h"
29 #include "libavutil/display.h"
30 #include "libavutil/imgutils.h"
31 #include "libavutil/stereo3d.h"
32 #include "libavutil/timer.h"
33 #include "internal.h"
34 #include "cabac.h"
35 #include "cabac_functions.h"
36 #include "error_resilience.h"
37 #include "avcodec.h"
38 #include "h264.h"
39 #include "h264dec.h"
40 #include "h264data.h"
41 #include "h264chroma.h"
42 #include "h264_mvpred.h"
43 #include "h264_ps.h"
44 #include "golomb.h"
45 #include "mathops.h"
46 #include "mpegutils.h"
47 #include "rectangle.h"
48 #include "thread.h"
49 
50 static const uint8_t field_scan[16] = {
51  0 + 0 * 4, 0 + 1 * 4, 1 + 0 * 4, 0 + 2 * 4,
52  0 + 3 * 4, 1 + 1 * 4, 1 + 2 * 4, 1 + 3 * 4,
53  2 + 0 * 4, 2 + 1 * 4, 2 + 2 * 4, 2 + 3 * 4,
54  3 + 0 * 4, 3 + 1 * 4, 3 + 2 * 4, 3 + 3 * 4,
55 };
56 
57 static const uint8_t field_scan8x8[64] = {
58  0 + 0 * 8, 0 + 1 * 8, 0 + 2 * 8, 1 + 0 * 8,
59  1 + 1 * 8, 0 + 3 * 8, 0 + 4 * 8, 1 + 2 * 8,
60  2 + 0 * 8, 1 + 3 * 8, 0 + 5 * 8, 0 + 6 * 8,
61  0 + 7 * 8, 1 + 4 * 8, 2 + 1 * 8, 3 + 0 * 8,
62  2 + 2 * 8, 1 + 5 * 8, 1 + 6 * 8, 1 + 7 * 8,
63  2 + 3 * 8, 3 + 1 * 8, 4 + 0 * 8, 3 + 2 * 8,
64  2 + 4 * 8, 2 + 5 * 8, 2 + 6 * 8, 2 + 7 * 8,
65  3 + 3 * 8, 4 + 1 * 8, 5 + 0 * 8, 4 + 2 * 8,
66  3 + 4 * 8, 3 + 5 * 8, 3 + 6 * 8, 3 + 7 * 8,
67  4 + 3 * 8, 5 + 1 * 8, 6 + 0 * 8, 5 + 2 * 8,
68  4 + 4 * 8, 4 + 5 * 8, 4 + 6 * 8, 4 + 7 * 8,
69  5 + 3 * 8, 6 + 1 * 8, 6 + 2 * 8, 5 + 4 * 8,
70  5 + 5 * 8, 5 + 6 * 8, 5 + 7 * 8, 6 + 3 * 8,
71  7 + 0 * 8, 7 + 1 * 8, 6 + 4 * 8, 6 + 5 * 8,
72  6 + 6 * 8, 6 + 7 * 8, 7 + 2 * 8, 7 + 3 * 8,
73  7 + 4 * 8, 7 + 5 * 8, 7 + 6 * 8, 7 + 7 * 8,
74 };
75 
76 static const uint8_t field_scan8x8_cavlc[64] = {
77  0 + 0 * 8, 1 + 1 * 8, 2 + 0 * 8, 0 + 7 * 8,
78  2 + 2 * 8, 2 + 3 * 8, 2 + 4 * 8, 3 + 3 * 8,
79  3 + 4 * 8, 4 + 3 * 8, 4 + 4 * 8, 5 + 3 * 8,
80  5 + 5 * 8, 7 + 0 * 8, 6 + 6 * 8, 7 + 4 * 8,
81  0 + 1 * 8, 0 + 3 * 8, 1 + 3 * 8, 1 + 4 * 8,
82  1 + 5 * 8, 3 + 1 * 8, 2 + 5 * 8, 4 + 1 * 8,
83  3 + 5 * 8, 5 + 1 * 8, 4 + 5 * 8, 6 + 1 * 8,
84  5 + 6 * 8, 7 + 1 * 8, 6 + 7 * 8, 7 + 5 * 8,
85  0 + 2 * 8, 0 + 4 * 8, 0 + 5 * 8, 2 + 1 * 8,
86  1 + 6 * 8, 4 + 0 * 8, 2 + 6 * 8, 5 + 0 * 8,
87  3 + 6 * 8, 6 + 0 * 8, 4 + 6 * 8, 6 + 2 * 8,
88  5 + 7 * 8, 6 + 4 * 8, 7 + 2 * 8, 7 + 6 * 8,
89  1 + 0 * 8, 1 + 2 * 8, 0 + 6 * 8, 3 + 0 * 8,
90  1 + 7 * 8, 3 + 2 * 8, 2 + 7 * 8, 4 + 2 * 8,
91  3 + 7 * 8, 5 + 2 * 8, 4 + 7 * 8, 5 + 4 * 8,
92  6 + 3 * 8, 6 + 5 * 8, 7 + 3 * 8, 7 + 7 * 8,
93 };
94 
95 // zigzag_scan8x8_cavlc[i] = zigzag_scan8x8[(i/4) + 16*(i%4)]
96 static const uint8_t zigzag_scan8x8_cavlc[64] = {
97  0 + 0 * 8, 1 + 1 * 8, 1 + 2 * 8, 2 + 2 * 8,
98  4 + 1 * 8, 0 + 5 * 8, 3 + 3 * 8, 7 + 0 * 8,
99  3 + 4 * 8, 1 + 7 * 8, 5 + 3 * 8, 6 + 3 * 8,
100  2 + 7 * 8, 6 + 4 * 8, 5 + 6 * 8, 7 + 5 * 8,
101  1 + 0 * 8, 2 + 0 * 8, 0 + 3 * 8, 3 + 1 * 8,
102  3 + 2 * 8, 0 + 6 * 8, 4 + 2 * 8, 6 + 1 * 8,
103  2 + 5 * 8, 2 + 6 * 8, 6 + 2 * 8, 5 + 4 * 8,
104  3 + 7 * 8, 7 + 3 * 8, 4 + 7 * 8, 7 + 6 * 8,
105  0 + 1 * 8, 3 + 0 * 8, 0 + 4 * 8, 4 + 0 * 8,
106  2 + 3 * 8, 1 + 5 * 8, 5 + 1 * 8, 5 + 2 * 8,
107  1 + 6 * 8, 3 + 5 * 8, 7 + 1 * 8, 4 + 5 * 8,
108  4 + 6 * 8, 7 + 4 * 8, 5 + 7 * 8, 6 + 7 * 8,
109  0 + 2 * 8, 2 + 1 * 8, 1 + 3 * 8, 5 + 0 * 8,
110  1 + 4 * 8, 2 + 4 * 8, 6 + 0 * 8, 4 + 3 * 8,
111  0 + 7 * 8, 4 + 4 * 8, 7 + 2 * 8, 3 + 6 * 8,
112  5 + 5 * 8, 6 + 5 * 8, 6 + 6 * 8, 7 + 7 * 8,
113 };
114 
115 static void release_unused_pictures(H264Context *h, int remove_current)
116 {
117  int i;
118 
119  /* release non reference frames */
120  for (i = 0; i < H264_MAX_PICTURE_COUNT; i++) {
121  if (h->DPB[i].f->buf[0] && !h->DPB[i].reference &&
122  (remove_current || &h->DPB[i] != h->cur_pic_ptr)) {
123  ff_h264_unref_picture(h, &h->DPB[i]);
124  }
125  }
126 }
127 
128 static int alloc_scratch_buffers(H264SliceContext *sl, int linesize)
129 {
130  const H264Context *h = sl->h264;
131  int alloc_size = FFALIGN(FFABS(linesize) + 32, 32);
132 
133  av_fast_malloc(&sl->bipred_scratchpad, &sl->bipred_scratchpad_allocated, 16 * 6 * alloc_size);
134  // edge emu needs blocksize + filter length - 1
135  // (= 21x21 for H.264)
136  av_fast_malloc(&sl->edge_emu_buffer, &sl->edge_emu_buffer_allocated, alloc_size * 2 * 21);
137 
139  h->mb_width * 16 * 3 * sizeof(uint8_t) * 2);
141  h->mb_width * 16 * 3 * sizeof(uint8_t) * 2);
142 
143  if (!sl->bipred_scratchpad || !sl->edge_emu_buffer ||
144  !sl->top_borders[0] || !sl->top_borders[1]) {
147  av_freep(&sl->top_borders[0]);
148  av_freep(&sl->top_borders[1]);
149 
152  sl->top_borders_allocated[0] = 0;
153  sl->top_borders_allocated[1] = 0;
154  return AVERROR(ENOMEM);
155  }
156 
157  return 0;
158 }
159 
161 {
162  const int big_mb_num = h->mb_stride * (h->mb_height + 1) + 1;
163  const int mb_array_size = h->mb_stride * h->mb_height;
164  const int b4_stride = h->mb_width * 4 + 1;
165  const int b4_array_size = b4_stride * h->mb_height * 4;
166 
167  h->qscale_table_pool = av_buffer_pool_init(big_mb_num + h->mb_stride,
169  h->mb_type_pool = av_buffer_pool_init((big_mb_num + h->mb_stride) *
170  sizeof(uint32_t), av_buffer_allocz);
171  h->motion_val_pool = av_buffer_pool_init(2 * (b4_array_size + 4) *
172  sizeof(int16_t), av_buffer_allocz);
173  h->ref_index_pool = av_buffer_pool_init(4 * mb_array_size, av_buffer_allocz);
174 
175  if (!h->qscale_table_pool || !h->mb_type_pool || !h->motion_val_pool ||
176  !h->ref_index_pool) {
181  return AVERROR(ENOMEM);
182  }
183 
184  return 0;
185 }
186 
188 {
189  int i, ret = 0;
190 
191  av_assert0(!pic->f->data[0]);
192 
193  pic->tf.f = pic->f;
194  ret = ff_thread_get_buffer(h->avctx, &pic->tf, pic->reference ?
196  if (ret < 0)
197  goto fail;
198 
199  if (h->avctx->hwaccel) {
200  const AVHWAccel *hwaccel = h->avctx->hwaccel;
202  if (hwaccel->frame_priv_data_size) {
204  if (!pic->hwaccel_priv_buf)
205  return AVERROR(ENOMEM);
207  }
208  }
209 
210  if (!h->qscale_table_pool) {
211  ret = init_table_pools(h);
212  if (ret < 0)
213  goto fail;
214  }
215 
218  if (!pic->qscale_table_buf || !pic->mb_type_buf)
219  goto fail;
220 
221  pic->mb_type = (uint32_t*)pic->mb_type_buf->data + 2 * h->mb_stride + 1;
222  pic->qscale_table = pic->qscale_table_buf->data + 2 * h->mb_stride + 1;
223 
224  for (i = 0; i < 2; i++) {
227  if (!pic->motion_val_buf[i] || !pic->ref_index_buf[i])
228  goto fail;
229 
230  pic->motion_val[i] = (int16_t (*)[2])pic->motion_val_buf[i]->data + 4;
231  pic->ref_index[i] = pic->ref_index_buf[i]->data;
232  }
233 
234  return 0;
235 fail:
236  ff_h264_unref_picture(h, pic);
237  return (ret < 0) ? ret : AVERROR(ENOMEM);
238 }
239 
240 static inline int pic_is_unused(H264Context *h, H264Picture *pic)
241 {
242  if (!pic->f->buf[0])
243  return 1;
244  return 0;
245 }
246 
248 {
249  int i;
250 
251  for (i = 0; i < H264_MAX_PICTURE_COUNT; i++) {
252  if (pic_is_unused(h, &h->DPB[i]))
253  break;
254  }
255  if (i == H264_MAX_PICTURE_COUNT)
256  return AVERROR_INVALIDDATA;
257 
258  return i;
259 }
260 
262 {
263  H264Picture *cur;
264  int ret;
265 
268  h->cur_pic_ptr = NULL;
269 
270  ret = find_unused_picture(h);
271  if (ret < 0) {
272  av_log(h->avctx, AV_LOG_ERROR, "no frame buffer available\n");
273  return ret;
274  }
275  cur = &h->DPB[ret];
276 
277  ret = alloc_picture(h, cur);
278  if (ret < 0)
279  return ret;
280 
281  ret = ff_h264_ref_picture(h, &h->cur_pic, cur);
282  if (ret < 0)
283  return ret;
284  h->cur_pic_ptr = cur;
285 
286  return 0;
287 }
288 
289 #define IN_RANGE(a, b, size) (((a) >= (b)) && ((a) < ((b) + (size))))
290 
291 #define REBASE_PICTURE(pic, new_ctx, old_ctx) \
292  ((pic && pic >= old_ctx->DPB && \
293  pic < old_ctx->DPB + H264_MAX_PICTURE_COUNT) ? \
294  &new_ctx->DPB[pic - old_ctx->DPB] : NULL)
295 
296 static void copy_picture_range(H264Picture **to, H264Picture **from, int count,
297  H264Context *new_base,
298  H264Context *old_base)
299 {
300  int i;
301 
302  for (i = 0; i < count; i++) {
303  assert((IN_RANGE(from[i], old_base, sizeof(*old_base)) ||
304  IN_RANGE(from[i], old_base->DPB,
305  sizeof(H264Picture) * H264_MAX_PICTURE_COUNT) ||
306  !from[i]));
307  to[i] = REBASE_PICTURE(from[i], new_base, old_base);
308  }
309 }
310 
311 static int h264_slice_header_init(H264Context *h);
312 
314  const AVCodecContext *src)
315 {
316  H264Context *h = dst->priv_data, *h1 = src->priv_data;
317  int inited = h->context_initialized, err = 0;
318  int need_reinit = 0;
319  int i, ret;
320 
321  if (dst == src || !h1->context_initialized)
322  return 0;
323 
324  if (!h1->ps.sps)
325  return AVERROR_INVALIDDATA;
326 
327  if (inited &&
328  (h->width != h1->width ||
329  h->height != h1->height ||
330  h->mb_width != h1->mb_width ||
331  h->mb_height != h1->mb_height ||
332  !h->ps.sps ||
333  h->ps.sps->bit_depth_luma != h1->ps.sps->bit_depth_luma ||
334  h->ps.sps->chroma_format_idc != h1->ps.sps->chroma_format_idc ||
335  h->ps.sps->colorspace != h1->ps.sps->colorspace)) {
336  need_reinit = 1;
337  }
338 
339  // SPS/PPS
340  for (i = 0; i < FF_ARRAY_ELEMS(h->ps.sps_list); i++) {
341  av_buffer_unref(&h->ps.sps_list[i]);
342  if (h1->ps.sps_list[i]) {
343  h->ps.sps_list[i] = av_buffer_ref(h1->ps.sps_list[i]);
344  if (!h->ps.sps_list[i])
345  return AVERROR(ENOMEM);
346  }
347  }
348  for (i = 0; i < FF_ARRAY_ELEMS(h->ps.pps_list); i++) {
349  av_buffer_unref(&h->ps.pps_list[i]);
350  if (h1->ps.pps_list[i]) {
351  h->ps.pps_list[i] = av_buffer_ref(h1->ps.pps_list[i]);
352  if (!h->ps.pps_list[i])
353  return AVERROR(ENOMEM);
354  }
355  }
356 
357  h->ps.sps = h1->ps.sps;
358 
359  if (need_reinit || !inited) {
360  h->width = h1->width;
361  h->height = h1->height;
362  h->mb_height = h1->mb_height;
363  h->mb_width = h1->mb_width;
364  h->mb_num = h1->mb_num;
365  h->mb_stride = h1->mb_stride;
366  h->b_stride = h1->b_stride;
367 
368  if ((err = h264_slice_header_init(h)) < 0) {
369  av_log(h->avctx, AV_LOG_ERROR, "h264_slice_header_init() failed");
370  return err;
371  }
372 
373  /* copy block_offset since frame_start may not be called */
374  memcpy(h->block_offset, h1->block_offset, sizeof(h->block_offset));
375  }
376 
377  h->avctx->coded_height = h1->avctx->coded_height;
378  h->avctx->coded_width = h1->avctx->coded_width;
379  h->avctx->width = h1->avctx->width;
380  h->avctx->height = h1->avctx->height;
381  h->coded_picture_number = h1->coded_picture_number;
382  h->first_field = h1->first_field;
383  h->picture_structure = h1->picture_structure;
384  h->mb_aff_frame = h1->mb_aff_frame;
385  h->droppable = h1->droppable;
386 
387  for (i = 0; i < H264_MAX_PICTURE_COUNT; i++) {
388  ff_h264_unref_picture(h, &h->DPB[i]);
389  if (h1->DPB[i].f->buf[0] &&
390  (ret = ff_h264_ref_picture(h, &h->DPB[i], &h1->DPB[i])) < 0)
391  return ret;
392  }
393 
394  h->cur_pic_ptr = REBASE_PICTURE(h1->cur_pic_ptr, h, h1);
396  if (h1->cur_pic.f->buf[0]) {
397  ret = ff_h264_ref_picture(h, &h->cur_pic, &h1->cur_pic);
398  if (ret < 0)
399  return ret;
400  }
401 
402  h->enable_er = h1->enable_er;
403  h->workaround_bugs = h1->workaround_bugs;
404  h->x264_build = h1->x264_build;
405  h->droppable = h1->droppable;
406 
407  // extradata/NAL handling
408  h->is_avc = h1->is_avc;
409  h->nal_length_size = h1->nal_length_size;
410 
411  memcpy(&h->poc, &h1->poc, sizeof(h->poc));
412 
413  memcpy(h->short_ref, h1->short_ref, sizeof(h->short_ref));
414  memcpy(h->long_ref, h1->long_ref, sizeof(h->long_ref));
415  memcpy(h->delayed_pic, h1->delayed_pic, sizeof(h->delayed_pic));
416  memcpy(h->last_pocs, h1->last_pocs, sizeof(h->last_pocs));
417 
418  h->next_outputed_poc = h1->next_outputed_poc;
419 
420  memcpy(h->mmco, h1->mmco, sizeof(h->mmco));
421  h->nb_mmco = h1->nb_mmco;
422  h->mmco_reset = h1->mmco_reset;
423  h->explicit_ref_marking = h1->explicit_ref_marking;
424  h->long_ref_count = h1->long_ref_count;
425  h->short_ref_count = h1->short_ref_count;
426 
427  copy_picture_range(h->short_ref, h1->short_ref, 32, h, h1);
428  copy_picture_range(h->long_ref, h1->long_ref, 32, h, h1);
429  copy_picture_range(h->delayed_pic, h1->delayed_pic,
430  MAX_DELAYED_PIC_COUNT + 2, h, h1);
431 
432  if (!h->cur_pic_ptr)
433  return 0;
434 
435  if (!h->droppable) {
437  h->poc.prev_poc_msb = h->poc.poc_msb;
438  h->poc.prev_poc_lsb = h->poc.poc_lsb;
439  }
442 
443  h->recovery_frame = h1->recovery_frame;
444  h->frame_recovered = h1->frame_recovered;
445 
446  return err;
447 }
448 
450 {
451  H264Picture *pic;
452  int i, ret;
453  const int pixel_shift = h->pixel_shift;
454 
455  ret = initialize_cur_frame(h);
456  if (ret < 0)
457  return ret;
458 
459  pic = h->cur_pic_ptr;
460  pic->reference = h->droppable ? 0 : h->picture_structure;
463  pic->frame_num = h->poc.frame_num;
464  /*
465  * Zero key_frame here; IDR markings per slice in frame or fields are ORed
466  * in later.
467  * See decode_nal_units().
468  */
469  pic->f->key_frame = 0;
470  pic->mmco_reset = 0;
471  pic->recovered = 0;
472 
473  pic->f->pict_type = h->slice_ctx[0].slice_type;
474 
477 
478  for (i = 0; i < 16; i++) {
479  h->block_offset[i] = (4 * ((scan8[i] - scan8[0]) & 7) << pixel_shift) + 4 * pic->f->linesize[0] * ((scan8[i] - scan8[0]) >> 3);
480  h->block_offset[48 + i] = (4 * ((scan8[i] - scan8[0]) & 7) << pixel_shift) + 8 * pic->f->linesize[0] * ((scan8[i] - scan8[0]) >> 3);
481  }
482  for (i = 0; i < 16; i++) {
483  h->block_offset[16 + i] =
484  h->block_offset[32 + i] = (4 * ((scan8[i] - scan8[0]) & 7) << pixel_shift) + 4 * pic->f->linesize[1] * ((scan8[i] - scan8[0]) >> 3);
485  h->block_offset[48 + 16 + i] =
486  h->block_offset[48 + 32 + i] = (4 * ((scan8[i] - scan8[0]) & 7) << pixel_shift) + 8 * pic->f->linesize[1] * ((scan8[i] - scan8[0]) >> 3);
487  }
488 
489  /* Some macroblocks can be accessed before they're available in case
490  * of lost slices, MBAFF or threading. */
491  memset(h->slice_table, -1,
492  (h->mb_height * h->mb_stride - 1) * sizeof(*h->slice_table));
493 
494  /* We mark the current picture as non-reference after allocating it, so
495  * that if we break out due to an error it can be released automatically
496  * in the next ff_mpv_frame_start().
497  */
498  h->cur_pic_ptr->reference = 0;
499 
500  h->cur_pic_ptr->field_poc[0] = h->cur_pic_ptr->field_poc[1] = INT_MAX;
501 
502  h->postpone_filter = 0;
503 
505 
506  if (h->sei.unregistered.x264_build >= 0)
508 
509  assert(h->cur_pic_ptr->long_ref == 0);
510 
511  return 0;
512 }
513 
515  uint8_t *src_y,
516  uint8_t *src_cb, uint8_t *src_cr,
517  int linesize, int uvlinesize,
518  int simple)
519 {
520  uint8_t *top_border;
521  int top_idx = 1;
522  const int pixel_shift = h->pixel_shift;
523  int chroma444 = CHROMA444(h);
524  int chroma422 = CHROMA422(h);
525 
526  src_y -= linesize;
527  src_cb -= uvlinesize;
528  src_cr -= uvlinesize;
529 
530  if (!simple && FRAME_MBAFF(h)) {
531  if (sl->mb_y & 1) {
532  if (!MB_MBAFF(sl)) {
533  top_border = sl->top_borders[0][sl->mb_x];
534  AV_COPY128(top_border, src_y + 15 * linesize);
535  if (pixel_shift)
536  AV_COPY128(top_border + 16, src_y + 15 * linesize + 16);
537  if (simple || !CONFIG_GRAY || !(h->flags & AV_CODEC_FLAG_GRAY)) {
538  if (chroma444) {
539  if (pixel_shift) {
540  AV_COPY128(top_border + 32, src_cb + 15 * uvlinesize);
541  AV_COPY128(top_border + 48, src_cb + 15 * uvlinesize + 16);
542  AV_COPY128(top_border + 64, src_cr + 15 * uvlinesize);
543  AV_COPY128(top_border + 80, src_cr + 15 * uvlinesize + 16);
544  } else {
545  AV_COPY128(top_border + 16, src_cb + 15 * uvlinesize);
546  AV_COPY128(top_border + 32, src_cr + 15 * uvlinesize);
547  }
548  } else if (chroma422) {
549  if (pixel_shift) {
550  AV_COPY128(top_border + 32, src_cb + 15 * uvlinesize);
551  AV_COPY128(top_border + 48, src_cr + 15 * uvlinesize);
552  } else {
553  AV_COPY64(top_border + 16, src_cb + 15 * uvlinesize);
554  AV_COPY64(top_border + 24, src_cr + 15 * uvlinesize);
555  }
556  } else {
557  if (pixel_shift) {
558  AV_COPY128(top_border + 32, src_cb + 7 * uvlinesize);
559  AV_COPY128(top_border + 48, src_cr + 7 * uvlinesize);
560  } else {
561  AV_COPY64(top_border + 16, src_cb + 7 * uvlinesize);
562  AV_COPY64(top_border + 24, src_cr + 7 * uvlinesize);
563  }
564  }
565  }
566  }
567  } else if (MB_MBAFF(sl)) {
568  top_idx = 0;
569  } else
570  return;
571  }
572 
573  top_border = sl->top_borders[top_idx][sl->mb_x];
574  /* There are two lines saved, the line above the top macroblock
575  * of a pair, and the line above the bottom macroblock. */
576  AV_COPY128(top_border, src_y + 16 * linesize);
577  if (pixel_shift)
578  AV_COPY128(top_border + 16, src_y + 16 * linesize + 16);
579 
580  if (simple || !CONFIG_GRAY || !(h->flags & AV_CODEC_FLAG_GRAY)) {
581  if (chroma444) {
582  if (pixel_shift) {
583  AV_COPY128(top_border + 32, src_cb + 16 * linesize);
584  AV_COPY128(top_border + 48, src_cb + 16 * linesize + 16);
585  AV_COPY128(top_border + 64, src_cr + 16 * linesize);
586  AV_COPY128(top_border + 80, src_cr + 16 * linesize + 16);
587  } else {
588  AV_COPY128(top_border + 16, src_cb + 16 * linesize);
589  AV_COPY128(top_border + 32, src_cr + 16 * linesize);
590  }
591  } else if (chroma422) {
592  if (pixel_shift) {
593  AV_COPY128(top_border + 32, src_cb + 16 * uvlinesize);
594  AV_COPY128(top_border + 48, src_cr + 16 * uvlinesize);
595  } else {
596  AV_COPY64(top_border + 16, src_cb + 16 * uvlinesize);
597  AV_COPY64(top_border + 24, src_cr + 16 * uvlinesize);
598  }
599  } else {
600  if (pixel_shift) {
601  AV_COPY128(top_border + 32, src_cb + 8 * uvlinesize);
602  AV_COPY128(top_border + 48, src_cr + 8 * uvlinesize);
603  } else {
604  AV_COPY64(top_border + 16, src_cb + 8 * uvlinesize);
605  AV_COPY64(top_border + 24, src_cr + 8 * uvlinesize);
606  }
607  }
608  }
609 }
610 
616 static void implicit_weight_table(const H264Context *h, H264SliceContext *sl, int field)
617 {
618  int ref0, ref1, i, cur_poc, ref_start, ref_count0, ref_count1;
619 
620  for (i = 0; i < 2; i++) {
621  sl->pwt.luma_weight_flag[i] = 0;
622  sl->pwt.chroma_weight_flag[i] = 0;
623  }
624 
625  if (field < 0) {
626  if (h->picture_structure == PICT_FRAME) {
627  cur_poc = h->cur_pic_ptr->poc;
628  } else {
629  cur_poc = h->cur_pic_ptr->field_poc[h->picture_structure - 1];
630  }
631  if (sl->ref_count[0] == 1 && sl->ref_count[1] == 1 && !FRAME_MBAFF(h) &&
632  sl->ref_list[0][0].poc + sl->ref_list[1][0].poc == 2 * cur_poc) {
633  sl->pwt.use_weight = 0;
634  sl->pwt.use_weight_chroma = 0;
635  return;
636  }
637  ref_start = 0;
638  ref_count0 = sl->ref_count[0];
639  ref_count1 = sl->ref_count[1];
640  } else {
641  cur_poc = h->cur_pic_ptr->field_poc[field];
642  ref_start = 16;
643  ref_count0 = 16 + 2 * sl->ref_count[0];
644  ref_count1 = 16 + 2 * sl->ref_count[1];
645  }
646 
647  sl->pwt.use_weight = 2;
648  sl->pwt.use_weight_chroma = 2;
649  sl->pwt.luma_log2_weight_denom = 5;
651 
652  for (ref0 = ref_start; ref0 < ref_count0; ref0++) {
653  int poc0 = sl->ref_list[0][ref0].poc;
654  for (ref1 = ref_start; ref1 < ref_count1; ref1++) {
655  int w = 32;
656  if (!sl->ref_list[0][ref0].parent->long_ref && !sl->ref_list[1][ref1].parent->long_ref) {
657  int poc1 = sl->ref_list[1][ref1].poc;
658  int td = av_clip_int8(poc1 - poc0);
659  if (td) {
660  int tb = av_clip_int8(cur_poc - poc0);
661  int tx = (16384 + (FFABS(td) >> 1)) / td;
662  int dist_scale_factor = (tb * tx + 32) >> 8;
663  if (dist_scale_factor >= -64 && dist_scale_factor <= 128)
664  w = 64 - dist_scale_factor;
665  }
666  }
667  if (field < 0) {
668  sl->pwt.implicit_weight[ref0][ref1][0] =
669  sl->pwt.implicit_weight[ref0][ref1][1] = w;
670  } else {
671  sl->pwt.implicit_weight[ref0][ref1][field] = w;
672  }
673  }
674  }
675 }
676 
681 {
682  int i;
683  for (i = 0; i < 16; i++) {
684 #define TRANSPOSE(x) (x >> 2) | ((x << 2) & 0xF)
686  h->field_scan[i] = TRANSPOSE(field_scan[i]);
687 #undef TRANSPOSE
688  }
689  for (i = 0; i < 64; i++) {
690 #define TRANSPOSE(x) (x >> 3) | ((x & 7) << 3)
695 #undef TRANSPOSE
696  }
697  if (h->ps.sps->transform_bypass) { // FIXME same ugly
704  } else {
705  h->zigzag_scan_q0 = h->zigzag_scan;
708  h->field_scan_q0 = h->field_scan;
711  }
712 }
713 
715 {
716 #define HWACCEL_MAX (CONFIG_H264_DXVA2_HWACCEL + \
717  CONFIG_H264_D3D11VA_HWACCEL + \
718  CONFIG_H264_VAAPI_HWACCEL + \
719  (CONFIG_H264_VDA_HWACCEL * 2) + \
720  CONFIG_H264_VDPAU_HWACCEL)
721  enum AVPixelFormat pix_fmts[HWACCEL_MAX + 2], *fmt = pix_fmts;
722  const enum AVPixelFormat *choices = pix_fmts;
723 
724  switch (h->ps.sps->bit_depth_luma) {
725  case 9:
726  if (CHROMA444(h)) {
727  if (h->avctx->colorspace == AVCOL_SPC_RGB) {
728  *fmt++ = AV_PIX_FMT_GBRP9;
729  } else
730  *fmt++ = AV_PIX_FMT_YUV444P9;
731  } else if (CHROMA422(h))
732  *fmt++ = AV_PIX_FMT_YUV422P9;
733  else
734  *fmt++ = AV_PIX_FMT_YUV420P9;
735  break;
736  case 10:
737  if (CHROMA444(h)) {
738  if (h->avctx->colorspace == AVCOL_SPC_RGB) {
739  *fmt++ = AV_PIX_FMT_GBRP10;
740  } else
741  *fmt++ = AV_PIX_FMT_YUV444P10;
742  } else if (CHROMA422(h))
743  *fmt++ = AV_PIX_FMT_YUV422P10;
744  else
745  *fmt++ = AV_PIX_FMT_YUV420P10;
746  break;
747  case 8:
748 #if CONFIG_H264_VDPAU_HWACCEL
749  *fmt++ = AV_PIX_FMT_VDPAU;
750 #endif
751  if (CHROMA444(h)) {
752  if (h->avctx->colorspace == AVCOL_SPC_RGB)
753  *fmt++ = AV_PIX_FMT_GBRP;
754  else if (h->avctx->color_range == AVCOL_RANGE_JPEG)
755  *fmt++ = AV_PIX_FMT_YUVJ444P;
756  else
757  *fmt++ = AV_PIX_FMT_YUV444P;
758  } else if (CHROMA422(h)) {
760  *fmt++ = AV_PIX_FMT_YUVJ422P;
761  else
762  *fmt++ = AV_PIX_FMT_YUV422P;
763  } else {
764 #if CONFIG_H264_DXVA2_HWACCEL
765  *fmt++ = AV_PIX_FMT_DXVA2_VLD;
766 #endif
767 #if CONFIG_H264_D3D11VA_HWACCEL
768  *fmt++ = AV_PIX_FMT_D3D11VA_VLD;
769 #endif
770 #if CONFIG_H264_VAAPI_HWACCEL
771  *fmt++ = AV_PIX_FMT_VAAPI;
772 #endif
773 #if CONFIG_H264_VDA_HWACCEL
774  *fmt++ = AV_PIX_FMT_VDA_VLD;
775  *fmt++ = AV_PIX_FMT_VDA;
776 #endif
777  if (h->avctx->codec->pix_fmts)
778  choices = h->avctx->codec->pix_fmts;
779  else if (h->avctx->color_range == AVCOL_RANGE_JPEG)
780  *fmt++ = AV_PIX_FMT_YUVJ420P;
781  else
782  *fmt++ = AV_PIX_FMT_YUV420P;
783  }
784  break;
785  default:
787  "Unsupported bit depth %d\n", h->ps.sps->bit_depth_luma);
788  return AVERROR_INVALIDDATA;
789  }
790 
791  *fmt = AV_PIX_FMT_NONE;
792 
793  return ff_get_format(h->avctx, choices);
794 }
795 
796 /* export coded and cropped frame dimensions to AVCodecContext */
798 {
799  SPS *sps = h->ps.sps;
800  int width = h->width - (sps->crop_right + sps->crop_left);
801  int height = h->height - (sps->crop_top + sps->crop_bottom);
802 
803  /* handle container cropping */
804  if (FFALIGN(h->avctx->width, 16) == FFALIGN(width, 16) &&
805  FFALIGN(h->avctx->height, 16) == FFALIGN(height, 16)) {
806  width = h->avctx->width;
807  height = h->avctx->height;
808  }
809 
810  if (width <= 0 || height <= 0) {
811  av_log(h->avctx, AV_LOG_ERROR, "Invalid cropped dimensions: %dx%d.\n",
812  width, height);
814  return AVERROR_INVALIDDATA;
815 
816  av_log(h->avctx, AV_LOG_WARNING, "Ignoring cropping information.\n");
817  sps->crop_bottom =
818  sps->crop_top =
819  sps->crop_right =
820  sps->crop_left =
821  sps->crop = 0;
822 
823  width = h->width;
824  height = h->height;
825  }
826 
827  h->avctx->coded_width = h->width;
828  h->avctx->coded_height = h->height;
829  h->avctx->width = width;
830  h->avctx->height = height;
831 
832  return 0;
833 }
834 
836 {
837  const SPS *sps = h->ps.sps;
838  int i, ret;
839 
840  ff_set_sar(h->avctx, sps->sar);
842  &h->chroma_x_shift, &h->chroma_y_shift);
843 
844  if (sps->timing_info_present_flag) {
845  int64_t den = sps->time_scale;
846  if (h->x264_build < 44U)
847  den *= 2;
849  sps->num_units_in_tick, den, 1 << 30);
850  }
851 
853 
854  h->first_field = 0;
855  h->prev_interlaced_frame = 1;
856 
857  init_scan_tables(h);
858  ret = ff_h264_alloc_tables(h);
859  if (ret < 0) {
860  av_log(h->avctx, AV_LOG_ERROR, "Could not allocate memory\n");
861  return ret;
862  }
863 
864  if (sps->bit_depth_luma < 8 || sps->bit_depth_luma > 10) {
865  av_log(h->avctx, AV_LOG_ERROR, "Unsupported bit depth %d\n",
866  sps->bit_depth_luma);
867  return AVERROR_INVALIDDATA;
868  }
869 
871  h->pixel_shift = sps->bit_depth_luma > 8;
873  h->bit_depth_luma = sps->bit_depth_luma;
874 
876  sps->chroma_format_idc);
880  sps->chroma_format_idc);
882 
884  ret = ff_h264_slice_context_init(h, &h->slice_ctx[0]);
885  if (ret < 0) {
886  av_log(h->avctx, AV_LOG_ERROR, "context_init() failed.\n");
887  return ret;
888  }
889  } else {
890  for (i = 0; i < h->nb_slice_ctx; i++) {
891  H264SliceContext *sl = &h->slice_ctx[i];
892 
893  sl->h264 = h;
894  sl->intra4x4_pred_mode = h->intra4x4_pred_mode + i * 8 * 2 * h->mb_stride;
895  sl->mvd_table[0] = h->mvd_table[0] + i * 8 * 2 * h->mb_stride;
896  sl->mvd_table[1] = h->mvd_table[1] + i * 8 * 2 * h->mb_stride;
897 
898  if ((ret = ff_h264_slice_context_init(h, sl)) < 0) {
899  av_log(h->avctx, AV_LOG_ERROR, "context_init() failed.\n");
900  return ret;
901  }
902  }
903  }
904 
905  h->context_initialized = 1;
906 
907  return 0;
908 }
909 
910 static int h264_init_ps(H264Context *h, const H264SliceContext *sl)
911 {
912  const SPS *sps;
913  int needs_reinit = 0, ret;
914 
915  h->ps.pps = (const PPS*)h->ps.pps_list[sl->pps_id]->data;
916  if (h->ps.sps != (const SPS*)h->ps.sps_list[h->ps.pps->sps_id]->data) {
917  h->ps.sps = (SPS*)h->ps.sps_list[h->ps.pps->sps_id]->data;
918 
919  if (h->bit_depth_luma != h->ps.sps->bit_depth_luma ||
921  needs_reinit = 1;
922  }
923  sps = h->ps.sps;
924 
925  h->avctx->profile = ff_h264_get_profile(sps);
926  h->avctx->level = sps->level_idc;
927  h->avctx->refs = sps->ref_frame_count;
928 
929  if (h->mb_width != sps->mb_width ||
930  h->mb_height != sps->mb_height)
931  needs_reinit = 1;
932 
933  h->mb_width = sps->mb_width;
934  h->mb_height = sps->mb_height;
935  h->mb_num = h->mb_width * h->mb_height;
936  h->mb_stride = h->mb_width + 1;
937 
938  h->b_stride = h->mb_width * 4;
939 
940  h->chroma_y_shift = sps->chroma_format_idc <= 1; // 400 uses yuv420p
941 
942  h->width = 16 * h->mb_width;
943  h->height = 16 * h->mb_height;
944 
945  ret = init_dimensions(h);
946  if (ret < 0)
947  return ret;
948 
953  if (h->avctx->colorspace != sps->colorspace)
954  needs_reinit = 1;
956  h->avctx->color_trc = sps->color_trc;
957  h->avctx->colorspace = sps->colorspace;
958  }
959  }
960 
961  if (!h->context_initialized || needs_reinit) {
962  h->context_initialized = 0;
963  if (sl != h->slice_ctx) {
965  "changing width %d -> %d / height %d -> %d on "
966  "slice %d\n",
967  h->width, h->avctx->coded_width,
968  h->height, h->avctx->coded_height,
969  h->current_slice + 1);
970  return AVERROR_INVALIDDATA;
971  }
972 
974 
975  if ((ret = get_pixel_format(h)) < 0)
976  return ret;
977  h->avctx->pix_fmt = ret;
978 
979  av_log(h->avctx, AV_LOG_VERBOSE, "Reinit context to %dx%d, "
980  "pix_fmt: %d\n", h->width, h->height, h->avctx->pix_fmt);
981 
982  if ((ret = h264_slice_header_init(h)) < 0) {
984  "h264_slice_header_init() failed\n");
985  return ret;
986  }
987  }
988 
989  return 0;
990 }
991 
993 {
994  const SPS *sps = h->ps.sps;
995  H264Picture *cur = h->cur_pic_ptr;
996 
997  cur->f->interlaced_frame = 0;
998  cur->f->repeat_pict = 0;
999 
1000  /* Signal interlacing information externally. */
1001  /* Prioritize picture timing SEI information over used
1002  * decoding process if it exists. */
1003 
1004  if (sps->pic_struct_present_flag) {
1006  switch (pt->pic_struct) {
1007  case SEI_PIC_STRUCT_FRAME:
1008  break;
1011  cur->f->interlaced_frame = 1;
1012  break;
1015  if (FIELD_OR_MBAFF_PICTURE(h))
1016  cur->f->interlaced_frame = 1;
1017  else
1018  // try to flag soft telecine progressive
1020  break;
1023  /* Signal the possibility of telecined film externally
1024  * (pic_struct 5,6). From these hints, let the applications
1025  * decide if they apply deinterlacing. */
1026  cur->f->repeat_pict = 1;
1027  break;
1029  cur->f->repeat_pict = 2;
1030  break;
1032  cur->f->repeat_pict = 4;
1033  break;
1034  }
1035 
1036  if ((pt->ct_type & 3) &&
1038  cur->f->interlaced_frame = (pt->ct_type & (1 << 1)) != 0;
1039  } else {
1040  /* Derive interlacing flag from used decoding process. */
1042  }
1044 
1045  if (cur->field_poc[0] != cur->field_poc[1]) {
1046  /* Derive top_field_first from field pocs. */
1047  cur->f->top_field_first = cur->field_poc[0] < cur->field_poc[1];
1048  } else {
1049  if (cur->f->interlaced_frame || sps->pic_struct_present_flag) {
1050  /* Use picture timing SEI information. Even if it is a
1051  * information of a past frame, better than nothing. */
1054  cur->f->top_field_first = 1;
1055  else
1056  cur->f->top_field_first = 0;
1057  } else {
1058  /* Most likely progressive */
1059  cur->f->top_field_first = 0;
1060  }
1061  }
1062 
1063  if (h->sei.frame_packing.present &&
1069  AVStereo3D *stereo = av_stereo3d_create_side_data(cur->f);
1070  if (!stereo)
1071  return AVERROR(ENOMEM);
1072 
1073  switch (fp->arrangement_type) {
1074  case 0:
1075  stereo->type = AV_STEREO3D_CHECKERBOARD;
1076  break;
1077  case 1:
1078  stereo->type = AV_STEREO3D_COLUMNS;
1079  break;
1080  case 2:
1081  stereo->type = AV_STEREO3D_LINES;
1082  break;
1083  case 3:
1084  if (fp->quincunx_subsampling)
1086  else
1087  stereo->type = AV_STEREO3D_SIDEBYSIDE;
1088  break;
1089  case 4:
1090  stereo->type = AV_STEREO3D_TOPBOTTOM;
1091  break;
1092  case 5:
1093  stereo->type = AV_STEREO3D_FRAMESEQUENCE;
1094  break;
1095  case 6:
1096  stereo->type = AV_STEREO3D_2D;
1097  break;
1098  }
1099 
1100  if (fp->content_interpretation_type == 2)
1101  stereo->flags = AV_STEREO3D_FLAG_INVERT;
1102  }
1103 
1104  if (h->sei.display_orientation.present &&
1109  double angle = o->anticlockwise_rotation * 360 / (double) (1 << 16);
1110  AVFrameSideData *rotation = av_frame_new_side_data(cur->f,
1112  sizeof(int32_t) * 9);
1113  if (!rotation)
1114  return AVERROR(ENOMEM);
1115 
1116  av_display_rotation_set((int32_t *)rotation->data, angle);
1117  av_display_matrix_flip((int32_t *)rotation->data,
1118  o->hflip, o->vflip);
1119  }
1120 
1121  if (h->sei.afd.present) {
1123  sizeof(uint8_t));
1124  if (!sd)
1125  return AVERROR(ENOMEM);
1126 
1128  h->sei.afd.present = 0;
1129  }
1130 
1131  if (h->sei.a53_caption.a53_caption) {
1132  H264SEIA53Caption *a53 = &h->sei.a53_caption;
1135  a53->a53_caption_size);
1136  if (!sd)
1137  return AVERROR(ENOMEM);
1138 
1139  memcpy(sd->data, a53->a53_caption, a53->a53_caption_size);
1140  av_freep(&a53->a53_caption);
1141  a53->a53_caption_size = 0;
1142  }
1143 
1144  return 0;
1145 }
1146 
1148 {
1149  const SPS *sps = h->ps.sps;
1150  H264Picture *out = h->cur_pic_ptr;
1151  H264Picture *cur = h->cur_pic_ptr;
1152  int i, pics, out_of_order, out_idx;
1153  int invalid = 0, cnt = 0;
1154  int ret;
1155 
1156  if (sps->bitstream_restriction_flag ||
1159  }
1160 
1161  pics = 0;
1162  while (h->delayed_pic[pics])
1163  pics++;
1164 
1165  assert(pics <= MAX_DELAYED_PIC_COUNT);
1166 
1167  h->delayed_pic[pics++] = cur;
1168  if (cur->reference == 0)
1169  cur->reference = DELAYED_PIC_REF;
1170 
1171  /* Frame reordering. This code takes pictures from coding order and sorts
1172  * them by their incremental POC value into display order. It supports POC
1173  * gaps, MMCO reset codes and random resets.
1174  * A "display group" can start either with a IDR frame (f.key_frame = 1),
1175  * and/or can be closed down with a MMCO reset code. In sequences where
1176  * there is no delay, we can't detect that (since the frame was already
1177  * output to the user), so we also set h->mmco_reset to detect the MMCO
1178  * reset code.
1179  * FIXME: if we detect insufficient delays (as per h->avctx->has_b_frames),
1180  * we increase the delay between input and output. All frames affected by
1181  * the lag (e.g. those that should have been output before another frame
1182  * that we already returned to the user) will be dropped. This is a bug
1183  * that we will fix later. */
1184  for (i = 0; i < MAX_DELAYED_PIC_COUNT; i++) {
1185  cnt += out->poc < h->last_pocs[i];
1186  invalid += out->poc == INT_MIN;
1187  }
1188  if (!h->mmco_reset && !cur->f->key_frame &&
1189  cnt + invalid == MAX_DELAYED_PIC_COUNT && cnt > 0) {
1190  h->mmco_reset = 2;
1191  if (pics > 1)
1192  h->delayed_pic[pics - 2]->mmco_reset = 2;
1193  }
1194  if (h->mmco_reset || cur->f->key_frame) {
1195  for (i = 0; i < MAX_DELAYED_PIC_COUNT; i++)
1196  h->last_pocs[i] = INT_MIN;
1197  cnt = 0;
1198  invalid = MAX_DELAYED_PIC_COUNT;
1199  }
1200  out = h->delayed_pic[0];
1201  out_idx = 0;
1202  for (i = 1; i < MAX_DELAYED_PIC_COUNT &&
1203  h->delayed_pic[i] &&
1204  !h->delayed_pic[i - 1]->mmco_reset &&
1205  !h->delayed_pic[i]->f->key_frame;
1206  i++)
1207  if (h->delayed_pic[i]->poc < out->poc) {
1208  out = h->delayed_pic[i];
1209  out_idx = i;
1210  }
1211  if (h->avctx->has_b_frames == 0 &&
1212  (h->delayed_pic[0]->f->key_frame || h->mmco_reset))
1213  h->next_outputed_poc = INT_MIN;
1214  out_of_order = !out->f->key_frame && !h->mmco_reset &&
1215  (out->poc < h->next_outputed_poc);
1216 
1217  if (sps->bitstream_restriction_flag &&
1218  h->avctx->has_b_frames >= sps->num_reorder_frames) {
1219  } else if (out_of_order && pics - 1 == h->avctx->has_b_frames &&
1220  h->avctx->has_b_frames < MAX_DELAYED_PIC_COUNT) {
1221  if (invalid + cnt < MAX_DELAYED_PIC_COUNT) {
1222  h->avctx->has_b_frames = FFMAX(h->avctx->has_b_frames, cnt);
1223  }
1224  } else if (!h->avctx->has_b_frames &&
1225  ((h->next_outputed_poc != INT_MIN &&
1226  out->poc > h->next_outputed_poc + 2) ||
1227  cur->f->pict_type == AV_PICTURE_TYPE_B)) {
1228  h->avctx->has_b_frames++;
1229  }
1230 
1231  if (pics > h->avctx->has_b_frames) {
1232  out->reference &= ~DELAYED_PIC_REF;
1233  for (i = out_idx; h->delayed_pic[i]; i++)
1234  h->delayed_pic[i] = h->delayed_pic[i + 1];
1235  }
1236  memmove(h->last_pocs, &h->last_pocs[1],
1237  sizeof(*h->last_pocs) * (MAX_DELAYED_PIC_COUNT - 1));
1238  h->last_pocs[MAX_DELAYED_PIC_COUNT - 1] = cur->poc;
1239  if (!out_of_order && pics > h->avctx->has_b_frames) {
1241  ret = av_frame_ref(h->output_frame, out->f);
1242  if (ret < 0)
1243  return ret;
1244 
1245  if (out->recovered) {
1246  // We have reached an recovery point and all frames after it in
1247  // display order are "recovered".
1249  }
1251 
1252  if (!out->recovered) {
1255  else
1257  }
1258 
1259  if (out->mmco_reset) {
1260  if (out_idx > 0) {
1261  h->next_outputed_poc = out->poc;
1262  h->delayed_pic[out_idx - 1]->mmco_reset = out->mmco_reset;
1263  } else {
1264  h->next_outputed_poc = INT_MIN;
1265  }
1266  } else {
1267  if (out_idx == 0 && pics > 1 && h->delayed_pic[0]->f->key_frame) {
1268  h->next_outputed_poc = INT_MIN;
1269  } else {
1270  h->next_outputed_poc = out->poc;
1271  }
1272  }
1273  h->mmco_reset = 0;
1274  } else {
1275  av_log(h->avctx, AV_LOG_DEBUG, "no picture\n");
1276  }
1277 
1278  return 0;
1279 }
1280 
1281 /* This function is called right after decoding the slice header for a first
1282  * slice in a field (or a frame). It decides whether we are decoding a new frame
1283  * or a second field in a pair and does the necessary setup.
1284  */
1286  const H2645NAL *nal)
1287 {
1288  const SPS *sps;
1289 
1290  int last_pic_structure, last_pic_droppable, ret;
1291 
1292  ret = h264_init_ps(h, sl);
1293  if (ret < 0)
1294  return ret;
1295 
1296  sps = h->ps.sps;
1297 
1298  last_pic_droppable = h->droppable;
1299  last_pic_structure = h->picture_structure;
1300  h->droppable = (nal->ref_idc == 0);
1302 
1303  h->poc.frame_num = sl->frame_num;
1304  h->poc.poc_lsb = sl->poc_lsb;
1306  h->poc.delta_poc[0] = sl->delta_poc[0];
1307  h->poc.delta_poc[1] = sl->delta_poc[1];
1308 
1309  /* Shorten frame num gaps so we don't have to allocate reference
1310  * frames just to throw them away */
1311  if (h->poc.frame_num != h->poc.prev_frame_num) {
1312  int unwrap_prev_frame_num = h->poc.prev_frame_num;
1313  int max_frame_num = 1 << sps->log2_max_frame_num;
1314 
1315  if (unwrap_prev_frame_num > h->poc.frame_num)
1316  unwrap_prev_frame_num -= max_frame_num;
1317 
1318  if ((h->poc.frame_num - unwrap_prev_frame_num) > sps->ref_frame_count) {
1319  unwrap_prev_frame_num = (h->poc.frame_num - sps->ref_frame_count) - 1;
1320  if (unwrap_prev_frame_num < 0)
1321  unwrap_prev_frame_num += max_frame_num;
1322 
1323  h->poc.prev_frame_num = unwrap_prev_frame_num;
1324  }
1325  }
1326 
1327  /* See if we have a decoded first field looking for a pair...
1328  * Here, we're using that to see if we should mark previously
1329  * decode frames as "finished".
1330  * We have to do that before the "dummy" in-between frame allocation,
1331  * since that can modify s->current_picture_ptr. */
1332  if (h->first_field) {
1333  assert(h->cur_pic_ptr);
1334  assert(h->cur_pic_ptr->f->buf[0]);
1335  assert(h->cur_pic_ptr->reference != DELAYED_PIC_REF);
1336 
1337  /* figure out if we have a complementary field pair */
1338  if (!FIELD_PICTURE(h) || h->picture_structure == last_pic_structure) {
1339  /* Previous field is unmatched. Don't display it, but let it
1340  * remain for reference if marked as such. */
1341  if (!last_pic_droppable && last_pic_structure != PICT_FRAME) {
1343  last_pic_structure == PICT_TOP_FIELD);
1344  }
1345  } else {
1346  if (h->cur_pic_ptr->frame_num != h->poc.frame_num) {
1347  /* This and previous field were reference, but had
1348  * different frame_nums. Consider this field first in
1349  * pair. Throw away previous field except for reference
1350  * purposes. */
1351  if (!last_pic_droppable && last_pic_structure != PICT_FRAME) {
1353  last_pic_structure == PICT_TOP_FIELD);
1354  }
1355  } else {
1356  /* Second field in complementary pair */
1357  if (!((last_pic_structure == PICT_TOP_FIELD &&
1359  (last_pic_structure == PICT_BOTTOM_FIELD &&
1362  "Invalid field mode combination %d/%d\n",
1363  last_pic_structure, h->picture_structure);
1364  h->picture_structure = last_pic_structure;
1365  h->droppable = last_pic_droppable;
1366  return AVERROR_INVALIDDATA;
1367  } else if (last_pic_droppable != h->droppable) {
1369  "Found reference and non-reference fields in the same frame, which");
1370  h->picture_structure = last_pic_structure;
1371  h->droppable = last_pic_droppable;
1372  return AVERROR_PATCHWELCOME;
1373  }
1374  }
1375  }
1376  }
1377 
1378  while (h->poc.frame_num != h->poc.prev_frame_num &&
1379  h->poc.frame_num != (h->poc.prev_frame_num + 1) % (1 << sps->log2_max_frame_num)) {
1380  H264Picture *prev = h->short_ref_count ? h->short_ref[0] : NULL;
1381  av_log(h->avctx, AV_LOG_DEBUG, "Frame num gap %d %d\n",
1382  h->poc.frame_num, h->poc.prev_frame_num);
1383  ret = initialize_cur_frame(h);
1384  if (ret < 0) {
1385  h->first_field = 0;
1386  return ret;
1387  }
1388 
1389  h->poc.prev_frame_num++;
1390  h->poc.prev_frame_num %= 1 << sps->log2_max_frame_num;
1392  ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX, 0);
1393  ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX, 1);
1394 
1395  h->explicit_ref_marking = 0;
1397  if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
1398  return ret;
1399  /* Error concealment: If a ref is missing, copy the previous ref
1400  * in its place.
1401  * FIXME: Avoiding a memcpy would be nice, but ref handling makes
1402  * many assumptions about there being no actual duplicates.
1403  * FIXME: This does not copy padding for out-of-frame motion
1404  * vectors. Given we are concealing a lost frame, this probably
1405  * is not noticeable by comparison, but it should be fixed. */
1406  if (h->short_ref_count) {
1407  if (prev &&
1408  h->short_ref[0]->f->width == prev->f->width &&
1409  h->short_ref[0]->f->height == prev->f->height &&
1410  h->short_ref[0]->f->format == prev->f->format) {
1411  ff_thread_await_progress(&prev->tf, INT_MAX, 0);
1412  if (prev->field_picture)
1413  ff_thread_await_progress(&prev->tf, INT_MAX, 1);
1414  av_image_copy(h->short_ref[0]->f->data,
1415  h->short_ref[0]->f->linesize,
1416  (const uint8_t **)prev->f->data,
1417  prev->f->linesize,
1418  prev->f->format,
1419  h->mb_width * 16,
1420  h->mb_height * 16);
1421  h->short_ref[0]->poc = prev->poc + 2;
1422  }
1423  h->short_ref[0]->frame_num = h->poc.prev_frame_num;
1424  }
1425  }
1426 
1427  /* See if we have a decoded first field looking for a pair...
1428  * We're using that to see whether to continue decoding in that
1429  * frame, or to allocate a new one. */
1430  if (h->first_field) {
1431  assert(h->cur_pic_ptr);
1432  assert(h->cur_pic_ptr->f->buf[0]);
1433  assert(h->cur_pic_ptr->reference != DELAYED_PIC_REF);
1434 
1435  /* figure out if we have a complementary field pair */
1436  if (!FIELD_PICTURE(h) || h->picture_structure == last_pic_structure) {
1437  /* Previous field is unmatched. Don't display it, but let it
1438  * remain for reference if marked as such. */
1439  h->cur_pic_ptr = NULL;
1440  h->first_field = FIELD_PICTURE(h);
1441  } else {
1442  if (h->cur_pic_ptr->frame_num != h->poc.frame_num) {
1443  /* This and the previous field had different frame_nums.
1444  * Consider this field first in pair. Throw away previous
1445  * one except for reference purposes. */
1446  h->first_field = 1;
1447  h->cur_pic_ptr = NULL;
1448  } else {
1449  /* Second field in complementary pair */
1450  h->first_field = 0;
1451  }
1452  }
1453  } else {
1454  /* Frame or first field in a potentially complementary pair */
1455  h->first_field = FIELD_PICTURE(h);
1456  }
1457 
1458  if (!FIELD_PICTURE(h) || h->first_field) {
1459  if (h264_frame_start(h) < 0) {
1460  h->first_field = 0;
1461  return AVERROR_INVALIDDATA;
1462  }
1463  } else {
1465  }
1466 
1468  h->ps.sps, &h->poc, h->picture_structure, nal->ref_idc);
1469 
1470  memcpy(h->mmco, sl->mmco, sl->nb_mmco * sizeof(*h->mmco));
1471  h->nb_mmco = sl->nb_mmco;
1473 
1474  h->picture_idr = nal->type == H264_NAL_IDR_SLICE;
1475 
1476  if (h->sei.recovery_point.recovery_frame_cnt >= 0 && h->recovery_frame < 0) {
1478  ((1 << h->ps.sps->log2_max_frame_num) - 1);
1479  }
1480 
1481  h->cur_pic_ptr->f->key_frame |= (nal->type == H264_NAL_IDR_SLICE) ||
1483 
1484  if (nal->type == H264_NAL_IDR_SLICE || h->recovery_frame == h->poc.frame_num) {
1485  h->recovery_frame = -1;
1486  h->cur_pic_ptr->recovered = 1;
1487  }
1488  // If we have an IDR, all frames after it in decoded order are
1489  // "recovered".
1490  if (nal->type == H264_NAL_IDR_SLICE)
1493 
1494  /* Set the frame properties/side data. Only done for the second field in
1495  * field coded frames, since some SEI information is present for each field
1496  * and is merged by the SEI parsing code. */
1497  if (!FIELD_PICTURE(h) || !h->first_field) {
1498  ret = h264_export_frame_props(h);
1499  if (ret < 0)
1500  return ret;
1501 
1502  ret = h264_select_output_frame(h);
1503  if (ret < 0)
1504  return ret;
1505  }
1506 
1507  if (h->avctx->hwaccel) {
1508  ret = h->avctx->hwaccel->start_frame(h->avctx, NULL, 0);
1509  if (ret < 0)
1510  return ret;
1511  }
1512 
1513  return 0;
1514 }
1515 
1517  const H264ParamSets *ps, AVCodecContext *avctx)
1518 {
1519  const SPS *sps;
1520  const PPS *pps;
1521  int ret;
1522  unsigned int slice_type, tmp, i;
1523  int field_pic_flag, bottom_field_flag, picture_structure;
1524 
1525  sl->first_mb_addr = get_ue_golomb(&sl->gb);
1526 
1527  slice_type = get_ue_golomb_31(&sl->gb);
1528  if (slice_type > 9) {
1529  av_log(avctx, AV_LOG_ERROR,
1530  "slice type %d too large at %d\n",
1531  slice_type, sl->first_mb_addr);
1532  return AVERROR_INVALIDDATA;
1533  }
1534  if (slice_type > 4) {
1535  slice_type -= 5;
1536  sl->slice_type_fixed = 1;
1537  } else
1538  sl->slice_type_fixed = 0;
1539 
1540  slice_type = ff_h264_golomb_to_pict_type[slice_type];
1541  sl->slice_type = slice_type;
1542  sl->slice_type_nos = slice_type & 3;
1543 
1544  if (nal->type == H264_NAL_IDR_SLICE &&
1546  av_log(avctx, AV_LOG_ERROR, "A non-intra slice in an IDR NAL unit.\n");
1547  return AVERROR_INVALIDDATA;
1548  }
1549 
1550  sl->pps_id = get_ue_golomb(&sl->gb);
1551  if (sl->pps_id >= MAX_PPS_COUNT) {
1552  av_log(avctx, AV_LOG_ERROR, "pps_id %u out of range\n", sl->pps_id);
1553  return AVERROR_INVALIDDATA;
1554  }
1555  if (!ps->pps_list[sl->pps_id]) {
1556  av_log(avctx, AV_LOG_ERROR,
1557  "non-existing PPS %u referenced\n",
1558  sl->pps_id);
1559  return AVERROR_INVALIDDATA;
1560  }
1561  pps = (const PPS*)ps->pps_list[sl->pps_id]->data;
1562 
1563  if (!ps->sps_list[pps->sps_id]) {
1564  av_log(avctx, AV_LOG_ERROR,
1565  "non-existing SPS %u referenced\n", pps->sps_id);
1566  return AVERROR_INVALIDDATA;
1567  }
1568  sps = (const SPS*)ps->sps_list[pps->sps_id]->data;
1569 
1570  sl->frame_num = get_bits(&sl->gb, sps->log2_max_frame_num);
1571 
1572  sl->mb_mbaff = 0;
1573 
1574  if (sps->frame_mbs_only_flag) {
1575  picture_structure = PICT_FRAME;
1576  } else {
1577  field_pic_flag = get_bits1(&sl->gb);
1578  if (field_pic_flag) {
1579  bottom_field_flag = get_bits1(&sl->gb);
1580  picture_structure = PICT_TOP_FIELD + bottom_field_flag;
1581  } else {
1582  picture_structure = PICT_FRAME;
1583  }
1584  }
1585  sl->picture_structure = picture_structure;
1586  sl->mb_field_decoding_flag = picture_structure != PICT_FRAME;
1587 
1588  if (picture_structure == PICT_FRAME) {
1589  sl->curr_pic_num = sl->frame_num;
1590  sl->max_pic_num = 1 << sps->log2_max_frame_num;
1591  } else {
1592  sl->curr_pic_num = 2 * sl->frame_num + 1;
1593  sl->max_pic_num = 1 << (sps->log2_max_frame_num + 1);
1594  }
1595 
1596  if (nal->type == H264_NAL_IDR_SLICE)
1597  get_ue_golomb(&sl->gb); /* idr_pic_id */
1598 
1599  if (sps->poc_type == 0) {
1600  sl->poc_lsb = get_bits(&sl->gb, sps->log2_max_poc_lsb);
1601 
1602  if (pps->pic_order_present == 1 && picture_structure == PICT_FRAME)
1603  sl->delta_poc_bottom = get_se_golomb(&sl->gb);
1604  }
1605 
1606  if (sps->poc_type == 1 && !sps->delta_pic_order_always_zero_flag) {
1607  sl->delta_poc[0] = get_se_golomb(&sl->gb);
1608 
1609  if (pps->pic_order_present == 1 && picture_structure == PICT_FRAME)
1610  sl->delta_poc[1] = get_se_golomb(&sl->gb);
1611  }
1612 
1613  sl->redundant_pic_count = 0;
1614  if (pps->redundant_pic_cnt_present)
1615  sl->redundant_pic_count = get_ue_golomb(&sl->gb);
1616 
1617  if (sl->slice_type_nos == AV_PICTURE_TYPE_B)
1618  sl->direct_spatial_mv_pred = get_bits1(&sl->gb);
1619 
1621  &sl->gb, pps, sl->slice_type_nos,
1622  picture_structure);
1623  if (ret < 0)
1624  return ret;
1625 
1626  if (sl->slice_type_nos != AV_PICTURE_TYPE_I) {
1627  ret = ff_h264_decode_ref_pic_list_reordering(sl, avctx);
1628  if (ret < 0) {
1629  sl->ref_count[1] = sl->ref_count[0] = 0;
1630  return ret;
1631  }
1632  }
1633 
1634  sl->pwt.use_weight = 0;
1635  for (i = 0; i < 2; i++) {
1636  sl->pwt.luma_weight_flag[i] = 0;
1637  sl->pwt.chroma_weight_flag[i] = 0;
1638  }
1639  if ((pps->weighted_pred && sl->slice_type_nos == AV_PICTURE_TYPE_P) ||
1640  (pps->weighted_bipred_idc == 1 &&
1642  ff_h264_pred_weight_table(&sl->gb, sps, sl->ref_count,
1643  sl->slice_type_nos, &sl->pwt);
1644 
1645  sl->explicit_ref_marking = 0;
1646  if (nal->ref_idc) {
1647  ret = ff_h264_decode_ref_pic_marking(sl, &sl->gb, nal, avctx);
1648  if (ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE))
1649  return AVERROR_INVALIDDATA;
1650  }
1651 
1652  if (sl->slice_type_nos != AV_PICTURE_TYPE_I && pps->cabac) {
1653  tmp = get_ue_golomb_31(&sl->gb);
1654  if (tmp > 2) {
1655  av_log(avctx, AV_LOG_ERROR, "cabac_init_idc %u overflow\n", tmp);
1656  return AVERROR_INVALIDDATA;
1657  }
1658  sl->cabac_init_idc = tmp;
1659  }
1660 
1661  sl->last_qscale_diff = 0;
1662  tmp = pps->init_qp + get_se_golomb(&sl->gb);
1663  if (tmp > 51 + 6 * (sps->bit_depth_luma - 8)) {
1664  av_log(avctx, AV_LOG_ERROR, "QP %u out of range\n", tmp);
1665  return AVERROR_INVALIDDATA;
1666  }
1667  sl->qscale = tmp;
1668  sl->chroma_qp[0] = get_chroma_qp(pps, 0, sl->qscale);
1669  sl->chroma_qp[1] = get_chroma_qp(pps, 1, sl->qscale);
1670  // FIXME qscale / qp ... stuff
1671  if (sl->slice_type == AV_PICTURE_TYPE_SP)
1672  get_bits1(&sl->gb); /* sp_for_switch_flag */
1673  if (sl->slice_type == AV_PICTURE_TYPE_SP ||
1675  get_se_golomb(&sl->gb); /* slice_qs_delta */
1676 
1677  sl->deblocking_filter = 1;
1678  sl->slice_alpha_c0_offset = 0;
1679  sl->slice_beta_offset = 0;
1681  tmp = get_ue_golomb_31(&sl->gb);
1682  if (tmp > 2) {
1683  av_log(avctx, AV_LOG_ERROR,
1684  "deblocking_filter_idc %u out of range\n", tmp);
1685  return AVERROR_INVALIDDATA;
1686  }
1687  sl->deblocking_filter = tmp;
1688  if (sl->deblocking_filter < 2)
1689  sl->deblocking_filter ^= 1; // 1<->0
1690 
1691  if (sl->deblocking_filter) {
1692  sl->slice_alpha_c0_offset = get_se_golomb(&sl->gb) * 2;
1693  sl->slice_beta_offset = get_se_golomb(&sl->gb) * 2;
1694  if (sl->slice_alpha_c0_offset > 12 ||
1695  sl->slice_alpha_c0_offset < -12 ||
1696  sl->slice_beta_offset > 12 ||
1697  sl->slice_beta_offset < -12) {
1698  av_log(avctx, AV_LOG_ERROR,
1699  "deblocking filter parameters %d %d out of range\n",
1701  return AVERROR_INVALIDDATA;
1702  }
1703  }
1704  }
1705 
1706  return 0;
1707 }
1708 
1709 /* do all the per-slice initialization needed before we can start decoding the
1710  * actual MBs */
1712  const H2645NAL *nal)
1713 {
1714  int i, j, ret = 0;
1715 
1716  if (h->current_slice > 0) {
1717  if (h->ps.pps != (const PPS*)h->ps.pps_list[sl->pps_id]->data) {
1718  av_log(h->avctx, AV_LOG_ERROR, "PPS changed between slices\n");
1719  return AVERROR_INVALIDDATA;
1720  }
1721 
1722  if (h->picture_structure != sl->picture_structure ||
1723  h->droppable != (nal->ref_idc == 0)) {
1725  "Changing field mode (%d -> %d) between slices is not allowed\n",
1727  return AVERROR_INVALIDDATA;
1728  } else if (!h->cur_pic_ptr) {
1730  "unset cur_pic_ptr on slice %d\n",
1731  h->current_slice + 1);
1732  return AVERROR_INVALIDDATA;
1733  }
1734  }
1735 
1736  if (h->picture_idr && nal->type != H264_NAL_IDR_SLICE) {
1737  av_log(h->avctx, AV_LOG_ERROR, "Invalid mix of IDR and non-IDR slices\n");
1738  return AVERROR_INVALIDDATA;
1739  }
1740 
1741  assert(h->mb_num == h->mb_width * h->mb_height);
1742  if (sl->first_mb_addr << FIELD_OR_MBAFF_PICTURE(h) >= h->mb_num ||
1743  sl->first_mb_addr >= h->mb_num) {
1744  av_log(h->avctx, AV_LOG_ERROR, "first_mb_in_slice overflow\n");
1745  return AVERROR_INVALIDDATA;
1746  }
1747  sl->resync_mb_x = sl->mb_x = sl->first_mb_addr % h->mb_width;
1748  sl->resync_mb_y = sl->mb_y = (sl->first_mb_addr / h->mb_width) <<
1751  sl->resync_mb_y = sl->mb_y = sl->mb_y + 1;
1752  assert(sl->mb_y < h->mb_height);
1753 
1754  ret = ff_h264_build_ref_list(h, sl);
1755  if (ret < 0)
1756  return ret;
1757 
1758  if (h->ps.pps->weighted_bipred_idc == 2 &&
1760  implicit_weight_table(h, sl, -1);
1761  if (FRAME_MBAFF(h)) {
1762  implicit_weight_table(h, sl, 0);
1763  implicit_weight_table(h, sl, 1);
1764  }
1765  }
1766 
1770 
1771  if (h->avctx->skip_loop_filter >= AVDISCARD_ALL ||
1777  nal->ref_idc == 0))
1778  sl->deblocking_filter = 0;
1779 
1780  if (sl->deblocking_filter == 1 && h->nb_slice_ctx > 1) {
1781  if (h->avctx->flags2 & AV_CODEC_FLAG2_FAST) {
1782  /* Cheat slightly for speed:
1783  * Do not bother to deblock across slices. */
1784  sl->deblocking_filter = 2;
1785  } else {
1786  h->postpone_filter = 1;
1787  }
1788  }
1789  sl->qp_thresh = 15 -
1791  FFMAX3(0,
1792  h->ps.pps->chroma_qp_index_offset[0],
1793  h->ps.pps->chroma_qp_index_offset[1]) +
1794  6 * (h->ps.sps->bit_depth_luma - 8);
1795 
1796  sl->slice_num = ++h->current_slice;
1797  if (sl->slice_num >= MAX_SLICES) {
1799  "Too many slices, increase MAX_SLICES and recompile\n");
1800  }
1801 
1802  for (j = 0; j < 2; j++) {
1803  int id_list[16];
1804  int *ref2frm = h->ref2frm[sl->slice_num & (MAX_SLICES - 1)][j];
1805  for (i = 0; i < 16; i++) {
1806  id_list[i] = 60;
1807  if (j < sl->list_count && i < sl->ref_count[j] &&
1808  sl->ref_list[j][i].parent->f->buf[0]) {
1809  int k;
1810  AVBuffer *buf = sl->ref_list[j][i].parent->f->buf[0]->buffer;
1811  for (k = 0; k < h->short_ref_count; k++)
1812  if (h->short_ref[k]->f->buf[0]->buffer == buf) {
1813  id_list[i] = k;
1814  break;
1815  }
1816  for (k = 0; k < h->long_ref_count; k++)
1817  if (h->long_ref[k] && h->long_ref[k]->f->buf[0]->buffer == buf) {
1818  id_list[i] = h->short_ref_count + k;
1819  break;
1820  }
1821  }
1822  }
1823 
1824  ref2frm[0] =
1825  ref2frm[1] = -1;
1826  for (i = 0; i < 16; i++)
1827  ref2frm[i + 2] = 4 * id_list[i] + (sl->ref_list[j][i].reference & 3);
1828  ref2frm[18 + 0] =
1829  ref2frm[18 + 1] = -1;
1830  for (i = 16; i < 48; i++)
1831  ref2frm[i + 4] = 4 * id_list[(i - 16) >> 1] +
1832  (sl->ref_list[j][i].reference & 3);
1833  }
1834 
1835  if (h->avctx->debug & FF_DEBUG_PICT_INFO) {
1837  "slice:%d %s mb:%d %c%s%s frame:%d poc:%d/%d ref:%d/%d qp:%d loop:%d:%d:%d weight:%d%s %s\n",
1838  sl->slice_num,
1839  (h->picture_structure == PICT_FRAME ? "F" : h->picture_structure == PICT_TOP_FIELD ? "T" : "B"),
1840  sl->mb_y * h->mb_width + sl->mb_x,
1842  sl->slice_type_fixed ? " fix" : "",
1843  nal->type == H264_NAL_IDR_SLICE ? " IDR" : "",
1844  h->poc.frame_num,
1845  h->cur_pic_ptr->field_poc[0],
1846  h->cur_pic_ptr->field_poc[1],
1847  sl->ref_count[0], sl->ref_count[1],
1848  sl->qscale,
1849  sl->deblocking_filter,
1851  sl->pwt.use_weight,
1852  sl->pwt.use_weight == 1 && sl->pwt.use_weight_chroma ? "c" : "",
1853  sl->slice_type == AV_PICTURE_TYPE_B ? (sl->direct_spatial_mv_pred ? "SPAT" : "TEMP") : "");
1854  }
1855 
1856  return 0;
1857 }
1858 
1860 {
1862  int ret;
1863 
1864  sl->gb = nal->gb;
1865 
1866  ret = h264_slice_header_parse(sl, nal, &h->ps, h->avctx);
1867  if (ret < 0)
1868  return ret;
1869 
1870  // discard redundant pictures
1871  if (sl->redundant_pic_count > 0)
1872  return 0;
1873 
1874  if (!h->setup_finished) {
1875  if (sl->first_mb_addr == 0) { // FIXME better field boundary detection
1876  // this slice starts a new field
1877  // first decode any pending queued slices
1878  if (h->nb_slice_ctx_queued) {
1879  H264SliceContext tmp_ctx;
1880 
1882  if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
1883  return ret;
1884 
1885  memcpy(&tmp_ctx, h->slice_ctx, sizeof(tmp_ctx));
1886  memcpy(h->slice_ctx, sl, sizeof(tmp_ctx));
1887  memcpy(sl, &tmp_ctx, sizeof(tmp_ctx));
1888  sl = h->slice_ctx;
1889  }
1890 
1891  if (h->field_started)
1892  ff_h264_field_end(h, sl, 1);
1893 
1894  h->current_slice = 0;
1895  if (!h->first_field) {
1896  if (h->cur_pic_ptr && !h->droppable) {
1899  }
1900  h->cur_pic_ptr = NULL;
1901  }
1902  }
1903 
1904  if (h->current_slice == 0) {
1905  ret = h264_field_start(h, sl, nal);
1906  if (ret < 0)
1907  return ret;
1908  h->field_started = 1;
1909  }
1910  }
1911 
1912  ret = h264_slice_init(h, sl, nal);
1913  if (ret < 0)
1914  return ret;
1915 
1916  if ((h->avctx->skip_frame < AVDISCARD_NONREF || nal->ref_idc) &&
1917  (h->avctx->skip_frame < AVDISCARD_BIDIR ||
1920  h->cur_pic_ptr->f->key_frame) &&
1921  h->avctx->skip_frame < AVDISCARD_ALL) {
1922  h->nb_slice_ctx_queued++;
1923  }
1924 
1925  return 0;
1926 }
1927 
1929 {
1930  switch (sl->slice_type) {
1931  case AV_PICTURE_TYPE_P:
1932  return 0;
1933  case AV_PICTURE_TYPE_B:
1934  return 1;
1935  case AV_PICTURE_TYPE_I:
1936  return 2;
1937  case AV_PICTURE_TYPE_SP:
1938  return 3;
1939  case AV_PICTURE_TYPE_SI:
1940  return 4;
1941  default:
1942  return AVERROR_INVALIDDATA;
1943  }
1944 }
1945 
1947  H264SliceContext *sl,
1948  int mb_type, int top_xy,
1949  int left_xy[LEFT_MBS],
1950  int top_type,
1951  int left_type[LEFT_MBS],
1952  int mb_xy, int list)
1953 {
1954  int b_stride = h->b_stride;
1955  int16_t(*mv_dst)[2] = &sl->mv_cache[list][scan8[0]];
1956  int8_t *ref_cache = &sl->ref_cache[list][scan8[0]];
1957  if (IS_INTER(mb_type) || IS_DIRECT(mb_type)) {
1958  if (USES_LIST(top_type, list)) {
1959  const int b_xy = h->mb2b_xy[top_xy] + 3 * b_stride;
1960  const int b8_xy = 4 * top_xy + 2;
1961  const int *ref2frm = &h->ref2frm[h->slice_table[top_xy] & (MAX_SLICES - 1)][list][(MB_MBAFF(sl) ? 20 : 2)];
1962  AV_COPY128(mv_dst - 1 * 8, h->cur_pic.motion_val[list][b_xy + 0]);
1963  ref_cache[0 - 1 * 8] =
1964  ref_cache[1 - 1 * 8] = ref2frm[h->cur_pic.ref_index[list][b8_xy + 0]];
1965  ref_cache[2 - 1 * 8] =
1966  ref_cache[3 - 1 * 8] = ref2frm[h->cur_pic.ref_index[list][b8_xy + 1]];
1967  } else {
1968  AV_ZERO128(mv_dst - 1 * 8);
1969  AV_WN32A(&ref_cache[0 - 1 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
1970  }
1971 
1972  if (!IS_INTERLACED(mb_type ^ left_type[LTOP])) {
1973  if (USES_LIST(left_type[LTOP], list)) {
1974  const int b_xy = h->mb2b_xy[left_xy[LTOP]] + 3;
1975  const int b8_xy = 4 * left_xy[LTOP] + 1;
1976  const int *ref2frm = &h->ref2frm[h->slice_table[left_xy[LTOP]] & (MAX_SLICES - 1)][list][(MB_MBAFF(sl) ? 20 : 2)];
1977  AV_COPY32(mv_dst - 1 + 0, h->cur_pic.motion_val[list][b_xy + b_stride * 0]);
1978  AV_COPY32(mv_dst - 1 + 8, h->cur_pic.motion_val[list][b_xy + b_stride * 1]);
1979  AV_COPY32(mv_dst - 1 + 16, h->cur_pic.motion_val[list][b_xy + b_stride * 2]);
1980  AV_COPY32(mv_dst - 1 + 24, h->cur_pic.motion_val[list][b_xy + b_stride * 3]);
1981  ref_cache[-1 + 0] =
1982  ref_cache[-1 + 8] = ref2frm[h->cur_pic.ref_index[list][b8_xy + 2 * 0]];
1983  ref_cache[-1 + 16] =
1984  ref_cache[-1 + 24] = ref2frm[h->cur_pic.ref_index[list][b8_xy + 2 * 1]];
1985  } else {
1986  AV_ZERO32(mv_dst - 1 + 0);
1987  AV_ZERO32(mv_dst - 1 + 8);
1988  AV_ZERO32(mv_dst - 1 + 16);
1989  AV_ZERO32(mv_dst - 1 + 24);
1990  ref_cache[-1 + 0] =
1991  ref_cache[-1 + 8] =
1992  ref_cache[-1 + 16] =
1993  ref_cache[-1 + 24] = LIST_NOT_USED;
1994  }
1995  }
1996  }
1997 
1998  if (!USES_LIST(mb_type, list)) {
1999  fill_rectangle(mv_dst, 4, 4, 8, pack16to32(0, 0), 4);
2000  AV_WN32A(&ref_cache[0 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
2001  AV_WN32A(&ref_cache[1 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
2002  AV_WN32A(&ref_cache[2 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
2003  AV_WN32A(&ref_cache[3 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
2004  return;
2005  }
2006 
2007  {
2008  int8_t *ref = &h->cur_pic.ref_index[list][4 * mb_xy];
2009  const int *ref2frm = &h->ref2frm[sl->slice_num & (MAX_SLICES - 1)][list][(MB_MBAFF(sl) ? 20 : 2)];
2010  uint32_t ref01 = (pack16to32(ref2frm[ref[0]], ref2frm[ref[1]]) & 0x00FF00FF) * 0x0101;
2011  uint32_t ref23 = (pack16to32(ref2frm[ref[2]], ref2frm[ref[3]]) & 0x00FF00FF) * 0x0101;
2012  AV_WN32A(&ref_cache[0 * 8], ref01);
2013  AV_WN32A(&ref_cache[1 * 8], ref01);
2014  AV_WN32A(&ref_cache[2 * 8], ref23);
2015  AV_WN32A(&ref_cache[3 * 8], ref23);
2016  }
2017 
2018  {
2019  int16_t(*mv_src)[2] = &h->cur_pic.motion_val[list][4 * sl->mb_x + 4 * sl->mb_y * b_stride];
2020  AV_COPY128(mv_dst + 8 * 0, mv_src + 0 * b_stride);
2021  AV_COPY128(mv_dst + 8 * 1, mv_src + 1 * b_stride);
2022  AV_COPY128(mv_dst + 8 * 2, mv_src + 2 * b_stride);
2023  AV_COPY128(mv_dst + 8 * 3, mv_src + 3 * b_stride);
2024  }
2025 }
2026 
2030 static int fill_filter_caches(const H264Context *h, H264SliceContext *sl, int mb_type)
2031 {
2032  const int mb_xy = sl->mb_xy;
2033  int top_xy, left_xy[LEFT_MBS];
2034  int top_type, left_type[LEFT_MBS];
2035  uint8_t *nnz;
2036  uint8_t *nnz_cache;
2037 
2038  top_xy = mb_xy - (h->mb_stride << MB_FIELD(sl));
2039 
2040  left_xy[LBOT] = left_xy[LTOP] = mb_xy - 1;
2041  if (FRAME_MBAFF(h)) {
2042  const int left_mb_field_flag = IS_INTERLACED(h->cur_pic.mb_type[mb_xy - 1]);
2043  const int curr_mb_field_flag = IS_INTERLACED(mb_type);
2044  if (sl->mb_y & 1) {
2045  if (left_mb_field_flag != curr_mb_field_flag)
2046  left_xy[LTOP] -= h->mb_stride;
2047  } else {
2048  if (curr_mb_field_flag)
2049  top_xy += h->mb_stride &
2050  (((h->cur_pic.mb_type[top_xy] >> 7) & 1) - 1);
2051  if (left_mb_field_flag != curr_mb_field_flag)
2052  left_xy[LBOT] += h->mb_stride;
2053  }
2054  }
2055 
2056  sl->top_mb_xy = top_xy;
2057  sl->left_mb_xy[LTOP] = left_xy[LTOP];
2058  sl->left_mb_xy[LBOT] = left_xy[LBOT];
2059  {
2060  /* For sufficiently low qp, filtering wouldn't do anything.
2061  * This is a conservative estimate: could also check beta_offset
2062  * and more accurate chroma_qp. */
2063  int qp_thresh = sl->qp_thresh; // FIXME strictly we should store qp_thresh for each mb of a slice
2064  int qp = h->cur_pic.qscale_table[mb_xy];
2065  if (qp <= qp_thresh &&
2066  (left_xy[LTOP] < 0 ||
2067  ((qp + h->cur_pic.qscale_table[left_xy[LTOP]] + 1) >> 1) <= qp_thresh) &&
2068  (top_xy < 0 ||
2069  ((qp + h->cur_pic.qscale_table[top_xy] + 1) >> 1) <= qp_thresh)) {
2070  if (!FRAME_MBAFF(h))
2071  return 1;
2072  if ((left_xy[LTOP] < 0 ||
2073  ((qp + h->cur_pic.qscale_table[left_xy[LBOT]] + 1) >> 1) <= qp_thresh) &&
2074  (top_xy < h->mb_stride ||
2075  ((qp + h->cur_pic.qscale_table[top_xy - h->mb_stride] + 1) >> 1) <= qp_thresh))
2076  return 1;
2077  }
2078  }
2079 
2080  top_type = h->cur_pic.mb_type[top_xy];
2081  left_type[LTOP] = h->cur_pic.mb_type[left_xy[LTOP]];
2082  left_type[LBOT] = h->cur_pic.mb_type[left_xy[LBOT]];
2083  if (sl->deblocking_filter == 2) {
2084  if (h->slice_table[top_xy] != sl->slice_num)
2085  top_type = 0;
2086  if (h->slice_table[left_xy[LBOT]] != sl->slice_num)
2087  left_type[LTOP] = left_type[LBOT] = 0;
2088  } else {
2089  if (h->slice_table[top_xy] == 0xFFFF)
2090  top_type = 0;
2091  if (h->slice_table[left_xy[LBOT]] == 0xFFFF)
2092  left_type[LTOP] = left_type[LBOT] = 0;
2093  }
2094  sl->top_type = top_type;
2095  sl->left_type[LTOP] = left_type[LTOP];
2096  sl->left_type[LBOT] = left_type[LBOT];
2097 
2098  if (IS_INTRA(mb_type))
2099  return 0;
2100 
2101  fill_filter_caches_inter(h, sl, mb_type, top_xy, left_xy,
2102  top_type, left_type, mb_xy, 0);
2103  if (sl->list_count == 2)
2104  fill_filter_caches_inter(h, sl, mb_type, top_xy, left_xy,
2105  top_type, left_type, mb_xy, 1);
2106 
2107  nnz = h->non_zero_count[mb_xy];
2108  nnz_cache = sl->non_zero_count_cache;
2109  AV_COPY32(&nnz_cache[4 + 8 * 1], &nnz[0]);
2110  AV_COPY32(&nnz_cache[4 + 8 * 2], &nnz[4]);
2111  AV_COPY32(&nnz_cache[4 + 8 * 3], &nnz[8]);
2112  AV_COPY32(&nnz_cache[4 + 8 * 4], &nnz[12]);
2113  sl->cbp = h->cbp_table[mb_xy];
2114 
2115  if (top_type) {
2116  nnz = h->non_zero_count[top_xy];
2117  AV_COPY32(&nnz_cache[4 + 8 * 0], &nnz[3 * 4]);
2118  }
2119 
2120  if (left_type[LTOP]) {
2121  nnz = h->non_zero_count[left_xy[LTOP]];
2122  nnz_cache[3 + 8 * 1] = nnz[3 + 0 * 4];
2123  nnz_cache[3 + 8 * 2] = nnz[3 + 1 * 4];
2124  nnz_cache[3 + 8 * 3] = nnz[3 + 2 * 4];
2125  nnz_cache[3 + 8 * 4] = nnz[3 + 3 * 4];
2126  }
2127 
2128  /* CAVLC 8x8dct requires NNZ values for residual decoding that differ
2129  * from what the loop filter needs */
2130  if (!CABAC(h) && h->ps.pps->transform_8x8_mode) {
2131  if (IS_8x8DCT(top_type)) {
2132  nnz_cache[4 + 8 * 0] =
2133  nnz_cache[5 + 8 * 0] = (h->cbp_table[top_xy] & 0x4000) >> 12;
2134  nnz_cache[6 + 8 * 0] =
2135  nnz_cache[7 + 8 * 0] = (h->cbp_table[top_xy] & 0x8000) >> 12;
2136  }
2137  if (IS_8x8DCT(left_type[LTOP])) {
2138  nnz_cache[3 + 8 * 1] =
2139  nnz_cache[3 + 8 * 2] = (h->cbp_table[left_xy[LTOP]] & 0x2000) >> 12; // FIXME check MBAFF
2140  }
2141  if (IS_8x8DCT(left_type[LBOT])) {
2142  nnz_cache[3 + 8 * 3] =
2143  nnz_cache[3 + 8 * 4] = (h->cbp_table[left_xy[LBOT]] & 0x8000) >> 12; // FIXME check MBAFF
2144  }
2145 
2146  if (IS_8x8DCT(mb_type)) {
2147  nnz_cache[scan8[0]] =
2148  nnz_cache[scan8[1]] =
2149  nnz_cache[scan8[2]] =
2150  nnz_cache[scan8[3]] = (sl->cbp & 0x1000) >> 12;
2151 
2152  nnz_cache[scan8[0 + 4]] =
2153  nnz_cache[scan8[1 + 4]] =
2154  nnz_cache[scan8[2 + 4]] =
2155  nnz_cache[scan8[3 + 4]] = (sl->cbp & 0x2000) >> 12;
2156 
2157  nnz_cache[scan8[0 + 8]] =
2158  nnz_cache[scan8[1 + 8]] =
2159  nnz_cache[scan8[2 + 8]] =
2160  nnz_cache[scan8[3 + 8]] = (sl->cbp & 0x4000) >> 12;
2161 
2162  nnz_cache[scan8[0 + 12]] =
2163  nnz_cache[scan8[1 + 12]] =
2164  nnz_cache[scan8[2 + 12]] =
2165  nnz_cache[scan8[3 + 12]] = (sl->cbp & 0x8000) >> 12;
2166  }
2167  }
2168 
2169  return 0;
2170 }
2171 
2172 static void loop_filter(const H264Context *h, H264SliceContext *sl, int start_x, int end_x)
2173 {
2174  uint8_t *dest_y, *dest_cb, *dest_cr;
2175  int linesize, uvlinesize, mb_x, mb_y;
2176  const int end_mb_y = sl->mb_y + FRAME_MBAFF(h);
2177  const int old_slice_type = sl->slice_type;
2178  const int pixel_shift = h->pixel_shift;
2179  const int block_h = 16 >> h->chroma_y_shift;
2180 
2181  if (h->postpone_filter)
2182  return;
2183 
2184  if (sl->deblocking_filter) {
2185  for (mb_x = start_x; mb_x < end_x; mb_x++)
2186  for (mb_y = end_mb_y - FRAME_MBAFF(h); mb_y <= end_mb_y; mb_y++) {
2187  int mb_xy, mb_type;
2188  mb_xy = sl->mb_xy = mb_x + mb_y * h->mb_stride;
2189  mb_type = h->cur_pic.mb_type[mb_xy];
2190 
2191  if (FRAME_MBAFF(h))
2192  sl->mb_mbaff =
2193  sl->mb_field_decoding_flag = !!IS_INTERLACED(mb_type);
2194 
2195  sl->mb_x = mb_x;
2196  sl->mb_y = mb_y;
2197  dest_y = h->cur_pic.f->data[0] +
2198  ((mb_x << pixel_shift) + mb_y * sl->linesize) * 16;
2199  dest_cb = h->cur_pic.f->data[1] +
2200  (mb_x << pixel_shift) * (8 << CHROMA444(h)) +
2201  mb_y * sl->uvlinesize * block_h;
2202  dest_cr = h->cur_pic.f->data[2] +
2203  (mb_x << pixel_shift) * (8 << CHROMA444(h)) +
2204  mb_y * sl->uvlinesize * block_h;
2205  // FIXME simplify above
2206 
2207  if (MB_FIELD(sl)) {
2208  linesize = sl->mb_linesize = sl->linesize * 2;
2209  uvlinesize = sl->mb_uvlinesize = sl->uvlinesize * 2;
2210  if (mb_y & 1) { // FIXME move out of this function?
2211  dest_y -= sl->linesize * 15;
2212  dest_cb -= sl->uvlinesize * (block_h - 1);
2213  dest_cr -= sl->uvlinesize * (block_h - 1);
2214  }
2215  } else {
2216  linesize = sl->mb_linesize = sl->linesize;
2217  uvlinesize = sl->mb_uvlinesize = sl->uvlinesize;
2218  }
2219  backup_mb_border(h, sl, dest_y, dest_cb, dest_cr, linesize,
2220  uvlinesize, 0);
2221  if (fill_filter_caches(h, sl, mb_type))
2222  continue;
2223  sl->chroma_qp[0] = get_chroma_qp(h->ps.pps, 0, h->cur_pic.qscale_table[mb_xy]);
2224  sl->chroma_qp[1] = get_chroma_qp(h->ps.pps, 1, h->cur_pic.qscale_table[mb_xy]);
2225 
2226  if (FRAME_MBAFF(h)) {
2227  ff_h264_filter_mb(h, sl, mb_x, mb_y, dest_y, dest_cb, dest_cr,
2228  linesize, uvlinesize);
2229  } else {
2230  ff_h264_filter_mb_fast(h, sl, mb_x, mb_y, dest_y, dest_cb,
2231  dest_cr, linesize, uvlinesize);
2232  }
2233  }
2234  }
2235  sl->slice_type = old_slice_type;
2236  sl->mb_x = end_x;
2237  sl->mb_y = end_mb_y - FRAME_MBAFF(h);
2238  sl->chroma_qp[0] = get_chroma_qp(h->ps.pps, 0, sl->qscale);
2239  sl->chroma_qp[1] = get_chroma_qp(h->ps.pps, 1, sl->qscale);
2240 }
2241 
2243 {
2244  const int mb_xy = sl->mb_x + sl->mb_y * h->mb_stride;
2245  int mb_type = (h->slice_table[mb_xy - 1] == sl->slice_num) ?
2246  h->cur_pic.mb_type[mb_xy - 1] :
2247  (h->slice_table[mb_xy - h->mb_stride] == sl->slice_num) ?
2248  h->cur_pic.mb_type[mb_xy - h->mb_stride] : 0;
2249  sl->mb_mbaff = sl->mb_field_decoding_flag = IS_INTERLACED(mb_type) ? 1 : 0;
2250 }
2251 
2256 {
2257  int top = 16 * (sl->mb_y >> FIELD_PICTURE(h));
2258  int pic_height = 16 * h->mb_height >> FIELD_PICTURE(h);
2259  int height = 16 << FRAME_MBAFF(h);
2260  int deblock_border = (16 + 4) << FRAME_MBAFF(h);
2261 
2262  if (sl->deblocking_filter) {
2263  if ((top + height) >= pic_height)
2264  height += deblock_border;
2265  top -= deblock_border;
2266  }
2267 
2268  if (top >= pic_height || (top + height) < 0)
2269  return;
2270 
2271  height = FFMIN(height, pic_height - top);
2272  if (top < 0) {
2273  height = top + height;
2274  top = 0;
2275  }
2276 
2277  ff_h264_draw_horiz_band(h, sl, top, height);
2278 
2279  if (h->droppable)
2280  return;
2281 
2282  ff_thread_report_progress(&h->cur_pic_ptr->tf, top + height - 1,
2284 }
2285 
2287  int startx, int starty,
2288  int endx, int endy, int status)
2289 {
2290 #if CONFIG_ERROR_RESILIENCE
2291  ERContext *er = &sl->er;
2292 
2293  if (!sl->h264->enable_er)
2294  return;
2295 
2296  er->ref_count = sl->ref_count[0];
2297  ff_er_add_slice(er, startx, starty, endx, endy, status);
2298 #endif
2299 }
2300 
2301 static int decode_slice(struct AVCodecContext *avctx, void *arg)
2302 {
2303  H264SliceContext *sl = arg;
2304  const H264Context *h = sl->h264;
2305  int lf_x_start = sl->mb_x;
2306  int orig_deblock = sl->deblocking_filter;
2307  int ret;
2308 
2309  sl->linesize = h->cur_pic_ptr->f->linesize[0];
2310  sl->uvlinesize = h->cur_pic_ptr->f->linesize[1];
2311 
2312  ret = alloc_scratch_buffers(sl, sl->linesize);
2313  if (ret < 0)
2314  return ret;
2315 
2316  sl->mb_skip_run = -1;
2317 
2318  if (h->postpone_filter)
2319  sl->deblocking_filter = 0;
2320 
2321  sl->is_complex = FRAME_MBAFF(h) || h->picture_structure != PICT_FRAME ||
2322  (CONFIG_GRAY && (h->flags & AV_CODEC_FLAG_GRAY));
2323 
2324  if (h->ps.pps->cabac) {
2325  /* realign */
2326  align_get_bits(&sl->gb);
2327 
2328  /* init cabac */
2330  sl->gb.buffer + get_bits_count(&sl->gb) / 8,
2331  (get_bits_left(&sl->gb) + 7) / 8);
2332 
2334 
2335  for (;;) {
2336  // START_TIMER
2337  int ret, eos;
2338 
2339  if (sl->mb_x + sl->mb_y * h->mb_width >= sl->next_slice_idx) {
2340  av_log(h->avctx, AV_LOG_ERROR, "Slice overlaps with next at %d\n",
2341  sl->next_slice_idx);
2342  return AVERROR_INVALIDDATA;
2343  }
2344 
2345  ret = ff_h264_decode_mb_cabac(h, sl);
2346  // STOP_TIMER("decode_mb_cabac")
2347 
2348  if (ret >= 0)
2349  ff_h264_hl_decode_mb(h, sl);
2350 
2351  // FIXME optimal? or let mb_decode decode 16x32 ?
2352  if (ret >= 0 && FRAME_MBAFF(h)) {
2353  sl->mb_y++;
2354 
2355  ret = ff_h264_decode_mb_cabac(h, sl);
2356 
2357  if (ret >= 0)
2358  ff_h264_hl_decode_mb(h, sl);
2359  sl->mb_y--;
2360  }
2361  eos = get_cabac_terminate(&sl->cabac);
2362 
2363  if ((h->workaround_bugs & FF_BUG_TRUNCATED) &&
2364  sl->cabac.bytestream > sl->cabac.bytestream_end + 2) {
2365  er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y, sl->mb_x - 1,
2366  sl->mb_y, ER_MB_END);
2367  if (sl->mb_x >= lf_x_start)
2368  loop_filter(h, sl, lf_x_start, sl->mb_x + 1);
2369  goto finish;
2370  }
2371  if (ret < 0 || sl->cabac.bytestream > sl->cabac.bytestream_end + 2) {
2373  "error while decoding MB %d %d, bytestream %td\n",
2374  sl->mb_x, sl->mb_y,
2375  sl->cabac.bytestream_end - sl->cabac.bytestream);
2376  er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y, sl->mb_x,
2377  sl->mb_y, ER_MB_ERROR);
2378  return AVERROR_INVALIDDATA;
2379  }
2380 
2381  if (++sl->mb_x >= h->mb_width) {
2382  loop_filter(h, sl, lf_x_start, sl->mb_x);
2383  sl->mb_x = lf_x_start = 0;
2384  decode_finish_row(h, sl);
2385  ++sl->mb_y;
2386  if (FIELD_OR_MBAFF_PICTURE(h)) {
2387  ++sl->mb_y;
2388  if (FRAME_MBAFF(h) && sl->mb_y < h->mb_height)
2390  }
2391  }
2392 
2393  if (eos || sl->mb_y >= h->mb_height) {
2394  ff_tlog(h->avctx, "slice end %d %d\n",
2395  get_bits_count(&sl->gb), sl->gb.size_in_bits);
2396  er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y, sl->mb_x - 1,
2397  sl->mb_y, ER_MB_END);
2398  if (sl->mb_x > lf_x_start)
2399  loop_filter(h, sl, lf_x_start, sl->mb_x);
2400  goto finish;
2401  }
2402  }
2403  } else {
2404  for (;;) {
2405  int ret;
2406 
2407  if (sl->mb_x + sl->mb_y * h->mb_width >= sl->next_slice_idx) {
2408  av_log(h->avctx, AV_LOG_ERROR, "Slice overlaps with next at %d\n",
2409  sl->next_slice_idx);
2410  return AVERROR_INVALIDDATA;
2411  }
2412 
2413  ret = ff_h264_decode_mb_cavlc(h, sl);
2414 
2415  if (ret >= 0)
2416  ff_h264_hl_decode_mb(h, sl);
2417 
2418  // FIXME optimal? or let mb_decode decode 16x32 ?
2419  if (ret >= 0 && FRAME_MBAFF(h)) {
2420  sl->mb_y++;
2421  ret = ff_h264_decode_mb_cavlc(h, sl);
2422 
2423  if (ret >= 0)
2424  ff_h264_hl_decode_mb(h, sl);
2425  sl->mb_y--;
2426  }
2427 
2428  if (ret < 0) {
2430  "error while decoding MB %d %d\n", sl->mb_x, sl->mb_y);
2431  er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y, sl->mb_x,
2432  sl->mb_y, ER_MB_ERROR);
2433  return ret;
2434  }
2435 
2436  if (++sl->mb_x >= h->mb_width) {
2437  loop_filter(h, sl, lf_x_start, sl->mb_x);
2438  sl->mb_x = lf_x_start = 0;
2439  decode_finish_row(h, sl);
2440  ++sl->mb_y;
2441  if (FIELD_OR_MBAFF_PICTURE(h)) {
2442  ++sl->mb_y;
2443  if (FRAME_MBAFF(h) && sl->mb_y < h->mb_height)
2445  }
2446  if (sl->mb_y >= h->mb_height) {
2447  ff_tlog(h->avctx, "slice end %d %d\n",
2448  get_bits_count(&sl->gb), sl->gb.size_in_bits);
2449 
2450  if (get_bits_left(&sl->gb) == 0) {
2451  er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y,
2452  sl->mb_x - 1, sl->mb_y, ER_MB_END);
2453 
2454  goto finish;
2455  } else {
2456  er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y,
2457  sl->mb_x - 1, sl->mb_y, ER_MB_END);
2458 
2459  return AVERROR_INVALIDDATA;
2460  }
2461  }
2462  }
2463 
2464  if (get_bits_left(&sl->gb) <= 0 && sl->mb_skip_run <= 0) {
2465  ff_tlog(h->avctx, "slice end %d %d\n",
2466  get_bits_count(&sl->gb), sl->gb.size_in_bits);
2467 
2468  if (get_bits_left(&sl->gb) == 0) {
2469  er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y,
2470  sl->mb_x - 1, sl->mb_y, ER_MB_END);
2471  if (sl->mb_x > lf_x_start)
2472  loop_filter(h, sl, lf_x_start, sl->mb_x);
2473 
2474  goto finish;
2475  } else {
2476  er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y, sl->mb_x,
2477  sl->mb_y, ER_MB_ERROR);
2478 
2479  return AVERROR_INVALIDDATA;
2480  }
2481  }
2482  }
2483  }
2484 
2485 finish:
2486  sl->deblocking_filter = orig_deblock;
2487  return 0;
2488 }
2489 
2496 {
2497  AVCodecContext *const avctx = h->avctx;
2498  H264SliceContext *sl;
2499  int context_count = h->nb_slice_ctx_queued;
2500  int ret = 0;
2501  int i, j;
2502 
2503  if (h->avctx->hwaccel || context_count < 1)
2504  return 0;
2505  if (context_count == 1) {
2506 
2507  h->slice_ctx[0].next_slice_idx = h->mb_width * h->mb_height;
2508  h->postpone_filter = 0;
2509 
2510  ret = decode_slice(avctx, &h->slice_ctx[0]);
2511  h->mb_y = h->slice_ctx[0].mb_y;
2512  if (ret < 0)
2513  goto finish;
2514  } else {
2515  for (i = 0; i < context_count; i++) {
2516  int next_slice_idx = h->mb_width * h->mb_height;
2517  int slice_idx;
2518 
2519  sl = &h->slice_ctx[i];
2520  sl->er.error_count = 0;
2521 
2522  /* make sure none of those slices overlap */
2523  slice_idx = sl->mb_y * h->mb_width + sl->mb_x;
2524  for (j = 0; j < context_count; j++) {
2525  H264SliceContext *sl2 = &h->slice_ctx[j];
2526  int slice_idx2 = sl2->mb_y * h->mb_width + sl2->mb_x;
2527 
2528  if (i == j || slice_idx2 < slice_idx)
2529  continue;
2530  next_slice_idx = FFMIN(next_slice_idx, slice_idx2);
2531  }
2532  sl->next_slice_idx = next_slice_idx;
2533  }
2534 
2535  avctx->execute(avctx, decode_slice, h->slice_ctx,
2536  NULL, context_count, sizeof(h->slice_ctx[0]));
2537 
2538  /* pull back stuff from slices to master context */
2539  sl = &h->slice_ctx[context_count - 1];
2540  h->mb_y = sl->mb_y;
2541  for (i = 1; i < context_count; i++)
2543 
2544  if (h->postpone_filter) {
2545  h->postpone_filter = 0;
2546 
2547  for (i = 0; i < context_count; i++) {
2548  int y_end, x_end;
2549 
2550  sl = &h->slice_ctx[i];
2551  y_end = FFMIN(sl->mb_y + 1, h->mb_height);
2552  x_end = (sl->mb_y >= h->mb_height) ? h->mb_width : sl->mb_x;
2553 
2554  for (j = sl->resync_mb_y; j < y_end; j += 1 + FIELD_OR_MBAFF_PICTURE(h)) {
2555  sl->mb_y = j;
2556  loop_filter(h, sl, j > sl->resync_mb_y ? 0 : sl->resync_mb_x,
2557  j == y_end - 1 ? x_end : h->mb_width);
2558  }
2559  }
2560  }
2561  }
2562 
2563 finish:
2564  h->nb_slice_ctx_queued = 0;
2565  return ret;
2566 }
int chroma_format_idc
Definition: h264_ps.h:47
int video_signal_type_present_flag
Definition: h264_ps.h:73
struct H264Context * h264
Definition: h264dec.h:175
#define ff_tlog(ctx,...)
Definition: internal.h:66
const struct AVCodec * codec
Definition: avcodec.h:1418
AVRational framerate
Definition: avcodec.h:3063
discard all frames except keyframes
Definition: avcodec.h:688
int nb_mmco
Definition: h264dec.h:460
int workaround_bugs
Definition: h264dec.h:363
int long_ref
1->long term reference 0->short term reference
Definition: h264dec.h:153
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
Views are packed per line, as if interlaced.
Definition: stereo3d.h:97
int ff_h264_queue_decode_slice(H264Context *h, const H2645NAL *nal)
Submit a slice for decoding.
Definition: h264_slice.c:1859
H264POCContext poc
Definition: h264dec.h:448
int mb_num
Definition: h264dec.h:427
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it...
Definition: buffer.c:106
static int h264_slice_header_parse(H264SliceContext *sl, const H2645NAL *nal, const H264ParamSets *ps, AVCodecContext *avctx)
Definition: h264_slice.c:1516
int mb_aff_frame
Definition: h264dec.h:397
int recovery_frame_cnt
recovery_frame_cnt
Definition: h264_sei.h:95
int16_t mv_cache[2][5 *8][2]
Motion vector cache.
Definition: h264dec.h:296
Views are alternated temporally.
Definition: stereo3d.h:66
static int initialize_cur_frame(H264Context *h)
Definition: h264_slice.c:261
static int get_se_golomb(GetBitContext *gb)
read signed exp golomb code.
Definition: golomb.h:179
int edge_emu_buffer_allocated
Definition: h264dec.h:284
int coded_width
Bitstream width / height, may be different from width/height e.g.
Definition: avcodec.h:1595
static void decode_finish_row(const H264Context *h, H264SliceContext *sl)
Draw edges and report progress for the last MB row.
Definition: h264_slice.c:2255
Definition: vf_drawbox.c:37
int first_field
Definition: h264dec.h:399
const uint8_t ff_zigzag_scan[16]
Definition: mathtables.c:126
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:64
misc image utilities
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:228
Views are next to each other, but when upscaling apply a checkerboard pattern.
Definition: stereo3d.h:87
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:130
#define ER_MB_END
AVFrame * f
Definition: thread.h:36
int weighted_bipred_idc
Definition: h264_ps.h:114
int left_mb_xy[LEFT_MBS]
Definition: h264dec.h:208
int chroma_qp_index_offset[2]
Definition: h264_ps.h:117
AVBufferRef * sps_list[MAX_SPS_COUNT]
Definition: h264_ps.h:134
const uint8_t * bytestream_end
Definition: cabac.h:48
static av_always_inline int get_chroma_qp(const PPS *pps, int t, int qscale)
Get the chroma qp.
Definition: h264dec.h:659
AVBufferRef * buf[AV_NUM_DATA_POINTERS]
AVBuffer references backing the data for this frame.
Definition: frame.h:308
static const uint8_t zigzag_scan8x8_cavlc[64]
Definition: h264_slice.c:96
H264ChromaContext h264chroma
Definition: h264dec.h:339
uint16_t * cbp_table
Definition: h264dec.h:404
int luma_weight_flag[2]
7.4.3.2 luma_weight_lX_flag
Definition: h264_parse.h:35
MMCO mmco[MAX_MMCO_COUNT]
memory management control operations buffer.
Definition: h264dec.h:459
static void implicit_weight_table(const H264Context *h, H264SliceContext *sl, int field)
Initialize implicit_weight table.
Definition: h264_slice.c:616
int ff_h264_pred_weight_table(GetBitContext *gb, const SPS *sps, const int *ref_count, int slice_type_nos, H264PredWeightTable *pwt)
Definition: h264_parse.c:27
Sequence parameter set.
Definition: h264_ps.h:43
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: avcodec.h:2127
int mb_y
Definition: h264dec.h:424
int coded_picture_number
Definition: h264dec.h:359
int bitstream_restriction_flag
Definition: h264_ps.h:84
planar GBR 4:4:4 24bpp
Definition: pixfmt.h:162
int num
numerator
Definition: rational.h:44
AVBufferRef * mb_type_buf
Definition: h264dec.h:137
int repeat_pict
When decoding, this signals how much the picture must be delayed.
Definition: frame.h:258
int bipred_scratchpad_allocated
Definition: h264dec.h:283
av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (%s)\, len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic ? ac->func_descr_generic :ac->func_descr)
#define AV_PIX_FMT_GBRP10
Definition: pixfmt.h:271
AVBufferPool * mb_type_pool
Definition: h264dec.h:525
int ff_h264_init_poc(int pic_field_poc[2], int *pic_poc, const SPS *sps, H264POCContext *pc, int picture_structure, int nal_ref_idc)
Definition: h264_parse.c:240
int chroma_x_shift
Definition: h264dec.h:356
const uint8_t * buffer
Definition: get_bits.h:55
Picture parameter set.
Definition: h264_ps.h:106
int16_t(*[2] motion_val)[2]
Definition: h264dec.h:135
int flags
Definition: h264dec.h:362
void ff_h264_flush_change(H264Context *h)
Definition: h264dec.c:442
const uint8_t * field_scan8x8_q0
Definition: h264dec.h:421
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1621
int frame_mbs_only_flag
Definition: h264_ps.h:61
int mb_height
Definition: h264dec.h:425
H264Picture * delayed_pic[MAX_DELAYED_PIC_COUNT+2]
Definition: h264dec.h:452
int is_avc
Used to parse AVC variant of H.264.
Definition: h264dec.h:438
3: top field, bottom field, in that order
Definition: h264_sei.h:44
AVBufferPool * ref_index_pool
Definition: h264dec.h:527
uint8_t zigzag_scan8x8_cavlc[64]
Definition: h264dec.h:413
av_cold void ff_h264chroma_init(H264ChromaContext *c, int bit_depth)
Definition: h264chroma.c:39
H264SEIDisplayOrientation display_orientation
Definition: h264_sei.h:124
int next_slice_idx
Definition: h264dec.h:233
#define FF_ARRAY_ELEMS(a)
H264Context.
Definition: h264dec.h:334
discard all
Definition: avcodec.h:689
AVFrame * f
Definition: h264dec.h:128
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:2776
uint32_t num_units_in_tick
Definition: h264_ps.h:80
void ff_thread_await_progress(ThreadFrame *f, int n, int field)
Wait for earlier decoding threads to finish reference pictures.
#define MB_FIELD(h)
Definition: h264dec.h:72
H264Picture * long_ref[32]
Definition: h264dec.h:451
8: frame tripling
Definition: h264_sei.h:49
int profile
profile
Definition: avcodec.h:2880
int picture_structure
Definition: h264dec.h:398
order of coefficients is actually GBR, also IEC 61966-2-1 (sRGB)
Definition: pixfmt.h:344
#define AV_WN32A(p, v)
Definition: intreadwrite.h:469
#define AV_COPY32(d, s)
Definition: intreadwrite.h:517
unsigned int ref_count[2]
num_ref_idx_l0/1_active_minus1 + 1
Definition: h264dec.h:264
#define IN_RANGE(a, b, size)
Definition: h264_slice.c:289
static const uint8_t field_scan8x8_cavlc[64]
Definition: h264_slice.c:76
#define REBASE_PICTURE(pic, new_ctx, old_ctx)
Definition: h264_slice.c:291
MMCO mmco[MAX_MMCO_COUNT]
Definition: h264dec.h:319
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:202
int ff_h264_update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
Definition: h264_slice.c:313
Switching Intra.
Definition: avutil.h:264
int setup_finished
Definition: h264dec.h:511
enum AVDiscard skip_frame
Definition: avcodec.h:2989
int ff_h264_execute_decode_slices(H264Context *h)
Call decode_slice() for each context.
Definition: h264_slice.c:2495
H264SEIContext sei
Definition: h264dec.h:522
struct AVHWAccel * hwaccel
Hardware accelerator in use.
Definition: avcodec.h:2696
unsigned int crop_top
frame_cropping_rect_top_offset
Definition: h264_ps.h:69
#define USES_LIST(a, list)
Definition: mpegutils.h:101
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
const uint8_t * bytestream
Definition: cabac.h:47
int ref2frm[MAX_SLICES][2][64]
reference to frame number lists, used in the loop filter, the first 2 are for -2,-1 ...
Definition: h264dec.h:528
int deblocking_filter_parameters_present
deblocking_filter_parameters_present_flag
Definition: h264_ps.h:118
void void avpriv_request_sample(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
const PPS * pps
Definition: h264_ps.h:138
uint8_t
int full_range
Definition: h264_ps.h:74
unsigned int crop_left
frame_cropping_rect_left_offset
Definition: h264_ps.h:67
#define MB_MBAFF(h)
Definition: h264dec.h:71
int slice_alpha_c0_offset
Definition: h264dec.h:191
Stereo 3D type: this structure describes how two videos are packed within a single video surface...
Definition: stereo3d.h:123
int poc
Definition: h264dec.h:168
int field_picture
whether or not picture was encoded in separate fields
Definition: h264dec.h:157
int bit_depth_chroma
bit_depth_chroma_minus8 + 8
Definition: h264_ps.h:98
void ff_h264_hl_decode_mb(const H264Context *h, H264SliceContext *sl)
Definition: h264_mb.c:798
#define FF_DEBUG_PICT_INFO
Definition: avcodec.h:2627
enum AVColorPrimaries color_primaries
Definition: h264_ps.h:76
int poc
frame POC
Definition: h264dec.h:147
int frame_num_offset
for POC type 2
Definition: h264_parse.h:51
int chroma_weight_flag[2]
7.4.3.2 chroma_weight_lX_flag
Definition: h264_parse.h:36
Multithreading support functions.
#define ER_MB_ERROR
int cabac
entropy_coding_mode_flag
Definition: h264_ps.h:108
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
Definition: frame.c:199
AVFrame * output_frame
Definition: h264dec.h:518
unsigned int crop_right
frame_cropping_rect_right_offset
Definition: h264_ps.h:68
#define AV_PIX_FMT_GBRP9
Definition: pixfmt.h:270
int frame_recovered
Initial frame has been completely recovered.
Definition: h264dec.h:505
int height
Definition: h264dec.h:355
#define PICT_BOTTOM_FIELD
Definition: mpegutils.h:38
#define MAX_PPS_COUNT
Definition: h264_ps.h:38
int pt
Definition: rtp.c:34
int transform_bypass
qpprime_y_zero_transform_bypass_flag
Definition: h264_ps.h:48
static void finish(void)
Definition: movenc.c:338
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:182
void ff_h264_filter_mb(const H264Context *h, H264SliceContext *sl, int mb_x, int mb_y, uint8_t *img_y, uint8_t *img_cb, uint8_t *img_cr, unsigned int linesize, unsigned int uvlinesize)
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:50
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV422P and setting col...
Definition: pixfmt.h:72
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:146
int redundant_pic_cnt_present
redundant_pic_cnt_present_flag
Definition: h264_ps.h:120
int picture_structure
Definition: h264dec.h:237
int chroma_y_shift
Definition: h264dec.h:356
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:140
int interlaced_frame
The content of the picture is interlaced.
Definition: frame.h:263
AVBufferRef * qscale_table_buf
Definition: h264dec.h:131
static int h264_export_frame_props(H264Context *h)
Definition: h264_slice.c:992
H264Picture * parent
Definition: h264dec.h:171
#define FRAME_RECOVERED_SEI
Sufficient number of frames have been decoded since a SEI recovery point, so all the following frames...
Definition: h264dec.h:503
H264SEIAFD afd
Definition: h264_sei.h:118
#define AV_CODEC_FLAG_GRAY
Only decode/encode grayscale.
Definition: avcodec.h:763
high precision timer, useful to profile code
int recovered
picture at IDR or recovery point + recovery count
Definition: h264dec.h:160
Active Format Description data consisting of a single byte as specified in ETSI TS 101 154 using enum...
Definition: frame.h:88
#define AV_COPY64(d, s)
Definition: intreadwrite.h:521
int ff_h264_decode_ref_pic_list_reordering(H264SliceContext *sl, void *logctx)
Definition: h264_refs.c:370
#define FFALIGN(x, a)
Definition: macros.h:48
int chroma_qp[2]
Definition: h264dec.h:185
7: frame doubling
Definition: h264_sei.h:48
int last_pocs[MAX_DELAYED_PIC_COUNT]
Definition: h264dec.h:453
void ff_h264_direct_ref_list_init(const H264Context *const h, H264SliceContext *sl)
Definition: h264_direct.c:111
int width
Definition: h264dec.h:355
static int h264_frame_start(H264Context *h)
Definition: h264_slice.c:449
const uint8_t * zigzag_scan8x8_cavlc_q0
Definition: h264dec.h:419
int a53_caption_size
Definition: h264_sei.h:79
H.264 common definitions.
void ff_h264_draw_horiz_band(const H264Context *h, H264SliceContext *sl, int y, int height)
Definition: h264dec.c:85
#define src
Definition: vp8dsp.c:254
#define HWACCEL_MAX
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:526
H.264 parameter set handling.
H264Picture DPB[H264_MAX_PICTURE_COUNT]
Definition: h264dec.h:342
enum AVColorTransferCharacteristic color_trc
Definition: h264_ps.h:77
int mb_aff
mb_adaptive_frame_field_flag
Definition: h264_ps.h:62
H264PredContext hpc
Definition: h264dec.h:377
int chroma_log2_weight_denom
Definition: h264_parse.h:34
int width
width and height of the video frame
Definition: frame.h:179
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:124
int has_b_frames
Size of the frame reordering buffer in the decoder.
Definition: avcodec.h:1715
int flags
Additional information about the frame packing.
Definition: stereo3d.h:132
static int get_ue_golomb(GetBitContext *gb)
read unsigned exp golomb code.
Definition: golomb.h:53
const uint8_t * zigzag_scan_q0
Definition: h264dec.h:417
static int alloc_scratch_buffers(H264SliceContext *sl, int linesize)
Definition: h264_slice.c:128
int poc_type
pic_order_cnt_type
Definition: h264_ps.h:50
void ff_er_add_slice(ERContext *s, int startx, int starty, int endx, int endy, int status)
Add a slice.
int context_initialized
Definition: h264dec.h:361
ERContext er
Definition: h264dec.h:177
int ff_h264_decode_ref_pic_marking(H264SliceContext *sl, GetBitContext *gb, const H2645NAL *nal, void *logctx)
Definition: h264_refs.c:738
int ff_h264_get_profile(const SPS *sps)
Compute profile from profile_idc and constraint_set?_flags.
Definition: h264_parse.c:470
int num_reorder_frames
Definition: h264_ps.h:85
discard all bidirectional frames
Definition: avcodec.h:687
#define AVERROR(e)
Definition: error.h:43
void * hwaccel_picture_private
hardware accelerator private data
Definition: h264dec.h:141
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:1793
int active_thread_type
Which multithreading methods are in use by the codec.
Definition: avcodec.h:2825
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:145
ATSC A53 Part 4 Closed Captions.
Definition: frame.h:57
#define FIELD_PICTURE(h)
Definition: h264dec.h:74
int picture_idr
Definition: h264dec.h:374
static int init_dimensions(H264Context *h)
Definition: h264_slice.c:797
int deblocking_filter
disable_deblocking_filter_idc with 1 <-> 0
Definition: h264dec.h:190
uint8_t(*[2] mvd_table)[2]
Definition: h264dec.h:408
int prev_interlaced_frame
Complement sei_pic_struct SEI_PIC_STRUCT_TOP_BOTTOM and SEI_PIC_STRUCT_BOTTOM_TOP indicate interlaced...
Definition: h264dec.h:484
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:1503
static int fill_filter_caches(const H264Context *h, H264SliceContext *sl, int mb_type)
Definition: h264_slice.c:2030
ThreadFrame tf
Definition: h264dec.h:129
simple assert() macros that are a bit more flexible than ISO C assert().
uint8_t(*[2] top_borders)[(16 *3) *2]
Definition: h264dec.h:282
int weighted_pred
weighted_pred_flag
Definition: h264_ps.h:113
#define PICT_TOP_FIELD
Definition: mpegutils.h:37
H264QpelContext h264qpel
Definition: h264dec.h:340
#define AV_PIX_FMT_YUV444P10
Definition: pixfmt.h:265
int direct_spatial_mv_pred
Definition: h264dec.h:248
H264SEIUnregistered unregistered
Definition: h264_sei.h:120
HW acceleration through VDA, data[3] contains a CVPixelBufferRef.
Definition: pixfmt.h:203
int frame_num
frame_num (raw frame_num from slice header)
Definition: h264dec.h:148
const uint8_t ff_h264_golomb_to_pict_type[5]
Definition: h264data.c:37
#define MAX_SLICES
Definition: dxva2_hevc.c:32
static const uint8_t field_scan8x8[64]
Definition: h264_slice.c:57
Video is not stereoscopic (and metadata has to be there).
Definition: stereo3d.h:35
int ff_h264_get_slice_type(const H264SliceContext *sl)
Reconstruct bitstream slice_type.
Definition: h264_slice.c:1928
#define FFMAX(a, b)
Definition: common.h:64
#define fail()
Definition: checkasm.h:80
uint8_t active_format_description
Definition: h264_sei.h:75
int delta_pic_order_always_zero_flag
Definition: h264_ps.h:52
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:268
int slice_type_nos
S free slice type (SI/SP are remapped to I/P)
Definition: h264dec.h:181
uint8_t zigzag_scan8x8[64]
Definition: h264dec.h:412
AVBufferRef * hwaccel_priv_buf
Definition: h264dec.h:140
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:63
int crop
frame_cropping_flag
Definition: h264_ps.h:64
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
Reduce a fraction.
Definition: rational.c:35
int nal_length_size
Number of bytes used for nal length (1, 2 or 4)
Definition: h264dec.h:439
useful rectangle filling function
#define AV_PIX_FMT_YUV422P9
Definition: pixfmt.h:261
int refs
number of reference frames
Definition: avcodec.h:2071
static const uint8_t field_scan[16]
Definition: h264_slice.c:50
av_cold void ff_videodsp_init(VideoDSPContext *ctx, int bpc)
Definition: videodsp.c:37
int prev_poc_msb
poc_msb of the last reference pic for POC type 0
Definition: h264_parse.h:49
AVBufferRef * motion_val_buf[2]
Definition: h264dec.h:134
int ref_frame_count
num_ref_frames
Definition: h264_ps.h:56
enum AVPixelFormat * pix_fmts
array of supported pixel formats, or NULL if unknown, array is terminated by -1
Definition: avcodec.h:3141
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:201
int flags
Frame flags, a combination of AV_FRAME_FLAGS.
Definition: frame.h:349
int err_recognition
Error recognition; may misdetect some more or less valid parts as errors.
Definition: avcodec.h:2670
int x264_build
Definition: h264dec.h:364
int ct_type
Bit set of clock types for fields/frames in picture timing SEI message.
Definition: h264_sei.h:60
#define FFMIN(a, b)
Definition: common.h:66
uint16_t * slice_table
slice_table_base + 2*mb_stride + 1
Definition: h264dec.h:394
static void copy_picture_range(H264Picture **to, H264Picture **from, int count, H264Context *new_base, H264Context *old_base)
Definition: h264_slice.c:296
uint8_t field_scan8x8_cavlc[64]
Definition: h264dec.h:416
#define IS_DIRECT(a)
Definition: mpegutils.h:86
CABACContext cabac
Cabac.
Definition: h264dec.h:315
int colour_description_present_flag
Definition: h264_ps.h:75
unsigned int first_mb_addr
Definition: h264dec.h:231
int reference
Definition: h264dec.h:159
static void er_add_slice(H264SliceContext *sl, int startx, int starty, int endx, int endy, int status)
Definition: h264_slice.c:2286
#define LEFT_MBS
Definition: h264dec.h:75
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV420P and setting col...
Definition: pixfmt.h:71
AVRational sar
Definition: h264_ps.h:72
int width
picture width / height.
Definition: avcodec.h:1580
int redundant_pic_count
Definition: h264dec.h:241
int nb_slice_ctx
Definition: h264dec.h:347
H264PredWeightTable pwt
Definition: h264dec.h:194
int field_started
Definition: h264dec.h:516
int long_ref_count
number of actual long term references
Definition: h264dec.h:464
uint32_t * mb_type
Definition: h264dec.h:138
#define AV_FRAME_FLAG_CORRUPT
The frame data may be corrupted, e.g.
Definition: frame.h:341
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
int32_t
#define AV_STEREO3D_FLAG_INVERT
Inverted views, Right/Bottom represents the left view.
Definition: stereo3d.h:114
int ff_h264_build_ref_list(const H264Context *h, H264SliceContext *sl)
Definition: h264_refs.c:256
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
Definition: avcodec.h:2106
static void fill_rectangle(SDL_Surface *screen, int x, int y, int w, int h, int color)
Definition: avplay.c:392
#define FFABS(a)
Definition: common.h:61
#define FF_THREAD_SLICE
Decode more than one part of a single frame at once.
Definition: avcodec.h:2818
Context Adaptive Binary Arithmetic Coder inline functions.
int level
level
Definition: avcodec.h:2970
int init_qp
pic_init_qp_minus26 + 26
Definition: h264_ps.h:115
H.264 / AVC / MPEG-4 part10 codec.
int mmco_reset
Definition: h264dec.h:461
H264SliceContext * slice_ctx
Definition: h264dec.h:346
static int h264_select_output_frame(H264Context *h)
Definition: h264_slice.c:1147
#define CONFIG_GRAY
Definition: config.h:399
#define AV_EF_EXPLODE
Definition: avcodec.h:2681
int reference
Definition: h264dec.h:167
int top_borders_allocated[2]
Definition: h264dec.h:285
static int pic_is_unused(H264Context *h, H264Picture *pic)
Definition: h264_slice.c:240
#define FIELD_OR_MBAFF_PICTURE(h)
Definition: h264dec.h:91
uint8_t * a53_caption
Definition: h264_sei.h:80
const uint8_t * zigzag_scan8x8_q0
Definition: h264dec.h:418
int ref_idc
H.264 only, nal_ref_idc.
Definition: h2645_parse.h:60
static void init_scan_tables(H264Context *h)
initialize scan tables
Definition: h264_slice.c:680
static int h264_init_ps(H264Context *h, const H264SliceContext *sl)
Definition: h264_slice.c:910
static int av_unused get_cabac_terminate(CABACContext *c)
#define AV_PIX_FMT_YUV444P9
Definition: pixfmt.h:262
#define DELAYED_PIC_REF
Value of Picture.reference when Picture is not a reference picture, but is held for delayed output...
Definition: mpegutils.h:45
int type
NAL unit type.
Definition: h2645_parse.h:50
2: bottom field
Definition: h264_sei.h:43
void av_display_rotation_set(int32_t matrix[9], double angle)
Initialize a transformation matrix describing a pure counterclockwise rotation by the specified angle...
Definition: display.c:52
av_cold void ff_h264_pred_init(H264PredContext *h, int codec_id, const int bit_depth, const int chroma_format_idc)
Set the intra prediction function pointers.
Definition: h264pred.c:402
the normal 2^n-1 "JPEG" YUV ranges
Definition: pixfmt.h:365
static int init_table_pools(H264Context *h)
Definition: h264_slice.c:160
uint8_t * edge_emu_buffer
Definition: h264dec.h:281
if(ac->has_optimized_func)
int ff_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
Select the (possibly hardware accelerated) pixel format.
Definition: utils.c:785
#define AVERROR_PATCHWELCOME
Not yet implemented in Libav, patches welcome.
Definition: error.h:57
int pic_order_present
pic_order_present_flag
Definition: h264_ps.h:109
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:191
int bit_depth_luma
luma bit depth from sps to detect changes
Definition: h264dec.h:441
int chroma_format_idc
chroma format from sps to detect changes
Definition: h264dec.h:442
VideoDSPContext vdsp
Definition: h264dec.h:337
int timing_info_present_flag
Definition: h264_ps.h:79
NULL
Definition: eval.c:55
static int width
Definition: utils.c:156
int coded_picture_number
picture number in bitstream order
Definition: frame.h:230
enum AVStereo3DType type
How views are packed within the video.
Definition: stereo3d.h:127
int mb_stride
Definition: h264dec.h:426
int postpone_filter
Definition: h264dec.h:369
#define IS_INTERLACED(a)
Definition: mpegutils.h:85
AVCodecContext * avctx
Definition: h264dec.h:336
Libavcodec external API header.
#define MAX_DELAYED_PIC_COUNT
Definition: h264dec.h:56
int last_qscale_diff
Definition: h264dec.h:187
This side data contains a 3x3 transformation matrix describing an affine transformation that needs to...
Definition: frame.h:83
AVBufferRef * pps_list[MAX_PPS_COUNT]
Definition: h264_ps.h:135
enum AVCodecID codec_id
Definition: avcodec.h:1426
static int get_ue_golomb_31(GetBitContext *gb)
read unsigned exp golomb code, constraint to a max of 31.
Definition: golomb.h:96
int delta_poc_bottom
Definition: h264_parse.h:46
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:158
H264Picture * short_ref[32]
Definition: h264dec.h:450
int next_outputed_poc
Definition: h264dec.h:454
int ff_h264_decode_mb_cabac(const H264Context *h, H264SliceContext *sl)
Decode a macroblock.
Definition: h264_cabac.c:1908
int explicit_ref_marking
Definition: h264dec.h:462
#define AV_CODEC_FLAG2_FAST
Allow non spec compliant speedup tricks.
Definition: avcodec.h:803
int field_poc[2]
top/bottom POC
Definition: h264dec.h:146
SEI_PicStructType pic_struct
Definition: h264_sei.h:53
int debug
debug
Definition: avcodec.h:2626
int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
Wrapper around get_buffer() for frame-multithreaded codecs.
int recovery_frame
recovery_frame is the frame_num at which the next frame should be fully constructed.
Definition: h264dec.h:492
main external API structure.
Definition: avcodec.h:1409
void av_display_matrix_flip(int32_t matrix[9], int hflip, int vflip)
Flip the input matrix horizontally and/or vertically.
Definition: display.c:67
int qp_thresh
QP threshold to skip loopfilter.
Definition: h264dec.h:186
int explicit_ref_marking
Definition: h264dec.h:321
uint8_t * data
The data buffer.
Definition: buffer.h:89
uint8_t * data
Definition: frame.h:109
static av_always_inline uint32_t pack16to32(int a, int b)
Definition: h264dec.h:638
H264SEIA53Caption a53_caption
Definition: h264_sei.h:119
int implicit_weight[48][48][2]
Definition: h264_parse.h:40
int8_t * qscale_table
Definition: h264dec.h:132
static const uint8_t scan8[16 *3+3]
Definition: h264dec.h:622
#define CABAC(h)
Definition: h264_cabac.c:28
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 unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:267
#define AV_PIX_FMT_YUV420P10
Definition: pixfmt.h:263
int coded_height
Definition: avcodec.h:1595
Switching Predicted.
Definition: avutil.h:265
int prev_frame_num
frame_num of the last pic for POC type 1/2
Definition: h264_parse.h:53
AVFrameSideData * av_frame_new_side_data(AVFrame *frame, enum AVFrameSideDataType type, int size)
Add a new side data to a frame.
Definition: frame.c:468
#define FF_COMPLIANCE_NORMAL
Definition: avcodec.h:2608
const uint8_t * field_scan8x8_cavlc_q0
Definition: h264dec.h:422
uint8_t non_zero_count_cache[15 *8]
non zero coeff count cache.
Definition: h264dec.h:291
5: top field, bottom field, top field repeated, in that order
Definition: h264_sei.h:46
void av_buffer_pool_uninit(AVBufferPool **ppool)
Mark the pool as being available for freeing.
Definition: buffer.c:254
#define FRAME_MBAFF(h)
Definition: h264dec.h:73
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:2120
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
Definition: avcodec.h:2113
#define LBOT
Definition: h264dec.h:77
HW acceleration through VDPAU, Picture.data[3] contains a VdpVideoSurface.
Definition: pixfmt.h:189
int8_t * ref_index[2]
Definition: h264dec.h:144
int ff_h264_ref_picture(H264Context *h, H264Picture *dst, H264Picture *src)
Definition: h264_picture.c:66
A reference counted buffer type.
int pixel_shift
0 for 8-bit H.264, 1 for high-bit-depth H.264
Definition: h264dec.h:352
int mmco_reset
MMCO_RESET set this 1.
Definition: h264dec.h:149
int content_interpretation_type
Definition: h264_sei.h:106
H264Picture * cur_pic_ptr
Definition: h264dec.h:343
#define LIST_NOT_USED
Definition: h264dec.h:381
const uint8_t ff_zigzag_direct[64]
Definition: mathtables.c:115
ptrdiff_t mb_uvlinesize
Definition: h264dec.h:225
static int h264_slice_header_init(H264Context *h)
Definition: h264_slice.c:835
int mb_mbaff
mb_aff_frame && mb_field_decoding_flag
Definition: h264dec.h:239
int enable_er
Definition: h264dec.h:520
#define AV_PIX_FMT_YUV420P9
Definition: pixfmt.h:260
#define IS_INTER(a)
Definition: mpegutils.h:81
unsigned int sps_id
Definition: h264_ps.h:107
#define TRANSPOSE(x)
H264SEIPictureTiming picture_timing
Definition: h264_sei.h:117
int log2_max_poc_lsb
log2_max_pic_order_cnt_lsb_minus4
Definition: h264_ps.h:51
H264SEIRecoveryPoint recovery_point
Definition: h264_sei.h:121
ptrdiff_t mb_linesize
may be equal to s->linesize or s->linesize * 2, for mbaff
Definition: h264dec.h:224
HW decoding through DXVA2, Picture.data[3] contains a LPDIRECT3DSURFACE9 pointer. ...
Definition: pixfmt.h:137
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:257
static int alloc_picture(H264Context *h, H264Picture *pic)
Definition: h264_slice.c:187
ptrdiff_t linesize
Definition: h264dec.h:223
Views are on top of each other.
Definition: stereo3d.h:55
int block_offset[2 *(16 *3)]
block_offset[ 0..23] for frame macroblocks block_offset[24..47] for field macroblocks ...
Definition: h264dec.h:388
uint32_t time_scale
Definition: h264_ps.h:81
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:302
int transform_8x8_mode
transform_8x8_mode_flag
Definition: h264_ps.h:121
ptrdiff_t uvlinesize
Definition: h264dec.h:223
#define AV_PIX_FMT_YUV422P10
Definition: pixfmt.h:264
static int h264_slice_init(H264Context *h, H264SliceContext *sl, const H2645NAL *nal)
Definition: h264_slice.c:1711
int pic_struct_present_flag
Definition: h264_ps.h:91
#define CHROMA444(h)
Definition: h264dec.h:98
AVStereo3D * av_stereo3d_create_side_data(AVFrame *frame)
Allocate a complete AVFrameSideData and add it to the frame.
Definition: stereo3d.c:33
unsigned int list_count
Definition: h264dec.h:265
uint8_t zigzag_scan[16]
Definition: h264dec.h:411
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:146
int prev_poc_lsb
poc_lsb of the last reference pic for POC type 0
Definition: h264_parse.h:50
static void release_unused_pictures(H264Context *h, int remove_current)
Definition: h264_slice.c:115
int ff_h264_alloc_tables(H264Context *h)
Allocate tables.
Definition: h264dec.c:163
#define AV_ZERO128(d)
Definition: intreadwrite.h:553
static int h264_field_start(H264Context *h, const H264SliceContext *sl, const H2645NAL *nal)
Definition: h264_slice.c:1285
the normal 219*2^(n-8) "MPEG" YUV ranges
Definition: pixfmt.h:364
int(* start_frame)(AVCodecContext *avctx, const uint8_t *buf, uint32_t buf_size)
Called at the beginning of each frame or field picture.
Definition: avcodec.h:3301
int height
Definition: gxfenc.c:72
Views are next to each other.
Definition: stereo3d.h:45
#define CONFIG_ERROR_RESILIENCE
Definition: config.h:424
int left_type[LEFT_MBS]
Definition: h264dec.h:213
hardware decoding through VDA
Definition: pixfmt.h:161
int nb_slice_ctx_queued
Definition: h264dec.h:348
discard all non reference
Definition: avcodec.h:686
int ff_h264_field_end(H264Context *h, H264SliceContext *sl, int in_setup)
Definition: h264_picture.c:145
AVBufferPool * qscale_table_pool
Definition: h264dec.h:524
int mb_height
Definition: h264_ps.h:60
AVBufferPool * motion_val_pool
Definition: h264dec.h:526
void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
Allocate a buffer, reusing the given one if large enough.
Definition: mem.c:394
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:59
int delta_poc_bottom
Definition: h264dec.h:325
#define IS_8x8DCT(a)
Definition: h264dec.h:103
common internal api header.
AVBufferPool * av_buffer_pool_init(int size, AVBufferRef *(*alloc)(int size))
Allocate and initialize a buffer pool.
Definition: buffer.c:217
#define AV_COPY128(d, s)
Definition: intreadwrite.h:525
int log2_max_frame_num
log2_max_frame_num_minus4 + 4
Definition: h264_ps.h:49
H264ParamSets ps
Definition: h264dec.h:444
H264SEIFramePacking frame_packing
Definition: h264_sei.h:123
H.264 / AVC / MPEG-4 part10 motion vector prediction.
AVBufferRef * av_buffer_ref(AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition: buffer.c:92
Bi-dir predicted.
Definition: avutil.h:262
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV444P and setting col...
Definition: pixfmt.h:73
int8_t * intra4x4_pred_mode
Definition: h264dec.h:203
enum AVDiscard skip_loop_filter
Definition: avcodec.h:2977
int den
denominator
Definition: rational.h:45
static void predict_field_decoding_flag(const H264Context *h, H264SliceContext *sl)
Definition: h264_slice.c:2242
int ff_h264_decode_mb_cavlc(const H264Context *h, H264SliceContext *sl)
Decode a macroblock.
Definition: h264_cavlc.c:699
GetBitContext gb
Definition: h2645_parse.h:45
int bit_depth_luma
bit_depth_luma_minus8 + 8
Definition: h264_ps.h:97
#define IS_INTRA(x, y)
int present
Definition: h264_sei.h:74
int delta_poc[2]
Definition: h264_parse.h:47
void ff_h264_free_tables(H264Context *h)
Definition: h264dec.c:120
void * priv_data
Definition: avcodec.h:1451
#define LTOP
Definition: h264dec.h:76
#define PICT_FRAME
Definition: mpegutils.h:39
4: bottom field, top field, in that order
Definition: h264_sei.h:45
static av_always_inline void backup_mb_border(const H264Context *h, H264SliceContext *sl, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr, int linesize, int uvlinesize, int simple)
Definition: h264_slice.c:514
int8_t ref_cache[2][5 *8]
Definition: h264dec.h:297
void ff_init_cabac_decoder(CABACContext *c, const uint8_t *buf, int buf_size)
Definition: cabac.c:163
#define AV_CODEC_FLAG_OUTPUT_CORRUPT
Output even those frames that might be corrupted.
Definition: avcodec.h:743
unsigned int pps_id
Definition: h264dec.h:275
int top_field_first
If the content is interlaced, is top field displayed first.
Definition: frame.h:268
int frame_priv_data_size
Size of per-frame hardware accelerator private data.
Definition: avcodec.h:3334
#define CHROMA422(h)
Definition: h264dec.h:97
#define FF_BUG_TRUNCATED
Definition: avcodec.h:2591
static uint8_t tmp[8]
Definition: des.c:38
Views are packed in a checkerboard-like structure per pixel.
Definition: stereo3d.h:76
H264Picture cur_pic
Definition: h264dec.h:344
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:196
#define AV_ZERO32(d)
Definition: intreadwrite.h:545
Views are packed per column.
Definition: stereo3d.h:107
int mb_width
Definition: h264dec.h:425
static int find_unused_picture(H264Context *h)
Definition: h264_slice.c:247
int current_slice
current slice number, used to initialize slice_num of each thread/context
Definition: h264dec.h:474
int ff_h264_execute_ref_pic_marking(H264Context *h)
Execute the reference picture marking (memory management control operations).
Definition: h264_refs.c:552
static const uint8_t * align_get_bits(GetBitContext *s)
Definition: get_bits.h:403
int mb_width
pic_width_in_mbs_minus1 + 1
Definition: h264_ps.h:58
int flags2
AV_CODEC_FLAG2_*.
Definition: avcodec.h:1510
uint32_t * mb2b_xy
Definition: h264dec.h:390
H264Ref ref_list[2][48]
0..15: frame refs, 16..47: mbaff field refs.
Definition: h264dec.h:266
#define HAVE_THREADS
Definition: config.h:321
AVBufferRef * ref_index_buf[2]
Definition: h264dec.h:143
const uint8_t * field_scan_q0
Definition: h264dec.h:420
SPS * sps
Definition: h264_ps.h:140
av_cold void ff_h264dsp_init(H264DSPContext *c, const int bit_depth, const int chroma_format_idc)
Definition: h264dsp.c:57
H264DSPContext h264dsp
Definition: h264dec.h:338
void ff_er_frame_start(ERContext *s)
int height
Definition: frame.h:179
FILE * out
Definition: movenc.c:54
1: top field
Definition: h264_sei.h:42
uint8_t(*[2] mvd_table)[2]
Definition: h264dec.h:310
uint8_t field_scan8x8[64]
Definition: h264dec.h:415
int slice_type_fixed
Definition: h264dec.h:182
static av_always_inline void fill_filter_caches_inter(const H264Context *h, H264SliceContext *sl, int mb_type, int top_xy, int left_xy[LEFT_MBS], int top_type, int left_type[LEFT_MBS], int mb_xy, int list)
Definition: h264_slice.c:1946
int prev_frame_num_offset
for POC type 2
Definition: h264_parse.h:52
#define av_always_inline
Definition: attributes.h:40
HW decoding through Direct3D11, Picture.data[3] contains a ID3D11VideoDecoderOutputView pointer...
Definition: pixfmt.h:222
int slice_beta_offset
Definition: h264dec.h:192
int8_t * intra4x4_pred_mode
Definition: h264dec.h:376
AVBufferRef * av_buffer_pool_get(AVBufferPool *pool)
Allocate a new AVBuffer, reusing an old buffer from the pool when available.
Definition: buffer.c:310
static int decode_slice(struct AVCodecContext *avctx, void *arg)
Definition: h264_slice.c:2301
int delta_poc[2]
Definition: h264dec.h:326
void ff_h264_direct_dist_scale_factor(const H264Context *const h, H264SliceContext *sl)
Definition: h264_direct.c:52
int ff_h264_slice_context_init(H264Context *h, H264SliceContext *sl)
Init context Allocate buffers which are not shared amongst multiple threads.
Definition: h264dec.c:221
int mb_field_decoding_flag
Definition: h264dec.h:238
static enum AVPixelFormat get_pixel_format(H264Context *h)
Definition: h264_slice.c:714
uint8_t(* non_zero_count)[48]
Definition: h264dec.h:379
static void loop_filter(const H264Context *h, H264SliceContext *sl, int start_x, int end_x)
Definition: h264_slice.c:2172
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:2846
unsigned int crop_bottom
frame_cropping_rect_bottom_offset
Definition: h264_ps.h:70
exp golomb vlc stuff
uint8_t * bipred_scratchpad
Definition: h264dec.h:280
void ff_h264_unref_picture(H264Context *h, H264Picture *pic)
Definition: h264_picture.c:45
AVPixelFormat
Pixel format.
Definition: pixfmt.h:57
av_cold void ff_h264qpel_init(H264QpelContext *c, int bit_depth)
Definition: h264qpel.c:37
#define AV_GET_BUFFER_FLAG_REF
The decoder will keep a reference to the frame and may reuse it later.
Definition: avcodec.h:1183
int droppable
Definition: h264dec.h:358
int level_idc
Definition: h264_ps.h:46
int ff_h264_parse_ref_count(int *plist_count, int ref_count[2], GetBitContext *gb, const PPS *pps, int slice_type_nos, int picture_structure)
Definition: h264_parse.c:191
int strict_std_compliance
strictly follow the standard (MPEG-4, ...).
Definition: avcodec.h:2605
void ff_h264_filter_mb_fast(const H264Context *h, H264SliceContext *sl, int mb_x, int mb_y, uint8_t *img_y, uint8_t *img_cb, uint8_t *img_cr, unsigned int linesize, unsigned int uvlinesize)
GetBitContext gb
Definition: h264dec.h:176
uint8_t field_scan[16]
Definition: h264dec.h:414
int cabac_init_idc
Definition: h264dec.h:317
#define FRAME_RECOVERED_IDR
We have seen an IDR, so all the following frames in coded order are correctly decodable.
Definition: h264dec.h:498
for(j=16;j >0;--j)
#define FFMAX3(a, b, c)
Definition: common.h:65
int b_stride
Definition: h264dec.h:392
6: bottom field, top field, bottom field repeated, in that order
Definition: h264_sei.h:47
Predicted.
Definition: avutil.h:261
Context Adaptive Binary Arithmetic Coder.
#define H264_MAX_PICTURE_COUNT
Definition: h264dec.h:52
void ff_h264_init_cabac_states(const H264Context *h, H264SliceContext *sl)
Definition: h264_cabac.c:1263
int short_ref_count
number of actual short term references
Definition: h264dec.h:465
enum AVColorSpace colorspace
Definition: h264_ps.h:78