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