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/mathematics.h"
38 #include "libavutil/pixdesc.h"
39 #include "libavutil/imgutils.h"
40 #include "libavutil/samplefmt.h"
41 #include "libavutil/dict.h"
42 #include "libavutil/avassert.h"
43 #include "avcodec.h"
44 #include "dsputil.h"
45 #include "libavutil/opt.h"
46 #include "thread.h"
47 #include "frame_thread_encoder.h"
48 #include "internal.h"
49 #include "bytestream.h"
50 #include "version.h"
51 #include <stdlib.h>
52 #include <stdarg.h>
53 #include <limits.h>
54 #include <float.h>
55 #if CONFIG_ICONV
56 # include <iconv.h>
57 #endif
58 
59 volatile int ff_avcodec_locked;
60 static int volatile entangled_thread_counter = 0;
61 static int (*lockmgr_cb)(void **mutex, enum AVLockOp op);
62 static void *codec_mutex;
63 static void *avformat_mutex;
64 
65 void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
66 {
67  if (min_size < *size)
68  return ptr;
69 
70  min_size = FFMAX(17 * min_size / 16 + 32, min_size);
71 
72  ptr = av_realloc(ptr, min_size);
73  /* we could set this to the unmodified min_size but this is safer
74  * if the user lost the ptr and uses NULL now
75  */
76  if (!ptr)
77  min_size = 0;
78 
79  *size = min_size;
80 
81  return ptr;
82 }
83 
84 static inline int ff_fast_malloc(void *ptr, unsigned int *size, size_t min_size, int zero_realloc)
85 {
86  void **p = ptr;
87  if (min_size < *size)
88  return 0;
89  min_size = FFMAX(17 * min_size / 16 + 32, min_size);
90  av_free(*p);
91  *p = zero_realloc ? av_mallocz(min_size) : av_malloc(min_size);
92  if (!*p)
93  min_size = 0;
94  *size = min_size;
95  return 1;
96 }
97 
98 void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
99 {
100  ff_fast_malloc(ptr, size, min_size, 0);
101 }
102 
103 void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
104 {
105  uint8_t **p = ptr;
106  if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
107  av_freep(p);
108  *size = 0;
109  return;
110  }
111  if (!ff_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE, 1))
112  memset(*p + min_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
113 }
114 
115 void av_fast_padded_mallocz(void *ptr, unsigned int *size, size_t min_size)
116 {
117  uint8_t **p = ptr;
118  if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
119  av_freep(p);
120  *size = 0;
121  return;
122  }
123  if (!ff_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE, 1))
124  memset(*p, 0, min_size + FF_INPUT_BUFFER_PADDING_SIZE);
125 }
126 
127 /* encoder management */
128 static AVCodec *first_avcodec = NULL;
129 
131 {
132  if (c)
133  return c->next;
134  else
135  return first_avcodec;
136 }
137 
138 static av_cold void avcodec_init(void)
139 {
140  static int initialized = 0;
141 
142  if (initialized != 0)
143  return;
144  initialized = 1;
145 
146  if (CONFIG_DSPUTIL)
148 }
149 
150 int av_codec_is_encoder(const AVCodec *codec)
151 {
152  return codec && (codec->encode_sub || codec->encode2);
153 }
154 
155 int av_codec_is_decoder(const AVCodec *codec)
156 {
157  return codec && codec->decode;
158 }
159 
161 {
162  AVCodec **p;
163  avcodec_init();
164  p = &first_avcodec;
165  codec->next = NULL;
166  while(avpriv_atomic_ptr_cas((void * volatile *)p, NULL, codec))
167  p = &(*p)->next;
168 
169  if (codec->init_static_data)
170  codec->init_static_data(codec);
171 }
172 
174 {
175  return EDGE_WIDTH;
176 }
177 
179 {
180  s->coded_width = width;
181  s->coded_height = height;
182  s->width = FF_CEIL_RSHIFT(width, s->lowres);
183  s->height = FF_CEIL_RSHIFT(height, s->lowres);
184 }
185 
186 #if (ARCH_ARM && HAVE_NEON) || ARCH_PPC || HAVE_MMX
187 # define STRIDE_ALIGN 16
188 #else
189 # define STRIDE_ALIGN 8
190 #endif
191 
193  int linesize_align[AV_NUM_DATA_POINTERS])
194 {
195  int i;
196  int w_align = 1;
197  int h_align = 1;
198 
199  switch (s->pix_fmt) {
200  case AV_PIX_FMT_YUV420P:
201  case AV_PIX_FMT_YUYV422:
202  case AV_PIX_FMT_UYVY422:
203  case AV_PIX_FMT_YUV422P:
204  case AV_PIX_FMT_YUV440P:
205  case AV_PIX_FMT_YUV444P:
206  case AV_PIX_FMT_GBRAP:
207  case AV_PIX_FMT_GBRP:
208  case AV_PIX_FMT_GRAY8:
209  case AV_PIX_FMT_GRAY16BE:
210  case AV_PIX_FMT_GRAY16LE:
211  case AV_PIX_FMT_YUVJ420P:
212  case AV_PIX_FMT_YUVJ422P:
213  case AV_PIX_FMT_YUVJ440P:
214  case AV_PIX_FMT_YUVJ444P:
215  case AV_PIX_FMT_YUVA420P:
216  case AV_PIX_FMT_YUVA422P:
217  case AV_PIX_FMT_YUVA444P:
254  case AV_PIX_FMT_GBRP9LE:
255  case AV_PIX_FMT_GBRP9BE:
256  case AV_PIX_FMT_GBRP10LE:
257  case AV_PIX_FMT_GBRP10BE:
258  case AV_PIX_FMT_GBRP12LE:
259  case AV_PIX_FMT_GBRP12BE:
260  case AV_PIX_FMT_GBRP14LE:
261  case AV_PIX_FMT_GBRP14BE:
262  w_align = 16; //FIXME assume 16 pixel per macroblock
263  h_align = 16 * 2; // interlaced needs 2 macroblocks height
264  break;
265  case AV_PIX_FMT_YUV411P:
266  case AV_PIX_FMT_YUVJ411P:
268  w_align = 32;
269  h_align = 8;
270  break;
271  case AV_PIX_FMT_YUV410P:
272  if (s->codec_id == AV_CODEC_ID_SVQ1) {
273  w_align = 64;
274  h_align = 64;
275  }
276  break;
277  case AV_PIX_FMT_RGB555:
278  if (s->codec_id == AV_CODEC_ID_RPZA) {
279  w_align = 4;
280  h_align = 4;
281  }
282  break;
283  case AV_PIX_FMT_PAL8:
284  case AV_PIX_FMT_BGR8:
285  case AV_PIX_FMT_RGB8:
286  if (s->codec_id == AV_CODEC_ID_SMC ||
288  w_align = 4;
289  h_align = 4;
290  }
291  break;
292  case AV_PIX_FMT_BGR24:
293  if ((s->codec_id == AV_CODEC_ID_MSZH) ||
294  (s->codec_id == AV_CODEC_ID_ZLIB)) {
295  w_align = 4;
296  h_align = 4;
297  }
298  break;
299  case AV_PIX_FMT_RGB24:
300  if (s->codec_id == AV_CODEC_ID_CINEPAK) {
301  w_align = 4;
302  h_align = 4;
303  }
304  break;
305  default:
306  w_align = 1;
307  h_align = 1;
308  break;
309  }
310 
312  w_align = FFMAX(w_align, 8);
313  }
314 
315  *width = FFALIGN(*width, w_align);
316  *height = FFALIGN(*height, h_align);
317  if (s->codec_id == AV_CODEC_ID_H264 || s->lowres)
318  // some of the optimized chroma MC reads one line too much
319  // which is also done in mpeg decoders with lowres > 0
320  *height += 2;
321 
322  for (i = 0; i < 4; i++)
323  linesize_align[i] = STRIDE_ALIGN;
324 }
325 
327 {
329  int chroma_shift = desc->log2_chroma_w;
330  int linesize_align[AV_NUM_DATA_POINTERS];
331  int align;
332 
333  avcodec_align_dimensions2(s, width, height, linesize_align);
334  align = FFMAX(linesize_align[0], linesize_align[3]);
335  linesize_align[1] <<= chroma_shift;
336  linesize_align[2] <<= chroma_shift;
337  align = FFMAX3(align, linesize_align[1], linesize_align[2]);
338  *width = FFALIGN(*width, align);
339 }
340 
342  enum AVSampleFormat sample_fmt, const uint8_t *buf,
343  int buf_size, int align)
344 {
345  int ch, planar, needed_size, ret = 0;
346 
347  needed_size = av_samples_get_buffer_size(NULL, nb_channels,
348  frame->nb_samples, sample_fmt,
349  align);
350  if (buf_size < needed_size)
351  return AVERROR(EINVAL);
352 
353  planar = av_sample_fmt_is_planar(sample_fmt);
354  if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
355  if (!(frame->extended_data = av_mallocz(nb_channels *
356  sizeof(*frame->extended_data))))
357  return AVERROR(ENOMEM);
358  } else {
359  frame->extended_data = frame->data;
360  }
361 
362  if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
363  (uint8_t *)(intptr_t)buf, nb_channels, frame->nb_samples,
364  sample_fmt, align)) < 0) {
365  if (frame->extended_data != frame->data)
366  av_freep(&frame->extended_data);
367  return ret;
368  }
369  if (frame->extended_data != frame->data) {
370  for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
371  frame->data[ch] = frame->extended_data[ch];
372  }
373 
374  return ret;
375 }
376 
378 {
379  FramePool *pool = avctx->internal->pool;
380  int i, ret;
381 
382  switch (avctx->codec_type) {
383  case AVMEDIA_TYPE_VIDEO: {
384  AVPicture picture;
385  int size[4] = { 0 };
386  int w = frame->width;
387  int h = frame->height;
388  int tmpsize, unaligned;
389 
390  if (pool->format == frame->format &&
391  pool->width == frame->width && pool->height == frame->height)
392  return 0;
393 
394  avcodec_align_dimensions2(avctx, &w, &h, pool->stride_align);
395 
396  if (!(avctx->flags & CODEC_FLAG_EMU_EDGE)) {
397  w += EDGE_WIDTH * 2;
398  h += EDGE_WIDTH * 2;
399  }
400 
401  do {
402  // NOTE: do not align linesizes individually, this breaks e.g. assumptions
403  // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
404  av_image_fill_linesizes(picture.linesize, avctx->pix_fmt, w);
405  // increase alignment of w for next try (rhs gives the lowest bit set in w)
406  w += w & ~(w - 1);
407 
408  unaligned = 0;
409  for (i = 0; i < 4; i++)
410  unaligned |= picture.linesize[i] % pool->stride_align[i];
411  } while (unaligned);
412 
413  tmpsize = av_image_fill_pointers(picture.data, avctx->pix_fmt, h,
414  NULL, picture.linesize);
415  if (tmpsize < 0)
416  return -1;
417 
418  for (i = 0; i < 3 && picture.data[i + 1]; i++)
419  size[i] = picture.data[i + 1] - picture.data[i];
420  size[i] = tmpsize - (picture.data[i] - picture.data[0]);
421 
422  for (i = 0; i < 4; i++) {
423  av_buffer_pool_uninit(&pool->pools[i]);
424  pool->linesize[i] = picture.linesize[i];
425  if (size[i]) {
426  pool->pools[i] = av_buffer_pool_init(size[i] + 16 + STRIDE_ALIGN - 1,
427  CONFIG_MEMORY_POISONING ?
428  NULL :
430  if (!pool->pools[i]) {
431  ret = AVERROR(ENOMEM);
432  goto fail;
433  }
434  }
435  }
436  pool->format = frame->format;
437  pool->width = frame->width;
438  pool->height = frame->height;
439 
440  break;
441  }
442  case AVMEDIA_TYPE_AUDIO: {
443  int ch = av_frame_get_channels(frame); //av_get_channel_layout_nb_channels(frame->channel_layout);
444  int planar = av_sample_fmt_is_planar(frame->format);
445  int planes = planar ? ch : 1;
446 
447  if (pool->format == frame->format && pool->planes == planes &&
448  pool->channels == ch && frame->nb_samples == pool->samples)
449  return 0;
450 
451  av_buffer_pool_uninit(&pool->pools[0]);
452  ret = av_samples_get_buffer_size(&pool->linesize[0], ch,
453  frame->nb_samples, frame->format, 0);
454  if (ret < 0)
455  goto fail;
456 
457  pool->pools[0] = av_buffer_pool_init(pool->linesize[0], NULL);
458  if (!pool->pools[0]) {
459  ret = AVERROR(ENOMEM);
460  goto fail;
461  }
462 
463  pool->format = frame->format;
464  pool->planes = planes;
465  pool->channels = ch;
466  pool->samples = frame->nb_samples;
467  break;
468  }
469  default: av_assert0(0);
470  }
471  return 0;
472 fail:
473  for (i = 0; i < 4; i++)
474  av_buffer_pool_uninit(&pool->pools[i]);
475  pool->format = -1;
476  pool->planes = pool->channels = pool->samples = 0;
477  pool->width = pool->height = 0;
478  return ret;
479 }
480 
482 {
483  FramePool *pool = avctx->internal->pool;
484  int planes = pool->planes;
485  int i;
486 
487  frame->linesize[0] = pool->linesize[0];
488 
489  if (planes > AV_NUM_DATA_POINTERS) {
490  frame->extended_data = av_mallocz(planes * sizeof(*frame->extended_data));
491  frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
492  frame->extended_buf = av_mallocz(frame->nb_extended_buf *
493  sizeof(*frame->extended_buf));
494  if (!frame->extended_data || !frame->extended_buf) {
495  av_freep(&frame->extended_data);
496  av_freep(&frame->extended_buf);
497  return AVERROR(ENOMEM);
498  }
499  } else {
500  frame->extended_data = frame->data;
501  av_assert0(frame->nb_extended_buf == 0);
502  }
503 
504  for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
505  frame->buf[i] = av_buffer_pool_get(pool->pools[0]);
506  if (!frame->buf[i])
507  goto fail;
508  frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
509  }
510  for (i = 0; i < frame->nb_extended_buf; i++) {
511  frame->extended_buf[i] = av_buffer_pool_get(pool->pools[0]);
512  if (!frame->extended_buf[i])
513  goto fail;
514  frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
515  }
516 
517  if (avctx->debug & FF_DEBUG_BUFFERS)
518  av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p", frame);
519 
520  return 0;
521 fail:
522  av_frame_unref(frame);
523  return AVERROR(ENOMEM);
524 }
525 
527 {
528  FramePool *pool = s->internal->pool;
529  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pic->format);
530  int pixel_size = desc->comp[0].step_minus1 + 1;
531  int h_chroma_shift, v_chroma_shift;
532  int i;
533 
534  if (pic->data[0] != NULL) {
535  av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
536  return -1;
537  }
538 
539  memset(pic->data, 0, sizeof(pic->data));
540  pic->extended_data = pic->data;
541 
542  av_pix_fmt_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
543 
544  for (i = 0; i < 4 && pool->pools[i]; i++) {
545  const int h_shift = i == 0 ? 0 : h_chroma_shift;
546  const int v_shift = i == 0 ? 0 : v_chroma_shift;
547 
548  pic->linesize[i] = pool->linesize[i];
549 
550  pic->buf[i] = av_buffer_pool_get(pool->pools[i]);
551  if (!pic->buf[i])
552  goto fail;
553 
554  // no edge if EDGE EMU or not planar YUV
555  if ((s->flags & CODEC_FLAG_EMU_EDGE) || !pool->pools[2])
556  pic->data[i] = pic->buf[i]->data;
557  else {
558  pic->data[i] = pic->buf[i]->data +
559  FFALIGN((pic->linesize[i] * EDGE_WIDTH >> v_shift) +
560  (pixel_size * EDGE_WIDTH >> h_shift), pool->stride_align[i]);
561  }
562  }
563  for (; i < AV_NUM_DATA_POINTERS; i++) {
564  pic->data[i] = NULL;
565  pic->linesize[i] = 0;
566  }
567  if (pic->data[1] && !pic->data[2])
568  avpriv_set_systematic_pal2((uint32_t *)pic->data[1], s->pix_fmt);
569 
570  if (s->debug & FF_DEBUG_BUFFERS)
571  av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p\n", pic);
572 
573  return 0;
574 fail:
575  av_frame_unref(pic);
576  return AVERROR(ENOMEM);
577 }
578 
579 void avpriv_color_frame(AVFrame *frame, const int c[4])
580 {
581  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
582  int p, y, x;
583 
585 
586  for (p = 0; p<desc->nb_components; p++) {
587  uint8_t *dst = frame->data[p];
588  int is_chroma = p == 1 || p == 2;
589  int bytes = is_chroma ? FF_CEIL_RSHIFT(frame->width, desc->log2_chroma_w) : frame->width;
590  int height = is_chroma ? FF_CEIL_RSHIFT(frame->height, desc->log2_chroma_h) : frame->height;
591  for (y = 0; y < height; y++) {
592  if (desc->comp[0].depth_minus1 >= 8) {
593  for (x = 0; x<bytes; x++)
594  ((uint16_t*)dst)[x] = c[p];
595  }else
596  memset(dst, c[p], bytes);
597  dst += frame->linesize[p];
598  }
599  }
600 }
601 
603 {
604  int ret;
605 
606  if ((ret = update_frame_pool(avctx, frame)) < 0)
607  return ret;
608 
609 #if FF_API_GET_BUFFER
610  frame->type = FF_BUFFER_TYPE_INTERNAL;
611 #endif
612 
613  switch (avctx->codec_type) {
614  case AVMEDIA_TYPE_VIDEO:
615  return video_get_buffer(avctx, frame);
616  case AVMEDIA_TYPE_AUDIO:
617  return audio_get_buffer(avctx, frame);
618  default:
619  return -1;
620  }
621 }
622 
624 {
625  if (avctx->pkt) {
626  frame->pkt_pts = avctx->pkt->pts;
627  av_frame_set_pkt_pos (frame, avctx->pkt->pos);
628  av_frame_set_pkt_duration(frame, avctx->pkt->duration);
629  av_frame_set_pkt_size (frame, avctx->pkt->size);
630  } else {
631  frame->pkt_pts = AV_NOPTS_VALUE;
632  av_frame_set_pkt_pos (frame, -1);
633  av_frame_set_pkt_duration(frame, 0);
634  av_frame_set_pkt_size (frame, -1);
635  }
636  frame->reordered_opaque = avctx->reordered_opaque;
637 
638  switch (avctx->codec->type) {
639  case AVMEDIA_TYPE_VIDEO:
640  frame->width = FFMAX(avctx->width, FF_CEIL_RSHIFT(avctx->coded_width, avctx->lowres));
641  frame->height = FFMAX(avctx->height, FF_CEIL_RSHIFT(avctx->coded_height, avctx->lowres));
642  if (frame->format < 0)
643  frame->format = avctx->pix_fmt;
644  if (!frame->sample_aspect_ratio.num)
646  break;
647  case AVMEDIA_TYPE_AUDIO:
648  if (!frame->sample_rate)
649  frame->sample_rate = avctx->sample_rate;
650  if (frame->format < 0)
651  frame->format = avctx->sample_fmt;
652  if (!frame->channel_layout) {
653  if (avctx->channel_layout) {
655  avctx->channels) {
656  av_log(avctx, AV_LOG_ERROR, "Inconsistent channel "
657  "configuration.\n");
658  return AVERROR(EINVAL);
659  }
660 
661  frame->channel_layout = avctx->channel_layout;
662  } else {
663  if (avctx->channels > FF_SANE_NB_CHANNELS) {
664  av_log(avctx, AV_LOG_ERROR, "Too many channels: %d.\n",
665  avctx->channels);
666  return AVERROR(ENOSYS);
667  }
668  }
669  }
670  av_frame_set_channels(frame, avctx->channels);
671  break;
672  }
673  return 0;
674 }
675 
676 #if FF_API_GET_BUFFER
677 int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
678 {
679  return avcodec_default_get_buffer2(avctx, frame, 0);
680 }
681 
682 typedef struct CompatReleaseBufPriv {
683  AVCodecContext avctx;
684  AVFrame frame;
685 } CompatReleaseBufPriv;
686 
687 static void compat_free_buffer(void *opaque, uint8_t *data)
688 {
689  CompatReleaseBufPriv *priv = opaque;
690  if (priv->avctx.release_buffer)
691  priv->avctx.release_buffer(&priv->avctx, &priv->frame);
692  av_freep(&priv);
693 }
694 
695 static void compat_release_buffer(void *opaque, uint8_t *data)
696 {
697  AVBufferRef *buf = opaque;
698  av_buffer_unref(&buf);
699 }
700 #endif
701 
703 {
704  int ret;
705 
706  if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
707  if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0 || avctx->pix_fmt<0) {
708  av_log(avctx, AV_LOG_ERROR, "video_get_buffer: image parameters invalid\n");
709  return AVERROR(EINVAL);
710  }
711  }
712  if ((ret = ff_init_buffer_info(avctx, frame)) < 0)
713  return ret;
714 
715 #if FF_API_GET_BUFFER
716  /*
717  * Wrap an old get_buffer()-allocated buffer in an bunch of AVBuffers.
718  * We wrap each plane in its own AVBuffer. Each of those has a reference to
719  * a dummy AVBuffer as its private data, unreffing it on free.
720  * When all the planes are freed, the dummy buffer's free callback calls
721  * release_buffer().
722  */
723  if (avctx->get_buffer) {
724  CompatReleaseBufPriv *priv = NULL;
725  AVBufferRef *dummy_buf = NULL;
726  int planes, i, ret;
727 
728  if (flags & AV_GET_BUFFER_FLAG_REF)
729  frame->reference = 1;
730 
731  ret = avctx->get_buffer(avctx, frame);
732  if (ret < 0)
733  return ret;
734 
735  /* return if the buffers are already set up
736  * this would happen e.g. when a custom get_buffer() calls
737  * avcodec_default_get_buffer
738  */
739  if (frame->buf[0])
740  goto end;
741 
742  priv = av_mallocz(sizeof(*priv));
743  if (!priv) {
744  ret = AVERROR(ENOMEM);
745  goto fail;
746  }
747  priv->avctx = *avctx;
748  priv->frame = *frame;
749 
750  dummy_buf = av_buffer_create(NULL, 0, compat_free_buffer, priv, 0);
751  if (!dummy_buf) {
752  ret = AVERROR(ENOMEM);
753  goto fail;
754  }
755 
756 #define WRAP_PLANE(ref_out, data, data_size) \
757 do { \
758  AVBufferRef *dummy_ref = av_buffer_ref(dummy_buf); \
759  if (!dummy_ref) { \
760  ret = AVERROR(ENOMEM); \
761  goto fail; \
762  } \
763  ref_out = av_buffer_create(data, data_size, compat_release_buffer, \
764  dummy_ref, 0); \
765  if (!ref_out) { \
766  av_frame_unref(frame); \
767  ret = AVERROR(ENOMEM); \
768  goto fail; \
769  } \
770 } while (0)
771 
772  if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
773  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
774 
775  planes = av_pix_fmt_count_planes(frame->format);
776  /* workaround for AVHWAccel plane count of 0, buf[0] is used as
777  check for allocated buffers: make libavcodec happy */
778  if (desc && desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
779  planes = 1;
780  if (!desc || planes <= 0) {
781  ret = AVERROR(EINVAL);
782  goto fail;
783  }
784 
785  for (i = 0; i < planes; i++) {
786  int v_shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
787  int plane_size = (frame->height >> v_shift) * frame->linesize[i];
788 
789  WRAP_PLANE(frame->buf[i], frame->data[i], plane_size);
790  }
791  } else {
792  int planar = av_sample_fmt_is_planar(frame->format);
793  planes = planar ? avctx->channels : 1;
794 
795  if (planes > FF_ARRAY_ELEMS(frame->buf)) {
796  frame->nb_extended_buf = planes - FF_ARRAY_ELEMS(frame->buf);
797  frame->extended_buf = av_malloc(sizeof(*frame->extended_buf) *
798  frame->nb_extended_buf);
799  if (!frame->extended_buf) {
800  ret = AVERROR(ENOMEM);
801  goto fail;
802  }
803  }
804 
805  for (i = 0; i < FFMIN(planes, FF_ARRAY_ELEMS(frame->buf)); i++)
806  WRAP_PLANE(frame->buf[i], frame->extended_data[i], frame->linesize[0]);
807 
808  for (i = 0; i < frame->nb_extended_buf; i++)
809  WRAP_PLANE(frame->extended_buf[i],
810  frame->extended_data[i + FF_ARRAY_ELEMS(frame->buf)],
811  frame->linesize[0]);
812  }
813 
814  av_buffer_unref(&dummy_buf);
815 
816 end:
817  frame->width = avctx->width;
818  frame->height = avctx->height;
819 
820  return 0;
821 
822 fail:
823  avctx->release_buffer(avctx, frame);
824  av_freep(&priv);
825  av_buffer_unref(&dummy_buf);
826  return ret;
827  }
828 #endif
829 
830  ret = avctx->get_buffer2(avctx, frame, flags);
831 
832  if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
833  frame->width = avctx->width;
834  frame->height = avctx->height;
835  }
836 
837  return ret;
838 }
839 
841 {
842  int ret = get_buffer_internal(avctx, frame, flags);
843  if (ret < 0)
844  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
845  return ret;
846 }
847 
849 {
850  AVFrame tmp;
851  int ret;
852 
854 
855  if (frame->data[0] && (frame->width != avctx->width || frame->height != avctx->height || frame->format != avctx->pix_fmt)) {
856  av_log(avctx, AV_LOG_WARNING, "Picture changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s in reget buffer()\n",
857  frame->width, frame->height, av_get_pix_fmt_name(frame->format), avctx->width, avctx->height, av_get_pix_fmt_name(avctx->pix_fmt));
858  av_frame_unref(frame);
859  }
860 
861  ff_init_buffer_info(avctx, frame);
862 
863  if (!frame->data[0])
864  return ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
865 
866  if (av_frame_is_writable(frame))
867  return 0;
868 
869  av_frame_move_ref(&tmp, frame);
870 
871  ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
872  if (ret < 0) {
873  av_frame_unref(&tmp);
874  return ret;
875  }
876 
877  av_image_copy(frame->data, frame->linesize, tmp.data, tmp.linesize,
878  frame->format, frame->width, frame->height);
879 
880  av_frame_unref(&tmp);
881 
882  return 0;
883 }
884 
886 {
887  int ret = reget_buffer_internal(avctx, frame);
888  if (ret < 0)
889  av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
890  return ret;
891 }
892 
893 #if FF_API_GET_BUFFER
894 void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic)
895 {
897 
898  av_frame_unref(pic);
899 }
900 
901 int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic)
902 {
903  av_assert0(0);
904  return AVERROR_BUG;
905 }
906 #endif
907 
908 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
909 {
910  int i;
911 
912  for (i = 0; i < count; i++) {
913  int r = func(c, (char *)arg + i * size);
914  if (ret)
915  ret[i] = r;
916  }
917  return 0;
918 }
919 
920 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
921 {
922  int i;
923 
924  for (i = 0; i < count; i++) {
925  int r = func(c, arg, i, 0);
926  if (ret)
927  ret[i] = r;
928  }
929  return 0;
930 }
931 
933 {
934  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
935  return desc->flags & AV_PIX_FMT_FLAG_HWACCEL;
936 }
937 
939 {
940  while (*fmt != AV_PIX_FMT_NONE && is_hwaccel_pix_fmt(*fmt))
941  ++fmt;
942  return fmt[0];
943 }
944 
946 {
947 #if LIBAVCODEC_VERSION_MAJOR >= 55
948  // extended_data should explicitly be freed when needed, this code is unsafe currently
949  // also this is not compatible to the <55 ABI/API
950  if (frame->extended_data != frame->data && 0)
951  av_freep(&frame->extended_data);
952 #endif
953 
954  memset(frame, 0, sizeof(AVFrame));
955 
956  frame->pts =
957  frame->pkt_dts =
958  frame->pkt_pts = AV_NOPTS_VALUE;
960  av_frame_set_pkt_duration (frame, 0);
961  av_frame_set_pkt_pos (frame, -1);
962  av_frame_set_pkt_size (frame, -1);
963  frame->key_frame = 1;
964  frame->sample_aspect_ratio = (AVRational) {0, 1 };
965  frame->format = -1; /* unknown */
966  frame->extended_data = frame->data;
967 }
968 
970 {
971  AVFrame *frame = av_malloc(sizeof(AVFrame));
972 
973  if (frame == NULL)
974  return NULL;
975 
976  frame->extended_data = NULL;
978 
979  return frame;
980 }
981 
983 {
984  AVFrame *f;
985 
986  if (!frame || !*frame)
987  return;
988 
989  f = *frame;
990 
991  if (f->extended_data != f->data)
992  av_freep(&f->extended_data);
993 
994  av_freep(frame);
995 }
996 
997 #define MAKE_ACCESSORS(str, name, type, field) \
998  type av_##name##_get_##field(const str *s) { return s->field; } \
999  void av_##name##_set_##field(str *s, type v) { s->field = v; }
1000 
1001 MAKE_ACCESSORS(AVCodecContext, codec, AVRational, pkt_timebase)
1002 MAKE_ACCESSORS(AVCodecContext, codec, const AVCodecDescriptor *, codec_descriptor)
1003 MAKE_ACCESSORS(AVCodecContext, codec, int, lowres)
1004 
1006 {
1007  memset(sub, 0, sizeof(*sub));
1008  sub->pts = AV_NOPTS_VALUE;
1009 }
1010 
1012 {
1013  int bit_rate;
1014  int bits_per_sample;
1015 
1016  switch (ctx->codec_type) {
1017  case AVMEDIA_TYPE_VIDEO:
1018  case AVMEDIA_TYPE_DATA:
1019  case AVMEDIA_TYPE_SUBTITLE:
1021  bit_rate = ctx->bit_rate;
1022  break;
1023  case AVMEDIA_TYPE_AUDIO:
1024  bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
1025  bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
1026  break;
1027  default:
1028  bit_rate = 0;
1029  break;
1030  }
1031  return bit_rate;
1032 }
1033 
1034 #if FF_API_AVCODEC_OPEN
1035 int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
1036 {
1037  return avcodec_open2(avctx, codec, NULL);
1038 }
1039 #endif
1040 
1042 {
1043  int ret = 0;
1044 
1046 
1047  ret = avcodec_open2(avctx, codec, options);
1048 
1049  ff_lock_avcodec(avctx);
1050  return ret;
1051 }
1052 
1054 {
1055  int ret = 0;
1056  AVDictionary *tmp = NULL;
1057 
1058  if (avcodec_is_open(avctx))
1059  return 0;
1060 
1061  if ((!codec && !avctx->codec)) {
1062  av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2()\n");
1063  return AVERROR(EINVAL);
1064  }
1065  if ((codec && avctx->codec && codec != avctx->codec)) {
1066  av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
1067  "but %s passed to avcodec_open2()\n", avctx->codec->name, codec->name);
1068  return AVERROR(EINVAL);
1069  }
1070  if (!codec)
1071  codec = avctx->codec;
1072 
1073  if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
1074  return AVERROR(EINVAL);
1075 
1076  if (options)
1077  av_dict_copy(&tmp, *options, 0);
1078 
1079  ret = ff_lock_avcodec(avctx);
1080  if (ret < 0)
1081  return ret;
1082 
1083  avctx->internal = av_mallocz(sizeof(AVCodecInternal));
1084  if (!avctx->internal) {
1085  ret = AVERROR(ENOMEM);
1086  goto end;
1087  }
1088 
1089  avctx->internal->pool = av_mallocz(sizeof(*avctx->internal->pool));
1090  if (!avctx->internal->pool) {
1091  ret = AVERROR(ENOMEM);
1092  goto free_and_end;
1093  }
1094 
1095  if (codec->priv_data_size > 0) {
1096  if (!avctx->priv_data) {
1097  avctx->priv_data = av_mallocz(codec->priv_data_size);
1098  if (!avctx->priv_data) {
1099  ret = AVERROR(ENOMEM);
1100  goto end;
1101  }
1102  if (codec->priv_class) {
1103  *(const AVClass **)avctx->priv_data = codec->priv_class;
1105  }
1106  }
1107  if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
1108  goto free_and_end;
1109  } else {
1110  avctx->priv_data = NULL;
1111  }
1112  if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
1113  goto free_and_end;
1114 
1115  // only call avcodec_set_dimensions() for non H.264/VP6F codecs so as not to overwrite previously setup dimensions
1116  if (!(avctx->coded_width && avctx->coded_height && avctx->width && avctx->height &&
1117  (avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_VP6F))) {
1118  if (avctx->coded_width && avctx->coded_height)
1119  avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
1120  else if (avctx->width && avctx->height)
1121  avcodec_set_dimensions(avctx, avctx->width, avctx->height);
1122  }
1123 
1124  if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
1125  && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
1126  || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
1127  av_log(avctx, AV_LOG_WARNING, "Ignoring invalid width/height values\n");
1128  avcodec_set_dimensions(avctx, 0, 0);
1129  }
1130 
1131  /* if the decoder init function was already called previously,
1132  * free the already allocated subtitle_header before overwriting it */
1133  if (av_codec_is_decoder(codec))
1134  av_freep(&avctx->subtitle_header);
1135 
1136  if (avctx->channels > FF_SANE_NB_CHANNELS) {
1137  ret = AVERROR(EINVAL);
1138  goto free_and_end;
1139  }
1140 
1141  avctx->codec = codec;
1142  if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
1143  avctx->codec_id == AV_CODEC_ID_NONE) {
1144  avctx->codec_type = codec->type;
1145  avctx->codec_id = codec->id;
1146  }
1147  if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
1148  && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
1149  av_log(avctx, AV_LOG_ERROR, "Codec type or id mismatches\n");
1150  ret = AVERROR(EINVAL);
1151  goto free_and_end;
1152  }
1153  avctx->frame_number = 0;
1155 
1156  if (avctx->codec->capabilities & CODEC_CAP_EXPERIMENTAL &&
1158  const char *codec_string = av_codec_is_encoder(codec) ? "encoder" : "decoder";
1159  AVCodec *codec2;
1160  av_log(avctx, AV_LOG_ERROR,
1161  "The %s '%s' is experimental but experimental codecs are not enabled, "
1162  "add '-strict %d' if you want to use it.\n",
1163  codec_string, codec->name, FF_COMPLIANCE_EXPERIMENTAL);
1164  codec2 = av_codec_is_encoder(codec) ? avcodec_find_encoder(codec->id) : avcodec_find_decoder(codec->id);
1165  if (!(codec2->capabilities & CODEC_CAP_EXPERIMENTAL))
1166  av_log(avctx, AV_LOG_ERROR, "Alternatively use the non experimental %s '%s'.\n",
1167  codec_string, codec2->name);
1168  ret = AVERROR_EXPERIMENTAL;
1169  goto free_and_end;
1170  }
1171 
1172  if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
1173  (!avctx->time_base.num || !avctx->time_base.den)) {
1174  avctx->time_base.num = 1;
1175  avctx->time_base.den = avctx->sample_rate;
1176  }
1177 
1178  if (!HAVE_THREADS)
1179  av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
1180 
1181  if (CONFIG_FRAME_THREAD_ENCODER) {
1182  ff_unlock_avcodec(); //we will instanciate a few encoders thus kick the counter to prevent false detection of a problem
1183  ret = ff_frame_thread_encoder_init(avctx, options ? *options : NULL);
1184  ff_lock_avcodec(avctx);
1185  if (ret < 0)
1186  goto free_and_end;
1187  }
1188 
1189  if (HAVE_THREADS
1191  ret = ff_thread_init(avctx);
1192  if (ret < 0) {
1193  goto free_and_end;
1194  }
1195  }
1196  if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
1197  avctx->thread_count = 1;
1198 
1199  if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
1200  av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
1201  avctx->codec->max_lowres);
1202  ret = AVERROR(EINVAL);
1203  goto free_and_end;
1204  }
1205 
1206  if (av_codec_is_encoder(avctx->codec)) {
1207  int i;
1208  if (avctx->codec->sample_fmts) {
1209  for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
1210  if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
1211  break;
1212  if (avctx->channels == 1 &&
1215  avctx->sample_fmt = avctx->codec->sample_fmts[i];
1216  break;
1217  }
1218  }
1219  if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
1220  char buf[128];
1221  snprintf(buf, sizeof(buf), "%d", avctx->sample_fmt);
1222  av_log(avctx, AV_LOG_ERROR, "Specified sample format %s is invalid or not supported\n",
1223  (char *)av_x_if_null(av_get_sample_fmt_name(avctx->sample_fmt), buf));
1224  ret = AVERROR(EINVAL);
1225  goto free_and_end;
1226  }
1227  }
1228  if (avctx->codec->pix_fmts) {
1229  for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
1230  if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
1231  break;
1232  if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE
1233  && !((avctx->codec_id == AV_CODEC_ID_MJPEG || avctx->codec_id == AV_CODEC_ID_LJPEG)
1235  char buf[128];
1236  snprintf(buf, sizeof(buf), "%d", avctx->pix_fmt);
1237  av_log(avctx, AV_LOG_ERROR, "Specified pixel format %s is invalid or not supported\n",
1238  (char *)av_x_if_null(av_get_pix_fmt_name(avctx->pix_fmt), buf));
1239  ret = AVERROR(EINVAL);
1240  goto free_and_end;
1241  }
1242  }
1243  if (avctx->codec->supported_samplerates) {
1244  for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
1245  if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
1246  break;
1247  if (avctx->codec->supported_samplerates[i] == 0) {
1248  av_log(avctx, AV_LOG_ERROR, "Specified sample rate %d is not supported\n",
1249  avctx->sample_rate);
1250  ret = AVERROR(EINVAL);
1251  goto free_and_end;
1252  }
1253  }
1254  if (avctx->codec->channel_layouts) {
1255  if (!avctx->channel_layout) {
1256  av_log(avctx, AV_LOG_WARNING, "Channel layout not specified\n");
1257  } else {
1258  for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
1259  if (avctx->channel_layout == avctx->codec->channel_layouts[i])
1260  break;
1261  if (avctx->codec->channel_layouts[i] == 0) {
1262  char buf[512];
1263  av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
1264  av_log(avctx, AV_LOG_ERROR, "Specified channel layout '%s' is not supported\n", buf);
1265  ret = AVERROR(EINVAL);
1266  goto free_and_end;
1267  }
1268  }
1269  }
1270  if (avctx->channel_layout && avctx->channels) {
1271  int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
1272  if (channels != avctx->channels) {
1273  char buf[512];
1274  av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
1275  av_log(avctx, AV_LOG_ERROR,
1276  "Channel layout '%s' with %d channels does not match number of specified channels %d\n",
1277  buf, channels, avctx->channels);
1278  ret = AVERROR(EINVAL);
1279  goto free_and_end;
1280  }
1281  } else if (avctx->channel_layout) {
1283  }
1284  if(avctx->codec_type == AVMEDIA_TYPE_VIDEO &&
1285  avctx->codec_id != AV_CODEC_ID_PNG // For mplayer
1286  ) {
1287  if (avctx->width <= 0 || avctx->height <= 0) {
1288  av_log(avctx, AV_LOG_ERROR, "dimensions not set\n");
1289  ret = AVERROR(EINVAL);
1290  goto free_and_end;
1291  }
1292  }
1293  if ( (avctx->codec_type == AVMEDIA_TYPE_VIDEO || avctx->codec_type == AVMEDIA_TYPE_AUDIO)
1294  && avctx->bit_rate>0 && avctx->bit_rate<1000) {
1295  av_log(avctx, AV_LOG_WARNING, "Bitrate %d is extremely low, maybe you mean %dk\n", avctx->bit_rate, avctx->bit_rate);
1296  }
1297 
1298  if (!avctx->rc_initial_buffer_occupancy)
1299  avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3 / 4;
1300  }
1301 
1303  avctx->pts_correction_num_faulty_dts = 0;
1304  avctx->pts_correction_last_pts =
1305  avctx->pts_correction_last_dts = INT64_MIN;
1306 
1307  if ( avctx->codec->init && (!(avctx->active_thread_type&FF_THREAD_FRAME)
1308  || avctx->internal->frame_thread_encoder)) {
1309  ret = avctx->codec->init(avctx);
1310  if (ret < 0) {
1311  goto free_and_end;
1312  }
1313  }
1314 
1315  ret=0;
1316 
1317  if (av_codec_is_decoder(avctx->codec)) {
1318  if (!avctx->bit_rate)
1319  avctx->bit_rate = get_bit_rate(avctx);
1320  /* validate channel layout from the decoder */
1321  if (avctx->channel_layout) {
1322  int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
1323  if (!avctx->channels)
1324  avctx->channels = channels;
1325  else if (channels != avctx->channels) {
1326  char buf[512];
1327  av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
1328  av_log(avctx, AV_LOG_WARNING,
1329  "Channel layout '%s' with %d channels does not match specified number of channels %d: "
1330  "ignoring specified channel layout\n",
1331  buf, channels, avctx->channels);
1332  avctx->channel_layout = 0;
1333  }
1334  }
1335  if (avctx->channels && avctx->channels < 0 ||
1336  avctx->channels > FF_SANE_NB_CHANNELS) {
1337  ret = AVERROR(EINVAL);
1338  goto free_and_end;
1339  }
1340  if (avctx->sub_charenc) {
1341  if (avctx->codec_type != AVMEDIA_TYPE_SUBTITLE) {
1342  av_log(avctx, AV_LOG_ERROR, "Character encoding is only "
1343  "supported with subtitles codecs\n");
1344  ret = AVERROR(EINVAL);
1345  goto free_and_end;
1346  } else if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB) {
1347  av_log(avctx, AV_LOG_WARNING, "Codec '%s' is bitmap-based, "
1348  "subtitles character encoding will be ignored\n",
1349  avctx->codec_descriptor->name);
1351  } else {
1352  /* input character encoding is set for a text based subtitle
1353  * codec at this point */
1356 
1358 #if CONFIG_ICONV
1359  iconv_t cd = iconv_open("UTF-8", avctx->sub_charenc);
1360  if (cd == (iconv_t)-1) {
1361  av_log(avctx, AV_LOG_ERROR, "Unable to open iconv context "
1362  "with input character encoding \"%s\"\n", avctx->sub_charenc);
1363  ret = AVERROR(errno);
1364  goto free_and_end;
1365  }
1366  iconv_close(cd);
1367 #else
1368  av_log(avctx, AV_LOG_ERROR, "Character encoding subtitles "
1369  "conversion needs a libavcodec built with iconv support "
1370  "for this codec\n");
1371  ret = AVERROR(ENOSYS);
1372  goto free_and_end;
1373 #endif
1374  }
1375  }
1376  }
1377  }
1378 end:
1380  if (options) {
1381  av_dict_free(options);
1382  *options = tmp;
1383  }
1384 
1385  return ret;
1386 free_and_end:
1387  av_dict_free(&tmp);
1388  av_freep(&avctx->priv_data);
1389  if (avctx->internal)
1390  av_freep(&avctx->internal->pool);
1391  av_freep(&avctx->internal);
1392  avctx->codec = NULL;
1393  goto end;
1394 }
1395 
1397 {
1398  if (avpkt->size < 0) {
1399  av_log(avctx, AV_LOG_ERROR, "Invalid negative user packet size %d\n", avpkt->size);
1400  return AVERROR(EINVAL);
1401  }
1402  if (size < 0 || size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
1403  av_log(avctx, AV_LOG_ERROR, "Invalid minimum required packet size %d (max allowed is %d)\n",
1404  size, INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE);
1405  return AVERROR(EINVAL);
1406  }
1407 
1408  if (avctx) {
1409  av_assert0(!avpkt->data || avpkt->data != avctx->internal->byte_buffer);
1410  if (!avpkt->data || avpkt->size < size) {
1412  avpkt->data = avctx->internal->byte_buffer;
1413  avpkt->size = avctx->internal->byte_buffer_size;
1414  avpkt->destruct = NULL;
1415  }
1416  }
1417 
1418  if (avpkt->data) {
1419  AVBufferRef *buf = avpkt->buf;
1420 #if FF_API_DESTRUCT_PACKET
1421  void *destruct = avpkt->destruct;
1422 #endif
1423 
1424  if (avpkt->size < size) {
1425  av_log(avctx, AV_LOG_ERROR, "User packet is too small (%d < %d)\n", avpkt->size, size);
1426  return AVERROR(EINVAL);
1427  }
1428 
1429  av_init_packet(avpkt);
1430 #if FF_API_DESTRUCT_PACKET
1431  avpkt->destruct = destruct;
1432 #endif
1433  avpkt->buf = buf;
1434  avpkt->size = size;
1435  return 0;
1436  } else {
1437  int ret = av_new_packet(avpkt, size);
1438  if (ret < 0)
1439  av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %d\n", size);
1440  return ret;
1441  }
1442 }
1443 
1445 {
1446  return ff_alloc_packet2(NULL, avpkt, size);
1447 }
1448 
1449 /**
1450  * Pad last frame with silence.
1451  */
1452 static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
1453 {
1454  AVFrame *frame = NULL;
1455  int ret;
1456 
1457  if (!(frame = avcodec_alloc_frame()))
1458  return AVERROR(ENOMEM);
1459 
1460  frame->format = src->format;
1461  frame->channel_layout = src->channel_layout;
1463  frame->nb_samples = s->frame_size;
1464  ret = av_frame_get_buffer(frame, 32);
1465  if (ret < 0)
1466  goto fail;
1467 
1468  ret = av_frame_copy_props(frame, src);
1469  if (ret < 0)
1470  goto fail;
1471 
1472  if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
1473  src->nb_samples, s->channels, s->sample_fmt)) < 0)
1474  goto fail;
1475  if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
1476  frame->nb_samples - src->nb_samples,
1477  s->channels, s->sample_fmt)) < 0)
1478  goto fail;
1479 
1480  *dst = frame;
1481 
1482  return 0;
1483 
1484 fail:
1485  av_frame_free(&frame);
1486  return ret;
1487 }
1488 
1490  AVPacket *avpkt,
1491  const AVFrame *frame,
1492  int *got_packet_ptr)
1493 {
1494  AVFrame tmp;
1495  AVFrame *padded_frame = NULL;
1496  int ret;
1497  AVPacket user_pkt = *avpkt;
1498  int needs_realloc = !user_pkt.data;
1499 
1500  *got_packet_ptr = 0;
1501 
1502  if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
1503  av_free_packet(avpkt);
1504  av_init_packet(avpkt);
1505  return 0;
1506  }
1507 
1508  /* ensure that extended_data is properly set */
1509  if (frame && !frame->extended_data) {
1510  if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
1511  avctx->channels > AV_NUM_DATA_POINTERS) {
1512  av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
1513  "with more than %d channels, but extended_data is not set.\n",
1515  return AVERROR(EINVAL);
1516  }
1517  av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
1518 
1519  tmp = *frame;
1520  tmp.extended_data = tmp.data;
1521  frame = &tmp;
1522  }
1523 
1524  /* check for valid frame size */
1525  if (frame) {
1527  if (frame->nb_samples > avctx->frame_size) {
1528  av_log(avctx, AV_LOG_ERROR, "more samples than frame size (avcodec_encode_audio2)\n");
1529  return AVERROR(EINVAL);
1530  }
1531  } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
1532  if (frame->nb_samples < avctx->frame_size &&
1533  !avctx->internal->last_audio_frame) {
1534  ret = pad_last_frame(avctx, &padded_frame, frame);
1535  if (ret < 0)
1536  return ret;
1537 
1538  frame = padded_frame;
1539  avctx->internal->last_audio_frame = 1;
1540  }
1541 
1542  if (frame->nb_samples != avctx->frame_size) {
1543  av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) != frame_size (%d) (avcodec_encode_audio2)\n", frame->nb_samples, avctx->frame_size);
1544  ret = AVERROR(EINVAL);
1545  goto end;
1546  }
1547  }
1548  }
1549 
1550  ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1551  if (!ret) {
1552  if (*got_packet_ptr) {
1553  if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
1554  if (avpkt->pts == AV_NOPTS_VALUE)
1555  avpkt->pts = frame->pts;
1556  if (!avpkt->duration)
1557  avpkt->duration = ff_samples_to_time_base(avctx,
1558  frame->nb_samples);
1559  }
1560  avpkt->dts = avpkt->pts;
1561  } else {
1562  avpkt->size = 0;
1563  }
1564  }
1565  if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
1566  needs_realloc = 0;
1567  if (user_pkt.data) {
1568  if (user_pkt.size >= avpkt->size) {
1569  memcpy(user_pkt.data, avpkt->data, avpkt->size);
1570  } else {
1571  av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
1572  avpkt->size = user_pkt.size;
1573  ret = -1;
1574  }
1575  avpkt->buf = user_pkt.buf;
1576  avpkt->data = user_pkt.data;
1577  avpkt->destruct = user_pkt.destruct;
1578  } else {
1579  if (av_dup_packet(avpkt) < 0) {
1580  ret = AVERROR(ENOMEM);
1581  }
1582  }
1583  }
1584 
1585  if (!ret) {
1586  if (needs_realloc && avpkt->data) {
1587  ret = av_buffer_realloc(&avpkt->buf, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
1588  if (ret >= 0)
1589  avpkt->data = avpkt->buf->data;
1590  }
1591 
1592  avctx->frame_number++;
1593  }
1594 
1595  if (ret < 0 || !*got_packet_ptr) {
1596  av_free_packet(avpkt);
1597  av_init_packet(avpkt);
1598  goto end;
1599  }
1600 
1601  /* NOTE: if we add any audio encoders which output non-keyframe packets,
1602  * this needs to be moved to the encoders, but for now we can do it
1603  * here to simplify things */
1604  avpkt->flags |= AV_PKT_FLAG_KEY;
1605 
1606 end:
1607  av_frame_free(&padded_frame);
1608 
1609  return ret;
1610 }
1611 
1612 #if FF_API_OLD_ENCODE_AUDIO
1613 int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
1614  uint8_t *buf, int buf_size,
1615  const short *samples)
1616 {
1617  AVPacket pkt;
1618  AVFrame frame0 = { { 0 } };
1619  AVFrame *frame;
1620  int ret, samples_size, got_packet;
1621 
1622  av_init_packet(&pkt);
1623  pkt.data = buf;
1624  pkt.size = buf_size;
1625 
1626  if (samples) {
1627  frame = &frame0;
1629 
1630  if (avctx->frame_size) {
1631  frame->nb_samples = avctx->frame_size;
1632  } else {
1633  /* if frame_size is not set, the number of samples must be
1634  * calculated from the buffer size */
1635  int64_t nb_samples;
1636  if (!av_get_bits_per_sample(avctx->codec_id)) {
1637  av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
1638  "support this codec\n");
1639  return AVERROR(EINVAL);
1640  }
1641  nb_samples = (int64_t)buf_size * 8 /
1642  (av_get_bits_per_sample(avctx->codec_id) *
1643  avctx->channels);
1644  if (nb_samples >= INT_MAX)
1645  return AVERROR(EINVAL);
1646  frame->nb_samples = nb_samples;
1647  }
1648 
1649  /* it is assumed that the samples buffer is large enough based on the
1650  * relevant parameters */
1651  samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
1652  frame->nb_samples,
1653  avctx->sample_fmt, 1);
1654  if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
1655  avctx->sample_fmt,
1656  (const uint8_t *)samples,
1657  samples_size, 1)) < 0)
1658  return ret;
1659 
1660  /* fabricate frame pts from sample count.
1661  * this is needed because the avcodec_encode_audio() API does not have
1662  * a way for the user to provide pts */
1663  if (avctx->sample_rate && avctx->time_base.num)
1664  frame->pts = ff_samples_to_time_base(avctx,
1665  avctx->internal->sample_count);
1666  else
1667  frame->pts = AV_NOPTS_VALUE;
1668  avctx->internal->sample_count += frame->nb_samples;
1669  } else {
1670  frame = NULL;
1671  }
1672 
1673  got_packet = 0;
1674  ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
1675  if (!ret && got_packet && avctx->coded_frame) {
1676  avctx->coded_frame->pts = pkt.pts;
1677  avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
1678  }
1679  /* free any side data since we cannot return it */
1681 
1682  if (frame && frame->extended_data != frame->data)
1683  av_freep(&frame->extended_data);
1684 
1685  return ret ? ret : pkt.size;
1686 }
1687 
1688 #endif
1689 
1690 #if FF_API_OLD_ENCODE_VIDEO
1691 int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
1692  const AVFrame *pict)
1693 {
1694  AVPacket pkt;
1695  int ret, got_packet = 0;
1696 
1697  if (buf_size < FF_MIN_BUFFER_SIZE) {
1698  av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
1699  return -1;
1700  }
1701 
1702  av_init_packet(&pkt);
1703  pkt.data = buf;
1704  pkt.size = buf_size;
1705 
1706  ret = avcodec_encode_video2(avctx, &pkt, pict, &got_packet);
1707  if (!ret && got_packet && avctx->coded_frame) {
1708  avctx->coded_frame->pts = pkt.pts;
1709  avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
1710  }
1711 
1712  /* free any side data since we cannot return it */
1713  if (pkt.side_data_elems > 0) {
1714  int i;
1715  for (i = 0; i < pkt.side_data_elems; i++)
1716  av_free(pkt.side_data[i].data);
1717  av_freep(&pkt.side_data);
1718  pkt.side_data_elems = 0;
1719  }
1720 
1721  return ret ? ret : pkt.size;
1722 }
1723 
1724 #endif
1725 
1727  AVPacket *avpkt,
1728  const AVFrame *frame,
1729  int *got_packet_ptr)
1730 {
1731  int ret;
1732  AVPacket user_pkt = *avpkt;
1733  int needs_realloc = !user_pkt.data;
1734 
1735  *got_packet_ptr = 0;
1736 
1737  if(CONFIG_FRAME_THREAD_ENCODER &&
1739  return ff_thread_video_encode_frame(avctx, avpkt, frame, got_packet_ptr);
1740 
1741  if ((avctx->flags&CODEC_FLAG_PASS1) && avctx->stats_out)
1742  avctx->stats_out[0] = '\0';
1743 
1744  if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
1745  av_free_packet(avpkt);
1746  av_init_packet(avpkt);
1747  avpkt->size = 0;
1748  return 0;
1749  }
1750 
1751  if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
1752  return AVERROR(EINVAL);
1753 
1754  av_assert0(avctx->codec->encode2);
1755 
1756  ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1757  av_assert0(ret <= 0);
1758 
1759  if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
1760  needs_realloc = 0;
1761  if (user_pkt.data) {
1762  if (user_pkt.size >= avpkt->size) {
1763  memcpy(user_pkt.data, avpkt->data, avpkt->size);
1764  } else {
1765  av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
1766  avpkt->size = user_pkt.size;
1767  ret = -1;
1768  }
1769  avpkt->buf = user_pkt.buf;
1770  avpkt->data = user_pkt.data;
1771  avpkt->destruct = user_pkt.destruct;
1772  } else {
1773  if (av_dup_packet(avpkt) < 0) {
1774  ret = AVERROR(ENOMEM);
1775  }
1776  }
1777  }
1778 
1779  if (!ret) {
1780  if (!*got_packet_ptr)
1781  avpkt->size = 0;
1782  else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
1783  avpkt->pts = avpkt->dts = frame->pts;
1784 
1785  if (needs_realloc && avpkt->data) {
1786  ret = av_buffer_realloc(&avpkt->buf, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
1787  if (ret >= 0)
1788  avpkt->data = avpkt->buf->data;
1789  }
1790 
1791  avctx->frame_number++;
1792  }
1793 
1794  if (ret < 0 || !*got_packet_ptr)
1795  av_free_packet(avpkt);
1796  else
1798 
1799  emms_c();
1800  return ret;
1801 }
1802 
1803 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
1804  const AVSubtitle *sub)
1805 {
1806  int ret;
1807  if (sub->start_display_time) {
1808  av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
1809  return -1;
1810  }
1811 
1812  ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
1813  avctx->frame_number++;
1814  return ret;
1815 }
1816 
1817 /**
1818  * Attempt to guess proper monotonic timestamps for decoded video frames
1819  * which might have incorrect times. Input timestamps may wrap around, in
1820  * which case the output will as well.
1821  *
1822  * @param pts the pts field of the decoded AVPacket, as passed through
1823  * AVFrame.pkt_pts
1824  * @param dts the dts field of the decoded AVPacket
1825  * @return one of the input values, may be AV_NOPTS_VALUE
1826  */
1827 static int64_t guess_correct_pts(AVCodecContext *ctx,
1828  int64_t reordered_pts, int64_t dts)
1829 {
1830  int64_t pts = AV_NOPTS_VALUE;
1831 
1832  if (dts != AV_NOPTS_VALUE) {
1834  ctx->pts_correction_last_dts = dts;
1835  }
1836  if (reordered_pts != AV_NOPTS_VALUE) {
1837  ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
1838  ctx->pts_correction_last_pts = reordered_pts;
1839  }
1841  && reordered_pts != AV_NOPTS_VALUE)
1842  pts = reordered_pts;
1843  else
1844  pts = dts;
1845 
1846  return pts;
1847 }
1848 
1849 static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
1850 {
1851  int size = 0;
1852  const uint8_t *data;
1853  uint32_t flags;
1854 
1855  if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
1856  return;
1857 
1858  data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
1859  if (!data || size < 4)
1860  return;
1861  flags = bytestream_get_le32(&data);
1862  size -= 4;
1863  if (size < 4) /* Required for any of the changes */
1864  return;
1866  avctx->channels = bytestream_get_le32(&data);
1867  size -= 4;
1868  }
1870  if (size < 8)
1871  return;
1872  avctx->channel_layout = bytestream_get_le64(&data);
1873  size -= 8;
1874  }
1875  if (size < 4)
1876  return;
1878  avctx->sample_rate = bytestream_get_le32(&data);
1879  size -= 4;
1880  }
1882  if (size < 8)
1883  return;
1884  avctx->width = bytestream_get_le32(&data);
1885  avctx->height = bytestream_get_le32(&data);
1886  avcodec_set_dimensions(avctx, avctx->width, avctx->height);
1887  size -= 8;
1888  }
1889 }
1890 
1892 {
1893  int size, ret = 0;
1894  const uint8_t *side_metadata;
1895  const uint8_t *end;
1896 
1897  side_metadata = av_packet_get_side_data(avctx->pkt,
1899  if (!side_metadata)
1900  goto end;
1901  end = side_metadata + size;
1902  while (side_metadata < end) {
1903  const uint8_t *key = side_metadata;
1904  const uint8_t *val = side_metadata + strlen(key) + 1;
1905  int ret = av_dict_set(avpriv_frame_get_metadatap(frame), key, val, 0);
1906  if (ret < 0)
1907  break;
1908  side_metadata = val + strlen(val) + 1;
1909  }
1910 end:
1911  return ret;
1912 }
1913 
1915  int *got_picture_ptr,
1916  const AVPacket *avpkt)
1917 {
1918  AVCodecInternal *avci = avctx->internal;
1919  int ret;
1920  // copy to ensure we do not change avpkt
1921  AVPacket tmp = *avpkt;
1922 
1923  if (!avctx->codec)
1924  return AVERROR(EINVAL);
1925  if (avctx->codec->type != AVMEDIA_TYPE_VIDEO) {
1926  av_log(avctx, AV_LOG_ERROR, "Invalid media type for video\n");
1927  return AVERROR(EINVAL);
1928  }
1929 
1930  *got_picture_ptr = 0;
1931  if ((avctx->coded_width || avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
1932  return AVERROR(EINVAL);
1933 
1934  avcodec_get_frame_defaults(picture);
1935 
1936  if (!avctx->refcounted_frames)
1937  av_frame_unref(&avci->to_free);
1938 
1939  if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
1940  int did_split = av_packet_split_side_data(&tmp);
1941  apply_param_change(avctx, &tmp);
1942  avctx->pkt = &tmp;
1943  if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
1944  ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
1945  &tmp);
1946  else {
1947  ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
1948  &tmp);
1949  picture->pkt_dts = avpkt->dts;
1950 
1951  if(!avctx->has_b_frames){
1952  av_frame_set_pkt_pos(picture, avpkt->pos);
1953  }
1954  //FIXME these should be under if(!avctx->has_b_frames)
1955  /* get_buffer is supposed to set frame parameters */
1956  if (!(avctx->codec->capabilities & CODEC_CAP_DR1)) {
1957  if (!picture->sample_aspect_ratio.num) picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
1958  if (!picture->width) picture->width = avctx->width;
1959  if (!picture->height) picture->height = avctx->height;
1960  if (picture->format == AV_PIX_FMT_NONE) picture->format = avctx->pix_fmt;
1961  }
1962  }
1963  add_metadata_from_side_data(avctx, picture);
1964 
1965  emms_c(); //needed to avoid an emms_c() call before every return;
1966 
1967  avctx->pkt = NULL;
1968  if (did_split) {
1970  if(ret == tmp.size)
1971  ret = avpkt->size;
1972  }
1973 
1974  if (ret < 0 && picture->data[0])
1975  av_frame_unref(picture);
1976 
1977  if (*got_picture_ptr) {
1978  if (!avctx->refcounted_frames) {
1979  avci->to_free = *picture;
1980  avci->to_free.extended_data = avci->to_free.data;
1981  memset(picture->buf, 0, sizeof(picture->buf));
1982  }
1983 
1984  avctx->frame_number++;
1986  guess_correct_pts(avctx,
1987  picture->pkt_pts,
1988  picture->pkt_dts));
1989  }
1990  } else
1991  ret = 0;
1992 
1993  /* many decoders assign whole AVFrames, thus overwriting extended_data;
1994  * make sure it's set correctly */
1995  picture->extended_data = picture->data;
1996 
1997  return ret;
1998 }
1999 
2000 #if FF_API_OLD_DECODE_AUDIO
2001 int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
2002  int *frame_size_ptr,
2003  AVPacket *avpkt)
2004 {
2005  AVFrame frame = { { 0 } };
2006  int ret, got_frame = 0;
2007 
2008  if (avctx->get_buffer != avcodec_default_get_buffer) {
2009  av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
2010  "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
2011  av_log(avctx, AV_LOG_ERROR, "Please port your application to "
2012  "avcodec_decode_audio4()\n");
2013  avctx->get_buffer = avcodec_default_get_buffer;
2014  avctx->release_buffer = avcodec_default_release_buffer;
2015  }
2016 
2017  ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
2018 
2019  if (ret >= 0 && got_frame) {
2020  int ch, plane_size;
2021  int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
2022  int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
2023  frame.nb_samples,
2024  avctx->sample_fmt, 1);
2025  if (*frame_size_ptr < data_size) {
2026  av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
2027  "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
2028  return AVERROR(EINVAL);
2029  }
2030 
2031  memcpy(samples, frame.extended_data[0], plane_size);
2032 
2033  if (planar && avctx->channels > 1) {
2034  uint8_t *out = ((uint8_t *)samples) + plane_size;
2035  for (ch = 1; ch < avctx->channels; ch++) {
2036  memcpy(out, frame.extended_data[ch], plane_size);
2037  out += plane_size;
2038  }
2039  }
2040  *frame_size_ptr = data_size;
2041  } else {
2042  *frame_size_ptr = 0;
2043  }
2044  return ret;
2045 }
2046 
2047 #endif
2048 
2050  AVFrame *frame,
2051  int *got_frame_ptr,
2052  const AVPacket *avpkt)
2053 {
2054  AVCodecInternal *avci = avctx->internal;
2055  int planar, channels;
2056  int ret = 0;
2057 
2058  *got_frame_ptr = 0;
2059 
2060  if (!avpkt->data && avpkt->size) {
2061  av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
2062  return AVERROR(EINVAL);
2063  }
2064  if (!avctx->codec)
2065  return AVERROR(EINVAL);
2066  if (avctx->codec->type != AVMEDIA_TYPE_AUDIO) {
2067  av_log(avctx, AV_LOG_ERROR, "Invalid media type for audio\n");
2068  return AVERROR(EINVAL);
2069  }
2070 
2072 
2073  if (!avctx->refcounted_frames)
2074  av_frame_unref(&avci->to_free);
2075 
2076  if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
2077  uint8_t *side;
2078  int side_size;
2079  // copy to ensure we do not change avpkt
2080  AVPacket tmp = *avpkt;
2081  int did_split = av_packet_split_side_data(&tmp);
2082  apply_param_change(avctx, &tmp);
2083 
2084  avctx->pkt = &tmp;
2085  if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
2086  ret = ff_thread_decode_frame(avctx, frame, got_frame_ptr, &tmp);
2087  else {
2088  ret = avctx->codec->decode(avctx, frame, got_frame_ptr, &tmp);
2089  frame->pkt_dts = avpkt->dts;
2090  }
2091  if (ret >= 0 && *got_frame_ptr) {
2092  add_metadata_from_side_data(avctx, frame);
2093  avctx->frame_number++;
2095  guess_correct_pts(avctx,
2096  frame->pkt_pts,
2097  frame->pkt_dts));
2098  if (frame->format == AV_SAMPLE_FMT_NONE)
2099  frame->format = avctx->sample_fmt;
2100  if (!frame->channel_layout)
2101  frame->channel_layout = avctx->channel_layout;
2102  if (!av_frame_get_channels(frame))
2103  av_frame_set_channels(frame, avctx->channels);
2104  if (!frame->sample_rate)
2105  frame->sample_rate = avctx->sample_rate;
2106  }
2107 
2108  side= av_packet_get_side_data(avctx->pkt, AV_PKT_DATA_SKIP_SAMPLES, &side_size);
2109  if(side && side_size>=10) {
2110  avctx->internal->skip_samples = AV_RL32(side);
2111  av_log(avctx, AV_LOG_DEBUG, "skip %d samples due to side data\n",
2112  avctx->internal->skip_samples);
2113  }
2114  if (avctx->internal->skip_samples && *got_frame_ptr) {
2115  if(frame->nb_samples <= avctx->internal->skip_samples){
2116  *got_frame_ptr = 0;
2117  avctx->internal->skip_samples -= frame->nb_samples;
2118  av_log(avctx, AV_LOG_DEBUG, "skip whole frame, skip left: %d\n",
2119  avctx->internal->skip_samples);
2120  } else {
2122  frame->nb_samples - avctx->internal->skip_samples, avctx->channels, frame->format);
2123  if(avctx->pkt_timebase.num && avctx->sample_rate) {
2124  int64_t diff_ts = av_rescale_q(avctx->internal->skip_samples,
2125  (AVRational){1, avctx->sample_rate},
2126  avctx->pkt_timebase);
2127  if(frame->pkt_pts!=AV_NOPTS_VALUE)
2128  frame->pkt_pts += diff_ts;
2129  if(frame->pkt_dts!=AV_NOPTS_VALUE)
2130  frame->pkt_dts += diff_ts;
2131  if (av_frame_get_pkt_duration(frame) >= diff_ts)
2132  av_frame_set_pkt_duration(frame, av_frame_get_pkt_duration(frame) - diff_ts);
2133  } else {
2134  av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for skipped samples.\n");
2135  }
2136  av_log(avctx, AV_LOG_DEBUG, "skip %d/%d samples\n",
2137  avctx->internal->skip_samples, frame->nb_samples);
2138  frame->nb_samples -= avctx->internal->skip_samples;
2139  avctx->internal->skip_samples = 0;
2140  }
2141  }
2142 
2143  avctx->pkt = NULL;
2144  if (did_split) {
2146  if(ret == tmp.size)
2147  ret = avpkt->size;
2148  }
2149 
2150  if (ret >= 0 && *got_frame_ptr) {
2151  if (!avctx->refcounted_frames) {
2152  avci->to_free = *frame;
2153  avci->to_free.extended_data = avci->to_free.data;
2154  memset(frame->buf, 0, sizeof(frame->buf));
2155  frame->extended_buf = NULL;
2156  frame->nb_extended_buf = 0;
2157  }
2158  } else if (frame->data[0])
2159  av_frame_unref(frame);
2160  }
2161 
2162  /* many decoders assign whole AVFrames, thus overwriting extended_data;
2163  * make sure it's set correctly; assume decoders that actually use
2164  * extended_data are doing it correctly */
2165  if (*got_frame_ptr) {
2166  planar = av_sample_fmt_is_planar(frame->format);
2167  channels = av_frame_get_channels(frame);
2168  if (!(planar && channels > AV_NUM_DATA_POINTERS))
2169  frame->extended_data = frame->data;
2170  } else {
2171  frame->extended_data = NULL;
2172  }
2173 
2174  return ret;
2175 }
2176 
2177 #define UTF8_MAX_BYTES 4 /* 5 and 6 bytes sequences should not be used */
2179  AVPacket *outpkt, const AVPacket *inpkt)
2180 {
2181 #if CONFIG_ICONV
2182  iconv_t cd = (iconv_t)-1;
2183  int ret = 0;
2184  char *inb, *outb;
2185  size_t inl, outl;
2186  AVPacket tmp;
2187 #endif
2188 
2190  return 0;
2191 
2192 #if CONFIG_ICONV
2193  cd = iconv_open("UTF-8", avctx->sub_charenc);
2194  av_assert0(cd != (iconv_t)-1);
2195 
2196  inb = inpkt->data;
2197  inl = inpkt->size;
2198 
2199  if (inl >= INT_MAX / UTF8_MAX_BYTES - FF_INPUT_BUFFER_PADDING_SIZE) {
2200  av_log(avctx, AV_LOG_ERROR, "Subtitles packet is too big for recoding\n");
2201  ret = AVERROR(ENOMEM);
2202  goto end;
2203  }
2204 
2205  ret = av_new_packet(&tmp, inl * UTF8_MAX_BYTES);
2206  if (ret < 0)
2207  goto end;
2208  outpkt->buf = tmp.buf;
2209  outpkt->data = tmp.data;
2210  outpkt->size = tmp.size;
2211  outb = outpkt->data;
2212  outl = outpkt->size;
2213 
2214  if (iconv(cd, &inb, &inl, &outb, &outl) == (size_t)-1 ||
2215  iconv(cd, NULL, NULL, &outb, &outl) == (size_t)-1 ||
2216  outl >= outpkt->size || inl != 0) {
2217  av_log(avctx, AV_LOG_ERROR, "Unable to recode subtitle event \"%s\" "
2218  "from %s to UTF-8\n", inpkt->data, avctx->sub_charenc);
2219  av_free_packet(&tmp);
2220  ret = AVERROR(errno);
2221  goto end;
2222  }
2223  outpkt->size -= outl;
2224  memset(outpkt->data + outpkt->size, 0, outl);
2225 
2226 end:
2227  if (cd != (iconv_t)-1)
2228  iconv_close(cd);
2229  return ret;
2230 #else
2231  av_assert0(!"requesting subtitles recoding without iconv");
2232 #endif
2233 }
2234 
2235 static int utf8_check(const uint8_t *str)
2236 {
2237  const uint8_t *byte;
2238  uint32_t codepoint, min;
2239 
2240  while (*str) {
2241  byte = str;
2242  GET_UTF8(codepoint, *(byte++), return 0;);
2243  min = byte - str == 1 ? 0 : byte - str == 2 ? 0x80 :
2244  1 << (5 * (byte - str) - 4);
2245  if (codepoint < min || codepoint >= 0x110000 ||
2246  codepoint == 0xFFFE /* BOM */ ||
2247  codepoint >= 0xD800 && codepoint <= 0xDFFF /* surrogates */)
2248  return 0;
2249  str = byte;
2250  }
2251  return 1;
2252 }
2253 
2255  int *got_sub_ptr,
2256  AVPacket *avpkt)
2257 {
2258  int i, ret = 0;
2259 
2260  if (avctx->codec->type != AVMEDIA_TYPE_SUBTITLE) {
2261  av_log(avctx, AV_LOG_ERROR, "Invalid media type for subtitles\n");
2262  return AVERROR(EINVAL);
2263  }
2264 
2265  *got_sub_ptr = 0;
2267 
2268  if (avpkt->size) {
2269  AVPacket pkt_recoded;
2270  AVPacket tmp = *avpkt;
2271  int did_split = av_packet_split_side_data(&tmp);
2272  //apply_param_change(avctx, &tmp);
2273 
2274  pkt_recoded = tmp;
2275  ret = recode_subtitle(avctx, &pkt_recoded, &tmp);
2276  if (ret < 0) {
2277  *got_sub_ptr = 0;
2278  } else {
2279  avctx->pkt = &pkt_recoded;
2280 
2281  if (avctx->pkt_timebase.den && avpkt->pts != AV_NOPTS_VALUE)
2282  sub->pts = av_rescale_q(avpkt->pts,
2283  avctx->pkt_timebase, AV_TIME_BASE_Q);
2284  ret = avctx->codec->decode(avctx, sub, got_sub_ptr, &pkt_recoded);
2285  av_assert1((ret >= 0) >= !!*got_sub_ptr &&
2286  !!*got_sub_ptr >= !!sub->num_rects);
2287 
2288  if (sub->num_rects && !sub->end_display_time && avpkt->duration &&
2289  avctx->pkt_timebase.num) {
2290  AVRational ms = { 1, 1000 };
2291  sub->end_display_time = av_rescale_q(avpkt->duration,
2292  avctx->pkt_timebase, ms);
2293  }
2294 
2295  for (i = 0; i < sub->num_rects; i++) {
2296  if (sub->rects[i]->ass && !utf8_check(sub->rects[i]->ass)) {
2297  av_log(avctx, AV_LOG_ERROR,
2298  "Invalid UTF-8 in decoded subtitles text; "
2299  "maybe missing -sub_charenc option\n");
2300  avsubtitle_free(sub);
2301  return AVERROR_INVALIDDATA;
2302  }
2303  }
2304 
2305  if (tmp.data != pkt_recoded.data) { // did we recode?
2306  /* prevent from destroying side data from original packet */
2307  pkt_recoded.side_data = NULL;
2308  pkt_recoded.side_data_elems = 0;
2309 
2310  av_free_packet(&pkt_recoded);
2311  }
2313  avctx->pkt = NULL;
2314  }
2315 
2316  if (did_split) {
2318  if(ret == tmp.size)
2319  ret = avpkt->size;
2320  }
2321 
2322  if (*got_sub_ptr)
2323  avctx->frame_number++;
2324  }
2325 
2326  return ret;
2327 }
2328 
2330 {
2331  int i;
2332 
2333  for (i = 0; i < sub->num_rects; i++) {
2334  av_freep(&sub->rects[i]->pict.data[0]);
2335  av_freep(&sub->rects[i]->pict.data[1]);
2336  av_freep(&sub->rects[i]->pict.data[2]);
2337  av_freep(&sub->rects[i]->pict.data[3]);
2338  av_freep(&sub->rects[i]->text);
2339  av_freep(&sub->rects[i]->ass);
2340  av_freep(&sub->rects[i]);
2341  }
2342 
2343  av_freep(&sub->rects);
2344 
2345  memset(sub, 0, sizeof(AVSubtitle));
2346 }
2347 
2349 {
2350  int ret = 0;
2351 
2353 
2354  ret = avcodec_close(avctx);
2355 
2356  ff_lock_avcodec(NULL);
2357  return ret;
2358 }
2359 
2361 {
2362  int ret = ff_lock_avcodec(avctx);
2363  if (ret < 0)
2364  return ret;
2365 
2366  if (avcodec_is_open(avctx)) {
2367  FramePool *pool = avctx->internal->pool;
2368  int i;
2369  if (CONFIG_FRAME_THREAD_ENCODER &&
2370  avctx->internal->frame_thread_encoder && avctx->thread_count > 1) {
2373  ff_lock_avcodec(avctx);
2374  }
2375  if (HAVE_THREADS && avctx->thread_opaque)
2376  ff_thread_free(avctx);
2377  if (avctx->codec && avctx->codec->close)
2378  avctx->codec->close(avctx);
2379  avctx->coded_frame = NULL;
2380  avctx->internal->byte_buffer_size = 0;
2381  av_freep(&avctx->internal->byte_buffer);
2382  if (!avctx->refcounted_frames)
2383  av_frame_unref(&avctx->internal->to_free);
2384  for (i = 0; i < FF_ARRAY_ELEMS(pool->pools); i++)
2385  av_buffer_pool_uninit(&pool->pools[i]);
2386  av_freep(&avctx->internal->pool);
2387  av_freep(&avctx->internal);
2388  }
2389 
2390  if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
2391  av_opt_free(avctx->priv_data);
2392  av_opt_free(avctx);
2393  av_freep(&avctx->priv_data);
2394  if (av_codec_is_encoder(avctx->codec))
2395  av_freep(&avctx->extradata);
2396  avctx->codec = NULL;
2397  avctx->active_thread_type = 0;
2398 
2400  return 0;
2401 }
2402 
2404 {
2405  switch(id){
2406  //This is for future deprecatec codec ids, its empty since
2407  //last major bump but will fill up again over time, please don't remove it
2408 // case AV_CODEC_ID_UTVIDEO_DEPRECATED: return AV_CODEC_ID_UTVIDEO;
2413  default : return id;
2414  }
2415 }
2416 
2417 static AVCodec *find_encdec(enum AVCodecID id, int encoder)
2418 {
2419  AVCodec *p, *experimental = NULL;
2420  p = first_avcodec;
2421  id= remap_deprecated_codec_id(id);
2422  while (p) {
2423  if ((encoder ? av_codec_is_encoder(p) : av_codec_is_decoder(p)) &&
2424  p->id == id) {
2425  if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
2426  experimental = p;
2427  } else
2428  return p;
2429  }
2430  p = p->next;
2431  }
2432  return experimental;
2433 }
2434 
2436 {
2437  return find_encdec(id, 1);
2438 }
2439 
2441 {
2442  AVCodec *p;
2443  if (!name)
2444  return NULL;
2445  p = first_avcodec;
2446  while (p) {
2447  if (av_codec_is_encoder(p) && strcmp(name, p->name) == 0)
2448  return p;
2449  p = p->next;
2450  }
2451  return NULL;
2452 }
2453 
2455 {
2456  return find_encdec(id, 0);
2457 }
2458 
2460 {
2461  AVCodec *p;
2462  if (!name)
2463  return NULL;
2464  p = first_avcodec;
2465  while (p) {
2466  if (av_codec_is_decoder(p) && strcmp(name, p->name) == 0)
2467  return p;
2468  p = p->next;
2469  }
2470  return NULL;
2471 }
2472 
2473 const char *avcodec_get_name(enum AVCodecID id)
2474 {
2475  const AVCodecDescriptor *cd;
2476  AVCodec *codec;
2477 
2478  if (id == AV_CODEC_ID_NONE)
2479  return "none";
2480  cd = avcodec_descriptor_get(id);
2481  if (cd)
2482  return cd->name;
2483  av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
2484  codec = avcodec_find_decoder(id);
2485  if (codec)
2486  return codec->name;
2487  codec = avcodec_find_encoder(id);
2488  if (codec)
2489  return codec->name;
2490  return "unknown_codec";
2491 }
2492 
2493 size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
2494 {
2495  int i, len, ret = 0;
2496 
2497 #define TAG_PRINT(x) \
2498  (((x) >= '0' && (x) <= '9') || \
2499  ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
2500  ((x) == '.' || (x) == ' ' || (x) == '-' || (x) == '_'))
2501 
2502  for (i = 0; i < 4; i++) {
2503  len = snprintf(buf, buf_size,
2504  TAG_PRINT(codec_tag & 0xFF) ? "%c" : "[%d]", codec_tag & 0xFF);
2505  buf += len;
2506  buf_size = buf_size > len ? buf_size - len : 0;
2507  ret += len;
2508  codec_tag >>= 8;
2509  }
2510  return ret;
2511 }
2512 
2513 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
2514 {
2515  const char *codec_type;
2516  const char *codec_name;
2517  const char *profile = NULL;
2518  const AVCodec *p;
2519  int bitrate;
2520  AVRational display_aspect_ratio;
2521 
2522  if (!buf || buf_size <= 0)
2523  return;
2524  codec_type = av_get_media_type_string(enc->codec_type);
2525  codec_name = avcodec_get_name(enc->codec_id);
2526  if (enc->profile != FF_PROFILE_UNKNOWN) {
2527  if (enc->codec)
2528  p = enc->codec;
2529  else
2530  p = encode ? avcodec_find_encoder(enc->codec_id) :
2532  if (p)
2533  profile = av_get_profile_name(p, enc->profile);
2534  }
2535 
2536  snprintf(buf, buf_size, "%s: %s", codec_type ? codec_type : "unknown",
2537  codec_name);
2538  buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
2539 
2540  if (enc->codec && strcmp(enc->codec->name, codec_name))
2541  snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", enc->codec->name);
2542 
2543  if (profile)
2544  snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
2545  if (enc->codec_tag) {
2546  char tag_buf[32];
2547  av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
2548  snprintf(buf + strlen(buf), buf_size - strlen(buf),
2549  " (%s / 0x%04X)", tag_buf, enc->codec_tag);
2550  }
2551 
2552  switch (enc->codec_type) {
2553  case AVMEDIA_TYPE_VIDEO:
2554  if (enc->pix_fmt != AV_PIX_FMT_NONE) {
2555  snprintf(buf + strlen(buf), buf_size - strlen(buf),
2556  ", %s",
2558  if (enc->bits_per_raw_sample &&
2560  snprintf(buf + strlen(buf), buf_size - strlen(buf),
2561  " (%d bpc)", enc->bits_per_raw_sample);
2562  }
2563  if (enc->width) {
2564  snprintf(buf + strlen(buf), buf_size - strlen(buf),
2565  ", %dx%d",
2566  enc->width, enc->height);
2567  if (enc->sample_aspect_ratio.num) {
2568  av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
2569  enc->width * enc->sample_aspect_ratio.num,
2570  enc->height * enc->sample_aspect_ratio.den,
2571  1024 * 1024);
2572  snprintf(buf + strlen(buf), buf_size - strlen(buf),
2573  " [SAR %d:%d DAR %d:%d]",
2575  display_aspect_ratio.num, display_aspect_ratio.den);
2576  }
2577  if (av_log_get_level() >= AV_LOG_DEBUG) {
2578  int g = av_gcd(enc->time_base.num, enc->time_base.den);
2579  snprintf(buf + strlen(buf), buf_size - strlen(buf),
2580  ", %d/%d",
2581  enc->time_base.num / g, enc->time_base.den / g);
2582  }
2583  }
2584  if (encode) {
2585  snprintf(buf + strlen(buf), buf_size - strlen(buf),
2586  ", q=%d-%d", enc->qmin, enc->qmax);
2587  }
2588  break;
2589  case AVMEDIA_TYPE_AUDIO:
2590  if (enc->sample_rate) {
2591  snprintf(buf + strlen(buf), buf_size - strlen(buf),
2592  ", %d Hz", enc->sample_rate);
2593  }
2594  av_strlcat(buf, ", ", buf_size);
2595  av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
2596  if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
2597  snprintf(buf + strlen(buf), buf_size - strlen(buf),
2598  ", %s", av_get_sample_fmt_name(enc->sample_fmt));
2599  }
2600  break;
2601  case AVMEDIA_TYPE_DATA:
2602  if (av_log_get_level() >= AV_LOG_DEBUG) {
2603  int g = av_gcd(enc->time_base.num, enc->time_base.den);
2604  if (g)
2605  snprintf(buf + strlen(buf), buf_size - strlen(buf),
2606  ", %d/%d",
2607  enc->time_base.num / g, enc->time_base.den / g);
2608  }
2609  break;
2610  default:
2611  return;
2612  }
2613  if (encode) {
2614  if (enc->flags & CODEC_FLAG_PASS1)
2615  snprintf(buf + strlen(buf), buf_size - strlen(buf),
2616  ", pass 1");
2617  if (enc->flags & CODEC_FLAG_PASS2)
2618  snprintf(buf + strlen(buf), buf_size - strlen(buf),
2619  ", pass 2");
2620  }
2621  bitrate = get_bit_rate(enc);
2622  if (bitrate != 0) {
2623  snprintf(buf + strlen(buf), buf_size - strlen(buf),
2624  ", %d kb/s", bitrate / 1000);
2625  }
2626 }
2627 
2628 const char *av_get_profile_name(const AVCodec *codec, int profile)
2629 {
2630  const AVProfile *p;
2631  if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
2632  return NULL;
2633 
2634  for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
2635  if (p->profile == profile)
2636  return p->name;
2637 
2638  return NULL;
2639 }
2640 
2641 unsigned avcodec_version(void)
2642 {
2643 // av_assert0(AV_CODEC_ID_V410==164);
2646 // av_assert0(AV_CODEC_ID_BMV_AUDIO==86071);
2647  av_assert0(AV_CODEC_ID_SRT==94216);
2649 
2655  return LIBAVCODEC_VERSION_INT;
2656 }
2657 
2658 const char *avcodec_configuration(void)
2659 {
2660  return FFMPEG_CONFIGURATION;
2661 }
2662 
2663 const char *avcodec_license(void)
2664 {
2665 #define LICENSE_PREFIX "libavcodec license: "
2666  return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
2667 }
2668 
2670 {
2671  if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
2672  ff_thread_flush(avctx);
2673  else if (avctx->codec->flush)
2674  avctx->codec->flush(avctx);
2675 
2676  avctx->pts_correction_last_pts =
2677  avctx->pts_correction_last_dts = INT64_MIN;
2678 
2679  if (!avctx->refcounted_frames)
2680  av_frame_unref(&avctx->internal->to_free);
2681 }
2682 
2684 {
2685  switch (codec_id) {
2686  case AV_CODEC_ID_8SVX_EXP:
2687  case AV_CODEC_ID_8SVX_FIB:
2688  case AV_CODEC_ID_ADPCM_CT:
2695  return 4;
2696  case AV_CODEC_ID_PCM_ALAW:
2697  case AV_CODEC_ID_PCM_MULAW:
2698  case AV_CODEC_ID_PCM_S8:
2700  case AV_CODEC_ID_PCM_U8:
2701  case AV_CODEC_ID_PCM_ZORK:
2702  return 8;
2703  case AV_CODEC_ID_PCM_S16BE:
2705  case AV_CODEC_ID_PCM_S16LE:
2707  case AV_CODEC_ID_PCM_U16BE:
2708  case AV_CODEC_ID_PCM_U16LE:
2709  return 16;
2711  case AV_CODEC_ID_PCM_S24BE:
2712  case AV_CODEC_ID_PCM_S24LE:
2714  case AV_CODEC_ID_PCM_U24BE:
2715  case AV_CODEC_ID_PCM_U24LE:
2716  return 24;
2717  case AV_CODEC_ID_PCM_S32BE:
2718  case AV_CODEC_ID_PCM_S32LE:
2720  case AV_CODEC_ID_PCM_U32BE:
2721  case AV_CODEC_ID_PCM_U32LE:
2722  case AV_CODEC_ID_PCM_F32BE:
2723  case AV_CODEC_ID_PCM_F32LE:
2724  return 32;
2725  case AV_CODEC_ID_PCM_F64BE:
2726  case AV_CODEC_ID_PCM_F64LE:
2727  return 64;
2728  default:
2729  return 0;
2730  }
2731 }
2732 
2734 {
2735  static const enum AVCodecID map[AV_SAMPLE_FMT_NB][2] = {
2746  };
2747  if (fmt < 0 || fmt >= AV_SAMPLE_FMT_NB)
2748  return AV_CODEC_ID_NONE;
2749  if (be < 0 || be > 1)
2750  be = AV_NE(1, 0);
2751  return map[fmt][be];
2752 }
2753 
2755 {
2756  switch (codec_id) {
2758  return 2;
2760  return 3;
2764  case AV_CODEC_ID_ADPCM_SWF:
2765  case AV_CODEC_ID_ADPCM_MS:
2766  return 4;
2767  default:
2768  return av_get_exact_bits_per_sample(codec_id);
2769  }
2770 }
2771 
2772 int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
2773 {
2774  int id, sr, ch, ba, tag, bps;
2775 
2776  id = avctx->codec_id;
2777  sr = avctx->sample_rate;
2778  ch = avctx->channels;
2779  ba = avctx->block_align;
2780  tag = avctx->codec_tag;
2781  bps = av_get_exact_bits_per_sample(avctx->codec_id);
2782 
2783  /* codecs with an exact constant bits per sample */
2784  if (bps > 0 && ch > 0 && frame_bytes > 0 && ch < 32768 && bps < 32768)
2785  return (frame_bytes * 8LL) / (bps * ch);
2786  bps = avctx->bits_per_coded_sample;
2787 
2788  /* codecs with a fixed packet duration */
2789  switch (id) {
2790  case AV_CODEC_ID_ADPCM_ADX: return 32;
2791  case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
2792  case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
2793  case AV_CODEC_ID_AMR_NB:
2794  case AV_CODEC_ID_EVRC:
2795  case AV_CODEC_ID_GSM:
2796  case AV_CODEC_ID_QCELP:
2797  case AV_CODEC_ID_RA_288: return 160;
2798  case AV_CODEC_ID_AMR_WB:
2799  case AV_CODEC_ID_GSM_MS: return 320;
2800  case AV_CODEC_ID_MP1: return 384;
2801  case AV_CODEC_ID_ATRAC1: return 512;
2802  case AV_CODEC_ID_ATRAC3: return 1024;
2803  case AV_CODEC_ID_MP2:
2804  case AV_CODEC_ID_MUSEPACK7: return 1152;
2805  case AV_CODEC_ID_AC3: return 1536;
2806  }
2807 
2808  if (sr > 0) {
2809  /* calc from sample rate */
2810  if (id == AV_CODEC_ID_TTA)
2811  return 256 * sr / 245;
2812 
2813  if (ch > 0) {
2814  /* calc from sample rate and channels */
2815  if (id == AV_CODEC_ID_BINKAUDIO_DCT)
2816  return (480 << (sr / 22050)) / ch;
2817  }
2818  }
2819 
2820  if (ba > 0) {
2821  /* calc from block_align */
2822  if (id == AV_CODEC_ID_SIPR) {
2823  switch (ba) {
2824  case 20: return 160;
2825  case 19: return 144;
2826  case 29: return 288;
2827  case 37: return 480;
2828  }
2829  } else if (id == AV_CODEC_ID_ILBC) {
2830  switch (ba) {
2831  case 38: return 160;
2832  case 50: return 240;
2833  }
2834  }
2835  }
2836 
2837  if (frame_bytes > 0) {
2838  /* calc from frame_bytes only */
2839  if (id == AV_CODEC_ID_TRUESPEECH)
2840  return 240 * (frame_bytes / 32);
2841  if (id == AV_CODEC_ID_NELLYMOSER)
2842  return 256 * (frame_bytes / 64);
2843  if (id == AV_CODEC_ID_RA_144)
2844  return 160 * (frame_bytes / 20);
2845  if (id == AV_CODEC_ID_G723_1)
2846  return 240 * (frame_bytes / 24);
2847 
2848  if (bps > 0) {
2849  /* calc from frame_bytes and bits_per_coded_sample */
2850  if (id == AV_CODEC_ID_ADPCM_G726)
2851  return frame_bytes * 8 / bps;
2852  }
2853 
2854  if (ch > 0) {
2855  /* calc from frame_bytes and channels */
2856  switch (id) {
2857  case AV_CODEC_ID_ADPCM_AFC:
2858  return frame_bytes / (9 * ch) * 16;
2859  case AV_CODEC_ID_ADPCM_DTK:
2860  return frame_bytes / (16 * ch) * 28;
2861  case AV_CODEC_ID_ADPCM_4XM:
2863  return (frame_bytes - 4 * ch) * 2 / ch;
2865  return (frame_bytes - 4) * 2 / ch;
2867  return (frame_bytes - 8) * 2 / ch;
2868  case AV_CODEC_ID_ADPCM_XA:
2869  return (frame_bytes / 128) * 224 / ch;
2871  return (frame_bytes - 6 - ch) / ch;
2872  case AV_CODEC_ID_ROQ_DPCM:
2873  return (frame_bytes - 8) / ch;
2874  case AV_CODEC_ID_XAN_DPCM:
2875  return (frame_bytes - 2 * ch) / ch;
2876  case AV_CODEC_ID_MACE3:
2877  return 3 * frame_bytes / ch;
2878  case AV_CODEC_ID_MACE6:
2879  return 6 * frame_bytes / ch;
2880  case AV_CODEC_ID_PCM_LXF:
2881  return 2 * (frame_bytes / (5 * ch));
2882  case AV_CODEC_ID_IAC:
2883  case AV_CODEC_ID_IMC:
2884  return 4 * frame_bytes / ch;
2885  }
2886 
2887  if (tag) {
2888  /* calc from frame_bytes, channels, and codec_tag */
2889  if (id == AV_CODEC_ID_SOL_DPCM) {
2890  if (tag == 3)
2891  return frame_bytes / ch;
2892  else
2893  return frame_bytes * 2 / ch;
2894  }
2895  }
2896 
2897  if (ba > 0) {
2898  /* calc from frame_bytes, channels, and block_align */
2899  int blocks = frame_bytes / ba;
2900  switch (avctx->codec_id) {
2902  if (bps < 2 || bps > 5)
2903  return 0;
2904  return blocks * (1 + (ba - 4 * ch) / (bps * ch) * 8);
2906  return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
2908  return blocks * (1 + (ba - 4 * ch) * 2 / ch);
2910  return blocks * ((ba - 4 * ch) * 2 / ch);
2911  case AV_CODEC_ID_ADPCM_MS:
2912  return blocks * (2 + (ba - 7 * ch) * 2 / ch);
2913  }
2914  }
2915 
2916  if (bps > 0) {
2917  /* calc from frame_bytes, channels, and bits_per_coded_sample */
2918  switch (avctx->codec_id) {
2919  case AV_CODEC_ID_PCM_DVD:
2920  if(bps<4)
2921  return 0;
2922  return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
2924  if(bps<4)
2925  return 0;
2926  return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
2927  case AV_CODEC_ID_S302M:
2928  return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
2929  }
2930  }
2931  }
2932  }
2933 
2934  return 0;
2935 }
2936 
2937 #if !HAVE_THREADS
2939 {
2940  return -1;
2941 }
2942 
2943 #endif
2944 
2945 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
2946 {
2947  unsigned int n = 0;
2948 
2949  while (v >= 0xff) {
2950  *s++ = 0xff;
2951  v -= 0xff;
2952  n++;
2953  }
2954  *s = v;
2955  n++;
2956  return n;
2957 }
2958 
2959 int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
2960 {
2961  int i;
2962  for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
2963  return i;
2964 }
2965 
2966 #if FF_API_MISSING_SAMPLE
2967 void av_log_missing_feature(void *avc, const char *feature, int want_sample)
2968 {
2969  av_log(avc, AV_LOG_WARNING, "%s is not implemented. Update your FFmpeg "
2970  "version to the newest one from Git. If the problem still "
2971  "occurs, it means that your file has a feature which has not "
2972  "been implemented.\n", feature);
2973  if(want_sample)
2974  av_log_ask_for_sample(avc, NULL);
2975 }
2976 
2977 void av_log_ask_for_sample(void *avc, const char *msg, ...)
2978 {
2979  va_list argument_list;
2980 
2981  va_start(argument_list, msg);
2982 
2983  if (msg)
2984  av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
2985  av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
2986  "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
2987  "and contact the ffmpeg-devel mailing list.\n");
2988 
2989  va_end(argument_list);
2990 }
2991 #endif /* FF_API_MISSING_SAMPLE */
2992 
2993 static AVHWAccel *first_hwaccel = NULL;
2994 
2996 {
2997  AVHWAccel **p = &first_hwaccel;
2998  hwaccel->next = NULL;
2999  while(avpriv_atomic_ptr_cas((void * volatile *)p, NULL, hwaccel))
3000  p = &(*p)->next;
3001 }
3002 
3004 {
3005  return hwaccel ? hwaccel->next : first_hwaccel;
3006 }
3007 
3009 {
3010  AVHWAccel *hwaccel = NULL;
3011 
3012  while ((hwaccel = av_hwaccel_next(hwaccel)))
3013  if (hwaccel->id == codec_id
3014  && hwaccel->pix_fmt == pix_fmt)
3015  return hwaccel;
3016  return NULL;
3017 }
3018 
3019 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
3020 {
3021  if (lockmgr_cb) {
3023  return -1;
3025  return -1;
3026  }
3027 
3028  lockmgr_cb = cb;
3029 
3030  if (lockmgr_cb) {
3032  return -1;
3034  return -1;
3035  }
3036  return 0;
3037 }
3038 
3040 {
3041  if (lockmgr_cb) {
3043  return -1;
3044  }
3046  if (entangled_thread_counter != 1) {
3047  av_log(log_ctx, AV_LOG_ERROR, "Insufficient thread locking around avcodec_open/close()\n");
3048  ff_avcodec_locked = 1;
3050  return AVERROR(EINVAL);
3051  }
3053  ff_avcodec_locked = 1;
3054  return 0;
3055 }
3056 
3058 {
3060  ff_avcodec_locked = 0;
3062  if (lockmgr_cb) {
3064  return -1;
3065  }
3066  return 0;
3067 }
3068 
3070 {
3071  if (lockmgr_cb) {
3073  return -1;
3074  }
3075  return 0;
3076 }
3077 
3079 {
3080  if (lockmgr_cb) {
3082  return -1;
3083  }
3084  return 0;
3085 }
3086 
3087 unsigned int avpriv_toupper4(unsigned int x)
3088 {
3089  return av_toupper(x & 0xFF) +
3090  (av_toupper((x >> 8) & 0xFF) << 8) +
3091  (av_toupper((x >> 16) & 0xFF) << 16) +
3092  (av_toupper((x >> 24) & 0xFF) << 24);
3093 }
3094 
3096 {
3097  int ret;
3098 
3099  dst->owner = src->owner;
3100 
3101  ret = av_frame_ref(dst->f, src->f);
3102  if (ret < 0)
3103  return ret;
3104 
3105  if (src->progress &&
3106  !(dst->progress = av_buffer_ref(src->progress))) {
3107  ff_thread_release_buffer(dst->owner, dst);
3108  return AVERROR(ENOMEM);
3109  }
3110 
3111  return 0;
3112 }
3113 
3114 #if !HAVE_THREADS
3115 
3117 {
3118  return avctx->get_format(avctx, fmt);
3119 }
3120 
3122 {
3123  f->owner = avctx;
3124  return ff_get_buffer(avctx, f->f, flags);
3125 }
3126 
3128 {
3129  av_frame_unref(f->f);
3130 }
3131 
3133 {
3134 }
3135 
3136 void ff_thread_report_progress(ThreadFrame *f, int progress, int field)
3137 {
3138 }
3139 
3140 void ff_thread_await_progress(ThreadFrame *f, int progress, int field)
3141 {
3142 }
3143 
3145 {
3146  return 1;
3147 }
3148 
3149 #endif
3150 
3152 {
3153  AVCodec *c= avcodec_find_decoder(codec_id);
3154  if(!c)
3155  c= avcodec_find_encoder(codec_id);
3156  if(c)
3157  return c->type;
3158 
3159  if (codec_id <= AV_CODEC_ID_NONE)
3160  return AVMEDIA_TYPE_UNKNOWN;
3161  else if (codec_id < AV_CODEC_ID_FIRST_AUDIO)
3162  return AVMEDIA_TYPE_VIDEO;
3163  else if (codec_id < AV_CODEC_ID_FIRST_SUBTITLE)
3164  return AVMEDIA_TYPE_AUDIO;
3165  else if (codec_id < AV_CODEC_ID_FIRST_UNKNOWN)
3166  return AVMEDIA_TYPE_SUBTITLE;
3167 
3168  return AVMEDIA_TYPE_UNKNOWN;
3169 }
3170 
3172 {
3173  return !!s->internal;
3174 }
3175 
3177 {
3178  int ret;
3179  char *str;
3180 
3181  ret = av_bprint_finalize(buf, &str);
3182  if (ret < 0)
3183  return ret;
3184  avctx->extradata = str;
3185  /* Note: the string is NUL terminated (so extradata can be read as a
3186  * string), but the ending character is not accounted in the size (in
3187  * binary formats you are likely not supposed to mux that character). When
3188  * extradata is copied, it is also padded with FF_INPUT_BUFFER_PADDING_SIZE
3189  * zeros. */
3190  avctx->extradata_size = buf->len;
3191  return 0;
3192 }
3193 
3195  const uint8_t *end,
3196  uint32_t *av_restrict state)
3197 {
3198  int i;
3199 
3200  assert(p <= end);
3201  if (p >= end)
3202  return end;
3203 
3204  for (i = 0; i < 3; i++) {
3205  uint32_t tmp = *state << 8;
3206  *state = tmp + *(p++);
3207  if (tmp == 0x100 || p == end)
3208  return p;
3209  }
3210 
3211  while (p < end) {
3212  if (p[-1] > 1 ) p += 3;
3213  else if (p[-2] ) p += 2;
3214  else if (p[-3]|(p[-1]-1)) p++;
3215  else {
3216  p++;
3217  break;
3218  }
3219  }
3220 
3221  p = FFMIN(p, end) - 4;
3222  *state = AV_RB32(p);
3223 
3224  return p + 4;
3225 }