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