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