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