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