Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * utils for libavcodec
  3.  * Copyright (c) 2001 Fabrice Bellard
  4.  * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
  5.  *
  6.  * This file is part of FFmpeg.
  7.  *
  8.  * FFmpeg is free software; you can redistribute it and/or
  9.  * modify it under the terms of the GNU Lesser General Public
  10.  * License as published by the Free Software Foundation; either
  11.  * version 2.1 of the License, or (at your option) any later version.
  12.  *
  13.  * FFmpeg is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.  * Lesser General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU Lesser General Public
  19.  * License along with FFmpeg; if not, write to the Free Software
  20.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21.  */
  22.  
  23. /**
  24.  * @file
  25.  * utils.
  26.  */
  27.  
  28. #include "config.h"
  29. #include "libavutil/atomic.h"
  30. #include "libavutil/attributes.h"
  31. #include "libavutil/avassert.h"
  32. #include "libavutil/avstring.h"
  33. #include "libavutil/bprint.h"
  34. #include "libavutil/channel_layout.h"
  35. #include "libavutil/crc.h"
  36. #include "libavutil/frame.h"
  37. #include "libavutil/internal.h"
  38. #include "libavutil/mathematics.h"
  39. #include "libavutil/pixdesc.h"
  40. #include "libavutil/imgutils.h"
  41. #include "libavutil/samplefmt.h"
  42. #include "libavutil/dict.h"
  43. #include "avcodec.h"
  44. #include "dsputil.h"
  45. #include "libavutil/opt.h"
  46. #include "thread.h"
  47. #include "frame_thread_encoder.h"
  48. #include "internal.h"
  49. #include "bytestream.h"
  50. #include "version.h"
  51. #include <stdlib.h>
  52. #include <stdarg.h>
  53. #include <limits.h>
  54. #include <float.h>
  55. #if CONFIG_ICONV
  56. # include <iconv.h>
  57. #endif
  58.  
  59. #if HAVE_PTHREADS
  60. #include <pthread.h>
  61. #elif HAVE_W32THREADS
  62. #include "compat/w32pthreads.h"
  63. #elif HAVE_OS2THREADS
  64. #include "compat/os2threads.h"
  65. #endif
  66.  
  67. #if HAVE_PTHREADS || HAVE_W32THREADS || HAVE_OS2THREADS
  68. static int default_lockmgr_cb(void **arg, enum AVLockOp op)
  69. {
  70.     void * volatile * mutex = arg;
  71.     int err;
  72.  
  73.     switch (op) {
  74.     case AV_LOCK_CREATE:
  75.         return 0;
  76.     case AV_LOCK_OBTAIN:
  77.         if (!*mutex) {
  78.             pthread_mutex_t *tmp = av_malloc(sizeof(pthread_mutex_t));
  79.             if (!tmp)
  80.                 return AVERROR(ENOMEM);
  81.             if ((err = pthread_mutex_init(tmp, NULL))) {
  82.                 av_free(tmp);
  83.                 return AVERROR(err);
  84.             }
  85.             if (avpriv_atomic_ptr_cas(mutex, NULL, tmp)) {
  86.                 pthread_mutex_destroy(tmp);
  87.                 av_free(tmp);
  88.             }
  89.         }
  90.  
  91.         if ((err = pthread_mutex_lock(*mutex)))
  92.             return AVERROR(err);
  93.  
  94.         return 0;
  95.     case AV_LOCK_RELEASE:
  96.         if ((err = pthread_mutex_unlock(*mutex)))
  97.             return AVERROR(err);
  98.  
  99.         return 0;
  100.     case AV_LOCK_DESTROY:
  101.         if (*mutex)
  102.             pthread_mutex_destroy(*mutex);
  103.         av_free(*mutex);
  104.         avpriv_atomic_ptr_cas(mutex, *mutex, NULL);
  105.         return 0;
  106.     }
  107.     return 1;
  108. }
  109. static int (*lockmgr_cb)(void **mutex, enum AVLockOp op) = default_lockmgr_cb;
  110. #else
  111. static int (*lockmgr_cb)(void **mutex, enum AVLockOp op) = NULL;
  112. #endif
  113.  
  114.  
  115. volatile int ff_avcodec_locked;
  116. static int volatile entangled_thread_counter = 0;
  117. static void *codec_mutex;
  118. static void *avformat_mutex;
  119.  
  120. void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
  121. {
  122.     if (min_size < *size)
  123.         return ptr;
  124.  
  125.     min_size = FFMAX(17 * min_size / 16 + 32, min_size);
  126.  
  127.     ptr = av_realloc(ptr, min_size);
  128.     /* we could set this to the unmodified min_size but this is safer
  129.      * if the user lost the ptr and uses NULL now
  130.      */
  131.     if (!ptr)
  132.         min_size = 0;
  133.  
  134.     *size = min_size;
  135.  
  136.     return ptr;
  137. }
  138.  
  139. static inline int ff_fast_malloc(void *ptr, unsigned int *size, size_t min_size, int zero_realloc)
  140. {
  141.     void **p = ptr;
  142.     if (min_size < *size)
  143.         return 0;
  144.     min_size = FFMAX(17 * min_size / 16 + 32, min_size);
  145.     av_free(*p);
  146.     *p = zero_realloc ? av_mallocz(min_size) : av_malloc(min_size);
  147.     if (!*p)
  148.         min_size = 0;
  149.     *size = min_size;
  150.     return 1;
  151. }
  152.  
  153. void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
  154. {
  155.     ff_fast_malloc(ptr, size, min_size, 0);
  156. }
  157.  
  158. void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
  159. {
  160.     uint8_t **p = ptr;
  161.     if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
  162.         av_freep(p);
  163.         *size = 0;
  164.         return;
  165.     }
  166.     if (!ff_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE, 1))
  167.         memset(*p + min_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  168. }
  169.  
  170. void av_fast_padded_mallocz(void *ptr, unsigned int *size, size_t min_size)
  171. {
  172.     uint8_t **p = ptr;
  173.     if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
  174.         av_freep(p);
  175.         *size = 0;
  176.         return;
  177.     }
  178.     if (!ff_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE, 1))
  179.         memset(*p, 0, min_size + FF_INPUT_BUFFER_PADDING_SIZE);
  180. }
  181.  
  182. /* encoder management */
  183. static AVCodec *first_avcodec = NULL;
  184.  
  185. AVCodec *av_codec_next(const AVCodec *c)
  186. {
  187.     if (c)
  188.         return c->next;
  189.     else
  190.         return first_avcodec;
  191. }
  192.  
  193. static av_cold void avcodec_init(void)
  194. {
  195.     static int initialized = 0;
  196.  
  197.     if (initialized != 0)
  198.         return;
  199.     initialized = 1;
  200.  
  201.     if (CONFIG_DSPUTIL)
  202.         ff_dsputil_static_init();
  203. }
  204.  
  205. int av_codec_is_encoder(const AVCodec *codec)
  206. {
  207.     return codec && (codec->encode_sub || codec->encode2);
  208. }
  209.  
  210. int av_codec_is_decoder(const AVCodec *codec)
  211. {
  212.     return codec && codec->decode;
  213. }
  214.  
  215. av_cold void avcodec_register(AVCodec *codec)
  216. {
  217.     AVCodec **p;
  218.     avcodec_init();
  219.     p = &first_avcodec;
  220.     codec->next = NULL;
  221.     while(avpriv_atomic_ptr_cas((void * volatile *)p, NULL, codec))
  222.         p = &(*p)->next;
  223.  
  224.     if (codec->init_static_data)
  225.         codec->init_static_data(codec);
  226. }
  227.  
  228. unsigned avcodec_get_edge_width(void)
  229. {
  230.     return EDGE_WIDTH;
  231. }
  232.  
  233. void avcodec_set_dimensions(AVCodecContext *s, int width, int height)
  234. {
  235.     s->coded_width  = width;
  236.     s->coded_height = height;
  237.     s->width        = FF_CEIL_RSHIFT(width,  s->lowres);
  238.     s->height       = FF_CEIL_RSHIFT(height, s->lowres);
  239. }
  240.  
  241. #if HAVE_NEON || ARCH_PPC || HAVE_MMX
  242. #   define STRIDE_ALIGN 16
  243. #else
  244. #   define STRIDE_ALIGN 8
  245. #endif
  246.  
  247. void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
  248.                                int linesize_align[AV_NUM_DATA_POINTERS])
  249. {
  250.     int i;
  251.     int w_align = 1;
  252.     int h_align = 1;
  253.  
  254.     switch (s->pix_fmt) {
  255.     case AV_PIX_FMT_YUV420P:
  256.     case AV_PIX_FMT_YUYV422:
  257.     case AV_PIX_FMT_UYVY422:
  258.     case AV_PIX_FMT_YUV422P:
  259.     case AV_PIX_FMT_YUV440P:
  260.     case AV_PIX_FMT_YUV444P:
  261.     case AV_PIX_FMT_GBRAP:
  262.     case AV_PIX_FMT_GBRP:
  263.     case AV_PIX_FMT_GRAY8:
  264.     case AV_PIX_FMT_GRAY16BE:
  265.     case AV_PIX_FMT_GRAY16LE:
  266.     case AV_PIX_FMT_YUVJ420P:
  267.     case AV_PIX_FMT_YUVJ422P:
  268.     case AV_PIX_FMT_YUVJ440P:
  269.     case AV_PIX_FMT_YUVJ444P:
  270.     case AV_PIX_FMT_YUVA420P:
  271.     case AV_PIX_FMT_YUVA422P:
  272.     case AV_PIX_FMT_YUVA444P:
  273.     case AV_PIX_FMT_YUV420P9LE:
  274.     case AV_PIX_FMT_YUV420P9BE:
  275.     case AV_PIX_FMT_YUV420P10LE:
  276.     case AV_PIX_FMT_YUV420P10BE:
  277.     case AV_PIX_FMT_YUV420P12LE:
  278.     case AV_PIX_FMT_YUV420P12BE:
  279.     case AV_PIX_FMT_YUV420P14LE:
  280.     case AV_PIX_FMT_YUV420P14BE:
  281.     case AV_PIX_FMT_YUV420P16LE:
  282.     case AV_PIX_FMT_YUV420P16BE:
  283.     case AV_PIX_FMT_YUV422P9LE:
  284.     case AV_PIX_FMT_YUV422P9BE:
  285.     case AV_PIX_FMT_YUV422P10LE:
  286.     case AV_PIX_FMT_YUV422P10BE:
  287.     case AV_PIX_FMT_YUV422P12LE:
  288.     case AV_PIX_FMT_YUV422P12BE:
  289.     case AV_PIX_FMT_YUV422P14LE:
  290.     case AV_PIX_FMT_YUV422P14BE:
  291.     case AV_PIX_FMT_YUV422P16LE:
  292.     case AV_PIX_FMT_YUV422P16BE:
  293.     case AV_PIX_FMT_YUV444P9LE:
  294.     case AV_PIX_FMT_YUV444P9BE:
  295.     case AV_PIX_FMT_YUV444P10LE:
  296.     case AV_PIX_FMT_YUV444P10BE:
  297.     case AV_PIX_FMT_YUV444P12LE:
  298.     case AV_PIX_FMT_YUV444P12BE:
  299.     case AV_PIX_FMT_YUV444P14LE:
  300.     case AV_PIX_FMT_YUV444P14BE:
  301.     case AV_PIX_FMT_YUV444P16LE:
  302.     case AV_PIX_FMT_YUV444P16BE:
  303.     case AV_PIX_FMT_YUVA420P9LE:
  304.     case AV_PIX_FMT_YUVA420P9BE:
  305.     case AV_PIX_FMT_YUVA420P10LE:
  306.     case AV_PIX_FMT_YUVA420P10BE:
  307.     case AV_PIX_FMT_YUVA420P16LE:
  308.     case AV_PIX_FMT_YUVA420P16BE:
  309.     case AV_PIX_FMT_YUVA422P9LE:
  310.     case AV_PIX_FMT_YUVA422P9BE:
  311.     case AV_PIX_FMT_YUVA422P10LE:
  312.     case AV_PIX_FMT_YUVA422P10BE:
  313.     case AV_PIX_FMT_YUVA422P16LE:
  314.     case AV_PIX_FMT_YUVA422P16BE:
  315.     case AV_PIX_FMT_YUVA444P9LE:
  316.     case AV_PIX_FMT_YUVA444P9BE:
  317.     case AV_PIX_FMT_YUVA444P10LE:
  318.     case AV_PIX_FMT_YUVA444P10BE:
  319.     case AV_PIX_FMT_YUVA444P16LE:
  320.     case AV_PIX_FMT_YUVA444P16BE:
  321.     case AV_PIX_FMT_GBRP9LE:
  322.     case AV_PIX_FMT_GBRP9BE:
  323.     case AV_PIX_FMT_GBRP10LE:
  324.     case AV_PIX_FMT_GBRP10BE:
  325.     case AV_PIX_FMT_GBRP12LE:
  326.     case AV_PIX_FMT_GBRP12BE:
  327.     case AV_PIX_FMT_GBRP14LE:
  328.     case AV_PIX_FMT_GBRP14BE:
  329.         w_align = 16; //FIXME assume 16 pixel per macroblock
  330.         h_align = 16 * 2; // interlaced needs 2 macroblocks height
  331.         break;
  332.     case AV_PIX_FMT_YUV411P:
  333.     case AV_PIX_FMT_YUVJ411P:
  334.     case AV_PIX_FMT_UYYVYY411:
  335.         w_align = 32;
  336.         h_align = 8;
  337.         break;
  338.     case AV_PIX_FMT_YUV410P:
  339.         if (s->codec_id == AV_CODEC_ID_SVQ1) {
  340.             w_align = 64;
  341.             h_align = 64;
  342.         }
  343.         break;
  344.     case AV_PIX_FMT_RGB555:
  345.         if (s->codec_id == AV_CODEC_ID_RPZA) {
  346.             w_align = 4;
  347.             h_align = 4;
  348.         }
  349.         break;
  350.     case AV_PIX_FMT_PAL8:
  351.     case AV_PIX_FMT_BGR8:
  352.     case AV_PIX_FMT_RGB8:
  353.         if (s->codec_id == AV_CODEC_ID_SMC ||
  354.             s->codec_id == AV_CODEC_ID_CINEPAK) {
  355.             w_align = 4;
  356.             h_align = 4;
  357.         }
  358.         break;
  359.     case AV_PIX_FMT_BGR24:
  360.         if ((s->codec_id == AV_CODEC_ID_MSZH) ||
  361.             (s->codec_id == AV_CODEC_ID_ZLIB)) {
  362.             w_align = 4;
  363.             h_align = 4;
  364.         }
  365.         break;
  366.     case AV_PIX_FMT_RGB24:
  367.         if (s->codec_id == AV_CODEC_ID_CINEPAK) {
  368.             w_align = 4;
  369.             h_align = 4;
  370.         }
  371.         break;
  372.     default:
  373.         w_align = 1;
  374.         h_align = 1;
  375.         break;
  376.     }
  377.  
  378.     if (s->codec_id == AV_CODEC_ID_IFF_ILBM || s->codec_id == AV_CODEC_ID_IFF_BYTERUN1) {
  379.         w_align = FFMAX(w_align, 8);
  380.     }
  381.  
  382.     *width  = FFALIGN(*width, w_align);
  383.     *height = FFALIGN(*height, h_align);
  384.     if (s->codec_id == AV_CODEC_ID_H264 || s->lowres)
  385.         // some of the optimized chroma MC reads one line too much
  386.         // which is also done in mpeg decoders with lowres > 0
  387.         *height += 2;
  388.  
  389.     for (i = 0; i < 4; i++)
  390.         linesize_align[i] = STRIDE_ALIGN;
  391. }
  392.  
  393. void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height)
  394. {
  395.     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
  396.     int chroma_shift = desc->log2_chroma_w;
  397.     int linesize_align[AV_NUM_DATA_POINTERS];
  398.     int align;
  399.  
  400.     avcodec_align_dimensions2(s, width, height, linesize_align);
  401.     align               = FFMAX(linesize_align[0], linesize_align[3]);
  402.     linesize_align[1] <<= chroma_shift;
  403.     linesize_align[2] <<= chroma_shift;
  404.     align               = FFMAX3(align, linesize_align[1], linesize_align[2]);
  405.     *width              = FFALIGN(*width, align);
  406. }
  407.  
  408. int avcodec_enum_to_chroma_pos(int *xpos, int *ypos, enum AVChromaLocation pos)
  409. {
  410.     if (pos <= AVCHROMA_LOC_UNSPECIFIED || pos >= AVCHROMA_LOC_NB)
  411.         return AVERROR(EINVAL);
  412.     pos--;
  413.  
  414.     *xpos = (pos&1) * 128;
  415.     *ypos = ((pos>>1)^(pos<4)) * 128;
  416.  
  417.     return 0;
  418. }
  419.  
  420. enum AVChromaLocation avcodec_chroma_pos_to_enum(int xpos, int ypos)
  421. {
  422.     int pos, xout, yout;
  423.  
  424.     for (pos = AVCHROMA_LOC_UNSPECIFIED + 1; pos < AVCHROMA_LOC_NB; pos++) {
  425.         if (avcodec_enum_to_chroma_pos(&xout, &yout, pos) == 0 && xout == xpos && yout == ypos)
  426.             return pos;
  427.     }
  428.     return AVCHROMA_LOC_UNSPECIFIED;
  429. }
  430.  
  431. int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
  432.                              enum AVSampleFormat sample_fmt, const uint8_t *buf,
  433.                              int buf_size, int align)
  434. {
  435.     int ch, planar, needed_size, ret = 0;
  436.  
  437.     needed_size = av_samples_get_buffer_size(NULL, nb_channels,
  438.                                              frame->nb_samples, sample_fmt,
  439.                                              align);
  440.     if (buf_size < needed_size)
  441.         return AVERROR(EINVAL);
  442.  
  443.     planar = av_sample_fmt_is_planar(sample_fmt);
  444.     if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
  445.         if (!(frame->extended_data = av_mallocz(nb_channels *
  446.                                                 sizeof(*frame->extended_data))))
  447.             return AVERROR(ENOMEM);
  448.     } else {
  449.         frame->extended_data = frame->data;
  450.     }
  451.  
  452.     if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
  453.                                       (uint8_t *)(intptr_t)buf, nb_channels, frame->nb_samples,
  454.                                       sample_fmt, align)) < 0) {
  455.         if (frame->extended_data != frame->data)
  456.             av_freep(&frame->extended_data);
  457.         return ret;
  458.     }
  459.     if (frame->extended_data != frame->data) {
  460.         for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
  461.             frame->data[ch] = frame->extended_data[ch];
  462.     }
  463.  
  464.     return ret;
  465. }
  466.  
  467. static int update_frame_pool(AVCodecContext *avctx, AVFrame *frame)
  468. {
  469.     FramePool *pool = avctx->internal->pool;
  470.     int i, ret;
  471.  
  472.     switch (avctx->codec_type) {
  473.     case AVMEDIA_TYPE_VIDEO: {
  474.         AVPicture picture;
  475.         int size[4] = { 0 };
  476.         int w = frame->width;
  477.         int h = frame->height;
  478.         int tmpsize, unaligned;
  479.  
  480.         if (pool->format == frame->format &&
  481.             pool->width == frame->width && pool->height == frame->height)
  482.             return 0;
  483.  
  484.         avcodec_align_dimensions2(avctx, &w, &h, pool->stride_align);
  485.  
  486.         if (!(avctx->flags & CODEC_FLAG_EMU_EDGE)) {
  487.             w += EDGE_WIDTH * 2;
  488.             h += EDGE_WIDTH * 2;
  489.         }
  490.  
  491.         do {
  492.             // NOTE: do not align linesizes individually, this breaks e.g. assumptions
  493.             // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
  494.             av_image_fill_linesizes(picture.linesize, avctx->pix_fmt, w);
  495.             // increase alignment of w for next try (rhs gives the lowest bit set in w)
  496.             w += w & ~(w - 1);
  497.  
  498.             unaligned = 0;
  499.             for (i = 0; i < 4; i++)
  500.                 unaligned |= picture.linesize[i] % pool->stride_align[i];
  501.         } while (unaligned);
  502.  
  503.         tmpsize = av_image_fill_pointers(picture.data, avctx->pix_fmt, h,
  504.                                          NULL, picture.linesize);
  505.         if (tmpsize < 0)
  506.             return -1;
  507.  
  508.         for (i = 0; i < 3 && picture.data[i + 1]; i++)
  509.             size[i] = picture.data[i + 1] - picture.data[i];
  510.         size[i] = tmpsize - (picture.data[i] - picture.data[0]);
  511.  
  512.         for (i = 0; i < 4; i++) {
  513.             av_buffer_pool_uninit(&pool->pools[i]);
  514.             pool->linesize[i] = picture.linesize[i];
  515.             if (size[i]) {
  516.                 pool->pools[i] = av_buffer_pool_init(size[i] + 16 + STRIDE_ALIGN - 1,
  517.                                                      CONFIG_MEMORY_POISONING ?
  518.                                                         NULL :
  519.                                                         av_buffer_allocz);
  520.                 if (!pool->pools[i]) {
  521.                     ret = AVERROR(ENOMEM);
  522.                     goto fail;
  523.                 }
  524.             }
  525.         }
  526.         pool->format = frame->format;
  527.         pool->width  = frame->width;
  528.         pool->height = frame->height;
  529.  
  530.         break;
  531.         }
  532.     case AVMEDIA_TYPE_AUDIO: {
  533.         int ch     = av_frame_get_channels(frame); //av_get_channel_layout_nb_channels(frame->channel_layout);
  534.         int planar = av_sample_fmt_is_planar(frame->format);
  535.         int planes = planar ? ch : 1;
  536.  
  537.         if (pool->format == frame->format && pool->planes == planes &&
  538.             pool->channels == ch && frame->nb_samples == pool->samples)
  539.             return 0;
  540.  
  541.         av_buffer_pool_uninit(&pool->pools[0]);
  542.         ret = av_samples_get_buffer_size(&pool->linesize[0], ch,
  543.                                          frame->nb_samples, frame->format, 0);
  544.         if (ret < 0)
  545.             goto fail;
  546.  
  547.         pool->pools[0] = av_buffer_pool_init(pool->linesize[0], NULL);
  548.         if (!pool->pools[0]) {
  549.             ret = AVERROR(ENOMEM);
  550.             goto fail;
  551.         }
  552.  
  553.         pool->format     = frame->format;
  554.         pool->planes     = planes;
  555.         pool->channels   = ch;
  556.         pool->samples = frame->nb_samples;
  557.         break;
  558.         }
  559.     default: av_assert0(0);
  560.     }
  561.     return 0;
  562. fail:
  563.     for (i = 0; i < 4; i++)
  564.         av_buffer_pool_uninit(&pool->pools[i]);
  565.     pool->format = -1;
  566.     pool->planes = pool->channels = pool->samples = 0;
  567.     pool->width  = pool->height = 0;
  568.     return ret;
  569. }
  570.  
  571. static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  572. {
  573.     FramePool *pool = avctx->internal->pool;
  574.     int planes = pool->planes;
  575.     int i;
  576.  
  577.     frame->linesize[0] = pool->linesize[0];
  578.  
  579.     if (planes > AV_NUM_DATA_POINTERS) {
  580.         frame->extended_data = av_mallocz(planes * sizeof(*frame->extended_data));
  581.         frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
  582.         frame->extended_buf  = av_mallocz(frame->nb_extended_buf *
  583.                                           sizeof(*frame->extended_buf));
  584.         if (!frame->extended_data || !frame->extended_buf) {
  585.             av_freep(&frame->extended_data);
  586.             av_freep(&frame->extended_buf);
  587.             return AVERROR(ENOMEM);
  588.         }
  589.     } else {
  590.         frame->extended_data = frame->data;
  591.         av_assert0(frame->nb_extended_buf == 0);
  592.     }
  593.  
  594.     for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
  595.         frame->buf[i] = av_buffer_pool_get(pool->pools[0]);
  596.         if (!frame->buf[i])
  597.             goto fail;
  598.         frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
  599.     }
  600.     for (i = 0; i < frame->nb_extended_buf; i++) {
  601.         frame->extended_buf[i] = av_buffer_pool_get(pool->pools[0]);
  602.         if (!frame->extended_buf[i])
  603.             goto fail;
  604.         frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
  605.     }
  606.  
  607.     if (avctx->debug & FF_DEBUG_BUFFERS)
  608.         av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p", frame);
  609.  
  610.     return 0;
  611. fail:
  612.     av_frame_unref(frame);
  613.     return AVERROR(ENOMEM);
  614. }
  615.  
  616. static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
  617. {
  618.     FramePool *pool = s->internal->pool;
  619.     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pic->format);
  620.     int pixel_size = desc->comp[0].step_minus1 + 1;
  621.     int h_chroma_shift, v_chroma_shift;
  622.     int i;
  623.  
  624.     if (pic->data[0] != NULL) {
  625.         av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
  626.         return -1;
  627.     }
  628.  
  629.     memset(pic->data, 0, sizeof(pic->data));
  630.     pic->extended_data = pic->data;
  631.  
  632.     av_pix_fmt_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  633.  
  634.     for (i = 0; i < 4 && pool->pools[i]; i++) {
  635.         const int h_shift = i == 0 ? 0 : h_chroma_shift;
  636.         const int v_shift = i == 0 ? 0 : v_chroma_shift;
  637.         int is_planar = pool->pools[2] || (i==0 && s->pix_fmt == AV_PIX_FMT_GRAY8);
  638.  
  639.         pic->linesize[i] = pool->linesize[i];
  640.  
  641.         pic->buf[i] = av_buffer_pool_get(pool->pools[i]);
  642.         if (!pic->buf[i])
  643.             goto fail;
  644.  
  645.         // no edge if EDGE EMU or not planar YUV
  646.         if ((s->flags & CODEC_FLAG_EMU_EDGE) || !is_planar)
  647.             pic->data[i] = pic->buf[i]->data;
  648.         else {
  649.             pic->data[i] = pic->buf[i]->data +
  650.                 FFALIGN((pic->linesize[i] * EDGE_WIDTH >> v_shift) +
  651.                         (pixel_size * EDGE_WIDTH >> h_shift), pool->stride_align[i]);
  652.         }
  653.     }
  654.     for (; i < AV_NUM_DATA_POINTERS; i++) {
  655.         pic->data[i] = NULL;
  656.         pic->linesize[i] = 0;
  657.     }
  658.     if (pic->data[1] && !pic->data[2])
  659.         avpriv_set_systematic_pal2((uint32_t *)pic->data[1], s->pix_fmt);
  660.  
  661.     if (s->debug & FF_DEBUG_BUFFERS)
  662.         av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p\n", pic);
  663.  
  664.     return 0;
  665. fail:
  666.     av_frame_unref(pic);
  667.     return AVERROR(ENOMEM);
  668. }
  669.  
  670. void avpriv_color_frame(AVFrame *frame, const int c[4])
  671. {
  672.     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
  673.     int p, y, x;
  674.  
  675.     av_assert0(desc->flags & AV_PIX_FMT_FLAG_PLANAR);
  676.  
  677.     for (p = 0; p<desc->nb_components; p++) {
  678.         uint8_t *dst = frame->data[p];
  679.         int is_chroma = p == 1 || p == 2;
  680.         int bytes  = is_chroma ? FF_CEIL_RSHIFT(frame->width,  desc->log2_chroma_w) : frame->width;
  681.         int height = is_chroma ? FF_CEIL_RSHIFT(frame->height, desc->log2_chroma_h) : frame->height;
  682.         for (y = 0; y < height; y++) {
  683.             if (desc->comp[0].depth_minus1 >= 8) {
  684.                 for (x = 0; x<bytes; x++)
  685.                     ((uint16_t*)dst)[x] = c[p];
  686.             }else
  687.                 memset(dst, c[p], bytes);
  688.             dst += frame->linesize[p];
  689.         }
  690.     }
  691. }
  692.  
  693. int avcodec_default_get_buffer2(AVCodecContext *avctx, AVFrame *frame, int flags)
  694. {
  695.     int ret;
  696.  
  697.     if ((ret = update_frame_pool(avctx, frame)) < 0)
  698.         return ret;
  699.  
  700. #if FF_API_GET_BUFFER
  701. FF_DISABLE_DEPRECATION_WARNINGS
  702.     frame->type = FF_BUFFER_TYPE_INTERNAL;
  703. FF_ENABLE_DEPRECATION_WARNINGS
  704. #endif
  705.  
  706.     switch (avctx->codec_type) {
  707.     case AVMEDIA_TYPE_VIDEO:
  708.         return video_get_buffer(avctx, frame);
  709.     case AVMEDIA_TYPE_AUDIO:
  710.         return audio_get_buffer(avctx, frame);
  711.     default:
  712.         return -1;
  713.     }
  714. }
  715.  
  716. int ff_init_buffer_info(AVCodecContext *avctx, AVFrame *frame)
  717. {
  718.     if (avctx->pkt) {
  719.         frame->pkt_pts = avctx->pkt->pts;
  720.         av_frame_set_pkt_pos     (frame, avctx->pkt->pos);
  721.         av_frame_set_pkt_duration(frame, avctx->pkt->duration);
  722.         av_frame_set_pkt_size    (frame, avctx->pkt->size);
  723.     } else {
  724.         frame->pkt_pts = AV_NOPTS_VALUE;
  725.         av_frame_set_pkt_pos     (frame, -1);
  726.         av_frame_set_pkt_duration(frame, 0);
  727.         av_frame_set_pkt_size    (frame, -1);
  728.     }
  729.     frame->reordered_opaque = avctx->reordered_opaque;
  730.  
  731.     switch (avctx->codec->type) {
  732.     case AVMEDIA_TYPE_VIDEO:
  733.         frame->width  = FFMAX(avctx->width,  FF_CEIL_RSHIFT(avctx->coded_width,  avctx->lowres));
  734.         frame->height = FFMAX(avctx->height, FF_CEIL_RSHIFT(avctx->coded_height, avctx->lowres));
  735.         if (frame->format < 0)
  736.             frame->format              = avctx->pix_fmt;
  737.         if (!frame->sample_aspect_ratio.num)
  738.             frame->sample_aspect_ratio = avctx->sample_aspect_ratio;
  739.         if (av_frame_get_colorspace(frame) == AVCOL_SPC_UNSPECIFIED)
  740.             av_frame_set_colorspace(frame, avctx->colorspace);
  741.         if (av_frame_get_color_range(frame) == AVCOL_RANGE_UNSPECIFIED)
  742.             av_frame_set_color_range(frame, avctx->color_range);
  743.         break;
  744.     case AVMEDIA_TYPE_AUDIO:
  745.         if (!frame->sample_rate)
  746.             frame->sample_rate    = avctx->sample_rate;
  747.         if (frame->format < 0)
  748.             frame->format         = avctx->sample_fmt;
  749.         if (!frame->channel_layout) {
  750.             if (avctx->channel_layout) {
  751.                  if (av_get_channel_layout_nb_channels(avctx->channel_layout) !=
  752.                      avctx->channels) {
  753.                      av_log(avctx, AV_LOG_ERROR, "Inconsistent channel "
  754.                             "configuration.\n");
  755.                      return AVERROR(EINVAL);
  756.                  }
  757.  
  758.                 frame->channel_layout = avctx->channel_layout;
  759.             } else {
  760.                 if (avctx->channels > FF_SANE_NB_CHANNELS) {
  761.                     av_log(avctx, AV_LOG_ERROR, "Too many channels: %d.\n",
  762.                            avctx->channels);
  763.                     return AVERROR(ENOSYS);
  764.                 }
  765.             }
  766.         }
  767.         av_frame_set_channels(frame, avctx->channels);
  768.         break;
  769.     }
  770.     return 0;
  771. }
  772.  
  773. #if FF_API_GET_BUFFER
  774. FF_DISABLE_DEPRECATION_WARNINGS
  775. int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  776. {
  777.     return avcodec_default_get_buffer2(avctx, frame, 0);
  778. }
  779.  
  780. typedef struct CompatReleaseBufPriv {
  781.     AVCodecContext avctx;
  782.     AVFrame frame;
  783. } CompatReleaseBufPriv;
  784.  
  785. static void compat_free_buffer(void *opaque, uint8_t *data)
  786. {
  787.     CompatReleaseBufPriv *priv = opaque;
  788.     if (priv->avctx.release_buffer)
  789.         priv->avctx.release_buffer(&priv->avctx, &priv->frame);
  790.     av_freep(&priv);
  791. }
  792.  
  793. static void compat_release_buffer(void *opaque, uint8_t *data)
  794. {
  795.     AVBufferRef *buf = opaque;
  796.     av_buffer_unref(&buf);
  797. }
  798. FF_ENABLE_DEPRECATION_WARNINGS
  799. #endif
  800.  
  801. static int get_buffer_internal(AVCodecContext *avctx, AVFrame *frame, int flags)
  802. {
  803.     int ret;
  804.  
  805.     if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
  806.         if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0 || avctx->pix_fmt<0) {
  807.             av_log(avctx, AV_LOG_ERROR, "video_get_buffer: image parameters invalid\n");
  808.             return AVERROR(EINVAL);
  809.         }
  810.     }
  811.     if ((ret = ff_init_buffer_info(avctx, frame)) < 0)
  812.         return ret;
  813.  
  814. #if FF_API_GET_BUFFER
  815. FF_DISABLE_DEPRECATION_WARNINGS
  816.     /*
  817.      * Wrap an old get_buffer()-allocated buffer in a bunch of AVBuffers.
  818.      * We wrap each plane in its own AVBuffer. Each of those has a reference to
  819.      * a dummy AVBuffer as its private data, unreffing it on free.
  820.      * When all the planes are freed, the dummy buffer's free callback calls
  821.      * release_buffer().
  822.      */
  823.     if (avctx->get_buffer) {
  824.         CompatReleaseBufPriv *priv = NULL;
  825.         AVBufferRef *dummy_buf = NULL;
  826.         int planes, i, ret;
  827.  
  828.         if (flags & AV_GET_BUFFER_FLAG_REF)
  829.             frame->reference    = 1;
  830.  
  831.         ret = avctx->get_buffer(avctx, frame);
  832.         if (ret < 0)
  833.             return ret;
  834.  
  835.         /* return if the buffers are already set up
  836.          * this would happen e.g. when a custom get_buffer() calls
  837.          * avcodec_default_get_buffer
  838.          */
  839.         if (frame->buf[0])
  840.             goto end;
  841.  
  842.         priv = av_mallocz(sizeof(*priv));
  843.         if (!priv) {
  844.             ret = AVERROR(ENOMEM);
  845.             goto fail;
  846.         }
  847.         priv->avctx = *avctx;
  848.         priv->frame = *frame;
  849.  
  850.         dummy_buf = av_buffer_create(NULL, 0, compat_free_buffer, priv, 0);
  851.         if (!dummy_buf) {
  852.             ret = AVERROR(ENOMEM);
  853.             goto fail;
  854.         }
  855.  
  856. #define WRAP_PLANE(ref_out, data, data_size)                            \
  857. do {                                                                    \
  858.     AVBufferRef *dummy_ref = av_buffer_ref(dummy_buf);                  \
  859.     if (!dummy_ref) {                                                   \
  860.         ret = AVERROR(ENOMEM);                                          \
  861.         goto fail;                                                      \
  862.     }                                                                   \
  863.     ref_out = av_buffer_create(data, data_size, compat_release_buffer,  \
  864.                                dummy_ref, 0);                           \
  865.     if (!ref_out) {                                                     \
  866.         av_frame_unref(frame);                                          \
  867.         ret = AVERROR(ENOMEM);                                          \
  868.         goto fail;                                                      \
  869.     }                                                                   \
  870. } while (0)
  871.  
  872.         if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
  873.             const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
  874.  
  875.             planes = av_pix_fmt_count_planes(frame->format);
  876.             /* workaround for AVHWAccel plane count of 0, buf[0] is used as
  877.                check for allocated buffers: make libavcodec happy */
  878.             if (desc && desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
  879.                 planes = 1;
  880.             if (!desc || planes <= 0) {
  881.                 ret = AVERROR(EINVAL);
  882.                 goto fail;
  883.             }
  884.  
  885.             for (i = 0; i < planes; i++) {
  886.                 int v_shift    = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
  887.                 int plane_size = (frame->height >> v_shift) * frame->linesize[i];
  888.  
  889.                 WRAP_PLANE(frame->buf[i], frame->data[i], plane_size);
  890.             }
  891.         } else {
  892.             int planar = av_sample_fmt_is_planar(frame->format);
  893.             planes = planar ? avctx->channels : 1;
  894.  
  895.             if (planes > FF_ARRAY_ELEMS(frame->buf)) {
  896.                 frame->nb_extended_buf = planes - FF_ARRAY_ELEMS(frame->buf);
  897.                 frame->extended_buf = av_malloc(sizeof(*frame->extended_buf) *
  898.                                                 frame->nb_extended_buf);
  899.                 if (!frame->extended_buf) {
  900.                     ret = AVERROR(ENOMEM);
  901.                     goto fail;
  902.                 }
  903.             }
  904.  
  905.             for (i = 0; i < FFMIN(planes, FF_ARRAY_ELEMS(frame->buf)); i++)
  906.                 WRAP_PLANE(frame->buf[i], frame->extended_data[i], frame->linesize[0]);
  907.  
  908.             for (i = 0; i < frame->nb_extended_buf; i++)
  909.                 WRAP_PLANE(frame->extended_buf[i],
  910.                            frame->extended_data[i + FF_ARRAY_ELEMS(frame->buf)],
  911.                            frame->linesize[0]);
  912.         }
  913.  
  914.         av_buffer_unref(&dummy_buf);
  915.  
  916. end:
  917.         frame->width  = avctx->width;
  918.         frame->height = avctx->height;
  919.  
  920.         return 0;
  921.  
  922. fail:
  923.         avctx->release_buffer(avctx, frame);
  924.         av_freep(&priv);
  925.         av_buffer_unref(&dummy_buf);
  926.         return ret;
  927.     }
  928. FF_ENABLE_DEPRECATION_WARNINGS
  929. #endif
  930.  
  931.     ret = avctx->get_buffer2(avctx, frame, flags);
  932.  
  933.     if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
  934.         frame->width  = avctx->width;
  935.         frame->height = avctx->height;
  936.     }
  937.  
  938.     return ret;
  939. }
  940.  
  941. int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
  942. {
  943.     int ret = get_buffer_internal(avctx, frame, flags);
  944.     if (ret < 0)
  945.         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  946.     return ret;
  947. }
  948.  
  949. static int reget_buffer_internal(AVCodecContext *avctx, AVFrame *frame)
  950. {
  951.     AVFrame tmp;
  952.     int ret;
  953.  
  954.     av_assert0(avctx->codec_type == AVMEDIA_TYPE_VIDEO);
  955.  
  956.     if (frame->data[0] && (frame->width != avctx->width || frame->height != avctx->height || frame->format != avctx->pix_fmt)) {
  957.         av_log(avctx, AV_LOG_WARNING, "Picture changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s in reget buffer()\n",
  958.                frame->width, frame->height, av_get_pix_fmt_name(frame->format), avctx->width, avctx->height, av_get_pix_fmt_name(avctx->pix_fmt));
  959.         av_frame_unref(frame);
  960.     }
  961.  
  962.     ff_init_buffer_info(avctx, frame);
  963.  
  964.     if (!frame->data[0])
  965.         return ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
  966.  
  967.     if (av_frame_is_writable(frame))
  968.         return 0;
  969.  
  970.     av_frame_move_ref(&tmp, frame);
  971.  
  972.     ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
  973.     if (ret < 0) {
  974.         av_frame_unref(&tmp);
  975.         return ret;
  976.     }
  977.  
  978.     av_image_copy(frame->data, frame->linesize, tmp.data, tmp.linesize,
  979.                   frame->format, frame->width, frame->height);
  980.  
  981.     av_frame_unref(&tmp);
  982.  
  983.     return 0;
  984. }
  985.  
  986. int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame)
  987. {
  988.     int ret = reget_buffer_internal(avctx, frame);
  989.     if (ret < 0)
  990.         av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  991.     return ret;
  992. }
  993.  
  994. #if FF_API_GET_BUFFER
  995. void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic)
  996. {
  997.     av_assert0(s->codec_type == AVMEDIA_TYPE_VIDEO);
  998.  
  999.     av_frame_unref(pic);
  1000. }
  1001.  
  1002. int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic)
  1003. {
  1004.     av_assert0(0);
  1005.     return AVERROR_BUG;
  1006. }
  1007. #endif
  1008.  
  1009. int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
  1010. {
  1011.     int i;
  1012.  
  1013.     for (i = 0; i < count; i++) {
  1014.         int r = func(c, (char *)arg + i * size);
  1015.         if (ret)
  1016.             ret[i] = r;
  1017.     }
  1018.     return 0;
  1019. }
  1020.  
  1021. int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
  1022. {
  1023.     int i;
  1024.  
  1025.     for (i = 0; i < count; i++) {
  1026.         int r = func(c, arg, i, 0);
  1027.         if (ret)
  1028.             ret[i] = r;
  1029.     }
  1030.     return 0;
  1031. }
  1032.  
  1033. static int is_hwaccel_pix_fmt(enum AVPixelFormat pix_fmt)
  1034. {
  1035.     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  1036.     return desc->flags & AV_PIX_FMT_FLAG_HWACCEL;
  1037. }
  1038.  
  1039. enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
  1040. {
  1041.     while (*fmt != AV_PIX_FMT_NONE && is_hwaccel_pix_fmt(*fmt))
  1042.         ++fmt;
  1043.     return fmt[0];
  1044. }
  1045.  
  1046. void avcodec_get_frame_defaults(AVFrame *frame)
  1047. {
  1048. #if LIBAVCODEC_VERSION_MAJOR >= 55
  1049.      // extended_data should explicitly be freed when needed, this code is unsafe currently
  1050.      // also this is not compatible to the <55 ABI/API
  1051.     if (frame->extended_data != frame->data && 0)
  1052.         av_freep(&frame->extended_data);
  1053. #endif
  1054.  
  1055.     memset(frame, 0, sizeof(AVFrame));
  1056.  
  1057.     frame->pts                   =
  1058.     frame->pkt_dts               =
  1059.     frame->pkt_pts               = AV_NOPTS_VALUE;
  1060.     av_frame_set_best_effort_timestamp(frame, AV_NOPTS_VALUE);
  1061.     av_frame_set_pkt_duration         (frame, 0);
  1062.     av_frame_set_pkt_pos              (frame, -1);
  1063.     av_frame_set_pkt_size             (frame, -1);
  1064.     frame->key_frame           = 1;
  1065.     frame->sample_aspect_ratio = (AVRational) {0, 1 };
  1066.     frame->format              = -1; /* unknown */
  1067.     frame->extended_data       = frame->data;
  1068.     av_frame_set_colorspace(frame, AVCOL_SPC_UNSPECIFIED);
  1069. }
  1070.  
  1071. AVFrame *avcodec_alloc_frame(void)
  1072. {
  1073.     AVFrame *frame = av_malloc(sizeof(AVFrame));
  1074.  
  1075.     if (frame == NULL)
  1076.         return NULL;
  1077.  
  1078.     frame->extended_data = NULL;
  1079.     avcodec_get_frame_defaults(frame);
  1080.  
  1081.     return frame;
  1082. }
  1083.  
  1084. void avcodec_free_frame(AVFrame **frame)
  1085. {
  1086.     AVFrame *f;
  1087.  
  1088.     if (!frame || !*frame)
  1089.         return;
  1090.  
  1091.     f = *frame;
  1092.  
  1093.     if (f->extended_data != f->data)
  1094.         av_freep(&f->extended_data);
  1095.  
  1096.     av_freep(frame);
  1097. }
  1098.  
  1099. MAKE_ACCESSORS(AVCodecContext, codec, AVRational, pkt_timebase)
  1100. MAKE_ACCESSORS(AVCodecContext, codec, const AVCodecDescriptor *, codec_descriptor)
  1101. MAKE_ACCESSORS(AVCodecContext, codec, int, lowres)
  1102. MAKE_ACCESSORS(AVCodecContext, codec, int, seek_preroll)
  1103.  
  1104. int av_codec_get_max_lowres(const AVCodec *codec)
  1105. {
  1106.     return codec->max_lowres;
  1107. }
  1108.  
  1109. static void avcodec_get_subtitle_defaults(AVSubtitle *sub)
  1110. {
  1111.     memset(sub, 0, sizeof(*sub));
  1112.     sub->pts = AV_NOPTS_VALUE;
  1113. }
  1114.  
  1115. static int get_bit_rate(AVCodecContext *ctx)
  1116. {
  1117.     int bit_rate;
  1118.     int bits_per_sample;
  1119.  
  1120.     switch (ctx->codec_type) {
  1121.     case AVMEDIA_TYPE_VIDEO:
  1122.     case AVMEDIA_TYPE_DATA:
  1123.     case AVMEDIA_TYPE_SUBTITLE:
  1124.     case AVMEDIA_TYPE_ATTACHMENT:
  1125.         bit_rate = ctx->bit_rate;
  1126.         break;
  1127.     case AVMEDIA_TYPE_AUDIO:
  1128.         bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
  1129.         bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
  1130.         break;
  1131.     default:
  1132.         bit_rate = 0;
  1133.         break;
  1134.     }
  1135.     return bit_rate;
  1136. }
  1137.  
  1138. #if FF_API_AVCODEC_OPEN
  1139. int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
  1140. {
  1141.     return avcodec_open2(avctx, codec, NULL);
  1142. }
  1143. #endif
  1144.  
  1145. int attribute_align_arg ff_codec_open2_recursive(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
  1146. {
  1147.     int ret = 0;
  1148.  
  1149.     ff_unlock_avcodec();
  1150.  
  1151.     ret = avcodec_open2(avctx, codec, options);
  1152.  
  1153.     ff_lock_avcodec(avctx);
  1154.     return ret;
  1155. }
  1156.  
  1157. int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
  1158. {
  1159.     int ret = 0;
  1160.     AVDictionary *tmp = NULL;
  1161.  
  1162.     if (avcodec_is_open(avctx))
  1163.         return 0;
  1164.  
  1165.     if ((!codec && !avctx->codec)) {
  1166.         av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2()\n");
  1167.         return AVERROR(EINVAL);
  1168.     }
  1169.     if ((codec && avctx->codec && codec != avctx->codec)) {
  1170.         av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
  1171.                                     "but %s passed to avcodec_open2()\n", avctx->codec->name, codec->name);
  1172.         return AVERROR(EINVAL);
  1173.     }
  1174.     if (!codec)
  1175.         codec = avctx->codec;
  1176.  
  1177.     if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
  1178.         return AVERROR(EINVAL);
  1179.  
  1180.     if (options)
  1181.         av_dict_copy(&tmp, *options, 0);
  1182.  
  1183.     ret = ff_lock_avcodec(avctx);
  1184.     if (ret < 0)
  1185.         return ret;
  1186.  
  1187.     avctx->internal = av_mallocz(sizeof(AVCodecInternal));
  1188.     if (!avctx->internal) {
  1189.         ret = AVERROR(ENOMEM);
  1190.         goto end;
  1191.     }
  1192.  
  1193.     avctx->internal->pool = av_mallocz(sizeof(*avctx->internal->pool));
  1194.     if (!avctx->internal->pool) {
  1195.         ret = AVERROR(ENOMEM);
  1196.         goto free_and_end;
  1197.     }
  1198.  
  1199.     if (codec->priv_data_size > 0) {
  1200.         if (!avctx->priv_data) {
  1201.             avctx->priv_data = av_mallocz(codec->priv_data_size);
  1202.             if (!avctx->priv_data) {
  1203.                 ret = AVERROR(ENOMEM);
  1204.                 goto end;
  1205.             }
  1206.             if (codec->priv_class) {
  1207.                 *(const AVClass **)avctx->priv_data = codec->priv_class;
  1208.                 av_opt_set_defaults(avctx->priv_data);
  1209.             }
  1210.         }
  1211.         if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
  1212.             goto free_and_end;
  1213.     } else {
  1214.         avctx->priv_data = NULL;
  1215.     }
  1216.     if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
  1217.         goto free_and_end;
  1218.  
  1219.     // only call avcodec_set_dimensions() for non H.264/VP6F codecs so as not to overwrite previously setup dimensions
  1220.     if (!(avctx->coded_width && avctx->coded_height && avctx->width && avctx->height &&
  1221.           (avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_VP6F))) {
  1222.     if (avctx->coded_width && avctx->coded_height)
  1223.         avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
  1224.     else if (avctx->width && avctx->height)
  1225.         avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  1226.     }
  1227.  
  1228.     if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
  1229.         && (  av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
  1230.            || av_image_check_size(avctx->width,       avctx->height,       0, avctx) < 0)) {
  1231.         av_log(avctx, AV_LOG_WARNING, "Ignoring invalid width/height values\n");
  1232.         avcodec_set_dimensions(avctx, 0, 0);
  1233.     }
  1234.  
  1235.     /* if the decoder init function was already called previously,
  1236.      * free the already allocated subtitle_header before overwriting it */
  1237.     if (av_codec_is_decoder(codec))
  1238.         av_freep(&avctx->subtitle_header);
  1239.  
  1240.     if (avctx->channels > FF_SANE_NB_CHANNELS) {
  1241.         ret = AVERROR(EINVAL);
  1242.         goto free_and_end;
  1243.     }
  1244.  
  1245.     avctx->codec = codec;
  1246.     if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
  1247.         avctx->codec_id == AV_CODEC_ID_NONE) {
  1248.         avctx->codec_type = codec->type;
  1249.         avctx->codec_id   = codec->id;
  1250.     }
  1251.     if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
  1252.                                          && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
  1253.         av_log(avctx, AV_LOG_ERROR, "Codec type or id mismatches\n");
  1254.         ret = AVERROR(EINVAL);
  1255.         goto free_and_end;
  1256.     }
  1257.     avctx->frame_number = 0;
  1258.     avctx->codec_descriptor = avcodec_descriptor_get(avctx->codec_id);
  1259.  
  1260.     if (avctx->codec->capabilities & CODEC_CAP_EXPERIMENTAL &&
  1261.         avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
  1262.         const char *codec_string = av_codec_is_encoder(codec) ? "encoder" : "decoder";
  1263.         AVCodec *codec2;
  1264.         av_log(avctx, AV_LOG_ERROR,
  1265.                "The %s '%s' is experimental but experimental codecs are not enabled, "
  1266.                "add '-strict %d' if you want to use it.\n",
  1267.                codec_string, codec->name, FF_COMPLIANCE_EXPERIMENTAL);
  1268.         codec2 = av_codec_is_encoder(codec) ? avcodec_find_encoder(codec->id) : avcodec_find_decoder(codec->id);
  1269.         if (!(codec2->capabilities & CODEC_CAP_EXPERIMENTAL))
  1270.             av_log(avctx, AV_LOG_ERROR, "Alternatively use the non experimental %s '%s'.\n",
  1271.                 codec_string, codec2->name);
  1272.         ret = AVERROR_EXPERIMENTAL;
  1273.         goto free_and_end;
  1274.     }
  1275.  
  1276.     if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
  1277.         (!avctx->time_base.num || !avctx->time_base.den)) {
  1278.         avctx->time_base.num = 1;
  1279.         avctx->time_base.den = avctx->sample_rate;
  1280.     }
  1281.  
  1282.     if (!HAVE_THREADS)
  1283.         av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
  1284.  
  1285.     if (CONFIG_FRAME_THREAD_ENCODER) {
  1286.         ff_unlock_avcodec(); //we will instanciate a few encoders thus kick the counter to prevent false detection of a problem
  1287.         ret = ff_frame_thread_encoder_init(avctx, options ? *options : NULL);
  1288.         ff_lock_avcodec(avctx);
  1289.         if (ret < 0)
  1290.             goto free_and_end;
  1291.     }
  1292.  
  1293.     if (HAVE_THREADS
  1294.         && !(avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))) {
  1295.         ret = ff_thread_init(avctx);
  1296.         if (ret < 0) {
  1297.             goto free_and_end;
  1298.         }
  1299.     }
  1300.     if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
  1301.         avctx->thread_count = 1;
  1302.  
  1303.     if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
  1304.         av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
  1305.                avctx->codec->max_lowres);
  1306.         ret = AVERROR(EINVAL);
  1307.         goto free_and_end;
  1308.     }
  1309.  
  1310.     if (av_codec_is_encoder(avctx->codec)) {
  1311.         int i;
  1312.         if (avctx->codec->sample_fmts) {
  1313.             for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
  1314.                 if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
  1315.                     break;
  1316.                 if (avctx->channels == 1 &&
  1317.                     av_get_planar_sample_fmt(avctx->sample_fmt) ==
  1318.                     av_get_planar_sample_fmt(avctx->codec->sample_fmts[i])) {
  1319.                     avctx->sample_fmt = avctx->codec->sample_fmts[i];
  1320.                     break;
  1321.                 }
  1322.             }
  1323.             if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
  1324.                 char buf[128];
  1325.                 snprintf(buf, sizeof(buf), "%d", avctx->sample_fmt);
  1326.                 av_log(avctx, AV_LOG_ERROR, "Specified sample format %s is invalid or not supported\n",
  1327.                        (char *)av_x_if_null(av_get_sample_fmt_name(avctx->sample_fmt), buf));
  1328.                 ret = AVERROR(EINVAL);
  1329.                 goto free_and_end;
  1330.             }
  1331.         }
  1332.         if (avctx->codec->pix_fmts) {
  1333.             for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
  1334.                 if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
  1335.                     break;
  1336.             if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE
  1337.                 && !((avctx->codec_id == AV_CODEC_ID_MJPEG || avctx->codec_id == AV_CODEC_ID_LJPEG)
  1338.                      && avctx->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL)) {
  1339.                 char buf[128];
  1340.                 snprintf(buf, sizeof(buf), "%d", avctx->pix_fmt);
  1341.                 av_log(avctx, AV_LOG_ERROR, "Specified pixel format %s is invalid or not supported\n",
  1342.                        (char *)av_x_if_null(av_get_pix_fmt_name(avctx->pix_fmt), buf));
  1343.                 ret = AVERROR(EINVAL);
  1344.                 goto free_and_end;
  1345.             }
  1346.         }
  1347.         if (avctx->codec->supported_samplerates) {
  1348.             for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
  1349.                 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
  1350.                     break;
  1351.             if (avctx->codec->supported_samplerates[i] == 0) {
  1352.                 av_log(avctx, AV_LOG_ERROR, "Specified sample rate %d is not supported\n",
  1353.                        avctx->sample_rate);
  1354.                 ret = AVERROR(EINVAL);
  1355.                 goto free_and_end;
  1356.             }
  1357.         }
  1358.         if (avctx->codec->channel_layouts) {
  1359.             if (!avctx->channel_layout) {
  1360.                 av_log(avctx, AV_LOG_WARNING, "Channel layout not specified\n");
  1361.             } else {
  1362.                 for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
  1363.                     if (avctx->channel_layout == avctx->codec->channel_layouts[i])
  1364.                         break;
  1365.                 if (avctx->codec->channel_layouts[i] == 0) {
  1366.                     char buf[512];
  1367.                     av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
  1368.                     av_log(avctx, AV_LOG_ERROR, "Specified channel layout '%s' is not supported\n", buf);
  1369.                     ret = AVERROR(EINVAL);
  1370.                     goto free_and_end;
  1371.                 }
  1372.             }
  1373.         }
  1374.         if (avctx->channel_layout && avctx->channels) {
  1375.             int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  1376.             if (channels != avctx->channels) {
  1377.                 char buf[512];
  1378.                 av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
  1379.                 av_log(avctx, AV_LOG_ERROR,
  1380.                        "Channel layout '%s' with %d channels does not match number of specified channels %d\n",
  1381.                        buf, channels, avctx->channels);
  1382.                 ret = AVERROR(EINVAL);
  1383.                 goto free_and_end;
  1384.             }
  1385.         } else if (avctx->channel_layout) {
  1386.             avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  1387.         }
  1388.         if(avctx->codec_type == AVMEDIA_TYPE_VIDEO &&
  1389.            avctx->codec_id != AV_CODEC_ID_PNG // For mplayer
  1390.         ) {
  1391.             if (avctx->width <= 0 || avctx->height <= 0) {
  1392.                 av_log(avctx, AV_LOG_ERROR, "dimensions not set\n");
  1393.                 ret = AVERROR(EINVAL);
  1394.                 goto free_and_end;
  1395.             }
  1396.         }
  1397.         if (   (avctx->codec_type == AVMEDIA_TYPE_VIDEO || avctx->codec_type == AVMEDIA_TYPE_AUDIO)
  1398.             && avctx->bit_rate>0 && avctx->bit_rate<1000) {
  1399.             av_log(avctx, AV_LOG_WARNING, "Bitrate %d is extremely low, maybe you mean %dk\n", avctx->bit_rate, avctx->bit_rate);
  1400.         }
  1401.  
  1402.         if (!avctx->rc_initial_buffer_occupancy)
  1403.             avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3 / 4;
  1404.     }
  1405.  
  1406.     avctx->pts_correction_num_faulty_pts =
  1407.     avctx->pts_correction_num_faulty_dts = 0;
  1408.     avctx->pts_correction_last_pts =
  1409.     avctx->pts_correction_last_dts = INT64_MIN;
  1410.  
  1411.     if (   avctx->codec->init && (!(avctx->active_thread_type&FF_THREAD_FRAME)
  1412.         || avctx->internal->frame_thread_encoder)) {
  1413.         ret = avctx->codec->init(avctx);
  1414.         if (ret < 0) {
  1415.             goto free_and_end;
  1416.         }
  1417.     }
  1418.  
  1419.     ret=0;
  1420.  
  1421.     if (av_codec_is_decoder(avctx->codec)) {
  1422.         if (!avctx->bit_rate)
  1423.             avctx->bit_rate = get_bit_rate(avctx);
  1424.         /* validate channel layout from the decoder */
  1425.         if (avctx->channel_layout) {
  1426.             int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  1427.             if (!avctx->channels)
  1428.                 avctx->channels = channels;
  1429.             else if (channels != avctx->channels) {
  1430.                 char buf[512];
  1431.                 av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
  1432.                 av_log(avctx, AV_LOG_WARNING,
  1433.                        "Channel layout '%s' with %d channels does not match specified number of channels %d: "
  1434.                        "ignoring specified channel layout\n",
  1435.                        buf, channels, avctx->channels);
  1436.                 avctx->channel_layout = 0;
  1437.             }
  1438.         }
  1439.         if (avctx->channels && avctx->channels < 0 ||
  1440.             avctx->channels > FF_SANE_NB_CHANNELS) {
  1441.             ret = AVERROR(EINVAL);
  1442.             goto free_and_end;
  1443.         }
  1444.         if (avctx->sub_charenc) {
  1445.             if (avctx->codec_type != AVMEDIA_TYPE_SUBTITLE) {
  1446.                 av_log(avctx, AV_LOG_ERROR, "Character encoding is only "
  1447.                        "supported with subtitles codecs\n");
  1448.                 ret = AVERROR(EINVAL);
  1449.                 goto free_and_end;
  1450.             } else if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB) {
  1451.                 av_log(avctx, AV_LOG_WARNING, "Codec '%s' is bitmap-based, "
  1452.                        "subtitles character encoding will be ignored\n",
  1453.                        avctx->codec_descriptor->name);
  1454.                 avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_DO_NOTHING;
  1455.             } else {
  1456.                 /* input character encoding is set for a text based subtitle
  1457.                  * codec at this point */
  1458.                 if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_AUTOMATIC)
  1459.                     avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_PRE_DECODER;
  1460.  
  1461.                 if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_PRE_DECODER) {
  1462. #if CONFIG_ICONV
  1463.                     iconv_t cd = iconv_open("UTF-8", avctx->sub_charenc);
  1464.                     if (cd == (iconv_t)-1) {
  1465.                         av_log(avctx, AV_LOG_ERROR, "Unable to open iconv context "
  1466.                                "with input character encoding \"%s\"\n", avctx->sub_charenc);
  1467.                         ret = AVERROR(errno);
  1468.                         goto free_and_end;
  1469.                     }
  1470.                     iconv_close(cd);
  1471. #else
  1472.                     av_log(avctx, AV_LOG_ERROR, "Character encoding subtitles "
  1473.                            "conversion needs a libavcodec built with iconv support "
  1474.                            "for this codec\n");
  1475.                     ret = AVERROR(ENOSYS);
  1476.                     goto free_and_end;
  1477. #endif
  1478.                 }
  1479.             }
  1480.         }
  1481.     }
  1482. end:
  1483.     ff_unlock_avcodec();
  1484.     if (options) {
  1485.         av_dict_free(options);
  1486.         *options = tmp;
  1487.     }
  1488.  
  1489.     return ret;
  1490. free_and_end:
  1491.     av_dict_free(&tmp);
  1492.     av_freep(&avctx->priv_data);
  1493.     if (avctx->internal)
  1494.         av_freep(&avctx->internal->pool);
  1495.     av_freep(&avctx->internal);
  1496.     avctx->codec = NULL;
  1497.     goto end;
  1498. }
  1499.  
  1500. int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
  1501. {
  1502.     if (avpkt->size < 0) {
  1503.         av_log(avctx, AV_LOG_ERROR, "Invalid negative user packet size %d\n", avpkt->size);
  1504.         return AVERROR(EINVAL);
  1505.     }
  1506.     if (size < 0 || size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
  1507.         av_log(avctx, AV_LOG_ERROR, "Invalid minimum required packet size %"PRId64" (max allowed is %d)\n",
  1508.                size, INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE);
  1509.         return AVERROR(EINVAL);
  1510.     }
  1511.  
  1512.     if (avctx) {
  1513.         av_assert0(!avpkt->data || avpkt->data != avctx->internal->byte_buffer);
  1514.         if (!avpkt->data || avpkt->size < size) {
  1515.             av_fast_padded_malloc(&avctx->internal->byte_buffer, &avctx->internal->byte_buffer_size, size);
  1516.             avpkt->data = avctx->internal->byte_buffer;
  1517.             avpkt->size = avctx->internal->byte_buffer_size;
  1518.             avpkt->destruct = NULL;
  1519.         }
  1520.     }
  1521.  
  1522.     if (avpkt->data) {
  1523.         AVBufferRef *buf = avpkt->buf;
  1524. #if FF_API_DESTRUCT_PACKET
  1525. FF_DISABLE_DEPRECATION_WARNINGS
  1526.         void *destruct = avpkt->destruct;
  1527. FF_ENABLE_DEPRECATION_WARNINGS
  1528. #endif
  1529.  
  1530.         if (avpkt->size < size) {
  1531.             av_log(avctx, AV_LOG_ERROR, "User packet is too small (%d < %"PRId64")\n", avpkt->size, size);
  1532.             return AVERROR(EINVAL);
  1533.         }
  1534.  
  1535.         av_init_packet(avpkt);
  1536. #if FF_API_DESTRUCT_PACKET
  1537. FF_DISABLE_DEPRECATION_WARNINGS
  1538.         avpkt->destruct = destruct;
  1539. FF_ENABLE_DEPRECATION_WARNINGS
  1540. #endif
  1541.         avpkt->buf      = buf;
  1542.         avpkt->size     = size;
  1543.         return 0;
  1544.     } else {
  1545.         int ret = av_new_packet(avpkt, size);
  1546.         if (ret < 0)
  1547.             av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %"PRId64"\n", size);
  1548.         return ret;
  1549.     }
  1550. }
  1551.  
  1552. int ff_alloc_packet(AVPacket *avpkt, int size)
  1553. {
  1554.     return ff_alloc_packet2(NULL, avpkt, size);
  1555. }
  1556.  
  1557. /**
  1558.  * Pad last frame with silence.
  1559.  */
  1560. static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
  1561. {
  1562.     AVFrame *frame = NULL;
  1563.     int ret;
  1564.  
  1565.     if (!(frame = avcodec_alloc_frame()))
  1566.         return AVERROR(ENOMEM);
  1567.  
  1568.     frame->format         = src->format;
  1569.     frame->channel_layout = src->channel_layout;
  1570.     av_frame_set_channels(frame, av_frame_get_channels(src));
  1571.     frame->nb_samples     = s->frame_size;
  1572.     ret = av_frame_get_buffer(frame, 32);
  1573.     if (ret < 0)
  1574.         goto fail;
  1575.  
  1576.     ret = av_frame_copy_props(frame, src);
  1577.     if (ret < 0)
  1578.         goto fail;
  1579.  
  1580.     if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
  1581.                                src->nb_samples, s->channels, s->sample_fmt)) < 0)
  1582.         goto fail;
  1583.     if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
  1584.                                       frame->nb_samples - src->nb_samples,
  1585.                                       s->channels, s->sample_fmt)) < 0)
  1586.         goto fail;
  1587.  
  1588.     *dst = frame;
  1589.  
  1590.     return 0;
  1591.  
  1592. fail:
  1593.     av_frame_free(&frame);
  1594.     return ret;
  1595. }
  1596.  
  1597. int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
  1598.                                               AVPacket *avpkt,
  1599.                                               const AVFrame *frame,
  1600.                                               int *got_packet_ptr)
  1601. {
  1602.     AVFrame tmp;
  1603.     AVFrame *padded_frame = NULL;
  1604.     int ret;
  1605.     AVPacket user_pkt = *avpkt;
  1606.     int needs_realloc = !user_pkt.data;
  1607.  
  1608.     *got_packet_ptr = 0;
  1609.  
  1610.     if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
  1611.         av_free_packet(avpkt);
  1612.         av_init_packet(avpkt);
  1613.         return 0;
  1614.     }
  1615.  
  1616.     /* ensure that extended_data is properly set */
  1617.     if (frame && !frame->extended_data) {
  1618.         if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
  1619.             avctx->channels > AV_NUM_DATA_POINTERS) {
  1620.             av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
  1621.                                         "with more than %d channels, but extended_data is not set.\n",
  1622.                    AV_NUM_DATA_POINTERS);
  1623.             return AVERROR(EINVAL);
  1624.         }
  1625.         av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
  1626.  
  1627.         tmp = *frame;
  1628.         tmp.extended_data = tmp.data;
  1629.         frame = &tmp;
  1630.     }
  1631.  
  1632.     /* check for valid frame size */
  1633.     if (frame) {
  1634.         if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
  1635.             if (frame->nb_samples > avctx->frame_size) {
  1636.                 av_log(avctx, AV_LOG_ERROR, "more samples than frame size (avcodec_encode_audio2)\n");
  1637.                 return AVERROR(EINVAL);
  1638.             }
  1639.         } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
  1640.             if (frame->nb_samples < avctx->frame_size &&
  1641.                 !avctx->internal->last_audio_frame) {
  1642.                 ret = pad_last_frame(avctx, &padded_frame, frame);
  1643.                 if (ret < 0)
  1644.                     return ret;
  1645.  
  1646.                 frame = padded_frame;
  1647.                 avctx->internal->last_audio_frame = 1;
  1648.             }
  1649.  
  1650.             if (frame->nb_samples != avctx->frame_size) {
  1651.                 av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) != frame_size (%d) (avcodec_encode_audio2)\n", frame->nb_samples, avctx->frame_size);
  1652.                 ret = AVERROR(EINVAL);
  1653.                 goto end;
  1654.             }
  1655.         }
  1656.     }
  1657.  
  1658.     ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  1659.     if (!ret) {
  1660.         if (*got_packet_ptr) {
  1661.             if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
  1662.                 if (avpkt->pts == AV_NOPTS_VALUE)
  1663.                     avpkt->pts = frame->pts;
  1664.                 if (!avpkt->duration)
  1665.                     avpkt->duration = ff_samples_to_time_base(avctx,
  1666.                                                               frame->nb_samples);
  1667.             }
  1668.             avpkt->dts = avpkt->pts;
  1669.         } else {
  1670.             avpkt->size = 0;
  1671.         }
  1672.     }
  1673.     if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
  1674.         needs_realloc = 0;
  1675.         if (user_pkt.data) {
  1676.             if (user_pkt.size >= avpkt->size) {
  1677.                 memcpy(user_pkt.data, avpkt->data, avpkt->size);
  1678.             } else {
  1679.                 av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
  1680.                 avpkt->size = user_pkt.size;
  1681.                 ret = -1;
  1682.             }
  1683.             avpkt->buf      = user_pkt.buf;
  1684.             avpkt->data     = user_pkt.data;
  1685.             avpkt->destruct = user_pkt.destruct;
  1686.         } else {
  1687.             if (av_dup_packet(avpkt) < 0) {
  1688.                 ret = AVERROR(ENOMEM);
  1689.             }
  1690.         }
  1691.     }
  1692.  
  1693.     if (!ret) {
  1694.         if (needs_realloc && avpkt->data) {
  1695.             ret = av_buffer_realloc(&avpkt->buf, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
  1696.             if (ret >= 0)
  1697.                 avpkt->data = avpkt->buf->data;
  1698.         }
  1699.  
  1700.         avctx->frame_number++;
  1701.     }
  1702.  
  1703.     if (ret < 0 || !*got_packet_ptr) {
  1704.         av_free_packet(avpkt);
  1705.         av_init_packet(avpkt);
  1706.         goto end;
  1707.     }
  1708.  
  1709.     /* NOTE: if we add any audio encoders which output non-keyframe packets,
  1710.      *       this needs to be moved to the encoders, but for now we can do it
  1711.      *       here to simplify things */
  1712.     avpkt->flags |= AV_PKT_FLAG_KEY;
  1713.  
  1714. end:
  1715.     av_frame_free(&padded_frame);
  1716.  
  1717.     return ret;
  1718. }
  1719.  
  1720. #if FF_API_OLD_ENCODE_AUDIO
  1721. int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
  1722.                                              uint8_t *buf, int buf_size,
  1723.                                              const short *samples)
  1724. {
  1725.     AVPacket pkt;
  1726.     AVFrame frame0 = { { 0 } };
  1727.     AVFrame *frame;
  1728.     int ret, samples_size, got_packet;
  1729.  
  1730.     av_init_packet(&pkt);
  1731.     pkt.data = buf;
  1732.     pkt.size = buf_size;
  1733.  
  1734.     if (samples) {
  1735.         frame = &frame0;
  1736.         avcodec_get_frame_defaults(frame);
  1737.  
  1738.         if (avctx->frame_size) {
  1739.             frame->nb_samples = avctx->frame_size;
  1740.         } else {
  1741.             /* if frame_size is not set, the number of samples must be
  1742.              * calculated from the buffer size */
  1743.             int64_t nb_samples;
  1744.             if (!av_get_bits_per_sample(avctx->codec_id)) {
  1745.                 av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
  1746.                                             "support this codec\n");
  1747.                 return AVERROR(EINVAL);
  1748.             }
  1749.             nb_samples = (int64_t)buf_size * 8 /
  1750.                          (av_get_bits_per_sample(avctx->codec_id) *
  1751.                           avctx->channels);
  1752.             if (nb_samples >= INT_MAX)
  1753.                 return AVERROR(EINVAL);
  1754.             frame->nb_samples = nb_samples;
  1755.         }
  1756.  
  1757.         /* it is assumed that the samples buffer is large enough based on the
  1758.          * relevant parameters */
  1759.         samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
  1760.                                                   frame->nb_samples,
  1761.                                                   avctx->sample_fmt, 1);
  1762.         if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
  1763.                                             avctx->sample_fmt,
  1764.                                             (const uint8_t *)samples,
  1765.                                             samples_size, 1)) < 0)
  1766.             return ret;
  1767.  
  1768.         /* fabricate frame pts from sample count.
  1769.          * this is needed because the avcodec_encode_audio() API does not have
  1770.          * a way for the user to provide pts */
  1771.         if (avctx->sample_rate && avctx->time_base.num)
  1772.             frame->pts = ff_samples_to_time_base(avctx,
  1773.                                                  avctx->internal->sample_count);
  1774.         else
  1775.             frame->pts = AV_NOPTS_VALUE;
  1776.         avctx->internal->sample_count += frame->nb_samples;
  1777.     } else {
  1778.         frame = NULL;
  1779.     }
  1780.  
  1781.     got_packet = 0;
  1782.     ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
  1783.     if (!ret && got_packet && avctx->coded_frame) {
  1784.         avctx->coded_frame->pts       = pkt.pts;
  1785.         avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
  1786.     }
  1787.     /* free any side data since we cannot return it */
  1788.     av_packet_free_side_data(&pkt);
  1789.  
  1790.     if (frame && frame->extended_data != frame->data)
  1791.         av_freep(&frame->extended_data);
  1792.  
  1793.     return ret ? ret : pkt.size;
  1794. }
  1795.  
  1796. #endif
  1797.  
  1798. #if FF_API_OLD_ENCODE_VIDEO
  1799. int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  1800.                                              const AVFrame *pict)
  1801. {
  1802.     AVPacket pkt;
  1803.     int ret, got_packet = 0;
  1804.  
  1805.     if (buf_size < FF_MIN_BUFFER_SIZE) {
  1806.         av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  1807.         return -1;
  1808.     }
  1809.  
  1810.     av_init_packet(&pkt);
  1811.     pkt.data = buf;
  1812.     pkt.size = buf_size;
  1813.  
  1814.     ret = avcodec_encode_video2(avctx, &pkt, pict, &got_packet);
  1815.     if (!ret && got_packet && avctx->coded_frame) {
  1816.         avctx->coded_frame->pts       = pkt.pts;
  1817.         avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
  1818.     }
  1819.  
  1820.     /* free any side data since we cannot return it */
  1821.     if (pkt.side_data_elems > 0) {
  1822.         int i;
  1823.         for (i = 0; i < pkt.side_data_elems; i++)
  1824.             av_free(pkt.side_data[i].data);
  1825.         av_freep(&pkt.side_data);
  1826.         pkt.side_data_elems = 0;
  1827.     }
  1828.  
  1829.     return ret ? ret : pkt.size;
  1830. }
  1831.  
  1832. #endif
  1833.  
  1834. int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
  1835.                                               AVPacket *avpkt,
  1836.                                               const AVFrame *frame,
  1837.                                               int *got_packet_ptr)
  1838. {
  1839.     int ret;
  1840.     AVPacket user_pkt = *avpkt;
  1841.     int needs_realloc = !user_pkt.data;
  1842.  
  1843.     *got_packet_ptr = 0;
  1844.  
  1845.     if(CONFIG_FRAME_THREAD_ENCODER &&
  1846.        avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))
  1847.         return ff_thread_video_encode_frame(avctx, avpkt, frame, got_packet_ptr);
  1848.  
  1849.     if ((avctx->flags&CODEC_FLAG_PASS1) && avctx->stats_out)
  1850.         avctx->stats_out[0] = '\0';
  1851.  
  1852.     if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
  1853.         av_free_packet(avpkt);
  1854.         av_init_packet(avpkt);
  1855.         avpkt->size = 0;
  1856.         return 0;
  1857.     }
  1858.  
  1859.     if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
  1860.         return AVERROR(EINVAL);
  1861.  
  1862.     av_assert0(avctx->codec->encode2);
  1863.  
  1864.     ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  1865.     av_assert0(ret <= 0);
  1866.  
  1867.     if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
  1868.         needs_realloc = 0;
  1869.         if (user_pkt.data) {
  1870.             if (user_pkt.size >= avpkt->size) {
  1871.                 memcpy(user_pkt.data, avpkt->data, avpkt->size);
  1872.             } else {
  1873.                 av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
  1874.                 avpkt->size = user_pkt.size;
  1875.                 ret = -1;
  1876.             }
  1877.             avpkt->buf      = user_pkt.buf;
  1878.             avpkt->data     = user_pkt.data;
  1879.             avpkt->destruct = user_pkt.destruct;
  1880.         } else {
  1881.             if (av_dup_packet(avpkt) < 0) {
  1882.                 ret = AVERROR(ENOMEM);
  1883.             }
  1884.         }
  1885.     }
  1886.  
  1887.     if (!ret) {
  1888.         if (!*got_packet_ptr)
  1889.             avpkt->size = 0;
  1890.         else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
  1891.             avpkt->pts = avpkt->dts = frame->pts;
  1892.  
  1893.         if (needs_realloc && avpkt->data) {
  1894.             ret = av_buffer_realloc(&avpkt->buf, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
  1895.             if (ret >= 0)
  1896.                 avpkt->data = avpkt->buf->data;
  1897.         }
  1898.  
  1899.         avctx->frame_number++;
  1900.     }
  1901.  
  1902.     if (ret < 0 || !*got_packet_ptr)
  1903.         av_free_packet(avpkt);
  1904.     else
  1905.         av_packet_merge_side_data(avpkt);
  1906.  
  1907.     emms_c();
  1908.     return ret;
  1909. }
  1910.  
  1911. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  1912.                             const AVSubtitle *sub)
  1913. {
  1914.     int ret;
  1915.     if (sub->start_display_time) {
  1916.         av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
  1917.         return -1;
  1918.     }
  1919.  
  1920.     ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
  1921.     avctx->frame_number++;
  1922.     return ret;
  1923. }
  1924.  
  1925. /**
  1926.  * Attempt to guess proper monotonic timestamps for decoded video frames
  1927.  * which might have incorrect times. Input timestamps may wrap around, in
  1928.  * which case the output will as well.
  1929.  *
  1930.  * @param pts the pts field of the decoded AVPacket, as passed through
  1931.  * AVFrame.pkt_pts
  1932.  * @param dts the dts field of the decoded AVPacket
  1933.  * @return one of the input values, may be AV_NOPTS_VALUE
  1934.  */
  1935. static int64_t guess_correct_pts(AVCodecContext *ctx,
  1936.                                  int64_t reordered_pts, int64_t dts)
  1937. {
  1938.     int64_t pts = AV_NOPTS_VALUE;
  1939.  
  1940.     if (dts != AV_NOPTS_VALUE) {
  1941.         ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
  1942.         ctx->pts_correction_last_dts = dts;
  1943.     }
  1944.     if (reordered_pts != AV_NOPTS_VALUE) {
  1945.         ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
  1946.         ctx->pts_correction_last_pts = reordered_pts;
  1947.     }
  1948.     if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
  1949.        && reordered_pts != AV_NOPTS_VALUE)
  1950.         pts = reordered_pts;
  1951.     else
  1952.         pts = dts;
  1953.  
  1954.     return pts;
  1955. }
  1956.  
  1957. static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
  1958. {
  1959.     int size = 0;
  1960.     const uint8_t *data;
  1961.     uint32_t flags;
  1962.  
  1963.     if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
  1964.         return;
  1965.  
  1966.     data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
  1967.     if (!data || size < 4)
  1968.         return;
  1969.     flags = bytestream_get_le32(&data);
  1970.     size -= 4;
  1971.     if (size < 4) /* Required for any of the changes */
  1972.         return;
  1973.     if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
  1974.         avctx->channels = bytestream_get_le32(&data);
  1975.         size -= 4;
  1976.     }
  1977.     if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
  1978.         if (size < 8)
  1979.             return;
  1980.         avctx->channel_layout = bytestream_get_le64(&data);
  1981.         size -= 8;
  1982.     }
  1983.     if (size < 4)
  1984.         return;
  1985.     if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
  1986.         avctx->sample_rate = bytestream_get_le32(&data);
  1987.         size -= 4;
  1988.     }
  1989.     if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
  1990.         if (size < 8)
  1991.             return;
  1992.         avctx->width  = bytestream_get_le32(&data);
  1993.         avctx->height = bytestream_get_le32(&data);
  1994.         avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  1995.         size -= 8;
  1996.     }
  1997. }
  1998.  
  1999. static int add_metadata_from_side_data(AVCodecContext *avctx, AVFrame *frame)
  2000. {
  2001.     int size, ret = 0;
  2002.     const uint8_t *side_metadata;
  2003.     const uint8_t *end;
  2004.  
  2005.     side_metadata = av_packet_get_side_data(avctx->pkt,
  2006.                                             AV_PKT_DATA_STRINGS_METADATA, &size);
  2007.     if (!side_metadata)
  2008.         goto end;
  2009.     end = side_metadata + size;
  2010.     if (size && end[-1])
  2011.         return AVERROR_INVALIDDATA;
  2012.     while (side_metadata < end) {
  2013.         const uint8_t *key = side_metadata;
  2014.         const uint8_t *val = side_metadata + strlen(key) + 1;
  2015.         int ret;
  2016.  
  2017.         if (val >= end)
  2018.             return AVERROR_INVALIDDATA;
  2019.  
  2020.         ret = av_dict_set(avpriv_frame_get_metadatap(frame), key, val, 0);
  2021.         if (ret < 0)
  2022.             break;
  2023.         side_metadata = val + strlen(val) + 1;
  2024.     }
  2025. end:
  2026.     return ret;
  2027. }
  2028.  
  2029. int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  2030.                                               int *got_picture_ptr,
  2031.                                               const AVPacket *avpkt)
  2032. {
  2033.     AVCodecInternal *avci = avctx->internal;
  2034.     int ret;
  2035.     // copy to ensure we do not change avpkt
  2036.     AVPacket tmp = *avpkt;
  2037.  
  2038.     if (!avctx->codec)
  2039.         return AVERROR(EINVAL);
  2040.     if (avctx->codec->type != AVMEDIA_TYPE_VIDEO) {
  2041.         av_log(avctx, AV_LOG_ERROR, "Invalid media type for video\n");
  2042.         return AVERROR(EINVAL);
  2043.     }
  2044.  
  2045.     *got_picture_ptr = 0;
  2046.     if ((avctx->coded_width || avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
  2047.         return AVERROR(EINVAL);
  2048.  
  2049.     avcodec_get_frame_defaults(picture);
  2050.  
  2051.     if (!avctx->refcounted_frames)
  2052.         av_frame_unref(&avci->to_free);
  2053.  
  2054.     if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
  2055.         int did_split = av_packet_split_side_data(&tmp);
  2056.         apply_param_change(avctx, &tmp);
  2057.         avctx->pkt = &tmp;
  2058.         if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  2059.             ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
  2060.                                          &tmp);
  2061.         else {
  2062.             ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  2063.                                        &tmp);
  2064.             picture->pkt_dts = avpkt->dts;
  2065.  
  2066.             if(!avctx->has_b_frames){
  2067.                 av_frame_set_pkt_pos(picture, avpkt->pos);
  2068.             }
  2069.             //FIXME these should be under if(!avctx->has_b_frames)
  2070.             /* get_buffer is supposed to set frame parameters */
  2071.             if (!(avctx->codec->capabilities & CODEC_CAP_DR1)) {
  2072.                 if (!picture->sample_aspect_ratio.num)    picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
  2073.                 if (!picture->width)                      picture->width               = avctx->width;
  2074.                 if (!picture->height)                     picture->height              = avctx->height;
  2075.                 if (picture->format == AV_PIX_FMT_NONE)   picture->format              = avctx->pix_fmt;
  2076.             }
  2077.         }
  2078.         add_metadata_from_side_data(avctx, picture);
  2079.  
  2080.         emms_c(); //needed to avoid an emms_c() call before every return;
  2081.  
  2082.         avctx->pkt = NULL;
  2083.         if (did_split) {
  2084.             av_packet_free_side_data(&tmp);
  2085.             if(ret == tmp.size)
  2086.                 ret = avpkt->size;
  2087.         }
  2088.  
  2089.         if (ret < 0 && picture->data[0])
  2090.             av_frame_unref(picture);
  2091.  
  2092.         if (*got_picture_ptr) {
  2093.             if (!avctx->refcounted_frames) {
  2094.                 avci->to_free = *picture;
  2095.                 avci->to_free.extended_data = avci->to_free.data;
  2096.                 memset(picture->buf, 0, sizeof(picture->buf));
  2097.             }
  2098.  
  2099.             avctx->frame_number++;
  2100.             av_frame_set_best_effort_timestamp(picture,
  2101.                                                guess_correct_pts(avctx,
  2102.                                                                  picture->pkt_pts,
  2103.                                                                  picture->pkt_dts));
  2104.         }
  2105.     } else
  2106.         ret = 0;
  2107.  
  2108.     /* many decoders assign whole AVFrames, thus overwriting extended_data;
  2109.      * make sure it's set correctly */
  2110.     picture->extended_data = picture->data;
  2111.  
  2112.     return ret;
  2113. }
  2114.  
  2115. #if FF_API_OLD_DECODE_AUDIO
  2116. int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
  2117.                                               int *frame_size_ptr,
  2118.                                               AVPacket *avpkt)
  2119. {
  2120.     AVFrame frame = { { 0 } };
  2121.     int ret, got_frame = 0;
  2122.  
  2123.     if (avctx->get_buffer != avcodec_default_get_buffer) {
  2124.         av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
  2125.                                     "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
  2126.         av_log(avctx, AV_LOG_ERROR, "Please port your application to "
  2127.                                     "avcodec_decode_audio4()\n");
  2128.         avctx->get_buffer = avcodec_default_get_buffer;
  2129.         avctx->release_buffer = avcodec_default_release_buffer;
  2130.     }
  2131.  
  2132.     ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
  2133.  
  2134.     if (ret >= 0 && got_frame) {
  2135.         int ch, plane_size;
  2136.         int planar    = av_sample_fmt_is_planar(avctx->sample_fmt);
  2137.         int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
  2138.                                                    frame.nb_samples,
  2139.                                                    avctx->sample_fmt, 1);
  2140.         if (*frame_size_ptr < data_size) {
  2141.             av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
  2142.                                         "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
  2143.             return AVERROR(EINVAL);
  2144.         }
  2145.  
  2146.         memcpy(samples, frame.extended_data[0], plane_size);
  2147.  
  2148.         if (planar && avctx->channels > 1) {
  2149.             uint8_t *out = ((uint8_t *)samples) + plane_size;
  2150.             for (ch = 1; ch < avctx->channels; ch++) {
  2151.                 memcpy(out, frame.extended_data[ch], plane_size);
  2152.                 out += plane_size;
  2153.             }
  2154.         }
  2155.         *frame_size_ptr = data_size;
  2156.     } else {
  2157.         *frame_size_ptr = 0;
  2158.     }
  2159.     return ret;
  2160. }
  2161.  
  2162. #endif
  2163.  
  2164. int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
  2165.                                               AVFrame *frame,
  2166.                                               int *got_frame_ptr,
  2167.                                               const AVPacket *avpkt)
  2168. {
  2169.     AVCodecInternal *avci = avctx->internal;
  2170.     int planar, channels;
  2171.     int ret = 0;
  2172.  
  2173.     *got_frame_ptr = 0;
  2174.  
  2175.     if (!avpkt->data && avpkt->size) {
  2176.         av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
  2177.         return AVERROR(EINVAL);
  2178.     }
  2179.     if (!avctx->codec)
  2180.         return AVERROR(EINVAL);
  2181.     if (avctx->codec->type != AVMEDIA_TYPE_AUDIO) {
  2182.         av_log(avctx, AV_LOG_ERROR, "Invalid media type for audio\n");
  2183.         return AVERROR(EINVAL);
  2184.     }
  2185.  
  2186.     avcodec_get_frame_defaults(frame);
  2187.  
  2188.     if (!avctx->refcounted_frames)
  2189.         av_frame_unref(&avci->to_free);
  2190.  
  2191.     if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
  2192.         uint8_t *side;
  2193.         int side_size;
  2194.         uint32_t discard_padding = 0;
  2195.         // copy to ensure we do not change avpkt
  2196.         AVPacket tmp = *avpkt;
  2197.         int did_split = av_packet_split_side_data(&tmp);
  2198.         apply_param_change(avctx, &tmp);
  2199.  
  2200.         avctx->pkt = &tmp;
  2201.         if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  2202.             ret = ff_thread_decode_frame(avctx, frame, got_frame_ptr, &tmp);
  2203.         else {
  2204.             ret = avctx->codec->decode(avctx, frame, got_frame_ptr, &tmp);
  2205.             frame->pkt_dts = avpkt->dts;
  2206.         }
  2207.         if (ret >= 0 && *got_frame_ptr) {
  2208.             add_metadata_from_side_data(avctx, frame);
  2209.             avctx->frame_number++;
  2210.             av_frame_set_best_effort_timestamp(frame,
  2211.                                                guess_correct_pts(avctx,
  2212.                                                                  frame->pkt_pts,
  2213.                                                                  frame->pkt_dts));
  2214.             if (frame->format == AV_SAMPLE_FMT_NONE)
  2215.                 frame->format = avctx->sample_fmt;
  2216.             if (!frame->channel_layout)
  2217.                 frame->channel_layout = avctx->channel_layout;
  2218.             if (!av_frame_get_channels(frame))
  2219.                 av_frame_set_channels(frame, avctx->channels);
  2220.             if (!frame->sample_rate)
  2221.                 frame->sample_rate = avctx->sample_rate;
  2222.         }
  2223.  
  2224.         side= av_packet_get_side_data(avctx->pkt, AV_PKT_DATA_SKIP_SAMPLES, &side_size);
  2225.         if(side && side_size>=10) {
  2226.             avctx->internal->skip_samples = AV_RL32(side);
  2227.             av_log(avctx, AV_LOG_DEBUG, "skip %d samples due to side data\n",
  2228.                    avctx->internal->skip_samples);
  2229.             discard_padding = AV_RL32(side + 4);
  2230.         }
  2231.         if (avctx->internal->skip_samples && *got_frame_ptr) {
  2232.             if(frame->nb_samples <= avctx->internal->skip_samples){
  2233.                 *got_frame_ptr = 0;
  2234.                 avctx->internal->skip_samples -= frame->nb_samples;
  2235.                 av_log(avctx, AV_LOG_DEBUG, "skip whole frame, skip left: %d\n",
  2236.                        avctx->internal->skip_samples);
  2237.             } else {
  2238.                 av_samples_copy(frame->extended_data, frame->extended_data, 0, avctx->internal->skip_samples,
  2239.                                 frame->nb_samples - avctx->internal->skip_samples, avctx->channels, frame->format);
  2240.                 if(avctx->pkt_timebase.num && avctx->sample_rate) {
  2241.                     int64_t diff_ts = av_rescale_q(avctx->internal->skip_samples,
  2242.                                                    (AVRational){1, avctx->sample_rate},
  2243.                                                    avctx->pkt_timebase);
  2244.                     if(frame->pkt_pts!=AV_NOPTS_VALUE)
  2245.                         frame->pkt_pts += diff_ts;
  2246.                     if(frame->pkt_dts!=AV_NOPTS_VALUE)
  2247.                         frame->pkt_dts += diff_ts;
  2248.                     if (av_frame_get_pkt_duration(frame) >= diff_ts)
  2249.                         av_frame_set_pkt_duration(frame, av_frame_get_pkt_duration(frame) - diff_ts);
  2250.                 } else {
  2251.                     av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for skipped samples.\n");
  2252.                 }
  2253.                 av_log(avctx, AV_LOG_DEBUG, "skip %d/%d samples\n",
  2254.                        avctx->internal->skip_samples, frame->nb_samples);
  2255.                 frame->nb_samples -= avctx->internal->skip_samples;
  2256.                 avctx->internal->skip_samples = 0;
  2257.             }
  2258.         }
  2259.  
  2260.         if (discard_padding > 0 && discard_padding <= frame->nb_samples && *got_frame_ptr) {
  2261.             if (discard_padding == frame->nb_samples) {
  2262.                 *got_frame_ptr = 0;
  2263.             } else {
  2264.                 if(avctx->pkt_timebase.num && avctx->sample_rate) {
  2265.                     int64_t diff_ts = av_rescale_q(frame->nb_samples - discard_padding,
  2266.                                                    (AVRational){1, avctx->sample_rate},
  2267.                                                    avctx->pkt_timebase);
  2268.                     if (av_frame_get_pkt_duration(frame) >= diff_ts)
  2269.                         av_frame_set_pkt_duration(frame, av_frame_get_pkt_duration(frame) - diff_ts);
  2270.                 } else {
  2271.                     av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for discarded samples.\n");
  2272.                 }
  2273.                 av_log(avctx, AV_LOG_DEBUG, "discard %d/%d samples\n",
  2274.                        discard_padding, frame->nb_samples);
  2275.                 frame->nb_samples -= discard_padding;
  2276.             }
  2277.         }
  2278.  
  2279.         avctx->pkt = NULL;
  2280.         if (did_split) {
  2281.             av_packet_free_side_data(&tmp);
  2282.             if(ret == tmp.size)
  2283.                 ret = avpkt->size;
  2284.         }
  2285.  
  2286.         if (ret >= 0 && *got_frame_ptr) {
  2287.             if (!avctx->refcounted_frames) {
  2288.                 avci->to_free = *frame;
  2289.                 avci->to_free.extended_data = avci->to_free.data;
  2290.                 memset(frame->buf, 0, sizeof(frame->buf));
  2291.                 frame->extended_buf    = NULL;
  2292.                 frame->nb_extended_buf = 0;
  2293.             }
  2294.         } else if (frame->data[0])
  2295.             av_frame_unref(frame);
  2296.     }
  2297.  
  2298.     /* many decoders assign whole AVFrames, thus overwriting extended_data;
  2299.      * make sure it's set correctly; assume decoders that actually use
  2300.      * extended_data are doing it correctly */
  2301.     if (*got_frame_ptr) {
  2302.         planar   = av_sample_fmt_is_planar(frame->format);
  2303.         channels = av_frame_get_channels(frame);
  2304.         if (!(planar && channels > AV_NUM_DATA_POINTERS))
  2305.             frame->extended_data = frame->data;
  2306.     } else {
  2307.         frame->extended_data = NULL;
  2308.     }
  2309.  
  2310.     return ret;
  2311. }
  2312.  
  2313. #define UTF8_MAX_BYTES 4 /* 5 and 6 bytes sequences should not be used */
  2314. static int recode_subtitle(AVCodecContext *avctx,
  2315.                            AVPacket *outpkt, const AVPacket *inpkt)
  2316. {
  2317. #if CONFIG_ICONV
  2318.     iconv_t cd = (iconv_t)-1;
  2319.     int ret = 0;
  2320.     char *inb, *outb;
  2321.     size_t inl, outl;
  2322.     AVPacket tmp;
  2323. #endif
  2324.  
  2325.     if (avctx->sub_charenc_mode != FF_SUB_CHARENC_MODE_PRE_DECODER || inpkt->size == 0)
  2326.         return 0;
  2327.  
  2328. #if CONFIG_ICONV
  2329.     cd = iconv_open("UTF-8", avctx->sub_charenc);
  2330.     av_assert0(cd != (iconv_t)-1);
  2331.  
  2332.     inb = inpkt->data;
  2333.     inl = inpkt->size;
  2334.  
  2335.     if (inl >= INT_MAX / UTF8_MAX_BYTES - FF_INPUT_BUFFER_PADDING_SIZE) {
  2336.         av_log(avctx, AV_LOG_ERROR, "Subtitles packet is too big for recoding\n");
  2337.         ret = AVERROR(ENOMEM);
  2338.         goto end;
  2339.     }
  2340.  
  2341.     ret = av_new_packet(&tmp, inl * UTF8_MAX_BYTES);
  2342.     if (ret < 0)
  2343.         goto end;
  2344.     outpkt->buf  = tmp.buf;
  2345.     outpkt->data = tmp.data;
  2346.     outpkt->size = tmp.size;
  2347.     outb = outpkt->data;
  2348.     outl = outpkt->size;
  2349.  
  2350.     if (iconv(cd, &inb, &inl, &outb, &outl) == (size_t)-1 ||
  2351.         iconv(cd, NULL, NULL, &outb, &outl) == (size_t)-1 ||
  2352.         outl >= outpkt->size || inl != 0) {
  2353.         av_log(avctx, AV_LOG_ERROR, "Unable to recode subtitle event \"%s\" "
  2354.                "from %s to UTF-8\n", inpkt->data, avctx->sub_charenc);
  2355.         av_free_packet(&tmp);
  2356.         ret = AVERROR(errno);
  2357.         goto end;
  2358.     }
  2359.     outpkt->size -= outl;
  2360.     memset(outpkt->data + outpkt->size, 0, outl);
  2361.  
  2362. end:
  2363.     if (cd != (iconv_t)-1)
  2364.         iconv_close(cd);
  2365.     return ret;
  2366. #else
  2367.     av_assert0(!"requesting subtitles recoding without iconv");
  2368. #endif
  2369. }
  2370.  
  2371. static int utf8_check(const uint8_t *str)
  2372. {
  2373.     const uint8_t *byte;
  2374.     uint32_t codepoint, min;
  2375.  
  2376.     while (*str) {
  2377.         byte = str;
  2378.         GET_UTF8(codepoint, *(byte++), return 0;);
  2379.         min = byte - str == 1 ? 0 : byte - str == 2 ? 0x80 :
  2380.               1 << (5 * (byte - str) - 4);
  2381.         if (codepoint < min || codepoint >= 0x110000 ||
  2382.             codepoint == 0xFFFE /* BOM */ ||
  2383.             codepoint >= 0xD800 && codepoint <= 0xDFFF /* surrogates */)
  2384.             return 0;
  2385.         str = byte;
  2386.     }
  2387.     return 1;
  2388. }
  2389.  
  2390. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  2391.                              int *got_sub_ptr,
  2392.                              AVPacket *avpkt)
  2393. {
  2394.     int i, ret = 0;
  2395.  
  2396.     if (!avpkt->data && avpkt->size) {
  2397.         av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
  2398.         return AVERROR(EINVAL);
  2399.     }
  2400.     if (!avctx->codec)
  2401.         return AVERROR(EINVAL);
  2402.     if (avctx->codec->type != AVMEDIA_TYPE_SUBTITLE) {
  2403.         av_log(avctx, AV_LOG_ERROR, "Invalid media type for subtitles\n");
  2404.         return AVERROR(EINVAL);
  2405.     }
  2406.  
  2407.     *got_sub_ptr = 0;
  2408.     avcodec_get_subtitle_defaults(sub);
  2409.  
  2410.     if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
  2411.         AVPacket pkt_recoded;
  2412.         AVPacket tmp = *avpkt;
  2413.         int did_split = av_packet_split_side_data(&tmp);
  2414.         //apply_param_change(avctx, &tmp);
  2415.  
  2416.         pkt_recoded = tmp;
  2417.         ret = recode_subtitle(avctx, &pkt_recoded, &tmp);
  2418.         if (ret < 0) {
  2419.             *got_sub_ptr = 0;
  2420.         } else {
  2421.             avctx->pkt = &pkt_recoded;
  2422.  
  2423.             if (avctx->pkt_timebase.den && avpkt->pts != AV_NOPTS_VALUE)
  2424.                 sub->pts = av_rescale_q(avpkt->pts,
  2425.                                         avctx->pkt_timebase, AV_TIME_BASE_Q);
  2426.             ret = avctx->codec->decode(avctx, sub, got_sub_ptr, &pkt_recoded);
  2427.             av_assert1((ret >= 0) >= !!*got_sub_ptr &&
  2428.                        !!*got_sub_ptr >= !!sub->num_rects);
  2429.  
  2430.             if (sub->num_rects && !sub->end_display_time && avpkt->duration &&
  2431.                 avctx->pkt_timebase.num) {
  2432.                 AVRational ms = { 1, 1000 };
  2433.                 sub->end_display_time = av_rescale_q(avpkt->duration,
  2434.                                                      avctx->pkt_timebase, ms);
  2435.             }
  2436.  
  2437.             for (i = 0; i < sub->num_rects; i++) {
  2438.                 if (sub->rects[i]->ass && !utf8_check(sub->rects[i]->ass)) {
  2439.                     av_log(avctx, AV_LOG_ERROR,
  2440.                            "Invalid UTF-8 in decoded subtitles text; "
  2441.                            "maybe missing -sub_charenc option\n");
  2442.                     avsubtitle_free(sub);
  2443.                     return AVERROR_INVALIDDATA;
  2444.                 }
  2445.             }
  2446.  
  2447.             if (tmp.data != pkt_recoded.data) { // did we recode?
  2448.                 /* prevent from destroying side data from original packet */
  2449.                 pkt_recoded.side_data = NULL;
  2450.                 pkt_recoded.side_data_elems = 0;
  2451.  
  2452.                 av_free_packet(&pkt_recoded);
  2453.             }
  2454.             if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB)
  2455.                 sub->format = 0;
  2456.             else if (avctx->codec_descriptor->props & AV_CODEC_PROP_TEXT_SUB)
  2457.                 sub->format = 1;
  2458.             avctx->pkt = NULL;
  2459.         }
  2460.  
  2461.         if (did_split) {
  2462.             av_packet_free_side_data(&tmp);
  2463.             if(ret == tmp.size)
  2464.                 ret = avpkt->size;
  2465.         }
  2466.  
  2467.         if (*got_sub_ptr)
  2468.             avctx->frame_number++;
  2469.     }
  2470.  
  2471.     return ret;
  2472. }
  2473.  
  2474. void avsubtitle_free(AVSubtitle *sub)
  2475. {
  2476.     int i;
  2477.  
  2478.     for (i = 0; i < sub->num_rects; i++) {
  2479.         av_freep(&sub->rects[i]->pict.data[0]);
  2480.         av_freep(&sub->rects[i]->pict.data[1]);
  2481.         av_freep(&sub->rects[i]->pict.data[2]);
  2482.         av_freep(&sub->rects[i]->pict.data[3]);
  2483.         av_freep(&sub->rects[i]->text);
  2484.         av_freep(&sub->rects[i]->ass);
  2485.         av_freep(&sub->rects[i]);
  2486.     }
  2487.  
  2488.     av_freep(&sub->rects);
  2489.  
  2490.     memset(sub, 0, sizeof(AVSubtitle));
  2491. }
  2492.  
  2493. av_cold int ff_codec_close_recursive(AVCodecContext *avctx)
  2494. {
  2495.     int ret = 0;
  2496.  
  2497.     ff_unlock_avcodec();
  2498.  
  2499.     ret = avcodec_close(avctx);
  2500.  
  2501.     ff_lock_avcodec(NULL);
  2502.     return ret;
  2503. }
  2504.  
  2505. av_cold int avcodec_close(AVCodecContext *avctx)
  2506. {
  2507.     int ret;
  2508.  
  2509.     if (!avctx)
  2510.         return 0;
  2511.  
  2512.     ret = ff_lock_avcodec(avctx);
  2513.     if (ret < 0)
  2514.         return ret;
  2515.  
  2516.     if (avcodec_is_open(avctx)) {
  2517.         FramePool *pool = avctx->internal->pool;
  2518.         int i;
  2519.         if (CONFIG_FRAME_THREAD_ENCODER &&
  2520.             avctx->internal->frame_thread_encoder && avctx->thread_count > 1) {
  2521.             ff_unlock_avcodec();
  2522.             ff_frame_thread_encoder_free(avctx);
  2523.             ff_lock_avcodec(avctx);
  2524.         }
  2525.         if (HAVE_THREADS && avctx->thread_opaque)
  2526.             ff_thread_free(avctx);
  2527.         if (avctx->codec && avctx->codec->close)
  2528.             avctx->codec->close(avctx);
  2529.         avctx->coded_frame = NULL;
  2530.         avctx->internal->byte_buffer_size = 0;
  2531.         av_freep(&avctx->internal->byte_buffer);
  2532.         if (!avctx->refcounted_frames)
  2533.             av_frame_unref(&avctx->internal->to_free);
  2534.         for (i = 0; i < FF_ARRAY_ELEMS(pool->pools); i++)
  2535.             av_buffer_pool_uninit(&pool->pools[i]);
  2536.         av_freep(&avctx->internal->pool);
  2537.         av_freep(&avctx->internal);
  2538.     }
  2539.  
  2540.     if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
  2541.         av_opt_free(avctx->priv_data);
  2542.     av_opt_free(avctx);
  2543.     av_freep(&avctx->priv_data);
  2544.     if (av_codec_is_encoder(avctx->codec))
  2545.         av_freep(&avctx->extradata);
  2546.     avctx->codec = NULL;
  2547.     avctx->active_thread_type = 0;
  2548.  
  2549.     ff_unlock_avcodec();
  2550.     return 0;
  2551. }
  2552.  
  2553. static enum AVCodecID remap_deprecated_codec_id(enum AVCodecID id)
  2554. {
  2555.     switch(id){
  2556.         //This is for future deprecatec codec ids, its empty since
  2557.         //last major bump but will fill up again over time, please don't remove it
  2558. //         case AV_CODEC_ID_UTVIDEO_DEPRECATED: return AV_CODEC_ID_UTVIDEO;
  2559.         case AV_CODEC_ID_OPUS_DEPRECATED: return AV_CODEC_ID_OPUS;
  2560.         case AV_CODEC_ID_TAK_DEPRECATED : return AV_CODEC_ID_TAK;
  2561.         case AV_CODEC_ID_PCM_S24LE_PLANAR_DEPRECATED : return AV_CODEC_ID_PCM_S24LE_PLANAR;
  2562.         case AV_CODEC_ID_PCM_S32LE_PLANAR_DEPRECATED : return AV_CODEC_ID_PCM_S32LE_PLANAR;
  2563.         case AV_CODEC_ID_ESCAPE130_DEPRECATED : return AV_CODEC_ID_ESCAPE130;
  2564.         case AV_CODEC_ID_G2M_DEPRECATED : return AV_CODEC_ID_G2M;
  2565.         case AV_CODEC_ID_WEBP_DEPRECATED: return AV_CODEC_ID_WEBP;
  2566.         default                         : return id;
  2567.     }
  2568. }
  2569.  
  2570. static AVCodec *find_encdec(enum AVCodecID id, int encoder)
  2571. {
  2572.     AVCodec *p, *experimental = NULL;
  2573.     p = first_avcodec;
  2574.     id= remap_deprecated_codec_id(id);
  2575.     while (p) {
  2576.         if ((encoder ? av_codec_is_encoder(p) : av_codec_is_decoder(p)) &&
  2577.             p->id == id) {
  2578.             if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  2579.                 experimental = p;
  2580.             } else
  2581.                 return p;
  2582.         }
  2583.         p = p->next;
  2584.     }
  2585.     return experimental;
  2586. }
  2587.  
  2588. AVCodec *avcodec_find_encoder(enum AVCodecID id)
  2589. {
  2590.     return find_encdec(id, 1);
  2591. }
  2592.  
  2593. AVCodec *avcodec_find_encoder_by_name(const char *name)
  2594. {
  2595.     AVCodec *p;
  2596.     if (!name)
  2597.         return NULL;
  2598.     p = first_avcodec;
  2599.     while (p) {
  2600.         if (av_codec_is_encoder(p) && strcmp(name, p->name) == 0)
  2601.             return p;
  2602.         p = p->next;
  2603.     }
  2604.     return NULL;
  2605. }
  2606.  
  2607. AVCodec *avcodec_find_decoder(enum AVCodecID id)
  2608. {
  2609.     return find_encdec(id, 0);
  2610. }
  2611.  
  2612. AVCodec *avcodec_find_decoder_by_name(const char *name)
  2613. {
  2614.     AVCodec *p;
  2615.     if (!name)
  2616.         return NULL;
  2617.     p = first_avcodec;
  2618.     while (p) {
  2619.         if (av_codec_is_decoder(p) && strcmp(name, p->name) == 0)
  2620.             return p;
  2621.         p = p->next;
  2622.     }
  2623.     return NULL;
  2624. }
  2625.  
  2626. const char *avcodec_get_name(enum AVCodecID id)
  2627. {
  2628.     const AVCodecDescriptor *cd;
  2629.     AVCodec *codec;
  2630.  
  2631.     if (id == AV_CODEC_ID_NONE)
  2632.         return "none";
  2633.     cd = avcodec_descriptor_get(id);
  2634.     if (cd)
  2635.         return cd->name;
  2636.     av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
  2637.     codec = avcodec_find_decoder(id);
  2638.     if (codec)
  2639.         return codec->name;
  2640.     codec = avcodec_find_encoder(id);
  2641.     if (codec)
  2642.         return codec->name;
  2643.     return "unknown_codec";
  2644. }
  2645.  
  2646. size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
  2647. {
  2648.     int i, len, ret = 0;
  2649.  
  2650. #define TAG_PRINT(x)                                              \
  2651.     (((x) >= '0' && (x) <= '9') ||                                \
  2652.      ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') ||  \
  2653.      ((x) == '.' || (x) == ' ' || (x) == '-' || (x) == '_'))
  2654.  
  2655.     for (i = 0; i < 4; i++) {
  2656.         len = snprintf(buf, buf_size,
  2657.                        TAG_PRINT(codec_tag & 0xFF) ? "%c" : "[%d]", codec_tag & 0xFF);
  2658.         buf        += len;
  2659.         buf_size    = buf_size > len ? buf_size - len : 0;
  2660.         ret        += len;
  2661.         codec_tag >>= 8;
  2662.     }
  2663.     return ret;
  2664. }
  2665.  
  2666. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  2667. {
  2668.     const char *codec_type;
  2669.     const char *codec_name;
  2670.     const char *profile = NULL;
  2671.     const AVCodec *p;
  2672.     int bitrate;
  2673.     AVRational display_aspect_ratio;
  2674.  
  2675.     if (!buf || buf_size <= 0)
  2676.         return;
  2677.     codec_type = av_get_media_type_string(enc->codec_type);
  2678.     codec_name = avcodec_get_name(enc->codec_id);
  2679.     if (enc->profile != FF_PROFILE_UNKNOWN) {
  2680.         if (enc->codec)
  2681.             p = enc->codec;
  2682.         else
  2683.             p = encode ? avcodec_find_encoder(enc->codec_id) :
  2684.                         avcodec_find_decoder(enc->codec_id);
  2685.         if (p)
  2686.             profile = av_get_profile_name(p, enc->profile);
  2687.     }
  2688.  
  2689.     snprintf(buf, buf_size, "%s: %s", codec_type ? codec_type : "unknown",
  2690.              codec_name);
  2691.     buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
  2692.  
  2693.     if (enc->codec && strcmp(enc->codec->name, codec_name))
  2694.         snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", enc->codec->name);
  2695.  
  2696.     if (profile)
  2697.         snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
  2698.     if (enc->codec_tag) {
  2699.         char tag_buf[32];
  2700.         av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  2701.         snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2702.                  " (%s / 0x%04X)", tag_buf, enc->codec_tag);
  2703.     }
  2704.  
  2705.     switch (enc->codec_type) {
  2706.     case AVMEDIA_TYPE_VIDEO:
  2707.         if (enc->pix_fmt != AV_PIX_FMT_NONE) {
  2708.             char detail[256] = "(";
  2709.             const char *colorspace_name;
  2710.             snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2711.                      ", %s",
  2712.                      av_get_pix_fmt_name(enc->pix_fmt));
  2713.             if (enc->bits_per_raw_sample &&
  2714.                 enc->bits_per_raw_sample <= av_pix_fmt_desc_get(enc->pix_fmt)->comp[0].depth_minus1)
  2715.                 av_strlcatf(detail, sizeof(detail), "%d bpc, ", enc->bits_per_raw_sample);
  2716.             if (enc->color_range != AVCOL_RANGE_UNSPECIFIED)
  2717.                 av_strlcatf(detail, sizeof(detail),
  2718.                             enc->color_range == AVCOL_RANGE_MPEG ? "tv, ": "pc, ");
  2719.  
  2720.             colorspace_name = av_get_colorspace_name(enc->colorspace);
  2721.             if (colorspace_name)
  2722.                 av_strlcatf(detail, sizeof(detail), "%s, ", colorspace_name);
  2723.  
  2724.             if (strlen(detail) > 1) {
  2725.                 detail[strlen(detail) - 2] = 0;
  2726.                 av_strlcatf(buf, buf_size, "%s)", detail);
  2727.             }
  2728.         }
  2729.         if (enc->width) {
  2730.             snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2731.                      ", %dx%d",
  2732.                      enc->width, enc->height);
  2733.             if (enc->sample_aspect_ratio.num) {
  2734.                 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  2735.                           enc->width * enc->sample_aspect_ratio.num,
  2736.                           enc->height * enc->sample_aspect_ratio.den,
  2737.                           1024 * 1024);
  2738.                 snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2739.                          " [SAR %d:%d DAR %d:%d]",
  2740.                          enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  2741.                          display_aspect_ratio.num, display_aspect_ratio.den);
  2742.             }
  2743.             if (av_log_get_level() >= AV_LOG_DEBUG) {
  2744.                 int g = av_gcd(enc->time_base.num, enc->time_base.den);
  2745.                 snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2746.                          ", %d/%d",
  2747.                          enc->time_base.num / g, enc->time_base.den / g);
  2748.             }
  2749.         }
  2750.         if (encode) {
  2751.             snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2752.                      ", q=%d-%d", enc->qmin, enc->qmax);
  2753.         }
  2754.         break;
  2755.     case AVMEDIA_TYPE_AUDIO:
  2756.         if (enc->sample_rate) {
  2757.             snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2758.                      ", %d Hz", enc->sample_rate);
  2759.         }
  2760.         av_strlcat(buf, ", ", buf_size);
  2761.         av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  2762.         if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
  2763.             snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2764.                      ", %s", av_get_sample_fmt_name(enc->sample_fmt));
  2765.         }
  2766.         break;
  2767.     case AVMEDIA_TYPE_DATA:
  2768.         if (av_log_get_level() >= AV_LOG_DEBUG) {
  2769.             int g = av_gcd(enc->time_base.num, enc->time_base.den);
  2770.             if (g)
  2771.                 snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2772.                          ", %d/%d",
  2773.                          enc->time_base.num / g, enc->time_base.den / g);
  2774.         }
  2775.         break;
  2776.     case AVMEDIA_TYPE_SUBTITLE:
  2777.         if (enc->width)
  2778.             snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2779.                      ", %dx%d", enc->width, enc->height);
  2780.         break;
  2781.     default:
  2782.         return;
  2783.     }
  2784.     if (encode) {
  2785.         if (enc->flags & CODEC_FLAG_PASS1)
  2786.             snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2787.                      ", pass 1");
  2788.         if (enc->flags & CODEC_FLAG_PASS2)
  2789.             snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2790.                      ", pass 2");
  2791.     }
  2792.     bitrate = get_bit_rate(enc);
  2793.     if (bitrate != 0) {
  2794.         snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2795.                  ", %d kb/s", bitrate / 1000);
  2796.     } else if (enc->rc_max_rate > 0) {
  2797.         snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2798.                  ", max. %d kb/s", enc->rc_max_rate / 1000);
  2799.     }
  2800. }
  2801.  
  2802. const char *av_get_profile_name(const AVCodec *codec, int profile)
  2803. {
  2804.     const AVProfile *p;
  2805.     if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
  2806.         return NULL;
  2807.  
  2808.     for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  2809.         if (p->profile == profile)
  2810.             return p->name;
  2811.  
  2812.     return NULL;
  2813. }
  2814.  
  2815. unsigned avcodec_version(void)
  2816. {
  2817. //    av_assert0(AV_CODEC_ID_V410==164);
  2818.     av_assert0(AV_CODEC_ID_PCM_S8_PLANAR==65563);
  2819.     av_assert0(AV_CODEC_ID_ADPCM_G722==69660);
  2820. //     av_assert0(AV_CODEC_ID_BMV_AUDIO==86071);
  2821.     av_assert0(AV_CODEC_ID_SRT==94216);
  2822.     av_assert0(LIBAVCODEC_VERSION_MICRO >= 100);
  2823.  
  2824.     av_assert0(CODEC_ID_CLLC == AV_CODEC_ID_CLLC);
  2825.     av_assert0(CODEC_ID_PCM_S8_PLANAR == AV_CODEC_ID_PCM_S8_PLANAR);
  2826.     av_assert0(CODEC_ID_ADPCM_IMA_APC == AV_CODEC_ID_ADPCM_IMA_APC);
  2827.     av_assert0(CODEC_ID_ILBC == AV_CODEC_ID_ILBC);
  2828.     av_assert0(CODEC_ID_SRT == AV_CODEC_ID_SRT);
  2829.     return LIBAVCODEC_VERSION_INT;
  2830. }
  2831.  
  2832. const char *avcodec_configuration(void)
  2833. {
  2834.     return FFMPEG_CONFIGURATION;
  2835. }
  2836.  
  2837. const char *avcodec_license(void)
  2838. {
  2839. #define LICENSE_PREFIX "libavcodec license: "
  2840.     return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  2841. }
  2842.  
  2843. void avcodec_flush_buffers(AVCodecContext *avctx)
  2844. {
  2845.     if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  2846.         ff_thread_flush(avctx);
  2847.     else if (avctx->codec->flush)
  2848.         avctx->codec->flush(avctx);
  2849.  
  2850.     avctx->pts_correction_last_pts =
  2851.     avctx->pts_correction_last_dts = INT64_MIN;
  2852.  
  2853.     if (!avctx->refcounted_frames)
  2854.         av_frame_unref(&avctx->internal->to_free);
  2855. }
  2856.  
  2857. int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
  2858. {
  2859.     switch (codec_id) {
  2860.     case AV_CODEC_ID_8SVX_EXP:
  2861.     case AV_CODEC_ID_8SVX_FIB:
  2862.     case AV_CODEC_ID_ADPCM_CT:
  2863.     case AV_CODEC_ID_ADPCM_IMA_APC:
  2864.     case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
  2865.     case AV_CODEC_ID_ADPCM_IMA_OKI:
  2866.     case AV_CODEC_ID_ADPCM_IMA_WS:
  2867.     case AV_CODEC_ID_ADPCM_G722:
  2868.     case AV_CODEC_ID_ADPCM_YAMAHA:
  2869.         return 4;
  2870.     case AV_CODEC_ID_PCM_ALAW:
  2871.     case AV_CODEC_ID_PCM_MULAW:
  2872.     case AV_CODEC_ID_PCM_S8:
  2873.     case AV_CODEC_ID_PCM_S8_PLANAR:
  2874.     case AV_CODEC_ID_PCM_U8:
  2875.     case AV_CODEC_ID_PCM_ZORK:
  2876.         return 8;
  2877.     case AV_CODEC_ID_PCM_S16BE:
  2878.     case AV_CODEC_ID_PCM_S16BE_PLANAR:
  2879.     case AV_CODEC_ID_PCM_S16LE:
  2880.     case AV_CODEC_ID_PCM_S16LE_PLANAR:
  2881.     case AV_CODEC_ID_PCM_U16BE:
  2882.     case AV_CODEC_ID_PCM_U16LE:
  2883.         return 16;
  2884.     case AV_CODEC_ID_PCM_S24DAUD:
  2885.     case AV_CODEC_ID_PCM_S24BE:
  2886.     case AV_CODEC_ID_PCM_S24LE:
  2887.     case AV_CODEC_ID_PCM_S24LE_PLANAR:
  2888.     case AV_CODEC_ID_PCM_U24BE:
  2889.     case AV_CODEC_ID_PCM_U24LE:
  2890.         return 24;
  2891.     case AV_CODEC_ID_PCM_S32BE:
  2892.     case AV_CODEC_ID_PCM_S32LE:
  2893.     case AV_CODEC_ID_PCM_S32LE_PLANAR:
  2894.     case AV_CODEC_ID_PCM_U32BE:
  2895.     case AV_CODEC_ID_PCM_U32LE:
  2896.     case AV_CODEC_ID_PCM_F32BE:
  2897.     case AV_CODEC_ID_PCM_F32LE:
  2898.         return 32;
  2899.     case AV_CODEC_ID_PCM_F64BE:
  2900.     case AV_CODEC_ID_PCM_F64LE:
  2901.         return 64;
  2902.     default:
  2903.         return 0;
  2904.     }
  2905. }
  2906.  
  2907. enum AVCodecID av_get_pcm_codec(enum AVSampleFormat fmt, int be)
  2908. {
  2909.     static const enum AVCodecID map[AV_SAMPLE_FMT_NB][2] = {
  2910.         [AV_SAMPLE_FMT_U8  ] = { AV_CODEC_ID_PCM_U8,    AV_CODEC_ID_PCM_U8    },
  2911.         [AV_SAMPLE_FMT_S16 ] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
  2912.         [AV_SAMPLE_FMT_S32 ] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
  2913.         [AV_SAMPLE_FMT_FLT ] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
  2914.         [AV_SAMPLE_FMT_DBL ] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
  2915.         [AV_SAMPLE_FMT_U8P ] = { AV_CODEC_ID_PCM_U8,    AV_CODEC_ID_PCM_U8    },
  2916.         [AV_SAMPLE_FMT_S16P] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
  2917.         [AV_SAMPLE_FMT_S32P] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
  2918.         [AV_SAMPLE_FMT_FLTP] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
  2919.         [AV_SAMPLE_FMT_DBLP] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
  2920.     };
  2921.     if (fmt < 0 || fmt >= AV_SAMPLE_FMT_NB)
  2922.         return AV_CODEC_ID_NONE;
  2923.     if (be < 0 || be > 1)
  2924.         be = AV_NE(1, 0);
  2925.     return map[fmt][be];
  2926. }
  2927.  
  2928. int av_get_bits_per_sample(enum AVCodecID codec_id)
  2929. {
  2930.     switch (codec_id) {
  2931.     case AV_CODEC_ID_ADPCM_SBPRO_2:
  2932.         return 2;
  2933.     case AV_CODEC_ID_ADPCM_SBPRO_3:
  2934.         return 3;
  2935.     case AV_CODEC_ID_ADPCM_SBPRO_4:
  2936.     case AV_CODEC_ID_ADPCM_IMA_WAV:
  2937.     case AV_CODEC_ID_ADPCM_IMA_QT:
  2938.     case AV_CODEC_ID_ADPCM_SWF:
  2939.     case AV_CODEC_ID_ADPCM_MS:
  2940.         return 4;
  2941.     default:
  2942.         return av_get_exact_bits_per_sample(codec_id);
  2943.     }
  2944. }
  2945.  
  2946. int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
  2947. {
  2948.     int id, sr, ch, ba, tag, bps;
  2949.  
  2950.     id  = avctx->codec_id;
  2951.     sr  = avctx->sample_rate;
  2952.     ch  = avctx->channels;
  2953.     ba  = avctx->block_align;
  2954.     tag = avctx->codec_tag;
  2955.     bps = av_get_exact_bits_per_sample(avctx->codec_id);
  2956.  
  2957.     /* codecs with an exact constant bits per sample */
  2958.     if (bps > 0 && ch > 0 && frame_bytes > 0 && ch < 32768 && bps < 32768)
  2959.         return (frame_bytes * 8LL) / (bps * ch);
  2960.     bps = avctx->bits_per_coded_sample;
  2961.  
  2962.     /* codecs with a fixed packet duration */
  2963.     switch (id) {
  2964.     case AV_CODEC_ID_ADPCM_ADX:    return   32;
  2965.     case AV_CODEC_ID_ADPCM_IMA_QT: return   64;
  2966.     case AV_CODEC_ID_ADPCM_EA_XAS: return  128;
  2967.     case AV_CODEC_ID_AMR_NB:
  2968.     case AV_CODEC_ID_EVRC:
  2969.     case AV_CODEC_ID_GSM:
  2970.     case AV_CODEC_ID_QCELP:
  2971.     case AV_CODEC_ID_RA_288:       return  160;
  2972.     case AV_CODEC_ID_AMR_WB:
  2973.     case AV_CODEC_ID_GSM_MS:       return  320;
  2974.     case AV_CODEC_ID_MP1:          return  384;
  2975.     case AV_CODEC_ID_ATRAC1:       return  512;
  2976.     case AV_CODEC_ID_ATRAC3:       return 1024;
  2977.     case AV_CODEC_ID_MP2:
  2978.     case AV_CODEC_ID_MUSEPACK7:    return 1152;
  2979.     case AV_CODEC_ID_AC3:          return 1536;
  2980.     }
  2981.  
  2982.     if (sr > 0) {
  2983.         /* calc from sample rate */
  2984.         if (id == AV_CODEC_ID_TTA)
  2985.             return 256 * sr / 245;
  2986.  
  2987.         if (ch > 0) {
  2988.             /* calc from sample rate and channels */
  2989.             if (id == AV_CODEC_ID_BINKAUDIO_DCT)
  2990.                 return (480 << (sr / 22050)) / ch;
  2991.         }
  2992.     }
  2993.  
  2994.     if (ba > 0) {
  2995.         /* calc from block_align */
  2996.         if (id == AV_CODEC_ID_SIPR) {
  2997.             switch (ba) {
  2998.             case 20: return 160;
  2999.             case 19: return 144;
  3000.             case 29: return 288;
  3001.             case 37: return 480;
  3002.             }
  3003.         } else if (id == AV_CODEC_ID_ILBC) {
  3004.             switch (ba) {
  3005.             case 38: return 160;
  3006.             case 50: return 240;
  3007.             }
  3008.         }
  3009.     }
  3010.  
  3011.     if (frame_bytes > 0) {
  3012.         /* calc from frame_bytes only */
  3013.         if (id == AV_CODEC_ID_TRUESPEECH)
  3014.             return 240 * (frame_bytes / 32);
  3015.         if (id == AV_CODEC_ID_NELLYMOSER)
  3016.             return 256 * (frame_bytes / 64);
  3017.         if (id == AV_CODEC_ID_RA_144)
  3018.             return 160 * (frame_bytes / 20);
  3019.         if (id == AV_CODEC_ID_G723_1)
  3020.             return 240 * (frame_bytes / 24);
  3021.  
  3022.         if (bps > 0) {
  3023.             /* calc from frame_bytes and bits_per_coded_sample */
  3024.             if (id == AV_CODEC_ID_ADPCM_G726)
  3025.                 return frame_bytes * 8 / bps;
  3026.         }
  3027.  
  3028.         if (ch > 0) {
  3029.             /* calc from frame_bytes and channels */
  3030.             switch (id) {
  3031.             case AV_CODEC_ID_ADPCM_AFC:
  3032.                 return frame_bytes / (9 * ch) * 16;
  3033.             case AV_CODEC_ID_ADPCM_DTK:
  3034.                 return frame_bytes / (16 * ch) * 28;
  3035.             case AV_CODEC_ID_ADPCM_4XM:
  3036.             case AV_CODEC_ID_ADPCM_IMA_ISS:
  3037.                 return (frame_bytes - 4 * ch) * 2 / ch;
  3038.             case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
  3039.                 return (frame_bytes - 4) * 2 / ch;
  3040.             case AV_CODEC_ID_ADPCM_IMA_AMV:
  3041.                 return (frame_bytes - 8) * 2 / ch;
  3042.             case AV_CODEC_ID_ADPCM_XA:
  3043.                 return (frame_bytes / 128) * 224 / ch;
  3044.             case AV_CODEC_ID_INTERPLAY_DPCM:
  3045.                 return (frame_bytes - 6 - ch) / ch;
  3046.             case AV_CODEC_ID_ROQ_DPCM:
  3047.                 return (frame_bytes - 8) / ch;
  3048.             case AV_CODEC_ID_XAN_DPCM:
  3049.                 return (frame_bytes - 2 * ch) / ch;
  3050.             case AV_CODEC_ID_MACE3:
  3051.                 return 3 * frame_bytes / ch;
  3052.             case AV_CODEC_ID_MACE6:
  3053.                 return 6 * frame_bytes / ch;
  3054.             case AV_CODEC_ID_PCM_LXF:
  3055.                 return 2 * (frame_bytes / (5 * ch));
  3056.             case AV_CODEC_ID_IAC:
  3057.             case AV_CODEC_ID_IMC:
  3058.                 return 4 * frame_bytes / ch;
  3059.             }
  3060.  
  3061.             if (tag) {
  3062.                 /* calc from frame_bytes, channels, and codec_tag */
  3063.                 if (id == AV_CODEC_ID_SOL_DPCM) {
  3064.                     if (tag == 3)
  3065.                         return frame_bytes / ch;
  3066.                     else
  3067.                         return frame_bytes * 2 / ch;
  3068.                 }
  3069.             }
  3070.  
  3071.             if (ba > 0) {
  3072.                 /* calc from frame_bytes, channels, and block_align */
  3073.                 int blocks = frame_bytes / ba;
  3074.                 switch (avctx->codec_id) {
  3075.                 case AV_CODEC_ID_ADPCM_IMA_WAV:
  3076.                     if (bps < 2 || bps > 5)
  3077.                         return 0;
  3078.                     return blocks * (1 + (ba - 4 * ch) / (bps * ch) * 8);
  3079.                 case AV_CODEC_ID_ADPCM_IMA_DK3:
  3080.                     return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
  3081.                 case AV_CODEC_ID_ADPCM_IMA_DK4:
  3082.                     return blocks * (1 + (ba - 4 * ch) * 2 / ch);
  3083.                 case AV_CODEC_ID_ADPCM_IMA_RAD:
  3084.                     return blocks * ((ba - 4 * ch) * 2 / ch);
  3085.                 case AV_CODEC_ID_ADPCM_MS:
  3086.                     return blocks * (2 + (ba - 7 * ch) * 2 / ch);
  3087.                 }
  3088.             }
  3089.  
  3090.             if (bps > 0) {
  3091.                 /* calc from frame_bytes, channels, and bits_per_coded_sample */
  3092.                 switch (avctx->codec_id) {
  3093.                 case AV_CODEC_ID_PCM_DVD:
  3094.                     if(bps<4)
  3095.                         return 0;
  3096.                     return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
  3097.                 case AV_CODEC_ID_PCM_BLURAY:
  3098.                     if(bps<4)
  3099.                         return 0;
  3100.                     return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
  3101.                 case AV_CODEC_ID_S302M:
  3102.                     return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
  3103.                 }
  3104.             }
  3105.         }
  3106.     }
  3107.  
  3108.     return 0;
  3109. }
  3110.  
  3111. #if !HAVE_THREADS
  3112. int ff_thread_init(AVCodecContext *s)
  3113. {
  3114.     return -1;
  3115. }
  3116.  
  3117. #endif
  3118.  
  3119. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  3120. {
  3121.     unsigned int n = 0;
  3122.  
  3123.     while (v >= 0xff) {
  3124.         *s++ = 0xff;
  3125.         v -= 0xff;
  3126.         n++;
  3127.     }
  3128.     *s = v;
  3129.     n++;
  3130.     return n;
  3131. }
  3132.  
  3133. int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
  3134. {
  3135.     int i;
  3136.     for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
  3137.     return i;
  3138. }
  3139.  
  3140. #if FF_API_MISSING_SAMPLE
  3141. FF_DISABLE_DEPRECATION_WARNINGS
  3142. void av_log_missing_feature(void *avc, const char *feature, int want_sample)
  3143. {
  3144.     av_log(avc, AV_LOG_WARNING, "%s is not implemented. Update your FFmpeg "
  3145.             "version to the newest one from Git. If the problem still "
  3146.             "occurs, it means that your file has a feature which has not "
  3147.             "been implemented.\n", feature);
  3148.     if(want_sample)
  3149.         av_log_ask_for_sample(avc, NULL);
  3150. }
  3151.  
  3152. void av_log_ask_for_sample(void *avc, const char *msg, ...)
  3153. {
  3154.     va_list argument_list;
  3155.  
  3156.     va_start(argument_list, msg);
  3157.  
  3158.     if (msg)
  3159.         av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
  3160.     av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  3161.             "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
  3162.             "and contact the ffmpeg-devel mailing list.\n");
  3163.  
  3164.     va_end(argument_list);
  3165. }
  3166. FF_ENABLE_DEPRECATION_WARNINGS
  3167. #endif /* FF_API_MISSING_SAMPLE */
  3168.  
  3169. static AVHWAccel *first_hwaccel = NULL;
  3170.  
  3171. void av_register_hwaccel(AVHWAccel *hwaccel)
  3172. {
  3173.     AVHWAccel **p = &first_hwaccel;
  3174.     hwaccel->next = NULL;
  3175.     while(avpriv_atomic_ptr_cas((void * volatile *)p, NULL, hwaccel))
  3176.         p = &(*p)->next;
  3177. }
  3178.  
  3179. AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
  3180. {
  3181.     return hwaccel ? hwaccel->next : first_hwaccel;
  3182. }
  3183.  
  3184. AVHWAccel *ff_find_hwaccel(enum AVCodecID codec_id, enum AVPixelFormat pix_fmt)
  3185. {
  3186.     AVHWAccel *hwaccel = NULL;
  3187.  
  3188.     while ((hwaccel = av_hwaccel_next(hwaccel)))
  3189.         if (hwaccel->id == codec_id
  3190.             && hwaccel->pix_fmt == pix_fmt)
  3191.             return hwaccel;
  3192.     return NULL;
  3193. }
  3194.  
  3195. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  3196. {
  3197.     if (lockmgr_cb) {
  3198.         if (lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
  3199.             return -1;
  3200.         if (lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
  3201.             return -1;
  3202.     }
  3203.  
  3204.     lockmgr_cb = cb;
  3205.  
  3206.     if (lockmgr_cb) {
  3207.         if (lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
  3208.             return -1;
  3209.         if (lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
  3210.             return -1;
  3211.     }
  3212.     return 0;
  3213. }
  3214.  
  3215. int ff_lock_avcodec(AVCodecContext *log_ctx)
  3216. {
  3217.     if (lockmgr_cb) {
  3218.         if ((*lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  3219.             return -1;
  3220.     }
  3221.     entangled_thread_counter++;
  3222.     if (entangled_thread_counter != 1) {
  3223.         av_log(log_ctx, AV_LOG_ERROR, "Insufficient thread locking around avcodec_open/close()\n");
  3224.         if (!lockmgr_cb)
  3225.             av_log(log_ctx, AV_LOG_ERROR, "No lock manager is set, please see av_lockmgr_register()\n");
  3226.         ff_avcodec_locked = 1;
  3227.         ff_unlock_avcodec();
  3228.         return AVERROR(EINVAL);
  3229.     }
  3230.     av_assert0(!ff_avcodec_locked);
  3231.     ff_avcodec_locked = 1;
  3232.     return 0;
  3233. }
  3234.  
  3235. int ff_unlock_avcodec(void)
  3236. {
  3237.     av_assert0(ff_avcodec_locked);
  3238.     ff_avcodec_locked = 0;
  3239.     entangled_thread_counter--;
  3240.     if (lockmgr_cb) {
  3241.         if ((*lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE))
  3242.             return -1;
  3243.     }
  3244.     return 0;
  3245. }
  3246.  
  3247. int avpriv_lock_avformat(void)
  3248. {
  3249.     if (lockmgr_cb) {
  3250.         if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
  3251.             return -1;
  3252.     }
  3253.     return 0;
  3254. }
  3255.  
  3256. int avpriv_unlock_avformat(void)
  3257. {
  3258.     if (lockmgr_cb) {
  3259.         if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
  3260.             return -1;
  3261.     }
  3262.     return 0;
  3263. }
  3264.  
  3265. unsigned int avpriv_toupper4(unsigned int x)
  3266. {
  3267.     return av_toupper(x & 0xFF) +
  3268.           (av_toupper((x >>  8) & 0xFF) << 8)  +
  3269.           (av_toupper((x >> 16) & 0xFF) << 16) +
  3270.           (av_toupper((x >> 24) & 0xFF) << 24);
  3271. }
  3272.  
  3273. int ff_thread_ref_frame(ThreadFrame *dst, ThreadFrame *src)
  3274. {
  3275.     int ret;
  3276.  
  3277.     dst->owner = src->owner;
  3278.  
  3279.     ret = av_frame_ref(dst->f, src->f);
  3280.     if (ret < 0)
  3281.         return ret;
  3282.  
  3283.     if (src->progress &&
  3284.         !(dst->progress = av_buffer_ref(src->progress))) {
  3285.         ff_thread_release_buffer(dst->owner, dst);
  3286.         return AVERROR(ENOMEM);
  3287.     }
  3288.  
  3289.     return 0;
  3290. }
  3291.  
  3292. #if !HAVE_THREADS
  3293.  
  3294. enum AVPixelFormat ff_thread_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
  3295. {
  3296.     return avctx->get_format(avctx, fmt);
  3297. }
  3298.  
  3299. int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
  3300. {
  3301.     f->owner = avctx;
  3302.     return ff_get_buffer(avctx, f->f, flags);
  3303. }
  3304.  
  3305. void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
  3306. {
  3307.     av_frame_unref(f->f);
  3308. }
  3309.  
  3310. void ff_thread_finish_setup(AVCodecContext *avctx)
  3311. {
  3312. }
  3313.  
  3314. void ff_thread_report_progress(ThreadFrame *f, int progress, int field)
  3315. {
  3316. }
  3317.  
  3318. void ff_thread_await_progress(ThreadFrame *f, int progress, int field)
  3319. {
  3320. }
  3321.  
  3322. int ff_thread_can_start_frame(AVCodecContext *avctx)
  3323. {
  3324.     return 1;
  3325. }
  3326.  
  3327. int ff_alloc_entries(AVCodecContext *avctx, int count)
  3328. {
  3329.     return 0;
  3330. }
  3331.  
  3332. void ff_reset_entries(AVCodecContext *avctx)
  3333. {
  3334. }
  3335.  
  3336. void ff_thread_await_progress2(AVCodecContext *avctx, int field, int thread, int shift)
  3337. {
  3338. }
  3339.  
  3340. void ff_thread_report_progress2(AVCodecContext *avctx, int field, int thread, int n)
  3341. {
  3342. }
  3343.  
  3344. #endif
  3345.  
  3346. enum AVMediaType avcodec_get_type(enum AVCodecID codec_id)
  3347. {
  3348.     AVCodec *c= avcodec_find_decoder(codec_id);
  3349.     if(!c)
  3350.         c= avcodec_find_encoder(codec_id);
  3351.     if(c)
  3352.         return c->type;
  3353.  
  3354.     if (codec_id <= AV_CODEC_ID_NONE)
  3355.         return AVMEDIA_TYPE_UNKNOWN;
  3356.     else if (codec_id < AV_CODEC_ID_FIRST_AUDIO)
  3357.         return AVMEDIA_TYPE_VIDEO;
  3358.     else if (codec_id < AV_CODEC_ID_FIRST_SUBTITLE)
  3359.         return AVMEDIA_TYPE_AUDIO;
  3360.     else if (codec_id < AV_CODEC_ID_FIRST_UNKNOWN)
  3361.         return AVMEDIA_TYPE_SUBTITLE;
  3362.  
  3363.     return AVMEDIA_TYPE_UNKNOWN;
  3364. }
  3365.  
  3366. int avcodec_is_open(AVCodecContext *s)
  3367. {
  3368.     return !!s->internal;
  3369. }
  3370.  
  3371. int avpriv_bprint_to_extradata(AVCodecContext *avctx, struct AVBPrint *buf)
  3372. {
  3373.     int ret;
  3374.     char *str;
  3375.  
  3376.     ret = av_bprint_finalize(buf, &str);
  3377.     if (ret < 0)
  3378.         return ret;
  3379.     avctx->extradata = str;
  3380.     /* Note: the string is NUL terminated (so extradata can be read as a
  3381.      * string), but the ending character is not accounted in the size (in
  3382.      * binary formats you are likely not supposed to mux that character). When
  3383.      * extradata is copied, it is also padded with FF_INPUT_BUFFER_PADDING_SIZE
  3384.      * zeros. */
  3385.     avctx->extradata_size = buf->len;
  3386.     return 0;
  3387. }
  3388.  
  3389. const uint8_t *avpriv_find_start_code(const uint8_t *av_restrict p,
  3390.                                       const uint8_t *end,
  3391.                                       uint32_t *av_restrict state)
  3392. {
  3393.     int i;
  3394.  
  3395.     av_assert0(p <= end);
  3396.     if (p >= end)
  3397.         return end;
  3398.  
  3399.     for (i = 0; i < 3; i++) {
  3400.         uint32_t tmp = *state << 8;
  3401.         *state = tmp + *(p++);
  3402.         if (tmp == 0x100 || p == end)
  3403.             return p;
  3404.     }
  3405.  
  3406.     while (p < end) {
  3407.         if      (p[-1] > 1      ) p += 3;
  3408.         else if (p[-2]          ) p += 2;
  3409.         else if (p[-3]|(p[-1]-1)) p++;
  3410.         else {
  3411.             p++;
  3412.             break;
  3413.         }
  3414.     }
  3415.  
  3416.     p = FFMIN(p, end) - 4;
  3417.     *state = AV_RB32(p);
  3418.  
  3419.     return p + 4;
  3420. }
  3421.