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