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