FFmpeg  2.8.15
 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 
1473  avctx->codec = codec;
1474  if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
1475  avctx->codec_id == AV_CODEC_ID_NONE) {
1476  avctx->codec_type = codec->type;
1477  avctx->codec_id = codec->id;
1478  }
1479  if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
1480  && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
1481  av_log(avctx, AV_LOG_ERROR, "Codec type or id mismatches\n");
1482  ret = AVERROR(EINVAL);
1483  goto free_and_end;
1484  }
1485  avctx->frame_number = 0;
1487 
1488  if ((avctx->codec->capabilities & AV_CODEC_CAP_EXPERIMENTAL) &&
1490  const char *codec_string = av_codec_is_encoder(codec) ? "encoder" : "decoder";
1491  AVCodec *codec2;
1492  av_log(avctx, AV_LOG_ERROR,
1493  "The %s '%s' is experimental but experimental codecs are not enabled, "
1494  "add '-strict %d' if you want to use it.\n",
1495  codec_string, codec->name, FF_COMPLIANCE_EXPERIMENTAL);
1496  codec2 = av_codec_is_encoder(codec) ? avcodec_find_encoder(codec->id) : avcodec_find_decoder(codec->id);
1497  if (!(codec2->capabilities & AV_CODEC_CAP_EXPERIMENTAL))
1498  av_log(avctx, AV_LOG_ERROR, "Alternatively use the non experimental %s '%s'.\n",
1499  codec_string, codec2->name);
1500  ret = AVERROR_EXPERIMENTAL;
1501  goto free_and_end;
1502  }
1503 
1504  if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
1505  (!avctx->time_base.num || !avctx->time_base.den)) {
1506  avctx->time_base.num = 1;
1507  avctx->time_base.den = avctx->sample_rate;
1508  }
1509 
1510  if (!HAVE_THREADS)
1511  av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
1512 
1514  ff_unlock_avcodec(); //we will instanciate a few encoders thus kick the counter to prevent false detection of a problem
1515  ret = ff_frame_thread_encoder_init(avctx, options ? *options : NULL);
1516  ff_lock_avcodec(avctx, codec);
1517  if (ret < 0)
1518  goto free_and_end;
1519  }
1520 
1521  if (HAVE_THREADS
1523  ret = ff_thread_init(avctx);
1524  if (ret < 0) {
1525  goto free_and_end;
1526  }
1527  }
1529  avctx->thread_count = 1;
1530 
1531  if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
1532  av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
1533  avctx->codec->max_lowres);
1534  ret = AVERROR(EINVAL);
1535  goto free_and_end;
1536  }
1537 
1538 #if FF_API_VISMV
1539  if (avctx->debug_mv)
1540  av_log(avctx, AV_LOG_WARNING, "The 'vismv' option is deprecated, "
1541  "see the codecview filter instead.\n");
1542 #endif
1543 
1544  if (av_codec_is_encoder(avctx->codec)) {
1545  int i;
1546 #if FF_API_CODED_FRAME
1548  avctx->coded_frame = av_frame_alloc();
1549  if (!avctx->coded_frame) {
1550  ret = AVERROR(ENOMEM);
1551  goto free_and_end;
1552  }
1554 #endif
1555  if (avctx->codec->sample_fmts) {
1556  for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
1557  if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
1558  break;
1559  if (avctx->channels == 1 &&
1562  avctx->sample_fmt = avctx->codec->sample_fmts[i];
1563  break;
1564  }
1565  }
1566  if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
1567  char buf[128];
1568  snprintf(buf, sizeof(buf), "%d", avctx->sample_fmt);
1569  av_log(avctx, AV_LOG_ERROR, "Specified sample format %s is invalid or not supported\n",
1570  (char *)av_x_if_null(av_get_sample_fmt_name(avctx->sample_fmt), buf));
1571  ret = AVERROR(EINVAL);
1572  goto free_and_end;
1573  }
1574  }
1575  if (avctx->codec->pix_fmts) {
1576  for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
1577  if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
1578  break;
1579  if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE
1580  && !((avctx->codec_id == AV_CODEC_ID_MJPEG || avctx->codec_id == AV_CODEC_ID_LJPEG)
1582  char buf[128];
1583  snprintf(buf, sizeof(buf), "%d", avctx->pix_fmt);
1584  av_log(avctx, AV_LOG_ERROR, "Specified pixel format %s is invalid or not supported\n",
1585  (char *)av_x_if_null(av_get_pix_fmt_name(avctx->pix_fmt), buf));
1586  ret = AVERROR(EINVAL);
1587  goto free_and_end;
1588  }
1589  if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ420P ||
1590  avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ411P ||
1591  avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ422P ||
1592  avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ440P ||
1593  avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ444P)
1594  avctx->color_range = AVCOL_RANGE_JPEG;
1595  }
1596  if (avctx->codec->supported_samplerates) {
1597  for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
1598  if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
1599  break;
1600  if (avctx->codec->supported_samplerates[i] == 0) {
1601  av_log(avctx, AV_LOG_ERROR, "Specified sample rate %d is not supported\n",
1602  avctx->sample_rate);
1603  ret = AVERROR(EINVAL);
1604  goto free_and_end;
1605  }
1606  }
1607  if (avctx->sample_rate < 0) {
1608  av_log(avctx, AV_LOG_ERROR, "Specified sample rate %d is not supported\n",
1609  avctx->sample_rate);
1610  ret = AVERROR(EINVAL);
1611  goto free_and_end;
1612  }
1613  if (avctx->codec->channel_layouts) {
1614  if (!avctx->channel_layout) {
1615  av_log(avctx, AV_LOG_WARNING, "Channel layout not specified\n");
1616  } else {
1617  for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
1618  if (avctx->channel_layout == avctx->codec->channel_layouts[i])
1619  break;
1620  if (avctx->codec->channel_layouts[i] == 0) {
1621  char buf[512];
1622  av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
1623  av_log(avctx, AV_LOG_ERROR, "Specified channel layout '%s' is not supported\n", buf);
1624  ret = AVERROR(EINVAL);
1625  goto free_and_end;
1626  }
1627  }
1628  }
1629  if (avctx->channel_layout && avctx->channels) {
1630  int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
1631  if (channels != avctx->channels) {
1632  char buf[512];
1633  av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
1634  av_log(avctx, AV_LOG_ERROR,
1635  "Channel layout '%s' with %d channels does not match number of specified channels %d\n",
1636  buf, channels, avctx->channels);
1637  ret = AVERROR(EINVAL);
1638  goto free_and_end;
1639  }
1640  } else if (avctx->channel_layout) {
1642  }
1643  if (avctx->channels < 0) {
1644  av_log(avctx, AV_LOG_ERROR, "Specified number of channels %d is not supported\n",
1645  avctx->channels);
1646  ret = AVERROR(EINVAL);
1647  goto free_and_end;
1648  }
1649  if(avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
1650  if (avctx->width <= 0 || avctx->height <= 0) {
1651  av_log(avctx, AV_LOG_ERROR, "dimensions not set\n");
1652  ret = AVERROR(EINVAL);
1653  goto free_and_end;
1654  }
1655  }
1656  if ( (avctx->codec_type == AVMEDIA_TYPE_VIDEO || avctx->codec_type == AVMEDIA_TYPE_AUDIO)
1657  && avctx->bit_rate>0 && avctx->bit_rate<1000) {
1658  av_log(avctx, AV_LOG_WARNING, "Bitrate %d is extremely low, maybe you mean %dk\n", avctx->bit_rate, avctx->bit_rate);
1659  }
1660 
1661  if (!avctx->rc_initial_buffer_occupancy)
1662  avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3 / 4;
1663  }
1664 
1666  avctx->pts_correction_num_faulty_dts = 0;
1667  avctx->pts_correction_last_pts =
1668  avctx->pts_correction_last_dts = INT64_MIN;
1669 
1670  if ( !CONFIG_GRAY && avctx->flags & AV_CODEC_FLAG_GRAY
1672  av_log(avctx, AV_LOG_WARNING,
1673  "gray decoding requested but not enabled at configuration time\n");
1674 
1675  if ( avctx->codec->init && (!(avctx->active_thread_type&FF_THREAD_FRAME)
1676  || avctx->internal->frame_thread_encoder)) {
1677  ret = avctx->codec->init(avctx);
1678  if (ret < 0) {
1679  goto free_and_end;
1680  }
1681  }
1682 
1683  ret=0;
1684 
1685 #if FF_API_AUDIOENC_DELAY
1686  if (av_codec_is_encoder(avctx->codec))
1687  avctx->delay = avctx->initial_padding;
1688 #endif
1689 
1690  if (av_codec_is_decoder(avctx->codec)) {
1691  if (!avctx->bit_rate)
1692  avctx->bit_rate = get_bit_rate(avctx);
1693  /* validate channel layout from the decoder */
1694  if (avctx->channel_layout) {
1695  int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
1696  if (!avctx->channels)
1697  avctx->channels = channels;
1698  else if (channels != avctx->channels) {
1699  char buf[512];
1700  av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
1701  av_log(avctx, AV_LOG_WARNING,
1702  "Channel layout '%s' with %d channels does not match specified number of channels %d: "
1703  "ignoring specified channel layout\n",
1704  buf, channels, avctx->channels);
1705  avctx->channel_layout = 0;
1706  }
1707  }
1708  if (avctx->channels && avctx->channels < 0 ||
1709  avctx->channels > FF_SANE_NB_CHANNELS) {
1710  ret = AVERROR(EINVAL);
1711  goto free_and_end;
1712  }
1713  if (avctx->sub_charenc) {
1714  if (avctx->codec_type != AVMEDIA_TYPE_SUBTITLE) {
1715  av_log(avctx, AV_LOG_ERROR, "Character encoding is only "
1716  "supported with subtitles codecs\n");
1717  ret = AVERROR(EINVAL);
1718  goto free_and_end;
1719  } else if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB) {
1720  av_log(avctx, AV_LOG_WARNING, "Codec '%s' is bitmap-based, "
1721  "subtitles character encoding will be ignored\n",
1722  avctx->codec_descriptor->name);
1724  } else {
1725  /* input character encoding is set for a text based subtitle
1726  * codec at this point */
1729 
1731 #if CONFIG_ICONV
1732  iconv_t cd = iconv_open("UTF-8", avctx->sub_charenc);
1733  if (cd == (iconv_t)-1) {
1734  ret = AVERROR(errno);
1735  av_log(avctx, AV_LOG_ERROR, "Unable to open iconv context "
1736  "with input character encoding \"%s\"\n", avctx->sub_charenc);
1737  goto free_and_end;
1738  }
1739  iconv_close(cd);
1740 #else
1741  av_log(avctx, AV_LOG_ERROR, "Character encoding subtitles "
1742  "conversion needs a libavcodec built with iconv support "
1743  "for this codec\n");
1744  ret = AVERROR(ENOSYS);
1745  goto free_and_end;
1746 #endif
1747  }
1748  }
1749  }
1750 
1751 #if FF_API_AVCTX_TIMEBASE
1752  if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
1753  avctx->time_base = av_inv_q(av_mul_q(avctx->framerate, (AVRational){avctx->ticks_per_frame, 1}));
1754 #endif
1755  }
1756  if (codec->priv_data_size > 0 && avctx->priv_data && codec->priv_class) {
1757  av_assert0(*(const AVClass **)avctx->priv_data == codec->priv_class);
1758  }
1759 
1760 end:
1762  if (options) {
1763  av_dict_free(options);
1764  *options = tmp;
1765  }
1766 
1767  return ret;
1768 free_and_end:
1769  if (avctx->codec &&
1771  avctx->codec->close(avctx);
1772 
1773  if (codec->priv_class && codec->priv_data_size)
1774  av_opt_free(avctx->priv_data);
1775  av_opt_free(avctx);
1776 
1777 #if FF_API_CODED_FRAME
1779  av_frame_free(&avctx->coded_frame);
1781 #endif
1782 
1783  av_dict_free(&tmp);
1784  av_freep(&avctx->priv_data);
1785  if (avctx->internal) {
1786  av_frame_free(&avctx->internal->to_free);
1787  av_freep(&avctx->internal->pool);
1788  }
1789  av_freep(&avctx->internal);
1790  avctx->codec = NULL;
1791  goto end;
1792 }
1793 
1794 int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
1795 {
1796  if (avpkt->size < 0) {
1797  av_log(avctx, AV_LOG_ERROR, "Invalid negative user packet size %d\n", avpkt->size);
1798  return AVERROR(EINVAL);
1799  }
1801  av_log(avctx, AV_LOG_ERROR, "Invalid minimum required packet size %"PRId64" (max allowed is %d)\n",
1802  size, INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE);
1803  return AVERROR(EINVAL);
1804  }
1805 
1806  if (avctx && 2*min_size < size) { // FIXME The factor needs to be finetuned
1807  av_assert0(!avpkt->data || avpkt->data != avctx->internal->byte_buffer);
1808  if (!avpkt->data || avpkt->size < size) {
1810  avpkt->data = avctx->internal->byte_buffer;
1811  avpkt->size = avctx->internal->byte_buffer_size;
1812 #if FF_API_DESTRUCT_PACKET
1814  avpkt->destruct = NULL;
1816 #endif
1817  }
1818  }
1819 
1820  if (avpkt->data) {
1821  AVBufferRef *buf = avpkt->buf;
1822 #if FF_API_DESTRUCT_PACKET
1824  void *destruct = avpkt->destruct;
1826 #endif
1827 
1828  if (avpkt->size < size) {
1829  av_log(avctx, AV_LOG_ERROR, "User packet is too small (%d < %"PRId64")\n", avpkt->size, size);
1830  return AVERROR(EINVAL);
1831  }
1832 
1833  av_init_packet(avpkt);
1834 #if FF_API_DESTRUCT_PACKET
1836  avpkt->destruct = destruct;
1838 #endif
1839  avpkt->buf = buf;
1840  avpkt->size = size;
1841  return 0;
1842  } else {
1843  int ret = av_new_packet(avpkt, size);
1844  if (ret < 0)
1845  av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %"PRId64"\n", size);
1846  return ret;
1847  }
1848 }
1849 
1851 {
1852  return ff_alloc_packet2(NULL, avpkt, size, 0);
1853 }
1854 
1855 /**
1856  * Pad last frame with silence.
1857  */
1858 static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
1859 {
1860  AVFrame *frame = NULL;
1861  int ret;
1862 
1863  if (!(frame = av_frame_alloc()))
1864  return AVERROR(ENOMEM);
1865 
1866  frame->format = src->format;
1867  frame->channel_layout = src->channel_layout;
1869  frame->nb_samples = s->frame_size;
1870  ret = av_frame_get_buffer(frame, 32);
1871  if (ret < 0)
1872  goto fail;
1873 
1874  ret = av_frame_copy_props(frame, src);
1875  if (ret < 0)
1876  goto fail;
1877 
1878  if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
1879  src->nb_samples, s->channels, s->sample_fmt)) < 0)
1880  goto fail;
1881  if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
1882  frame->nb_samples - src->nb_samples,
1883  s->channels, s->sample_fmt)) < 0)
1884  goto fail;
1885 
1886  *dst = frame;
1887 
1888  return 0;
1889 
1890 fail:
1891  av_frame_free(&frame);
1892  return ret;
1893 }
1894 
1896  AVPacket *avpkt,
1897  const AVFrame *frame,
1898  int *got_packet_ptr)
1899 {
1900  AVFrame *extended_frame = NULL;
1901  AVFrame *padded_frame = NULL;
1902  int ret;
1903  AVPacket user_pkt = *avpkt;
1904  int needs_realloc = !user_pkt.data;
1905 
1906  *got_packet_ptr = 0;
1907 
1908  if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY) && !frame) {
1909  av_free_packet(avpkt);
1910  av_init_packet(avpkt);
1911  return 0;
1912  }
1913 
1914  /* ensure that extended_data is properly set */
1915  if (frame && !frame->extended_data) {
1916  if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
1917  avctx->channels > AV_NUM_DATA_POINTERS) {
1918  av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
1919  "with more than %d channels, but extended_data is not set.\n",
1921  return AVERROR(EINVAL);
1922  }
1923  av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
1924 
1925  extended_frame = av_frame_alloc();
1926  if (!extended_frame)
1927  return AVERROR(ENOMEM);
1928 
1929  memcpy(extended_frame, frame, sizeof(AVFrame));
1930  extended_frame->extended_data = extended_frame->data;
1931  frame = extended_frame;
1932  }
1933 
1934  /* extract audio service type metadata */
1935  if (frame) {
1937  if (sd && sd->size >= sizeof(enum AVAudioServiceType))
1938  avctx->audio_service_type = *(enum AVAudioServiceType*)sd->data;
1939  }
1940 
1941  /* check for valid frame size */
1942  if (frame) {
1944  if (frame->nb_samples > avctx->frame_size) {
1945  av_log(avctx, AV_LOG_ERROR, "more samples than frame size (avcodec_encode_audio2)\n");
1946  ret = AVERROR(EINVAL);
1947  goto end;
1948  }
1949  } else if (!(avctx->codec->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE)) {
1950  if (frame->nb_samples < avctx->frame_size &&
1951  !avctx->internal->last_audio_frame) {
1952  ret = pad_last_frame(avctx, &padded_frame, frame);
1953  if (ret < 0)
1954  goto end;
1955 
1956  frame = padded_frame;
1957  avctx->internal->last_audio_frame = 1;
1958  }
1959 
1960  if (frame->nb_samples != avctx->frame_size) {
1961  av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) != frame_size (%d) (avcodec_encode_audio2)\n", frame->nb_samples, avctx->frame_size);
1962  ret = AVERROR(EINVAL);
1963  goto end;
1964  }
1965  }
1966  }
1967 
1968  av_assert0(avctx->codec->encode2);
1969 
1970  ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1971  if (!ret) {
1972  if (*got_packet_ptr) {
1973  if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY)) {
1974  if (avpkt->pts == AV_NOPTS_VALUE)
1975  avpkt->pts = frame->pts;
1976  if (!avpkt->duration)
1977  avpkt->duration = ff_samples_to_time_base(avctx,
1978  frame->nb_samples);
1979  }
1980  avpkt->dts = avpkt->pts;
1981  } else {
1982  avpkt->size = 0;
1983  }
1984  }
1985  if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
1986  needs_realloc = 0;
1987  if (user_pkt.data) {
1988  if (user_pkt.size >= avpkt->size) {
1989  memcpy(user_pkt.data, avpkt->data, avpkt->size);
1990  } else {
1991  av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
1992  avpkt->size = user_pkt.size;
1993  ret = -1;
1994  }
1995  avpkt->buf = user_pkt.buf;
1996  avpkt->data = user_pkt.data;
1997 #if FF_API_DESTRUCT_PACKET
1999  avpkt->destruct = user_pkt.destruct;
2001 #endif
2002  } else {
2003  if (av_dup_packet(avpkt) < 0) {
2004  ret = AVERROR(ENOMEM);
2005  }
2006  }
2007  }
2008 
2009  if (!ret) {
2010  if (needs_realloc && avpkt->data) {
2011  ret = av_buffer_realloc(&avpkt->buf, avpkt->size + AV_INPUT_BUFFER_PADDING_SIZE);
2012  if (ret >= 0)
2013  avpkt->data = avpkt->buf->data;
2014  }
2015 
2016  avctx->frame_number++;
2017  }
2018 
2019  if (ret < 0 || !*got_packet_ptr) {
2020  av_free_packet(avpkt);
2021  av_init_packet(avpkt);
2022  goto end;
2023  }
2024 
2025  /* NOTE: if we add any audio encoders which output non-keyframe packets,
2026  * this needs to be moved to the encoders, but for now we can do it
2027  * here to simplify things */
2028  avpkt->flags |= AV_PKT_FLAG_KEY;
2029 
2030 end:
2031  av_frame_free(&padded_frame);
2032  av_free(extended_frame);
2033 
2034 #if FF_API_AUDIOENC_DELAY
2035  avctx->delay = avctx->initial_padding;
2036 #endif
2037 
2038  return ret;
2039 }
2040 
2041 #if FF_API_OLD_ENCODE_AUDIO
2043  uint8_t *buf, int buf_size,
2044  const short *samples)
2045 {
2046  AVPacket pkt;
2047  AVFrame *frame;
2048  int ret, samples_size, got_packet;
2049 
2050  av_init_packet(&pkt);
2051  pkt.data = buf;
2052  pkt.size = buf_size;
2053 
2054  if (samples) {
2055  frame = av_frame_alloc();
2056  if (!frame)
2057  return AVERROR(ENOMEM);
2058 
2059  if (avctx->frame_size) {
2060  frame->nb_samples = avctx->frame_size;
2061  } else {
2062  /* if frame_size is not set, the number of samples must be
2063  * calculated from the buffer size */
2064  int64_t nb_samples;
2065  if (!av_get_bits_per_sample(avctx->codec_id)) {
2066  av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
2067  "support this codec\n");
2068  av_frame_free(&frame);
2069  return AVERROR(EINVAL);
2070  }
2071  nb_samples = (int64_t)buf_size * 8 /
2072  (av_get_bits_per_sample(avctx->codec_id) *
2073  avctx->channels);
2074  if (nb_samples >= INT_MAX) {
2075  av_frame_free(&frame);
2076  return AVERROR(EINVAL);
2077  }
2078  frame->nb_samples = nb_samples;
2079  }
2080 
2081  /* it is assumed that the samples buffer is large enough based on the
2082  * relevant parameters */
2083  samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
2084  frame->nb_samples,
2085  avctx->sample_fmt, 1);
2086  if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
2087  avctx->sample_fmt,
2088  (const uint8_t *)samples,
2089  samples_size, 1)) < 0) {
2090  av_frame_free(&frame);
2091  return ret;
2092  }
2093 
2094  /* fabricate frame pts from sample count.
2095  * this is needed because the avcodec_encode_audio() API does not have
2096  * a way for the user to provide pts */
2097  if (avctx->sample_rate && avctx->time_base.num)
2098  frame->pts = ff_samples_to_time_base(avctx,
2099  avctx->internal->sample_count);
2100  else
2101  frame->pts = AV_NOPTS_VALUE;
2102  avctx->internal->sample_count += frame->nb_samples;
2103  } else {
2104  frame = NULL;
2105  }
2106 
2107  got_packet = 0;
2108  ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
2109 #if FF_API_CODED_FRAME
2111  if (!ret && got_packet && avctx->coded_frame) {
2112  avctx->coded_frame->pts = pkt.pts;
2113  avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
2114  }
2116 #endif
2117 
2118  /* free any side data since we cannot return it */
2120 
2121  if (frame && frame->extended_data != frame->data)
2122  av_freep(&frame->extended_data);
2123 
2124  av_frame_free(&frame);
2125  return ret ? ret : pkt.size;
2126 }
2127 
2128 #endif
2129 
2130 #if FF_API_OLD_ENCODE_VIDEO
2132  const AVFrame *pict)
2133 {
2134  AVPacket pkt;
2135  int ret, got_packet = 0;
2136 
2137  if (buf_size < AV_INPUT_BUFFER_MIN_SIZE) {
2138  av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
2139  return -1;
2140  }
2141 
2142  av_init_packet(&pkt);
2143  pkt.data = buf;
2144  pkt.size = buf_size;
2145 
2146  ret = avcodec_encode_video2(avctx, &pkt, pict, &got_packet);
2147 #if FF_API_CODED_FRAME
2149  if (!ret && got_packet && avctx->coded_frame) {
2150  avctx->coded_frame->pts = pkt.pts;
2151  avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
2154  }
2156 #endif
2157 
2158  /* free any side data since we cannot return it */
2159  if (pkt.side_data_elems > 0) {
2160  int i;
2161  for (i = 0; i < pkt.side_data_elems; i++)
2162  av_free(pkt.side_data[i].data);
2163  av_freep(&pkt.side_data);
2164  pkt.side_data_elems = 0;
2165  }
2166 
2167  return ret ? ret : pkt.size;
2168 }
2169 
2170 #endif
2171 
2173  AVPacket *avpkt,
2174  const AVFrame *frame,
2175  int *got_packet_ptr)
2176 {
2177  int ret;
2178  AVPacket user_pkt = *avpkt;
2179  int needs_realloc = !user_pkt.data;
2180 
2181  *got_packet_ptr = 0;
2182 
2185  return ff_thread_video_encode_frame(avctx, avpkt, frame, got_packet_ptr);
2186 
2187  if ((avctx->flags&AV_CODEC_FLAG_PASS1) && avctx->stats_out)
2188  avctx->stats_out[0] = '\0';
2189 
2190  if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY) && !frame) {
2191  av_free_packet(avpkt);
2192  av_init_packet(avpkt);
2193  avpkt->size = 0;
2194  return 0;
2195  }
2196 
2197  if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
2198  return AVERROR(EINVAL);
2199 
2200  if (frame && frame->format == AV_PIX_FMT_NONE)
2201  av_log(avctx, AV_LOG_WARNING, "AVFrame.format is not set\n");
2202  if (frame && (frame->width == 0 || frame->height == 0))
2203  av_log(avctx, AV_LOG_WARNING, "AVFrame.width or height is not set\n");
2204 
2205  av_assert0(avctx->codec->encode2);
2206 
2207  ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
2208  av_assert0(ret <= 0);
2209 
2210  if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
2211  needs_realloc = 0;
2212  if (user_pkt.data) {
2213  if (user_pkt.size >= avpkt->size) {
2214  memcpy(user_pkt.data, avpkt->data, avpkt->size);
2215  } else {
2216  av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
2217  avpkt->size = user_pkt.size;
2218  ret = -1;
2219  }
2220  avpkt->buf = user_pkt.buf;
2221  avpkt->data = user_pkt.data;
2222 #if FF_API_DESTRUCT_PACKET
2224  avpkt->destruct = user_pkt.destruct;
2226 #endif
2227  } else {
2228  if (av_dup_packet(avpkt) < 0) {
2229  ret = AVERROR(ENOMEM);
2230  }
2231  }
2232  }
2233 
2234  if (!ret) {
2235  if (!*got_packet_ptr)
2236  avpkt->size = 0;
2237  else if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
2238  avpkt->pts = avpkt->dts = frame->pts;
2239 
2240  if (needs_realloc && avpkt->data) {
2241  ret = av_buffer_realloc(&avpkt->buf, avpkt->size + AV_INPUT_BUFFER_PADDING_SIZE);
2242  if (ret >= 0)
2243  avpkt->data = avpkt->buf->data;
2244  }
2245 
2246  avctx->frame_number++;
2247  }
2248 
2249  if (ret < 0 || !*got_packet_ptr)
2250  av_free_packet(avpkt);
2251 
2252  emms_c();
2253  return ret;
2254 }
2255 
2257  const AVSubtitle *sub)
2258 {
2259  int ret;
2260  if (sub->start_display_time) {
2261  av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
2262  return -1;
2263  }
2264 
2265  ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
2266  avctx->frame_number++;
2267  return ret;
2268 }
2269 
2270 /**
2271  * Attempt to guess proper monotonic timestamps for decoded video frames
2272  * which might have incorrect times. Input timestamps may wrap around, in
2273  * which case the output will as well.
2274  *
2275  * @param pts the pts field of the decoded AVPacket, as passed through
2276  * AVFrame.pkt_pts
2277  * @param dts the dts field of the decoded AVPacket
2278  * @return one of the input values, may be AV_NOPTS_VALUE
2279  */
2280 static int64_t guess_correct_pts(AVCodecContext *ctx,
2281  int64_t reordered_pts, int64_t dts)
2282 {
2283  int64_t pts = AV_NOPTS_VALUE;
2284 
2285  if (dts != AV_NOPTS_VALUE) {
2287  ctx->pts_correction_last_dts = dts;
2288  } else if (reordered_pts != AV_NOPTS_VALUE)
2289  ctx->pts_correction_last_dts = reordered_pts;
2290 
2291  if (reordered_pts != AV_NOPTS_VALUE) {
2292  ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
2293  ctx->pts_correction_last_pts = reordered_pts;
2294  } else if(dts != AV_NOPTS_VALUE)
2295  ctx->pts_correction_last_pts = dts;
2296 
2298  && reordered_pts != AV_NOPTS_VALUE)
2299  pts = reordered_pts;
2300  else
2301  pts = dts;
2302 
2303  return pts;
2304 }
2305 
2306 static int apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
2307 {
2308  int size = 0, ret;
2309  const uint8_t *data;
2310  uint32_t flags;
2311  int64_t val;
2312 
2313  data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
2314  if (!data)
2315  return 0;
2316 
2317  if (!(avctx->codec->capabilities & AV_CODEC_CAP_PARAM_CHANGE)) {
2318  av_log(avctx, AV_LOG_ERROR, "This decoder does not support parameter "
2319  "changes, but PARAM_CHANGE side data was sent to it.\n");
2320  return AVERROR(EINVAL);
2321  }
2322 
2323  if (size < 4)
2324  goto fail;
2325 
2326  flags = bytestream_get_le32(&data);
2327  size -= 4;
2328 
2330  if (size < 4)
2331  goto fail;
2332  val = bytestream_get_le32(&data);
2333  if (val <= 0 || val > INT_MAX) {
2334  av_log(avctx, AV_LOG_ERROR, "Invalid channel count");
2335  return AVERROR_INVALIDDATA;
2336  }
2337  avctx->channels = val;
2338  size -= 4;
2339  }
2341  if (size < 8)
2342  goto fail;
2343  avctx->channel_layout = bytestream_get_le64(&data);
2344  size -= 8;
2345  }
2347  if (size < 4)
2348  goto fail;
2349  val = bytestream_get_le32(&data);
2350  if (val <= 0 || val > INT_MAX) {
2351  av_log(avctx, AV_LOG_ERROR, "Invalid sample rate");
2352  return AVERROR_INVALIDDATA;
2353  }
2354  avctx->sample_rate = val;
2355  size -= 4;
2356  }
2358  if (size < 8)
2359  goto fail;
2360  avctx->width = bytestream_get_le32(&data);
2361  avctx->height = bytestream_get_le32(&data);
2362  size -= 8;
2363  ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
2364  if (ret < 0)
2365  return ret;
2366  }
2367 
2368  return 0;
2369 fail:
2370  av_log(avctx, AV_LOG_ERROR, "PARAM_CHANGE side data too small.\n");
2371  return AVERROR_INVALIDDATA;
2372 }
2373 
2375 {
2376  int ret;
2377 
2378  /* move the original frame to our backup */
2379  av_frame_unref(avci->to_free);
2380  av_frame_move_ref(avci->to_free, frame);
2381 
2382  /* now copy everything except the AVBufferRefs back
2383  * note that we make a COPY of the side data, so calling av_frame_free() on
2384  * the caller's frame will work properly */
2385  ret = av_frame_copy_props(frame, avci->to_free);
2386  if (ret < 0)
2387  return ret;
2388 
2389  memcpy(frame->data, avci->to_free->data, sizeof(frame->data));
2390  memcpy(frame->linesize, avci->to_free->linesize, sizeof(frame->linesize));
2391  if (avci->to_free->extended_data != avci->to_free->data) {
2392  int planes = av_frame_get_channels(avci->to_free);
2393  int size = planes * sizeof(*frame->extended_data);
2394 
2395  if (!size) {
2396  av_frame_unref(frame);
2397  return AVERROR_BUG;
2398  }
2399 
2400  frame->extended_data = av_malloc(size);
2401  if (!frame->extended_data) {
2402  av_frame_unref(frame);
2403  return AVERROR(ENOMEM);
2404  }
2405  memcpy(frame->extended_data, avci->to_free->extended_data,
2406  size);
2407  } else
2408  frame->extended_data = frame->data;
2409 
2410  frame->format = avci->to_free->format;
2411  frame->width = avci->to_free->width;
2412  frame->height = avci->to_free->height;
2413  frame->channel_layout = avci->to_free->channel_layout;
2414  frame->nb_samples = avci->to_free->nb_samples;
2416 
2417  return 0;
2418 }
2419 
2421  int *got_picture_ptr,
2422  const AVPacket *avpkt)
2423 {
2424  AVCodecInternal *avci = avctx->internal;
2425  int ret;
2426  // copy to ensure we do not change avpkt
2427  AVPacket tmp = *avpkt;
2428 
2429  if (!avctx->codec)
2430  return AVERROR(EINVAL);
2431  if (avctx->codec->type != AVMEDIA_TYPE_VIDEO) {
2432  av_log(avctx, AV_LOG_ERROR, "Invalid media type for video\n");
2433  return AVERROR(EINVAL);
2434  }
2435 
2436  *got_picture_ptr = 0;
2437  if ((avctx->coded_width || avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
2438  return AVERROR(EINVAL);
2439 
2440  av_frame_unref(picture);
2441 
2442  if ((avctx->codec->capabilities & AV_CODEC_CAP_DELAY) || avpkt->size ||
2443  (avctx->active_thread_type & FF_THREAD_FRAME)) {
2444  int did_split = av_packet_split_side_data(&tmp);
2445  ret = apply_param_change(avctx, &tmp);
2446  if (ret < 0) {
2447  av_log(avctx, AV_LOG_ERROR, "Error applying parameter changes.\n");
2448  if (avctx->err_recognition & AV_EF_EXPLODE)
2449  goto fail;
2450  }
2451 
2452  avctx->internal->pkt = &tmp;
2454  ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
2455  &tmp);
2456  else {
2457  ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
2458  &tmp);
2459  picture->pkt_dts = avpkt->dts;
2460 
2461  if(!avctx->has_b_frames){
2462  av_frame_set_pkt_pos(picture, avpkt->pos);
2463  }
2464  //FIXME these should be under if(!avctx->has_b_frames)
2465  /* get_buffer is supposed to set frame parameters */
2466  if (!(avctx->codec->capabilities & AV_CODEC_CAP_DR1)) {
2467  if (!picture->sample_aspect_ratio.num) picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
2468  if (!picture->width) picture->width = avctx->width;
2469  if (!picture->height) picture->height = avctx->height;
2470  if (picture->format == AV_PIX_FMT_NONE) picture->format = avctx->pix_fmt;
2471  }
2472  }
2473 
2474 fail:
2475  emms_c(); //needed to avoid an emms_c() call before every return;
2476 
2477  avctx->internal->pkt = NULL;
2478  if (did_split) {
2480  if(ret == tmp.size)
2481  ret = avpkt->size;
2482  }
2483 
2484  if (*got_picture_ptr) {
2485  if (!avctx->refcounted_frames) {
2486  int err = unrefcount_frame(avci, picture);
2487  if (err < 0)
2488  return err;
2489  }
2490 
2491  avctx->frame_number++;
2493  guess_correct_pts(avctx,
2494  picture->pkt_pts,
2495  picture->pkt_dts));
2496  } else
2497  av_frame_unref(picture);
2498  } else
2499  ret = 0;
2500 
2501  /* many decoders assign whole AVFrames, thus overwriting extended_data;
2502  * make sure it's set correctly */
2503  av_assert0(!picture->extended_data || picture->extended_data == picture->data);
2504 
2505 #if FF_API_AVCTX_TIMEBASE
2506  if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
2507  avctx->time_base = av_inv_q(av_mul_q(avctx->framerate, (AVRational){avctx->ticks_per_frame, 1}));
2508 #endif
2509 
2510  return ret;
2511 }
2512 
2513 #if FF_API_OLD_DECODE_AUDIO
2515  int *frame_size_ptr,
2516  AVPacket *avpkt)
2517 {
2519  int ret, got_frame = 0;
2520 
2521  if (!frame)
2522  return AVERROR(ENOMEM);
2523 #if FF_API_GET_BUFFER
2525  if (avctx->get_buffer != avcodec_default_get_buffer) {
2526  av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
2527  "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
2528  av_log(avctx, AV_LOG_ERROR, "Please port your application to "
2529  "avcodec_decode_audio4()\n");
2532  }
2534 #endif
2535 
2536  ret = avcodec_decode_audio4(avctx, frame, &got_frame, avpkt);
2537 
2538  if (ret >= 0 && got_frame) {
2539  int ch, plane_size;
2540  int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
2541  int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
2542  frame->nb_samples,
2543  avctx->sample_fmt, 1);
2544  if (*frame_size_ptr < data_size) {
2545  av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
2546  "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
2547  av_frame_free(&frame);
2548  return AVERROR(EINVAL);
2549  }
2550 
2551  memcpy(samples, frame->extended_data[0], plane_size);
2552 
2553  if (planar && avctx->channels > 1) {
2554  uint8_t *out = ((uint8_t *)samples) + plane_size;
2555  for (ch = 1; ch < avctx->channels; ch++) {
2556  memcpy(out, frame->extended_data[ch], plane_size);
2557  out += plane_size;
2558  }
2559  }
2560  *frame_size_ptr = data_size;
2561  } else {
2562  *frame_size_ptr = 0;
2563  }
2564  av_frame_free(&frame);
2565  return ret;
2566 }
2567 
2568 #endif
2569 
2571  AVFrame *frame,
2572  int *got_frame_ptr,
2573  const AVPacket *avpkt)
2574 {
2575  AVCodecInternal *avci = avctx->internal;
2576  int ret = 0;
2577 
2578  *got_frame_ptr = 0;
2579 
2580  if (!avpkt->data && avpkt->size) {
2581  av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
2582  return AVERROR(EINVAL);
2583  }
2584  if (!avctx->codec)
2585  return AVERROR(EINVAL);
2586  if (avctx->codec->type != AVMEDIA_TYPE_AUDIO) {
2587  av_log(avctx, AV_LOG_ERROR, "Invalid media type for audio\n");
2588  return AVERROR(EINVAL);
2589  }
2590 
2591  av_frame_unref(frame);
2592 
2593  if ((avctx->codec->capabilities & AV_CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
2594  uint8_t *side;
2595  int side_size;
2596  uint32_t discard_padding = 0;
2597  uint8_t skip_reason = 0;
2598  uint8_t discard_reason = 0;
2599  // copy to ensure we do not change avpkt
2600  AVPacket tmp = *avpkt;
2601  int did_split = av_packet_split_side_data(&tmp);
2602  ret = apply_param_change(avctx, &tmp);
2603  if (ret < 0) {
2604  av_log(avctx, AV_LOG_ERROR, "Error applying parameter changes.\n");
2605  if (avctx->err_recognition & AV_EF_EXPLODE)
2606  goto fail;
2607  }
2608 
2609  avctx->internal->pkt = &tmp;
2611  ret = ff_thread_decode_frame(avctx, frame, got_frame_ptr, &tmp);
2612  else {
2613  ret = avctx->codec->decode(avctx, frame, got_frame_ptr, &tmp);
2614  av_assert0(ret <= tmp.size);
2615  frame->pkt_dts = avpkt->dts;
2616  }
2617  if (ret >= 0 && *got_frame_ptr) {
2618  avctx->frame_number++;
2620  guess_correct_pts(avctx,
2621  frame->pkt_pts,
2622  frame->pkt_dts));
2623  if (frame->format == AV_SAMPLE_FMT_NONE)
2624  frame->format = avctx->sample_fmt;
2625  if (!frame->channel_layout)
2626  frame->channel_layout = avctx->channel_layout;
2627  if (!av_frame_get_channels(frame))
2628  av_frame_set_channels(frame, avctx->channels);
2629  if (!frame->sample_rate)
2630  frame->sample_rate = avctx->sample_rate;
2631  }
2632 
2633  side= av_packet_get_side_data(avctx->internal->pkt, AV_PKT_DATA_SKIP_SAMPLES, &side_size);
2634  if(side && side_size>=10) {
2635  avctx->internal->skip_samples = AV_RL32(side);
2636  discard_padding = AV_RL32(side + 4);
2637  av_log(avctx, AV_LOG_DEBUG, "skip %d / discard %d samples due to side data\n",
2638  avctx->internal->skip_samples, (int)discard_padding);
2639  skip_reason = AV_RL8(side + 8);
2640  discard_reason = AV_RL8(side + 9);
2641  }
2642  if (avctx->internal->skip_samples > 0 && *got_frame_ptr &&
2643  !(avctx->flags2 & AV_CODEC_FLAG2_SKIP_MANUAL)) {
2644  if(frame->nb_samples <= avctx->internal->skip_samples){
2645  *got_frame_ptr = 0;
2646  avctx->internal->skip_samples -= frame->nb_samples;
2647  av_log(avctx, AV_LOG_DEBUG, "skip whole frame, skip left: %d\n",
2648  avctx->internal->skip_samples);
2649  } else {
2651  frame->nb_samples - avctx->internal->skip_samples, avctx->channels, frame->format);
2652  if(avctx->pkt_timebase.num && avctx->sample_rate) {
2653  int64_t diff_ts = av_rescale_q(avctx->internal->skip_samples,
2654  (AVRational){1, avctx->sample_rate},
2655  avctx->pkt_timebase);
2656  if(frame->pkt_pts!=AV_NOPTS_VALUE)
2657  frame->pkt_pts += diff_ts;
2658  if(frame->pkt_dts!=AV_NOPTS_VALUE)
2659  frame->pkt_dts += diff_ts;
2660  if (av_frame_get_pkt_duration(frame) >= diff_ts)
2661  av_frame_set_pkt_duration(frame, av_frame_get_pkt_duration(frame) - diff_ts);
2662  } else {
2663  av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for skipped samples.\n");
2664  }
2665  av_log(avctx, AV_LOG_DEBUG, "skip %d/%d samples\n",
2666  avctx->internal->skip_samples, frame->nb_samples);
2667  frame->nb_samples -= avctx->internal->skip_samples;
2668  avctx->internal->skip_samples = 0;
2669  }
2670  }
2671 
2672  if (discard_padding > 0 && discard_padding <= frame->nb_samples && *got_frame_ptr &&
2673  !(avctx->flags2 & AV_CODEC_FLAG2_SKIP_MANUAL)) {
2674  if (discard_padding == frame->nb_samples) {
2675  *got_frame_ptr = 0;
2676  } else {
2677  if(avctx->pkt_timebase.num && avctx->sample_rate) {
2678  int64_t diff_ts = av_rescale_q(frame->nb_samples - discard_padding,
2679  (AVRational){1, avctx->sample_rate},
2680  avctx->pkt_timebase);
2681  if (av_frame_get_pkt_duration(frame) >= diff_ts)
2682  av_frame_set_pkt_duration(frame, av_frame_get_pkt_duration(frame) - diff_ts);
2683  } else {
2684  av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for discarded samples.\n");
2685  }
2686  av_log(avctx, AV_LOG_DEBUG, "discard %d/%d samples\n",
2687  (int)discard_padding, frame->nb_samples);
2688  frame->nb_samples -= discard_padding;
2689  }
2690  }
2691 
2692  if ((avctx->flags2 & AV_CODEC_FLAG2_SKIP_MANUAL) && *got_frame_ptr) {
2694  if (fside) {
2695  AV_WL32(fside->data, avctx->internal->skip_samples);
2696  AV_WL32(fside->data + 4, discard_padding);
2697  AV_WL8(fside->data + 8, skip_reason);
2698  AV_WL8(fside->data + 9, discard_reason);
2699  avctx->internal->skip_samples = 0;
2700  }
2701  }
2702 fail:
2703  avctx->internal->pkt = NULL;
2704  if (did_split) {
2706  if(ret == tmp.size)
2707  ret = avpkt->size;
2708  }
2709 
2710  if (ret >= 0 && *got_frame_ptr) {
2711  if (!avctx->refcounted_frames) {
2712  int err = unrefcount_frame(avci, frame);
2713  if (err < 0)
2714  return err;
2715  }
2716  } else
2717  av_frame_unref(frame);
2718  }
2719 
2720  return ret;
2721 }
2722 
2723 #define UTF8_MAX_BYTES 4 /* 5 and 6 bytes sequences should not be used */
2725  AVPacket *outpkt, const AVPacket *inpkt)
2726 {
2727 #if CONFIG_ICONV
2728  iconv_t cd = (iconv_t)-1;
2729  int ret = 0;
2730  char *inb, *outb;
2731  size_t inl, outl;
2732  AVPacket tmp;
2733 #endif
2734 
2735  if (avctx->sub_charenc_mode != FF_SUB_CHARENC_MODE_PRE_DECODER || inpkt->size == 0)
2736  return 0;
2737 
2738 #if CONFIG_ICONV
2739  cd = iconv_open("UTF-8", avctx->sub_charenc);
2740  av_assert0(cd != (iconv_t)-1);
2741 
2742  inb = inpkt->data;
2743  inl = inpkt->size;
2744 
2745  if (inl >= INT_MAX / UTF8_MAX_BYTES - AV_INPUT_BUFFER_PADDING_SIZE) {
2746  av_log(avctx, AV_LOG_ERROR, "Subtitles packet is too big for recoding\n");
2747  ret = AVERROR(ENOMEM);
2748  goto end;
2749  }
2750 
2751  ret = av_new_packet(&tmp, inl * UTF8_MAX_BYTES);
2752  if (ret < 0)
2753  goto end;
2754  outpkt->buf = tmp.buf;
2755  outpkt->data = tmp.data;
2756  outpkt->size = tmp.size;
2757  outb = outpkt->data;
2758  outl = outpkt->size;
2759 
2760  if (iconv(cd, &inb, &inl, &outb, &outl) == (size_t)-1 ||
2761  iconv(cd, NULL, NULL, &outb, &outl) == (size_t)-1 ||
2762  outl >= outpkt->size || inl != 0) {
2763  ret = FFMIN(AVERROR(errno), -1);
2764  av_log(avctx, AV_LOG_ERROR, "Unable to recode subtitle event \"%s\" "
2765  "from %s to UTF-8\n", inpkt->data, avctx->sub_charenc);
2766  av_free_packet(&tmp);
2767  goto end;
2768  }
2769  outpkt->size -= outl;
2770  memset(outpkt->data + outpkt->size, 0, outl);
2771 
2772 end:
2773  if (cd != (iconv_t)-1)
2774  iconv_close(cd);
2775  return ret;
2776 #else
2777  av_log(avctx, AV_LOG_ERROR, "requesting subtitles recoding without iconv");
2778  return AVERROR(EINVAL);
2779 #endif
2780 }
2781 
2782 static int utf8_check(const uint8_t *str)
2783 {
2784  const uint8_t *byte;
2785  uint32_t codepoint, min;
2786 
2787  while (*str) {
2788  byte = str;
2789  GET_UTF8(codepoint, *(byte++), return 0;);
2790  min = byte - str == 1 ? 0 : byte - str == 2 ? 0x80 :
2791  1 << (5 * (byte - str) - 4);
2792  if (codepoint < min || codepoint >= 0x110000 ||
2793  codepoint == 0xFFFE /* BOM */ ||
2794  codepoint >= 0xD800 && codepoint <= 0xDFFF /* surrogates */)
2795  return 0;
2796  str = byte;
2797  }
2798  return 1;
2799 }
2800 
2802  int *got_sub_ptr,
2803  AVPacket *avpkt)
2804 {
2805  int i, ret = 0;
2806 
2807  if (!avpkt->data && avpkt->size) {
2808  av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
2809  return AVERROR(EINVAL);
2810  }
2811  if (!avctx->codec)
2812  return AVERROR(EINVAL);
2813  if (avctx->codec->type != AVMEDIA_TYPE_SUBTITLE) {
2814  av_log(avctx, AV_LOG_ERROR, "Invalid media type for subtitles\n");
2815  return AVERROR(EINVAL);
2816  }
2817 
2818  *got_sub_ptr = 0;
2819  get_subtitle_defaults(sub);
2820 
2821  if ((avctx->codec->capabilities & AV_CODEC_CAP_DELAY) || avpkt->size) {
2822  AVPacket pkt_recoded;
2823  AVPacket tmp = *avpkt;
2824  int did_split = av_packet_split_side_data(&tmp);
2825  //apply_param_change(avctx, &tmp);
2826 
2827  if (did_split) {
2828  /* FFMIN() prevents overflow in case the packet wasn't allocated with
2829  * proper padding.
2830  * If the side data is smaller than the buffer padding size, the
2831  * remaining bytes should have already been filled with zeros by the
2832  * original packet allocation anyway. */
2833  memset(tmp.data + tmp.size, 0,
2834  FFMIN(avpkt->size - tmp.size, AV_INPUT_BUFFER_PADDING_SIZE));
2835  }
2836 
2837  pkt_recoded = tmp;
2838  ret = recode_subtitle(avctx, &pkt_recoded, &tmp);
2839  if (ret < 0) {
2840  *got_sub_ptr = 0;
2841  } else {
2842  avctx->internal->pkt = &pkt_recoded;
2843 
2844  if (avctx->pkt_timebase.den && avpkt->pts != AV_NOPTS_VALUE)
2845  sub->pts = av_rescale_q(avpkt->pts,
2846  avctx->pkt_timebase, AV_TIME_BASE_Q);
2847  ret = avctx->codec->decode(avctx, sub, got_sub_ptr, &pkt_recoded);
2848  av_assert1((ret >= 0) >= !!*got_sub_ptr &&
2849  !!*got_sub_ptr >= !!sub->num_rects);
2850 
2851  if (sub->num_rects && !sub->end_display_time && avpkt->duration &&
2852  avctx->pkt_timebase.num) {
2853  AVRational ms = { 1, 1000 };
2854  sub->end_display_time = av_rescale_q(avpkt->duration,
2855  avctx->pkt_timebase, ms);
2856  }
2857 
2858  for (i = 0; i < sub->num_rects; i++) {
2859  if (sub->rects[i]->ass && !utf8_check(sub->rects[i]->ass)) {
2860  av_log(avctx, AV_LOG_ERROR,
2861  "Invalid UTF-8 in decoded subtitles text; "
2862  "maybe missing -sub_charenc option\n");
2863  avsubtitle_free(sub);
2864  return AVERROR_INVALIDDATA;
2865  }
2866  }
2867 
2868  if (tmp.data != pkt_recoded.data) { // did we recode?
2869  /* prevent from destroying side data from original packet */
2870  pkt_recoded.side_data = NULL;
2871  pkt_recoded.side_data_elems = 0;
2872 
2873  av_free_packet(&pkt_recoded);
2874  }
2876  sub->format = 0;
2877  else if (avctx->codec_descriptor->props & AV_CODEC_PROP_TEXT_SUB)
2878  sub->format = 1;
2879  avctx->internal->pkt = NULL;
2880  }
2881 
2882  if (did_split) {
2884  if(ret == tmp.size)
2885  ret = avpkt->size;
2886  }
2887 
2888  if (*got_sub_ptr)
2889  avctx->frame_number++;
2890  }
2891 
2892  return ret;
2893 }
2894 
2896 {
2897  int i;
2898 
2899  for (i = 0; i < sub->num_rects; i++) {
2900  av_freep(&sub->rects[i]->pict.data[0]);
2901  av_freep(&sub->rects[i]->pict.data[1]);
2902  av_freep(&sub->rects[i]->pict.data[2]);
2903  av_freep(&sub->rects[i]->pict.data[3]);
2904  av_freep(&sub->rects[i]->text);
2905  av_freep(&sub->rects[i]->ass);
2906  av_freep(&sub->rects[i]);
2907  }
2908 
2909  av_freep(&sub->rects);
2910 
2911  memset(sub, 0, sizeof(AVSubtitle));
2912 }
2913 
2915 {
2916  if (!avctx)
2917  return 0;
2918 
2919  if (avcodec_is_open(avctx)) {
2920  FramePool *pool = avctx->internal->pool;
2921  int i;
2923  avctx->internal->frame_thread_encoder && avctx->thread_count > 1) {
2925  }
2926  if (HAVE_THREADS && avctx->internal->thread_ctx)
2927  ff_thread_free(avctx);
2928  if (avctx->codec && avctx->codec->close)
2929  avctx->codec->close(avctx);
2930  avctx->internal->byte_buffer_size = 0;
2931  av_freep(&avctx->internal->byte_buffer);
2932  av_frame_free(&avctx->internal->to_free);
2933  for (i = 0; i < FF_ARRAY_ELEMS(pool->pools); i++)
2934  av_buffer_pool_uninit(&pool->pools[i]);
2935  av_freep(&avctx->internal->pool);
2936 
2937  if (avctx->hwaccel && avctx->hwaccel->uninit)
2938  avctx->hwaccel->uninit(avctx);
2940 
2941  av_freep(&avctx->internal);
2942  }
2943 
2944  if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
2945  av_opt_free(avctx->priv_data);
2946  av_opt_free(avctx);
2947  av_freep(&avctx->priv_data);
2948  if (av_codec_is_encoder(avctx->codec)) {
2949  av_freep(&avctx->extradata);
2950 #if FF_API_CODED_FRAME
2952  av_frame_free(&avctx->coded_frame);
2954 #endif
2955  }
2956  avctx->codec = NULL;
2957  avctx->active_thread_type = 0;
2958 
2959  return 0;
2960 }
2961 
2963 {
2964  switch(id){
2965  //This is for future deprecatec codec ids, its empty since
2966  //last major bump but will fill up again over time, please don't remove it
2967 // case AV_CODEC_ID_UTVIDEO_DEPRECATED: return AV_CODEC_ID_UTVIDEO;
2987  default : return id;
2988  }
2989 }
2990 
2991 static AVCodec *find_encdec(enum AVCodecID id, int encoder)
2992 {
2993  AVCodec *p, *experimental = NULL;
2994  p = first_avcodec;
2995  id= remap_deprecated_codec_id(id);
2996  while (p) {
2997  if ((encoder ? av_codec_is_encoder(p) : av_codec_is_decoder(p)) &&
2998  p->id == id) {
2999  if (p->capabilities & AV_CODEC_CAP_EXPERIMENTAL && !experimental) {
3000  experimental = p;
3001  } else
3002  return p;
3003  }
3004  p = p->next;
3005  }
3006  return experimental;
3007 }
3008 
3010 {
3011  return find_encdec(id, 1);
3012 }
3013 
3015 {
3016  AVCodec *p;
3017  if (!name)
3018  return NULL;
3019  p = first_avcodec;
3020  while (p) {
3021  if (av_codec_is_encoder(p) && strcmp(name, p->name) == 0)
3022  return p;
3023  p = p->next;
3024  }
3025  return NULL;
3026 }
3027 
3029 {
3030  return find_encdec(id, 0);
3031 }
3032 
3034 {
3035  AVCodec *p;
3036  if (!name)
3037  return NULL;
3038  p = first_avcodec;
3039  while (p) {
3040  if (av_codec_is_decoder(p) && strcmp(name, p->name) == 0)
3041  return p;
3042  p = p->next;
3043  }
3044  return NULL;
3045 }
3046 
3047 const char *avcodec_get_name(enum AVCodecID id)
3048 {
3049  const AVCodecDescriptor *cd;
3050  AVCodec *codec;
3051 
3052  if (id == AV_CODEC_ID_NONE)
3053  return "none";
3054  cd = avcodec_descriptor_get(id);
3055  if (cd)
3056  return cd->name;
3057  av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
3058  codec = avcodec_find_decoder(id);
3059  if (codec)
3060  return codec->name;
3061  codec = avcodec_find_encoder(id);
3062  if (codec)
3063  return codec->name;
3064  return "unknown_codec";
3065 }
3066 
3067 size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
3068 {
3069  int i, len, ret = 0;
3070 
3071 #define TAG_PRINT(x) \
3072  (((x) >= '0' && (x) <= '9') || \
3073  ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
3074  ((x) == '.' || (x) == ' ' || (x) == '-' || (x) == '_'))
3075 
3076  for (i = 0; i < 4; i++) {
3077  len = snprintf(buf, buf_size,
3078  TAG_PRINT(codec_tag & 0xFF) ? "%c" : "[%d]", codec_tag & 0xFF);
3079  buf += len;
3080  buf_size = buf_size > len ? buf_size - len : 0;
3081  ret += len;
3082  codec_tag >>= 8;
3083  }
3084  return ret;
3085 }
3086 
3087 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
3088 {
3089  const char *codec_type;
3090  const char *codec_name;
3091  const char *profile = NULL;
3092  const AVCodec *p;
3093  int bitrate;
3094  int new_line = 0;
3095  AVRational display_aspect_ratio;
3096  const char *separator = enc->dump_separator ? (const char *)enc->dump_separator : ", ";
3097 
3098  if (!buf || buf_size <= 0)
3099  return;
3100  codec_type = av_get_media_type_string(enc->codec_type);
3101  codec_name = avcodec_get_name(enc->codec_id);
3102  if (enc->profile != FF_PROFILE_UNKNOWN) {
3103  if (enc->codec)
3104  p = enc->codec;
3105  else
3106  p = encode ? avcodec_find_encoder(enc->codec_id) :
3108  if (p)
3109  profile = av_get_profile_name(p, enc->profile);
3110  }
3111 
3112  snprintf(buf, buf_size, "%s: %s", codec_type ? codec_type : "unknown",
3113  codec_name);
3114  buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
3115 
3116  if (enc->codec && strcmp(enc->codec->name, codec_name))
3117  snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", enc->codec->name);
3118 
3119  if (profile)
3120  snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
3121  if ( enc->codec_type == AVMEDIA_TYPE_VIDEO
3123  && enc->refs)
3124  snprintf(buf + strlen(buf), buf_size - strlen(buf),
3125  ", %d reference frame%s",
3126  enc->refs, enc->refs > 1 ? "s" : "");
3127 
3128  if (enc->codec_tag) {
3129  char tag_buf[32];
3130  av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
3131  snprintf(buf + strlen(buf), buf_size - strlen(buf),
3132  " (%s / 0x%04X)", tag_buf, enc->codec_tag);
3133  }
3134 
3135  switch (enc->codec_type) {
3136  case AVMEDIA_TYPE_VIDEO:
3137  {
3138  char detail[256] = "(";
3139 
3140  av_strlcat(buf, separator, buf_size);
3141 
3142  snprintf(buf + strlen(buf), buf_size - strlen(buf),
3143  "%s", enc->pix_fmt == AV_PIX_FMT_NONE ? "none" :
3145  if (enc->bits_per_raw_sample && enc->pix_fmt != AV_PIX_FMT_NONE &&
3147  av_strlcatf(detail, sizeof(detail), "%d bpc, ", enc->bits_per_raw_sample);
3149  av_strlcatf(detail, sizeof(detail), "%s, ",
3151 
3152  if (enc->colorspace != AVCOL_SPC_UNSPECIFIED ||
3154  enc->color_trc != AVCOL_TRC_UNSPECIFIED) {
3155  if (enc->colorspace != (int)enc->color_primaries ||
3156  enc->colorspace != (int)enc->color_trc) {
3157  new_line = 1;
3158  av_strlcatf(detail, sizeof(detail), "%s/%s/%s, ",
3162  } else
3163  av_strlcatf(detail, sizeof(detail), "%s, ",
3165  }
3166 
3167  if (av_log_get_level() >= AV_LOG_DEBUG &&
3169  av_strlcatf(detail, sizeof(detail), "%s, ",
3171 
3172  if (strlen(detail) > 1) {
3173  detail[strlen(detail) - 2] = 0;
3174  av_strlcatf(buf, buf_size, "%s)", detail);
3175  }
3176  }
3177 
3178  if (enc->width) {
3179  av_strlcat(buf, new_line ? separator : ", ", buf_size);
3180 
3181  snprintf(buf + strlen(buf), buf_size - strlen(buf),
3182  "%dx%d",
3183  enc->width, enc->height);
3184 
3185  if (av_log_get_level() >= AV_LOG_VERBOSE &&
3186  (enc->width != enc->coded_width ||
3187  enc->height != enc->coded_height))
3188  snprintf(buf + strlen(buf), buf_size - strlen(buf),
3189  " (%dx%d)", enc->coded_width, enc->coded_height);
3190 
3191  if (enc->sample_aspect_ratio.num) {
3192  av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
3193  enc->width * (int64_t)enc->sample_aspect_ratio.num,
3194  enc->height * (int64_t)enc->sample_aspect_ratio.den,
3195  1024 * 1024);
3196  snprintf(buf + strlen(buf), buf_size - strlen(buf),
3197  " [SAR %d:%d DAR %d:%d]",
3199  display_aspect_ratio.num, display_aspect_ratio.den);
3200  }
3201  if (av_log_get_level() >= AV_LOG_DEBUG) {
3202  int g = av_gcd(enc->time_base.num, enc->time_base.den);
3203  snprintf(buf + strlen(buf), buf_size - strlen(buf),
3204  ", %d/%d",
3205  enc->time_base.num / g, enc->time_base.den / g);
3206  }
3207  }
3208  if (encode) {
3209  snprintf(buf + strlen(buf), buf_size - strlen(buf),
3210  ", q=%d-%d", enc->qmin, enc->qmax);
3211  } else {
3213  snprintf(buf + strlen(buf), buf_size - strlen(buf),
3214  ", Closed Captions");
3216  snprintf(buf + strlen(buf), buf_size - strlen(buf),
3217  ", lossless");
3218  }
3219  break;
3220  case AVMEDIA_TYPE_AUDIO:
3221  av_strlcat(buf, separator, buf_size);
3222 
3223  if (enc->sample_rate) {
3224  snprintf(buf + strlen(buf), buf_size - strlen(buf),
3225  "%d Hz, ", enc->sample_rate);
3226  }
3227  av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
3228  if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
3229  snprintf(buf + strlen(buf), buf_size - strlen(buf),
3230  ", %s", av_get_sample_fmt_name(enc->sample_fmt));
3231  }
3232  if ( enc->bits_per_raw_sample > 0
3234  snprintf(buf + strlen(buf), buf_size - strlen(buf),
3235  " (%d bit)", enc->bits_per_raw_sample);
3236  break;
3237  case AVMEDIA_TYPE_DATA:
3238  if (av_log_get_level() >= AV_LOG_DEBUG) {
3239  int g = av_gcd(enc->time_base.num, enc->time_base.den);
3240  if (g)
3241  snprintf(buf + strlen(buf), buf_size - strlen(buf),
3242  ", %d/%d",
3243  enc->time_base.num / g, enc->time_base.den / g);
3244  }
3245  break;
3246  case AVMEDIA_TYPE_SUBTITLE:
3247  if (enc->width)
3248  snprintf(buf + strlen(buf), buf_size - strlen(buf),
3249  ", %dx%d", enc->width, enc->height);
3250  break;
3251  default:
3252  return;
3253  }
3254  if (encode) {
3255  if (enc->flags & AV_CODEC_FLAG_PASS1)
3256  snprintf(buf + strlen(buf), buf_size - strlen(buf),
3257  ", pass 1");
3258  if (enc->flags & AV_CODEC_FLAG_PASS2)
3259  snprintf(buf + strlen(buf), buf_size - strlen(buf),
3260  ", pass 2");
3261  }
3262  bitrate = get_bit_rate(enc);
3263  if (bitrate != 0) {
3264  snprintf(buf + strlen(buf), buf_size - strlen(buf),
3265  ", %d kb/s", bitrate / 1000);
3266  } else if (enc->rc_max_rate > 0) {
3267  snprintf(buf + strlen(buf), buf_size - strlen(buf),
3268  ", max. %d kb/s", enc->rc_max_rate / 1000);
3269  }
3270 }
3271 
3272 const char *av_get_profile_name(const AVCodec *codec, int profile)
3273 {
3274  const AVProfile *p;
3275  if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
3276  return NULL;
3277 
3278  for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
3279  if (p->profile == profile)
3280  return p->name;
3281 
3282  return NULL;
3283 }
3284 
3285 unsigned avcodec_version(void)
3286 {
3287 // av_assert0(AV_CODEC_ID_V410==164);
3290 // av_assert0(AV_CODEC_ID_BMV_AUDIO==86071);
3291  av_assert0(AV_CODEC_ID_SRT==94216);
3293 
3294 #if FF_API_CODEC_ID
3300 #endif
3301  return LIBAVCODEC_VERSION_INT;
3302 }
3303 
3304 const char *avcodec_configuration(void)
3305 {
3306  return FFMPEG_CONFIGURATION;
3307 }
3308 
3309 const char *avcodec_license(void)
3310 {
3311 #define LICENSE_PREFIX "libavcodec license: "
3312  return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
3313 }
3314 
3316 {
3318  ff_thread_flush(avctx);
3319  else if (avctx->codec->flush)
3320  avctx->codec->flush(avctx);
3321 
3322  avctx->pts_correction_last_pts =
3323  avctx->pts_correction_last_dts = INT64_MIN;
3324 
3325  if (!avctx->refcounted_frames)
3326  av_frame_unref(avctx->internal->to_free);
3327 }
3328 
3330 {
3331  switch (codec_id) {
3332  case AV_CODEC_ID_8SVX_EXP:
3333  case AV_CODEC_ID_8SVX_FIB:
3334  case AV_CODEC_ID_ADPCM_CT:
3341  return 4;
3342  case AV_CODEC_ID_DSD_LSBF:
3343  case AV_CODEC_ID_DSD_MSBF:
3346  case AV_CODEC_ID_PCM_ALAW:
3347  case AV_CODEC_ID_PCM_MULAW:
3348  case AV_CODEC_ID_PCM_S8:
3350  case AV_CODEC_ID_PCM_U8:
3351  case AV_CODEC_ID_PCM_ZORK:
3352  return 8;
3353  case AV_CODEC_ID_PCM_S16BE:
3355  case AV_CODEC_ID_PCM_S16LE:
3357  case AV_CODEC_ID_PCM_U16BE:
3358  case AV_CODEC_ID_PCM_U16LE:
3359  return 16;
3361  case AV_CODEC_ID_PCM_S24BE:
3362  case AV_CODEC_ID_PCM_S24LE:
3364  case AV_CODEC_ID_PCM_U24BE:
3365  case AV_CODEC_ID_PCM_U24LE:
3366  return 24;
3367  case AV_CODEC_ID_PCM_S32BE:
3368  case AV_CODEC_ID_PCM_S32LE:
3370  case AV_CODEC_ID_PCM_U32BE:
3371  case AV_CODEC_ID_PCM_U32LE:
3372  case AV_CODEC_ID_PCM_F32BE:
3373  case AV_CODEC_ID_PCM_F32LE:
3374  return 32;
3375  case AV_CODEC_ID_PCM_F64BE:
3376  case AV_CODEC_ID_PCM_F64LE:
3377  return 64;
3378  default:
3379  return 0;
3380  }
3381 }
3382 
3384 {
3385  static const enum AVCodecID map[AV_SAMPLE_FMT_NB][2] = {
3396  };
3397  if (fmt < 0 || fmt >= AV_SAMPLE_FMT_NB)
3398  return AV_CODEC_ID_NONE;
3399  if (be < 0 || be > 1)
3400  be = AV_NE(1, 0);
3401  return map[fmt][be];
3402 }
3403 
3405 {
3406  switch (codec_id) {
3408  return 2;
3410  return 3;
3414  case AV_CODEC_ID_ADPCM_SWF:
3415  case AV_CODEC_ID_ADPCM_MS:
3416  return 4;
3417  default:
3418  return av_get_exact_bits_per_sample(codec_id);
3419  }
3420 }
3421 
3422 int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
3423 {
3424  int id, sr, ch, ba, tag, bps;
3425 
3426  id = avctx->codec_id;
3427  sr = avctx->sample_rate;
3428  ch = avctx->channels;
3429  ba = avctx->block_align;
3430  tag = avctx->codec_tag;
3431  bps = av_get_exact_bits_per_sample(avctx->codec_id);
3432 
3433  /* codecs with an exact constant bits per sample */
3434  if (bps > 0 && ch > 0 && frame_bytes > 0 && ch < 32768 && bps < 32768)
3435  return (frame_bytes * 8LL) / (bps * ch);
3436  bps = avctx->bits_per_coded_sample;
3437 
3438  /* codecs with a fixed packet duration */
3439  switch (id) {
3440  case AV_CODEC_ID_ADPCM_ADX: return 32;
3441  case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
3442  case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
3443  case AV_CODEC_ID_AMR_NB:
3444  case AV_CODEC_ID_EVRC:
3445  case AV_CODEC_ID_GSM:
3446  case AV_CODEC_ID_QCELP:
3447  case AV_CODEC_ID_RA_288: return 160;
3448  case AV_CODEC_ID_AMR_WB:
3449  case AV_CODEC_ID_GSM_MS: return 320;
3450  case AV_CODEC_ID_MP1: return 384;
3451  case AV_CODEC_ID_ATRAC1: return 512;
3452  case AV_CODEC_ID_ATRAC3: return 1024;
3453  case AV_CODEC_ID_ATRAC3P: return 2048;
3454  case AV_CODEC_ID_MP2:
3455  case AV_CODEC_ID_MUSEPACK7: return 1152;
3456  case AV_CODEC_ID_AC3: return 1536;
3457  }
3458 
3459  if (sr > 0) {
3460  /* calc from sample rate */
3461  if (id == AV_CODEC_ID_TTA)
3462  return 256 * sr / 245;
3463 
3464  if (ch > 0) {
3465  /* calc from sample rate and channels */
3466  if (id == AV_CODEC_ID_BINKAUDIO_DCT)
3467  return (480 << (sr / 22050)) / ch;
3468  }
3469  }
3470 
3471  if (ba > 0) {
3472  /* calc from block_align */
3473  if (id == AV_CODEC_ID_SIPR) {
3474  switch (ba) {
3475  case 20: return 160;
3476  case 19: return 144;
3477  case 29: return 288;
3478  case 37: return 480;
3479  }
3480  } else if (id == AV_CODEC_ID_ILBC) {
3481  switch (ba) {
3482  case 38: return 160;
3483  case 50: return 240;
3484  }
3485  }
3486  }
3487 
3488  if (frame_bytes > 0) {
3489  /* calc from frame_bytes only */
3490  if (id == AV_CODEC_ID_TRUESPEECH)
3491  return 240 * (frame_bytes / 32);
3492  if (id == AV_CODEC_ID_NELLYMOSER)
3493  return 256 * (frame_bytes / 64);
3494  if (id == AV_CODEC_ID_RA_144)
3495  return 160 * (frame_bytes / 20);
3496  if (id == AV_CODEC_ID_G723_1)
3497  return 240 * (frame_bytes / 24);
3498 
3499  if (bps > 0) {
3500  /* calc from frame_bytes and bits_per_coded_sample */
3501  if (id == AV_CODEC_ID_ADPCM_G726)
3502  return frame_bytes * 8 / bps;
3503  }
3504 
3505  if (ch > 0 && ch < INT_MAX/16) {
3506  /* calc from frame_bytes and channels */
3507  switch (id) {
3508  case AV_CODEC_ID_ADPCM_AFC:
3509  return frame_bytes / (9 * ch) * 16;
3510  case AV_CODEC_ID_ADPCM_DTK:
3511  return frame_bytes / (16 * ch) * 28;
3512  case AV_CODEC_ID_ADPCM_4XM:
3514  return (frame_bytes - 4 * ch) * 2 / ch;
3516  return (frame_bytes - 4) * 2 / ch;
3518  return (frame_bytes - 8) * 2 / ch;
3519  case AV_CODEC_ID_ADPCM_THP:
3521  if (avctx->extradata)
3522  return frame_bytes * 14 / (8 * ch);
3523  break;
3524  case AV_CODEC_ID_ADPCM_XA:
3525  return (frame_bytes / 128) * 224 / ch;
3527  return (frame_bytes - 6 - ch) / ch;
3528  case AV_CODEC_ID_ROQ_DPCM:
3529  return (frame_bytes - 8) / ch;
3530  case AV_CODEC_ID_XAN_DPCM:
3531  return (frame_bytes - 2 * ch) / ch;
3532  case AV_CODEC_ID_MACE3:
3533  return 3 * frame_bytes / ch;
3534  case AV_CODEC_ID_MACE6:
3535  return 6 * frame_bytes / ch;
3536  case AV_CODEC_ID_PCM_LXF:
3537  return 2 * (frame_bytes / (5 * ch));
3538  case AV_CODEC_ID_IAC:
3539  case AV_CODEC_ID_IMC:
3540  return 4 * frame_bytes / ch;
3541  }
3542 
3543  if (tag) {
3544  /* calc from frame_bytes, channels, and codec_tag */
3545  if (id == AV_CODEC_ID_SOL_DPCM) {
3546  if (tag == 3)
3547  return frame_bytes / ch;
3548  else
3549  return frame_bytes * 2 / ch;
3550  }
3551  }
3552 
3553  if (ba > 0) {
3554  /* calc from frame_bytes, channels, and block_align */
3555  int blocks = frame_bytes / ba;
3556  switch (avctx->codec_id) {
3558  if (bps < 2 || bps > 5)
3559  return 0;
3560  return blocks * (1 + (ba - 4 * ch) / (bps * ch) * 8);
3562  return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
3564  return blocks * (1 + (ba - 4 * ch) * 2 / ch);
3566  return blocks * ((ba - 4 * ch) * 2 / ch);
3567  case AV_CODEC_ID_ADPCM_MS:
3568  return blocks * (2 + (ba - 7 * ch) * 2 / ch);
3569  }
3570  }
3571 
3572  if (bps > 0) {
3573  /* calc from frame_bytes, channels, and bits_per_coded_sample */
3574  switch (avctx->codec_id) {
3575  case AV_CODEC_ID_PCM_DVD:
3576  if(bps<4)
3577  return 0;
3578  return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
3580  if(bps<4)
3581  return 0;
3582  return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
3583  case AV_CODEC_ID_S302M:
3584  return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
3585  }
3586  }
3587  }
3588  }
3589 
3590  /* Fall back on using frame_size */
3591  if (avctx->frame_size > 1 && frame_bytes)
3592  return avctx->frame_size;
3593 
3594  //For WMA we currently have no other means to calculate duration thus we
3595  //do it here by assuming CBR, which is true for all known cases.
3596  if (avctx->bit_rate>0 && frame_bytes>0 && avctx->sample_rate>0 && avctx->block_align>1) {
3597  if (avctx->codec_id == AV_CODEC_ID_WMAV1 || avctx->codec_id == AV_CODEC_ID_WMAV2)
3598  return (frame_bytes * 8LL * avctx->sample_rate) / avctx->bit_rate;
3599  }
3600 
3601  return 0;
3602 }
3603 
3604 #if !HAVE_THREADS
3606 {
3607  return -1;
3608 }
3609 
3610 #endif
3611 
3612 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
3613 {
3614  unsigned int n = 0;
3615 
3616  while (v >= 0xff) {
3617  *s++ = 0xff;
3618  v -= 0xff;
3619  n++;
3620  }
3621  *s = v;
3622  n++;
3623  return n;
3624 }
3625 
3626 int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
3627 {
3628  int i;
3629  for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
3630  return i;
3631 }
3632 
3633 #if FF_API_MISSING_SAMPLE
3635 void av_log_missing_feature(void *avc, const char *feature, int want_sample)
3636 {
3637  av_log(avc, AV_LOG_WARNING, "%s is not implemented. Update your FFmpeg "
3638  "version to the newest one from Git. If the problem still "
3639  "occurs, it means that your file has a feature which has not "
3640  "been implemented.\n", feature);
3641  if(want_sample)
3643 }
3644 
3645 void av_log_ask_for_sample(void *avc, const char *msg, ...)
3646 {
3647  va_list argument_list;
3648 
3649  va_start(argument_list, msg);
3650 
3651  if (msg)
3652  av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
3653  av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
3654  "of this file to ftp://upload.ffmpeg.org/incoming/ "
3655  "and contact the ffmpeg-devel mailing list. (ffmpeg-devel@ffmpeg.org)\n");
3656 
3657  va_end(argument_list);
3658 }
3660 #endif /* FF_API_MISSING_SAMPLE */
3661 
3664 
3666 {
3667  AVHWAccel **p = last_hwaccel;
3668  hwaccel->next = NULL;
3669  while(*p || avpriv_atomic_ptr_cas((void * volatile *)p, NULL, hwaccel))
3670  p = &(*p)->next;
3671  last_hwaccel = &hwaccel->next;
3672 }
3673 
3675 {
3676  return hwaccel ? hwaccel->next : first_hwaccel;
3677 }
3678 
3679 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
3680 {
3681  if (lockmgr_cb) {
3682  // There is no good way to rollback a failure to destroy the
3683  // mutex, so we ignore failures.
3686  lockmgr_cb = NULL;
3687  codec_mutex = NULL;
3688  avformat_mutex = NULL;
3689  }
3690 
3691  if (cb) {
3692  void *new_codec_mutex = NULL;
3693  void *new_avformat_mutex = NULL;
3694  int err;
3695  if (err = cb(&new_codec_mutex, AV_LOCK_CREATE)) {
3696  return err > 0 ? AVERROR_UNKNOWN : err;
3697  }
3698  if (err = cb(&new_avformat_mutex, AV_LOCK_CREATE)) {
3699  // Ignore failures to destroy the newly created mutex.
3700  cb(&new_codec_mutex, AV_LOCK_DESTROY);
3701  return err > 0 ? AVERROR_UNKNOWN : err;
3702  }
3703  lockmgr_cb = cb;
3704  codec_mutex = new_codec_mutex;
3705  avformat_mutex = new_avformat_mutex;
3706  }
3707 
3708  return 0;
3709 }
3710 
3711 int ff_lock_avcodec(AVCodecContext *log_ctx, const AVCodec *codec)
3712 {
3713  if (lockmgr_cb) {
3715  return -1;
3716  }
3717 
3720  av_log(log_ctx, AV_LOG_ERROR,
3721  "Insufficient thread locking. At least %d threads are "
3722  "calling avcodec_open2() at the same time right now.\n",
3724  if (!lockmgr_cb)
3725  av_log(log_ctx, AV_LOG_ERROR, "No lock manager is set, please see av_lockmgr_register()\n");
3726  ff_avcodec_locked = 1;
3728  return AVERROR(EINVAL);
3729  }
3731  ff_avcodec_locked = 1;
3732  return 0;
3733 }
3734 
3736 {
3738  ff_avcodec_locked = 0;
3740  if (lockmgr_cb) {
3742  return -1;
3743  }
3744 
3745  return 0;
3746 }
3747 
3749 {
3750  if (lockmgr_cb) {
3752  return -1;
3753  }
3754  return 0;
3755 }
3756 
3758 {
3759  if (lockmgr_cb) {
3761  return -1;
3762  }
3763  return 0;
3764 }
3765 
3766 unsigned int avpriv_toupper4(unsigned int x)
3767 {
3768  return av_toupper(x & 0xFF) +
3769  (av_toupper((x >> 8) & 0xFF) << 8) +
3770  (av_toupper((x >> 16) & 0xFF) << 16) +
3771 ((unsigned)av_toupper((x >> 24) & 0xFF) << 24);
3772 }
3773 
3775 {
3776  int ret;
3777 
3778  dst->owner = src->owner;
3779 
3780  ret = av_frame_ref(dst->f, src->f);
3781  if (ret < 0)
3782  return ret;
3783 
3784  av_assert0(!dst->progress);
3785 
3786  if (src->progress &&
3787  !(dst->progress = av_buffer_ref(src->progress))) {
3788  ff_thread_release_buffer(dst->owner, dst);
3789  return AVERROR(ENOMEM);
3790  }
3791 
3792  return 0;
3793 }
3794 
3795 #if !HAVE_THREADS
3796 
3798 {
3799  return ff_get_format(avctx, fmt);
3800 }
3801 
3803 {
3804  f->owner = avctx;
3805  return ff_get_buffer(avctx, f->f, flags);
3806 }
3807 
3809 {
3810  if (f->f)
3811  av_frame_unref(f->f);
3812 }
3813 
3815 {
3816 }
3817 
3818 void ff_thread_report_progress(ThreadFrame *f, int progress, int field)
3819 {
3820 }
3821 
3822 void ff_thread_await_progress(ThreadFrame *f, int progress, int field)
3823 {
3824 }
3825 
3827 {
3828  return 1;
3829 }
3830 
3832 {
3833  return 0;
3834 }
3835 
3837 {
3838 }
3839 
3840 void ff_thread_await_progress2(AVCodecContext *avctx, int field, int thread, int shift)
3841 {
3842 }
3843 
3844 void ff_thread_report_progress2(AVCodecContext *avctx, int field, int thread, int n)
3845 {
3846 }
3847 
3848 #endif
3849 
3851 {
3852  AVCodec *c= avcodec_find_decoder(codec_id);
3853  if(!c)
3854  c= avcodec_find_encoder(codec_id);
3855  if(c)
3856  return c->type;
3857 
3858  if (codec_id <= AV_CODEC_ID_NONE)
3859  return AVMEDIA_TYPE_UNKNOWN;
3860  else if (codec_id < AV_CODEC_ID_FIRST_AUDIO)
3861  return AVMEDIA_TYPE_VIDEO;
3862  else if (codec_id < AV_CODEC_ID_FIRST_SUBTITLE)
3863  return AVMEDIA_TYPE_AUDIO;
3864  else if (codec_id < AV_CODEC_ID_FIRST_UNKNOWN)
3865  return AVMEDIA_TYPE_SUBTITLE;
3866 
3867  return AVMEDIA_TYPE_UNKNOWN;
3868 }
3869 
3871 {
3872  return !!s->internal;
3873 }
3874 
3875 int avpriv_bprint_to_extradata(AVCodecContext *avctx, struct AVBPrint *buf)
3876 {
3877  int ret;
3878  char *str;
3879 
3880  ret = av_bprint_finalize(buf, &str);
3881  if (ret < 0)
3882  return ret;
3883  if (!av_bprint_is_complete(buf)) {
3884  av_free(str);
3885  return AVERROR(ENOMEM);
3886  }
3887 
3888  avctx->extradata = str;
3889  /* Note: the string is NUL terminated (so extradata can be read as a
3890  * string), but the ending character is not accounted in the size (in
3891  * binary formats you are likely not supposed to mux that character). When
3892  * extradata is copied, it is also padded with AV_INPUT_BUFFER_PADDING_SIZE
3893  * zeros. */
3894  avctx->extradata_size = buf->len;
3895  return 0;
3896 }
3897 
3899  const uint8_t *end,
3900  uint32_t *av_restrict state)
3901 {
3902  int i;
3903 
3904  av_assert0(p <= end);
3905  if (p >= end)
3906  return end;
3907 
3908  for (i = 0; i < 3; i++) {
3909  uint32_t tmp = *state << 8;
3910  *state = tmp + *(p++);
3911  if (tmp == 0x100 || p == end)
3912  return p;
3913  }
3914 
3915  while (p < end) {
3916  if (p[-1] > 1 ) p += 3;
3917  else if (p[-2] ) p += 2;
3918  else if (p[-3]|(p[-1]-1)) p++;
3919  else {
3920  p++;
3921  break;
3922  }
3923  }
3924 
3925  p = FFMIN(p, end) - 4;
3926  *state = AV_RB32(p);
3927 
3928  return p + 4;
3929 }
#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:2306
packed YUV 4:2:2, 16bpp, Cb Y0 Cr Y1
Definition: pixfmt.h:83
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:2723
#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:178
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:2991
planar YUV 4:4:0,20bpp, (1 Cr & Cb sample per 1x2 Y samples), little-endian
Definition: pixfmt.h:319
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:293
#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:286
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:3009
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:290
planar YUV 4:2:0, 15bpp, (1 Cr & Cb sample per 2x2 Y samples), little-endian
Definition: pixfmt.h:171
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:3679
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:68
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:65
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:174
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:291
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:3304
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:188
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:3309
#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:1858
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:215
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:296
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:168
#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:3067
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:1895
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:3797
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:2801
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:295
AVCodec.
Definition: avcodec.h:3482
planar YUV 4:2:0, 24bpp, (1 Cr & Cb sample per 2x2 Y samples), little-endian
Definition: pixfmt.h:140
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:3635
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:3840
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:205
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:216
int ff_unlock_avcodec(void)
Definition: utils.c:3735
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:103
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:74
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:292
#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:2172
static AVFrame * frame
planar YUV 4:2:0 40bpp, (1 Cr & Cb sample per 2x2 Y & A samples, little-endian)
Definition: pixfmt.h:217
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:320
uint8_t * data
Definition: avcodec.h:1433
planar GBR 4:4:4 48bpp, big-endian
Definition: pixfmt.h:193
planar YUV 4:4:0 full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV440P and setting color_range...
Definition: pixfmt.h:102
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:76
planar YUV 4:4:4 64bpp, (1 Cr & Cb sample per 1x1 Y & A samples, big-endian)
Definition: pixfmt.h:220
#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:3626
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:322
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:1794
#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:3014
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:169
#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:2914
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:189
planar YUV 4:4:4, 30bpp, (1 Cr & Cb sample per 1x1 Y samples), little-endian
Definition: pixfmt.h:177
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:281
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:3404
#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:2420
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:145
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:3766
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:3870
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:436
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:3605
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:208
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:173
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:289
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:3850
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:143
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:209
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:210
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:67
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:2514
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:75
int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
Return codec bits per sample.
Definition: utils.c:3329
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:3844
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:214
packed YUV 4:2:2, 16bpp, Y0 Cr Y1 Cb
Definition: pixfmt.h:242
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:3814
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:2570
#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:206
#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:66
planar YUV 4:2:0,18bpp, (1 Cr & Cb sample per 2x2 Y samples), little-endian
Definition: pixfmt.h:284
planar YUV 4:2:0, 15bpp, (1 Cr & Cb sample per 2x2 Y samples), big-endian
Definition: pixfmt.h:170
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:3645
Opaque data information usually sparse.
Definition: avutil.h:197
int ff_alloc_packet(AVPacket *avpkt, int size)
Definition: utils.c:1850
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:179
#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:3047
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:540
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:3315
#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:3272
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:85
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:141
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:3383
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:280
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:191
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:3028
static int recode_subtitle(AVCodecContext *avctx, AVPacket *outpkt, const AVPacket *inpkt)
Definition: utils.c:2724
planar YUV 4:2:2 48bpp, (1 Cr & Cb sample per 2x1 Y & A samples, big-endian)
Definition: pixfmt.h:218
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:2895
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:64
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:321
#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:298
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:2131
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:3612
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:69
planar YUV 4:2:2 48bpp, (1 Cr & Cb sample per 2x1 Y & A samples, little-endian)
Definition: pixfmt.h:219
static int utf8_check(const uint8_t *str)
Definition: utils.c:2782
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:221
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:99
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:3808
#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:285
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:297
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:3662
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:204
#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:3663
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:3422
static enum AVCodecID remap_deprecated_codec_id(enum AVCodecID id)
Definition: utils.c:2962
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:3674
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:3875
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:213
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:2042
AVCodec * avcodec_find_decoder_by_name(const char *name)
Find a registered decoder with the specified name.
Definition: utils.c:3033
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:175
planar YUV 4:4:4, 48bpp, (1 Cr & Cb sample per 1x1 Y samples), little-endian
Definition: pixfmt.h:144
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:63
planar YUV 4:2:2,24bpp, (1 Cr & Cb sample per 2x1 Y samples), little-endian
Definition: pixfmt.h:288
Y , 8bpp.
Definition: pixfmt.h:71
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:3748
#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:3665
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:299
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:2280
signed 16 bits
Definition: samplefmt.h:62
planar GBR 4:4:4 27bpp, little-endian
Definition: pixfmt.h:190
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:142
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV444P and setting col...
Definition: pixfmt.h:77
int ff_thread_can_start_frame(AVCodecContext *avctx)
Definition: utils.c:3826
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:3818
packed RGB 3:3:2, 8bpp, (msb)2R 3G 3B(lsb)
Definition: pixfmt.h:88
static const uint64_t c2
Definition: murmur3.c:50
#define AV_PIX_FMT_RGB555
Definition: pixfmt.h:368
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition: pixfmt.h:70
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:211
#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:3087
#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:3711
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:294
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:3285
Y , 16bpp, little-endian.
Definition: pixfmt.h:100
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:302
Not part of ABI.
Definition: pixfmt.h:567
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:3822
planar YUV 4:2:2 30bpp, (1 Cr & Cb sample per 2x1 Y & A samples, big-endian)
Definition: pixfmt.h:212
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:101
signed 16 bits, planar
Definition: samplefmt.h:68
static int unrefcount_frame(AVCodecInternal *avci, AVFrame *frame)
Definition: utils.c:2374
AVChromaLocation
Location of chroma samples.
Definition: pixfmt.h:559
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:3774
planar GBR 4:4:4 48bpp, little-endian
Definition: pixfmt.h:194
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:3802
int ff_alloc_entries(AVCodecContext *avctx, int count)
Definition: utils.c:3831
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:207
int avpriv_unlock_avformat(void)
Definition: utils.c:3757
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:3898
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:3836
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:84
float min
Stereoscopic 3d metadata.
Definition: frame.h:63
AVCodecContext avctx
Definition: utils.c:862
AVPixelFormat
Pixel format.
Definition: pixfmt.h:61
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:2256
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:283
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:176
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:287
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:192
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:172