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