FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
h264.c
Go to the documentation of this file.
1 /*
2  * H.26L/H.264/AVC/JVT/14496-10/... decoder
3  * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 /**
23  * @file
24  * H.264 / AVC / MPEG4 part10 codec.
25  * @author Michael Niedermayer <michaelni@gmx.at>
26  */
27 
28 #define UNCHECKED_BITSTREAM_READER 1
29 
30 #include "libavutil/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 
345 /**
346  * Check if the top & left blocks are available if needed and
347  * change the dc mode so it only uses the available blocks.
348  */
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 
392 /**
393  * Check if the top & left blocks are available if needed and
394  * change the dc mode so it only uses the available blocks.
395  */
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 (is_chroma && (h->left_samples_available & 0x8080)) {
421  // mad cow disease mode, aka MBAFF + constrained_intra_pred
422  mode = ALZHEIMER_DC_L0T_PRED8x8 +
423  (!(h->left_samples_available & 0x8000)) +
424  2 * (mode == DC_128_PRED8x8);
425  }
426  if (mode < 0) {
428  "left block unavailable for requested intra mode at %d %d\n",
429  h->mb_x, h->mb_y);
430  return -1;
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 
549 /**
550  * Identify the exact end of the bitstream
551  * @return the length of the trailing, or 0 if damaged
552  */
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 
619 /**
620  * Wait until all reference frames are available for MC operations.
621  *
622  * @param h the H264 context
623  */
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  for (i = 0; i < h->picture_count && !h->avctx->internal->is_copy; i++)
1047  free_picture(h, &h->DPB[i]);
1048  av_freep(&h->DPB);
1049  h->picture_count = 0;
1050  } else if (h->DPB) {
1051  for (i = 0; i < h->picture_count; i++)
1052  h->DPB[i].needs_realloc = 1;
1053  }
1054 
1055  h->cur_pic_ptr = NULL;
1056 
1057  for (i = 0; i < MAX_THREADS; i++) {
1058  hx = h->thread_context[i];
1059  if (!hx)
1060  continue;
1061  av_freep(&hx->top_borders[1]);
1062  av_freep(&hx->top_borders[0]);
1064  av_freep(&hx->edge_emu_buffer);
1065  av_freep(&hx->dc_val_base);
1066  av_freep(&hx->me.scratchpad);
1067  av_freep(&hx->er.mb_index2xy);
1069  av_freep(&hx->er.er_temp_buffer);
1070  av_freep(&hx->er.mbintra_table);
1071  av_freep(&hx->er.mbskip_table);
1072 
1073  if (free_rbsp) {
1074  av_freep(&hx->rbsp_buffer[1]);
1075  av_freep(&hx->rbsp_buffer[0]);
1076  hx->rbsp_buffer_size[0] = 0;
1077  hx->rbsp_buffer_size[1] = 0;
1078  }
1079  if (i)
1080  av_freep(&h->thread_context[i]);
1081  }
1082 }
1083 
1085 {
1086  int i, j, q, x;
1087  const int max_qp = 51 + 6 * (h->sps.bit_depth_luma - 8);
1088 
1089  for (i = 0; i < 6; i++) {
1090  h->dequant8_coeff[i] = h->dequant8_buffer[i];
1091  for (j = 0; j < i; j++)
1092  if (!memcmp(h->pps.scaling_matrix8[j], h->pps.scaling_matrix8[i],
1093  64 * sizeof(uint8_t))) {
1094  h->dequant8_coeff[i] = h->dequant8_buffer[j];
1095  break;
1096  }
1097  if (j < i)
1098  continue;
1099 
1100  for (q = 0; q < max_qp + 1; q++) {
1101  int shift = div6[q];
1102  int idx = rem6[q];
1103  for (x = 0; x < 64; x++)
1104  h->dequant8_coeff[i][q][(x >> 3) | ((x & 7) << 3)] =
1105  ((uint32_t)dequant8_coeff_init[idx][dequant8_coeff_init_scan[((x >> 1) & 12) | (x & 3)]] *
1106  h->pps.scaling_matrix8[i][x]) << shift;
1107  }
1108  }
1109 }
1110 
1112 {
1113  int i, j, q, x;
1114  const int max_qp = 51 + 6 * (h->sps.bit_depth_luma - 8);
1115  for (i = 0; i < 6; i++) {
1116  h->dequant4_coeff[i] = h->dequant4_buffer[i];
1117  for (j = 0; j < i; j++)
1118  if (!memcmp(h->pps.scaling_matrix4[j], h->pps.scaling_matrix4[i],
1119  16 * sizeof(uint8_t))) {
1120  h->dequant4_coeff[i] = h->dequant4_buffer[j];
1121  break;
1122  }
1123  if (j < i)
1124  continue;
1125 
1126  for (q = 0; q < max_qp + 1; q++) {
1127  int shift = div6[q] + 2;
1128  int idx = rem6[q];
1129  for (x = 0; x < 16; x++)
1130  h->dequant4_coeff[i][q][(x >> 2) | ((x << 2) & 0xF)] =
1131  ((uint32_t)dequant4_coeff_init[idx][(x & 1) + ((x >> 2) & 1)] *
1132  h->pps.scaling_matrix4[i][x]) << shift;
1133  }
1134  }
1135 }
1136 
1138 {
1139  int i, x;
1141  if (h->pps.transform_8x8_mode)
1143  if (h->sps.transform_bypass) {
1144  for (i = 0; i < 6; i++)
1145  for (x = 0; x < 16; x++)
1146  h->dequant4_coeff[i][0][x] = 1 << 6;
1148  for (i = 0; i < 6; i++)
1149  for (x = 0; x < 64; x++)
1150  h->dequant8_coeff[i][0][x] = 1 << 6;
1151  }
1152 }
1153 
1155 {
1156  const int big_mb_num = h->mb_stride * (h->mb_height + 1);
1157  const int row_mb_num = 2*h->mb_stride*FFMAX(h->avctx->thread_count, 1);
1158  int x, y, i;
1159 
1161  row_mb_num * 8 * sizeof(uint8_t), fail)
1163  big_mb_num * 48 * sizeof(uint8_t), fail)
1165  (big_mb_num + h->mb_stride) * sizeof(*h->slice_table_base), fail)
1167  big_mb_num * sizeof(uint16_t), fail)
1169  big_mb_num * sizeof(uint8_t), fail)
1170  FF_ALLOCZ_OR_GOTO(h->avctx, h->mvd_table[0],
1171  16 * row_mb_num * sizeof(uint8_t), fail);
1172  FF_ALLOCZ_OR_GOTO(h->avctx, h->mvd_table[1],
1173  16 * row_mb_num * sizeof(uint8_t), fail);
1175  4 * big_mb_num * sizeof(uint8_t), fail);
1177  big_mb_num * sizeof(uint8_t), fail)
1178 
1179  memset(h->slice_table_base, -1,
1180  (big_mb_num + h->mb_stride) * sizeof(*h->slice_table_base));
1181  h->slice_table = h->slice_table_base + h->mb_stride * 2 + 1;
1182 
1184  big_mb_num * sizeof(uint32_t), fail);
1186  big_mb_num * sizeof(uint32_t), fail);
1187  for (y = 0; y < h->mb_height; y++)
1188  for (x = 0; x < h->mb_width; x++) {
1189  const int mb_xy = x + y * h->mb_stride;
1190  const int b_xy = 4 * x + 4 * y * h->b_stride;
1191 
1192  h->mb2b_xy[mb_xy] = b_xy;
1193  h->mb2br_xy[mb_xy] = 8 * (FMO ? mb_xy : (mb_xy % (2 * h->mb_stride)));
1194  }
1195 
1196  if (!h->dequant4_coeff[0])
1198 
1199  if (!h->DPB) {
1201  h->DPB = av_mallocz_array(h->picture_count, sizeof(*h->DPB));
1202  if (!h->DPB)
1203  return AVERROR(ENOMEM);
1204  for (i = 0; i < h->picture_count; i++)
1207  }
1208 
1209  return 0;
1210 
1211 fail:
1212  free_tables(h, 1);
1213  return -1;
1214 }
1215 
1216 /**
1217  * Mimic alloc_tables(), but for every context thread.
1218  */
1219 static void clone_tables(H264Context *dst, H264Context *src, int i)
1220 {
1221  dst->intra4x4_pred_mode = src->intra4x4_pred_mode + i * 8 * 2 * src->mb_stride;
1222  dst->non_zero_count = src->non_zero_count;
1223  dst->slice_table = src->slice_table;
1224  dst->cbp_table = src->cbp_table;
1225  dst->mb2b_xy = src->mb2b_xy;
1226  dst->mb2br_xy = src->mb2br_xy;
1228  dst->mvd_table[0] = src->mvd_table[0] + i * 8 * 2 * src->mb_stride;
1229  dst->mvd_table[1] = src->mvd_table[1] + i * 8 * 2 * src->mb_stride;
1230  dst->direct_table = src->direct_table;
1231  dst->list_counts = src->list_counts;
1232  dst->DPB = src->DPB;
1233  dst->cur_pic_ptr = src->cur_pic_ptr;
1234  dst->cur_pic = src->cur_pic;
1235  dst->bipred_scratchpad = NULL;
1236  dst->edge_emu_buffer = NULL;
1237  dst->me.scratchpad = NULL;
1239  src->sps.chroma_format_idc);
1240 }
1241 
1242 /**
1243  * Init context
1244  * Allocate buffers which are not shared amongst multiple threads.
1245  */
1247 {
1248  ERContext *er = &h->er;
1249  int mb_array_size = h->mb_height * h->mb_stride;
1250  int y_size = (2 * h->mb_width + 1) * (2 * h->mb_height + 1);
1251  int c_size = h->mb_stride * (h->mb_height + 1);
1252  int yc_size = y_size + 2 * c_size;
1253  int x, y, i;
1254 
1256  h->mb_width * 16 * 3 * sizeof(uint8_t) * 2, fail)
1258  h->mb_width * 16 * 3 * sizeof(uint8_t) * 2, fail)
1259 
1260  h->ref_cache[0][scan8[5] + 1] =
1261  h->ref_cache[0][scan8[7] + 1] =
1262  h->ref_cache[0][scan8[13] + 1] =
1263  h->ref_cache[1][scan8[5] + 1] =
1264  h->ref_cache[1][scan8[7] + 1] =
1265  h->ref_cache[1][scan8[13] + 1] = PART_NOT_AVAILABLE;
1266 
1267  if (CONFIG_ERROR_RESILIENCE) {
1268  /* init ER */
1269  er->avctx = h->avctx;
1270  er->dsp = &h->dsp;
1272  er->opaque = h;
1273  er->quarter_sample = 1;
1274 
1275  er->mb_num = h->mb_num;
1276  er->mb_width = h->mb_width;
1277  er->mb_height = h->mb_height;
1278  er->mb_stride = h->mb_stride;
1279  er->b8_stride = h->mb_width * 2 + 1;
1280 
1281  FF_ALLOCZ_OR_GOTO(h->avctx, er->mb_index2xy, (h->mb_num + 1) * sizeof(int),
1282  fail); // error ressilience code looks cleaner with this
1283  for (y = 0; y < h->mb_height; y++)
1284  for (x = 0; x < h->mb_width; x++)
1285  er->mb_index2xy[x + y * h->mb_width] = x + y * h->mb_stride;
1286 
1287  er->mb_index2xy[h->mb_height * h->mb_width] = (h->mb_height - 1) *
1288  h->mb_stride + h->mb_width;
1289 
1291  mb_array_size * sizeof(uint8_t), fail);
1292 
1293  FF_ALLOC_OR_GOTO(h->avctx, er->mbintra_table, mb_array_size, fail);
1294  memset(er->mbintra_table, 1, mb_array_size);
1295 
1296  FF_ALLOCZ_OR_GOTO(h->avctx, er->mbskip_table, mb_array_size + 2, fail);
1297 
1299  fail);
1300 
1301  FF_ALLOCZ_OR_GOTO(h->avctx, h->dc_val_base, yc_size * sizeof(int16_t), fail);
1302  er->dc_val[0] = h->dc_val_base + h->mb_width * 2 + 2;
1303  er->dc_val[1] = h->dc_val_base + y_size + h->mb_stride + 1;
1304  er->dc_val[2] = er->dc_val[1] + c_size;
1305  for (i = 0; i < yc_size; i++)
1306  h->dc_val_base[i] = 1024;
1307  }
1308 
1309  return 0;
1310 
1311 fail:
1312  return -1; // free_tables will clean up for us
1313 }
1314 
1315 static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size,
1316  int parse_extradata);
1317 
1319 {
1320 
1321  h->width = h->avctx->width;
1322  h->height = h->avctx->height;
1323 
1324  h->bit_depth_luma = 8;
1325  h->chroma_format_idc = 1;
1326 
1327  h->avctx->bits_per_raw_sample = 8;
1328  h->cur_chroma_format_idc = 1;
1329 
1330  ff_h264dsp_init(&h->h264dsp, 8, 1);
1331  av_assert0(h->sps.bit_depth_chroma == 0);
1333  ff_h264qpel_init(&h->h264qpel, 8);
1334  ff_h264_pred_init(&h->hpc, h->avctx->codec_id, 8, 1);
1335 
1336  h->dequant_coeff_pps = -1;
1337 
1338  if (CONFIG_ERROR_RESILIENCE) {
1339  h->dsp.dct_bits = 16;
1340  /* needed so that IDCT permutation is known early */
1341  ff_dsputil_init(&h->dsp, h->avctx);
1342  }
1343  ff_videodsp_init(&h->vdsp, 8);
1344 
1345  memset(h->pps.scaling_matrix4, 16, 6 * 16 * sizeof(uint8_t));
1346  memset(h->pps.scaling_matrix8, 16, 2 * 64 * sizeof(uint8_t));
1347 }
1348 
1350 {
1351  AVCodecContext *avctx = h->avctx;
1352 
1353  if (!buf || size <= 0)
1354  return -1;
1355 
1356  if (buf[0] == 1) {
1357  int i, cnt, nalsize;
1358  const unsigned char *p = buf;
1359 
1360  h->is_avc = 1;
1361 
1362  if (size < 7) {
1363  av_log(avctx, AV_LOG_ERROR, "avcC too short\n");
1364  return -1;
1365  }
1366  /* sps and pps in the avcC always have length coded with 2 bytes,
1367  * so put a fake nal_length_size = 2 while parsing them */
1368  h->nal_length_size = 2;
1369  // Decode sps from avcC
1370  cnt = *(p + 5) & 0x1f; // Number of sps
1371  p += 6;
1372  for (i = 0; i < cnt; i++) {
1373  nalsize = AV_RB16(p) + 2;
1374  if(nalsize > size - (p-buf))
1375  return -1;
1376  if (decode_nal_units(h, p, nalsize, 1) < 0) {
1377  av_log(avctx, AV_LOG_ERROR,
1378  "Decoding sps %d from avcC failed\n", i);
1379  return -1;
1380  }
1381  p += nalsize;
1382  }
1383  // Decode pps from avcC
1384  cnt = *(p++); // Number of pps
1385  for (i = 0; i < cnt; i++) {
1386  nalsize = AV_RB16(p) + 2;
1387  if(nalsize > size - (p-buf))
1388  return -1;
1389  if (decode_nal_units(h, p, nalsize, 1) < 0) {
1390  av_log(avctx, AV_LOG_ERROR,
1391  "Decoding pps %d from avcC failed\n", i);
1392  return -1;
1393  }
1394  p += nalsize;
1395  }
1396  // Now store right nal length size, that will be used to parse all other nals
1397  h->nal_length_size = (buf[4] & 0x03) + 1;
1398  } else {
1399  h->is_avc = 0;
1400  if (decode_nal_units(h, buf, size, 1) < 0)
1401  return -1;
1402  }
1403  return size;
1404 }
1405 
1407 {
1408  H264Context *h = avctx->priv_data;
1409  int i;
1410 
1411  h->avctx = avctx;
1412  common_init(h);
1413 
1415  h->picture_range_start = 0;
1417  h->slice_context_count = 1;
1418  h->workaround_bugs = avctx->workaround_bugs;
1419  h->flags = avctx->flags;
1420 
1421  /* set defaults */
1422  // s->decode_mb = ff_h263_decode_mb;
1423  if (!avctx->has_b_frames)
1424  h->low_delay = 1;
1425 
1427 
1429 
1430  h->pixel_shift = 0;
1431  h->sps.bit_depth_luma = avctx->bits_per_raw_sample = 8;
1432 
1433  h->thread_context[0] = h;
1434  h->outputed_poc = h->next_outputed_poc = INT_MIN;
1435  for (i = 0; i < MAX_DELAYED_PIC_COUNT; i++)
1436  h->last_pocs[i] = INT_MIN;
1437  h->prev_poc_msb = 1 << 16;
1438  h->prev_frame_num = -1;
1439  h->x264_build = -1;
1440  ff_h264_reset_sei(h);
1441  if (avctx->codec_id == AV_CODEC_ID_H264) {
1442  if (avctx->ticks_per_frame == 1) {
1443  if(h->avctx->time_base.den < INT_MAX/2) {
1444  h->avctx->time_base.den *= 2;
1445  } else
1446  h->avctx->time_base.num /= 2;
1447  }
1448  avctx->ticks_per_frame = 2;
1449  }
1450 
1451  if (avctx->extradata_size > 0 && avctx->extradata &&
1452  ff_h264_decode_extradata(h, avctx->extradata, avctx->extradata_size) < 0) {
1454  return -1;
1455  }
1456 
1460  h->low_delay = 0;
1461  }
1462 
1464 
1465  return 0;
1466 }
1467 
1468 #define IN_RANGE(a, b, size) (((a) >= (b)) && ((a) < ((b) + (size))))
1469 #undef REBASE_PICTURE
1470 #define REBASE_PICTURE(pic, new_ctx, old_ctx) \
1471  ((pic && pic >= old_ctx->DPB && \
1472  pic < old_ctx->DPB + old_ctx->picture_count) ? \
1473  &new_ctx->DPB[pic - old_ctx->DPB] : NULL)
1474 
1475 static void copy_picture_range(Picture **to, Picture **from, int count,
1476  H264Context *new_base,
1477  H264Context *old_base)
1478 {
1479  int i;
1480 
1481  for (i = 0; i < count; i++) {
1482  assert((IN_RANGE(from[i], old_base, sizeof(*old_base)) ||
1483  IN_RANGE(from[i], old_base->DPB,
1484  sizeof(Picture) * old_base->picture_count) ||
1485  !from[i]));
1486  to[i] = REBASE_PICTURE(from[i], new_base, old_base);
1487  }
1488 }
1489 
1490 static void copy_parameter_set(void **to, void **from, int count, int size)
1491 {
1492  int i;
1493 
1494  for (i = 0; i < count; i++) {
1495  if (to[i] && !from[i])
1496  av_freep(&to[i]);
1497  else if (from[i] && !to[i])
1498  to[i] = av_malloc(size);
1499 
1500  if (from[i])
1501  memcpy(to[i], from[i], size);
1502  }
1503 }
1504 
1506 {
1507  H264Context *h = avctx->priv_data;
1508 
1509  if (!avctx->internal->is_copy)
1510  return 0;
1511  memset(h->sps_buffers, 0, sizeof(h->sps_buffers));
1512  memset(h->pps_buffers, 0, sizeof(h->pps_buffers));
1513 
1514  h->context_initialized = 0;
1515 
1516  return 0;
1517 }
1518 
1519 #define copy_fields(to, from, start_field, end_field) \
1520  memcpy(&to->start_field, &from->start_field, \
1521  (char *)&to->end_field - (char *)&to->start_field)
1522 
1523 static int h264_slice_header_init(H264Context *, int);
1524 
1526 
1528  const AVCodecContext *src)
1529 {
1530  H264Context *h = dst->priv_data, *h1 = src->priv_data;
1531  int inited = h->context_initialized, err = 0;
1532  int context_reinitialized = 0;
1533  int i;
1534 
1535  if (dst == src)
1536  return 0;
1537 
1538  if (inited &&
1539  (h->width != h1->width ||
1540  h->height != h1->height ||
1541  h->mb_width != h1->mb_width ||
1542  h->mb_height != h1->mb_height ||
1543  h->sps.bit_depth_luma != h1->sps.bit_depth_luma ||
1544  h->sps.chroma_format_idc != h1->sps.chroma_format_idc ||
1545  h->sps.colorspace != h1->sps.colorspace)) {
1546 
1548 
1549  h->width = h1->width;
1550  h->height = h1->height;
1551  h->mb_height = h1->mb_height;
1552  h->mb_width = h1->mb_width;
1553  h->mb_num = h1->mb_num;
1554  h->mb_stride = h1->mb_stride;
1555  h->b_stride = h1->b_stride;
1556  // SPS/PPS
1557  copy_parameter_set((void **)h->sps_buffers, (void **)h1->sps_buffers,
1558  MAX_SPS_COUNT, sizeof(SPS));
1559  h->sps = h1->sps;
1560  copy_parameter_set((void **)h->pps_buffers, (void **)h1->pps_buffers,
1561  MAX_PPS_COUNT, sizeof(PPS));
1562  h->pps = h1->pps;
1563 
1564  if ((err = h264_slice_header_init(h, 1)) < 0) {
1565  av_log(h->avctx, AV_LOG_ERROR, "h264_slice_header_init() failed");
1566  return err;
1567  }
1568  context_reinitialized = 1;
1569 
1570 #if 0
1572  //Note we set context_reinitialized which will cause h264_set_parameter_from_sps to be reexecuted
1573  h->cur_chroma_format_idc = h1->cur_chroma_format_idc;
1574 #endif
1575  }
1576  /* update linesize on resize for h264. The h264 decoder doesn't
1577  * necessarily call ff_MPV_frame_start in the new thread */
1578  h->linesize = h1->linesize;
1579  h->uvlinesize = h1->uvlinesize;
1580 
1581  /* copy block_offset since frame_start may not be called */
1582  memcpy(h->block_offset, h1->block_offset, sizeof(h->block_offset));
1583 
1584  if (!inited) {
1585  for (i = 0; i < MAX_SPS_COUNT; i++)
1586  av_freep(h->sps_buffers + i);
1587 
1588  for (i = 0; i < MAX_PPS_COUNT; i++)
1589  av_freep(h->pps_buffers + i);
1590 
1591  memcpy(h, h1, offsetof(H264Context, intra_pcm_ptr));
1592  memcpy(&h->cabac, &h1->cabac,
1593  sizeof(H264Context) - offsetof(H264Context, cabac));
1594  av_assert0((void*)&h->cabac == &h->mb_padding + 1);
1595 
1596  memset(h->sps_buffers, 0, sizeof(h->sps_buffers));
1597  memset(h->pps_buffers, 0, sizeof(h->pps_buffers));
1598 
1599  memset(&h->er, 0, sizeof(h->er));
1600  memset(&h->me, 0, sizeof(h->me));
1601  h->avctx = dst;
1602  h->DPB = NULL;
1603 
1604  if (h1->context_initialized) {
1605  h->context_initialized = 0;
1606 
1609 
1610  h->cur_pic.f.extended_data = h->cur_pic.f.data;
1611 
1612  if (ff_h264_alloc_tables(h) < 0) {
1613  av_log(dst, AV_LOG_ERROR, "Could not allocate memory for h264\n");
1614  return AVERROR(ENOMEM);
1615  }
1616  context_init(h);
1617  }
1618 
1619  for (i = 0; i < 2; i++) {
1620  h->rbsp_buffer[i] = NULL;
1621  h->rbsp_buffer_size[i] = 0;
1622  }
1623  h->bipred_scratchpad = NULL;
1624  h->edge_emu_buffer = NULL;
1625 
1626  h->thread_context[0] = h;
1627  h->context_initialized = h1->context_initialized;
1628  }
1629 
1630  h->avctx->coded_height = h1->avctx->coded_height;
1631  h->avctx->coded_width = h1->avctx->coded_width;
1632  h->avctx->width = h1->avctx->width;
1633  h->avctx->height = h1->avctx->height;
1634  h->coded_picture_number = h1->coded_picture_number;
1635  h->first_field = h1->first_field;
1636  h->picture_structure = h1->picture_structure;
1637  h->qscale = h1->qscale;
1638  h->droppable = h1->droppable;
1639  h->data_partitioning = h1->data_partitioning;
1640  h->low_delay = h1->low_delay;
1641 
1642  memcpy(h->DPB, h1->DPB, h1->picture_count * sizeof(*h1->DPB));
1643 
1644  // reset s->picture[].f.extended_data to s->picture[].f.data
1645  for (i = 0; i < h->picture_count; i++) {
1646  h->DPB[i].f.extended_data = h->DPB[i].f.data;
1647  h->DPB[i].period_since_free ++;
1648  }
1649 
1650  h->cur_pic_ptr = REBASE_PICTURE(h1->cur_pic_ptr, h, h1);
1651  h->cur_pic = h1->cur_pic;
1652  h->cur_pic.f.extended_data = h->cur_pic.f.data;
1653 
1654  h->workaround_bugs = h1->workaround_bugs;
1655  h->low_delay = h1->low_delay;
1656  h->droppable = h1->droppable;
1657 
1658  // extradata/NAL handling
1659  h->is_avc = h1->is_avc;
1660 
1661  // SPS/PPS
1662  copy_parameter_set((void **)h->sps_buffers, (void **)h1->sps_buffers,
1663  MAX_SPS_COUNT, sizeof(SPS));
1664  h->sps = h1->sps;
1665  copy_parameter_set((void **)h->pps_buffers, (void **)h1->pps_buffers,
1666  MAX_PPS_COUNT, sizeof(PPS));
1667  h->pps = h1->pps;
1668 
1669  // Dequantization matrices
1670  // FIXME these are big - can they be only copied when PPS changes?
1671  copy_fields(h, h1, dequant4_buffer, dequant4_coeff);
1672 
1673  for (i = 0; i < 6; i++)
1674  h->dequant4_coeff[i] = h->dequant4_buffer[0] +
1675  (h1->dequant4_coeff[i] - h1->dequant4_buffer[0]);
1676 
1677  for (i = 0; i < 6; i++)
1678  h->dequant8_coeff[i] = h->dequant8_buffer[0] +
1679  (h1->dequant8_coeff[i] - h1->dequant8_buffer[0]);
1680 
1681  h->dequant_coeff_pps = h1->dequant_coeff_pps;
1682 
1683  // POC timing
1684  copy_fields(h, h1, poc_lsb, redundant_pic_count);
1685 
1686  // reference lists
1687  copy_fields(h, h1, short_ref, cabac_init_idc);
1688 
1689  copy_picture_range(h->short_ref, h1->short_ref, 32, h, h1);
1690  copy_picture_range(h->long_ref, h1->long_ref, 32, h, h1);
1691  copy_picture_range(h->delayed_pic, h1->delayed_pic,
1692  MAX_DELAYED_PIC_COUNT + 2, h, h1);
1693 
1694  h->last_slice_type = h1->last_slice_type;
1695  h->sync = h1->sync;
1696  memcpy(h->last_ref_count, h1->last_ref_count, sizeof(h->last_ref_count));
1697 
1698  if (context_reinitialized)
1700 
1701  if (!h->cur_pic_ptr)
1702  return 0;
1703 
1704  if (!h->droppable) {
1706  h->prev_poc_msb = h->poc_msb;
1707  h->prev_poc_lsb = h->poc_lsb;
1708  }
1710  h->prev_frame_num = h->frame_num;
1712 
1713  return err;
1714 }
1715 
1717 {
1718  Picture *pic;
1719  int i, ret;
1720  const int pixel_shift = h->pixel_shift;
1721  int c[4] = {
1722  1<<(h->sps.bit_depth_luma-1),
1723  1<<(h->sps.bit_depth_chroma-1),
1724  1<<(h->sps.bit_depth_chroma-1),
1725  -1
1726  };
1727 
1728  if (!ff_thread_can_start_frame(h->avctx)) {
1729  av_log(h->avctx, AV_LOG_ERROR, "Attempt to start a frame outside SETUP state\n");
1730  return -1;
1731  }
1732 
1734  h->cur_pic_ptr = NULL;
1735 
1736  i = find_unused_picture(h);
1737  if (i < 0) {
1738  av_log(h->avctx, AV_LOG_ERROR, "no frame buffer available\n");
1739  return i;
1740  }
1741  pic = &h->DPB[i];
1742 
1743  pic->f.reference = h->droppable ? 0 : h->picture_structure;
1746 
1747  /*
1748  * Zero key_frame here; IDR markings per slice in frame or fields are ORed
1749  * in later.
1750  * See decode_nal_units().
1751  */
1752  pic->f.key_frame = 0;
1753  pic->sync = 0;
1754  pic->mmco_reset = 0;
1755 
1756  if ((ret = alloc_picture(h, pic)) < 0)
1757  return ret;
1758  if(!h->sync && !h->avctx->hwaccel &&
1760  avpriv_color_frame(&pic->f, c);
1761 
1762  h->cur_pic_ptr = pic;
1763  h->cur_pic = *h->cur_pic_ptr;
1764  h->cur_pic.f.extended_data = h->cur_pic.f.data;
1765 
1766  if (CONFIG_ERROR_RESILIENCE) {
1767  ff_er_frame_start(&h->er);
1768  h->er.last_pic =
1769  h->er.next_pic = NULL;
1770  }
1771 
1772  assert(h->linesize && h->uvlinesize);
1773 
1774  for (i = 0; i < 16; i++) {
1775  h->block_offset[i] = (4 * ((scan8[i] - scan8[0]) & 7) << pixel_shift) + 4 * h->linesize * ((scan8[i] - scan8[0]) >> 3);
1776  h->block_offset[48 + i] = (4 * ((scan8[i] - scan8[0]) & 7) << pixel_shift) + 8 * h->linesize * ((scan8[i] - scan8[0]) >> 3);
1777  }
1778  for (i = 0; i < 16; i++) {
1779  h->block_offset[16 + i] =
1780  h->block_offset[32 + i] = (4 * ((scan8[i] - scan8[0]) & 7) << pixel_shift) + 4 * h->uvlinesize * ((scan8[i] - scan8[0]) >> 3);
1781  h->block_offset[48 + 16 + i] =
1782  h->block_offset[48 + 32 + i] = (4 * ((scan8[i] - scan8[0]) & 7) << pixel_shift) + 8 * h->uvlinesize * ((scan8[i] - scan8[0]) >> 3);
1783  }
1784 
1785  /* Some macroblocks can be accessed before they're available in case
1786  * of lost slices, MBAFF or threading. */
1787  memset(h->slice_table, -1,
1788  (h->mb_height * h->mb_stride - 1) * sizeof(*h->slice_table));
1789 
1790  // s->decode = (h->flags & CODEC_FLAG_PSNR) || !s->encoding ||
1791  // h->cur_pic.f.reference /* || h->contains_intra */ || 1;
1792 
1793  /* We mark the current picture as non-reference after allocating it, so
1794  * that if we break out due to an error it can be released automatically
1795  * in the next ff_MPV_frame_start().
1796  * SVQ3 as well as most other codecs have only last/next/current and thus
1797  * get released even with set reference, besides SVQ3 and others do not
1798  * mark frames as reference later "naturally". */
1799  if (h->avctx->codec_id != AV_CODEC_ID_SVQ3)
1800  h->cur_pic_ptr->f.reference = 0;
1801 
1802  h->cur_pic_ptr->field_poc[0] = h->cur_pic_ptr->field_poc[1] = INT_MAX;
1803 
1804  h->next_output_pic = NULL;
1805 
1806  assert(h->cur_pic_ptr->long_ref == 0);
1807 
1808  return 0;
1809 }
1810 
1811 /**
1812  * Run setup operations that must be run after slice header decoding.
1813  * This includes finding the next displayed frame.
1814  *
1815  * @param h h264 master context
1816  * @param setup_finished enough NALs have been read that we can call
1817  * ff_thread_finish_setup()
1818  */
1819 static void decode_postinit(H264Context *h, int setup_finished)
1820 {
1821  Picture *out = h->cur_pic_ptr;
1822  Picture *cur = h->cur_pic_ptr;
1823  int i, pics, out_of_order, out_idx;
1824 
1826  h->cur_pic_ptr->f.pict_type = h->pict_type;
1827 
1828  if (h->next_output_pic)
1829  return;
1830 
1831  if (cur->field_poc[0] == INT_MAX || cur->field_poc[1] == INT_MAX) {
1832  /* FIXME: if we have two PAFF fields in one packet, we can't start
1833  * the next thread here. If we have one field per packet, we can.
1834  * The check in decode_nal_units() is not good enough to find this
1835  * yet, so we assume the worst for now. */
1836  // if (setup_finished)
1837  // ff_thread_finish_setup(h->avctx);
1838  return;
1839  }
1840 
1841  cur->f.interlaced_frame = 0;
1842  cur->f.repeat_pict = 0;
1843 
1844  /* Signal interlacing information externally. */
1845  /* Prioritize picture timing SEI information over used
1846  * decoding process if it exists. */
1847 
1848  if (h->sps.pic_struct_present_flag) {
1849  switch (h->sei_pic_struct) {
1850  case SEI_PIC_STRUCT_FRAME:
1851  break;
1854  cur->f.interlaced_frame = 1;
1855  break;
1859  cur->f.interlaced_frame = 1;
1860  else
1861  // try to flag soft telecine progressive
1863  break;
1866  /* Signal the possibility of telecined film externally
1867  * (pic_struct 5,6). From these hints, let the applications
1868  * decide if they apply deinterlacing. */
1869  cur->f.repeat_pict = 1;
1870  break;
1872  cur->f.repeat_pict = 2;
1873  break;
1875  cur->f.repeat_pict = 4;
1876  break;
1877  }
1878 
1879  if ((h->sei_ct_type & 3) &&
1881  cur->f.interlaced_frame = (h->sei_ct_type & (1 << 1)) != 0;
1882  } else {
1883  /* Derive interlacing flag from used decoding process. */
1885  }
1887 
1888  if (cur->field_poc[0] != cur->field_poc[1]) {
1889  /* Derive top_field_first from field pocs. */
1890  cur->f.top_field_first = cur->field_poc[0] < cur->field_poc[1];
1891  } else {
1892  if (cur->f.interlaced_frame || h->sps.pic_struct_present_flag) {
1893  /* Use picture timing SEI information. Even if it is a
1894  * information of a past frame, better than nothing. */
1897  cur->f.top_field_first = 1;
1898  else
1899  cur->f.top_field_first = 0;
1900  } else {
1901  /* Most likely progressive */
1902  cur->f.top_field_first = 0;
1903  }
1904  }
1905 
1906  cur->mmco_reset = h->mmco_reset;
1907  h->mmco_reset = 0;
1908  // FIXME do something with unavailable reference frames
1909 
1910  /* Sort B-frames into display order */
1911 
1915  h->low_delay = 0;
1916  }
1917 
1921  h->low_delay = 0;
1922  }
1923 
1924  for (i = 0; 1; i++) {
1925  if(i == MAX_DELAYED_PIC_COUNT || cur->poc < h->last_pocs[i]){
1926  if(i)
1927  h->last_pocs[i-1] = cur->poc;
1928  break;
1929  } else if(i) {
1930  h->last_pocs[i-1]= h->last_pocs[i];
1931  }
1932  }
1933  out_of_order = MAX_DELAYED_PIC_COUNT - i;
1934  if( cur->f.pict_type == AV_PICTURE_TYPE_B
1936  out_of_order = FFMAX(out_of_order, 1);
1937  if (out_of_order == MAX_DELAYED_PIC_COUNT) {
1938  av_log(h->avctx, AV_LOG_VERBOSE, "Invalid POC %d<%d\n", cur->poc, h->last_pocs[0]);
1939  for (i = 1; i < MAX_DELAYED_PIC_COUNT; i++)
1940  h->last_pocs[i] = INT_MIN;
1941  h->last_pocs[0] = cur->poc;
1942  cur->mmco_reset = 1;
1943  } else if(h->avctx->has_b_frames < out_of_order && !h->sps.bitstream_restriction_flag){
1944  av_log(h->avctx, AV_LOG_VERBOSE, "Increasing reorder buffer to %d\n", out_of_order);
1945  h->avctx->has_b_frames = out_of_order;
1946  h->low_delay = 0;
1947  }
1948 
1949  pics = 0;
1950  while (h->delayed_pic[pics])
1951  pics++;
1952 
1954 
1955  h->delayed_pic[pics++] = cur;
1956  if (cur->f.reference == 0)
1957  cur->f.reference = DELAYED_PIC_REF;
1958 
1959  out = h->delayed_pic[0];
1960  out_idx = 0;
1961  for (i = 1; h->delayed_pic[i] &&
1962  !h->delayed_pic[i]->f.key_frame &&
1963  !h->delayed_pic[i]->mmco_reset;
1964  i++)
1965  if (h->delayed_pic[i]->poc < out->poc) {
1966  out = h->delayed_pic[i];
1967  out_idx = i;
1968  }
1969  if (h->avctx->has_b_frames == 0 &&
1970  (h->delayed_pic[0]->f.key_frame || h->delayed_pic[0]->mmco_reset))
1971  h->next_outputed_poc = INT_MIN;
1972  out_of_order = out->poc < h->next_outputed_poc;
1973 
1974  if (out_of_order || pics > h->avctx->has_b_frames) {
1975  out->f.reference &= ~DELAYED_PIC_REF;
1976  // for frame threading, the owner must be the second field's thread or
1977  // else the first thread can release the picture and reuse it unsafely
1978  out->owner2 = h;
1979  for (i = out_idx; h->delayed_pic[i]; i++)
1980  h->delayed_pic[i] = h->delayed_pic[i + 1];
1981  }
1982  if (!out_of_order && pics > h->avctx->has_b_frames) {
1983  h->next_output_pic = out;
1984  if (out_idx == 0 && h->delayed_pic[0] && (h->delayed_pic[0]->f.key_frame || h->delayed_pic[0]->mmco_reset)) {
1985  h->next_outputed_poc = INT_MIN;
1986  } else
1987  h->next_outputed_poc = out->poc;
1988  } else {
1989  av_log(h->avctx, AV_LOG_DEBUG, "no picture %s\n", out_of_order ? "ooo" : "");
1990  }
1991 
1992  if (h->next_output_pic && h->next_output_pic->sync) {
1993  h->sync |= 2;
1994  }
1995 
1996  if (setup_finished)
1998 }
1999 
2001  uint8_t *src_cb, uint8_t *src_cr,
2002  int linesize, int uvlinesize,
2003  int simple)
2004 {
2005  uint8_t *top_border;
2006  int top_idx = 1;
2007  const int pixel_shift = h->pixel_shift;
2008  int chroma444 = CHROMA444;
2009  int chroma422 = CHROMA422;
2010 
2011  src_y -= linesize;
2012  src_cb -= uvlinesize;
2013  src_cr -= uvlinesize;
2014 
2015  if (!simple && FRAME_MBAFF) {
2016  if (h->mb_y & 1) {
2017  if (!MB_MBAFF) {
2018  top_border = h->top_borders[0][h->mb_x];
2019  AV_COPY128(top_border, src_y + 15 * linesize);
2020  if (pixel_shift)
2021  AV_COPY128(top_border + 16, src_y + 15 * linesize + 16);
2022  if (simple || !CONFIG_GRAY || !(h->flags & CODEC_FLAG_GRAY)) {
2023  if (chroma444) {
2024  if (pixel_shift) {
2025  AV_COPY128(top_border + 32, src_cb + 15 * uvlinesize);
2026  AV_COPY128(top_border + 48, src_cb + 15 * uvlinesize + 16);
2027  AV_COPY128(top_border + 64, src_cr + 15 * uvlinesize);
2028  AV_COPY128(top_border + 80, src_cr + 15 * uvlinesize + 16);
2029  } else {
2030  AV_COPY128(top_border + 16, src_cb + 15 * uvlinesize);
2031  AV_COPY128(top_border + 32, src_cr + 15 * uvlinesize);
2032  }
2033  } else if (chroma422) {
2034  if (pixel_shift) {
2035  AV_COPY128(top_border + 32, src_cb + 15 * uvlinesize);
2036  AV_COPY128(top_border + 48, src_cr + 15 * uvlinesize);
2037  } else {
2038  AV_COPY64(top_border + 16, src_cb + 15 * uvlinesize);
2039  AV_COPY64(top_border + 24, src_cr + 15 * uvlinesize);
2040  }
2041  } else {
2042  if (pixel_shift) {
2043  AV_COPY128(top_border + 32, src_cb + 7 * uvlinesize);
2044  AV_COPY128(top_border + 48, src_cr + 7 * uvlinesize);
2045  } else {
2046  AV_COPY64(top_border + 16, src_cb + 7 * uvlinesize);
2047  AV_COPY64(top_border + 24, src_cr + 7 * uvlinesize);
2048  }
2049  }
2050  }
2051  }
2052  } else if (MB_MBAFF) {
2053  top_idx = 0;
2054  } else
2055  return;
2056  }
2057 
2058  top_border = h->top_borders[top_idx][h->mb_x];
2059  /* There are two lines saved, the line above the top macroblock
2060  * of a pair, and the line above the bottom macroblock. */
2061  AV_COPY128(top_border, src_y + 16 * linesize);
2062  if (pixel_shift)
2063  AV_COPY128(top_border + 16, src_y + 16 * linesize + 16);
2064 
2065  if (simple || !CONFIG_GRAY || !(h->flags & CODEC_FLAG_GRAY)) {
2066  if (chroma444) {
2067  if (pixel_shift) {
2068  AV_COPY128(top_border + 32, src_cb + 16 * linesize);
2069  AV_COPY128(top_border + 48, src_cb + 16 * linesize + 16);
2070  AV_COPY128(top_border + 64, src_cr + 16 * linesize);
2071  AV_COPY128(top_border + 80, src_cr + 16 * linesize + 16);
2072  } else {
2073  AV_COPY128(top_border + 16, src_cb + 16 * linesize);
2074  AV_COPY128(top_border + 32, src_cr + 16 * linesize);
2075  }
2076  } else if (chroma422) {
2077  if (pixel_shift) {
2078  AV_COPY128(top_border + 32, src_cb + 16 * uvlinesize);
2079  AV_COPY128(top_border + 48, src_cr + 16 * uvlinesize);
2080  } else {
2081  AV_COPY64(top_border + 16, src_cb + 16 * uvlinesize);
2082  AV_COPY64(top_border + 24, src_cr + 16 * uvlinesize);
2083  }
2084  } else {
2085  if (pixel_shift) {
2086  AV_COPY128(top_border + 32, src_cb + 8 * uvlinesize);
2087  AV_COPY128(top_border + 48, src_cr + 8 * uvlinesize);
2088  } else {
2089  AV_COPY64(top_border + 16, src_cb + 8 * uvlinesize);
2090  AV_COPY64(top_border + 24, src_cr + 8 * uvlinesize);
2091  }
2092  }
2093  }
2094 }
2095 
2097  uint8_t *src_cb, uint8_t *src_cr,
2098  int linesize, int uvlinesize,
2099  int xchg, int chroma444,
2100  int simple, int pixel_shift)
2101 {
2102  int deblock_topleft;
2103  int deblock_top;
2104  int top_idx = 1;
2105  uint8_t *top_border_m1;
2106  uint8_t *top_border;
2107 
2108  if (!simple && FRAME_MBAFF) {
2109  if (h->mb_y & 1) {
2110  if (!MB_MBAFF)
2111  return;
2112  } else {
2113  top_idx = MB_MBAFF ? 0 : 1;
2114  }
2115  }
2116 
2117  if (h->deblocking_filter == 2) {
2118  deblock_topleft = h->slice_table[h->mb_xy - 1 - h->mb_stride] == h->slice_num;
2119  deblock_top = h->top_type;
2120  } else {
2121  deblock_topleft = (h->mb_x > 0);
2122  deblock_top = (h->mb_y > !!MB_FIELD);
2123  }
2124 
2125  src_y -= linesize + 1 + pixel_shift;
2126  src_cb -= uvlinesize + 1 + pixel_shift;
2127  src_cr -= uvlinesize + 1 + pixel_shift;
2128 
2129  top_border_m1 = h->top_borders[top_idx][h->mb_x - 1];
2130  top_border = h->top_borders[top_idx][h->mb_x];
2131 
2132 #define XCHG(a, b, xchg) \
2133  if (pixel_shift) { \
2134  if (xchg) { \
2135  AV_SWAP64(b + 0, a + 0); \
2136  AV_SWAP64(b + 8, a + 8); \
2137  } else { \
2138  AV_COPY128(b, a); \
2139  } \
2140  } else if (xchg) \
2141  AV_SWAP64(b, a); \
2142  else \
2143  AV_COPY64(b, a);
2144 
2145  if (deblock_top) {
2146  if (deblock_topleft) {
2147  XCHG(top_border_m1 + (8 << pixel_shift),
2148  src_y - (7 << pixel_shift), 1);
2149  }
2150  XCHG(top_border + (0 << pixel_shift), src_y + (1 << pixel_shift), xchg);
2151  XCHG(top_border + (8 << pixel_shift), src_y + (9 << pixel_shift), 1);
2152  if (h->mb_x + 1 < h->mb_width) {
2153  XCHG(h->top_borders[top_idx][h->mb_x + 1],
2154  src_y + (17 << pixel_shift), 1);
2155  }
2156  }
2157  if (simple || !CONFIG_GRAY || !(h->flags & CODEC_FLAG_GRAY)) {
2158  if (chroma444) {
2159  if (deblock_topleft) {
2160  XCHG(top_border_m1 + (24 << pixel_shift), src_cb - (7 << pixel_shift), 1);
2161  XCHG(top_border_m1 + (40 << pixel_shift), src_cr - (7 << pixel_shift), 1);
2162  }
2163  XCHG(top_border + (16 << pixel_shift), src_cb + (1 << pixel_shift), xchg);
2164  XCHG(top_border + (24 << pixel_shift), src_cb + (9 << pixel_shift), 1);
2165  XCHG(top_border + (32 << pixel_shift), src_cr + (1 << pixel_shift), xchg);
2166  XCHG(top_border + (40 << pixel_shift), src_cr + (9 << pixel_shift), 1);
2167  if (h->mb_x + 1 < h->mb_width) {
2168  XCHG(h->top_borders[top_idx][h->mb_x + 1] + (16 << pixel_shift), src_cb + (17 << pixel_shift), 1);
2169  XCHG(h->top_borders[top_idx][h->mb_x + 1] + (32 << pixel_shift), src_cr + (17 << pixel_shift), 1);
2170  }
2171  } else {
2172  if (deblock_top) {
2173  if (deblock_topleft) {
2174  XCHG(top_border_m1 + (16 << pixel_shift), src_cb - (7 << pixel_shift), 1);
2175  XCHG(top_border_m1 + (24 << pixel_shift), src_cr - (7 << pixel_shift), 1);
2176  }
2177  XCHG(top_border + (16 << pixel_shift), src_cb + 1 + pixel_shift, 1);
2178  XCHG(top_border + (24 << pixel_shift), src_cr + 1 + pixel_shift, 1);
2179  }
2180  }
2181  }
2182 }
2183 
2184 static av_always_inline int dctcoef_get(int16_t *mb, int high_bit_depth,
2185  int index)
2186 {
2187  if (high_bit_depth) {
2188  return AV_RN32A(((int32_t *)mb) + index);
2189  } else
2190  return AV_RN16A(mb + index);
2191 }
2192 
2193 static av_always_inline void dctcoef_set(int16_t *mb, int high_bit_depth,
2194  int index, int value)
2195 {
2196  if (high_bit_depth) {
2197  AV_WN32A(((int32_t *)mb) + index, value);
2198  } else
2199  AV_WN16A(mb + index, value);
2200 }
2201 
2203  int mb_type, int is_h264,
2204  int simple,
2205  int transform_bypass,
2206  int pixel_shift,
2207  int *block_offset,
2208  int linesize,
2209  uint8_t *dest_y, int p)
2210 {
2211  void (*idct_add)(uint8_t *dst, int16_t *block, int stride);
2212  void (*idct_dc_add)(uint8_t *dst, int16_t *block, int stride);
2213  int i;
2214  int qscale = p == 0 ? h->qscale : h->chroma_qp[p - 1];
2215  block_offset += 16 * p;
2216  if (IS_INTRA4x4(mb_type)) {
2217  if (IS_8x8DCT(mb_type)) {
2218  if (transform_bypass) {
2219  idct_dc_add =
2221  } else {
2222  idct_dc_add = h->h264dsp.h264_idct8_dc_add;
2224  }
2225  for (i = 0; i < 16; i += 4) {
2226  uint8_t *const ptr = dest_y + block_offset[i];
2227  const int dir = h->intra4x4_pred_mode_cache[scan8[i]];
2228  if (transform_bypass && h->sps.profile_idc == 244 && dir <= 1) {
2229  h->hpc.pred8x8l_add[dir](ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize);
2230  } else {
2231  const int nnz = h->non_zero_count_cache[scan8[i + p * 16]];
2232  h->hpc.pred8x8l[dir](ptr, (h->topleft_samples_available << i) & 0x8000,
2233  (h->topright_samples_available << i) & 0x4000, linesize);
2234  if (nnz) {
2235  if (nnz == 1 && dctcoef_get(h->mb, pixel_shift, i * 16 + p * 256))
2236  idct_dc_add(ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize);
2237  else
2238  idct_add(ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize);
2239  }
2240  }
2241  }
2242  } else {
2243  if (transform_bypass) {
2244  idct_dc_add =
2246  } else {
2247  idct_dc_add = h->h264dsp.h264_idct_dc_add;
2249  }
2250  for (i = 0; i < 16; i++) {
2251  uint8_t *const ptr = dest_y + block_offset[i];
2252  const int dir = h->intra4x4_pred_mode_cache[scan8[i]];
2253 
2254  if (transform_bypass && h->sps.profile_idc == 244 && dir <= 1) {
2255  h->hpc.pred4x4_add[dir](ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize);
2256  } else {
2257  uint8_t *topright;
2258  int nnz, tr;
2259  uint64_t tr_high;
2260  if (dir == DIAG_DOWN_LEFT_PRED || dir == VERT_LEFT_PRED) {
2261  const int topright_avail = (h->topright_samples_available << i) & 0x8000;
2262  av_assert2(h->mb_y || linesize <= block_offset[i]);
2263  if (!topright_avail) {
2264  if (pixel_shift) {
2265  tr_high = ((uint16_t *)ptr)[3 - linesize / 2] * 0x0001000100010001ULL;
2266  topright = (uint8_t *)&tr_high;
2267  } else {
2268  tr = ptr[3 - linesize] * 0x01010101u;
2269  topright = (uint8_t *)&tr;
2270  }
2271  } else
2272  topright = ptr + (4 << pixel_shift) - linesize;
2273  } else
2274  topright = NULL;
2275 
2276  h->hpc.pred4x4[dir](ptr, topright, linesize);
2277  nnz = h->non_zero_count_cache[scan8[i + p * 16]];
2278  if (nnz) {
2279  if (is_h264) {
2280  if (nnz == 1 && dctcoef_get(h->mb, pixel_shift, i * 16 + p * 256))
2281  idct_dc_add(ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize);
2282  else
2283  idct_add(ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize);
2284  } else if (CONFIG_SVQ3_DECODER)
2285  ff_svq3_add_idct_c(ptr, h->mb + i * 16 + p * 256, linesize, qscale, 0);
2286  }
2287  }
2288  }
2289  }
2290  } else {
2291  h->hpc.pred16x16[h->intra16x16_pred_mode](dest_y, linesize);
2292  if (is_h264) {
2294  if (!transform_bypass)
2295  h->h264dsp.h264_luma_dc_dequant_idct(h->mb + (p * 256 << pixel_shift),
2296  h->mb_luma_dc[p],
2297  h->dequant4_coeff[p][qscale][0]);
2298  else {
2299  static const uint8_t dc_mapping[16] = {
2300  0 * 16, 1 * 16, 4 * 16, 5 * 16,
2301  2 * 16, 3 * 16, 6 * 16, 7 * 16,
2302  8 * 16, 9 * 16, 12 * 16, 13 * 16,
2303  10 * 16, 11 * 16, 14 * 16, 15 * 16 };
2304  for (i = 0; i < 16; i++)
2305  dctcoef_set(h->mb + (p * 256 << pixel_shift),
2306  pixel_shift, dc_mapping[i],
2307  dctcoef_get(h->mb_luma_dc[p],
2308  pixel_shift, i));
2309  }
2310  }
2311  } else if (CONFIG_SVQ3_DECODER)
2312  ff_svq3_luma_dc_dequant_idct_c(h->mb + p * 256,
2313  h->mb_luma_dc[p], qscale);
2314  }
2315 }
2316 
2318  int is_h264, int simple,
2319  int transform_bypass,
2320  int pixel_shift,
2321  int *block_offset,
2322  int linesize,
2323  uint8_t *dest_y, int p)
2324 {
2325  void (*idct_add)(uint8_t *dst, int16_t *block, int stride);
2326  int i;
2327  block_offset += 16 * p;
2328  if (!IS_INTRA4x4(mb_type)) {
2329  if (is_h264) {
2330  if (IS_INTRA16x16(mb_type)) {
2331  if (transform_bypass) {
2332  if (h->sps.profile_idc == 244 &&
2335  h->hpc.pred16x16_add[h->intra16x16_pred_mode](dest_y, block_offset,
2336  h->mb + (p * 256 << pixel_shift),
2337  linesize);
2338  } else {
2339  for (i = 0; i < 16; i++)
2340  if (h->non_zero_count_cache[scan8[i + p * 16]] ||
2341  dctcoef_get(h->mb, pixel_shift, i * 16 + p * 256))
2342  h->h264dsp.h264_add_pixels4_clear(dest_y + block_offset[i],
2343  h->mb + (i * 16 + p * 256 << pixel_shift),
2344  linesize);
2345  }
2346  } else {
2347  h->h264dsp.h264_idct_add16intra(dest_y, block_offset,
2348  h->mb + (p * 256 << pixel_shift),
2349  linesize,
2350  h->non_zero_count_cache + p * 5 * 8);
2351  }
2352  } else if (h->cbp & 15) {
2353  if (transform_bypass) {
2354  const int di = IS_8x8DCT(mb_type) ? 4 : 1;
2357  for (i = 0; i < 16; i += di)
2358  if (h->non_zero_count_cache[scan8[i + p * 16]])
2359  idct_add(dest_y + block_offset[i],
2360  h->mb + (i * 16 + p * 256 << pixel_shift),
2361  linesize);
2362  } else {
2363  if (IS_8x8DCT(mb_type))
2364  h->h264dsp.h264_idct8_add4(dest_y, block_offset,
2365  h->mb + (p * 256 << pixel_shift),
2366  linesize,
2367  h->non_zero_count_cache + p * 5 * 8);
2368  else
2369  h->h264dsp.h264_idct_add16(dest_y, block_offset,
2370  h->mb + (p * 256 << pixel_shift),
2371  linesize,
2372  h->non_zero_count_cache + p * 5 * 8);
2373  }
2374  }
2375  } else if (CONFIG_SVQ3_DECODER) {
2376  for (i = 0; i < 16; i++)
2377  if (h->non_zero_count_cache[scan8[i + p * 16]] || h->mb[i * 16 + p * 256]) {
2378  // FIXME benchmark weird rule, & below
2379  uint8_t *const ptr = dest_y + block_offset[i];
2380  ff_svq3_add_idct_c(ptr, h->mb + i * 16 + p * 256, linesize,
2381  h->qscale, IS_INTRA(mb_type) ? 1 : 0);
2382  }
2383  }
2384  }
2385 }
2386 
2387 #define BITS 8
2388 #define SIMPLE 1
2389 #include "h264_mb_template.c"
2390 
2391 #undef BITS
2392 #define BITS 16
2393 #include "h264_mb_template.c"
2394 
2395 #undef SIMPLE
2396 #define SIMPLE 0
2397 #include "h264_mb_template.c"
2398 
2400 {
2401  const int mb_xy = h->mb_xy;
2402  const int mb_type = h->cur_pic.f.mb_type[mb_xy];
2403  int is_complex = CONFIG_SMALL || h->is_complex || IS_INTRA_PCM(mb_type) || h->qscale == 0;
2404 
2405  if (CHROMA444) {
2406  if (is_complex || h->pixel_shift)
2407  hl_decode_mb_444_complex(h);
2408  else
2409  hl_decode_mb_444_simple_8(h);
2410  } else if (is_complex) {
2411  hl_decode_mb_complex(h);
2412  } else if (h->pixel_shift) {
2413  hl_decode_mb_simple_16(h);
2414  } else
2415  hl_decode_mb_simple_8(h);
2416 }
2417 
2419 {
2420  int list, i;
2421  int luma_def, chroma_def;
2422 
2423  h->use_weight = 0;
2424  h->use_weight_chroma = 0;
2426  if (h->sps.chroma_format_idc)
2428  luma_def = 1 << h->luma_log2_weight_denom;
2429  chroma_def = 1 << h->chroma_log2_weight_denom;
2430 
2431  for (list = 0; list < 2; list++) {
2432  h->luma_weight_flag[list] = 0;
2433  h->chroma_weight_flag[list] = 0;
2434  for (i = 0; i < h->ref_count[list]; i++) {
2435  int luma_weight_flag, chroma_weight_flag;
2436 
2437  luma_weight_flag = get_bits1(&h->gb);
2438  if (luma_weight_flag) {
2439  h->luma_weight[i][list][0] = get_se_golomb(&h->gb);
2440  h->luma_weight[i][list][1] = get_se_golomb(&h->gb);
2441  if (h->luma_weight[i][list][0] != luma_def ||
2442  h->luma_weight[i][list][1] != 0) {
2443  h->use_weight = 1;
2444  h->luma_weight_flag[list] = 1;
2445  }
2446  } else {
2447  h->luma_weight[i][list][0] = luma_def;
2448  h->luma_weight[i][list][1] = 0;
2449  }
2450 
2451  if (h->sps.chroma_format_idc) {
2452  chroma_weight_flag = get_bits1(&h->gb);
2453  if (chroma_weight_flag) {
2454  int j;
2455  for (j = 0; j < 2; j++) {
2456  h->chroma_weight[i][list][j][0] = get_se_golomb(&h->gb);
2457  h->chroma_weight[i][list][j][1] = get_se_golomb(&h->gb);
2458  if (h->chroma_weight[i][list][j][0] != chroma_def ||
2459  h->chroma_weight[i][list][j][1] != 0) {
2460  h->use_weight_chroma = 1;
2461  h->chroma_weight_flag[list] = 1;
2462  }
2463  }
2464  } else {
2465  int j;
2466  for (j = 0; j < 2; j++) {
2467  h->chroma_weight[i][list][j][0] = chroma_def;
2468  h->chroma_weight[i][list][j][1] = 0;
2469  }
2470  }
2471  }
2472  }
2474  break;
2475  }
2476  h->use_weight = h->use_weight || h->use_weight_chroma;
2477  return 0;
2478 }
2479 
2480 /**
2481  * Initialize implicit_weight table.
2482  * @param field 0/1 initialize the weight for interlaced MBAFF
2483  * -1 initializes the rest
2484  */
2485 static void implicit_weight_table(H264Context *h, int field)
2486 {
2487  int ref0, ref1, i, cur_poc, ref_start, ref_count0, ref_count1;
2488 
2489  for (i = 0; i < 2; i++) {
2490  h->luma_weight_flag[i] = 0;
2491  h->chroma_weight_flag[i] = 0;
2492  }
2493 
2494  if (field < 0) {
2495  if (h->picture_structure == PICT_FRAME) {
2496  cur_poc = h->cur_pic_ptr->poc;
2497  } else {
2498  cur_poc = h->cur_pic_ptr->field_poc[h->picture_structure - 1];
2499  }
2500  if (h->ref_count[0] == 1 && h->ref_count[1] == 1 && !FRAME_MBAFF &&
2501  h->ref_list[0][0].poc + h->ref_list[1][0].poc == 2 * cur_poc) {
2502  h->use_weight = 0;
2503  h->use_weight_chroma = 0;
2504  return;
2505  }
2506  ref_start = 0;
2507  ref_count0 = h->ref_count[0];
2508  ref_count1 = h->ref_count[1];
2509  } else {
2510  cur_poc = h->cur_pic_ptr->field_poc[field];
2511  ref_start = 16;
2512  ref_count0 = 16 + 2 * h->ref_count[0];
2513  ref_count1 = 16 + 2 * h->ref_count[1];
2514  }
2515 
2516  h->use_weight = 2;
2517  h->use_weight_chroma = 2;
2518  h->luma_log2_weight_denom = 5;
2519  h->chroma_log2_weight_denom = 5;
2520 
2521  for (ref0 = ref_start; ref0 < ref_count0; ref0++) {
2522  int poc0 = h->ref_list[0][ref0].poc;
2523  for (ref1 = ref_start; ref1 < ref_count1; ref1++) {
2524  int w = 32;
2525  if (!h->ref_list[0][ref0].long_ref && !h->ref_list[1][ref1].long_ref) {
2526  int poc1 = h->ref_list[1][ref1].poc;
2527  int td = av_clip(poc1 - poc0, -128, 127);
2528  if (td) {
2529  int tb = av_clip(cur_poc - poc0, -128, 127);
2530  int tx = (16384 + (FFABS(td) >> 1)) / td;
2531  int dist_scale_factor = (tb * tx + 32) >> 8;
2532  if (dist_scale_factor >= -64 && dist_scale_factor <= 128)
2533  w = 64 - dist_scale_factor;
2534  }
2535  }
2536  if (field < 0) {
2537  h->implicit_weight[ref0][ref1][0] =
2538  h->implicit_weight[ref0][ref1][1] = w;
2539  } else {
2540  h->implicit_weight[ref0][ref1][field] = w;
2541  }
2542  }
2543  }
2544 }
2545 
2546 /**
2547  * instantaneous decoder refresh.
2548  */
2549 static void idr(H264Context *h)
2550 {
2551  int i;
2553  h->prev_frame_num = 0;
2554  h->prev_frame_num_offset = 0;
2555  h->prev_poc_msb = 1<<16;
2556  h->prev_poc_lsb = 0;
2557  for (i = 0; i < MAX_DELAYED_PIC_COUNT; i++)
2558  h->last_pocs[i] = INT_MIN;
2559 }
2560 
2561 /* forget old pics after a seek */
2562 static void flush_change(H264Context *h)
2563 {
2564  int i, j;
2565 
2566  h->outputed_poc = h->next_outputed_poc = INT_MIN;
2567  h->prev_interlaced_frame = 1;
2568  idr(h);
2569  h->prev_frame_num = -1;
2570  if (h->cur_pic_ptr) {
2571  h->cur_pic_ptr->f.reference = 0;
2572  for (j=i=0; h->delayed_pic[i]; i++)
2573  if (h->delayed_pic[i] != h->cur_pic_ptr)
2574  h->delayed_pic[j++] = h->delayed_pic[i];
2575  h->delayed_pic[j] = NULL;
2576  }
2577  h->first_field = 0;
2578  memset(h->ref_list[0], 0, sizeof(h->ref_list[0]));
2579  memset(h->ref_list[1], 0, sizeof(h->ref_list[1]));
2580  memset(h->default_ref_list[0], 0, sizeof(h->default_ref_list[0]));
2581  memset(h->default_ref_list[1], 0, sizeof(h->default_ref_list[1]));
2582  ff_h264_reset_sei(h);
2583  h->recovery_frame= -1;
2584  h->sync= 0;
2585  h->list_count = 0;
2586  h->current_slice = 0;
2587 }
2588 
2589 /* forget old pics after a seek */
2590 static void flush_dpb(AVCodecContext *avctx)
2591 {
2592  H264Context *h = avctx->priv_data;
2593  int i;
2594 
2595  for (i = 0; i <= MAX_DELAYED_PIC_COUNT; i++) {
2596  if (h->delayed_pic[i])
2597  h->delayed_pic[i]->f.reference = 0;
2598  h->delayed_pic[i] = NULL;
2599  }
2600 
2601  flush_change(h);
2602 
2603  for (i = 0; i < h->picture_count; i++) {
2604  if (h->DPB[i].f.data[0])
2605  free_frame_buffer(h, &h->DPB[i]);
2606  }
2607  h->cur_pic_ptr = NULL;
2608 
2609  h->mb_x = h->mb_y = 0;
2610 
2611  h->parse_context.state = -1;
2613  h->parse_context.overread = 0;
2615  h->parse_context.index = 0;
2616  h->parse_context.last_index = 0;
2617 }
2618 
2619 static int init_poc(H264Context *h)
2620 {
2621  const int max_frame_num = 1 << h->sps.log2_max_frame_num;
2622  int field_poc[2];
2623  Picture *cur = h->cur_pic_ptr;
2624 
2626  if (h->frame_num < h->prev_frame_num)
2627  h->frame_num_offset += max_frame_num;
2628 
2629  if (h->sps.poc_type == 0) {
2630  const int max_poc_lsb = 1 << h->sps.log2_max_poc_lsb;
2631 
2632  if (h->poc_lsb < h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb >= max_poc_lsb / 2)
2633  h->poc_msb = h->prev_poc_msb + max_poc_lsb;
2634  else if (h->poc_lsb > h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb < -max_poc_lsb / 2)
2635  h->poc_msb = h->prev_poc_msb - max_poc_lsb;
2636  else
2637  h->poc_msb = h->prev_poc_msb;
2638  field_poc[0] =
2639  field_poc[1] = h->poc_msb + h->poc_lsb;
2640  if (h->picture_structure == PICT_FRAME)
2641  field_poc[1] += h->delta_poc_bottom;
2642  } else if (h->sps.poc_type == 1) {
2643  int abs_frame_num, expected_delta_per_poc_cycle, expectedpoc;
2644  int i;
2645 
2646  if (h->sps.poc_cycle_length != 0)
2647  abs_frame_num = h->frame_num_offset + h->frame_num;
2648  else
2649  abs_frame_num = 0;
2650 
2651  if (h->nal_ref_idc == 0 && abs_frame_num > 0)
2652  abs_frame_num--;
2653 
2654  expected_delta_per_poc_cycle = 0;
2655  for (i = 0; i < h->sps.poc_cycle_length; i++)
2656  // FIXME integrate during sps parse
2657  expected_delta_per_poc_cycle += h->sps.offset_for_ref_frame[i];
2658 
2659  if (abs_frame_num > 0) {
2660  int poc_cycle_cnt = (abs_frame_num - 1) / h->sps.poc_cycle_length;
2661  int frame_num_in_poc_cycle = (abs_frame_num - 1) % h->sps.poc_cycle_length;
2662 
2663  expectedpoc = poc_cycle_cnt * expected_delta_per_poc_cycle;
2664  for (i = 0; i <= frame_num_in_poc_cycle; i++)
2665  expectedpoc = expectedpoc + h->sps.offset_for_ref_frame[i];
2666  } else
2667  expectedpoc = 0;
2668 
2669  if (h->nal_ref_idc == 0)
2670  expectedpoc = expectedpoc + h->sps.offset_for_non_ref_pic;
2671 
2672  field_poc[0] = expectedpoc + h->delta_poc[0];
2673  field_poc[1] = field_poc[0] + h->sps.offset_for_top_to_bottom_field;
2674 
2675  if (h->picture_structure == PICT_FRAME)
2676  field_poc[1] += h->delta_poc[1];
2677  } else {
2678  int poc = 2 * (h->frame_num_offset + h->frame_num);
2679 
2680  if (!h->nal_ref_idc)
2681  poc--;
2682 
2683  field_poc[0] = poc;
2684  field_poc[1] = poc;
2685  }
2686 
2688  h->cur_pic_ptr->field_poc[0] = field_poc[0];
2690  h->cur_pic_ptr->field_poc[1] = field_poc[1];
2691  cur->poc = FFMIN(cur->field_poc[0], cur->field_poc[1]);
2692 
2693  return 0;
2694 }
2695 
2696 /**
2697  * initialize scan tables
2698  */
2700 {
2701  int i;
2702  for (i = 0; i < 16; i++) {
2703 #define T(x) (x >> 2) | ((x << 2) & 0xF)
2704  h->zigzag_scan[i] = T(zigzag_scan[i]);
2705  h->field_scan[i] = T(field_scan[i]);
2706 #undef T
2707  }
2708  for (i = 0; i < 64; i++) {
2709 #define T(x) (x >> 3) | ((x & 7) << 3)
2710  h->zigzag_scan8x8[i] = T(ff_zigzag_direct[i]);
2712  h->field_scan8x8[i] = T(field_scan8x8[i]);
2714 #undef T
2715  }
2716  if (h->sps.transform_bypass) { // FIXME same ugly
2717  memcpy(h->zigzag_scan_q0 , zigzag_scan , sizeof(h->zigzag_scan_q0 ));
2718  memcpy(h->zigzag_scan8x8_q0 , ff_zigzag_direct , sizeof(h->zigzag_scan8x8_q0 ));
2720  memcpy(h->field_scan_q0 , field_scan , sizeof(h->field_scan_q0 ));
2721  memcpy(h->field_scan8x8_q0 , field_scan8x8 , sizeof(h->field_scan8x8_q0 ));
2723  } else {
2724  memcpy(h->zigzag_scan_q0 , h->zigzag_scan , sizeof(h->zigzag_scan_q0 ));
2725  memcpy(h->zigzag_scan8x8_q0 , h->zigzag_scan8x8 , sizeof(h->zigzag_scan8x8_q0 ));
2727  memcpy(h->field_scan_q0 , h->field_scan , sizeof(h->field_scan_q0 ));
2728  memcpy(h->field_scan8x8_q0 , h->field_scan8x8 , sizeof(h->field_scan8x8_q0 ));
2730  }
2731 }
2732 
2733 static int field_end(H264Context *h, int in_setup)
2734 {
2735  AVCodecContext *const avctx = h->avctx;
2736  int err = 0;
2737  h->mb_y = 0;
2738 
2739  if (!in_setup && !h->droppable)
2740  ff_thread_report_progress(&h->cur_pic_ptr->f, INT_MAX,
2742 
2743  if (CONFIG_H264_VDPAU_DECODER &&
2746 
2747  if (in_setup || !(avctx->active_thread_type & FF_THREAD_FRAME)) {
2748  if (!h->droppable) {
2750  h->prev_poc_msb = h->poc_msb;
2751  h->prev_poc_lsb = h->poc_lsb;
2752  }
2754  h->prev_frame_num = h->frame_num;
2756  }
2757 
2758  if (avctx->hwaccel) {
2759  if (avctx->hwaccel->end_frame(avctx) < 0)
2760  av_log(avctx, AV_LOG_ERROR,
2761  "hardware accelerator failed to decode picture\n");
2762  }
2763 
2764  if (CONFIG_H264_VDPAU_DECODER &&
2767 
2768  /*
2769  * FIXME: Error handling code does not seem to support interlaced
2770  * when slices span multiple rows
2771  * The ff_er_add_slice calls don't work right for bottom
2772  * fields; they cause massive erroneous error concealing
2773  * Error marking covers both fields (top and bottom).
2774  * This causes a mismatched s->error_count
2775  * and a bad error table. Further, the error count goes to
2776  * INT_MAX when called for bottom field, because mb_y is
2777  * past end by one (callers fault) and resync_mb_y != 0
2778  * causes problems for the first MB line, too.
2779  */
2780  if (CONFIG_ERROR_RESILIENCE &&
2781  !FIELD_PICTURE && h->current_slice && !h->sps.new) {
2782  h->er.cur_pic = h->cur_pic_ptr;
2783  ff_er_frame_end(&h->er);
2784  }
2785  emms_c();
2786 
2787  h->current_slice = 0;
2788 
2789  return err;
2790 }
2791 
2792 /**
2793  * Replicate H264 "master" context to thread contexts.
2794  */
2796 {
2797  memcpy(dst->block_offset, src->block_offset, sizeof(dst->block_offset));
2798  dst->cur_pic_ptr = src->cur_pic_ptr;
2799  dst->cur_pic = src->cur_pic;
2800  dst->linesize = src->linesize;
2801  dst->uvlinesize = src->uvlinesize;
2802  dst->first_field = src->first_field;
2803 
2804  dst->prev_poc_msb = src->prev_poc_msb;
2805  dst->prev_poc_lsb = src->prev_poc_lsb;
2807  dst->prev_frame_num = src->prev_frame_num;
2808  dst->short_ref_count = src->short_ref_count;
2809 
2810  memcpy(dst->short_ref, src->short_ref, sizeof(dst->short_ref));
2811  memcpy(dst->long_ref, src->long_ref, sizeof(dst->long_ref));
2812  memcpy(dst->default_ref_list, src->default_ref_list, sizeof(dst->default_ref_list));
2813 
2814  memcpy(dst->dequant4_coeff, src->dequant4_coeff, sizeof(src->dequant4_coeff));
2815  memcpy(dst->dequant8_coeff, src->dequant8_coeff, sizeof(src->dequant8_coeff));
2816 
2817  return 0;
2818 }
2819 
2820 /**
2821  * Compute profile from profile_idc and constraint_set?_flags.
2822  *
2823  * @param sps SPS
2824  *
2825  * @return profile as defined by FF_PROFILE_H264_*
2826  */
2828 {
2829  int profile = sps->profile_idc;
2830 
2831  switch (sps->profile_idc) {
2833  // constraint_set1_flag set to 1
2834  profile |= (sps->constraint_set_flags & 1 << 1) ? FF_PROFILE_H264_CONSTRAINED : 0;
2835  break;
2839  // constraint_set3_flag set to 1
2840  profile |= (sps->constraint_set_flags & 1 << 3) ? FF_PROFILE_H264_INTRA : 0;
2841  break;
2842  }
2843 
2844  return profile;
2845 }
2846 
2848 {
2849  if (h->flags & CODEC_FLAG_LOW_DELAY ||
2851  !h->sps.num_reorder_frames)) {
2852  if (h->avctx->has_b_frames > 1 || h->delayed_pic[0])
2853  av_log(h->avctx, AV_LOG_WARNING, "Delayed frames seen. "
2854  "Reenabling low delay requires a codec flush.\n");
2855  else
2856  h->low_delay = 1;
2857  }
2858 
2859  if (h->avctx->has_b_frames < 2)
2860  h->avctx->has_b_frames = !h->low_delay;
2861 
2862  if (h->sps.bit_depth_luma != h->sps.bit_depth_chroma) {
2864  "Different bit depth between chroma and luma", 1);
2865  return AVERROR_PATCHWELCOME;
2866  }
2867 
2868  if (h->avctx->bits_per_raw_sample != h->sps.bit_depth_luma ||
2870  if (h->avctx->codec &&
2872  (h->sps.bit_depth_luma != 8 || h->sps.chroma_format_idc > 1)) {
2874  "VDPAU decoding does not support video colorspace.\n");
2875  return AVERROR_INVALIDDATA;
2876  }
2877  if (h->sps.bit_depth_luma >= 8 && h->sps.bit_depth_luma <= 14 &&
2878  h->sps.bit_depth_luma != 11 && h->sps.bit_depth_luma != 13 &&
2879  (h->sps.bit_depth_luma != 9 || !CHROMA422)) {
2882  h->pixel_shift = h->sps.bit_depth_luma > 8;
2883 
2885  h->sps.chroma_format_idc);
2889  h->sps.chroma_format_idc);
2890  if (CONFIG_ERROR_RESILIENCE) {
2891  h->dsp.dct_bits = h->sps.bit_depth_luma > 8 ? 32 : 16;
2892  ff_dsputil_init(&h->dsp, h->avctx);
2893  }
2895  } else {
2896  av_log(h->avctx, AV_LOG_ERROR, "Unsupported bit depth: %d\n",
2897  h->sps.bit_depth_luma);
2898  return AVERROR_INVALIDDATA;
2899  }
2900  }
2901  return 0;
2902 }
2903 
2904 static enum PixelFormat get_pixel_format(H264Context *h, int force_callback)
2905 {
2906  switch (h->sps.bit_depth_luma) {
2907  case 9:
2908  if (CHROMA444) {
2909  if (h->avctx->colorspace == AVCOL_SPC_RGB) {
2910  return AV_PIX_FMT_GBRP9;
2911  } else
2912  return AV_PIX_FMT_YUV444P9;
2913  } else if (CHROMA422)
2914  return AV_PIX_FMT_YUV422P9;
2915  else
2916  return AV_PIX_FMT_YUV420P9;
2917  break;
2918  case 10:
2919  if (CHROMA444) {
2920  if (h->avctx->colorspace == AVCOL_SPC_RGB) {
2921  return AV_PIX_FMT_GBRP10;
2922  } else
2923  return AV_PIX_FMT_YUV444P10;
2924  } else if (CHROMA422)
2925  return AV_PIX_FMT_YUV422P10;
2926  else
2927  return AV_PIX_FMT_YUV420P10;
2928  break;
2929  case 12:
2930  if (CHROMA444) {
2931  if (h->avctx->colorspace == AVCOL_SPC_RGB) {
2932  return AV_PIX_FMT_GBRP12;
2933  } else
2934  return AV_PIX_FMT_YUV444P12;
2935  } else if (CHROMA422)
2936  return AV_PIX_FMT_YUV422P12;
2937  else
2938  return AV_PIX_FMT_YUV420P12;
2939  break;
2940  case 14:
2941  if (CHROMA444) {
2942  if (h->avctx->colorspace == AVCOL_SPC_RGB) {
2943  return AV_PIX_FMT_GBRP14;
2944  } else
2945  return AV_PIX_FMT_YUV444P14;
2946  } else if (CHROMA422)
2947  return AV_PIX_FMT_YUV422P14;
2948  else
2949  return AV_PIX_FMT_YUV420P14;
2950  break;
2951  case 8:
2952  if (CHROMA444) {
2953  if (h->avctx->colorspace == AVCOL_SPC_RGB) {
2954  av_log(h->avctx, AV_LOG_DEBUG, "Detected GBR colorspace.\n");
2955  return AV_PIX_FMT_GBR24P;
2956  } else if (h->avctx->colorspace == AVCOL_SPC_YCGCO) {
2957  av_log(h->avctx, AV_LOG_WARNING, "Detected unsupported YCgCo colorspace.\n");
2958  }
2961  } else if (CHROMA422) {
2964  } else {
2965  int i;
2966  const enum AVPixelFormat * fmt = h->avctx->codec->pix_fmts ?
2967  h->avctx->codec->pix_fmts :
2971 
2972  for (i=0; fmt[i] != AV_PIX_FMT_NONE; i++)
2973  if (fmt[i] == h->avctx->pix_fmt && !force_callback)
2974  return fmt[i];
2975  return h->avctx->get_format(h->avctx, fmt);
2976  }
2977  break;
2978  default:
2980  "Unsupported bit depth: %d\n", h->sps.bit_depth_luma);
2981  return AVERROR_INVALIDDATA;
2982  }
2983 }
2984 
2986 {
2987  int nb_slices = (HAVE_THREADS &&
2989  h->avctx->thread_count : 1;
2990  int i;
2991 
2992  if( FFALIGN(h->avctx->width , 16 ) == h->width
2993  && FFALIGN(h->avctx->height, 16*(2 - h->sps.frame_mbs_only_flag)) == h->height
2994  && !h->sps.crop_right && !h->sps.crop_bottom
2995  && (h->avctx->width != h->width || h->avctx->height && h->height)
2996  ) {
2997  av_log(h->avctx, AV_LOG_DEBUG, "Using externally provided dimensions\n");
2998  h->avctx->coded_width = h->width;
2999  h->avctx->coded_height = h->height;
3000  } else{
3002  h->avctx->width -= (2>>CHROMA444)*FFMIN(h->sps.crop_right, (8<<CHROMA444)-1);
3003  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);
3004  }
3005 
3006  h->avctx->sample_aspect_ratio = h->sps.sar;
3009  &h->chroma_x_shift, &h->chroma_y_shift);
3010 
3011  if (h->sps.timing_info_present_flag) {
3012  int64_t den = h->sps.time_scale;
3013  if (h->x264_build < 44U)
3014  den *= 2;
3016  h->sps.num_units_in_tick, den, 1 << 30);
3017  }
3018 
3020 
3021  if (reinit)
3022  free_tables(h, 0);
3023  h->first_field = 0;
3024  h->prev_interlaced_frame = 1;
3025 
3026  init_scan_tables(h);
3027  if (ff_h264_alloc_tables(h) < 0) {
3029  "Could not allocate memory for h264\n");
3030  return AVERROR(ENOMEM);
3031  }
3032 
3033  if (nb_slices > MAX_THREADS || (nb_slices > h->mb_height && h->mb_height)) {
3034  int max_slices;
3035  if (h->mb_height)
3036  max_slices = FFMIN(MAX_THREADS, h->mb_height);
3037  else
3038  max_slices = MAX_THREADS;
3039  av_log(h->avctx, AV_LOG_WARNING, "too many threads/slices (%d),"
3040  " reducing to %d\n", nb_slices, max_slices);
3041  nb_slices = max_slices;
3042  }
3043  h->slice_context_count = nb_slices;
3044 
3045  if (!HAVE_THREADS || !(h->avctx->active_thread_type & FF_THREAD_SLICE)) {
3046  if (context_init(h) < 0) {
3047  av_log(h->avctx, AV_LOG_ERROR, "context_init() failed.\n");
3048  return -1;
3049  }
3050  } else {
3051  for (i = 1; i < h->slice_context_count; i++) {
3052  H264Context *c;
3053  c = h->thread_context[i] = av_mallocz(sizeof(H264Context));
3054  c->avctx = h->avctx;
3055  if (CONFIG_ERROR_RESILIENCE) {
3056  c->dsp = h->dsp;
3057  }
3058  c->vdsp = h->vdsp;
3059  c->h264dsp = h->h264dsp;
3060  c->h264qpel = h->h264qpel;
3061  c->h264chroma = h->h264chroma;
3062  c->sps = h->sps;
3063  c->pps = h->pps;
3064  c->pixel_shift = h->pixel_shift;
3066  c->width = h->width;
3067  c->height = h->height;
3068  c->linesize = h->linesize;
3069  c->uvlinesize = h->uvlinesize;
3072  c->qscale = h->qscale;
3073  c->droppable = h->droppable;
3075  c->low_delay = h->low_delay;
3076  c->mb_width = h->mb_width;
3077  c->mb_height = h->mb_height;
3078  c->mb_stride = h->mb_stride;
3079  c->mb_num = h->mb_num;
3080  c->flags = h->flags;
3082  c->pict_type = h->pict_type;
3083 
3084  init_scan_tables(c);
3085  clone_tables(c, h, i);
3086  c->context_initialized = 1;
3087  }
3088 
3089  for (i = 0; i < h->slice_context_count; i++)
3090  if (context_init(h->thread_context[i]) < 0) {
3091  av_log(h->avctx, AV_LOG_ERROR, "context_init() failed.\n");
3092  return -1;
3093  }
3094  }
3095 
3096  h->context_initialized = 1;
3097 
3098  return 0;
3099 }
3100 
3101 /**
3102  * Decode a slice header.
3103  * This will also call ff_MPV_common_init() and frame_start() as needed.
3104  *
3105  * @param h h264context
3106  * @param h0 h264 master context (differs from 'h' when doing sliced based
3107  * parallel decoding)
3108  *
3109  * @return 0 if okay, <0 if an error occurred, 1 if decoding must not be multithreaded
3110  */
3112 {
3113  unsigned int first_mb_in_slice;
3114  unsigned int pps_id;
3115  int num_ref_idx_active_override_flag, ret;
3116  unsigned int slice_type, tmp, i, j;
3117  int default_ref_list_done = 0;
3118  int last_pic_structure, last_pic_droppable;
3119  int must_reinit;
3120  int needs_reinit = 0;
3121 
3124 
3125  first_mb_in_slice = get_ue_golomb_long(&h->gb);
3126 
3127  if (first_mb_in_slice == 0) { // FIXME better field boundary detection
3128  if (h0->current_slice && FIELD_PICTURE) {
3129  field_end(h, 1);
3130  }
3131 
3132  h0->current_slice = 0;
3133  if (!h0->first_field) {
3134  if (h->cur_pic_ptr && !h->droppable &&
3135  h->cur_pic_ptr->owner2 == h) {
3136  ff_thread_report_progress(&h->cur_pic_ptr->f, INT_MAX,
3138  }
3139  h->cur_pic_ptr = NULL;
3140  }
3141  }
3142 
3143  slice_type = get_ue_golomb_31(&h->gb);
3144  if (slice_type > 9) {
3146  "slice type too large (%d) at %d %d\n",
3147  slice_type, h->mb_x, h->mb_y);
3148  return -1;
3149  }
3150  if (slice_type > 4) {
3151  slice_type -= 5;
3152  h->slice_type_fixed = 1;
3153  } else
3154  h->slice_type_fixed = 0;
3155 
3156  slice_type = golomb_to_pict_type[slice_type];
3157  if (slice_type == AV_PICTURE_TYPE_I ||
3158  (h0->current_slice != 0 &&
3159  slice_type == h0->last_slice_type &&
3160  !memcmp(h0->last_ref_count, h0->ref_count, sizeof(h0->ref_count)))) {
3161  default_ref_list_done = 1;
3162  }
3163  h->slice_type = slice_type;
3164  h->slice_type_nos = slice_type & 3;
3165 
3166  // to make a few old functions happy, it's wrong though
3167  h->pict_type = h->slice_type;
3168 
3169  pps_id = get_ue_golomb(&h->gb);
3170  if (pps_id >= MAX_PPS_COUNT) {
3171  av_log(h->avctx, AV_LOG_ERROR, "pps_id %d out of range\n", pps_id);
3172  return -1;
3173  }
3174  if (!h0->pps_buffers[pps_id]) {
3176  "non-existing PPS %u referenced\n",
3177  pps_id);
3178  return -1;
3179  }
3180  h->pps = *h0->pps_buffers[pps_id];
3181 
3182  if (!h0->sps_buffers[h->pps.sps_id]) {
3184  "non-existing SPS %u referenced\n",
3185  h->pps.sps_id);
3186  return -1;
3187  }
3188 
3189  if (h->pps.sps_id != h->current_sps_id ||
3190  h0->sps_buffers[h->pps.sps_id]->new) {
3191  h0->sps_buffers[h->pps.sps_id]->new = 0;
3192 
3193  h->current_sps_id = h->pps.sps_id;
3194  h->sps = *h0->sps_buffers[h->pps.sps_id];
3195 
3196  if (h->mb_width != h->sps.mb_width ||
3197  h->mb_height != h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag) ||
3200  )
3201  needs_reinit = 1;
3202 
3203  if (h->bit_depth_luma != h->sps.bit_depth_luma ||
3207  needs_reinit = 1;
3208  }
3209  if ((ret = h264_set_parameter_from_sps(h)) < 0)
3210  return ret;
3211  }
3212 
3213  h->avctx->profile = ff_h264_get_profile(&h->sps);
3214  h->avctx->level = h->sps.level_idc;
3215  h->avctx->refs = h->sps.ref_frame_count;
3216 
3217  must_reinit = (h->context_initialized &&
3218  ( 16*h->sps.mb_width != h->avctx->coded_width
3219  || 16*h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag) != h->avctx->coded_height
3222  || av_cmp_q(h->sps.sar, h->avctx->sample_aspect_ratio)));
3223  if (h0->avctx->pix_fmt != get_pixel_format(h0, 0))
3224  must_reinit = 1;
3225 
3226  h->mb_width = h->sps.mb_width;
3227  h->mb_height = h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag);
3228  h->mb_num = h->mb_width * h->mb_height;
3229  h->mb_stride = h->mb_width + 1;
3230 
3231  h->b_stride = h->mb_width * 4;
3232 
3233  h->chroma_y_shift = h->sps.chroma_format_idc <= 1; // 400 uses yuv420p
3234 
3235  h->width = 16 * h->mb_width;
3236  h->height = 16 * h->mb_height;
3237 
3240  : AVCOL_RANGE_MPEG;
3242  if (h->avctx->colorspace != h->sps.colorspace)
3243  needs_reinit = 1;
3245  h->avctx->color_trc = h->sps.color_trc;
3246  h->avctx->colorspace = h->sps.colorspace;
3247  }
3248  }
3249 
3250  if (h->context_initialized &&
3251  (
3252  needs_reinit ||
3253  must_reinit)) {
3254 
3255  if (h != h0) {
3256  av_log(h->avctx, AV_LOG_ERROR, "changing width/height on "
3257  "slice %d\n", h0->current_slice + 1);
3258  return AVERROR_INVALIDDATA;
3259  }
3260 
3261  flush_change(h);
3262 
3263  if ((ret = get_pixel_format(h, 1)) < 0)
3264  return ret;
3265  h->avctx->pix_fmt = ret;
3266 
3267  av_log(h->avctx, AV_LOG_INFO, "Reinit context to %dx%d, "
3268  "pix_fmt: %d\n", h->width, h->height, h->avctx->pix_fmt);
3269 
3270  if ((ret = h264_slice_header_init(h, 1)) < 0) {
3272  "h264_slice_header_init() failed\n");
3273  return ret;
3274  }
3275  }
3276  if (!h->context_initialized) {
3277  if (h != h0) {
3279  "Cannot (re-)initialize context during parallel decoding.\n");
3280  return -1;
3281  }
3282 
3283  if ((ret = get_pixel_format(h, 1)) < 0)
3284  return ret;
3285  h->avctx->pix_fmt = ret;
3286 
3287  if ((ret = h264_slice_header_init(h, 0)) < 0) {
3289  "h264_slice_header_init() failed\n");
3290  return ret;
3291  }
3292  }
3293 
3294  if (h == h0 && h->dequant_coeff_pps != pps_id) {
3295  h->dequant_coeff_pps = pps_id;
3297  }
3298 
3299  h->frame_num = get_bits(&h->gb, h->sps.log2_max_frame_num);
3300 
3301  h->mb_mbaff = 0;
3302  h->mb_aff_frame = 0;
3303  last_pic_structure = h0->picture_structure;
3304  last_pic_droppable = h0->droppable;
3305  h->droppable = h->nal_ref_idc == 0;
3306  if (h->sps.frame_mbs_only_flag) {
3308  } else {
3309  if (!h->sps.direct_8x8_inference_flag && slice_type == AV_PICTURE_TYPE_B) {
3310  av_log(h->avctx, AV_LOG_ERROR, "This stream was generated by a broken encoder, invalid 8x8 inference\n");
3311  return -1;
3312  }
3313  if (get_bits1(&h->gb)) { // field_pic_flag
3314  h->picture_structure = PICT_TOP_FIELD + get_bits1(&h->gb); // bottom_field_flag
3315  } else {
3317  h->mb_aff_frame = h->sps.mb_aff;
3318  }
3319  }
3321 
3322  if (h0->current_slice != 0) {
3323  if (last_pic_structure != h->picture_structure ||
3324  last_pic_droppable != h->droppable) {
3326  "Changing field mode (%d -> %d) between slices is not allowed\n",
3327  last_pic_structure, h->picture_structure);
3328  h->picture_structure = last_pic_structure;
3329  h->droppable = last_pic_droppable;
3330  return AVERROR_INVALIDDATA;
3331  } else if (!h0->cur_pic_ptr) {
3333  "unset cur_pic_ptr on %d. slice\n",
3334  h0->current_slice + 1);
3335  return AVERROR_INVALIDDATA;
3336  }
3337  } else {
3338  /* Shorten frame num gaps so we don't have to allocate reference
3339  * frames just to throw them away */
3340  if (h->frame_num != h->prev_frame_num && h->prev_frame_num >= 0) {
3341  int unwrap_prev_frame_num = h->prev_frame_num;
3342  int max_frame_num = 1 << h->sps.log2_max_frame_num;
3343 
3344  if (unwrap_prev_frame_num > h->frame_num)
3345  unwrap_prev_frame_num -= max_frame_num;
3346 
3347  if ((h->frame_num - unwrap_prev_frame_num) > h->sps.ref_frame_count) {
3348  unwrap_prev_frame_num = (h->frame_num - h->sps.ref_frame_count) - 1;
3349  if (unwrap_prev_frame_num < 0)
3350  unwrap_prev_frame_num += max_frame_num;
3351 
3352  h->prev_frame_num = unwrap_prev_frame_num;
3353  }
3354  }
3355 
3356  /* See if we have a decoded first field looking for a pair...
3357  * Here, we're using that to see if we should mark previously
3358  * decode frames as "finished".
3359  * We have to do that before the "dummy" in-between frame allocation,
3360  * since that can modify h->cur_pic_ptr. */
3361  if (h0->first_field) {
3362  assert(h0->cur_pic_ptr);
3363  assert(h0->cur_pic_ptr->f.data[0]);
3364  assert(h0->cur_pic_ptr->f.reference != DELAYED_PIC_REF);
3365 
3366  /* Mark old field/frame as completed */
3367  if (!last_pic_droppable && h0->cur_pic_ptr->owner2 == h0) {
3368  ff_thread_report_progress(&h0->cur_pic_ptr->f, INT_MAX,
3369  last_pic_structure == PICT_BOTTOM_FIELD);
3370  }
3371 
3372  /* figure out if we have a complementary field pair */
3373  if (!FIELD_PICTURE || h->picture_structure == last_pic_structure) {
3374  /* Previous field is unmatched. Don't display it, but let it
3375  * remain for reference if marked as such. */
3376  if (!last_pic_droppable && last_pic_structure != PICT_FRAME) {
3377  ff_thread_report_progress(&h0->cur_pic_ptr->f, INT_MAX,
3378  last_pic_structure == PICT_TOP_FIELD);
3379  }
3380  } else {
3381  if (h0->cur_pic_ptr->frame_num != h->frame_num) {
3382  /* This and previous field were reference, but had
3383  * different frame_nums. Consider this field first in
3384  * pair. Throw away previous field except for reference
3385  * purposes. */
3386  if (!last_pic_droppable && last_pic_structure != PICT_FRAME) {
3387  ff_thread_report_progress(&h0->cur_pic_ptr->f, INT_MAX,
3388  last_pic_structure == PICT_TOP_FIELD);
3389  }
3390  } else {
3391  /* Second field in complementary pair */
3392  if (!((last_pic_structure == PICT_TOP_FIELD &&
3394  (last_pic_structure == PICT_BOTTOM_FIELD &&
3397  "Invalid field mode combination %d/%d\n",
3398  last_pic_structure, h->picture_structure);
3399  h->picture_structure = last_pic_structure;
3400  h->droppable = last_pic_droppable;
3401  return AVERROR_INVALIDDATA;
3402  } else if (last_pic_droppable != h->droppable) {
3404  "Cannot combine reference and non-reference fields in the same frame\n");
3406  h->picture_structure = last_pic_structure;
3407  h->droppable = last_pic_droppable;
3408  return AVERROR_PATCHWELCOME;
3409  }
3410 
3411  /* Take ownership of this buffer. Note that if another thread owned
3412  * the first field of this buffer, we're not operating on that pointer,
3413  * so the original thread is still responsible for reporting progress
3414  * on that first field (or if that was us, we just did that above).
3415  * By taking ownership, we assign responsibility to ourselves to
3416  * report progress on the second field. */
3417  h0->cur_pic_ptr->owner2 = h0;
3418  }
3419  }
3420  }
3421 
3422  while (h->frame_num != h->prev_frame_num && h->prev_frame_num >= 0 && !h0->first_field &&
3423  h->frame_num != (h->prev_frame_num + 1) % (1 << h->sps.log2_max_frame_num)) {
3424  Picture *prev = h->short_ref_count ? h->short_ref[0] : NULL;
3425  av_log(h->avctx, AV_LOG_DEBUG, "Frame num gap %d %d\n",
3426  h->frame_num, h->prev_frame_num);
3428  for(i=0; i<FF_ARRAY_ELEMS(h->last_pocs); i++)
3429  h->last_pocs[i] = INT_MIN;
3430  if (ff_h264_frame_start(h) < 0)
3431  return -1;
3432  h->prev_frame_num++;
3433  h->prev_frame_num %= 1 << h->sps.log2_max_frame_num;
3435  ff_thread_report_progress(&h->cur_pic_ptr->f, INT_MAX, 0);
3436  ff_thread_report_progress(&h->cur_pic_ptr->f, INT_MAX, 1);
3437  if ((ret = ff_generate_sliding_window_mmcos(h, 1)) < 0 &&
3439  return ret;
3440  if (ff_h264_execute_ref_pic_marking(h, h->mmco, h->mmco_index) < 0 &&
3442  return AVERROR_INVALIDDATA;
3443  /* Error concealment: if a ref is missing, copy the previous ref in its place.
3444  * FIXME: avoiding a memcpy would be nice, but ref handling makes many assumptions
3445  * about there being no actual duplicates.
3446  * FIXME: this doesn't copy padding for out-of-frame motion vectors. Given we're
3447  * concealing a lost frame, this probably isn't noticeable by comparison, but it should
3448  * be fixed. */
3449  if (h->short_ref_count) {
3450  if (prev) {
3451  av_image_copy(h->short_ref[0]->f.data, h->short_ref[0]->f.linesize,
3452  (const uint8_t **)prev->f.data, prev->f.linesize,
3453  h->avctx->pix_fmt, h->mb_width * 16, h->mb_height * 16);
3454  h->short_ref[0]->poc = prev->poc + 2;
3455  }
3456  h->short_ref[0]->frame_num = h->prev_frame_num;
3457  }
3458  }
3459 
3460  /* See if we have a decoded first field looking for a pair...
3461  * We're using that to see whether to continue decoding in that
3462  * frame, or to allocate a new one. */
3463  if (h0->first_field) {
3464  assert(h0->cur_pic_ptr);
3465  assert(h0->cur_pic_ptr->f.data[0]);
3466  assert(h0->cur_pic_ptr->f.reference != DELAYED_PIC_REF);
3467 
3468  /* figure out if we have a complementary field pair */
3469  if (!FIELD_PICTURE || h->picture_structure == last_pic_structure) {
3470  /* Previous field is unmatched. Don't display it, but let it
3471  * remain for reference if marked as such. */
3472  h0->cur_pic_ptr = NULL;
3473  h0->first_field = FIELD_PICTURE;
3474  } else {
3475  if (h0->cur_pic_ptr->frame_num != h->frame_num) {
3478  /* This and the previous field had different frame_nums.
3479  * Consider this field first in pair. Throw away previous
3480  * one except for reference purposes. */
3481  h0->first_field = 1;
3482  h0->cur_pic_ptr = NULL;
3483  } else {
3484  /* Second field in complementary pair */
3485  h0->first_field = 0;
3486  }
3487  }
3488  } else {
3489  /* Frame or first field in a potentially complementary pair */
3490  h0->first_field = FIELD_PICTURE;
3491  }
3492 
3493  if (!FIELD_PICTURE || h0->first_field) {
3494  if (ff_h264_frame_start(h) < 0) {
3495  h0->first_field = 0;
3496  return -1;
3497  }
3498  } else {
3500  }
3501  }
3502  if (h != h0 && (ret = clone_slice(h, h0)) < 0)
3503  return ret;
3504 
3505  /* can't be in alloc_tables because linesize isn't known there.
3506  * FIXME: redo bipred weight to not require extra buffer? */
3507  for (i = 0; i < h->slice_context_count; i++)
3508  if (h->thread_context[i]) {
3510  if (ret < 0)
3511  return ret;
3512  }
3513 
3514  h->cur_pic_ptr->frame_num = h->frame_num; // FIXME frame_num cleanup
3515 
3516  av_assert1(h->mb_num == h->mb_width * h->mb_height);
3517  if (first_mb_in_slice << FIELD_OR_MBAFF_PICTURE >= h->mb_num ||
3518  first_mb_in_slice >= h->mb_num) {
3519  av_log(h->avctx, AV_LOG_ERROR, "first_mb_in_slice overflow\n");
3520  return -1;
3521  }
3522  h->resync_mb_x = h->mb_x = first_mb_in_slice % h->mb_width;
3523  h->resync_mb_y = h->mb_y = (first_mb_in_slice / h->mb_width) << FIELD_OR_MBAFF_PICTURE;
3525  h->resync_mb_y = h->mb_y = h->mb_y + 1;
3526  av_assert1(h->mb_y < h->mb_height);
3527 
3528  if (h->picture_structure == PICT_FRAME) {
3529  h->curr_pic_num = h->frame_num;
3530  h->max_pic_num = 1 << h->sps.log2_max_frame_num;
3531  } else {
3532  h->curr_pic_num = 2 * h->frame_num + 1;
3533  h->max_pic_num = 1 << (h->sps.log2_max_frame_num + 1);
3534  }
3535 
3536  if (h->nal_unit_type == NAL_IDR_SLICE)
3537  get_ue_golomb(&h->gb); /* idr_pic_id */
3538 
3539  if (h->sps.poc_type == 0) {
3540  h->poc_lsb = get_bits(&h->gb, h->sps.log2_max_poc_lsb);
3541 
3542  if (h->pps.pic_order_present == 1 && h->picture_structure == PICT_FRAME)
3543  h->delta_poc_bottom = get_se_golomb(&h->gb);
3544  }
3545 
3546  if (h->sps.poc_type == 1 && !h->sps.delta_pic_order_always_zero_flag) {
3547  h->delta_poc[0] = get_se_golomb(&h->gb);
3548 
3549  if (h->pps.pic_order_present == 1 && h->picture_structure == PICT_FRAME)
3550  h->delta_poc[1] = get_se_golomb(&h->gb);
3551  }
3552 
3553  init_poc(h);
3554 
3557 
3558  // set defaults, might be overridden a few lines later
3559  h->ref_count[0] = h->pps.ref_count[0];
3560  h->ref_count[1] = h->pps.ref_count[1];
3561 
3562  if (h->slice_type_nos != AV_PICTURE_TYPE_I) {
3563  unsigned max[2];
3564  max[0] = max[1] = h->picture_structure == PICT_FRAME ? 15 : 31;
3565 
3568  num_ref_idx_active_override_flag = get_bits1(&h->gb);
3569 
3570  if (num_ref_idx_active_override_flag) {
3571  h->ref_count[0] = get_ue_golomb(&h->gb) + 1;
3572  if (h->slice_type_nos == AV_PICTURE_TYPE_B) {
3573  h->ref_count[1] = get_ue_golomb(&h->gb) + 1;
3574  } else
3575  // full range is spec-ok in this case, even for frames
3576  h->ref_count[1] = 1;
3577  }
3578 
3579  if (h->ref_count[0]-1 > max[0] || h->ref_count[1]-1 > max[1]){
3580  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]);
3581  h->ref_count[0] = h->ref_count[1] = 0;
3582  return AVERROR_INVALIDDATA;
3583  }
3584 
3586  h->list_count = 2;
3587  else
3588  h->list_count = 1;
3589  } else {
3590  h->list_count = 0;
3591  h->ref_count[0] = h->ref_count[1] = 0;
3592  }
3593 
3594  if (!default_ref_list_done)
3596 
3597  if (h->slice_type_nos != AV_PICTURE_TYPE_I &&
3599  h->ref_count[1] = h->ref_count[0] = 0;
3600  return -1;
3601  }
3602 
3603  if ((h->pps.weighted_pred && h->slice_type_nos == AV_PICTURE_TYPE_P) ||
3604  (h->pps.weighted_bipred_idc == 1 &&
3606  pred_weight_table(h);
3607  else if (h->pps.weighted_bipred_idc == 2 &&
3609  implicit_weight_table(h, -1);
3610  } else {
3611  h->use_weight = 0;
3612  for (i = 0; i < 2; i++) {
3613  h->luma_weight_flag[i] = 0;
3614  h->chroma_weight_flag[i] = 0;
3615  }
3616  }
3617 
3618  // If frame-mt is enabled, only update mmco tables for the first slice
3619  // in a field. Subsequent slices can temporarily clobber h->mmco_index
3620  // or h->mmco, which will cause ref list mix-ups and decoding errors
3621  // further down the line. This may break decoding if the first slice is
3622  // corrupt, thus we only do this if frame-mt is enabled.
3623  if (h->nal_ref_idc &&
3626  h0->current_slice == 0) < 0 &&
3628  return AVERROR_INVALIDDATA;
3629 
3630  if (FRAME_MBAFF) {
3632 
3634  implicit_weight_table(h, 0);
3635  implicit_weight_table(h, 1);
3636  }
3637  }
3638 
3642 
3643  if (h->slice_type_nos != AV_PICTURE_TYPE_I && h->pps.cabac) {
3644  tmp = get_ue_golomb_31(&h->gb);
3645  if (tmp > 2) {
3646  av_log(h->avctx, AV_LOG_ERROR, "cabac_init_idc overflow\n");
3647  return -1;
3648  }
3649  h->cabac_init_idc = tmp;
3650  }
3651 
3652  h->last_qscale_diff = 0;
3653  tmp = h->pps.init_qp + get_se_golomb(&h->gb);
3654  if (tmp > 51 + 6 * (h->sps.bit_depth_luma - 8)) {
3655  av_log(h->avctx, AV_LOG_ERROR, "QP %u out of range\n", tmp);
3656  return -1;
3657  }
3658  h->qscale = tmp;
3659  h->chroma_qp[0] = get_chroma_qp(h, 0, h->qscale);
3660  h->chroma_qp[1] = get_chroma_qp(h, 1, h->qscale);
3661  // FIXME qscale / qp ... stuff
3662  if (h->slice_type == AV_PICTURE_TYPE_SP)
3663  get_bits1(&h->gb); /* sp_for_switch_flag */
3664  if (h->slice_type == AV_PICTURE_TYPE_SP ||
3666  get_se_golomb(&h->gb); /* slice_qs_delta */
3667 
3668  h->deblocking_filter = 1;
3669  h->slice_alpha_c0_offset = 52;
3670  h->slice_beta_offset = 52;
3672  tmp = get_ue_golomb_31(&h->gb);
3673  if (tmp > 2) {
3675  "deblocking_filter_idc %u out of range\n", tmp);
3676  return -1;
3677  }
3678  h->deblocking_filter = tmp;
3679  if (h->deblocking_filter < 2)
3680  h->deblocking_filter ^= 1; // 1<->0
3681 
3682  if (h->deblocking_filter) {
3683  h->slice_alpha_c0_offset += get_se_golomb(&h->gb) << 1;
3684  h->slice_beta_offset += get_se_golomb(&h->gb) << 1;
3685  if (h->slice_alpha_c0_offset > 104U ||
3686  h->slice_beta_offset > 104U) {
3688  "deblocking filter parameters %d %d out of range\n",
3690  return -1;
3691  }
3692  }
3693  }
3694 
3695  if (h->avctx->skip_loop_filter >= AVDISCARD_ALL ||
3701  h->nal_ref_idc == 0))
3702  h->deblocking_filter = 0;
3703 
3704  if (h->deblocking_filter == 1 && h0->max_contexts > 1) {
3705  if (h->avctx->flags2 & CODEC_FLAG2_FAST) {
3706  /* Cheat slightly for speed:
3707  * Do not bother to deblock across slices. */
3708  h->deblocking_filter = 2;
3709  } else {
3710  h0->max_contexts = 1;
3711  if (!h0->single_decode_warning) {
3712  av_log(h->avctx, AV_LOG_INFO,
3713  "Cannot parallelize deblocking type 1, decoding such frames in sequential order\n");
3714  h0->single_decode_warning = 1;
3715  }
3716  if (h != h0) {
3718  "Deblocking switched inside frame.\n");
3719  return 1;
3720  }
3721  }
3722  }
3723  h->qp_thresh = 15 + 52 -
3725  FFMAX3(0,
3727  h->pps.chroma_qp_index_offset[1]) +
3728  6 * (h->sps.bit_depth_luma - 8);
3729 
3730  h0->last_slice_type = slice_type;
3731  memcpy(h0->last_ref_count, h0->ref_count, sizeof(h0->last_ref_count));
3732  h->slice_num = ++h0->current_slice;
3733 
3734  if (h->slice_num)
3735  h0->slice_row[(h->slice_num-1)&(MAX_SLICES-1)]= h->resync_mb_y;
3736  if ( h0->slice_row[h->slice_num&(MAX_SLICES-1)] + 3 >= h->resync_mb_y
3737  && h0->slice_row[h->slice_num&(MAX_SLICES-1)] <= h->resync_mb_y
3738  && h->slice_num >= MAX_SLICES) {
3739  //in case of ASO this check needs to be updated depending on how we decide to assign slice numbers in this case
3740  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);
3741  }
3742 
3743  for (j = 0; j < 2; j++) {
3744  int id_list[16];
3745  int *ref2frm = h->ref2frm[h->slice_num & (MAX_SLICES - 1)][j];
3746  for (i = 0; i < 16; i++) {
3747  id_list[i] = 60;
3748  if (h->ref_list[j][i].f.data[0]) {
3749  int k;
3750  uint8_t *base = h->ref_list[j][i].f.base[0];
3751  for (k = 0; k < h->short_ref_count; k++)
3752  if (h->short_ref[k]->f.base[0] == base) {
3753  id_list[i] = k;
3754  break;
3755  }
3756  for (k = 0; k < h->long_ref_count; k++)
3757  if (h->long_ref[k] && h->long_ref[k]->f.base[0] == base) {
3758  id_list[i] = h->short_ref_count + k;
3759  break;
3760  }
3761  }
3762  }
3763 
3764  ref2frm[0] =
3765  ref2frm[1] = -1;
3766  for (i = 0; i < 16; i++)
3767  ref2frm[i + 2] = 4 * id_list[i] +
3768  (h->ref_list[j][i].f.reference & 3);
3769  ref2frm[18 + 0] =
3770  ref2frm[18 + 1] = -1;
3771  for (i = 16; i < 48; i++)
3772  ref2frm[i + 4] = 4 * id_list[(i - 16) >> 1] +
3773  (h->ref_list[j][i].f.reference & 3);
3774  }
3775 
3776  if (h->ref_count[0]) h->er.last_pic = &h->ref_list[0][0];
3777  if (h->ref_count[1]) h->er.next_pic = &h->ref_list[1][0];
3778 
3779  if (h->avctx->debug & FF_DEBUG_PICT_INFO) {
3781  "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",
3782  h->slice_num,
3783  (h->picture_structure == PICT_FRAME ? "F" : h->picture_structure == PICT_TOP_FIELD ? "T" : "B"),
3784  first_mb_in_slice,
3786  h->slice_type_fixed ? " fix" : "",
3787  h->nal_unit_type == NAL_IDR_SLICE ? " IDR" : "",
3788  pps_id, h->frame_num,
3789  h->cur_pic_ptr->field_poc[0],
3790  h->cur_pic_ptr->field_poc[1],
3791  h->ref_count[0], h->ref_count[1],
3792  h->qscale,
3793  h->deblocking_filter,
3794  h->slice_alpha_c0_offset / 2 - 26, h->slice_beta_offset / 2 - 26,
3795  h->use_weight,
3796  h->use_weight == 1 && h->use_weight_chroma ? "c" : "",
3797  h->slice_type == AV_PICTURE_TYPE_B ? (h->direct_spatial_mv_pred ? "SPAT" : "TEMP") : "");
3798  }
3799 
3800  return 0;
3801 }
3802 
3804 {
3805  switch (h->slice_type) {
3806  case AV_PICTURE_TYPE_P:
3807  return 0;
3808  case AV_PICTURE_TYPE_B:
3809  return 1;
3810  case AV_PICTURE_TYPE_I:
3811  return 2;
3812  case AV_PICTURE_TYPE_SP:
3813  return 3;
3814  case AV_PICTURE_TYPE_SI:
3815  return 4;
3816  default:
3817  return -1;
3818  }
3819 }
3820 
3822  int mb_type, int top_xy,
3823  int left_xy[LEFT_MBS],
3824  int top_type,
3825  int left_type[LEFT_MBS],
3826  int mb_xy, int list)
3827 {
3828  int b_stride = h->b_stride;
3829  int16_t(*mv_dst)[2] = &h->mv_cache[list][scan8[0]];
3830  int8_t *ref_cache = &h->ref_cache[list][scan8[0]];
3831  if (IS_INTER(mb_type) || IS_DIRECT(mb_type)) {
3832  if (USES_LIST(top_type, list)) {
3833  const int b_xy = h->mb2b_xy[top_xy] + 3 * b_stride;
3834  const int b8_xy = 4 * top_xy + 2;
3835  int (*ref2frm)[64] = (void*)(h->ref2frm[h->slice_table[top_xy] & (MAX_SLICES - 1)][0] + (MB_MBAFF ? 20 : 2));
3836  AV_COPY128(mv_dst - 1 * 8, h->cur_pic.f.motion_val[list][b_xy + 0]);
3837  ref_cache[0 - 1 * 8] =
3838  ref_cache[1 - 1 * 8] = ref2frm[list][h->cur_pic.f.ref_index[list][b8_xy + 0]];
3839  ref_cache[2 - 1 * 8] =
3840  ref_cache[3 - 1 * 8] = ref2frm[list][h->cur_pic.f.ref_index[list][b8_xy + 1]];
3841  } else {
3842  AV_ZERO128(mv_dst - 1 * 8);
3843  AV_WN32A(&ref_cache[0 - 1 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
3844  }
3845 
3846  if (!IS_INTERLACED(mb_type ^ left_type[LTOP])) {
3847  if (USES_LIST(left_type[LTOP], list)) {
3848  const int b_xy = h->mb2b_xy[left_xy[LTOP]] + 3;
3849  const int b8_xy = 4 * left_xy[LTOP] + 1;
3850  int (*ref2frm)[64] =(void*)( h->ref2frm[h->slice_table[left_xy[LTOP]] & (MAX_SLICES - 1)][0] + (MB_MBAFF ? 20 : 2));
3851  AV_COPY32(mv_dst - 1 + 0, h->cur_pic.f.motion_val[list][b_xy + b_stride * 0]);
3852  AV_COPY32(mv_dst - 1 + 8, h->cur_pic.f.motion_val[list][b_xy + b_stride * 1]);
3853  AV_COPY32(mv_dst - 1 + 16, h->cur_pic.f.motion_val[list][b_xy + b_stride * 2]);
3854  AV_COPY32(mv_dst - 1 + 24, h->cur_pic.f.motion_val[list][b_xy + b_stride * 3]);
3855  ref_cache[-1 + 0] =
3856  ref_cache[-1 + 8] = ref2frm[list][h->cur_pic.f.ref_index[list][b8_xy + 2 * 0]];
3857  ref_cache[-1 + 16] =
3858  ref_cache[-1 + 24] = ref2frm[list][h->cur_pic.f.ref_index[list][b8_xy + 2 * 1]];
3859  } else {
3860  AV_ZERO32(mv_dst - 1 + 0);
3861  AV_ZERO32(mv_dst - 1 + 8);
3862  AV_ZERO32(mv_dst - 1 + 16);
3863  AV_ZERO32(mv_dst - 1 + 24);
3864  ref_cache[-1 + 0] =
3865  ref_cache[-1 + 8] =
3866  ref_cache[-1 + 16] =
3867  ref_cache[-1 + 24] = LIST_NOT_USED;
3868  }
3869  }
3870  }
3871 
3872  if (!USES_LIST(mb_type, list)) {
3873  fill_rectangle(mv_dst, 4, 4, 8, pack16to32(0, 0), 4);
3874  AV_WN32A(&ref_cache[0 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
3875  AV_WN32A(&ref_cache[1 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
3876  AV_WN32A(&ref_cache[2 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
3877  AV_WN32A(&ref_cache[3 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
3878  return;
3879  }
3880 
3881  {
3882  int8_t *ref = &h->cur_pic.f.ref_index[list][4 * mb_xy];
3883  int (*ref2frm)[64] = (void*)(h->ref2frm[h->slice_num & (MAX_SLICES - 1)][0] + (MB_MBAFF ? 20 : 2));
3884  uint32_t ref01 = (pack16to32(ref2frm[list][ref[0]], ref2frm[list][ref[1]]) & 0x00FF00FF) * 0x0101;
3885  uint32_t ref23 = (pack16to32(ref2frm[list][ref[2]], ref2frm[list][ref[3]]) & 0x00FF00FF) * 0x0101;
3886  AV_WN32A(&ref_cache[0 * 8], ref01);
3887  AV_WN32A(&ref_cache[1 * 8], ref01);
3888  AV_WN32A(&ref_cache[2 * 8], ref23);
3889  AV_WN32A(&ref_cache[3 * 8], ref23);
3890  }
3891 
3892  {
3893  int16_t(*mv_src)[2] = &h->cur_pic.f.motion_val[list][4 * h->mb_x + 4 * h->mb_y * b_stride];
3894  AV_COPY128(mv_dst + 8 * 0, mv_src + 0 * b_stride);
3895  AV_COPY128(mv_dst + 8 * 1, mv_src + 1 * b_stride);
3896  AV_COPY128(mv_dst + 8 * 2, mv_src + 2 * b_stride);
3897  AV_COPY128(mv_dst + 8 * 3, mv_src + 3 * b_stride);
3898  }
3899 }
3900 
3901 /**
3902  *
3903  * @return non zero if the loop filter can be skipped
3904  */
3905 static int fill_filter_caches(H264Context *h, int mb_type)
3906 {
3907  const int mb_xy = h->mb_xy;
3908  int top_xy, left_xy[LEFT_MBS];
3909  int top_type, left_type[LEFT_MBS];
3910  uint8_t *nnz;
3911  uint8_t *nnz_cache;
3912 
3913  top_xy = mb_xy - (h->mb_stride << MB_FIELD);
3914 
3915  /* Wow, what a mess, why didn't they simplify the interlacing & intra
3916  * stuff, I can't imagine that these complex rules are worth it. */
3917 
3918  left_xy[LBOT] = left_xy[LTOP] = mb_xy - 1;
3919  if (FRAME_MBAFF) {
3920  const int left_mb_field_flag = IS_INTERLACED(h->cur_pic.f.mb_type[mb_xy - 1]);
3921  const int curr_mb_field_flag = IS_INTERLACED(mb_type);
3922  if (h->mb_y & 1) {
3923  if (left_mb_field_flag != curr_mb_field_flag)
3924  left_xy[LTOP] -= h->mb_stride;
3925  } else {
3926  if (curr_mb_field_flag)
3927  top_xy += h->mb_stride &
3928  (((h->cur_pic.f.mb_type[top_xy] >> 7) & 1) - 1);
3929  if (left_mb_field_flag != curr_mb_field_flag)
3930  left_xy[LBOT] += h->mb_stride;
3931  }
3932  }
3933 
3934  h->top_mb_xy = top_xy;
3935  h->left_mb_xy[LTOP] = left_xy[LTOP];
3936  h->left_mb_xy[LBOT] = left_xy[LBOT];
3937  {
3938  /* For sufficiently low qp, filtering wouldn't do anything.
3939  * This is a conservative estimate: could also check beta_offset
3940  * and more accurate chroma_qp. */
3941  int qp_thresh = h->qp_thresh; // FIXME strictly we should store qp_thresh for each mb of a slice
3942  int qp = h->cur_pic.f.qscale_table[mb_xy];
3943  if (qp <= qp_thresh &&
3944  (left_xy[LTOP] < 0 ||
3945  ((qp + h->cur_pic.f.qscale_table[left_xy[LTOP]] + 1) >> 1) <= qp_thresh) &&
3946  (top_xy < 0 ||
3947  ((qp + h->cur_pic.f.qscale_table[top_xy] + 1) >> 1) <= qp_thresh)) {
3948  if (!FRAME_MBAFF)
3949  return 1;
3950  if ((left_xy[LTOP] < 0 ||
3951  ((qp + h->cur_pic.f.qscale_table[left_xy[LBOT]] + 1) >> 1) <= qp_thresh) &&
3952  (top_xy < h->mb_stride ||
3953  ((qp + h->cur_pic.f.qscale_table[top_xy - h->mb_stride] + 1) >> 1) <= qp_thresh))
3954  return 1;
3955  }
3956  }
3957 
3958  top_type = h->cur_pic.f.mb_type[top_xy];
3959  left_type[LTOP] = h->cur_pic.f.mb_type[left_xy[LTOP]];
3960  left_type[LBOT] = h->cur_pic.f.mb_type[left_xy[LBOT]];
3961  if (h->deblocking_filter == 2) {
3962  if (h->slice_table[top_xy] != h->slice_num)
3963  top_type = 0;
3964  if (h->slice_table[left_xy[LBOT]] != h->slice_num)
3965  left_type[LTOP] = left_type[LBOT] = 0;
3966  } else {
3967  if (h->slice_table[top_xy] == 0xFFFF)
3968  top_type = 0;
3969  if (h->slice_table[left_xy[LBOT]] == 0xFFFF)
3970  left_type[LTOP] = left_type[LBOT] = 0;
3971  }
3972  h->top_type = top_type;
3973  h->left_type[LTOP] = left_type[LTOP];
3974  h->left_type[LBOT] = left_type[LBOT];
3975 
3976  if (IS_INTRA(mb_type))
3977  return 0;
3978 
3979  fill_filter_caches_inter(h, mb_type, top_xy, left_xy,
3980  top_type, left_type, mb_xy, 0);
3981  if (h->list_count == 2)
3982  fill_filter_caches_inter(h, mb_type, top_xy, left_xy,
3983  top_type, left_type, mb_xy, 1);
3984 
3985  nnz = h->non_zero_count[mb_xy];
3986  nnz_cache = h->non_zero_count_cache;
3987  AV_COPY32(&nnz_cache[4 + 8 * 1], &nnz[0]);
3988  AV_COPY32(&nnz_cache[4 + 8 * 2], &nnz[4]);
3989  AV_COPY32(&nnz_cache[4 + 8 * 3], &nnz[8]);
3990  AV_COPY32(&nnz_cache[4 + 8 * 4], &nnz[12]);
3991  h->cbp = h->cbp_table[mb_xy];
3992 
3993  if (top_type) {
3994  nnz = h->non_zero_count[top_xy];
3995  AV_COPY32(&nnz_cache[4 + 8 * 0], &nnz[3 * 4]);
3996  }
3997 
3998  if (left_type[LTOP]) {
3999  nnz = h->non_zero_count[left_xy[LTOP]];
4000  nnz_cache[3 + 8 * 1] = nnz[3 + 0 * 4];
4001  nnz_cache[3 + 8 * 2] = nnz[3 + 1 * 4];
4002  nnz_cache[3 + 8 * 3] = nnz[3 + 2 * 4];
4003  nnz_cache[3 + 8 * 4] = nnz[3 + 3 * 4];
4004  }
4005 
4006  /* CAVLC 8x8dct requires NNZ values for residual decoding that differ
4007  * from what the loop filter needs */
4008  if (!CABAC && h->pps.transform_8x8_mode) {
4009  if (IS_8x8DCT(top_type)) {
4010  nnz_cache[4 + 8 * 0] =
4011  nnz_cache[5 + 8 * 0] = (h->cbp_table[top_xy] & 0x4000) >> 12;
4012  nnz_cache[6 + 8 * 0] =
4013  nnz_cache[7 + 8 * 0] = (h->cbp_table[top_xy] & 0x8000) >> 12;
4014  }
4015  if (IS_8x8DCT(left_type[LTOP])) {
4016  nnz_cache[3 + 8 * 1] =
4017  nnz_cache[3 + 8 * 2] = (h->cbp_table[left_xy[LTOP]] & 0x2000) >> 12; // FIXME check MBAFF
4018  }
4019  if (IS_8x8DCT(left_type[LBOT])) {
4020  nnz_cache[3 + 8 * 3] =
4021  nnz_cache[3 + 8 * 4] = (h->cbp_table[left_xy[LBOT]] & 0x8000) >> 12; // FIXME check MBAFF
4022  }
4023 
4024  if (IS_8x8DCT(mb_type)) {
4025  nnz_cache[scan8[0]] =
4026  nnz_cache[scan8[1]] =
4027  nnz_cache[scan8[2]] =
4028  nnz_cache[scan8[3]] = (h->cbp & 0x1000) >> 12;
4029 
4030  nnz_cache[scan8[0 + 4]] =
4031  nnz_cache[scan8[1 + 4]] =
4032  nnz_cache[scan8[2 + 4]] =
4033  nnz_cache[scan8[3 + 4]] = (h->cbp & 0x2000) >> 12;
4034 
4035  nnz_cache[scan8[0 + 8]] =
4036  nnz_cache[scan8[1 + 8]] =
4037  nnz_cache[scan8[2 + 8]] =
4038  nnz_cache[scan8[3 + 8]] = (h->cbp & 0x4000) >> 12;
4039 
4040  nnz_cache[scan8[0 + 12]] =
4041  nnz_cache[scan8[1 + 12]] =
4042  nnz_cache[scan8[2 + 12]] =
4043  nnz_cache[scan8[3 + 12]] = (h->cbp & 0x8000) >> 12;
4044  }
4045  }
4046 
4047  return 0;
4048 }
4049 
4050 static void loop_filter(H264Context *h, int start_x, int end_x)
4051 {
4052  uint8_t *dest_y, *dest_cb, *dest_cr;
4053  int linesize, uvlinesize, mb_x, mb_y;
4054  const int end_mb_y = h->mb_y + FRAME_MBAFF;
4055  const int old_slice_type = h->slice_type;
4056  const int pixel_shift = h->pixel_shift;
4057  const int block_h = 16 >> h->chroma_y_shift;
4058 
4059  if (h->deblocking_filter) {
4060  for (mb_x = start_x; mb_x < end_x; mb_x++)
4061  for (mb_y = end_mb_y - FRAME_MBAFF; mb_y <= end_mb_y; mb_y++) {
4062  int mb_xy, mb_type;
4063  mb_xy = h->mb_xy = mb_x + mb_y * h->mb_stride;
4064  h->slice_num = h->slice_table[mb_xy];
4065  mb_type = h->cur_pic.f.mb_type[mb_xy];
4066  h->list_count = h->list_counts[mb_xy];
4067 
4068  if (FRAME_MBAFF)
4069  h->mb_mbaff =
4070  h->mb_field_decoding_flag = !!IS_INTERLACED(mb_type);
4071 
4072  h->mb_x = mb_x;
4073  h->mb_y = mb_y;
4074  dest_y = h->cur_pic.f.data[0] +
4075  ((mb_x << pixel_shift) + mb_y * h->linesize) * 16;
4076  dest_cb = h->cur_pic.f.data[1] +
4077  (mb_x << pixel_shift) * (8 << CHROMA444) +
4078  mb_y * h->uvlinesize * block_h;
4079  dest_cr = h->cur_pic.f.data[2] +
4080  (mb_x << pixel_shift) * (8 << CHROMA444) +
4081  mb_y * h->uvlinesize * block_h;
4082  // FIXME simplify above
4083 
4084  if (MB_FIELD) {
4085  linesize = h->mb_linesize = h->linesize * 2;
4086  uvlinesize = h->mb_uvlinesize = h->uvlinesize * 2;
4087  if (mb_y & 1) { // FIXME move out of this function?
4088  dest_y -= h->linesize * 15;
4089  dest_cb -= h->uvlinesize * (block_h - 1);
4090  dest_cr -= h->uvlinesize * (block_h - 1);
4091  }
4092  } else {
4093  linesize = h->mb_linesize = h->linesize;
4094  uvlinesize = h->mb_uvlinesize = h->uvlinesize;
4095  }
4096  backup_mb_border(h, dest_y, dest_cb, dest_cr, linesize,
4097  uvlinesize, 0);
4098  if (fill_filter_caches(h, mb_type))
4099  continue;
4100  h->chroma_qp[0] = get_chroma_qp(h, 0, h->cur_pic.f.qscale_table[mb_xy]);
4101  h->chroma_qp[1] = get_chroma_qp(h, 1, h->cur_pic.f.qscale_table[mb_xy]);
4102 
4103  if (FRAME_MBAFF) {
4104  ff_h264_filter_mb(h, mb_x, mb_y, dest_y, dest_cb, dest_cr,
4105  linesize, uvlinesize);
4106  } else {
4107  ff_h264_filter_mb_fast(h, mb_x, mb_y, dest_y, dest_cb,
4108  dest_cr, linesize, uvlinesize);
4109  }
4110  }
4111  }
4112  h->slice_type = old_slice_type;
4113  h->mb_x = end_x;
4114  h->mb_y = end_mb_y - FRAME_MBAFF;
4115  h->chroma_qp[0] = get_chroma_qp(h, 0, h->qscale);
4116  h->chroma_qp[1] = get_chroma_qp(h, 1, h->qscale);
4117 }
4118 
4120 {
4121  const int mb_xy = h->mb_x + h->mb_y * h->mb_stride;
4122  int mb_type = (h->slice_table[mb_xy - 1] == h->slice_num) ?
4123  h->cur_pic.f.mb_type[mb_xy - 1] :
4124  (h->slice_table[mb_xy - h->mb_stride] == h->slice_num) ?
4125  h->cur_pic.f.mb_type[mb_xy - h->mb_stride] : 0;
4126  h->mb_mbaff = h->mb_field_decoding_flag = IS_INTERLACED(mb_type) ? 1 : 0;
4127 }
4128 
4129 /**
4130  * Draw edges and report progress for the last MB row.
4131  */
4133 {
4134  int top = 16 * (h->mb_y >> FIELD_PICTURE);
4135  int pic_height = 16 * h->mb_height >> FIELD_PICTURE;
4136  int height = 16 << FRAME_MBAFF;
4137  int deblock_border = (16 + 4) << FRAME_MBAFF;
4138 
4139  if (h->deblocking_filter) {
4140  if ((top + height) >= pic_height)
4141  height += deblock_border;
4142  top -= deblock_border;
4143  }
4144 
4145  if (top >= pic_height || (top + height) < 0)
4146  return;
4147 
4148  height = FFMIN(height, pic_height - top);
4149  if (top < 0) {
4150  height = top + height;
4151  top = 0;
4152  }
4153 
4154  ff_h264_draw_horiz_band(h, top, height);
4155 
4156  if (h->droppable)
4157  return;
4158 
4159  ff_thread_report_progress(&h->cur_pic_ptr->f, top + height - 1,
4161 }
4162 
4163 static void er_add_slice(H264Context *h, int startx, int starty,
4164  int endx, int endy, int status)
4165 {
4166  if (CONFIG_ERROR_RESILIENCE) {
4167  ERContext *er = &h->er;
4168 
4169  er->ref_count = h->ref_count[0];
4170  ff_er_add_slice(er, startx, starty, endx, endy, status);
4171  }
4172 }
4173 
4174 static int decode_slice(struct AVCodecContext *avctx, void *arg)
4175 {
4176  H264Context *h = *(void **)arg;
4177  int lf_x_start = h->mb_x;
4178 
4179  h->mb_skip_run = -1;
4180 
4181  av_assert0(h->block_offset[15] == (4 * ((scan8[15] - scan8[0]) & 7) << h->pixel_shift) + 4 * h->linesize * ((scan8[15] - scan8[0]) >> 3));
4182 
4184  avctx->codec_id != AV_CODEC_ID_H264 ||
4185  (CONFIG_GRAY && (h->flags & CODEC_FLAG_GRAY));
4186 
4187  if (h->pps.cabac) {
4188  /* realign */
4189  align_get_bits(&h->gb);
4190 
4191  /* init cabac */
4193  h->gb.buffer + get_bits_count(&h->gb) / 8,
4194  (get_bits_left(&h->gb) + 7) / 8);
4195 
4197 
4198  for (;;) {
4199  // START_TIMER
4200  int ret = ff_h264_decode_mb_cabac(h);
4201  int eos;
4202  // STOP_TIMER("decode_mb_cabac")
4203 
4204  if (ret >= 0)
4206 
4207  // FIXME optimal? or let mb_decode decode 16x32 ?
4208  if (ret >= 0 && FRAME_MBAFF) {
4209  h->mb_y++;
4210 
4211  ret = ff_h264_decode_mb_cabac(h);
4212 
4213  if (ret >= 0)
4215  h->mb_y--;
4216  }
4217  eos = get_cabac_terminate(&h->cabac);
4218 
4219  if ((h->workaround_bugs & FF_BUG_TRUNCATED) &&
4220  h->cabac.bytestream > h->cabac.bytestream_end + 2) {
4221  er_add_slice(h, h->resync_mb_x, h->resync_mb_y, h->mb_x - 1,
4222  h->mb_y, ER_MB_END);
4223  if (h->mb_x >= lf_x_start)
4224  loop_filter(h, lf_x_start, h->mb_x + 1);
4225  return 0;
4226  }
4227  if (h->cabac.bytestream > h->cabac.bytestream_end + 2 )
4228  av_log(h->avctx, AV_LOG_DEBUG, "bytestream overread %td\n", h->cabac.bytestream_end - h->cabac.bytestream);
4229  if (ret < 0 || h->cabac.bytestream > h->cabac.bytestream_end + 4) {
4231  "error while decoding MB %d %d, bytestream (%td)\n",
4232  h->mb_x, h->mb_y,
4234  er_add_slice(h, h->resync_mb_x, h->resync_mb_y, h->mb_x,
4235  h->mb_y, ER_MB_ERROR);
4236  return -1;
4237  }
4238 
4239  if (++h->mb_x >= h->mb_width) {
4240  loop_filter(h, lf_x_start, h->mb_x);
4241  h->mb_x = lf_x_start = 0;
4242  decode_finish_row(h);
4243  ++h->mb_y;
4244  if (FIELD_OR_MBAFF_PICTURE) {
4245  ++h->mb_y;
4246  if (FRAME_MBAFF && h->mb_y < h->mb_height)
4248  }
4249  }
4250 
4251  if (eos || h->mb_y >= h->mb_height) {
4252  tprintf(h->avctx, "slice end %d %d\n",
4253  get_bits_count(&h->gb), h->gb.size_in_bits);
4254  er_add_slice(h, h->resync_mb_x, h->resync_mb_y, h->mb_x - 1,
4255  h->mb_y, ER_MB_END);
4256  if (h->mb_x > lf_x_start)
4257  loop_filter(h, lf_x_start, h->mb_x);
4258  return 0;
4259  }
4260  }
4261  } else {
4262  for (;;) {
4263  int ret = ff_h264_decode_mb_cavlc(h);
4264 
4265  if (ret >= 0)
4267 
4268  // FIXME optimal? or let mb_decode decode 16x32 ?
4269  if (ret >= 0 && FRAME_MBAFF) {
4270  h->mb_y++;
4271  ret = ff_h264_decode_mb_cavlc(h);
4272 
4273  if (ret >= 0)
4275  h->mb_y--;
4276  }
4277 
4278  if (ret < 0) {
4280  "error while decoding MB %d %d\n", h->mb_x, h->mb_y);
4281  er_add_slice(h, h->resync_mb_x, h->resync_mb_y, h->mb_x,
4282  h->mb_y, ER_MB_ERROR);
4283  return -1;
4284  }
4285 
4286  if (++h->mb_x >= h->mb_width) {
4287  loop_filter(h, lf_x_start, h->mb_x);
4288  h->mb_x = lf_x_start = 0;
4289  decode_finish_row(h);
4290  ++h->mb_y;
4291  if (FIELD_OR_MBAFF_PICTURE) {
4292  ++h->mb_y;
4293  if (FRAME_MBAFF && h->mb_y < h->mb_height)
4295  }
4296  if (h->mb_y >= h->mb_height) {
4297  tprintf(h->avctx, "slice end %d %d\n",
4298  get_bits_count(&h->gb), h->gb.size_in_bits);
4299 
4300  if ( get_bits_left(&h->gb) == 0
4301  || get_bits_left(&h->gb) > 0 && !(h->avctx->err_recognition & AV_EF_AGGRESSIVE)) {
4303  h->mb_x - 1, h->mb_y,
4304  ER_MB_END);
4305 
4306  return 0;
4307  } else {
4309  h->mb_x, h->mb_y,
4310  ER_MB_END);
4311 
4312  return -1;
4313  }
4314  }
4315  }
4316 
4317  if (get_bits_left(&h->gb) <= 0 && h->mb_skip_run <= 0) {
4318  tprintf(h->avctx, "slice end %d %d\n",
4319  get_bits_count(&h->gb), h->gb.size_in_bits);
4320  if (get_bits_left(&h->gb) == 0) {
4322  h->mb_x - 1, h->mb_y,
4323  ER_MB_END);
4324  if (h->mb_x > lf_x_start)
4325  loop_filter(h, lf_x_start, h->mb_x);
4326 
4327  return 0;
4328  } else {
4329  er_add_slice(h, h->resync_mb_x, h->resync_mb_y, h->mb_x,
4330  h->mb_y, ER_MB_ERROR);
4331 
4332  return -1;
4333  }
4334  }
4335  }
4336  }
4337 }
4338 
4339 /**
4340  * Call decode_slice() for each context.
4341  *
4342  * @param h h264 master context
4343  * @param context_count number of contexts to execute
4344  */
4345 static int execute_decode_slices(H264Context *h, int context_count)
4346 {
4347  AVCodecContext *const avctx = h->avctx;
4348  H264Context *hx;
4349  int i;
4350 
4351  if (h->avctx->hwaccel ||
4353  return 0;
4354  if (context_count == 1) {
4355  return decode_slice(avctx, &h);
4356  } else {
4357  av_assert0(context_count > 0);
4358  for (i = 1; i < context_count; i++) {
4359  hx = h->thread_context[i];
4360  if (CONFIG_ERROR_RESILIENCE) {
4361  hx->er.error_count = 0;
4362  }
4363  hx->x264_build = h->x264_build;
4364  }
4365 
4366  avctx->execute(avctx, decode_slice, h->thread_context,
4367  NULL, context_count, sizeof(void *));
4368 
4369  /* pull back stuff from slices to master context */
4370  hx = h->thread_context[context_count - 1];
4371  h->mb_x = hx->mb_x;
4372  h->mb_y = hx->mb_y;
4373  h->droppable = hx->droppable;
4375  if (CONFIG_ERROR_RESILIENCE) {
4376  for (i = 1; i < context_count; i++)
4378  }
4379  }
4380 
4381  return 0;
4382 }
4383 
4384 static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size,
4385  int parse_extradata)
4386 {
4387  AVCodecContext *const avctx = h->avctx;
4388  H264Context *hx; ///< thread context
4389  int buf_index;
4390  int context_count;
4391  int next_avc;
4392  int pass = !(avctx->active_thread_type & FF_THREAD_FRAME);
4393  int nals_needed = 0; ///< number of NALs that need decoding before the next frame thread starts
4394  int nal_index;
4395  int idr_cleared=0;
4396  int first_slice = 0;
4397 
4398  h->nal_unit_type= 0;
4399 
4400  if(!h->slice_context_count)
4401  h->slice_context_count= 1;
4403  if (!(avctx->flags2 & CODEC_FLAG2_CHUNKS)) {
4404  h->current_slice = 0;
4405  if (!h->first_field)
4406  h->cur_pic_ptr = NULL;
4407  ff_h264_reset_sei(h);
4408  }
4409 
4410  if (h->nal_length_size == 4) {
4411  if (buf_size > 8 && AV_RB32(buf) == 1 && AV_RB32(buf+5) > (unsigned)buf_size) {
4412  h->is_avc = 0;
4413  }else if(buf_size > 3 && AV_RB32(buf) > 1 && AV_RB32(buf) <= (unsigned)buf_size)
4414  h->is_avc = 1;
4415  }
4416 
4417  for (; pass <= 1; pass++) {
4418  buf_index = 0;
4419  context_count = 0;
4420  next_avc = h->is_avc ? 0 : buf_size;
4421  nal_index = 0;
4422  for (;;) {
4423  int consumed;
4424  int dst_length;
4425  int bit_length;
4426  const uint8_t *ptr;
4427  int i, nalsize = 0;
4428  int err;
4429 
4430  if (buf_index >= next_avc) {
4431  if (buf_index >= buf_size - h->nal_length_size)
4432  break;
4433  nalsize = 0;
4434  for (i = 0; i < h->nal_length_size; i++)
4435  nalsize = (nalsize << 8) | buf[buf_index++];
4436  if (nalsize <= 0 || nalsize > buf_size - buf_index) {
4438  "AVC: nal size %d\n", nalsize);
4439  break;
4440  }
4441  next_avc = buf_index + nalsize;
4442  } else {
4443  // start code prefix search
4444  for (; buf_index + 3 < next_avc; buf_index++)
4445  // This should always succeed in the first iteration.
4446  if (buf[buf_index] == 0 &&
4447  buf[buf_index + 1] == 0 &&
4448  buf[buf_index + 2] == 1)
4449  break;
4450 
4451  if (buf_index + 3 >= buf_size) {
4452  buf_index = buf_size;
4453  break;
4454  }
4455 
4456  buf_index += 3;
4457  if (buf_index >= next_avc)
4458  continue;
4459  }
4460 
4461  hx = h->thread_context[context_count];
4462 
4463  ptr = ff_h264_decode_nal(hx, buf + buf_index, &dst_length,
4464  &consumed, next_avc - buf_index);
4465  if (ptr == NULL || dst_length < 0) {
4466  buf_index = -1;
4467  goto end;
4468  }
4469  i = buf_index + consumed;
4470  if ((h->workaround_bugs & FF_BUG_AUTODETECT) && i + 3 < next_avc &&
4471  buf[i] == 0x00 && buf[i + 1] == 0x00 &&
4472  buf[i + 2] == 0x01 && buf[i + 3] == 0xE0)
4474 
4475  if (!(h->workaround_bugs & FF_BUG_TRUNCATED))
4476  while(dst_length > 0 && ptr[dst_length - 1] == 0)
4477  dst_length--;
4478  bit_length = !dst_length ? 0
4479  : (8 * dst_length -
4480  decode_rbsp_trailing(h, ptr + dst_length - 1));
4481 
4482  if (h->avctx->debug & FF_DEBUG_STARTCODE)
4483  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);
4484 
4485  if (h->is_avc && (nalsize != consumed) && nalsize)
4487  "AVC: Consumed only %d bytes instead of %d\n",
4488  consumed, nalsize);
4489 
4490  buf_index += consumed;
4491  nal_index++;
4492 
4493  if (pass == 0) {
4494  /* packets can sometimes contain multiple PPS/SPS,
4495  * e.g. two PAFF field pictures in one packet, or a demuxer
4496  * which splits NALs strangely if so, when frame threading we
4497  * can't start the next thread until we've read all of them */
4498  switch (hx->nal_unit_type) {
4499  case NAL_SPS:
4500  case NAL_PPS:
4501  nals_needed = nal_index;
4502  break;
4503  case NAL_DPA:
4504  case NAL_IDR_SLICE:
4505  case NAL_SLICE:
4506  init_get_bits(&hx->gb, ptr, bit_length);
4507  if (!get_ue_golomb(&hx->gb) || !first_slice)
4508  nals_needed = nal_index;
4509  if (!first_slice)
4510  first_slice = hx->nal_unit_type;
4511  }
4512  continue;
4513  }
4514 
4515  if (!first_slice)
4516  switch (hx->nal_unit_type) {
4517  case NAL_DPA:
4518  case NAL_IDR_SLICE:
4519  case NAL_SLICE:
4520  first_slice = hx->nal_unit_type;
4521  }
4522 
4523  // FIXME do not discard SEI id
4524  if (avctx->skip_frame >= AVDISCARD_NONREF && h->nal_ref_idc == 0)
4525  continue;
4526 
4527 again:
4528  /* Ignore per frame NAL unit type during extradata
4529  * parsing. Decoding slices is not possible in codec init
4530  * with frame-mt */
4531  if (parse_extradata) {
4532  switch (hx->nal_unit_type) {
4533  case NAL_IDR_SLICE:
4534  case NAL_SLICE:
4535  case NAL_DPA:
4536  case NAL_DPB:
4537  case NAL_DPC:
4538  case NAL_AUXILIARY_SLICE:
4539  av_log(h->avctx, AV_LOG_WARNING, "Ignoring NAL %d in global header/extradata\n", hx->nal_unit_type);
4541  }
4542  }
4543 
4544  err = 0;
4545 
4546  switch (hx->nal_unit_type) {
4547  case NAL_IDR_SLICE:
4548  if (first_slice != NAL_IDR_SLICE) {
4550  "Invalid mix of idr and non-idr slices\n");
4551  buf_index = -1;
4552  goto end;
4553  }
4554  if(!idr_cleared)
4555  idr(h); // FIXME ensure we don't lose some frames if there is reordering
4556  idr_cleared = 1;
4557  case NAL_SLICE:
4558  init_get_bits(&hx->gb, ptr, bit_length);
4559  hx->intra_gb_ptr =
4560  hx->inter_gb_ptr = &hx->gb;
4561  hx->data_partitioning = 0;
4562 
4563  if ((err = decode_slice_header(hx, h)))
4564  break;
4565 
4567  h->valid_recovery_point = 1;
4568 
4569  if ( h->sei_recovery_frame_cnt >= 0
4570  && ( h->recovery_frame<0
4571  || ((h->recovery_frame - h->frame_num) & ((1 << h->sps.log2_max_frame_num)-1)) > h->sei_recovery_frame_cnt)) {
4573  (1 << h->sps.log2_max_frame_num);
4574 
4575  if (!h->valid_recovery_point)
4576  h->recovery_frame = h->frame_num;
4577  }
4578 
4579  h->cur_pic_ptr->f.key_frame |=
4580  (hx->nal_unit_type == NAL_IDR_SLICE);
4581 
4582  if (h->recovery_frame == h->frame_num) {
4583  h->cur_pic_ptr->sync |= 1;
4584  h->recovery_frame = -1;
4585  }
4586 
4587  h->sync |= !!h->cur_pic_ptr->f.key_frame;
4588  h->sync |= 3*!!(avctx->flags2 & CODEC_FLAG2_SHOW_ALL);
4589  h->cur_pic_ptr->sync |= h->sync;
4590 
4591  if (h->current_slice == 1) {
4592  if (!(avctx->flags2 & CODEC_FLAG2_CHUNKS))
4593  decode_postinit(h, nal_index >= nals_needed);
4594 
4595  if (h->avctx->hwaccel &&
4596  h->avctx->hwaccel->start_frame(h->avctx, NULL, 0) < 0)
4597  return -1;
4598  if (CONFIG_H264_VDPAU_DECODER &&
4601  }
4602 
4603  if (hx->redundant_pic_count == 0 &&
4604  (avctx->skip_frame < AVDISCARD_NONREF ||
4605  hx->nal_ref_idc) &&
4606  (avctx->skip_frame < AVDISCARD_BIDIR ||
4608  (avctx->skip_frame < AVDISCARD_NONKEY ||
4610  avctx->skip_frame < AVDISCARD_ALL) {
4611  if (avctx->hwaccel) {
4612  if (avctx->hwaccel->decode_slice(avctx,
4613  &buf[buf_index - consumed],
4614  consumed) < 0)
4615  return -1;
4616  } else if (CONFIG_H264_VDPAU_DECODER &&
4618  static const uint8_t start_code[] = {
4619  0x00, 0x00, 0x01 };
4620  ff_vdpau_add_data_chunk(h->cur_pic_ptr->f.data[0], start_code,
4621  sizeof(start_code));
4622  ff_vdpau_add_data_chunk(h->cur_pic_ptr->f.data[0], &buf[buf_index - consumed],
4623  consumed);
4624  } else
4625  context_count++;
4626  }
4627  break;
4628  case NAL_DPA:
4629  init_get_bits(&hx->gb, ptr, bit_length);
4630  hx->intra_gb_ptr =
4631  hx->inter_gb_ptr = NULL;
4632 
4633  if ((err = decode_slice_header(hx, h)) < 0)
4634  break;
4635 
4636  hx->data_partitioning = 1;
4637  break;
4638  case NAL_DPB:
4639  init_get_bits(&hx->intra_gb, ptr, bit_length);
4640  hx->intra_gb_ptr = &hx->intra_gb;
4641  break;
4642  case NAL_DPC:
4643  init_get_bits(&hx->inter_gb, ptr, bit_length);
4644  hx->inter_gb_ptr = &hx->inter_gb;
4645 
4646  av_log(h->avctx, AV_LOG_ERROR, "Partitioned H.264 support is incomplete\n");
4647  break;
4648 
4649  if (hx->redundant_pic_count == 0 &&
4650  hx->intra_gb_ptr &&
4651  hx->data_partitioning &&
4652  h->cur_pic_ptr && h->context_initialized &&
4653  (avctx->skip_frame < AVDISCARD_NONREF || hx->nal_ref_idc) &&
4654  (avctx->skip_frame < AVDISCARD_BIDIR ||
4656  (avctx->skip_frame < AVDISCARD_NONKEY ||
4658  avctx->skip_frame < AVDISCARD_ALL)
4659  context_count++;
4660  break;
4661  case NAL_SEI:
4662  init_get_bits(&h->gb, ptr, bit_length);
4663  ff_h264_decode_sei(h);
4664  break;
4665  case NAL_SPS:
4666  init_get_bits(&h->gb, ptr, bit_length);
4667  if (ff_h264_decode_seq_parameter_set(h) < 0 && (h->is_avc ? (nalsize != consumed) && nalsize : 1)) {
4669  "SPS decoding failure, trying again with the complete NAL\n");
4670  if (h->is_avc)
4671  av_assert0(next_avc - buf_index + consumed == nalsize);
4672  if ((next_avc - buf_index + consumed - 1) >= INT_MAX/8)
4673  break;
4674  init_get_bits(&h->gb, &buf[buf_index + 1 - consumed],
4675  8*(next_avc - buf_index + consumed - 1));
4677  }
4678 
4679  break;
4680  case NAL_PPS:
4681  init_get_bits(&h->gb, ptr, bit_length);
4682  ff_h264_decode_picture_parameter_set(h, bit_length);
4683  break;
4684  case NAL_AUD:
4685  case NAL_END_SEQUENCE:
4686  case NAL_END_STREAM:
4687  case NAL_FILLER_DATA:
4688  case NAL_SPS_EXT:
4689  case NAL_AUXILIARY_SLICE:
4690  break;
4691  case NAL_FF_IGNORE:
4692  break;
4693  default:
4694  av_log(avctx, AV_LOG_DEBUG, "Unknown NAL code: %d (%d bits)\n",
4695  hx->nal_unit_type, bit_length);
4696  }
4697 
4698  if (context_count == h->max_contexts) {
4699  execute_decode_slices(h, context_count);
4700  context_count = 0;
4701  }
4702 
4703  if (err < 0)
4704  av_log(h->avctx, AV_LOG_ERROR, "decode_slice_header error\n");
4705  else if (err == 1) {
4706  /* Slice could not be decoded in parallel mode, copy down
4707  * NAL unit stuff to context 0 and restart. Note that
4708  * rbsp_buffer is not transferred, but since we no longer
4709  * run in parallel mode this should not be an issue. */
4710  h->nal_unit_type = hx->nal_unit_type;
4711  h->nal_ref_idc = hx->nal_ref_idc;
4712  hx = h;
4713  goto again;
4714  }
4715  }
4716  }
4717  if (context_count)
4718  execute_decode_slices(h, context_count);
4719 
4720 end:
4721  /* clean up */
4722  if (h->cur_pic_ptr && h->cur_pic_ptr->owner2 == h &&
4723  !h->droppable) {
4724  ff_thread_report_progress(&h->cur_pic_ptr->f, INT_MAX,
4726  }
4727 
4728  return buf_index;
4729 }
4730 
4731 /**
4732  * Return the number of bytes consumed for building the current frame.
4733  */
4734 static int get_consumed_bytes(int pos, int buf_size)
4735 {
4736  if (pos == 0)
4737  pos = 1; // avoid infinite loops (i doubt that is needed but ...)
4738  if (pos + 10 > buf_size)
4739  pos = buf_size; // oops ;)
4740 
4741  return pos;
4742 }
4743 
4744 static int decode_frame(AVCodecContext *avctx, void *data,
4745  int *got_frame, AVPacket *avpkt)
4746 {
4747  const uint8_t *buf = avpkt->data;
4748  int buf_size = avpkt->size;
4749  H264Context *h = avctx->priv_data;
4750  AVFrame *pict = data;
4751  int buf_index = 0;
4752  Picture *out;
4753  int i, out_idx;
4754 
4755  h->flags = avctx->flags;
4756 
4757  /* end of stream, output what is still in the buffers */
4758  if (buf_size == 0) {
4759  out:
4760 
4761  h->cur_pic_ptr = NULL;
4762  h->first_field = 0;
4763 
4764  // FIXME factorize this with the output code below
4765  out = h->delayed_pic[0];
4766  out_idx = 0;
4767  for (i = 1;
4768  h->delayed_pic[i] &&
4769  !h->delayed_pic[i]->f.key_frame &&
4770  !h->delayed_pic[i]->mmco_reset;
4771  i++)
4772  if (h->delayed_pic[i]->poc < out->poc) {
4773  out = h->delayed_pic[i];
4774  out_idx = i;
4775  }
4776 
4777  for (i = out_idx; h->delayed_pic[i]; i++)
4778  h->delayed_pic[i] = h->delayed_pic[i + 1];
4779 
4780  if (out) {
4781  out->f.reference &= ~DELAYED_PIC_REF;
4782  *got_frame = 1;
4783  *pict = out->f;
4784  }
4785 
4786  return buf_index;
4787  }
4788  if(h->is_avc && buf_size >= 9 && buf[0]==1 && buf[2]==0 && (buf[4]&0xFC)==0xFC && (buf[5]&0x1F) && buf[8]==0x67){
4789  int cnt= buf[5]&0x1f;
4790  const uint8_t *p= buf+6;
4791  while(cnt--){
4792  int nalsize= AV_RB16(p) + 2;
4793  if(nalsize > buf_size - (p-buf) || p[2]!=0x67)
4794  goto not_extra;
4795  p += nalsize;
4796  }
4797  cnt = *(p++);
4798  if(!cnt)
4799  goto not_extra;
4800  while(cnt--){
4801  int nalsize= AV_RB16(p) + 2;
4802  if(nalsize > buf_size - (p-buf) || p[2]!=0x68)
4803  goto not_extra;
4804  p += nalsize;
4805  }
4806 
4807  return ff_h264_decode_extradata(h, buf, buf_size);
4808  }
4809 not_extra:
4810 
4811  buf_index = decode_nal_units(h, buf, buf_size, 0);
4812  if (buf_index < 0)
4813  return -1;
4814 
4815  if (!h->cur_pic_ptr && h->nal_unit_type == NAL_END_SEQUENCE) {
4816  av_assert0(buf_index <= buf_size);
4817  goto out;
4818  }
4819 
4820  if (!(avctx->flags2 & CODEC_FLAG2_CHUNKS) && !h->cur_pic_ptr) {
4821  if (avctx->skip_frame >= AVDISCARD_NONREF ||
4822  buf_size >= 4 && !memcmp("Q264", buf, 4))
4823  return buf_size;
4824  av_log(avctx, AV_LOG_ERROR, "no frame!\n");
4825  return -1;
4826  }
4827 
4828  if (!(avctx->flags2 & CODEC_FLAG2_CHUNKS) ||
4829  (h->mb_y >= h->mb_height && h->mb_height)) {
4830  if (avctx->flags2 & CODEC_FLAG2_CHUNKS)
4831  decode_postinit(h, 1);
4832 
4833  field_end(h, 0);
4834 
4835  /* Wait for second field. */
4836  *got_frame = 0;
4837  if (h->next_output_pic && (h->next_output_pic->sync || h->sync>1)) {
4838  *got_frame = 1;
4839  *pict = h->next_output_pic->f;
4840  }
4841  }
4842 
4843  assert(pict->data[0] || !*got_frame);
4844 
4845  if (CONFIG_MPEGVIDEO) {
4847  h->mb_width, h->mb_height, h->mb_stride, 1);
4848  }
4849 
4850  return get_consumed_bytes(buf_index, buf_size);
4851 }
4852 
4854 {
4855  int i;
4856 
4857  free_tables(h, 1); // FIXME cleanup init stuff perhaps
4858 
4859  for (i = 0; i < MAX_SPS_COUNT; i++)
4860  av_freep(h->sps_buffers + i);
4861 
4862  for (i = 0; i < MAX_PPS_COUNT; i++)
4863  av_freep(h->pps_buffers + i);
4864 }
4865 
4867 {
4868  H264Context *h = avctx->priv_data;
4869  int i;
4870 
4873 
4874  if (h->DPB && !h->avctx->internal->is_copy) {
4875  for (i = 0; i < h->picture_count; i++) {
4876  free_picture(h, &h->DPB[i]);
4877  }
4878  }
4879  av_freep(&h->DPB);
4880 
4881  return 0;
4882 }
4883 
4884 static const AVProfile profiles[] = {
4885  { FF_PROFILE_H264_BASELINE, "Baseline" },
4886  { FF_PROFILE_H264_CONSTRAINED_BASELINE, "Constrained Baseline" },
4887  { FF_PROFILE_H264_MAIN, "Main" },
4888  { FF_PROFILE_H264_EXTENDED, "Extended" },
4889  { FF_PROFILE_H264_HIGH, "High" },
4890  { FF_PROFILE_H264_HIGH_10, "High 10" },
4891  { FF_PROFILE_H264_HIGH_10_INTRA, "High 10 Intra" },
4892  { FF_PROFILE_H264_HIGH_422, "High 4:2:2" },
4893  { FF_PROFILE_H264_HIGH_422_INTRA, "High 4:2:2 Intra" },
4894  { FF_PROFILE_H264_HIGH_444, "High 4:4:4" },
4895  { FF_PROFILE_H264_HIGH_444_PREDICTIVE, "High 4:4:4 Predictive" },
4896  { FF_PROFILE_H264_HIGH_444_INTRA, "High 4:4:4 Intra" },
4897  { FF_PROFILE_H264_CAVLC_444, "CAVLC 4:4:4" },
4898  { FF_PROFILE_UNKNOWN },
4899 };
4900 
4901 static const AVOption h264_options[] = {
4902  {"is_avc", "is avc", offsetof(H264Context, is_avc), FF_OPT_TYPE_INT, {.i64 = 0}, 0, 1, 0},
4903  {"nal_length_size", "nal_length_size", offsetof(H264Context, nal_length_size), FF_OPT_TYPE_INT, {.i64 = 0}, 0, 4, 0},
4904  {NULL}
4905 };
4906 
4907 static const AVClass h264_class = {
4908  .class_name = "H264 Decoder",
4909  .item_name = av_default_item_name,
4910  .option = h264_options,
4911  .version = LIBAVUTIL_VERSION_INT,
4912 };
4913 
4914 static const AVClass h264_vdpau_class = {
4915  .class_name = "H264 VDPAU Decoder",
4916  .item_name = av_default_item_name,
4917  .option = h264_options,
4918  .version = LIBAVUTIL_VERSION_INT,
4919 };
4920 
4922  .name = "h264",
4923  .type = AVMEDIA_TYPE_VIDEO,
4924  .id = AV_CODEC_ID_H264,
4925  .priv_data_size = sizeof(H264Context),
4928  .decode = decode_frame,
4929  .capabilities = /*CODEC_CAP_DRAW_HORIZ_BAND |*/ CODEC_CAP_DR1 |
4932  .flush = flush_dpb,
4933  .long_name = NULL_IF_CONFIG_SMALL("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10"),
4934  .init_thread_copy = ONLY_IF_THREADS_ENABLED(decode_init_thread_copy),
4935  .update_thread_context = ONLY_IF_THREADS_ENABLED(decode_update_thread_context),
4936  .profiles = NULL_IF_CONFIG_SMALL(profiles),
4937  .priv_class = &h264_class,
4938 };
4939 
4940 #if CONFIG_H264_VDPAU_DECODER
4941 AVCodec ff_h264_vdpau_decoder = {
4942  .name = "h264_vdpau",
4943  .type = AVMEDIA_TYPE_VIDEO,
4944  .id = AV_CODEC_ID_H264,
4945  .priv_data_size = sizeof(H264Context),
4948  .decode = decode_frame,
4950  .flush = flush_dpb,
4951  .long_name = NULL_IF_CONFIG_SMALL("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (VDPAU acceleration)"),
4952  .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_VDPAU_H264,
4953  AV_PIX_FMT_NONE},
4954  .profiles = NULL_IF_CONFIG_SMALL(profiles),
4955  .priv_class = &h264_vdpau_class,
4956 };
4957 #endif