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