FFmpeg  1.2.12
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 
28 #include "config.h"
29 #include "libavutil/avassert.h"
30 #include "libavutil/avstring.h"
31 #include "libavutil/bprint.h"
32 #include "libavutil/channel_layout.h"
33 #include "libavutil/crc.h"
34 #include "libavutil/mathematics.h"
35 #include "libavutil/pixdesc.h"
36 #include "libavutil/imgutils.h"
37 #include "libavutil/samplefmt.h"
38 #include "libavutil/dict.h"
39 #include "libavutil/avassert.h"
40 #include "avcodec.h"
41 #include "dsputil.h"
42 #include "libavutil/opt.h"
43 #include "thread.h"
44 #include "frame_thread_encoder.h"
45 #include "internal.h"
46 #include "bytestream.h"
47 #include <stdlib.h>
48 #include <stdarg.h>
49 #include <limits.h>
50 #include <float.h>
51 #if CONFIG_ICONV
52 # include <iconv.h>
53 #endif
54 
55 volatile int ff_avcodec_locked;
56 static int volatile entangled_thread_counter = 0;
57 static int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
58 static void *codec_mutex;
59 static void *avformat_mutex;
60 
61 void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
62 {
63  if (min_size < *size)
64  return ptr;
65 
66  min_size = FFMAX(17 * min_size / 16 + 32, min_size);
67 
68  ptr = av_realloc(ptr, min_size);
69  /* we could set this to the unmodified min_size but this is safer
70  * if the user lost the ptr and uses NULL now
71  */
72  if (!ptr)
73  min_size = 0;
74 
75  *size = min_size;
76 
77  return ptr;
78 }
79 
80 static inline int ff_fast_malloc(void *ptr, unsigned int *size, size_t min_size, int zero_realloc)
81 {
82  void **p = ptr;
83  if (min_size < *size)
84  return 0;
85  min_size = FFMAX(17 * min_size / 16 + 32, min_size);
86  av_free(*p);
87  *p = zero_realloc ? av_mallocz(min_size) : av_malloc(min_size);
88  if (!*p)
89  min_size = 0;
90  *size = min_size;
91  return 1;
92 }
93 
94 void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
95 {
96  ff_fast_malloc(ptr, size, min_size, 0);
97 }
98 
99 void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
100 {
101  uint8_t **p = ptr;
102  if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
103  av_freep(p);
104  *size = 0;
105  return;
106  }
107  if (!ff_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE, 1))
108  memset(*p + min_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
109 }
110 
111 void av_fast_padded_mallocz(void *ptr, unsigned int *size, size_t min_size)
112 {
113  uint8_t **p = ptr;
114  if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
115  av_freep(p);
116  *size = 0;
117  return;
118  }
119  if (!ff_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE, 1))
120  memset(*p, 0, min_size + FF_INPUT_BUFFER_PADDING_SIZE);
121 }
122 
123 /* encoder management */
125 
127 {
128  if (c)
129  return c->next;
130  else
131  return first_avcodec;
132 }
133 
134 static void avcodec_init(void)
135 {
136  static int initialized = 0;
137 
138  if (initialized != 0)
139  return;
140  initialized = 1;
141 
143 }
144 
145 int av_codec_is_encoder(const AVCodec *codec)
146 {
147  return codec && (codec->encode_sub || codec->encode2);
148 }
149 
150 int av_codec_is_decoder(const AVCodec *codec)
151 {
152  return codec && codec->decode;
153 }
154 
156 {
157  AVCodec **p;
158  avcodec_init();
159  p = &first_avcodec;
160  while (*p != NULL)
161  p = &(*p)->next;
162  *p = codec;
163  codec->next = NULL;
164 
165  if (codec->init_static_data)
166  codec->init_static_data(codec);
167 }
168 
170 {
171  return EDGE_WIDTH;
172 }
173 
175 {
176  s->coded_width = width;
177  s->coded_height = height;
178  s->width = -((-width ) >> s->lowres);
179  s->height = -((-height) >> s->lowres);
180 }
181 
182 #define INTERNAL_BUFFER_SIZE (32 + 1)
183 
184 #if (ARCH_ARM && HAVE_NEON) || ARCH_PPC || HAVE_MMX
185 # define STRIDE_ALIGN 16
186 #else
187 # define STRIDE_ALIGN 8
188 #endif
189 
191  int linesize_align[AV_NUM_DATA_POINTERS])
192 {
193  int i;
194  int w_align = 1;
195  int h_align = 1;
197 
198  if (desc) {
199  w_align = 1 << desc->log2_chroma_w;
200  h_align = 1 << desc->log2_chroma_h;
201  }
202 
203  switch (s->pix_fmt) {
204  case AV_PIX_FMT_YUV420P:
205  case AV_PIX_FMT_YUYV422:
206  case AV_PIX_FMT_UYVY422:
207  case AV_PIX_FMT_YUV422P:
208  case AV_PIX_FMT_YUV440P:
209  case AV_PIX_FMT_YUV444P:
210  case AV_PIX_FMT_GBRP:
211  case AV_PIX_FMT_GRAY8:
212  case AV_PIX_FMT_GRAY16BE:
213  case AV_PIX_FMT_GRAY16LE:
214  case AV_PIX_FMT_YUVJ420P:
215  case AV_PIX_FMT_YUVJ422P:
216  case AV_PIX_FMT_YUVJ440P:
217  case AV_PIX_FMT_YUVJ444P:
218  case AV_PIX_FMT_YUVA420P:
219  case AV_PIX_FMT_YUVA422P:
220  case AV_PIX_FMT_YUVA444P:
245  case AV_PIX_FMT_GBRP9LE:
246  case AV_PIX_FMT_GBRP9BE:
247  case AV_PIX_FMT_GBRP10LE:
248  case AV_PIX_FMT_GBRP10BE:
249  case AV_PIX_FMT_GBRP12LE:
250  case AV_PIX_FMT_GBRP12BE:
251  case AV_PIX_FMT_GBRP14LE:
252  case AV_PIX_FMT_GBRP14BE:
253  case AV_PIX_FMT_GBRP16LE:
254  case AV_PIX_FMT_GBRP16BE:
255  w_align = 16; //FIXME assume 16 pixel per macroblock
256  h_align = 16 * 2; // interlaced needs 2 macroblocks height
257  break;
258  case AV_PIX_FMT_YUV411P:
260  w_align = 32;
261  h_align = 8;
262  break;
263  case AV_PIX_FMT_YUV410P:
264  if (s->codec_id == AV_CODEC_ID_SVQ1) {
265  w_align = 64;
266  h_align = 64;
267  }
268  break;
269  case AV_PIX_FMT_RGB555:
270  if (s->codec_id == AV_CODEC_ID_RPZA) {
271  w_align = 4;
272  h_align = 4;
273  }
274  break;
275  case AV_PIX_FMT_PAL8:
276  case AV_PIX_FMT_BGR8:
277  case AV_PIX_FMT_RGB8:
278  if (s->codec_id == AV_CODEC_ID_SMC ||
280  w_align = 4;
281  h_align = 4;
282  }
283  if (s->codec_id == AV_CODEC_ID_JV) {
284  w_align = 8;
285  h_align = 8;
286  }
287  break;
288  case AV_PIX_FMT_BGR24:
289  if ((s->codec_id == AV_CODEC_ID_MSZH) ||
290  (s->codec_id == AV_CODEC_ID_ZLIB)) {
291  w_align = 4;
292  h_align = 4;
293  }
294  break;
295  case AV_PIX_FMT_RGB24:
296  if (s->codec_id == AV_CODEC_ID_CINEPAK) {
297  w_align = 4;
298  h_align = 4;
299  }
300  break;
301  default:
302  break;
303  }
304 
306  w_align = FFMAX(w_align, 8);
307  }
308 
309  *width = FFALIGN(*width, w_align);
310  *height = FFALIGN(*height, h_align);
311  if (s->codec_id == AV_CODEC_ID_H264 || s->lowres)
312  // some of the optimized chroma MC reads one line too much
313  // which is also done in mpeg decoders with lowres > 0
314  *height += 2;
315 
316  for (i = 0; i < 4; i++)
317  linesize_align[i] = STRIDE_ALIGN;
318 }
319 
321 {
323  int chroma_shift = desc->log2_chroma_w;
324  int linesize_align[AV_NUM_DATA_POINTERS];
325  int align;
326 
327  avcodec_align_dimensions2(s, width, height, linesize_align);
328  align = FFMAX(linesize_align[0], linesize_align[3]);
329  linesize_align[1] <<= chroma_shift;
330  linesize_align[2] <<= chroma_shift;
331  align = FFMAX3(align, linesize_align[1], linesize_align[2]);
332  *width = FFALIGN(*width, align);
333 }
334 
336  enum AVSampleFormat sample_fmt, const uint8_t *buf,
337  int buf_size, int align)
338 {
339  int ch, planar, needed_size, ret = 0;
340 
341  needed_size = av_samples_get_buffer_size(NULL, nb_channels,
342  frame->nb_samples, sample_fmt,
343  align);
344  if (buf_size < needed_size)
345  return AVERROR(EINVAL);
346 
347  planar = av_sample_fmt_is_planar(sample_fmt);
348  if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
349  if (!(frame->extended_data = av_mallocz(nb_channels *
350  sizeof(*frame->extended_data))))
351  return AVERROR(ENOMEM);
352  } else {
353  frame->extended_data = frame->data;
354  }
355 
356  if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
357  (uint8_t *)(intptr_t)buf, nb_channels, frame->nb_samples,
358  sample_fmt, align)) < 0) {
359  if (frame->extended_data != frame->data)
360  av_freep(&frame->extended_data);
361  return ret;
362  }
363  if (frame->extended_data != frame->data) {
364  for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
365  frame->data[ch] = frame->extended_data[ch];
366  }
367 
368  return ret;
369 }
370 
372 {
373  AVCodecInternal *avci = avctx->internal;
374  int buf_size, ret;
375 
376  av_freep(&avci->audio_data);
377  buf_size = av_samples_get_buffer_size(NULL, avctx->channels,
378  frame->nb_samples, avctx->sample_fmt,
379  0);
380  if (buf_size < 0)
381  return AVERROR(EINVAL);
382 
383  frame->data[0] = av_mallocz(buf_size);
384  if (!frame->data[0])
385  return AVERROR(ENOMEM);
386 
387  ret = avcodec_fill_audio_frame(frame, avctx->channels, avctx->sample_fmt,
388  frame->data[0], buf_size, 0);
389  if (ret < 0) {
390  av_freep(&frame->data[0]);
391  return ret;
392  }
393 
394  avci->audio_data = frame->data[0];
395  if (avctx->debug & FF_DEBUG_BUFFERS)
396  av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p, "
397  "internal audio buffer used\n", frame);
398 
399  return 0;
400 }
401 
403 {
404  int i;
405  int w = s->width;
406  int h = s->height;
407  InternalBuffer *buf;
408  AVCodecInternal *avci = s->internal;
409 
410  if (pic->data[0] != NULL) {
411  av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
412  return -1;
413  }
414  if (avci->buffer_count >= INTERNAL_BUFFER_SIZE) {
415  av_log(s, AV_LOG_ERROR, "buffer_count overflow (missing release_buffer?)\n");
416  return -1;
417  }
418 
419  if (av_image_check_size(w, h, 0, s) || s->pix_fmt<0) {
420  av_log(s, AV_LOG_ERROR, "video_get_buffer: image parameters invalid\n");
421  return -1;
422  }
423 
424  if (!avci->buffer) {
425  avci->buffer = av_mallocz((INTERNAL_BUFFER_SIZE + 1) *
426  sizeof(InternalBuffer));
427  }
428 
429  buf = &avci->buffer[avci->buffer_count];
430 
431  if (buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)) {
432  for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
433  av_freep(&buf->base[i]);
434  buf->data[i] = NULL;
435  }
436  }
437 
438  if (!buf->base[0]) {
439  int h_chroma_shift, v_chroma_shift;
440  int size[4] = { 0 };
441  int tmpsize;
442  int unaligned;
443  AVPicture picture;
444  int stride_align[AV_NUM_DATA_POINTERS];
446  const int pixel_size = desc->comp[0].step_minus1 + 1;
447 
448  av_pix_fmt_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift,
449  &v_chroma_shift);
450 
451  avcodec_align_dimensions2(s, &w, &h, stride_align);
452 
453  if (!(s->flags & CODEC_FLAG_EMU_EDGE)) {
454  w += EDGE_WIDTH * 2;
455  h += EDGE_WIDTH * 2;
456  }
457 
458  do {
459  // NOTE: do not align linesizes individually, this breaks e.g. assumptions
460  // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
461  av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
462  // increase alignment of w for next try (rhs gives the lowest bit set in w)
463  w += w & ~(w - 1);
464 
465  unaligned = 0;
466  for (i = 0; i < 4; i++)
467  unaligned |= picture.linesize[i] % stride_align[i];
468  } while (unaligned);
469 
470  tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
471  if (tmpsize < 0)
472  return -1;
473 
474  for (i = 0; i < 3 && picture.data[i + 1]; i++)
475  size[i] = picture.data[i + 1] - picture.data[i];
476  size[i] = tmpsize - (picture.data[i] - picture.data[0]);
477 
478  memset(buf->base, 0, sizeof(buf->base));
479  memset(buf->data, 0, sizeof(buf->data));
480 
481  for (i = 0; i < 4 && size[i]; i++) {
482  const int h_shift = i == 0 ? 0 : h_chroma_shift;
483  const int v_shift = i == 0 ? 0 : v_chroma_shift;
484 
485  buf->linesize[i] = picture.linesize[i];
486 
487  buf->base[i] = av_malloc(size[i] + 16 + STRIDE_ALIGN - 1); //FIXME 16
488  if (buf->base[i] == NULL)
489  return AVERROR(ENOMEM);
490 
491  // no edge if EDGE EMU or not planar YUV
492  if ((s->flags & CODEC_FLAG_EMU_EDGE) || !size[2])
493  buf->data[i] = buf->base[i];
494  else
495  buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i] * EDGE_WIDTH >> v_shift) + (pixel_size * EDGE_WIDTH >> h_shift), stride_align[i]);
496  }
497  for (; i < AV_NUM_DATA_POINTERS; i++) {
498  buf->base[i] = buf->data[i] = NULL;
499  buf->linesize[i] = 0;
500  }
501  if (size[1] && !size[2])
502  avpriv_set_systematic_pal2((uint32_t *)buf->data[1], s->pix_fmt);
503  buf->width = s->width;
504  buf->height = s->height;
505  buf->pix_fmt = s->pix_fmt;
506  }
507 
508  for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
509  pic->base[i] = buf->base[i];
510  pic->data[i] = buf->data[i];
511  pic->linesize[i] = buf->linesize[i];
512  }
513  pic->extended_data = pic->data;
514  avci->buffer_count++;
515 
516  if (s->debug & FF_DEBUG_BUFFERS)
517  av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d "
518  "buffers used\n", pic, avci->buffer_count);
519 
520  return 0;
521 }
522 
523 void avpriv_color_frame(AVFrame *frame, const int c[4])
524 {
525  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
526  int p, y, x;
527 
529 
530  for (p = 0; p<desc->nb_components; p++) {
531  uint8_t *dst = frame->data[p];
532  int is_chroma = p == 1 || p == 2;
533  int bytes = -((-frame->width) >> (is_chroma ? desc->log2_chroma_w : 0));
534  for (y = 0; y<-((-frame->height) >> (is_chroma ? desc->log2_chroma_h : 0)); y++){
535  if (desc->comp[0].depth_minus1 >= 8) {
536  for (x = 0; x<bytes; x++)
537  ((uint16_t*)dst)[x] = c[p];
538  }else
539  memset(dst, c[p], bytes);
540  dst += frame->linesize[p];
541  }
542  }
543 }
544 
546 {
547  frame->type = FF_BUFFER_TYPE_INTERNAL;
548  switch (avctx->codec_type) {
549  case AVMEDIA_TYPE_VIDEO:
550  return video_get_buffer(avctx, frame);
551  case AVMEDIA_TYPE_AUDIO:
552  return audio_get_buffer(avctx, frame);
553  default:
554  return -1;
555  }
556 }
557 
559 {
560  if (s->pkt) {
561  frame->pkt_pts = s->pkt->pts;
562  av_frame_set_pkt_pos (frame, s->pkt->pos);
564  av_frame_set_pkt_size (frame, s->pkt->size);
565  } else {
566  frame->pkt_pts = AV_NOPTS_VALUE;
567  av_frame_set_pkt_pos (frame, -1);
568  av_frame_set_pkt_duration(frame, 0);
569  av_frame_set_pkt_size (frame, -1);
570  }
572 
573  switch (s->codec->type) {
574  case AVMEDIA_TYPE_VIDEO:
575  frame->width = s->width;
576  frame->height = s->height;
577  frame->format = s->pix_fmt;
579  break;
580  case AVMEDIA_TYPE_AUDIO:
581  frame->sample_rate = s->sample_rate;
582  frame->format = s->sample_fmt;
583  frame->channel_layout = s->channel_layout;
584  av_frame_set_channels(frame, s->channels);
585  break;
586  }
587 }
588 
590 {
591  ff_init_buffer_info(avctx, frame);
592 
593  return avctx->get_buffer(avctx, frame);
594 }
595 
597 {
598  int i;
599  InternalBuffer *buf, *last;
600  AVCodecInternal *avci = s->internal;
601 
603 
604  assert(pic->type == FF_BUFFER_TYPE_INTERNAL);
605  assert(avci->buffer_count);
606 
607  if (avci->buffer) {
608  buf = NULL; /* avoids warning */
609  for (i = 0; i < avci->buffer_count; i++) { //just 3-5 checks so is not worth to optimize
610  buf = &avci->buffer[i];
611  if (buf->data[0] == pic->data[0])
612  break;
613  }
614  av_assert0(i < avci->buffer_count);
615  avci->buffer_count--;
616  last = &avci->buffer[avci->buffer_count];
617 
618  if (buf != last)
619  FFSWAP(InternalBuffer, *buf, *last);
620  }
621 
622  for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
623  pic->data[i] = NULL;
624 // pic->base[i]=NULL;
625 
626  if (s->debug & FF_DEBUG_BUFFERS)
627  av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d "
628  "buffers used\n", pic, avci->buffer_count);
629 }
630 
632 {
633  AVFrame temp_pic;
634  int i, ret;
635 
637 
638  if (pic->data[0] && (pic->width != s->width || pic->height != s->height || pic->format != s->pix_fmt)) {
639  av_log(s, AV_LOG_WARNING, "Picture changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s in reget buffer()\n",
641  s->release_buffer(s, pic);
642  }
643 
644  ff_init_buffer_info(s, pic);
645 
646  /* If no picture return a new buffer */
647  if (pic->data[0] == NULL) {
648  /* We will copy from buffer, so must be readable */
650  return ff_get_buffer(s, pic);
651  }
652 
653  assert(s->pix_fmt == pic->format);
654 
655  /* If internal buffer type return the same buffer */
656  if (pic->type == FF_BUFFER_TYPE_INTERNAL) {
657  return 0;
658  }
659 
660  /*
661  * Not internal type and reget_buffer not overridden, emulate cr buffer
662  */
663  temp_pic = *pic;
664  for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
665  pic->data[i] = pic->base[i] = NULL;
666  pic->opaque = NULL;
667  /* Allocate new frame */
668  if ((ret = ff_get_buffer(s, pic)))
669  return ret;
670  /* Copy image data from old buffer to new buffer */
671  av_picture_copy((AVPicture *)pic, (AVPicture *)&temp_pic, s->pix_fmt, s->width,
672  s->height);
673  s->release_buffer(s, &temp_pic); // Release old frame
674  return 0;
675 }
676 
677 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
678 {
679  int i;
680 
681  for (i = 0; i < count; i++) {
682  int r = func(c, (char *)arg + i * size);
683  if (ret)
684  ret[i] = r;
685  }
686  return 0;
687 }
688 
689 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
690 {
691  int i;
692 
693  for (i = 0; i < count; i++) {
694  int r = func(c, arg, i, 0);
695  if (ret)
696  ret[i] = r;
697  }
698  return 0;
699 }
700 
702 {
703  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
704  return desc->flags & PIX_FMT_HWACCEL;
705 }
706 
708 {
709  while (*fmt != AV_PIX_FMT_NONE && is_hwaccel_pix_fmt(*fmt))
710  ++fmt;
711  return fmt[0];
712 }
713 
715 {
716 #if LIBAVCODEC_VERSION_MAJOR >= 55
717  // extended_data should explicitly be freed when needed, this code is unsafe currently
718  // also this is not compatible to the <55 ABI/API
719  if (frame->extended_data != frame->data && 0)
720  av_freep(&frame->extended_data);
721 #endif
722 
723  memset(frame, 0, sizeof(AVFrame));
724 
725  frame->pts =
726  frame->pkt_dts =
727  frame->pkt_pts = AV_NOPTS_VALUE;
729  av_frame_set_pkt_duration (frame, 0);
730  av_frame_set_pkt_pos (frame, -1);
731  av_frame_set_pkt_size (frame, -1);
732  frame->key_frame = 1;
733  frame->sample_aspect_ratio = (AVRational) {0, 1 };
734  frame->format = -1; /* unknown */
735  frame->extended_data = frame->data;
736 }
737 
739 {
740  AVFrame *frame = av_malloc(sizeof(AVFrame));
741 
742  if (frame == NULL)
743  return NULL;
744 
745  frame->extended_data = NULL;
747 
748  return frame;
749 }
750 
752 {
753  AVFrame *f;
754 
755  if (!frame || !*frame)
756  return;
757 
758  f = *frame;
759 
760  if (f->extended_data != f->data)
761  av_freep(&f->extended_data);
762 
763  av_freep(frame);
764 }
765 
766 #define MAKE_ACCESSORS(str, name, type, field) \
767  type av_##name##_get_##field(const str *s) { return s->field; } \
768  void av_##name##_set_##field(str *s, type v) { s->field = v; }
769 
770 MAKE_ACCESSORS(AVFrame, frame, int64_t, best_effort_timestamp)
771 MAKE_ACCESSORS(AVFrame, frame, int64_t, pkt_duration)
772 MAKE_ACCESSORS(AVFrame, frame, int64_t, pkt_pos)
773 MAKE_ACCESSORS(AVFrame, frame, int64_t, channel_layout)
774 MAKE_ACCESSORS(AVFrame, frame, int, channels)
775 MAKE_ACCESSORS(AVFrame, frame, int, sample_rate)
776 MAKE_ACCESSORS(AVFrame, frame, AVDictionary *, metadata)
777 MAKE_ACCESSORS(AVFrame, frame, int, decode_error_flags)
778 MAKE_ACCESSORS(AVFrame, frame, int, pkt_size)
779 
780 AVDictionary **ff_frame_get_metadatap(AVFrame *frame) {return &frame->metadata;};
781 
782 MAKE_ACCESSORS(AVCodecContext, codec, AVRational, pkt_timebase)
783 MAKE_ACCESSORS(AVCodecContext, codec, const AVCodecDescriptor *, codec_descriptor)
784 
786 {
787  memset(sub, 0, sizeof(*sub));
788  sub->pts = AV_NOPTS_VALUE;
789 }
790 
791 static int get_bit_rate(AVCodecContext *ctx)
792 {
793  int bit_rate;
794  int bits_per_sample;
795 
796  switch (ctx->codec_type) {
797  case AVMEDIA_TYPE_VIDEO:
798  case AVMEDIA_TYPE_DATA:
801  bit_rate = ctx->bit_rate;
802  break;
803  case AVMEDIA_TYPE_AUDIO:
804  bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
805  bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
806  break;
807  default:
808  bit_rate = 0;
809  break;
810  }
811  return bit_rate;
812 }
813 
814 #if FF_API_AVCODEC_OPEN
815 int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
816 {
817  return avcodec_open2(avctx, codec, NULL);
818 }
819 #endif
820 
822 {
823  int ret = 0;
824 
826 
827  ret = avcodec_open2(avctx, codec, options);
828 
829  ff_lock_avcodec(avctx);
830  return ret;
831 }
832 
834 {
835  int ret = 0;
836  AVDictionary *tmp = NULL;
837 
838  if (avcodec_is_open(avctx))
839  return 0;
840 
841  if ((!codec && !avctx->codec)) {
842  av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2()\n");
843  return AVERROR(EINVAL);
844  }
845  if ((codec && avctx->codec && codec != avctx->codec)) {
846  av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
847  "but %s passed to avcodec_open2()\n", avctx->codec->name, codec->name);
848  return AVERROR(EINVAL);
849  }
850  if (!codec)
851  codec = avctx->codec;
852 
853  if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
854  return AVERROR(EINVAL);
855 
856  if (options)
857  av_dict_copy(&tmp, *options, 0);
858 
859  ret = ff_lock_avcodec(avctx);
860  if (ret < 0)
861  return ret;
862 
863  avctx->internal = av_mallocz(sizeof(AVCodecInternal));
864  if (!avctx->internal) {
865  ret = AVERROR(ENOMEM);
866  goto end;
867  }
868 
869  if (codec->priv_data_size > 0) {
870  if (!avctx->priv_data) {
871  avctx->priv_data = av_mallocz(codec->priv_data_size);
872  if (!avctx->priv_data) {
873  ret = AVERROR(ENOMEM);
874  goto end;
875  }
876  if (codec->priv_class) {
877  *(const AVClass **)avctx->priv_data = codec->priv_class;
879  }
880  }
881  if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
882  goto free_and_end;
883  } else {
884  avctx->priv_data = NULL;
885  }
886  if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
887  goto free_and_end;
888 
889  //We only call avcodec_set_dimensions() for non h264 codecs so as not to overwrite previously setup dimensions
890  if (!( avctx->coded_width && avctx->coded_height && avctx->width && avctx->height && avctx->codec_id == AV_CODEC_ID_H264)){
891 
892  if (avctx->coded_width && avctx->coded_height)
893  avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
894  else if (avctx->width && avctx->height)
895  avcodec_set_dimensions(avctx, avctx->width, avctx->height);
896  }
897 
898  if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
899  && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
900  || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
901  av_log(avctx, AV_LOG_WARNING, "Ignoring invalid width/height values\n");
902  avcodec_set_dimensions(avctx, 0, 0);
903  }
904 
905  /* if the decoder init function was already called previously,
906  * free the already allocated subtitle_header before overwriting it */
907  if (av_codec_is_decoder(codec))
908  av_freep(&avctx->subtitle_header);
909 
910  if (avctx->channels > FF_SANE_NB_CHANNELS) {
911  ret = AVERROR(EINVAL);
912  goto free_and_end;
913  }
914 
915  avctx->codec = codec;
916  if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
917  avctx->codec_id == AV_CODEC_ID_NONE) {
918  avctx->codec_type = codec->type;
919  avctx->codec_id = codec->id;
920  }
921  if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
922  && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
923  av_log(avctx, AV_LOG_ERROR, "Codec type or id mismatches\n");
924  ret = AVERROR(EINVAL);
925  goto free_and_end;
926  }
927  avctx->frame_number = 0;
929 
930  if (avctx->codec->capabilities & CODEC_CAP_EXPERIMENTAL &&
932  const char *codec_string = av_codec_is_encoder(codec) ? "encoder" : "decoder";
933  AVCodec *codec2;
935  "The %s '%s' is experimental but experimental codecs are not enabled, "
936  "add '-strict %d' if you want to use it.\n",
937  codec_string, codec->name, FF_COMPLIANCE_EXPERIMENTAL);
938  codec2 = av_codec_is_encoder(codec) ? avcodec_find_encoder(codec->id) : avcodec_find_decoder(codec->id);
939  if (!(codec2->capabilities & CODEC_CAP_EXPERIMENTAL))
940  av_log(NULL, AV_LOG_ERROR, "Alternatively use the non experimental %s '%s'.\n",
941  codec_string, codec2->name);
942  ret = AVERROR_EXPERIMENTAL;
943  goto free_and_end;
944  }
945 
946  if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
947  (!avctx->time_base.num || !avctx->time_base.den)) {
948  avctx->time_base.num = 1;
949  avctx->time_base.den = avctx->sample_rate;
950  }
951 
952  if (!HAVE_THREADS)
953  av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
954 
955  if (HAVE_THREADS) {
956  ff_unlock_avcodec(); //we will instanciate a few encoders thus kick the counter to prevent false detection of a problem
957  ret = ff_frame_thread_encoder_init(avctx, options ? *options : NULL);
958  ff_lock_avcodec(avctx);
959  if (ret < 0)
960  goto free_and_end;
961  }
962 
963  if (HAVE_THREADS && !avctx->thread_opaque
965  ret = ff_thread_init(avctx);
966  if (ret < 0) {
967  goto free_and_end;
968  }
969  }
971  avctx->thread_count = 1;
972 
973  if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
974  av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
975  avctx->codec->max_lowres);
976  ret = AVERROR(EINVAL);
977  goto free_and_end;
978  }
979 
980  if (av_codec_is_encoder(avctx->codec)) {
981  int i;
982  if (avctx->codec->sample_fmts) {
983  for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
984  if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
985  break;
986  if (avctx->channels == 1 &&
989  avctx->sample_fmt = avctx->codec->sample_fmts[i];
990  break;
991  }
992  }
993  if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
994  char buf[128];
995  snprintf(buf, sizeof(buf), "%d", avctx->sample_fmt);
996  av_log(avctx, AV_LOG_ERROR, "Specified sample format %s is invalid or not supported\n",
997  (char *)av_x_if_null(av_get_sample_fmt_name(avctx->sample_fmt), buf));
998  ret = AVERROR(EINVAL);
999  goto free_and_end;
1000  }
1001  }
1002  if (avctx->codec->pix_fmts) {
1003  for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
1004  if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
1005  break;
1006  if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE
1007  && !((avctx->codec_id == AV_CODEC_ID_MJPEG || avctx->codec_id == AV_CODEC_ID_LJPEG)
1009  char buf[128];
1010  snprintf(buf, sizeof(buf), "%d", avctx->pix_fmt);
1011  av_log(avctx, AV_LOG_ERROR, "Specified pixel format %s is invalid or not supported\n",
1012  (char *)av_x_if_null(av_get_pix_fmt_name(avctx->pix_fmt), buf));
1013  ret = AVERROR(EINVAL);
1014  goto free_and_end;
1015  }
1016  }
1017  if (avctx->codec->supported_samplerates) {
1018  for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
1019  if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
1020  break;
1021  if (avctx->codec->supported_samplerates[i] == 0) {
1022  av_log(avctx, AV_LOG_ERROR, "Specified sample rate %d is not supported\n",
1023  avctx->sample_rate);
1024  ret = AVERROR(EINVAL);
1025  goto free_and_end;
1026  }
1027  }
1028  if (avctx->codec->channel_layouts) {
1029  if (!avctx->channel_layout) {
1030  av_log(avctx, AV_LOG_WARNING, "Channel layout not specified\n");
1031  } else {
1032  for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
1033  if (avctx->channel_layout == avctx->codec->channel_layouts[i])
1034  break;
1035  if (avctx->codec->channel_layouts[i] == 0) {
1036  char buf[512];
1037  av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
1038  av_log(avctx, AV_LOG_ERROR, "Specified channel layout '%s' is not supported\n", buf);
1039  ret = AVERROR(EINVAL);
1040  goto free_and_end;
1041  }
1042  }
1043  }
1044  if (avctx->channel_layout && avctx->channels) {
1045  int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
1046  if (channels != avctx->channels) {
1047  char buf[512];
1048  av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
1049  av_log(avctx, AV_LOG_ERROR,
1050  "Channel layout '%s' with %d channels does not match number of specified channels %d\n",
1051  buf, channels, avctx->channels);
1052  ret = AVERROR(EINVAL);
1053  goto free_and_end;
1054  }
1055  } else if (avctx->channel_layout) {
1057  }
1058  if(avctx->codec_type == AVMEDIA_TYPE_VIDEO &&
1059  avctx->codec_id != AV_CODEC_ID_PNG // For mplayer
1060  ) {
1061  if (avctx->width <= 0 || avctx->height <= 0) {
1062  av_log(avctx, AV_LOG_ERROR, "dimensions not set\n");
1063  ret = AVERROR(EINVAL);
1064  goto free_and_end;
1065  }
1066  }
1067  if ( (avctx->codec_type == AVMEDIA_TYPE_VIDEO || avctx->codec_type == AVMEDIA_TYPE_AUDIO)
1068  && avctx->bit_rate>0 && avctx->bit_rate<1000) {
1069  av_log(avctx, AV_LOG_WARNING, "Bitrate %d is extreemly low, did you mean %dk\n", avctx->bit_rate, avctx->bit_rate);
1070  }
1071 
1072  if (!avctx->rc_initial_buffer_occupancy)
1073  avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3 / 4;
1074  }
1075 
1077  avctx->pts_correction_num_faulty_dts = 0;
1078  avctx->pts_correction_last_pts =
1079  avctx->pts_correction_last_dts = INT64_MIN;
1080 
1081  if ( avctx->codec->init && (!(avctx->active_thread_type&FF_THREAD_FRAME)
1082  || avctx->internal->frame_thread_encoder)) {
1083  ret = avctx->codec->init(avctx);
1084  if (ret < 0) {
1085  goto free_and_end;
1086  }
1087  }
1088 
1089  ret=0;
1090 
1091  if (av_codec_is_decoder(avctx->codec)) {
1092  if (!avctx->bit_rate)
1093  avctx->bit_rate = get_bit_rate(avctx);
1094  /* validate channel layout from the decoder */
1095  if (avctx->channel_layout) {
1096  int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
1097  if (!avctx->channels)
1098  avctx->channels = channels;
1099  else if (channels != avctx->channels) {
1100  char buf[512];
1101  av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
1102  av_log(avctx, AV_LOG_WARNING,
1103  "Channel layout '%s' with %d channels does not match specified number of channels %d: "
1104  "ignoring specified channel layout\n",
1105  buf, channels, avctx->channels);
1106  avctx->channel_layout = 0;
1107  }
1108  }
1109  if (avctx->channels && avctx->channels < 0 ||
1110  avctx->channels > FF_SANE_NB_CHANNELS) {
1111  ret = AVERROR(EINVAL);
1112  goto free_and_end;
1113  }
1114  if (avctx->sub_charenc) {
1115  if (avctx->codec_type != AVMEDIA_TYPE_SUBTITLE) {
1116  av_log(avctx, AV_LOG_ERROR, "Character encoding is only "
1117  "supported with subtitles codecs\n");
1118  ret = AVERROR(EINVAL);
1119  goto free_and_end;
1120  } else if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB) {
1121  av_log(avctx, AV_LOG_WARNING, "Codec '%s' is bitmap-based, "
1122  "subtitles character encoding will be ignored\n",
1123  avctx->codec_descriptor->name);
1125  } else {
1126  /* input character encoding is set for a text based subtitle
1127  * codec at this point */
1130 
1132  av_log(avctx, AV_LOG_ERROR, "Character encoding subtitles "
1133  "conversion needs a libavcodec built with iconv support "
1134  "for this codec\n");
1135  ret = AVERROR(ENOSYS);
1136  goto free_and_end;
1137  }
1138  }
1139  }
1140  }
1141 end:
1143  if (options) {
1144  av_dict_free(options);
1145  *options = tmp;
1146  }
1147 
1148  return ret;
1149 free_and_end:
1150  av_dict_free(&tmp);
1151  av_freep(&avctx->priv_data);
1152  av_freep(&avctx->internal);
1153  avctx->codec = NULL;
1154  goto end;
1155 }
1156 
1158 {
1159  if (size < 0 || avpkt->size < 0 || size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
1160  av_log(avctx, AV_LOG_ERROR, "Size %d invalid\n", size);
1161  return AVERROR(EINVAL);
1162  }
1163 
1164  if (avctx) {
1165  av_assert0(!avpkt->data || avpkt->data != avctx->internal->byte_buffer);
1166  if (!avpkt->data || avpkt->size < size) {
1168  avpkt->data = avctx->internal->byte_buffer;
1169  avpkt->size = avctx->internal->byte_buffer_size;
1170  avpkt->destruct = NULL;
1171  }
1172  }
1173 
1174  if (avpkt->data) {
1175  void *destruct = avpkt->destruct;
1176 
1177  if (avpkt->size < size) {
1178  av_log(avctx, AV_LOG_ERROR, "User packet is too small (%d < %d)\n", avpkt->size, size);
1179  return AVERROR(EINVAL);
1180  }
1181 
1182  av_init_packet(avpkt);
1183  avpkt->destruct = destruct;
1184  avpkt->size = size;
1185  return 0;
1186  } else {
1187  int ret = av_new_packet(avpkt, size);
1188  if (ret < 0)
1189  av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %d\n", size);
1190  return ret;
1191  }
1192 }
1193 
1195 {
1196  return ff_alloc_packet2(NULL, avpkt, size);
1197 }
1198 
1202 static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
1203 {
1204  AVFrame *frame = NULL;
1205  uint8_t *buf = NULL;
1206  int ret;
1207 
1208  if (!(frame = avcodec_alloc_frame()))
1209  return AVERROR(ENOMEM);
1210  *frame = *src;
1211 
1212  if ((ret = av_samples_get_buffer_size(&frame->linesize[0], s->channels,
1213  s->frame_size, s->sample_fmt, 0)) < 0)
1214  goto fail;
1215 
1216  if (!(buf = av_malloc(ret))) {
1217  ret = AVERROR(ENOMEM);
1218  goto fail;
1219  }
1220 
1221  frame->nb_samples = s->frame_size;
1222  if ((ret = avcodec_fill_audio_frame(frame, s->channels, s->sample_fmt,
1223  buf, ret, 0)) < 0)
1224  goto fail;
1225  if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
1226  src->nb_samples, s->channels, s->sample_fmt)) < 0)
1227  goto fail;
1228  if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
1229  frame->nb_samples - src->nb_samples,
1230  s->channels, s->sample_fmt)) < 0)
1231  goto fail;
1232 
1233  *dst = frame;
1234 
1235  return 0;
1236 
1237 fail:
1238  if (frame->extended_data != frame->data)
1239  av_freep(&frame->extended_data);
1240  av_freep(&buf);
1241  av_freep(&frame);
1242  return ret;
1243 }
1244 
1246  AVPacket *avpkt,
1247  const AVFrame *frame,
1248  int *got_packet_ptr)
1249 {
1250  AVFrame tmp;
1251  AVFrame *padded_frame = NULL;
1252  int ret;
1253  AVPacket user_pkt = *avpkt;
1254  int needs_realloc = !user_pkt.data;
1255 
1256  *got_packet_ptr = 0;
1257 
1258  if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
1259  av_free_packet(avpkt);
1260  av_init_packet(avpkt);
1261  return 0;
1262  }
1263 
1264  /* ensure that extended_data is properly set */
1265  if (frame && !frame->extended_data) {
1266  if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
1267  avctx->channels > AV_NUM_DATA_POINTERS) {
1268  av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
1269  "with more than %d channels, but extended_data is not set.\n",
1271  return AVERROR(EINVAL);
1272  }
1273  av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
1274 
1275  tmp = *frame;
1276  tmp.extended_data = tmp.data;
1277  frame = &tmp;
1278  }
1279 
1280  /* check for valid frame size */
1281  if (frame) {
1283  if (frame->nb_samples > avctx->frame_size) {
1284  av_log(avctx, AV_LOG_ERROR, "more samples than frame size (avcodec_encode_audio2)\n");
1285  return AVERROR(EINVAL);
1286  }
1287  } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
1288  if (frame->nb_samples < avctx->frame_size &&
1289  !avctx->internal->last_audio_frame) {
1290  ret = pad_last_frame(avctx, &padded_frame, frame);
1291  if (ret < 0)
1292  return ret;
1293 
1294  frame = padded_frame;
1295  avctx->internal->last_audio_frame = 1;
1296  }
1297 
1298  if (frame->nb_samples != avctx->frame_size) {
1299  av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) != frame_size (%d) (avcodec_encode_audio2)\n", frame->nb_samples, avctx->frame_size);
1300  ret = AVERROR(EINVAL);
1301  goto end;
1302  }
1303  }
1304  }
1305 
1306  ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1307  if (!ret) {
1308  if (*got_packet_ptr) {
1309  if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
1310  if (avpkt->pts == AV_NOPTS_VALUE)
1311  avpkt->pts = frame->pts;
1312  if (!avpkt->duration)
1313  avpkt->duration = ff_samples_to_time_base(avctx,
1314  frame->nb_samples);
1315  }
1316  avpkt->dts = avpkt->pts;
1317  } else {
1318  avpkt->size = 0;
1319  }
1320  }
1321  if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
1322  needs_realloc = 0;
1323  if (user_pkt.data) {
1324  if (user_pkt.size >= avpkt->size) {
1325  memcpy(user_pkt.data, avpkt->data, avpkt->size);
1326  } else {
1327  av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
1328  avpkt->size = user_pkt.size;
1329  ret = -1;
1330  }
1331  avpkt->data = user_pkt.data;
1332  avpkt->destruct = user_pkt.destruct;
1333  } else {
1334  if (av_dup_packet(avpkt) < 0) {
1335  ret = AVERROR(ENOMEM);
1336  }
1337  }
1338  }
1339 
1340  if (!ret) {
1341  if (needs_realloc && avpkt->data) {
1342  uint8_t *new_data = av_realloc(avpkt->data, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
1343  if (new_data)
1344  avpkt->data = new_data;
1345  }
1346 
1347  avctx->frame_number++;
1348  }
1349 
1350  if (ret < 0 || !*got_packet_ptr) {
1351  av_free_packet(avpkt);
1352  av_init_packet(avpkt);
1353  goto end;
1354  }
1355 
1356  /* NOTE: if we add any audio encoders which output non-keyframe packets,
1357  * this needs to be moved to the encoders, but for now we can do it
1358  * here to simplify things */
1359  avpkt->flags |= AV_PKT_FLAG_KEY;
1360 
1361 end:
1362  if (padded_frame) {
1363  av_freep(&padded_frame->data[0]);
1364  if (padded_frame->extended_data != padded_frame->data)
1365  av_freep(&padded_frame->extended_data);
1366  av_freep(&padded_frame);
1367  }
1368 
1369  return ret;
1370 }
1371 
1372 #if FF_API_OLD_ENCODE_AUDIO
1373 int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
1374  uint8_t *buf, int buf_size,
1375  const short *samples)
1376 {
1377  AVPacket pkt;
1378  AVFrame frame0 = { { 0 } };
1379  AVFrame *frame;
1380  int ret, samples_size, got_packet;
1381 
1382  av_init_packet(&pkt);
1383  pkt.data = buf;
1384  pkt.size = buf_size;
1385 
1386  if (samples) {
1387  frame = &frame0;
1389 
1390  if (avctx->frame_size) {
1391  frame->nb_samples = avctx->frame_size;
1392  } else {
1393  /* if frame_size is not set, the number of samples must be
1394  * calculated from the buffer size */
1395  int64_t nb_samples;
1396  if (!av_get_bits_per_sample(avctx->codec_id)) {
1397  av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
1398  "support this codec\n");
1399  return AVERROR(EINVAL);
1400  }
1401  nb_samples = (int64_t)buf_size * 8 /
1402  (av_get_bits_per_sample(avctx->codec_id) *
1403  avctx->channels);
1404  if (nb_samples >= INT_MAX)
1405  return AVERROR(EINVAL);
1406  frame->nb_samples = nb_samples;
1407  }
1408 
1409  /* it is assumed that the samples buffer is large enough based on the
1410  * relevant parameters */
1411  samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
1412  frame->nb_samples,
1413  avctx->sample_fmt, 1);
1414  if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
1415  avctx->sample_fmt,
1416  (const uint8_t *)samples,
1417  samples_size, 1)) < 0)
1418  return ret;
1419 
1420  /* fabricate frame pts from sample count.
1421  * this is needed because the avcodec_encode_audio() API does not have
1422  * a way for the user to provide pts */
1423  if (avctx->sample_rate && avctx->time_base.num)
1424  frame->pts = ff_samples_to_time_base(avctx,
1425  avctx->internal->sample_count);
1426  else
1427  frame->pts = AV_NOPTS_VALUE;
1428  avctx->internal->sample_count += frame->nb_samples;
1429  } else {
1430  frame = NULL;
1431  }
1432 
1433  got_packet = 0;
1434  ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
1435  if (!ret && got_packet && avctx->coded_frame) {
1436  avctx->coded_frame->pts = pkt.pts;
1437  avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
1438  }
1439  /* free any side data since we cannot return it */
1441 
1442  if (frame && frame->extended_data != frame->data)
1443  av_freep(&frame->extended_data);
1444 
1445  return ret ? ret : pkt.size;
1446 }
1447 
1448 #endif
1449 
1450 #if FF_API_OLD_ENCODE_VIDEO
1451 int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
1452  const AVFrame *pict)
1453 {
1454  AVPacket pkt;
1455  int ret, got_packet = 0;
1456 
1457  if (buf_size < FF_MIN_BUFFER_SIZE) {
1458  av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
1459  return -1;
1460  }
1461 
1462  av_init_packet(&pkt);
1463  pkt.data = buf;
1464  pkt.size = buf_size;
1465 
1466  ret = avcodec_encode_video2(avctx, &pkt, pict, &got_packet);
1467  if (!ret && got_packet && avctx->coded_frame) {
1468  avctx->coded_frame->pts = pkt.pts;
1469  avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
1470  }
1471 
1472  /* free any side data since we cannot return it */
1473  if (pkt.side_data_elems > 0) {
1474  int i;
1475  for (i = 0; i < pkt.side_data_elems; i++)
1476  av_free(pkt.side_data[i].data);
1477  av_freep(&pkt.side_data);
1478  pkt.side_data_elems = 0;
1479  }
1480 
1481  return ret ? ret : pkt.size;
1482 }
1483 
1484 #endif
1485 
1487  AVPacket *avpkt,
1488  const AVFrame *frame,
1489  int *got_packet_ptr)
1490 {
1491  int ret;
1492  AVPacket user_pkt = *avpkt;
1493  int needs_realloc = !user_pkt.data;
1494 
1495  *got_packet_ptr = 0;
1496 
1498  return ff_thread_video_encode_frame(avctx, avpkt, frame, got_packet_ptr);
1499 
1500  if ((avctx->flags&CODEC_FLAG_PASS1) && avctx->stats_out)
1501  avctx->stats_out[0] = '\0';
1502 
1503  if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
1504  av_free_packet(avpkt);
1505  av_init_packet(avpkt);
1506  avpkt->size = 0;
1507  return 0;
1508  }
1509 
1510  if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
1511  return AVERROR(EINVAL);
1512 
1513  av_assert0(avctx->codec->encode2);
1514 
1515  ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1516  av_assert0(ret <= 0);
1517 
1518  if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
1519  needs_realloc = 0;
1520  if (user_pkt.data) {
1521  if (user_pkt.size >= avpkt->size) {
1522  memcpy(user_pkt.data, avpkt->data, avpkt->size);
1523  } else {
1524  av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
1525  avpkt->size = user_pkt.size;
1526  ret = -1;
1527  }
1528  avpkt->data = user_pkt.data;
1529  avpkt->destruct = user_pkt.destruct;
1530  } else {
1531  if (av_dup_packet(avpkt) < 0) {
1532  ret = AVERROR(ENOMEM);
1533  }
1534  }
1535  }
1536 
1537  if (!ret) {
1538  if (!*got_packet_ptr)
1539  avpkt->size = 0;
1540  else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
1541  avpkt->pts = avpkt->dts = frame->pts;
1542 
1543  if (needs_realloc && avpkt->data &&
1544  avpkt->destruct == av_destruct_packet) {
1545  uint8_t *new_data = av_realloc(avpkt->data, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
1546  if (new_data)
1547  avpkt->data = new_data;
1548  }
1549 
1550  avctx->frame_number++;
1551  }
1552 
1553  if (ret < 0 || !*got_packet_ptr)
1554  av_free_packet(avpkt);
1555 
1556  emms_c();
1557  return ret;
1558 }
1559 
1560 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
1561  const AVSubtitle *sub)
1562 {
1563  int ret;
1564  if (sub->start_display_time) {
1565  av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
1566  return -1;
1567  }
1568 
1569  ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
1570  avctx->frame_number++;
1571  return ret;
1572 }
1573 
1584 static int64_t guess_correct_pts(AVCodecContext *ctx,
1585  int64_t reordered_pts, int64_t dts)
1586 {
1587  int64_t pts = AV_NOPTS_VALUE;
1588 
1589  if (dts != AV_NOPTS_VALUE) {
1591  ctx->pts_correction_last_dts = dts;
1592  }
1593  if (reordered_pts != AV_NOPTS_VALUE) {
1594  ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
1595  ctx->pts_correction_last_pts = reordered_pts;
1596  }
1598  && reordered_pts != AV_NOPTS_VALUE)
1599  pts = reordered_pts;
1600  else
1601  pts = dts;
1602 
1603  return pts;
1604 }
1605 
1606 static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
1607 {
1608  int size = 0;
1609  const uint8_t *data;
1610  uint32_t flags;
1611 
1612  if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
1613  return;
1614 
1615  data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
1616  if (!data || size < 4)
1617  return;
1618  flags = bytestream_get_le32(&data);
1619  size -= 4;
1620  if (size < 4) /* Required for any of the changes */
1621  return;
1623  avctx->channels = bytestream_get_le32(&data);
1624  size -= 4;
1625  }
1627  if (size < 8)
1628  return;
1629  avctx->channel_layout = bytestream_get_le64(&data);
1630  size -= 8;
1631  }
1632  if (size < 4)
1633  return;
1635  avctx->sample_rate = bytestream_get_le32(&data);
1636  size -= 4;
1637  }
1639  if (size < 8)
1640  return;
1641  avctx->width = bytestream_get_le32(&data);
1642  avctx->height = bytestream_get_le32(&data);
1643  avcodec_set_dimensions(avctx, avctx->width, avctx->height);
1644  size -= 8;
1645  }
1646 }
1647 
1649 {
1650  int size, ret = 0;
1651  const uint8_t *side_metadata;
1652  const uint8_t *end;
1653 
1654  av_dict_free(&avctx->metadata);
1655  side_metadata = av_packet_get_side_data(avctx->pkt,
1657  if (!side_metadata)
1658  goto end;
1659  end = side_metadata + size;
1660  if (size && end[-1])
1661  return AVERROR_INVALIDDATA;
1662  while (side_metadata < end) {
1663  const uint8_t *key = side_metadata;
1664  const uint8_t *val = side_metadata + strlen(key) + 1;
1665  int ret;
1666 
1667  if (val >= end)
1668  return AVERROR_INVALIDDATA;
1669 
1670  ret = av_dict_set(ff_frame_get_metadatap(frame), key, val, 0);
1671  if (ret < 0)
1672  break;
1673  side_metadata = val + strlen(val) + 1;
1674  }
1675 end:
1676  avctx->metadata = av_frame_get_metadata(frame);
1677  return ret;
1678 }
1679 
1681  int *got_picture_ptr,
1682  const AVPacket *avpkt)
1683 {
1684  int ret;
1685  // copy to ensure we do not change avpkt
1686  AVPacket tmp = *avpkt;
1687 
1688  if (avctx->codec->type != AVMEDIA_TYPE_VIDEO) {
1689  av_log(avctx, AV_LOG_ERROR, "Invalid media type for video\n");
1690  return AVERROR(EINVAL);
1691  }
1692 
1693  *got_picture_ptr = 0;
1694  if ((avctx->coded_width || avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
1695  return AVERROR(EINVAL);
1696 
1697  avcodec_get_frame_defaults(picture);
1698 
1699  if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
1700  int did_split = av_packet_split_side_data(&tmp);
1701  apply_param_change(avctx, &tmp);
1702  avctx->pkt = &tmp;
1704  ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
1705  &tmp);
1706  else {
1707  ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
1708  &tmp);
1709  picture->pkt_dts = avpkt->dts;
1710 
1711  if(!avctx->has_b_frames){
1712  av_frame_set_pkt_pos(picture, avpkt->pos);
1713  }
1714  //FIXME these should be under if(!avctx->has_b_frames)
1715  /* get_buffer is supposed to set frame parameters */
1716  if (!(avctx->codec->capabilities & CODEC_CAP_DR1)) {
1717  if (!picture->sample_aspect_ratio.num) picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
1718  if (!picture->width) picture->width = avctx->width;
1719  if (!picture->height) picture->height = avctx->height;
1720  if (picture->format == AV_PIX_FMT_NONE) picture->format = avctx->pix_fmt;
1721  }
1722  }
1723  add_metadata_from_side_data(avctx, picture);
1724 
1725  emms_c(); //needed to avoid an emms_c() call before every return;
1726 
1727  avctx->pkt = NULL;
1728  if (did_split) {
1730  if(ret == tmp.size)
1731  ret = avpkt->size;
1732  }
1733 
1734  if (*got_picture_ptr){
1735  avctx->frame_number++;
1737  guess_correct_pts(avctx,
1738  picture->pkt_pts,
1739  picture->pkt_dts));
1740  }
1741  } else
1742  ret = 0;
1743 
1744  /* many decoders assign whole AVFrames, thus overwriting extended_data;
1745  * make sure it's set correctly */
1746  picture->extended_data = picture->data;
1747 
1748  return ret;
1749 }
1750 
1751 #if FF_API_OLD_DECODE_AUDIO
1752 int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
1753  int *frame_size_ptr,
1754  AVPacket *avpkt)
1755 {
1756  AVFrame frame = { { 0 } };
1757  int ret, got_frame = 0;
1758 
1759  if (avctx->get_buffer != avcodec_default_get_buffer) {
1760  av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
1761  "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
1762  av_log(avctx, AV_LOG_ERROR, "Please port your application to "
1763  "avcodec_decode_audio4()\n");
1766  }
1767 
1768  ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
1769 
1770  if (ret >= 0 && got_frame) {
1771  int ch, plane_size;
1772  int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
1773  int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
1774  frame.nb_samples,
1775  avctx->sample_fmt, 1);
1776  if (*frame_size_ptr < data_size) {
1777  av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
1778  "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
1779  return AVERROR(EINVAL);
1780  }
1781 
1782  memcpy(samples, frame.extended_data[0], plane_size);
1783 
1784  if (planar && avctx->channels > 1) {
1785  uint8_t *out = ((uint8_t *)samples) + plane_size;
1786  for (ch = 1; ch < avctx->channels; ch++) {
1787  memcpy(out, frame.extended_data[ch], plane_size);
1788  out += plane_size;
1789  }
1790  }
1791  *frame_size_ptr = data_size;
1792  } else {
1793  *frame_size_ptr = 0;
1794  }
1795  return ret;
1796 }
1797 
1798 #endif
1799 
1801  AVFrame *frame,
1802  int *got_frame_ptr,
1803  const AVPacket *avpkt)
1804 {
1805  int planar, channels;
1806  int ret = 0;
1807 
1808  *got_frame_ptr = 0;
1809 
1810  if (!avpkt->data && avpkt->size) {
1811  av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
1812  return AVERROR(EINVAL);
1813  }
1814  if (avctx->codec->type != AVMEDIA_TYPE_AUDIO) {
1815  av_log(avctx, AV_LOG_ERROR, "Invalid media type for audio\n");
1816  return AVERROR(EINVAL);
1817  }
1818 
1820 
1821  if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
1822  uint8_t *side;
1823  int side_size;
1824  // copy to ensure we do not change avpkt
1825  AVPacket tmp = *avpkt;
1826  int did_split = av_packet_split_side_data(&tmp);
1827  apply_param_change(avctx, &tmp);
1828 
1829  avctx->pkt = &tmp;
1830  ret = avctx->codec->decode(avctx, frame, got_frame_ptr, &tmp);
1831  if (ret >= 0 && *got_frame_ptr) {
1832  avctx->frame_number++;
1833  frame->pkt_dts = avpkt->dts;
1835  guess_correct_pts(avctx,
1836  frame->pkt_pts,
1837  frame->pkt_dts));
1838  if (frame->format == AV_SAMPLE_FMT_NONE)
1839  frame->format = avctx->sample_fmt;
1840  if (!frame->channel_layout)
1841  frame->channel_layout = avctx->channel_layout;
1842  if (!av_frame_get_channels(frame))
1843  av_frame_set_channels(frame, avctx->channels);
1844  if (!frame->sample_rate)
1845  frame->sample_rate = avctx->sample_rate;
1846  }
1847  add_metadata_from_side_data(avctx, frame);
1848 
1849  side= av_packet_get_side_data(avctx->pkt, AV_PKT_DATA_SKIP_SAMPLES, &side_size);
1850  if(side && side_size>=10) {
1851  avctx->internal->skip_samples = AV_RL32(side);
1852  av_log(avctx, AV_LOG_DEBUG, "skip %d samples due to side data\n",
1853  avctx->internal->skip_samples);
1854  }
1855  if (avctx->internal->skip_samples && *got_frame_ptr) {
1856  if(frame->nb_samples <= avctx->internal->skip_samples){
1857  *got_frame_ptr = 0;
1858  avctx->internal->skip_samples -= frame->nb_samples;
1859  av_log(avctx, AV_LOG_DEBUG, "skip whole frame, skip left: %d\n",
1860  avctx->internal->skip_samples);
1861  } else {
1863  frame->nb_samples - avctx->internal->skip_samples, avctx->channels, frame->format);
1864  if(avctx->pkt_timebase.num && avctx->sample_rate) {
1865  int64_t diff_ts = av_rescale_q(avctx->internal->skip_samples,
1866  (AVRational){1, avctx->sample_rate},
1867  avctx->pkt_timebase);
1868  if(frame->pkt_pts!=AV_NOPTS_VALUE)
1869  frame->pkt_pts += diff_ts;
1870  if(frame->pkt_dts!=AV_NOPTS_VALUE)
1871  frame->pkt_dts += diff_ts;
1872  if (av_frame_get_pkt_duration(frame) >= diff_ts)
1873  av_frame_set_pkt_duration(frame, av_frame_get_pkt_duration(frame) - diff_ts);
1874  } else {
1875  av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for skipped samples.\n");
1876  }
1877  av_log(avctx, AV_LOG_DEBUG, "skip %d/%d samples\n",
1878  avctx->internal->skip_samples, frame->nb_samples);
1879  frame->nb_samples -= avctx->internal->skip_samples;
1880  avctx->internal->skip_samples = 0;
1881  }
1882  }
1883 
1884  avctx->pkt = NULL;
1885  if (did_split) {
1887  if(ret == tmp.size)
1888  ret = avpkt->size;
1889  }
1890  }
1891 
1892  /* many decoders assign whole AVFrames, thus overwriting extended_data;
1893  * make sure it's set correctly; assume decoders that actually use
1894  * extended_data are doing it correctly */
1895  if (*got_frame_ptr) {
1896  planar = av_sample_fmt_is_planar(frame->format);
1897  channels = av_frame_get_channels(frame);
1898  if (!(planar && channels > AV_NUM_DATA_POINTERS))
1899  frame->extended_data = frame->data;
1900  } else {
1901  frame->extended_data = NULL;
1902  }
1903 
1904  return ret;
1905 }
1906 
1907 #define UTF8_MAX_BYTES 4 /* 5 and 6 bytes sequences should not be used */
1909  AVPacket *outpkt, const AVPacket *inpkt)
1910 {
1911 #if CONFIG_ICONV
1912  iconv_t cd = (iconv_t)-1;
1913  int ret = 0;
1914  char *inb, *outb;
1915  size_t inl, outl;
1916  AVPacket tmp;
1917 #endif
1918 
1920  return 0;
1921 
1922 #if CONFIG_ICONV
1923  cd = iconv_open("UTF-8", avctx->sub_charenc);
1924  if (cd == (iconv_t)-1) {
1925  av_log(avctx, AV_LOG_ERROR, "Unable to open iconv context "
1926  "with input character encoding \"%s\"\n", avctx->sub_charenc);
1927  ret = AVERROR(errno);
1928  goto end;
1929  }
1930 
1931  inb = inpkt->data;
1932  inl = inpkt->size;
1933 
1934  if (inl >= INT_MAX / UTF8_MAX_BYTES - FF_INPUT_BUFFER_PADDING_SIZE) {
1935  av_log(avctx, AV_LOG_ERROR, "Subtitles packet is too big for recoding\n");
1936  ret = AVERROR(ENOMEM);
1937  goto end;
1938  }
1939 
1940  ret = av_new_packet(&tmp, inl * UTF8_MAX_BYTES);
1941  if (ret < 0)
1942  goto end;
1943  outpkt->data = tmp.data;
1944  outpkt->size = tmp.size;
1945  outb = outpkt->data;
1946  outl = outpkt->size;
1947 
1948  if (iconv(cd, &inb, &inl, &outb, &outl) == (size_t)-1 ||
1949  iconv(cd, NULL, NULL, &outb, &outl) == (size_t)-1 ||
1950  outl >= outpkt->size || inl != 0) {
1951  av_log(avctx, AV_LOG_ERROR, "Unable to recode subtitle event \"%s\" "
1952  "from %s to UTF-8\n", inpkt->data, avctx->sub_charenc);
1953  av_free_packet(&tmp);
1954  ret = AVERROR(errno);
1955  goto end;
1956  }
1957  outpkt->size -= outl;
1958  memset(outpkt->data + outpkt->size, 0, outl);
1959 
1960 end:
1961  if (cd != (iconv_t)-1)
1962  iconv_close(cd);
1963  return ret;
1964 #else
1965  av_assert0(!"requesting subtitles recoding without iconv");
1966 #endif
1967 }
1968 
1970  int *got_sub_ptr,
1971  AVPacket *avpkt)
1972 {
1973  int ret = 0;
1974 
1975  if (avctx->codec->type != AVMEDIA_TYPE_SUBTITLE) {
1976  av_log(avctx, AV_LOG_ERROR, "Invalid media type for subtitles\n");
1977  return AVERROR(EINVAL);
1978  }
1979 
1980  *got_sub_ptr = 0;
1982 
1983  if (avpkt->size) {
1984  AVPacket pkt_recoded;
1985  AVPacket tmp = *avpkt;
1986  int did_split = av_packet_split_side_data(&tmp);
1987  //apply_param_change(avctx, &tmp);
1988 
1989  if (did_split) {
1990  /* FFMIN() prevents overflow in case the packet wasn't allocated with
1991  * proper padding.
1992  * If the side data is smaller than the buffer padding size, the
1993  * remaining bytes should have already been filled with zeros by the
1994  * original packet allocation anyway. */
1995  memset(tmp.data + tmp.size, 0,
1996  FFMIN(avpkt->size - tmp.size, FF_INPUT_BUFFER_PADDING_SIZE));
1997  }
1998 
1999  pkt_recoded = tmp;
2000  ret = recode_subtitle(avctx, &pkt_recoded, &tmp);
2001  if (ret < 0) {
2002  *got_sub_ptr = 0;
2003  } else {
2004  avctx->pkt = &pkt_recoded;
2005 
2006  if (avctx->pkt_timebase.den && avpkt->pts != AV_NOPTS_VALUE)
2007  sub->pts = av_rescale_q(avpkt->pts,
2008  avctx->pkt_timebase, AV_TIME_BASE_Q);
2009  ret = avctx->codec->decode(avctx, sub, got_sub_ptr, &pkt_recoded);
2010  av_assert1((ret >= 0) >= !!*got_sub_ptr &&
2011  !!*got_sub_ptr >= !!sub->num_rects);
2012  if (tmp.data != pkt_recoded.data)
2013  av_free(pkt_recoded.data);
2015  avctx->pkt = NULL;
2016  }
2017 
2018  if (did_split) {
2020  if(ret == tmp.size)
2021  ret = avpkt->size;
2022  }
2023 
2024  if (*got_sub_ptr)
2025  avctx->frame_number++;
2026  }
2027 
2028  return ret;
2029 }
2030 
2032 {
2033  int i;
2034 
2035  for (i = 0; i < sub->num_rects; i++) {
2036  av_freep(&sub->rects[i]->pict.data[0]);
2037  av_freep(&sub->rects[i]->pict.data[1]);
2038  av_freep(&sub->rects[i]->pict.data[2]);
2039  av_freep(&sub->rects[i]->pict.data[3]);
2040  av_freep(&sub->rects[i]->text);
2041  av_freep(&sub->rects[i]->ass);
2042  av_freep(&sub->rects[i]);
2043  }
2044 
2045  av_freep(&sub->rects);
2046 
2047  memset(sub, 0, sizeof(AVSubtitle));
2048 }
2049 
2051 {
2052  int ret = 0;
2053 
2055 
2056  ret = avcodec_close(avctx);
2057 
2059  return ret;
2060 }
2061 
2063 {
2064  int ret = ff_lock_avcodec(avctx);
2065  if (ret < 0)
2066  return ret;
2067 
2068  if (avcodec_is_open(avctx)) {
2069  if (HAVE_THREADS && avctx->internal->frame_thread_encoder && avctx->thread_count > 1) {
2072  ff_lock_avcodec(avctx);
2073  }
2074  if (HAVE_THREADS && avctx->thread_opaque)
2075  ff_thread_free(avctx);
2076  if (avctx->codec && avctx->codec->close)
2077  avctx->codec->close(avctx);
2079  avctx->coded_frame = NULL;
2080  avctx->internal->byte_buffer_size = 0;
2081  av_freep(&avctx->internal->byte_buffer);
2082  av_freep(&avctx->internal);
2083  av_dict_free(&avctx->metadata);
2084  }
2085 
2086  if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
2087  av_opt_free(avctx->priv_data);
2088  av_opt_free(avctx);
2089  av_freep(&avctx->priv_data);
2090  if (av_codec_is_encoder(avctx->codec))
2091  av_freep(&avctx->extradata);
2092  avctx->codec = NULL;
2093  avctx->active_thread_type = 0;
2094 
2096  return 0;
2097 }
2098 
2100 {
2101  switch(id){
2102  //This is for future deprecatec codec ids, its empty since
2103  //last major bump but will fill up again over time, please don't remove it
2104 // case AV_CODEC_ID_UTVIDEO_DEPRECATED: return AV_CODEC_ID_UTVIDEO;
2107  default : return id;
2108  }
2109 }
2110 
2111 static AVCodec *find_encdec(enum AVCodecID id, int encoder)
2112 {
2113  AVCodec *p, *experimental = NULL;
2114  p = first_avcodec;
2115  id= remap_deprecated_codec_id(id);
2116  while (p) {
2117  if ((encoder ? av_codec_is_encoder(p) : av_codec_is_decoder(p)) &&
2118  p->id == id) {
2119  if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
2120  experimental = p;
2121  } else
2122  return p;
2123  }
2124  p = p->next;
2125  }
2126  return experimental;
2127 }
2128 
2130 {
2131  return find_encdec(id, 1);
2132 }
2133 
2135 {
2136  AVCodec *p;
2137  if (!name)
2138  return NULL;
2139  p = first_avcodec;
2140  while (p) {
2141  if (av_codec_is_encoder(p) && strcmp(name, p->name) == 0)
2142  return p;
2143  p = p->next;
2144  }
2145  return NULL;
2146 }
2147 
2149 {
2150  return find_encdec(id, 0);
2151 }
2152 
2154 {
2155  AVCodec *p;
2156  if (!name)
2157  return NULL;
2158  p = first_avcodec;
2159  while (p) {
2160  if (av_codec_is_decoder(p) && strcmp(name, p->name) == 0)
2161  return p;
2162  p = p->next;
2163  }
2164  return NULL;
2165 }
2166 
2167 const char *avcodec_get_name(enum AVCodecID id)
2168 {
2169  const AVCodecDescriptor *cd;
2170  AVCodec *codec;
2171 
2172  if (id == AV_CODEC_ID_NONE)
2173  return "none";
2174  cd = avcodec_descriptor_get(id);
2175  if (cd)
2176  return cd->name;
2177  av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
2178  codec = avcodec_find_decoder(id);
2179  if (codec)
2180  return codec->name;
2181  codec = avcodec_find_encoder(id);
2182  if (codec)
2183  return codec->name;
2184  return "unknown_codec";
2185 }
2186 
2187 size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
2188 {
2189  int i, len, ret = 0;
2190 
2191 #define TAG_PRINT(x) \
2192  (((x) >= '0' && (x) <= '9') || \
2193  ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
2194  ((x) == '.' || (x) == ' ' || (x) == '-' || (x) == '_'))
2195 
2196  for (i = 0; i < 4; i++) {
2197  len = snprintf(buf, buf_size,
2198  TAG_PRINT(codec_tag & 0xFF) ? "%c" : "[%d]", codec_tag & 0xFF);
2199  buf += len;
2200  buf_size = buf_size > len ? buf_size - len : 0;
2201  ret += len;
2202  codec_tag >>= 8;
2203  }
2204  return ret;
2205 }
2206 
2207 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
2208 {
2209  const char *codec_type;
2210  const char *codec_name;
2211  const char *profile = NULL;
2212  const AVCodec *p;
2213  int bitrate;
2214  AVRational display_aspect_ratio;
2215 
2216  if (!buf || buf_size <= 0)
2217  return;
2218  codec_type = av_get_media_type_string(enc->codec_type);
2219  codec_name = avcodec_get_name(enc->codec_id);
2220  if (enc->profile != FF_PROFILE_UNKNOWN) {
2221  if (enc->codec)
2222  p = enc->codec;
2223  else
2224  p = encode ? avcodec_find_encoder(enc->codec_id) :
2226  if (p)
2227  profile = av_get_profile_name(p, enc->profile);
2228  }
2229 
2230  snprintf(buf, buf_size, "%s: %s%s", codec_type ? codec_type : "unknown",
2231  codec_name, enc->mb_decision ? " (hq)" : "");
2232  buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
2233  if (profile)
2234  snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
2235  if (enc->codec_tag) {
2236  char tag_buf[32];
2237  av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
2238  snprintf(buf + strlen(buf), buf_size - strlen(buf),
2239  " (%s / 0x%04X)", tag_buf, enc->codec_tag);
2240  }
2241 
2242  switch (enc->codec_type) {
2243  case AVMEDIA_TYPE_VIDEO:
2244  if (enc->pix_fmt != AV_PIX_FMT_NONE) {
2245  snprintf(buf + strlen(buf), buf_size - strlen(buf),
2246  ", %s",
2248  if (enc->bits_per_raw_sample &&
2250  snprintf(buf + strlen(buf), buf_size - strlen(buf),
2251  " (%d bpc)", enc->bits_per_raw_sample);
2252  }
2253  if (enc->width) {
2254  snprintf(buf + strlen(buf), buf_size - strlen(buf),
2255  ", %dx%d",
2256  enc->width, enc->height);
2257  if (enc->sample_aspect_ratio.num) {
2258  av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
2259  enc->width * enc->sample_aspect_ratio.num,
2260  enc->height * enc->sample_aspect_ratio.den,
2261  1024 * 1024);
2262  snprintf(buf + strlen(buf), buf_size - strlen(buf),
2263  " [SAR %d:%d DAR %d:%d]",
2265  display_aspect_ratio.num, display_aspect_ratio.den);
2266  }
2267  if (av_log_get_level() >= AV_LOG_DEBUG) {
2268  int g = av_gcd(enc->time_base.num, enc->time_base.den);
2269  snprintf(buf + strlen(buf), buf_size - strlen(buf),
2270  ", %d/%d",
2271  enc->time_base.num / g, enc->time_base.den / g);
2272  }
2273  }
2274  if (encode) {
2275  snprintf(buf + strlen(buf), buf_size - strlen(buf),
2276  ", q=%d-%d", enc->qmin, enc->qmax);
2277  }
2278  break;
2279  case AVMEDIA_TYPE_AUDIO:
2280  if (enc->sample_rate) {
2281  snprintf(buf + strlen(buf), buf_size - strlen(buf),
2282  ", %d Hz", enc->sample_rate);
2283  }
2284  av_strlcat(buf, ", ", buf_size);
2285  av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
2286  if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
2287  snprintf(buf + strlen(buf), buf_size - strlen(buf),
2288  ", %s", av_get_sample_fmt_name(enc->sample_fmt));
2289  }
2290  break;
2291  case AVMEDIA_TYPE_DATA:
2292  if (av_log_get_level() >= AV_LOG_DEBUG) {
2293  int g = av_gcd(enc->time_base.num, enc->time_base.den);
2294  if (g)
2295  snprintf(buf + strlen(buf), buf_size - strlen(buf),
2296  ", %d/%d",
2297  enc->time_base.num / g, enc->time_base.den / g);
2298  }
2299  break;
2300  default:
2301  return;
2302  }
2303  if (encode) {
2304  if (enc->flags & CODEC_FLAG_PASS1)
2305  snprintf(buf + strlen(buf), buf_size - strlen(buf),
2306  ", pass 1");
2307  if (enc->flags & CODEC_FLAG_PASS2)
2308  snprintf(buf + strlen(buf), buf_size - strlen(buf),
2309  ", pass 2");
2310  }
2311  bitrate = get_bit_rate(enc);
2312  if (bitrate != 0) {
2313  snprintf(buf + strlen(buf), buf_size - strlen(buf),
2314  ", %d kb/s", bitrate / 1000);
2315  }
2316 }
2317 
2318 const char *av_get_profile_name(const AVCodec *codec, int profile)
2319 {
2320  const AVProfile *p;
2321  if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
2322  return NULL;
2323 
2324  for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
2325  if (p->profile == profile)
2326  return p->name;
2327 
2328  return NULL;
2329 }
2330 
2331 unsigned avcodec_version(void)
2332 {
2333 // av_assert0(AV_CODEC_ID_V410==164);
2336 // av_assert0(AV_CODEC_ID_BMV_AUDIO==86071);
2337  av_assert0(AV_CODEC_ID_SRT==94216);
2339 
2340  return LIBAVCODEC_VERSION_INT;
2341 }
2342 
2343 const char *avcodec_configuration(void)
2344 {
2345  return FFMPEG_CONFIGURATION;
2346 }
2347 
2348 const char *avcodec_license(void)
2349 {
2350 #define LICENSE_PREFIX "libavcodec license: "
2351  return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
2352 }
2353 
2355 {
2357  ff_thread_flush(avctx);
2358  else if (avctx->codec->flush)
2359  avctx->codec->flush(avctx);
2360 
2361  avctx->pts_correction_last_pts =
2362  avctx->pts_correction_last_dts = INT64_MIN;
2363 }
2364 
2366 {
2367  AVCodecInternal *avci = s->internal;
2368  int i, j;
2369 
2370  if (!avci->buffer)
2371  return;
2372 
2373  if (avci->buffer_count)
2374  av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n",
2375  avci->buffer_count);
2376  for (i = 0; i < INTERNAL_BUFFER_SIZE; i++) {
2377  InternalBuffer *buf = &avci->buffer[i];
2378  for (j = 0; j < 4; j++) {
2379  av_freep(&buf->base[j]);
2380  buf->data[j] = NULL;
2381  }
2382  }
2383  av_freep(&avci->buffer);
2384 
2385  avci->buffer_count = 0;
2386 }
2387 
2389 {
2390  AVCodecInternal *avci = avctx->internal;
2391  av_freep(&avci->audio_data);
2392 }
2393 
2395 {
2396  switch (avctx->codec_type) {
2397  case AVMEDIA_TYPE_VIDEO:
2398  video_free_buffers(avctx);
2399  break;
2400  case AVMEDIA_TYPE_AUDIO:
2401  audio_free_buffers(avctx);
2402  break;
2403  default:
2404  break;
2405  }
2406 }
2407 
2409 {
2410  switch (codec_id) {
2411  case AV_CODEC_ID_8SVX_EXP:
2412  case AV_CODEC_ID_8SVX_FIB:
2413  case AV_CODEC_ID_ADPCM_CT:
2420  return 4;
2421  case AV_CODEC_ID_PCM_ALAW:
2422  case AV_CODEC_ID_PCM_MULAW:
2423  case AV_CODEC_ID_PCM_S8:
2425  case AV_CODEC_ID_PCM_U8:
2426  case AV_CODEC_ID_PCM_ZORK:
2427  return 8;
2428  case AV_CODEC_ID_PCM_S16BE:
2430  case AV_CODEC_ID_PCM_S16LE:
2432  case AV_CODEC_ID_PCM_U16BE:
2433  case AV_CODEC_ID_PCM_U16LE:
2434  return 16;
2436  case AV_CODEC_ID_PCM_S24BE:
2437  case AV_CODEC_ID_PCM_S24LE:
2439  case AV_CODEC_ID_PCM_U24BE:
2440  case AV_CODEC_ID_PCM_U24LE:
2441  return 24;
2442  case AV_CODEC_ID_PCM_S32BE:
2443  case AV_CODEC_ID_PCM_S32LE:
2445  case AV_CODEC_ID_PCM_U32BE:
2446  case AV_CODEC_ID_PCM_U32LE:
2447  case AV_CODEC_ID_PCM_F32BE:
2448  case AV_CODEC_ID_PCM_F32LE:
2449  return 32;
2450  case AV_CODEC_ID_PCM_F64BE:
2451  case AV_CODEC_ID_PCM_F64LE:
2452  return 64;
2453  default:
2454  return 0;
2455  }
2456 }
2457 
2459 {
2460  static const enum AVCodecID map[AV_SAMPLE_FMT_NB][2] = {
2471  };
2472  if (fmt < 0 || fmt >= AV_SAMPLE_FMT_NB)
2473  return AV_CODEC_ID_NONE;
2474  if (be < 0 || be > 1)
2475  be = AV_NE(1, 0);
2476  return map[fmt][be];
2477 }
2478 
2480 {
2481  switch (codec_id) {
2483  return 2;
2485  return 3;
2489  case AV_CODEC_ID_ADPCM_SWF:
2490  case AV_CODEC_ID_ADPCM_MS:
2491  return 4;
2492  default:
2493  return av_get_exact_bits_per_sample(codec_id);
2494  }
2495 }
2496 
2497 int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
2498 {
2499  int id, sr, ch, ba, tag, bps;
2500 
2501  id = avctx->codec_id;
2502  sr = avctx->sample_rate;
2503  ch = avctx->channels;
2504  ba = avctx->block_align;
2505  tag = avctx->codec_tag;
2506  bps = av_get_exact_bits_per_sample(avctx->codec_id);
2507 
2508  /* codecs with an exact constant bits per sample */
2509  if (bps > 0 && ch > 0 && frame_bytes > 0 && ch < 32768 && bps < 32768)
2510  return (frame_bytes * 8LL) / (bps * ch);
2511  bps = avctx->bits_per_coded_sample;
2512 
2513  /* codecs with a fixed packet duration */
2514  switch (id) {
2515  case AV_CODEC_ID_ADPCM_ADX: return 32;
2516  case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
2517  case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
2518  case AV_CODEC_ID_AMR_NB:
2519  case AV_CODEC_ID_EVRC:
2520  case AV_CODEC_ID_GSM:
2521  case AV_CODEC_ID_QCELP:
2522  case AV_CODEC_ID_RA_288: return 160;
2523  case AV_CODEC_ID_AMR_WB:
2524  case AV_CODEC_ID_GSM_MS: return 320;
2525  case AV_CODEC_ID_MP1: return 384;
2526  case AV_CODEC_ID_ATRAC1: return 512;
2527  case AV_CODEC_ID_ATRAC3: return 1024;
2528  case AV_CODEC_ID_MP2:
2529  case AV_CODEC_ID_MUSEPACK7: return 1152;
2530  case AV_CODEC_ID_AC3: return 1536;
2531  }
2532 
2533  if (sr > 0) {
2534  /* calc from sample rate */
2535  if (id == AV_CODEC_ID_TTA)
2536  return 256 * sr / 245;
2537 
2538  if (ch > 0) {
2539  /* calc from sample rate and channels */
2540  if (id == AV_CODEC_ID_BINKAUDIO_DCT)
2541  return (480 << (sr / 22050)) / ch;
2542  }
2543  }
2544 
2545  if (ba > 0) {
2546  /* calc from block_align */
2547  if (id == AV_CODEC_ID_SIPR) {
2548  switch (ba) {
2549  case 20: return 160;
2550  case 19: return 144;
2551  case 29: return 288;
2552  case 37: return 480;
2553  }
2554  } else if (id == AV_CODEC_ID_ILBC) {
2555  switch (ba) {
2556  case 38: return 160;
2557  case 50: return 240;
2558  }
2559  }
2560  }
2561 
2562  if (frame_bytes > 0) {
2563  /* calc from frame_bytes only */
2564  if (id == AV_CODEC_ID_TRUESPEECH)
2565  return 240 * (frame_bytes / 32);
2566  if (id == AV_CODEC_ID_NELLYMOSER)
2567  return 256 * (frame_bytes / 64);
2568  if (id == AV_CODEC_ID_RA_144)
2569  return 160 * (frame_bytes / 20);
2570  if (id == AV_CODEC_ID_G723_1)
2571  return 240 * (frame_bytes / 24);
2572 
2573  if (bps > 0) {
2574  /* calc from frame_bytes and bits_per_coded_sample */
2575  if (id == AV_CODEC_ID_ADPCM_G726)
2576  return frame_bytes * 8 / bps;
2577  }
2578 
2579  if (ch > 0) {
2580  /* calc from frame_bytes and channels */
2581  switch (id) {
2582  case AV_CODEC_ID_ADPCM_AFC:
2583  return frame_bytes / (9 * ch) * 16;
2584  case AV_CODEC_ID_ADPCM_4XM:
2586  return (frame_bytes - 4 * ch) * 2 / ch;
2588  return (frame_bytes - 4) * 2 / ch;
2590  return (frame_bytes - 8) * 2 / ch;
2591  case AV_CODEC_ID_ADPCM_XA:
2592  return (frame_bytes / 128) * 224 / ch;
2594  return (frame_bytes - 6 - ch) / ch;
2595  case AV_CODEC_ID_ROQ_DPCM:
2596  return (frame_bytes - 8) / ch;
2597  case AV_CODEC_ID_XAN_DPCM:
2598  return (frame_bytes - 2 * ch) / ch;
2599  case AV_CODEC_ID_MACE3:
2600  return 3 * frame_bytes / ch;
2601  case AV_CODEC_ID_MACE6:
2602  return 6 * frame_bytes / ch;
2603  case AV_CODEC_ID_PCM_LXF:
2604  return 2 * (frame_bytes / (5 * ch));
2605  case AV_CODEC_ID_IAC:
2606  case AV_CODEC_ID_IMC:
2607  return 4 * frame_bytes / ch;
2608  }
2609 
2610  if (tag) {
2611  /* calc from frame_bytes, channels, and codec_tag */
2612  if (id == AV_CODEC_ID_SOL_DPCM) {
2613  if (tag == 3)
2614  return frame_bytes / ch;
2615  else
2616  return frame_bytes * 2 / ch;
2617  }
2618  }
2619 
2620  if (ba > 0) {
2621  /* calc from frame_bytes, channels, and block_align */
2622  int blocks = frame_bytes / ba;
2623  switch (avctx->codec_id) {
2625  return blocks * (1 + (ba - 4 * ch) / (4 * ch) * 8);
2627  return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
2629  return blocks * (1 + (ba - 4 * ch) * 2 / ch);
2630  case AV_CODEC_ID_ADPCM_MS:
2631  return blocks * (2 + (ba - 7 * ch) * 2 / ch);
2632  }
2633  }
2634 
2635  if (bps > 0) {
2636  /* calc from frame_bytes, channels, and bits_per_coded_sample */
2637  switch (avctx->codec_id) {
2638  case AV_CODEC_ID_PCM_DVD:
2639  if(bps<4)
2640  return 0;
2641  return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
2643  if(bps<4)
2644  return 0;
2645  return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
2646  case AV_CODEC_ID_S302M:
2647  return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
2648  }
2649  }
2650  }
2651  }
2652 
2653  return 0;
2654 }
2655 
2656 #if !HAVE_THREADS
2658 {
2659  return -1;
2660 }
2661 
2662 #endif
2663 
2664 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
2665 {
2666  unsigned int n = 0;
2667 
2668  while (v >= 0xff) {
2669  *s++ = 0xff;
2670  v -= 0xff;
2671  n++;
2672  }
2673  *s = v;
2674  n++;
2675  return n;
2676 }
2677 
2678 int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
2679 {
2680  int i;
2681  for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
2682  return i;
2683 }
2684 
2685 void av_log_missing_feature(void *avc, const char *feature, int want_sample)
2686 {
2687  av_log(avc, AV_LOG_WARNING, "%s is not implemented. Update your FFmpeg "
2688  "version to the newest one from Git. If the problem still "
2689  "occurs, it means that your file has a feature which has not "
2690  "been implemented.\n", feature);
2691  if(want_sample)
2693 }
2694 
2695 void av_log_ask_for_sample(void *avc, const char *msg, ...)
2696 {
2697  va_list argument_list;
2698 
2699  va_start(argument_list, msg);
2700 
2701  if (msg)
2702  av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
2703  av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
2704  "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
2705  "and contact the ffmpeg-devel mailing list.\n");
2706 
2707  va_end(argument_list);
2708 }
2709 
2711 
2713 {
2714  AVHWAccel **p = &first_hwaccel;
2715  while (*p)
2716  p = &(*p)->next;
2717  *p = hwaccel;
2718  hwaccel->next = NULL;
2719 }
2720 
2722 {
2723  return hwaccel ? hwaccel->next : first_hwaccel;
2724 }
2725 
2727 {
2728  AVHWAccel *hwaccel = NULL;
2729 
2730  while ((hwaccel = av_hwaccel_next(hwaccel)))
2731  if (hwaccel->id == codec_id
2732  && hwaccel->pix_fmt == pix_fmt)
2733  return hwaccel;
2734  return NULL;
2735 }
2736 
2737 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
2738 {
2739  if (ff_lockmgr_cb) {
2741  return -1;
2743  return -1;
2744  }
2745 
2746  ff_lockmgr_cb = cb;
2747 
2748  if (ff_lockmgr_cb) {
2750  return -1;
2752  return -1;
2753  }
2754  return 0;
2755 }
2756 
2758 {
2759  if (ff_lockmgr_cb) {
2761  return -1;
2762  }
2764  if (entangled_thread_counter != 1) {
2765  av_log(log_ctx, AV_LOG_ERROR, "Insufficient thread locking around avcodec_open/close()\n");
2766  ff_avcodec_locked = 1;
2768  return AVERROR(EINVAL);
2769  }
2771  ff_avcodec_locked = 1;
2772  return 0;
2773 }
2774 
2776 {
2778  ff_avcodec_locked = 0;
2780  if (ff_lockmgr_cb) {
2782  return -1;
2783  }
2784  return 0;
2785 }
2786 
2788 {
2789  if (ff_lockmgr_cb) {
2791  return -1;
2792  }
2793  return 0;
2794 }
2795 
2797 {
2798  if (ff_lockmgr_cb) {
2800  return -1;
2801  }
2802  return 0;
2803 }
2804 
2805 unsigned int avpriv_toupper4(unsigned int x)
2806 {
2807  return av_toupper(x & 0xFF)
2808  + (av_toupper((x >> 8) & 0xFF) << 8)
2809  + (av_toupper((x >> 16) & 0xFF) << 16)
2810  + (av_toupper((x >> 24) & 0xFF) << 24);
2811 }
2812 
2813 #if !HAVE_THREADS
2814 
2816 {
2817  f->owner = avctx;
2818 
2819  return ff_get_buffer(avctx, f);
2820 }
2821 
2823 {
2824  f->owner->release_buffer(f->owner, f);
2825 }
2826 
2828 {
2829 }
2830 
2831 void ff_thread_report_progress(AVFrame *f, int progress, int field)
2832 {
2833 }
2834 
2835 void ff_thread_await_progress(AVFrame *f, int progress, int field)
2836 {
2837 }
2838 
2840 {
2841  return 1;
2842 }
2843 
2844 #endif
2845 
2847 {
2848  AVCodec *c= avcodec_find_decoder(codec_id);
2849  if(!c)
2850  c= avcodec_find_encoder(codec_id);
2851  if(c)
2852  return c->type;
2853 
2854  if (codec_id <= AV_CODEC_ID_NONE)
2855  return AVMEDIA_TYPE_UNKNOWN;
2856  else if (codec_id < AV_CODEC_ID_FIRST_AUDIO)
2857  return AVMEDIA_TYPE_VIDEO;
2858  else if (codec_id < AV_CODEC_ID_FIRST_SUBTITLE)
2859  return AVMEDIA_TYPE_AUDIO;
2860  else if (codec_id < AV_CODEC_ID_FIRST_UNKNOWN)
2861  return AVMEDIA_TYPE_SUBTITLE;
2862 
2863  return AVMEDIA_TYPE_UNKNOWN;
2864 }
2865 
2867 {
2868  return !!s->internal;
2869 }
2870 
2872 {
2873  int ret;
2874  char *str;
2875 
2876  ret = av_bprint_finalize(buf, &str);
2877  if (ret < 0)
2878  return ret;
2879  if (!av_bprint_is_complete(buf)) {
2880  av_free(str);
2881  return AVERROR(ENOMEM);
2882  }
2883 
2884  avctx->extradata = str;
2885  /* Note: the string is NUL terminated (so extradata can be read as a
2886  * string), but the ending character is not accounted in the size (in
2887  * binary formats you are likely not supposed to mux that character). When
2888  * extradata is copied, it is also padded with FF_INPUT_BUFFER_PADDING_SIZE
2889  * zeros. */
2890  avctx->extradata_size = buf->len;
2891  return 0;
2892 }