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