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 Libav.
6  *
7  * Libav is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * Libav is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
28 #include "libavutil/imgutils.h"
29 #include "internal.h"
30 #include "cabac.h"
31 #include "cabac_functions.h"
32 #include "dsputil.h"
33 #include "avcodec.h"
34 #include "mpegvideo.h"
35 #include "h264.h"
36 #include "h264data.h"
37 #include "h264_mvpred.h"
38 #include "golomb.h"
39 #include "mathops.h"
40 #include "rectangle.h"
41 #include "thread.h"
42 #include "vdpau_internal.h"
43 #include "libavutil/avassert.h"
44 
45 // #undef NDEBUG
46 #include <assert.h>
47 
48 const uint16_t ff_h264_mb_sizes[4] = { 256, 384, 512, 768 };
49 
50 static const uint8_t rem6[QP_MAX_NUM + 1] = {
51  0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2,
52  3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5,
53  0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3,
54 };
55 
56 static const uint8_t div6[QP_MAX_NUM + 1] = {
57  0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3,
58  3, 3, 3, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6,
59  7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10,
60 };
61 
68 };
69 
75 {
76  MpegEncContext *const s = &h->s;
77  static const int8_t top[12] = {
78  -1, 0, LEFT_DC_PRED, -1, -1, -1, -1, -1, 0
79  };
80  static const int8_t left[12] = {
81  0, -1, TOP_DC_PRED, 0, -1, -1, -1, 0, -1, DC_128_PRED
82  };
83  int i;
84 
85  if (!(h->top_samples_available & 0x8000)) {
86  for (i = 0; i < 4; i++) {
87  int status = top[h->intra4x4_pred_mode_cache[scan8[0] + i]];
88  if (status < 0) {
90  "top block unavailable for requested intra4x4 mode %d at %d %d\n",
91  status, s->mb_x, s->mb_y);
92  return -1;
93  } else if (status) {
94  h->intra4x4_pred_mode_cache[scan8[0] + i] = status;
95  }
96  }
97  }
98 
99  if ((h->left_samples_available & 0x8888) != 0x8888) {
100  static const int mask[4] = { 0x8000, 0x2000, 0x80, 0x20 };
101  for (i = 0; i < 4; i++)
102  if (!(h->left_samples_available & mask[i])) {
103  int status = left[h->intra4x4_pred_mode_cache[scan8[0] + 8 * i]];
104  if (status < 0) {
106  "left block unavailable for requested intra4x4 mode %d at %d %d\n",
107  status, s->mb_x, s->mb_y);
108  return -1;
109  } else if (status) {
110  h->intra4x4_pred_mode_cache[scan8[0] + 8 * i] = status;
111  }
112  }
113  }
114 
115  return 0;
116 } // FIXME cleanup like ff_h264_check_intra_pred_mode
117 
122 int ff_h264_check_intra_pred_mode(H264Context *h, int mode, int is_chroma)
123 {
124  MpegEncContext *const s = &h->s;
125  static const int8_t top[7] = { LEFT_DC_PRED8x8, 1, -1, -1 };
126  static const int8_t left[7] = { TOP_DC_PRED8x8, -1, 2, -1, DC_128_PRED8x8 };
127 
128  if (mode > 6U) {
130  "out of range intra chroma pred mode at %d %d\n",
131  s->mb_x, s->mb_y);
132  return -1;
133  }
134 
135  if (!(h->top_samples_available & 0x8000)) {
136  mode = top[mode];
137  if (mode < 0) {
139  "top block unavailable for requested intra mode at %d %d\n",
140  s->mb_x, s->mb_y);
141  return -1;
142  }
143  }
144 
145  if ((h->left_samples_available & 0x8080) != 0x8080) {
146  mode = left[mode];
147  if (is_chroma && (h->left_samples_available & 0x8080)) {
148  // mad cow disease mode, aka MBAFF + constrained_intra_pred
149  mode = ALZHEIMER_DC_L0T_PRED8x8 +
150  (!(h->left_samples_available & 0x8000)) +
151  2 * (mode == DC_128_PRED8x8);
152  }
153  if (mode < 0) {
155  "left block unavailable for requested intra mode at %d %d\n",
156  s->mb_x, s->mb_y);
157  return -1;
158  }
159  }
160 
161  return mode;
162 }
163 
165  int *dst_length, int *consumed, int length)
166 {
167  int i, si, di;
168  uint8_t *dst;
169  int bufidx;
170 
171  // src[0]&0x80; // forbidden bit
172  h->nal_ref_idc = src[0] >> 5;
173  h->nal_unit_type = src[0] & 0x1F;
174 
175  src++;
176  length--;
177 
178 #define STARTCODE_TEST \
179  if (i + 2 < length && src[i + 1] == 0 && src[i + 2] <= 3) { \
180  if (src[i + 2] != 3) { \
181  /* startcode, so we must be past the end */ \
182  length = i; \
183  } \
184  break; \
185  }
186 #if HAVE_FAST_UNALIGNED
187 #define FIND_FIRST_ZERO \
188  if (i > 0 && !src[i]) \
189  i--; \
190  while (src[i]) \
191  i++
192 #if HAVE_FAST_64BIT
193  for (i = 0; i + 1 < length; i += 9) {
194  if (!((~AV_RN64A(src + i) &
195  (AV_RN64A(src + i) - 0x0100010001000101ULL)) &
196  0x8000800080008080ULL))
197  continue;
198  FIND_FIRST_ZERO;
200  i -= 7;
201  }
202 #else
203  for (i = 0; i + 1 < length; i += 5) {
204  if (!((~AV_RN32A(src + i) &
205  (AV_RN32A(src + i) - 0x01000101U)) &
206  0x80008080U))
207  continue;
208  FIND_FIRST_ZERO;
210  i -= 3;
211  }
212 #endif
213 #else
214  for (i = 0; i + 1 < length; i += 2) {
215  if (src[i])
216  continue;
217  if (i > 0 && src[i - 1] == 0)
218  i--;
220  }
221 #endif
222 
223  if (i >= length - 1) { // no escaped 0
224  *dst_length = length;
225  *consumed = length + 1; // +1 for the header
226  return src;
227  }
228 
229  // use second escape buffer for inter data
230  bufidx = h->nal_unit_type == NAL_DPC ? 1 : 0;
231  av_fast_malloc(&h->rbsp_buffer[bufidx], &h->rbsp_buffer_size[bufidx],
233  dst = h->rbsp_buffer[bufidx];
234 
235  if (dst == NULL)
236  return NULL;
237 
238  memcpy(dst, src, i);
239  si = di = i;
240  while (si + 2 < length) {
241  // remove escapes (very rare 1:2^22)
242  if (src[si + 2] > 3) {
243  dst[di++] = src[si++];
244  dst[di++] = src[si++];
245  } else if (src[si] == 0 && src[si + 1] == 0) {
246  if (src[si + 2] == 3) { // escape
247  dst[di++] = 0;
248  dst[di++] = 0;
249  si += 3;
250  continue;
251  } else // next start code
252  goto nsc;
253  }
254 
255  dst[di++] = src[si++];
256  }
257  while (si < length)
258  dst[di++] = src[si++];
259 nsc:
260 
261  memset(dst + di, 0, FF_INPUT_BUFFER_PADDING_SIZE);
262 
263  *dst_length = di;
264  *consumed = si + 1; // +1 for the header
265  /* FIXME store exact number of bits in the getbitcontext
266  * (it is needed for decoding) */
267  return dst;
268 }
269 
274 static int decode_rbsp_trailing(H264Context *h, const uint8_t *src)
275 {
276  int v = *src;
277  int r;
278 
279  tprintf(h->s.avctx, "rbsp trailing %X\n", v);
280 
281  for (r = 1; r < 9; r++) {
282  if (v & 1)
283  return r;
284  v >>= 1;
285  }
286  return 0;
287 }
288 
289 static inline int get_lowest_part_list_y(H264Context *h, Picture *pic, int n,
290  int height, int y_offset, int list)
291 {
292  int raw_my = h->mv_cache[list][scan8[n]][1];
293  int filter_height_up = (raw_my & 3) ? 2 : 0;
294  int filter_height_down = (raw_my & 3) ? 3 : 0;
295  int full_my = (raw_my >> 2) + y_offset;
296  int top = full_my - filter_height_up;
297  int bottom = full_my + filter_height_down + height;
298 
299  return FFMAX(abs(top), bottom);
300 }
301 
302 static inline void get_lowest_part_y(H264Context *h, int refs[2][48], int n,
303  int height, int y_offset, int list0,
304  int list1, int *nrefs)
305 {
306  MpegEncContext *const s = &h->s;
307  int my;
308 
309  y_offset += 16 * (s->mb_y >> MB_FIELD);
310 
311  if (list0) {
312  int ref_n = h->ref_cache[0][scan8[n]];
313  Picture *ref = &h->ref_list[0][ref_n];
314 
315  // Error resilience puts the current picture in the ref list.
316  // Don't try to wait on these as it will cause a deadlock.
317  // Fields can wait on each other, though.
318  if (ref->f.thread_opaque != s->current_picture.f.thread_opaque ||
319  (ref->f.reference & 3) != s->picture_structure) {
320  my = get_lowest_part_list_y(h, ref, n, height, y_offset, 0);
321  if (refs[0][ref_n] < 0)
322  nrefs[0] += 1;
323  refs[0][ref_n] = FFMAX(refs[0][ref_n], my);
324  }
325  }
326 
327  if (list1) {
328  int ref_n = h->ref_cache[1][scan8[n]];
329  Picture *ref = &h->ref_list[1][ref_n];
330 
331  if (ref->f.thread_opaque != s->current_picture.f.thread_opaque ||
332  (ref->f.reference & 3) != s->picture_structure) {
333  my = get_lowest_part_list_y(h, ref, n, height, y_offset, 1);
334  if (refs[1][ref_n] < 0)
335  nrefs[1] += 1;
336  refs[1][ref_n] = FFMAX(refs[1][ref_n], my);
337  }
338  }
339 }
340 
347 {
348  MpegEncContext *const s = &h->s;
349  const int mb_xy = h->mb_xy;
350  const int mb_type = s->current_picture.f.mb_type[mb_xy];
351  int refs[2][48];
352  int nrefs[2] = { 0 };
353  int ref, list;
354 
355  memset(refs, -1, sizeof(refs));
356 
357  if (IS_16X16(mb_type)) {
358  get_lowest_part_y(h, refs, 0, 16, 0,
359  IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1), nrefs);
360  } else if (IS_16X8(mb_type)) {
361  get_lowest_part_y(h, refs, 0, 8, 0,
362  IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1), nrefs);
363  get_lowest_part_y(h, refs, 8, 8, 8,
364  IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1), nrefs);
365  } else if (IS_8X16(mb_type)) {
366  get_lowest_part_y(h, refs, 0, 16, 0,
367  IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1), nrefs);
368  get_lowest_part_y(h, refs, 4, 16, 0,
369  IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1), nrefs);
370  } else {
371  int i;
372 
373  assert(IS_8X8(mb_type));
374 
375  for (i = 0; i < 4; i++) {
376  const int sub_mb_type = h->sub_mb_type[i];
377  const int n = 4 * i;
378  int y_offset = (i & 2) << 2;
379 
380  if (IS_SUB_8X8(sub_mb_type)) {
381  get_lowest_part_y(h, refs, n, 8, y_offset,
382  IS_DIR(sub_mb_type, 0, 0),
383  IS_DIR(sub_mb_type, 0, 1),
384  nrefs);
385  } else if (IS_SUB_8X4(sub_mb_type)) {
386  get_lowest_part_y(h, refs, n, 4, y_offset,
387  IS_DIR(sub_mb_type, 0, 0),
388  IS_DIR(sub_mb_type, 0, 1),
389  nrefs);
390  get_lowest_part_y(h, refs, n + 2, 4, y_offset + 4,
391  IS_DIR(sub_mb_type, 0, 0),
392  IS_DIR(sub_mb_type, 0, 1),
393  nrefs);
394  } else if (IS_SUB_4X8(sub_mb_type)) {
395  get_lowest_part_y(h, refs, n, 8, y_offset,
396  IS_DIR(sub_mb_type, 0, 0),
397  IS_DIR(sub_mb_type, 0, 1),
398  nrefs);
399  get_lowest_part_y(h, refs, n + 1, 8, y_offset,
400  IS_DIR(sub_mb_type, 0, 0),
401  IS_DIR(sub_mb_type, 0, 1),
402  nrefs);
403  } else {
404  int j;
405  assert(IS_SUB_4X4(sub_mb_type));
406  for (j = 0; j < 4; j++) {
407  int sub_y_offset = y_offset + 2 * (j & 2);
408  get_lowest_part_y(h, refs, n + j, 4, sub_y_offset,
409  IS_DIR(sub_mb_type, 0, 0),
410  IS_DIR(sub_mb_type, 0, 1),
411  nrefs);
412  }
413  }
414  }
415  }
416 
417  for (list = h->list_count - 1; list >= 0; list--)
418  for (ref = 0; ref < 48 && nrefs[list]; ref++) {
419  int row = refs[list][ref];
420  if (row >= 0) {
421  Picture *ref_pic = &h->ref_list[list][ref];
422  int ref_field = ref_pic->f.reference - 1;
423  int ref_field_picture = ref_pic->field_picture;
424  int pic_height = 16 * s->mb_height >> ref_field_picture;
425 
426  row <<= MB_MBAFF;
427  nrefs[list]--;
428 
429  if (!FIELD_PICTURE && ref_field_picture) { // frame referencing two fields
430  ff_thread_await_progress(&ref_pic->f,
431  FFMIN((row >> 1) - !(row & 1),
432  pic_height - 1),
433  1);
434  ff_thread_await_progress(&ref_pic->f,
435  FFMIN((row >> 1), pic_height - 1),
436  0);
437  } else if (FIELD_PICTURE && !ref_field_picture) { // field referencing one field of a frame
438  ff_thread_await_progress(&ref_pic->f,
439  FFMIN(row * 2 + ref_field,
440  pic_height - 1),
441  0);
442  } else if (FIELD_PICTURE) {
443  ff_thread_await_progress(&ref_pic->f,
444  FFMIN(row, pic_height - 1),
445  ref_field);
446  } else {
447  ff_thread_await_progress(&ref_pic->f,
448  FFMIN(row, pic_height - 1),
449  0);
450  }
451  }
452  }
453 }
454 
456  int n, int square, int height,
457  int delta, int list,
458  uint8_t *dest_y, uint8_t *dest_cb,
459  uint8_t *dest_cr,
460  int src_x_offset, int src_y_offset,
461  qpel_mc_func *qpix_op,
462  h264_chroma_mc_func chroma_op,
463  int pixel_shift, int chroma_idc)
464 {
465  MpegEncContext *const s = &h->s;
466  const int mx = h->mv_cache[list][scan8[n]][0] + src_x_offset * 8;
467  int my = h->mv_cache[list][scan8[n]][1] + src_y_offset * 8;
468  const int luma_xy = (mx & 3) + ((my & 3) << 2);
469  int offset = ((mx >> 2) << pixel_shift) + (my >> 2) * h->mb_linesize;
470  uint8_t *src_y = pic->f.data[0] + offset;
471  uint8_t *src_cb, *src_cr;
472  int extra_width = h->emu_edge_width;
473  int extra_height = h->emu_edge_height;
474  int emu = 0;
475  const int full_mx = mx >> 2;
476  const int full_my = my >> 2;
477  const int pic_width = 16 * s->mb_width;
478  const int pic_height = 16 * s->mb_height >> MB_FIELD;
479  int ysh;
480 
481  if (mx & 7)
482  extra_width -= 3;
483  if (my & 7)
484  extra_height -= 3;
485 
486  if (full_mx < 0 - extra_width ||
487  full_my < 0 - extra_height ||
488  full_mx + 16 /*FIXME*/ > pic_width + extra_width ||
489  full_my + 16 /*FIXME*/ > pic_height + extra_height) {
491  src_y - (2 << pixel_shift) - 2 * h->mb_linesize,
492  h->mb_linesize,
493  16 + 5, 16 + 5 /*FIXME*/, full_mx - 2,
494  full_my - 2, pic_width, pic_height);
495  src_y = s->edge_emu_buffer + (2 << pixel_shift) + 2 * h->mb_linesize;
496  emu = 1;
497  }
498 
499  qpix_op[luma_xy](dest_y, src_y, h->mb_linesize); // FIXME try variable height perhaps?
500  if (!square)
501  qpix_op[luma_xy](dest_y + delta, src_y + delta, h->mb_linesize);
502 
503  if (CONFIG_GRAY && s->flags & CODEC_FLAG_GRAY)
504  return;
505 
506  if (chroma_idc == 3 /* yuv444 */) {
507  src_cb = pic->f.data[1] + offset;
508  if (emu) {
510  src_cb - (2 << pixel_shift) - 2 * h->mb_linesize,
511  h->mb_linesize,
512  16 + 5, 16 + 5 /*FIXME*/,
513  full_mx - 2, full_my - 2,
514  pic_width, pic_height);
515  src_cb = s->edge_emu_buffer + (2 << pixel_shift) + 2 * h->mb_linesize;
516  }
517  qpix_op[luma_xy](dest_cb, src_cb, h->mb_linesize); // FIXME try variable height perhaps?
518  if (!square)
519  qpix_op[luma_xy](dest_cb + delta, src_cb + delta, h->mb_linesize);
520 
521  src_cr = pic->f.data[2] + offset;
522  if (emu) {
524  src_cr - (2 << pixel_shift) - 2 * h->mb_linesize,
525  h->mb_linesize,
526  16 + 5, 16 + 5 /*FIXME*/,
527  full_mx - 2, full_my - 2,
528  pic_width, pic_height);
529  src_cr = s->edge_emu_buffer + (2 << pixel_shift) + 2 * h->mb_linesize;
530  }
531  qpix_op[luma_xy](dest_cr, src_cr, h->mb_linesize); // FIXME try variable height perhaps?
532  if (!square)
533  qpix_op[luma_xy](dest_cr + delta, src_cr + delta, h->mb_linesize);
534  return;
535  }
536 
537  ysh = 3 - (chroma_idc == 2 /* yuv422 */);
538  if (chroma_idc == 1 /* yuv420 */ && MB_FIELD) {
539  // chroma offset when predicting from a field of opposite parity
540  my += 2 * ((s->mb_y & 1) - (pic->f.reference - 1));
541  emu |= (my >> 3) < 0 || (my >> 3) + 8 >= (pic_height >> 1);
542  }
543 
544  src_cb = pic->f.data[1] + ((mx >> 3) << pixel_shift) +
545  (my >> ysh) * h->mb_uvlinesize;
546  src_cr = pic->f.data[2] + ((mx >> 3) << pixel_shift) +
547  (my >> ysh) * h->mb_uvlinesize;
548 
549  if (emu) {
551  9, 8 * chroma_idc + 1, (mx >> 3), (my >> ysh),
552  pic_width >> 1, pic_height >> (chroma_idc == 1 /* yuv420 */));
553  src_cb = s->edge_emu_buffer;
554  }
555  chroma_op(dest_cb, src_cb, h->mb_uvlinesize,
556  height >> (chroma_idc == 1 /* yuv420 */),
557  mx & 7, (my << (chroma_idc == 2 /* yuv422 */)) & 7);
558 
559  if (emu) {
561  9, 8 * chroma_idc + 1, (mx >> 3), (my >> ysh),
562  pic_width >> 1, pic_height >> (chroma_idc == 1 /* yuv420 */));
563  src_cr = s->edge_emu_buffer;
564  }
565  chroma_op(dest_cr, src_cr, h->mb_uvlinesize, height >> (chroma_idc == 1 /* yuv420 */),
566  mx & 7, (my << (chroma_idc == 2 /* yuv422 */)) & 7);
567 }
568 
569 static av_always_inline void mc_part_std(H264Context *h, int n, int square,
570  int height, int delta,
571  uint8_t *dest_y, uint8_t *dest_cb,
572  uint8_t *dest_cr,
573  int x_offset, int y_offset,
574  qpel_mc_func *qpix_put,
575  h264_chroma_mc_func chroma_put,
576  qpel_mc_func *qpix_avg,
577  h264_chroma_mc_func chroma_avg,
578  int list0, int list1,
579  int pixel_shift, int chroma_idc)
580 {
581  MpegEncContext *const s = &h->s;
582  qpel_mc_func *qpix_op = qpix_put;
583  h264_chroma_mc_func chroma_op = chroma_put;
584 
585  dest_y += (2 * x_offset << pixel_shift) + 2 * y_offset * h->mb_linesize;
586  if (chroma_idc == 3 /* yuv444 */) {
587  dest_cb += (2 * x_offset << pixel_shift) + 2 * y_offset * h->mb_linesize;
588  dest_cr += (2 * x_offset << pixel_shift) + 2 * y_offset * h->mb_linesize;
589  } else if (chroma_idc == 2 /* yuv422 */) {
590  dest_cb += (x_offset << pixel_shift) + 2 * y_offset * h->mb_uvlinesize;
591  dest_cr += (x_offset << pixel_shift) + 2 * y_offset * h->mb_uvlinesize;
592  } else { /* yuv420 */
593  dest_cb += (x_offset << pixel_shift) + y_offset * h->mb_uvlinesize;
594  dest_cr += (x_offset << pixel_shift) + y_offset * h->mb_uvlinesize;
595  }
596  x_offset += 8 * s->mb_x;
597  y_offset += 8 * (s->mb_y >> MB_FIELD);
598 
599  if (list0) {
600  Picture *ref = &h->ref_list[0][h->ref_cache[0][scan8[n]]];
601  mc_dir_part(h, ref, n, square, height, delta, 0,
602  dest_y, dest_cb, dest_cr, x_offset, y_offset,
603  qpix_op, chroma_op, pixel_shift, chroma_idc);
604 
605  qpix_op = qpix_avg;
606  chroma_op = chroma_avg;
607  }
608 
609  if (list1) {
610  Picture *ref = &h->ref_list[1][h->ref_cache[1][scan8[n]]];
611  mc_dir_part(h, ref, n, square, height, delta, 1,
612  dest_y, dest_cb, dest_cr, x_offset, y_offset,
613  qpix_op, chroma_op, pixel_shift, chroma_idc);
614  }
615 }
616 
618  int height, int delta,
619  uint8_t *dest_y, uint8_t *dest_cb,
620  uint8_t *dest_cr,
621  int x_offset, int y_offset,
622  qpel_mc_func *qpix_put,
623  h264_chroma_mc_func chroma_put,
624  h264_weight_func luma_weight_op,
625  h264_weight_func chroma_weight_op,
626  h264_biweight_func luma_weight_avg,
627  h264_biweight_func chroma_weight_avg,
628  int list0, int list1,
629  int pixel_shift, int chroma_idc)
630 {
631  MpegEncContext *const s = &h->s;
632  int chroma_height;
633 
634  dest_y += (2 * x_offset << pixel_shift) + 2 * y_offset * h->mb_linesize;
635  if (chroma_idc == 3 /* yuv444 */) {
636  chroma_height = height;
637  chroma_weight_avg = luma_weight_avg;
638  chroma_weight_op = luma_weight_op;
639  dest_cb += (2 * x_offset << pixel_shift) + 2 * y_offset * h->mb_linesize;
640  dest_cr += (2 * x_offset << pixel_shift) + 2 * y_offset * h->mb_linesize;
641  } else if (chroma_idc == 2 /* yuv422 */) {
642  chroma_height = height;
643  dest_cb += (x_offset << pixel_shift) + 2 * y_offset * h->mb_uvlinesize;
644  dest_cr += (x_offset << pixel_shift) + 2 * y_offset * h->mb_uvlinesize;
645  } else { /* yuv420 */
646  chroma_height = height >> 1;
647  dest_cb += (x_offset << pixel_shift) + y_offset * h->mb_uvlinesize;
648  dest_cr += (x_offset << pixel_shift) + y_offset * h->mb_uvlinesize;
649  }
650  x_offset += 8 * s->mb_x;
651  y_offset += 8 * (s->mb_y >> MB_FIELD);
652 
653  if (list0 && list1) {
654  /* don't optimize for luma-only case, since B-frames usually
655  * use implicit weights => chroma too. */
656  uint8_t *tmp_cb = h->bipred_scratchpad;
657  uint8_t *tmp_cr = h->bipred_scratchpad + (16 << pixel_shift);
658  uint8_t *tmp_y = h->bipred_scratchpad + 16 * h->mb_uvlinesize;
659  int refn0 = h->ref_cache[0][scan8[n]];
660  int refn1 = h->ref_cache[1][scan8[n]];
661 
662  mc_dir_part(h, &h->ref_list[0][refn0], n, square, height, delta, 0,
663  dest_y, dest_cb, dest_cr,
664  x_offset, y_offset, qpix_put, chroma_put,
665  pixel_shift, chroma_idc);
666  mc_dir_part(h, &h->ref_list[1][refn1], n, square, height, delta, 1,
667  tmp_y, tmp_cb, tmp_cr,
668  x_offset, y_offset, qpix_put, chroma_put,
669  pixel_shift, chroma_idc);
670 
671  if (h->use_weight == 2) {
672  int weight0 = h->implicit_weight[refn0][refn1][s->mb_y & 1];
673  int weight1 = 64 - weight0;
674  luma_weight_avg(dest_y, tmp_y, h->mb_linesize,
675  height, 5, weight0, weight1, 0);
676  chroma_weight_avg(dest_cb, tmp_cb, h->mb_uvlinesize,
677  chroma_height, 5, weight0, weight1, 0);
678  chroma_weight_avg(dest_cr, tmp_cr, h->mb_uvlinesize,
679  chroma_height, 5, weight0, weight1, 0);
680  } else {
681  luma_weight_avg(dest_y, tmp_y, h->mb_linesize, height,
683  h->luma_weight[refn0][0][0],
684  h->luma_weight[refn1][1][0],
685  h->luma_weight[refn0][0][1] +
686  h->luma_weight[refn1][1][1]);
687  chroma_weight_avg(dest_cb, tmp_cb, h->mb_uvlinesize, chroma_height,
689  h->chroma_weight[refn0][0][0][0],
690  h->chroma_weight[refn1][1][0][0],
691  h->chroma_weight[refn0][0][0][1] +
692  h->chroma_weight[refn1][1][0][1]);
693  chroma_weight_avg(dest_cr, tmp_cr, h->mb_uvlinesize, chroma_height,
695  h->chroma_weight[refn0][0][1][0],
696  h->chroma_weight[refn1][1][1][0],
697  h->chroma_weight[refn0][0][1][1] +
698  h->chroma_weight[refn1][1][1][1]);
699  }
700  } else {
701  int list = list1 ? 1 : 0;
702  int refn = h->ref_cache[list][scan8[n]];
703  Picture *ref = &h->ref_list[list][refn];
704  mc_dir_part(h, ref, n, square, height, delta, list,
705  dest_y, dest_cb, dest_cr, x_offset, y_offset,
706  qpix_put, chroma_put, pixel_shift, chroma_idc);
707 
708  luma_weight_op(dest_y, h->mb_linesize, height,
710  h->luma_weight[refn][list][0],
711  h->luma_weight[refn][list][1]);
712  if (h->use_weight_chroma) {
713  chroma_weight_op(dest_cb, h->mb_uvlinesize, chroma_height,
715  h->chroma_weight[refn][list][0][0],
716  h->chroma_weight[refn][list][0][1]);
717  chroma_weight_op(dest_cr, h->mb_uvlinesize, chroma_height,
719  h->chroma_weight[refn][list][1][0],
720  h->chroma_weight[refn][list][1][1]);
721  }
722  }
723 }
724 
726  int pixel_shift, int chroma_idc)
727 {
728  /* fetch pixels for estimated mv 4 macroblocks ahead
729  * optimized for 64byte cache lines */
730  MpegEncContext *const s = &h->s;
731  const int refn = h->ref_cache[list][scan8[0]];
732  if (refn >= 0) {
733  const int mx = (h->mv_cache[list][scan8[0]][0] >> 2) + 16 * s->mb_x + 8;
734  const int my = (h->mv_cache[list][scan8[0]][1] >> 2) + 16 * s->mb_y;
735  uint8_t **src = h->ref_list[list][refn].f.data;
736  int off = (mx << pixel_shift) +
737  (my + (s->mb_x & 3) * 4) * h->mb_linesize +
738  (64 << pixel_shift);
739  s->vdsp.prefetch(src[0] + off, s->linesize, 4);
740  if (chroma_idc == 3 /* yuv444 */) {
741  s->vdsp.prefetch(src[1] + off, s->linesize, 4);
742  s->vdsp.prefetch(src[2] + off, s->linesize, 4);
743  } else {
744  off = ((mx >> 1) << pixel_shift) +
745  ((my >> 1) + (s->mb_x & 7)) * s->uvlinesize +
746  (64 << pixel_shift);
747  s->vdsp.prefetch(src[1] + off, src[2] - src[1], 2);
748  }
749  }
750 }
751 
752 static void free_tables(H264Context *h, int free_rbsp)
753 {
754  int i;
755  H264Context *hx;
756 
759  av_freep(&h->cbp_table);
760  av_freep(&h->mvd_table[0]);
761  av_freep(&h->mvd_table[1]);
762  av_freep(&h->direct_table);
765  h->slice_table = NULL;
766  av_freep(&h->list_counts);
767 
768  av_freep(&h->mb2b_xy);
769  av_freep(&h->mb2br_xy);
770 
771  for (i = 0; i < MAX_THREADS; i++) {
772  hx = h->thread_context[i];
773  if (!hx)
774  continue;
775  av_freep(&hx->top_borders[1]);
776  av_freep(&hx->top_borders[0]);
778  if (free_rbsp) {
779  av_freep(&hx->rbsp_buffer[1]);
780  av_freep(&hx->rbsp_buffer[0]);
781  hx->rbsp_buffer_size[0] = 0;
782  hx->rbsp_buffer_size[1] = 0;
783  }
784  if (i)
785  av_freep(&h->thread_context[i]);
786  }
787 }
788 
790 {
791  int i, j, q, x;
792  const int max_qp = 51 + 6 * (h->sps.bit_depth_luma - 8);
793 
794  for (i = 0; i < 6; i++) {
795  h->dequant8_coeff[i] = h->dequant8_buffer[i];
796  for (j = 0; j < i; j++)
797  if (!memcmp(h->pps.scaling_matrix8[j], h->pps.scaling_matrix8[i],
798  64 * sizeof(uint8_t))) {
799  h->dequant8_coeff[i] = h->dequant8_buffer[j];
800  break;
801  }
802  if (j < i)
803  continue;
804 
805  for (q = 0; q < max_qp + 1; q++) {
806  int shift = div6[q];
807  int idx = rem6[q];
808  for (x = 0; x < 64; x++)
809  h->dequant8_coeff[i][q][(x >> 3) | ((x & 7) << 3)] =
810  ((uint32_t)dequant8_coeff_init[idx][dequant8_coeff_init_scan[((x >> 1) & 12) | (x & 3)]] *
811  h->pps.scaling_matrix8[i][x]) << shift;
812  }
813  }
814 }
815 
817 {
818  int i, j, q, x;
819  const int max_qp = 51 + 6 * (h->sps.bit_depth_luma - 8);
820  for (i = 0; i < 6; i++) {
821  h->dequant4_coeff[i] = h->dequant4_buffer[i];
822  for (j = 0; j < i; j++)
823  if (!memcmp(h->pps.scaling_matrix4[j], h->pps.scaling_matrix4[i],
824  16 * sizeof(uint8_t))) {
825  h->dequant4_coeff[i] = h->dequant4_buffer[j];
826  break;
827  }
828  if (j < i)
829  continue;
830 
831  for (q = 0; q < max_qp + 1; q++) {
832  int shift = div6[q] + 2;
833  int idx = rem6[q];
834  for (x = 0; x < 16; x++)
835  h->dequant4_coeff[i][q][(x >> 2) | ((x << 2) & 0xF)] =
836  ((uint32_t)dequant4_coeff_init[idx][(x & 1) + ((x >> 2) & 1)] *
837  h->pps.scaling_matrix4[i][x]) << shift;
838  }
839  }
840 }
841 
843 {
844  int i, x;
846  if (h->pps.transform_8x8_mode)
848  if (h->sps.transform_bypass) {
849  for (i = 0; i < 6; i++)
850  for (x = 0; x < 16; x++)
851  h->dequant4_coeff[i][0][x] = 1 << 6;
853  for (i = 0; i < 6; i++)
854  for (x = 0; x < 64; x++)
855  h->dequant8_coeff[i][0][x] = 1 << 6;
856  }
857 }
858 
860 {
861  MpegEncContext *const s = &h->s;
862  const int big_mb_num = s->mb_stride * (s->mb_height + 1);
863  const int row_mb_num = s->mb_stride * 2 * s->avctx->thread_count;
864  int x, y;
865 
867  row_mb_num * 8 * sizeof(uint8_t), fail)
869  big_mb_num * 48 * sizeof(uint8_t), fail)
871  (big_mb_num + s->mb_stride) * sizeof(*h->slice_table_base), fail)
873  big_mb_num * sizeof(uint16_t), fail)
875  big_mb_num * sizeof(uint8_t), fail)
877  16 * row_mb_num * sizeof(uint8_t), fail);
879  16 * row_mb_num * sizeof(uint8_t), fail);
881  4 * big_mb_num * sizeof(uint8_t), fail);
883  big_mb_num * sizeof(uint8_t), fail)
884 
885  memset(h->slice_table_base, -1,
886  (big_mb_num + s->mb_stride) * sizeof(*h->slice_table_base));
887  h->slice_table = h->slice_table_base + s->mb_stride * 2 + 1;
888 
890  big_mb_num * sizeof(uint32_t), fail);
892  big_mb_num * sizeof(uint32_t), fail);
893  for (y = 0; y < s->mb_height; y++)
894  for (x = 0; x < s->mb_width; x++) {
895  const int mb_xy = x + y * s->mb_stride;
896  const int b_xy = 4 * x + 4 * y * h->b_stride;
897 
898  h->mb2b_xy[mb_xy] = b_xy;
899  h->mb2br_xy[mb_xy] = 8 * (FMO ? mb_xy : (mb_xy % (2 * s->mb_stride)));
900  }
901 
902  if (!h->dequant4_coeff[0])
904 
905  return 0;
906 
907 fail:
908  free_tables(h, 1);
909  return -1;
910 }
911 
915 static void clone_tables(H264Context *dst, H264Context *src, int i)
916 {
917  MpegEncContext *const s = &src->s;
918  dst->intra4x4_pred_mode = src->intra4x4_pred_mode + i * 8 * 2 * s->mb_stride;
919  dst->non_zero_count = src->non_zero_count;
920  dst->slice_table = src->slice_table;
921  dst->cbp_table = src->cbp_table;
922  dst->mb2b_xy = src->mb2b_xy;
923  dst->mb2br_xy = src->mb2br_xy;
925  dst->mvd_table[0] = src->mvd_table[0] + i * 8 * 2 * s->mb_stride;
926  dst->mvd_table[1] = src->mvd_table[1] + i * 8 * 2 * s->mb_stride;
927  dst->direct_table = src->direct_table;
928  dst->list_counts = src->list_counts;
929  dst->bipred_scratchpad = NULL;
930  ff_h264_pred_init(&dst->hpc, src->s.codec_id, src->sps.bit_depth_luma,
931  src->sps.chroma_format_idc);
932 }
933 
938 static int context_init(H264Context *h)
939 {
941  h->s.mb_width * 16 * 3 * sizeof(uint8_t) * 2, fail)
943  h->s.mb_width * 16 * 3 * sizeof(uint8_t) * 2, fail)
944 
945  h->ref_cache[0][scan8[5] + 1] =
946  h->ref_cache[0][scan8[7] + 1] =
947  h->ref_cache[0][scan8[13] + 1] =
948  h->ref_cache[1][scan8[5] + 1] =
949  h->ref_cache[1][scan8[7] + 1] =
950  h->ref_cache[1][scan8[13] + 1] = PART_NOT_AVAILABLE;
951 
952  return 0;
953 
954 fail:
955  return -1; // free_tables will clean up for us
956 }
957 
958 static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size,
959  int parse_extradata);
960 
962 {
963  MpegEncContext *const s = &h->s;
964 
965  s->width = s->avctx->width;
966  s->height = s->avctx->height;
967  s->codec_id = s->avctx->codec->id;
968 
969  ff_h264dsp_init(&h->h264dsp, 8, 1);
970  ff_h264_pred_init(&h->hpc, s->codec_id, 8, 1);
971 
972  h->dequant_coeff_pps = -1;
973  s->unrestricted_mv = 1;
974 
975  /* needed so that IDCT permutation is known early */
976  ff_dsputil_init(&s->dsp, s->avctx);
977  ff_videodsp_init(&s->vdsp, 8);
978 
979  memset(h->pps.scaling_matrix4, 16, 6 * 16 * sizeof(uint8_t));
980  memset(h->pps.scaling_matrix8, 16, 2 * 64 * sizeof(uint8_t));
981 }
982 
984 {
985  AVCodecContext *avctx = h->s.avctx;
986 
987  if (avctx->extradata[0] == 1) {
988  int i, cnt, nalsize;
989  unsigned char *p = avctx->extradata;
990 
991  h->is_avc = 1;
992 
993  if (avctx->extradata_size < 7) {
994  av_log(avctx, AV_LOG_ERROR, "avcC too short\n");
995  return -1;
996  }
997  /* sps and pps in the avcC always have length coded with 2 bytes,
998  * so put a fake nal_length_size = 2 while parsing them */
999  h->nal_length_size = 2;
1000  // Decode sps from avcC
1001  cnt = *(p + 5) & 0x1f; // Number of sps
1002  p += 6;
1003  for (i = 0; i < cnt; i++) {
1004  nalsize = AV_RB16(p) + 2;
1005  if (p - avctx->extradata + nalsize > avctx->extradata_size)
1006  return -1;
1007  if (decode_nal_units(h, p, nalsize, 1) < 0) {
1008  av_log(avctx, AV_LOG_ERROR,
1009  "Decoding sps %d from avcC failed\n", i);
1010  return -1;
1011  }
1012  p += nalsize;
1013  }
1014  // Decode pps from avcC
1015  cnt = *(p++); // Number of pps
1016  for (i = 0; i < cnt; i++) {
1017  nalsize = AV_RB16(p) + 2;
1018  if (p - avctx->extradata + nalsize > avctx->extradata_size)
1019  return -1;
1020  if (decode_nal_units(h, p, nalsize, 1) < 0) {
1021  av_log(avctx, AV_LOG_ERROR,
1022  "Decoding pps %d from avcC failed\n", i);
1023  return -1;
1024  }
1025  p += nalsize;
1026  }
1027  // Now store right nal length size, that will be used to parse all other nals
1028  h->nal_length_size = (avctx->extradata[4] & 0x03) + 1;
1029  } else {
1030  h->is_avc = 0;
1031  if (decode_nal_units(h, avctx->extradata, avctx->extradata_size, 1) < 0)
1032  return -1;
1033  }
1034  return 0;
1035 }
1036 
1038 {
1039  H264Context *h = avctx->priv_data;
1040  MpegEncContext *const s = &h->s;
1041  int i;
1042 
1044 
1045  s->avctx = avctx;
1046  common_init(h);
1047 
1048  s->out_format = FMT_H264;
1049  s->workaround_bugs = avctx->workaround_bugs;
1050 
1051  /* set defaults */
1052  // s->decode_mb = ff_h263_decode_mb;
1053  s->quarter_sample = 1;
1054  if (!avctx->has_b_frames)
1055  s->low_delay = 1;
1056 
1058 
1060 
1061  h->pixel_shift = 0;
1062  h->sps.bit_depth_luma = avctx->bits_per_raw_sample = 8;
1063 
1064  h->thread_context[0] = h;
1065  h->outputed_poc = h->next_outputed_poc = INT_MIN;
1066  for (i = 0; i < MAX_DELAYED_PIC_COUNT; i++)
1067  h->last_pocs[i] = INT_MIN;
1068  h->prev_poc_msb = 1 << 16;
1069  h->x264_build = -1;
1070  ff_h264_reset_sei(h);
1071  if (avctx->codec_id == AV_CODEC_ID_H264) {
1072  if (avctx->ticks_per_frame == 1)
1073  s->avctx->time_base.den *= 2;
1074  avctx->ticks_per_frame = 2;
1075  }
1076 
1077  if (avctx->extradata_size > 0 && avctx->extradata &&
1079  return -1;
1080 
1084  s->low_delay = 0;
1085  }
1086 
1087  return 0;
1088 }
1089 
1090 #define IN_RANGE(a, b, size) (((a) >= (b)) && ((a) < ((b) + (size))))
1091 
1092 static void copy_picture_range(Picture **to, Picture **from, int count,
1093  MpegEncContext *new_base,
1094  MpegEncContext *old_base)
1095 {
1096  int i;
1097 
1098  for (i = 0; i < count; i++) {
1099  assert((IN_RANGE(from[i], old_base, sizeof(*old_base)) ||
1100  IN_RANGE(from[i], old_base->picture,
1101  sizeof(Picture) * old_base->picture_count) ||
1102  !from[i]));
1103  to[i] = REBASE_PICTURE(from[i], new_base, old_base);
1104  }
1105 }
1106 
1107 static void copy_parameter_set(void **to, void **from, int count, int size)
1108 {
1109  int i;
1110 
1111  for (i = 0; i < count; i++) {
1112  if (to[i] && !from[i])
1113  av_freep(&to[i]);
1114  else if (from[i] && !to[i])
1115  to[i] = av_malloc(size);
1116 
1117  if (from[i])
1118  memcpy(to[i], from[i], size);
1119  }
1120 }
1121 
1123 {
1124  H264Context *h = avctx->priv_data;
1125 
1126  if (!avctx->internal->is_copy)
1127  return 0;
1128  memset(h->sps_buffers, 0, sizeof(h->sps_buffers));
1129  memset(h->pps_buffers, 0, sizeof(h->pps_buffers));
1130 
1131  h->s.context_initialized = 0;
1132 
1133  return 0;
1134 }
1135 
1136 #define copy_fields(to, from, start_field, end_field) \
1137  memcpy(&to->start_field, &from->start_field, \
1138  (char *)&to->end_field - (char *)&to->start_field)
1139 
1140 static int h264_slice_header_init(H264Context *, int);
1141 
1143 
1145  const AVCodecContext *src)
1146 {
1147  H264Context *h = dst->priv_data, *h1 = src->priv_data;
1148  MpegEncContext *const s = &h->s, *const s1 = &h1->s;
1149  int inited = s->context_initialized, err;
1150  int i;
1151 
1152  if (dst == src || !s1->context_initialized)
1153  return 0;
1154 
1155  if (inited &&
1156  (s->width != s1->width ||
1157  s->height != s1->height ||
1158  s->mb_width != s1->mb_width ||
1159  s->mb_height != s1->mb_height ||
1160  h->sps.bit_depth_luma != h1->sps.bit_depth_luma ||
1161  h->sps.chroma_format_idc != h1->sps.chroma_format_idc ||
1162  h->sps.colorspace != h1->sps.colorspace)) {
1163 
1165 
1166  s->width = s1->width;
1167  s->height = s1->height;
1168  s->mb_height = s1->mb_height;
1169  h->b_stride = h1->b_stride;
1170 
1171  if ((err = h264_slice_header_init(h, 1)) < 0) {
1172  av_log(h->s.avctx, AV_LOG_ERROR, "h264_slice_header_init() failed");
1173  return err;
1174  }
1175  h->context_reinitialized = 1;
1176 
1177  /* update linesize on resize for h264. The h264 decoder doesn't
1178  * necessarily call ff_MPV_frame_start in the new thread */
1179  s->linesize = s1->linesize;
1180  s->uvlinesize = s1->uvlinesize;
1181 
1182  /* copy block_offset since frame_start may not be called */
1183  memcpy(h->block_offset, h1->block_offset, sizeof(h->block_offset));
1185  }
1186 
1187  err = ff_mpeg_update_thread_context(dst, src);
1188  if (err)
1189  return err;
1190 
1191  if (!inited) {
1192  for (i = 0; i < MAX_SPS_COUNT; i++)
1193  av_freep(h->sps_buffers + i);
1194 
1195  for (i = 0; i < MAX_PPS_COUNT; i++)
1196  av_freep(h->pps_buffers + i);
1197 
1198  // copy all fields after MpegEnc
1199  memcpy(&h->s + 1, &h1->s + 1,
1200  sizeof(H264Context) - sizeof(MpegEncContext));
1201  memset(h->sps_buffers, 0, sizeof(h->sps_buffers));
1202  memset(h->pps_buffers, 0, sizeof(h->pps_buffers));
1203  if (ff_h264_alloc_tables(h) < 0) {
1204  av_log(dst, AV_LOG_ERROR, "Could not allocate memory for h264\n");
1205  return AVERROR(ENOMEM);
1206  }
1207  context_init(h);
1208 
1209  for (i = 0; i < 2; i++) {
1210  h->rbsp_buffer[i] = NULL;
1211  h->rbsp_buffer_size[i] = 0;
1212  }
1213  h->bipred_scratchpad = NULL;
1214 
1215  h->thread_context[0] = h;
1216 
1217  s->dsp.clear_blocks(h->mb);
1218  s->dsp.clear_blocks(h->mb + (24 * 16 << h->pixel_shift));
1219  }
1220 
1221  /* frame_start may not be called for the next thread (if it's decoding
1222  * a bottom field) so this has to be allocated here */
1223  if (!h->bipred_scratchpad)
1224  h->bipred_scratchpad = av_malloc(16 * 6 * s->linesize);
1225 
1226  // extradata/NAL handling
1227  h->is_avc = h1->is_avc;
1228 
1229  // SPS/PPS
1230  copy_parameter_set((void **)h->sps_buffers, (void **)h1->sps_buffers,
1231  MAX_SPS_COUNT, sizeof(SPS));
1232  h->sps = h1->sps;
1233  copy_parameter_set((void **)h->pps_buffers, (void **)h1->pps_buffers,
1234  MAX_PPS_COUNT, sizeof(PPS));
1235  h->pps = h1->pps;
1236 
1237  // Dequantization matrices
1238  // FIXME these are big - can they be only copied when PPS changes?
1239  copy_fields(h, h1, dequant4_buffer, dequant4_coeff);
1240 
1241  for (i = 0; i < 6; i++)
1242  h->dequant4_coeff[i] = h->dequant4_buffer[0] +
1243  (h1->dequant4_coeff[i] - h1->dequant4_buffer[0]);
1244 
1245  for (i = 0; i < 6; i++)
1246  h->dequant8_coeff[i] = h->dequant8_buffer[0] +
1247  (h1->dequant8_coeff[i] - h1->dequant8_buffer[0]);
1248 
1249  h->dequant_coeff_pps = h1->dequant_coeff_pps;
1250 
1251  // POC timing
1252  copy_fields(h, h1, poc_lsb, redundant_pic_count);
1253 
1254  // reference lists
1255  copy_fields(h, h1, ref_count, list_count);
1256  copy_fields(h, h1, ref_list, intra_gb);
1257  copy_fields(h, h1, short_ref, cabac_init_idc);
1258 
1259  copy_picture_range(h->short_ref, h1->short_ref, 32, s, s1);
1260  copy_picture_range(h->long_ref, h1->long_ref, 32, s, s1);
1261  copy_picture_range(h->delayed_pic, h1->delayed_pic,
1262  MAX_DELAYED_PIC_COUNT + 2, s, s1);
1263 
1264  h->last_slice_type = h1->last_slice_type;
1265 
1266  if (!s->current_picture_ptr)
1267  return 0;
1268 
1269  if (!s->droppable) {
1271  h->prev_poc_msb = h->poc_msb;
1272  h->prev_poc_lsb = h->poc_lsb;
1273  }
1275  h->prev_frame_num = h->frame_num;
1277 
1278  return err;
1279 }
1280 
1282 {
1283  MpegEncContext *const s = &h->s;
1284  int i;
1285  const int pixel_shift = h->pixel_shift;
1286 
1287  if (ff_MPV_frame_start(s, s->avctx) < 0)
1288  return -1;
1289  ff_er_frame_start(s);
1290  /*
1291  * ff_MPV_frame_start uses pict_type to derive key_frame.
1292  * This is incorrect for H.264; IDR markings must be used.
1293  * Zero here; IDR markings per slice in frame or fields are ORed in later.
1294  * See decode_nal_units().
1295  */
1298 
1299  assert(s->linesize && s->uvlinesize);
1300 
1301  for (i = 0; i < 16; i++) {
1302  h->block_offset[i] = (4 * ((scan8[i] - scan8[0]) & 7) << pixel_shift) + 4 * s->linesize * ((scan8[i] - scan8[0]) >> 3);
1303  h->block_offset[48 + i] = (4 * ((scan8[i] - scan8[0]) & 7) << pixel_shift) + 8 * s->linesize * ((scan8[i] - scan8[0]) >> 3);
1304  }
1305  for (i = 0; i < 16; i++) {
1306  h->block_offset[16 + i] =
1307  h->block_offset[32 + i] = (4 * ((scan8[i] - scan8[0]) & 7) << pixel_shift) + 4 * s->uvlinesize * ((scan8[i] - scan8[0]) >> 3);
1308  h->block_offset[48 + 16 + i] =
1309  h->block_offset[48 + 32 + i] = (4 * ((scan8[i] - scan8[0]) & 7) << pixel_shift) + 8 * s->uvlinesize * ((scan8[i] - scan8[0]) >> 3);
1310  }
1311 
1312  /* can't be in alloc_tables because linesize isn't known there.
1313  * FIXME: redo bipred weight to not require extra buffer? */
1314  for (i = 0; i < s->slice_context_count; i++)
1315  if (h->thread_context[i] && !h->thread_context[i]->bipred_scratchpad)
1316  h->thread_context[i]->bipred_scratchpad = av_malloc(16 * 6 * s->linesize);
1317 
1318  /* Some macroblocks can be accessed before they're available in case
1319  * of lost slices, MBAFF or threading. */
1320  memset(h->slice_table, -1,
1321  (s->mb_height * s->mb_stride - 1) * sizeof(*h->slice_table));
1322 
1323  // s->decode = (s->flags & CODEC_FLAG_PSNR) || !s->encoding ||
1324  // s->current_picture.f.reference /* || h->contains_intra */ || 1;
1325 
1326  /* We mark the current picture as non-reference after allocating it, so
1327  * that if we break out due to an error it can be released automatically
1328  * in the next ff_MPV_frame_start().
1329  * SVQ3 as well as most other codecs have only last/next/current and thus
1330  * get released even with set reference, besides SVQ3 and others do not
1331  * mark frames as reference later "naturally". */
1332  if (s->codec_id != AV_CODEC_ID_SVQ3)
1334 
1336  s->current_picture_ptr->field_poc[1] = INT_MAX;
1337 
1338  h->next_output_pic = NULL;
1339 
1340  assert(s->current_picture_ptr->long_ref == 0);
1341 
1342  return 0;
1343 }
1344 
1353 static void decode_postinit(H264Context *h, int setup_finished)
1354 {
1355  MpegEncContext *const s = &h->s;
1356  Picture *out = s->current_picture_ptr;
1357  Picture *cur = s->current_picture_ptr;
1358  int i, pics, out_of_order, out_idx;
1359  int invalid = 0, cnt = 0;
1360 
1363 
1364  if (h->next_output_pic)
1365  return;
1366 
1367  if (cur->field_poc[0] == INT_MAX || cur->field_poc[1] == INT_MAX) {
1368  /* FIXME: if we have two PAFF fields in one packet, we can't start
1369  * the next thread here. If we have one field per packet, we can.
1370  * The check in decode_nal_units() is not good enough to find this
1371  * yet, so we assume the worst for now. */
1372  // if (setup_finished)
1373  // ff_thread_finish_setup(s->avctx);
1374  return;
1375  }
1376 
1377  cur->f.interlaced_frame = 0;
1378  cur->f.repeat_pict = 0;
1379 
1380  /* Signal interlacing information externally. */
1381  /* Prioritize picture timing SEI information over used
1382  * decoding process if it exists. */
1383 
1384  if (h->sps.pic_struct_present_flag) {
1385  switch (h->sei_pic_struct) {
1386  case SEI_PIC_STRUCT_FRAME:
1387  break;
1390  cur->f.interlaced_frame = 1;
1391  break;
1395  cur->f.interlaced_frame = 1;
1396  else
1397  // try to flag soft telecine progressive
1399  break;
1402  /* Signal the possibility of telecined film externally
1403  * (pic_struct 5,6). From these hints, let the applications
1404  * decide if they apply deinterlacing. */
1405  cur->f.repeat_pict = 1;
1406  break;
1408  // Force progressive here, doubling interlaced frame is a bad idea.
1409  cur->f.repeat_pict = 2;
1410  break;
1412  cur->f.repeat_pict = 4;
1413  break;
1414  }
1415 
1416  if ((h->sei_ct_type & 3) &&
1418  cur->f.interlaced_frame = (h->sei_ct_type & (1 << 1)) != 0;
1419  } else {
1420  /* Derive interlacing flag from used decoding process. */
1422  }
1424 
1425  if (cur->field_poc[0] != cur->field_poc[1]) {
1426  /* Derive top_field_first from field pocs. */
1427  cur->f.top_field_first = cur->field_poc[0] < cur->field_poc[1];
1428  } else {
1429  if (cur->f.interlaced_frame || h->sps.pic_struct_present_flag) {
1430  /* Use picture timing SEI information. Even if it is a
1431  * information of a past frame, better than nothing. */
1434  cur->f.top_field_first = 1;
1435  else
1436  cur->f.top_field_first = 0;
1437  } else {
1438  /* Most likely progressive */
1439  cur->f.top_field_first = 0;
1440  }
1441  }
1442 
1443  // FIXME do something with unavailable reference frames
1444 
1445  /* Sort B-frames into display order */
1446 
1450  s->low_delay = 0;
1451  }
1452 
1456  s->low_delay = 0;
1457  }
1458 
1459  pics = 0;
1460  while (h->delayed_pic[pics])
1461  pics++;
1462 
1463  assert(pics <= MAX_DELAYED_PIC_COUNT);
1464 
1465  h->delayed_pic[pics++] = cur;
1466  if (cur->f.reference == 0)
1467  cur->f.reference = DELAYED_PIC_REF;
1468 
1469  /* Frame reordering. This code takes pictures from coding order and sorts
1470  * them by their incremental POC value into display order. It supports POC
1471  * gaps, MMCO reset codes and random resets.
1472  * A "display group" can start either with a IDR frame (f.key_frame = 1),
1473  * and/or can be closed down with a MMCO reset code. In sequences where
1474  * there is no delay, we can't detect that (since the frame was already
1475  * output to the user), so we also set h->mmco_reset to detect the MMCO
1476  * reset code.
1477  * FIXME: if we detect insufficient delays (as per s->avctx->has_b_frames),
1478  * we increase the delay between input and output. All frames affected by
1479  * the lag (e.g. those that should have been output before another frame
1480  * that we already returned to the user) will be dropped. This is a bug
1481  * that we will fix later. */
1482  for (i = 0; i < MAX_DELAYED_PIC_COUNT; i++) {
1483  cnt += out->poc < h->last_pocs[i];
1484  invalid += out->poc == INT_MIN;
1485  }
1486  if (!h->mmco_reset && !cur->f.key_frame &&
1487  cnt + invalid == MAX_DELAYED_PIC_COUNT && cnt > 0) {
1488  h->mmco_reset = 2;
1489  if (pics > 1)
1490  h->delayed_pic[pics - 2]->mmco_reset = 2;
1491  }
1492  if (h->mmco_reset || cur->f.key_frame) {
1493  for (i = 0; i < MAX_DELAYED_PIC_COUNT; i++)
1494  h->last_pocs[i] = INT_MIN;
1495  cnt = 0;
1496  invalid = MAX_DELAYED_PIC_COUNT;
1497  }
1498  out = h->delayed_pic[0];
1499  out_idx = 0;
1500  for (i = 1; i < MAX_DELAYED_PIC_COUNT &&
1501  h->delayed_pic[i] &&
1502  !h->delayed_pic[i - 1]->mmco_reset &&
1503  !h->delayed_pic[i]->f.key_frame;
1504  i++)
1505  if (h->delayed_pic[i]->poc < out->poc) {
1506  out = h->delayed_pic[i];
1507  out_idx = i;
1508  }
1509  if (s->avctx->has_b_frames == 0 &&
1510  (h->delayed_pic[0]->f.key_frame || h->mmco_reset))
1511  h->next_outputed_poc = INT_MIN;
1512  out_of_order = !out->f.key_frame && !h->mmco_reset &&
1513  (out->poc < h->next_outputed_poc);
1514 
1517  } else if (out_of_order && pics - 1 == s->avctx->has_b_frames &&
1518  s->avctx->has_b_frames < MAX_DELAYED_PIC_COUNT) {
1519  if (invalid + cnt < MAX_DELAYED_PIC_COUNT) {
1520  s->avctx->has_b_frames = FFMAX(s->avctx->has_b_frames, cnt);
1521  }
1522  s->low_delay = 0;
1523  } else if (s->low_delay &&
1524  ((h->next_outputed_poc != INT_MIN &&
1525  out->poc > h->next_outputed_poc + 2) ||
1526  cur->f.pict_type == AV_PICTURE_TYPE_B)) {
1527  s->low_delay = 0;
1528  s->avctx->has_b_frames++;
1529  }
1530 
1531  if (pics > s->avctx->has_b_frames) {
1532  out->f.reference &= ~DELAYED_PIC_REF;
1533  // for frame threading, the owner must be the second field's thread or
1534  // else the first thread can release the picture and reuse it unsafely
1535  out->owner2 = s;
1536  for (i = out_idx; h->delayed_pic[i]; i++)
1537  h->delayed_pic[i] = h->delayed_pic[i + 1];
1538  }
1539  memmove(h->last_pocs, &h->last_pocs[1],
1540  sizeof(*h->last_pocs) * (MAX_DELAYED_PIC_COUNT - 1));
1541  h->last_pocs[MAX_DELAYED_PIC_COUNT - 1] = cur->poc;
1542  if (!out_of_order && pics > s->avctx->has_b_frames) {
1543  h->next_output_pic = out;
1544  if (out->mmco_reset) {
1545  if (out_idx > 0) {
1546  h->next_outputed_poc = out->poc;
1547  h->delayed_pic[out_idx - 1]->mmco_reset = out->mmco_reset;
1548  } else {
1549  h->next_outputed_poc = INT_MIN;
1550  }
1551  } else {
1552  if (out_idx == 0 && pics > 1 && h->delayed_pic[0]->f.key_frame) {
1553  h->next_outputed_poc = INT_MIN;
1554  } else {
1555  h->next_outputed_poc = out->poc;
1556  }
1557  }
1558  h->mmco_reset = 0;
1559  } else {
1560  av_log(s->avctx, AV_LOG_DEBUG, "no picture\n");
1561  }
1562 
1563  if (setup_finished)
1565 }
1566 
1568  uint8_t *src_cb, uint8_t *src_cr,
1569  int linesize, int uvlinesize,
1570  int simple)
1571 {
1572  MpegEncContext *const s = &h->s;
1573  uint8_t *top_border;
1574  int top_idx = 1;
1575  const int pixel_shift = h->pixel_shift;
1576  int chroma444 = CHROMA444;
1577  int chroma422 = CHROMA422;
1578 
1579  src_y -= linesize;
1580  src_cb -= uvlinesize;
1581  src_cr -= uvlinesize;
1582 
1583  if (!simple && FRAME_MBAFF) {
1584  if (s->mb_y & 1) {
1585  if (!MB_MBAFF) {
1586  top_border = h->top_borders[0][s->mb_x];
1587  AV_COPY128(top_border, src_y + 15 * linesize);
1588  if (pixel_shift)
1589  AV_COPY128(top_border + 16, src_y + 15 * linesize + 16);
1590  if (simple || !CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY)) {
1591  if (chroma444) {
1592  if (pixel_shift) {
1593  AV_COPY128(top_border + 32, src_cb + 15 * uvlinesize);
1594  AV_COPY128(top_border + 48, src_cb + 15 * uvlinesize + 16);
1595  AV_COPY128(top_border + 64, src_cr + 15 * uvlinesize);
1596  AV_COPY128(top_border + 80, src_cr + 15 * uvlinesize + 16);
1597  } else {
1598  AV_COPY128(top_border + 16, src_cb + 15 * uvlinesize);
1599  AV_COPY128(top_border + 32, src_cr + 15 * uvlinesize);
1600  }
1601  } else if (chroma422) {
1602  if (pixel_shift) {
1603  AV_COPY128(top_border + 32, src_cb + 15 * uvlinesize);
1604  AV_COPY128(top_border + 48, src_cr + 15 * uvlinesize);
1605  } else {
1606  AV_COPY64(top_border + 16, src_cb + 15 * uvlinesize);
1607  AV_COPY64(top_border + 24, src_cr + 15 * uvlinesize);
1608  }
1609  } else {
1610  if (pixel_shift) {
1611  AV_COPY128(top_border + 32, src_cb + 7 * uvlinesize);
1612  AV_COPY128(top_border + 48, src_cr + 7 * uvlinesize);
1613  } else {
1614  AV_COPY64(top_border + 16, src_cb + 7 * uvlinesize);
1615  AV_COPY64(top_border + 24, src_cr + 7 * uvlinesize);
1616  }
1617  }
1618  }
1619  }
1620  } else if (MB_MBAFF) {
1621  top_idx = 0;
1622  } else
1623  return;
1624  }
1625 
1626  top_border = h->top_borders[top_idx][s->mb_x];
1627  /* There are two lines saved, the line above the top macroblock
1628  * of a pair, and the line above the bottom macroblock. */
1629  AV_COPY128(top_border, src_y + 16 * linesize);
1630  if (pixel_shift)
1631  AV_COPY128(top_border + 16, src_y + 16 * linesize + 16);
1632 
1633  if (simple || !CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY)) {
1634  if (chroma444) {
1635  if (pixel_shift) {
1636  AV_COPY128(top_border + 32, src_cb + 16 * linesize);
1637  AV_COPY128(top_border + 48, src_cb + 16 * linesize + 16);
1638  AV_COPY128(top_border + 64, src_cr + 16 * linesize);
1639  AV_COPY128(top_border + 80, src_cr + 16 * linesize + 16);
1640  } else {
1641  AV_COPY128(top_border + 16, src_cb + 16 * linesize);
1642  AV_COPY128(top_border + 32, src_cr + 16 * linesize);
1643  }
1644  } else if (chroma422) {
1645  if (pixel_shift) {
1646  AV_COPY128(top_border + 32, src_cb + 16 * uvlinesize);
1647  AV_COPY128(top_border + 48, src_cr + 16 * uvlinesize);
1648  } else {
1649  AV_COPY64(top_border + 16, src_cb + 16 * uvlinesize);
1650  AV_COPY64(top_border + 24, src_cr + 16 * uvlinesize);
1651  }
1652  } else {
1653  if (pixel_shift) {
1654  AV_COPY128(top_border + 32, src_cb + 8 * uvlinesize);
1655  AV_COPY128(top_border + 48, src_cr + 8 * uvlinesize);
1656  } else {
1657  AV_COPY64(top_border + 16, src_cb + 8 * uvlinesize);
1658  AV_COPY64(top_border + 24, src_cr + 8 * uvlinesize);
1659  }
1660  }
1661  }
1662 }
1663 
1665  uint8_t *src_cb, uint8_t *src_cr,
1666  int linesize, int uvlinesize,
1667  int xchg, int chroma444,
1668  int simple, int pixel_shift)
1669 {
1670  MpegEncContext *const s = &h->s;
1671  int deblock_topleft;
1672  int deblock_top;
1673  int top_idx = 1;
1674  uint8_t *top_border_m1;
1675  uint8_t *top_border;
1676 
1677  if (!simple && FRAME_MBAFF) {
1678  if (s->mb_y & 1) {
1679  if (!MB_MBAFF)
1680  return;
1681  } else {
1682  top_idx = MB_MBAFF ? 0 : 1;
1683  }
1684  }
1685 
1686  if (h->deblocking_filter == 2) {
1687  deblock_topleft = h->slice_table[h->mb_xy - 1 - s->mb_stride] == h->slice_num;
1688  deblock_top = h->top_type;
1689  } else {
1690  deblock_topleft = (s->mb_x > 0);
1691  deblock_top = (s->mb_y > !!MB_FIELD);
1692  }
1693 
1694  src_y -= linesize + 1 + pixel_shift;
1695  src_cb -= uvlinesize + 1 + pixel_shift;
1696  src_cr -= uvlinesize + 1 + pixel_shift;
1697 
1698  top_border_m1 = h->top_borders[top_idx][s->mb_x - 1];
1699  top_border = h->top_borders[top_idx][s->mb_x];
1700 
1701 #define XCHG(a, b, xchg) \
1702  if (pixel_shift) { \
1703  if (xchg) { \
1704  AV_SWAP64(b + 0, a + 0); \
1705  AV_SWAP64(b + 8, a + 8); \
1706  } else { \
1707  AV_COPY128(b, a); \
1708  } \
1709  } else if (xchg) \
1710  AV_SWAP64(b, a); \
1711  else \
1712  AV_COPY64(b, a);
1713 
1714  if (deblock_top) {
1715  if (deblock_topleft) {
1716  XCHG(top_border_m1 + (8 << pixel_shift),
1717  src_y - (7 << pixel_shift), 1);
1718  }
1719  XCHG(top_border + (0 << pixel_shift), src_y + (1 << pixel_shift), xchg);
1720  XCHG(top_border + (8 << pixel_shift), src_y + (9 << pixel_shift), 1);
1721  if (s->mb_x + 1 < s->mb_width) {
1722  XCHG(h->top_borders[top_idx][s->mb_x + 1],
1723  src_y + (17 << pixel_shift), 1);
1724  }
1725  }
1726  if (simple || !CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY)) {
1727  if (chroma444) {
1728  if (deblock_topleft) {
1729  XCHG(top_border_m1 + (24 << pixel_shift), src_cb - (7 << pixel_shift), 1);
1730  XCHG(top_border_m1 + (40 << pixel_shift), src_cr - (7 << pixel_shift), 1);
1731  }
1732  XCHG(top_border + (16 << pixel_shift), src_cb + (1 << pixel_shift), xchg);
1733  XCHG(top_border + (24 << pixel_shift), src_cb + (9 << pixel_shift), 1);
1734  XCHG(top_border + (32 << pixel_shift), src_cr + (1 << pixel_shift), xchg);
1735  XCHG(top_border + (40 << pixel_shift), src_cr + (9 << pixel_shift), 1);
1736  if (s->mb_x + 1 < s->mb_width) {
1737  XCHG(h->top_borders[top_idx][s->mb_x + 1] + (16 << pixel_shift), src_cb + (17 << pixel_shift), 1);
1738  XCHG(h->top_borders[top_idx][s->mb_x + 1] + (32 << pixel_shift), src_cr + (17 << pixel_shift), 1);
1739  }
1740  } else {
1741  if (deblock_top) {
1742  if (deblock_topleft) {
1743  XCHG(top_border_m1 + (16 << pixel_shift), src_cb - (7 << pixel_shift), 1);
1744  XCHG(top_border_m1 + (24 << pixel_shift), src_cr - (7 << pixel_shift), 1);
1745  }
1746  XCHG(top_border + (16 << pixel_shift), src_cb + 1 + pixel_shift, 1);
1747  XCHG(top_border + (24 << pixel_shift), src_cr + 1 + pixel_shift, 1);
1748  }
1749  }
1750  }
1751 }
1752 
1753 static av_always_inline int dctcoef_get(DCTELEM *mb, int high_bit_depth,
1754  int index)
1755 {
1756  if (high_bit_depth) {
1757  return AV_RN32A(((int32_t *)mb) + index);
1758  } else
1759  return AV_RN16A(mb + index);
1760 }
1761 
1762 static av_always_inline void dctcoef_set(DCTELEM *mb, int high_bit_depth,
1763  int index, int value)
1764 {
1765  if (high_bit_depth) {
1766  AV_WN32A(((int32_t *)mb) + index, value);
1767  } else
1768  AV_WN16A(mb + index, value);
1769 }
1770 
1772  int mb_type, int is_h264,
1773  int simple,
1774  int transform_bypass,
1775  int pixel_shift,
1776  int *block_offset,
1777  int linesize,
1778  uint8_t *dest_y, int p)
1779 {
1780  MpegEncContext *const s = &h->s;
1781  void (*idct_add)(uint8_t *dst, DCTELEM *block, int stride);
1782  void (*idct_dc_add)(uint8_t *dst, DCTELEM *block, int stride);
1783  int i;
1784  int qscale = p == 0 ? s->qscale : h->chroma_qp[p - 1];
1785  block_offset += 16 * p;
1786  if (IS_INTRA4x4(mb_type)) {
1787  if (simple || !s->encoding) {
1788  if (IS_8x8DCT(mb_type)) {
1789  if (transform_bypass) {
1790  idct_dc_add =
1791  idct_add = s->dsp.add_pixels8;
1792  } else {
1793  idct_dc_add = h->h264dsp.h264_idct8_dc_add;
1795  }
1796  for (i = 0; i < 16; i += 4) {
1797  uint8_t *const ptr = dest_y + block_offset[i];
1798  const int dir = h->intra4x4_pred_mode_cache[scan8[i]];
1799  if (transform_bypass && h->sps.profile_idc == 244 && dir <= 1) {
1800  h->hpc.pred8x8l_add[dir](ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize);
1801  } else {
1802  const int nnz = h->non_zero_count_cache[scan8[i + p * 16]];
1803  h->hpc.pred8x8l[dir](ptr, (h->topleft_samples_available << i) & 0x8000,
1804  (h->topright_samples_available << i) & 0x4000, linesize);
1805  if (nnz) {
1806  if (nnz == 1 && dctcoef_get(h->mb, pixel_shift, i * 16 + p * 256))
1807  idct_dc_add(ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize);
1808  else
1809  idct_add(ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize);
1810  }
1811  }
1812  }
1813  } else {
1814  if (transform_bypass) {
1815  idct_dc_add =
1816  idct_add = s->dsp.add_pixels4;
1817  } else {
1818  idct_dc_add = h->h264dsp.h264_idct_dc_add;
1820  }
1821  for (i = 0; i < 16; i++) {
1822  uint8_t *const ptr = dest_y + block_offset[i];
1823  const int dir = h->intra4x4_pred_mode_cache[scan8[i]];
1824 
1825  if (transform_bypass && h->sps.profile_idc == 244 && dir <= 1) {
1826  h->hpc.pred4x4_add[dir](ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize);
1827  } else {
1828  uint8_t *topright;
1829  int nnz, tr;
1830  uint64_t tr_high;
1831  if (dir == DIAG_DOWN_LEFT_PRED || dir == VERT_LEFT_PRED) {
1832  const int topright_avail = (h->topright_samples_available << i) & 0x8000;
1833  assert(s->mb_y || linesize <= block_offset[i]);
1834  if (!topright_avail) {
1835  if (pixel_shift) {
1836  tr_high = ((uint16_t *)ptr)[3 - linesize / 2] * 0x0001000100010001ULL;
1837  topright = (uint8_t *)&tr_high;
1838  } else {
1839  tr = ptr[3 - linesize] * 0x01010101u;
1840  topright = (uint8_t *)&tr;
1841  }
1842  } else
1843  topright = ptr + (4 << pixel_shift) - linesize;
1844  } else
1845  topright = NULL;
1846 
1847  h->hpc.pred4x4[dir](ptr, topright, linesize);
1848  nnz = h->non_zero_count_cache[scan8[i + p * 16]];
1849  if (nnz) {
1850  if (is_h264) {
1851  if (nnz == 1 && dctcoef_get(h->mb, pixel_shift, i * 16 + p * 256))
1852  idct_dc_add(ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize);
1853  else
1854  idct_add(ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize);
1855  } else if (CONFIG_SVQ3_DECODER)
1856  ff_svq3_add_idct_c(ptr, h->mb + i * 16 + p * 256, linesize, qscale, 0);
1857  }
1858  }
1859  }
1860  }
1861  }
1862  } else {
1863  h->hpc.pred16x16[h->intra16x16_pred_mode](dest_y, linesize);
1864  if (is_h264) {
1866  if (!transform_bypass)
1867  h->h264dsp.h264_luma_dc_dequant_idct(h->mb + (p * 256 << pixel_shift),
1868  h->mb_luma_dc[p],
1869  h->dequant4_coeff[p][qscale][0]);
1870  else {
1871  static const uint8_t dc_mapping[16] = {
1872  0 * 16, 1 * 16, 4 * 16, 5 * 16,
1873  2 * 16, 3 * 16, 6 * 16, 7 * 16,
1874  8 * 16, 9 * 16, 12 * 16, 13 * 16,
1875  10 * 16, 11 * 16, 14 * 16, 15 * 16 };
1876  for (i = 0; i < 16; i++)
1877  dctcoef_set(h->mb + (p * 256 << pixel_shift),
1878  pixel_shift, dc_mapping[i],
1879  dctcoef_get(h->mb_luma_dc[p],
1880  pixel_shift, i));
1881  }
1882  }
1883  } else if (CONFIG_SVQ3_DECODER)
1884  ff_svq3_luma_dc_dequant_idct_c(h->mb + p * 256,
1885  h->mb_luma_dc[p], qscale);
1886  }
1887 }
1888 
1890  int is_h264, int simple,
1891  int transform_bypass,
1892  int pixel_shift,
1893  int *block_offset,
1894  int linesize,
1895  uint8_t *dest_y, int p)
1896 {
1897  MpegEncContext *const s = &h->s;
1898  void (*idct_add)(uint8_t *dst, DCTELEM *block, int stride);
1899  int i;
1900  block_offset += 16 * p;
1901  if (!IS_INTRA4x4(mb_type)) {
1902  if (is_h264) {
1903  if (IS_INTRA16x16(mb_type)) {
1904  if (transform_bypass) {
1905  if (h->sps.profile_idc == 244 &&
1908  h->hpc.pred16x16_add[h->intra16x16_pred_mode](dest_y, block_offset,
1909  h->mb + (p * 256 << pixel_shift),
1910  linesize);
1911  } else {
1912  for (i = 0; i < 16; i++)
1913  if (h->non_zero_count_cache[scan8[i + p * 16]] ||
1914  dctcoef_get(h->mb, pixel_shift, i * 16 + p * 256))
1915  s->dsp.add_pixels4(dest_y + block_offset[i],
1916  h->mb + (i * 16 + p * 256 << pixel_shift),
1917  linesize);
1918  }
1919  } else {
1920  h->h264dsp.h264_idct_add16intra(dest_y, block_offset,
1921  h->mb + (p * 256 << pixel_shift),
1922  linesize,
1923  h->non_zero_count_cache + p * 5 * 8);
1924  }
1925  } else if (h->cbp & 15) {
1926  if (transform_bypass) {
1927  const int di = IS_8x8DCT(mb_type) ? 4 : 1;
1928  idct_add = IS_8x8DCT(mb_type) ? s->dsp.add_pixels8
1929  : s->dsp.add_pixels4;
1930  for (i = 0; i < 16; i += di)
1931  if (h->non_zero_count_cache[scan8[i + p * 16]])
1932  idct_add(dest_y + block_offset[i],
1933  h->mb + (i * 16 + p * 256 << pixel_shift),
1934  linesize);
1935  } else {
1936  if (IS_8x8DCT(mb_type))
1937  h->h264dsp.h264_idct8_add4(dest_y, block_offset,
1938  h->mb + (p * 256 << pixel_shift),
1939  linesize,
1940  h->non_zero_count_cache + p * 5 * 8);
1941  else
1942  h->h264dsp.h264_idct_add16(dest_y, block_offset,
1943  h->mb + (p * 256 << pixel_shift),
1944  linesize,
1945  h->non_zero_count_cache + p * 5 * 8);
1946  }
1947  }
1948  } else if (CONFIG_SVQ3_DECODER) {
1949  for (i = 0; i < 16; i++)
1950  if (h->non_zero_count_cache[scan8[i + p * 16]] || h->mb[i * 16 + p * 256]) {
1951  // FIXME benchmark weird rule, & below
1952  uint8_t *const ptr = dest_y + block_offset[i];
1953  ff_svq3_add_idct_c(ptr, h->mb + i * 16 + p * 256, linesize,
1954  s->qscale, IS_INTRA(mb_type) ? 1 : 0);
1955  }
1956  }
1957  }
1958 }
1959 
1960 #define BITS 8
1961 #define SIMPLE 1
1962 #include "h264_mb_template.c"
1963 
1964 #undef BITS
1965 #define BITS 16
1966 #include "h264_mb_template.c"
1967 
1968 #undef SIMPLE
1969 #define SIMPLE 0
1970 #include "h264_mb_template.c"
1971 
1973 {
1974  MpegEncContext *const s = &h->s;
1975  const int mb_xy = h->mb_xy;
1976  const int mb_type = s->current_picture.f.mb_type[mb_xy];
1977  int is_complex = CONFIG_SMALL || h->is_complex || IS_INTRA_PCM(mb_type) || s->qscale == 0;
1978 
1979  if (CHROMA444) {
1980  if (is_complex || h->pixel_shift)
1981  hl_decode_mb_444_complex(h);
1982  else
1983  hl_decode_mb_444_simple_8(h);
1984  } else if (is_complex) {
1985  hl_decode_mb_complex(h);
1986  } else if (h->pixel_shift) {
1987  hl_decode_mb_simple_16(h);
1988  } else
1989  hl_decode_mb_simple_8(h);
1990 }
1991 
1993 {
1994  MpegEncContext *const s = &h->s;
1995  int list, i;
1996  int luma_def, chroma_def;
1997 
1998  h->use_weight = 0;
1999  h->use_weight_chroma = 0;
2001  if (h->sps.chroma_format_idc)
2003  luma_def = 1 << h->luma_log2_weight_denom;
2004  chroma_def = 1 << h->chroma_log2_weight_denom;
2005 
2006  for (list = 0; list < 2; list++) {
2007  h->luma_weight_flag[list] = 0;
2008  h->chroma_weight_flag[list] = 0;
2009  for (i = 0; i < h->ref_count[list]; i++) {
2010  int luma_weight_flag, chroma_weight_flag;
2011 
2012  luma_weight_flag = get_bits1(&s->gb);
2013  if (luma_weight_flag) {
2014  h->luma_weight[i][list][0] = get_se_golomb(&s->gb);
2015  h->luma_weight[i][list][1] = get_se_golomb(&s->gb);
2016  if (h->luma_weight[i][list][0] != luma_def ||
2017  h->luma_weight[i][list][1] != 0) {
2018  h->use_weight = 1;
2019  h->luma_weight_flag[list] = 1;
2020  }
2021  } else {
2022  h->luma_weight[i][list][0] = luma_def;
2023  h->luma_weight[i][list][1] = 0;
2024  }
2025 
2026  if (h->sps.chroma_format_idc) {
2027  chroma_weight_flag = get_bits1(&s->gb);
2028  if (chroma_weight_flag) {
2029  int j;
2030  for (j = 0; j < 2; j++) {
2031  h->chroma_weight[i][list][j][0] = get_se_golomb(&s->gb);
2032  h->chroma_weight[i][list][j][1] = get_se_golomb(&s->gb);
2033  if (h->chroma_weight[i][list][j][0] != chroma_def ||
2034  h->chroma_weight[i][list][j][1] != 0) {
2035  h->use_weight_chroma = 1;
2036  h->chroma_weight_flag[list] = 1;
2037  }
2038  }
2039  } else {
2040  int j;
2041  for (j = 0; j < 2; j++) {
2042  h->chroma_weight[i][list][j][0] = chroma_def;
2043  h->chroma_weight[i][list][j][1] = 0;
2044  }
2045  }
2046  }
2047  }
2049  break;
2050  }
2051  h->use_weight = h->use_weight || h->use_weight_chroma;
2052  return 0;
2053 }
2054 
2060 static void implicit_weight_table(H264Context *h, int field)
2061 {
2062  MpegEncContext *const s = &h->s;
2063  int ref0, ref1, i, cur_poc, ref_start, ref_count0, ref_count1;
2064 
2065  for (i = 0; i < 2; i++) {
2066  h->luma_weight_flag[i] = 0;
2067  h->chroma_weight_flag[i] = 0;
2068  }
2069 
2070  if (field < 0) {
2071  if (s->picture_structure == PICT_FRAME) {
2072  cur_poc = s->current_picture_ptr->poc;
2073  } else {
2074  cur_poc = s->current_picture_ptr->field_poc[s->picture_structure - 1];
2075  }
2076  if (h->ref_count[0] == 1 && h->ref_count[1] == 1 && !FRAME_MBAFF &&
2077  h->ref_list[0][0].poc + h->ref_list[1][0].poc == 2 * cur_poc) {
2078  h->use_weight = 0;
2079  h->use_weight_chroma = 0;
2080  return;
2081  }
2082  ref_start = 0;
2083  ref_count0 = h->ref_count[0];
2084  ref_count1 = h->ref_count[1];
2085  } else {
2086  cur_poc = s->current_picture_ptr->field_poc[field];
2087  ref_start = 16;
2088  ref_count0 = 16 + 2 * h->ref_count[0];
2089  ref_count1 = 16 + 2 * h->ref_count[1];
2090  }
2091 
2092  h->use_weight = 2;
2093  h->use_weight_chroma = 2;
2094  h->luma_log2_weight_denom = 5;
2095  h->chroma_log2_weight_denom = 5;
2096 
2097  for (ref0 = ref_start; ref0 < ref_count0; ref0++) {
2098  int poc0 = h->ref_list[0][ref0].poc;
2099  for (ref1 = ref_start; ref1 < ref_count1; ref1++) {
2100  int w = 32;
2101  if (!h->ref_list[0][ref0].long_ref && !h->ref_list[1][ref1].long_ref) {
2102  int poc1 = h->ref_list[1][ref1].poc;
2103  int td = av_clip(poc1 - poc0, -128, 127);
2104  if (td) {
2105  int tb = av_clip(cur_poc - poc0, -128, 127);
2106  int tx = (16384 + (FFABS(td) >> 1)) / td;
2107  int dist_scale_factor = (tb * tx + 32) >> 8;
2108  if (dist_scale_factor >= -64 && dist_scale_factor <= 128)
2109  w = 64 - dist_scale_factor;
2110  }
2111  }
2112  if (field < 0) {
2113  h->implicit_weight[ref0][ref1][0] =
2114  h->implicit_weight[ref0][ref1][1] = w;
2115  } else {
2116  h->implicit_weight[ref0][ref1][field] = w;
2117  }
2118  }
2119  }
2120 }
2121 
2125 static void idr(H264Context *h)
2126 {
2128  h->prev_frame_num = 0;
2129  h->prev_frame_num_offset = 0;
2130  h->prev_poc_msb =
2131  h->prev_poc_lsb = 0;
2132 }
2133 
2134 /* forget old pics after a seek */
2135 static void flush_change(H264Context *h)
2136 {
2137  int i;
2138  for (i = 0; i < MAX_DELAYED_PIC_COUNT; i++)
2139  h->last_pocs[i] = INT_MIN;
2140  h->outputed_poc = h->next_outputed_poc = INT_MIN;
2141  h->prev_interlaced_frame = 1;
2142  idr(h);
2143  if (h->s.current_picture_ptr)
2144  h->s.current_picture_ptr->f.reference = 0;
2145  h->s.first_field = 0;
2146  memset(h->ref_list[0], 0, sizeof(h->ref_list[0]));
2147  memset(h->ref_list[1], 0, sizeof(h->ref_list[1]));
2148  memset(h->default_ref_list[0], 0, sizeof(h->default_ref_list[0]));
2149  memset(h->default_ref_list[1], 0, sizeof(h->default_ref_list[1]));
2150  ff_h264_reset_sei(h);
2151 }
2152 
2153 /* forget old pics after a seek */
2154 static void flush_dpb(AVCodecContext *avctx)
2155 {
2156  H264Context *h = avctx->priv_data;
2157  int i;
2158 
2159  for (i = 0; i < MAX_DELAYED_PIC_COUNT; i++) {
2160  if (h->delayed_pic[i])
2161  h->delayed_pic[i]->f.reference = 0;
2162  h->delayed_pic[i] = NULL;
2163  }
2164 
2165  flush_change(h);
2166  ff_mpeg_flush(avctx);
2167 }
2168 
2169 static int init_poc(H264Context *h)
2170 {
2171  MpegEncContext *const s = &h->s;
2172  const int max_frame_num = 1 << h->sps.log2_max_frame_num;
2173  int field_poc[2];
2174  Picture *cur = s->current_picture_ptr;
2175 
2177  if (h->frame_num < h->prev_frame_num)
2178  h->frame_num_offset += max_frame_num;
2179 
2180  if (h->sps.poc_type == 0) {
2181  const int max_poc_lsb = 1 << h->sps.log2_max_poc_lsb;
2182 
2183  if (h->poc_lsb < h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb >= max_poc_lsb / 2)
2184  h->poc_msb = h->prev_poc_msb + max_poc_lsb;
2185  else if (h->poc_lsb > h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb < -max_poc_lsb / 2)
2186  h->poc_msb = h->prev_poc_msb - max_poc_lsb;
2187  else
2188  h->poc_msb = h->prev_poc_msb;
2189  field_poc[0] =
2190  field_poc[1] = h->poc_msb + h->poc_lsb;
2191  if (s->picture_structure == PICT_FRAME)
2192  field_poc[1] += h->delta_poc_bottom;
2193  } else if (h->sps.poc_type == 1) {
2194  int abs_frame_num, expected_delta_per_poc_cycle, expectedpoc;
2195  int i;
2196 
2197  if (h->sps.poc_cycle_length != 0)
2198  abs_frame_num = h->frame_num_offset + h->frame_num;
2199  else
2200  abs_frame_num = 0;
2201 
2202  if (h->nal_ref_idc == 0 && abs_frame_num > 0)
2203  abs_frame_num--;
2204 
2205  expected_delta_per_poc_cycle = 0;
2206  for (i = 0; i < h->sps.poc_cycle_length; i++)
2207  // FIXME integrate during sps parse
2208  expected_delta_per_poc_cycle += h->sps.offset_for_ref_frame[i];
2209 
2210  if (abs_frame_num > 0) {
2211  int poc_cycle_cnt = (abs_frame_num - 1) / h->sps.poc_cycle_length;
2212  int frame_num_in_poc_cycle = (abs_frame_num - 1) % h->sps.poc_cycle_length;
2213 
2214  expectedpoc = poc_cycle_cnt * expected_delta_per_poc_cycle;
2215  for (i = 0; i <= frame_num_in_poc_cycle; i++)
2216  expectedpoc = expectedpoc + h->sps.offset_for_ref_frame[i];
2217  } else
2218  expectedpoc = 0;
2219 
2220  if (h->nal_ref_idc == 0)
2221  expectedpoc = expectedpoc + h->sps.offset_for_non_ref_pic;
2222 
2223  field_poc[0] = expectedpoc + h->delta_poc[0];
2224  field_poc[1] = field_poc[0] + h->sps.offset_for_top_to_bottom_field;
2225 
2226  if (s->picture_structure == PICT_FRAME)
2227  field_poc[1] += h->delta_poc[1];
2228  } else {
2229  int poc = 2 * (h->frame_num_offset + h->frame_num);
2230 
2231  if (!h->nal_ref_idc)
2232  poc--;
2233 
2234  field_poc[0] = poc;
2235  field_poc[1] = poc;
2236  }
2237 
2239  s->current_picture_ptr->field_poc[0] = field_poc[0];
2241  s->current_picture_ptr->field_poc[1] = field_poc[1];
2242  cur->poc = FFMIN(cur->field_poc[0], cur->field_poc[1]);
2243 
2244  return 0;
2245 }
2246 
2251 {
2252  int i;
2253  for (i = 0; i < 16; i++) {
2254 #define T(x) (x >> 2) | ((x << 2) & 0xF)
2255  h->zigzag_scan[i] = T(zigzag_scan[i]);
2256  h->field_scan[i] = T(field_scan[i]);
2257 #undef T
2258  }
2259  for (i = 0; i < 64; i++) {
2260 #define T(x) (x >> 3) | ((x & 7) << 3)
2261  h->zigzag_scan8x8[i] = T(ff_zigzag_direct[i]);
2263  h->field_scan8x8[i] = T(field_scan8x8[i]);
2265 #undef T
2266  }
2267  if (h->sps.transform_bypass) { // FIXME same ugly
2274  } else {
2275  h->zigzag_scan_q0 = h->zigzag_scan;
2278  h->field_scan_q0 = h->field_scan;
2281  }
2282 }
2283 
2284 static int field_end(H264Context *h, int in_setup)
2285 {
2286  MpegEncContext *const s = &h->s;
2287  AVCodecContext *const avctx = s->avctx;
2288  int err = 0;
2289  s->mb_y = 0;
2290 
2291  if (!in_setup && !s->droppable)
2294 
2298 
2299  if (in_setup || !(avctx->active_thread_type & FF_THREAD_FRAME)) {
2300  if (!s->droppable) {
2302  h->prev_poc_msb = h->poc_msb;
2303  h->prev_poc_lsb = h->poc_lsb;
2304  }
2306  h->prev_frame_num = h->frame_num;
2308  }
2309 
2310  if (avctx->hwaccel) {
2311  if (avctx->hwaccel->end_frame(avctx) < 0)
2312  av_log(avctx, AV_LOG_ERROR,
2313  "hardware accelerator failed to decode picture\n");
2314  }
2315 
2319 
2320  /*
2321  * FIXME: Error handling code does not seem to support interlaced
2322  * when slices span multiple rows
2323  * The ff_er_add_slice calls don't work right for bottom
2324  * fields; they cause massive erroneous error concealing
2325  * Error marking covers both fields (top and bottom).
2326  * This causes a mismatched s->error_count
2327  * and a bad error table. Further, the error count goes to
2328  * INT_MAX when called for bottom field, because mb_y is
2329  * past end by one (callers fault) and resync_mb_y != 0
2330  * causes problems for the first MB line, too.
2331  */
2332  if (!FIELD_PICTURE)
2333  ff_er_frame_end(s);
2334 
2335  ff_MPV_frame_end(s);
2336 
2337  h->current_slice = 0;
2338 
2339  return err;
2340 }
2341 
2345 static int clone_slice(H264Context *dst, H264Context *src)
2346 {
2347  int ret;
2348 
2349  memcpy(dst->block_offset, src->block_offset, sizeof(dst->block_offset));
2351  dst->s.current_picture = src->s.current_picture;
2352  dst->s.linesize = src->s.linesize;
2353  dst->s.uvlinesize = src->s.uvlinesize;
2354  dst->s.first_field = src->s.first_field;
2355 
2356  if (!dst->s.edge_emu_buffer &&
2357  (ret = ff_mpv_frame_size_alloc(&dst->s, dst->s.linesize))) {
2358  av_log(dst->s.avctx, AV_LOG_ERROR,
2359  "Failed to allocate scratch buffers\n");
2360  return ret;
2361  }
2362 
2363  dst->prev_poc_msb = src->prev_poc_msb;
2364  dst->prev_poc_lsb = src->prev_poc_lsb;
2366  dst->prev_frame_num = src->prev_frame_num;
2367  dst->short_ref_count = src->short_ref_count;
2368 
2369  memcpy(dst->short_ref, src->short_ref, sizeof(dst->short_ref));
2370  memcpy(dst->long_ref, src->long_ref, sizeof(dst->long_ref));
2371  memcpy(dst->default_ref_list, src->default_ref_list, sizeof(dst->default_ref_list));
2372  memcpy(dst->ref_list, src->ref_list, sizeof(dst->ref_list));
2373 
2374  memcpy(dst->dequant4_coeff, src->dequant4_coeff, sizeof(src->dequant4_coeff));
2375  memcpy(dst->dequant8_coeff, src->dequant8_coeff, sizeof(src->dequant8_coeff));
2376 
2377  return 0;
2378 }
2379 
2388 {
2389  int profile = sps->profile_idc;
2390 
2391  switch (sps->profile_idc) {
2393  // constraint_set1_flag set to 1
2394  profile |= (sps->constraint_set_flags & 1 << 1) ? FF_PROFILE_H264_CONSTRAINED : 0;
2395  break;
2399  // constraint_set3_flag set to 1
2400  profile |= (sps->constraint_set_flags & 1 << 3) ? FF_PROFILE_H264_INTRA : 0;
2401  break;
2402  }
2403 
2404  return profile;
2405 }
2406 
2408 {
2409  MpegEncContext *s = &h->s;
2410 
2411  if (s->flags & CODEC_FLAG_LOW_DELAY ||
2413  !h->sps.num_reorder_frames)) {
2414  if (s->avctx->has_b_frames > 1 || h->delayed_pic[0])
2415  av_log(h->s.avctx, AV_LOG_WARNING, "Delayed frames seen. "
2416  "Reenabling low delay requires a codec flush.\n");
2417  else
2418  s->low_delay = 1;
2419  }
2420 
2421  if (s->avctx->has_b_frames < 2)
2422  s->avctx->has_b_frames = !s->low_delay;
2423 
2424  if (h->sps.bit_depth_luma != h->sps.bit_depth_chroma) {
2426  "Different bit depth between chroma and luma", 1);
2427  return AVERROR_PATCHWELCOME;
2428  }
2429 
2430  if (s->avctx->bits_per_raw_sample != h->sps.bit_depth_luma ||
2432  if (s->avctx->codec &&
2434  (h->sps.bit_depth_luma != 8 || h->sps.chroma_format_idc > 1)) {
2436  "VDPAU decoding does not support video colorspace.\n");
2437  return AVERROR_INVALIDDATA;
2438  }
2439  if (h->sps.bit_depth_luma >= 8 && h->sps.bit_depth_luma <= 10) {
2442  h->pixel_shift = h->sps.bit_depth_luma > 8;
2443 
2445  h->sps.chroma_format_idc);
2447  h->sps.chroma_format_idc);
2448  s->dsp.dct_bits = h->sps.bit_depth_luma > 8 ? 32 : 16;
2449  ff_dsputil_init(&s->dsp, s->avctx);
2451  } else {
2452  av_log(s->avctx, AV_LOG_ERROR, "Unsupported bit depth: %d\n",
2453  h->sps.bit_depth_luma);
2454  return AVERROR_INVALIDDATA;
2455  }
2456  }
2457  return 0;
2458 }
2459 
2461 {
2462  MpegEncContext *const s = &h->s;
2463  switch (h->sps.bit_depth_luma) {
2464  case 9:
2465  if (CHROMA444) {
2466  if (s->avctx->colorspace == AVCOL_SPC_RGB) {
2467  return AV_PIX_FMT_GBRP9;
2468  } else
2469  return AV_PIX_FMT_YUV444P9;
2470  } else if (CHROMA422)
2471  return AV_PIX_FMT_YUV422P9;
2472  else
2473  return AV_PIX_FMT_YUV420P9;
2474  break;
2475  case 10:
2476  if (CHROMA444) {
2477  if (s->avctx->colorspace == AVCOL_SPC_RGB) {
2478  return AV_PIX_FMT_GBRP10;
2479  } else
2480  return AV_PIX_FMT_YUV444P10;
2481  } else if (CHROMA422)
2482  return AV_PIX_FMT_YUV422P10;
2483  else
2484  return AV_PIX_FMT_YUV420P10;
2485  break;
2486  case 8:
2487  if (CHROMA444) {
2488  if (s->avctx->colorspace == AVCOL_SPC_RGB) {
2489  return AV_PIX_FMT_GBRP;
2490  } else
2493  } else if (CHROMA422) {
2496  } else {
2497  return s->avctx->get_format(s->avctx, s->avctx->codec->pix_fmts ?
2498  s->avctx->codec->pix_fmts :
2502  }
2503  break;
2504  default:
2506  "Unsupported bit depth: %d\n", h->sps.bit_depth_luma);
2507  return AVERROR_INVALIDDATA;
2508  }
2509 }
2510 
2511 static int h264_slice_header_init(H264Context *h, int reinit)
2512 {
2513  MpegEncContext *const s = &h->s;
2514  int i, ret;
2515 
2517  s->avctx->sample_aspect_ratio = h->sps.sar;
2519 
2520  if (h->sps.timing_info_present_flag) {
2521  int64_t den = h->sps.time_scale;
2522  if (h->x264_build < 44U)
2523  den *= 2;
2525  h->sps.num_units_in_tick, den, 1 << 30);
2526  }
2527 
2529 
2530  if (reinit) {
2531  free_tables(h, 0);
2532  if ((ret = ff_MPV_common_frame_size_change(s)) < 0) {
2533  av_log(h->s.avctx, AV_LOG_ERROR, "ff_MPV_common_frame_size_change() failed.\n");
2534  return ret;
2535  }
2536  } else {
2537  if ((ret = ff_MPV_common_init(s) < 0)) {
2538  av_log(h->s.avctx, AV_LOG_ERROR, "ff_MPV_common_init() failed.\n");
2539  return ret;
2540  }
2541  }
2542  s->first_field = 0;
2543  h->prev_interlaced_frame = 1;
2544 
2545  init_scan_tables(h);
2546  if (ff_h264_alloc_tables(h) < 0) {
2547  av_log(h->s.avctx, AV_LOG_ERROR,
2548  "Could not allocate memory for h264\n");
2549  return AVERROR(ENOMEM);
2550  }
2551 
2553  if (context_init(h) < 0) {
2554  av_log(h->s.avctx, AV_LOG_ERROR, "context_init() failed.\n");
2555  return -1;
2556  }
2557  } else {
2558  for (i = 1; i < s->slice_context_count; i++) {
2559  H264Context *c;
2560  c = h->thread_context[i] = av_malloc(sizeof(H264Context));
2561  memcpy(c, h->s.thread_context[i], sizeof(MpegEncContext));
2562  memset(&c->s + 1, 0, sizeof(H264Context) - sizeof(MpegEncContext));
2563  c->h264dsp = h->h264dsp;
2564  c->sps = h->sps;
2565  c->pps = h->pps;
2566  c->pixel_shift = h->pixel_shift;
2567  init_scan_tables(c);
2568  clone_tables(c, h, i);
2569  }
2570 
2571  for (i = 0; i < s->slice_context_count; i++)
2572  if (context_init(h->thread_context[i]) < 0) {
2573  av_log(h->s.avctx, AV_LOG_ERROR, "context_init() failed.\n");
2574  return -1;
2575  }
2576  }
2577 
2578  return 0;
2579 }
2580 
2592 {
2593  MpegEncContext *const s = &h->s;
2594  MpegEncContext *const s0 = &h0->s;
2595  unsigned int first_mb_in_slice;
2596  unsigned int pps_id;
2597  int num_ref_idx_active_override_flag, max_refs, ret;
2598  unsigned int slice_type, tmp, i, j;
2599  int default_ref_list_done = 0;
2600  int last_pic_structure, last_pic_droppable;
2601  int needs_reinit = 0;
2602 
2603  /* FIXME: 2tap qpel isn't implemented for high bit depth. */
2604  if ((s->avctx->flags2 & CODEC_FLAG2_FAST) &&
2605  !h->nal_ref_idc && !h->pixel_shift) {
2608  } else {
2611  }
2612 
2613  first_mb_in_slice = get_ue_golomb(&s->gb);
2614 
2615  if (first_mb_in_slice == 0) { // FIXME better field boundary detection
2616  if (h0->current_slice && FIELD_PICTURE) {
2617  field_end(h, 1);
2618  }
2619 
2620  h0->current_slice = 0;
2621  if (!s0->first_field) {
2622  if (s->current_picture_ptr && !s->droppable &&
2623  s->current_picture_ptr->owner2 == s) {
2626  }
2628  }
2629  }
2630 
2631  slice_type = get_ue_golomb_31(&s->gb);
2632  if (slice_type > 9) {
2633  av_log(h->s.avctx, AV_LOG_ERROR,
2634  "slice type too large (%d) at %d %d\n",
2635  h->slice_type, s->mb_x, s->mb_y);
2636  return -1;
2637  }
2638  if (slice_type > 4) {
2639  slice_type -= 5;
2640  h->slice_type_fixed = 1;
2641  } else
2642  h->slice_type_fixed = 0;
2643 
2644  slice_type = golomb_to_pict_type[slice_type];
2645  if (slice_type == AV_PICTURE_TYPE_I ||
2646  (h0->current_slice != 0 && slice_type == h0->last_slice_type)) {
2647  default_ref_list_done = 1;
2648  }
2649  h->slice_type = slice_type;
2650  h->slice_type_nos = slice_type & 3;
2651 
2652  // to make a few old functions happy, it's wrong though
2653  s->pict_type = h->slice_type;
2654 
2655  pps_id = get_ue_golomb(&s->gb);
2656  if (pps_id >= MAX_PPS_COUNT) {
2657  av_log(h->s.avctx, AV_LOG_ERROR, "pps_id out of range\n");
2658  return -1;
2659  }
2660  if (!h0->pps_buffers[pps_id]) {
2661  av_log(h->s.avctx, AV_LOG_ERROR,
2662  "non-existing PPS %u referenced\n",
2663  pps_id);
2664  return -1;
2665  }
2666  h->pps = *h0->pps_buffers[pps_id];
2667 
2668  if (!h0->sps_buffers[h->pps.sps_id]) {
2669  av_log(h->s.avctx, AV_LOG_ERROR,
2670  "non-existing SPS %u referenced\n",
2671  h->pps.sps_id);
2672  return -1;
2673  }
2674 
2675  if (h->pps.sps_id != h->current_sps_id ||
2676  h->context_reinitialized ||
2677  h0->sps_buffers[h->pps.sps_id]->new) {
2678  SPS *new_sps = h0->sps_buffers[h->pps.sps_id];
2679 
2680  h0->sps_buffers[h->pps.sps_id]->new = 0;
2681 
2682  if (h->sps.chroma_format_idc != new_sps->chroma_format_idc ||
2683  h->sps.bit_depth_luma != new_sps->bit_depth_luma)
2684  needs_reinit = 1;
2685 
2686  h->current_sps_id = h->pps.sps_id;
2687  h->sps = *h0->sps_buffers[h->pps.sps_id];
2688 
2689  if ((ret = h264_set_parameter_from_sps(h)) < 0)
2690  return ret;
2691  }
2692 
2693  s->avctx->profile = ff_h264_get_profile(&h->sps);
2694  s->avctx->level = h->sps.level_idc;
2695  s->avctx->refs = h->sps.ref_frame_count;
2696 
2697  if (s->mb_width != h->sps.mb_width ||
2698  s->mb_height != h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag))
2699  needs_reinit = 1;
2700 
2701  s->mb_width = h->sps.mb_width;
2702  s->mb_height = h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag);
2703 
2704  h->b_stride = s->mb_width * 4;
2705 
2706  s->chroma_y_shift = h->sps.chroma_format_idc <= 1; // 400 uses yuv420p
2707 
2708  s->width = 16 * s->mb_width - (2 >> CHROMA444) * FFMIN(h->sps.crop_right, (8 << CHROMA444) - 1);
2709  if (h->sps.frame_mbs_only_flag)
2710  s->height = 16 * s->mb_height - (1 << s->chroma_y_shift) * FFMIN(h->sps.crop_bottom, (16 >> s->chroma_y_shift) - 1);
2711  else
2712  s->height = 16 * s->mb_height - (2 << s->chroma_y_shift) * FFMIN(h->sps.crop_bottom, (16 >> s->chroma_y_shift) - 1);
2713 
2714  if (FFALIGN(s->avctx->width, 16) == s->width &&
2715  FFALIGN(s->avctx->height, 16) == s->height) {
2716  s->width = s->avctx->width;
2717  s->height = s->avctx->height;
2718  }
2719 
2722  : AVCOL_RANGE_MPEG;
2724  if (s->avctx->colorspace != h->sps.colorspace)
2725  needs_reinit = 1;
2727  s->avctx->color_trc = h->sps.color_trc;
2728  s->avctx->colorspace = h->sps.colorspace;
2729  }
2730  }
2731 
2732  if (s->context_initialized &&
2733  (s->width != s->avctx->width ||
2734  s->height != s->avctx->height ||
2735  needs_reinit ||
2737 
2738  if (h != h0) {
2739  av_log(s->avctx, AV_LOG_ERROR, "changing width/height on "
2740  "slice %d\n", h0->current_slice + 1);
2741  return AVERROR_INVALIDDATA;
2742  }
2743 
2744  flush_change(h);
2745 
2746  if ((ret = get_pixel_format(h)) < 0)
2747  return ret;
2748  s->avctx->pix_fmt = ret;
2749 
2750  av_log(h->s.avctx, AV_LOG_INFO, "Reinit context to %dx%d, "
2751  "pix_fmt: %d\n", s->width, s->height, s->avctx->pix_fmt);
2752 
2753  if ((ret = h264_slice_header_init(h, 1)) < 0) {
2754  av_log(h->s.avctx, AV_LOG_ERROR,
2755  "h264_slice_header_init() failed\n");
2756  return ret;
2757  }
2758  h->context_reinitialized = 1;
2759  }
2760  if (!s->context_initialized) {
2761  if (h != h0) {
2762  av_log(h->s.avctx, AV_LOG_ERROR,
2763  "Cannot (re-)initialize context during parallel decoding.\n");
2764  return -1;
2765  }
2766 
2767  if ((ret = get_pixel_format(h)) < 0)
2768  return ret;
2769  s->avctx->pix_fmt = ret;
2770 
2771  if ((ret = h264_slice_header_init(h, 0)) < 0) {
2772  av_log(h->s.avctx, AV_LOG_ERROR,
2773  "h264_slice_header_init() failed\n");
2774  return ret;
2775  }
2776  }
2777 
2778  if (h == h0 && h->dequant_coeff_pps != pps_id) {
2779  h->dequant_coeff_pps = pps_id;
2781  }
2782 
2783  h->frame_num = get_bits(&s->gb, h->sps.log2_max_frame_num);
2784 
2785  h->mb_mbaff = 0;
2786  h->mb_aff_frame = 0;
2787  last_pic_structure = s0->picture_structure;
2788  last_pic_droppable = s0->droppable;
2789  s->droppable = h->nal_ref_idc == 0;
2790  if (h->sps.frame_mbs_only_flag) {
2792  } else {
2793  if (get_bits1(&s->gb)) { // field_pic_flag
2794  s->picture_structure = PICT_TOP_FIELD + get_bits1(&s->gb); // bottom_field_flag
2795  } else {
2797  h->mb_aff_frame = h->sps.mb_aff;
2798  }
2799  }
2801 
2802  if (h0->current_slice != 0) {
2803  if (last_pic_structure != s->picture_structure ||
2804  last_pic_droppable != s->droppable) {
2805  av_log(h->s.avctx, AV_LOG_ERROR,
2806  "Changing field mode (%d -> %d) between slices is not allowed\n",
2807  last_pic_structure, s->picture_structure);
2808  s->picture_structure = last_pic_structure;
2809  s->droppable = last_pic_droppable;
2810  return AVERROR_INVALIDDATA;
2811  } else if (!s0->current_picture_ptr) {
2813  "unset current_picture_ptr on %d. slice\n",
2814  h0->current_slice + 1);
2815  return AVERROR_INVALIDDATA;
2816  }
2817  } else {
2818  /* Shorten frame num gaps so we don't have to allocate reference
2819  * frames just to throw them away */
2820  if (h->frame_num != h->prev_frame_num) {
2821  int unwrap_prev_frame_num = h->prev_frame_num;
2822  int max_frame_num = 1 << h->sps.log2_max_frame_num;
2823 
2824  if (unwrap_prev_frame_num > h->frame_num)
2825  unwrap_prev_frame_num -= max_frame_num;
2826 
2827  if ((h->frame_num - unwrap_prev_frame_num) > h->sps.ref_frame_count) {
2828  unwrap_prev_frame_num = (h->frame_num - h->sps.ref_frame_count) - 1;
2829  if (unwrap_prev_frame_num < 0)
2830  unwrap_prev_frame_num += max_frame_num;
2831 
2832  h->prev_frame_num = unwrap_prev_frame_num;
2833  }
2834  }
2835 
2836  /* See if we have a decoded first field looking for a pair...
2837  * Here, we're using that to see if we should mark previously
2838  * decode frames as "finished".
2839  * We have to do that before the "dummy" in-between frame allocation,
2840  * since that can modify s->current_picture_ptr. */
2841  if (s0->first_field) {
2842  assert(s0->current_picture_ptr);
2843  assert(s0->current_picture_ptr->f.data[0]);
2845 
2846  /* Mark old field/frame as completed */
2847  if (!last_pic_droppable && s0->current_picture_ptr->owner2 == s0) {
2849  last_pic_structure == PICT_BOTTOM_FIELD);
2850  }
2851 
2852  /* figure out if we have a complementary field pair */
2853  if (!FIELD_PICTURE || s->picture_structure == last_pic_structure) {
2854  /* Previous field is unmatched. Don't display it, but let it
2855  * remain for reference if marked as such. */
2856  if (!last_pic_droppable && last_pic_structure != PICT_FRAME) {
2858  last_pic_structure == PICT_TOP_FIELD);
2859  }
2860  } else {
2861  if (s0->current_picture_ptr->frame_num != h->frame_num) {
2862  /* This and previous field were reference, but had
2863  * different frame_nums. Consider this field first in
2864  * pair. Throw away previous field except for reference
2865  * purposes. */
2866  if (!last_pic_droppable && last_pic_structure != PICT_FRAME) {
2868  last_pic_structure == PICT_TOP_FIELD);
2869  }
2870  } else {
2871  /* Second field in complementary pair */
2872  if (!((last_pic_structure == PICT_TOP_FIELD &&
2874  (last_pic_structure == PICT_BOTTOM_FIELD &&
2877  "Invalid field mode combination %d/%d\n",
2878  last_pic_structure, s->picture_structure);
2879  s->picture_structure = last_pic_structure;
2880  s->droppable = last_pic_droppable;
2881  return AVERROR_INVALIDDATA;
2882  } else if (last_pic_droppable != s->droppable) {
2884  "Cannot combine reference and non-reference fields in the same frame\n");
2886  s->picture_structure = last_pic_structure;
2887  s->droppable = last_pic_droppable;
2888  return AVERROR_PATCHWELCOME;
2889  }
2890 
2891  /* Take ownership of this buffer. Note that if another thread owned
2892  * the first field of this buffer, we're not operating on that pointer,
2893  * so the original thread is still responsible for reporting progress
2894  * on that first field (or if that was us, we just did that above).
2895  * By taking ownership, we assign responsibility to ourselves to
2896  * report progress on the second field. */
2897  s0->current_picture_ptr->owner2 = s0;
2898  }
2899  }
2900  }
2901 
2902  while (h->frame_num != h->prev_frame_num &&
2903  h->frame_num != (h->prev_frame_num + 1) % (1 << h->sps.log2_max_frame_num)) {
2904  Picture *prev = h->short_ref_count ? h->short_ref[0] : NULL;
2905  av_log(h->s.avctx, AV_LOG_DEBUG, "Frame num gap %d %d\n",
2906  h->frame_num, h->prev_frame_num);
2907  if (ff_h264_frame_start(h) < 0)
2908  return -1;
2909  h->prev_frame_num++;
2910  h->prev_frame_num %= 1 << h->sps.log2_max_frame_num;
2914  if ((ret = ff_generate_sliding_window_mmcos(h, 1)) < 0 &&
2916  return ret;
2917  if (ff_h264_execute_ref_pic_marking(h, h->mmco, h->mmco_index) < 0 &&
2919  return AVERROR_INVALIDDATA;
2920  /* Error concealment: if a ref is missing, copy the previous ref in its place.
2921  * FIXME: avoiding a memcpy would be nice, but ref handling makes many assumptions
2922  * about there being no actual duplicates.
2923  * FIXME: this doesn't copy padding for out-of-frame motion vectors. Given we're
2924  * concealing a lost frame, this probably isn't noticeable by comparison, but it should
2925  * be fixed. */
2926  if (h->short_ref_count) {
2927  if (prev) {
2928  av_image_copy(h->short_ref[0]->f.data, h->short_ref[0]->f.linesize,
2929  (const uint8_t **)prev->f.data, prev->f.linesize,
2930  s->avctx->pix_fmt, s->mb_width * 16, s->mb_height * 16);
2931  h->short_ref[0]->poc = prev->poc + 2;
2932  }
2933  h->short_ref[0]->frame_num = h->prev_frame_num;
2934  }
2935  }
2936 
2937  /* See if we have a decoded first field looking for a pair...
2938  * We're using that to see whether to continue decoding in that
2939  * frame, or to allocate a new one. */
2940  if (s0->first_field) {
2941  assert(s0->current_picture_ptr);
2942  assert(s0->current_picture_ptr->f.data[0]);
2944 
2945  /* figure out if we have a complementary field pair */
2946  if (!FIELD_PICTURE || s->picture_structure == last_pic_structure) {
2947  /* Previous field is unmatched. Don't display it, but let it
2948  * remain for reference if marked as such. */
2949  s0->current_picture_ptr = NULL;
2950  s0->first_field = FIELD_PICTURE;
2951  } else {
2952  if (s0->current_picture_ptr->frame_num != h->frame_num) {
2953  /* This and the previous field had different frame_nums.
2954  * Consider this field first in pair. Throw away previous
2955  * one except for reference purposes. */
2956  s0->first_field = 1;
2957  s0->current_picture_ptr = NULL;
2958  } else {
2959  /* Second field in complementary pair */
2960  s0->first_field = 0;
2961  }
2962  }
2963  } else {
2964  /* Frame or first field in a potentially complementary pair */
2965  s0->first_field = FIELD_PICTURE;
2966  }
2967 
2968  if (!FIELD_PICTURE || s0->first_field) {
2969  if (ff_h264_frame_start(h) < 0) {
2970  s0->first_field = 0;
2971  return -1;
2972  }
2973  } else {
2975  }
2976  }
2977  if (h != h0 && (ret = clone_slice(h, h0)) < 0)
2978  return ret;
2979 
2980  s->current_picture_ptr->frame_num = h->frame_num; // FIXME frame_num cleanup
2981 
2982  assert(s->mb_num == s->mb_width * s->mb_height);
2983  if (first_mb_in_slice << FIELD_OR_MBAFF_PICTURE >= s->mb_num ||
2984  first_mb_in_slice >= s->mb_num) {
2985  av_log(h->s.avctx, AV_LOG_ERROR, "first_mb_in_slice overflow\n");
2986  return -1;
2987  }
2988  s->resync_mb_x = s->mb_x = first_mb_in_slice % s->mb_width;
2989  s->resync_mb_y = s->mb_y = (first_mb_in_slice / s->mb_width) << FIELD_OR_MBAFF_PICTURE;
2991  s->resync_mb_y = s->mb_y = s->mb_y + 1;
2992  assert(s->mb_y < s->mb_height);
2993 
2994  if (s->picture_structure == PICT_FRAME) {
2995  h->curr_pic_num = h->frame_num;
2996  h->max_pic_num = 1 << h->sps.log2_max_frame_num;
2997  } else {
2998  h->curr_pic_num = 2 * h->frame_num + 1;
2999  h->max_pic_num = 1 << (h->sps.log2_max_frame_num + 1);
3000  }
3001 
3002  if (h->nal_unit_type == NAL_IDR_SLICE)
3003  get_ue_golomb(&s->gb); /* idr_pic_id */
3004 
3005  if (h->sps.poc_type == 0) {
3006  h->poc_lsb = get_bits(&s->gb, h->sps.log2_max_poc_lsb);
3007 
3008  if (h->pps.pic_order_present == 1 && s->picture_structure == PICT_FRAME)
3009  h->delta_poc_bottom = get_se_golomb(&s->gb);
3010  }
3011 
3012  if (h->sps.poc_type == 1 && !h->sps.delta_pic_order_always_zero_flag) {
3013  h->delta_poc[0] = get_se_golomb(&s->gb);
3014 
3015  if (h->pps.pic_order_present == 1 && s->picture_structure == PICT_FRAME)
3016  h->delta_poc[1] = get_se_golomb(&s->gb);
3017  }
3018 
3019  init_poc(h);
3020 
3023 
3024  // set defaults, might be overridden a few lines later
3025  h->ref_count[0] = h->pps.ref_count[0];
3026  h->ref_count[1] = h->pps.ref_count[1];
3027 
3028  if (h->slice_type_nos != AV_PICTURE_TYPE_I) {
3031  num_ref_idx_active_override_flag = get_bits1(&s->gb);
3032 
3033  if (num_ref_idx_active_override_flag) {
3034  h->ref_count[0] = get_ue_golomb(&s->gb) + 1;
3035  if (h->ref_count[0] < 1)
3036  return AVERROR_INVALIDDATA;
3037  if (h->slice_type_nos == AV_PICTURE_TYPE_B) {
3038  h->ref_count[1] = get_ue_golomb(&s->gb) + 1;
3039  if (h->ref_count[1] < 1)
3040  return AVERROR_INVALIDDATA;
3041  }
3042  }
3043 
3045  h->list_count = 2;
3046  else
3047  h->list_count = 1;
3048  } else {
3049  h->list_count = 0;
3050  h->ref_count[0] = h->ref_count[1] = 0;
3051  }
3052 
3053 
3054  max_refs = s->picture_structure == PICT_FRAME ? 16 : 32;
3055 
3056  if (h->ref_count[0] > max_refs || h->ref_count[1] > max_refs) {
3057  av_log(h->s.avctx, AV_LOG_ERROR, "reference overflow\n");
3058  h->ref_count[0] = h->ref_count[1] = 0;
3059  return AVERROR_INVALIDDATA;
3060  }
3061 
3062  if (!default_ref_list_done)
3064 
3065  if (h->slice_type_nos != AV_PICTURE_TYPE_I &&
3067  h->ref_count[1] = h->ref_count[0] = 0;
3068  return -1;
3069  }
3070 
3071  if (h->slice_type_nos != AV_PICTURE_TYPE_I) {
3072  s->last_picture_ptr = &h->ref_list[0][0];
3073  s->last_picture_ptr->owner2 = s;
3075  }
3076  if (h->slice_type_nos == AV_PICTURE_TYPE_B) {
3077  s->next_picture_ptr = &h->ref_list[1][0];
3078  s->next_picture_ptr->owner2 = s;
3080  }
3081 
3082  if ((h->pps.weighted_pred && h->slice_type_nos == AV_PICTURE_TYPE_P) ||
3083  (h->pps.weighted_bipred_idc == 1 &&
3085  pred_weight_table(h);
3086  else if (h->pps.weighted_bipred_idc == 2 &&
3088  implicit_weight_table(h, -1);
3089  } else {
3090  h->use_weight = 0;
3091  for (i = 0; i < 2; i++) {
3092  h->luma_weight_flag[i] = 0;
3093  h->chroma_weight_flag[i] = 0;
3094  }
3095  }
3096 
3097  // If frame-mt is enabled, only update mmco tables for the first slice
3098  // in a field. Subsequent slices can temporarily clobber h->mmco_index
3099  // or h->mmco, which will cause ref list mix-ups and decoding errors
3100  // further down the line. This may break decoding if the first slice is
3101  // corrupt, thus we only do this if frame-mt is enabled.
3102  if (h->nal_ref_idc &&
3105  h0->current_slice == 0) < 0 &&
3107  return AVERROR_INVALIDDATA;
3108 
3109  if (FRAME_MBAFF) {
3111 
3113  implicit_weight_table(h, 0);
3114  implicit_weight_table(h, 1);
3115  }
3116  }
3117 
3121 
3122  if (h->slice_type_nos != AV_PICTURE_TYPE_I && h->pps.cabac) {
3123  tmp = get_ue_golomb_31(&s->gb);
3124  if (tmp > 2) {
3125  av_log(s->avctx, AV_LOG_ERROR, "cabac_init_idc overflow\n");
3126  return -1;
3127  }
3128  h->cabac_init_idc = tmp;
3129  }
3130 
3131  h->last_qscale_diff = 0;
3132  tmp = h->pps.init_qp + get_se_golomb(&s->gb);
3133  if (tmp > 51 + 6 * (h->sps.bit_depth_luma - 8)) {
3134  av_log(s->avctx, AV_LOG_ERROR, "QP %u out of range\n", tmp);
3135  return -1;
3136  }
3137  s->qscale = tmp;
3138  h->chroma_qp[0] = get_chroma_qp(h, 0, s->qscale);
3139  h->chroma_qp[1] = get_chroma_qp(h, 1, s->qscale);
3140  // FIXME qscale / qp ... stuff
3141  if (h->slice_type == AV_PICTURE_TYPE_SP)
3142  get_bits1(&s->gb); /* sp_for_switch_flag */
3143  if (h->slice_type == AV_PICTURE_TYPE_SP ||
3145  get_se_golomb(&s->gb); /* slice_qs_delta */
3146 
3147  h->deblocking_filter = 1;
3148  h->slice_alpha_c0_offset = 52;
3149  h->slice_beta_offset = 52;
3151  tmp = get_ue_golomb_31(&s->gb);
3152  if (tmp > 2) {
3154  "deblocking_filter_idc %u out of range\n", tmp);
3155  return -1;
3156  }
3157  h->deblocking_filter = tmp;
3158  if (h->deblocking_filter < 2)
3159  h->deblocking_filter ^= 1; // 1<->0
3160 
3161  if (h->deblocking_filter) {
3162  h->slice_alpha_c0_offset += get_se_golomb(&s->gb) << 1;
3163  h->slice_beta_offset += get_se_golomb(&s->gb) << 1;
3164  if (h->slice_alpha_c0_offset > 104U ||
3165  h->slice_beta_offset > 104U) {
3167  "deblocking filter parameters %d %d out of range\n",
3169  return -1;
3170  }
3171  }
3172  }
3173 
3174  if (s->avctx->skip_loop_filter >= AVDISCARD_ALL ||
3180  h->nal_ref_idc == 0))
3181  h->deblocking_filter = 0;
3182 
3183  if (h->deblocking_filter == 1 && h0->max_contexts > 1) {
3184  if (s->avctx->flags2 & CODEC_FLAG2_FAST) {
3185  /* Cheat slightly for speed:
3186  * Do not bother to deblock across slices. */
3187  h->deblocking_filter = 2;
3188  } else {
3189  h0->max_contexts = 1;
3190  if (!h0->single_decode_warning) {
3191  av_log(s->avctx, AV_LOG_INFO,
3192  "Cannot parallelize deblocking type 1, decoding such frames in sequential order\n");
3193  h0->single_decode_warning = 1;
3194  }
3195  if (h != h0) {
3196  av_log(h->s.avctx, AV_LOG_ERROR,
3197  "Deblocking switched inside frame.\n");
3198  return 1;
3199  }
3200  }
3201  }
3202  h->qp_thresh = 15 + 52 -
3204  FFMAX3(0,
3206  h->pps.chroma_qp_index_offset[1]) +
3207  6 * (h->sps.bit_depth_luma - 8);
3208 
3209  h0->last_slice_type = slice_type;
3210  h->slice_num = ++h0->current_slice;
3211  if (h->slice_num >= MAX_SLICES) {
3213  "Too many slices, increase MAX_SLICES and recompile\n");
3214  }
3215 
3216  for (j = 0; j < 2; j++) {
3217  int id_list[16];
3218  int *ref2frm = h->ref2frm[h->slice_num & (MAX_SLICES - 1)][j];
3219  for (i = 0; i < 16; i++) {
3220  id_list[i] = 60;
3221  if (h->ref_list[j][i].f.data[0]) {
3222  int k;
3223  uint8_t *base = h->ref_list[j][i].f.base[0];
3224  for (k = 0; k < h->short_ref_count; k++)
3225  if (h->short_ref[k]->f.base[0] == base) {
3226  id_list[i] = k;
3227  break;
3228  }
3229  for (k = 0; k < h->long_ref_count; k++)
3230  if (h->long_ref[k] && h->long_ref[k]->f.base[0] == base) {
3231  id_list[i] = h->short_ref_count + k;
3232  break;
3233  }
3234  }
3235  }
3236 
3237  ref2frm[0] =
3238  ref2frm[1] = -1;
3239  for (i = 0; i < 16; i++)
3240  ref2frm[i + 2] = 4 * id_list[i] +
3241  (h->ref_list[j][i].f.reference & 3);
3242  ref2frm[18 + 0] =
3243  ref2frm[18 + 1] = -1;
3244  for (i = 16; i < 48; i++)
3245  ref2frm[i + 4] = 4 * id_list[(i - 16) >> 1] +
3246  (h->ref_list[j][i].f.reference & 3);
3247  }
3248 
3249  // FIXME: fix draw_edges + PAFF + frame threads
3251  (!h->sps.frame_mbs_only_flag &&
3253  ? 0 : 16;
3255 
3256  if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
3257  av_log(h->s.avctx, AV_LOG_DEBUG,
3258  "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",
3259  h->slice_num,
3260  (s->picture_structure == PICT_FRAME ? "F" : s->picture_structure == PICT_TOP_FIELD ? "T" : "B"),
3261  first_mb_in_slice,
3263  h->slice_type_fixed ? " fix" : "",
3264  h->nal_unit_type == NAL_IDR_SLICE ? " IDR" : "",
3265  pps_id, h->frame_num,
3268  h->ref_count[0], h->ref_count[1],
3269  s->qscale,
3270  h->deblocking_filter,
3271  h->slice_alpha_c0_offset / 2 - 26, h->slice_beta_offset / 2 - 26,
3272  h->use_weight,
3273  h->use_weight == 1 && h->use_weight_chroma ? "c" : "",
3274  h->slice_type == AV_PICTURE_TYPE_B ? (h->direct_spatial_mv_pred ? "SPAT" : "TEMP") : "");
3275  }
3276 
3277  return 0;
3278 }
3279 
3281 {
3282  switch (h->slice_type) {
3283  case AV_PICTURE_TYPE_P:
3284  return 0;
3285  case AV_PICTURE_TYPE_B:
3286  return 1;
3287  case AV_PICTURE_TYPE_I:
3288  return 2;
3289  case AV_PICTURE_TYPE_SP:
3290  return 3;
3291  case AV_PICTURE_TYPE_SI:
3292  return 4;
3293  default:
3294  return -1;
3295  }
3296 }
3297 
3299  MpegEncContext *const s,
3300  int mb_type, int top_xy,
3301  int left_xy[LEFT_MBS],
3302  int top_type,
3303  int left_type[LEFT_MBS],
3304  int mb_xy, int list)
3305 {
3306  int b_stride = h->b_stride;
3307  int16_t(*mv_dst)[2] = &h->mv_cache[list][scan8[0]];
3308  int8_t *ref_cache = &h->ref_cache[list][scan8[0]];
3309  if (IS_INTER(mb_type) || IS_DIRECT(mb_type)) {
3310  if (USES_LIST(top_type, list)) {
3311  const int b_xy = h->mb2b_xy[top_xy] + 3 * b_stride;
3312  const int b8_xy = 4 * top_xy + 2;
3313  int (*ref2frm)[64] = h->ref2frm[h->slice_table[top_xy] & (MAX_SLICES - 1)][0] + (MB_MBAFF ? 20 : 2);
3314  AV_COPY128(mv_dst - 1 * 8, s->current_picture.f.motion_val[list][b_xy + 0]);
3315  ref_cache[0 - 1 * 8] =
3316  ref_cache[1 - 1 * 8] = ref2frm[list][s->current_picture.f.ref_index[list][b8_xy + 0]];
3317  ref_cache[2 - 1 * 8] =
3318  ref_cache[3 - 1 * 8] = ref2frm[list][s->current_picture.f.ref_index[list][b8_xy + 1]];
3319  } else {
3320  AV_ZERO128(mv_dst - 1 * 8);
3321  AV_WN32A(&ref_cache[0 - 1 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
3322  }
3323 
3324  if (!IS_INTERLACED(mb_type ^ left_type[LTOP])) {
3325  if (USES_LIST(left_type[LTOP], list)) {
3326  const int b_xy = h->mb2b_xy[left_xy[LTOP]] + 3;
3327  const int b8_xy = 4 * left_xy[LTOP] + 1;
3328  int (*ref2frm)[64] = h->ref2frm[h->slice_table[left_xy[LTOP]] & (MAX_SLICES - 1)][0] + (MB_MBAFF ? 20 : 2);
3329  AV_COPY32(mv_dst - 1 + 0, s->current_picture.f.motion_val[list][b_xy + b_stride * 0]);
3330  AV_COPY32(mv_dst - 1 + 8, s->current_picture.f.motion_val[list][b_xy + b_stride * 1]);
3331  AV_COPY32(mv_dst - 1 + 16, s->current_picture.f.motion_val[list][b_xy + b_stride * 2]);
3332  AV_COPY32(mv_dst - 1 + 24, s->current_picture.f.motion_val[list][b_xy + b_stride * 3]);
3333  ref_cache[-1 + 0] =
3334  ref_cache[-1 + 8] = ref2frm[list][s->current_picture.f.ref_index[list][b8_xy + 2 * 0]];
3335  ref_cache[-1 + 16] =
3336  ref_cache[-1 + 24] = ref2frm[list][s->current_picture.f.ref_index[list][b8_xy + 2 * 1]];
3337  } else {
3338  AV_ZERO32(mv_dst - 1 + 0);
3339  AV_ZERO32(mv_dst - 1 + 8);
3340  AV_ZERO32(mv_dst - 1 + 16);
3341  AV_ZERO32(mv_dst - 1 + 24);
3342  ref_cache[-1 + 0] =
3343  ref_cache[-1 + 8] =
3344  ref_cache[-1 + 16] =
3345  ref_cache[-1 + 24] = LIST_NOT_USED;
3346  }
3347  }
3348  }
3349 
3350  if (!USES_LIST(mb_type, list)) {
3351  fill_rectangle(mv_dst, 4, 4, 8, pack16to32(0, 0), 4);
3352  AV_WN32A(&ref_cache[0 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
3353  AV_WN32A(&ref_cache[1 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
3354  AV_WN32A(&ref_cache[2 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
3355  AV_WN32A(&ref_cache[3 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
3356  return;
3357  }
3358 
3359  {
3360  int8_t *ref = &s->current_picture.f.ref_index[list][4 * mb_xy];
3361  int (*ref2frm)[64] = h->ref2frm[h->slice_num & (MAX_SLICES - 1)][0] + (MB_MBAFF ? 20 : 2);
3362  uint32_t ref01 = (pack16to32(ref2frm[list][ref[0]], ref2frm[list][ref[1]]) & 0x00FF00FF) * 0x0101;
3363  uint32_t ref23 = (pack16to32(ref2frm[list][ref[2]], ref2frm[list][ref[3]]) & 0x00FF00FF) * 0x0101;
3364  AV_WN32A(&ref_cache[0 * 8], ref01);
3365  AV_WN32A(&ref_cache[1 * 8], ref01);
3366  AV_WN32A(&ref_cache[2 * 8], ref23);
3367  AV_WN32A(&ref_cache[3 * 8], ref23);
3368  }
3369 
3370  {
3371  int16_t(*mv_src)[2] = &s->current_picture.f.motion_val[list][4 * s->mb_x + 4 * s->mb_y * b_stride];
3372  AV_COPY128(mv_dst + 8 * 0, mv_src + 0 * b_stride);
3373  AV_COPY128(mv_dst + 8 * 1, mv_src + 1 * b_stride);
3374  AV_COPY128(mv_dst + 8 * 2, mv_src + 2 * b_stride);
3375  AV_COPY128(mv_dst + 8 * 3, mv_src + 3 * b_stride);
3376  }
3377 }
3378 
3383 static int fill_filter_caches(H264Context *h, int mb_type)
3384 {
3385  MpegEncContext *const s = &h->s;
3386  const int mb_xy = h->mb_xy;
3387  int top_xy, left_xy[LEFT_MBS];
3388  int top_type, left_type[LEFT_MBS];
3389  uint8_t *nnz;
3390  uint8_t *nnz_cache;
3391 
3392  top_xy = mb_xy - (s->mb_stride << MB_FIELD);
3393 
3394  /* Wow, what a mess, why didn't they simplify the interlacing & intra
3395  * stuff, I can't imagine that these complex rules are worth it. */
3396 
3397  left_xy[LBOT] = left_xy[LTOP] = mb_xy - 1;
3398  if (FRAME_MBAFF) {
3399  const int left_mb_field_flag = IS_INTERLACED(s->current_picture.f.mb_type[mb_xy - 1]);
3400  const int curr_mb_field_flag = IS_INTERLACED(mb_type);
3401  if (s->mb_y & 1) {
3402  if (left_mb_field_flag != curr_mb_field_flag)
3403  left_xy[LTOP] -= s->mb_stride;
3404  } else {
3405  if (curr_mb_field_flag)
3406  top_xy += s->mb_stride &
3407  (((s->current_picture.f.mb_type[top_xy] >> 7) & 1) - 1);
3408  if (left_mb_field_flag != curr_mb_field_flag)
3409  left_xy[LBOT] += s->mb_stride;
3410  }
3411  }
3412 
3413  h->top_mb_xy = top_xy;
3414  h->left_mb_xy[LTOP] = left_xy[LTOP];
3415  h->left_mb_xy[LBOT] = left_xy[LBOT];
3416  {
3417  /* For sufficiently low qp, filtering wouldn't do anything.
3418  * This is a conservative estimate: could also check beta_offset
3419  * and more accurate chroma_qp. */
3420  int qp_thresh = h->qp_thresh; // FIXME strictly we should store qp_thresh for each mb of a slice
3421  int qp = s->current_picture.f.qscale_table[mb_xy];
3422  if (qp <= qp_thresh &&
3423  (left_xy[LTOP] < 0 ||
3424  ((qp + s->current_picture.f.qscale_table[left_xy[LTOP]] + 1) >> 1) <= qp_thresh) &&
3425  (top_xy < 0 ||
3426  ((qp + s->current_picture.f.qscale_table[top_xy] + 1) >> 1) <= qp_thresh)) {
3427  if (!FRAME_MBAFF)
3428  return 1;
3429  if ((left_xy[LTOP] < 0 ||
3430  ((qp + s->current_picture.f.qscale_table[left_xy[LBOT]] + 1) >> 1) <= qp_thresh) &&
3431  (top_xy < s->mb_stride ||
3432  ((qp + s->current_picture.f.qscale_table[top_xy - s->mb_stride] + 1) >> 1) <= qp_thresh))
3433  return 1;
3434  }
3435  }
3436 
3437  top_type = s->current_picture.f.mb_type[top_xy];
3438  left_type[LTOP] = s->current_picture.f.mb_type[left_xy[LTOP]];
3439  left_type[LBOT] = s->current_picture.f.mb_type[left_xy[LBOT]];
3440  if (h->deblocking_filter == 2) {
3441  if (h->slice_table[top_xy] != h->slice_num)
3442  top_type = 0;
3443  if (h->slice_table[left_xy[LBOT]] != h->slice_num)
3444  left_type[LTOP] = left_type[LBOT] = 0;
3445  } else {
3446  if (h->slice_table[top_xy] == 0xFFFF)
3447  top_type = 0;
3448  if (h->slice_table[left_xy[LBOT]] == 0xFFFF)
3449  left_type[LTOP] = left_type[LBOT] = 0;
3450  }
3451  h->top_type = top_type;
3452  h->left_type[LTOP] = left_type[LTOP];
3453  h->left_type[LBOT] = left_type[LBOT];
3454 
3455  if (IS_INTRA(mb_type))
3456  return 0;
3457 
3458  fill_filter_caches_inter(h, s, mb_type, top_xy, left_xy,
3459  top_type, left_type, mb_xy, 0);
3460  if (h->list_count == 2)
3461  fill_filter_caches_inter(h, s, mb_type, top_xy, left_xy,
3462  top_type, left_type, mb_xy, 1);
3463 
3464  nnz = h->non_zero_count[mb_xy];
3465  nnz_cache = h->non_zero_count_cache;
3466  AV_COPY32(&nnz_cache[4 + 8 * 1], &nnz[0]);
3467  AV_COPY32(&nnz_cache[4 + 8 * 2], &nnz[4]);
3468  AV_COPY32(&nnz_cache[4 + 8 * 3], &nnz[8]);
3469  AV_COPY32(&nnz_cache[4 + 8 * 4], &nnz[12]);
3470  h->cbp = h->cbp_table[mb_xy];
3471 
3472  if (top_type) {
3473  nnz = h->non_zero_count[top_xy];
3474  AV_COPY32(&nnz_cache[4 + 8 * 0], &nnz[3 * 4]);
3475  }
3476 
3477  if (left_type[LTOP]) {
3478  nnz = h->non_zero_count[left_xy[LTOP]];
3479  nnz_cache[3 + 8 * 1] = nnz[3 + 0 * 4];
3480  nnz_cache[3 + 8 * 2] = nnz[3 + 1 * 4];
3481  nnz_cache[3 + 8 * 3] = nnz[3 + 2 * 4];
3482  nnz_cache[3 + 8 * 4] = nnz[3 + 3 * 4];
3483  }
3484 
3485  /* CAVLC 8x8dct requires NNZ values for residual decoding that differ
3486  * from what the loop filter needs */
3487  if (!CABAC && h->pps.transform_8x8_mode) {
3488  if (IS_8x8DCT(top_type)) {
3489  nnz_cache[4 + 8 * 0] =
3490  nnz_cache[5 + 8 * 0] = (h->cbp_table[top_xy] & 0x4000) >> 12;
3491  nnz_cache[6 + 8 * 0] =
3492  nnz_cache[7 + 8 * 0] = (h->cbp_table[top_xy] & 0x8000) >> 12;
3493  }
3494  if (IS_8x8DCT(left_type[LTOP])) {
3495  nnz_cache[3 + 8 * 1] =
3496  nnz_cache[3 + 8 * 2] = (h->cbp_table[left_xy[LTOP]] & 0x2000) >> 12; // FIXME check MBAFF
3497  }
3498  if (IS_8x8DCT(left_type[LBOT])) {
3499  nnz_cache[3 + 8 * 3] =
3500  nnz_cache[3 + 8 * 4] = (h->cbp_table[left_xy[LBOT]] & 0x8000) >> 12; // FIXME check MBAFF
3501  }
3502 
3503  if (IS_8x8DCT(mb_type)) {
3504  nnz_cache[scan8[0]] =
3505  nnz_cache[scan8[1]] =
3506  nnz_cache[scan8[2]] =
3507  nnz_cache[scan8[3]] = (h->cbp & 0x1000) >> 12;
3508 
3509  nnz_cache[scan8[0 + 4]] =
3510  nnz_cache[scan8[1 + 4]] =
3511  nnz_cache[scan8[2 + 4]] =
3512  nnz_cache[scan8[3 + 4]] = (h->cbp & 0x2000) >> 12;
3513 
3514  nnz_cache[scan8[0 + 8]] =
3515  nnz_cache[scan8[1 + 8]] =
3516  nnz_cache[scan8[2 + 8]] =
3517  nnz_cache[scan8[3 + 8]] = (h->cbp & 0x4000) >> 12;
3518 
3519  nnz_cache[scan8[0 + 12]] =
3520  nnz_cache[scan8[1 + 12]] =
3521  nnz_cache[scan8[2 + 12]] =
3522  nnz_cache[scan8[3 + 12]] = (h->cbp & 0x8000) >> 12;
3523  }
3524  }
3525 
3526  return 0;
3527 }
3528 
3529 static void loop_filter(H264Context *h, int start_x, int end_x)
3530 {
3531  MpegEncContext *const s = &h->s;
3532  uint8_t *dest_y, *dest_cb, *dest_cr;
3533  int linesize, uvlinesize, mb_x, mb_y;
3534  const int end_mb_y = s->mb_y + FRAME_MBAFF;
3535  const int old_slice_type = h->slice_type;
3536  const int pixel_shift = h->pixel_shift;
3537  const int block_h = 16 >> s->chroma_y_shift;
3538 
3539  if (h->deblocking_filter) {
3540  for (mb_x = start_x; mb_x < end_x; mb_x++)
3541  for (mb_y = end_mb_y - FRAME_MBAFF; mb_y <= end_mb_y; mb_y++) {
3542  int mb_xy, mb_type;
3543  mb_xy = h->mb_xy = mb_x + mb_y * s->mb_stride;
3544  h->slice_num = h->slice_table[mb_xy];
3545  mb_type = s->current_picture.f.mb_type[mb_xy];
3546  h->list_count = h->list_counts[mb_xy];
3547 
3548  if (FRAME_MBAFF)
3549  h->mb_mbaff =
3550  h->mb_field_decoding_flag = !!IS_INTERLACED(mb_type);
3551 
3552  s->mb_x = mb_x;
3553  s->mb_y = mb_y;
3554  dest_y = s->current_picture.f.data[0] +
3555  ((mb_x << pixel_shift) + mb_y * s->linesize) * 16;
3556  dest_cb = s->current_picture.f.data[1] +
3557  (mb_x << pixel_shift) * (8 << CHROMA444) +
3558  mb_y * s->uvlinesize * block_h;
3559  dest_cr = s->current_picture.f.data[2] +
3560  (mb_x << pixel_shift) * (8 << CHROMA444) +
3561  mb_y * s->uvlinesize * block_h;
3562  // FIXME simplify above
3563 
3564  if (MB_FIELD) {
3565  linesize = h->mb_linesize = s->linesize * 2;
3566  uvlinesize = h->mb_uvlinesize = s->uvlinesize * 2;
3567  if (mb_y & 1) { // FIXME move out of this function?
3568  dest_y -= s->linesize * 15;
3569  dest_cb -= s->uvlinesize * (block_h - 1);
3570  dest_cr -= s->uvlinesize * (block_h - 1);
3571  }
3572  } else {
3573  linesize = h->mb_linesize = s->linesize;
3574  uvlinesize = h->mb_uvlinesize = s->uvlinesize;
3575  }
3576  backup_mb_border(h, dest_y, dest_cb, dest_cr, linesize,
3577  uvlinesize, 0);
3578  if (fill_filter_caches(h, mb_type))
3579  continue;
3580  h->chroma_qp[0] = get_chroma_qp(h, 0, s->current_picture.f.qscale_table[mb_xy]);
3581  h->chroma_qp[1] = get_chroma_qp(h, 1, s->current_picture.f.qscale_table[mb_xy]);
3582 
3583  if (FRAME_MBAFF) {
3584  ff_h264_filter_mb(h, mb_x, mb_y, dest_y, dest_cb, dest_cr,
3585  linesize, uvlinesize);
3586  } else {
3587  ff_h264_filter_mb_fast(h, mb_x, mb_y, dest_y, dest_cb,
3588  dest_cr, linesize, uvlinesize);
3589  }
3590  }
3591  }
3592  h->slice_type = old_slice_type;
3593  s->mb_x = end_x;
3594  s->mb_y = end_mb_y - FRAME_MBAFF;
3595  h->chroma_qp[0] = get_chroma_qp(h, 0, s->qscale);
3596  h->chroma_qp[1] = get_chroma_qp(h, 1, s->qscale);
3597 }
3598 
3600 {
3601  MpegEncContext *const s = &h->s;
3602  const int mb_xy = s->mb_x + s->mb_y * s->mb_stride;
3603  int mb_type = (h->slice_table[mb_xy - 1] == h->slice_num) ?
3604  s->current_picture.f.mb_type[mb_xy - 1] :
3605  (h->slice_table[mb_xy - s->mb_stride] == h->slice_num) ?
3606  s->current_picture.f.mb_type[mb_xy - s->mb_stride] : 0;
3607  h->mb_mbaff = h->mb_field_decoding_flag = IS_INTERLACED(mb_type) ? 1 : 0;
3608 }
3609 
3614 {
3615  MpegEncContext *const s = &h->s;
3616  int top = 16 * (s->mb_y >> FIELD_PICTURE);
3617  int pic_height = 16 * s->mb_height >> FIELD_PICTURE;
3618  int height = 16 << FRAME_MBAFF;
3619  int deblock_border = (16 + 4) << FRAME_MBAFF;
3620 
3621  if (h->deblocking_filter) {
3622  if ((top + height) >= pic_height)
3623  height += deblock_border;
3624  top -= deblock_border;
3625  }
3626 
3627  if (top >= pic_height || (top + height) < h->emu_edge_height)
3628  return;
3629 
3630  height = FFMIN(height, pic_height - top);
3631  if (top < h->emu_edge_height) {
3632  height = top + height;
3633  top = 0;
3634  }
3635 
3636  ff_draw_horiz_band(s, top, height);
3637 
3638  if (s->droppable)
3639  return;
3640 
3641  ff_thread_report_progress(&s->current_picture_ptr->f, top + height - 1,
3643 }
3644 
3645 static int decode_slice(struct AVCodecContext *avctx, void *arg)
3646 {
3647  H264Context *h = *(void **)arg;
3648  MpegEncContext *const s = &h->s;
3649  const int part_mask = s->partitioned_frame ? (ER_AC_END | ER_AC_ERROR)
3650  : 0x7F;
3651  int lf_x_start = s->mb_x;
3652 
3653  s->mb_skip_run = -1;
3654 
3656  s->codec_id != AV_CODEC_ID_H264 ||
3657  (CONFIG_GRAY && (s->flags & CODEC_FLAG_GRAY));
3658 
3659  if (h->pps.cabac) {
3660  /* realign */
3661  align_get_bits(&s->gb);
3662 
3663  /* init cabac */
3666  s->gb.buffer + get_bits_count(&s->gb) / 8,
3667  (get_bits_left(&s->gb) + 7) / 8);
3668 
3670 
3671  for (;;) {
3672  // START_TIMER
3673  int ret = ff_h264_decode_mb_cabac(h);
3674  int eos;
3675  // STOP_TIMER("decode_mb_cabac")
3676 
3677  if (ret >= 0)
3679 
3680  // FIXME optimal? or let mb_decode decode 16x32 ?
3681  if (ret >= 0 && FRAME_MBAFF) {
3682  s->mb_y++;
3683 
3684  ret = ff_h264_decode_mb_cabac(h);
3685 
3686  if (ret >= 0)
3688  s->mb_y--;
3689  }
3690  eos = get_cabac_terminate(&h->cabac);
3691 
3692  if ((s->workaround_bugs & FF_BUG_TRUNCATED) &&
3693  h->cabac.bytestream > h->cabac.bytestream_end + 2) {
3694  ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x - 1,
3695  s->mb_y, ER_MB_END & part_mask);
3696  if (s->mb_x >= lf_x_start)
3697  loop_filter(h, lf_x_start, s->mb_x + 1);
3698  return 0;
3699  }
3700  if (ret < 0 || h->cabac.bytestream > h->cabac.bytestream_end + 2) {
3701  av_log(h->s.avctx, AV_LOG_ERROR,
3702  "error while decoding MB %d %d, bytestream (%td)\n",
3703  s->mb_x, s->mb_y,
3706  s->mb_y, ER_MB_ERROR & part_mask);
3707  return -1;
3708  }
3709 
3710  if (++s->mb_x >= s->mb_width) {
3711  loop_filter(h, lf_x_start, s->mb_x);
3712  s->mb_x = lf_x_start = 0;
3713  decode_finish_row(h);
3714  ++s->mb_y;
3715  if (FIELD_OR_MBAFF_PICTURE) {
3716  ++s->mb_y;
3717  if (FRAME_MBAFF && s->mb_y < s->mb_height)
3719  }
3720  }
3721 
3722  if (eos || s->mb_y >= s->mb_height) {
3723  tprintf(s->avctx, "slice end %d %d\n",
3724  get_bits_count(&s->gb), s->gb.size_in_bits);
3725  ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x - 1,
3726  s->mb_y, ER_MB_END & part_mask);
3727  if (s->mb_x > lf_x_start)
3728  loop_filter(h, lf_x_start, s->mb_x);
3729  return 0;
3730  }
3731  }
3732  } else {
3733  for (;;) {
3734  int ret = ff_h264_decode_mb_cavlc(h);
3735 
3736  if (ret >= 0)
3738 
3739  // FIXME optimal? or let mb_decode decode 16x32 ?
3740  if (ret >= 0 && FRAME_MBAFF) {
3741  s->mb_y++;
3742  ret = ff_h264_decode_mb_cavlc(h);
3743 
3744  if (ret >= 0)
3746  s->mb_y--;
3747  }
3748 
3749  if (ret < 0) {
3750  av_log(h->s.avctx, AV_LOG_ERROR,
3751  "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
3753  s->mb_y, ER_MB_ERROR & part_mask);
3754  return -1;
3755  }
3756 
3757  if (++s->mb_x >= s->mb_width) {
3758  loop_filter(h, lf_x_start, s->mb_x);
3759  s->mb_x = lf_x_start = 0;
3760  decode_finish_row(h);
3761  ++s->mb_y;
3762  if (FIELD_OR_MBAFF_PICTURE) {
3763  ++s->mb_y;
3764  if (FRAME_MBAFF && s->mb_y < s->mb_height)
3766  }
3767  if (s->mb_y >= s->mb_height) {
3768  tprintf(s->avctx, "slice end %d %d\n",
3769  get_bits_count(&s->gb), s->gb.size_in_bits);
3770 
3771  if (get_bits_left(&s->gb) == 0) {
3773  s->mb_x - 1, s->mb_y,
3774  ER_MB_END & part_mask);
3775 
3776  return 0;
3777  } else {
3779  s->mb_x - 1, s->mb_y,
3780  ER_MB_END & part_mask);
3781 
3782  return -1;
3783  }
3784  }
3785  }
3786 
3787  if (get_bits_left(&s->gb) <= 0 && s->mb_skip_run <= 0) {
3788  tprintf(s->avctx, "slice end %d %d\n",
3789  get_bits_count(&s->gb), s->gb.size_in_bits);
3790  if (get_bits_left(&s->gb) == 0) {
3792  s->mb_x - 1, s->mb_y,
3793  ER_MB_END & part_mask);
3794  if (s->mb_x > lf_x_start)
3795  loop_filter(h, lf_x_start, s->mb_x);
3796 
3797  return 0;
3798  } else {
3800  s->mb_y, ER_MB_ERROR & part_mask);
3801 
3802  return -1;
3803  }
3804  }
3805  }
3806  }
3807 }
3808 
3815 static int execute_decode_slices(H264Context *h, int context_count)
3816 {
3817  MpegEncContext *const s = &h->s;
3818  AVCodecContext *const avctx = s->avctx;
3819  H264Context *hx;
3820  int i;
3821 
3822  if (s->avctx->hwaccel ||
3824  return 0;
3825  if (context_count == 1) {
3826  return decode_slice(avctx, &h);
3827  } else {
3828  for (i = 1; i < context_count; i++) {
3829  hx = h->thread_context[i];
3830  hx->s.err_recognition = avctx->err_recognition;
3831  hx->s.error_count = 0;
3832  }
3833 
3834  avctx->execute(avctx, decode_slice, h->thread_context,
3835  NULL, context_count, sizeof(void *));
3836 
3837  /* pull back stuff from slices to master context */
3838  hx = h->thread_context[context_count - 1];
3839  s->mb_x = hx->s.mb_x;
3840  s->mb_y = hx->s.mb_y;
3841  s->droppable = hx->s.droppable;
3843  for (i = 1; i < context_count; i++)
3844  h->s.error_count += h->thread_context[i]->s.error_count;
3845  }
3846 
3847  return 0;
3848 }
3849 
3850 static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size,
3851  int parse_extradata)
3852 {
3853  MpegEncContext *const s = &h->s;
3854  AVCodecContext *const avctx = s->avctx;
3855  H264Context *hx;
3856  int buf_index;
3857  int context_count;
3858  int next_avc;
3859  int pass = !(avctx->active_thread_type & FF_THREAD_FRAME);
3860  int nals_needed = 0;
3861  int nal_index;
3862 
3864  if (!(s->flags2 & CODEC_FLAG2_CHUNKS)) {
3865  h->current_slice = 0;
3866  if (!s->first_field)
3868  ff_h264_reset_sei(h);
3869  }
3870 
3871  for (; pass <= 1; pass++) {
3872  buf_index = 0;
3873  context_count = 0;
3874  next_avc = h->is_avc ? 0 : buf_size;
3875  nal_index = 0;
3876  for (;;) {
3877  int consumed;
3878  int dst_length;
3879  int bit_length;
3880  const uint8_t *ptr;
3881  int i, nalsize = 0;
3882  int err;
3883 
3884  if (buf_index >= next_avc) {
3885  if (buf_index >= buf_size - h->nal_length_size)
3886  break;
3887  nalsize = 0;
3888  for (i = 0; i < h->nal_length_size; i++)
3889  nalsize = (nalsize << 8) | buf[buf_index++];
3890  if (nalsize <= 0 || nalsize > buf_size - buf_index) {
3891  av_log(h->s.avctx, AV_LOG_ERROR,
3892  "AVC: nal size %d\n", nalsize);
3893  break;
3894  }
3895  next_avc = buf_index + nalsize;
3896  } else {
3897  // start code prefix search
3898  for (; buf_index + 3 < next_avc; buf_index++)
3899  // This should always succeed in the first iteration.
3900  if (buf[buf_index] == 0 &&
3901  buf[buf_index + 1] == 0 &&
3902  buf[buf_index + 2] == 1)
3903  break;
3904 
3905  if (buf_index + 3 >= buf_size) {
3906  buf_index = buf_size;
3907  break;
3908  }
3909 
3910  buf_index += 3;
3911  if (buf_index >= next_avc)
3912  continue;
3913  }
3914 
3915  hx = h->thread_context[context_count];
3916 
3917  ptr = ff_h264_decode_nal(hx, buf + buf_index, &dst_length,
3918  &consumed, next_avc - buf_index);
3919  if (ptr == NULL || dst_length < 0) {
3920  buf_index = -1;
3921  goto end;
3922  }
3923  i = buf_index + consumed;
3924  if ((s->workaround_bugs & FF_BUG_AUTODETECT) && i + 3 < next_avc &&
3925  buf[i] == 0x00 && buf[i + 1] == 0x00 &&
3926  buf[i + 2] == 0x01 && buf[i + 3] == 0xE0)
3928 
3929  if (!(s->workaround_bugs & FF_BUG_TRUNCATED))
3930  while (ptr[dst_length - 1] == 0 && dst_length > 0)
3931  dst_length--;
3932  bit_length = !dst_length ? 0
3933  : (8 * dst_length -
3934  decode_rbsp_trailing(h, ptr + dst_length - 1));
3935 
3936  if (s->avctx->debug & FF_DEBUG_STARTCODE)
3937  av_log(h->s.avctx, AV_LOG_DEBUG,
3938  "NAL %d at %d/%d length %d\n",
3939  hx->nal_unit_type, buf_index, buf_size, dst_length);
3940 
3941  if (h->is_avc && (nalsize != consumed) && nalsize)
3942  av_log(h->s.avctx, AV_LOG_DEBUG,
3943  "AVC: Consumed only %d bytes instead of %d\n",
3944  consumed, nalsize);
3945 
3946  buf_index += consumed;
3947  nal_index++;
3948 
3949  if (pass == 0) {
3950  /* packets can sometimes contain multiple PPS/SPS,
3951  * e.g. two PAFF field pictures in one packet, or a demuxer
3952  * which splits NALs strangely if so, when frame threading we
3953  * can't start the next thread until we've read all of them */
3954  switch (hx->nal_unit_type) {
3955  case NAL_SPS:
3956  case NAL_PPS:
3957  nals_needed = nal_index;
3958  break;
3959  case NAL_DPA:
3960  case NAL_IDR_SLICE:
3961  case NAL_SLICE:
3962  init_get_bits(&hx->s.gb, ptr, bit_length);
3963  if (!get_ue_golomb(&hx->s.gb))
3964  nals_needed = nal_index;
3965  }
3966  continue;
3967  }
3968 
3969  // FIXME do not discard SEI id
3970  if (avctx->skip_frame >= AVDISCARD_NONREF && h->nal_ref_idc == 0)
3971  continue;
3972 
3973 again:
3974  /* Ignore every NAL unit type except PPS and SPS during extradata
3975  * parsing. Decoding slices is not possible in codec init
3976  * with frame-mt */
3977  if (parse_extradata && HAVE_THREADS &&
3979  (hx->nal_unit_type != NAL_PPS &&
3980  hx->nal_unit_type != NAL_SPS)) {
3981  av_log(avctx, AV_LOG_INFO, "Ignoring NAL unit %d during "
3982  "extradata parsing\n", hx->nal_unit_type);
3984  }
3985  err = 0;
3986  switch (hx->nal_unit_type) {
3987  case NAL_IDR_SLICE:
3988  if (h->nal_unit_type != NAL_IDR_SLICE) {
3989  av_log(h->s.avctx, AV_LOG_ERROR,
3990  "Invalid mix of idr and non-idr slices\n");
3991  buf_index = -1;
3992  goto end;
3993  }
3994  idr(h); // FIXME ensure we don't lose some frames if there is reordering
3995  case NAL_SLICE:
3996  init_get_bits(&hx->s.gb, ptr, bit_length);
3997  hx->intra_gb_ptr =
3998  hx->inter_gb_ptr = &hx->s.gb;
3999  hx->s.data_partitioning = 0;
4000 
4001  if ((err = decode_slice_header(hx, h)))
4002  break;
4003 
4005  (hx->nal_unit_type == NAL_IDR_SLICE) ||
4006  (h->sei_recovery_frame_cnt >= 0);
4007 
4008  if (h->current_slice == 1) {
4009  if (!(s->flags2 & CODEC_FLAG2_CHUNKS))
4010  decode_postinit(h, nal_index >= nals_needed);
4011 
4012  if (s->avctx->hwaccel &&
4013  s->avctx->hwaccel->start_frame(s->avctx, NULL, 0) < 0)
4014  return -1;
4018  }
4019 
4020  if (hx->redundant_pic_count == 0 &&
4021  (avctx->skip_frame < AVDISCARD_NONREF ||
4022  hx->nal_ref_idc) &&
4023  (avctx->skip_frame < AVDISCARD_BIDIR ||
4025  (avctx->skip_frame < AVDISCARD_NONKEY ||
4027  avctx->skip_frame < AVDISCARD_ALL) {
4028  if (avctx->hwaccel) {
4029  if (avctx->hwaccel->decode_slice(avctx,
4030  &buf[buf_index - consumed],
4031  consumed) < 0)
4032  return -1;
4033  } else if (CONFIG_H264_VDPAU_DECODER &&
4035  static const uint8_t start_code[] = {
4036  0x00, 0x00, 0x01 };
4037  ff_vdpau_add_data_chunk(s, start_code,
4038  sizeof(start_code));
4039  ff_vdpau_add_data_chunk(s, &buf[buf_index - consumed],
4040  consumed);
4041  } else
4042  context_count++;
4043  }
4044  break;
4045  case NAL_DPA:
4046  init_get_bits(&hx->s.gb, ptr, bit_length);
4047  hx->intra_gb_ptr =
4048  hx->inter_gb_ptr = NULL;
4049 
4050  if ((err = decode_slice_header(hx, h)) < 0)
4051  break;
4052 
4053  hx->s.data_partitioning = 1;
4054  break;
4055  case NAL_DPB:
4056  init_get_bits(&hx->intra_gb, ptr, bit_length);
4057  hx->intra_gb_ptr = &hx->intra_gb;
4058  break;
4059  case NAL_DPC:
4060  init_get_bits(&hx->inter_gb, ptr, bit_length);
4061  hx->inter_gb_ptr = &hx->inter_gb;
4062 
4063  if (hx->redundant_pic_count == 0 &&
4064  hx->intra_gb_ptr &&
4065  hx->s.data_partitioning &&
4066  s->current_picture_ptr &&
4067  s->context_initialized &&
4068  (avctx->skip_frame < AVDISCARD_NONREF || hx->nal_ref_idc) &&
4069  (avctx->skip_frame < AVDISCARD_BIDIR ||
4071  (avctx->skip_frame < AVDISCARD_NONKEY ||
4073  avctx->skip_frame < AVDISCARD_ALL)
4074  context_count++;
4075  break;
4076  case NAL_SEI:
4077  init_get_bits(&s->gb, ptr, bit_length);
4078  ff_h264_decode_sei(h);
4079  break;
4080  case NAL_SPS:
4081  init_get_bits(&s->gb, ptr, bit_length);
4082  if (ff_h264_decode_seq_parameter_set(h) < 0 &&
4083  h->is_avc && (nalsize != consumed) && nalsize) {
4084  av_log(h->s.avctx, AV_LOG_DEBUG,
4085  "SPS decoding failure, trying again with the complete NAL\n");
4086  init_get_bits(&s->gb, buf + buf_index + 1 - consumed,
4087  8 * (nalsize - 1));
4089  }
4090 
4091  if (h264_set_parameter_from_sps(h) < 0) {
4092  buf_index = -1;
4093  goto end;
4094  }
4095  break;
4096  case NAL_PPS:
4097  init_get_bits(&s->gb, ptr, bit_length);
4098  ff_h264_decode_picture_parameter_set(h, bit_length);
4099  break;
4100  case NAL_AUD:
4101  case NAL_END_SEQUENCE:
4102  case NAL_END_STREAM:
4103  case NAL_FILLER_DATA:
4104  case NAL_SPS_EXT:
4105  case NAL_AUXILIARY_SLICE:
4106  break;
4107  case NAL_FF_IGNORE:
4108  break;
4109  default:
4110  av_log(avctx, AV_LOG_DEBUG, "Unknown NAL code: %d (%d bits)\n",
4111  hx->nal_unit_type, bit_length);
4112  }
4113 
4114  if (context_count == h->max_contexts) {
4115  execute_decode_slices(h, context_count);
4116  context_count = 0;
4117  }
4118 
4119  if (err < 0)
4120  av_log(h->s.avctx, AV_LOG_ERROR, "decode_slice_header error\n");
4121  else if (err == 1) {
4122  /* Slice could not be decoded in parallel mode, copy down
4123  * NAL unit stuff to context 0 and restart. Note that
4124  * rbsp_buffer is not transferred, but since we no longer
4125  * run in parallel mode this should not be an issue. */
4126  h->nal_unit_type = hx->nal_unit_type;
4127  h->nal_ref_idc = hx->nal_ref_idc;
4128  hx = h;
4129  goto again;
4130  }
4131  }
4132  }
4133  if (context_count)
4134  execute_decode_slices(h, context_count);
4135 
4136 end:
4137  /* clean up */
4138  if (s->current_picture_ptr && s->current_picture_ptr->owner2 == s &&
4139  !s->droppable) {
4142  }
4143 
4144  return buf_index;
4145 }
4146 
4150 static int get_consumed_bytes(MpegEncContext *s, int pos, int buf_size)
4151 {
4152  if (pos == 0)
4153  pos = 1; // avoid infinite loops (i doubt that is needed but ...)
4154  if (pos + 10 > buf_size)
4155  pos = buf_size; // oops ;)
4156 
4157  return pos;
4158 }
4159 
4160 static int decode_frame(AVCodecContext *avctx, void *data,
4161  int *got_frame, AVPacket *avpkt)
4162 {
4163  const uint8_t *buf = avpkt->data;
4164  int buf_size = avpkt->size;
4165  H264Context *h = avctx->priv_data;
4166  MpegEncContext *s = &h->s;
4167  AVFrame *pict = data;
4168  int buf_index = 0;
4169 
4170  s->flags = avctx->flags;
4171  s->flags2 = avctx->flags2;
4172 
4173  /* end of stream, output what is still in the buffers */
4174 out:
4175  if (buf_size == 0) {
4176  Picture *out;
4177  int i, out_idx;
4178 
4180 
4181  // FIXME factorize this with the output code below
4182  out = h->delayed_pic[0];
4183  out_idx = 0;
4184  for (i = 1;
4185  h->delayed_pic[i] &&
4186  !h->delayed_pic[i]->f.key_frame &&
4187  !h->delayed_pic[i]->mmco_reset;
4188  i++)
4189  if (h->delayed_pic[i]->poc < out->poc) {
4190  out = h->delayed_pic[i];
4191  out_idx = i;
4192  }
4193 
4194  for (i = out_idx; h->delayed_pic[i]; i++)
4195  h->delayed_pic[i] = h->delayed_pic[i + 1];
4196 
4197  if (out) {
4198  *got_frame = 1;
4199  *pict = out->f;
4200  }
4201 
4202  return buf_index;
4203  }
4204 
4205  buf_index = decode_nal_units(h, buf, buf_size, 0);
4206  if (buf_index < 0)
4207  return -1;
4208 
4210  buf_size = 0;
4211  goto out;
4212  }
4213 
4214  if (!(s->flags2 & CODEC_FLAG2_CHUNKS) && !s->current_picture_ptr) {
4215  if (avctx->skip_frame >= AVDISCARD_NONREF)
4216  return 0;
4217  av_log(avctx, AV_LOG_ERROR, "no frame!\n");
4218  return -1;
4219  }
4220 
4221  if (!(s->flags2 & CODEC_FLAG2_CHUNKS) ||
4222  (s->mb_y >= s->mb_height && s->mb_height)) {
4223  if (s->flags2 & CODEC_FLAG2_CHUNKS)
4224  decode_postinit(h, 1);
4225 
4226  field_end(h, 0);
4227  h->context_reinitialized = 0;
4228 
4229  if (!h->next_output_pic) {
4230  /* Wait for second field. */
4231  *got_frame = 0;
4232  } else {
4233  *got_frame = 1;
4234  *pict = h->next_output_pic->f;
4235  }
4236  }
4237 
4238  assert(pict->data[0] || !*got_frame);
4239  ff_print_debug_info(s, pict);
4240 
4241  return get_consumed_bytes(s, buf_index, buf_size);
4242 }
4243 
4245 {
4246  int i;
4247 
4248  free_tables(h, 1); // FIXME cleanup init stuff perhaps
4249 
4250  for (i = 0; i < MAX_SPS_COUNT; i++)
4251  av_freep(h->sps_buffers + i);
4252 
4253  for (i = 0; i < MAX_PPS_COUNT; i++)
4254  av_freep(h->pps_buffers + i);
4255 }
4256 
4258 {
4259  H264Context *h = avctx->priv_data;
4260  MpegEncContext *s = &h->s;
4261 
4263 
4264  ff_MPV_common_end(s);
4265 
4266  // memset(h, 0, sizeof(H264Context));
4267 
4268  return 0;
4269 }
4270 
4271 static const AVProfile profiles[] = {
4272  { FF_PROFILE_H264_BASELINE, "Baseline" },
4273  { FF_PROFILE_H264_CONSTRAINED_BASELINE, "Constrained Baseline" },
4274  { FF_PROFILE_H264_MAIN, "Main" },
4275  { FF_PROFILE_H264_EXTENDED, "Extended" },
4276  { FF_PROFILE_H264_HIGH, "High" },
4277  { FF_PROFILE_H264_HIGH_10, "High 10" },
4278  { FF_PROFILE_H264_HIGH_10_INTRA, "High 10 Intra" },
4279  { FF_PROFILE_H264_HIGH_422, "High 4:2:2" },
4280  { FF_PROFILE_H264_HIGH_422_INTRA, "High 4:2:2 Intra" },
4281  { FF_PROFILE_H264_HIGH_444, "High 4:4:4" },
4282  { FF_PROFILE_H264_HIGH_444_PREDICTIVE, "High 4:4:4 Predictive" },
4283  { FF_PROFILE_H264_HIGH_444_INTRA, "High 4:4:4 Intra" },
4284  { FF_PROFILE_H264_CAVLC_444, "CAVLC 4:4:4" },
4285  { FF_PROFILE_UNKNOWN },
4286 };
4287 
4289  .name = "h264",
4290  .type = AVMEDIA_TYPE_VIDEO,
4291  .id = AV_CODEC_ID_H264,
4292  .priv_data_size = sizeof(H264Context),
4295  .decode = decode_frame,
4296  .capabilities = /*CODEC_CAP_DRAW_HORIZ_BAND |*/ CODEC_CAP_DR1 |
4299  .flush = flush_dpb,
4300  .long_name = NULL_IF_CONFIG_SMALL("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10"),
4301  .init_thread_copy = ONLY_IF_THREADS_ENABLED(decode_init_thread_copy),
4302  .update_thread_context = ONLY_IF_THREADS_ENABLED(decode_update_thread_context),
4303  .profiles = NULL_IF_CONFIG_SMALL(profiles),
4304 };
4305 
4306 #if CONFIG_H264_VDPAU_DECODER
4307 AVCodec ff_h264_vdpau_decoder = {
4308  .name = "h264_vdpau",
4309  .type = AVMEDIA_TYPE_VIDEO,
4310  .id = AV_CODEC_ID_H264,
4311  .priv_data_size = sizeof(H264Context),
4314  .decode = decode_frame,
4315  .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_HWACCEL_VDPAU,
4316  .flush = flush_dpb,
4317  .long_name = NULL_IF_CONFIG_SMALL("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (VDPAU acceleration)"),
4318  .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_VDPAU_H264,
4319  AV_PIX_FMT_NONE},
4320  .profiles = NULL_IF_CONFIG_SMALL(profiles),
4321 };
4322 #endif