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