libavcodec/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/avassert.h"
30 #include "libavutil/avstring.h"
31 #include "libavutil/bprint.h"
33 #include "libavutil/crc.h"
34 #include "libavutil/frame.h"
35 #include "libavutil/mathematics.h"
36 #include "libavutil/pixdesc.h"
37 #include "libavutil/imgutils.h"
38 #include "libavutil/samplefmt.h"
39 #include "libavutil/dict.h"
40 #include "libavutil/avassert.h"
41 #include "avcodec.h"
42 #include "dsputil.h"
43 #include "libavutil/opt.h"
44 #include "thread.h"
45 #include "frame_thread_encoder.h"
46 #include "internal.h"
47 #include "bytestream.h"
48 #include "version.h"
49 #include <stdlib.h>
50 #include <stdarg.h>
51 #include <limits.h>
52 #include <float.h>
53 #if CONFIG_ICONV
54 # include <iconv.h>
55 #endif
56 
57 volatile int ff_avcodec_locked;
58 static int volatile entangled_thread_counter = 0;
59 static int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
60 static void *codec_mutex;
61 static void *avformat_mutex;
62 
63 void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
64 {
65  if (min_size < *size)
66  return ptr;
67 
68  min_size = FFMAX(17 * min_size / 16 + 32, min_size);
69 
70  ptr = av_realloc(ptr, min_size);
71  /* we could set this to the unmodified min_size but this is safer
72  * if the user lost the ptr and uses NULL now
73  */
74  if (!ptr)
75  min_size = 0;
76 
77  *size = min_size;
78 
79  return ptr;
80 }
81 
82 static inline int ff_fast_malloc(void *ptr, unsigned int *size, size_t min_size, int zero_realloc)
83 {
84  void **p = ptr;
85  if (min_size < *size)
86  return 0;
87  min_size = FFMAX(17 * min_size / 16 + 32, min_size);
88  av_free(*p);
89  *p = zero_realloc ? av_mallocz(min_size) : av_malloc(min_size);
90  if (!*p)
91  min_size = 0;
92  *size = min_size;
93  return 1;
94 }
95 
96 void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
97 {
98  ff_fast_malloc(ptr, size, min_size, 0);
99 }
100 
101 void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
102 {
103  uint8_t **p = ptr;
104  if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
105  av_freep(p);
106  *size = 0;
107  return;
108  }
109  if (!ff_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE, 1))
110  memset(*p + min_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
111 }
112 
113 void av_fast_padded_mallocz(void *ptr, unsigned int *size, size_t min_size)
114 {
115  uint8_t **p = ptr;
116  if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
117  av_freep(p);
118  *size = 0;
119  return;
120  }
121  if (!ff_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE, 1))
122  memset(*p, 0, min_size + FF_INPUT_BUFFER_PADDING_SIZE);
123 }
124 
125 /* encoder management */
127 
129 {
130  if (c)
131  return c->next;
132  else
133  return first_avcodec;
134 }
135 
136 static void avcodec_init(void)
137 {
138  static int initialized = 0;
139 
140  if (initialized != 0)
141  return;
142  initialized = 1;
143 
144  if (CONFIG_DSPUTIL)
146 }
147 
148 int av_codec_is_encoder(const AVCodec *codec)
149 {
150  return codec && (codec->encode_sub || codec->encode2);
151 }
152 
153 int av_codec_is_decoder(const AVCodec *codec)
154 {
155  return codec && codec->decode;
156 }
157 
159 {
160  AVCodec **p;
161  avcodec_init();
162  p = &first_avcodec;
163  while (*p != NULL)
164  p = &(*p)->next;
165  *p = codec;
166  codec->next = NULL;
167 
168  if (codec->init_static_data)
169  codec->init_static_data(codec);
170 }
171 
173 {
174  return EDGE_WIDTH;
175 }
176 
178 {
179  s->coded_width = width;
180  s->coded_height = height;
181  s->width = -((-width ) >> s->lowres);
182  s->height = -((-height) >> s->lowres);
183 }
184 
185 #if (ARCH_ARM && HAVE_NEON) || ARCH_PPC || HAVE_MMX
186 # define STRIDE_ALIGN 16
187 #else
188 # define STRIDE_ALIGN 8
189 #endif
190 
192  int linesize_align[AV_NUM_DATA_POINTERS])
193 {
194  int i;
195  int w_align = 1;
196  int h_align = 1;
197 
198  switch (s->pix_fmt) {
199  case AV_PIX_FMT_YUV420P:
200  case AV_PIX_FMT_YUYV422:
201  case AV_PIX_FMT_UYVY422:
202  case AV_PIX_FMT_YUV422P:
203  case AV_PIX_FMT_YUV440P:
204  case AV_PIX_FMT_YUV444P:
205  case AV_PIX_FMT_GBRP:
206  case AV_PIX_FMT_GRAY8:
207  case AV_PIX_FMT_GRAY16BE:
208  case AV_PIX_FMT_GRAY16LE:
209  case AV_PIX_FMT_YUVJ420P:
210  case AV_PIX_FMT_YUVJ422P:
211  case AV_PIX_FMT_YUVJ440P:
212  case AV_PIX_FMT_YUVJ444P:
213  case AV_PIX_FMT_YUVA420P:
214  case AV_PIX_FMT_YUVA422P:
215  case AV_PIX_FMT_YUVA444P:
240  case AV_PIX_FMT_GBRP9LE:
241  case AV_PIX_FMT_GBRP9BE:
242  case AV_PIX_FMT_GBRP10LE:
243  case AV_PIX_FMT_GBRP10BE:
244  case AV_PIX_FMT_GBRP12LE:
245  case AV_PIX_FMT_GBRP12BE:
246  case AV_PIX_FMT_GBRP14LE:
247  case AV_PIX_FMT_GBRP14BE:
248  w_align = 16; //FIXME assume 16 pixel per macroblock
249  h_align = 16 * 2; // interlaced needs 2 macroblocks height
250  break;
251  case AV_PIX_FMT_YUV411P:
253  w_align = 32;
254  h_align = 8;
255  break;
256  case AV_PIX_FMT_YUV410P:
257  if (s->codec_id == AV_CODEC_ID_SVQ1) {
258  w_align = 64;
259  h_align = 64;
260  }
261  break;
262  case AV_PIX_FMT_RGB555:
263  if (s->codec_id == AV_CODEC_ID_RPZA) {
264  w_align = 4;
265  h_align = 4;
266  }
267  break;
268  case AV_PIX_FMT_PAL8:
269  case AV_PIX_FMT_BGR8:
270  case AV_PIX_FMT_RGB8:
271  if (s->codec_id == AV_CODEC_ID_SMC ||
273  w_align = 4;
274  h_align = 4;
275  }
276  break;
277  case AV_PIX_FMT_BGR24:
278  if ((s->codec_id == AV_CODEC_ID_MSZH) ||
279  (s->codec_id == AV_CODEC_ID_ZLIB)) {
280  w_align = 4;
281  h_align = 4;
282  }
283  break;
284  case AV_PIX_FMT_RGB24:
285  if (s->codec_id == AV_CODEC_ID_CINEPAK) {
286  w_align = 4;
287  h_align = 4;
288  }
289  break;
290  default:
291  w_align = 1;
292  h_align = 1;
293  break;
294  }
295 
297  w_align = FFMAX(w_align, 8);
298  }
299 
300  *width = FFALIGN(*width, w_align);
301  *height = FFALIGN(*height, h_align);
302  if (s->codec_id == AV_CODEC_ID_H264 || s->lowres)
303  // some of the optimized chroma MC reads one line too much
304  // which is also done in mpeg decoders with lowres > 0
305  *height += 2;
306 
307  for (i = 0; i < 4; i++)
308  linesize_align[i] = STRIDE_ALIGN;
309 }
310 
312 {
314  int chroma_shift = desc->log2_chroma_w;
315  int linesize_align[AV_NUM_DATA_POINTERS];
316  int align;
317 
318  avcodec_align_dimensions2(s, width, height, linesize_align);
319  align = FFMAX(linesize_align[0], linesize_align[3]);
320  linesize_align[1] <<= chroma_shift;
321  linesize_align[2] <<= chroma_shift;
322  align = FFMAX3(align, linesize_align[1], linesize_align[2]);
323  *width = FFALIGN(*width, align);
324 }
325 
327  enum AVSampleFormat sample_fmt, const uint8_t *buf,
328  int buf_size, int align)
329 {
330  int ch, planar, needed_size, ret = 0;
331 
332  needed_size = av_samples_get_buffer_size(NULL, nb_channels,
333  frame->nb_samples, sample_fmt,
334  align);
335  if (buf_size < needed_size)
336  return AVERROR(EINVAL);
337 
338  planar = av_sample_fmt_is_planar(sample_fmt);
339  if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
340  if (!(frame->extended_data = av_mallocz(nb_channels *
341  sizeof(*frame->extended_data))))
342  return AVERROR(ENOMEM);
343  } else {
344  frame->extended_data = frame->data;
345  }
346 
347  if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
348  (uint8_t *)(intptr_t)buf, nb_channels, frame->nb_samples,
349  sample_fmt, align)) < 0) {
350  if (frame->extended_data != frame->data)
351  av_freep(&frame->extended_data);
352  return ret;
353  }
354  if (frame->extended_data != frame->data) {
355  for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
356  frame->data[ch] = frame->extended_data[ch];
357  }
358 
359  return ret;
360 }
361 
363 {
364  FramePool *pool = avctx->internal->pool;
365  int i, ret;
366 
367  switch (avctx->codec_type) {
368  case AVMEDIA_TYPE_VIDEO: {
369  AVPicture picture;
370  int size[4] = { 0 };
371  int w = frame->width;
372  int h = frame->height;
373  int tmpsize, unaligned;
374 
375  if (pool->format == frame->format &&
376  pool->width == frame->width && pool->height == frame->height)
377  return 0;
378 
379  avcodec_align_dimensions2(avctx, &w, &h, pool->stride_align);
380 
381  if (!(avctx->flags & CODEC_FLAG_EMU_EDGE)) {
382  w += EDGE_WIDTH * 2;
383  h += EDGE_WIDTH * 2;
384  }
385 
386  do {
387  // NOTE: do not align linesizes individually, this breaks e.g. assumptions
388  // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
389  av_image_fill_linesizes(picture.linesize, avctx->pix_fmt, w);
390  // increase alignment of w for next try (rhs gives the lowest bit set in w)
391  w += w & ~(w - 1);
392 
393  unaligned = 0;
394  for (i = 0; i < 4; i++)
395  unaligned |= picture.linesize[i] % pool->stride_align[i];
396  } while (unaligned);
397 
398  tmpsize = av_image_fill_pointers(picture.data, avctx->pix_fmt, h,
399  NULL, picture.linesize);
400  if (tmpsize < 0)
401  return -1;
402 
403  for (i = 0; i < 3 && picture.data[i + 1]; i++)
404  size[i] = picture.data[i + 1] - picture.data[i];
405  size[i] = tmpsize - (picture.data[i] - picture.data[0]);
406 
407  for (i = 0; i < 4; i++) {
408  av_buffer_pool_uninit(&pool->pools[i]);
409  pool->linesize[i] = picture.linesize[i];
410  if (size[i]) {
411  pool->pools[i] = av_buffer_pool_init(size[i] + 16,
413  NULL :
415  if (!pool->pools[i]) {
416  ret = AVERROR(ENOMEM);
417  goto fail;
418  }
419  }
420  }
421  pool->format = frame->format;
422  pool->width = frame->width;
423  pool->height = frame->height;
424 
425  break;
426  }
427  case AVMEDIA_TYPE_AUDIO: {
428  int ch = av_frame_get_channels(frame); //av_get_channel_layout_nb_channels(frame->channel_layout);
429  int planar = av_sample_fmt_is_planar(frame->format);
430  int planes = planar ? ch : 1;
431 
432  if (pool->format == frame->format && pool->planes == planes &&
433  pool->channels == ch && frame->nb_samples == pool->samples)
434  return 0;
435 
436  av_buffer_pool_uninit(&pool->pools[0]);
437  ret = av_samples_get_buffer_size(&pool->linesize[0], ch,
438  frame->nb_samples, frame->format, 0);
439  if (ret < 0)
440  goto fail;
441 
442  pool->pools[0] = av_buffer_pool_init(pool->linesize[0], NULL);
443  if (!pool->pools[0]) {
444  ret = AVERROR(ENOMEM);
445  goto fail;
446  }
447 
448  pool->format = frame->format;
449  pool->planes = planes;
450  pool->channels = ch;
451  pool->samples = frame->nb_samples;
452  break;
453  }
454  default: av_assert0(0);
455  }
456  return 0;
457 fail:
458  for (i = 0; i < 4; i++)
459  av_buffer_pool_uninit(&pool->pools[i]);
460  pool->format = -1;
461  pool->planes = pool->channels = pool->samples = 0;
462  pool->width = pool->height = 0;
463  return ret;
464 }
465 
467 {
468  FramePool *pool = avctx->internal->pool;
469  int planes = pool->planes;
470  int i;
471 
472  frame->linesize[0] = pool->linesize[0];
473 
474  if (planes > AV_NUM_DATA_POINTERS) {
475  frame->extended_data = av_mallocz(planes * sizeof(*frame->extended_data));
476  frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
477  frame->extended_buf = av_mallocz(frame->nb_extended_buf *
478  sizeof(*frame->extended_buf));
479  if (!frame->extended_data || !frame->extended_buf) {
480  av_freep(&frame->extended_data);
481  av_freep(&frame->extended_buf);
482  return AVERROR(ENOMEM);
483  }
484  } else {
485  frame->extended_data = frame->data;
486  av_assert0(frame->nb_extended_buf == 0);
487  }
488 
489  for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
490  frame->buf[i] = av_buffer_pool_get(pool->pools[0]);
491  if (!frame->buf[i])
492  goto fail;
493  frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
494  }
495  for (i = 0; i < frame->nb_extended_buf; i++) {
496  frame->extended_buf[i] = av_buffer_pool_get(pool->pools[0]);
497  if (!frame->extended_buf[i])
498  goto fail;
499  frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
500  }
501 
502  if (avctx->debug & FF_DEBUG_BUFFERS)
503  av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p", frame);
504 
505  return 0;
506 fail:
507  av_frame_unref(frame);
508  return AVERROR(ENOMEM);
509 }
510 
512 {
513  FramePool *pool = s->internal->pool;
514  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pic->format);
515  int pixel_size = desc->comp[0].step_minus1 + 1;
516  int h_chroma_shift, v_chroma_shift;
517  int i;
518 
519  if (pic->data[0] != NULL) {
520  av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
521  return -1;
522  }
523 
524  memset(pic->data, 0, sizeof(pic->data));
525  pic->extended_data = pic->data;
526 
527  av_pix_fmt_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
528 
529  for (i = 0; i < 4 && pool->pools[i]; i++) {
530  const int h_shift = i == 0 ? 0 : h_chroma_shift;
531  const int v_shift = i == 0 ? 0 : v_chroma_shift;
532 
533  pic->linesize[i] = pool->linesize[i];
534 
535  pic->buf[i] = av_buffer_pool_get(pool->pools[i]);
536  if (!pic->buf[i])
537  goto fail;
538 
539  // no edge if EDGE EMU or not planar YUV
540  if ((s->flags & CODEC_FLAG_EMU_EDGE) || !pool->pools[2])
541  pic->data[i] = pic->buf[i]->data;
542  else {
543  pic->data[i] = pic->buf[i]->data +
544  FFALIGN((pic->linesize[i] * EDGE_WIDTH >> v_shift) +
545  (pixel_size * EDGE_WIDTH >> h_shift), pool->stride_align[i]);
546  }
547  }
548  for (; i < AV_NUM_DATA_POINTERS; i++) {
549  pic->data[i] = NULL;
550  pic->linesize[i] = 0;
551  }
552  if (pic->data[1] && !pic->data[2])
553  avpriv_set_systematic_pal2((uint32_t *)pic->data[1], s->pix_fmt);
554 
555  if (s->debug & FF_DEBUG_BUFFERS)
556  av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p\n", pic);
557 
558  return 0;
559 fail:
560  av_frame_unref(pic);
561  return AVERROR(ENOMEM);
562 }
563 
564 void avpriv_color_frame(AVFrame *frame, const int c[4])
565 {
566  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
567  int p, y, x;
568 
570 
571  for (p = 0; p<desc->nb_components; p++) {
572  uint8_t *dst = frame->data[p];
573  int is_chroma = p == 1 || p == 2;
574  int bytes = -((-frame->width) >> (is_chroma ? desc->log2_chroma_w : 0));
575  for (y = 0; y<-((-frame->height) >> (is_chroma ? desc->log2_chroma_h : 0)); y++){
576  if (desc->comp[0].depth_minus1 >= 8) {
577  for (x = 0; x<bytes; x++)
578  ((uint16_t*)dst)[x] = c[p];
579  }else
580  memset(dst, c[p], bytes);
581  dst += frame->linesize[p];
582  }
583  }
584 }
585 
587 {
588  int ret;
589 
590  if ((ret = update_frame_pool(avctx, frame)) < 0)
591  return ret;
592 
593 #if FF_API_GET_BUFFER
594  frame->type = FF_BUFFER_TYPE_INTERNAL;
595 #endif
596 
597  switch (avctx->codec_type) {
598  case AVMEDIA_TYPE_VIDEO:
599  return video_get_buffer(avctx, frame);
600  case AVMEDIA_TYPE_AUDIO:
601  return audio_get_buffer(avctx, frame);
602  default:
603  return -1;
604  }
605 }
606 
608 {
609  if (avctx->pkt) {
610  frame->pkt_pts = avctx->pkt->pts;
611  av_frame_set_pkt_pos (frame, avctx->pkt->pos);
612  av_frame_set_pkt_duration(frame, avctx->pkt->duration);
613  av_frame_set_pkt_size (frame, avctx->pkt->size);
614  } else {
615  frame->pkt_pts = AV_NOPTS_VALUE;
616  av_frame_set_pkt_pos (frame, -1);
617  av_frame_set_pkt_duration(frame, 0);
618  av_frame_set_pkt_size (frame, -1);
619  }
620  frame->reordered_opaque = avctx->reordered_opaque;
621 
622  switch (avctx->codec->type) {
623  case AVMEDIA_TYPE_VIDEO:
624  frame->width = FFMAX(avctx->width , -((-avctx->coded_width )>>avctx->lowres));
625  frame->height = FFMAX(avctx->height, -((-avctx->coded_height)>>avctx->lowres));
626  if (frame->format < 0)
627  frame->format = avctx->pix_fmt;
628  if (!frame->sample_aspect_ratio.num)
630  break;
631  case AVMEDIA_TYPE_AUDIO:
632  if (!frame->sample_rate)
633  frame->sample_rate = avctx->sample_rate;
634  if (frame->format < 0)
635  frame->format = avctx->sample_fmt;
636  if (!frame->channel_layout) {
637  if (avctx->channel_layout) {
639  avctx->channels) {
640  av_log(avctx, AV_LOG_ERROR, "Inconsistent channel "
641  "configuration.\n");
642  return AVERROR(EINVAL);
643  }
644 
645  frame->channel_layout = avctx->channel_layout;
646  } else {
647  if (avctx->channels > FF_SANE_NB_CHANNELS) {
648  av_log(avctx, AV_LOG_ERROR, "Too many channels: %d.\n",
649  avctx->channels);
650  return AVERROR(ENOSYS);
651  }
652  }
653  }
654  av_frame_set_channels(frame, avctx->channels);
655  break;
656  }
657  return 0;
658 }
659 
660 #if FF_API_GET_BUFFER
662 {
663  return avcodec_default_get_buffer2(avctx, frame, 0);
664 }
665 
666 typedef struct CompatReleaseBufPriv {
670 
671 static void compat_free_buffer(void *opaque, uint8_t *data)
672 {
673  CompatReleaseBufPriv *priv = opaque;
674  if (priv->avctx.release_buffer)
675  priv->avctx.release_buffer(&priv->avctx, &priv->frame);
676  av_freep(&priv);
677 }
678 
679 static void compat_release_buffer(void *opaque, uint8_t *data)
680 {
681  AVBufferRef *buf = opaque;
682  av_buffer_unref(&buf);
683 }
684 #endif
685 
687 {
688  int ret;
689 
690  if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
691  if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0 || avctx->pix_fmt<0) {
692  av_log(avctx, AV_LOG_ERROR, "video_get_buffer: image parameters invalid\n");
693  return AVERROR(EINVAL);
694  }
695  }
696  if ((ret = ff_init_buffer_info(avctx, frame)) < 0)
697  return ret;
698 
699 #if FF_API_GET_BUFFER
700  /*
701  * Wrap an old get_buffer()-allocated buffer in an bunch of AVBuffers.
702  * We wrap each plane in its own AVBuffer. Each of those has a reference to
703  * a dummy AVBuffer as its private data, unreffing it on free.
704  * When all the planes are freed, the dummy buffer's free callback calls
705  * release_buffer().
706  */
707  if (avctx->get_buffer) {
708  CompatReleaseBufPriv *priv = NULL;
709  AVBufferRef *dummy_buf = NULL;
710  int planes, i, ret;
711 
712  if (flags & AV_GET_BUFFER_FLAG_REF)
713  frame->reference = 1;
714 
715  ret = avctx->get_buffer(avctx, frame);
716  if (ret < 0)
717  return ret;
718 
719  /* return if the buffers are already set up
720  * this would happen e.g. when a custom get_buffer() calls
721  * avcodec_default_get_buffer
722  */
723  if (frame->buf[0])
724  return 0;
725 
726  priv = av_mallocz(sizeof(*priv));
727  if (!priv) {
728  ret = AVERROR(ENOMEM);
729  goto fail;
730  }
731  priv->avctx = *avctx;
732  priv->frame = *frame;
733 
734  dummy_buf = av_buffer_create(NULL, 0, compat_free_buffer, priv, 0);
735  if (!dummy_buf) {
736  ret = AVERROR(ENOMEM);
737  goto fail;
738  }
739 
740 #define WRAP_PLANE(ref_out, data, data_size) \
741 do { \
742  AVBufferRef *dummy_ref = av_buffer_ref(dummy_buf); \
743  if (!dummy_ref) { \
744  ret = AVERROR(ENOMEM); \
745  goto fail; \
746  } \
747  ref_out = av_buffer_create(data, data_size, compat_release_buffer, \
748  dummy_ref, 0); \
749  if (!ref_out) { \
750  av_frame_unref(frame); \
751  ret = AVERROR(ENOMEM); \
752  goto fail; \
753  } \
754 } while (0)
755 
756  if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
757  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
758 
759  planes = av_pix_fmt_count_planes(frame->format);
760  /* workaround for AVHWAccel plane count of 0, buf[0] is used as
761  check for allocated buffers: make libavcodec happy */
762  if (desc && desc->flags & PIX_FMT_HWACCEL)
763  planes = 1;
764  if (!desc || planes <= 0) {
765  ret = AVERROR(EINVAL);
766  goto fail;
767  }
768 
769  for (i = 0; i < planes; i++) {
770  int v_shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
771  int plane_size = (frame->height >> v_shift) * frame->linesize[i];
772 
773  WRAP_PLANE(frame->buf[i], frame->data[i], plane_size);
774  }
775  } else {
776  int planar = av_sample_fmt_is_planar(frame->format);
777  planes = planar ? avctx->channels : 1;
778 
779  if (planes > FF_ARRAY_ELEMS(frame->buf)) {
780  frame->nb_extended_buf = planes - FF_ARRAY_ELEMS(frame->buf);
781  frame->extended_buf = av_malloc(sizeof(*frame->extended_buf) *
782  frame->nb_extended_buf);
783  if (!frame->extended_buf) {
784  ret = AVERROR(ENOMEM);
785  goto fail;
786  }
787  }
788 
789  for (i = 0; i < FFMIN(planes, FF_ARRAY_ELEMS(frame->buf)); i++)
790  WRAP_PLANE(frame->buf[i], frame->extended_data[i], frame->linesize[0]);
791 
792  for (i = 0; i < frame->nb_extended_buf; i++)
793  WRAP_PLANE(frame->extended_buf[i],
794  frame->extended_data[i + FF_ARRAY_ELEMS(frame->buf)],
795  frame->linesize[0]);
796  }
797 
798  av_buffer_unref(&dummy_buf);
799 
800  frame->width = avctx->width;
801  frame->height = avctx->height;
802 
803  return 0;
804 
805 fail:
806  avctx->release_buffer(avctx, frame);
807  av_freep(&priv);
808  av_buffer_unref(&dummy_buf);
809  return ret;
810  }
811 #endif
812 
813  ret = avctx->get_buffer2(avctx, frame, flags);
814 
815  if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
816  frame->width = avctx->width;
817  frame->height = avctx->height;
818  }
819 
820  return ret;
821 }
822 
824 {
825  int ret = get_buffer_internal(avctx, frame, flags);
826  if (ret < 0)
827  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
828  return ret;
829 }
830 
832 {
833  AVFrame tmp;
834  int ret;
835 
837 
838  if (frame->data[0] && (frame->width != avctx->width || frame->height != avctx->height || frame->format != avctx->pix_fmt)) {
839  av_log(avctx, AV_LOG_WARNING, "Picture changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s in reget buffer()\n",
840  frame->width, frame->height, av_get_pix_fmt_name(frame->format), avctx->width, avctx->height, av_get_pix_fmt_name(avctx->pix_fmt));
841  av_frame_unref(frame);
842  }
843 
844  ff_init_buffer_info(avctx, frame);
845 
846  if (!frame->data[0])
847  return ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
848 
849  if (av_frame_is_writable(frame))
850  return 0;
851 
852  av_frame_move_ref(&tmp, frame);
853 
854  ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
855  if (ret < 0) {
856  av_frame_unref(&tmp);
857  return ret;
858  }
859 
860  av_image_copy(frame->data, frame->linesize, tmp.data, tmp.linesize,
861  frame->format, frame->width, frame->height);
862 
863  av_frame_unref(&tmp);
864 
865  return 0;
866 }
867 
869 {
870  int ret = reget_buffer_internal(avctx, frame);
871  if (ret < 0)
872  av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
873  return ret;
874 }
875 
876 #if FF_API_GET_BUFFER
878 {
880 
881  av_frame_unref(pic);
882 }
883 
885 {
886  av_assert0(0);
887 }
888 #endif
889 
890 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
891 {
892  int i;
893 
894  for (i = 0; i < count; i++) {
895  int r = func(c, (char *)arg + i * size);
896  if (ret)
897  ret[i] = r;
898  }
899  return 0;
900 }
901 
902 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
903 {
904  int i;
905 
906  for (i = 0; i < count; i++) {
907  int r = func(c, arg, i, 0);
908  if (ret)
909  ret[i] = r;
910  }
911  return 0;
912 }
913 
915 {
916  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
917  return desc->flags & PIX_FMT_HWACCEL;
918 }
919 
921 {
922  while (*fmt != AV_PIX_FMT_NONE && is_hwaccel_pix_fmt(*fmt))
923  ++fmt;
924  return fmt[0];
925 }
926 
928 {
929 #if LIBAVCODEC_VERSION_MAJOR >= 55
930  // extended_data should explicitly be freed when needed, this code is unsafe currently
931  // also this is not compatible to the <55 ABI/API
932  if (frame->extended_data != frame->data && 0)
933  av_freep(&frame->extended_data);
934 #endif
935 
936  memset(frame, 0, sizeof(AVFrame));
937 
938  frame->pts =
939  frame->pkt_dts =
940  frame->pkt_pts = AV_NOPTS_VALUE;
942  av_frame_set_pkt_duration (frame, 0);
943  av_frame_set_pkt_pos (frame, -1);
944  av_frame_set_pkt_size (frame, -1);
945  frame->key_frame = 1;
946  frame->sample_aspect_ratio = (AVRational) {0, 1 };
947  frame->format = -1; /* unknown */
948  frame->extended_data = frame->data;
949 }
950 
952 {
953  AVFrame *frame = av_malloc(sizeof(AVFrame));
954 
955  if (frame == NULL)
956  return NULL;
957 
958  frame->extended_data = NULL;
960 
961  return frame;
962 }
963 
965 {
966  AVFrame *f;
967 
968  if (!frame || !*frame)
969  return;
970 
971  f = *frame;
972 
973  if (f->extended_data != f->data)
974  av_freep(&f->extended_data);
975 
976  av_freep(frame);
977 }
978 
979 #define MAKE_ACCESSORS(str, name, type, field) \
980  type av_##name##_get_##field(const str *s) { return s->field; } \
981  void av_##name##_set_##field(str *s, type v) { s->field = v; }
982 
983 MAKE_ACCESSORS(AVCodecContext, codec, AVRational, pkt_timebase)
984 MAKE_ACCESSORS(AVCodecContext, codec, const AVCodecDescriptor *, codec_descriptor)
985 
987 {
988  memset(sub, 0, sizeof(*sub));
989  sub->pts = AV_NOPTS_VALUE;
990 }
991 
992 static int get_bit_rate(AVCodecContext *ctx)
993 {
994  int bit_rate;
995  int bits_per_sample;
996 
997  switch (ctx->codec_type) {
998  case AVMEDIA_TYPE_VIDEO:
999  case AVMEDIA_TYPE_DATA:
1000  case AVMEDIA_TYPE_SUBTITLE:
1002  bit_rate = ctx->bit_rate;
1003  break;
1004  case AVMEDIA_TYPE_AUDIO:
1005  bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
1006  bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
1007  break;
1008  default:
1009  bit_rate = 0;
1010  break;
1011  }
1012  return bit_rate;
1013 }
1014 
1015 #if FF_API_AVCODEC_OPEN
1016 int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
1017 {
1018  return avcodec_open2(avctx, codec, NULL);
1019 }
1020 #endif
1021 
1023 {
1024  int ret = 0;
1025 
1027 
1028  ret = avcodec_open2(avctx, codec, options);
1029 
1030  ff_lock_avcodec(avctx);
1031  return ret;
1032 }
1033 
1035 {
1036  int ret = 0;
1037  AVDictionary *tmp = NULL;
1038 
1039  if (avcodec_is_open(avctx))
1040  return 0;
1041 
1042  if ((!codec && !avctx->codec)) {
1043  av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2()\n");
1044  return AVERROR(EINVAL);
1045  }
1046  if ((codec && avctx->codec && codec != avctx->codec)) {
1047  av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
1048  "but %s passed to avcodec_open2()\n", avctx->codec->name, codec->name);
1049  return AVERROR(EINVAL);
1050  }
1051  if (!codec)
1052  codec = avctx->codec;
1053 
1054  if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
1055  return AVERROR(EINVAL);
1056 
1057  if (options)
1058  av_dict_copy(&tmp, *options, 0);
1059 
1060  ret = ff_lock_avcodec(avctx);
1061  if (ret < 0)
1062  return ret;
1063 
1064  avctx->internal = av_mallocz(sizeof(AVCodecInternal));
1065  if (!avctx->internal) {
1066  ret = AVERROR(ENOMEM);
1067  goto end;
1068  }
1069 
1070  avctx->internal->pool = av_mallocz(sizeof(*avctx->internal->pool));
1071  if (!avctx->internal->pool) {
1072  ret = AVERROR(ENOMEM);
1073  goto free_and_end;
1074  }
1075 
1076  if (codec->priv_data_size > 0) {
1077  if (!avctx->priv_data) {
1078  avctx->priv_data = av_mallocz(codec->priv_data_size);
1079  if (!avctx->priv_data) {
1080  ret = AVERROR(ENOMEM);
1081  goto end;
1082  }
1083  if (codec->priv_class) {
1084  *(const AVClass **)avctx->priv_data = codec->priv_class;
1086  }
1087  }
1088  if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
1089  goto free_and_end;
1090  } else {
1091  avctx->priv_data = NULL;
1092  }
1093  if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
1094  goto free_and_end;
1095 
1096  // only call avcodec_set_dimensions() for non H.264/VP6F codecs so as not to overwrite previously setup dimensions
1097  if (!(avctx->coded_width && avctx->coded_height && avctx->width && avctx->height &&
1098  (avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_VP6F))) {
1099  if (avctx->coded_width && avctx->coded_height)
1100  avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
1101  else if (avctx->width && avctx->height)
1102  avcodec_set_dimensions(avctx, avctx->width, avctx->height);
1103  }
1104 
1105  if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
1106  && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
1107  || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
1108  av_log(avctx, AV_LOG_WARNING, "Ignoring invalid width/height values\n");
1109  avcodec_set_dimensions(avctx, 0, 0);
1110  }
1111 
1112  /* if the decoder init function was already called previously,
1113  * free the already allocated subtitle_header before overwriting it */
1114  if (av_codec_is_decoder(codec))
1115  av_freep(&avctx->subtitle_header);
1116 
1117  if (avctx->channels > FF_SANE_NB_CHANNELS) {
1118  ret = AVERROR(EINVAL);
1119  goto free_and_end;
1120  }
1121 
1122  avctx->codec = codec;
1123  if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
1124  avctx->codec_id == AV_CODEC_ID_NONE) {
1125  avctx->codec_type = codec->type;
1126  avctx->codec_id = codec->id;
1127  }
1128  if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
1129  && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
1130  av_log(avctx, AV_LOG_ERROR, "Codec type or id mismatches\n");
1131  ret = AVERROR(EINVAL);
1132  goto free_and_end;
1133  }
1134  avctx->frame_number = 0;
1136 
1137  if (avctx->codec->capabilities & CODEC_CAP_EXPERIMENTAL &&
1139  const char *codec_string = av_codec_is_encoder(codec) ? "encoder" : "decoder";
1140  AVCodec *codec2;
1141  av_log(avctx, AV_LOG_ERROR,
1142  "The %s '%s' is experimental but experimental codecs are not enabled, "
1143  "add '-strict %d' if you want to use it.\n",
1144  codec_string, codec->name, FF_COMPLIANCE_EXPERIMENTAL);
1145  codec2 = av_codec_is_encoder(codec) ? avcodec_find_encoder(codec->id) : avcodec_find_decoder(codec->id);
1146  if (!(codec2->capabilities & CODEC_CAP_EXPERIMENTAL))
1147  av_log(avctx, AV_LOG_ERROR, "Alternatively use the non experimental %s '%s'.\n",
1148  codec_string, codec2->name);
1149  ret = AVERROR_EXPERIMENTAL;
1150  goto free_and_end;
1151  }
1152 
1153  if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
1154  (!avctx->time_base.num || !avctx->time_base.den)) {
1155  avctx->time_base.num = 1;
1156  avctx->time_base.den = avctx->sample_rate;
1157  }
1158 
1159  if (!HAVE_THREADS)
1160  av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
1161 
1163  ff_unlock_avcodec(); //we will instanciate a few encoders thus kick the counter to prevent false detection of a problem
1164  ret = ff_frame_thread_encoder_init(avctx, options ? *options : NULL);
1165  ff_lock_avcodec(avctx);
1166  if (ret < 0)
1167  goto free_and_end;
1168  }
1169 
1170  if (HAVE_THREADS && !avctx->thread_opaque
1172  ret = ff_thread_init(avctx);
1173  if (ret < 0) {
1174  goto free_and_end;
1175  }
1176  }
1177  if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
1178  avctx->thread_count = 1;
1179 
1180  if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
1181  av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
1182  avctx->codec->max_lowres);
1183  ret = AVERROR(EINVAL);
1184  goto free_and_end;
1185  }
1186 
1187  if (av_codec_is_encoder(avctx->codec)) {
1188  int i;
1189  if (avctx->codec->sample_fmts) {
1190  for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
1191  if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
1192  break;
1193  if (avctx->channels == 1 &&
1196  avctx->sample_fmt = avctx->codec->sample_fmts[i];
1197  break;
1198  }
1199  }
1200  if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
1201  char buf[128];
1202  snprintf(buf, sizeof(buf), "%d", avctx->sample_fmt);
1203  av_log(avctx, AV_LOG_ERROR, "Specified sample format %s is invalid or not supported\n",
1204  (char *)av_x_if_null(av_get_sample_fmt_name(avctx->sample_fmt), buf));
1205  ret = AVERROR(EINVAL);
1206  goto free_and_end;
1207  }
1208  }
1209  if (avctx->codec->pix_fmts) {
1210  for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
1211  if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
1212  break;
1213  if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE
1214  && !((avctx->codec_id == AV_CODEC_ID_MJPEG || avctx->codec_id == AV_CODEC_ID_LJPEG)
1216  char buf[128];
1217  snprintf(buf, sizeof(buf), "%d", avctx->pix_fmt);
1218  av_log(avctx, AV_LOG_ERROR, "Specified pixel format %s is invalid or not supported\n",
1219  (char *)av_x_if_null(av_get_pix_fmt_name(avctx->pix_fmt), buf));
1220  ret = AVERROR(EINVAL);
1221  goto free_and_end;
1222  }
1223  }
1224  if (avctx->codec->supported_samplerates) {
1225  for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
1226  if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
1227  break;
1228  if (avctx->codec->supported_samplerates[i] == 0) {
1229  av_log(avctx, AV_LOG_ERROR, "Specified sample rate %d is not supported\n",
1230  avctx->sample_rate);
1231  ret = AVERROR(EINVAL);
1232  goto free_and_end;
1233  }
1234  }
1235  if (avctx->codec->channel_layouts) {
1236  if (!avctx->channel_layout) {
1237  av_log(avctx, AV_LOG_WARNING, "Channel layout not specified\n");
1238  } else {
1239  for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
1240  if (avctx->channel_layout == avctx->codec->channel_layouts[i])
1241  break;
1242  if (avctx->codec->channel_layouts[i] == 0) {
1243  char buf[512];
1244  av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
1245  av_log(avctx, AV_LOG_ERROR, "Specified channel layout '%s' is not supported\n", buf);
1246  ret = AVERROR(EINVAL);
1247  goto free_and_end;
1248  }
1249  }
1250  }
1251  if (avctx->channel_layout && avctx->channels) {
1252  int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
1253  if (channels != avctx->channels) {
1254  char buf[512];
1255  av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
1256  av_log(avctx, AV_LOG_ERROR,
1257  "Channel layout '%s' with %d channels does not match number of specified channels %d\n",
1258  buf, channels, avctx->channels);
1259  ret = AVERROR(EINVAL);
1260  goto free_and_end;
1261  }
1262  } else if (avctx->channel_layout) {
1264  }
1265  if(avctx->codec_type == AVMEDIA_TYPE_VIDEO &&
1266  avctx->codec_id != AV_CODEC_ID_PNG // For mplayer
1267  ) {
1268  if (avctx->width <= 0 || avctx->height <= 0) {
1269  av_log(avctx, AV_LOG_ERROR, "dimensions not set\n");
1270  ret = AVERROR(EINVAL);
1271  goto free_and_end;
1272  }
1273  }
1274  if ( (avctx->codec_type == AVMEDIA_TYPE_VIDEO || avctx->codec_type == AVMEDIA_TYPE_AUDIO)
1275  && avctx->bit_rate>0 && avctx->bit_rate<1000) {
1276  av_log(avctx, AV_LOG_WARNING, "Bitrate %d is extremely low, maybe you mean %dk\n", avctx->bit_rate, avctx->bit_rate);
1277  }
1278 
1279  if (!avctx->rc_initial_buffer_occupancy)
1280  avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3 / 4;
1281  }
1282 
1284  avctx->pts_correction_num_faulty_dts = 0;
1285  avctx->pts_correction_last_pts =
1286  avctx->pts_correction_last_dts = INT64_MIN;
1287 
1288  if ( avctx->codec->init && (!(avctx->active_thread_type&FF_THREAD_FRAME)
1289  || avctx->internal->frame_thread_encoder)) {
1290  ret = avctx->codec->init(avctx);
1291  if (ret < 0) {
1292  goto free_and_end;
1293  }
1294  }
1295 
1296  ret=0;
1297 
1298  if (av_codec_is_decoder(avctx->codec)) {
1299  if (!avctx->bit_rate)
1300  avctx->bit_rate = get_bit_rate(avctx);
1301  /* validate channel layout from the decoder */
1302  if (avctx->channel_layout) {
1303  int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
1304  if (!avctx->channels)
1305  avctx->channels = channels;
1306  else if (channels != avctx->channels) {
1307  char buf[512];
1308  av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
1309  av_log(avctx, AV_LOG_WARNING,
1310  "Channel layout '%s' with %d channels does not match specified number of channels %d: "
1311  "ignoring specified channel layout\n",
1312  buf, channels, avctx->channels);
1313  avctx->channel_layout = 0;
1314  }
1315  }
1316  if (avctx->channels && avctx->channels < 0 ||
1317  avctx->channels > FF_SANE_NB_CHANNELS) {
1318  ret = AVERROR(EINVAL);
1319  goto free_and_end;
1320  }
1321  if (avctx->sub_charenc) {
1322  if (avctx->codec_type != AVMEDIA_TYPE_SUBTITLE) {
1323  av_log(avctx, AV_LOG_ERROR, "Character encoding is only "
1324  "supported with subtitles codecs\n");
1325  ret = AVERROR(EINVAL);
1326  goto free_and_end;
1327  } else if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB) {
1328  av_log(avctx, AV_LOG_WARNING, "Codec '%s' is bitmap-based, "
1329  "subtitles character encoding will be ignored\n",
1330  avctx->codec_descriptor->name);
1332  } else {
1333  /* input character encoding is set for a text based subtitle
1334  * codec at this point */
1337 
1339 #if CONFIG_ICONV
1340  iconv_t cd = iconv_open("UTF-8", avctx->sub_charenc);
1341  if (cd == (iconv_t)-1) {
1342  av_log(avctx, AV_LOG_ERROR, "Unable to open iconv context "
1343  "with input character encoding \"%s\"\n", avctx->sub_charenc);
1344  ret = AVERROR(errno);
1345  goto free_and_end;
1346  }
1347  iconv_close(cd);
1348 #else
1349  av_log(avctx, AV_LOG_ERROR, "Character encoding subtitles "
1350  "conversion needs a libavcodec built with iconv support "
1351  "for this codec\n");
1352  ret = AVERROR(ENOSYS);
1353  goto free_and_end;
1354 #endif
1355  }
1356  }
1357  }
1358  }
1359 end:
1361  if (options) {
1362  av_dict_free(options);
1363  *options = tmp;
1364  }
1365 
1366  return ret;
1367 free_and_end:
1368  av_dict_free(&tmp);
1369  av_freep(&avctx->priv_data);
1370  if (avctx->internal)
1371  av_freep(&avctx->internal->pool);
1372  av_freep(&avctx->internal);
1373  avctx->codec = NULL;
1374  goto end;
1375 }
1376 
1378 {
1379  if (size < 0 || avpkt->size < 0 || size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
1380  av_log(avctx, AV_LOG_ERROR, "Size %d invalid\n", size);
1381  return AVERROR(EINVAL);
1382  }
1383 
1384  if (avctx) {
1385  av_assert0(!avpkt->data || avpkt->data != avctx->internal->byte_buffer);
1386  if (!avpkt->data || avpkt->size < size) {
1388  avpkt->data = avctx->internal->byte_buffer;
1389  avpkt->size = avctx->internal->byte_buffer_size;
1390  avpkt->destruct = NULL;
1391  }
1392  }
1393 
1394  if (avpkt->data) {
1395  AVBufferRef *buf = avpkt->buf;
1396 #if FF_API_DESTRUCT_PACKET
1397  void *destruct = avpkt->destruct;
1398 #endif
1399 
1400  if (avpkt->size < size) {
1401  av_log(avctx, AV_LOG_ERROR, "User packet is too small (%d < %d)\n", avpkt->size, size);
1402  return AVERROR(EINVAL);
1403  }
1404 
1405  av_init_packet(avpkt);
1406 #if FF_API_DESTRUCT_PACKET
1407  avpkt->destruct = destruct;
1408 #endif
1409  avpkt->buf = buf;
1410  avpkt->size = size;
1411  return 0;
1412  } else {
1413  int ret = av_new_packet(avpkt, size);
1414  if (ret < 0)
1415  av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %d\n", size);
1416  return ret;
1417  }
1418 }
1419 
1421 {
1422  return ff_alloc_packet2(NULL, avpkt, size);
1423 }
1424 
1425 /**
1426  * Pad last frame with silence.
1427  */
1429 {
1430  AVFrame *frame = NULL;
1431  uint8_t *buf = NULL;
1432  int ret;
1433 
1434  if (!(frame = avcodec_alloc_frame()))
1435  return AVERROR(ENOMEM);
1436  *frame = *src;
1437 
1438  if ((ret = av_samples_get_buffer_size(&frame->linesize[0], s->channels,
1439  s->frame_size, s->sample_fmt, 0)) < 0)
1440  goto fail;
1441 
1442  if (!(buf = av_malloc(ret))) {
1443  ret = AVERROR(ENOMEM);
1444  goto fail;
1445  }
1446 
1447  frame->nb_samples = s->frame_size;
1448  if ((ret = avcodec_fill_audio_frame(frame, s->channels, s->sample_fmt,
1449  buf, ret, 0)) < 0)
1450  goto fail;
1451  if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
1452  src->nb_samples, s->channels, s->sample_fmt)) < 0)
1453  goto fail;
1454  if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
1455  frame->nb_samples - src->nb_samples,
1456  s->channels, s->sample_fmt)) < 0)
1457  goto fail;
1458 
1459  *dst = frame;
1460 
1461  return 0;
1462 
1463 fail:
1464  if (frame->extended_data != frame->data)
1465  av_freep(&frame->extended_data);
1466  av_freep(&buf);
1467  av_freep(&frame);
1468  return ret;
1469 }
1470 
1472  AVPacket *avpkt,
1473  const AVFrame *frame,
1474  int *got_packet_ptr)
1475 {
1476  AVFrame tmp;
1477  AVFrame *padded_frame = NULL;
1478  int ret;
1479  AVPacket user_pkt = *avpkt;
1480  int needs_realloc = !user_pkt.data;
1481 
1482  *got_packet_ptr = 0;
1483 
1484  if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
1485  av_free_packet(avpkt);
1486  av_init_packet(avpkt);
1487  return 0;
1488  }
1489 
1490  /* ensure that extended_data is properly set */
1491  if (frame && !frame->extended_data) {
1492  if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
1493  avctx->channels > AV_NUM_DATA_POINTERS) {
1494  av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
1495  "with more than %d channels, but extended_data is not set.\n",
1497  return AVERROR(EINVAL);
1498  }
1499  av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
1500 
1501  tmp = *frame;
1502  tmp.extended_data = tmp.data;
1503  frame = &tmp;
1504  }
1505 
1506  /* check for valid frame size */
1507  if (frame) {
1509  if (frame->nb_samples > avctx->frame_size) {
1510  av_log(avctx, AV_LOG_ERROR, "more samples than frame size (avcodec_encode_audio2)\n");
1511  return AVERROR(EINVAL);
1512  }
1513  } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
1514  if (frame->nb_samples < avctx->frame_size &&
1515  !avctx->internal->last_audio_frame) {
1516  ret = pad_last_frame(avctx, &padded_frame, frame);
1517  if (ret < 0)
1518  return ret;
1519 
1520  frame = padded_frame;
1521  avctx->internal->last_audio_frame = 1;
1522  }
1523 
1524  if (frame->nb_samples != avctx->frame_size) {
1525  av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) != frame_size (%d) (avcodec_encode_audio2)\n", frame->nb_samples, avctx->frame_size);
1526  ret = AVERROR(EINVAL);
1527  goto end;
1528  }
1529  }
1530  }
1531 
1532  ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1533  if (!ret) {
1534  if (*got_packet_ptr) {
1535  if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
1536  if (avpkt->pts == AV_NOPTS_VALUE)
1537  avpkt->pts = frame->pts;
1538  if (!avpkt->duration)
1539  avpkt->duration = ff_samples_to_time_base(avctx,
1540  frame->nb_samples);
1541  }
1542  avpkt->dts = avpkt->pts;
1543  } else {
1544  avpkt->size = 0;
1545  }
1546  }
1547  if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
1548  needs_realloc = 0;
1549  if (user_pkt.data) {
1550  if (user_pkt.size >= avpkt->size) {
1551  memcpy(user_pkt.data, avpkt->data, avpkt->size);
1552  } else {
1553  av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
1554  avpkt->size = user_pkt.size;
1555  ret = -1;
1556  }
1557  avpkt->buf = user_pkt.buf;
1558  avpkt->data = user_pkt.data;
1559  avpkt->destruct = user_pkt.destruct;
1560  } else {
1561  if (av_dup_packet(avpkt) < 0) {
1562  ret = AVERROR(ENOMEM);
1563  }
1564  }
1565  }
1566 
1567  if (!ret) {
1568  if (needs_realloc && avpkt->data) {
1569  ret = av_buffer_realloc(&avpkt->buf, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
1570  if (ret >= 0)
1571  avpkt->data = avpkt->buf->data;
1572  }
1573 
1574  avctx->frame_number++;
1575  }
1576 
1577  if (ret < 0 || !*got_packet_ptr) {
1578  av_free_packet(avpkt);
1579  av_init_packet(avpkt);
1580  goto end;
1581  }
1582 
1583  /* NOTE: if we add any audio encoders which output non-keyframe packets,
1584  * this needs to be moved to the encoders, but for now we can do it
1585  * here to simplify things */
1586  avpkt->flags |= AV_PKT_FLAG_KEY;
1587 
1588 end:
1589  if (padded_frame) {
1590  av_freep(&padded_frame->data[0]);
1591  if (padded_frame->extended_data != padded_frame->data)
1592  av_freep(&padded_frame->extended_data);
1593  av_freep(&padded_frame);
1594  }
1595 
1596  return ret;
1597 }
1598 
1599 #if FF_API_OLD_ENCODE_AUDIO
1601  uint8_t *buf, int buf_size,
1602  const short *samples)
1603 {
1604  AVPacket pkt;
1605  AVFrame frame0 = { { 0 } };
1606  AVFrame *frame;
1607  int ret, samples_size, got_packet;
1608 
1609  av_init_packet(&pkt);
1610  pkt.data = buf;
1611  pkt.size = buf_size;
1612 
1613  if (samples) {
1614  frame = &frame0;
1616 
1617  if (avctx->frame_size) {
1618  frame->nb_samples = avctx->frame_size;
1619  } else {
1620  /* if frame_size is not set, the number of samples must be
1621  * calculated from the buffer size */
1622  int64_t nb_samples;
1623  if (!av_get_bits_per_sample(avctx->codec_id)) {
1624  av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
1625  "support this codec\n");
1626  return AVERROR(EINVAL);
1627  }
1628  nb_samples = (int64_t)buf_size * 8 /
1629  (av_get_bits_per_sample(avctx->codec_id) *
1630  avctx->channels);
1631  if (nb_samples >= INT_MAX)
1632  return AVERROR(EINVAL);
1633  frame->nb_samples = nb_samples;
1634  }
1635 
1636  /* it is assumed that the samples buffer is large enough based on the
1637  * relevant parameters */
1638  samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
1639  frame->nb_samples,
1640  avctx->sample_fmt, 1);
1641  if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
1642  avctx->sample_fmt,
1643  (const uint8_t *)samples,
1644  samples_size, 1)) < 0)
1645  return ret;
1646 
1647  /* fabricate frame pts from sample count.
1648  * this is needed because the avcodec_encode_audio() API does not have
1649  * a way for the user to provide pts */
1650  if (avctx->sample_rate && avctx->time_base.num)
1651  frame->pts = ff_samples_to_time_base(avctx,
1652  avctx->internal->sample_count);
1653  else
1654  frame->pts = AV_NOPTS_VALUE;
1655  avctx->internal->sample_count += frame->nb_samples;
1656  } else {
1657  frame = NULL;
1658  }
1659 
1660  got_packet = 0;
1661  ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
1662  if (!ret && got_packet && avctx->coded_frame) {
1663  avctx->coded_frame->pts = pkt.pts;
1664  avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
1665  }
1666  /* free any side data since we cannot return it */
1668 
1669  if (frame && frame->extended_data != frame->data)
1670  av_freep(&frame->extended_data);
1671 
1672  return ret ? ret : pkt.size;
1673 }
1674 
1675 #endif
1676 
1677 #if FF_API_OLD_ENCODE_VIDEO
1679  const AVFrame *pict)
1680 {
1681  AVPacket pkt;
1682  int ret, got_packet = 0;
1683 
1684  if (buf_size < FF_MIN_BUFFER_SIZE) {
1685  av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
1686  return -1;
1687  }
1688 
1689  av_init_packet(&pkt);
1690  pkt.data = buf;
1691  pkt.size = buf_size;
1692 
1693  ret = avcodec_encode_video2(avctx, &pkt, pict, &got_packet);
1694  if (!ret && got_packet && avctx->coded_frame) {
1695  avctx->coded_frame->pts = pkt.pts;
1696  avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
1697  }
1698 
1699  /* free any side data since we cannot return it */
1700  if (pkt.side_data_elems > 0) {
1701  int i;
1702  for (i = 0; i < pkt.side_data_elems; i++)
1703  av_free(pkt.side_data[i].data);
1704  av_freep(&pkt.side_data);
1705  pkt.side_data_elems = 0;
1706  }
1707 
1708  return ret ? ret : pkt.size;
1709 }
1710 
1711 #endif
1712 
1714  AVPacket *avpkt,
1715  const AVFrame *frame,
1716  int *got_packet_ptr)
1717 {
1718  int ret;
1719  AVPacket user_pkt = *avpkt;
1720  int needs_realloc = !user_pkt.data;
1721 
1722  *got_packet_ptr = 0;
1723 
1726  return ff_thread_video_encode_frame(avctx, avpkt, frame, got_packet_ptr);
1727 
1728  if ((avctx->flags&CODEC_FLAG_PASS1) && avctx->stats_out)
1729  avctx->stats_out[0] = '\0';
1730 
1731  if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
1732  av_free_packet(avpkt);
1733  av_init_packet(avpkt);
1734  avpkt->size = 0;
1735  return 0;
1736  }
1737 
1738  if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
1739  return AVERROR(EINVAL);
1740 
1741  av_assert0(avctx->codec->encode2);
1742 
1743  ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1744  av_assert0(ret <= 0);
1745 
1746  if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
1747  needs_realloc = 0;
1748  if (user_pkt.data) {
1749  if (user_pkt.size >= avpkt->size) {
1750  memcpy(user_pkt.data, avpkt->data, avpkt->size);
1751  } else {
1752  av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
1753  avpkt->size = user_pkt.size;
1754  ret = -1;
1755  }
1756  avpkt->buf = user_pkt.buf;
1757  avpkt->data = user_pkt.data;
1758  avpkt->destruct = user_pkt.destruct;
1759  } else {
1760  if (av_dup_packet(avpkt) < 0) {
1761  ret = AVERROR(ENOMEM);
1762  }
1763  }
1764  }
1765 
1766  if (!ret) {
1767  if (!*got_packet_ptr)
1768  avpkt->size = 0;
1769  else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
1770  avpkt->pts = avpkt->dts = frame->pts;
1771 
1772  if (needs_realloc && avpkt->data) {
1773  ret = av_buffer_realloc(&avpkt->buf, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
1774  if (ret >= 0)
1775  avpkt->data = avpkt->buf->data;
1776  }
1777 
1778  avctx->frame_number++;
1779  }
1780 
1781  if (ret < 0 || !*got_packet_ptr)
1782  av_free_packet(avpkt);
1783  else
1785 
1786  emms_c();
1787  return ret;
1788 }
1789 
1791  const AVSubtitle *sub)
1792 {
1793  int ret;
1794  if (sub->start_display_time) {
1795  av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
1796  return -1;
1797  }
1798 
1799  ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
1800  avctx->frame_number++;
1801  return ret;
1802 }
1803 
1804 /**
1805  * Attempt to guess proper monotonic timestamps for decoded video frames
1806  * which might have incorrect times. Input timestamps may wrap around, in
1807  * which case the output will as well.
1808  *
1809  * @param pts the pts field of the decoded AVPacket, as passed through
1810  * AVFrame.pkt_pts
1811  * @param dts the dts field of the decoded AVPacket
1812  * @return one of the input values, may be AV_NOPTS_VALUE
1813  */
1814 static int64_t guess_correct_pts(AVCodecContext *ctx,
1815  int64_t reordered_pts, int64_t dts)
1816 {
1817  int64_t pts = AV_NOPTS_VALUE;
1818 
1819  if (dts != AV_NOPTS_VALUE) {
1821  ctx->pts_correction_last_dts = dts;
1822  }
1823  if (reordered_pts != AV_NOPTS_VALUE) {
1824  ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
1825  ctx->pts_correction_last_pts = reordered_pts;
1826  }
1828  && reordered_pts != AV_NOPTS_VALUE)
1829  pts = reordered_pts;
1830  else
1831  pts = dts;
1832 
1833  return pts;
1834 }
1835 
1836 static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
1837 {
1838  int size = 0;
1839  const uint8_t *data;
1840  uint32_t flags;
1841 
1842  if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
1843  return;
1844 
1845  data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
1846  if (!data || size < 4)
1847  return;
1848  flags = bytestream_get_le32(&data);
1849  size -= 4;
1850  if (size < 4) /* Required for any of the changes */
1851  return;
1853  avctx->channels = bytestream_get_le32(&data);
1854  size -= 4;
1855  }
1857  if (size < 8)
1858  return;
1859  avctx->channel_layout = bytestream_get_le64(&data);
1860  size -= 8;
1861  }
1862  if (size < 4)
1863  return;
1865  avctx->sample_rate = bytestream_get_le32(&data);
1866  size -= 4;
1867  }
1869  if (size < 8)
1870  return;
1871  avctx->width = bytestream_get_le32(&data);
1872  avctx->height = bytestream_get_le32(&data);
1873  avcodec_set_dimensions(avctx, avctx->width, avctx->height);
1874  size -= 8;
1875  }
1876 }
1877 
1879 {
1880  int size, ret = 0;
1881  const uint8_t *side_metadata;
1882  const uint8_t *end;
1883 
1884  side_metadata = av_packet_get_side_data(avctx->pkt,
1886  if (!side_metadata)
1887  goto end;
1888  end = side_metadata + size;
1889  while (side_metadata < end) {
1890  const uint8_t *key = side_metadata;
1891  const uint8_t *val = side_metadata + strlen(key) + 1;
1892  int ret = av_dict_set(avpriv_frame_get_metadatap(frame), key, val, 0);
1893  if (ret < 0)
1894  break;
1895  side_metadata = val + strlen(val) + 1;
1896  }
1897 end:
1898  return ret;
1899 }
1900 
1902  int *got_picture_ptr,
1903  const AVPacket *avpkt)
1904 {
1905  AVCodecInternal *avci = avctx->internal;
1906  int ret;
1907  // copy to ensure we do not change avpkt
1908  AVPacket tmp = *avpkt;
1909 
1910  if (avctx->codec->type != AVMEDIA_TYPE_VIDEO) {
1911  av_log(avctx, AV_LOG_ERROR, "Invalid media type for video\n");
1912  return AVERROR(EINVAL);
1913  }
1914 
1915  *got_picture_ptr = 0;
1916  if ((avctx->coded_width || avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
1917  return AVERROR(EINVAL);
1918 
1919  avcodec_get_frame_defaults(picture);
1920 
1921  if (!avctx->refcounted_frames)
1922  av_frame_unref(&avci->to_free);
1923 
1924  if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
1925  int did_split = av_packet_split_side_data(&tmp);
1926  apply_param_change(avctx, &tmp);
1927  avctx->pkt = &tmp;
1929  ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
1930  &tmp);
1931  else {
1932  ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
1933  &tmp);
1934  picture->pkt_dts = avpkt->dts;
1935 
1936  if(!avctx->has_b_frames){
1937  av_frame_set_pkt_pos(picture, avpkt->pos);
1938  }
1939  //FIXME these should be under if(!avctx->has_b_frames)
1940  /* get_buffer is supposed to set frame parameters */
1941  if (!(avctx->codec->capabilities & CODEC_CAP_DR1)) {
1942  if (!picture->sample_aspect_ratio.num) picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
1943  if (!picture->width) picture->width = avctx->width;
1944  if (!picture->height) picture->height = avctx->height;
1945  if (picture->format == AV_PIX_FMT_NONE) picture->format = avctx->pix_fmt;
1946  }
1947  }
1948  add_metadata_from_side_data(avctx, picture);
1949 
1950  emms_c(); //needed to avoid an emms_c() call before every return;
1951 
1952  avctx->pkt = NULL;
1953  if (did_split) {
1955  if(ret == tmp.size)
1956  ret = avpkt->size;
1957  }
1958 
1959  if (ret < 0 && picture->data[0])
1960  av_frame_unref(picture);
1961 
1962  if (*got_picture_ptr) {
1963  if (!avctx->refcounted_frames) {
1964  avci->to_free = *picture;
1965  avci->to_free.extended_data = avci->to_free.data;
1966  }
1967 
1968  avctx->frame_number++;
1970  guess_correct_pts(avctx,
1971  picture->pkt_pts,
1972  picture->pkt_dts));
1973  }
1974  } else
1975  ret = 0;
1976 
1977  /* many decoders assign whole AVFrames, thus overwriting extended_data;
1978  * make sure it's set correctly */
1979  picture->extended_data = picture->data;
1980 
1981  return ret;
1982 }
1983 
1984 #if FF_API_OLD_DECODE_AUDIO
1986  int *frame_size_ptr,
1987  AVPacket *avpkt)
1988 {
1989  AVFrame frame = { { 0 } };
1990  int ret, got_frame = 0;
1991 
1992  if (avctx->get_buffer != avcodec_default_get_buffer) {
1993  av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
1994  "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
1995  av_log(avctx, AV_LOG_ERROR, "Please port your application to "
1996  "avcodec_decode_audio4()\n");
1997  avctx->get_buffer = avcodec_default_get_buffer;
1998  avctx->release_buffer = avcodec_default_release_buffer;
1999  }
2000 
2001  ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
2002 
2003  if (ret >= 0 && got_frame) {
2004  int ch, plane_size;
2005  int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
2006  int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
2007  frame.nb_samples,
2008  avctx->sample_fmt, 1);
2009  if (*frame_size_ptr < data_size) {
2010  av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
2011  "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
2012  return AVERROR(EINVAL);
2013  }
2014 
2015  memcpy(samples, frame.extended_data[0], plane_size);
2016 
2017  if (planar && avctx->channels > 1) {
2018  uint8_t *out = ((uint8_t *)samples) + plane_size;
2019  for (ch = 1; ch < avctx->channels; ch++) {
2020  memcpy(out, frame.extended_data[ch], plane_size);
2021  out += plane_size;
2022  }
2023  }
2024  *frame_size_ptr = data_size;
2025  } else {
2026  *frame_size_ptr = 0;
2027  }
2028  return ret;
2029 }
2030 
2031 #endif
2032 
2034  AVFrame *frame,
2035  int *got_frame_ptr,
2036  const AVPacket *avpkt)
2037 {
2038  AVCodecInternal *avci = avctx->internal;
2039  int planar, channels;
2040  int ret = 0;
2041 
2042  *got_frame_ptr = 0;
2043 
2044  if (!avpkt->data && avpkt->size) {
2045  av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
2046  return AVERROR(EINVAL);
2047  }
2048  if (avctx->codec->type != AVMEDIA_TYPE_AUDIO) {
2049  av_log(avctx, AV_LOG_ERROR, "Invalid media type for audio\n");
2050  return AVERROR(EINVAL);
2051  }
2052 
2054 
2055  if (!avctx->refcounted_frames)
2056  av_frame_unref(&avci->to_free);
2057 
2058  if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
2059  uint8_t *side;
2060  int side_size;
2061  // copy to ensure we do not change avpkt
2062  AVPacket tmp = *avpkt;
2063  int did_split = av_packet_split_side_data(&tmp);
2064  apply_param_change(avctx, &tmp);
2065 
2066  avctx->pkt = &tmp;
2067  ret = avctx->codec->decode(avctx, frame, got_frame_ptr, &tmp);
2068  if (ret >= 0 && *got_frame_ptr) {
2069  add_metadata_from_side_data(avctx, frame);
2070  avctx->frame_number++;
2071  frame->pkt_dts = avpkt->dts;
2073  guess_correct_pts(avctx,
2074  frame->pkt_pts,
2075  frame->pkt_dts));
2076  if (frame->format == AV_SAMPLE_FMT_NONE)
2077  frame->format = avctx->sample_fmt;
2078  if (!frame->channel_layout)
2079  frame->channel_layout = avctx->channel_layout;
2080  if (!av_frame_get_channels(frame))
2081  av_frame_set_channels(frame, avctx->channels);
2082  if (!frame->sample_rate)
2083  frame->sample_rate = avctx->sample_rate;
2084  if (!avctx->refcounted_frames) {
2085  avci->to_free = *frame;
2086  avci->to_free.extended_data = avci->to_free.data;
2087  }
2088  }
2089 
2090  side= av_packet_get_side_data(avctx->pkt, AV_PKT_DATA_SKIP_SAMPLES, &side_size);
2091  if(side && side_size>=10) {
2092  avctx->internal->skip_samples = AV_RL32(side);
2093  av_log(avctx, AV_LOG_DEBUG, "skip %d samples due to side data\n",
2094  avctx->internal->skip_samples);
2095  }
2096  if (avctx->internal->skip_samples && *got_frame_ptr) {
2097  if(frame->nb_samples <= avctx->internal->skip_samples){
2098  *got_frame_ptr = 0;
2099  avctx->internal->skip_samples -= frame->nb_samples;
2100  av_log(avctx, AV_LOG_DEBUG, "skip whole frame, skip left: %d\n",
2101  avctx->internal->skip_samples);
2102  } else {
2104  frame->nb_samples - avctx->internal->skip_samples, avctx->channels, frame->format);
2105  if(avctx->pkt_timebase.num && avctx->sample_rate) {
2106  int64_t diff_ts = av_rescale_q(avctx->internal->skip_samples,
2107  (AVRational){1, avctx->sample_rate},
2108  avctx->pkt_timebase);
2109  if(frame->pkt_pts!=AV_NOPTS_VALUE)
2110  frame->pkt_pts += diff_ts;
2111  if(frame->pkt_dts!=AV_NOPTS_VALUE)
2112  frame->pkt_dts += diff_ts;
2113  if (av_frame_get_pkt_duration(frame) >= diff_ts)
2114  av_frame_set_pkt_duration(frame, av_frame_get_pkt_duration(frame) - diff_ts);
2115  } else {
2116  av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for skipped samples.\n");
2117  }
2118  av_log(avctx, AV_LOG_DEBUG, "skip %d/%d samples\n",
2119  avctx->internal->skip_samples, frame->nb_samples);
2120  frame->nb_samples -= avctx->internal->skip_samples;
2121  avctx->internal->skip_samples = 0;
2122  }
2123  }
2124 
2125  avctx->pkt = NULL;
2126  if (did_split) {
2128  if(ret == tmp.size)
2129  ret = avpkt->size;
2130  }
2131 
2132  if (ret < 0 && frame->data[0])
2133  av_frame_unref(frame);
2134  }
2135 
2136  /* many decoders assign whole AVFrames, thus overwriting extended_data;
2137  * make sure it's set correctly; assume decoders that actually use
2138  * extended_data are doing it correctly */
2139  if (*got_frame_ptr) {
2140  planar = av_sample_fmt_is_planar(frame->format);
2141  channels = av_frame_get_channels(frame);
2142  if (!(planar && channels > AV_NUM_DATA_POINTERS))
2143  frame->extended_data = frame->data;
2144  } else {
2145  frame->extended_data = NULL;
2146  }
2147 
2148  return ret;
2149 }
2150 
2151 #define UTF8_MAX_BYTES 4 /* 5 and 6 bytes sequences should not be used */
2153  AVPacket *outpkt, const AVPacket *inpkt)
2154 {
2155 #if CONFIG_ICONV
2156  iconv_t cd = (iconv_t)-1;
2157  int ret = 0;
2158  char *inb, *outb;
2159  size_t inl, outl;
2160  AVPacket tmp;
2161 #endif
2162 
2164  return 0;
2165 
2166 #if CONFIG_ICONV
2167  cd = iconv_open("UTF-8", avctx->sub_charenc);
2168  av_assert0(cd != (iconv_t)-1);
2169 
2170  inb = inpkt->data;
2171  inl = inpkt->size;
2172 
2173  if (inl >= INT_MAX / UTF8_MAX_BYTES - FF_INPUT_BUFFER_PADDING_SIZE) {
2174  av_log(avctx, AV_LOG_ERROR, "Subtitles packet is too big for recoding\n");
2175  ret = AVERROR(ENOMEM);
2176  goto end;
2177  }
2178 
2179  ret = av_new_packet(&tmp, inl * UTF8_MAX_BYTES);
2180  if (ret < 0)
2181  goto end;
2182  outpkt->buf = tmp.buf;
2183  outpkt->data = tmp.data;
2184  outpkt->size = tmp.size;
2185  outb = outpkt->data;
2186  outl = outpkt->size;
2187 
2188  if (iconv(cd, &inb, &inl, &outb, &outl) == (size_t)-1 ||
2189  iconv(cd, NULL, NULL, &outb, &outl) == (size_t)-1 ||
2190  outl >= outpkt->size || inl != 0) {
2191  av_log(avctx, AV_LOG_ERROR, "Unable to recode subtitle event \"%s\" "
2192  "from %s to UTF-8\n", inpkt->data, avctx->sub_charenc);
2193  av_free_packet(&tmp);
2194  ret = AVERROR(errno);
2195  goto end;
2196  }
2197  outpkt->size -= outl;
2198  memset(outpkt->data + outpkt->size, 0, outl);
2199 
2200 end:
2201  if (cd != (iconv_t)-1)
2202  iconv_close(cd);
2203  return ret;
2204 #else
2205  av_assert0(!"requesting subtitles recoding without iconv");
2206 #endif
2207 }
2208 
2210  int *got_sub_ptr,
2211  AVPacket *avpkt)
2212 {
2213  int ret = 0;
2214 
2215  if (avctx->codec->type != AVMEDIA_TYPE_SUBTITLE) {
2216  av_log(avctx, AV_LOG_ERROR, "Invalid media type for subtitles\n");
2217  return AVERROR(EINVAL);
2218  }
2219 
2220  *got_sub_ptr = 0;
2222 
2223  if (avpkt->size) {
2224  AVPacket pkt_recoded;
2225  AVPacket tmp = *avpkt;
2226  int did_split = av_packet_split_side_data(&tmp);
2227  //apply_param_change(avctx, &tmp);
2228 
2229  pkt_recoded = tmp;
2230  ret = recode_subtitle(avctx, &pkt_recoded, &tmp);
2231  if (ret < 0) {
2232  *got_sub_ptr = 0;
2233  } else {
2234  avctx->pkt = &pkt_recoded;
2235 
2236  if (avctx->pkt_timebase.den && avpkt->pts != AV_NOPTS_VALUE)
2237  sub->pts = av_rescale_q(avpkt->pts,
2238  avctx->pkt_timebase, AV_TIME_BASE_Q);
2239  ret = avctx->codec->decode(avctx, sub, got_sub_ptr, &pkt_recoded);
2240  av_assert1((ret >= 0) >= !!*got_sub_ptr &&
2241  !!*got_sub_ptr >= !!sub->num_rects);
2242 
2243  if (sub->num_rects && !sub->end_display_time && avpkt->duration &&
2244  avctx->pkt_timebase.num) {
2245  AVRational ms = { 1, 1000 };
2246  sub->end_display_time = av_rescale_q(avpkt->duration,
2247  avctx->pkt_timebase, ms);
2248  }
2249 
2250  if (tmp.data != pkt_recoded.data) { // did we recode?
2251  /* prevent from destroying side data from original packet */
2252  pkt_recoded.side_data = NULL;
2253  pkt_recoded.side_data_elems = 0;
2254 
2255  av_free_packet(&pkt_recoded);
2256  }
2258  avctx->pkt = NULL;
2259  }
2260 
2261  if (did_split) {
2263  if(ret == tmp.size)
2264  ret = avpkt->size;
2265  }
2266 
2267  if (*got_sub_ptr)
2268  avctx->frame_number++;
2269  }
2270 
2271  return ret;
2272 }
2273 
2275 {
2276  int i;
2277 
2278  for (i = 0; i < sub->num_rects; i++) {
2279  av_freep(&sub->rects[i]->pict.data[0]);
2280  av_freep(&sub->rects[i]->pict.data[1]);
2281  av_freep(&sub->rects[i]->pict.data[2]);
2282  av_freep(&sub->rects[i]->pict.data[3]);
2283  av_freep(&sub->rects[i]->text);
2284  av_freep(&sub->rects[i]->ass);
2285  av_freep(&sub->rects[i]);
2286  }
2287 
2288  av_freep(&sub->rects);
2289 
2290  memset(sub, 0, sizeof(AVSubtitle));
2291 }
2292 
2294 {
2295  int ret = 0;
2296 
2298 
2299  ret = avcodec_close(avctx);
2300 
2302  return ret;
2303 }
2304 
2306 {
2307  int ret = ff_lock_avcodec(avctx);
2308  if (ret < 0)
2309  return ret;
2310 
2311  if (avcodec_is_open(avctx)) {
2312  FramePool *pool = avctx->internal->pool;
2313  int i;
2315  avctx->internal->frame_thread_encoder && avctx->thread_count > 1) {
2318  ff_lock_avcodec(avctx);
2319  }
2320  if (HAVE_THREADS && avctx->thread_opaque)
2321  ff_thread_free(avctx);
2322  if (avctx->codec && avctx->codec->close)
2323  avctx->codec->close(avctx);
2324  avctx->coded_frame = NULL;
2325  avctx->internal->byte_buffer_size = 0;
2326  av_freep(&avctx->internal->byte_buffer);
2327  if (!avctx->refcounted_frames)
2328  av_frame_unref(&avctx->internal->to_free);
2329  for (i = 0; i < FF_ARRAY_ELEMS(pool->pools); i++)
2330  av_buffer_pool_uninit(&pool->pools[i]);
2331  av_freep(&avctx->internal->pool);
2332  av_freep(&avctx->internal);
2333  }
2334 
2335  if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
2336  av_opt_free(avctx->priv_data);
2337  av_opt_free(avctx);
2338  av_freep(&avctx->priv_data);
2339  if (av_codec_is_encoder(avctx->codec))
2340  av_freep(&avctx->extradata);
2341  avctx->codec = NULL;
2342  avctx->active_thread_type = 0;
2343 
2345  return 0;
2346 }
2347 
2349 {
2350  switch(id){
2351  //This is for future deprecatec codec ids, its empty since
2352  //last major bump but will fill up again over time, please don't remove it
2353 // case AV_CODEC_ID_UTVIDEO_DEPRECATED: return AV_CODEC_ID_UTVIDEO;
2356  default : return id;
2357  }
2358 }
2359 
2360 static AVCodec *find_encdec(enum AVCodecID id, int encoder)
2361 {
2362  AVCodec *p, *experimental = NULL;
2363  p = first_avcodec;
2364  id= remap_deprecated_codec_id(id);
2365  while (p) {
2366  if ((encoder ? av_codec_is_encoder(p) : av_codec_is_decoder(p)) &&
2367  p->id == id) {
2368  if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
2369  experimental = p;
2370  } else
2371  return p;
2372  }
2373  p = p->next;
2374  }
2375  return experimental;
2376 }
2377 
2379 {
2380  return find_encdec(id, 1);
2381 }
2382 
2384 {
2385  AVCodec *p;
2386  if (!name)
2387  return NULL;
2388  p = first_avcodec;
2389  while (p) {
2390  if (av_codec_is_encoder(p) && strcmp(name, p->name) == 0)
2391  return p;
2392  p = p->next;
2393  }
2394  return NULL;
2395 }
2396 
2398 {
2399  return find_encdec(id, 0);
2400 }
2401 
2403 {
2404  AVCodec *p;
2405  if (!name)
2406  return NULL;
2407  p = first_avcodec;
2408  while (p) {
2409  if (av_codec_is_decoder(p) && strcmp(name, p->name) == 0)
2410  return p;
2411  p = p->next;
2412  }
2413  return NULL;
2414 }
2415 
2416 const char *avcodec_get_name(enum AVCodecID id)
2417 {
2418  const AVCodecDescriptor *cd;
2419  AVCodec *codec;
2420 
2421  if (id == AV_CODEC_ID_NONE)
2422  return "none";
2423  cd = avcodec_descriptor_get(id);
2424  if (cd)
2425  return cd->name;
2426  av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
2427  codec = avcodec_find_decoder(id);
2428  if (codec)
2429  return codec->name;
2430  codec = avcodec_find_encoder(id);
2431  if (codec)
2432  return codec->name;
2433  return "unknown_codec";
2434 }
2435 
2436 size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
2437 {
2438  int i, len, ret = 0;
2439 
2440 #define TAG_PRINT(x) \
2441  (((x) >= '0' && (x) <= '9') || \
2442  ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
2443  ((x) == '.' || (x) == ' ' || (x) == '-' || (x) == '_'))
2444 
2445  for (i = 0; i < 4; i++) {
2446  len = snprintf(buf, buf_size,
2447  TAG_PRINT(codec_tag & 0xFF) ? "%c" : "[%d]", codec_tag & 0xFF);
2448  buf += len;
2449  buf_size = buf_size > len ? buf_size - len : 0;
2450  ret += len;
2451  codec_tag >>= 8;
2452  }
2453  return ret;
2454 }
2455 
2456 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
2457 {
2458  const char *codec_type;
2459  const char *codec_name;
2460  const char *profile = NULL;
2461  const AVCodec *p;
2462  int bitrate;
2463  AVRational display_aspect_ratio;
2464 
2465  if (!buf || buf_size <= 0)
2466  return;
2467  codec_type = av_get_media_type_string(enc->codec_type);
2468  codec_name = avcodec_get_name(enc->codec_id);
2469  if (enc->profile != FF_PROFILE_UNKNOWN) {
2470  if (enc->codec)
2471  p = enc->codec;
2472  else
2473  p = encode ? avcodec_find_encoder(enc->codec_id) :
2475  if (p)
2476  profile = av_get_profile_name(p, enc->profile);
2477  }
2478 
2479  snprintf(buf, buf_size, "%s: %s%s", codec_type ? codec_type : "unknown",
2480  codec_name, enc->mb_decision ? " (hq)" : "");
2481  buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
2482  if (profile)
2483  snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
2484  if (enc->codec_tag) {
2485  char tag_buf[32];
2486  av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
2487  snprintf(buf + strlen(buf), buf_size - strlen(buf),
2488  " (%s / 0x%04X)", tag_buf, enc->codec_tag);
2489  }
2490 
2491  switch (enc->codec_type) {
2492  case AVMEDIA_TYPE_VIDEO:
2493  if (enc->pix_fmt != AV_PIX_FMT_NONE) {
2494  snprintf(buf + strlen(buf), buf_size - strlen(buf),
2495  ", %s",
2497  if (enc->bits_per_raw_sample &&
2499  snprintf(buf + strlen(buf), buf_size - strlen(buf),
2500  " (%d bpc)", enc->bits_per_raw_sample);
2501  }
2502  if (enc->width) {
2503  snprintf(buf + strlen(buf), buf_size - strlen(buf),
2504  ", %dx%d",
2505  enc->width, enc->height);
2506  if (enc->sample_aspect_ratio.num) {
2507  av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
2508  enc->width * enc->sample_aspect_ratio.num,
2509  enc->height * enc->sample_aspect_ratio.den,
2510  1024 * 1024);
2511  snprintf(buf + strlen(buf), buf_size - strlen(buf),
2512  " [SAR %d:%d DAR %d:%d]",
2514  display_aspect_ratio.num, display_aspect_ratio.den);
2515  }
2516  if (av_log_get_level() >= AV_LOG_DEBUG) {
2517  int g = av_gcd(enc->time_base.num, enc->time_base.den);
2518  snprintf(buf + strlen(buf), buf_size - strlen(buf),
2519  ", %d/%d",
2520  enc->time_base.num / g, enc->time_base.den / g);
2521  }
2522  }
2523  if (encode) {
2524  snprintf(buf + strlen(buf), buf_size - strlen(buf),
2525  ", q=%d-%d", enc->qmin, enc->qmax);
2526  }
2527  break;
2528  case AVMEDIA_TYPE_AUDIO:
2529  if (enc->sample_rate) {
2530  snprintf(buf + strlen(buf), buf_size - strlen(buf),
2531  ", %d Hz", enc->sample_rate);
2532  }
2533  av_strlcat(buf, ", ", buf_size);
2534  av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
2535  if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
2536  snprintf(buf + strlen(buf), buf_size - strlen(buf),
2537  ", %s", av_get_sample_fmt_name(enc->sample_fmt));
2538  }
2539  break;
2540  case AVMEDIA_TYPE_DATA:
2541  if (av_log_get_level() >= AV_LOG_DEBUG) {
2542  int g = av_gcd(enc->time_base.num, enc->time_base.den);
2543  if (g)
2544  snprintf(buf + strlen(buf), buf_size - strlen(buf),
2545  ", %d/%d",
2546  enc->time_base.num / g, enc->time_base.den / g);
2547  }
2548  break;
2549  default:
2550  return;
2551  }
2552  if (encode) {
2553  if (enc->flags & CODEC_FLAG_PASS1)
2554  snprintf(buf + strlen(buf), buf_size - strlen(buf),
2555  ", pass 1");
2556  if (enc->flags & CODEC_FLAG_PASS2)
2557  snprintf(buf + strlen(buf), buf_size - strlen(buf),
2558  ", pass 2");
2559  }
2560  bitrate = get_bit_rate(enc);
2561  if (bitrate != 0) {
2562  snprintf(buf + strlen(buf), buf_size - strlen(buf),
2563  ", %d kb/s", bitrate / 1000);
2564  }
2565 }
2566 
2567 const char *av_get_profile_name(const AVCodec *codec, int profile)
2568 {
2569  const AVProfile *p;
2570  if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
2571  return NULL;
2572 
2573  for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
2574  if (p->profile == profile)
2575  return p->name;
2576 
2577  return NULL;
2578 }
2579 
2580 unsigned avcodec_version(void)
2581 {
2582 // av_assert0(AV_CODEC_ID_V410==164);
2585 // av_assert0(AV_CODEC_ID_BMV_AUDIO==86071);
2586  av_assert0(AV_CODEC_ID_SRT==94216);
2588 
2594  return LIBAVCODEC_VERSION_INT;
2595 }
2596 
2597 const char *avcodec_configuration(void)
2598 {
2599  return FFMPEG_CONFIGURATION;
2600 }
2601 
2602 const char *avcodec_license(void)
2603 {
2604 #define LICENSE_PREFIX "libavcodec license: "
2605  return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
2606 }
2607 
2609 {
2611  ff_thread_flush(avctx);
2612  else if (avctx->codec->flush)
2613  avctx->codec->flush(avctx);
2614 
2615  avctx->pts_correction_last_pts =
2616  avctx->pts_correction_last_dts = INT64_MIN;
2617 }
2618 
2620 {
2621  switch (codec_id) {
2622  case AV_CODEC_ID_8SVX_EXP:
2623  case AV_CODEC_ID_8SVX_FIB:
2624  case AV_CODEC_ID_ADPCM_CT:
2631  return 4;
2632  case AV_CODEC_ID_PCM_ALAW:
2633  case AV_CODEC_ID_PCM_MULAW:
2634  case AV_CODEC_ID_PCM_S8:
2636  case AV_CODEC_ID_PCM_U8:
2637  case AV_CODEC_ID_PCM_ZORK:
2638  return 8;
2639  case AV_CODEC_ID_PCM_S16BE:
2641  case AV_CODEC_ID_PCM_S16LE:
2643  case AV_CODEC_ID_PCM_U16BE:
2644  case AV_CODEC_ID_PCM_U16LE:
2645  return 16;
2647  case AV_CODEC_ID_PCM_S24BE:
2648  case AV_CODEC_ID_PCM_S24LE:
2650  case AV_CODEC_ID_PCM_U24BE:
2651  case AV_CODEC_ID_PCM_U24LE:
2652  return 24;
2653  case AV_CODEC_ID_PCM_S32BE:
2654  case AV_CODEC_ID_PCM_S32LE:
2656  case AV_CODEC_ID_PCM_U32BE:
2657  case AV_CODEC_ID_PCM_U32LE:
2658  case AV_CODEC_ID_PCM_F32BE:
2659  case AV_CODEC_ID_PCM_F32LE:
2660  return 32;
2661  case AV_CODEC_ID_PCM_F64BE:
2662  case AV_CODEC_ID_PCM_F64LE:
2663  return 64;
2664  default:
2665  return 0;
2666  }
2667 }
2668 
2670 {
2671  static const enum AVCodecID map[AV_SAMPLE_FMT_NB][2] = {
2677  [AV_SAMPLE_FMT_U8P ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
2678  [AV_SAMPLE_FMT_S16P] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
2679  [AV_SAMPLE_FMT_S32P] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
2680  [AV_SAMPLE_FMT_FLTP] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
2681  [AV_SAMPLE_FMT_DBLP] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
2682  };
2683  if (fmt < 0 || fmt >= AV_SAMPLE_FMT_NB)
2684  return AV_CODEC_ID_NONE;
2685  if (be < 0 || be > 1)
2686  be = AV_NE(1, 0);
2687  return map[fmt][be];
2688 }
2689 
2691 {
2692  switch (codec_id) {
2694  return 2;
2696  return 3;
2700  case AV_CODEC_ID_ADPCM_SWF:
2701  case AV_CODEC_ID_ADPCM_MS:
2702  return 4;
2703  default:
2704  return av_get_exact_bits_per_sample(codec_id);
2705  }
2706 }
2707 
2708 int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
2709 {
2710  int id, sr, ch, ba, tag, bps;
2711 
2712  id = avctx->codec_id;
2713  sr = avctx->sample_rate;
2714  ch = avctx->channels;
2715  ba = avctx->block_align;
2716  tag = avctx->codec_tag;
2717  bps = av_get_exact_bits_per_sample(avctx->codec_id);
2718 
2719  /* codecs with an exact constant bits per sample */
2720  if (bps > 0 && ch > 0 && frame_bytes > 0 && ch < 32768 && bps < 32768)
2721  return (frame_bytes * 8LL) / (bps * ch);
2722  bps = avctx->bits_per_coded_sample;
2723 
2724  /* codecs with a fixed packet duration */
2725  switch (id) {
2726  case AV_CODEC_ID_ADPCM_ADX: return 32;
2727  case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
2728  case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
2729  case AV_CODEC_ID_AMR_NB:
2730  case AV_CODEC_ID_EVRC:
2731  case AV_CODEC_ID_GSM:
2732  case AV_CODEC_ID_QCELP:
2733  case AV_CODEC_ID_RA_288: return 160;
2734  case AV_CODEC_ID_AMR_WB:
2735  case AV_CODEC_ID_GSM_MS: return 320;
2736  case AV_CODEC_ID_MP1: return 384;
2737  case AV_CODEC_ID_ATRAC1: return 512;
2738  case AV_CODEC_ID_ATRAC3: return 1024;
2739  case AV_CODEC_ID_MP2:
2740  case AV_CODEC_ID_MUSEPACK7: return 1152;
2741  case AV_CODEC_ID_AC3: return 1536;
2742  }
2743 
2744  if (sr > 0) {
2745  /* calc from sample rate */
2746  if (id == AV_CODEC_ID_TTA)
2747  return 256 * sr / 245;
2748 
2749  if (ch > 0) {
2750  /* calc from sample rate and channels */
2751  if (id == AV_CODEC_ID_BINKAUDIO_DCT)
2752  return (480 << (sr / 22050)) / ch;
2753  }
2754  }
2755 
2756  if (ba > 0) {
2757  /* calc from block_align */
2758  if (id == AV_CODEC_ID_SIPR) {
2759  switch (ba) {
2760  case 20: return 160;
2761  case 19: return 144;
2762  case 29: return 288;
2763  case 37: return 480;
2764  }
2765  } else if (id == AV_CODEC_ID_ILBC) {
2766  switch (ba) {
2767  case 38: return 160;
2768  case 50: return 240;
2769  }
2770  }
2771  }
2772 
2773  if (frame_bytes > 0) {
2774  /* calc from frame_bytes only */
2775  if (id == AV_CODEC_ID_TRUESPEECH)
2776  return 240 * (frame_bytes / 32);
2777  if (id == AV_CODEC_ID_NELLYMOSER)
2778  return 256 * (frame_bytes / 64);
2779  if (id == AV_CODEC_ID_RA_144)
2780  return 160 * (frame_bytes / 20);
2781  if (id == AV_CODEC_ID_G723_1)
2782  return 240 * (frame_bytes / 24);
2783 
2784  if (bps > 0) {
2785  /* calc from frame_bytes and bits_per_coded_sample */
2786  if (id == AV_CODEC_ID_ADPCM_G726)
2787  return frame_bytes * 8 / bps;
2788  }
2789 
2790  if (ch > 0) {
2791  /* calc from frame_bytes and channels */
2792  switch (id) {
2793  case AV_CODEC_ID_ADPCM_AFC:
2794  return frame_bytes / (9 * ch) * 16;
2795  case AV_CODEC_ID_ADPCM_4XM:
2797  return (frame_bytes - 4 * ch) * 2 / ch;
2799  return (frame_bytes - 4) * 2 / ch;
2801  return (frame_bytes - 8) * 2 / ch;
2802  case AV_CODEC_ID_ADPCM_XA:
2803  return (frame_bytes / 128) * 224 / ch;
2805  return (frame_bytes - 6 - ch) / ch;
2806  case AV_CODEC_ID_ROQ_DPCM:
2807  return (frame_bytes - 8) / ch;
2808  case AV_CODEC_ID_XAN_DPCM:
2809  return (frame_bytes - 2 * ch) / ch;
2810  case AV_CODEC_ID_MACE3:
2811  return 3 * frame_bytes / ch;
2812  case AV_CODEC_ID_MACE6:
2813  return 6 * frame_bytes / ch;
2814  case AV_CODEC_ID_PCM_LXF:
2815  return 2 * (frame_bytes / (5 * ch));
2816  case AV_CODEC_ID_IAC:
2817  case AV_CODEC_ID_IMC:
2818  return 4 * frame_bytes / ch;
2819  }
2820 
2821  if (tag) {
2822  /* calc from frame_bytes, channels, and codec_tag */
2823  if (id == AV_CODEC_ID_SOL_DPCM) {
2824  if (tag == 3)
2825  return frame_bytes / ch;
2826  else
2827  return frame_bytes * 2 / ch;
2828  }
2829  }
2830 
2831  if (ba > 0) {
2832  /* calc from frame_bytes, channels, and block_align */
2833  int blocks = frame_bytes / ba;
2834  switch (avctx->codec_id) {
2836  return blocks * (1 + (ba - 4 * ch) / (4 * ch) * 8);
2838  return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
2840  return blocks * (1 + (ba - 4 * ch) * 2 / ch);
2841  case AV_CODEC_ID_ADPCM_MS:
2842  return blocks * (2 + (ba - 7 * ch) * 2 / ch);
2843  }
2844  }
2845 
2846  if (bps > 0) {
2847  /* calc from frame_bytes, channels, and bits_per_coded_sample */
2848  switch (avctx->codec_id) {
2849  case AV_CODEC_ID_PCM_DVD:
2850  if(bps<4)
2851  return 0;
2852  return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
2854  if(bps<4)
2855  return 0;
2856  return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
2857  case AV_CODEC_ID_S302M:
2858  return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
2859  }
2860  }
2861  }
2862  }
2863 
2864  return 0;
2865 }
2866 
2867 #if !HAVE_THREADS
2869 {
2870  return -1;
2871 }
2872 
2873 #endif
2874 
2875 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
2876 {
2877  unsigned int n = 0;
2878 
2879  while (v >= 0xff) {
2880  *s++ = 0xff;
2881  v -= 0xff;
2882  n++;
2883  }
2884  *s = v;
2885  n++;
2886  return n;
2887 }
2888 
2889 int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
2890 {
2891  int i;
2892  for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
2893  return i;
2894 }
2895 
2896 #if FF_API_MISSING_SAMPLE
2897 void av_log_missing_feature(void *avc, const char *feature, int want_sample)
2898 {
2899  av_log(avc, AV_LOG_WARNING, "%s is not implemented. Update your FFmpeg "
2900  "version to the newest one from Git. If the problem still "
2901  "occurs, it means that your file has a feature which has not "
2902  "been implemented.\n", feature);
2903  if(want_sample)
2905 }
2906 
2907 void av_log_ask_for_sample(void *avc, const char *msg, ...)
2908 {
2909  va_list argument_list;
2910 
2911  va_start(argument_list, msg);
2912 
2913  if (msg)
2914  av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
2915  av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
2916  "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
2917  "and contact the ffmpeg-devel mailing list.\n");
2918 
2919  va_end(argument_list);
2920 }
2921 #endif /* FF_API_MISSING_SAMPLE */
2922 
2924 
2926 {
2927  AVHWAccel **p = &first_hwaccel;
2928  while (*p)
2929  p = &(*p)->next;
2930  *p = hwaccel;
2931  hwaccel->next = NULL;
2932 }
2933 
2935 {
2936  return hwaccel ? hwaccel->next : first_hwaccel;
2937 }
2938 
2940 {
2941  AVHWAccel *hwaccel = NULL;
2942 
2943  while ((hwaccel = av_hwaccel_next(hwaccel)))
2944  if (hwaccel->id == codec_id
2945  && hwaccel->pix_fmt == pix_fmt)
2946  return hwaccel;
2947  return NULL;
2948 }
2949 
2950 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
2951 {
2952  if (ff_lockmgr_cb) {
2954  return -1;
2956  return -1;
2957  }
2958 
2959  ff_lockmgr_cb = cb;
2960 
2961  if (ff_lockmgr_cb) {
2963  return -1;
2965  return -1;
2966  }
2967  return 0;
2968 }
2969 
2971 {
2972  if (ff_lockmgr_cb) {
2974  return -1;
2975  }
2977  if (entangled_thread_counter != 1) {
2978  av_log(log_ctx, AV_LOG_ERROR, "Insufficient thread locking around avcodec_open/close()\n");
2979  ff_avcodec_locked = 1;
2981  return AVERROR(EINVAL);
2982  }
2984  ff_avcodec_locked = 1;
2985  return 0;
2986 }
2987 
2989 {
2991  ff_avcodec_locked = 0;
2993  if (ff_lockmgr_cb) {
2995  return -1;
2996  }
2997  return 0;
2998 }
2999 
3001 {
3002  if (ff_lockmgr_cb) {
3004  return -1;
3005  }
3006  return 0;
3007 }
3008 
3010 {
3011  if (ff_lockmgr_cb) {
3013  return -1;
3014  }
3015  return 0;
3016 }
3017 
3018 unsigned int avpriv_toupper4(unsigned int x)
3019 {
3020  return av_toupper(x & 0xFF) +
3021  (av_toupper((x >> 8) & 0xFF) << 8) +
3022  (av_toupper((x >> 16) & 0xFF) << 16) +
3023  (av_toupper((x >> 24) & 0xFF) << 24);
3024 }
3025 
3027 {
3028  int ret;
3029 
3030  dst->owner = src->owner;
3031 
3032  ret = av_frame_ref(dst->f, src->f);
3033  if (ret < 0)
3034  return ret;
3035 
3036  if (src->progress &&
3037  !(dst->progress = av_buffer_ref(src->progress))) {
3038  ff_thread_release_buffer(dst->owner, dst);
3039  return AVERROR(ENOMEM);
3040  }
3041 
3042  return 0;
3043 }
3044 
3045 #if !HAVE_THREADS
3046 
3048 {
3049  return avctx->get_format(avctx, fmt);
3050 }
3051 
3053 {
3054  f->owner = avctx;
3055  return ff_get_buffer(avctx, f->f, flags);
3056 }
3057 
3059 {
3060  av_frame_unref(f->f);
3061 }
3062 
3064 {
3065 }
3066 
3067 void ff_thread_report_progress(ThreadFrame *f, int progress, int field)
3068 {
3069 }
3070 
3071 void ff_thread_await_progress(ThreadFrame *f, int progress, int field)
3072 {
3073 }
3074 
3076 {
3077  return 1;
3078 }
3079 
3080 #endif
3081 
3083 {
3084  AVCodec *c= avcodec_find_decoder(codec_id);
3085  if(!c)
3086  c= avcodec_find_encoder(codec_id);
3087  if(c)
3088  return c->type;
3089 
3090  if (codec_id <= AV_CODEC_ID_NONE)
3091  return AVMEDIA_TYPE_UNKNOWN;
3092  else if (codec_id < AV_CODEC_ID_FIRST_AUDIO)
3093  return AVMEDIA_TYPE_VIDEO;
3094  else if (codec_id < AV_CODEC_ID_FIRST_SUBTITLE)
3095  return AVMEDIA_TYPE_AUDIO;
3096  else if (codec_id < AV_CODEC_ID_FIRST_UNKNOWN)
3097  return AVMEDIA_TYPE_SUBTITLE;
3098 
3099  return AVMEDIA_TYPE_UNKNOWN;
3100 }
3101 
3103 {
3104  return !!s->internal;
3105 }
3106 
3108 {
3109  int ret;
3110  char *str;
3111 
3112  ret = av_bprint_finalize(buf, &str);
3113  if (ret < 0)
3114  return ret;
3115  avctx->extradata = str;
3116  /* Note: the string is NUL terminated (so extradata can be read as a
3117  * string), but the ending character is not accounted in the size (in
3118  * binary formats you are likely not supposed to mux that character). When
3119  * extradata is copied, it is also padded with FF_INPUT_BUFFER_PADDING_SIZE
3120  * zeros. */
3121  avctx->extradata_size = buf->len;
3122  return 0;
3123 }
3124 
3126  const uint8_t *end,
3127  uint32_t *av_restrict state)
3128 {
3129  int i;
3130 
3131  assert(p <= end);
3132  if (p >= end)
3133  return end;
3134 
3135  for (i = 0; i < 3; i++) {
3136  uint32_t tmp = *state << 8;
3137  *state = tmp + *(p++);
3138  if (tmp == 0x100 || p == end)
3139  return p;
3140  }
3141 
3142  while (p < end) {
3143  if (p[-1] > 1 ) p += 3;
3144  else if (p[-2] ) p += 2;
3145  else if (p[-3]|(p[-1]-1)) p++;
3146  else {
3147  p++;
3148  break;
3149  }
3150  }
3151 
3152  p = FFMIN(p, end) - 4;
3153  *state = AV_RB32(p);
3154 
3155  return p + 4;
3156 }
#define WRAP_PLANE(ref_out, data, data_size)
const char * name
Definition: avisynth_c.h:675
#define FF_SANE_NB_CHANNELS
enum AVPixelFormat(* get_format)(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
callback to negotiate the pixelFormat
packed YUV 4:2:2, 16bpp, Cb Y0 Cr Y1
Definition: pixfmt.h:85
#define CONFIG_FRAME_THREAD_ENCODER
Definition: config.h:402
Number of sample formats. DO NOT USE if linking dynamically.
Definition: samplefmt.h:63
#define UTF8_MAX_BYTES
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:205
const struct AVCodec * codec
planar YUV 4:2:2, 18bpp, (1 Cr & Cb sample per 2x1 Y samples), big-endian
Definition: pixfmt.h:158
float v
const AVCodecDescriptor * codec_descriptor
AVCodecDescriptor Code outside libavcodec should access this field using: av_codec_{get,set}_codec_descriptor(avctx)
static AVCodec * find_encdec(enum AVCodecID id, int encoder)
const char * s
Definition: avisynth_c.h:668
planar YUV 4:4:4,42bpp, (1 Cr & Cb sample per 1x1 Y samples), big-endian
Definition: pixfmt.h:231
#define AV_NUM_DATA_POINTERS
Definition: frame.h:77
int ff_thread_video_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet_ptr)
planar YUV 4:2:0,21bpp, (1 Cr & Cb sample per 2x2 Y samples), little-endian
Definition: pixfmt.h:224
int64_t pts_correction_num_faulty_dts
Number of incorrect PTS values so far.
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it...
void av_frame_set_best_effort_timestamp(AVFrame *frame, int64_t val)
int linesize[AV_NUM_DATA_POINTERS]
number of bytes per line
void av_free_packet(AVPacket *pkt)
Free a packet.
Definition: avpacket.c:242
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:1778
This structure describes decoded (raw) audio or video data.
Definition: frame.h:76
void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic)
AVCodec * avcodec_find_encoder(enum AVCodecID id)
Find a registered encoder with a matching codec ID.
#define c2
Definition: idct_sh4.c:27
int av_samples_get_buffer_size(int *linesize, int nb_channels, int nb_samples, enum AVSampleFormat sample_fmt, int align)
Get the required buffer size for the given audio parameters.
Definition: samplefmt.c:125
int stride_align[AV_NUM_DATA_POINTERS]
A dummy id pointing at the start of audio codecs.
planar YUV 4:2:2,28bpp, (1 Cr & Cb sample per 2x1 Y samples), little-endian
Definition: pixfmt.h:228
planar YUV 4:2:0, 15bpp, (1 Cr & Cb sample per 2x2 Y samples), little-endian
Definition: pixfmt.h:151
enum AVCodecID id
Definition: mxfenc.c:89
#define CODEC_FLAG_PASS2
Use internal 2pass ratecontrol in second pass mode.
#define CODEC_CAP_VARIABLE_FRAME_SIZE
Audio encoder supports receiving a different number of samples in each call.
int coded_width
Bitstream width / height, may be different from width/height e.g.
const char * fmt
Definition: avisynth_c.h:669
int av_lockmgr_register(int(*cb)(void **mutex, enum AVLockOp op))
Register a user provided lock manager supporting the operations specified by AVLockOp.
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:73
misc image utilities
Unlock the mutex.
AVFrame * f
Definition: thread.h:36
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:1818
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:70
int64_t pos
byte position in stream, -1 if unknown
int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples, int *frame_size_ptr, AVPacket *avpkt)
AVBufferRef * buf[AV_NUM_DATA_POINTERS]
AVBuffer references backing the data for this frame.
Definition: frame.h:343
#define CODEC_FLAG_PASS1
Use internal 2pass ratecontrol in first pass mode.
AVFrame * coded_frame
the picture in the bitstream
#define LIBAVCODEC_VERSION_MICRO
planar YUV 4:4:4, 27bpp, (1 Cr & Cb sample per 1x1 Y samples), big-endian
Definition: pixfmt.h:154
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:154
int rc_initial_buffer_occupancy
Number of bits which should be loaded into the rc buffer before decoding starts.
planar YUV 4:4:4,36bpp, (1 Cr & Cb sample per 1x1 Y samples), big-endian
Definition: pixfmt.h:229
void av_opt_set_defaults(void *s)
Set the values of all AVOption fields to their default values.
Definition: opt.c:942
const char * avcodec_configuration(void)
Return the libavcodec build-time configuration.
int nb_extended_buf
Number of elements in extended_buf.
Definition: frame.h:361
planar GBR 4:4:4 24bpp
Definition: pixfmt.h:168
int num
numerator
Definition: rational.h:44
void avcodec_set_dimensions(AVCodecContext *s, int width, int height)
const char * avcodec_license(void)
Return the libavcodec license.
CODEC_ID_PCM_S8_PLANAR
Sinusoidal phase f
enum AVPixelFormat pix_fmt
Supported pixel format.
static int get_buffer_internal(AVCodecContext *avctx, AVFrame *frame, int flags)
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown) That is the width of a pixel divided by the height of the pixel...
static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
Pad last frame with silence.
enum AVMediaType codec_type
Definition: rtp.c:36
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
void avpriv_color_frame(AVFrame *frame, const int c[4])
void ff_thread_free(AVCodecContext *avctx)
Definition: pthread.c:1178
void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
Same behaviour av_fast_malloc but the buffer has additional FF_INPUT_BUFFER_PADDING_SIZE at the end w...
planar GBR 4:4:4 36bpp, little-endian
Definition: pixfmt.h:234
A dummy ID pointing at the start of various fake codecs.
planar YUV 4:2:0, 13.5bpp, (1 Cr & Cb sample per 2x2 Y samples), big-endian
Definition: pixfmt.h:148
enum AVMediaType type
void * av_realloc(void *ptr, size_t size)
Allocate or reallocate a block of memory.
Definition: mem.c:141
size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
Put a string representing the codec tag codec_tag in buf.
#define FF_ARRAY_ELEMS(a)
AVBufferPool * pools[4]
Pools for each data plane.
int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Encode a frame of audio.
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
enum AVPixelFormat ff_thread_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
Wrapper around get_format() for frame-multithreaded codecs.
int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub, int *got_sub_ptr, AVPacket *avpkt)
Decode a subtitle message.
signed 16 bits
Definition: samplefmt.h:52
int av_dup_packet(AVPacket *pkt)
Definition: avpacket.c:221
four components are given, that&#39;s all.
planar GBR 4:4:4 36bpp, big-endian
Definition: pixfmt.h:233
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs...
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
Definition: pixdesc.h:66
AVLockOp
Lock operation used by lockmgr.
int av_bprint_finalize(AVBPrint *buf, char **ret_str)
Finalize a print buffer.
Definition: bprint.c:193
output residual component w
#define FFMPEG_LICENSE
Definition: config.h:5
char * text
0 terminated plain UTF-8 text
#define FFALIGN(x, a)
Definition: common.h:63
static void * codec_mutex
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:198
AVSubtitleRect ** rects
int av_codec_is_decoder(const AVCodec *codec)
void av_frame_set_pkt_duration(AVFrame *frame, int64_t val)
int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels, enum AVSampleFormat sample_fmt, const uint8_t *buf, int buf_size, int align)
Fill AVFrame audio data and linesize pointers.
int av_codec_is_encoder(const AVCodec *codec)
void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height)
Modify width and height values so that they will result in a memory buffer that is acceptable for the...
static int volatile entangled_thread_counter
int ff_lock_avcodec(AVCodecContext *log_ctx)
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
Allocate a buffer, reusing the given one if large enough.
int av_samples_fill_arrays(uint8_t **audio_data, int *linesize, const uint8_t *buf, int nb_channels, int nb_samples, enum AVSampleFormat sample_fmt, int align)
Fill plane data pointers and linesize for samples with sample format sample_fmt.
Definition: samplefmt.c:155
Public dictionary API.
int ff_unlock_avcodec(void)
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
Definition: pixfmt.h:105
enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
enum AVSampleFormat sample_fmt
audio sample format
AVComponentDescriptor comp[4]
Parameters that describe how pixels are packed.
Definition: pixdesc.h:86
Lock the mutex.
uint8_t
#define CONFIG_DSPUTIL
Definition: config.h:401
int av_log_get_level(void)
Definition: log.c:264
#define av_cold
Definition: attributes.h:78
AV_SAMPLE_FMT_U8
Opaque data information usually continuous.
Definition: avutil.h:145
8 bit with PIX_FMT_RGB32 palette
Definition: pixfmt.h:79
AVOptions.
uint8_t * data[AV_NUM_DATA_POINTERS]
#define AV_RB32
static AVPacket pkt
Definition: demuxing.c:56
static AVCodec * first_avcodec
int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
#define b
Definition: input.c:42
end end
#define AV_NE(be, le)
Definition: common.h:44
int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic)
planar YUV 4:4:4,36bpp, (1 Cr & Cb sample per 1x1 Y samples), little-endian
Definition: pixfmt.h:230
#define FF_PROFILE_UNKNOWN
#define emms_c()
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:159
#define LIBAVCODEC_VERSION_INT
int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame)
Identical in function to av_frame_make_writable(), except it uses ff_get_buffer() to allocate the buf...
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
#define LICENSE_PREFIX
struct CompatReleaseBufPriv CompatReleaseBufPriv
int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Encode a frame of video.
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
int ff_thread_decode_frame(AVCodecContext *avctx, AVFrame *picture, int *got_picture_ptr, AVPacket *avpkt)
Submit a new frame to a decoding thread.
Definition: pthread.c:632
void av_frame_move_ref(AVFrame *dst, AVFrame *src)
Move everythnig contained in src to dst and reset src.
Definition: frame.c:352
int attribute_align_arg ff_codec_open2_recursive(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
Call avcodec_open2 recursively by decrementing counter, unlocking mutex, calling the function and the...
uint8_t * data
void av_register_hwaccel(AVHWAccel *hwaccel)
Register the hardware accelerator hwaccel.
enum AVSampleFormat av_get_planar_sample_fmt(enum AVSampleFormat sample_fmt)
Get the planar alternative form of the given sample format.
Definition: samplefmt.c:82
planar YUV 4:4:0 full scale (JPEG), deprecated in favor of PIX_FMT_YUV440P and setting color_range ...
Definition: pixfmt.h:104
int ff_init_buffer_info(AVCodecContext *avctx, AVFrame *frame)
does needed setup of pkt_pts/pos and such for (re)get_buffer();
uint32_t tag
Definition: movenc.c:894
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of PIX_FMT_YUV422P and setting color_...
Definition: pixfmt.h:81
#define av_restrict
Definition: config.h:9
enum AVPixelFormat pix_fmt
Definition: v4l.c:63
int64_t av_frame_get_pkt_duration(const AVFrame *frame)
int lowres
low resolution decoding, 1-> 1/2 size, 2->1/4 size
int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
Return the index into tab at which {a,b} match elements {[0],[1]} of tab.
signed 32 bits, planar
Definition: samplefmt.h:59
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
char * stats_out
pass1 encoding statistics output buffer
int duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
const OptionDef options[]
Definition: ffserver.c:4697
AVCodecContext * owner
Definition: thread.h:37
float, planar
Definition: samplefmt.h:60
static void compat_release_buffer(void *opaque, uint8_t *data)
void av_vlog(void *avcl, int level, const char *fmt, va_list vl)
Definition: log.c:258
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
AVCodec * avcodec_find_encoder_by_name(const char *name)
Find a registered encoder with the specified name.
frame
Definition: stft.m:14
void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height, int linesize_align[AV_NUM_DATA_POINTERS])
Modify width and height values so that they will result in a memory buffer that is acceptable for the...
static void * av_x_if_null(const void *p, const void *x)
Return x default pointer in case p is NULL.
Definition: avutil.h:250
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:130
planar YUV 4:2:0, 13.5bpp, (1 Cr & Cb sample per 2x2 Y samples), little-endian
Definition: pixfmt.h:149
Discrete Time axis x
void av_dict_copy(AVDictionary **dst, AVDictionary *src, int flags)
Copy entries from one AVDictionary struct into another.
Definition: dict.c:176
Libavcodec version macros.
void av_frame_set_pkt_size(AVFrame *frame, int val)
av_cold int avcodec_close(AVCodecContext *avctx)
Close a given AVCodecContext and free all the data associated with it (but not the AVCodecContext its...
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:73
AVRational pkt_timebase
Timebase in which pkt_dts/pts and AVPacket.dts/pts are.
enum AVCodecID id
const uint64_t * channel_layouts
array of support channel layouts, or NULL if unknown. array is terminated by 0
#define STRIDE_ALIGN
static AVHWAccel * first_hwaccel
planar GBR 4:4:4 27bpp, big-endian
Definition: pixfmt.h:169
planar YUV 4:4:4, 30bpp, (1 Cr & Cb sample per 1x1 Y samples), little-endian
Definition: pixfmt.h:157
#define MAKE_ACCESSORS(str, name, type, field)
AVHWAccel * av_hwaccel_next(AVHWAccel *hwaccel)
If hwaccel is NULL, returns the first registered hardware accelerator, if hwaccel is non-NULL...
planar YUV 4:2:2 24bpp, (1 Cr & Cb sample per 2x1 Y & A samples)
Definition: pixfmt.h:219
uint16_t depth_minus1
number of bits in the component minus 1
Definition: pixdesc.h:43
AVCodecID
Identify the syntax and semantics of the bitstream.
int width
width and height of the video frame
Definition: frame.h:122
void av_log_missing_feature(void *avc, const char *feature, int want_sample)
int has_b_frames
Size of the frame reordering buffer in the decoder.
int av_get_bits_per_sample(enum AVCodecID codec_id)
Return codec bits per sample.
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:183
AVDictionary ** avpriv_frame_get_metadatap(AVFrame *frame)
Definition: frame.c:49
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition: pixdesc.h:75
int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture, int *got_picture_ptr, const AVPacket *avpkt)
Decode the video frame of size avpkt->size from avpkt->data into picture.
Create a mutex.
#define CODEC_CAP_PARAM_CHANGE
Codec supports changed parameters at any point.
Multithreading support functions.
void * av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
Reallocate the given block if it is not large enough, otherwise do nothing.
#define CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
An AV_PKT_DATA_PARAM_CHANGE side data packet is laid out as follows:
unsigned int avpriv_toupper4(unsigned int x)
int qmax
maximum quantizer
AVCodec * av_codec_next(const AVCodec *c)
If c is NULL, returns the first registered codec, if c is non-NULL, returns the next registered codec...
#define CODEC_CAP_SMALL_LAST_FRAME
Codec can be fed a final frame with a smaller size.
int av_pix_fmt_get_chroma_sub_sample(enum AVPixelFormat pix_fmt, int *h_shift, int *v_shift)
Utility function to access log2_chroma_w log2_chroma_h from the pixel format AVPixFmtDescriptor.
Definition: pixdesc.c:1806
int64_t pts_correction_last_pts
Number of incorrect DTS values so far.
int active_thread_type
Which multithreading methods are in use by the codec.
void av_frame_set_pkt_pos(AVFrame *frame, int64_t val)
int avcodec_is_open(AVCodecContext *s)
Spectrum Plot time data
const char * r
Definition: vf_curves.c:94
int capabilities
Codec capabilities.
int ff_thread_init(AVCodecContext *s)
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values. ...
Definition: dict.c:162
int av_get_channel_layout_nb_channels(uint64_t channel_layout)
Return the number of channels in the channel layout.
AVBufferRef * buf
A reference to the reference-counted buffer where the packet data is stored.
const char * arg
void av_frame_set_channels(AVFrame *frame, int val)
planar YUV 4:2:2, 20bpp, (1 Cr & Cb sample per 2x1 Y samples), little-endian
Definition: pixfmt.h:153
int flags
CODEC_FLAG_*.
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
Definition: frame.c:361
simple assert() macros that are a bit more flexible than ISO C assert().
planar YUV 4:2:2,28bpp, (1 Cr & Cb sample per 2x1 Y samples), big-endian
Definition: pixfmt.h:227
int64_t av_gcd(int64_t a, int64_t b)
Return the greatest common divisor of a and b.
Definition: mathematics.c:55
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
enum AVMediaType avcodec_get_type(enum AVCodecID codec_id)
Get the type of the given codec.
int avpriv_set_systematic_pal2(uint32_t pal[256], enum AVPixelFormat pix_fmt)
Definition: imgutils.c:150
const char * name
Name of the codec implementation.
AVBufferRef * av_buffer_create(uint8_t *data, int size, void(*free)(void *opaque, uint8_t *data), void *opaque, int flags)
Create an AVBuffer from an existing array.
int av_buffer_realloc(AVBufferRef **pbuf, int size)
Reallocate a given buffer.
enum AVCodecID codec_id
Definition: mov_chan.c:433
void * thread_opaque
thread opaque Can be used by execute() to store some per AVCodecContext stuff.
#define FFMAX(a, b)
Definition: common.h:56
external API header
AVHWAccel * ff_find_hwaccel(enum AVCodecID codec_id, enum AVPixelFormat pix_fmt)
Return the hardware accelerated codec for codec codec_id and pixel format pix_fmt.
void av_image_copy(uint8_t *dst_data[4], int dst_linesizes[4], const uint8_t *src_data[4], const int src_linesizes[4], enum AVPixelFormat pix_fmt, int width, int height)
Copy image in src_data to dst_data.
Definition: imgutils.c:257
int size
int flags
A combination of AV_PKT_FLAG values.
const AVCodecDescriptor * avcodec_descriptor_get(enum AVCodecID id)
Definition: codec_desc.c:2566
uint64_t channel_layout
Audio channel layout.
uint32_t end_display_time
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:72
int64_t pts
Same as packet pts, in AV_TIME_BASE.
int rc_buffer_size
decoder bitstream buffer size
int av_packet_merge_side_data(AVPacket *pkt)
Definition: avpacket.c:306
uint64_t channel_layout
Channel layout of the audio data.
Definition: frame.h:331
int props
Codec properties, a combination of AV_CODEC_PROP_* flags.
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
Reduce a fraction.
Definition: rational.c:36
signed 32 bits
Definition: samplefmt.h:53
int ff_frame_thread_encoder_init(AVCodecContext *avctx, AVDictionary *options)
#define FF_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
#define FF_MAX_EXTRADATA_SIZE
Maximum size in bytes of extradata.
AVFrame * avcodec_alloc_frame(void)
Allocate an AVFrame and set its fields to default values.
struct AVRational AVRational
rational number numerator/denominator
#define FF_COMPLIANCE_EXPERIMENTAL
Allow nonstandardized experimental things.
int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
Check if the given dimension of an image is valid, meaning that all bytes of the image can be address...
Definition: imgutils.c:231
uint8_t nb_components
The number of components each pixel has, (1-4)
Definition: pixdesc.h:57
Buffer to print data progressively.
Definition: bprint.h:75
static int update_frame_pool(AVCodecContext *avctx, AVFrame *frame)
FFT buffer for g
Definition: stft_peak.m:17
int bit_rate
the average bitrate
audio channel layout utility functions
CODEC_ID_ILBC
enum AVPixelFormat * pix_fmts
array of supported pixel formats, or NULL if unknown, array is terminated by -1
int av_frame_get_channels(const AVFrame *frame)
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:53
AVPicture pict
data+linesize for the bitmap of this subtitle.
#define FFMIN(a, b)
Definition: common.h:58
volatile int ff_avcodec_locked
int av_samples_set_silence(uint8_t **audio_data, int offset, int nb_samples, int nb_channels, enum AVSampleFormat sample_fmt)
Fill an audio buffer with silence.
Definition: samplefmt.c:249
#define FF_MIN_BUFFER_SIZE
minimum encoding buffer size Used to avoid some checks during header writing.
AVBufferRef ** extended_buf
For planar audio which requires more than AV_NUM_DATA_POINTERS AVBufferRef pointers, this array will hold all the references which cannot fit into AVFrame.buf.
Definition: frame.h:357
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of PIX_FMT_YUV420P and setting color_...
Definition: pixfmt.h:80
int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
Return codec bits per sample.
static int is_hwaccel_pix_fmt(enum AVPixelFormat pix_fmt)
ret
Definition: avfilter.c:821
int width
picture width / height.
attribute_deprecated int reference
Definition: frame.h:189
#define FF_DEBUG_BUFFERS
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int size)
Check AVPacket size and/or allocate data.
#define CODEC_CAP_AUTO_THREADS
Codec supports avctx->thread_count == 0 (auto).
void ff_thread_finish_setup(AVCodecContext *avctx)
If the codec defines update_thread_context(), call this when they are ready for the next thread to st...
int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, const AVPacket *avpkt)
Decode the audio frame of size avpkt->size from avpkt->data into frame.
#define AV_RL32
const AVProfile * profiles
array of recognized profiles, or NULL if unknown, array is terminated by {FF_PROFILE_UNKNOWN} ...
int64_t reordered_opaque
opaque 64bit number (generally a PTS) that will be reordered and output in AVFrame.reordered_opaque
static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
int refcounted_frames
If non-zero, the decoded audio and video frames returned from avcodec_decode_video2() and avcodec_dec...
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:71
planar YUV 4:2:0,18bpp, (1 Cr & Cb sample per 2x2 Y samples), little-endian
Definition: pixfmt.h:222
planar YUV 4:2:0, 15bpp, (1 Cr & Cb sample per 2x2 Y samples), big-endian
Definition: pixfmt.h:150
int mb_decision
macroblock decision mode
Usually treated as AVMEDIA_TYPE_DATA.
Definition: avutil.h:142
void av_log_ask_for_sample(void *avc, const char *msg,...)
Opaque data information usually sparse.
Definition: avutil.h:147
int ff_alloc_packet(AVPacket *avpkt, int size)
static pthread_mutex_t * mutex
Definition: w32pthreads.h:68
static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
#define FF_SUB_CHARENC_MODE_AUTOMATIC
libavcodec will select the mode itself
CODEC_ID_ADPCM_IMA_APC
char * sub_charenc
DTS of the last frame.
const char * av_get_sample_fmt_name(enum AVSampleFormat sample_fmt)
Return the name of sample_fmt, or NULL if sample_fmt is not recognized.
Definition: samplefmt.c:47
static int ff_fast_malloc(void *ptr, unsigned int *size, size_t min_size, int zero_realloc)
planar YUV 4:2:2, 18bpp, (1 Cr & Cb sample per 2x1 Y samples), little-endian
Definition: pixfmt.h:159
#define AVERROR_EXPERIMENTAL
Requested feature is flagged experimental. Set strict_std_compliance if you really want to use it...
Definition: error.h:72
const char * avcodec_get_name(enum AVCodecID id)
Get the name of a codec.
int thread_count
thread count is used to decide how many independent tasks should be passed to execute() ...
int sub_charenc_mode
Subtitles character encoding mode.
int av_packet_split_side_data(AVPacket *pkt)
Definition: avpacket.c:344
void avcodec_flush_buffers(AVCodecContext *avctx)
Flush buffers, should be called when seeking or when switching to a different stream.
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:134
const AVS_VideoInfo int align
Definition: avisynth_c.h:695
AVBufferRef * progress
Definition: thread.h:40
const char * av_get_profile_name(const AVCodec *codec, int profile)
Return a name for the specified profile, if available.
int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size, const AVFrame *pict)
int frame_size
Number of samples per channel in an audio frame.
#define attribute_align_arg
av_cold int ff_codec_close_recursive(AVCodecContext *avctx)
Call avcodec_close recursively, counterpart to avcodec_open2_recursive.
#define PIX_FMT_HWACCEL
Pixel format is an HW accelerated format.
Definition: pixdesc.h:92
NULL
Definition: eval.c:55
#define FF_COMPLIANCE_UNOFFICIAL
Allow unofficial extensions.
packed RGB 3:3:2, 8bpp, (msb)2B 3G 3R(lsb)
Definition: pixfmt.h:87
static int width
Definition: tests/utils.c:158
AVS_Value src
Definition: avisynth_c.h:523
int avcodec_default_get_buffer2(AVCodecContext *avctx, AVFrame *frame, int flags)
The default callback for AVCodecContext.get_buffer2().
int av_image_fill_pointers(uint8_t *data[4], enum AVPixelFormat pix_fmt, int height, uint8_t *ptr, const int linesizes[4])
Fill plane data pointers for an image with pixel format pix_fmt and height height.
Definition: imgutils.c:108
static int add_metadata_from_side_data(AVCodecContext *avctx, AVFrame *frame)
enum AVMediaType codec_type
A list of zero terminated key/value strings.
enum AVCodecID av_get_pcm_codec(enum AVSampleFormat fmt, int be)
Return the PCM codec associated with a sample format.
enum AVCodecID codec_id
AVHWAccel.
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition: avutil.h:202
int av_opt_set_dict(void *obj, AVDictionary **options)
Set all the options from a given dictionary on an object.
Definition: opt.c:1202
#define PIX_FMT_PLANAR
At least one pixel component is not in the first data plane.
Definition: pixdesc.h:93
int sample_rate
samples per second
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:101
planar YUV 4:4:4 32bpp, (1 Cr & Cb sample per 1x1 Y & A samples)
Definition: pixfmt.h:218
unsigned avcodec_get_edge_width(void)
Return the amount of padding in pixels which the get_buffer callback must provide around the edge of ...
uint8_t flags
Definition: pixdesc.h:76
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:55
FIXME Range Coding of cb
Definition: snow.txt:367
planar GBR 4:4:4 30bpp, big-endian
Definition: pixfmt.h:171
main external API structure.
AVCodec * avcodec_find_decoder(enum AVCodecID id)
Find a registered decoder with a matching codec ID.
static int recode_subtitle(AVCodecContext *avctx, AVPacket *outpkt, const AVPacket *inpkt)
uint8_t * data
The data buffer.
Definition: buffer.h:89
int qmin
minimum quantizer
void avsubtitle_free(AVSubtitle *sub)
Free all allocated data in the given subtitle struct.
AVRational sample_aspect_ratio
Sample aspect ratio for the video frame, 0/1 if unknown/unspecified.
Definition: frame.h:154
int av_samples_copy(uint8_t **dst, uint8_t *const *src, int dst_offset, int src_offset, int nb_samples, int nb_channels, enum AVSampleFormat sample_fmt)
Copy samples from src to dst.
Definition: samplefmt.c:225
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:148
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> (&#39;D&#39;<<24) + (&#39;C&#39;<<16) + (&#39;B&#39;<<8) + &#39;A&#39;).
packed YUV 4:2:2, 16bpp, Y0 Cb Y1 Cr
Definition: pixfmt.h:69
#define AV_CODEC_PROP_BITMAP_SUB
Subtitle codec is bitmap based Decoded AVSubtitle data can be read from the AVSubtitleRect->pict fiel...
planar GBR 4:4:4 42bpp, little-endian
Definition: pixfmt.h:236
void * buf
Definition: avisynth_c.h:594
void ff_packet_free_side_data(AVPacket *pkt)
Remove and free all side data from packet.
Definition: avpacket.c:31
AVBufferRef * av_buffer_allocz(int size)
Same as av_buffer_alloc(), except the returned buffer will be initialized to zero.
int(* close)(AVCodecContext *)
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition: dict.c:62
static void avcodec_get_subtitle_defaults(AVSubtitle *sub)
unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
Encode extradata length to a buffer.
struct AVCodec * next
BYTE int const BYTE int int int height
Definition: avisynth_c.h:713
#define FF_SUB_CHARENC_MODE_DO_NOTHING
do nothing (demuxer outputs a stream supposed to be already in UTF-8, or the codec is bitmap for inst...
#define FF_THREAD_FRAME
Decode more than one frame at once.
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
Definition: pixfmt.h:74
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:73
void avcodec_get_frame_defaults(AVFrame *frame)
Set the fields of the given AVFrame to default values.
int64_t reordered_opaque
reordered opaque 64bit (generally an integer or a double precision float PTS but can be anything)...
Definition: frame.h:302
int(* func)(AVBPrint *dst, const char *in, const char *arg)
Describe the class of an AVClass context structure.
Definition: log.h:50
int sample_rate
Sample rate of the audio data.
Definition: frame.h:326
int(* get_buffer2)(struct AVCodecContext *s, AVFrame *frame, int flags)
This callback is called at the beginning of each frame to get data buffer(s) for it.
int av_image_fill_linesizes(int linesizes[4], enum AVPixelFormat pix_fmt, int width)
Fill plane linesizes for an image with pixel format pix_fmt and width width.
Definition: imgutils.c:86
av_cold void ff_dsputil_static_init(void)
Definition: dsputil.c:2644
Y , 16bpp, big-endian.
Definition: pixfmt.h:101
void av_buffer_pool_uninit(AVBufferPool **ppool)
Mark the pool as being available for freeing.
CODEC_ID_SRT
int av_sample_fmt_is_planar(enum AVSampleFormat sample_fmt)
Check if the sample format is planar.
Definition: samplefmt.c:118
synthesis window for stochastic i
rational number numerator/denominator
Definition: rational.h:43
void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
Wrapper around release_buffer() frame-for multithreaded codecs.
#define CONFIG_MEMORY_POISONING
Definition: config.h:383
const char * name
short name for the profile
Recommmends skipping the specified number of samples.
planar YUV 4:2:0,21bpp, (1 Cr & Cb sample per 2x2 Y samples), big-endian
Definition: pixfmt.h:223
uint16_t step_minus1
Number of elements between 2 horizontally consecutive pixels minus 1.
Definition: pixdesc.h:35
AVMediaType
Definition: avutil.h:141
int skip_samples
Number of audio samples to skip at the start of the next decoded frame.
planar GBR 4:4:4 42bpp, big-endian
Definition: pixfmt.h:235
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:330
const char * name
Name of the codec described by this descriptor.
int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
Initialize the AVCodecContext to use the given AVCodec.
#define snprintf
Definition: snprintf.h:34
void ff_thread_flush(AVCodecContext *avctx)
Wait for decoding threads to finish and reset internal state.
Definition: pthread.c:931
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Buffer references ownership and permissions
int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
Return audio frame duration.
static enum AVCodecID remap_deprecated_codec_id(enum AVCodecID id)
size_t av_strlcat(char *dst, const char *src, size_t size)
Append the string src to the string dst, but to a total length of no more than size - 1 bytes...
Definition: avstring.c:92
This struct describes the properties of a single codec described by an AVCodecID. ...
int64_t pkt_pts
PTS copied from the AVPacket that was decoded to produce this frame.
Definition: frame.h:164
CODEC_ID_CLLC
Definition: old_codec_ids.h:32
int avpriv_bprint_to_extradata(AVCodecContext *avctx, struct AVBPrint *buf)
Finalize buf into extradata and set its size appropriately.
const char * av_get_media_type_string(enum AVMediaType media_type)
Return a string describing the media_type enum, NULL if media_type is unknown.
static uint32_t state
Definition: trasher.c:27
AVCodec * avcodec_find_decoder_by_name(const char *name)
Find a registered decoder with the specified name.
static int flags
Definition: cpu.c:23
int av_frame_ref(AVFrame *dst, AVFrame *src)
Setup a new reference to the data described by an given frame.
Definition: frame.c:228
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
const AVClass * priv_class
AVClass for the private context.
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:87
planar YUV 4:4:4, 27bpp, (1 Cr & Cb sample per 1x1 Y samples), little-endian
Definition: pixfmt.h:155
static int av_toupper(int c)
Locale-independent conversion of ASCII characters to uppercase.
Definition: avstring.h:206
uint8_t max_lowres
maximum value for lowres supported by the decoder
int64_t pkt_dts
DTS copied from the AVPacket that triggered returning this frame.
Definition: frame.h:171
AVPacket * pkt
Current packet as passed into the decoder, to avoid having to pass the packet into every function...
A reference to a data buffer.
Definition: buffer.h:81
static int op(uint8_t **dst, const uint8_t *dst_end, GetByteContext *gb, int pixel, int count, int *x, int width, int linesize)
Perform decode operation.
#define CODEC_FLAG_EMU_EDGE
Don&#39;t draw edges.
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:68
planar YUV 4:2:2,24bpp, (1 Cr & Cb sample per 2x1 Y samples), little-endian
Definition: pixfmt.h:226
Y , 8bpp.
Definition: pixfmt.h:76
void av_opt_free(void *obj)
Free all string and binary options in obj.
Definition: opt.c:1194
int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size, const short *samples)
int(* decode)(AVCodecContext *, void *outdata, int *outdata_size, AVPacket *avpkt)
common internal api header.
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:162
Free mutex resources.
AVBufferPool * av_buffer_pool_init(int size, AVBufferRef *(*alloc)(int size))
Allocate and initialize a buffer pool.
int avpriv_lock_avformat(void)
static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
struct AVHWAccel * next
static int64_t guess_correct_pts(AVCodecContext *ctx, int64_t reordered_pts, int64_t dts)
Attempt to guess proper monotonic timestamps for decoded video frames which might have incorrect time...
void(* flush)(AVCodecContext *)
Flush buffers.
static void avcodec_init(void)
planar GBR 4:4:4 27bpp, little-endian
Definition: pixfmt.h:170
int(* init)(AVCodecContext *)
static double c[64]
uint32_t start_display_time
void av_fast_padded_mallocz(void *ptr, unsigned int *size, size_t min_size)
Same behaviour av_fast_padded_malloc except that buffer will always be 0-initialized after call...
int(* encode_sub)(AVCodecContext *, uint8_t *buf, int buf_size, const struct AVSubtitle *sub)
AVBufferRef * av_buffer_ref(AVBufferRef *buf)
Create a new reference to an AVBuffer.
AVSampleFormat
Audio Sample Formats.
Definition: samplefmt.h:49
#define CODEC_CAP_EXPERIMENTAL
Codec is experimental and is thus avoided in favor of non experimental encoders.
AVProfile.
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of PIX_FMT_YUV444P and setting color_...
Definition: pixfmt.h:82
int ff_thread_can_start_frame(AVCodecContext *avctx)
enum AVCodecID id
Codec implemented by the hardware accelerator.
void ff_thread_report_progress(ThreadFrame *f, int progress, int field)
Notify later decoding threads when part of their reference picture is ready.
packed RGB 3:3:2, 8bpp, (msb)2R 3G 3B(lsb)
Definition: pixfmt.h:90
#define AV_PIX_FMT_RGB555
Definition: pixfmt.h:269
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition: pixfmt.h:75
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
Definition: avpacket.c:56
int den
denominator
Definition: rational.h:45
function y
Definition: D.m:1
int avcodec_default_execute2(AVCodecContext *c, int(*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
unsigned bps
Definition: movenc.c:895
#define FFMPEG_CONFIGURATION
Definition: config.h:4
DSP utils.
void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
static int reget_buffer_internal(AVCodecContext *avctx, AVFrame *frame)
unsigned 8 bits, planar
Definition: samplefmt.h:57
int(* encode2)(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Encode data to an AVPacket.
#define TAG_PRINT(x)
planar YUV 4:4:4,42bpp, (1 Cr & Cb sample per 1x1 Y samples), little-endian
Definition: pixfmt.h:232
as in Berlin toast format
int len
int channels
number of audio channels
const int * supported_samplerates
array of supported audio samplerates, or NULL if unknown, array is terminated by 0 ...
struct AVCodecInternal * internal
Private context used for internal data.
else dst[i][x+y *dst_stride[i]]
Definition: vf_mcdeint.c:160
unsigned avcodec_version(void)
Return the LIBAVCODEC_VERSION_INT constant.
Y , 16bpp, little-endian.
Definition: pixfmt.h:102
char * ass
0 terminated ASS/SSA compatible event line.
static void * avformat_mutex
#define EDGE_WIDTH
Definition: dsputil.h:264
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:139
static int get_bit_rate(AVCodecContext *ctx)
static const struct twinvq_data tab
unsigned int byte_buffer_size
#define HAVE_THREADS
Definition: config.h:274
void avcodec_free_frame(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
void ff_thread_await_progress(ThreadFrame *f, int progress, int field)
Wait for earlier decoding threads to finish reference pictures.
signed 16 bits, planar
Definition: samplefmt.h:58
struct AVPacket::@35 * side_data
Additional packet data that can be provided by the container.
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
void avcodec_register(AVCodec *codec)
Register the codec codec and initialize libavcodec.
int64_t pts_correction_last_dts
PTS of the last frame.
int frame_number
Frame counter, set by libavcodec.
int height
Definition: frame.h:122
Filter the word “frame” indicates either a video frame or a group of audio samples
void ff_frame_thread_encoder_free(AVCodecContext *avctx)
void INT64 INT64 count
Definition: avisynth_c.h:594
int64_t pts_correction_num_faulty_pts
Current statistics for PTS correction.
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
Definition: pixfmt.h:103
void(* init_static_data)(struct AVCodec *codec)
Initialize codec static data, called from avcodec_register().
uint8_t * av_packet_get_side_data(AVPacket *pkt, enum AVPacketSideDataType type, int *size)
Get side information from packet.
Definition: avpacket.c:289
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31))))#define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac){}void ff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map){AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt) > 2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);return NULL;}return ac;}in_planar=av_sample_fmt_is_planar(in_fmt);out_planar=av_sample_fmt_is_planar(out_fmt);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);return ac;}int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){int use_generic=1;int len=in->nb_samples;int p;if(ac->dc){av_dlog(ac->avr,"%d samples - audio_convert: %s to %s (dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> out
int ff_thread_ref_frame(ThreadFrame *dst, ThreadFrame *src)
static int(* ff_lockmgr_cb)(void **mutex, enum AVLockOp op)
static av_always_inline int64_t ff_samples_to_time_base(AVCodecContext *avctx, int64_t samples)
Rescale from sample rate to AVCodecContext.time_base.
AVBufferRef * av_buffer_pool_get(AVBufferPool *pool)
Allocate a new AVBuffer, reusing an old buffer from the pool when available.
enum AVSampleFormat * sample_fmts
array of supported sample formats, or NULL if unknown, array is terminated by -1
int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
Wrapper around get_buffer() for frame-multithreaded codecs.
int nb_channels
const char * av_get_pix_fmt_name(enum AVPixelFormat pix_fmt)
Return the short name for a pixel format, NULL in case pix_fmt is unknown.
Definition: pixdesc.c:1700
int avpriv_unlock_avformat(void)
const uint8_t * avpriv_find_start_code(const uint8_t *av_restrict p, const uint8_t *end, uint32_t *av_restrict state)
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:117
attribute_deprecated int type
Definition: frame.h:258
packed YUV 4:1:1, 12bpp, Cb Y0 Y1 Cr Y2 Y3
Definition: pixfmt.h:86
AVPixelFormat
Pixel format.
Definition: pixfmt.h:66
This structure stores compressed data.
uint8_t * byte_buffer
temporary buffer used for encoders to store their bitstream
int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size, const AVSubtitle *sub)
#define AV_GET_BUFFER_FLAG_REF
The decoder will keep a reference to the frame and may reuse it later.
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:127
int strict_std_compliance
strictly follow the standard (MPEG4, ...).
planar YUV 4:2:0,18bpp, (1 Cr & Cb sample per 2x2 Y samples), big-endian
Definition: pixfmt.h:221
double, planar
Definition: samplefmt.h:61
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
planar YUV 4:4:4, 30bpp, (1 Cr & Cb sample per 1x1 Y samples), big-endian
Definition: pixfmt.h:156
A dummy ID pointing at the start of subtitle codecs.
planar YUV 4:2:2,24bpp, (1 Cr & Cb sample per 2x1 Y samples), big-endian
Definition: pixfmt.h:225
#define FFMAX3(a, b, c)
Definition: common.h:57
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:190
static void compat_free_buffer(void *opaque, uint8_t *data)
planar GBR 4:4:4 30bpp, little-endian
Definition: pixfmt.h:172
int avcodec_default_execute(AVCodecContext *c, int(*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
#define FF_SUB_CHARENC_MODE_PRE_DECODER
the AVPacket data needs to be recoded to UTF-8 before being fed to the decoder, requires iconv ...
void av_get_channel_layout_string(char *buf, int buf_size, int nb_channels, uint64_t channel_layout)
Return a description of a channel layout.
int last_audio_frame
An audio frame with less than required samples has been submitted and padded with silence...
uint8_t * subtitle_header
Header containing style information for text subtitles.
planar YUV 4:2:2, 20bpp, (1 Cr & Cb sample per 2x1 Y samples), big-endian
Definition: pixfmt.h:152