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