FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
h264.c
Go to the documentation of this file.
1 /*
2  * H.26L/H.264/AVC/JVT/14496-10/... decoder
3  * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 /**
23  * @file
24  * H.264 / AVC / MPEG4 part10 codec.
25  * @author Michael Niedermayer <michaelni@gmx.at>
26  */
27 
28 #define UNCHECKED_BITSTREAM_READER 1
29 
30 #include "libavutil/avassert.h"
31 #include "libavutil/imgutils.h"
32 #include "libavutil/opt.h"
33 #include "libavutil/stereo3d.h"
34 #include "libavutil/timer.h"
35 #include "internal.h"
36 #include "cabac.h"
37 #include "cabac_functions.h"
38 #include "dsputil.h"
39 #include "error_resilience.h"
40 #include "avcodec.h"
41 #include "mpegvideo.h"
42 #include "h264.h"
43 #include "h264data.h"
44 #include "h264chroma.h"
45 #include "h264_mvpred.h"
46 #include "golomb.h"
47 #include "mathops.h"
48 #include "rectangle.h"
49 #include "svq3.h"
50 #include "thread.h"
51 #include "vdpau_internal.h"
52 
53 #include <assert.h>
54 
55 static void flush_change(H264Context *h);
56 
57 const uint16_t ff_h264_mb_sizes[4] = { 256, 384, 512, 768 };
58 
59 static const uint8_t rem6[QP_MAX_NUM + 1] = {
60  0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2,
61  3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5,
62  0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2,
63  3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5,
64  0, 1, 2, 3,
65 };
66 
67 static const uint8_t div6[QP_MAX_NUM + 1] = {
68  0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3,
69  3, 3, 3, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6,
70  7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 10, 10, 10,
71  10,10,10,11,11,11,11,11,11,12,12,12,12,12,12,13,13,13, 13, 13, 13,
72  14,14,14,14,
73 };
74 
75 static const uint8_t field_scan[16+1] = {
76  0 + 0 * 4, 0 + 1 * 4, 1 + 0 * 4, 0 + 2 * 4,
77  0 + 3 * 4, 1 + 1 * 4, 1 + 2 * 4, 1 + 3 * 4,
78  2 + 0 * 4, 2 + 1 * 4, 2 + 2 * 4, 2 + 3 * 4,
79  3 + 0 * 4, 3 + 1 * 4, 3 + 2 * 4, 3 + 3 * 4,
80 };
81 
82 static const uint8_t field_scan8x8[64+1] = {
83  0 + 0 * 8, 0 + 1 * 8, 0 + 2 * 8, 1 + 0 * 8,
84  1 + 1 * 8, 0 + 3 * 8, 0 + 4 * 8, 1 + 2 * 8,
85  2 + 0 * 8, 1 + 3 * 8, 0 + 5 * 8, 0 + 6 * 8,
86  0 + 7 * 8, 1 + 4 * 8, 2 + 1 * 8, 3 + 0 * 8,
87  2 + 2 * 8, 1 + 5 * 8, 1 + 6 * 8, 1 + 7 * 8,
88  2 + 3 * 8, 3 + 1 * 8, 4 + 0 * 8, 3 + 2 * 8,
89  2 + 4 * 8, 2 + 5 * 8, 2 + 6 * 8, 2 + 7 * 8,
90  3 + 3 * 8, 4 + 1 * 8, 5 + 0 * 8, 4 + 2 * 8,
91  3 + 4 * 8, 3 + 5 * 8, 3 + 6 * 8, 3 + 7 * 8,
92  4 + 3 * 8, 5 + 1 * 8, 6 + 0 * 8, 5 + 2 * 8,
93  4 + 4 * 8, 4 + 5 * 8, 4 + 6 * 8, 4 + 7 * 8,
94  5 + 3 * 8, 6 + 1 * 8, 6 + 2 * 8, 5 + 4 * 8,
95  5 + 5 * 8, 5 + 6 * 8, 5 + 7 * 8, 6 + 3 * 8,
96  7 + 0 * 8, 7 + 1 * 8, 6 + 4 * 8, 6 + 5 * 8,
97  6 + 6 * 8, 6 + 7 * 8, 7 + 2 * 8, 7 + 3 * 8,
98  7 + 4 * 8, 7 + 5 * 8, 7 + 6 * 8, 7 + 7 * 8,
99 };
100 
101 static const uint8_t field_scan8x8_cavlc[64+1] = {
102  0 + 0 * 8, 1 + 1 * 8, 2 + 0 * 8, 0 + 7 * 8,
103  2 + 2 * 8, 2 + 3 * 8, 2 + 4 * 8, 3 + 3 * 8,
104  3 + 4 * 8, 4 + 3 * 8, 4 + 4 * 8, 5 + 3 * 8,
105  5 + 5 * 8, 7 + 0 * 8, 6 + 6 * 8, 7 + 4 * 8,
106  0 + 1 * 8, 0 + 3 * 8, 1 + 3 * 8, 1 + 4 * 8,
107  1 + 5 * 8, 3 + 1 * 8, 2 + 5 * 8, 4 + 1 * 8,
108  3 + 5 * 8, 5 + 1 * 8, 4 + 5 * 8, 6 + 1 * 8,
109  5 + 6 * 8, 7 + 1 * 8, 6 + 7 * 8, 7 + 5 * 8,
110  0 + 2 * 8, 0 + 4 * 8, 0 + 5 * 8, 2 + 1 * 8,
111  1 + 6 * 8, 4 + 0 * 8, 2 + 6 * 8, 5 + 0 * 8,
112  3 + 6 * 8, 6 + 0 * 8, 4 + 6 * 8, 6 + 2 * 8,
113  5 + 7 * 8, 6 + 4 * 8, 7 + 2 * 8, 7 + 6 * 8,
114  1 + 0 * 8, 1 + 2 * 8, 0 + 6 * 8, 3 + 0 * 8,
115  1 + 7 * 8, 3 + 2 * 8, 2 + 7 * 8, 4 + 2 * 8,
116  3 + 7 * 8, 5 + 2 * 8, 4 + 7 * 8, 5 + 4 * 8,
117  6 + 3 * 8, 6 + 5 * 8, 7 + 3 * 8, 7 + 7 * 8,
118 };
119 
120 // zigzag_scan8x8_cavlc[i] = zigzag_scan8x8[(i/4) + 16*(i%4)]
121 static const uint8_t zigzag_scan8x8_cavlc[64+1] = {
122  0 + 0 * 8, 1 + 1 * 8, 1 + 2 * 8, 2 + 2 * 8,
123  4 + 1 * 8, 0 + 5 * 8, 3 + 3 * 8, 7 + 0 * 8,
124  3 + 4 * 8, 1 + 7 * 8, 5 + 3 * 8, 6 + 3 * 8,
125  2 + 7 * 8, 6 + 4 * 8, 5 + 6 * 8, 7 + 5 * 8,
126  1 + 0 * 8, 2 + 0 * 8, 0 + 3 * 8, 3 + 1 * 8,
127  3 + 2 * 8, 0 + 6 * 8, 4 + 2 * 8, 6 + 1 * 8,
128  2 + 5 * 8, 2 + 6 * 8, 6 + 2 * 8, 5 + 4 * 8,
129  3 + 7 * 8, 7 + 3 * 8, 4 + 7 * 8, 7 + 6 * 8,
130  0 + 1 * 8, 3 + 0 * 8, 0 + 4 * 8, 4 + 0 * 8,
131  2 + 3 * 8, 1 + 5 * 8, 5 + 1 * 8, 5 + 2 * 8,
132  1 + 6 * 8, 3 + 5 * 8, 7 + 1 * 8, 4 + 5 * 8,
133  4 + 6 * 8, 7 + 4 * 8, 5 + 7 * 8, 6 + 7 * 8,
134  0 + 2 * 8, 2 + 1 * 8, 1 + 3 * 8, 5 + 0 * 8,
135  1 + 4 * 8, 2 + 4 * 8, 6 + 0 * 8, 4 + 3 * 8,
136  0 + 7 * 8, 4 + 4 * 8, 7 + 2 * 8, 3 + 6 * 8,
137  5 + 5 * 8, 6 + 5 * 8, 6 + 6 * 8, 7 + 7 * 8,
138 };
139 
140 static const uint8_t dequant4_coeff_init[6][3] = {
141  { 10, 13, 16 },
142  { 11, 14, 18 },
143  { 13, 16, 20 },
144  { 14, 18, 23 },
145  { 16, 20, 25 },
146  { 18, 23, 29 },
147 };
148 
149 static const uint8_t dequant8_coeff_init_scan[16] = {
150  0, 3, 4, 3, 3, 1, 5, 1, 4, 5, 2, 5, 3, 1, 5, 1
151 };
152 
153 static const uint8_t dequant8_coeff_init[6][6] = {
154  { 20, 18, 32, 19, 25, 24 },
155  { 22, 19, 35, 21, 28, 26 },
156  { 26, 23, 42, 24, 33, 31 },
157  { 28, 25, 45, 26, 35, 33 },
158  { 32, 28, 51, 30, 40, 38 },
159  { 36, 32, 58, 34, 46, 43 },
160 };
161 
163 #if CONFIG_H264_DXVA2_HWACCEL
165 #endif
166 #if CONFIG_H264_VAAPI_HWACCEL
168 #endif
169 #if CONFIG_H264_VDA_HWACCEL
171 #endif
172 #if CONFIG_H264_VDPAU_HWACCEL
174 #endif
177 };
178 
180 #if CONFIG_H264_DXVA2_HWACCEL
182 #endif
183 #if CONFIG_H264_VAAPI_HWACCEL
185 #endif
186 #if CONFIG_H264_VDA_HWACCEL
188 #endif
189 #if CONFIG_H264_VDPAU_HWACCEL
191 #endif
194 };
195 
197 {
198  H264Context *h = avctx->priv_data;
199  return h ? h->sps.num_reorder_frames : 0;
200 }
201 
202 static void h264_er_decode_mb(void *opaque, int ref, int mv_dir, int mv_type,
203  int (*mv)[2][4][2],
204  int mb_x, int mb_y, int mb_intra, int mb_skipped)
205 {
206  H264Context *h = opaque;
207 
208  h->mb_x = mb_x;
209  h->mb_y = mb_y;
210  h->mb_xy = mb_x + mb_y * h->mb_stride;
211  memset(h->non_zero_count_cache, 0, sizeof(h->non_zero_count_cache));
212  av_assert1(ref >= 0);
213  /* FIXME: It is possible albeit uncommon that slice references
214  * differ between slices. We take the easy approach and ignore
215  * it for now. If this turns out to have any relevance in
216  * practice then correct remapping should be added. */
217  if (ref >= h->ref_count[0])
218  ref = 0;
219  if (!h->ref_list[0][ref].f.data[0]) {
220  av_log(h->avctx, AV_LOG_DEBUG, "Reference not available for error concealing\n");
221  ref = 0;
222  }
223  if ((h->ref_list[0][ref].reference&3) != 3) {
224  av_log(h->avctx, AV_LOG_DEBUG, "Reference invalid\n");
225  return;
226  }
227  fill_rectangle(&h->cur_pic.ref_index[0][4 * h->mb_xy],
228  2, 2, 2, ref, 1);
229  fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, ref, 1);
230  fill_rectangle(h->mv_cache[0][scan8[0]], 4, 4, 8,
231  pack16to32((*mv)[0][0][0], (*mv)[0][0][1]), 4);
232  h->mb_mbaff =
233  h->mb_field_decoding_flag = 0;
235 }
236 
238 {
239  AVCodecContext *avctx = h->avctx;
240  Picture *cur = &h->cur_pic;
241  Picture *last = h->ref_list[0][0].f.data[0] ? &h->ref_list[0][0] : NULL;
242  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
243  int vshift = desc->log2_chroma_h;
244  const int field_pic = h->picture_structure != PICT_FRAME;
245  if (field_pic) {
246  height <<= 1;
247  y <<= 1;
248  }
249 
250  height = FFMIN(height, avctx->height - y);
251 
252  if (field_pic && h->first_field && !(avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD))
253  return;
254 
255  if (avctx->draw_horiz_band) {
256  AVFrame *src;
258  int i;
259 
260  if (cur->f.pict_type == AV_PICTURE_TYPE_B || h->low_delay ||
262  src = &cur->f;
263  else if (last)
264  src = &last->f;
265  else
266  return;
267 
268  offset[0] = y * src->linesize[0];
269  offset[1] =
270  offset[2] = (y >> vshift) * src->linesize[1];
271  for (i = 3; i < AV_NUM_DATA_POINTERS; i++)
272  offset[i] = 0;
273 
274  emms_c();
275 
276  avctx->draw_horiz_band(avctx, src, offset,
277  y, h->picture_structure, height);
278  }
279 }
280 
281 static void unref_picture(H264Context *h, Picture *pic)
282 {
283  int off = offsetof(Picture, tf) + sizeof(pic->tf);
284  int i;
285 
286  if (!pic->f.buf[0])
287  return;
288 
289  ff_thread_release_buffer(h->avctx, &pic->tf);
291 
294  for (i = 0; i < 2; i++) {
296  av_buffer_unref(&pic->ref_index_buf[i]);
297  }
298 
299  memset((uint8_t*)pic + off, 0, sizeof(*pic) - off);
300 }
301 
302 static void release_unused_pictures(H264Context *h, int remove_current)
303 {
304  int i;
305 
306  /* release non reference frames */
307  for (i = 0; i < MAX_PICTURE_COUNT; i++) {
308  if (h->DPB[i].f.buf[0] && !h->DPB[i].reference &&
309  (remove_current || &h->DPB[i] != h->cur_pic_ptr)) {
310  unref_picture(h, &h->DPB[i]);
311  }
312  }
313 }
314 
315 static int ref_picture(H264Context *h, Picture *dst, Picture *src)
316 {
317  int ret, i;
318 
319  av_assert0(!dst->f.buf[0]);
320  av_assert0(src->f.buf[0]);
321 
322  src->tf.f = &src->f;
323  dst->tf.f = &dst->f;
324  ret = ff_thread_ref_frame(&dst->tf, &src->tf);
325  if (ret < 0)
326  goto fail;
327 
330  if (!dst->qscale_table_buf || !dst->mb_type_buf)
331  goto fail;
332  dst->qscale_table = src->qscale_table;
333  dst->mb_type = src->mb_type;
334 
335  for (i = 0; i < 2; i++) {
336  dst->motion_val_buf[i] = av_buffer_ref(src->motion_val_buf[i]);
337  dst->ref_index_buf[i] = av_buffer_ref(src->ref_index_buf[i]);
338  if (!dst->motion_val_buf[i] || !dst->ref_index_buf[i])
339  goto fail;
340  dst->motion_val[i] = src->motion_val[i];
341  dst->ref_index[i] = src->ref_index[i];
342  }
343 
344  if (src->hwaccel_picture_private) {
346  if (!dst->hwaccel_priv_buf)
347  goto fail;
349  }
350 
351  for (i = 0; i < 2; i++)
352  dst->field_poc[i] = src->field_poc[i];
353 
354  memcpy(dst->ref_poc, src->ref_poc, sizeof(src->ref_poc));
355  memcpy(dst->ref_count, src->ref_count, sizeof(src->ref_count));
356 
357  dst->poc = src->poc;
358  dst->frame_num = src->frame_num;
359  dst->mmco_reset = src->mmco_reset;
360  dst->pic_id = src->pic_id;
361  dst->long_ref = src->long_ref;
362  dst->mbaff = src->mbaff;
363  dst->field_picture = src->field_picture;
364  dst->needs_realloc = src->needs_realloc;
365  dst->reference = src->reference;
366  dst->crop = src->crop;
367  dst->crop_left = src->crop_left;
368  dst->crop_top = src->crop_top;
369  dst->recovered = src->recovered;
370  dst->invalid_gap = src->invalid_gap;
371 
372  return 0;
373 fail:
374  unref_picture(h, dst);
375  return ret;
376 }
377 
378 static int alloc_scratch_buffers(H264Context *h, int linesize)
379 {
380  int alloc_size = FFALIGN(FFABS(linesize) + 32, 32);
381 
382  if (h->bipred_scratchpad)
383  return 0;
384 
385  h->bipred_scratchpad = av_malloc(16 * 6 * alloc_size);
386  // edge emu needs blocksize + filter length - 1
387  // (= 21x21 for h264)
388  h->edge_emu_buffer = av_mallocz(alloc_size * 2 * 21);
389  h->me.scratchpad = av_mallocz(alloc_size * 2 * 16 * 2);
390 
391  if (!h->bipred_scratchpad || !h->edge_emu_buffer || !h->me.scratchpad) {
394  av_freep(&h->me.scratchpad);
395  return AVERROR(ENOMEM);
396  }
397 
398  h->me.temp = h->me.scratchpad;
399 
400  return 0;
401 }
402 
404 {
405  const int big_mb_num = h->mb_stride * (h->mb_height + 1) + 1;
406  const int mb_array_size = h->mb_stride * h->mb_height;
407  const int b4_stride = h->mb_width * 4 + 1;
408  const int b4_array_size = b4_stride * h->mb_height * 4;
409 
410  h->qscale_table_pool = av_buffer_pool_init(big_mb_num + h->mb_stride,
412  h->mb_type_pool = av_buffer_pool_init((big_mb_num + h->mb_stride) *
413  sizeof(uint32_t), av_buffer_allocz);
414  h->motion_val_pool = av_buffer_pool_init(2 * (b4_array_size + 4) *
415  sizeof(int16_t), av_buffer_allocz);
416  h->ref_index_pool = av_buffer_pool_init(4 * mb_array_size, av_buffer_allocz);
417 
418  if (!h->qscale_table_pool || !h->mb_type_pool || !h->motion_val_pool ||
419  !h->ref_index_pool) {
424  return AVERROR(ENOMEM);
425  }
426 
427  return 0;
428 }
429 
430 static int alloc_picture(H264Context *h, Picture *pic)
431 {
432  int i, ret = 0;
433 
434  av_assert0(!pic->f.data[0]);
435 
436  pic->tf.f = &pic->f;
437  ret = ff_thread_get_buffer(h->avctx, &pic->tf, pic->reference ?
439  if (ret < 0)
440  goto fail;
441 
442  h->linesize = pic->f.linesize[0];
443  h->uvlinesize = pic->f.linesize[1];
444  pic->crop = h->sps.crop;
445  pic->crop_top = h->sps.crop_top;
446  pic->crop_left= h->sps.crop_left;
447 
448  if (h->avctx->hwaccel) {
449  const AVHWAccel *hwaccel = h->avctx->hwaccel;
451  if (hwaccel->priv_data_size) {
453  if (!pic->hwaccel_priv_buf)
454  return AVERROR(ENOMEM);
456  }
457  }
458  if (!h->avctx->hwaccel && CONFIG_GRAY && h->flags & CODEC_FLAG_GRAY && pic->f.data[2]) {
459  int h_chroma_shift, v_chroma_shift;
461  &h_chroma_shift, &v_chroma_shift);
462 
463  for(i=0; i<FF_CEIL_RSHIFT(h->avctx->height, v_chroma_shift); i++) {
464  memset(pic->f.data[1] + pic->f.linesize[1]*i,
465  0x80, FF_CEIL_RSHIFT(h->avctx->width, h_chroma_shift));
466  memset(pic->f.data[2] + pic->f.linesize[2]*i,
467  0x80, FF_CEIL_RSHIFT(h->avctx->width, h_chroma_shift));
468  }
469  }
470 
471  if (!h->qscale_table_pool) {
472  ret = init_table_pools(h);
473  if (ret < 0)
474  goto fail;
475  }
476 
479  if (!pic->qscale_table_buf || !pic->mb_type_buf)
480  goto fail;
481 
482  pic->mb_type = (uint32_t*)pic->mb_type_buf->data + 2 * h->mb_stride + 1;
483  pic->qscale_table = pic->qscale_table_buf->data + 2 * h->mb_stride + 1;
484 
485  for (i = 0; i < 2; i++) {
488  if (!pic->motion_val_buf[i] || !pic->ref_index_buf[i])
489  goto fail;
490 
491  pic->motion_val[i] = (int16_t (*)[2])pic->motion_val_buf[i]->data + 4;
492  pic->ref_index[i] = pic->ref_index_buf[i]->data;
493  }
494 
495  return 0;
496 fail:
497  unref_picture(h, pic);
498  return (ret < 0) ? ret : AVERROR(ENOMEM);
499 }
500 
501 static inline int pic_is_unused(H264Context *h, Picture *pic)
502 {
503  if (!pic->f.buf[0])
504  return 1;
505  if (pic->needs_realloc && !(pic->reference & DELAYED_PIC_REF))
506  return 1;
507  return 0;
508 }
509 
511 {
512  int i;
513 
514  for (i = 0; i < MAX_PICTURE_COUNT; i++) {
515  if (pic_is_unused(h, &h->DPB[i]))
516  break;
517  }
518  if (i == MAX_PICTURE_COUNT)
519  return AVERROR_INVALIDDATA;
520 
521  if (h->DPB[i].needs_realloc) {
522  h->DPB[i].needs_realloc = 0;
523  unref_picture(h, &h->DPB[i]);
524  }
525 
526  return i;
527 }
528 
529 /**
530  * Check if the top & left blocks are available if needed and
531  * change the dc mode so it only uses the available blocks.
532  */
534 {
535  static const int8_t top[12] = {
536  -1, 0, LEFT_DC_PRED, -1, -1, -1, -1, -1, 0
537  };
538  static const int8_t left[12] = {
539  0, -1, TOP_DC_PRED, 0, -1, -1, -1, 0, -1, DC_128_PRED
540  };
541  int i;
542 
543  if (!(h->top_samples_available & 0x8000)) {
544  for (i = 0; i < 4; i++) {
545  int status = top[h->intra4x4_pred_mode_cache[scan8[0] + i]];
546  if (status < 0) {
548  "top block unavailable for requested intra4x4 mode %d at %d %d\n",
549  status, h->mb_x, h->mb_y);
550  return AVERROR_INVALIDDATA;
551  } else if (status) {
552  h->intra4x4_pred_mode_cache[scan8[0] + i] = status;
553  }
554  }
555  }
556 
557  if ((h->left_samples_available & 0x8888) != 0x8888) {
558  static const int mask[4] = { 0x8000, 0x2000, 0x80, 0x20 };
559  for (i = 0; i < 4; i++)
560  if (!(h->left_samples_available & mask[i])) {
561  int status = left[h->intra4x4_pred_mode_cache[scan8[0] + 8 * i]];
562  if (status < 0) {
564  "left block unavailable for requested intra4x4 mode %d at %d %d\n",
565  status, h->mb_x, h->mb_y);
566  return AVERROR_INVALIDDATA;
567  } else if (status) {
568  h->intra4x4_pred_mode_cache[scan8[0] + 8 * i] = status;
569  }
570  }
571  }
572 
573  return 0;
574 } // FIXME cleanup like ff_h264_check_intra_pred_mode
575 
576 /**
577  * Check if the top & left blocks are available if needed and
578  * change the dc mode so it only uses the available blocks.
579  */
580 int ff_h264_check_intra_pred_mode(H264Context *h, int mode, int is_chroma)
581 {
582  static const int8_t top[4] = { LEFT_DC_PRED8x8, 1, -1, -1 };
583  static const int8_t left[5] = { TOP_DC_PRED8x8, -1, 2, -1, DC_128_PRED8x8 };
584 
585  if (mode > 3U) {
587  "out of range intra chroma pred mode at %d %d\n",
588  h->mb_x, h->mb_y);
589  return AVERROR_INVALIDDATA;
590  }
591 
592  if (!(h->top_samples_available & 0x8000)) {
593  mode = top[mode];
594  if (mode < 0) {
596  "top block unavailable for requested intra mode at %d %d\n",
597  h->mb_x, h->mb_y);
598  return AVERROR_INVALIDDATA;
599  }
600  }
601 
602  if ((h->left_samples_available & 0x8080) != 0x8080) {
603  mode = left[mode];
604  if (is_chroma && (h->left_samples_available & 0x8080)) {
605  // mad cow disease mode, aka MBAFF + constrained_intra_pred
606  mode = ALZHEIMER_DC_L0T_PRED8x8 +
607  (!(h->left_samples_available & 0x8000)) +
608  2 * (mode == DC_128_PRED8x8);
609  }
610  if (mode < 0) {
612  "left block unavailable for requested intra mode at %d %d\n",
613  h->mb_x, h->mb_y);
614  return AVERROR_INVALIDDATA;
615  }
616  }
617 
618  return mode;
619 }
620 
622  int *dst_length, int *consumed, int length)
623 {
624  int i, si, di;
625  uint8_t *dst;
626  int bufidx;
627 
628  // src[0]&0x80; // forbidden bit
629  h->nal_ref_idc = src[0] >> 5;
630  h->nal_unit_type = src[0] & 0x1F;
631 
632  src++;
633  length--;
634 
635 #define STARTCODE_TEST \
636  if (i + 2 < length && src[i + 1] == 0 && src[i + 2] <= 3) { \
637  if (src[i + 2] != 3) { \
638  /* startcode, so we must be past the end */ \
639  length = i; \
640  } \
641  break; \
642  }
643 
644 #if HAVE_FAST_UNALIGNED
645 #define FIND_FIRST_ZERO \
646  if (i > 0 && !src[i]) \
647  i--; \
648  while (src[i]) \
649  i++
650 
651 #if HAVE_FAST_64BIT
652  for (i = 0; i + 1 < length; i += 9) {
653  if (!((~AV_RN64A(src + i) &
654  (AV_RN64A(src + i) - 0x0100010001000101ULL)) &
655  0x8000800080008080ULL))
656  continue;
657  FIND_FIRST_ZERO;
659  i -= 7;
660  }
661 #else
662  for (i = 0; i + 1 < length; i += 5) {
663  if (!((~AV_RN32A(src + i) &
664  (AV_RN32A(src + i) - 0x01000101U)) &
665  0x80008080U))
666  continue;
667  FIND_FIRST_ZERO;
669  i -= 3;
670  }
671 #endif
672 #else
673  for (i = 0; i + 1 < length; i += 2) {
674  if (src[i])
675  continue;
676  if (i > 0 && src[i - 1] == 0)
677  i--;
679  }
680 #endif
681 
682  // use second escape buffer for inter data
683  bufidx = h->nal_unit_type == NAL_DPC ? 1 : 0;
684 
685  si = h->rbsp_buffer_size[bufidx];
686  av_fast_padded_malloc(&h->rbsp_buffer[bufidx], &h->rbsp_buffer_size[bufidx], length+MAX_MBPAIR_SIZE);
687  dst = h->rbsp_buffer[bufidx];
688 
689  if (dst == NULL)
690  return NULL;
691 
692  if(i>=length-1){ //no escaped 0
693  *dst_length= length;
694  *consumed= length+1; //+1 for the header
695  if(h->avctx->flags2 & CODEC_FLAG2_FAST){
696  return src;
697  }else{
698  memcpy(dst, src, length);
699  return dst;
700  }
701  }
702 
703  memcpy(dst, src, i);
704  si = di = i;
705  while (si + 2 < length) {
706  // remove escapes (very rare 1:2^22)
707  if (src[si + 2] > 3) {
708  dst[di++] = src[si++];
709  dst[di++] = src[si++];
710  } else if (src[si] == 0 && src[si + 1] == 0) {
711  if (src[si + 2] == 3) { // escape
712  dst[di++] = 0;
713  dst[di++] = 0;
714  si += 3;
715  continue;
716  } else // next start code
717  goto nsc;
718  }
719 
720  dst[di++] = src[si++];
721  }
722  while (si < length)
723  dst[di++] = src[si++];
724 
725 nsc:
726  memset(dst + di, 0, FF_INPUT_BUFFER_PADDING_SIZE);
727 
728  *dst_length = di;
729  *consumed = si + 1; // +1 for the header
730  /* FIXME store exact number of bits in the getbitcontext
731  * (it is needed for decoding) */
732  return dst;
733 }
734 
735 /**
736  * Identify the exact end of the bitstream
737  * @return the length of the trailing, or 0 if damaged
738  */
740 {
741  int v = *src;
742  int r;
743 
744  tprintf(h->avctx, "rbsp trailing %X\n", v);
745 
746  for (r = 1; r < 9; r++) {
747  if (v & 1)
748  return r;
749  v >>= 1;
750  }
751  return 0;
752 }
753 
754 static inline int get_lowest_part_list_y(H264Context *h, Picture *pic, int n,
755  int height, int y_offset, int list)
756 {
757  int raw_my = h->mv_cache[list][scan8[n]][1];
758  int filter_height_down = (raw_my & 3) ? 3 : 0;
759  int full_my = (raw_my >> 2) + y_offset;
760  int bottom = full_my + filter_height_down + height;
761 
762  av_assert2(height >= 0);
763 
764  return FFMAX(0, bottom);
765 }
766 
767 static inline void get_lowest_part_y(H264Context *h, int refs[2][48], int n,
768  int height, int y_offset, int list0,
769  int list1, int *nrefs)
770 {
771  int my;
772 
773  y_offset += 16 * (h->mb_y >> MB_FIELD(h));
774 
775  if (list0) {
776  int ref_n = h->ref_cache[0][scan8[n]];
777  Picture *ref = &h->ref_list[0][ref_n];
778 
779  // Error resilience puts the current picture in the ref list.
780  // Don't try to wait on these as it will cause a deadlock.
781  // Fields can wait on each other, though.
782  if (ref->tf.progress->data != h->cur_pic.tf.progress->data ||
783  (ref->reference & 3) != h->picture_structure) {
784  my = get_lowest_part_list_y(h, ref, n, height, y_offset, 0);
785  if (refs[0][ref_n] < 0)
786  nrefs[0] += 1;
787  refs[0][ref_n] = FFMAX(refs[0][ref_n], my);
788  }
789  }
790 
791  if (list1) {
792  int ref_n = h->ref_cache[1][scan8[n]];
793  Picture *ref = &h->ref_list[1][ref_n];
794 
795  if (ref->tf.progress->data != h->cur_pic.tf.progress->data ||
796  (ref->reference & 3) != h->picture_structure) {
797  my = get_lowest_part_list_y(h, ref, n, height, y_offset, 1);
798  if (refs[1][ref_n] < 0)
799  nrefs[1] += 1;
800  refs[1][ref_n] = FFMAX(refs[1][ref_n], my);
801  }
802  }
803 }
804 
805 /**
806  * Wait until all reference frames are available for MC operations.
807  *
808  * @param h the H264 context
809  */
811 {
812  const int mb_xy = h->mb_xy;
813  const int mb_type = h->cur_pic.mb_type[mb_xy];
814  int refs[2][48];
815  int nrefs[2] = { 0 };
816  int ref, list;
817 
818  memset(refs, -1, sizeof(refs));
819 
820  if (IS_16X16(mb_type)) {
821  get_lowest_part_y(h, refs, 0, 16, 0,
822  IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1), nrefs);
823  } else if (IS_16X8(mb_type)) {
824  get_lowest_part_y(h, refs, 0, 8, 0,
825  IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1), nrefs);
826  get_lowest_part_y(h, refs, 8, 8, 8,
827  IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1), nrefs);
828  } else if (IS_8X16(mb_type)) {
829  get_lowest_part_y(h, refs, 0, 16, 0,
830  IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1), nrefs);
831  get_lowest_part_y(h, refs, 4, 16, 0,
832  IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1), nrefs);
833  } else {
834  int i;
835 
836  av_assert2(IS_8X8(mb_type));
837 
838  for (i = 0; i < 4; i++) {
839  const int sub_mb_type = h->sub_mb_type[i];
840  const int n = 4 * i;
841  int y_offset = (i & 2) << 2;
842 
843  if (IS_SUB_8X8(sub_mb_type)) {
844  get_lowest_part_y(h, refs, n, 8, y_offset,
845  IS_DIR(sub_mb_type, 0, 0),
846  IS_DIR(sub_mb_type, 0, 1),
847  nrefs);
848  } else if (IS_SUB_8X4(sub_mb_type)) {
849  get_lowest_part_y(h, refs, n, 4, y_offset,
850  IS_DIR(sub_mb_type, 0, 0),
851  IS_DIR(sub_mb_type, 0, 1),
852  nrefs);
853  get_lowest_part_y(h, refs, n + 2, 4, y_offset + 4,
854  IS_DIR(sub_mb_type, 0, 0),
855  IS_DIR(sub_mb_type, 0, 1),
856  nrefs);
857  } else if (IS_SUB_4X8(sub_mb_type)) {
858  get_lowest_part_y(h, refs, n, 8, y_offset,
859  IS_DIR(sub_mb_type, 0, 0),
860  IS_DIR(sub_mb_type, 0, 1),
861  nrefs);
862  get_lowest_part_y(h, refs, n + 1, 8, y_offset,
863  IS_DIR(sub_mb_type, 0, 0),
864  IS_DIR(sub_mb_type, 0, 1),
865  nrefs);
866  } else {
867  int j;
868  av_assert2(IS_SUB_4X4(sub_mb_type));
869  for (j = 0; j < 4; j++) {
870  int sub_y_offset = y_offset + 2 * (j & 2);
871  get_lowest_part_y(h, refs, n + j, 4, sub_y_offset,
872  IS_DIR(sub_mb_type, 0, 0),
873  IS_DIR(sub_mb_type, 0, 1),
874  nrefs);
875  }
876  }
877  }
878  }
879 
880  for (list = h->list_count - 1; list >= 0; list--)
881  for (ref = 0; ref < 48 && nrefs[list]; ref++) {
882  int row = refs[list][ref];
883  if (row >= 0) {
884  Picture *ref_pic = &h->ref_list[list][ref];
885  int ref_field = ref_pic->reference - 1;
886  int ref_field_picture = ref_pic->field_picture;
887  int pic_height = 16 * h->mb_height >> ref_field_picture;
888 
889  row <<= MB_MBAFF(h);
890  nrefs[list]--;
891 
892  if (!FIELD_PICTURE(h) && ref_field_picture) { // frame referencing two fields
893  ff_thread_await_progress(&ref_pic->tf,
894  FFMIN((row >> 1) - !(row & 1),
895  pic_height - 1),
896  1);
897  ff_thread_await_progress(&ref_pic->tf,
898  FFMIN((row >> 1), pic_height - 1),
899  0);
900  } else if (FIELD_PICTURE(h) && !ref_field_picture) { // field referencing one field of a frame
901  ff_thread_await_progress(&ref_pic->tf,
902  FFMIN(row * 2 + ref_field,
903  pic_height - 1),
904  0);
905  } else if (FIELD_PICTURE(h)) {
906  ff_thread_await_progress(&ref_pic->tf,
907  FFMIN(row, pic_height - 1),
908  ref_field);
909  } else {
910  ff_thread_await_progress(&ref_pic->tf,
911  FFMIN(row, pic_height - 1),
912  0);
913  }
914  }
915  }
916 }
917 
919  int n, int square, int height,
920  int delta, int list,
921  uint8_t *dest_y, uint8_t *dest_cb,
922  uint8_t *dest_cr,
923  int src_x_offset, int src_y_offset,
924  qpel_mc_func *qpix_op,
925  h264_chroma_mc_func chroma_op,
926  int pixel_shift, int chroma_idc)
927 {
928  const int mx = h->mv_cache[list][scan8[n]][0] + src_x_offset * 8;
929  int my = h->mv_cache[list][scan8[n]][1] + src_y_offset * 8;
930  const int luma_xy = (mx & 3) + ((my & 3) << 2);
931  ptrdiff_t offset = ((mx >> 2) << pixel_shift) + (my >> 2) * h->mb_linesize;
932  uint8_t *src_y = pic->f.data[0] + offset;
933  uint8_t *src_cb, *src_cr;
934  int extra_width = 0;
935  int extra_height = 0;
936  int emu = 0;
937  const int full_mx = mx >> 2;
938  const int full_my = my >> 2;
939  const int pic_width = 16 * h->mb_width;
940  const int pic_height = 16 * h->mb_height >> MB_FIELD(h);
941  int ysh;
942 
943  if (mx & 7)
944  extra_width -= 3;
945  if (my & 7)
946  extra_height -= 3;
947 
948  if (full_mx < 0 - extra_width ||
949  full_my < 0 - extra_height ||
950  full_mx + 16 /*FIXME*/ > pic_width + extra_width ||
951  full_my + 16 /*FIXME*/ > pic_height + extra_height) {
953  src_y - (2 << pixel_shift) - 2 * h->mb_linesize,
954  h->mb_linesize, h->mb_linesize,
955  16 + 5, 16 + 5 /*FIXME*/, full_mx - 2,
956  full_my - 2, pic_width, pic_height);
957  src_y = h->edge_emu_buffer + (2 << pixel_shift) + 2 * h->mb_linesize;
958  emu = 1;
959  }
960 
961  qpix_op[luma_xy](dest_y, src_y, h->mb_linesize); // FIXME try variable height perhaps?
962  if (!square)
963  qpix_op[luma_xy](dest_y + delta, src_y + delta, h->mb_linesize);
964 
965  if (CONFIG_GRAY && h->flags & CODEC_FLAG_GRAY)
966  return;
967 
968  if (chroma_idc == 3 /* yuv444 */) {
969  src_cb = pic->f.data[1] + offset;
970  if (emu) {
972  src_cb - (2 << pixel_shift) - 2 * h->mb_linesize,
973  h->mb_linesize, h->mb_linesize,
974  16 + 5, 16 + 5 /*FIXME*/,
975  full_mx - 2, full_my - 2,
976  pic_width, pic_height);
977  src_cb = h->edge_emu_buffer + (2 << pixel_shift) + 2 * h->mb_linesize;
978  }
979  qpix_op[luma_xy](dest_cb, src_cb, h->mb_linesize); // FIXME try variable height perhaps?
980  if (!square)
981  qpix_op[luma_xy](dest_cb + delta, src_cb + delta, h->mb_linesize);
982 
983  src_cr = pic->f.data[2] + offset;
984  if (emu) {
986  src_cr - (2 << pixel_shift) - 2 * h->mb_linesize,
987  h->mb_linesize, h->mb_linesize,
988  16 + 5, 16 + 5 /*FIXME*/,
989  full_mx - 2, full_my - 2,
990  pic_width, pic_height);
991  src_cr = h->edge_emu_buffer + (2 << pixel_shift) + 2 * h->mb_linesize;
992  }
993  qpix_op[luma_xy](dest_cr, src_cr, h->mb_linesize); // FIXME try variable height perhaps?
994  if (!square)
995  qpix_op[luma_xy](dest_cr + delta, src_cr + delta, h->mb_linesize);
996  return;
997  }
998 
999  ysh = 3 - (chroma_idc == 2 /* yuv422 */);
1000  if (chroma_idc == 1 /* yuv420 */ && MB_FIELD(h)) {
1001  // chroma offset when predicting from a field of opposite parity
1002  my += 2 * ((h->mb_y & 1) - (pic->reference - 1));
1003  emu |= (my >> 3) < 0 || (my >> 3) + 8 >= (pic_height >> 1);
1004  }
1005 
1006  src_cb = pic->f.data[1] + ((mx >> 3) << pixel_shift) +
1007  (my >> ysh) * h->mb_uvlinesize;
1008  src_cr = pic->f.data[2] + ((mx >> 3) << pixel_shift) +
1009  (my >> ysh) * h->mb_uvlinesize;
1010 
1011  if (emu) {
1012  h->vdsp.emulated_edge_mc(h->edge_emu_buffer, src_cb,
1014  9, 8 * chroma_idc + 1, (mx >> 3), (my >> ysh),
1015  pic_width >> 1, pic_height >> (chroma_idc == 1 /* yuv420 */));
1016  src_cb = h->edge_emu_buffer;
1017  }
1018  chroma_op(dest_cb, src_cb, h->mb_uvlinesize,
1019  height >> (chroma_idc == 1 /* yuv420 */),
1020  mx & 7, (my << (chroma_idc == 2 /* yuv422 */)) & 7);
1021 
1022  if (emu) {
1023  h->vdsp.emulated_edge_mc(h->edge_emu_buffer, src_cr,
1025  9, 8 * chroma_idc + 1, (mx >> 3), (my >> ysh),
1026  pic_width >> 1, pic_height >> (chroma_idc == 1 /* yuv420 */));
1027  src_cr = h->edge_emu_buffer;
1028  }
1029  chroma_op(dest_cr, src_cr, h->mb_uvlinesize, height >> (chroma_idc == 1 /* yuv420 */),
1030  mx & 7, (my << (chroma_idc == 2 /* yuv422 */)) & 7);
1031 }
1032 
1034  int height, int delta,
1035  uint8_t *dest_y, uint8_t *dest_cb,
1036  uint8_t *dest_cr,
1037  int x_offset, int y_offset,
1038  qpel_mc_func *qpix_put,
1039  h264_chroma_mc_func chroma_put,
1040  qpel_mc_func *qpix_avg,
1041  h264_chroma_mc_func chroma_avg,
1042  int list0, int list1,
1043  int pixel_shift, int chroma_idc)
1044 {
1045  qpel_mc_func *qpix_op = qpix_put;
1046  h264_chroma_mc_func chroma_op = chroma_put;
1047 
1048  dest_y += (2 * x_offset << pixel_shift) + 2 * y_offset * h->mb_linesize;
1049  if (chroma_idc == 3 /* yuv444 */) {
1050  dest_cb += (2 * x_offset << pixel_shift) + 2 * y_offset * h->mb_linesize;
1051  dest_cr += (2 * x_offset << pixel_shift) + 2 * y_offset * h->mb_linesize;
1052  } else if (chroma_idc == 2 /* yuv422 */) {
1053  dest_cb += (x_offset << pixel_shift) + 2 * y_offset * h->mb_uvlinesize;
1054  dest_cr += (x_offset << pixel_shift) + 2 * y_offset * h->mb_uvlinesize;
1055  } else { /* yuv420 */
1056  dest_cb += (x_offset << pixel_shift) + y_offset * h->mb_uvlinesize;
1057  dest_cr += (x_offset << pixel_shift) + y_offset * h->mb_uvlinesize;
1058  }
1059  x_offset += 8 * h->mb_x;
1060  y_offset += 8 * (h->mb_y >> MB_FIELD(h));
1061 
1062  if (list0) {
1063  Picture *ref = &h->ref_list[0][h->ref_cache[0][scan8[n]]];
1064  mc_dir_part(h, ref, n, square, height, delta, 0,
1065  dest_y, dest_cb, dest_cr, x_offset, y_offset,
1066  qpix_op, chroma_op, pixel_shift, chroma_idc);
1067 
1068  qpix_op = qpix_avg;
1069  chroma_op = chroma_avg;
1070  }
1071 
1072  if (list1) {
1073  Picture *ref = &h->ref_list[1][h->ref_cache[1][scan8[n]]];
1074  mc_dir_part(h, ref, n, square, height, delta, 1,
1075  dest_y, dest_cb, dest_cr, x_offset, y_offset,
1076  qpix_op, chroma_op, pixel_shift, chroma_idc);
1077  }
1078 }
1079 
1081  int height, int delta,
1082  uint8_t *dest_y, uint8_t *dest_cb,
1083  uint8_t *dest_cr,
1084  int x_offset, int y_offset,
1085  qpel_mc_func *qpix_put,
1086  h264_chroma_mc_func chroma_put,
1087  h264_weight_func luma_weight_op,
1088  h264_weight_func chroma_weight_op,
1089  h264_biweight_func luma_weight_avg,
1090  h264_biweight_func chroma_weight_avg,
1091  int list0, int list1,
1092  int pixel_shift, int chroma_idc)
1093 {
1094  int chroma_height;
1095 
1096  dest_y += (2 * x_offset << pixel_shift) + 2 * y_offset * h->mb_linesize;
1097  if (chroma_idc == 3 /* yuv444 */) {
1098  chroma_height = height;
1099  chroma_weight_avg = luma_weight_avg;
1100  chroma_weight_op = luma_weight_op;
1101  dest_cb += (2 * x_offset << pixel_shift) + 2 * y_offset * h->mb_linesize;
1102  dest_cr += (2 * x_offset << pixel_shift) + 2 * y_offset * h->mb_linesize;
1103  } else if (chroma_idc == 2 /* yuv422 */) {
1104  chroma_height = height;
1105  dest_cb += (x_offset << pixel_shift) + 2 * y_offset * h->mb_uvlinesize;
1106  dest_cr += (x_offset << pixel_shift) + 2 * y_offset * h->mb_uvlinesize;
1107  } else { /* yuv420 */
1108  chroma_height = height >> 1;
1109  dest_cb += (x_offset << pixel_shift) + y_offset * h->mb_uvlinesize;
1110  dest_cr += (x_offset << pixel_shift) + y_offset * h->mb_uvlinesize;
1111  }
1112  x_offset += 8 * h->mb_x;
1113  y_offset += 8 * (h->mb_y >> MB_FIELD(h));
1114 
1115  if (list0 && list1) {
1116  /* don't optimize for luma-only case, since B-frames usually
1117  * use implicit weights => chroma too. */
1118  uint8_t *tmp_cb = h->bipred_scratchpad;
1119  uint8_t *tmp_cr = h->bipred_scratchpad + (16 << pixel_shift);
1120  uint8_t *tmp_y = h->bipred_scratchpad + 16 * h->mb_uvlinesize;
1121  int refn0 = h->ref_cache[0][scan8[n]];
1122  int refn1 = h->ref_cache[1][scan8[n]];
1123 
1124  mc_dir_part(h, &h->ref_list[0][refn0], n, square, height, delta, 0,
1125  dest_y, dest_cb, dest_cr,
1126  x_offset, y_offset, qpix_put, chroma_put,
1127  pixel_shift, chroma_idc);
1128  mc_dir_part(h, &h->ref_list[1][refn1], n, square, height, delta, 1,
1129  tmp_y, tmp_cb, tmp_cr,
1130  x_offset, y_offset, qpix_put, chroma_put,
1131  pixel_shift, chroma_idc);
1132 
1133  if (h->use_weight == 2) {
1134  int weight0 = h->implicit_weight[refn0][refn1][h->mb_y & 1];
1135  int weight1 = 64 - weight0;
1136  luma_weight_avg(dest_y, tmp_y, h->mb_linesize,
1137  height, 5, weight0, weight1, 0);
1138  chroma_weight_avg(dest_cb, tmp_cb, h->mb_uvlinesize,
1139  chroma_height, 5, weight0, weight1, 0);
1140  chroma_weight_avg(dest_cr, tmp_cr, h->mb_uvlinesize,
1141  chroma_height, 5, weight0, weight1, 0);
1142  } else {
1143  luma_weight_avg(dest_y, tmp_y, h->mb_linesize, height,
1145  h->luma_weight[refn0][0][0],
1146  h->luma_weight[refn1][1][0],
1147  h->luma_weight[refn0][0][1] +
1148  h->luma_weight[refn1][1][1]);
1149  chroma_weight_avg(dest_cb, tmp_cb, h->mb_uvlinesize, chroma_height,
1151  h->chroma_weight[refn0][0][0][0],
1152  h->chroma_weight[refn1][1][0][0],
1153  h->chroma_weight[refn0][0][0][1] +
1154  h->chroma_weight[refn1][1][0][1]);
1155  chroma_weight_avg(dest_cr, tmp_cr, h->mb_uvlinesize, chroma_height,
1157  h->chroma_weight[refn0][0][1][0],
1158  h->chroma_weight[refn1][1][1][0],
1159  h->chroma_weight[refn0][0][1][1] +
1160  h->chroma_weight[refn1][1][1][1]);
1161  }
1162  } else {
1163  int list = list1 ? 1 : 0;
1164  int refn = h->ref_cache[list][scan8[n]];
1165  Picture *ref = &h->ref_list[list][refn];
1166  mc_dir_part(h, ref, n, square, height, delta, list,
1167  dest_y, dest_cb, dest_cr, x_offset, y_offset,
1168  qpix_put, chroma_put, pixel_shift, chroma_idc);
1169 
1170  luma_weight_op(dest_y, h->mb_linesize, height,
1172  h->luma_weight[refn][list][0],
1173  h->luma_weight[refn][list][1]);
1174  if (h->use_weight_chroma) {
1175  chroma_weight_op(dest_cb, h->mb_uvlinesize, chroma_height,
1177  h->chroma_weight[refn][list][0][0],
1178  h->chroma_weight[refn][list][0][1]);
1179  chroma_weight_op(dest_cr, h->mb_uvlinesize, chroma_height,
1181  h->chroma_weight[refn][list][1][0],
1182  h->chroma_weight[refn][list][1][1]);
1183  }
1184  }
1185 }
1186 
1188  int pixel_shift, int chroma_idc)
1189 {
1190  /* fetch pixels for estimated mv 4 macroblocks ahead
1191  * optimized for 64byte cache lines */
1192  const int refn = h->ref_cache[list][scan8[0]];
1193  if (refn >= 0) {
1194  const int mx = (h->mv_cache[list][scan8[0]][0] >> 2) + 16 * h->mb_x + 8;
1195  const int my = (h->mv_cache[list][scan8[0]][1] >> 2) + 16 * h->mb_y;
1196  uint8_t **src = h->ref_list[list][refn].f.data;
1197  int off = (mx << pixel_shift) +
1198  (my + (h->mb_x & 3) * 4) * h->mb_linesize +
1199  (64 << pixel_shift);
1200  h->vdsp.prefetch(src[0] + off, h->linesize, 4);
1201  if (chroma_idc == 3 /* yuv444 */) {
1202  h->vdsp.prefetch(src[1] + off, h->linesize, 4);
1203  h->vdsp.prefetch(src[2] + off, h->linesize, 4);
1204  } else {
1205  off= (((mx>>1)+64)<<pixel_shift) + ((my>>1) + (h->mb_x&7))*h->uvlinesize;
1206  h->vdsp.prefetch(src[1] + off, src[2] - src[1], 2);
1207  }
1208  }
1209 }
1210 
1211 static void free_tables(H264Context *h, int free_rbsp)
1212 {
1213  int i;
1214  H264Context *hx;
1215 
1218  av_freep(&h->cbp_table);
1219  av_freep(&h->mvd_table[0]);
1220  av_freep(&h->mvd_table[1]);
1221  av_freep(&h->direct_table);
1222  av_freep(&h->non_zero_count);
1224  h->slice_table = NULL;
1225  av_freep(&h->list_counts);
1226 
1227  av_freep(&h->mb2b_xy);
1228  av_freep(&h->mb2br_xy);
1229 
1234 
1235  if (free_rbsp && h->DPB) {
1236  for (i = 0; i < MAX_PICTURE_COUNT; i++)
1237  unref_picture(h, &h->DPB[i]);
1238  av_freep(&h->DPB);
1239  } else if (h->DPB) {
1240  for (i = 0; i < MAX_PICTURE_COUNT; i++)
1241  h->DPB[i].needs_realloc = 1;
1242  }
1243 
1244  h->cur_pic_ptr = NULL;
1245 
1246  for (i = 0; i < MAX_THREADS; i++) {
1247  hx = h->thread_context[i];
1248  if (!hx)
1249  continue;
1250  av_freep(&hx->top_borders[1]);
1251  av_freep(&hx->top_borders[0]);
1253  av_freep(&hx->edge_emu_buffer);
1254  av_freep(&hx->dc_val_base);
1255  av_freep(&hx->me.scratchpad);
1256  av_freep(&hx->er.mb_index2xy);
1258  av_freep(&hx->er.er_temp_buffer);
1259  av_freep(&hx->er.mbintra_table);
1260  av_freep(&hx->er.mbskip_table);
1261 
1262  if (free_rbsp) {
1263  av_freep(&hx->rbsp_buffer[1]);
1264  av_freep(&hx->rbsp_buffer[0]);
1265  hx->rbsp_buffer_size[0] = 0;
1266  hx->rbsp_buffer_size[1] = 0;
1267  }
1268  if (i)
1269  av_freep(&h->thread_context[i]);
1270  }
1271 }
1272 
1274 {
1275  int i, j, q, x;
1276  const int max_qp = 51 + 6 * (h->sps.bit_depth_luma - 8);
1277 
1278  for (i = 0; i < 6; i++) {
1279  h->dequant8_coeff[i] = h->dequant8_buffer[i];
1280  for (j = 0; j < i; j++)
1281  if (!memcmp(h->pps.scaling_matrix8[j], h->pps.scaling_matrix8[i],
1282  64 * sizeof(uint8_t))) {
1283  h->dequant8_coeff[i] = h->dequant8_buffer[j];
1284  break;
1285  }
1286  if (j < i)
1287  continue;
1288 
1289  for (q = 0; q < max_qp + 1; q++) {
1290  int shift = div6[q];
1291  int idx = rem6[q];
1292  for (x = 0; x < 64; x++)
1293  h->dequant8_coeff[i][q][(x >> 3) | ((x & 7) << 3)] =
1294  ((uint32_t)dequant8_coeff_init[idx][dequant8_coeff_init_scan[((x >> 1) & 12) | (x & 3)]] *
1295  h->pps.scaling_matrix8[i][x]) << shift;
1296  }
1297  }
1298 }
1299 
1301 {
1302  int i, j, q, x;
1303  const int max_qp = 51 + 6 * (h->sps.bit_depth_luma - 8);
1304  for (i = 0; i < 6; i++) {
1305  h->dequant4_coeff[i] = h->dequant4_buffer[i];
1306  for (j = 0; j < i; j++)
1307  if (!memcmp(h->pps.scaling_matrix4[j], h->pps.scaling_matrix4[i],
1308  16 * sizeof(uint8_t))) {
1309  h->dequant4_coeff[i] = h->dequant4_buffer[j];
1310  break;
1311  }
1312  if (j < i)
1313  continue;
1314 
1315  for (q = 0; q < max_qp + 1; q++) {
1316  int shift = div6[q] + 2;
1317  int idx = rem6[q];
1318  for (x = 0; x < 16; x++)
1319  h->dequant4_coeff[i][q][(x >> 2) | ((x << 2) & 0xF)] =
1320  ((uint32_t)dequant4_coeff_init[idx][(x & 1) + ((x >> 2) & 1)] *
1321  h->pps.scaling_matrix4[i][x]) << shift;
1322  }
1323  }
1324 }
1325 
1327 {
1328  int i, x;
1330  memset(h->dequant8_coeff, 0, sizeof(h->dequant8_coeff));
1331 
1332  if (h->pps.transform_8x8_mode)
1334  if (h->sps.transform_bypass) {
1335  for (i = 0; i < 6; i++)
1336  for (x = 0; x < 16; x++)
1337  h->dequant4_coeff[i][0][x] = 1 << 6;
1339  for (i = 0; i < 6; i++)
1340  for (x = 0; x < 64; x++)
1341  h->dequant8_coeff[i][0][x] = 1 << 6;
1342  }
1343 }
1344 
1346 {
1347  const int big_mb_num = h->mb_stride * (h->mb_height + 1);
1348  const int row_mb_num = 2*h->mb_stride*FFMAX(h->avctx->thread_count, 1);
1349  int x, y, i;
1350 
1352  row_mb_num * 8 * sizeof(uint8_t), fail)
1354  big_mb_num * 48 * sizeof(uint8_t), fail)
1356  (big_mb_num + h->mb_stride) * sizeof(*h->slice_table_base), fail)
1358  big_mb_num * sizeof(uint16_t), fail)
1360  big_mb_num * sizeof(uint8_t), fail)
1361  FF_ALLOCZ_OR_GOTO(h->avctx, h->mvd_table[0],
1362  16 * row_mb_num * sizeof(uint8_t), fail);
1363  FF_ALLOCZ_OR_GOTO(h->avctx, h->mvd_table[1],
1364  16 * row_mb_num * sizeof(uint8_t), fail);
1366  4 * big_mb_num * sizeof(uint8_t), fail);
1368  big_mb_num * sizeof(uint8_t), fail)
1369 
1370  memset(h->slice_table_base, -1,
1371  (big_mb_num + h->mb_stride) * sizeof(*h->slice_table_base));
1372  h->slice_table = h->slice_table_base + h->mb_stride * 2 + 1;
1373 
1375  big_mb_num * sizeof(uint32_t), fail);
1377  big_mb_num * sizeof(uint32_t), fail);
1378  for (y = 0; y < h->mb_height; y++)
1379  for (x = 0; x < h->mb_width; x++) {
1380  const int mb_xy = x + y * h->mb_stride;
1381  const int b_xy = 4 * x + 4 * y * h->b_stride;
1382 
1383  h->mb2b_xy[mb_xy] = b_xy;
1384  h->mb2br_xy[mb_xy] = 8 * (FMO ? mb_xy : (mb_xy % (2 * h->mb_stride)));
1385  }
1386 
1387  if (!h->dequant4_coeff[0])
1389 
1390  if (!h->DPB) {
1391  h->DPB = av_mallocz_array(MAX_PICTURE_COUNT, sizeof(*h->DPB));
1392  if (!h->DPB)
1393  return AVERROR(ENOMEM);
1394  for (i = 0; i < MAX_PICTURE_COUNT; i++)
1395  av_frame_unref(&h->DPB[i].f);
1396  av_frame_unref(&h->cur_pic.f);
1397  }
1398 
1399  return 0;
1400 
1401 fail:
1402  free_tables(h, 1);
1403  return AVERROR(ENOMEM);
1404 }
1405 
1406 /**
1407  * Mimic alloc_tables(), but for every context thread.
1408  */
1409 static void clone_tables(H264Context *dst, H264Context *src, int i)
1410 {
1411  dst->intra4x4_pred_mode = src->intra4x4_pred_mode + i * 8 * 2 * src->mb_stride;
1412  dst->non_zero_count = src->non_zero_count;
1413  dst->slice_table = src->slice_table;
1414  dst->cbp_table = src->cbp_table;
1415  dst->mb2b_xy = src->mb2b_xy;
1416  dst->mb2br_xy = src->mb2br_xy;
1418  dst->mvd_table[0] = src->mvd_table[0] + i * 8 * 2 * src->mb_stride;
1419  dst->mvd_table[1] = src->mvd_table[1] + i * 8 * 2 * src->mb_stride;
1420  dst->direct_table = src->direct_table;
1421  dst->list_counts = src->list_counts;
1422  dst->DPB = src->DPB;
1423  dst->cur_pic_ptr = src->cur_pic_ptr;
1424  dst->cur_pic = src->cur_pic;
1425  dst->bipred_scratchpad = NULL;
1426  dst->edge_emu_buffer = NULL;
1427  dst->me.scratchpad = NULL;
1429  src->sps.chroma_format_idc);
1430 }
1431 
1432 /**
1433  * Init context
1434  * Allocate buffers which are not shared amongst multiple threads.
1435  */
1437 {
1438  ERContext *er = &h->er;
1439  int mb_array_size = h->mb_height * h->mb_stride;
1440  int y_size = (2 * h->mb_width + 1) * (2 * h->mb_height + 1);
1441  int c_size = h->mb_stride * (h->mb_height + 1);
1442  int yc_size = y_size + 2 * c_size;
1443  int x, y, i;
1444 
1446  h->mb_width * 16 * 3 * sizeof(uint8_t) * 2, fail)
1448  h->mb_width * 16 * 3 * sizeof(uint8_t) * 2, fail)
1449 
1450  h->ref_cache[0][scan8[5] + 1] =
1451  h->ref_cache[0][scan8[7] + 1] =
1452  h->ref_cache[0][scan8[13] + 1] =
1453  h->ref_cache[1][scan8[5] + 1] =
1454  h->ref_cache[1][scan8[7] + 1] =
1455  h->ref_cache[1][scan8[13] + 1] = PART_NOT_AVAILABLE;
1456 
1457  if (CONFIG_ERROR_RESILIENCE) {
1458  /* init ER */
1459  er->avctx = h->avctx;
1460  er->dsp = &h->dsp;
1462  er->opaque = h;
1463  er->quarter_sample = 1;
1464 
1465  er->mb_num = h->mb_num;
1466  er->mb_width = h->mb_width;
1467  er->mb_height = h->mb_height;
1468  er->mb_stride = h->mb_stride;
1469  er->b8_stride = h->mb_width * 2 + 1;
1470 
1471  FF_ALLOCZ_OR_GOTO(h->avctx, er->mb_index2xy, (h->mb_num + 1) * sizeof(int),
1472  fail); // error ressilience code looks cleaner with this
1473  for (y = 0; y < h->mb_height; y++)
1474  for (x = 0; x < h->mb_width; x++)
1475  er->mb_index2xy[x + y * h->mb_width] = x + y * h->mb_stride;
1476 
1477  er->mb_index2xy[h->mb_height * h->mb_width] = (h->mb_height - 1) *
1478  h->mb_stride + h->mb_width;
1479 
1481  mb_array_size * sizeof(uint8_t), fail);
1482 
1483  FF_ALLOC_OR_GOTO(h->avctx, er->mbintra_table, mb_array_size, fail);
1484  memset(er->mbintra_table, 1, mb_array_size);
1485 
1486  FF_ALLOCZ_OR_GOTO(h->avctx, er->mbskip_table, mb_array_size + 2, fail);
1487 
1489  fail);
1490 
1491  FF_ALLOCZ_OR_GOTO(h->avctx, h->dc_val_base, yc_size * sizeof(int16_t), fail);
1492  er->dc_val[0] = h->dc_val_base + h->mb_width * 2 + 2;
1493  er->dc_val[1] = h->dc_val_base + y_size + h->mb_stride + 1;
1494  er->dc_val[2] = er->dc_val[1] + c_size;
1495  for (i = 0; i < yc_size; i++)
1496  h->dc_val_base[i] = 1024;
1497  }
1498 
1499  return 0;
1500 
1501 fail:
1502  return AVERROR(ENOMEM); // free_tables will clean up for us
1503 }
1504 
1505 static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size,
1506  int parse_extradata);
1507 
1509 {
1510  AVCodecContext *avctx = h->avctx;
1511  int ret;
1512 
1513  if (!buf || size <= 0)
1514  return -1;
1515 
1516  if (buf[0] == 1) {
1517  int i, cnt, nalsize;
1518  const unsigned char *p = buf;
1519 
1520  h->is_avc = 1;
1521 
1522  if (size < 7) {
1523  av_log(avctx, AV_LOG_ERROR,
1524  "avcC %d too short\n", size);
1525  return AVERROR_INVALIDDATA;
1526  }
1527  /* sps and pps in the avcC always have length coded with 2 bytes,
1528  * so put a fake nal_length_size = 2 while parsing them */
1529  h->nal_length_size = 2;
1530  // Decode sps from avcC
1531  cnt = *(p + 5) & 0x1f; // Number of sps
1532  p += 6;
1533  for (i = 0; i < cnt; i++) {
1534  nalsize = AV_RB16(p) + 2;
1535  if(nalsize > size - (p-buf))
1536  return AVERROR_INVALIDDATA;
1537  ret = decode_nal_units(h, p, nalsize, 1);
1538  if (ret < 0) {
1539  av_log(avctx, AV_LOG_ERROR,
1540  "Decoding sps %d from avcC failed\n", i);
1541  return ret;
1542  }
1543  p += nalsize;
1544  }
1545  // Decode pps from avcC
1546  cnt = *(p++); // Number of pps
1547  for (i = 0; i < cnt; i++) {
1548  nalsize = AV_RB16(p) + 2;
1549  if(nalsize > size - (p-buf))
1550  return AVERROR_INVALIDDATA;
1551  ret = decode_nal_units(h, p, nalsize, 1);
1552  if (ret < 0) {
1553  av_log(avctx, AV_LOG_ERROR,
1554  "Decoding pps %d from avcC failed\n", i);
1555  return ret;
1556  }
1557  p += nalsize;
1558  }
1559  // Now store right nal length size, that will be used to parse all other nals
1560  h->nal_length_size = (buf[4] & 0x03) + 1;
1561  } else {
1562  h->is_avc = 0;
1563  ret = decode_nal_units(h, buf, size, 1);
1564  if (ret < 0)
1565  return ret;
1566  }
1567  return size;
1568 }
1569 
1571 {
1572  H264Context *h = avctx->priv_data;
1573  int i;
1574  int ret;
1575 
1576  h->avctx = avctx;
1577 
1578  h->bit_depth_luma = 8;
1579  h->chroma_format_idc = 1;
1580 
1581  h->avctx->bits_per_raw_sample = 8;
1582  h->cur_chroma_format_idc = 1;
1583 
1584  ff_h264dsp_init(&h->h264dsp, 8, 1);
1585  av_assert0(h->sps.bit_depth_chroma == 0);
1587  ff_h264qpel_init(&h->h264qpel, 8);
1588  ff_h264_pred_init(&h->hpc, h->avctx->codec_id, 8, 1);
1589 
1590  h->dequant_coeff_pps = -1;
1591  h->current_sps_id = -1;
1592 
1593  /* needed so that IDCT permutation is known early */
1594  if (CONFIG_ERROR_RESILIENCE)
1595  ff_dsputil_init(&h->dsp, h->avctx);
1596  ff_videodsp_init(&h->vdsp, 8);
1597 
1598  memset(h->pps.scaling_matrix4, 16, 6 * 16 * sizeof(uint8_t));
1599  memset(h->pps.scaling_matrix8, 16, 2 * 64 * sizeof(uint8_t));
1600 
1602  h->slice_context_count = 1;
1603  h->workaround_bugs = avctx->workaround_bugs;
1604  h->flags = avctx->flags;
1605 
1606  /* set defaults */
1607  // s->decode_mb = ff_h263_decode_mb;
1608  if (!avctx->has_b_frames)
1609  h->low_delay = 1;
1610 
1612 
1614 
1616 
1617  h->pixel_shift = 0;
1618  h->sps.bit_depth_luma = avctx->bits_per_raw_sample = 8;
1619 
1620  h->thread_context[0] = h;
1621  h->outputed_poc = h->next_outputed_poc = INT_MIN;
1622  for (i = 0; i < MAX_DELAYED_PIC_COUNT; i++)
1623  h->last_pocs[i] = INT_MIN;
1624  h->prev_poc_msb = 1 << 16;
1625  h->prev_frame_num = -1;
1626  h->x264_build = -1;
1628  ff_h264_reset_sei(h);
1629  if (avctx->codec_id == AV_CODEC_ID_H264) {
1630  if (avctx->ticks_per_frame == 1) {
1631  if(h->avctx->time_base.den < INT_MAX/2) {
1632  h->avctx->time_base.den *= 2;
1633  } else
1634  h->avctx->time_base.num /= 2;
1635  }
1636  avctx->ticks_per_frame = 2;
1637  }
1638 
1639  if (avctx->extradata_size > 0 && avctx->extradata) {
1640  ret = ff_h264_decode_extradata(h, avctx->extradata, avctx->extradata_size);
1641  if (ret < 0) {
1643  return ret;
1644  }
1645  }
1646 
1650  h->low_delay = 0;
1651  }
1652 
1653  avctx->internal->allocate_progress = 1;
1654 
1655  flush_change(h);
1656 
1657  return 0;
1658 }
1659 
1660 #define IN_RANGE(a, b, size) (((a) >= (b)) && ((a) < ((b) + (size))))
1661 #undef REBASE_PICTURE
1662 #define REBASE_PICTURE(pic, new_ctx, old_ctx) \
1663  ((pic && pic >= old_ctx->DPB && \
1664  pic < old_ctx->DPB + MAX_PICTURE_COUNT) ? \
1665  &new_ctx->DPB[pic - old_ctx->DPB] : NULL)
1666 
1668  H264Context *new_base,
1669  H264Context *old_base)
1670 {
1671  int i;
1672 
1673  for (i = 0; i < count; i++) {
1674  assert((IN_RANGE(from[i], old_base, sizeof(*old_base)) ||
1675  IN_RANGE(from[i], old_base->DPB,
1676  sizeof(Picture) * MAX_PICTURE_COUNT) ||
1677  !from[i]));
1678  to[i] = REBASE_PICTURE(from[i], new_base, old_base);
1679  }
1680 }
1681 
1682 static int copy_parameter_set(void **to, void **from, int count, int size)
1683 {
1684  int i;
1685 
1686  for (i = 0; i < count; i++) {
1687  if (to[i] && !from[i]) {
1688  av_freep(&to[i]);
1689  } else if (from[i] && !to[i]) {
1690  to[i] = av_malloc(size);
1691  if (!to[i])
1692  return AVERROR(ENOMEM);
1693  }
1694 
1695  if (from[i])
1696  memcpy(to[i], from[i], size);
1697  }
1698 
1699  return 0;
1700 }
1701 
1703 {
1704  H264Context *h = avctx->priv_data;
1705 
1706  if (!avctx->internal->is_copy)
1707  return 0;
1708  memset(h->sps_buffers, 0, sizeof(h->sps_buffers));
1709  memset(h->pps_buffers, 0, sizeof(h->pps_buffers));
1710 
1711  h->rbsp_buffer[0] = NULL;
1712  h->rbsp_buffer[1] = NULL;
1713  h->rbsp_buffer_size[0] = 0;
1714  h->rbsp_buffer_size[1] = 0;
1715  h->context_initialized = 0;
1716 
1717  return 0;
1718 }
1719 
1720 #define copy_fields(to, from, start_field, end_field) \
1721  memcpy(&to->start_field, &from->start_field, \
1722  (char *)&to->end_field - (char *)&to->start_field)
1723 
1724 static int h264_slice_header_init(H264Context *, int);
1725 
1727 
1729  const AVCodecContext *src)
1730 {
1731  H264Context *h = dst->priv_data, *h1 = src->priv_data;
1732  int inited = h->context_initialized, err = 0;
1733  int context_reinitialized = 0;
1734  int i, ret;
1735 
1736  if (dst == src)
1737  return 0;
1738 
1739  if (inited &&
1740  (h->width != h1->width ||
1741  h->height != h1->height ||
1742  h->mb_width != h1->mb_width ||
1743  h->mb_height != h1->mb_height ||
1744  h->sps.bit_depth_luma != h1->sps.bit_depth_luma ||
1745  h->sps.chroma_format_idc != h1->sps.chroma_format_idc ||
1746  h->sps.colorspace != h1->sps.colorspace)) {
1747 
1748  /* set bits_per_raw_sample to the previous value. the check for changed
1749  * bit depth in h264_set_parameter_from_sps() uses it and sets it to
1750  * the current value */
1752 
1754 
1755  h->width = h1->width;
1756  h->height = h1->height;
1757  h->mb_height = h1->mb_height;
1758  h->mb_width = h1->mb_width;
1759  h->mb_num = h1->mb_num;
1760  h->mb_stride = h1->mb_stride;
1761  h->b_stride = h1->b_stride;
1762  // SPS/PPS
1763  if ((ret = copy_parameter_set((void **)h->sps_buffers,
1764  (void **)h1->sps_buffers,
1765  MAX_SPS_COUNT, sizeof(SPS))) < 0)
1766  return ret;
1767  h->sps = h1->sps;
1768  if ((ret = copy_parameter_set((void **)h->pps_buffers,
1769  (void **)h1->pps_buffers,
1770  MAX_PPS_COUNT, sizeof(PPS))) < 0)
1771  return ret;
1772  h->pps = h1->pps;
1773 
1774  if ((err = h264_slice_header_init(h, 1)) < 0) {
1775  av_log(h->avctx, AV_LOG_ERROR, "h264_slice_header_init() failed");
1776  return err;
1777  }
1778  context_reinitialized = 1;
1779 
1780 #if 0
1782  //Note we set context_reinitialized which will cause h264_set_parameter_from_sps to be reexecuted
1783  h->cur_chroma_format_idc = h1->cur_chroma_format_idc;
1784 #endif
1785  }
1786  /* update linesize on resize for h264. The h264 decoder doesn't
1787  * necessarily call ff_MPV_frame_start in the new thread */
1788  h->linesize = h1->linesize;
1789  h->uvlinesize = h1->uvlinesize;
1790 
1791  /* copy block_offset since frame_start may not be called */
1792  memcpy(h->block_offset, h1->block_offset, sizeof(h->block_offset));
1793 
1794  if (!inited) {
1795  for (i = 0; i < MAX_SPS_COUNT; i++)
1796  av_freep(h->sps_buffers + i);
1797 
1798  for (i = 0; i < MAX_PPS_COUNT; i++)
1799  av_freep(h->pps_buffers + i);
1800 
1801  av_freep(&h->rbsp_buffer[0]);
1802  av_freep(&h->rbsp_buffer[1]);
1803  memcpy(h, h1, offsetof(H264Context, intra_pcm_ptr));
1804  memcpy(&h->cabac, &h1->cabac,
1805  sizeof(H264Context) - offsetof(H264Context, cabac));
1806  av_assert0((void*)&h->cabac == &h->mb_padding + 1);
1807 
1808  memset(h->sps_buffers, 0, sizeof(h->sps_buffers));
1809  memset(h->pps_buffers, 0, sizeof(h->pps_buffers));
1810 
1811  memset(&h->er, 0, sizeof(h->er));
1812  memset(&h->me, 0, sizeof(h->me));
1813  memset(&h->mb, 0, sizeof(h->mb));
1814  memset(&h->mb_luma_dc, 0, sizeof(h->mb_luma_dc));
1815  memset(&h->mb_padding, 0, sizeof(h->mb_padding));
1816 
1817  h->avctx = dst;
1818  h->DPB = NULL;
1819  h->qscale_table_pool = NULL;
1820  h->mb_type_pool = NULL;
1821  h->ref_index_pool = NULL;
1822  h->motion_val_pool = NULL;
1823  for (i = 0; i < 2; i++) {
1824  h->rbsp_buffer[i] = NULL;
1825  h->rbsp_buffer_size[i] = 0;
1826  }
1827 
1828  if (h1->context_initialized) {
1829  h->context_initialized = 0;
1830 
1831  memset(&h->cur_pic, 0, sizeof(h->cur_pic));
1832  av_frame_unref(&h->cur_pic.f);
1833  h->cur_pic.tf.f = &h->cur_pic.f;
1834 
1835  ret = ff_h264_alloc_tables(h);
1836  if (ret < 0) {
1837  av_log(dst, AV_LOG_ERROR, "Could not allocate memory\n");
1838  return ret;
1839  }
1840  ret = context_init(h);
1841  if (ret < 0) {
1842  av_log(dst, AV_LOG_ERROR, "context_init() failed.\n");
1843  return ret;
1844  }
1845  }
1846 
1847  h->bipred_scratchpad = NULL;
1848  h->edge_emu_buffer = NULL;
1849 
1850  h->thread_context[0] = h;
1851  h->context_initialized = h1->context_initialized;
1852  }
1853 
1854  h->avctx->coded_height = h1->avctx->coded_height;
1855  h->avctx->coded_width = h1->avctx->coded_width;
1856  h->avctx->width = h1->avctx->width;
1857  h->avctx->height = h1->avctx->height;
1858  h->coded_picture_number = h1->coded_picture_number;
1859  h->first_field = h1->first_field;
1860  h->picture_structure = h1->picture_structure;
1861  h->qscale = h1->qscale;
1862  h->droppable = h1->droppable;
1863  h->low_delay = h1->low_delay;
1864 
1865  for (i = 0; h->DPB && i < MAX_PICTURE_COUNT; i++) {
1866  unref_picture(h, &h->DPB[i]);
1867  if (h1->DPB && h1->DPB[i].f.buf[0] &&
1868  (ret = ref_picture(h, &h->DPB[i], &h1->DPB[i])) < 0)
1869  return ret;
1870  }
1871 
1872  h->cur_pic_ptr = REBASE_PICTURE(h1->cur_pic_ptr, h, h1);
1873  unref_picture(h, &h->cur_pic);
1874  if (h1->cur_pic.f.buf[0] && (ret = ref_picture(h, &h->cur_pic, &h1->cur_pic)) < 0)
1875  return ret;
1876 
1877  h->workaround_bugs = h1->workaround_bugs;
1878  h->low_delay = h1->low_delay;
1879  h->droppable = h1->droppable;
1880 
1881  // extradata/NAL handling
1882  h->is_avc = h1->is_avc;
1883 
1884  // SPS/PPS
1885  if ((ret = copy_parameter_set((void **)h->sps_buffers,
1886  (void **)h1->sps_buffers,
1887  MAX_SPS_COUNT, sizeof(SPS))) < 0)
1888  return ret;
1889  h->sps = h1->sps;
1890  if ((ret = copy_parameter_set((void **)h->pps_buffers,
1891  (void **)h1->pps_buffers,
1892  MAX_PPS_COUNT, sizeof(PPS))) < 0)
1893  return ret;
1894  h->pps = h1->pps;
1895 
1896  // Dequantization matrices
1897  // FIXME these are big - can they be only copied when PPS changes?
1898  copy_fields(h, h1, dequant4_buffer, dequant4_coeff);
1899 
1900  for (i = 0; i < 6; i++)
1901  h->dequant4_coeff[i] = h->dequant4_buffer[0] +
1902  (h1->dequant4_coeff[i] - h1->dequant4_buffer[0]);
1903 
1904  for (i = 0; i < 6; i++)
1905  h->dequant8_coeff[i] = h->dequant8_buffer[0] +
1906  (h1->dequant8_coeff[i] - h1->dequant8_buffer[0]);
1907 
1908  h->dequant_coeff_pps = h1->dequant_coeff_pps;
1909 
1910  // POC timing
1911  copy_fields(h, h1, poc_lsb, redundant_pic_count);
1912 
1913  // reference lists
1914  copy_fields(h, h1, short_ref, cabac_init_idc);
1915 
1916  copy_picture_range(h->short_ref, h1->short_ref, 32, h, h1);
1917  copy_picture_range(h->long_ref, h1->long_ref, 32, h, h1);
1918  copy_picture_range(h->delayed_pic, h1->delayed_pic,
1919  MAX_DELAYED_PIC_COUNT + 2, h, h1);
1920 
1921  h->frame_recovered = h1->frame_recovered;
1922 
1923  if (context_reinitialized)
1925 
1926  if (!h->cur_pic_ptr)
1927  return 0;
1928 
1929  if (!h->droppable) {
1931  h->prev_poc_msb = h->poc_msb;
1932  h->prev_poc_lsb = h->poc_lsb;
1933  }
1935  h->prev_frame_num = h->frame_num;
1937 
1938  h->recovery_frame = h1->recovery_frame;
1939 
1940  return err;
1941 }
1942 
1944 {
1945  Picture *pic;
1946  int i, ret;
1947  const int pixel_shift = h->pixel_shift;
1948  int c[4] = {
1949  1<<(h->sps.bit_depth_luma-1),
1950  1<<(h->sps.bit_depth_chroma-1),
1951  1<<(h->sps.bit_depth_chroma-1),
1952  -1
1953  };
1954 
1955  if (!ff_thread_can_start_frame(h->avctx)) {
1956  av_log(h->avctx, AV_LOG_ERROR, "Attempt to start a frame outside SETUP state\n");
1957  return -1;
1958  }
1959 
1961  h->cur_pic_ptr = NULL;
1962 
1963  i = find_unused_picture(h);
1964  if (i < 0) {
1965  av_log(h->avctx, AV_LOG_ERROR, "no frame buffer available\n");
1966  return i;
1967  }
1968  pic = &h->DPB[i];
1969 
1970  pic->reference = h->droppable ? 0 : h->picture_structure;
1973 
1974  /*
1975  * Zero key_frame here; IDR markings per slice in frame or fields are ORed
1976  * in later.
1977  * See decode_nal_units().
1978  */
1979  pic->f.key_frame = 0;
1980  pic->mmco_reset = 0;
1981  pic->recovered = 0;
1982  pic->invalid_gap = 0;
1983 
1984  if ((ret = alloc_picture(h, pic)) < 0)
1985  return ret;
1986  if(!h->frame_recovered && !h->avctx->hwaccel &&
1988  avpriv_color_frame(&pic->f, c);
1989 
1990  h->cur_pic_ptr = pic;
1991  unref_picture(h, &h->cur_pic);
1992  if (CONFIG_ERROR_RESILIENCE) {
1993  h->er.cur_pic = NULL;
1994  }
1995 
1996  if ((ret = ref_picture(h, &h->cur_pic, h->cur_pic_ptr)) < 0)
1997  return ret;
1998 
1999  if (CONFIG_ERROR_RESILIENCE) {
2000  ff_er_frame_start(&h->er);
2001  h->er.last_pic =
2002  h->er.next_pic = NULL;
2003  }
2004 
2005  assert(h->linesize && h->uvlinesize);
2006 
2007  for (i = 0; i < 16; i++) {
2008  h->block_offset[i] = (4 * ((scan8[i] - scan8[0]) & 7) << pixel_shift) + 4 * h->linesize * ((scan8[i] - scan8[0]) >> 3);
2009  h->block_offset[48 + i] = (4 * ((scan8[i] - scan8[0]) & 7) << pixel_shift) + 8 * h->linesize * ((scan8[i] - scan8[0]) >> 3);
2010  }
2011  for (i = 0; i < 16; i++) {
2012  h->block_offset[16 + i] =
2013  h->block_offset[32 + i] = (4 * ((scan8[i] - scan8[0]) & 7) << pixel_shift) + 4 * h->uvlinesize * ((scan8[i] - scan8[0]) >> 3);
2014  h->block_offset[48 + 16 + i] =
2015  h->block_offset[48 + 32 + i] = (4 * ((scan8[i] - scan8[0]) & 7) << pixel_shift) + 8 * h->uvlinesize * ((scan8[i] - scan8[0]) >> 3);
2016  }
2017 
2018  // s->decode = (h->flags & CODEC_FLAG_PSNR) || !s->encoding ||
2019  // h->cur_pic.reference /* || h->contains_intra */ || 1;
2020 
2021  /* We mark the current picture as non-reference after allocating it, so
2022  * that if we break out due to an error it can be released automatically
2023  * in the next ff_MPV_frame_start().
2024  */
2025  h->cur_pic_ptr->reference = 0;
2026 
2027  h->cur_pic_ptr->field_poc[0] = h->cur_pic_ptr->field_poc[1] = INT_MAX;
2028 
2029  h->next_output_pic = NULL;
2030 
2031  assert(h->cur_pic_ptr->long_ref == 0);
2032 
2033  return 0;
2034 }
2035 
2036 /**
2037  * Run setup operations that must be run after slice header decoding.
2038  * This includes finding the next displayed frame.
2039  *
2040  * @param h h264 master context
2041  * @param setup_finished enough NALs have been read that we can call
2042  * ff_thread_finish_setup()
2043  */
2044 static void decode_postinit(H264Context *h, int setup_finished)
2045 {
2046  Picture *out = h->cur_pic_ptr;
2047  Picture *cur = h->cur_pic_ptr;
2048  int i, pics, out_of_order, out_idx;
2049 
2050  h->cur_pic_ptr->f.pict_type = h->pict_type;
2051 
2052  if (h->next_output_pic)
2053  return;
2054 
2055  if (cur->field_poc[0] == INT_MAX || cur->field_poc[1] == INT_MAX) {
2056  /* FIXME: if we have two PAFF fields in one packet, we can't start
2057  * the next thread here. If we have one field per packet, we can.
2058  * The check in decode_nal_units() is not good enough to find this
2059  * yet, so we assume the worst for now. */
2060  // if (setup_finished)
2061  // ff_thread_finish_setup(h->avctx);
2062  return;
2063  }
2064 
2065  cur->f.interlaced_frame = 0;
2066  cur->f.repeat_pict = 0;
2067 
2068  /* Signal interlacing information externally. */
2069  /* Prioritize picture timing SEI information over used
2070  * decoding process if it exists. */
2071 
2072  if (h->sps.pic_struct_present_flag) {
2073  switch (h->sei_pic_struct) {
2074  case SEI_PIC_STRUCT_FRAME:
2075  break;
2078  cur->f.interlaced_frame = 1;
2079  break;
2082  if (FIELD_OR_MBAFF_PICTURE(h))
2083  cur->f.interlaced_frame = 1;
2084  else
2085  // try to flag soft telecine progressive
2087  break;
2090  /* Signal the possibility of telecined film externally
2091  * (pic_struct 5,6). From these hints, let the applications
2092  * decide if they apply deinterlacing. */
2093  cur->f.repeat_pict = 1;
2094  break;
2096  cur->f.repeat_pict = 2;
2097  break;
2099  cur->f.repeat_pict = 4;
2100  break;
2101  }
2102 
2103  if ((h->sei_ct_type & 3) &&
2105  cur->f.interlaced_frame = (h->sei_ct_type & (1 << 1)) != 0;
2106  } else {
2107  /* Derive interlacing flag from used decoding process. */
2109  }
2111 
2112  if (cur->field_poc[0] != cur->field_poc[1]) {
2113  /* Derive top_field_first from field pocs. */
2114  cur->f.top_field_first = cur->field_poc[0] < cur->field_poc[1];
2115  } else {
2116  if (cur->f.interlaced_frame || h->sps.pic_struct_present_flag) {
2117  /* Use picture timing SEI information. Even if it is a
2118  * information of a past frame, better than nothing. */
2121  cur->f.top_field_first = 1;
2122  else
2123  cur->f.top_field_first = 0;
2124  } else {
2125  /* Most likely progressive */
2126  cur->f.top_field_first = 0;
2127  }
2128  }
2129 
2130  if (h->sei_frame_packing_present &&
2133  h->content_interpretation_type > 0 &&
2134  h->content_interpretation_type < 3) {
2135  AVStereo3D *stereo = av_stereo3d_create_side_data(&cur->f);
2136  if (!stereo)
2137  return;
2138 
2139  switch (h->frame_packing_arrangement_type) {
2140  case 0:
2141  stereo->type = AV_STEREO3D_CHECKERBOARD;
2142  break;
2143  case 1:
2144  stereo->type = AV_STEREO3D_LINES;
2145  break;
2146  case 2:
2147  stereo->type = AV_STEREO3D_COLUMNS;
2148  break;
2149  case 3:
2150  if (h->quincunx_subsampling)
2152  else
2153  stereo->type = AV_STEREO3D_SIDEBYSIDE;
2154  break;
2155  case 4:
2156  stereo->type = AV_STEREO3D_TOPBOTTOM;
2157  break;
2158  case 5:
2159  stereo->type = AV_STEREO3D_FRAMESEQUENCE;
2160  break;
2161  case 6:
2162  stereo->type = AV_STEREO3D_2D;
2163  break;
2164  }
2165 
2166  if (h->content_interpretation_type == 2)
2167  stereo->flags = AV_STEREO3D_FLAG_INVERT;
2168  }
2169 
2170  cur->mmco_reset = h->mmco_reset;
2171  h->mmco_reset = 0;
2172 
2173  // FIXME do something with unavailable reference frames
2174 
2175  /* Sort B-frames into display order */
2176 
2180  h->low_delay = 0;
2181  }
2182 
2186  h->low_delay = 0;
2187  }
2188 
2189  for (i = 0; 1; i++) {
2190  if(i == MAX_DELAYED_PIC_COUNT || cur->poc < h->last_pocs[i]){
2191  if(i)
2192  h->last_pocs[i-1] = cur->poc;
2193  break;
2194  } else if(i) {
2195  h->last_pocs[i-1]= h->last_pocs[i];
2196  }
2197  }
2198  out_of_order = MAX_DELAYED_PIC_COUNT - i;
2199  if( cur->f.pict_type == AV_PICTURE_TYPE_B
2201  out_of_order = FFMAX(out_of_order, 1);
2202  if (out_of_order == MAX_DELAYED_PIC_COUNT) {
2203  av_log(h->avctx, AV_LOG_VERBOSE, "Invalid POC %d<%d\n", cur->poc, h->last_pocs[0]);
2204  for (i = 1; i < MAX_DELAYED_PIC_COUNT; i++)
2205  h->last_pocs[i] = INT_MIN;
2206  h->last_pocs[0] = cur->poc;
2207  cur->mmco_reset = 1;
2208  } else if(h->avctx->has_b_frames < out_of_order && !h->sps.bitstream_restriction_flag){
2209  av_log(h->avctx, AV_LOG_VERBOSE, "Increasing reorder buffer to %d\n", out_of_order);
2210  h->avctx->has_b_frames = out_of_order;
2211  h->low_delay = 0;
2212  }
2213 
2214  pics = 0;
2215  while (h->delayed_pic[pics])
2216  pics++;
2217 
2219 
2220  h->delayed_pic[pics++] = cur;
2221  if (cur->reference == 0)
2222  cur->reference = DELAYED_PIC_REF;
2223 
2224  out = h->delayed_pic[0];
2225  out_idx = 0;
2226  for (i = 1; h->delayed_pic[i] &&
2227  !h->delayed_pic[i]->f.key_frame &&
2228  !h->delayed_pic[i]->mmco_reset;
2229  i++)
2230  if (h->delayed_pic[i]->poc < out->poc) {
2231  out = h->delayed_pic[i];
2232  out_idx = i;
2233  }
2234  if (h->avctx->has_b_frames == 0 &&
2235  (h->delayed_pic[0]->f.key_frame || h->delayed_pic[0]->mmco_reset))
2236  h->next_outputed_poc = INT_MIN;
2237  out_of_order = out->poc < h->next_outputed_poc;
2238 
2239  if (out_of_order || pics > h->avctx->has_b_frames) {
2240  out->reference &= ~DELAYED_PIC_REF;
2241  // for frame threading, the owner must be the second field's thread or
2242  // else the first thread can release the picture and reuse it unsafely
2243  for (i = out_idx; h->delayed_pic[i]; i++)
2244  h->delayed_pic[i] = h->delayed_pic[i + 1];
2245  }
2246  if (!out_of_order && pics > h->avctx->has_b_frames) {
2247  h->next_output_pic = out;
2248  if (out_idx == 0 && h->delayed_pic[0] && (h->delayed_pic[0]->f.key_frame || h->delayed_pic[0]->mmco_reset)) {
2249  h->next_outputed_poc = INT_MIN;
2250  } else
2251  h->next_outputed_poc = out->poc;
2252  } else {
2253  av_log(h->avctx, AV_LOG_DEBUG, "no picture %s\n", out_of_order ? "ooo" : "");
2254  }
2255 
2256  if (h->next_output_pic) {
2257  if (h->next_output_pic->recovered) {
2258  // We have reached an recovery point and all frames after it in
2259  // display order are "recovered".
2261  }
2263  }
2264 
2265  if (setup_finished && !h->avctx->hwaccel)
2267 }
2268 
2270  uint8_t *src_cb, uint8_t *src_cr,
2271  int linesize, int uvlinesize,
2272  int simple)
2273 {
2274  uint8_t *top_border;
2275  int top_idx = 1;
2276  const int pixel_shift = h->pixel_shift;
2277  int chroma444 = CHROMA444(h);
2278  int chroma422 = CHROMA422(h);
2279 
2280  src_y -= linesize;
2281  src_cb -= uvlinesize;
2282  src_cr -= uvlinesize;
2283 
2284  if (!simple && FRAME_MBAFF(h)) {
2285  if (h->mb_y & 1) {
2286  if (!MB_MBAFF(h)) {
2287  top_border = h->top_borders[0][h->mb_x];
2288  AV_COPY128(top_border, src_y + 15 * linesize);
2289  if (pixel_shift)
2290  AV_COPY128(top_border + 16, src_y + 15 * linesize + 16);
2291  if (simple || !CONFIG_GRAY || !(h->flags & CODEC_FLAG_GRAY)) {
2292  if (chroma444) {
2293  if (pixel_shift) {
2294  AV_COPY128(top_border + 32, src_cb + 15 * uvlinesize);
2295  AV_COPY128(top_border + 48, src_cb + 15 * uvlinesize + 16);
2296  AV_COPY128(top_border + 64, src_cr + 15 * uvlinesize);
2297  AV_COPY128(top_border + 80, src_cr + 15 * uvlinesize + 16);
2298  } else {
2299  AV_COPY128(top_border + 16, src_cb + 15 * uvlinesize);
2300  AV_COPY128(top_border + 32, src_cr + 15 * uvlinesize);
2301  }
2302  } else if (chroma422) {
2303  if (pixel_shift) {
2304  AV_COPY128(top_border + 32, src_cb + 15 * uvlinesize);
2305  AV_COPY128(top_border + 48, src_cr + 15 * uvlinesize);
2306  } else {
2307  AV_COPY64(top_border + 16, src_cb + 15 * uvlinesize);
2308  AV_COPY64(top_border + 24, src_cr + 15 * uvlinesize);
2309  }
2310  } else {
2311  if (pixel_shift) {
2312  AV_COPY128(top_border + 32, src_cb + 7 * uvlinesize);
2313  AV_COPY128(top_border + 48, src_cr + 7 * uvlinesize);
2314  } else {
2315  AV_COPY64(top_border + 16, src_cb + 7 * uvlinesize);
2316  AV_COPY64(top_border + 24, src_cr + 7 * uvlinesize);
2317  }
2318  }
2319  }
2320  }
2321  } else if (MB_MBAFF(h)) {
2322  top_idx = 0;
2323  } else
2324  return;
2325  }
2326 
2327  top_border = h->top_borders[top_idx][h->mb_x];
2328  /* There are two lines saved, the line above the top macroblock
2329  * of a pair, and the line above the bottom macroblock. */
2330  AV_COPY128(top_border, src_y + 16 * linesize);
2331  if (pixel_shift)
2332  AV_COPY128(top_border + 16, src_y + 16 * linesize + 16);
2333 
2334  if (simple || !CONFIG_GRAY || !(h->flags & CODEC_FLAG_GRAY)) {
2335  if (chroma444) {
2336  if (pixel_shift) {
2337  AV_COPY128(top_border + 32, src_cb + 16 * linesize);
2338  AV_COPY128(top_border + 48, src_cb + 16 * linesize + 16);
2339  AV_COPY128(top_border + 64, src_cr + 16 * linesize);
2340  AV_COPY128(top_border + 80, src_cr + 16 * linesize + 16);
2341  } else {
2342  AV_COPY128(top_border + 16, src_cb + 16 * linesize);
2343  AV_COPY128(top_border + 32, src_cr + 16 * linesize);
2344  }
2345  } else if (chroma422) {
2346  if (pixel_shift) {
2347  AV_COPY128(top_border + 32, src_cb + 16 * uvlinesize);
2348  AV_COPY128(top_border + 48, src_cr + 16 * uvlinesize);
2349  } else {
2350  AV_COPY64(top_border + 16, src_cb + 16 * uvlinesize);
2351  AV_COPY64(top_border + 24, src_cr + 16 * uvlinesize);
2352  }
2353  } else {
2354  if (pixel_shift) {
2355  AV_COPY128(top_border + 32, src_cb + 8 * uvlinesize);
2356  AV_COPY128(top_border + 48, src_cr + 8 * uvlinesize);
2357  } else {
2358  AV_COPY64(top_border + 16, src_cb + 8 * uvlinesize);
2359  AV_COPY64(top_border + 24, src_cr + 8 * uvlinesize);
2360  }
2361  }
2362  }
2363 }
2364 
2366  uint8_t *src_cb, uint8_t *src_cr,
2367  int linesize, int uvlinesize,
2368  int xchg, int chroma444,
2369  int simple, int pixel_shift)
2370 {
2371  int deblock_topleft;
2372  int deblock_top;
2373  int top_idx = 1;
2374  uint8_t *top_border_m1;
2375  uint8_t *top_border;
2376 
2377  if (!simple && FRAME_MBAFF(h)) {
2378  if (h->mb_y & 1) {
2379  if (!MB_MBAFF(h))
2380  return;
2381  } else {
2382  top_idx = MB_MBAFF(h) ? 0 : 1;
2383  }
2384  }
2385 
2386  if (h->deblocking_filter == 2) {
2387  deblock_topleft = h->slice_table[h->mb_xy - 1 - h->mb_stride] == h->slice_num;
2388  deblock_top = h->top_type;
2389  } else {
2390  deblock_topleft = (h->mb_x > 0);
2391  deblock_top = (h->mb_y > !!MB_FIELD(h));
2392  }
2393 
2394  src_y -= linesize + 1 + pixel_shift;
2395  src_cb -= uvlinesize + 1 + pixel_shift;
2396  src_cr -= uvlinesize + 1 + pixel_shift;
2397 
2398  top_border_m1 = h->top_borders[top_idx][h->mb_x - 1];
2399  top_border = h->top_borders[top_idx][h->mb_x];
2400 
2401 #define XCHG(a, b, xchg) \
2402  if (pixel_shift) { \
2403  if (xchg) { \
2404  AV_SWAP64(b + 0, a + 0); \
2405  AV_SWAP64(b + 8, a + 8); \
2406  } else { \
2407  AV_COPY128(b, a); \
2408  } \
2409  } else if (xchg) \
2410  AV_SWAP64(b, a); \
2411  else \
2412  AV_COPY64(b, a);
2413 
2414  if (deblock_top) {
2415  if (deblock_topleft) {
2416  XCHG(top_border_m1 + (8 << pixel_shift),
2417  src_y - (7 << pixel_shift), 1);
2418  }
2419  XCHG(top_border + (0 << pixel_shift), src_y + (1 << pixel_shift), xchg);
2420  XCHG(top_border + (8 << pixel_shift), src_y + (9 << pixel_shift), 1);
2421  if (h->mb_x + 1 < h->mb_width) {
2422  XCHG(h->top_borders[top_idx][h->mb_x + 1],
2423  src_y + (17 << pixel_shift), 1);
2424  }
2425  if (simple || !CONFIG_GRAY || !(h->flags & CODEC_FLAG_GRAY)) {
2426  if (chroma444) {
2427  if (deblock_topleft) {
2428  XCHG(top_border_m1 + (24 << pixel_shift), src_cb - (7 << pixel_shift), 1);
2429  XCHG(top_border_m1 + (40 << pixel_shift), src_cr - (7 << pixel_shift), 1);
2430  }
2431  XCHG(top_border + (16 << pixel_shift), src_cb + (1 << pixel_shift), xchg);
2432  XCHG(top_border + (24 << pixel_shift), src_cb + (9 << pixel_shift), 1);
2433  XCHG(top_border + (32 << pixel_shift), src_cr + (1 << pixel_shift), xchg);
2434  XCHG(top_border + (40 << pixel_shift), src_cr + (9 << pixel_shift), 1);
2435  if (h->mb_x + 1 < h->mb_width) {
2436  XCHG(h->top_borders[top_idx][h->mb_x + 1] + (16 << pixel_shift), src_cb + (17 << pixel_shift), 1);
2437  XCHG(h->top_borders[top_idx][h->mb_x + 1] + (32 << pixel_shift), src_cr + (17 << pixel_shift), 1);
2438  }
2439  } else {
2440  if (deblock_topleft) {
2441  XCHG(top_border_m1 + (16 << pixel_shift), src_cb - (7 << pixel_shift), 1);
2442  XCHG(top_border_m1 + (24 << pixel_shift), src_cr - (7 << pixel_shift), 1);
2443  }
2444  XCHG(top_border + (16 << pixel_shift), src_cb + 1 + pixel_shift, 1);
2445  XCHG(top_border + (24 << pixel_shift), src_cr + 1 + pixel_shift, 1);
2446  }
2447  }
2448  }
2449 }
2450 
2451 static av_always_inline int dctcoef_get(int16_t *mb, int high_bit_depth,
2452  int index)
2453 {
2454  if (high_bit_depth) {
2455  return AV_RN32A(((int32_t *)mb) + index);
2456  } else
2457  return AV_RN16A(mb + index);
2458 }
2459 
2460 static av_always_inline void dctcoef_set(int16_t *mb, int high_bit_depth,
2461  int index, int value)
2462 {
2463  if (high_bit_depth) {
2464  AV_WN32A(((int32_t *)mb) + index, value);
2465  } else
2466  AV_WN16A(mb + index, value);
2467 }
2468 
2470  int mb_type, int is_h264,
2471  int simple,
2472  int transform_bypass,
2473  int pixel_shift,
2474  int *block_offset,
2475  int linesize,
2476  uint8_t *dest_y, int p)
2477 {
2478  void (*idct_add)(uint8_t *dst, int16_t *block, int stride);
2479  void (*idct_dc_add)(uint8_t *dst, int16_t *block, int stride);
2480  int i;
2481  int qscale = p == 0 ? h->qscale : h->chroma_qp[p - 1];
2482  block_offset += 16 * p;
2483  if (IS_INTRA4x4(mb_type)) {
2484  if (IS_8x8DCT(mb_type)) {
2485  if (transform_bypass) {
2486  idct_dc_add =
2488  } else {
2489  idct_dc_add = h->h264dsp.h264_idct8_dc_add;
2491  }
2492  for (i = 0; i < 16; i += 4) {
2493  uint8_t *const ptr = dest_y + block_offset[i];
2494  const int dir = h->intra4x4_pred_mode_cache[scan8[i]];
2495  if (transform_bypass && h->sps.profile_idc == 244 && dir <= 1) {
2496  h->hpc.pred8x8l_add[dir](ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize);
2497  } else {
2498  const int nnz = h->non_zero_count_cache[scan8[i + p * 16]];
2499  h->hpc.pred8x8l[dir](ptr, (h->topleft_samples_available << i) & 0x8000,
2500  (h->topright_samples_available << i) & 0x4000, linesize);
2501  if (nnz) {
2502  if (nnz == 1 && dctcoef_get(h->mb, pixel_shift, i * 16 + p * 256))
2503  idct_dc_add(ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize);
2504  else
2505  idct_add(ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize);
2506  }
2507  }
2508  }
2509  } else {
2510  if (transform_bypass) {
2511  idct_dc_add =
2513  } else {
2514  idct_dc_add = h->h264dsp.h264_idct_dc_add;
2516  }
2517  for (i = 0; i < 16; i++) {
2518  uint8_t *const ptr = dest_y + block_offset[i];
2519  const int dir = h->intra4x4_pred_mode_cache[scan8[i]];
2520 
2521  if (transform_bypass && h->sps.profile_idc == 244 && dir <= 1) {
2522  h->hpc.pred4x4_add[dir](ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize);
2523  } else {
2524  uint8_t *topright;
2525  int nnz, tr;
2526  uint64_t tr_high;
2527  if (dir == DIAG_DOWN_LEFT_PRED || dir == VERT_LEFT_PRED) {
2528  const int topright_avail = (h->topright_samples_available << i) & 0x8000;
2529  av_assert2(h->mb_y || linesize <= block_offset[i]);
2530  if (!topright_avail) {
2531  if (pixel_shift) {
2532  tr_high = ((uint16_t *)ptr)[3 - linesize / 2] * 0x0001000100010001ULL;
2533  topright = (uint8_t *)&tr_high;
2534  } else {
2535  tr = ptr[3 - linesize] * 0x01010101u;
2536  topright = (uint8_t *)&tr;
2537  }
2538  } else
2539  topright = ptr + (4 << pixel_shift) - linesize;
2540  } else
2541  topright = NULL;
2542 
2543  h->hpc.pred4x4[dir](ptr, topright, linesize);
2544  nnz = h->non_zero_count_cache[scan8[i + p * 16]];
2545  if (nnz) {
2546  if (is_h264) {
2547  if (nnz == 1 && dctcoef_get(h->mb, pixel_shift, i * 16 + p * 256))
2548  idct_dc_add(ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize);
2549  else
2550  idct_add(ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize);
2551  } else if (CONFIG_SVQ3_DECODER)
2552  ff_svq3_add_idct_c(ptr, h->mb + i * 16 + p * 256, linesize, qscale, 0);
2553  }
2554  }
2555  }
2556  }
2557  } else {
2558  h->hpc.pred16x16[h->intra16x16_pred_mode](dest_y, linesize);
2559  if (is_h264) {
2561  if (!transform_bypass)
2562  h->h264dsp.h264_luma_dc_dequant_idct(h->mb + (p * 256 << pixel_shift),
2563  h->mb_luma_dc[p],
2564  h->dequant4_coeff[p][qscale][0]);
2565  else {
2566  static const uint8_t dc_mapping[16] = {
2567  0 * 16, 1 * 16, 4 * 16, 5 * 16,
2568  2 * 16, 3 * 16, 6 * 16, 7 * 16,
2569  8 * 16, 9 * 16, 12 * 16, 13 * 16,
2570  10 * 16, 11 * 16, 14 * 16, 15 * 16
2571  };
2572  for (i = 0; i < 16; i++)
2573  dctcoef_set(h->mb + (p * 256 << pixel_shift),
2574  pixel_shift, dc_mapping[i],
2575  dctcoef_get(h->mb_luma_dc[p],
2576  pixel_shift, i));
2577  }
2578  }
2579  } else if (CONFIG_SVQ3_DECODER)
2580  ff_svq3_luma_dc_dequant_idct_c(h->mb + p * 256,
2581  h->mb_luma_dc[p], qscale);
2582  }
2583 }
2584 
2586  int is_h264, int simple,
2587  int transform_bypass,
2588  int pixel_shift,
2589  int *block_offset,
2590  int linesize,
2591  uint8_t *dest_y, int p)
2592 {
2593  void (*idct_add)(uint8_t *dst, int16_t *block, int stride);
2594  int i;
2595  block_offset += 16 * p;
2596  if (!IS_INTRA4x4(mb_type)) {
2597  if (is_h264) {
2598  if (IS_INTRA16x16(mb_type)) {
2599  if (transform_bypass) {
2600  if (h->sps.profile_idc == 244 &&
2603  h->hpc.pred16x16_add[h->intra16x16_pred_mode](dest_y, block_offset,
2604  h->mb + (p * 256 << pixel_shift),
2605  linesize);
2606  } else {
2607  for (i = 0; i < 16; i++)
2608  if (h->non_zero_count_cache[scan8[i + p * 16]] ||
2609  dctcoef_get(h->mb, pixel_shift, i * 16 + p * 256))
2610  h->h264dsp.h264_add_pixels4_clear(dest_y + block_offset[i],
2611  h->mb + (i * 16 + p * 256 << pixel_shift),
2612  linesize);
2613  }
2614  } else {
2615  h->h264dsp.h264_idct_add16intra(dest_y, block_offset,
2616  h->mb + (p * 256 << pixel_shift),
2617  linesize,
2618  h->non_zero_count_cache + p * 5 * 8);
2619  }
2620  } else if (h->cbp & 15) {
2621  if (transform_bypass) {
2622  const int di = IS_8x8DCT(mb_type) ? 4 : 1;
2625  for (i = 0; i < 16; i += di)
2626  if (h->non_zero_count_cache[scan8[i + p * 16]])
2627  idct_add(dest_y + block_offset[i],
2628  h->mb + (i * 16 + p * 256 << pixel_shift),
2629  linesize);
2630  } else {
2631  if (IS_8x8DCT(mb_type))
2632  h->h264dsp.h264_idct8_add4(dest_y, block_offset,
2633  h->mb + (p * 256 << pixel_shift),
2634  linesize,
2635  h->non_zero_count_cache + p * 5 * 8);
2636  else
2637  h->h264dsp.h264_idct_add16(dest_y, block_offset,
2638  h->mb + (p * 256 << pixel_shift),
2639  linesize,
2640  h->non_zero_count_cache + p * 5 * 8);
2641  }
2642  }
2643  } else if (CONFIG_SVQ3_DECODER) {
2644  for (i = 0; i < 16; i++)
2645  if (h->non_zero_count_cache[scan8[i + p * 16]] || h->mb[i * 16 + p * 256]) {
2646  // FIXME benchmark weird rule, & below
2647  uint8_t *const ptr = dest_y + block_offset[i];
2648  ff_svq3_add_idct_c(ptr, h->mb + i * 16 + p * 256, linesize,
2649  h->qscale, IS_INTRA(mb_type) ? 1 : 0);
2650  }
2651  }
2652  }
2653 }
2654 
2655 #define BITS 8
2656 #define SIMPLE 1
2657 #include "h264_mb_template.c"
2658 
2659 #undef BITS
2660 #define BITS 16
2661 #include "h264_mb_template.c"
2662 
2663 #undef SIMPLE
2664 #define SIMPLE 0
2665 #include "h264_mb_template.c"
2666 
2668 {
2669  const int mb_xy = h->mb_xy;
2670  const int mb_type = h->cur_pic.mb_type[mb_xy];
2671  int is_complex = CONFIG_SMALL || h->is_complex ||
2672  IS_INTRA_PCM(mb_type) || h->qscale == 0;
2673 
2674  if (CHROMA444(h)) {
2675  if (is_complex || h->pixel_shift)
2676  hl_decode_mb_444_complex(h);
2677  else
2678  hl_decode_mb_444_simple_8(h);
2679  } else if (is_complex) {
2680  hl_decode_mb_complex(h);
2681  } else if (h->pixel_shift) {
2682  hl_decode_mb_simple_16(h);
2683  } else
2684  hl_decode_mb_simple_8(h);
2685 }
2686 
2688 {
2689  int list, i;
2690  int luma_def, chroma_def;
2691 
2692  h->use_weight = 0;
2693  h->use_weight_chroma = 0;
2695  if (h->sps.chroma_format_idc)
2697  luma_def = 1 << h->luma_log2_weight_denom;
2698  chroma_def = 1 << h->chroma_log2_weight_denom;
2699 
2700  for (list = 0; list < 2; list++) {
2701  h->luma_weight_flag[list] = 0;
2702  h->chroma_weight_flag[list] = 0;
2703  for (i = 0; i < h->ref_count[list]; i++) {
2704  int luma_weight_flag, chroma_weight_flag;
2705 
2706  luma_weight_flag = get_bits1(&h->gb);
2707  if (luma_weight_flag) {
2708  h->luma_weight[i][list][0] = get_se_golomb(&h->gb);
2709  h->luma_weight[i][list][1] = get_se_golomb(&h->gb);
2710  if (h->luma_weight[i][list][0] != luma_def ||
2711  h->luma_weight[i][list][1] != 0) {
2712  h->use_weight = 1;
2713  h->luma_weight_flag[list] = 1;
2714  }
2715  } else {
2716  h->luma_weight[i][list][0] = luma_def;
2717  h->luma_weight[i][list][1] = 0;
2718  }
2719 
2720  if (h->sps.chroma_format_idc) {
2721  chroma_weight_flag = get_bits1(&h->gb);
2722  if (chroma_weight_flag) {
2723  int j;
2724  for (j = 0; j < 2; j++) {
2725  h->chroma_weight[i][list][j][0] = get_se_golomb(&h->gb);
2726  h->chroma_weight[i][list][j][1] = get_se_golomb(&h->gb);
2727  if (h->chroma_weight[i][list][j][0] != chroma_def ||
2728  h->chroma_weight[i][list][j][1] != 0) {
2729  h->use_weight_chroma = 1;
2730  h->chroma_weight_flag[list] = 1;
2731  }
2732  }
2733  } else {
2734  int j;
2735  for (j = 0; j < 2; j++) {
2736  h->chroma_weight[i][list][j][0] = chroma_def;
2737  h->chroma_weight[i][list][j][1] = 0;
2738  }
2739  }
2740  }
2741  }
2743  break;
2744  }
2745  h->use_weight = h->use_weight || h->use_weight_chroma;
2746  return 0;
2747 }
2748 
2749 /**
2750  * Initialize implicit_weight table.
2751  * @param field 0/1 initialize the weight for interlaced MBAFF
2752  * -1 initializes the rest
2753  */
2754 static void implicit_weight_table(H264Context *h, int field)
2755 {
2756  int ref0, ref1, i, cur_poc, ref_start, ref_count0, ref_count1;
2757 
2758  for (i = 0; i < 2; i++) {
2759  h->luma_weight_flag[i] = 0;
2760  h->chroma_weight_flag[i] = 0;
2761  }
2762 
2763  if (field < 0) {
2764  if (h->picture_structure == PICT_FRAME) {
2765  cur_poc = h->cur_pic_ptr->poc;
2766  } else {
2767  cur_poc = h->cur_pic_ptr->field_poc[h->picture_structure - 1];
2768  }
2769  if (h->ref_count[0] == 1 && h->ref_count[1] == 1 && !FRAME_MBAFF(h) &&
2770  h->ref_list[0][0].poc + h->ref_list[1][0].poc == 2 * cur_poc) {
2771  h->use_weight = 0;
2772  h->use_weight_chroma = 0;
2773  return;
2774  }
2775  ref_start = 0;
2776  ref_count0 = h->ref_count[0];
2777  ref_count1 = h->ref_count[1];
2778  } else {
2779  cur_poc = h->cur_pic_ptr->field_poc[field];
2780  ref_start = 16;
2781  ref_count0 = 16 + 2 * h->ref_count[0];
2782  ref_count1 = 16 + 2 * h->ref_count[1];
2783  }
2784 
2785  h->use_weight = 2;
2786  h->use_weight_chroma = 2;
2787  h->luma_log2_weight_denom = 5;
2788  h->chroma_log2_weight_denom = 5;
2789 
2790  for (ref0 = ref_start; ref0 < ref_count0; ref0++) {
2791  int poc0 = h->ref_list[0][ref0].poc;
2792  for (ref1 = ref_start; ref1 < ref_count1; ref1++) {
2793  int w = 32;
2794  if (!h->ref_list[0][ref0].long_ref && !h->ref_list[1][ref1].long_ref) {
2795  int poc1 = h->ref_list[1][ref1].poc;
2796  int td = av_clip(poc1 - poc0, -128, 127);
2797  if (td) {
2798  int tb = av_clip(cur_poc - poc0, -128, 127);
2799  int tx = (16384 + (FFABS(td) >> 1)) / td;
2800  int dist_scale_factor = (tb * tx + 32) >> 8;
2801  if (dist_scale_factor >= -64 && dist_scale_factor <= 128)
2802  w = 64 - dist_scale_factor;
2803  }
2804  }
2805  if (field < 0) {
2806  h->implicit_weight[ref0][ref1][0] =
2807  h->implicit_weight[ref0][ref1][1] = w;
2808  } else {
2809  h->implicit_weight[ref0][ref1][field] = w;
2810  }
2811  }
2812  }
2813 }
2814 
2815 /**
2816  * instantaneous decoder refresh.
2817  */
2818 static void idr(H264Context *h)
2819 {
2820  int i;
2822  h->prev_frame_num = 0;
2823  h->prev_frame_num_offset = 0;
2824  h->prev_poc_msb = 1<<16;
2825  h->prev_poc_lsb = 0;
2826  for (i = 0; i < MAX_DELAYED_PIC_COUNT; i++)
2827  h->last_pocs[i] = INT_MIN;
2828 }
2829 
2830 /* forget old pics after a seek */
2831 static void flush_change(H264Context *h)
2832 {
2833  int i, j;
2834 
2835  h->outputed_poc = h->next_outputed_poc = INT_MIN;
2836  h->prev_interlaced_frame = 1;
2837  idr(h);
2838 
2839  h->prev_frame_num = -1;
2840  if (h->cur_pic_ptr) {
2841  h->cur_pic_ptr->reference = 0;
2842  for (j=i=0; h->delayed_pic[i]; i++)
2843  if (h->delayed_pic[i] != h->cur_pic_ptr)
2844  h->delayed_pic[j++] = h->delayed_pic[i];
2845  h->delayed_pic[j] = NULL;
2846  }
2847  h->first_field = 0;
2848  memset(h->ref_list[0], 0, sizeof(h->ref_list[0]));
2849  memset(h->ref_list[1], 0, sizeof(h->ref_list[1]));
2850  memset(h->default_ref_list[0], 0, sizeof(h->default_ref_list[0]));
2851  memset(h->default_ref_list[1], 0, sizeof(h->default_ref_list[1]));
2852  ff_h264_reset_sei(h);
2853  h->recovery_frame = -1;
2854  h->frame_recovered = 0;
2855  h->list_count = 0;
2856  h->current_slice = 0;
2857  h->mmco_reset = 1;
2858 }
2859 
2860 /* forget old pics after a seek */
2861 static void flush_dpb(AVCodecContext *avctx)
2862 {
2863  H264Context *h = avctx->priv_data;
2864  int i;
2865 
2866  for (i = 0; i <= MAX_DELAYED_PIC_COUNT; i++) {
2867  if (h->delayed_pic[i])
2868  h->delayed_pic[i]->reference = 0;
2869  h->delayed_pic[i] = NULL;
2870  }
2871 
2872  flush_change(h);
2873 
2874  if (h->DPB)
2875  for (i = 0; i < MAX_PICTURE_COUNT; i++)
2876  unref_picture(h, &h->DPB[i]);
2877  h->cur_pic_ptr = NULL;
2878  unref_picture(h, &h->cur_pic);
2879 
2880  h->mb_x = h->mb_y = 0;
2881 
2882  h->parse_context.state = -1;
2884  h->parse_context.overread = 0;
2886  h->parse_context.index = 0;
2887  h->parse_context.last_index = 0;
2888 
2889  free_tables(h, 1);
2890  h->context_initialized = 0;
2891 }
2892 
2893 int ff_init_poc(H264Context *h, int pic_field_poc[2], int *pic_poc)
2894 {
2895  const int max_frame_num = 1 << h->sps.log2_max_frame_num;
2896  int field_poc[2];
2897 
2899  if (h->frame_num < h->prev_frame_num)
2900  h->frame_num_offset += max_frame_num;
2901 
2902  if (h->sps.poc_type == 0) {
2903  const int max_poc_lsb = 1 << h->sps.log2_max_poc_lsb;
2904 
2905  if (h->poc_lsb < h->prev_poc_lsb &&
2906  h->prev_poc_lsb - h->poc_lsb >= max_poc_lsb / 2)
2907  h->poc_msb = h->prev_poc_msb + max_poc_lsb;
2908  else if (h->poc_lsb > h->prev_poc_lsb &&
2909  h->prev_poc_lsb - h->poc_lsb < -max_poc_lsb / 2)
2910  h->poc_msb = h->prev_poc_msb - max_poc_lsb;
2911  else
2912  h->poc_msb = h->prev_poc_msb;
2913  field_poc[0] =
2914  field_poc[1] = h->poc_msb + h->poc_lsb;
2915  if (h->picture_structure == PICT_FRAME)
2916  field_poc[1] += h->delta_poc_bottom;
2917  } else if (h->sps.poc_type == 1) {
2918  int abs_frame_num, expected_delta_per_poc_cycle, expectedpoc;
2919  int i;
2920 
2921  if (h->sps.poc_cycle_length != 0)
2922  abs_frame_num = h->frame_num_offset + h->frame_num;
2923  else
2924  abs_frame_num = 0;
2925 
2926  if (h->nal_ref_idc == 0 && abs_frame_num > 0)
2927  abs_frame_num--;
2928 
2929  expected_delta_per_poc_cycle = 0;
2930  for (i = 0; i < h->sps.poc_cycle_length; i++)
2931  // FIXME integrate during sps parse
2932  expected_delta_per_poc_cycle += h->sps.offset_for_ref_frame[i];
2933 
2934  if (abs_frame_num > 0) {
2935  int poc_cycle_cnt = (abs_frame_num - 1) / h->sps.poc_cycle_length;
2936  int frame_num_in_poc_cycle = (abs_frame_num - 1) % h->sps.poc_cycle_length;
2937 
2938  expectedpoc = poc_cycle_cnt * expected_delta_per_poc_cycle;
2939  for (i = 0; i <= frame_num_in_poc_cycle; i++)
2940  expectedpoc = expectedpoc + h->sps.offset_for_ref_frame[i];
2941  } else
2942  expectedpoc = 0;
2943 
2944  if (h->nal_ref_idc == 0)
2945  expectedpoc = expectedpoc + h->sps.offset_for_non_ref_pic;
2946 
2947  field_poc[0] = expectedpoc + h->delta_poc[0];
2948  field_poc[1] = field_poc[0] + h->sps.offset_for_top_to_bottom_field;
2949 
2950  if (h->picture_structure == PICT_FRAME)
2951  field_poc[1] += h->delta_poc[1];
2952  } else {
2953  int poc = 2 * (h->frame_num_offset + h->frame_num);
2954 
2955  if (!h->nal_ref_idc)
2956  poc--;
2957 
2958  field_poc[0] = poc;
2959  field_poc[1] = poc;
2960  }
2961 
2963  pic_field_poc[0] = field_poc[0];
2965  pic_field_poc[1] = field_poc[1];
2966  *pic_poc = FFMIN(pic_field_poc[0], pic_field_poc[1]);
2967 
2968  return 0;
2969 }
2970 
2971 /**
2972  * initialize scan tables
2973  */
2975 {
2976  int i;
2977  for (i = 0; i < 16; i++) {
2978 #define TRANSPOSE(x) (x >> 2) | ((x << 2) & 0xF)
2979  h->zigzag_scan[i] = TRANSPOSE(zigzag_scan[i]);
2980  h->field_scan[i] = TRANSPOSE(field_scan[i]);
2981 #undef TRANSPOSE
2982  }
2983  for (i = 0; i < 64; i++) {
2984 #define TRANSPOSE(x) (x >> 3) | ((x & 7) << 3)
2989 #undef TRANSPOSE
2990  }
2991  if (h->sps.transform_bypass) { // FIXME same ugly
2992  memcpy(h->zigzag_scan_q0 , zigzag_scan , sizeof(h->zigzag_scan_q0 ));
2993  memcpy(h->zigzag_scan8x8_q0 , ff_zigzag_direct , sizeof(h->zigzag_scan8x8_q0 ));
2995  memcpy(h->field_scan_q0 , field_scan , sizeof(h->field_scan_q0 ));
2996  memcpy(h->field_scan8x8_q0 , field_scan8x8 , sizeof(h->field_scan8x8_q0 ));
2998  } else {
2999  memcpy(h->zigzag_scan_q0 , h->zigzag_scan , sizeof(h->zigzag_scan_q0 ));
3000  memcpy(h->zigzag_scan8x8_q0 , h->zigzag_scan8x8 , sizeof(h->zigzag_scan8x8_q0 ));
3002  memcpy(h->field_scan_q0 , h->field_scan , sizeof(h->field_scan_q0 ));
3003  memcpy(h->field_scan8x8_q0 , h->field_scan8x8 , sizeof(h->field_scan8x8_q0 ));
3005  }
3006 }
3007 
3008 static int field_end(H264Context *h, int in_setup)
3009 {
3010  AVCodecContext *const avctx = h->avctx;
3011  int err = 0;
3012  h->mb_y = 0;
3013 
3014  if (CONFIG_H264_VDPAU_DECODER &&
3017 
3018  if (in_setup || !(avctx->active_thread_type & FF_THREAD_FRAME)) {
3019  if (!h->droppable) {
3021  h->prev_poc_msb = h->poc_msb;
3022  h->prev_poc_lsb = h->poc_lsb;
3023  }
3025  h->prev_frame_num = h->frame_num;
3027  }
3028 
3029  if (avctx->hwaccel) {
3030  if (avctx->hwaccel->end_frame(avctx) < 0)
3031  av_log(avctx, AV_LOG_ERROR,
3032  "hardware accelerator failed to decode picture\n");
3033  }
3034 
3035  if (CONFIG_H264_VDPAU_DECODER &&
3038 
3039  /*
3040  * FIXME: Error handling code does not seem to support interlaced
3041  * when slices span multiple rows
3042  * The ff_er_add_slice calls don't work right for bottom
3043  * fields; they cause massive erroneous error concealing
3044  * Error marking covers both fields (top and bottom).
3045  * This causes a mismatched s->error_count
3046  * and a bad error table. Further, the error count goes to
3047  * INT_MAX when called for bottom field, because mb_y is
3048  * past end by one (callers fault) and resync_mb_y != 0
3049  * causes problems for the first MB line, too.
3050  */
3051  if (CONFIG_ERROR_RESILIENCE && !FIELD_PICTURE(h) && h->current_slice && !h->sps.new) {
3052  h->er.cur_pic = h->cur_pic_ptr;
3053  ff_er_frame_end(&h->er);
3054  }
3055  if (!in_setup && !h->droppable)
3058  emms_c();
3059 
3060  h->current_slice = 0;
3061 
3062  return err;
3063 }
3064 
3065 /**
3066  * Replicate H264 "master" context to thread contexts.
3067  */
3069 {
3070  memcpy(dst->block_offset, src->block_offset, sizeof(dst->block_offset));
3071  dst->cur_pic_ptr = src->cur_pic_ptr;
3072  dst->cur_pic = src->cur_pic;
3073  dst->linesize = src->linesize;
3074  dst->uvlinesize = src->uvlinesize;
3075  dst->first_field = src->first_field;
3076 
3077  dst->prev_poc_msb = src->prev_poc_msb;
3078  dst->prev_poc_lsb = src->prev_poc_lsb;
3080  dst->prev_frame_num = src->prev_frame_num;
3081  dst->short_ref_count = src->short_ref_count;
3082 
3083  memcpy(dst->short_ref, src->short_ref, sizeof(dst->short_ref));
3084  memcpy(dst->long_ref, src->long_ref, sizeof(dst->long_ref));
3085  memcpy(dst->default_ref_list, src->default_ref_list, sizeof(dst->default_ref_list));
3086 
3087  memcpy(dst->dequant4_coeff, src->dequant4_coeff, sizeof(src->dequant4_coeff));
3088  memcpy(dst->dequant8_coeff, src->dequant8_coeff, sizeof(src->dequant8_coeff));
3089 
3090  return 0;
3091 }
3092 
3093 /**
3094  * Compute profile from profile_idc and constraint_set?_flags.
3095  *
3096  * @param sps SPS
3097  *
3098  * @return profile as defined by FF_PROFILE_H264_*
3099  */
3101 {
3102  int profile = sps->profile_idc;
3103 
3104  switch (sps->profile_idc) {
3106  // constraint_set1_flag set to 1
3107  profile |= (sps->constraint_set_flags & 1 << 1) ? FF_PROFILE_H264_CONSTRAINED : 0;
3108  break;
3112  // constraint_set3_flag set to 1
3113  profile |= (sps->constraint_set_flags & 1 << 3) ? FF_PROFILE_H264_INTRA : 0;
3114  break;
3115  }
3116 
3117  return profile;
3118 }
3119 
3121 {
3122  if (h->flags & CODEC_FLAG_LOW_DELAY ||
3124  !h->sps.num_reorder_frames)) {
3125  if (h->avctx->has_b_frames > 1 || h->delayed_pic[0])
3126  av_log(h->avctx, AV_LOG_WARNING, "Delayed frames seen. "
3127  "Reenabling low delay requires a codec flush.\n");
3128  else
3129  h->low_delay = 1;
3130  }
3131 
3132  if (h->avctx->has_b_frames < 2)
3133  h->avctx->has_b_frames = !h->low_delay;
3134 
3135  if (h->avctx->bits_per_raw_sample != h->sps.bit_depth_luma ||
3137  if (h->avctx->codec &&
3139  (h->sps.bit_depth_luma != 8 || h->sps.chroma_format_idc > 1)) {
3141  "VDPAU decoding does not support video colorspace.\n");
3142  return AVERROR_INVALIDDATA;
3143  }
3144  if (h->sps.bit_depth_luma >= 8 && h->sps.bit_depth_luma <= 14 &&
3145  h->sps.bit_depth_luma != 11 && h->sps.bit_depth_luma != 13) {
3148  h->pixel_shift = h->sps.bit_depth_luma > 8;
3149 
3151  h->sps.chroma_format_idc);
3155  h->sps.chroma_format_idc);
3156 
3157  if (CONFIG_ERROR_RESILIENCE)
3158  ff_dsputil_init(&h->dsp, h->avctx);
3160  } else {
3161  av_log(h->avctx, AV_LOG_ERROR, "Unsupported bit depth %d\n",
3162  h->sps.bit_depth_luma);
3163  return AVERROR_INVALIDDATA;
3164  }
3165  }
3166  return 0;
3167 }
3168 
3169 static enum AVPixelFormat get_pixel_format(H264Context *h, int force_callback)
3170 {
3171  switch (h->sps.bit_depth_luma) {
3172  case 9:
3173  if (CHROMA444(h)) {
3174  if (h->avctx->colorspace == AVCOL_SPC_RGB) {
3175  return AV_PIX_FMT_GBRP9;
3176  } else
3177  return AV_PIX_FMT_YUV444P9;
3178  } else if (CHROMA422(h))
3179  return AV_PIX_FMT_YUV422P9;
3180  else
3181  return AV_PIX_FMT_YUV420P9;
3182  break;
3183  case 10:
3184  if (CHROMA444(h)) {
3185  if (h->avctx->colorspace == AVCOL_SPC_RGB) {
3186  return AV_PIX_FMT_GBRP10;
3187  } else
3188  return AV_PIX_FMT_YUV444P10;
3189  } else if (CHROMA422(h))
3190  return AV_PIX_FMT_YUV422P10;
3191  else
3192  return AV_PIX_FMT_YUV420P10;
3193  break;
3194  case 12:
3195  if (CHROMA444(h)) {
3196  if (h->avctx->colorspace == AVCOL_SPC_RGB) {
3197  return AV_PIX_FMT_GBRP12;
3198  } else
3199  return AV_PIX_FMT_YUV444P12;
3200  } else if (CHROMA422(h))
3201  return AV_PIX_FMT_YUV422P12;
3202  else
3203  return AV_PIX_FMT_YUV420P12;
3204  break;
3205  case 14:
3206  if (CHROMA444(h)) {
3207  if (h->avctx->colorspace == AVCOL_SPC_RGB) {
3208  return AV_PIX_FMT_GBRP14;
3209  } else
3210  return AV_PIX_FMT_YUV444P14;
3211  } else if (CHROMA422(h))
3212  return AV_PIX_FMT_YUV422P14;
3213  else
3214  return AV_PIX_FMT_YUV420P14;
3215  break;
3216  case 8:
3217  if (CHROMA444(h)) {
3218  if (h->avctx->colorspace == AVCOL_SPC_RGB) {
3219  av_log(h->avctx, AV_LOG_DEBUG, "Detected GBR colorspace.\n");
3220  return AV_PIX_FMT_GBR24P;
3221  } else if (h->avctx->colorspace == AVCOL_SPC_YCGCO) {
3222  av_log(h->avctx, AV_LOG_WARNING, "Detected unsupported YCgCo colorspace.\n");
3223  }
3226  } else if (CHROMA422(h)) {
3229  } else {
3230  int i;
3231  const enum AVPixelFormat * fmt = h->avctx->codec->pix_fmts ?
3232  h->avctx->codec->pix_fmts :
3236 
3237  for (i=0; fmt[i] != AV_PIX_FMT_NONE; i++)
3238  if (fmt[i] == h->avctx->pix_fmt && !force_callback)
3239  return fmt[i];
3240  return ff_thread_get_format(h->avctx, fmt);
3241  }
3242  break;
3243  default:
3245  "Unsupported bit depth %d\n", h->sps.bit_depth_luma);
3246  return AVERROR_INVALIDDATA;
3247  }
3248 }
3249 
3250 /* export coded and cropped frame dimensions to AVCodecContext */
3252 {
3253  int width = h->width - (h->sps.crop_right + h->sps.crop_left);
3254  int height = h->height - (h->sps.crop_top + h->sps.crop_bottom);
3255  av_assert0(h->sps.crop_right + h->sps.crop_left < (unsigned)h->width);
3256  av_assert0(h->sps.crop_top + h->sps.crop_bottom < (unsigned)h->height);
3257 
3258  /* handle container cropping */
3259  if (!h->sps.crop &&
3260  FFALIGN(h->avctx->width, 16) == h->width &&
3261  FFALIGN(h->avctx->height, 16) == h->height) {
3262  width = h->avctx->width;
3263  height = h->avctx->height;
3264  }
3265 
3266  if (width <= 0 || height <= 0) {
3267  av_log(h->avctx, AV_LOG_ERROR, "Invalid cropped dimensions: %dx%d.\n",
3268  width, height);
3270  return AVERROR_INVALIDDATA;
3271 
3272  av_log(h->avctx, AV_LOG_WARNING, "Ignoring cropping information.\n");
3273  h->sps.crop_bottom = h->sps.crop_top = h->sps.crop_right = h->sps.crop_left = 0;
3274  h->sps.crop = 0;
3275 
3276  width = h->width;
3277  height = h->height;
3278  }
3279 
3280  h->avctx->coded_width = h->width;
3281  h->avctx->coded_height = h->height;
3282  h->avctx->width = width;
3283  h->avctx->height = height;
3284 
3285  return 0;
3286 }
3287 
3289 {
3290  int nb_slices = (HAVE_THREADS &&
3292  h->avctx->thread_count : 1;
3293  int i, ret;
3294 
3295  h->avctx->sample_aspect_ratio = h->sps.sar;
3298  &h->chroma_x_shift, &h->chroma_y_shift);
3299 
3300  if (h->sps.timing_info_present_flag) {
3301  int64_t den = h->sps.time_scale;
3302  if (h->x264_build < 44U)
3303  den *= 2;
3305  h->sps.num_units_in_tick, den, 1 << 30);
3306  }
3307 
3308  h->avctx->hwaccel = ff_find_hwaccel(h->avctx);
3309 
3310  if (reinit)
3311  free_tables(h, 0);
3312  h->first_field = 0;
3313  h->prev_interlaced_frame = 1;
3314 
3315  init_scan_tables(h);
3316  ret = ff_h264_alloc_tables(h);
3317  if (ret < 0) {
3318  av_log(h->avctx, AV_LOG_ERROR, "Could not allocate memory\n");
3319  return ret;
3320  }
3321 
3322  if (nb_slices > MAX_THREADS || (nb_slices > h->mb_height && h->mb_height)) {
3323  int max_slices;
3324  if (h->mb_height)
3325  max_slices = FFMIN(MAX_THREADS, h->mb_height);
3326  else
3327  max_slices = MAX_THREADS;
3328  av_log(h->avctx, AV_LOG_WARNING, "too many threads/slices %d,"
3329  " reducing to %d\n", nb_slices, max_slices);
3330  nb_slices = max_slices;
3331  }
3332  h->slice_context_count = nb_slices;
3333 
3334  if (!HAVE_THREADS || !(h->avctx->active_thread_type & FF_THREAD_SLICE)) {
3335  ret = context_init(h);
3336  if (ret < 0) {
3337  av_log(h->avctx, AV_LOG_ERROR, "context_init() failed.\n");
3338  return ret;
3339  }
3340  } else {
3341  for (i = 1; i < h->slice_context_count; i++) {
3342  H264Context *c;
3343  c = h->thread_context[i] = av_mallocz(sizeof(H264Context));
3344  if (!c)
3345  return AVERROR(ENOMEM);
3346  c->avctx = h->avctx;
3347  if (CONFIG_ERROR_RESILIENCE) {
3348  c->dsp = h->dsp;
3349  }
3350  c->vdsp = h->vdsp;
3351  c->h264dsp = h->h264dsp;
3352  c->h264qpel = h->h264qpel;
3353  c->h264chroma = h->h264chroma;
3354  c->sps = h->sps;
3355  c->pps = h->pps;
3356  c->pixel_shift = h->pixel_shift;
3358  c->width = h->width;
3359  c->height = h->height;
3360  c->linesize = h->linesize;
3361  c->uvlinesize = h->uvlinesize;
3364  c->qscale = h->qscale;
3365  c->droppable = h->droppable;
3367  c->low_delay = h->low_delay;
3368  c->mb_width = h->mb_width;
3369  c->mb_height = h->mb_height;
3370  c->mb_stride = h->mb_stride;
3371  c->mb_num = h->mb_num;
3372  c->flags = h->flags;
3374  c->pict_type = h->pict_type;
3375 
3376  init_scan_tables(c);
3377  clone_tables(c, h, i);
3378  c->context_initialized = 1;
3379  }
3380 
3381  for (i = 0; i < h->slice_context_count; i++)
3382  if ((ret = context_init(h->thread_context[i])) < 0) {
3383  av_log(h->avctx, AV_LOG_ERROR, "context_init() failed.\n");
3384  return ret;
3385  }
3386  }
3387 
3388  h->context_initialized = 1;
3389 
3390  return 0;
3391 }
3392 
3394 {
3395  int ref_count[2], list_count;
3396  int num_ref_idx_active_override_flag;
3397 
3398  // set defaults, might be overridden a few lines later
3399  ref_count[0] = h->pps.ref_count[0];
3400  ref_count[1] = h->pps.ref_count[1];
3401 
3402  if (h->slice_type_nos != AV_PICTURE_TYPE_I) {
3403  unsigned max[2];
3404  max[0] = max[1] = h->picture_structure == PICT_FRAME ? 15 : 31;
3405 
3408  num_ref_idx_active_override_flag = get_bits1(&h->gb);
3409 
3410  if (num_ref_idx_active_override_flag) {
3411  ref_count[0] = get_ue_golomb(&h->gb) + 1;
3412  if (h->slice_type_nos == AV_PICTURE_TYPE_B) {
3413  ref_count[1] = get_ue_golomb(&h->gb) + 1;
3414  } else
3415  // full range is spec-ok in this case, even for frames
3416  ref_count[1] = 1;
3417  }
3418 
3419  if (ref_count[0]-1 > max[0] || ref_count[1]-1 > max[1]){
3420  av_log(h->avctx, AV_LOG_ERROR, "reference overflow %u > %u or %u > %u\n", ref_count[0]-1, max[0], ref_count[1]-1, max[1]);
3421  h->ref_count[0] = h->ref_count[1] = 0;
3422  h->list_count = 0;
3423  return AVERROR_INVALIDDATA;
3424  }
3425 
3427  list_count = 2;
3428  else
3429  list_count = 1;
3430  } else {
3431  list_count = 0;
3432  ref_count[0] = ref_count[1] = 0;
3433  }
3434 
3435  if (list_count != h->list_count ||
3436  ref_count[0] != h->ref_count[0] ||
3437  ref_count[1] != h->ref_count[1]) {
3438  h->ref_count[0] = ref_count[0];
3439  h->ref_count[1] = ref_count[1];
3440  h->list_count = list_count;
3441  return 1;
3442  }
3443 
3444  return 0;
3445 }
3446 
3448 {
3449  switch (a) {
3453  default:
3454  return a;
3455  }
3456 }
3457 
3458 /**
3459  * Decode a slice header.
3460  * This will (re)intialize the decoder and call h264_frame_start() as needed.
3461  *
3462  * @param h h264context
3463  * @param h0 h264 master context (differs from 'h' when doing sliced based
3464  * parallel decoding)
3465  *
3466  * @return 0 if okay, <0 if an error occurred, 1 if decoding must not be multithreaded
3467  */
3469 {
3470  unsigned int first_mb_in_slice;
3471  unsigned int pps_id;
3472  int ret;
3473  unsigned int slice_type, tmp, i, j;
3474  int last_pic_structure, last_pic_droppable;
3475  int must_reinit;
3476  int needs_reinit = 0;
3477  int field_pic_flag, bottom_field_flag;
3478 
3481 
3482  first_mb_in_slice = get_ue_golomb_long(&h->gb);
3483 
3484  if (first_mb_in_slice == 0) { // FIXME better field boundary detection
3485  if (h0->current_slice && h->cur_pic_ptr && FIELD_PICTURE(h)) {
3486  field_end(h, 1);
3487  }
3488 
3489  h0->current_slice = 0;
3490  if (!h0->first_field) {
3491  if (h->cur_pic_ptr && !h->droppable) {
3494  }
3495  h->cur_pic_ptr = NULL;
3496  }
3497  }
3498 
3499  slice_type = get_ue_golomb_31(&h->gb);
3500  if (slice_type > 9) {
3502  "slice type %d too large at %d %d\n",
3503  slice_type, h->mb_x, h->mb_y);
3504  return AVERROR_INVALIDDATA;
3505  }
3506  if (slice_type > 4) {
3507  slice_type -= 5;
3508  h->slice_type_fixed = 1;
3509  } else
3510  h->slice_type_fixed = 0;
3511 
3512  slice_type = golomb_to_pict_type[slice_type];
3513  h->slice_type = slice_type;
3514  h->slice_type_nos = slice_type & 3;
3515 
3516  if (h->nal_unit_type == NAL_IDR_SLICE &&
3518  av_log(h->avctx, AV_LOG_ERROR, "A non-intra slice in an IDR NAL unit.\n");
3519  return AVERROR_INVALIDDATA;
3520  }
3521 
3522  // to make a few old functions happy, it's wrong though
3523  h->pict_type = h->slice_type;
3524 
3525  pps_id = get_ue_golomb(&h->gb);
3526  if (pps_id >= MAX_PPS_COUNT) {
3527  av_log(h->avctx, AV_LOG_ERROR, "pps_id %u out of range\n", pps_id);
3528  return AVERROR_INVALIDDATA;
3529  }
3530  if (!h0->pps_buffers[pps_id]) {
3532  "non-existing PPS %u referenced\n",
3533  pps_id);
3534  return AVERROR_INVALIDDATA;
3535  }
3536  if (h0->au_pps_id >= 0 && pps_id != h0->au_pps_id) {
3538  "PPS change from %d to %d forbidden\n",
3539  h0->au_pps_id, pps_id);
3540  return AVERROR_INVALIDDATA;
3541  }
3542  h->pps = *h0->pps_buffers[pps_id];
3543 
3544  if (!h0->sps_buffers[h->pps.sps_id]) {
3546  "non-existing SPS %u referenced\n",
3547  h->pps.sps_id);
3548  return AVERROR_INVALIDDATA;
3549  }
3550 
3551  if (h->pps.sps_id != h->sps.sps_id ||
3552  h->pps.sps_id != h->current_sps_id ||
3553  h0->sps_buffers[h->pps.sps_id]->new) {
3554 
3555  h->sps = *h0->sps_buffers[h->pps.sps_id];
3556 
3557  if (h->mb_width != h->sps.mb_width ||
3558  h->mb_height != h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag) ||
3561  )
3562  needs_reinit = 1;
3563 
3564  if (h->bit_depth_luma != h->sps.bit_depth_luma ||
3568  needs_reinit = 1;
3569  }
3570  if ((ret = h264_set_parameter_from_sps(h)) < 0)
3571  return ret;
3572  }
3573 
3574  h->avctx->profile = ff_h264_get_profile(&h->sps);
3575  h->avctx->level = h->sps.level_idc;
3576  h->avctx->refs = h->sps.ref_frame_count;
3577 
3578  must_reinit = (h->context_initialized &&
3579  ( 16*h->sps.mb_width != h->avctx->coded_width
3580  || 16*h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag) != h->avctx->coded_height
3584  || h->mb_width != h->sps.mb_width
3585  || h->mb_height != h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag)
3586  ));
3588  must_reinit = 1;
3589 
3590  h->mb_width = h->sps.mb_width;
3591  h->mb_height = h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag);
3592  h->mb_num = h->mb_width * h->mb_height;
3593  h->mb_stride = h->mb_width + 1;
3594 
3595  h->b_stride = h->mb_width * 4;
3596 
3597  h->chroma_y_shift = h->sps.chroma_format_idc <= 1; // 400 uses yuv420p
3598 
3599  h->width = 16 * h->mb_width;
3600  h->height = 16 * h->mb_height;
3601 
3602  ret = init_dimensions(h);
3603  if (ret < 0)
3604  return ret;
3605 
3608  : AVCOL_RANGE_MPEG;
3610  if (h->avctx->colorspace != h->sps.colorspace)
3611  needs_reinit = 1;
3613  h->avctx->color_trc = h->sps.color_trc;
3614  h->avctx->colorspace = h->sps.colorspace;
3615  }
3616  }
3617 
3618  if (h->context_initialized &&
3619  (h->width != h->avctx->coded_width ||
3620  h->height != h->avctx->coded_height ||
3621  must_reinit ||
3622  needs_reinit)) {
3623  if (h != h0) {
3625  "changing width %d -> %d / height %d -> %d on "
3626  "slice %d\n",
3627  h->width, h->avctx->coded_width,
3628  h->height, h->avctx->coded_height,
3629  h0->current_slice + 1);
3630  return AVERROR_INVALIDDATA;
3631  }
3632 
3633  flush_change(h);
3634 
3635  if ((ret = get_pixel_format(h, 1)) < 0)
3636  return ret;
3637  h->avctx->pix_fmt = ret;
3638 
3639  av_log(h->avctx, AV_LOG_INFO, "Reinit context to %dx%d, "
3640  "pix_fmt: %s\n", h->width, h->height, av_get_pix_fmt_name(h->avctx->pix_fmt));
3641 
3642  if ((ret = h264_slice_header_init(h, 1)) < 0) {
3644  "h264_slice_header_init() failed\n");
3645  return ret;
3646  }
3647  }
3648  if (!h->context_initialized) {
3649  if (h != h0) {
3651  "Cannot (re-)initialize context during parallel decoding.\n");
3652  return AVERROR_PATCHWELCOME;
3653  }
3654 
3655  if ((ret = get_pixel_format(h, 1)) < 0)
3656  return ret;
3657  h->avctx->pix_fmt = ret;
3658 
3659  if ((ret = h264_slice_header_init(h, 0)) < 0) {
3661  "h264_slice_header_init() failed\n");
3662  return ret;
3663  }
3664  }
3665 
3666  if (h == h0 && h->dequant_coeff_pps != pps_id) {
3667  h->dequant_coeff_pps = pps_id;
3669  }
3670 
3671  h->frame_num = get_bits(&h->gb, h->sps.log2_max_frame_num);
3672 
3673  h->mb_mbaff = 0;
3674  h->mb_aff_frame = 0;
3675  last_pic_structure = h0->picture_structure;
3676  last_pic_droppable = h0->droppable;
3677  h->droppable = h->nal_ref_idc == 0;
3678  if (h->sps.frame_mbs_only_flag) {
3680  } else {
3681  if (!h->sps.direct_8x8_inference_flag && slice_type == AV_PICTURE_TYPE_B) {
3682  av_log(h->avctx, AV_LOG_ERROR, "This stream was generated by a broken encoder, invalid 8x8 inference\n");
3683  return -1;
3684  }
3685  field_pic_flag = get_bits1(&h->gb);
3686  if (field_pic_flag) {
3687  bottom_field_flag = get_bits1(&h->gb);
3688  h->picture_structure = PICT_TOP_FIELD + bottom_field_flag;
3689  } else {
3691  h->mb_aff_frame = h->sps.mb_aff;
3692  }
3693  }
3695 
3696  if (h0->current_slice != 0) {
3697  if (last_pic_structure != h->picture_structure ||
3698  last_pic_droppable != h->droppable) {
3700  "Changing field mode (%d -> %d) between slices is not allowed\n",
3701  last_pic_structure, h->picture_structure);
3702  h->picture_structure = last_pic_structure;
3703  h->droppable = last_pic_droppable;
3704  return AVERROR_INVALIDDATA;
3705  } else if (!h0->cur_pic_ptr) {
3707  "unset cur_pic_ptr on slice %d\n",
3708  h0->current_slice + 1);
3709  return AVERROR_INVALIDDATA;
3710  }
3711  } else {
3712  /* Shorten frame num gaps so we don't have to allocate reference
3713  * frames just to throw them away */
3714  if (h->frame_num != h->prev_frame_num) {
3715  int unwrap_prev_frame_num = h->prev_frame_num;
3716  int max_frame_num = 1 << h->sps.log2_max_frame_num;
3717 
3718  if (unwrap_prev_frame_num > h->frame_num)
3719  unwrap_prev_frame_num -= max_frame_num;
3720 
3721  if ((h->frame_num - unwrap_prev_frame_num) > h->sps.ref_frame_count) {
3722  unwrap_prev_frame_num = (h->frame_num - h->sps.ref_frame_count) - 1;
3723  if (unwrap_prev_frame_num < 0)
3724  unwrap_prev_frame_num += max_frame_num;
3725 
3726  h->prev_frame_num = unwrap_prev_frame_num;
3727  }
3728  }
3729 
3730  /* See if we have a decoded first field looking for a pair...
3731  * Here, we're using that to see if we should mark previously
3732  * decode frames as "finished".
3733  * We have to do that before the "dummy" in-between frame allocation,
3734  * since that can modify h->cur_pic_ptr. */
3735  if (h0->first_field) {
3736  assert(h0->cur_pic_ptr);
3737  assert(h0->cur_pic_ptr->f.buf[0]);
3738  assert(h0->cur_pic_ptr->reference != DELAYED_PIC_REF);
3739 
3740  /* Mark old field/frame as completed */
3741  if (h0->cur_pic_ptr->tf.owner == h0->avctx) {
3742  ff_thread_report_progress(&h0->cur_pic_ptr->tf, INT_MAX,
3743  last_pic_structure == PICT_BOTTOM_FIELD);
3744  }
3745 
3746  /* figure out if we have a complementary field pair */
3747  if (!FIELD_PICTURE(h) || h->picture_structure == last_pic_structure) {
3748  /* Previous field is unmatched. Don't display it, but let it
3749  * remain for reference if marked as such. */
3750  if (last_pic_structure != PICT_FRAME) {
3751  ff_thread_report_progress(&h0->cur_pic_ptr->tf, INT_MAX,
3752  last_pic_structure == PICT_TOP_FIELD);
3753  }
3754  } else {
3755  if (h0->cur_pic_ptr->frame_num != h->frame_num) {
3756  /* This and previous field were reference, but had
3757  * different frame_nums. Consider this field first in
3758  * pair. Throw away previous field except for reference
3759  * purposes. */
3760  if (last_pic_structure != PICT_FRAME) {
3761  ff_thread_report_progress(&h0->cur_pic_ptr->tf, INT_MAX,
3762  last_pic_structure == PICT_TOP_FIELD);
3763  }
3764  } else {
3765  /* Second field in complementary pair */
3766  if (!((last_pic_structure == PICT_TOP_FIELD &&
3768  (last_pic_structure == PICT_BOTTOM_FIELD &&
3771  "Invalid field mode combination %d/%d\n",
3772  last_pic_structure, h->picture_structure);
3773  h->picture_structure = last_pic_structure;
3774  h->droppable = last_pic_droppable;
3775  return AVERROR_INVALIDDATA;
3776  } else if (last_pic_droppable != h->droppable) {
3778  "Found reference and non-reference fields in the same frame, which");
3779  h->picture_structure = last_pic_structure;
3780  h->droppable = last_pic_droppable;
3781  return AVERROR_PATCHWELCOME;
3782  }
3783  }
3784  }
3785  }
3786 
3787  while (h->frame_num != h->prev_frame_num && !h0->first_field &&
3788  h->frame_num != (h->prev_frame_num + 1) % (1 << h->sps.log2_max_frame_num)) {
3789  Picture *prev = h->short_ref_count ? h->short_ref[0] : NULL;
3790  av_log(h->avctx, AV_LOG_DEBUG, "Frame num gap %d %d\n",
3791  h->frame_num, h->prev_frame_num);
3793  for(i=0; i<FF_ARRAY_ELEMS(h->last_pocs); i++)
3794  h->last_pocs[i] = INT_MIN;
3795  ret = h264_frame_start(h);
3796  if (ret < 0) {
3797  h0->first_field = 0;
3798  return ret;
3799  }
3800 
3801  h->prev_frame_num++;
3802  h->prev_frame_num %= 1 << h->sps.log2_max_frame_num;
3805  ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX, 0);
3806  ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX, 1);
3808  if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
3809  return ret;
3811  if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
3812  return ret;
3813  /* Error concealment: If a ref is missing, copy the previous ref
3814  * in its place.
3815  * FIXME: Avoiding a memcpy would be nice, but ref handling makes
3816  * many assumptions about there being no actual duplicates.
3817  * FIXME: This does not copy padding for out-of-frame motion
3818  * vectors. Given we are concealing a lost frame, this probably
3819  * is not noticeable by comparison, but it should be fixed. */
3820  if (h->short_ref_count) {
3821  if (prev) {
3822  av_image_copy(h->short_ref[0]->f.data,
3823  h->short_ref[0]->f.linesize,
3824  (const uint8_t **)prev->f.data,
3825  prev->f.linesize,
3826  h->avctx->pix_fmt,
3827  h->mb_width * 16,
3828  h->mb_height * 16);
3829  h->short_ref[0]->poc = prev->poc + 2;
3830  }
3831  h->short_ref[0]->frame_num = h->prev_frame_num;
3832  }
3833  }
3834 
3835  /* See if we have a decoded first field looking for a pair...
3836  * We're using that to see whether to continue decoding in that
3837  * frame, or to allocate a new one. */
3838  if (h0->first_field) {
3839  assert(h0->cur_pic_ptr);
3840  assert(h0->cur_pic_ptr->f.buf[0]);
3841  assert(h0->cur_pic_ptr->reference != DELAYED_PIC_REF);
3842 
3843  /* figure out if we have a complementary field pair */
3844  if (!FIELD_PICTURE(h) || h->picture_structure == last_pic_structure) {
3845  /* Previous field is unmatched. Don't display it, but let it
3846  * remain for reference if marked as such. */
3847  h0->cur_pic_ptr = NULL;
3848  h0->first_field = FIELD_PICTURE(h);
3849  } else {
3850  if (h0->cur_pic_ptr->frame_num != h->frame_num) {
3851  ff_thread_report_progress(&h0->cur_pic_ptr->tf, INT_MAX,
3853  /* This and the previous field had different frame_nums.
3854  * Consider this field first in pair. Throw away previous
3855  * one except for reference purposes. */
3856  h0->first_field = 1;
3857  h0->cur_pic_ptr = NULL;
3858  } else {
3859  /* Second field in complementary pair */
3860  h0->first_field = 0;
3861  }
3862  }
3863  } else {
3864  /* Frame or first field in a potentially complementary pair */
3865  h0->first_field = FIELD_PICTURE(h);
3866  }
3867 
3868  if (!FIELD_PICTURE(h) || h0->first_field) {
3869  if (h264_frame_start(h) < 0) {
3870  h0->first_field = 0;
3871  return AVERROR_INVALIDDATA;
3872  }
3873  } else {
3875  }
3876  /* Some macroblocks can be accessed before they're available in case
3877  * of lost slices, MBAFF or threading. */
3878  if (FIELD_PICTURE(h)) {
3879  for(i = (h->picture_structure == PICT_BOTTOM_FIELD); i<h->mb_height; i++)
3880  memset(h->slice_table + i*h->mb_stride, -1, (h->mb_stride - (i+1==h->mb_height)) * sizeof(*h->slice_table));
3881  } else {
3882  memset(h->slice_table, -1,
3883  (h->mb_height * h->mb_stride - 1) * sizeof(*h->slice_table));
3884  }
3885  h0->last_slice_type = -1;
3886  }
3887  if (h != h0 && (ret = clone_slice(h, h0)) < 0)
3888  return ret;
3889 
3890  /* can't be in alloc_tables because linesize isn't known there.
3891  * FIXME: redo bipred weight to not require extra buffer? */
3892  for (i = 0; i < h->slice_context_count; i++)
3893  if (h->thread_context[i]) {
3895  if (ret < 0)
3896  return ret;
3897  }
3898 
3899  h->cur_pic_ptr->frame_num = h->frame_num; // FIXME frame_num cleanup
3900 
3901  av_assert1(h->mb_num == h->mb_width * h->mb_height);
3902  if (first_mb_in_slice << FIELD_OR_MBAFF_PICTURE(h) >= h->mb_num ||
3903  first_mb_in_slice >= h->mb_num) {
3904  av_log(h->avctx, AV_LOG_ERROR, "first_mb_in_slice overflow\n");
3905  return AVERROR_INVALIDDATA;
3906  }
3907  h->resync_mb_x = h->mb_x = first_mb_in_slice % h->mb_width;
3908  h->resync_mb_y = h->mb_y = (first_mb_in_slice / h->mb_width) <<
3911  h->resync_mb_y = h->mb_y = h->mb_y + 1;
3912  av_assert1(h->mb_y < h->mb_height);
3913 
3914  if (h->picture_structure == PICT_FRAME) {
3915  h->curr_pic_num = h->frame_num;
3916  h->max_pic_num = 1 << h->sps.log2_max_frame_num;
3917  } else {
3918  h->curr_pic_num = 2 * h->frame_num + 1;
3919  h->max_pic_num = 1 << (h->sps.log2_max_frame_num + 1);
3920  }
3921 
3922  if (h->nal_unit_type == NAL_IDR_SLICE)
3923  get_ue_golomb(&h->gb); /* idr_pic_id */
3924 
3925  if (h->sps.poc_type == 0) {
3926  h->poc_lsb = get_bits(&h->gb, h->sps.log2_max_poc_lsb);
3927 
3928  if (h->pps.pic_order_present == 1 && h->picture_structure == PICT_FRAME)
3929  h->delta_poc_bottom = get_se_golomb(&h->gb);
3930  }
3931 
3932  if (h->sps.poc_type == 1 && !h->sps.delta_pic_order_always_zero_flag) {
3933  h->delta_poc[0] = get_se_golomb(&h->gb);
3934 
3935  if (h->pps.pic_order_present == 1 && h->picture_structure == PICT_FRAME)
3936  h->delta_poc[1] = get_se_golomb(&h->gb);
3937  }
3938 
3940 
3943 
3944  ret = ff_set_ref_count(h);
3945  if (ret < 0)
3946  return ret;
3947 
3948  if (slice_type != AV_PICTURE_TYPE_I &&
3949  (h0->current_slice == 0 ||
3950  slice_type != h0->last_slice_type ||
3951  memcmp(h0->last_ref_count, h0->ref_count, sizeof(h0->ref_count)))) {
3952 
3954  }
3955 
3956  if (h->slice_type_nos != AV_PICTURE_TYPE_I) {
3958  if (ret < 0) {
3959  h->ref_count[1] = h->ref_count[0] = 0;
3960  return ret;
3961  }
3962  }
3963 
3964  if ((h->pps.weighted_pred && h->slice_type_nos == AV_PICTURE_TYPE_P) ||
3965  (h->pps.weighted_bipred_idc == 1 &&
3968  else if (h->pps.weighted_bipred_idc == 2 &&
3970  implicit_weight_table(h, -1);
3971  } else {
3972  h->use_weight = 0;
3973  for (i = 0; i < 2; i++) {
3974  h->luma_weight_flag[i] = 0;
3975  h->chroma_weight_flag[i] = 0;
3976  }
3977  }
3978 
3979  // If frame-mt is enabled, only update mmco tables for the first slice
3980  // in a field. Subsequent slices can temporarily clobber h->mmco_index
3981  // or h->mmco, which will cause ref list mix-ups and decoding errors
3982  // further down the line. This may break decoding if the first slice is
3983  // corrupt, thus we only do this if frame-mt is enabled.
3984  if (h->nal_ref_idc) {
3985  ret = ff_h264_decode_ref_pic_marking(h0, &h->gb,
3987  h0->current_slice == 0);
3988  if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
3989  return AVERROR_INVALIDDATA;
3990  }
3991 
3992  if (FRAME_MBAFF(h)) {
3994 
3996  implicit_weight_table(h, 0);
3997  implicit_weight_table(h, 1);
3998  }
3999  }
4000 
4004 
4005  if (h->slice_type_nos != AV_PICTURE_TYPE_I && h->pps.cabac) {
4006  tmp = get_ue_golomb_31(&h->gb);
4007  if (tmp > 2) {
4008  av_log(h->avctx, AV_LOG_ERROR, "cabac_init_idc %u overflow\n", tmp);
4009  return AVERROR_INVALIDDATA;
4010  }
4011  h->cabac_init_idc = tmp;
4012  }
4013 
4014  h->last_qscale_diff = 0;
4015  tmp = h->pps.init_qp + get_se_golomb(&h->gb);
4016  if (tmp > 51 + 6 * (h->sps.bit_depth_luma - 8)) {
4017  av_log(h->avctx, AV_LOG_ERROR, "QP %u out of range\n", tmp);
4018  return AVERROR_INVALIDDATA;
4019  }
4020  h->qscale = tmp;
4021  h->chroma_qp[0] = get_chroma_qp(h, 0, h->qscale);
4022  h->chroma_qp[1] = get_chroma_qp(h, 1, h->qscale);
4023  // FIXME qscale / qp ... stuff
4024  if (h->slice_type == AV_PICTURE_TYPE_SP)
4025  get_bits1(&h->gb); /* sp_for_switch_flag */
4026  if (h->slice_type == AV_PICTURE_TYPE_SP ||
4028  get_se_golomb(&h->gb); /* slice_qs_delta */
4029 
4030  h->deblocking_filter = 1;
4031  h->slice_alpha_c0_offset = 0;
4032  h->slice_beta_offset = 0;
4034  tmp = get_ue_golomb_31(&h->gb);
4035  if (tmp > 2) {
4037  "deblocking_filter_idc %u out of range\n", tmp);
4038  return AVERROR_INVALIDDATA;
4039  }
4040  h->deblocking_filter = tmp;
4041  if (h->deblocking_filter < 2)
4042  h->deblocking_filter ^= 1; // 1<->0
4043 
4044  if (h->deblocking_filter) {
4045  h->slice_alpha_c0_offset = get_se_golomb(&h->gb) * 2;
4046  h->slice_beta_offset = get_se_golomb(&h->gb) * 2;
4047  if (h->slice_alpha_c0_offset > 12 ||
4048  h->slice_alpha_c0_offset < -12 ||
4049  h->slice_beta_offset > 12 ||
4050  h->slice_beta_offset < -12) {
4052  "deblocking filter parameters %d %d out of range\n",
4054  return AVERROR_INVALIDDATA;
4055  }
4056  }
4057  }
4058 
4059  if (h->avctx->skip_loop_filter >= AVDISCARD_ALL ||
4065  h->nal_ref_idc == 0))
4066  h->deblocking_filter = 0;
4067 
4068  if (h->deblocking_filter == 1 && h0->max_contexts > 1) {
4069  if (h->avctx->flags2 & CODEC_FLAG2_FAST) {
4070  /* Cheat slightly for speed:
4071  * Do not bother to deblock across slices. */
4072  h->deblocking_filter = 2;
4073  } else {
4074  h0->max_contexts = 1;
4075  if (!h0->single_decode_warning) {
4076  av_log(h->avctx, AV_LOG_INFO,
4077  "Cannot parallelize deblocking type 1, decoding such frames in sequential order\n");
4078  h0->single_decode_warning = 1;
4079  }
4080  if (h != h0) {
4082  "Deblocking switched inside frame.\n");
4083  return 1;
4084  }
4085  }
4086  }
4087  h->qp_thresh = 15 -
4089  FFMAX3(0,
4091  h->pps.chroma_qp_index_offset[1]) +
4092  6 * (h->sps.bit_depth_luma - 8);
4093 
4094  h0->last_slice_type = slice_type;
4095  memcpy(h0->last_ref_count, h0->ref_count, sizeof(h0->last_ref_count));
4096  h->slice_num = ++h0->current_slice;
4097 
4098  if (h->slice_num)
4099  h0->slice_row[(h->slice_num-1)&(MAX_SLICES-1)]= h->resync_mb_y;
4100  if ( h0->slice_row[h->slice_num&(MAX_SLICES-1)] + 3 >= h->resync_mb_y
4101  && h0->slice_row[h->slice_num&(MAX_SLICES-1)] <= h->resync_mb_y
4102  && h->slice_num >= MAX_SLICES) {
4103  //in case of ASO this check needs to be updated depending on how we decide to assign slice numbers in this case
4104  av_log(h->avctx, AV_LOG_WARNING, "Possibly too many slices (%d >= %d), increase MAX_SLICES and recompile if there are artifacts\n", h->slice_num, MAX_SLICES);
4105  }
4106 
4107  for (j = 0; j < 2; j++) {
4108  int id_list[16];
4109  int *ref2frm = h->ref2frm[h->slice_num & (MAX_SLICES - 1)][j];
4110  for (i = 0; i < 16; i++) {
4111  id_list[i] = 60;
4112  if (j < h->list_count && i < h->ref_count[j] &&
4113  h->ref_list[j][i].f.buf[0]) {
4114  int k;
4115  AVBuffer *buf = h->ref_list[j][i].f.buf[0]->buffer;
4116  for (k = 0; k < h->short_ref_count; k++)
4117  if (h->short_ref[k]->f.buf[0]->buffer == buf) {
4118  id_list[i] = k;
4119  break;
4120  }
4121  for (k = 0; k < h->long_ref_count; k++)
4122  if (h->long_ref[k] && h->long_ref[k]->f.buf[0]->buffer == buf) {
4123  id_list[i] = h->short_ref_count + k;
4124  break;
4125  }
4126  }
4127  }
4128 
4129  ref2frm[0] =
4130  ref2frm[1] = -1;
4131  for (i = 0; i < 16; i++)
4132  ref2frm[i + 2] = 4 * id_list[i] + (h->ref_list[j][i].reference & 3);
4133  ref2frm[18 + 0] =
4134  ref2frm[18 + 1] = -1;
4135  for (i = 16; i < 48; i++)
4136  ref2frm[i + 4] = 4 * id_list[(i - 16) >> 1] +
4137  (h->ref_list[j][i].reference & 3);
4138  }
4139 
4140  if (h->ref_count[0]) h->er.last_pic = &h->ref_list[0][0];
4141  if (h->ref_count[1]) h->er.next_pic = &h->ref_list[1][0];
4142  h->er.ref_count = h->ref_count[0];
4143  h0->au_pps_id = pps_id;
4144  h->sps.new =
4145  h0->sps_buffers[h->pps.sps_id]->new = 0;
4146  h->current_sps_id = h->pps.sps_id;
4147 
4148  if (h->avctx->debug & FF_DEBUG_PICT_INFO) {
4150  "slice:%d %s mb:%d %c%s%s pps:%u frame:%d poc:%d/%d ref:%d/%d qp:%d loop:%d:%d:%d weight:%d%s %s\n",
4151  h->slice_num,
4152  (h->picture_structure == PICT_FRAME ? "F" : h->picture_structure == PICT_TOP_FIELD ? "T" : "B"),
4153  first_mb_in_slice,
4155  h->slice_type_fixed ? " fix" : "",
4156  h->nal_unit_type == NAL_IDR_SLICE ? " IDR" : "",
4157  pps_id, h->frame_num,
4158  h->cur_pic_ptr->field_poc[0],
4159  h->cur_pic_ptr->field_poc[1],
4160  h->ref_count[0], h->ref_count[1],
4161  h->qscale,
4162  h->deblocking_filter,
4164  h->use_weight,
4165  h->use_weight == 1 && h->use_weight_chroma ? "c" : "",
4166  h->slice_type == AV_PICTURE_TYPE_B ? (h->direct_spatial_mv_pred ? "SPAT" : "TEMP") : "");
4167  }
4168 
4169  return 0;
4170 }
4171 
4173 {
4174  switch (h->slice_type) {
4175  case AV_PICTURE_TYPE_P:
4176  return 0;
4177  case AV_PICTURE_TYPE_B:
4178  return 1;
4179  case AV_PICTURE_TYPE_I:
4180  return 2;
4181  case AV_PICTURE_TYPE_SP:
4182  return 3;
4183  case AV_PICTURE_TYPE_SI:
4184  return 4;
4185  default:
4186  return AVERROR_INVALIDDATA;
4187  }
4188 }
4189 
4191  int mb_type, int top_xy,
4192  int left_xy[LEFT_MBS],
4193  int top_type,
4194  int left_type[LEFT_MBS],
4195  int mb_xy, int list)
4196 {
4197  int b_stride = h->b_stride;
4198  int16_t(*mv_dst)[2] = &h->mv_cache[list][scan8[0]];
4199  int8_t *ref_cache = &h->ref_cache[list][scan8[0]];
4200  if (IS_INTER(mb_type) || IS_DIRECT(mb_type)) {
4201  if (USES_LIST(top_type, list)) {
4202  const int b_xy = h->mb2b_xy[top_xy] + 3 * b_stride;
4203  const int b8_xy = 4 * top_xy + 2;
4204  int (*ref2frm)[64] = (void*)(h->ref2frm[h->slice_table[top_xy] & (MAX_SLICES - 1)][0] + (MB_MBAFF(h) ? 20 : 2));
4205  AV_COPY128(mv_dst - 1 * 8, h->cur_pic.motion_val[list][b_xy + 0]);
4206  ref_cache[0 - 1 * 8] =
4207  ref_cache[1 - 1 * 8] = ref2frm[list][h->cur_pic.ref_index[list][b8_xy + 0]];
4208  ref_cache[2 - 1 * 8] =
4209  ref_cache[3 - 1 * 8] = ref2frm[list][h->cur_pic.ref_index[list][b8_xy + 1]];
4210  } else {
4211  AV_ZERO128(mv_dst - 1 * 8);
4212  AV_WN32A(&ref_cache[0 - 1 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
4213  }
4214 
4215  if (!IS_INTERLACED(mb_type ^ left_type[LTOP])) {
4216  if (USES_LIST(left_type[LTOP], list)) {
4217  const int b_xy = h->mb2b_xy[left_xy[LTOP]] + 3;
4218  const int b8_xy = 4 * left_xy[LTOP] + 1;
4219  int (*ref2frm)[64] =(void*)( h->ref2frm[h->slice_table[left_xy[LTOP]] & (MAX_SLICES - 1)][0] + (MB_MBAFF(h) ? 20 : 2));
4220  AV_COPY32(mv_dst - 1 + 0, h->cur_pic.motion_val[list][b_xy + b_stride * 0]);
4221  AV_COPY32(mv_dst - 1 + 8, h->cur_pic.motion_val[list][b_xy + b_stride * 1]);
4222  AV_COPY32(mv_dst - 1 + 16, h->cur_pic.motion_val[list][b_xy + b_stride * 2]);
4223  AV_COPY32(mv_dst - 1 + 24, h->cur_pic.motion_val[list][b_xy + b_stride * 3]);
4224  ref_cache[-1 + 0] =
4225  ref_cache[-1 + 8] = ref2frm[list][h->cur_pic.ref_index[list][b8_xy + 2 * 0]];
4226  ref_cache[-1 + 16] =
4227  ref_cache[-1 + 24] = ref2frm[list][h->cur_pic.ref_index[list][b8_xy + 2 * 1]];
4228  } else {
4229  AV_ZERO32(mv_dst - 1 + 0);
4230  AV_ZERO32(mv_dst - 1 + 8);
4231  AV_ZERO32(mv_dst - 1 + 16);
4232  AV_ZERO32(mv_dst - 1 + 24);
4233  ref_cache[-1 + 0] =
4234  ref_cache[-1 + 8] =
4235  ref_cache[-1 + 16] =
4236  ref_cache[-1 + 24] = LIST_NOT_USED;
4237  }
4238  }
4239  }
4240 
4241  if (!USES_LIST(mb_type, list)) {
4242  fill_rectangle(mv_dst, 4, 4, 8, pack16to32(0, 0), 4);
4243  AV_WN32A(&ref_cache[0 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
4244  AV_WN32A(&ref_cache[1 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
4245  AV_WN32A(&ref_cache[2 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
4246  AV_WN32A(&ref_cache[3 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
4247  return;
4248  }
4249 
4250  {
4251  int8_t *ref = &h->cur_pic.ref_index[list][4 * mb_xy];
4252  int (*ref2frm)[64] = (void*)(h->ref2frm[h->slice_num & (MAX_SLICES - 1)][0] + (MB_MBAFF(h) ? 20 : 2));
4253  uint32_t ref01 = (pack16to32(ref2frm[list][ref[0]], ref2frm[list][ref[1]]) & 0x00FF00FF) * 0x0101;
4254  uint32_t ref23 = (pack16to32(ref2frm[list][ref[2]], ref2frm[list][ref[3]]) & 0x00FF00FF) * 0x0101;
4255  AV_WN32A(&ref_cache[0 * 8], ref01);
4256  AV_WN32A(&ref_cache[1 * 8], ref01);
4257  AV_WN32A(&ref_cache[2 * 8], ref23);
4258  AV_WN32A(&ref_cache[3 * 8], ref23);
4259  }
4260 
4261  {
4262  int16_t(*mv_src)[2] = &h->cur_pic.motion_val[list][4 * h->mb_x + 4 * h->mb_y * b_stride];
4263  AV_COPY128(mv_dst + 8 * 0, mv_src + 0 * b_stride);
4264  AV_COPY128(mv_dst + 8 * 1, mv_src + 1 * b_stride);
4265  AV_COPY128(mv_dst + 8 * 2, mv_src + 2 * b_stride);
4266  AV_COPY128(mv_dst + 8 * 3, mv_src + 3 * b_stride);
4267  }
4268 }
4269 
4270 /**
4271  *
4272  * @return non zero if the loop filter can be skipped
4273  */
4274 static int fill_filter_caches(H264Context *h, int mb_type)
4275 {
4276  const int mb_xy = h->mb_xy;
4277  int top_xy, left_xy[LEFT_MBS];
4278  int top_type, left_type[LEFT_MBS];
4279  uint8_t *nnz;
4280  uint8_t *nnz_cache;
4281 
4282  top_xy = mb_xy - (h->mb_stride << MB_FIELD(h));
4283 
4284  /* Wow, what a mess, why didn't they simplify the interlacing & intra
4285  * stuff, I can't imagine that these complex rules are worth it. */
4286 
4287  left_xy[LBOT] = left_xy[LTOP] = mb_xy - 1;
4288  if (FRAME_MBAFF(h)) {
4289  const int left_mb_field_flag = IS_INTERLACED(h->cur_pic.mb_type[mb_xy - 1]);
4290  const int curr_mb_field_flag = IS_INTERLACED(mb_type);
4291  if (h->mb_y & 1) {
4292  if (left_mb_field_flag != curr_mb_field_flag)
4293  left_xy[LTOP] -= h->mb_stride;
4294  } else {
4295  if (curr_mb_field_flag)
4296  top_xy += h->mb_stride &
4297  (((h->cur_pic.mb_type[top_xy] >> 7) & 1) - 1);
4298  if (left_mb_field_flag != curr_mb_field_flag)
4299  left_xy[LBOT] += h->mb_stride;
4300  }
4301  }
4302 
4303  h->top_mb_xy = top_xy;
4304  h->left_mb_xy[LTOP] = left_xy[LTOP];
4305  h->left_mb_xy[LBOT] = left_xy[LBOT];
4306  {
4307  /* For sufficiently low qp, filtering wouldn't do anything.
4308  * This is a conservative estimate: could also check beta_offset
4309  * and more accurate chroma_qp. */
4310  int qp_thresh = h->qp_thresh; // FIXME strictly we should store qp_thresh for each mb of a slice
4311  int qp = h->cur_pic.qscale_table[mb_xy];
4312  if (qp <= qp_thresh &&
4313  (left_xy[LTOP] < 0 ||
4314  ((qp + h->cur_pic.qscale_table[left_xy[LTOP]] + 1) >> 1) <= qp_thresh) &&
4315  (top_xy < 0 ||
4316  ((qp + h->cur_pic.qscale_table[top_xy] + 1) >> 1) <= qp_thresh)) {
4317  if (!FRAME_MBAFF(h))
4318  return 1;
4319  if ((left_xy[LTOP] < 0 ||
4320  ((qp + h->cur_pic.qscale_table[left_xy[LBOT]] + 1) >> 1) <= qp_thresh) &&
4321  (top_xy < h->mb_stride ||
4322  ((qp + h->cur_pic.qscale_table[top_xy - h->mb_stride] + 1) >> 1) <= qp_thresh))
4323  return 1;
4324  }
4325  }
4326 
4327  top_type = h->cur_pic.mb_type[top_xy];
4328  left_type[LTOP] = h->cur_pic.mb_type[left_xy[LTOP]];
4329  left_type[LBOT] = h->cur_pic.mb_type[left_xy[LBOT]];
4330  if (h->deblocking_filter == 2) {
4331  if (h->slice_table[top_xy] != h->slice_num)
4332  top_type = 0;
4333  if (h->slice_table[left_xy[LBOT]] != h->slice_num)
4334  left_type[LTOP] = left_type[LBOT] = 0;
4335  } else {
4336  if (h->slice_table[top_xy] == 0xFFFF)
4337  top_type = 0;
4338  if (h->slice_table[left_xy[LBOT]] == 0xFFFF)
4339  left_type[LTOP] = left_type[LBOT] = 0;
4340  }
4341  h->top_type = top_type;
4342  h->left_type[LTOP] = left_type[LTOP];
4343  h->left_type[LBOT] = left_type[LBOT];
4344 
4345  if (IS_INTRA(mb_type))
4346  return 0;
4347 
4348  fill_filter_caches_inter(h, mb_type, top_xy, left_xy,
4349  top_type, left_type, mb_xy, 0);
4350  if (h->list_count == 2)
4351  fill_filter_caches_inter(h, mb_type, top_xy, left_xy,
4352  top_type, left_type, mb_xy, 1);
4353 
4354  nnz = h->non_zero_count[mb_xy];
4355  nnz_cache = h->non_zero_count_cache;
4356  AV_COPY32(&nnz_cache[4 + 8 * 1], &nnz[0]);
4357  AV_COPY32(&nnz_cache[4 + 8 * 2], &nnz[4]);
4358  AV_COPY32(&nnz_cache[4 + 8 * 3], &nnz[8]);
4359  AV_COPY32(&nnz_cache[4 + 8 * 4], &nnz[12]);
4360  h->cbp = h->cbp_table[mb_xy];
4361 
4362  if (top_type) {
4363  nnz = h->non_zero_count[top_xy];
4364  AV_COPY32(&nnz_cache[4 + 8 * 0], &nnz[3 * 4]);
4365  }
4366 
4367  if (left_type[LTOP]) {
4368  nnz = h->non_zero_count[left_xy[LTOP]];
4369  nnz_cache[3 + 8 * 1] = nnz[3 + 0 * 4];
4370  nnz_cache[3 + 8 * 2] = nnz[3 + 1 * 4];
4371  nnz_cache[3 + 8 * 3] = nnz[3 + 2 * 4];
4372  nnz_cache[3 + 8 * 4] = nnz[3 + 3 * 4];
4373  }
4374 
4375  /* CAVLC 8x8dct requires NNZ values for residual decoding that differ
4376  * from what the loop filter needs */
4377  if (!CABAC(h) && h->pps.transform_8x8_mode) {
4378  if (IS_8x8DCT(top_type)) {
4379  nnz_cache[4 + 8 * 0] =
4380  nnz_cache[5 + 8 * 0] = (h->cbp_table[top_xy] & 0x4000) >> 12;
4381  nnz_cache[6 + 8 * 0] =
4382  nnz_cache[7 + 8 * 0] = (h->cbp_table[top_xy] & 0x8000) >> 12;
4383  }
4384  if (IS_8x8DCT(left_type[LTOP])) {
4385  nnz_cache[3 + 8 * 1] =
4386  nnz_cache[3 + 8 * 2] = (h->cbp_table[left_xy[LTOP]] & 0x2000) >> 12; // FIXME check MBAFF
4387  }
4388  if (IS_8x8DCT(left_type[LBOT])) {
4389  nnz_cache[3 + 8 * 3] =
4390  nnz_cache[3 + 8 * 4] = (h->cbp_table[left_xy[LBOT]] & 0x8000) >> 12; // FIXME check MBAFF
4391  }
4392 
4393  if (IS_8x8DCT(mb_type)) {
4394  nnz_cache[scan8[0]] =
4395  nnz_cache[scan8[1]] =
4396  nnz_cache[scan8[2]] =
4397  nnz_cache[scan8[3]] = (h->cbp & 0x1000) >> 12;
4398 
4399  nnz_cache[scan8[0 + 4]] =
4400  nnz_cache[scan8[1 + 4]] =
4401  nnz_cache[scan8[2 + 4]] =
4402  nnz_cache[scan8[3 + 4]] = (h->cbp & 0x2000) >> 12;
4403 
4404  nnz_cache[scan8[0 + 8]] =
4405  nnz_cache[scan8[1 + 8]] =
4406  nnz_cache[scan8[2 + 8]] =
4407  nnz_cache[scan8[3 + 8]] = (h->cbp & 0x4000) >> 12;
4408 
4409  nnz_cache[scan8[0 + 12]] =
4410  nnz_cache[scan8[1 + 12]] =
4411  nnz_cache[scan8[2 + 12]] =
4412  nnz_cache[scan8[3 + 12]] = (h->cbp & 0x8000) >> 12;
4413  }
4414  }
4415 
4416  return 0;
4417 }
4418 
4419 static void loop_filter(H264Context *h, int start_x, int end_x)
4420 {
4421  uint8_t *dest_y, *dest_cb, *dest_cr;
4422  int linesize, uvlinesize, mb_x, mb_y;
4423  const int end_mb_y = h->mb_y + FRAME_MBAFF(h);
4424  const int old_slice_type = h->slice_type;
4425  const int pixel_shift = h->pixel_shift;
4426  const int block_h = 16 >> h->chroma_y_shift;
4427 
4428  if (h->deblocking_filter) {
4429  for (mb_x = start_x; mb_x < end_x; mb_x++)
4430  for (mb_y = end_mb_y - FRAME_MBAFF(h); mb_y <= end_mb_y; mb_y++) {
4431  int mb_xy, mb_type;
4432  mb_xy = h->mb_xy = mb_x + mb_y * h->mb_stride;
4433  h->slice_num = h->slice_table[mb_xy];
4434  mb_type = h->cur_pic.mb_type[mb_xy];
4435  h->list_count = h->list_counts[mb_xy];
4436 
4437  if (FRAME_MBAFF(h))
4438  h->mb_mbaff =
4439  h->mb_field_decoding_flag = !!IS_INTERLACED(mb_type);
4440 
4441  h->mb_x = mb_x;
4442  h->mb_y = mb_y;
4443  dest_y = h->cur_pic.f.data[0] +
4444  ((mb_x << pixel_shift) + mb_y * h->linesize) * 16;
4445  dest_cb = h->cur_pic.f.data[1] +
4446  (mb_x << pixel_shift) * (8 << CHROMA444(h)) +
4447  mb_y * h->uvlinesize * block_h;
4448  dest_cr = h->cur_pic.f.data[2] +
4449  (mb_x << pixel_shift) * (8 << CHROMA444(h)) +
4450  mb_y * h->uvlinesize * block_h;
4451  // FIXME simplify above
4452 
4453  if (MB_FIELD(h)) {
4454  linesize = h->mb_linesize = h->linesize * 2;
4455  uvlinesize = h->mb_uvlinesize = h->uvlinesize * 2;
4456  if (mb_y & 1) { // FIXME move out of this function?
4457  dest_y -= h->linesize * 15;
4458  dest_cb -= h->uvlinesize * (block_h - 1);
4459  dest_cr -= h->uvlinesize * (block_h - 1);
4460  }
4461  } else {
4462  linesize = h->mb_linesize = h->linesize;
4463  uvlinesize = h->mb_uvlinesize = h->uvlinesize;
4464  }
4465  backup_mb_border(h, dest_y, dest_cb, dest_cr, linesize,
4466  uvlinesize, 0);
4467  if (fill_filter_caches(h, mb_type))
4468  continue;
4469  h->chroma_qp[0] = get_chroma_qp(h, 0, h->cur_pic.qscale_table[mb_xy]);
4470  h->chroma_qp[1] = get_chroma_qp(h, 1, h->cur_pic.qscale_table[mb_xy]);
4471 
4472  if (FRAME_MBAFF(h)) {
4473  ff_h264_filter_mb(h, mb_x, mb_y, dest_y, dest_cb, dest_cr,
4474  linesize, uvlinesize);
4475  } else {
4476  ff_h264_filter_mb_fast(h, mb_x, mb_y, dest_y, dest_cb,
4477  dest_cr, linesize, uvlinesize);
4478  }
4479  }
4480  }
4481  h->slice_type = old_slice_type;
4482  h->mb_x = end_x;
4483  h->mb_y = end_mb_y - FRAME_MBAFF(h);
4484  h->chroma_qp[0] = get_chroma_qp(h, 0, h->qscale);
4485  h->chroma_qp[1] = get_chroma_qp(h, 1, h->qscale);
4486 }
4487 
4489 {
4490  const int mb_xy = h->mb_x + h->mb_y * h->mb_stride;
4491  int mb_type = (h->slice_table[mb_xy - 1] == h->slice_num) ?
4492  h->cur_pic.mb_type[mb_xy - 1] :
4493  (h->slice_table[mb_xy - h->mb_stride] == h->slice_num) ?
4494  h->cur_pic.mb_type[mb_xy - h->mb_stride] : 0;
4495  h->mb_mbaff = h->mb_field_decoding_flag = IS_INTERLACED(mb_type) ? 1 : 0;
4496 }
4497 
4498 /**
4499  * Draw edges and report progress for the last MB row.
4500  */
4502 {
4503  int top = 16 * (h->mb_y >> FIELD_PICTURE(h));
4504  int pic_height = 16 * h->mb_height >> FIELD_PICTURE(h);
4505  int height = 16 << FRAME_MBAFF(h);
4506  int deblock_border = (16 + 4) << FRAME_MBAFF(h);
4507 
4508  if (h->deblocking_filter) {
4509  if ((top + height) >= pic_height)
4510  height += deblock_border;
4511  top -= deblock_border;
4512  }
4513 
4514  if (top >= pic_height || (top + height) < 0)
4515  return;
4516 
4517  height = FFMIN(height, pic_height - top);
4518  if (top < 0) {
4519  height = top + height;
4520  top = 0;
4521  }
4522 
4523  ff_h264_draw_horiz_band(h, top, height);
4524 
4525  if (h->droppable || h->er.error_occurred)
4526  return;
4527 
4528  ff_thread_report_progress(&h->cur_pic_ptr->tf, top + height - 1,
4530 }
4531 
4532 static void er_add_slice(H264Context *h, int startx, int starty,
4533  int endx, int endy, int status)
4534 {
4535  if (CONFIG_ERROR_RESILIENCE) {
4536  ERContext *er = &h->er;
4537 
4538  ff_er_add_slice(er, startx, starty, endx, endy, status);
4539  }
4540 }
4541 
4542 static int decode_slice(struct AVCodecContext *avctx, void *arg)
4543 {
4544  H264Context *h = *(void **)arg;
4545  int lf_x_start = h->mb_x;
4546 
4547  h->mb_skip_run = -1;
4548 
4549  av_assert0(h->block_offset[15] == (4 * ((scan8[15] - scan8[0]) & 7) << h->pixel_shift) + 4 * h->linesize * ((scan8[15] - scan8[0]) >> 3));
4550 
4552  avctx->codec_id != AV_CODEC_ID_H264 ||
4553  (CONFIG_GRAY && (h->flags & CODEC_FLAG_GRAY));
4554 
4556  const int start_i = av_clip(h->resync_mb_x + h->resync_mb_y * h->mb_width, 0, h->mb_num - 1);
4557  if (start_i) {
4558  int prev_status = h->er.error_status_table[h->er.mb_index2xy[start_i - 1]];
4559  prev_status &= ~ VP_START;
4560  if (prev_status != (ER_MV_END | ER_DC_END | ER_AC_END))
4561  h->er.error_occurred = 1;
4562  }
4563  }
4564 
4565  if (h->pps.cabac) {
4566  /* realign */
4567  align_get_bits(&h->gb);
4568 
4569  /* init cabac */
4571  h->gb.buffer + get_bits_count(&h->gb) / 8,
4572  (get_bits_left(&h->gb) + 7) / 8);
4573 
4575 
4576  for (;;) {
4577  // START_TIMER
4578  int ret = ff_h264_decode_mb_cabac(h);
4579  int eos;
4580  // STOP_TIMER("decode_mb_cabac")
4581 
4582  if (ret >= 0)
4584 
4585  // FIXME optimal? or let mb_decode decode 16x32 ?
4586  if (ret >= 0 && FRAME_MBAFF(h)) {
4587  h->mb_y++;
4588 
4589  ret = ff_h264_decode_mb_cabac(h);
4590 
4591  if (ret >= 0)
4593  h->mb_y--;
4594  }
4595  eos = get_cabac_terminate(&h->cabac);
4596 
4597  if ((h->workaround_bugs & FF_BUG_TRUNCATED) &&
4598  h->cabac.bytestream > h->cabac.bytestream_end + 2) {
4599  er_add_slice(h, h->resync_mb_x, h->resync_mb_y, h->mb_x - 1,
4600  h->mb_y, ER_MB_END);
4601  if (h->mb_x >= lf_x_start)
4602  loop_filter(h, lf_x_start, h->mb_x + 1);
4603  return 0;
4604  }
4605  if (h->cabac.bytestream > h->cabac.bytestream_end + 2 )
4606  av_log(h->avctx, AV_LOG_DEBUG, "bytestream overread %td\n", h->cabac.bytestream_end - h->cabac.bytestream);
4607  if (ret < 0 || h->cabac.bytestream > h->cabac.bytestream_end + 4) {
4609  "error while decoding MB %d %d, bytestream %td\n",
4610  h->mb_x, h->mb_y,
4612  er_add_slice(h, h->resync_mb_x, h->resync_mb_y, h->mb_x,
4613  h->mb_y, ER_MB_ERROR);
4614  return AVERROR_INVALIDDATA;
4615  }
4616 
4617  if (++h->mb_x >= h->mb_width) {
4618  loop_filter(h, lf_x_start, h->mb_x);
4619  h->mb_x = lf_x_start = 0;
4620  decode_finish_row(h);
4621  ++h->mb_y;
4622  if (FIELD_OR_MBAFF_PICTURE(h)) {
4623  ++h->mb_y;
4624  if (FRAME_MBAFF(h) && h->mb_y < h->mb_height)
4626  }
4627  }
4628 
4629  if (eos || h->mb_y >= h->mb_height) {
4630  tprintf(h->avctx, "slice end %d %d\n",
4631  get_bits_count(&h->gb), h->gb.size_in_bits);
4632  er_add_slice(h, h->resync_mb_x, h->resync_mb_y, h->mb_x - 1,
4633  h->mb_y, ER_MB_END);
4634  if (h->mb_x > lf_x_start)
4635  loop_filter(h, lf_x_start, h->mb_x);
4636  return 0;
4637  }
4638  }
4639  } else {
4640  for (;;) {
4641  int ret = ff_h264_decode_mb_cavlc(h);
4642 
4643  if (ret >= 0)
4645 
4646  // FIXME optimal? or let mb_decode decode 16x32 ?
4647  if (ret >= 0 && FRAME_MBAFF(h)) {
4648  h->mb_y++;
4649  ret = ff_h264_decode_mb_cavlc(h);
4650 
4651  if (ret >= 0)
4653  h->mb_y--;
4654  }
4655 
4656  if (ret < 0) {
4658  "error while decoding MB %d %d\n", h->mb_x, h->mb_y);
4659  er_add_slice(h, h->resync_mb_x, h->resync_mb_y, h->mb_x,
4660  h->mb_y, ER_MB_ERROR);
4661  return ret;
4662  }
4663 
4664  if (++h->mb_x >= h->mb_width) {
4665  loop_filter(h, lf_x_start, h->mb_x);
4666  h->mb_x = lf_x_start = 0;
4667  decode_finish_row(h);
4668  ++h->mb_y;
4669  if (FIELD_OR_MBAFF_PICTURE(h)) {
4670  ++h->mb_y;
4671  if (FRAME_MBAFF(h) && h->mb_y < h->mb_height)
4673  }
4674  if (h->mb_y >= h->mb_height) {
4675  tprintf(h->avctx, "slice end %d %d\n",
4676  get_bits_count(&h->gb), h->gb.size_in_bits);
4677 
4678  if ( get_bits_left(&h->gb) == 0
4679  || get_bits_left(&h->gb) > 0 && !(h->avctx->err_recognition & AV_EF_AGGRESSIVE)) {
4681  h->mb_x - 1, h->mb_y,
4682  ER_MB_END);
4683 
4684  return 0;
4685  } else {
4687  h->mb_x, h->mb_y,
4688  ER_MB_END);
4689 
4690  return AVERROR_INVALIDDATA;
4691  }
4692  }
4693  }
4694 
4695  if (get_bits_left(&h->gb) <= 0 && h->mb_skip_run <= 0) {
4696  tprintf(h->avctx, "slice end %d %d\n",
4697  get_bits_count(&h->gb), h->gb.size_in_bits);
4698 
4699  if (get_bits_left(&h->gb) == 0) {
4701  h->mb_x - 1, h->mb_y,
4702  ER_MB_END);
4703  if (h->mb_x > lf_x_start)
4704  loop_filter(h, lf_x_start, h->mb_x);
4705 
4706  return 0;
4707  } else {
4708  er_add_slice(h, h->resync_mb_x, h->resync_mb_y, h->mb_x,
4709  h->mb_y, ER_MB_ERROR);
4710 
4711  return AVERROR_INVALIDDATA;
4712  }
4713  }
4714  }
4715  }
4716 }
4717 
4718 /**
4719  * Call decode_slice() for each context.
4720  *
4721  * @param h h264 master context
4722  * @param context_count number of contexts to execute
4723  */
4724 static int execute_decode_slices(H264Context *h, unsigned context_count)
4725 {
4726  AVCodecContext *const avctx = h->avctx;
4727  H264Context *hx;
4728  int i;
4729 
4730  av_assert0(h->mb_y < h->mb_height);
4731 
4732  if (h->avctx->hwaccel ||
4734  return 0;
4735  if (context_count == 1) {
4736  return decode_slice(avctx, &h);
4737  } else {
4738  av_assert0(context_count > 0);
4739  for (i = 1; i < context_count; i++) {
4740  hx = h->thread_context[i];
4741  if (CONFIG_ERROR_RESILIENCE) {
4742  hx->er.error_count = 0;
4743  }
4744  hx->x264_build = h->x264_build;
4745  }
4746 
4747  avctx->execute(avctx, decode_slice, h->thread_context,
4748  NULL, context_count, sizeof(void *));
4749 
4750  /* pull back stuff from slices to master context */
4751  hx = h->thread_context[context_count - 1];
4752  h->mb_x = hx->mb_x;
4753  h->mb_y = hx->mb_y;
4754  h->droppable = hx->droppable;
4756  if (CONFIG_ERROR_RESILIENCE) {
4757  for (i = 1; i < context_count; i++)
4759  }
4760  }
4761 
4762  return 0;
4763 }
4764 
4765 static const uint8_t start_code[] = { 0x00, 0x00, 0x01 };
4766 
4767 static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size,
4768  int parse_extradata)
4769 {
4770  AVCodecContext *const avctx = h->avctx;
4771  H264Context *hx; ///< thread context
4772  int buf_index;
4773  unsigned context_count;
4774  int next_avc;
4775  int pass = !(avctx->active_thread_type & FF_THREAD_FRAME);
4776  int nals_needed = 0; ///< number of NALs that need decoding before the next frame thread starts
4777  int nal_index;
4778  int idr_cleared=0;
4779  int first_slice = 0;
4780  int ret = 0;
4781 
4782  h->nal_unit_type= 0;
4783 
4784  if(!h->slice_context_count)
4785  h->slice_context_count= 1;
4787  if (!(avctx->flags2 & CODEC_FLAG2_CHUNKS)) {
4788  h->current_slice = 0;
4789  if (!h->first_field)
4790  h->cur_pic_ptr = NULL;
4791  ff_h264_reset_sei(h);
4792  }
4793 
4794  if (h->nal_length_size == 4) {
4795  if (buf_size > 8 && AV_RB32(buf) == 1 && AV_RB32(buf+5) > (unsigned)buf_size) {
4796  h->is_avc = 0;
4797  }else if(buf_size > 3 && AV_RB32(buf) > 1 && AV_RB32(buf) <= (unsigned)buf_size)
4798  h->is_avc = 1;
4799  }
4800 
4801  for (; pass <= 1; pass++) {
4802  buf_index = 0;
4803  context_count = 0;
4804  next_avc = h->is_avc ? 0 : buf_size;
4805  nal_index = 0;
4806  for (;;) {
4807  int consumed;
4808  int dst_length;
4809  int bit_length;
4810  const uint8_t *ptr;
4811  int i, nalsize = 0;
4812  int err;
4813 
4814  if (buf_index >= next_avc) {
4815  if (buf_index >= buf_size - h->nal_length_size)
4816  break;
4817  nalsize = 0;
4818  for (i = 0; i < h->nal_length_size; i++)
4819  nalsize = (nalsize << 8) | buf[buf_index++];
4820  if (nalsize <= 0 || nalsize > buf_size - buf_index) {
4822  "AVC: nal size %d\n", nalsize);
4823  break;
4824  }
4825  next_avc = buf_index + nalsize;
4826  } else {
4827  // start code prefix search
4828  for (; buf_index + 3 < next_avc; buf_index++)
4829  // This should always succeed in the first iteration.
4830  if (buf[buf_index] == 0 &&
4831  buf[buf_index + 1] == 0 &&
4832  buf[buf_index + 2] == 1)
4833  break;
4834 
4835  if (buf_index + 3 >= buf_size) {
4836  buf_index = buf_size;
4837  break;
4838  }
4839 
4840  buf_index += 3;
4841  if (buf_index >= next_avc)
4842  continue;
4843  }
4844 
4845  hx = h->thread_context[context_count];
4846 
4847  ptr = ff_h264_decode_nal(hx, buf + buf_index, &dst_length,
4848  &consumed, next_avc - buf_index);
4849  if (ptr == NULL || dst_length < 0) {
4850  ret = -1;
4851  goto end;
4852  }
4853  i = buf_index + consumed;
4854  if ((h->workaround_bugs & FF_BUG_AUTODETECT) && i + 3 < next_avc &&
4855  buf[i] == 0x00 && buf[i + 1] == 0x00 &&
4856  buf[i + 2] == 0x01 && buf[i + 3] == 0xE0)
4858 
4859  if (!(h->workaround_bugs & FF_BUG_TRUNCATED))
4860  while (dst_length > 0 && ptr[dst_length - 1] == 0)
4861  dst_length--;
4862  bit_length = !dst_length ? 0
4863  : (8 * dst_length -
4864  decode_rbsp_trailing(h, ptr + dst_length - 1));
4865 
4866  if (h->avctx->debug & FF_DEBUG_STARTCODE)
4868  "NAL %d/%d at %d/%d length %d pass %d\n",
4869  hx->nal_unit_type, hx->nal_ref_idc, buf_index, buf_size, dst_length, pass);
4870 
4871  if (h->is_avc && (nalsize != consumed) && nalsize)
4873  "AVC: Consumed only %d bytes instead of %d\n",
4874  consumed, nalsize);
4875 
4876  buf_index += consumed;
4877  nal_index++;
4878 
4879  if (pass == 0) {
4880  /* packets can sometimes contain multiple PPS/SPS,
4881  * e.g. two PAFF field pictures in one packet, or a demuxer
4882  * which splits NALs strangely if so, when frame threading we
4883  * can't start the next thread until we've read all of them */
4884  switch (hx->nal_unit_type) {
4885  case NAL_SPS:
4886  case NAL_PPS:
4887  nals_needed = nal_index;
4888  break;
4889  case NAL_DPA:
4890  case NAL_IDR_SLICE:
4891  case NAL_SLICE:
4892  init_get_bits(&hx->gb, ptr, bit_length);
4893  if (!get_ue_golomb(&hx->gb) ||
4894  !first_slice ||
4895  first_slice != hx->nal_unit_type)
4896  nals_needed = nal_index;
4897  if (!first_slice)
4898  first_slice = hx->nal_unit_type;
4899  }
4900  continue;
4901  }
4902 
4903  if (!first_slice)
4904  switch (hx->nal_unit_type) {
4905  case NAL_DPA:
4906  case NAL_IDR_SLICE:
4907  case NAL_SLICE:
4908  first_slice = hx->nal_unit_type;
4909  }
4910 
4911  if (avctx->skip_frame >= AVDISCARD_NONREF &&
4912  h->nal_ref_idc == 0 &&
4913  h->nal_unit_type != NAL_SEI)
4914  continue;
4915 
4916 again:
4917  if ( !(avctx->active_thread_type & FF_THREAD_FRAME)
4918  || nals_needed >= nal_index)
4919  h->au_pps_id = -1;
4920  /* Ignore per frame NAL unit type during extradata
4921  * parsing. Decoding slices is not possible in codec init
4922  * with frame-mt */
4923  if (parse_extradata) {
4924  switch (hx->nal_unit_type) {
4925  case NAL_IDR_SLICE:
4926  case NAL_SLICE:
4927  case NAL_DPA:
4928  case NAL_DPB:
4929  case NAL_DPC:
4931  "Ignoring NAL %d in global header/extradata\n",
4932  hx->nal_unit_type);
4933  // fall through to next case
4934  case NAL_AUXILIARY_SLICE:
4936  }
4937  }
4938 
4939  err = 0;
4940 
4941  switch (hx->nal_unit_type) {
4942  case NAL_IDR_SLICE:
4943  if (h->nal_unit_type != NAL_IDR_SLICE) {
4945  "Invalid mix of idr and non-idr slices\n");
4946  ret = -1;
4947  goto end;
4948  }
4949  if(!idr_cleared)
4950  idr(h); // FIXME ensure we don't lose some frames if there is reordering
4951  idr_cleared = 1;
4952  case NAL_SLICE:
4953  init_get_bits(&hx->gb, ptr, bit_length);
4954  hx->intra_gb_ptr =
4955  hx->inter_gb_ptr = &hx->gb;
4956  hx->data_partitioning = 0;
4957 
4958  if ((err = decode_slice_header(hx, h)))
4959  break;
4960 
4961  if (h->sei_recovery_frame_cnt >= 0) {
4963  h->valid_recovery_point = 1;
4964 
4965  if ( h->recovery_frame < 0
4966  || ((h->recovery_frame - h->frame_num) & ((1 << h->sps.log2_max_frame_num)-1)) > h->sei_recovery_frame_cnt) {
4968  ((1 << h->sps.log2_max_frame_num) - 1);
4969 
4970  if (!h->valid_recovery_point)
4971  h->recovery_frame = h->frame_num;
4972  }
4973  }
4974 
4975  h->cur_pic_ptr->f.key_frame |=
4976  (hx->nal_unit_type == NAL_IDR_SLICE);
4977 
4978  if (hx->nal_unit_type == NAL_IDR_SLICE ||
4979  h->recovery_frame == h->frame_num) {
4980  h->recovery_frame = -1;
4981  h->cur_pic_ptr->recovered = 1;
4982  }
4983  // If we have an IDR, all frames after it in decoded order are
4984  // "recovered".
4985  if (hx->nal_unit_type == NAL_IDR_SLICE)
4987  h->frame_recovered |= 3*!!(avctx->flags2 & CODEC_FLAG2_SHOW_ALL);
4988  h->frame_recovered |= 3*!!(avctx->flags & CODEC_FLAG_OUTPUT_CORRUPT);
4989 #if 1
4991 #else
4993 #endif
4994 
4995  if (h->current_slice == 1) {
4996  if (!(avctx->flags2 & CODEC_FLAG2_CHUNKS))
4997  decode_postinit(h, nal_index >= nals_needed);
4998 
4999  if (h->avctx->hwaccel &&
5000  (ret = h->avctx->hwaccel->start_frame(h->avctx, NULL, 0)) < 0)
5001  return ret;
5002  if (CONFIG_H264_VDPAU_DECODER &&
5005  }
5006 
5007  if (hx->redundant_pic_count == 0 &&
5008  (avctx->skip_frame < AVDISCARD_NONREF ||
5009  hx->nal_ref_idc) &&
5010  (avctx->skip_frame < AVDISCARD_BIDIR ||
5012  (avctx->skip_frame < AVDISCARD_NONKEY ||
5014  avctx->skip_frame < AVDISCARD_ALL) {
5015  if (avctx->hwaccel) {
5016  ret = avctx->hwaccel->decode_slice(avctx,
5017  &buf[buf_index - consumed],
5018  consumed);
5019  if (ret < 0)
5020  return ret;
5021  } else if (CONFIG_H264_VDPAU_DECODER &&
5024  start_code,
5025  sizeof(start_code));
5027  &buf[buf_index - consumed],
5028  consumed);
5029  } else
5030  context_count++;
5031  }
5032  break;
5033  case NAL_DPA:
5034  if (h->avctx->flags & CODEC_FLAG2_CHUNKS) {
5036  "Decoding in chunks is not supported for "
5037  "partitioned slices.\n");
5038  return AVERROR(ENOSYS);
5039  }
5040 
5041  init_get_bits(&hx->gb, ptr, bit_length);
5042  hx->intra_gb_ptr =
5043  hx->inter_gb_ptr = NULL;
5044 
5045  if ((err = decode_slice_header(hx, h)) < 0) {
5046  /* make sure data_partitioning is cleared if it was set
5047  * before, so we don't try decoding a slice without a valid
5048  * slice header later */
5049  h->data_partitioning = 0;
5050  break;
5051  }
5052 
5053  hx->data_partitioning = 1;
5054  break;
5055  case NAL_DPB:
5056  init_get_bits(&hx->intra_gb, ptr, bit_length);
5057  hx->intra_gb_ptr = &hx->intra_gb;
5058  break;
5059  case NAL_DPC:
5060  init_get_bits(&hx->inter_gb, ptr, bit_length);
5061  hx->inter_gb_ptr = &hx->inter_gb;
5062 
5063  av_log(h->avctx, AV_LOG_ERROR, "Partitioned H.264 support is incomplete\n");
5064  break;
5065 
5066  if (hx->redundant_pic_count == 0 &&
5067  hx->intra_gb_ptr &&
5068  hx->data_partitioning &&
5069  h->cur_pic_ptr && h->context_initialized &&
5070  (avctx->skip_frame < AVDISCARD_NONREF || hx->nal_ref_idc) &&
5071  (avctx->skip_frame < AVDISCARD_BIDIR ||
5073  (avctx->skip_frame < AVDISCARD_NONKEY ||
5075  avctx->skip_frame < AVDISCARD_ALL)
5076  context_count++;
5077  break;
5078  case NAL_SEI:
5079  init_get_bits(&h->gb, ptr, bit_length);
5080  ff_h264_decode_sei(h);
5081  break;
5082  case NAL_SPS:
5083  init_get_bits(&h->gb, ptr, bit_length);
5084  if (ff_h264_decode_seq_parameter_set(h) < 0 && (h->is_avc ? nalsize : 1)) {
5086  "SPS decoding failure, trying again with the complete NAL\n");
5087  if (h->is_avc)
5088  av_assert0(next_avc - buf_index + consumed == nalsize);
5089  if ((next_avc - buf_index + consumed - 1) >= INT_MAX/8)
5090  break;
5091  init_get_bits(&h->gb, &buf[buf_index + 1 - consumed],
5092  8*(next_avc - buf_index + consumed - 1));
5094  }
5095 
5096  break;
5097  case NAL_PPS:
5098  init_get_bits(&h->gb, ptr, bit_length);
5099  ff_h264_decode_picture_parameter_set(h, bit_length);
5100  break;
5101  case NAL_AUD:
5102  case NAL_END_SEQUENCE:
5103  case NAL_END_STREAM:
5104  case NAL_FILLER_DATA:
5105  case NAL_SPS_EXT:
5106  case NAL_AUXILIARY_SLICE:
5107  break;
5108  case NAL_FF_IGNORE:
5109  break;
5110  default:
5111  av_log(avctx, AV_LOG_DEBUG, "Unknown NAL code: %d (%d bits)\n",
5112  hx->nal_unit_type, bit_length);
5113  }
5114 
5115  if (context_count == h->max_contexts) {
5116  execute_decode_slices(h, context_count);
5117  context_count = 0;
5118  }
5119 
5120  if (err < 0) {
5121  av_log(h->avctx, AV_LOG_ERROR, "decode_slice_header error\n");
5122  h->ref_count[0] = h->ref_count[1] = h->list_count = 0;
5123  } else if (err == 1) {
5124  /* Slice could not be decoded in parallel mode, copy down
5125  * NAL unit stuff to context 0 and restart. Note that
5126  * rbsp_buffer is not transferred, but since we no longer
5127  * run in parallel mode this should not be an issue. */
5128  h->nal_unit_type = hx->nal_unit_type;
5129  h->nal_ref_idc = hx->nal_ref_idc;
5130  hx = h;
5131  goto again;
5132  }
5133  }
5134  }
5135  if (context_count)
5136  execute_decode_slices(h, context_count);
5137 
5138 end:
5139  /* clean up */
5140  if (h->cur_pic_ptr && !h->droppable) {
5143  }
5144 
5145  return (ret < 0) ? ret : buf_index;
5146 }
5147 
5148 /**
5149  * Return the number of bytes consumed for building the current frame.
5150  */
5151 static int get_consumed_bytes(int pos, int buf_size)
5152 {
5153  if (pos == 0)
5154  pos = 1; // avoid infinite loops (i doubt that is needed but ...)
5155  if (pos + 10 > buf_size)
5156  pos = buf_size; // oops ;)
5157 
5158  return pos;
5159 }
5160 
5162 {
5163  AVFrame *src = &srcp->f;
5164  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(src->format);
5165  int i;
5166  int ret = av_frame_ref(dst, src);
5167  if (ret < 0)
5168  return ret;
5169 
5170  av_dict_set(&dst->metadata, "stereo_mode", ff_h264_sei_stereo_mode(h), 0);
5171 
5172  if (!srcp->crop)
5173  return 0;
5174 
5175  for (i = 0; i < desc->nb_components; i++) {
5176  int hshift = (i > 0) ? desc->log2_chroma_w : 0;
5177  int vshift = (i > 0) ? desc->log2_chroma_h : 0;
5178  int off = ((srcp->crop_left >> hshift) << h->pixel_shift) +
5179  (srcp->crop_top >> vshift) * dst->linesize[i];
5180  dst->data[i] += off;
5181  }
5182  return 0;
5183 }
5184 
5185 static int h264_decode_frame(AVCodecContext *avctx, void *data,
5186  int *got_frame, AVPacket *avpkt)
5187 {
5188  const uint8_t *buf = avpkt->data;
5189  int buf_size = avpkt->size;
5190  H264Context *h = avctx->priv_data;
5191  AVFrame *pict = data;
5192  int buf_index = 0;
5193  Picture *out;
5194  int i, out_idx;
5195  int ret;
5196 
5197  h->flags = avctx->flags;
5198  /* reset data partitioning here, to ensure GetBitContexts from previous
5199  * packets do not get used. */
5200  h->data_partitioning = 0;
5201 
5202  /* end of stream, output what is still in the buffers */
5203  if (buf_size == 0) {
5204  out:
5205 
5206  h->cur_pic_ptr = NULL;
5207  h->first_field = 0;
5208 
5209  // FIXME factorize this with the output code below
5210  out = h->delayed_pic[0];
5211  out_idx = 0;
5212  for (i = 1;
5213  h->delayed_pic[i] &&
5214  !h->delayed_pic[i]->f.key_frame &&
5215  !h->delayed_pic[i]->mmco_reset;
5216  i++)
5217  if (h->delayed_pic[i]->poc < out->poc) {
5218  out = h->delayed_pic[i];
5219  out_idx = i;
5220  }
5221 
5222  for (i = out_idx; h->delayed_pic[i]; i++)
5223  h->delayed_pic[i] = h->delayed_pic[i + 1];
5224 
5225  if (out) {
5226  out->reference &= ~DELAYED_PIC_REF;
5227  ret = output_frame(h, pict, out);
5228  if (ret < 0)
5229  return ret;
5230  *got_frame = 1;
5231  }
5232 
5233  return buf_index;
5234  }
5235  if(h->is_avc && buf_size >= 9 && buf[0]==1 && buf[2]==0 && (buf[4]&0xFC)==0xFC && (buf[5]&0x1F) && buf[8]==0x67){
5236  int cnt= buf[5]&0x1f;
5237  const uint8_t *p= buf+6;
5238  while(cnt--){
5239  int nalsize= AV_RB16(p) + 2;
5240  if(nalsize > buf_size - (p-buf) || p[2]!=0x67)
5241  goto not_extra;
5242  p += nalsize;
5243  }
5244  cnt = *(p++);
5245  if(!cnt)
5246  goto not_extra;
5247  while(cnt--){
5248  int nalsize= AV_RB16(p) + 2;
5249  if(nalsize > buf_size - (p-buf) || p[2]!=0x68)
5250  goto not_extra;
5251  p += nalsize;
5252  }
5253 
5254  return ff_h264_decode_extradata(h, buf, buf_size);
5255  }
5256 not_extra:
5257 
5258  buf_index = decode_nal_units(h, buf, buf_size, 0);
5259  if (buf_index < 0)
5260  return AVERROR_INVALIDDATA;
5261 
5262  if (!h->cur_pic_ptr && h->nal_unit_type == NAL_END_SEQUENCE) {
5263  av_assert0(buf_index <= buf_size);
5264  goto out;
5265  }
5266 
5267  if (!(avctx->flags2 & CODEC_FLAG2_CHUNKS) && !h->cur_pic_ptr) {
5268  if (avctx->skip_frame >= AVDISCARD_NONREF ||
5269  buf_size >= 4 && !memcmp("Q264", buf, 4))
5270  return buf_size;
5271  av_log(avctx, AV_LOG_ERROR, "no frame!\n");
5272  return AVERROR_INVALIDDATA;
5273  }
5274 
5275  if (!(avctx->flags2 & CODEC_FLAG2_CHUNKS) ||
5276  (h->mb_y >= h->mb_height && h->mb_height)) {
5277  if (avctx->flags2 & CODEC_FLAG2_CHUNKS)
5278  decode_postinit(h, 1);
5279 
5280  field_end(h, 0);
5281 
5282  /* Wait for second field. */
5283  *got_frame = 0;
5284  if (h->next_output_pic && (
5285  h->next_output_pic->recovered)) {
5286  if (!h->next_output_pic->recovered)
5288 
5289  ret = output_frame(h, pict, h->next_output_pic);
5290  if (ret < 0)
5291  return ret;
5292  *got_frame = 1;
5293  if (CONFIG_MPEGVIDEO) {
5295  &h->low_delay,
5296  h->mb_width, h->mb_height, h->mb_stride, 1);
5297  }
5298  }
5299  }
5300 
5301  assert(pict->buf[0] || !*got_frame);
5302 
5303  return get_consumed_bytes(buf_index, buf_size);
5304 }
5305 
5307 {
5308  int i;
5309 
5310  free_tables(h, 1); // FIXME cleanup init stuff perhaps
5311 
5312  for (i = 0; i < MAX_SPS_COUNT; i++)
5313  av_freep(h->sps_buffers + i);
5314 
5315  for (i = 0; i < MAX_PPS_COUNT; i++)
5316  av_freep(h->pps_buffers + i);
5317 }
5318 
5320 {
5321  H264Context *h = avctx->priv_data;
5322 
5325 
5326  unref_picture(h, &h->cur_pic);
5327 
5328  return 0;
5329 }
5330 
5331 static const AVProfile profiles[] = {
5332  { FF_PROFILE_H264_BASELINE, "Baseline" },
5333  { FF_PROFILE_H264_CONSTRAINED_BASELINE, "Constrained Baseline" },
5334  { FF_PROFILE_H264_MAIN, "Main" },
5335  { FF_PROFILE_H264_EXTENDED, "Extended" },
5336  { FF_PROFILE_H264_HIGH, "High" },
5337  { FF_PROFILE_H264_HIGH_10, "High 10" },
5338  { FF_PROFILE_H264_HIGH_10_INTRA, "High 10 Intra" },
5339  { FF_PROFILE_H264_HIGH_422, "High 4:2:2" },
5340  { FF_PROFILE_H264_HIGH_422_INTRA, "High 4:2:2 Intra" },
5341  { FF_PROFILE_H264_HIGH_444, "High 4:4:4" },
5342  { FF_PROFILE_H264_HIGH_444_PREDICTIVE, "High 4:4:4 Predictive" },
5343  { FF_PROFILE_H264_HIGH_444_INTRA, "High 4:4:4 Intra" },
5344  { FF_PROFILE_H264_CAVLC_444, "CAVLC 4:4:4" },
5345  { FF_PROFILE_UNKNOWN },
5346 };
5347 
5348 static const AVOption h264_options[] = {
5349  {"is_avc", "is avc", offsetof(H264Context, is_avc), FF_OPT_TYPE_INT, {.i64 = 0}, 0, 1, 0},
5350  {"nal_length_size", "nal_length_size", offsetof(H264Context, nal_length_size), FF_OPT_TYPE_INT, {.i64 = 0}, 0, 4, 0},
5351  {NULL}
5352 };
5353 
5354 static const AVClass h264_class = {
5355  .class_name = "H264 Decoder",
5356  .item_name = av_default_item_name,
5357  .option = h264_options,
5358  .version = LIBAVUTIL_VERSION_INT,
5359 };
5360 
5361 static const AVClass h264_vdpau_class = {
5362  .class_name = "H264 VDPAU Decoder",
5363  .item_name = av_default_item_name,
5364  .option = h264_options,
5365  .version = LIBAVUTIL_VERSION_INT,
5366 };
5367 
5369  .name = "h264",
5370  .long_name = NULL_IF_CONFIG_SMALL("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10"),
5371  .type = AVMEDIA_TYPE_VIDEO,
5372  .id = AV_CODEC_ID_H264,
5373  .priv_data_size = sizeof(H264Context),
5377  .capabilities = /*CODEC_CAP_DRAW_HORIZ_BAND |*/ CODEC_CAP_DR1 |
5380  .flush = flush_dpb,
5382  .update_thread_context = ONLY_IF_THREADS_ENABLED(decode_update_thread_context),
5383  .profiles = NULL_IF_CONFIG_SMALL(profiles),
5384  .priv_class = &h264_class,
5385 };
5386 
5387 #if CONFIG_H264_VDPAU_DECODER
5388 AVCodec ff_h264_vdpau_decoder = {
5389  .name = "h264_vdpau",
5390  .long_name = NULL_IF_CONFIG_SMALL("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (VDPAU acceleration)"),
5391  .type = AVMEDIA_TYPE_VIDEO,
5392  .id = AV_CODEC_ID_H264,
5393  .priv_data_size = sizeof(H264Context),
5398  .flush = flush_dpb,
5399  .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_VDPAU_H264,
5400  AV_PIX_FMT_NONE},
5401  .profiles = NULL_IF_CONFIG_SMALL(profiles),
5402  .priv_class = &h264_vdpau_class,
5403 };
5404 #endif