FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
utils.c
Go to the documentation of this file.
1 /*
2  * utils for libavcodec
3  * Copyright (c) 2001 Fabrice Bellard
4  * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 /**
24  * @file
25  * utils.
26  */
27 
28 #include "config.h"
29 #include "libavutil/atomic.h"
30 #include "libavutil/attributes.h"
31 #include "libavutil/avassert.h"
32 #include "libavutil/avstring.h"
33 #include "libavutil/bprint.h"
35 #include "libavutil/crc.h"
36 #include "libavutil/frame.h"
37 #include "libavutil/internal.h"
38 #include "libavutil/mathematics.h"
39 #include "libavutil/pixdesc.h"
40 #include "libavutil/imgutils.h"
41 #include "libavutil/samplefmt.h"
42 #include "libavutil/dict.h"
43 #include "avcodec.h"
44 #include "dsputil.h"
45 #include "libavutil/opt.h"
46 #include "thread.h"
47 #include "frame_thread_encoder.h"
48 #include "internal.h"
49 #include "bytestream.h"
50 #include "version.h"
51 #include <stdlib.h>
52 #include <stdarg.h>
53 #include <limits.h>
54 #include <float.h>
55 #if CONFIG_ICONV
56 # include <iconv.h>
57 #endif
58 
59 #if HAVE_PTHREADS
60 #include <pthread.h>
61 #elif HAVE_W32THREADS
62 #include "compat/w32pthreads.h"
63 #elif HAVE_OS2THREADS
64 #include "compat/os2threads.h"
65 #endif
66 
67 #if HAVE_PTHREADS || HAVE_W32THREADS || HAVE_OS2THREADS
68 static int default_lockmgr_cb(void **arg, enum AVLockOp op)
69 {
70  void * volatile * mutex = arg;
71  int err;
72 
73  switch (op) {
74  case AV_LOCK_CREATE:
75  return 0;
76  case AV_LOCK_OBTAIN:
77  if (!*mutex) {
79  if (!tmp)
80  return AVERROR(ENOMEM);
81  if ((err = pthread_mutex_init(tmp, NULL))) {
82  av_free(tmp);
83  return AVERROR(err);
84  }
85  if (avpriv_atomic_ptr_cas(mutex, NULL, tmp)) {
87  av_free(tmp);
88  }
89  }
90 
91  if ((err = pthread_mutex_lock(*mutex)))
92  return AVERROR(err);
93 
94  return 0;
95  case AV_LOCK_RELEASE:
96  if ((err = pthread_mutex_unlock(*mutex)))
97  return AVERROR(err);
98 
99  return 0;
100  case AV_LOCK_DESTROY:
101  if (*mutex)
102  pthread_mutex_destroy(*mutex);
103  av_free(*mutex);
104  avpriv_atomic_ptr_cas(mutex, *mutex, NULL);
105  return 0;
106  }
107  return 1;
108 }
109 static int (*lockmgr_cb)(void **mutex, enum AVLockOp op) = default_lockmgr_cb;
110 #else
111 static int (*lockmgr_cb)(void **mutex, enum AVLockOp op) = NULL;
112 #endif
113 
114 
115 volatile int ff_avcodec_locked;
116 static int volatile entangled_thread_counter = 0;
117 static void *codec_mutex;
118 static void *avformat_mutex;
119 
120 void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
121 {
122  if (min_size < *size)
123  return ptr;
124 
125  min_size = FFMAX(17 * min_size / 16 + 32, min_size);
126 
127  ptr = av_realloc(ptr, min_size);
128  /* we could set this to the unmodified min_size but this is safer
129  * if the user lost the ptr and uses NULL now
130  */
131  if (!ptr)
132  min_size = 0;
133 
134  *size = min_size;
135 
136  return ptr;
137 }
138 
139 static inline int ff_fast_malloc(void *ptr, unsigned int *size, size_t min_size, int zero_realloc)
140 {
141  void **p = ptr;
142  if (min_size < *size)
143  return 0;
144  min_size = FFMAX(17 * min_size / 16 + 32, min_size);
145  av_free(*p);
146  *p = zero_realloc ? av_mallocz(min_size) : av_malloc(min_size);
147  if (!*p)
148  min_size = 0;
149  *size = min_size;
150  return 1;
151 }
152 
153 void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
154 {
155  ff_fast_malloc(ptr, size, min_size, 0);
156 }
157 
158 void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
159 {
160  uint8_t **p = ptr;
161  if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
162  av_freep(p);
163  *size = 0;
164  return;
165  }
166  if (!ff_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE, 1))
167  memset(*p + min_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
168 }
169 
170 void av_fast_padded_mallocz(void *ptr, unsigned int *size, size_t min_size)
171 {
172  uint8_t **p = ptr;
173  if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
174  av_freep(p);
175  *size = 0;
176  return;
177  }
178  if (!ff_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE, 1))
179  memset(*p, 0, min_size + FF_INPUT_BUFFER_PADDING_SIZE);
180 }
181 
182 /* encoder management */
183 static AVCodec *first_avcodec = NULL;
184 
186 {
187  if (c)
188  return c->next;
189  else
190  return first_avcodec;
191 }
192 
193 static av_cold void avcodec_init(void)
194 {
195  static int initialized = 0;
196 
197  if (initialized != 0)
198  return;
199  initialized = 1;
200 
201  if (CONFIG_DSPUTIL)
203 }
204 
205 int av_codec_is_encoder(const AVCodec *codec)
206 {
207  return codec && (codec->encode_sub || codec->encode2);
208 }
209 
210 int av_codec_is_decoder(const AVCodec *codec)
211 {
212  return codec && codec->decode;
213 }
214 
216 {
217  AVCodec **p;
218  avcodec_init();
219  p = &first_avcodec;
220  codec->next = NULL;
221 
222  while(*p || avpriv_atomic_ptr_cas((void * volatile *)p, NULL, codec))
223  p = &(*p)->next;
224 
225  if (codec->init_static_data)
226  codec->init_static_data(codec);
227 }
228 
230 {
231  return EDGE_WIDTH;
232 }
233 
235 {
236  s->coded_width = width;
237  s->coded_height = height;
238  s->width = FF_CEIL_RSHIFT(width, s->lowres);
239  s->height = FF_CEIL_RSHIFT(height, s->lowres);
240 }
241 
242 #if HAVE_NEON || ARCH_PPC || HAVE_MMX
243 # define STRIDE_ALIGN 16
244 #else
245 # define STRIDE_ALIGN 8
246 #endif
247 
249  int linesize_align[AV_NUM_DATA_POINTERS])
250 {
251  int i;
252  int w_align = 1;
253  int h_align = 1;
254 
255  switch (s->pix_fmt) {
256  case AV_PIX_FMT_YUV420P:
257  case AV_PIX_FMT_YUYV422:
258  case AV_PIX_FMT_UYVY422:
259  case AV_PIX_FMT_YUV422P:
260  case AV_PIX_FMT_YUV440P:
261  case AV_PIX_FMT_YUV444P:
262  case AV_PIX_FMT_GBRAP:
263  case AV_PIX_FMT_GBRP:
264  case AV_PIX_FMT_GRAY8:
265  case AV_PIX_FMT_GRAY16BE:
266  case AV_PIX_FMT_GRAY16LE:
267  case AV_PIX_FMT_YUVJ420P:
268  case AV_PIX_FMT_YUVJ422P:
269  case AV_PIX_FMT_YUVJ440P:
270  case AV_PIX_FMT_YUVJ444P:
271  case AV_PIX_FMT_YUVA420P:
272  case AV_PIX_FMT_YUVA422P:
273  case AV_PIX_FMT_YUVA444P:
322  case AV_PIX_FMT_GBRP9LE:
323  case AV_PIX_FMT_GBRP9BE:
324  case AV_PIX_FMT_GBRP10LE:
325  case AV_PIX_FMT_GBRP10BE:
326  case AV_PIX_FMT_GBRP12LE:
327  case AV_PIX_FMT_GBRP12BE:
328  case AV_PIX_FMT_GBRP14LE:
329  case AV_PIX_FMT_GBRP14BE:
330  w_align = 16; //FIXME assume 16 pixel per macroblock
331  h_align = 16 * 2; // interlaced needs 2 macroblocks height
332  break;
333  case AV_PIX_FMT_YUV411P:
334  case AV_PIX_FMT_YUVJ411P:
336  w_align = 32;
337  h_align = 8;
338  break;
339  case AV_PIX_FMT_YUV410P:
340  if (s->codec_id == AV_CODEC_ID_SVQ1) {
341  w_align = 64;
342  h_align = 64;
343  }
344  break;
345  case AV_PIX_FMT_RGB555:
346  if (s->codec_id == AV_CODEC_ID_RPZA) {
347  w_align = 4;
348  h_align = 4;
349  }
350  break;
351  case AV_PIX_FMT_PAL8:
352  case AV_PIX_FMT_BGR8:
353  case AV_PIX_FMT_RGB8:
354  if (s->codec_id == AV_CODEC_ID_SMC ||
356  w_align = 4;
357  h_align = 4;
358  }
359  break;
360  case AV_PIX_FMT_BGR24:
361  if ((s->codec_id == AV_CODEC_ID_MSZH) ||
362  (s->codec_id == AV_CODEC_ID_ZLIB)) {
363  w_align = 4;
364  h_align = 4;
365  }
366  break;
367  case AV_PIX_FMT_RGB24:
368  if (s->codec_id == AV_CODEC_ID_CINEPAK) {
369  w_align = 4;
370  h_align = 4;
371  }
372  break;
373  default:
374  w_align = 1;
375  h_align = 1;
376  break;
377  }
378 
380  w_align = FFMAX(w_align, 8);
381  }
382 
383  *width = FFALIGN(*width, w_align);
384  *height = FFALIGN(*height, h_align);
385  if (s->codec_id == AV_CODEC_ID_H264 || s->lowres)
386  // some of the optimized chroma MC reads one line too much
387  // which is also done in mpeg decoders with lowres > 0
388  *height += 2;
389 
390  for (i = 0; i < 4; i++)
391  linesize_align[i] = STRIDE_ALIGN;
392 }
393 
395 {
397  int chroma_shift = desc->log2_chroma_w;
398  int linesize_align[AV_NUM_DATA_POINTERS];
399  int align;
400 
401  avcodec_align_dimensions2(s, width, height, linesize_align);
402  align = FFMAX(linesize_align[0], linesize_align[3]);
403  linesize_align[1] <<= chroma_shift;
404  linesize_align[2] <<= chroma_shift;
405  align = FFMAX3(align, linesize_align[1], linesize_align[2]);
406  *width = FFALIGN(*width, align);
407 }
408 
409 int avcodec_enum_to_chroma_pos(int *xpos, int *ypos, enum AVChromaLocation pos)
410 {
411  if (pos <= AVCHROMA_LOC_UNSPECIFIED || pos >= AVCHROMA_LOC_NB)
412  return AVERROR(EINVAL);
413  pos--;
414 
415  *xpos = (pos&1) * 128;
416  *ypos = ((pos>>1)^(pos<4)) * 128;
417 
418  return 0;
419 }
420 
422 {
423  int pos, xout, yout;
424 
425  for (pos = AVCHROMA_LOC_UNSPECIFIED + 1; pos < AVCHROMA_LOC_NB; pos++) {
426  if (avcodec_enum_to_chroma_pos(&xout, &yout, pos) == 0 && xout == xpos && yout == ypos)
427  return pos;
428  }
430 }
431 
433  enum AVSampleFormat sample_fmt, const uint8_t *buf,
434  int buf_size, int align)
435 {
436  int ch, planar, needed_size, ret = 0;
437 
438  needed_size = av_samples_get_buffer_size(NULL, nb_channels,
439  frame->nb_samples, sample_fmt,
440  align);
441  if (buf_size < needed_size)
442  return AVERROR(EINVAL);
443 
444  planar = av_sample_fmt_is_planar(sample_fmt);
445  if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
446  if (!(frame->extended_data = av_mallocz(nb_channels *
447  sizeof(*frame->extended_data))))
448  return AVERROR(ENOMEM);
449  } else {
450  frame->extended_data = frame->data;
451  }
452 
453  if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
454  (uint8_t *)(intptr_t)buf, nb_channels, frame->nb_samples,
455  sample_fmt, align)) < 0) {
456  if (frame->extended_data != frame->data)
457  av_freep(&frame->extended_data);
458  return ret;
459  }
460  if (frame->extended_data != frame->data) {
461  for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
462  frame->data[ch] = frame->extended_data[ch];
463  }
464 
465  return ret;
466 }
467 
469 {
470  FramePool *pool = avctx->internal->pool;
471  int i, ret;
472 
473  switch (avctx->codec_type) {
474  case AVMEDIA_TYPE_VIDEO: {
475  AVPicture picture;
476  int size[4] = { 0 };
477  int w = frame->width;
478  int h = frame->height;
479  int tmpsize, unaligned;
480 
481  if (pool->format == frame->format &&
482  pool->width == frame->width && pool->height == frame->height)
483  return 0;
484 
485  avcodec_align_dimensions2(avctx, &w, &h, pool->stride_align);
486 
487  if (!(avctx->flags & CODEC_FLAG_EMU_EDGE)) {
488  w += EDGE_WIDTH * 2;
489  h += EDGE_WIDTH * 2;
490  }
491 
492  do {
493  // NOTE: do not align linesizes individually, this breaks e.g. assumptions
494  // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
495  av_image_fill_linesizes(picture.linesize, avctx->pix_fmt, w);
496  // increase alignment of w for next try (rhs gives the lowest bit set in w)
497  w += w & ~(w - 1);
498 
499  unaligned = 0;
500  for (i = 0; i < 4; i++)
501  unaligned |= picture.linesize[i] % pool->stride_align[i];
502  } while (unaligned);
503 
504  tmpsize = av_image_fill_pointers(picture.data, avctx->pix_fmt, h,
505  NULL, picture.linesize);
506  if (tmpsize < 0)
507  return -1;
508 
509  for (i = 0; i < 3 && picture.data[i + 1]; i++)
510  size[i] = picture.data[i + 1] - picture.data[i];
511  size[i] = tmpsize - (picture.data[i] - picture.data[0]);
512 
513  for (i = 0; i < 4; i++) {
514  av_buffer_pool_uninit(&pool->pools[i]);
515  pool->linesize[i] = picture.linesize[i];
516  if (size[i]) {
517  pool->pools[i] = av_buffer_pool_init(size[i] + 16 + STRIDE_ALIGN - 1,
518  CONFIG_MEMORY_POISONING ?
519  NULL :
521  if (!pool->pools[i]) {
522  ret = AVERROR(ENOMEM);
523  goto fail;
524  }
525  }
526  }
527  pool->format = frame->format;
528  pool->width = frame->width;
529  pool->height = frame->height;
530 
531  break;
532  }
533  case AVMEDIA_TYPE_AUDIO: {
534  int ch = av_frame_get_channels(frame); //av_get_channel_layout_nb_channels(frame->channel_layout);
535  int planar = av_sample_fmt_is_planar(frame->format);
536  int planes = planar ? ch : 1;
537 
538  if (pool->format == frame->format && pool->planes == planes &&
539  pool->channels == ch && frame->nb_samples == pool->samples)
540  return 0;
541 
542  av_buffer_pool_uninit(&pool->pools[0]);
543  ret = av_samples_get_buffer_size(&pool->linesize[0], ch,
544  frame->nb_samples, frame->format, 0);
545  if (ret < 0)
546  goto fail;
547 
548  pool->pools[0] = av_buffer_pool_init(pool->linesize[0], NULL);
549  if (!pool->pools[0]) {
550  ret = AVERROR(ENOMEM);
551  goto fail;
552  }
553 
554  pool->format = frame->format;
555  pool->planes = planes;
556  pool->channels = ch;
557  pool->samples = frame->nb_samples;
558  break;
559  }
560  default: av_assert0(0);
561  }
562  return 0;
563 fail:
564  for (i = 0; i < 4; i++)
565  av_buffer_pool_uninit(&pool->pools[i]);
566  pool->format = -1;
567  pool->planes = pool->channels = pool->samples = 0;
568  pool->width = pool->height = 0;
569  return ret;
570 }
571 
573 {
574  FramePool *pool = avctx->internal->pool;
575  int planes = pool->planes;
576  int i;
577 
578  frame->linesize[0] = pool->linesize[0];
579 
580  if (planes > AV_NUM_DATA_POINTERS) {
581  frame->extended_data = av_mallocz(planes * sizeof(*frame->extended_data));
582  frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
583  frame->extended_buf = av_mallocz(frame->nb_extended_buf *
584  sizeof(*frame->extended_buf));
585  if (!frame->extended_data || !frame->extended_buf) {
586  av_freep(&frame->extended_data);
587  av_freep(&frame->extended_buf);
588  return AVERROR(ENOMEM);
589  }
590  } else {
591  frame->extended_data = frame->data;
592  av_assert0(frame->nb_extended_buf == 0);
593  }
594 
595  for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
596  frame->buf[i] = av_buffer_pool_get(pool->pools[0]);
597  if (!frame->buf[i])
598  goto fail;
599  frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
600  }
601  for (i = 0; i < frame->nb_extended_buf; i++) {
602  frame->extended_buf[i] = av_buffer_pool_get(pool->pools[0]);
603  if (!frame->extended_buf[i])
604  goto fail;
605  frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
606  }
607 
608  if (avctx->debug & FF_DEBUG_BUFFERS)
609  av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p", frame);
610 
611  return 0;
612 fail:
613  av_frame_unref(frame);
614  return AVERROR(ENOMEM);
615 }
616 
618 {
619  FramePool *pool = s->internal->pool;
620  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pic->format);
621  int pixel_size = desc->comp[0].step_minus1 + 1;
622  int h_chroma_shift, v_chroma_shift;
623  int i;
624 
625  if (pic->data[0] != NULL) {
626  av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
627  return -1;
628  }
629 
630  memset(pic->data, 0, sizeof(pic->data));
631  pic->extended_data = pic->data;
632 
633  av_pix_fmt_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
634 
635  for (i = 0; i < 4 && pool->pools[i]; i++) {
636  const int h_shift = i == 0 ? 0 : h_chroma_shift;
637  const int v_shift = i == 0 ? 0 : v_chroma_shift;
638  int is_planar = pool->pools[2] || (i==0 && s->pix_fmt == AV_PIX_FMT_GRAY8);
639 
640  pic->linesize[i] = pool->linesize[i];
641 
642  pic->buf[i] = av_buffer_pool_get(pool->pools[i]);
643  if (!pic->buf[i])
644  goto fail;
645 
646  // no edge if EDGE EMU or not planar YUV
647  if ((s->flags & CODEC_FLAG_EMU_EDGE) || !is_planar)
648  pic->data[i] = pic->buf[i]->data;
649  else {
650  pic->data[i] = pic->buf[i]->data +
651  FFALIGN((pic->linesize[i] * EDGE_WIDTH >> v_shift) +
652  (pixel_size * EDGE_WIDTH >> h_shift), pool->stride_align[i]);
653  }
654  }
655  for (; i < AV_NUM_DATA_POINTERS; i++) {
656  pic->data[i] = NULL;
657  pic->linesize[i] = 0;
658  }
659  if (pic->data[1] && !pic->data[2])
660  avpriv_set_systematic_pal2((uint32_t *)pic->data[1], s->pix_fmt);
661 
662  if (s->debug & FF_DEBUG_BUFFERS)
663  av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p\n", pic);
664 
665  return 0;
666 fail:
667  av_frame_unref(pic);
668  return AVERROR(ENOMEM);
669 }
670 
671 void avpriv_color_frame(AVFrame *frame, const int c[4])
672 {
673  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
674  int p, y, x;
675 
677 
678  for (p = 0; p<desc->nb_components; p++) {
679  uint8_t *dst = frame->data[p];
680  int is_chroma = p == 1 || p == 2;
681  int bytes = is_chroma ? FF_CEIL_RSHIFT(frame->width, desc->log2_chroma_w) : frame->width;
682  int height = is_chroma ? FF_CEIL_RSHIFT(frame->height, desc->log2_chroma_h) : frame->height;
683  for (y = 0; y < height; y++) {
684  if (desc->comp[0].depth_minus1 >= 8) {
685  for (x = 0; x<bytes; x++)
686  ((uint16_t*)dst)[x] = c[p];
687  }else
688  memset(dst, c[p], bytes);
689  dst += frame->linesize[p];
690  }
691  }
692 }
693 
695 {
696  int ret;
697 
698  if ((ret = update_frame_pool(avctx, frame)) < 0)
699  return ret;
700 
701 #if FF_API_GET_BUFFER
703  frame->type = FF_BUFFER_TYPE_INTERNAL;
705 #endif
706 
707  switch (avctx->codec_type) {
708  case AVMEDIA_TYPE_VIDEO:
709  return video_get_buffer(avctx, frame);
710  case AVMEDIA_TYPE_AUDIO:
711  return audio_get_buffer(avctx, frame);
712  default:
713  return -1;
714  }
715 }
716 
718 {
719  if (avctx->pkt) {
720  frame->pkt_pts = avctx->pkt->pts;
721  av_frame_set_pkt_pos (frame, avctx->pkt->pos);
722  av_frame_set_pkt_duration(frame, avctx->pkt->duration);
723  av_frame_set_pkt_size (frame, avctx->pkt->size);
724  } else {
725  frame->pkt_pts = AV_NOPTS_VALUE;
726  av_frame_set_pkt_pos (frame, -1);
727  av_frame_set_pkt_duration(frame, 0);
728  av_frame_set_pkt_size (frame, -1);
729  }
730  frame->reordered_opaque = avctx->reordered_opaque;
731 
732  switch (avctx->codec->type) {
733  case AVMEDIA_TYPE_VIDEO:
734  frame->width = FFMAX(avctx->width, FF_CEIL_RSHIFT(avctx->coded_width, avctx->lowres));
735  frame->height = FFMAX(avctx->height, FF_CEIL_RSHIFT(avctx->coded_height, avctx->lowres));
736  if (frame->format < 0)
737  frame->format = avctx->pix_fmt;
738  if (!frame->sample_aspect_ratio.num)
741  av_frame_set_colorspace(frame, avctx->colorspace);
743  av_frame_set_color_range(frame, avctx->color_range);
744  break;
745  case AVMEDIA_TYPE_AUDIO:
746  if (!frame->sample_rate)
747  frame->sample_rate = avctx->sample_rate;
748  if (frame->format < 0)
749  frame->format = avctx->sample_fmt;
750  if (!frame->channel_layout) {
751  if (avctx->channel_layout) {
753  avctx->channels) {
754  av_log(avctx, AV_LOG_ERROR, "Inconsistent channel "
755  "configuration.\n");
756  return AVERROR(EINVAL);
757  }
758 
759  frame->channel_layout = avctx->channel_layout;
760  } else {
761  if (avctx->channels > FF_SANE_NB_CHANNELS) {
762  av_log(avctx, AV_LOG_ERROR, "Too many channels: %d.\n",
763  avctx->channels);
764  return AVERROR(ENOSYS);
765  }
766  }
767  }
768  av_frame_set_channels(frame, avctx->channels);
769  break;
770  }
771  return 0;
772 }
773 
774 #if FF_API_GET_BUFFER
776 int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
777 {
778  return avcodec_default_get_buffer2(avctx, frame, 0);
779 }
780 
781 typedef struct CompatReleaseBufPriv {
782  AVCodecContext avctx;
783  AVFrame frame;
784 } CompatReleaseBufPriv;
785 
786 static void compat_free_buffer(void *opaque, uint8_t *data)
787 {
788  CompatReleaseBufPriv *priv = opaque;
789  if (priv->avctx.release_buffer)
790  priv->avctx.release_buffer(&priv->avctx, &priv->frame);
791  av_freep(&priv);
792 }
793 
794 static void compat_release_buffer(void *opaque, uint8_t *data)
795 {
796  AVBufferRef *buf = opaque;
797  av_buffer_unref(&buf);
798 }
800 #endif
801 
803 {
804  int ret;
805 
806  if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
807  if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0 || avctx->pix_fmt<0) {
808  av_log(avctx, AV_LOG_ERROR, "video_get_buffer: image parameters invalid\n");
809  return AVERROR(EINVAL);
810  }
811  }
812  if ((ret = ff_init_buffer_info(avctx, frame)) < 0)
813  return ret;
814 
815 #if FF_API_GET_BUFFER
817  /*
818  * Wrap an old get_buffer()-allocated buffer in a bunch of AVBuffers.
819  * We wrap each plane in its own AVBuffer. Each of those has a reference to
820  * a dummy AVBuffer as its private data, unreffing it on free.
821  * When all the planes are freed, the dummy buffer's free callback calls
822  * release_buffer().
823  */
824  if (avctx->get_buffer) {
825  CompatReleaseBufPriv *priv = NULL;
826  AVBufferRef *dummy_buf = NULL;
827  int planes, i, ret;
828 
829  if (flags & AV_GET_BUFFER_FLAG_REF)
830  frame->reference = 1;
831 
832  ret = avctx->get_buffer(avctx, frame);
833  if (ret < 0)
834  return ret;
835 
836  /* return if the buffers are already set up
837  * this would happen e.g. when a custom get_buffer() calls
838  * avcodec_default_get_buffer
839  */
840  if (frame->buf[0])
841  goto end;
842 
843  priv = av_mallocz(sizeof(*priv));
844  if (!priv) {
845  ret = AVERROR(ENOMEM);
846  goto fail;
847  }
848  priv->avctx = *avctx;
849  priv->frame = *frame;
850 
851  dummy_buf = av_buffer_create(NULL, 0, compat_free_buffer, priv, 0);
852  if (!dummy_buf) {
853  ret = AVERROR(ENOMEM);
854  goto fail;
855  }
856 
857 #define WRAP_PLANE(ref_out, data, data_size) \
858 do { \
859  AVBufferRef *dummy_ref = av_buffer_ref(dummy_buf); \
860  if (!dummy_ref) { \
861  ret = AVERROR(ENOMEM); \
862  goto fail; \
863  } \
864  ref_out = av_buffer_create(data, data_size, compat_release_buffer, \
865  dummy_ref, 0); \
866  if (!ref_out) { \
867  av_frame_unref(frame); \
868  ret = AVERROR(ENOMEM); \
869  goto fail; \
870  } \
871 } while (0)
872 
873  if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
874  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
875 
876  planes = av_pix_fmt_count_planes(frame->format);
877  /* workaround for AVHWAccel plane count of 0, buf[0] is used as
878  check for allocated buffers: make libavcodec happy */
879  if (desc && desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
880  planes = 1;
881  if (!desc || planes <= 0) {
882  ret = AVERROR(EINVAL);
883  goto fail;
884  }
885 
886  for (i = 0; i < planes; i++) {
887  int v_shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
888  int plane_size = (frame->height >> v_shift) * frame->linesize[i];
889 
890  WRAP_PLANE(frame->buf[i], frame->data[i], plane_size);
891  }
892  } else {
893  int planar = av_sample_fmt_is_planar(frame->format);
894  planes = planar ? avctx->channels : 1;
895 
896  if (planes > FF_ARRAY_ELEMS(frame->buf)) {
897  frame->nb_extended_buf = planes - FF_ARRAY_ELEMS(frame->buf);
898  frame->extended_buf = av_malloc(sizeof(*frame->extended_buf) *
899  frame->nb_extended_buf);
900  if (!frame->extended_buf) {
901  ret = AVERROR(ENOMEM);
902  goto fail;
903  }
904  }
905 
906  for (i = 0; i < FFMIN(planes, FF_ARRAY_ELEMS(frame->buf)); i++)
907  WRAP_PLANE(frame->buf[i], frame->extended_data[i], frame->linesize[0]);
908 
909  for (i = 0; i < frame->nb_extended_buf; i++)
910  WRAP_PLANE(frame->extended_buf[i],
911  frame->extended_data[i + FF_ARRAY_ELEMS(frame->buf)],
912  frame->linesize[0]);
913  }
914 
915  av_buffer_unref(&dummy_buf);
916 
917 end:
918  frame->width = avctx->width;
919  frame->height = avctx->height;
920 
921  return 0;
922 
923 fail:
924  avctx->release_buffer(avctx, frame);
925  av_freep(&priv);
926  av_buffer_unref(&dummy_buf);
927  return ret;
928  }
930 #endif
931 
932  ret = avctx->get_buffer2(avctx, frame, flags);
933 
934  if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
935  frame->width = avctx->width;
936  frame->height = avctx->height;
937  }
938 
939  return ret;
940 }
941 
943 {
944  int ret = get_buffer_internal(avctx, frame, flags);
945  if (ret < 0)
946  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
947  return ret;
948 }
949 
951 {
952  AVFrame tmp;
953  int ret;
954 
956 
957  if (frame->data[0] && (frame->width != avctx->width || frame->height != avctx->height || frame->format != avctx->pix_fmt)) {
958  av_log(avctx, AV_LOG_WARNING, "Picture changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s in reget buffer()\n",
959  frame->width, frame->height, av_get_pix_fmt_name(frame->format), avctx->width, avctx->height, av_get_pix_fmt_name(avctx->pix_fmt));
960  av_frame_unref(frame);
961  }
962 
963  ff_init_buffer_info(avctx, frame);
964 
965  if (!frame->data[0])
966  return ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
967 
968  if (av_frame_is_writable(frame))
969  return 0;
970 
971  av_frame_move_ref(&tmp, frame);
972 
973  ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
974  if (ret < 0) {
975  av_frame_unref(&tmp);
976  return ret;
977  }
978 
979  av_image_copy(frame->data, frame->linesize, tmp.data, tmp.linesize,
980  frame->format, frame->width, frame->height);
981 
982  av_frame_unref(&tmp);
983 
984  return 0;
985 }
986 
988 {
989  int ret = reget_buffer_internal(avctx, frame);
990  if (ret < 0)
991  av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
992  return ret;
993 }
994 
995 #if FF_API_GET_BUFFER
996 void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic)
997 {
999 
1000  av_frame_unref(pic);
1001 }
1002 
1003 int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic)
1004 {
1005  av_assert0(0);
1006  return AVERROR_BUG;
1007 }
1008 #endif
1009 
1010 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
1011 {
1012  int i;
1013 
1014  for (i = 0; i < count; i++) {
1015  int r = func(c, (char *)arg + i * size);
1016  if (ret)
1017  ret[i] = r;
1018  }
1019  return 0;
1020 }
1021 
1022 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
1023 {
1024  int i;
1025 
1026  for (i = 0; i < count; i++) {
1027  int r = func(c, arg, i, 0);
1028  if (ret)
1029  ret[i] = r;
1030  }
1031  return 0;
1032 }
1033 
1035 {
1036  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
1037  return desc->flags & AV_PIX_FMT_FLAG_HWACCEL;
1038 }
1039 
1041 {
1042  while (*fmt != AV_PIX_FMT_NONE && is_hwaccel_pix_fmt(*fmt))
1043  ++fmt;
1044  return fmt[0];
1045 }
1046 
1048 {
1049 #if LIBAVCODEC_VERSION_MAJOR >= 55
1050  // extended_data should explicitly be freed when needed, this code is unsafe currently
1051  // also this is not compatible to the <55 ABI/API
1052  if (frame->extended_data != frame->data && 0)
1053  av_freep(&frame->extended_data);
1054 #endif
1055 
1056  memset(frame, 0, sizeof(AVFrame));
1057 
1058  frame->pts =
1059  frame->pkt_dts =
1060  frame->pkt_pts = AV_NOPTS_VALUE;
1062  av_frame_set_pkt_duration (frame, 0);
1063  av_frame_set_pkt_pos (frame, -1);
1064  av_frame_set_pkt_size (frame, -1);
1065  frame->key_frame = 1;
1066  frame->sample_aspect_ratio = (AVRational) {0, 1 };
1067  frame->format = -1; /* unknown */
1068  frame->extended_data = frame->data;
1070 }
1071 
1073 {
1074  return av_frame_alloc();
1075 }
1076 
1078 {
1079  AVFrame *f;
1080 
1081  if (!frame || !*frame)
1082  return;
1083 
1084  f = *frame;
1085 
1086  if (f->extended_data != f->data)
1087  av_freep(&f->extended_data);
1088 
1089  av_freep(frame);
1090 }
1091 
1092 MAKE_ACCESSORS(AVCodecContext, codec, AVRational, pkt_timebase)
1093 MAKE_ACCESSORS(AVCodecContext, codec, const AVCodecDescriptor *, codec_descriptor)
1094 MAKE_ACCESSORS(AVCodecContext, codec, int, lowres)
1095 MAKE_ACCESSORS(AVCodecContext, codec, int, seek_preroll)
1096 
1098 {
1099  return codec->max_lowres;
1100 }
1101 
1103 {
1104  memset(sub, 0, sizeof(*sub));
1105  sub->pts = AV_NOPTS_VALUE;
1106 }
1107 
1109 {
1110  int bit_rate;
1111  int bits_per_sample;
1112 
1113  switch (ctx->codec_type) {
1114  case AVMEDIA_TYPE_VIDEO:
1115  case AVMEDIA_TYPE_DATA:
1116  case AVMEDIA_TYPE_SUBTITLE:
1118  bit_rate = ctx->bit_rate;
1119  break;
1120  case AVMEDIA_TYPE_AUDIO:
1121  bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
1122  bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
1123  break;
1124  default:
1125  bit_rate = 0;
1126  break;
1127  }
1128  return bit_rate;
1129 }
1130 
1131 #if FF_API_AVCODEC_OPEN
1132 int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
1133 {
1134  return avcodec_open2(avctx, codec, NULL);
1135 }
1136 #endif
1137 
1139 {
1140  int ret = 0;
1141 
1143 
1144  ret = avcodec_open2(avctx, codec, options);
1145 
1146  ff_lock_avcodec(avctx);
1147  return ret;
1148 }
1149 
1151 {
1152  int ret = 0;
1153  AVDictionary *tmp = NULL;
1154 
1155  if (avcodec_is_open(avctx))
1156  return 0;
1157 
1158  if ((!codec && !avctx->codec)) {
1159  av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2()\n");
1160  return AVERROR(EINVAL);
1161  }
1162  if ((codec && avctx->codec && codec != avctx->codec)) {
1163  av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
1164  "but %s passed to avcodec_open2()\n", avctx->codec->name, codec->name);
1165  return AVERROR(EINVAL);
1166  }
1167  if (!codec)
1168  codec = avctx->codec;
1169 
1170  if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
1171  return AVERROR(EINVAL);
1172 
1173  if (options)
1174  av_dict_copy(&tmp, *options, 0);
1175 
1176  ret = ff_lock_avcodec(avctx);
1177  if (ret < 0)
1178  return ret;
1179 
1180  avctx->internal = av_mallocz(sizeof(AVCodecInternal));
1181  if (!avctx->internal) {
1182  ret = AVERROR(ENOMEM);
1183  goto end;
1184  }
1185 
1186  avctx->internal->pool = av_mallocz(sizeof(*avctx->internal->pool));
1187  if (!avctx->internal->pool) {
1188  ret = AVERROR(ENOMEM);
1189  goto free_and_end;
1190  }
1191 
1192  if (codec->priv_data_size > 0) {
1193  if (!avctx->priv_data) {
1194  avctx->priv_data = av_mallocz(codec->priv_data_size);
1195  if (!avctx->priv_data) {
1196  ret = AVERROR(ENOMEM);
1197  goto end;
1198  }
1199  if (codec->priv_class) {
1200  *(const AVClass **)avctx->priv_data = codec->priv_class;
1202  }
1203  }
1204  if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
1205  goto free_and_end;
1206  } else {
1207  avctx->priv_data = NULL;
1208  }
1209  if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
1210  goto free_and_end;
1211 
1212  // only call avcodec_set_dimensions() for non H.264/VP6F codecs so as not to overwrite previously setup dimensions
1213  if (!(avctx->coded_width && avctx->coded_height && avctx->width && avctx->height &&
1214  (avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_VP6F))) {
1215  if (avctx->coded_width && avctx->coded_height)
1216  avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
1217  else if (avctx->width && avctx->height)
1218  avcodec_set_dimensions(avctx, avctx->width, avctx->height);
1219  }
1220 
1221  if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
1222  && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
1223  || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
1224  av_log(avctx, AV_LOG_WARNING, "Ignoring invalid width/height values\n");
1225  avcodec_set_dimensions(avctx, 0, 0);
1226  }
1227 
1228  /* if the decoder init function was already called previously,
1229  * free the already allocated subtitle_header before overwriting it */
1230  if (av_codec_is_decoder(codec))
1231  av_freep(&avctx->subtitle_header);
1232 
1233  if (avctx->channels > FF_SANE_NB_CHANNELS) {
1234  ret = AVERROR(EINVAL);
1235  goto free_and_end;
1236  }
1237 
1238  avctx->codec = codec;
1239  if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
1240  avctx->codec_id == AV_CODEC_ID_NONE) {
1241  avctx->codec_type = codec->type;
1242  avctx->codec_id = codec->id;
1243  }
1244  if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
1245  && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
1246  av_log(avctx, AV_LOG_ERROR, "Codec type or id mismatches\n");
1247  ret = AVERROR(EINVAL);
1248  goto free_and_end;
1249  }
1250  avctx->frame_number = 0;
1252 
1253  if (avctx->codec->capabilities & CODEC_CAP_EXPERIMENTAL &&
1255  const char *codec_string = av_codec_is_encoder(codec) ? "encoder" : "decoder";
1256  AVCodec *codec2;
1257  av_log(avctx, AV_LOG_ERROR,
1258  "The %s '%s' is experimental but experimental codecs are not enabled, "
1259  "add '-strict %d' if you want to use it.\n",
1260  codec_string, codec->name, FF_COMPLIANCE_EXPERIMENTAL);
1261  codec2 = av_codec_is_encoder(codec) ? avcodec_find_encoder(codec->id) : avcodec_find_decoder(codec->id);
1262  if (!(codec2->capabilities & CODEC_CAP_EXPERIMENTAL))
1263  av_log(avctx, AV_LOG_ERROR, "Alternatively use the non experimental %s '%s'.\n",
1264  codec_string, codec2->name);
1265  ret = AVERROR_EXPERIMENTAL;
1266  goto free_and_end;
1267  }
1268 
1269  if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
1270  (!avctx->time_base.num || !avctx->time_base.den)) {
1271  avctx->time_base.num = 1;
1272  avctx->time_base.den = avctx->sample_rate;
1273  }
1274 
1275  if (!HAVE_THREADS)
1276  av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
1277 
1278  if (CONFIG_FRAME_THREAD_ENCODER) {
1279  ff_unlock_avcodec(); //we will instanciate a few encoders thus kick the counter to prevent false detection of a problem
1280  ret = ff_frame_thread_encoder_init(avctx, options ? *options : NULL);
1281  ff_lock_avcodec(avctx);
1282  if (ret < 0)
1283  goto free_and_end;
1284  }
1285 
1286  if (HAVE_THREADS
1288  ret = ff_thread_init(avctx);
1289  if (ret < 0) {
1290  goto free_and_end;
1291  }
1292  }
1293  if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
1294  avctx->thread_count = 1;
1295 
1296  if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
1297  av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
1298  avctx->codec->max_lowres);
1299  ret = AVERROR(EINVAL);
1300  goto free_and_end;
1301  }
1302 
1303  if (av_codec_is_encoder(avctx->codec)) {
1304  int i;
1305  if (avctx->codec->sample_fmts) {
1306  for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
1307  if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
1308  break;
1309  if (avctx->channels == 1 &&
1312  avctx->sample_fmt = avctx->codec->sample_fmts[i];
1313  break;
1314  }
1315  }
1316  if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
1317  char buf[128];
1318  snprintf(buf, sizeof(buf), "%d", avctx->sample_fmt);
1319  av_log(avctx, AV_LOG_ERROR, "Specified sample format %s is invalid or not supported\n",
1320  (char *)av_x_if_null(av_get_sample_fmt_name(avctx->sample_fmt), buf));
1321  ret = AVERROR(EINVAL);
1322  goto free_and_end;
1323  }
1324  }
1325  if (avctx->codec->pix_fmts) {
1326  for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
1327  if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
1328  break;
1329  if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE
1330  && !((avctx->codec_id == AV_CODEC_ID_MJPEG || avctx->codec_id == AV_CODEC_ID_LJPEG)
1332  char buf[128];
1333  snprintf(buf, sizeof(buf), "%d", avctx->pix_fmt);
1334  av_log(avctx, AV_LOG_ERROR, "Specified pixel format %s is invalid or not supported\n",
1335  (char *)av_x_if_null(av_get_pix_fmt_name(avctx->pix_fmt), buf));
1336  ret = AVERROR(EINVAL);
1337  goto free_and_end;
1338  }
1339  }
1340  if (avctx->codec->supported_samplerates) {
1341  for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
1342  if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
1343  break;
1344  if (avctx->codec->supported_samplerates[i] == 0) {
1345  av_log(avctx, AV_LOG_ERROR, "Specified sample rate %d is not supported\n",
1346  avctx->sample_rate);
1347  ret = AVERROR(EINVAL);
1348  goto free_and_end;
1349  }
1350  }
1351  if (avctx->codec->channel_layouts) {
1352  if (!avctx->channel_layout) {
1353  av_log(avctx, AV_LOG_WARNING, "Channel layout not specified\n");
1354  } else {
1355  for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
1356  if (avctx->channel_layout == avctx->codec->channel_layouts[i])
1357  break;
1358  if (avctx->codec->channel_layouts[i] == 0) {
1359  char buf[512];
1360  av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
1361  av_log(avctx, AV_LOG_ERROR, "Specified channel layout '%s' is not supported\n", buf);
1362  ret = AVERROR(EINVAL);
1363  goto free_and_end;
1364  }
1365  }
1366  }
1367  if (avctx->channel_layout && avctx->channels) {
1368  int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
1369  if (channels != avctx->channels) {
1370  char buf[512];
1371  av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
1372  av_log(avctx, AV_LOG_ERROR,
1373  "Channel layout '%s' with %d channels does not match number of specified channels %d\n",
1374  buf, channels, avctx->channels);
1375  ret = AVERROR(EINVAL);
1376  goto free_and_end;
1377  }
1378  } else if (avctx->channel_layout) {
1380  }
1381  if(avctx->codec_type == AVMEDIA_TYPE_VIDEO &&
1382  avctx->codec_id != AV_CODEC_ID_PNG // For mplayer
1383  ) {
1384  if (avctx->width <= 0 || avctx->height <= 0) {
1385  av_log(avctx, AV_LOG_ERROR, "dimensions not set\n");
1386  ret = AVERROR(EINVAL);
1387  goto free_and_end;
1388  }
1389  }
1390  if ( (avctx->codec_type == AVMEDIA_TYPE_VIDEO || avctx->codec_type == AVMEDIA_TYPE_AUDIO)
1391  && avctx->bit_rate>0 && avctx->bit_rate<1000) {
1392  av_log(avctx, AV_LOG_WARNING, "Bitrate %d is extremely low, maybe you mean %dk\n", avctx->bit_rate, avctx->bit_rate);
1393  }
1394 
1395  if (!avctx->rc_initial_buffer_occupancy)
1396  avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3 / 4;
1397  }
1398 
1400  avctx->pts_correction_num_faulty_dts = 0;
1401  avctx->pts_correction_last_pts =
1402  avctx->pts_correction_last_dts = INT64_MIN;
1403 
1404  if ( avctx->codec->init && (!(avctx->active_thread_type&FF_THREAD_FRAME)
1405  || avctx->internal->frame_thread_encoder)) {
1406  ret = avctx->codec->init(avctx);
1407  if (ret < 0) {
1408  goto free_and_end;
1409  }
1410  }
1411 
1412  ret=0;
1413 
1414  if (av_codec_is_decoder(avctx->codec)) {
1415  if (!avctx->bit_rate)
1416  avctx->bit_rate = get_bit_rate(avctx);
1417  /* validate channel layout from the decoder */
1418  if (avctx->channel_layout) {
1419  int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
1420  if (!avctx->channels)
1421  avctx->channels = channels;
1422  else if (channels != avctx->channels) {
1423  char buf[512];
1424  av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
1425  av_log(avctx, AV_LOG_WARNING,
1426  "Channel layout '%s' with %d channels does not match specified number of channels %d: "
1427  "ignoring specified channel layout\n",
1428  buf, channels, avctx->channels);
1429  avctx->channel_layout = 0;
1430  }
1431  }
1432  if (avctx->channels && avctx->channels < 0 ||
1433  avctx->channels > FF_SANE_NB_CHANNELS) {
1434  ret = AVERROR(EINVAL);
1435  goto free_and_end;
1436  }
1437  if (avctx->sub_charenc) {
1438  if (avctx->codec_type != AVMEDIA_TYPE_SUBTITLE) {
1439  av_log(avctx, AV_LOG_ERROR, "Character encoding is only "
1440  "supported with subtitles codecs\n");
1441  ret = AVERROR(EINVAL);
1442  goto free_and_end;
1443  } else if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB) {
1444  av_log(avctx, AV_LOG_WARNING, "Codec '%s' is bitmap-based, "
1445  "subtitles character encoding will be ignored\n",
1446  avctx->codec_descriptor->name);
1448  } else {
1449  /* input character encoding is set for a text based subtitle
1450  * codec at this point */
1453 
1455 #if CONFIG_ICONV
1456  iconv_t cd = iconv_open("UTF-8", avctx->sub_charenc);
1457  if (cd == (iconv_t)-1) {
1458  av_log(avctx, AV_LOG_ERROR, "Unable to open iconv context "
1459  "with input character encoding \"%s\"\n", avctx->sub_charenc);
1460  ret = AVERROR(errno);
1461  goto free_and_end;
1462  }
1463  iconv_close(cd);
1464 #else
1465  av_log(avctx, AV_LOG_ERROR, "Character encoding subtitles "
1466  "conversion needs a libavcodec built with iconv support "
1467  "for this codec\n");
1468  ret = AVERROR(ENOSYS);
1469  goto free_and_end;
1470 #endif
1471  }
1472  }
1473  }
1474  }
1475 end:
1477  if (options) {
1478  av_dict_free(options);
1479  *options = tmp;
1480  }
1481 
1482  return ret;
1483 free_and_end:
1484  av_dict_free(&tmp);
1485  av_freep(&avctx->priv_data);
1486  if (avctx->internal)
1487  av_freep(&avctx->internal->pool);
1488  av_freep(&avctx->internal);
1489  avctx->codec = NULL;
1490  goto end;
1491 }
1492 
1493 int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
1494 {
1495  if (avpkt->size < 0) {
1496  av_log(avctx, AV_LOG_ERROR, "Invalid negative user packet size %d\n", avpkt->size);
1497  return AVERROR(EINVAL);
1498  }
1499  if (size < 0 || size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
1500  av_log(avctx, AV_LOG_ERROR, "Invalid minimum required packet size %"PRId64" (max allowed is %d)\n",
1501  size, INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE);
1502  return AVERROR(EINVAL);
1503  }
1504 
1505  if (avctx) {
1506  av_assert0(!avpkt->data || avpkt->data != avctx->internal->byte_buffer);
1507  if (!avpkt->data || avpkt->size < size) {
1509  avpkt->data = avctx->internal->byte_buffer;
1510  avpkt->size = avctx->internal->byte_buffer_size;
1511  avpkt->destruct = NULL;
1512  }
1513  }
1514 
1515  if (avpkt->data) {
1516  AVBufferRef *buf = avpkt->buf;
1517 #if FF_API_DESTRUCT_PACKET
1519  void *destruct = avpkt->destruct;
1521 #endif
1522 
1523  if (avpkt->size < size) {
1524  av_log(avctx, AV_LOG_ERROR, "User packet is too small (%d < %"PRId64")\n", avpkt->size, size);
1525  return AVERROR(EINVAL);
1526  }
1527 
1528  av_init_packet(avpkt);
1529 #if FF_API_DESTRUCT_PACKET
1531  avpkt->destruct = destruct;
1533 #endif
1534  avpkt->buf = buf;
1535  avpkt->size = size;
1536  return 0;
1537  } else {
1538  int ret = av_new_packet(avpkt, size);
1539  if (ret < 0)
1540  av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %"PRId64"\n", size);
1541  return ret;
1542  }
1543 }
1544 
1546 {
1547  return ff_alloc_packet2(NULL, avpkt, size);
1548 }
1549 
1550 /**
1551  * Pad last frame with silence.
1552  */
1553 static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
1554 {
1555  AVFrame *frame = NULL;
1556  int ret;
1557 
1558  if (!(frame = avcodec_alloc_frame()))
1559  return AVERROR(ENOMEM);
1560 
1561  frame->format = src->format;
1562  frame->channel_layout = src->channel_layout;
1564  frame->nb_samples = s->frame_size;
1565  ret = av_frame_get_buffer(frame, 32);
1566  if (ret < 0)
1567  goto fail;
1568 
1569  ret = av_frame_copy_props(frame, src);
1570  if (ret < 0)
1571  goto fail;
1572 
1573  if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
1574  src->nb_samples, s->channels, s->sample_fmt)) < 0)
1575  goto fail;
1576  if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
1577  frame->nb_samples - src->nb_samples,
1578  s->channels, s->sample_fmt)) < 0)
1579  goto fail;
1580 
1581  *dst = frame;
1582 
1583  return 0;
1584 
1585 fail:
1586  av_frame_free(&frame);
1587  return ret;
1588 }
1589 
1591  AVPacket *avpkt,
1592  const AVFrame *frame,
1593  int *got_packet_ptr)
1594 {
1595  AVFrame tmp;
1596  AVFrame *padded_frame = NULL;
1597  int ret;
1598  AVPacket user_pkt = *avpkt;
1599  int needs_realloc = !user_pkt.data;
1600 
1601  *got_packet_ptr = 0;
1602 
1603  if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
1604  av_free_packet(avpkt);
1605  av_init_packet(avpkt);
1606  return 0;
1607  }
1608 
1609  /* ensure that extended_data is properly set */
1610  if (frame && !frame->extended_data) {
1611  if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
1612  avctx->channels > AV_NUM_DATA_POINTERS) {
1613  av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
1614  "with more than %d channels, but extended_data is not set.\n",
1616  return AVERROR(EINVAL);
1617  }
1618  av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
1619 
1620  tmp = *frame;
1621  tmp.extended_data = tmp.data;
1622  frame = &tmp;
1623  }
1624 
1625  /* check for valid frame size */
1626  if (frame) {
1628  if (frame->nb_samples > avctx->frame_size) {
1629  av_log(avctx, AV_LOG_ERROR, "more samples than frame size (avcodec_encode_audio2)\n");
1630  return AVERROR(EINVAL);
1631  }
1632  } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
1633  if (frame->nb_samples < avctx->frame_size &&
1634  !avctx->internal->last_audio_frame) {
1635  ret = pad_last_frame(avctx, &padded_frame, frame);
1636  if (ret < 0)
1637  return ret;
1638 
1639  frame = padded_frame;
1640  avctx->internal->last_audio_frame = 1;
1641  }
1642 
1643  if (frame->nb_samples != avctx->frame_size) {
1644  av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) != frame_size (%d) (avcodec_encode_audio2)\n", frame->nb_samples, avctx->frame_size);
1645  ret = AVERROR(EINVAL);
1646  goto end;
1647  }
1648  }
1649  }
1650 
1651  ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1652  if (!ret) {
1653  if (*got_packet_ptr) {
1654  if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
1655  if (avpkt->pts == AV_NOPTS_VALUE)
1656  avpkt->pts = frame->pts;
1657  if (!avpkt->duration)
1658  avpkt->duration = ff_samples_to_time_base(avctx,
1659  frame->nb_samples);
1660  }
1661  avpkt->dts = avpkt->pts;
1662  } else {
1663  avpkt->size = 0;
1664  }
1665  }
1666  if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
1667  needs_realloc = 0;
1668  if (user_pkt.data) {
1669  if (user_pkt.size >= avpkt->size) {
1670  memcpy(user_pkt.data, avpkt->data, avpkt->size);
1671  } else {
1672  av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
1673  avpkt->size = user_pkt.size;
1674  ret = -1;
1675  }
1676  avpkt->buf = user_pkt.buf;
1677  avpkt->data = user_pkt.data;
1678  avpkt->destruct = user_pkt.destruct;
1679  } else {
1680  if (av_dup_packet(avpkt) < 0) {
1681  ret = AVERROR(ENOMEM);
1682  }
1683  }
1684  }
1685 
1686  if (!ret) {
1687  if (needs_realloc && avpkt->data) {
1688  ret = av_buffer_realloc(&avpkt->buf, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
1689  if (ret >= 0)
1690  avpkt->data = avpkt->buf->data;
1691  }
1692 
1693  avctx->frame_number++;
1694  }
1695 
1696  if (ret < 0 || !*got_packet_ptr) {
1697  av_free_packet(avpkt);
1698  av_init_packet(avpkt);
1699  goto end;
1700  }
1701 
1702  /* NOTE: if we add any audio encoders which output non-keyframe packets,
1703  * this needs to be moved to the encoders, but for now we can do it
1704  * here to simplify things */
1705  avpkt->flags |= AV_PKT_FLAG_KEY;
1706 
1707 end:
1708  av_frame_free(&padded_frame);
1709 
1710  return ret;
1711 }
1712 
1713 #if FF_API_OLD_ENCODE_AUDIO
1714 int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
1715  uint8_t *buf, int buf_size,
1716  const short *samples)
1717 {
1718  AVPacket pkt;
1719  AVFrame *frame;
1720  int ret, samples_size, got_packet;
1721 
1722  av_init_packet(&pkt);
1723  pkt.data = buf;
1724  pkt.size = buf_size;
1725 
1726  if (samples) {
1727  frame = av_frame_alloc();
1728  if (!frame)
1729  return AVERROR(ENOMEM);
1730 
1731  if (avctx->frame_size) {
1732  frame->nb_samples = avctx->frame_size;
1733  } else {
1734  /* if frame_size is not set, the number of samples must be
1735  * calculated from the buffer size */
1736  int64_t nb_samples;
1737  if (!av_get_bits_per_sample(avctx->codec_id)) {
1738  av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
1739  "support this codec\n");
1740  av_frame_free(&frame);
1741  return AVERROR(EINVAL);
1742  }
1743  nb_samples = (int64_t)buf_size * 8 /
1744  (av_get_bits_per_sample(avctx->codec_id) *
1745  avctx->channels);
1746  if (nb_samples >= INT_MAX) {
1747  av_frame_free(&frame);
1748  return AVERROR(EINVAL);
1749  }
1750  frame->nb_samples = nb_samples;
1751  }
1752 
1753  /* it is assumed that the samples buffer is large enough based on the
1754  * relevant parameters */
1755  samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
1756  frame->nb_samples,
1757  avctx->sample_fmt, 1);
1758  if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
1759  avctx->sample_fmt,
1760  (const uint8_t *)samples,
1761  samples_size, 1)) < 0) {
1762  av_frame_free(&frame);
1763  return ret;
1764  }
1765 
1766  /* fabricate frame pts from sample count.
1767  * this is needed because the avcodec_encode_audio() API does not have
1768  * a way for the user to provide pts */
1769  if (avctx->sample_rate && avctx->time_base.num)
1770  frame->pts = ff_samples_to_time_base(avctx,
1771  avctx->internal->sample_count);
1772  else
1773  frame->pts = AV_NOPTS_VALUE;
1774  avctx->internal->sample_count += frame->nb_samples;
1775  } else {
1776  frame = NULL;
1777  }
1778 
1779  got_packet = 0;
1780  ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
1781  if (!ret && got_packet && avctx->coded_frame) {
1782  avctx->coded_frame->pts = pkt.pts;
1783  avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
1784  }
1785  /* free any side data since we cannot return it */
1787 
1788  if (frame && frame->extended_data != frame->data)
1789  av_freep(&frame->extended_data);
1790 
1791  av_frame_free(&frame);
1792  return ret ? ret : pkt.size;
1793 }
1794 
1795 #endif
1796 
1797 #if FF_API_OLD_ENCODE_VIDEO
1798 int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
1799  const AVFrame *pict)
1800 {
1801  AVPacket pkt;
1802  int ret, got_packet = 0;
1803 
1804  if (buf_size < FF_MIN_BUFFER_SIZE) {
1805  av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
1806  return -1;
1807  }
1808 
1809  av_init_packet(&pkt);
1810  pkt.data = buf;
1811  pkt.size = buf_size;
1812 
1813  ret = avcodec_encode_video2(avctx, &pkt, pict, &got_packet);
1814  if (!ret && got_packet && avctx->coded_frame) {
1815  avctx->coded_frame->pts = pkt.pts;
1816  avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
1817  }
1818 
1819  /* free any side data since we cannot return it */
1820  if (pkt.side_data_elems > 0) {
1821  int i;
1822  for (i = 0; i < pkt.side_data_elems; i++)
1823  av_free(pkt.side_data[i].data);
1824  av_freep(&pkt.side_data);
1825  pkt.side_data_elems = 0;
1826  }
1827 
1828  return ret ? ret : pkt.size;
1829 }
1830 
1831 #endif
1832 
1834  AVPacket *avpkt,
1835  const AVFrame *frame,
1836  int *got_packet_ptr)
1837 {
1838  int ret;
1839  AVPacket user_pkt = *avpkt;
1840  int needs_realloc = !user_pkt.data;
1841 
1842  *got_packet_ptr = 0;
1843 
1844  if(CONFIG_FRAME_THREAD_ENCODER &&
1846  return ff_thread_video_encode_frame(avctx, avpkt, frame, got_packet_ptr);
1847 
1848  if ((avctx->flags&CODEC_FLAG_PASS1) && avctx->stats_out)
1849  avctx->stats_out[0] = '\0';
1850 
1851  if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
1852  av_free_packet(avpkt);
1853  av_init_packet(avpkt);
1854  avpkt->size = 0;
1855  return 0;
1856  }
1857 
1858  if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
1859  return AVERROR(EINVAL);
1860 
1861  av_assert0(avctx->codec->encode2);
1862 
1863  ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1864  av_assert0(ret <= 0);
1865 
1866  if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
1867  needs_realloc = 0;
1868  if (user_pkt.data) {
1869  if (user_pkt.size >= avpkt->size) {
1870  memcpy(user_pkt.data, avpkt->data, avpkt->size);
1871  } else {
1872  av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
1873  avpkt->size = user_pkt.size;
1874  ret = -1;
1875  }
1876  avpkt->buf = user_pkt.buf;
1877  avpkt->data = user_pkt.data;
1878  avpkt->destruct = user_pkt.destruct;
1879  } else {
1880  if (av_dup_packet(avpkt) < 0) {
1881  ret = AVERROR(ENOMEM);
1882  }
1883  }
1884  }
1885 
1886  if (!ret) {
1887  if (!*got_packet_ptr)
1888  avpkt->size = 0;
1889  else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
1890  avpkt->pts = avpkt->dts = frame->pts;
1891 
1892  if (needs_realloc && avpkt->data) {
1893  ret = av_buffer_realloc(&avpkt->buf, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
1894  if (ret >= 0)
1895  avpkt->data = avpkt->buf->data;
1896  }
1897 
1898  avctx->frame_number++;
1899  }
1900 
1901  if (ret < 0 || !*got_packet_ptr)
1902  av_free_packet(avpkt);
1903  else
1905 
1906  emms_c();
1907  return ret;
1908 }
1909 
1910 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
1911  const AVSubtitle *sub)
1912 {
1913  int ret;
1914  if (sub->start_display_time) {
1915  av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
1916  return -1;
1917  }
1918 
1919  ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
1920  avctx->frame_number++;
1921  return ret;
1922 }
1923 
1924 /**
1925  * Attempt to guess proper monotonic timestamps for decoded video frames
1926  * which might have incorrect times. Input timestamps may wrap around, in
1927  * which case the output will as well.
1928  *
1929  * @param pts the pts field of the decoded AVPacket, as passed through
1930  * AVFrame.pkt_pts
1931  * @param dts the dts field of the decoded AVPacket
1932  * @return one of the input values, may be AV_NOPTS_VALUE
1933  */
1934 static int64_t guess_correct_pts(AVCodecContext *ctx,
1935  int64_t reordered_pts, int64_t dts)
1936 {
1937  int64_t pts = AV_NOPTS_VALUE;
1938 
1939  if (dts != AV_NOPTS_VALUE) {
1941  ctx->pts_correction_last_dts = dts;
1942  }
1943  if (reordered_pts != AV_NOPTS_VALUE) {
1944  ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
1945  ctx->pts_correction_last_pts = reordered_pts;
1946  }
1948  && reordered_pts != AV_NOPTS_VALUE)
1949  pts = reordered_pts;
1950  else
1951  pts = dts;
1952 
1953  return pts;
1954 }
1955 
1956 static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
1957 {
1958  int size = 0;
1959  const uint8_t *data;
1960  uint32_t flags;
1961 
1962  if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
1963  return;
1964 
1965  data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
1966  if (!data || size < 4)
1967  return;
1968  flags = bytestream_get_le32(&data);
1969  size -= 4;
1970  if (size < 4) /* Required for any of the changes */
1971  return;
1973  avctx->channels = bytestream_get_le32(&data);
1974  size -= 4;
1975  }
1977  if (size < 8)
1978  return;
1979  avctx->channel_layout = bytestream_get_le64(&data);
1980  size -= 8;
1981  }
1982  if (size < 4)
1983  return;
1985  avctx->sample_rate = bytestream_get_le32(&data);
1986  size -= 4;
1987  }
1989  if (size < 8)
1990  return;
1991  avctx->width = bytestream_get_le32(&data);
1992  avctx->height = bytestream_get_le32(&data);
1993  avcodec_set_dimensions(avctx, avctx->width, avctx->height);
1994  size -= 8;
1995  }
1996 }
1997 
1999 {
2000  int size, ret = 0;
2001  const uint8_t *side_metadata;
2002  const uint8_t *end;
2003 
2004  side_metadata = av_packet_get_side_data(avctx->pkt,
2006  if (!side_metadata)
2007  goto end;
2008  end = side_metadata + size;
2009  if (size && end[-1])
2010  return AVERROR_INVALIDDATA;
2011  while (side_metadata < end) {
2012  const uint8_t *key = side_metadata;
2013  const uint8_t *val = side_metadata + strlen(key) + 1;
2014  int ret;
2015 
2016  if (val >= end)
2017  return AVERROR_INVALIDDATA;
2018 
2019  ret = av_dict_set(avpriv_frame_get_metadatap(frame), key, val, 0);
2020  if (ret < 0)
2021  break;
2022  side_metadata = val + strlen(val) + 1;
2023  }
2024 end:
2025  return ret;
2026 }
2027 
2029  int *got_picture_ptr,
2030  const AVPacket *avpkt)
2031 {
2032  AVCodecInternal *avci = avctx->internal;
2033  int ret;
2034  // copy to ensure we do not change avpkt
2035  AVPacket tmp = *avpkt;
2036 
2037  if (!avctx->codec)
2038  return AVERROR(EINVAL);
2039  if (avctx->codec->type != AVMEDIA_TYPE_VIDEO) {
2040  av_log(avctx, AV_LOG_ERROR, "Invalid media type for video\n");
2041  return AVERROR(EINVAL);
2042  }
2043 
2044  *got_picture_ptr = 0;
2045  if ((avctx->coded_width || avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
2046  return AVERROR(EINVAL);
2047 
2048  avcodec_get_frame_defaults(picture);
2049 
2050  if (!avctx->refcounted_frames)
2051  av_frame_unref(&avci->to_free);
2052 
2053  if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
2054  int did_split = av_packet_split_side_data(&tmp);
2055  apply_param_change(avctx, &tmp);
2056  avctx->pkt = &tmp;
2057  if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
2058  ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
2059  &tmp);
2060  else {
2061  ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
2062  &tmp);
2063  picture->pkt_dts = avpkt->dts;
2064 
2065  if(!avctx->has_b_frames){
2066  av_frame_set_pkt_pos(picture, avpkt->pos);
2067  }
2068  //FIXME these should be under if(!avctx->has_b_frames)
2069  /* get_buffer is supposed to set frame parameters */
2070  if (!(avctx->codec->capabilities & CODEC_CAP_DR1)) {
2071  if (!picture->sample_aspect_ratio.num) picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
2072  if (!picture->width) picture->width = avctx->width;
2073  if (!picture->height) picture->height = avctx->height;
2074  if (picture->format == AV_PIX_FMT_NONE) picture->format = avctx->pix_fmt;
2075  }
2076  }
2077  add_metadata_from_side_data(avctx, picture);
2078 
2079  emms_c(); //needed to avoid an emms_c() call before every return;
2080 
2081  avctx->pkt = NULL;
2082  if (did_split) {
2084  if(ret == tmp.size)
2085  ret = avpkt->size;
2086  }
2087 
2088  if (ret < 0 && picture->data[0])
2089  av_frame_unref(picture);
2090 
2091  if (*got_picture_ptr) {
2092  if (!avctx->refcounted_frames) {
2093  avci->to_free = *picture;
2094  avci->to_free.extended_data = avci->to_free.data;
2095  memset(picture->buf, 0, sizeof(picture->buf));
2096  }
2097 
2098  avctx->frame_number++;
2100  guess_correct_pts(avctx,
2101  picture->pkt_pts,
2102  picture->pkt_dts));
2103  }
2104  } else
2105  ret = 0;
2106 
2107  /* many decoders assign whole AVFrames, thus overwriting extended_data;
2108  * make sure it's set correctly */
2109  picture->extended_data = picture->data;
2110 
2111  return ret;
2112 }
2113 
2114 #if FF_API_OLD_DECODE_AUDIO
2115 int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
2116  int *frame_size_ptr,
2117  AVPacket *avpkt)
2118 {
2119  AVFrame *frame = av_frame_alloc();
2120  int ret, got_frame = 0;
2121 
2122  if (!frame)
2123  return AVERROR(ENOMEM);
2124  if (avctx->get_buffer != avcodec_default_get_buffer) {
2125  av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
2126  "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
2127  av_log(avctx, AV_LOG_ERROR, "Please port your application to "
2128  "avcodec_decode_audio4()\n");
2129  avctx->get_buffer = avcodec_default_get_buffer;
2130  avctx->release_buffer = avcodec_default_release_buffer;
2131  }
2132 
2133  ret = avcodec_decode_audio4(avctx, frame, &got_frame, avpkt);
2134 
2135  if (ret >= 0 && got_frame) {
2136  int ch, plane_size;
2137  int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
2138  int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
2139  frame->nb_samples,
2140  avctx->sample_fmt, 1);
2141  if (*frame_size_ptr < data_size) {
2142  av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
2143  "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
2144  av_frame_free(&frame);
2145  return AVERROR(EINVAL);
2146  }
2147 
2148  memcpy(samples, frame->extended_data[0], plane_size);
2149 
2150  if (planar && avctx->channels > 1) {
2151  uint8_t *out = ((uint8_t *)samples) + plane_size;
2152  for (ch = 1; ch < avctx->channels; ch++) {
2153  memcpy(out, frame->extended_data[ch], plane_size);
2154  out += plane_size;
2155  }
2156  }
2157  *frame_size_ptr = data_size;
2158  } else {
2159  *frame_size_ptr = 0;
2160  }
2161  av_frame_free(&frame);
2162  return ret;
2163 }
2164 
2165 #endif
2166 
2168  AVFrame *frame,
2169  int *got_frame_ptr,
2170  const AVPacket *avpkt)
2171 {
2172  AVCodecInternal *avci = avctx->internal;
2173  int planar, channels;
2174  int ret = 0;
2175 
2176  *got_frame_ptr = 0;
2177 
2178  if (!avpkt->data && avpkt->size) {
2179  av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
2180  return AVERROR(EINVAL);
2181  }
2182  if (!avctx->codec)
2183  return AVERROR(EINVAL);
2184  if (avctx->codec->type != AVMEDIA_TYPE_AUDIO) {
2185  av_log(avctx, AV_LOG_ERROR, "Invalid media type for audio\n");
2186  return AVERROR(EINVAL);
2187  }
2188 
2190 
2191  if (!avctx->refcounted_frames)
2192  av_frame_unref(&avci->to_free);
2193 
2194  if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
2195  uint8_t *side;
2196  int side_size;
2197  uint32_t discard_padding = 0;
2198  // copy to ensure we do not change avpkt
2199  AVPacket tmp = *avpkt;
2200  int did_split = av_packet_split_side_data(&tmp);
2201  apply_param_change(avctx, &tmp);
2202 
2203  avctx->pkt = &tmp;
2204  if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
2205  ret = ff_thread_decode_frame(avctx, frame, got_frame_ptr, &tmp);
2206  else {
2207  ret = avctx->codec->decode(avctx, frame, got_frame_ptr, &tmp);
2208  frame->pkt_dts = avpkt->dts;
2209  }
2210  if (ret >= 0 && *got_frame_ptr) {
2211  add_metadata_from_side_data(avctx, frame);
2212  avctx->frame_number++;
2214  guess_correct_pts(avctx,
2215  frame->pkt_pts,
2216  frame->pkt_dts));
2217  if (frame->format == AV_SAMPLE_FMT_NONE)
2218  frame->format = avctx->sample_fmt;
2219  if (!frame->channel_layout)
2220  frame->channel_layout = avctx->channel_layout;
2221  if (!av_frame_get_channels(frame))
2222  av_frame_set_channels(frame, avctx->channels);
2223  if (!frame->sample_rate)
2224  frame->sample_rate = avctx->sample_rate;
2225  }
2226 
2227  side= av_packet_get_side_data(avctx->pkt, AV_PKT_DATA_SKIP_SAMPLES, &side_size);
2228  if(side && side_size>=10) {
2229  avctx->internal->skip_samples = AV_RL32(side);
2230  av_log(avctx, AV_LOG_DEBUG, "skip %d samples due to side data\n",
2231  avctx->internal->skip_samples);
2232  discard_padding = AV_RL32(side + 4);
2233  }
2234  if (avctx->internal->skip_samples && *got_frame_ptr) {
2235  if(frame->nb_samples <= avctx->internal->skip_samples){
2236  *got_frame_ptr = 0;
2237  avctx->internal->skip_samples -= frame->nb_samples;
2238  av_log(avctx, AV_LOG_DEBUG, "skip whole frame, skip left: %d\n",
2239  avctx->internal->skip_samples);
2240  } else {
2242  frame->nb_samples - avctx->internal->skip_samples, avctx->channels, frame->format);
2243  if(avctx->pkt_timebase.num && avctx->sample_rate) {
2244  int64_t diff_ts = av_rescale_q(avctx->internal->skip_samples,
2245  (AVRational){1, avctx->sample_rate},
2246  avctx->pkt_timebase);
2247  if(frame->pkt_pts!=AV_NOPTS_VALUE)
2248  frame->pkt_pts += diff_ts;
2249  if(frame->pkt_dts!=AV_NOPTS_VALUE)
2250  frame->pkt_dts += diff_ts;
2251  if (av_frame_get_pkt_duration(frame) >= diff_ts)
2252  av_frame_set_pkt_duration(frame, av_frame_get_pkt_duration(frame) - diff_ts);
2253  } else {
2254  av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for skipped samples.\n");
2255  }
2256  av_log(avctx, AV_LOG_DEBUG, "skip %d/%d samples\n",
2257  avctx->internal->skip_samples, frame->nb_samples);
2258  frame->nb_samples -= avctx->internal->skip_samples;
2259  avctx->internal->skip_samples = 0;
2260  }
2261  }
2262 
2263  if (discard_padding > 0 && discard_padding <= frame->nb_samples && *got_frame_ptr) {
2264  if (discard_padding == frame->nb_samples) {
2265  *got_frame_ptr = 0;
2266  } else {
2267  if(avctx->pkt_timebase.num && avctx->sample_rate) {
2268  int64_t diff_ts = av_rescale_q(frame->nb_samples - discard_padding,
2269  (AVRational){1, avctx->sample_rate},
2270  avctx->pkt_timebase);
2271  if (av_frame_get_pkt_duration(frame) >= diff_ts)
2272  av_frame_set_pkt_duration(frame, av_frame_get_pkt_duration(frame) - diff_ts);
2273  } else {
2274  av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for discarded samples.\n");
2275  }
2276  av_log(avctx, AV_LOG_DEBUG, "discard %d/%d samples\n",
2277  discard_padding, frame->nb_samples);
2278  frame->nb_samples -= discard_padding;
2279  }
2280  }
2281 
2282  avctx->pkt = NULL;
2283  if (did_split) {
2285  if(ret == tmp.size)
2286  ret = avpkt->size;
2287  }
2288 
2289  if (ret >= 0 && *got_frame_ptr) {
2290  if (!avctx->refcounted_frames) {
2291  avci->to_free = *frame;
2292  avci->to_free.extended_data = avci->to_free.data;
2293  memset(frame->buf, 0, sizeof(frame->buf));
2294  frame->extended_buf = NULL;
2295  frame->nb_extended_buf = 0;
2296  }
2297  } else if (frame->data[0])
2298  av_frame_unref(frame);
2299  }
2300 
2301  /* many decoders assign whole AVFrames, thus overwriting extended_data;
2302  * make sure it's set correctly; assume decoders that actually use
2303  * extended_data are doing it correctly */
2304  if (*got_frame_ptr) {
2305  planar = av_sample_fmt_is_planar(frame->format);
2306  channels = av_frame_get_channels(frame);
2307  if (!(planar && channels > AV_NUM_DATA_POINTERS))
2308  frame->extended_data = frame->data;
2309  } else {
2310  frame->extended_data = NULL;
2311  }
2312 
2313  return ret;
2314 }
2315 
2316 #define UTF8_MAX_BYTES 4 /* 5 and 6 bytes sequences should not be used */
2318  AVPacket *outpkt, const AVPacket *inpkt)
2319 {
2320 #if CONFIG_ICONV
2321  iconv_t cd = (iconv_t)-1;
2322  int ret = 0;
2323  char *inb, *outb;
2324  size_t inl, outl;
2325  AVPacket tmp;
2326 #endif
2327 
2328  if (avctx->sub_charenc_mode != FF_SUB_CHARENC_MODE_PRE_DECODER || inpkt->size == 0)
2329  return 0;
2330 
2331 #if CONFIG_ICONV
2332  cd = iconv_open("UTF-8", avctx->sub_charenc);
2333  av_assert0(cd != (iconv_t)-1);
2334 
2335  inb = inpkt->data;
2336  inl = inpkt->size;
2337 
2338  if (inl >= INT_MAX / UTF8_MAX_BYTES - FF_INPUT_BUFFER_PADDING_SIZE) {
2339  av_log(avctx, AV_LOG_ERROR, "Subtitles packet is too big for recoding\n");
2340  ret = AVERROR(ENOMEM);
2341  goto end;
2342  }
2343 
2344  ret = av_new_packet(&tmp, inl * UTF8_MAX_BYTES);
2345  if (ret < 0)
2346  goto end;
2347  outpkt->buf = tmp.buf;
2348  outpkt->data = tmp.data;
2349  outpkt->size = tmp.size;
2350  outb = outpkt->data;
2351  outl = outpkt->size;
2352 
2353  if (iconv(cd, &inb, &inl, &outb, &outl) == (size_t)-1 ||
2354  iconv(cd, NULL, NULL, &outb, &outl) == (size_t)-1 ||
2355  outl >= outpkt->size || inl != 0) {
2356  av_log(avctx, AV_LOG_ERROR, "Unable to recode subtitle event \"%s\" "
2357  "from %s to UTF-8\n", inpkt->data, avctx->sub_charenc);
2358  av_free_packet(&tmp);
2359  ret = AVERROR(errno);
2360  goto end;
2361  }
2362  outpkt->size -= outl;
2363  memset(outpkt->data + outpkt->size, 0, outl);
2364 
2365 end:
2366  if (cd != (iconv_t)-1)
2367  iconv_close(cd);
2368  return ret;
2369 #else
2370  av_assert0(!"requesting subtitles recoding without iconv");
2371 #endif
2372 }
2373 
2374 static int utf8_check(const uint8_t *str)
2375 {
2376  const uint8_t *byte;
2377  uint32_t codepoint, min;
2378 
2379  while (*str) {
2380  byte = str;
2381  GET_UTF8(codepoint, *(byte++), return 0;);
2382  min = byte - str == 1 ? 0 : byte - str == 2 ? 0x80 :
2383  1 << (5 * (byte - str) - 4);
2384  if (codepoint < min || codepoint >= 0x110000 ||
2385  codepoint == 0xFFFE /* BOM */ ||
2386  codepoint >= 0xD800 && codepoint <= 0xDFFF /* surrogates */)
2387  return 0;
2388  str = byte;
2389  }
2390  return 1;
2391 }
2392 
2394  int *got_sub_ptr,
2395  AVPacket *avpkt)
2396 {
2397  int i, ret = 0;
2398 
2399  if (!avpkt->data && avpkt->size) {
2400  av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
2401  return AVERROR(EINVAL);
2402  }
2403  if (!avctx->codec)
2404  return AVERROR(EINVAL);
2405  if (avctx->codec->type != AVMEDIA_TYPE_SUBTITLE) {
2406  av_log(avctx, AV_LOG_ERROR, "Invalid media type for subtitles\n");
2407  return AVERROR(EINVAL);
2408  }
2409 
2410  *got_sub_ptr = 0;
2412 
2413  if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
2414  AVPacket pkt_recoded;
2415  AVPacket tmp = *avpkt;
2416  int did_split = av_packet_split_side_data(&tmp);
2417  //apply_param_change(avctx, &tmp);
2418 
2419  if (did_split) {
2420  /* FFMIN() prevents overflow in case the packet wasn't allocated with
2421  * proper padding.
2422  * If the side data is smaller than the buffer padding size, the
2423  * remaining bytes should have already been filled with zeros by the
2424  * original packet allocation anyway. */
2425  memset(tmp.data + tmp.size, 0,
2426  FFMIN(avpkt->size - tmp.size, FF_INPUT_BUFFER_PADDING_SIZE));
2427  }
2428 
2429  pkt_recoded = tmp;
2430  ret = recode_subtitle(avctx, &pkt_recoded, &tmp);
2431  if (ret < 0) {
2432  *got_sub_ptr = 0;
2433  } else {
2434  avctx->pkt = &pkt_recoded;
2435 
2436  if (avctx->pkt_timebase.den && avpkt->pts != AV_NOPTS_VALUE)
2437  sub->pts = av_rescale_q(avpkt->pts,
2438  avctx->pkt_timebase, AV_TIME_BASE_Q);
2439  ret = avctx->codec->decode(avctx, sub, got_sub_ptr, &pkt_recoded);
2440  av_assert1((ret >= 0) >= !!*got_sub_ptr &&
2441  !!*got_sub_ptr >= !!sub->num_rects);
2442 
2443  if (sub->num_rects && !sub->end_display_time && avpkt->duration &&
2444  avctx->pkt_timebase.num) {
2445  AVRational ms = { 1, 1000 };
2446  sub->end_display_time = av_rescale_q(avpkt->duration,
2447  avctx->pkt_timebase, ms);
2448  }
2449 
2450  for (i = 0; i < sub->num_rects; i++) {
2451  if (sub->rects[i]->ass && !utf8_check(sub->rects[i]->ass)) {
2452  av_log(avctx, AV_LOG_ERROR,
2453  "Invalid UTF-8 in decoded subtitles text; "
2454  "maybe missing -sub_charenc option\n");
2455  avsubtitle_free(sub);
2456  return AVERROR_INVALIDDATA;
2457  }
2458  }
2459 
2460  if (tmp.data != pkt_recoded.data) { // did we recode?
2461  /* prevent from destroying side data from original packet */
2462  pkt_recoded.side_data = NULL;
2463  pkt_recoded.side_data_elems = 0;
2464 
2465  av_free_packet(&pkt_recoded);
2466  }
2468  sub->format = 0;
2469  else if (avctx->codec_descriptor->props & AV_CODEC_PROP_TEXT_SUB)
2470  sub->format = 1;
2471  avctx->pkt = NULL;
2472  }
2473 
2474  if (did_split) {
2476  if(ret == tmp.size)
2477  ret = avpkt->size;
2478  }
2479 
2480  if (*got_sub_ptr)
2481  avctx->frame_number++;
2482  }
2483 
2484  return ret;
2485 }
2486 
2488 {
2489  int i;
2490 
2491  for (i = 0; i < sub->num_rects; i++) {
2492  av_freep(&sub->rects[i]->pict.data[0]);
2493  av_freep(&sub->rects[i]->pict.data[1]);
2494  av_freep(&sub->rects[i]->pict.data[2]);
2495  av_freep(&sub->rects[i]->pict.data[3]);
2496  av_freep(&sub->rects[i]->text);
2497  av_freep(&sub->rects[i]->ass);
2498  av_freep(&sub->rects[i]);
2499  }
2500 
2501  av_freep(&sub->rects);
2502 
2503  memset(sub, 0, sizeof(AVSubtitle));
2504 }
2505 
2507 {
2508  int ret = 0;
2509 
2511 
2512  ret = avcodec_close(avctx);
2513 
2514  ff_lock_avcodec(NULL);
2515  return ret;
2516 }
2517 
2519 {
2520  int ret;
2521 
2522  if (!avctx)
2523  return 0;
2524 
2525  ret = ff_lock_avcodec(avctx);
2526  if (ret < 0)
2527  return ret;
2528 
2529  if (avcodec_is_open(avctx)) {
2530  FramePool *pool = avctx->internal->pool;
2531  int i;
2532  if (CONFIG_FRAME_THREAD_ENCODER &&
2533  avctx->internal->frame_thread_encoder && avctx->thread_count > 1) {
2536  ff_lock_avcodec(avctx);
2537  }
2538  if (HAVE_THREADS && avctx->thread_opaque)
2539  ff_thread_free(avctx);
2540  if (avctx->codec && avctx->codec->close)
2541  avctx->codec->close(avctx);
2542  avctx->coded_frame = NULL;
2543  avctx->internal->byte_buffer_size = 0;
2544  av_freep(&avctx->internal->byte_buffer);
2545  if (!avctx->refcounted_frames)
2546  av_frame_unref(&avctx->internal->to_free);
2547  for (i = 0; i < FF_ARRAY_ELEMS(pool->pools); i++)
2548  av_buffer_pool_uninit(&pool->pools[i]);
2549  av_freep(&avctx->internal->pool);
2550  av_freep(&avctx->internal);
2551  }
2552 
2553  if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
2554  av_opt_free(avctx->priv_data);
2555  av_opt_free(avctx);
2556  av_freep(&avctx->priv_data);
2557  if (av_codec_is_encoder(avctx->codec))
2558  av_freep(&avctx->extradata);
2559  avctx->codec = NULL;
2560  avctx->active_thread_type = 0;
2561 
2563  return 0;
2564 }
2565 
2567 {
2568  switch(id){
2569  //This is for future deprecatec codec ids, its empty since
2570  //last major bump but will fill up again over time, please don't remove it
2571 // case AV_CODEC_ID_UTVIDEO_DEPRECATED: return AV_CODEC_ID_UTVIDEO;
2579  default : return id;
2580  }
2581 }
2582 
2583 static AVCodec *find_encdec(enum AVCodecID id, int encoder)
2584 {
2585  AVCodec *p, *experimental = NULL;
2586  p = first_avcodec;
2587  id= remap_deprecated_codec_id(id);
2588  while (p) {
2589  if ((encoder ? av_codec_is_encoder(p) : av_codec_is_decoder(p)) &&
2590  p->id == id) {
2591  if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
2592  experimental = p;
2593  } else
2594  return p;
2595  }
2596  p = p->next;
2597  }
2598  return experimental;
2599 }
2600 
2602 {
2603  return find_encdec(id, 1);
2604 }
2605 
2607 {
2608  AVCodec *p;
2609  if (!name)
2610  return NULL;
2611  p = first_avcodec;
2612  while (p) {
2613  if (av_codec_is_encoder(p) && strcmp(name, p->name) == 0)
2614  return p;
2615  p = p->next;
2616  }
2617  return NULL;
2618 }
2619 
2621 {
2622  return find_encdec(id, 0);
2623 }
2624 
2626 {
2627  AVCodec *p;
2628  if (!name)
2629  return NULL;
2630  p = first_avcodec;
2631  while (p) {
2632  if (av_codec_is_decoder(p) && strcmp(name, p->name) == 0)
2633  return p;
2634  p = p->next;
2635  }
2636  return NULL;
2637 }
2638 
2639 const char *avcodec_get_name(enum AVCodecID id)
2640 {
2641  const AVCodecDescriptor *cd;
2642  AVCodec *codec;
2643 
2644  if (id == AV_CODEC_ID_NONE)
2645  return "none";
2646  cd = avcodec_descriptor_get(id);
2647  if (cd)
2648  return cd->name;
2649  av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
2650  codec = avcodec_find_decoder(id);
2651  if (codec)
2652  return codec->name;
2653  codec = avcodec_find_encoder(id);
2654  if (codec)
2655  return codec->name;
2656  return "unknown_codec";
2657 }
2658 
2659 size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
2660 {
2661  int i, len, ret = 0;
2662 
2663 #define TAG_PRINT(x) \
2664  (((x) >= '0' && (x) <= '9') || \
2665  ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
2666  ((x) == '.' || (x) == ' ' || (x) == '-' || (x) == '_'))
2667 
2668  for (i = 0; i < 4; i++) {
2669  len = snprintf(buf, buf_size,
2670  TAG_PRINT(codec_tag & 0xFF) ? "%c" : "[%d]", codec_tag & 0xFF);
2671  buf += len;
2672  buf_size = buf_size > len ? buf_size - len : 0;
2673  ret += len;
2674  codec_tag >>= 8;
2675  }
2676  return ret;
2677 }
2678 
2679 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
2680 {
2681  const char *codec_type;
2682  const char *codec_name;
2683  const char *profile = NULL;
2684  const AVCodec *p;
2685  int bitrate;
2686  AVRational display_aspect_ratio;
2687 
2688  if (!buf || buf_size <= 0)
2689  return;
2690  codec_type = av_get_media_type_string(enc->codec_type);
2691  codec_name = avcodec_get_name(enc->codec_id);
2692  if (enc->profile != FF_PROFILE_UNKNOWN) {
2693  if (enc->codec)
2694  p = enc->codec;
2695  else
2696  p = encode ? avcodec_find_encoder(enc->codec_id) :
2698  if (p)
2699  profile = av_get_profile_name(p, enc->profile);
2700  }
2701 
2702  snprintf(buf, buf_size, "%s: %s", codec_type ? codec_type : "unknown",
2703  codec_name);
2704  buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
2705 
2706  if (enc->codec && strcmp(enc->codec->name, codec_name))
2707  snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", enc->codec->name);
2708 
2709  if (profile)
2710  snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
2711  if (enc->codec_tag) {
2712  char tag_buf[32];
2713  av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
2714  snprintf(buf + strlen(buf), buf_size - strlen(buf),
2715  " (%s / 0x%04X)", tag_buf, enc->codec_tag);
2716  }
2717 
2718  switch (enc->codec_type) {
2719  case AVMEDIA_TYPE_VIDEO:
2720  if (enc->pix_fmt != AV_PIX_FMT_NONE) {
2721  char detail[256] = "(";
2722  const char *colorspace_name;
2723  snprintf(buf + strlen(buf), buf_size - strlen(buf),
2724  ", %s",
2726  if (enc->bits_per_raw_sample &&
2728  av_strlcatf(detail, sizeof(detail), "%d bpc, ", enc->bits_per_raw_sample);
2730  av_strlcatf(detail, sizeof(detail),
2731  enc->color_range == AVCOL_RANGE_MPEG ? "tv, ": "pc, ");
2732 
2733  colorspace_name = av_get_colorspace_name(enc->colorspace);
2734  if (colorspace_name)
2735  av_strlcatf(detail, sizeof(detail), "%s, ", colorspace_name);
2736 
2737  if (strlen(detail) > 1) {
2738  detail[strlen(detail) - 2] = 0;
2739  av_strlcatf(buf, buf_size, "%s)", detail);
2740  }
2741  }
2742  if (enc->width) {
2743  snprintf(buf + strlen(buf), buf_size - strlen(buf),
2744  ", %dx%d",
2745  enc->width, enc->height);
2746  if (enc->sample_aspect_ratio.num) {
2747  av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
2748  enc->width * enc->sample_aspect_ratio.num,
2749  enc->height * enc->sample_aspect_ratio.den,
2750  1024 * 1024);
2751  snprintf(buf + strlen(buf), buf_size - strlen(buf),
2752  " [SAR %d:%d DAR %d:%d]",
2754  display_aspect_ratio.num, display_aspect_ratio.den);
2755  }
2756  if (av_log_get_level() >= AV_LOG_DEBUG) {
2757  int g = av_gcd(enc->time_base.num, enc->time_base.den);
2758  snprintf(buf + strlen(buf), buf_size - strlen(buf),
2759  ", %d/%d",
2760  enc->time_base.num / g, enc->time_base.den / g);
2761  }
2762  }
2763  if (encode) {
2764  snprintf(buf + strlen(buf), buf_size - strlen(buf),
2765  ", q=%d-%d", enc->qmin, enc->qmax);
2766  }
2767  break;
2768  case AVMEDIA_TYPE_AUDIO:
2769  if (enc->sample_rate) {
2770  snprintf(buf + strlen(buf), buf_size - strlen(buf),
2771  ", %d Hz", enc->sample_rate);
2772  }
2773  av_strlcat(buf, ", ", buf_size);
2774  av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
2775  if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
2776  snprintf(buf + strlen(buf), buf_size - strlen(buf),
2777  ", %s", av_get_sample_fmt_name(enc->sample_fmt));
2778  }
2779  break;
2780  case AVMEDIA_TYPE_DATA:
2781  if (av_log_get_level() >= AV_LOG_DEBUG) {
2782  int g = av_gcd(enc->time_base.num, enc->time_base.den);
2783  if (g)
2784  snprintf(buf + strlen(buf), buf_size - strlen(buf),
2785  ", %d/%d",
2786  enc->time_base.num / g, enc->time_base.den / g);
2787  }
2788  break;
2789  case AVMEDIA_TYPE_SUBTITLE:
2790  if (enc->width)
2791  snprintf(buf + strlen(buf), buf_size - strlen(buf),
2792  ", %dx%d", enc->width, enc->height);
2793  break;
2794  default:
2795  return;
2796  }
2797  if (encode) {
2798  if (enc->flags & CODEC_FLAG_PASS1)
2799  snprintf(buf + strlen(buf), buf_size - strlen(buf),
2800  ", pass 1");
2801  if (enc->flags & CODEC_FLAG_PASS2)
2802  snprintf(buf + strlen(buf), buf_size - strlen(buf),
2803  ", pass 2");
2804  }
2805  bitrate = get_bit_rate(enc);
2806  if (bitrate != 0) {
2807  snprintf(buf + strlen(buf), buf_size - strlen(buf),
2808  ", %d kb/s", bitrate / 1000);
2809  } else if (enc->rc_max_rate > 0) {
2810  snprintf(buf + strlen(buf), buf_size - strlen(buf),
2811  ", max. %d kb/s", enc->rc_max_rate / 1000);
2812  }
2813 }
2814 
2815 const char *av_get_profile_name(const AVCodec *codec, int profile)
2816 {
2817  const AVProfile *p;
2818  if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
2819  return NULL;
2820 
2821  for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
2822  if (p->profile == profile)
2823  return p->name;
2824 
2825  return NULL;
2826 }
2827 
2828 unsigned avcodec_version(void)
2829 {
2830 // av_assert0(AV_CODEC_ID_V410==164);
2833 // av_assert0(AV_CODEC_ID_BMV_AUDIO==86071);
2834  av_assert0(AV_CODEC_ID_SRT==94216);
2836 
2842  return LIBAVCODEC_VERSION_INT;
2843 }
2844 
2845 const char *avcodec_configuration(void)
2846 {
2847  return FFMPEG_CONFIGURATION;
2848 }
2849 
2850 const char *avcodec_license(void)
2851 {
2852 #define LICENSE_PREFIX "libavcodec license: "
2853  return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
2854 }
2855 
2857 {
2858  if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
2859  ff_thread_flush(avctx);
2860  else if (avctx->codec->flush)
2861  avctx->codec->flush(avctx);
2862 
2863  avctx->pts_correction_last_pts =
2864  avctx->pts_correction_last_dts = INT64_MIN;
2865 
2866  if (!avctx->refcounted_frames)
2867  av_frame_unref(&avctx->internal->to_free);
2868 }
2869 
2871 {
2872  switch (codec_id) {
2873  case AV_CODEC_ID_8SVX_EXP:
2874  case AV_CODEC_ID_8SVX_FIB:
2875  case AV_CODEC_ID_ADPCM_CT:
2882  return 4;
2883  case AV_CODEC_ID_PCM_ALAW:
2884  case AV_CODEC_ID_PCM_MULAW:
2885  case AV_CODEC_ID_PCM_S8:
2887  case AV_CODEC_ID_PCM_U8:
2888  case AV_CODEC_ID_PCM_ZORK:
2889  return 8;
2890  case AV_CODEC_ID_PCM_S16BE:
2892  case AV_CODEC_ID_PCM_S16LE:
2894  case AV_CODEC_ID_PCM_U16BE:
2895  case AV_CODEC_ID_PCM_U16LE:
2896  return 16;
2898  case AV_CODEC_ID_PCM_S24BE:
2899  case AV_CODEC_ID_PCM_S24LE:
2901  case AV_CODEC_ID_PCM_U24BE:
2902  case AV_CODEC_ID_PCM_U24LE:
2903  return 24;
2904  case AV_CODEC_ID_PCM_S32BE:
2905  case AV_CODEC_ID_PCM_S32LE:
2907  case AV_CODEC_ID_PCM_U32BE:
2908  case AV_CODEC_ID_PCM_U32LE:
2909  case AV_CODEC_ID_PCM_F32BE:
2910  case AV_CODEC_ID_PCM_F32LE:
2911  return 32;
2912  case AV_CODEC_ID_PCM_F64BE:
2913  case AV_CODEC_ID_PCM_F64LE:
2914  return 64;
2915  default:
2916  return 0;
2917  }
2918 }
2919 
2921 {
2922  static const enum AVCodecID map[AV_SAMPLE_FMT_NB][2] = {
2933  };
2934  if (fmt < 0 || fmt >= AV_SAMPLE_FMT_NB)
2935  return AV_CODEC_ID_NONE;
2936  if (be < 0 || be > 1)
2937  be = AV_NE(1, 0);
2938  return map[fmt][be];
2939 }
2940 
2942 {
2943  switch (codec_id) {
2945  return 2;
2947  return 3;
2951  case AV_CODEC_ID_ADPCM_SWF:
2952  case AV_CODEC_ID_ADPCM_MS:
2953  return 4;
2954  default:
2955  return av_get_exact_bits_per_sample(codec_id);
2956  }
2957 }
2958 
2959 int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
2960 {
2961  int id, sr, ch, ba, tag, bps;
2962 
2963  id = avctx->codec_id;
2964  sr = avctx->sample_rate;
2965  ch = avctx->channels;
2966  ba = avctx->block_align;
2967  tag = avctx->codec_tag;
2968  bps = av_get_exact_bits_per_sample(avctx->codec_id);
2969 
2970  /* codecs with an exact constant bits per sample */
2971  if (bps > 0 && ch > 0 && frame_bytes > 0 && ch < 32768 && bps < 32768)
2972  return (frame_bytes * 8LL) / (bps * ch);
2973  bps = avctx->bits_per_coded_sample;
2974 
2975  /* codecs with a fixed packet duration */
2976  switch (id) {
2977  case AV_CODEC_ID_ADPCM_ADX: return 32;
2978  case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
2979  case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
2980  case AV_CODEC_ID_AMR_NB:
2981  case AV_CODEC_ID_EVRC:
2982  case AV_CODEC_ID_GSM:
2983  case AV_CODEC_ID_QCELP:
2984  case AV_CODEC_ID_RA_288: return 160;
2985  case AV_CODEC_ID_AMR_WB:
2986  case AV_CODEC_ID_GSM_MS: return 320;
2987  case AV_CODEC_ID_MP1: return 384;
2988  case AV_CODEC_ID_ATRAC1: return 512;
2989  case AV_CODEC_ID_ATRAC3: return 1024;
2990  case AV_CODEC_ID_MP2:
2991  case AV_CODEC_ID_MUSEPACK7: return 1152;
2992  case AV_CODEC_ID_AC3: return 1536;
2993  }
2994 
2995  if (sr > 0) {
2996  /* calc from sample rate */
2997  if (id == AV_CODEC_ID_TTA)
2998  return 256 * sr / 245;
2999 
3000  if (ch > 0) {
3001  /* calc from sample rate and channels */
3002  if (id == AV_CODEC_ID_BINKAUDIO_DCT)
3003  return (480 << (sr / 22050)) / ch;
3004  }
3005  }
3006 
3007  if (ba > 0) {
3008  /* calc from block_align */
3009  if (id == AV_CODEC_ID_SIPR) {
3010  switch (ba) {
3011  case 20: return 160;
3012  case 19: return 144;
3013  case 29: return 288;
3014  case 37: return 480;
3015  }
3016  } else if (id == AV_CODEC_ID_ILBC) {
3017  switch (ba) {
3018  case 38: return 160;
3019  case 50: return 240;
3020  }
3021  }
3022  }
3023 
3024  if (frame_bytes > 0) {
3025  /* calc from frame_bytes only */
3026  if (id == AV_CODEC_ID_TRUESPEECH)
3027  return 240 * (frame_bytes / 32);
3028  if (id == AV_CODEC_ID_NELLYMOSER)
3029  return 256 * (frame_bytes / 64);
3030  if (id == AV_CODEC_ID_RA_144)
3031  return 160 * (frame_bytes / 20);
3032  if (id == AV_CODEC_ID_G723_1)
3033  return 240 * (frame_bytes / 24);
3034 
3035  if (bps > 0) {
3036  /* calc from frame_bytes and bits_per_coded_sample */
3037  if (id == AV_CODEC_ID_ADPCM_G726)
3038  return frame_bytes * 8 / bps;
3039  }
3040 
3041  if (ch > 0) {
3042  /* calc from frame_bytes and channels */
3043  switch (id) {
3044  case AV_CODEC_ID_ADPCM_AFC:
3045  return frame_bytes / (9 * ch) * 16;
3046  case AV_CODEC_ID_ADPCM_DTK:
3047  return frame_bytes / (16 * ch) * 28;
3048  case AV_CODEC_ID_ADPCM_4XM:
3050  return (frame_bytes - 4 * ch) * 2 / ch;
3052  return (frame_bytes - 4) * 2 / ch;
3054  return (frame_bytes - 8) * 2 / ch;
3055  case AV_CODEC_ID_ADPCM_XA:
3056  return (frame_bytes / 128) * 224 / ch;
3058  return (frame_bytes - 6 - ch) / ch;
3059  case AV_CODEC_ID_ROQ_DPCM:
3060  return (frame_bytes - 8) / ch;
3061  case AV_CODEC_ID_XAN_DPCM:
3062  return (frame_bytes - 2 * ch) / ch;
3063  case AV_CODEC_ID_MACE3:
3064  return 3 * frame_bytes / ch;
3065  case AV_CODEC_ID_MACE6:
3066  return 6 * frame_bytes / ch;
3067  case AV_CODEC_ID_PCM_LXF:
3068  return 2 * (frame_bytes / (5 * ch));
3069  case AV_CODEC_ID_IAC:
3070  case AV_CODEC_ID_IMC:
3071  return 4 * frame_bytes / ch;
3072  }
3073 
3074  if (tag) {
3075  /* calc from frame_bytes, channels, and codec_tag */
3076  if (id == AV_CODEC_ID_SOL_DPCM) {
3077  if (tag == 3)
3078  return frame_bytes / ch;
3079  else
3080  return frame_bytes * 2 / ch;
3081  }
3082  }
3083 
3084  if (ba > 0) {
3085  /* calc from frame_bytes, channels, and block_align */
3086  int blocks = frame_bytes / ba;
3087  switch (avctx->codec_id) {
3089  if (bps < 2 || bps > 5)
3090  return 0;
3091  return blocks * (1 + (ba - 4 * ch) / (bps * ch) * 8);
3093  return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
3095  return blocks * (1 + (ba - 4 * ch) * 2 / ch);
3097  return blocks * ((ba - 4 * ch) * 2 / ch);
3098  case AV_CODEC_ID_ADPCM_MS:
3099  return blocks * (2 + (ba - 7 * ch) * 2 / ch);
3100  }
3101  }
3102 
3103  if (bps > 0) {
3104  /* calc from frame_bytes, channels, and bits_per_coded_sample */
3105  switch (avctx->codec_id) {
3106  case AV_CODEC_ID_PCM_DVD:
3107  if(bps<4)
3108  return 0;
3109  return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
3111  if(bps<4)
3112  return 0;
3113  return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
3114  case AV_CODEC_ID_S302M:
3115  return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
3116  }
3117  }
3118  }
3119  }
3120 
3121  return 0;
3122 }
3123 
3124 #if !HAVE_THREADS
3126 {
3127  return -1;
3128 }
3129 
3130 #endif
3131 
3132 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
3133 {
3134  unsigned int n = 0;
3135 
3136  while (v >= 0xff) {
3137  *s++ = 0xff;
3138  v -= 0xff;
3139  n++;
3140  }
3141  *s = v;
3142  n++;
3143  return n;
3144 }
3145 
3146 int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
3147 {
3148  int i;
3149  for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
3150  return i;
3151 }
3152 
3153 #if FF_API_MISSING_SAMPLE
3155 void av_log_missing_feature(void *avc, const char *feature, int want_sample)
3156 {
3157  av_log(avc, AV_LOG_WARNING, "%s is not implemented. Update your FFmpeg "
3158  "version to the newest one from Git. If the problem still "
3159  "occurs, it means that your file has a feature which has not "
3160  "been implemented.\n", feature);
3161  if(want_sample)
3162  av_log_ask_for_sample(avc, NULL);
3163 }
3164 
3165 void av_log_ask_for_sample(void *avc, const char *msg, ...)
3166 {
3167  va_list argument_list;
3168 
3169  va_start(argument_list, msg);
3170 
3171  if (msg)
3172  av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
3173  av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
3174  "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
3175  "and contact the ffmpeg-devel mailing list.\n");
3176 
3177  va_end(argument_list);
3178 }
3180 #endif /* FF_API_MISSING_SAMPLE */
3181 
3182 static AVHWAccel *first_hwaccel = NULL;
3183 
3185 {
3186  AVHWAccel **p = &first_hwaccel;
3187  hwaccel->next = NULL;
3188  while(*p || avpriv_atomic_ptr_cas((void * volatile *)p, NULL, hwaccel))
3189  p = &(*p)->next;
3190 }
3191 
3193 {
3194  return hwaccel ? hwaccel->next : first_hwaccel;
3195 }
3196 
3198 {
3199  AVHWAccel *hwaccel = NULL;
3200 
3201  while ((hwaccel = av_hwaccel_next(hwaccel)))
3202  if (hwaccel->id == codec_id
3203  && hwaccel->pix_fmt == pix_fmt)
3204  return hwaccel;
3205  return NULL;
3206 }
3207 
3208 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
3209 {
3210  if (lockmgr_cb) {
3211  if (lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
3212  return -1;
3213  if (lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
3214  return -1;
3215  }
3216 
3217  lockmgr_cb = cb;
3218 
3219  if (lockmgr_cb) {
3220  if (lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
3221  return -1;
3222  if (lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
3223  return -1;
3224  }
3225  return 0;
3226 }
3227 
3229 {
3230  if (lockmgr_cb) {
3231  if ((*lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
3232  return -1;
3233  }
3234  entangled_thread_counter++;
3235  if (entangled_thread_counter != 1) {
3236  av_log(log_ctx, AV_LOG_ERROR, "Insufficient thread locking around avcodec_open/close()\n");
3237  if (!lockmgr_cb)
3238  av_log(log_ctx, AV_LOG_ERROR, "No lock manager is set, please see av_lockmgr_register()\n");
3239  ff_avcodec_locked = 1;
3241  return AVERROR(EINVAL);
3242  }
3243  av_assert0(!ff_avcodec_locked);
3244  ff_avcodec_locked = 1;
3245  return 0;
3246 }
3247 
3249 {
3250  av_assert0(ff_avcodec_locked);
3251  ff_avcodec_locked = 0;
3252  entangled_thread_counter--;
3253  if (lockmgr_cb) {
3254  if ((*lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE))
3255  return -1;
3256  }
3257  return 0;
3258 }
3259 
3261 {
3262  if (lockmgr_cb) {
3263  if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
3264  return -1;
3265  }
3266  return 0;
3267 }
3268 
3270 {
3271  if (lockmgr_cb) {
3272  if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
3273  return -1;
3274  }
3275  return 0;
3276 }
3277 
3278 unsigned int avpriv_toupper4(unsigned int x)
3279 {
3280  return av_toupper(x & 0xFF) +
3281  (av_toupper((x >> 8) & 0xFF) << 8) +
3282  (av_toupper((x >> 16) & 0xFF) << 16) +
3283  (av_toupper((x >> 24) & 0xFF) << 24);
3284 }
3285 
3287 {
3288  int ret;
3289 
3290  dst->owner = src->owner;
3291 
3292  ret = av_frame_ref(dst->f, src->f);
3293  if (ret < 0)
3294  return ret;
3295 
3296  if (src->progress &&
3297  !(dst->progress = av_buffer_ref(src->progress))) {
3298  ff_thread_release_buffer(dst->owner, dst);
3299  return AVERROR(ENOMEM);
3300  }
3301 
3302  return 0;
3303 }
3304 
3305 #if !HAVE_THREADS
3306 
3308 {
3309  return avctx->get_format(avctx, fmt);
3310 }
3311 
3313 {
3314  f->owner = avctx;
3315  return ff_get_buffer(avctx, f->f, flags);
3316 }
3317 
3319 {
3320  av_frame_unref(f->f);
3321 }
3322 
3324 {
3325 }
3326 
3327 void ff_thread_report_progress(ThreadFrame *f, int progress, int field)
3328 {
3329 }
3330 
3331 void ff_thread_await_progress(ThreadFrame *f, int progress, int field)
3332 {
3333 }
3334 
3336 {
3337  return 1;
3338 }
3339 
3341 {
3342  return 0;
3343 }
3344 
3346 {
3347 }
3348 
3349 void ff_thread_await_progress2(AVCodecContext *avctx, int field, int thread, int shift)
3350 {
3351 }
3352 
3353 void ff_thread_report_progress2(AVCodecContext *avctx, int field, int thread, int n)
3354 {
3355 }
3356 
3357 #endif
3358 
3360 {
3361  AVCodec *c= avcodec_find_decoder(codec_id);
3362  if(!c)
3363  c= avcodec_find_encoder(codec_id);
3364  if(c)
3365  return c->type;
3366 
3367  if (codec_id <= AV_CODEC_ID_NONE)
3368  return AVMEDIA_TYPE_UNKNOWN;
3369  else if (codec_id < AV_CODEC_ID_FIRST_AUDIO)
3370  return AVMEDIA_TYPE_VIDEO;
3371  else if (codec_id < AV_CODEC_ID_FIRST_SUBTITLE)
3372  return AVMEDIA_TYPE_AUDIO;
3373  else if (codec_id < AV_CODEC_ID_FIRST_UNKNOWN)
3374  return AVMEDIA_TYPE_SUBTITLE;
3375 
3376  return AVMEDIA_TYPE_UNKNOWN;
3377 }
3378 
3380 {
3381  return !!s->internal;
3382 }
3383 
3385 {
3386  int ret;
3387  char *str;
3388 
3389  ret = av_bprint_finalize(buf, &str);
3390  if (ret < 0)
3391  return ret;
3392  avctx->extradata = str;
3393  /* Note: the string is NUL terminated (so extradata can be read as a
3394  * string), but the ending character is not accounted in the size (in
3395  * binary formats you are likely not supposed to mux that character). When
3396  * extradata is copied, it is also padded with FF_INPUT_BUFFER_PADDING_SIZE
3397  * zeros. */
3398  avctx->extradata_size = buf->len;
3399  return 0;
3400 }
3401 
3402 const uint8_t *avpriv_find_start_code(const uint8_t *av_restrict p,
3403  const uint8_t *end,
3404  uint32_t *av_restrict state)
3405 {
3406  int i;
3407 
3408  av_assert0(p <= end);
3409  if (p >= end)
3410  return end;
3411 
3412  for (i = 0; i < 3; i++) {
3413  uint32_t tmp = *state << 8;
3414  *state = tmp + *(p++);
3415  if (tmp == 0x100 || p == end)
3416  return p;
3417  }
3418 
3419  while (p < end) {
3420  if (p[-1] > 1 ) p += 3;
3421  else if (p[-2] ) p += 2;
3422  else if (p[-3]|(p[-1]-1)) p++;
3423  else {
3424  p++;
3425  break;
3426  }
3427  }
3428 
3429  p = FFMIN(p, end) - 4;
3430  *state = AV_RB32(p);
3431 
3432  return p + 4;
3433 }