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 */
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: %d/%d\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[] = {
750  };
751 
752  if (pkt) {
753  frame->pkt_pts = pkt->pts;
754  av_frame_set_pkt_pos (frame, pkt->pos);
755  av_frame_set_pkt_duration(frame, pkt->duration);
756  av_frame_set_pkt_size (frame, pkt->size);
757 
758  for (i = 0; i < FF_ARRAY_ELEMS(sd); i++) {
759  int size;
760  uint8_t *packet_sd = av_packet_get_side_data(pkt, sd[i].packet, &size);
761  if (packet_sd) {
762  AVFrameSideData *frame_sd = av_frame_new_side_data(frame,
763  sd[i].frame,
764  size);
765  if (!frame_sd)
766  return AVERROR(ENOMEM);
767 
768  memcpy(frame_sd->data, packet_sd, size);
769  }
770  }
771  } else {
772  frame->pkt_pts = AV_NOPTS_VALUE;
773  av_frame_set_pkt_pos (frame, -1);
774  av_frame_set_pkt_duration(frame, 0);
775  av_frame_set_pkt_size (frame, -1);
776  }
777  frame->reordered_opaque = avctx->reordered_opaque;
778 
780  frame->color_primaries = avctx->color_primaries;
781  if (frame->color_trc == AVCOL_TRC_UNSPECIFIED)
782  frame->color_trc = avctx->color_trc;
784  av_frame_set_colorspace(frame, avctx->colorspace);
786  av_frame_set_color_range(frame, avctx->color_range);
788  frame->chroma_location = avctx->chroma_sample_location;
789 
790  switch (avctx->codec->type) {
791  case AVMEDIA_TYPE_VIDEO:
792  frame->format = avctx->pix_fmt;
793  if (!frame->sample_aspect_ratio.num)
795 
796  if (frame->width && frame->height &&
797  av_image_check_sar(frame->width, frame->height,
798  frame->sample_aspect_ratio) < 0) {
799  av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
800  frame->sample_aspect_ratio.num,
801  frame->sample_aspect_ratio.den);
802  frame->sample_aspect_ratio = (AVRational){ 0, 1 };
803  }
804 
805  break;
806  case AVMEDIA_TYPE_AUDIO:
807  if (!frame->sample_rate)
808  frame->sample_rate = avctx->sample_rate;
809  if (frame->format < 0)
810  frame->format = avctx->sample_fmt;
811  if (!frame->channel_layout) {
812  if (avctx->channel_layout) {
814  avctx->channels) {
815  av_log(avctx, AV_LOG_ERROR, "Inconsistent channel "
816  "configuration.\n");
817  return AVERROR(EINVAL);
818  }
819 
820  frame->channel_layout = avctx->channel_layout;
821  } else {
822  if (avctx->channels > FF_SANE_NB_CHANNELS) {
823  av_log(avctx, AV_LOG_ERROR, "Too many channels: %d.\n",
824  avctx->channels);
825  return AVERROR(ENOSYS);
826  }
827  }
828  }
829  av_frame_set_channels(frame, avctx->channels);
830  break;
831  }
832  return 0;
833 }
834 
835 #if FF_API_GET_BUFFER
838 {
839  return avcodec_default_get_buffer2(avctx, frame, 0);
840 }
841 
842 typedef struct CompatReleaseBufPriv {
845  uint8_t avframe_padding[1024]; // hack to allow linking to a avutil with larger AVFrame
847 
848 static void compat_free_buffer(void *opaque, uint8_t *data)
849 {
850  CompatReleaseBufPriv *priv = opaque;
851  if (priv->avctx.release_buffer)
852  priv->avctx.release_buffer(&priv->avctx, &priv->frame);
853  av_freep(&priv);
854 }
855 
856 static void compat_release_buffer(void *opaque, uint8_t *data)
857 {
858  AVBufferRef *buf = opaque;
859  av_buffer_unref(&buf);
860 }
862 #endif
863 
865 {
866  return ff_init_buffer_info(avctx, frame);
867 }
868 
870 {
871  const AVHWAccel *hwaccel = avctx->hwaccel;
872  int override_dimensions = 1;
873  int ret;
874 
875  if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
876  if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0 || avctx->pix_fmt<0) {
877  av_log(avctx, AV_LOG_ERROR, "video_get_buffer: image parameters invalid\n");
878  return AVERROR(EINVAL);
879  }
880  }
881  if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
882  if (frame->width <= 0 || frame->height <= 0) {
883  frame->width = FFMAX(avctx->width, FF_CEIL_RSHIFT(avctx->coded_width, avctx->lowres));
884  frame->height = FFMAX(avctx->height, FF_CEIL_RSHIFT(avctx->coded_height, avctx->lowres));
885  override_dimensions = 0;
886  }
887  }
888  ret = ff_decode_frame_props(avctx, frame);
889  if (ret < 0)
890  return ret;
891  if ((ret = ff_init_buffer_info(avctx, frame)) < 0)
892  return ret;
893 
894  if (hwaccel) {
895  if (hwaccel->alloc_frame) {
896  ret = hwaccel->alloc_frame(avctx, frame);
897  goto end;
898  }
899  } else
900  avctx->sw_pix_fmt = avctx->pix_fmt;
901 
902 #if FF_API_GET_BUFFER
904  /*
905  * Wrap an old get_buffer()-allocated buffer in a bunch of AVBuffers.
906  * We wrap each plane in its own AVBuffer. Each of those has a reference to
907  * a dummy AVBuffer as its private data, unreffing it on free.
908  * When all the planes are freed, the dummy buffer's free callback calls
909  * release_buffer().
910  */
911  if (avctx->get_buffer) {
912  CompatReleaseBufPriv *priv = NULL;
913  AVBufferRef *dummy_buf = NULL;
914  int planes, i, ret;
915 
916  if (flags & AV_GET_BUFFER_FLAG_REF)
917  frame->reference = 1;
918 
919  ret = avctx->get_buffer(avctx, frame);
920  if (ret < 0)
921  return ret;
922 
923  /* return if the buffers are already set up
924  * this would happen e.g. when a custom get_buffer() calls
925  * avcodec_default_get_buffer
926  */
927  if (frame->buf[0])
928  goto end0;
929 
930  priv = av_mallocz(sizeof(*priv));
931  if (!priv) {
932  ret = AVERROR(ENOMEM);
933  goto fail;
934  }
935  priv->avctx = *avctx;
936  priv->frame = *frame;
937 
938  dummy_buf = av_buffer_create(NULL, 0, compat_free_buffer, priv, 0);
939  if (!dummy_buf) {
940  ret = AVERROR(ENOMEM);
941  goto fail;
942  }
943 
944 #define WRAP_PLANE(ref_out, data, data_size) \
945 do { \
946  AVBufferRef *dummy_ref = av_buffer_ref(dummy_buf); \
947  if (!dummy_ref) { \
948  ret = AVERROR(ENOMEM); \
949  goto fail; \
950  } \
951  ref_out = av_buffer_create(data, data_size, compat_release_buffer, \
952  dummy_ref, 0); \
953  if (!ref_out) { \
954  av_frame_unref(frame); \
955  ret = AVERROR(ENOMEM); \
956  goto fail; \
957  } \
958 } while (0)
959 
960  if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
961  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
962 
963  planes = av_pix_fmt_count_planes(frame->format);
964  /* workaround for AVHWAccel plane count of 0, buf[0] is used as
965  check for allocated buffers: make libavcodec happy */
966  if (desc && desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
967  planes = 1;
968  if (!desc || planes <= 0) {
969  ret = AVERROR(EINVAL);
970  goto fail;
971  }
972 
973  for (i = 0; i < planes; i++) {
974  int v_shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
975  int plane_size = (frame->height >> v_shift) * frame->linesize[i];
976 
977  WRAP_PLANE(frame->buf[i], frame->data[i], plane_size);
978  }
979  } else {
980  int planar = av_sample_fmt_is_planar(frame->format);
981  planes = planar ? avctx->channels : 1;
982 
983  if (planes > FF_ARRAY_ELEMS(frame->buf)) {
984  frame->nb_extended_buf = planes - FF_ARRAY_ELEMS(frame->buf);
985  frame->extended_buf = av_malloc_array(sizeof(*frame->extended_buf),
986  frame->nb_extended_buf);
987  if (!frame->extended_buf) {
988  ret = AVERROR(ENOMEM);
989  goto fail;
990  }
991  }
992 
993  for (i = 0; i < FFMIN(planes, FF_ARRAY_ELEMS(frame->buf)); i++)
994  WRAP_PLANE(frame->buf[i], frame->extended_data[i], frame->linesize[0]);
995 
996  for (i = 0; i < frame->nb_extended_buf; i++)
997  WRAP_PLANE(frame->extended_buf[i],
998  frame->extended_data[i + FF_ARRAY_ELEMS(frame->buf)],
999  frame->linesize[0]);
1000  }
1001 
1002  av_buffer_unref(&dummy_buf);
1003 
1004 end0:
1005  frame->width = avctx->width;
1006  frame->height = avctx->height;
1007 
1008  return 0;
1009 
1010 fail:
1011  avctx->release_buffer(avctx, frame);
1012  av_freep(&priv);
1013  av_buffer_unref(&dummy_buf);
1014  return ret;
1015  }
1017 #endif
1018 
1019  ret = avctx->get_buffer2(avctx, frame, flags);
1020 
1021 end:
1022  if (avctx->codec_type == AVMEDIA_TYPE_VIDEO && !override_dimensions) {
1023  frame->width = avctx->width;
1024  frame->height = avctx->height;
1025  }
1026 
1027  return ret;
1028 }
1029 
1031 {
1032  int ret = get_buffer_internal(avctx, frame, flags);
1033  if (ret < 0)
1034  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1035  return ret;
1036 }
1037 
1039 {
1040  AVFrame *tmp;
1041  int ret;
1042 
1044 
1045  if (frame->data[0] && (frame->width != avctx->width || frame->height != avctx->height || frame->format != avctx->pix_fmt)) {
1046  av_log(avctx, AV_LOG_WARNING, "Picture changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s in reget buffer()\n",
1047  frame->width, frame->height, av_get_pix_fmt_name(frame->format), avctx->width, avctx->height, av_get_pix_fmt_name(avctx->pix_fmt));
1048  av_frame_unref(frame);
1049  }
1050 
1051  ff_init_buffer_info(avctx, frame);
1052 
1053  if (!frame->data[0])
1054  return ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
1055 
1056  if (av_frame_is_writable(frame))
1057  return ff_decode_frame_props(avctx, frame);
1058 
1059  tmp = av_frame_alloc();
1060  if (!tmp)
1061  return AVERROR(ENOMEM);
1062 
1063  av_frame_move_ref(tmp, frame);
1064 
1065  ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
1066  if (ret < 0) {
1067  av_frame_free(&tmp);
1068  return ret;
1069  }
1070 
1071  av_frame_copy(frame, tmp);
1072  av_frame_free(&tmp);
1073 
1074  return 0;
1075 }
1076 
1078 {
1079  int ret = reget_buffer_internal(avctx, frame);
1080  if (ret < 0)
1081  av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
1082  return ret;
1083 }
1084 
1085 #if FF_API_GET_BUFFER
1087 {
1089 
1090  av_frame_unref(pic);
1091 }
1092 
1094 {
1095  av_assert0(0);
1096  return AVERROR_BUG;
1097 }
1098 #endif
1099 
1100 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
1101 {
1102  int i;
1103 
1104  for (i = 0; i < count; i++) {
1105  int r = func(c, (char *)arg + i * size);
1106  if (ret)
1107  ret[i] = r;
1108  }
1109  return 0;
1110 }
1111 
1112 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
1113 {
1114  int i;
1115 
1116  for (i = 0; i < count; i++) {
1117  int r = func(c, arg, i, 0);
1118  if (ret)
1119  ret[i] = r;
1120  }
1121  return 0;
1122 }
1123 
1125  unsigned int fourcc)
1126 {
1127  while (tags->pix_fmt >= 0) {
1128  if (tags->fourcc == fourcc)
1129  return tags->pix_fmt;
1130  tags++;
1131  }
1132  return AV_PIX_FMT_NONE;
1133 }
1134 
1136 {
1137  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
1138  return desc->flags & AV_PIX_FMT_FLAG_HWACCEL;
1139 }
1140 
1142 {
1143  while (*fmt != AV_PIX_FMT_NONE && is_hwaccel_pix_fmt(*fmt))
1144  ++fmt;
1145  return fmt[0];
1146 }
1147 
1149  enum AVPixelFormat pix_fmt)
1150 {
1151  AVHWAccel *hwaccel = NULL;
1152 
1153  while ((hwaccel = av_hwaccel_next(hwaccel)))
1154  if (hwaccel->id == codec_id
1155  && hwaccel->pix_fmt == pix_fmt)
1156  return hwaccel;
1157  return NULL;
1158 }
1159 
1160 static int setup_hwaccel(AVCodecContext *avctx,
1161  const enum AVPixelFormat fmt,
1162  const char *name)
1163 {
1164  AVHWAccel *hwa = find_hwaccel(avctx->codec_id, fmt);
1165  int ret = 0;
1166 
1167  if (!hwa) {
1168  av_log(avctx, AV_LOG_ERROR,
1169  "Could not find an AVHWAccel for the pixel format: %s",
1170  name);
1171  return AVERROR(ENOENT);
1172  }
1173 
1174  if (hwa->priv_data_size) {
1176  if (!avctx->internal->hwaccel_priv_data)
1177  return AVERROR(ENOMEM);
1178  }
1179 
1180  if (hwa->init) {
1181  ret = hwa->init(avctx);
1182  if (ret < 0) {
1184  return ret;
1185  }
1186  }
1187 
1188  avctx->hwaccel = hwa;
1189 
1190  return 0;
1191 }
1192 
1194 {
1195  const AVPixFmtDescriptor *desc;
1196  enum AVPixelFormat *choices;
1197  enum AVPixelFormat ret;
1198  unsigned n = 0;
1199 
1200  while (fmt[n] != AV_PIX_FMT_NONE)
1201  ++n;
1202 
1203  av_assert0(n >= 1);
1204  avctx->sw_pix_fmt = fmt[n - 1];
1206 
1207  choices = av_malloc_array(n + 1, sizeof(*choices));
1208  if (!choices)
1209  return AV_PIX_FMT_NONE;
1210 
1211  memcpy(choices, fmt, (n + 1) * sizeof(*choices));
1212 
1213  for (;;) {
1214  if (avctx->hwaccel && avctx->hwaccel->uninit)
1215  avctx->hwaccel->uninit(avctx);
1217  avctx->hwaccel = NULL;
1218 
1219  ret = avctx->get_format(avctx, choices);
1220 
1221  desc = av_pix_fmt_desc_get(ret);
1222  if (!desc) {
1223  ret = AV_PIX_FMT_NONE;
1224  break;
1225  }
1226 
1227  if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL))
1228  break;
1230  break;
1231 
1232  if (!setup_hwaccel(avctx, ret, desc->name))
1233  break;
1234 
1235  /* Remove failed hwaccel from choices */
1236  for (n = 0; choices[n] != ret; n++)
1237  av_assert0(choices[n] != AV_PIX_FMT_NONE);
1238 
1239  do
1240  choices[n] = choices[n + 1];
1241  while (choices[n++] != AV_PIX_FMT_NONE);
1242  }
1243 
1244  av_freep(&choices);
1245  return ret;
1246 }
1247 
1248 #if FF_API_AVFRAME_LAVC
1249 void avcodec_get_frame_defaults(AVFrame *frame)
1250 {
1251 #if LIBAVCODEC_VERSION_MAJOR >= 55
1252  // extended_data should explicitly be freed when needed, this code is unsafe currently
1253  // also this is not compatible to the <55 ABI/API
1254  if (frame->extended_data != frame->data && 0)
1255  av_freep(&frame->extended_data);
1256 #endif
1257 
1258  memset(frame, 0, sizeof(AVFrame));
1259  av_frame_unref(frame);
1260 }
1261 
1262 AVFrame *avcodec_alloc_frame(void)
1263 {
1264  return av_frame_alloc();
1265 }
1266 
1267 void avcodec_free_frame(AVFrame **frame)
1268 {
1269  av_frame_free(frame);
1270 }
1271 #endif
1272 
1273 MAKE_ACCESSORS(AVCodecContext, codec, AVRational, pkt_timebase)
1274 MAKE_ACCESSORS(AVCodecContext, codec, const AVCodecDescriptor *, codec_descriptor)
1275 MAKE_ACCESSORS(AVCodecContext, codec, int, lowres)
1276 MAKE_ACCESSORS(AVCodecContext, codec, int, seek_preroll)
1277 MAKE_ACCESSORS(AVCodecContext, codec, uint16_t*, chroma_intra_matrix)
1278 
1280 {
1281  return codec->max_lowres;
1282 }
1283 
1285 {
1286  memset(sub, 0, sizeof(*sub));
1287  sub->pts = AV_NOPTS_VALUE;
1288 }
1289 
1291 {
1292  int bit_rate;
1293  int bits_per_sample;
1294 
1295  switch (ctx->codec_type) {
1296  case AVMEDIA_TYPE_VIDEO:
1297  case AVMEDIA_TYPE_DATA:
1298  case AVMEDIA_TYPE_SUBTITLE:
1300  bit_rate = ctx->bit_rate;
1301  break;
1302  case AVMEDIA_TYPE_AUDIO:
1303  bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
1304  bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
1305  break;
1306  default:
1307  bit_rate = 0;
1308  break;
1309  }
1310  return bit_rate;
1311 }
1312 
1313 int attribute_align_arg ff_codec_open2_recursive(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
1314 {
1315  int ret = 0;
1316 
1318 
1319  ret = avcodec_open2(avctx, codec, options);
1320 
1321  ff_lock_avcodec(avctx);
1322  return ret;
1323 }
1324 
1325 int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
1326 {
1327  int ret = 0;
1328  AVDictionary *tmp = NULL;
1329 
1330  if (avcodec_is_open(avctx))
1331  return 0;
1332 
1333  if ((!codec && !avctx->codec)) {
1334  av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2()\n");
1335  return AVERROR(EINVAL);
1336  }
1337  if ((codec && avctx->codec && codec != avctx->codec)) {
1338  av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
1339  "but %s passed to avcodec_open2()\n", avctx->codec->name, codec->name);
1340  return AVERROR(EINVAL);
1341  }
1342  if (!codec)
1343  codec = avctx->codec;
1344 
1345  if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
1346  return AVERROR(EINVAL);
1347 
1348  if (options)
1349  av_dict_copy(&tmp, *options, 0);
1350 
1351  ret = ff_lock_avcodec(avctx);
1352  if (ret < 0)
1353  return ret;
1354 
1355  avctx->internal = av_mallocz(sizeof(AVCodecInternal));
1356  if (!avctx->internal) {
1357  ret = AVERROR(ENOMEM);
1358  goto end;
1359  }
1360 
1361  avctx->internal->pool = av_mallocz(sizeof(*avctx->internal->pool));
1362  if (!avctx->internal->pool) {
1363  ret = AVERROR(ENOMEM);
1364  goto free_and_end;
1365  }
1366 
1367  avctx->internal->to_free = av_frame_alloc();
1368  if (!avctx->internal->to_free) {
1369  ret = AVERROR(ENOMEM);
1370  goto free_and_end;
1371  }
1372 
1373  if (codec->priv_data_size > 0) {
1374  if (!avctx->priv_data) {
1375  avctx->priv_data = av_mallocz(codec->priv_data_size);
1376  if (!avctx->priv_data) {
1377  ret = AVERROR(ENOMEM);
1378  goto end;
1379  }
1380  if (codec->priv_class) {
1381  *(const AVClass **)avctx->priv_data = codec->priv_class;
1383  }
1384  }
1385  if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
1386  goto free_and_end;
1387  } else {
1388  avctx->priv_data = NULL;
1389  }
1390  if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
1391  goto free_and_end;
1392 
1393  if (avctx->codec_whitelist && av_match_list(codec->name, avctx->codec_whitelist, ',') <= 0) {
1394  av_log(avctx, AV_LOG_ERROR, "Codec (%s) not on whitelist\n", codec->name);
1395  ret = AVERROR(EINVAL);
1396  goto free_and_end;
1397  }
1398 
1399  // only call ff_set_dimensions() for non H.264/VP6F codecs so as not to overwrite previously setup dimensions
1400  if (!(avctx->coded_width && avctx->coded_height && avctx->width && avctx->height &&
1401  (avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_VP6F))) {
1402  if (avctx->coded_width && avctx->coded_height)
1403  ret = ff_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
1404  else if (avctx->width && avctx->height)
1405  ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
1406  if (ret < 0)
1407  goto free_and_end;
1408  }
1409 
1410  if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
1411  && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
1412  || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
1413  av_log(avctx, AV_LOG_WARNING, "Ignoring invalid width/height values\n");
1414  ff_set_dimensions(avctx, 0, 0);
1415  }
1416 
1417  if (avctx->width > 0 && avctx->height > 0) {
1418  if (av_image_check_sar(avctx->width, avctx->height,
1419  avctx->sample_aspect_ratio) < 0) {
1420  av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
1421  avctx->sample_aspect_ratio.num,
1422  avctx->sample_aspect_ratio.den);
1423  avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
1424  }
1425  }
1426 
1427  /* if the decoder init function was already called previously,
1428  * free the already allocated subtitle_header before overwriting it */
1429  if (av_codec_is_decoder(codec))
1430  av_freep(&avctx->subtitle_header);
1431 
1432  if (avctx->channels > FF_SANE_NB_CHANNELS) {
1433  ret = AVERROR(EINVAL);
1434  goto free_and_end;
1435  }
1436 
1437  avctx->codec = codec;
1438  if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
1439  avctx->codec_id == AV_CODEC_ID_NONE) {
1440  avctx->codec_type = codec->type;
1441  avctx->codec_id = codec->id;
1442  }
1443  if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
1444  && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
1445  av_log(avctx, AV_LOG_ERROR, "Codec type or id mismatches\n");
1446  ret = AVERROR(EINVAL);
1447  goto free_and_end;
1448  }
1449  avctx->frame_number = 0;
1451 
1452  if (avctx->codec->capabilities & CODEC_CAP_EXPERIMENTAL &&
1454  const char *codec_string = av_codec_is_encoder(codec) ? "encoder" : "decoder";
1455  AVCodec *codec2;
1456  av_log(avctx, AV_LOG_ERROR,
1457  "The %s '%s' is experimental but experimental codecs are not enabled, "
1458  "add '-strict %d' if you want to use it.\n",
1459  codec_string, codec->name, FF_COMPLIANCE_EXPERIMENTAL);
1460  codec2 = av_codec_is_encoder(codec) ? avcodec_find_encoder(codec->id) : avcodec_find_decoder(codec->id);
1461  if (!(codec2->capabilities & CODEC_CAP_EXPERIMENTAL))
1462  av_log(avctx, AV_LOG_ERROR, "Alternatively use the non experimental %s '%s'.\n",
1463  codec_string, codec2->name);
1464  ret = AVERROR_EXPERIMENTAL;
1465  goto free_and_end;
1466  }
1467 
1468  if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
1469  (!avctx->time_base.num || !avctx->time_base.den)) {
1470  avctx->time_base.num = 1;
1471  avctx->time_base.den = avctx->sample_rate;
1472  }
1473 
1474  if (!HAVE_THREADS)
1475  av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
1476 
1477  if (CONFIG_FRAME_THREAD_ENCODER) {
1478  ff_unlock_avcodec(); //we will instanciate a few encoders thus kick the counter to prevent false detection of a problem
1479  ret = ff_frame_thread_encoder_init(avctx, options ? *options : NULL);
1480  ff_lock_avcodec(avctx);
1481  if (ret < 0)
1482  goto free_and_end;
1483  }
1484 
1485  if (HAVE_THREADS
1487  ret = ff_thread_init(avctx);
1488  if (ret < 0) {
1489  goto free_and_end;
1490  }
1491  }
1492  if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
1493  avctx->thread_count = 1;
1494 
1495  if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
1496  av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
1497  avctx->codec->max_lowres);
1498  ret = AVERROR(EINVAL);
1499  goto free_and_end;
1500  }
1501 
1502 #if FF_API_VISMV
1503  if (avctx->debug_mv)
1504  av_log(avctx, AV_LOG_WARNING, "The 'vismv' option is deprecated, "
1505  "see the codecview filter instead.\n");
1506 #endif
1507 
1508  if (av_codec_is_encoder(avctx->codec)) {
1509  int i;
1510  if (avctx->codec->sample_fmts) {
1511  for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
1512  if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
1513  break;
1514  if (avctx->channels == 1 &&
1517  avctx->sample_fmt = avctx->codec->sample_fmts[i];
1518  break;
1519  }
1520  }
1521  if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
1522  char buf[128];
1523  snprintf(buf, sizeof(buf), "%d", avctx->sample_fmt);
1524  av_log(avctx, AV_LOG_ERROR, "Specified sample format %s is invalid or not supported\n",
1525  (char *)av_x_if_null(av_get_sample_fmt_name(avctx->sample_fmt), buf));
1526  ret = AVERROR(EINVAL);
1527  goto free_and_end;
1528  }
1529  }
1530  if (avctx->codec->pix_fmts) {
1531  for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
1532  if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
1533  break;
1534  if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE
1535  && !((avctx->codec_id == AV_CODEC_ID_MJPEG || avctx->codec_id == AV_CODEC_ID_LJPEG)
1537  char buf[128];
1538  snprintf(buf, sizeof(buf), "%d", avctx->pix_fmt);
1539  av_log(avctx, AV_LOG_ERROR, "Specified pixel format %s is invalid or not supported\n",
1540  (char *)av_x_if_null(av_get_pix_fmt_name(avctx->pix_fmt), buf));
1541  ret = AVERROR(EINVAL);
1542  goto free_and_end;
1543  }
1544  if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ420P ||
1545  avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ411P ||
1546  avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ422P ||
1547  avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ440P ||
1548  avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ444P)
1549  avctx->color_range = AVCOL_RANGE_JPEG;
1550  }
1551  if (avctx->codec->supported_samplerates) {
1552  for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
1553  if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
1554  break;
1555  if (avctx->codec->supported_samplerates[i] == 0) {
1556  av_log(avctx, AV_LOG_ERROR, "Specified sample rate %d is not supported\n",
1557  avctx->sample_rate);
1558  ret = AVERROR(EINVAL);
1559  goto free_and_end;
1560  }
1561  }
1562  if (avctx->codec->channel_layouts) {
1563  if (!avctx->channel_layout) {
1564  av_log(avctx, AV_LOG_WARNING, "Channel layout not specified\n");
1565  } else {
1566  for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
1567  if (avctx->channel_layout == avctx->codec->channel_layouts[i])
1568  break;
1569  if (avctx->codec->channel_layouts[i] == 0) {
1570  char buf[512];
1571  av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
1572  av_log(avctx, AV_LOG_ERROR, "Specified channel layout '%s' is not supported\n", buf);
1573  ret = AVERROR(EINVAL);
1574  goto free_and_end;
1575  }
1576  }
1577  }
1578  if (avctx->channel_layout && avctx->channels) {
1579  int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
1580  if (channels != avctx->channels) {
1581  char buf[512];
1582  av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
1583  av_log(avctx, AV_LOG_ERROR,
1584  "Channel layout '%s' with %d channels does not match number of specified channels %d\n",
1585  buf, channels, avctx->channels);
1586  ret = AVERROR(EINVAL);
1587  goto free_and_end;
1588  }
1589  } else if (avctx->channel_layout) {
1591  }
1592  if(avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
1593  if (avctx->width <= 0 || avctx->height <= 0) {
1594  av_log(avctx, AV_LOG_ERROR, "dimensions not set\n");
1595  ret = AVERROR(EINVAL);
1596  goto free_and_end;
1597  }
1598  }
1599  if ( (avctx->codec_type == AVMEDIA_TYPE_VIDEO || avctx->codec_type == AVMEDIA_TYPE_AUDIO)
1600  && avctx->bit_rate>0 && avctx->bit_rate<1000) {
1601  av_log(avctx, AV_LOG_WARNING, "Bitrate %d is extremely low, maybe you mean %dk\n", avctx->bit_rate, avctx->bit_rate);
1602  }
1603 
1604  if (!avctx->rc_initial_buffer_occupancy)
1605  avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3 / 4;
1606  }
1607 
1609  avctx->pts_correction_num_faulty_dts = 0;
1610  avctx->pts_correction_last_pts =
1611  avctx->pts_correction_last_dts = INT64_MIN;
1612 
1613  if ( avctx->codec->init && (!(avctx->active_thread_type&FF_THREAD_FRAME)
1614  || avctx->internal->frame_thread_encoder)) {
1615  ret = avctx->codec->init(avctx);
1616  if (ret < 0) {
1617  goto free_and_end;
1618  }
1619  }
1620 
1621  ret=0;
1622 
1623 #if FF_API_AUDIOENC_DELAY
1624  if (av_codec_is_encoder(avctx->codec))
1625  avctx->delay = avctx->initial_padding;
1626 #endif
1627 
1628  if (av_codec_is_decoder(avctx->codec)) {
1629  if (!avctx->bit_rate)
1630  avctx->bit_rate = get_bit_rate(avctx);
1631  /* validate channel layout from the decoder */
1632  if (avctx->channel_layout) {
1633  int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
1634  if (!avctx->channels)
1635  avctx->channels = channels;
1636  else if (channels != avctx->channels) {
1637  char buf[512];
1638  av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
1639  av_log(avctx, AV_LOG_WARNING,
1640  "Channel layout '%s' with %d channels does not match specified number of channels %d: "
1641  "ignoring specified channel layout\n",
1642  buf, channels, avctx->channels);
1643  avctx->channel_layout = 0;
1644  }
1645  }
1646  if (avctx->channels && avctx->channels < 0 ||
1647  avctx->channels > FF_SANE_NB_CHANNELS) {
1648  ret = AVERROR(EINVAL);
1649  goto free_and_end;
1650  }
1651  if (avctx->sub_charenc) {
1652  if (avctx->codec_type != AVMEDIA_TYPE_SUBTITLE) {
1653  av_log(avctx, AV_LOG_ERROR, "Character encoding is only "
1654  "supported with subtitles codecs\n");
1655  ret = AVERROR(EINVAL);
1656  goto free_and_end;
1657  } else if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB) {
1658  av_log(avctx, AV_LOG_WARNING, "Codec '%s' is bitmap-based, "
1659  "subtitles character encoding will be ignored\n",
1660  avctx->codec_descriptor->name);
1662  } else {
1663  /* input character encoding is set for a text based subtitle
1664  * codec at this point */
1667 
1669 #if CONFIG_ICONV
1670  iconv_t cd = iconv_open("UTF-8", avctx->sub_charenc);
1671  if (cd == (iconv_t)-1) {
1672  ret = AVERROR(errno);
1673  av_log(avctx, AV_LOG_ERROR, "Unable to open iconv context "
1674  "with input character encoding \"%s\"\n", avctx->sub_charenc);
1675  goto free_and_end;
1676  }
1677  iconv_close(cd);
1678 #else
1679  av_log(avctx, AV_LOG_ERROR, "Character encoding subtitles "
1680  "conversion needs a libavcodec built with iconv support "
1681  "for this codec\n");
1682  ret = AVERROR(ENOSYS);
1683  goto free_and_end;
1684 #endif
1685  }
1686  }
1687  }
1688 
1689 #if FF_API_AVCTX_TIMEBASE
1690  if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
1691  avctx->time_base = av_inv_q(av_mul_q(avctx->framerate, (AVRational){avctx->ticks_per_frame, 1}));
1692 #endif
1693  }
1694  if (codec->priv_data_size > 0 && avctx->priv_data && codec->priv_class) {
1695  av_assert0(*(const AVClass **)avctx->priv_data == codec->priv_class);
1696  }
1697 
1698 end:
1700  if (options) {
1702  *options = tmp;
1703  }
1704 
1705  return ret;
1706 free_and_end:
1707  av_dict_free(&tmp);
1708  if (codec->priv_class && codec->priv_data_size)
1709  av_opt_free(avctx->priv_data);
1710  av_freep(&avctx->priv_data);
1711  if (avctx->internal) {
1712  av_frame_free(&avctx->internal->to_free);
1713  av_freep(&avctx->internal->pool);
1714  }
1715  av_freep(&avctx->internal);
1716  avctx->codec = NULL;
1717  goto end;
1718 }
1719 
1720 int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
1721 {
1722  if (avpkt->size < 0) {
1723  av_log(avctx, AV_LOG_ERROR, "Invalid negative user packet size %d\n", avpkt->size);
1724  return AVERROR(EINVAL);
1725  }
1727  av_log(avctx, AV_LOG_ERROR, "Invalid minimum required packet size %"PRId64" (max allowed is %d)\n",
1728  size, INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE);
1729  return AVERROR(EINVAL);
1730  }
1731 
1732  if (avctx) {
1733  av_assert0(!avpkt->data || avpkt->data != avctx->internal->byte_buffer);
1734  if (!avpkt->data || avpkt->size < size) {
1736  avpkt->data = avctx->internal->byte_buffer;
1737  avpkt->size = avctx->internal->byte_buffer_size;
1738 #if FF_API_DESTRUCT_PACKET
1740  avpkt->destruct = NULL;
1742 #endif
1743  }
1744  }
1745 
1746  if (avpkt->data) {
1747  AVBufferRef *buf = avpkt->buf;
1748 #if FF_API_DESTRUCT_PACKET
1750  void *destruct = avpkt->destruct;
1752 #endif
1753 
1754  if (avpkt->size < size) {
1755  av_log(avctx, AV_LOG_ERROR, "User packet is too small (%d < %"PRId64")\n", avpkt->size, size);
1756  return AVERROR(EINVAL);
1757  }
1758 
1759  av_init_packet(avpkt);
1760 #if FF_API_DESTRUCT_PACKET
1762  avpkt->destruct = destruct;
1764 #endif
1765  avpkt->buf = buf;
1766  avpkt->size = size;
1767  return 0;
1768  } else {
1769  int ret = av_new_packet(avpkt, size);
1770  if (ret < 0)
1771  av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %"PRId64"\n", size);
1772  return ret;
1773  }
1774 }
1775 
1777 {
1778  return ff_alloc_packet2(NULL, avpkt, size);
1779 }
1780 
1781 /**
1782  * Pad last frame with silence.
1783  */
1784 static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
1785 {
1786  AVFrame *frame = NULL;
1787  int ret;
1788 
1789  if (!(frame = av_frame_alloc()))
1790  return AVERROR(ENOMEM);
1791 
1792  frame->format = src->format;
1793  frame->channel_layout = src->channel_layout;
1795  frame->nb_samples = s->frame_size;
1796  ret = av_frame_get_buffer(frame, 32);
1797  if (ret < 0)
1798  goto fail;
1799 
1800  ret = av_frame_copy_props(frame, src);
1801  if (ret < 0)
1802  goto fail;
1803 
1804  if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
1805  src->nb_samples, s->channels, s->sample_fmt)) < 0)
1806  goto fail;
1807  if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
1808  frame->nb_samples - src->nb_samples,
1809  s->channels, s->sample_fmt)) < 0)
1810  goto fail;
1811 
1812  *dst = frame;
1813 
1814  return 0;
1815 
1816 fail:
1817  av_frame_free(&frame);
1818  return ret;
1819 }
1820 
1821 int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
1822  AVPacket *avpkt,
1823  const AVFrame *frame,
1824  int *got_packet_ptr)
1825 {
1826  AVFrame *extended_frame = NULL;
1827  AVFrame *padded_frame = NULL;
1828  int ret;
1829  AVPacket user_pkt = *avpkt;
1830  int needs_realloc = !user_pkt.data;
1831 
1832  *got_packet_ptr = 0;
1833 
1834  if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
1835  av_free_packet(avpkt);
1836  av_init_packet(avpkt);
1837  return 0;
1838  }
1839 
1840  /* ensure that extended_data is properly set */
1841  if (frame && !frame->extended_data) {
1842  if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
1843  avctx->channels > AV_NUM_DATA_POINTERS) {
1844  av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
1845  "with more than %d channels, but extended_data is not set.\n",
1847  return AVERROR(EINVAL);
1848  }
1849  av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
1850 
1851  extended_frame = av_frame_alloc();
1852  if (!extended_frame)
1853  return AVERROR(ENOMEM);
1854 
1855  memcpy(extended_frame, frame, sizeof(AVFrame));
1856  extended_frame->extended_data = extended_frame->data;
1857  frame = extended_frame;
1858  }
1859 
1860  /* extract audio service type metadata */
1861  if (frame) {
1863  if (sd && sd->size >= sizeof(enum AVAudioServiceType))
1864  avctx->audio_service_type = *(enum AVAudioServiceType*)sd->data;
1865  }
1866 
1867  /* check for valid frame size */
1868  if (frame) {
1870  if (frame->nb_samples > avctx->frame_size) {
1871  av_log(avctx, AV_LOG_ERROR, "more samples than frame size (avcodec_encode_audio2)\n");
1872  ret = AVERROR(EINVAL);
1873  goto end;
1874  }
1875  } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
1876  if (frame->nb_samples < avctx->frame_size &&
1877  !avctx->internal->last_audio_frame) {
1878  ret = pad_last_frame(avctx, &padded_frame, frame);
1879  if (ret < 0)
1880  goto end;
1881 
1882  frame = padded_frame;
1883  avctx->internal->last_audio_frame = 1;
1884  }
1885 
1886  if (frame->nb_samples != avctx->frame_size) {
1887  av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) != frame_size (%d) (avcodec_encode_audio2)\n", frame->nb_samples, avctx->frame_size);
1888  ret = AVERROR(EINVAL);
1889  goto end;
1890  }
1891  }
1892  }
1893 
1894  ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1895  if (!ret) {
1896  if (*got_packet_ptr) {
1897  if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
1898  if (avpkt->pts == AV_NOPTS_VALUE)
1899  avpkt->pts = frame->pts;
1900  if (!avpkt->duration)
1901  avpkt->duration = ff_samples_to_time_base(avctx,
1902  frame->nb_samples);
1903  }
1904  avpkt->dts = avpkt->pts;
1905  } else {
1906  avpkt->size = 0;
1907  }
1908  }
1909  if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
1910  needs_realloc = 0;
1911  if (user_pkt.data) {
1912  if (user_pkt.size >= avpkt->size) {
1913  memcpy(user_pkt.data, avpkt->data, avpkt->size);
1914  } else {
1915  av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
1916  avpkt->size = user_pkt.size;
1917  ret = -1;
1918  }
1919  avpkt->buf = user_pkt.buf;
1920  avpkt->data = user_pkt.data;
1921 #if FF_API_DESTRUCT_PACKET
1923  avpkt->destruct = user_pkt.destruct;
1925 #endif
1926  } else {
1927  if (av_dup_packet(avpkt) < 0) {
1928  ret = AVERROR(ENOMEM);
1929  }
1930  }
1931  }
1932 
1933  if (!ret) {
1934  if (needs_realloc && avpkt->data) {
1935  ret = av_buffer_realloc(&avpkt->buf, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
1936  if (ret >= 0)
1937  avpkt->data = avpkt->buf->data;
1938  }
1939 
1940  avctx->frame_number++;
1941  }
1942 
1943  if (ret < 0 || !*got_packet_ptr) {
1944  av_free_packet(avpkt);
1945  av_init_packet(avpkt);
1946  goto end;
1947  }
1948 
1949  /* NOTE: if we add any audio encoders which output non-keyframe packets,
1950  * this needs to be moved to the encoders, but for now we can do it
1951  * here to simplify things */
1952  avpkt->flags |= AV_PKT_FLAG_KEY;
1953 
1954 end:
1955  av_frame_free(&padded_frame);
1956  av_free(extended_frame);
1957 
1958 #if FF_API_AUDIOENC_DELAY
1959  avctx->delay = avctx->initial_padding;
1960 #endif
1961 
1962  return ret;
1963 }
1964 
1965 #if FF_API_OLD_ENCODE_AUDIO
1966 int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
1967  uint8_t *buf, int buf_size,
1968  const short *samples)
1969 {
1970  AVPacket pkt;
1971  AVFrame *frame;
1972  int ret, samples_size, got_packet;
1973 
1974  av_init_packet(&pkt);
1975  pkt.data = buf;
1976  pkt.size = buf_size;
1977 
1978  if (samples) {
1979  frame = av_frame_alloc();
1980  if (!frame)
1981  return AVERROR(ENOMEM);
1982 
1983  if (avctx->frame_size) {
1984  frame->nb_samples = avctx->frame_size;
1985  } else {
1986  /* if frame_size is not set, the number of samples must be
1987  * calculated from the buffer size */
1988  int64_t nb_samples;
1989  if (!av_get_bits_per_sample(avctx->codec_id)) {
1990  av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
1991  "support this codec\n");
1992  av_frame_free(&frame);
1993  return AVERROR(EINVAL);
1994  }
1995  nb_samples = (int64_t)buf_size * 8 /
1996  (av_get_bits_per_sample(avctx->codec_id) *
1997  avctx->channels);
1998  if (nb_samples >= INT_MAX) {
1999  av_frame_free(&frame);
2000  return AVERROR(EINVAL);
2001  }
2002  frame->nb_samples = nb_samples;
2003  }
2004 
2005  /* it is assumed that the samples buffer is large enough based on the
2006  * relevant parameters */
2007  samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
2008  frame->nb_samples,
2009  avctx->sample_fmt, 1);
2010  if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
2011  avctx->sample_fmt,
2012  (const uint8_t *)samples,
2013  samples_size, 1)) < 0) {
2014  av_frame_free(&frame);
2015  return ret;
2016  }
2017 
2018  /* fabricate frame pts from sample count.
2019  * this is needed because the avcodec_encode_audio() API does not have
2020  * a way for the user to provide pts */
2021  if (avctx->sample_rate && avctx->time_base.num)
2022  frame->pts = ff_samples_to_time_base(avctx,
2023  avctx->internal->sample_count);
2024  else
2025  frame->pts = AV_NOPTS_VALUE;
2026  avctx->internal->sample_count += frame->nb_samples;
2027  } else {
2028  frame = NULL;
2029  }
2030 
2031  got_packet = 0;
2032  ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
2033  if (!ret && got_packet && avctx->coded_frame) {
2034  avctx->coded_frame->pts = pkt.pts;
2035  avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
2036  }
2037  /* free any side data since we cannot return it */
2039 
2040  if (frame && frame->extended_data != frame->data)
2041  av_freep(&frame->extended_data);
2042 
2043  av_frame_free(&frame);
2044  return ret ? ret : pkt.size;
2045 }
2046 
2047 #endif
2048 
2049 #if FF_API_OLD_ENCODE_VIDEO
2050 int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
2051  const AVFrame *pict)
2052 {
2053  AVPacket pkt;
2054  int ret, got_packet = 0;
2055 
2056  if (buf_size < FF_MIN_BUFFER_SIZE) {
2057  av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
2058  return -1;
2059  }
2060 
2061  av_init_packet(&pkt);
2062  pkt.data = buf;
2063  pkt.size = buf_size;
2064 
2065  ret = avcodec_encode_video2(avctx, &pkt, pict, &got_packet);
2066  if (!ret && got_packet && avctx->coded_frame) {
2067  avctx->coded_frame->pts = pkt.pts;
2068  avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
2069  }
2070 
2071  /* free any side data since we cannot return it */
2072  if (pkt.side_data_elems > 0) {
2073  int i;
2074  for (i = 0; i < pkt.side_data_elems; i++)
2075  av_free(pkt.side_data[i].data);
2076  av_freep(&pkt.side_data);
2077  pkt.side_data_elems = 0;
2078  }
2079 
2080  return ret ? ret : pkt.size;
2081 }
2082 
2083 #endif
2084 
2085 int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
2086  AVPacket *avpkt,
2087  const AVFrame *frame,
2088  int *got_packet_ptr)
2089 {
2090  int ret;
2091  AVPacket user_pkt = *avpkt;
2092  int needs_realloc = !user_pkt.data;
2093 
2094  *got_packet_ptr = 0;
2095 
2096  if(CONFIG_FRAME_THREAD_ENCODER &&
2098  return ff_thread_video_encode_frame(avctx, avpkt, frame, got_packet_ptr);
2099 
2100  if ((avctx->flags&CODEC_FLAG_PASS1) && avctx->stats_out)
2101  avctx->stats_out[0] = '\0';
2102 
2103  if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
2104  av_free_packet(avpkt);
2105  av_init_packet(avpkt);
2106  avpkt->size = 0;
2107  return 0;
2108  }
2109 
2110  if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
2111  return AVERROR(EINVAL);
2112 
2113  if (frame && frame->format == AV_PIX_FMT_NONE)
2114  av_log(avctx, AV_LOG_WARNING, "AVFrame.format is not set\n");
2115  if (frame && (frame->width == 0 || frame->height == 0))
2116  av_log(avctx, AV_LOG_WARNING, "AVFrame.width or height is not set\n");
2117 
2118  av_assert0(avctx->codec->encode2);
2119 
2120  ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
2121  av_assert0(ret <= 0);
2122 
2123  if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
2124  needs_realloc = 0;
2125  if (user_pkt.data) {
2126  if (user_pkt.size >= avpkt->size) {
2127  memcpy(user_pkt.data, avpkt->data, avpkt->size);
2128  } else {
2129  av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
2130  avpkt->size = user_pkt.size;
2131  ret = -1;
2132  }
2133  avpkt->buf = user_pkt.buf;
2134  avpkt->data = user_pkt.data;
2135 #if FF_API_DESTRUCT_PACKET
2137  avpkt->destruct = user_pkt.destruct;
2139 #endif
2140  } else {
2141  if (av_dup_packet(avpkt) < 0) {
2142  ret = AVERROR(ENOMEM);
2143  }
2144  }
2145  }
2146 
2147  if (!ret) {
2148  if (!*got_packet_ptr)
2149  avpkt->size = 0;
2150  else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
2151  avpkt->pts = avpkt->dts = frame->pts;
2152 
2153  if (needs_realloc && avpkt->data) {
2154  ret = av_buffer_realloc(&avpkt->buf, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
2155  if (ret >= 0)
2156  avpkt->data = avpkt->buf->data;
2157  }
2158 
2159  avctx->frame_number++;
2160  }
2161 
2162  if (ret < 0 || !*got_packet_ptr)
2163  av_free_packet(avpkt);
2164  else
2166 
2167  emms_c();
2168  return ret;
2169 }
2170 
2172  const AVSubtitle *sub)
2173 {
2174  int ret;
2175  if (sub->start_display_time) {
2176  av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
2177  return -1;
2178  }
2179 
2180  ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
2181  avctx->frame_number++;
2182  return ret;
2183 }
2184 
2185 /**
2186  * Attempt to guess proper monotonic timestamps for decoded video frames
2187  * which might have incorrect times. Input timestamps may wrap around, in
2188  * which case the output will as well.
2189  *
2190  * @param pts the pts field of the decoded AVPacket, as passed through
2191  * AVFrame.pkt_pts
2192  * @param dts the dts field of the decoded AVPacket
2193  * @return one of the input values, may be AV_NOPTS_VALUE
2194  */
2195 static int64_t guess_correct_pts(AVCodecContext *ctx,
2196  int64_t reordered_pts, int64_t dts)
2197 {
2198  int64_t pts = AV_NOPTS_VALUE;
2199 
2200  if (dts != AV_NOPTS_VALUE) {
2202  ctx->pts_correction_last_dts = dts;
2203  } else if (reordered_pts != AV_NOPTS_VALUE)
2204  ctx->pts_correction_last_dts = reordered_pts;
2205 
2206  if (reordered_pts != AV_NOPTS_VALUE) {
2207  ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
2208  ctx->pts_correction_last_pts = reordered_pts;
2209  } else if(dts != AV_NOPTS_VALUE)
2210  ctx->pts_correction_last_pts = dts;
2211 
2213  && reordered_pts != AV_NOPTS_VALUE)
2214  pts = reordered_pts;
2215  else
2216  pts = dts;
2217 
2218  return pts;
2219 }
2220 
2221 static int apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
2222 {
2223  int size = 0, ret;
2224  const uint8_t *data;
2225  uint32_t flags;
2226 
2227  data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
2228  if (!data)
2229  return 0;
2230 
2231  if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE)) {
2232  av_log(avctx, AV_LOG_ERROR, "This decoder does not support parameter "
2233  "changes, but PARAM_CHANGE side data was sent to it.\n");
2234  return AVERROR(EINVAL);
2235  }
2236 
2237  if (size < 4)
2238  goto fail;
2239 
2240  flags = bytestream_get_le32(&data);
2241  size -= 4;
2242 
2244  if (size < 4)
2245  goto fail;
2246  avctx->channels = bytestream_get_le32(&data);
2247  size -= 4;
2248  }
2250  if (size < 8)
2251  goto fail;
2252  avctx->channel_layout = bytestream_get_le64(&data);
2253  size -= 8;
2254  }
2256  if (size < 4)
2257  goto fail;
2258  avctx->sample_rate = bytestream_get_le32(&data);
2259  size -= 4;
2260  }
2262  if (size < 8)
2263  goto fail;
2264  avctx->width = bytestream_get_le32(&data);
2265  avctx->height = bytestream_get_le32(&data);
2266  size -= 8;
2267  ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
2268  if (ret < 0)
2269  return ret;
2270  }
2271 
2272  return 0;
2273 fail:
2274  av_log(avctx, AV_LOG_ERROR, "PARAM_CHANGE side data too small.\n");
2275  return AVERROR_INVALIDDATA;
2276 }
2277 
2279 {
2280  int size;
2281  const uint8_t *side_metadata;
2282 
2283  AVDictionary **frame_md = avpriv_frame_get_metadatap(frame);
2284 
2285  side_metadata = av_packet_get_side_data(avctx->internal->pkt,
2287  return av_packet_unpack_dictionary(side_metadata, size, frame_md);
2288 }
2289 
2290 static int unrefcount_frame(AVCodecInternal *avci, AVFrame *frame)
2291 {
2292  int ret;
2293 
2294  /* move the original frame to our backup */
2295  av_frame_unref(avci->to_free);
2296  av_frame_move_ref(avci->to_free, frame);
2297 
2298  /* now copy everything except the AVBufferRefs back
2299  * note that we make a COPY of the side data, so calling av_frame_free() on
2300  * the caller's frame will work properly */
2301  ret = av_frame_copy_props(frame, avci->to_free);
2302  if (ret < 0)
2303  return ret;
2304 
2305  memcpy(frame->data, avci->to_free->data, sizeof(frame->data));
2306  memcpy(frame->linesize, avci->to_free->linesize, sizeof(frame->linesize));
2307  if (avci->to_free->extended_data != avci->to_free->data) {
2308  int planes = av_frame_get_channels(avci->to_free);
2309  int size = planes * sizeof(*frame->extended_data);
2310 
2311  if (!size) {
2312  av_frame_unref(frame);
2313  return AVERROR_BUG;
2314  }
2315 
2316  frame->extended_data = av_malloc(size);
2317  if (!frame->extended_data) {
2318  av_frame_unref(frame);
2319  return AVERROR(ENOMEM);
2320  }
2321  memcpy(frame->extended_data, avci->to_free->extended_data,
2322  size);
2323  } else
2324  frame->extended_data = frame->data;
2325 
2326  frame->format = avci->to_free->format;
2327  frame->width = avci->to_free->width;
2328  frame->height = avci->to_free->height;
2329  frame->channel_layout = avci->to_free->channel_layout;
2330  frame->nb_samples = avci->to_free->nb_samples;
2332 
2333  return 0;
2334 }
2335 
2336 int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
2337  int *got_picture_ptr,
2338  const AVPacket *avpkt)
2339 {
2340  AVCodecInternal *avci = avctx->internal;
2341  int ret;
2342  // copy to ensure we do not change avpkt
2343  AVPacket tmp = *avpkt;
2344 
2345  if (!avctx->codec)
2346  return AVERROR(EINVAL);
2347  if (avctx->codec->type != AVMEDIA_TYPE_VIDEO) {
2348  av_log(avctx, AV_LOG_ERROR, "Invalid media type for video\n");
2349  return AVERROR(EINVAL);
2350  }
2351 
2352  *got_picture_ptr = 0;
2353  if ((avctx->coded_width || avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
2354  return AVERROR(EINVAL);
2355 
2356  av_frame_unref(picture);
2357 
2358  if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
2359  int did_split = av_packet_split_side_data(&tmp);
2360  ret = apply_param_change(avctx, &tmp);
2361  if (ret < 0) {
2362  av_log(avctx, AV_LOG_ERROR, "Error applying parameter changes.\n");
2363  if (avctx->err_recognition & AV_EF_EXPLODE)
2364  goto fail;
2365  }
2366 
2367  avctx->internal->pkt = &tmp;
2368  if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
2369  ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
2370  &tmp);
2371  else {
2372  ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
2373  &tmp);
2374  picture->pkt_dts = avpkt->dts;
2375 
2376  if(!avctx->has_b_frames){
2377  av_frame_set_pkt_pos(picture, avpkt->pos);
2378  }
2379  //FIXME these should be under if(!avctx->has_b_frames)
2380  /* get_buffer is supposed to set frame parameters */
2381  if (!(avctx->codec->capabilities & CODEC_CAP_DR1)) {
2382  if (!picture->sample_aspect_ratio.num) picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
2383  if (!picture->width) picture->width = avctx->width;
2384  if (!picture->height) picture->height = avctx->height;
2385  if (picture->format == AV_PIX_FMT_NONE) picture->format = avctx->pix_fmt;
2386  }
2387  }
2388  add_metadata_from_side_data(avctx, picture);
2389 
2390 fail:
2391  emms_c(); //needed to avoid an emms_c() call before every return;
2392 
2393  avctx->internal->pkt = NULL;
2394  if (did_split) {
2396  if(ret == tmp.size)
2397  ret = avpkt->size;
2398  }
2399 
2400  if (*got_picture_ptr) {
2401  if (!avctx->refcounted_frames) {
2402  int err = unrefcount_frame(avci, picture);
2403  if (err < 0)
2404  return err;
2405  }
2406 
2407  avctx->frame_number++;
2409  guess_correct_pts(avctx,
2410  picture->pkt_pts,
2411  picture->pkt_dts));
2412  } else
2413  av_frame_unref(picture);
2414  } else
2415  ret = 0;
2416 
2417  /* many decoders assign whole AVFrames, thus overwriting extended_data;
2418  * make sure it's set correctly */
2419  av_assert0(!picture->extended_data || picture->extended_data == picture->data);
2420 
2421 #if FF_API_AVCTX_TIMEBASE
2422  if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
2423  avctx->time_base = av_inv_q(av_mul_q(avctx->framerate, (AVRational){avctx->ticks_per_frame, 1}));
2424 #endif
2425 
2426  return ret;
2427 }
2428 
2429 #if FF_API_OLD_DECODE_AUDIO
2430 int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
2431  int *frame_size_ptr,
2432  AVPacket *avpkt)
2433 {
2434  AVFrame *frame = av_frame_alloc();
2435  int ret, got_frame = 0;
2436 
2437  if (!frame)
2438  return AVERROR(ENOMEM);
2439  if (avctx->get_buffer != avcodec_default_get_buffer) {
2440  av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
2441  "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
2442  av_log(avctx, AV_LOG_ERROR, "Please port your application to "
2443  "avcodec_decode_audio4()\n");
2446  }
2447 
2448  ret = avcodec_decode_audio4(avctx, frame, &got_frame, avpkt);
2449 
2450  if (ret >= 0 && got_frame) {
2451  int ch, plane_size;
2452  int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
2453  int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
2454  frame->nb_samples,
2455  avctx->sample_fmt, 1);
2456  if (*frame_size_ptr < data_size) {
2457  av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
2458  "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
2459  av_frame_free(&frame);
2460  return AVERROR(EINVAL);
2461  }
2462 
2463  memcpy(samples, frame->extended_data[0], plane_size);
2464 
2465  if (planar && avctx->channels > 1) {
2466  uint8_t *out = ((uint8_t *)samples) + plane_size;
2467  for (ch = 1; ch < avctx->channels; ch++) {
2468  memcpy(out, frame->extended_data[ch], plane_size);
2469  out += plane_size;
2470  }
2471  }
2472  *frame_size_ptr = data_size;
2473  } else {
2474  *frame_size_ptr = 0;
2475  }
2476  av_frame_free(&frame);
2477  return ret;
2478 }
2479 
2480 #endif
2481 
2482 int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
2483  AVFrame *frame,
2484  int *got_frame_ptr,
2485  const AVPacket *avpkt)
2486 {
2487  AVCodecInternal *avci = avctx->internal;
2488  int ret = 0;
2489 
2490  *got_frame_ptr = 0;
2491 
2492  if (!avpkt->data && avpkt->size) {
2493  av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
2494  return AVERROR(EINVAL);
2495  }
2496  if (!avctx->codec)
2497  return AVERROR(EINVAL);
2498  if (avctx->codec->type != AVMEDIA_TYPE_AUDIO) {
2499  av_log(avctx, AV_LOG_ERROR, "Invalid media type for audio\n");
2500  return AVERROR(EINVAL);
2501  }
2502 
2503  av_frame_unref(frame);
2504 
2505  if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
2506  uint8_t *side;
2507  int side_size;
2508  uint32_t discard_padding = 0;
2509  uint8_t skip_reason = 0;
2510  uint8_t discard_reason = 0;
2511  // copy to ensure we do not change avpkt
2512  AVPacket tmp = *avpkt;
2513  int did_split = av_packet_split_side_data(&tmp);
2514  ret = apply_param_change(avctx, &tmp);
2515  if (ret < 0) {
2516  av_log(avctx, AV_LOG_ERROR, "Error applying parameter changes.\n");
2517  if (avctx->err_recognition & AV_EF_EXPLODE)
2518  goto fail;
2519  }
2520 
2521  avctx->internal->pkt = &tmp;
2522  if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
2523  ret = ff_thread_decode_frame(avctx, frame, got_frame_ptr, &tmp);
2524  else {
2525  ret = avctx->codec->decode(avctx, frame, got_frame_ptr, &tmp);
2526  frame->pkt_dts = avpkt->dts;
2527  }
2528  if (ret >= 0 && *got_frame_ptr) {
2529  add_metadata_from_side_data(avctx, frame);
2530  avctx->frame_number++;
2532  guess_correct_pts(avctx,
2533  frame->pkt_pts,
2534  frame->pkt_dts));
2535  if (frame->format == AV_SAMPLE_FMT_NONE)
2536  frame->format = avctx->sample_fmt;
2537  if (!frame->channel_layout)
2538  frame->channel_layout = avctx->channel_layout;
2539  if (!av_frame_get_channels(frame))
2540  av_frame_set_channels(frame, avctx->channels);
2541  if (!frame->sample_rate)
2542  frame->sample_rate = avctx->sample_rate;
2543  }
2544 
2545  side= av_packet_get_side_data(avctx->internal->pkt, AV_PKT_DATA_SKIP_SAMPLES, &side_size);
2546  if(side && side_size>=10) {
2547  avctx->internal->skip_samples = AV_RL32(side);
2548  av_log(avctx, AV_LOG_DEBUG, "skip %d samples due to side data\n",
2549  avctx->internal->skip_samples);
2550  discard_padding = AV_RL32(side + 4);
2551  skip_reason = AV_RL8(side + 8);
2552  discard_reason = AV_RL8(side + 9);
2553  }
2554  if (avctx->internal->skip_samples && *got_frame_ptr &&
2555  !(avctx->flags2 & CODEC_FLAG2_SKIP_MANUAL)) {
2556  if(frame->nb_samples <= avctx->internal->skip_samples){
2557  *got_frame_ptr = 0;
2558  avctx->internal->skip_samples -= frame->nb_samples;
2559  av_log(avctx, AV_LOG_DEBUG, "skip whole frame, skip left: %d\n",
2560  avctx->internal->skip_samples);
2561  } else {
2563  frame->nb_samples - avctx->internal->skip_samples, avctx->channels, frame->format);
2564  if(avctx->pkt_timebase.num && avctx->sample_rate) {
2565  int64_t diff_ts = av_rescale_q(avctx->internal->skip_samples,
2566  (AVRational){1, avctx->sample_rate},
2567  avctx->pkt_timebase);
2568  if(frame->pkt_pts!=AV_NOPTS_VALUE)
2569  frame->pkt_pts += diff_ts;
2570  if(frame->pkt_dts!=AV_NOPTS_VALUE)
2571  frame->pkt_dts += diff_ts;
2572  if (av_frame_get_pkt_duration(frame) >= diff_ts)
2573  av_frame_set_pkt_duration(frame, av_frame_get_pkt_duration(frame) - diff_ts);
2574  } else {
2575  av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for skipped samples.\n");
2576  }
2577  av_log(avctx, AV_LOG_DEBUG, "skip %d/%d samples\n",
2578  avctx->internal->skip_samples, frame->nb_samples);
2579  frame->nb_samples -= avctx->internal->skip_samples;
2580  avctx->internal->skip_samples = 0;
2581  }
2582  }
2583 
2584  if (discard_padding > 0 && discard_padding <= frame->nb_samples && *got_frame_ptr &&
2585  !(avctx->flags2 & CODEC_FLAG2_SKIP_MANUAL)) {
2586  if (discard_padding == frame->nb_samples) {
2587  *got_frame_ptr = 0;
2588  } else {
2589  if(avctx->pkt_timebase.num && avctx->sample_rate) {
2590  int64_t diff_ts = av_rescale_q(frame->nb_samples - discard_padding,
2591  (AVRational){1, avctx->sample_rate},
2592  avctx->pkt_timebase);
2593  if (av_frame_get_pkt_duration(frame) >= diff_ts)
2594  av_frame_set_pkt_duration(frame, av_frame_get_pkt_duration(frame) - diff_ts);
2595  } else {
2596  av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for discarded samples.\n");
2597  }
2598  av_log(avctx, AV_LOG_DEBUG, "discard %d/%d samples\n",
2599  discard_padding, frame->nb_samples);
2600  frame->nb_samples -= discard_padding;
2601  }
2602  }
2603 
2604  if ((avctx->flags2 & CODEC_FLAG2_SKIP_MANUAL) && *got_frame_ptr) {
2606  if (fside) {
2607  AV_WL32(fside->data, avctx->internal->skip_samples);
2608  AV_WL32(fside->data + 4, discard_padding);
2609  AV_WL8(fside->data + 8, skip_reason);
2610  AV_WL8(fside->data + 9, discard_reason);
2611  avctx->internal->skip_samples = 0;
2612  }
2613  }
2614 fail:
2615  avctx->internal->pkt = NULL;
2616  if (did_split) {
2618  if(ret == tmp.size)
2619  ret = avpkt->size;
2620  }
2621 
2622  if (ret >= 0 && *got_frame_ptr) {
2623  if (!avctx->refcounted_frames) {
2624  int err = unrefcount_frame(avci, frame);
2625  if (err < 0)
2626  return err;
2627  }
2628  } else
2629  av_frame_unref(frame);
2630  }
2631 
2632  return ret;
2633 }
2634 
2635 #define UTF8_MAX_BYTES 4 /* 5 and 6 bytes sequences should not be used */
2637  AVPacket *outpkt, const AVPacket *inpkt)
2638 {
2639 #if CONFIG_ICONV
2640  iconv_t cd = (iconv_t)-1;
2641  int ret = 0;
2642  char *inb, *outb;
2643  size_t inl, outl;
2644  AVPacket tmp;
2645 #endif
2646 
2647  if (avctx->sub_charenc_mode != FF_SUB_CHARENC_MODE_PRE_DECODER || inpkt->size == 0)
2648  return 0;
2649 
2650 #if CONFIG_ICONV
2651  cd = iconv_open("UTF-8", avctx->sub_charenc);
2652  av_assert0(cd != (iconv_t)-1);
2653 
2654  inb = inpkt->data;
2655  inl = inpkt->size;
2656 
2657  if (inl >= INT_MAX / UTF8_MAX_BYTES - FF_INPUT_BUFFER_PADDING_SIZE) {
2658  av_log(avctx, AV_LOG_ERROR, "Subtitles packet is too big for recoding\n");
2659  ret = AVERROR(ENOMEM);
2660  goto end;
2661  }
2662 
2663  ret = av_new_packet(&tmp, inl * UTF8_MAX_BYTES);
2664  if (ret < 0)
2665  goto end;
2666  outpkt->buf = tmp.buf;
2667  outpkt->data = tmp.data;
2668  outpkt->size = tmp.size;
2669  outb = outpkt->data;
2670  outl = outpkt->size;
2671 
2672  if (iconv(cd, &inb, &inl, &outb, &outl) == (size_t)-1 ||
2673  iconv(cd, NULL, NULL, &outb, &outl) == (size_t)-1 ||
2674  outl >= outpkt->size || inl != 0) {
2675  ret = FFMIN(AVERROR(errno), -1);
2676  av_log(avctx, AV_LOG_ERROR, "Unable to recode subtitle event \"%s\" "
2677  "from %s to UTF-8\n", inpkt->data, avctx->sub_charenc);
2678  av_free_packet(&tmp);
2679  goto end;
2680  }
2681  outpkt->size -= outl;
2682  memset(outpkt->data + outpkt->size, 0, outl);
2683 
2684 end:
2685  if (cd != (iconv_t)-1)
2686  iconv_close(cd);
2687  return ret;
2688 #else
2689  av_log(avctx, AV_LOG_ERROR, "requesting subtitles recoding without iconv");
2690  return AVERROR(EINVAL);
2691 #endif
2692 }
2693 
2694 static int utf8_check(const uint8_t *str)
2695 {
2696  const uint8_t *byte;
2697  uint32_t codepoint, min;
2698 
2699  while (*str) {
2700  byte = str;
2701  GET_UTF8(codepoint, *(byte++), return 0;);
2702  min = byte - str == 1 ? 0 : byte - str == 2 ? 0x80 :
2703  1 << (5 * (byte - str) - 4);
2704  if (codepoint < min || codepoint >= 0x110000 ||
2705  codepoint == 0xFFFE /* BOM */ ||
2706  codepoint >= 0xD800 && codepoint <= 0xDFFF /* surrogates */)
2707  return 0;
2708  str = byte;
2709  }
2710  return 1;
2711 }
2712 
2714  int *got_sub_ptr,
2715  AVPacket *avpkt)
2716 {
2717  int i, ret = 0;
2718 
2719  if (!avpkt->data && avpkt->size) {
2720  av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
2721  return AVERROR(EINVAL);
2722  }
2723  if (!avctx->codec)
2724  return AVERROR(EINVAL);
2725  if (avctx->codec->type != AVMEDIA_TYPE_SUBTITLE) {
2726  av_log(avctx, AV_LOG_ERROR, "Invalid media type for subtitles\n");
2727  return AVERROR(EINVAL);
2728  }
2729 
2730  *got_sub_ptr = 0;
2731  get_subtitle_defaults(sub);
2732 
2733  if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
2734  AVPacket pkt_recoded;
2735  AVPacket tmp = *avpkt;
2736  int did_split = av_packet_split_side_data(&tmp);
2737  //apply_param_change(avctx, &tmp);
2738 
2739  if (did_split) {
2740  /* FFMIN() prevents overflow in case the packet wasn't allocated with
2741  * proper padding.
2742  * If the side data is smaller than the buffer padding size, the
2743  * remaining bytes should have already been filled with zeros by the
2744  * original packet allocation anyway. */
2745  memset(tmp.data + tmp.size, 0,
2746  FFMIN(avpkt->size - tmp.size, FF_INPUT_BUFFER_PADDING_SIZE));
2747  }
2748 
2749  pkt_recoded = tmp;
2750  ret = recode_subtitle(avctx, &pkt_recoded, &tmp);
2751  if (ret < 0) {
2752  *got_sub_ptr = 0;
2753  } else {
2754  avctx->internal->pkt = &pkt_recoded;
2755 
2756  if (avctx->pkt_timebase.den && avpkt->pts != AV_NOPTS_VALUE)
2757  sub->pts = av_rescale_q(avpkt->pts,
2758  avctx->pkt_timebase, AV_TIME_BASE_Q);
2759  ret = avctx->codec->decode(avctx, sub, got_sub_ptr, &pkt_recoded);
2760  av_assert1((ret >= 0) >= !!*got_sub_ptr &&
2761  !!*got_sub_ptr >= !!sub->num_rects);
2762 
2763  if (sub->num_rects && !sub->end_display_time && avpkt->duration &&
2764  avctx->pkt_timebase.num) {
2765  AVRational ms = { 1, 1000 };
2766  sub->end_display_time = av_rescale_q(avpkt->duration,
2767  avctx->pkt_timebase, ms);
2768  }
2769 
2770  for (i = 0; i < sub->num_rects; i++) {
2771  if (sub->rects[i]->ass && !utf8_check(sub->rects[i]->ass)) {
2772  av_log(avctx, AV_LOG_ERROR,
2773  "Invalid UTF-8 in decoded subtitles text; "
2774  "maybe missing -sub_charenc option\n");
2775  avsubtitle_free(sub);
2776  return AVERROR_INVALIDDATA;
2777  }
2778  }
2779 
2780  if (tmp.data != pkt_recoded.data) { // did we recode?
2781  /* prevent from destroying side data from original packet */
2782  pkt_recoded.side_data = NULL;
2783  pkt_recoded.side_data_elems = 0;
2784 
2785  av_free_packet(&pkt_recoded);
2786  }
2788  sub->format = 0;
2789  else if (avctx->codec_descriptor->props & AV_CODEC_PROP_TEXT_SUB)
2790  sub->format = 1;
2791  avctx->internal->pkt = NULL;
2792  }
2793 
2794  if (did_split) {
2796  if(ret == tmp.size)
2797  ret = avpkt->size;
2798  }
2799 
2800  if (*got_sub_ptr)
2801  avctx->frame_number++;
2802  }
2803 
2804  return ret;
2805 }
2806 
2808 {
2809  int i;
2810 
2811  for (i = 0; i < sub->num_rects; i++) {
2812  av_freep(&sub->rects[i]->pict.data[0]);
2813  av_freep(&sub->rects[i]->pict.data[1]);
2814  av_freep(&sub->rects[i]->pict.data[2]);
2815  av_freep(&sub->rects[i]->pict.data[3]);
2816  av_freep(&sub->rects[i]->text);
2817  av_freep(&sub->rects[i]->ass);
2818  av_freep(&sub->rects[i]);
2819  }
2820 
2821  av_freep(&sub->rects);
2822 
2823  memset(sub, 0, sizeof(AVSubtitle));
2824 }
2825 
2827 {
2828  if (!avctx)
2829  return 0;
2830 
2831  if (avcodec_is_open(avctx)) {
2832  FramePool *pool = avctx->internal->pool;
2833  int i;
2834  if (CONFIG_FRAME_THREAD_ENCODER &&
2835  avctx->internal->frame_thread_encoder && avctx->thread_count > 1) {
2837  }
2838  if (HAVE_THREADS && avctx->internal->thread_ctx)
2839  ff_thread_free(avctx);
2840  if (avctx->codec && avctx->codec->close)
2841  avctx->codec->close(avctx);
2842  avctx->coded_frame = NULL;
2843  avctx->internal->byte_buffer_size = 0;
2844  av_freep(&avctx->internal->byte_buffer);
2845  av_frame_free(&avctx->internal->to_free);
2846  for (i = 0; i < FF_ARRAY_ELEMS(pool->pools); i++)
2847  av_buffer_pool_uninit(&pool->pools[i]);
2848  av_freep(&avctx->internal->pool);
2849 
2850  if (avctx->hwaccel && avctx->hwaccel->uninit)
2851  avctx->hwaccel->uninit(avctx);
2853 
2854  av_freep(&avctx->internal);
2855  }
2856 
2857  if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
2858  av_opt_free(avctx->priv_data);
2859  av_opt_free(avctx);
2860  av_freep(&avctx->priv_data);
2861  if (av_codec_is_encoder(avctx->codec))
2862  av_freep(&avctx->extradata);
2863  avctx->codec = NULL;
2864  avctx->active_thread_type = 0;
2865 
2866  return 0;
2867 }
2868 
2870 {
2871  switch(id){
2872  //This is for future deprecatec codec ids, its empty since
2873  //last major bump but will fill up again over time, please don't remove it
2874 // case AV_CODEC_ID_UTVIDEO_DEPRECATED: return AV_CODEC_ID_UTVIDEO;
2893  default : return id;
2894  }
2895 }
2896 
2897 static AVCodec *find_encdec(enum AVCodecID id, int encoder)
2898 {
2899  AVCodec *p, *experimental = NULL;
2900  p = first_avcodec;
2901  id= remap_deprecated_codec_id(id);
2902  while (p) {
2903  if ((encoder ? av_codec_is_encoder(p) : av_codec_is_decoder(p)) &&
2904  p->id == id) {
2905  if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
2906  experimental = p;
2907  } else
2908  return p;
2909  }
2910  p = p->next;
2911  }
2912  return experimental;
2913 }
2914 
2916 {
2917  return find_encdec(id, 1);
2918 }
2919 
2921 {
2922  AVCodec *p;
2923  if (!name)
2924  return NULL;
2925  p = first_avcodec;
2926  while (p) {
2927  if (av_codec_is_encoder(p) && strcmp(name, p->name) == 0)
2928  return p;
2929  p = p->next;
2930  }
2931  return NULL;
2932 }
2933 
2935 {
2936  return find_encdec(id, 0);
2937 }
2938 
2940 {
2941  AVCodec *p;
2942  if (!name)
2943  return NULL;
2944  p = first_avcodec;
2945  while (p) {
2946  if (av_codec_is_decoder(p) && strcmp(name, p->name) == 0)
2947  return p;
2948  p = p->next;
2949  }
2950  return NULL;
2951 }
2952 
2953 const char *avcodec_get_name(enum AVCodecID id)
2954 {
2955  const AVCodecDescriptor *cd;
2956  AVCodec *codec;
2957 
2958  if (id == AV_CODEC_ID_NONE)
2959  return "none";
2960  cd = avcodec_descriptor_get(id);
2961  if (cd)
2962  return cd->name;
2963  av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
2964  codec = avcodec_find_decoder(id);
2965  if (codec)
2966  return codec->name;
2967  codec = avcodec_find_encoder(id);
2968  if (codec)
2969  return codec->name;
2970  return "unknown_codec";
2971 }
2972 
2973 size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
2974 {
2975  int i, len, ret = 0;
2976 
2977 #define TAG_PRINT(x) \
2978  (((x) >= '0' && (x) <= '9') || \
2979  ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
2980  ((x) == '.' || (x) == ' ' || (x) == '-' || (x) == '_'))
2981 
2982  for (i = 0; i < 4; i++) {
2983  len = snprintf(buf, buf_size,
2984  TAG_PRINT(codec_tag & 0xFF) ? "%c" : "[%d]", codec_tag & 0xFF);
2985  buf += len;
2986  buf_size = buf_size > len ? buf_size - len : 0;
2987  ret += len;
2988  codec_tag >>= 8;
2989  }
2990  return ret;
2991 }
2992 
2993 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
2994 {
2995  const char *codec_type;
2996  const char *codec_name;
2997  const char *profile = NULL;
2998  const AVCodec *p;
2999  int bitrate;
3000  int new_line = 0;
3001  AVRational display_aspect_ratio;
3002  const char *separator = enc->dump_separator ? (const char *)enc->dump_separator : ", ";
3003 
3004  if (!buf || buf_size <= 0)
3005  return;
3006  codec_type = av_get_media_type_string(enc->codec_type);
3007  codec_name = avcodec_get_name(enc->codec_id);
3008  if (enc->profile != FF_PROFILE_UNKNOWN) {
3009  if (enc->codec)
3010  p = enc->codec;
3011  else
3012  p = encode ? avcodec_find_encoder(enc->codec_id) :
3014  if (p)
3015  profile = av_get_profile_name(p, enc->profile);
3016  }
3017 
3018  snprintf(buf, buf_size, "%s: %s", codec_type ? codec_type : "unknown",
3019  codec_name);
3020  buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
3021 
3022  if (enc->codec && strcmp(enc->codec->name, codec_name))
3023  snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", enc->codec->name);
3024 
3025  if (profile)
3026  snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
3027 
3028  if (enc->codec_tag) {
3029  char tag_buf[32];
3030  av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
3031  snprintf(buf + strlen(buf), buf_size - strlen(buf),
3032  " (%s / 0x%04X)", tag_buf, enc->codec_tag);
3033  }
3034 
3035  switch (enc->codec_type) {
3036  case AVMEDIA_TYPE_VIDEO:
3037  {
3038  char detail[256] = "(";
3039 
3040  av_strlcat(buf, separator, buf_size);
3041 
3042  snprintf(buf + strlen(buf), buf_size - strlen(buf),
3043  "%s", enc->pix_fmt == AV_PIX_FMT_NONE ? "none" :
3045  if (enc->bits_per_raw_sample && enc->pix_fmt != AV_PIX_FMT_NONE &&
3047  av_strlcatf(detail, sizeof(detail), "%d bpc, ", enc->bits_per_raw_sample);
3049  av_strlcatf(detail, sizeof(detail), "%s, ",
3051 
3052  if (enc->colorspace != AVCOL_SPC_UNSPECIFIED ||
3054  enc->color_trc != AVCOL_TRC_UNSPECIFIED) {
3055  if (enc->colorspace != (int)enc->color_primaries ||
3056  enc->colorspace != (int)enc->color_trc) {
3057  new_line = 1;
3058  av_strlcatf(detail, sizeof(detail), "%s/%s/%s, ",
3062  } else
3063  av_strlcatf(detail, sizeof(detail), "%s, ",
3065  }
3066 
3067  if (av_log_get_level() >= AV_LOG_DEBUG &&
3069  av_strlcatf(detail, sizeof(detail), "%s, ",
3071 
3072  if (strlen(detail) > 1) {
3073  detail[strlen(detail) - 2] = 0;
3074  av_strlcatf(buf, buf_size, "%s)", detail);
3075  }
3076  }
3077 
3078  if (enc->width) {
3079  av_strlcat(buf, new_line ? separator : ", ", buf_size);
3080 
3081  snprintf(buf + strlen(buf), buf_size - strlen(buf),
3082  "%dx%d",
3083  enc->width, enc->height);
3084 
3085  if (av_log_get_level() >= AV_LOG_VERBOSE &&
3086  (enc->width != enc->coded_width ||
3087  enc->height != enc->coded_height))
3088  snprintf(buf + strlen(buf), buf_size - strlen(buf),
3089  " (%dx%d)", enc->coded_width, enc->coded_height);
3090 
3091  if (enc->sample_aspect_ratio.num) {
3092  av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
3093  enc->width * enc->sample_aspect_ratio.num,
3094  enc->height * enc->sample_aspect_ratio.den,
3095  1024 * 1024);
3096  snprintf(buf + strlen(buf), buf_size - strlen(buf),
3097  " [SAR %d:%d DAR %d:%d]",
3099  display_aspect_ratio.num, display_aspect_ratio.den);
3100  }
3101  if (av_log_get_level() >= AV_LOG_DEBUG) {
3102  int g = av_gcd(enc->time_base.num, enc->time_base.den);
3103  snprintf(buf + strlen(buf), buf_size - strlen(buf),
3104  ", %d/%d",
3105  enc->time_base.num / g, enc->time_base.den / g);
3106  }
3107  }
3108  if (encode) {
3109  snprintf(buf + strlen(buf), buf_size - strlen(buf),
3110  ", q=%d-%d", enc->qmin, enc->qmax);
3111  }
3112  break;
3113  case AVMEDIA_TYPE_AUDIO:
3114  av_strlcat(buf, separator, buf_size);
3115 
3116  if (enc->sample_rate) {
3117  snprintf(buf + strlen(buf), buf_size - strlen(buf),
3118  "%d Hz, ", enc->sample_rate);
3119  }
3120  av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
3121  if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
3122  snprintf(buf + strlen(buf), buf_size - strlen(buf),
3123  ", %s", av_get_sample_fmt_name(enc->sample_fmt));
3124  }
3125  if ( enc->bits_per_raw_sample > 0
3127  snprintf(buf + strlen(buf), buf_size - strlen(buf),
3128  " (%d bit)", enc->bits_per_raw_sample);
3129  break;
3130  case AVMEDIA_TYPE_DATA:
3131  if (av_log_get_level() >= AV_LOG_DEBUG) {
3132  int g = av_gcd(enc->time_base.num, enc->time_base.den);
3133  if (g)
3134  snprintf(buf + strlen(buf), buf_size - strlen(buf),
3135  ", %d/%d",
3136  enc->time_base.num / g, enc->time_base.den / g);
3137  }
3138  break;
3139  case AVMEDIA_TYPE_SUBTITLE:
3140  if (enc->width)
3141  snprintf(buf + strlen(buf), buf_size - strlen(buf),
3142  ", %dx%d", enc->width, enc->height);
3143  break;
3144  default:
3145  return;
3146  }
3147  if (encode) {
3148  if (enc->flags & CODEC_FLAG_PASS1)
3149  snprintf(buf + strlen(buf), buf_size - strlen(buf),
3150  ", pass 1");
3151  if (enc->flags & CODEC_FLAG_PASS2)
3152  snprintf(buf + strlen(buf), buf_size - strlen(buf),
3153  ", pass 2");
3154  }
3155  bitrate = get_bit_rate(enc);
3156  if (bitrate != 0) {
3157  snprintf(buf + strlen(buf), buf_size - strlen(buf),
3158  ", %d kb/s", bitrate / 1000);
3159  } else if (enc->rc_max_rate > 0) {
3160  snprintf(buf + strlen(buf), buf_size - strlen(buf),
3161  ", max. %d kb/s", enc->rc_max_rate / 1000);
3162  }
3163 }
3164 
3165 const char *av_get_profile_name(const AVCodec *codec, int profile)
3166 {
3167  const AVProfile *p;
3168  if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
3169  return NULL;
3170 
3171  for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
3172  if (p->profile == profile)
3173  return p->name;
3174 
3175  return NULL;
3176 }
3177 
3178 unsigned avcodec_version(void)
3179 {
3180 // av_assert0(AV_CODEC_ID_V410==164);
3183 // av_assert0(AV_CODEC_ID_BMV_AUDIO==86071);
3184  av_assert0(AV_CODEC_ID_SRT==94216);
3186 
3192  return LIBAVCODEC_VERSION_INT;
3193 }
3194 
3195 const char *avcodec_configuration(void)
3196 {
3197  return FFMPEG_CONFIGURATION;
3198 }
3199 
3200 const char *avcodec_license(void)
3201 {
3202 #define LICENSE_PREFIX "libavcodec license: "
3203  return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
3204 }
3205 
3207 {
3208  if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
3209  ff_thread_flush(avctx);
3210  else if (avctx->codec->flush)
3211  avctx->codec->flush(avctx);
3212 
3213  avctx->pts_correction_last_pts =
3214  avctx->pts_correction_last_dts = INT64_MIN;
3215 
3216  if (!avctx->refcounted_frames)
3217  av_frame_unref(avctx->internal->to_free);
3218 }
3219 
3221 {
3222  switch (codec_id) {
3223  case AV_CODEC_ID_8SVX_EXP:
3224  case AV_CODEC_ID_8SVX_FIB:
3225  case AV_CODEC_ID_ADPCM_CT:
3232  return 4;
3233  case AV_CODEC_ID_DSD_LSBF:
3234  case AV_CODEC_ID_DSD_MSBF:
3237  case AV_CODEC_ID_PCM_ALAW:
3238  case AV_CODEC_ID_PCM_MULAW:
3239  case AV_CODEC_ID_PCM_S8:
3241  case AV_CODEC_ID_PCM_U8:
3242  case AV_CODEC_ID_PCM_ZORK:
3243  return 8;
3244  case AV_CODEC_ID_PCM_S16BE:
3246  case AV_CODEC_ID_PCM_S16LE:
3248  case AV_CODEC_ID_PCM_U16BE:
3249  case AV_CODEC_ID_PCM_U16LE:
3250  return 16;
3252  case AV_CODEC_ID_PCM_S24BE:
3253  case AV_CODEC_ID_PCM_S24LE:
3255  case AV_CODEC_ID_PCM_U24BE:
3256  case AV_CODEC_ID_PCM_U24LE:
3257  return 24;
3258  case AV_CODEC_ID_PCM_S32BE:
3259  case AV_CODEC_ID_PCM_S32LE:
3261  case AV_CODEC_ID_PCM_U32BE:
3262  case AV_CODEC_ID_PCM_U32LE:
3263  case AV_CODEC_ID_PCM_F32BE:
3264  case AV_CODEC_ID_PCM_F32LE:
3265  return 32;
3266  case AV_CODEC_ID_PCM_F64BE:
3267  case AV_CODEC_ID_PCM_F64LE:
3268  return 64;
3269  default:
3270  return 0;
3271  }
3272 }
3273 
3275 {
3276  static const enum AVCodecID map[AV_SAMPLE_FMT_NB][2] = {
3287  };
3288  if (fmt < 0 || fmt >= AV_SAMPLE_FMT_NB)
3289  return AV_CODEC_ID_NONE;
3290  if (be < 0 || be > 1)
3291  be = AV_NE(1, 0);
3292  return map[fmt][be];
3293 }
3294 
3296 {
3297  switch (codec_id) {
3299  return 2;
3301  return 3;
3305  case AV_CODEC_ID_ADPCM_SWF:
3306  case AV_CODEC_ID_ADPCM_MS:
3307  return 4;
3308  default:
3309  return av_get_exact_bits_per_sample(codec_id);
3310  }
3311 }
3312 
3313 int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
3314 {
3315  int id, sr, ch, ba, tag, bps;
3316 
3317  id = avctx->codec_id;
3318  sr = avctx->sample_rate;
3319  ch = avctx->channels;
3320  ba = avctx->block_align;
3321  tag = avctx->codec_tag;
3322  bps = av_get_exact_bits_per_sample(avctx->codec_id);
3323 
3324  /* codecs with an exact constant bits per sample */
3325  if (bps > 0 && ch > 0 && frame_bytes > 0 && ch < 32768 && bps < 32768)
3326  return (frame_bytes * 8LL) / (bps * ch);
3327  bps = avctx->bits_per_coded_sample;
3328 
3329  /* codecs with a fixed packet duration */
3330  switch (id) {
3331  case AV_CODEC_ID_ADPCM_ADX: return 32;
3332  case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
3333  case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
3334  case AV_CODEC_ID_AMR_NB:
3335  case AV_CODEC_ID_EVRC:
3336  case AV_CODEC_ID_GSM:
3337  case AV_CODEC_ID_QCELP:
3338  case AV_CODEC_ID_RA_288: return 160;
3339  case AV_CODEC_ID_AMR_WB:
3340  case AV_CODEC_ID_GSM_MS: return 320;
3341  case AV_CODEC_ID_MP1: return 384;
3342  case AV_CODEC_ID_ATRAC1: return 512;
3343  case AV_CODEC_ID_ATRAC3: return 1024;
3344  case AV_CODEC_ID_ATRAC3P: return 2048;
3345  case AV_CODEC_ID_MP2:
3346  case AV_CODEC_ID_MUSEPACK7: return 1152;
3347  case AV_CODEC_ID_AC3: return 1536;
3348  }
3349 
3350  if (sr > 0) {
3351  /* calc from sample rate */
3352  if (id == AV_CODEC_ID_TTA)
3353  return 256 * sr / 245;
3354 
3355  if (ch > 0) {
3356  /* calc from sample rate and channels */
3357  if (id == AV_CODEC_ID_BINKAUDIO_DCT)
3358  return (480 << (sr / 22050)) / ch;
3359  }
3360  }
3361 
3362  if (ba > 0) {
3363  /* calc from block_align */
3364  if (id == AV_CODEC_ID_SIPR) {
3365  switch (ba) {
3366  case 20: return 160;
3367  case 19: return 144;
3368  case 29: return 288;
3369  case 37: return 480;
3370  }
3371  } else if (id == AV_CODEC_ID_ILBC) {
3372  switch (ba) {
3373  case 38: return 160;
3374  case 50: return 240;
3375  }
3376  }
3377  }
3378 
3379  if (frame_bytes > 0) {
3380  /* calc from frame_bytes only */
3381  if (id == AV_CODEC_ID_TRUESPEECH)
3382  return 240 * (frame_bytes / 32);
3383  if (id == AV_CODEC_ID_NELLYMOSER)
3384  return 256 * (frame_bytes / 64);
3385  if (id == AV_CODEC_ID_RA_144)
3386  return 160 * (frame_bytes / 20);
3387  if (id == AV_CODEC_ID_G723_1)
3388  return 240 * (frame_bytes / 24);
3389 
3390  if (bps > 0) {
3391  /* calc from frame_bytes and bits_per_coded_sample */
3392  if (id == AV_CODEC_ID_ADPCM_G726)
3393  return frame_bytes * 8 / bps;
3394  }
3395 
3396  if (ch > 0) {
3397  /* calc from frame_bytes and channels */
3398  switch (id) {
3399  case AV_CODEC_ID_ADPCM_AFC:
3400  return frame_bytes / (9 * ch) * 16;
3401  case AV_CODEC_ID_ADPCM_DTK:
3402  return frame_bytes / (16 * ch) * 28;
3403  case AV_CODEC_ID_ADPCM_4XM:
3405  return (frame_bytes - 4 * ch) * 2 / ch;
3407  return (frame_bytes - 4) * 2 / ch;
3409  return (frame_bytes - 8) * 2 / ch;
3410  case AV_CODEC_ID_ADPCM_XA:
3411  return (frame_bytes / 128) * 224 / ch;
3413  return (frame_bytes - 6 - ch) / ch;
3414  case AV_CODEC_ID_ROQ_DPCM:
3415  return (frame_bytes - 8) / ch;
3416  case AV_CODEC_ID_XAN_DPCM:
3417  return (frame_bytes - 2 * ch) / ch;
3418  case AV_CODEC_ID_MACE3:
3419  return 3 * frame_bytes / ch;
3420  case AV_CODEC_ID_MACE6:
3421  return 6 * frame_bytes / ch;
3422  case AV_CODEC_ID_PCM_LXF:
3423  return 2 * (frame_bytes / (5 * ch));
3424  case AV_CODEC_ID_IAC:
3425  case AV_CODEC_ID_IMC:
3426  return 4 * frame_bytes / ch;
3427  }
3428 
3429  if (tag) {
3430  /* calc from frame_bytes, channels, and codec_tag */
3431  if (id == AV_CODEC_ID_SOL_DPCM) {
3432  if (tag == 3)
3433  return frame_bytes / ch;
3434  else
3435  return frame_bytes * 2 / ch;
3436  }
3437  }
3438 
3439  if (ba > 0) {
3440  /* calc from frame_bytes, channels, and block_align */
3441  int blocks = frame_bytes / ba;
3442  switch (avctx->codec_id) {
3444  if (bps < 2 || bps > 5)
3445  return 0;
3446  return blocks * (1 + (ba - 4 * ch) / (bps * ch) * 8);
3448  return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
3450  return blocks * (1 + (ba - 4 * ch) * 2 / ch);
3452  return blocks * ((ba - 4 * ch) * 2 / ch);
3453  case AV_CODEC_ID_ADPCM_MS:
3454  return blocks * (2 + (ba - 7 * ch) * 2 / ch);
3455  }
3456  }
3457 
3458  if (bps > 0) {
3459  /* calc from frame_bytes, channels, and bits_per_coded_sample */
3460  switch (avctx->codec_id) {
3461  case AV_CODEC_ID_PCM_DVD:
3462  if(bps<4)
3463  return 0;
3464  return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
3466  if(bps<4)
3467  return 0;
3468  return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
3469  case AV_CODEC_ID_S302M:
3470  return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
3471  }
3472  }
3473  }
3474  }
3475 
3476  /* Fall back on using frame_size */
3477  if (avctx->frame_size > 1 && frame_bytes)
3478  return avctx->frame_size;
3479 
3480  //For WMA we currently have no other means to calculate duration thus we
3481  //do it here by assuming CBR, which is true for all known cases.
3482  if (avctx->bit_rate>0 && frame_bytes>0 && avctx->sample_rate>0 && avctx->block_align>1) {
3483  if (avctx->codec_id == AV_CODEC_ID_WMAV1 || avctx->codec_id == AV_CODEC_ID_WMAV2)
3484  return (frame_bytes * 8LL * avctx->sample_rate) / avctx->bit_rate;
3485  }
3486 
3487  return 0;
3488 }
3489 
3490 #if !HAVE_THREADS
3492 {
3493  return -1;
3494 }
3495 
3496 #endif
3497 
3498 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
3499 {
3500  unsigned int n = 0;
3501 
3502  while (v >= 0xff) {
3503  *s++ = 0xff;
3504  v -= 0xff;
3505  n++;
3506  }
3507  *s = v;
3508  n++;
3509  return n;
3510 }
3511 
3512 int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
3513 {
3514  int i;
3515  for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
3516  return i;
3517 }
3518 
3519 #if FF_API_MISSING_SAMPLE
3521 void av_log_missing_feature(void *avc, const char *feature, int want_sample)
3522 {
3523  av_log(avc, AV_LOG_WARNING, "%s is not implemented. Update your FFmpeg "
3524  "version to the newest one from Git. If the problem still "
3525  "occurs, it means that your file has a feature which has not "
3526  "been implemented.\n", feature);
3527  if(want_sample)
3529 }
3530 
3531 void av_log_ask_for_sample(void *avc, const char *msg, ...)
3532 {
3533  va_list argument_list;
3534 
3535  va_start(argument_list, msg);
3536 
3537  if (msg)
3538  av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
3539  av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
3540  "of this file to ftp://upload.ffmpeg.org/incoming/ "
3541  "and contact the ffmpeg-devel mailing list. (ffmpeg-devel@ffmpeg.org)\n");
3542 
3543  va_end(argument_list);
3544 }
3546 #endif /* FF_API_MISSING_SAMPLE */
3547 
3550 
3552 {
3553  AVHWAccel **p = last_hwaccel;
3554  hwaccel->next = NULL;
3555  while(*p || avpriv_atomic_ptr_cas((void * volatile *)p, NULL, hwaccel))
3556  p = &(*p)->next;
3557  last_hwaccel = &hwaccel->next;
3558 }
3559 
3561 {
3562  return hwaccel ? hwaccel->next : first_hwaccel;
3563 }
3564 
3565 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
3566 {
3567  if (lockmgr_cb) {
3568  // There is no good way to rollback a failure to destroy the
3569  // mutex, so we ignore failures.
3572  lockmgr_cb = NULL;
3573  codec_mutex = NULL;
3574  avformat_mutex = NULL;
3575  }
3576 
3577  if (cb) {
3578  void *new_codec_mutex = NULL;
3579  void *new_avformat_mutex = NULL;
3580  int err;
3581  if (err = cb(&new_codec_mutex, AV_LOCK_CREATE)) {
3582  return err > 0 ? AVERROR_UNKNOWN : err;
3583  }
3584  if (err = cb(&new_avformat_mutex, AV_LOCK_CREATE)) {
3585  // Ignore failures to destroy the newly created mutex.
3586  cb(&new_codec_mutex, AV_LOCK_DESTROY);
3587  return err > 0 ? AVERROR_UNKNOWN : err;
3588  }
3589  lockmgr_cb = cb;
3590  codec_mutex = new_codec_mutex;
3591  avformat_mutex = new_avformat_mutex;
3592  }
3593 
3594  return 0;
3595 }
3596 
3598 {
3599  if (lockmgr_cb) {
3601  return -1;
3602  }
3604  if (entangled_thread_counter != 1) {
3605  av_log(log_ctx, AV_LOG_ERROR, "Insufficient thread locking around avcodec_open/close()\n");
3606  if (!lockmgr_cb)
3607  av_log(log_ctx, AV_LOG_ERROR, "No lock manager is set, please see av_lockmgr_register()\n");
3608  ff_avcodec_locked = 1;
3610  return AVERROR(EINVAL);
3611  }
3613  ff_avcodec_locked = 1;
3614  return 0;
3615 }
3616 
3618 {
3620  ff_avcodec_locked = 0;
3622  if (lockmgr_cb) {
3624  return -1;
3625  }
3626 
3627  return 0;
3628 }
3629 
3631 {
3632  if (lockmgr_cb) {
3634  return -1;
3635  }
3636  return 0;
3637 }
3638 
3640 {
3641  if (lockmgr_cb) {
3643  return -1;
3644  }
3645  return 0;
3646 }
3647 
3648 unsigned int avpriv_toupper4(unsigned int x)
3649 {
3650  return av_toupper(x & 0xFF) +
3651  (av_toupper((x >> 8) & 0xFF) << 8) +
3652  (av_toupper((x >> 16) & 0xFF) << 16) +
3653 ((unsigned)av_toupper((x >> 24) & 0xFF) << 24);
3654 }
3655 
3657 {
3658  int ret;
3659 
3660  dst->owner = src->owner;
3661 
3662  ret = av_frame_ref(dst->f, src->f);
3663  if (ret < 0)
3664  return ret;
3665 
3666  av_assert0(!dst->progress);
3667 
3668  if (src->progress &&
3669  !(dst->progress = av_buffer_ref(src->progress))) {
3670  ff_thread_release_buffer(dst->owner, dst);
3671  return AVERROR(ENOMEM);
3672  }
3673 
3674  return 0;
3675 }
3676 
3677 #if !HAVE_THREADS
3678 
3680 {
3681  return ff_get_format(avctx, fmt);
3682 }
3683 
3685 {
3686  f->owner = avctx;
3687  return ff_get_buffer(avctx, f->f, flags);
3688 }
3689 
3691 {
3692  if (f->f)
3693  av_frame_unref(f->f);
3694 }
3695 
3697 {
3698 }
3699 
3700 void ff_thread_report_progress(ThreadFrame *f, int progress, int field)
3701 {
3702 }
3703 
3704 void ff_thread_await_progress(ThreadFrame *f, int progress, int field)
3705 {
3706 }
3707 
3709 {
3710  return 1;
3711 }
3712 
3714 {
3715  return 0;
3716 }
3717 
3719 {
3720 }
3721 
3722 void ff_thread_await_progress2(AVCodecContext *avctx, int field, int thread, int shift)
3723 {
3724 }
3725 
3726 void ff_thread_report_progress2(AVCodecContext *avctx, int field, int thread, int n)
3727 {
3728 }
3729 
3730 #endif
3731 
3733 {
3734  AVCodec *c= avcodec_find_decoder(codec_id);
3735  if(!c)
3736  c= avcodec_find_encoder(codec_id);
3737  if(c)
3738  return c->type;
3739 
3740  if (codec_id <= AV_CODEC_ID_NONE)
3741  return AVMEDIA_TYPE_UNKNOWN;
3742  else if (codec_id < AV_CODEC_ID_FIRST_AUDIO)
3743  return AVMEDIA_TYPE_VIDEO;
3744  else if (codec_id < AV_CODEC_ID_FIRST_SUBTITLE)
3745  return AVMEDIA_TYPE_AUDIO;
3746  else if (codec_id < AV_CODEC_ID_FIRST_UNKNOWN)
3747  return AVMEDIA_TYPE_SUBTITLE;
3748 
3749  return AVMEDIA_TYPE_UNKNOWN;
3750 }
3751 
3753 {
3754  return !!s->internal;
3755 }
3756 
3757 int avpriv_bprint_to_extradata(AVCodecContext *avctx, struct AVBPrint *buf)
3758 {
3759  int ret;
3760  char *str;
3761 
3762  ret = av_bprint_finalize(buf, &str);
3763  if (ret < 0)
3764  return ret;
3765  if (!av_bprint_is_complete(buf)) {
3766  av_free(str);
3767  return AVERROR(ENOMEM);
3768  }
3769 
3770  avctx->extradata = str;
3771  /* Note: the string is NUL terminated (so extradata can be read as a
3772  * string), but the ending character is not accounted in the size (in
3773  * binary formats you are likely not supposed to mux that character). When
3774  * extradata is copied, it is also padded with FF_INPUT_BUFFER_PADDING_SIZE
3775  * zeros. */
3776  avctx->extradata_size = buf->len;
3777  return 0;
3778 }
3779 
3780 const uint8_t *avpriv_find_start_code(const uint8_t *av_restrict p,
3781  const uint8_t *end,
3782  uint32_t *av_restrict state)
3783 {
3784  int i;
3785 
3786  av_assert0(p <= end);
3787  if (p >= end)
3788  return end;
3789 
3790  for (i = 0; i < 3; i++) {
3791  uint32_t tmp = *state << 8;
3792  *state = tmp + *(p++);
3793  if (tmp == 0x100 || p == end)
3794  return p;
3795  }
3796 
3797  while (p < end) {
3798  if (p[-1] > 1 ) p += 3;
3799  else if (p[-2] ) p += 2;
3800  else if (p[-3]|(p[-1]-1)) p++;
3801  else {
3802  p++;
3803  break;
3804  }
3805  }
3806 
3807  p = FFMIN(p, end) - 4;
3808  *state = AV_RB32(p);
3809 
3810  return p + 4;
3811 }