int av_samples_get_buffer_size(int *linesize, int nb_channels, int nb_samples,
                               enum AVSampleFormat sample_fmt, int align)
{
    int line_size;
    int sample_size = av_get_bytes_per_sample(sample_fmt);
    int planar      = av_sample_fmt_is_planar(sample_fmt);
    if (!sample_size || nb_samples <= 0 || nb_channels <= 0)
        return AVERROR(EINVAL);
    if (!align) {
        if (nb_samples > INT_MAX - 31)
            return AVERROR(EINVAL);
        align = 1;
        nb_samples = FFALIGN(nb_samples, 32);
    }
    if (nb_channels > INT_MAX / align ||
        (int64_t)nb_channels * nb_samples > (INT_MAX - (align * nb_channels)) / sample_size)
        return AVERROR(EINVAL);
    line_size = planar ? FFALIGN(nb_samples * sample_size,               align) :
                         FFALIGN(nb_samples * sample_size * nb_channels, align);
    if (linesize)
        *linesize = line_size;
    return planar ? line_size * nb_channels : line_size;
}
['int ff_mjpeg_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,\n                          AVPacket *avpkt)\n{\n    AVFrame     *frame = data;\n    const uint8_t *buf = avpkt->data;\n    int buf_size       = avpkt->size;\n    MJpegDecodeContext *s = avctx->priv_data;\n    const uint8_t *buf_end, *buf_ptr;\n    const uint8_t *unescaped_buf_ptr;\n    int unescaped_buf_size;\n    int start_code;\n    int ret = 0;\n    s->got_picture = 0;\n    buf_ptr = buf;\n    buf_end = buf + buf_size;\n    while (buf_ptr < buf_end) {\n        start_code = ff_mjpeg_find_marker(s, &buf_ptr, buf_end,\n                                          &unescaped_buf_ptr,\n                                          &unescaped_buf_size);\n        if (start_code < 0) {\n            goto the_end;\n        } else if (unescaped_buf_size > INT_MAX / 8) {\n            av_log(avctx, AV_LOG_ERROR,\n                   "MJPEG packet 0x%x too big (%d/%d), corrupt data?\\n",\n                   start_code, unescaped_buf_size, buf_size);\n            return AVERROR_INVALIDDATA;\n        }\n        av_log(avctx, AV_LOG_DEBUG, "marker=%x avail_size_in_buf=%td\\n",\n               start_code, buf_end - buf_ptr);\n        ret = init_get_bits(&s->gb, unescaped_buf_ptr,\n                            unescaped_buf_size * 8);\n        if (ret < 0)\n            return ret;\n        s->start_code = start_code;\n        if (s->avctx->debug & FF_DEBUG_STARTCODE)\n            av_log(avctx, AV_LOG_DEBUG, "startcode: %X\\n", start_code);\n        if (start_code >= 0xd0 && start_code <= 0xd7)\n            av_log(avctx, AV_LOG_DEBUG,\n                   "restart marker: %d\\n", start_code & 0x0f);\n        else if (start_code >= APP0 && start_code <= APP15)\n            mjpeg_decode_app(s);\n        else if (start_code == COM)\n            mjpeg_decode_com(s);\n        if (!CONFIG_JPEGLS_DECODER &&\n            (start_code == SOF48 || start_code == LSE)) {\n            av_log(avctx, AV_LOG_ERROR, "JPEG-LS support not enabled.\\n");\n            return AVERROR(ENOSYS);\n        }\n        switch (start_code) {\n        case SOI:\n            s->restart_interval = 0;\n            s->restart_count    = 0;\n            break;\n        case DQT:\n            ff_mjpeg_decode_dqt(s);\n            break;\n        case DHT:\n            if ((ret = ff_mjpeg_decode_dht(s)) < 0) {\n                av_log(avctx, AV_LOG_ERROR, "huffman table decode error\\n");\n                return ret;\n            }\n            break;\n        case SOF0:\n        case SOF1:\n            s->lossless    = 0;\n            s->ls          = 0;\n            s->progressive = 0;\n            if ((ret = ff_mjpeg_decode_sof(s)) < 0)\n                return ret;\n            break;\n        case SOF2:\n            s->lossless    = 0;\n            s->ls          = 0;\n            s->progressive = 1;\n            if ((ret = ff_mjpeg_decode_sof(s)) < 0)\n                return ret;\n            break;\n        case SOF3:\n            s->lossless    = 1;\n            s->ls          = 0;\n            s->progressive = 0;\n            if ((ret = ff_mjpeg_decode_sof(s)) < 0)\n                return ret;\n            break;\n        case SOF48:\n            s->lossless    = 1;\n            s->ls          = 1;\n            s->progressive = 0;\n            if ((ret = ff_mjpeg_decode_sof(s)) < 0)\n                return ret;\n            break;\n        case LSE:\n            if (!CONFIG_JPEGLS_DECODER ||\n                (ret = ff_jpegls_decode_lse(s)) < 0)\n                return ret;\n            break;\n        case EOI:\n            s->cur_scan = 0;\n            if ((s->buggy_avid && !s->interlaced) || s->restart_interval)\n                break;\neoi_parser:\n            if (!s->got_picture) {\n                av_log(avctx, AV_LOG_WARNING,\n                       "Found EOI before any SOF, ignoring\\n");\n                break;\n            }\n            if (s->interlaced) {\n                s->bottom_field ^= 1;\n                if (s->bottom_field == !s->interlace_polarity)\n                    goto not_the_end;\n            }\n            if ((ret = av_frame_ref(frame, s->picture_ptr)) < 0)\n                return ret;\n            if (s->flipped) {\n                int i;\n                for (i = 0; frame->data[i]; i++) {\n                    int h = frame->height >> ((i == 1 || i == 2) ?\n                                              s->pix_desc->log2_chroma_h : 0);\n                    frame->data[i] += frame->linesize[i] * (h - 1);\n                    frame->linesize[i] *= -1;\n                }\n            }\n            *got_frame = 1;\n            if (!s->lossless &&\n                avctx->debug & FF_DEBUG_QP) {\n                av_log(avctx, AV_LOG_DEBUG,\n                       "QP: %d\\n", FFMAX3(s->qscale[0],\n                                          s->qscale[1],\n                                          s->qscale[2]));\n            }\n            goto the_end;\n        case SOS:\n            if (!s->got_picture) {\n                av_log(avctx, AV_LOG_WARNING,\n                       "Can not process SOS before SOF, skipping\\n");\n                break;\n                }\n            if ((ret = ff_mjpeg_decode_sos(s, NULL, NULL)) < 0 &&\n                (avctx->err_recognition & AV_EF_EXPLODE))\n                return ret;\n            if ((s->buggy_avid && !s->interlaced) || s->restart_interval)\n                goto eoi_parser;\n            break;\n        case DRI:\n            mjpeg_decode_dri(s);\n            break;\n        case SOF5:\n        case SOF6:\n        case SOF7:\n        case SOF9:\n        case SOF10:\n        case SOF11:\n        case SOF13:\n        case SOF14:\n        case SOF15:\n        case JPG:\n            av_log(avctx, AV_LOG_ERROR,\n                   "mjpeg: unsupported coding type (%x)\\n", start_code);\n            break;\n        }\nnot_the_end:\n        buf_ptr += (get_bits_count(&s->gb) + 7) / 8;\n        av_log(avctx, AV_LOG_DEBUG,\n               "marker parser used %d bytes (%d bits)\\n",\n               (get_bits_count(&s->gb) + 7) / 8, get_bits_count(&s->gb));\n    }\n    if (s->got_picture) {\n        av_log(avctx, AV_LOG_WARNING, "EOI missing, emulating\\n");\n        goto eoi_parser;\n    }\n    av_log(avctx, AV_LOG_FATAL, "No JPEG data found in image\\n");\n    return AVERROR_INVALIDDATA;\nthe_end:\n    av_log(avctx, AV_LOG_DEBUG, "mjpeg decode frame unused %td bytes\\n",\n           buf_end - buf_ptr);\n    return buf_ptr - buf;\n}', 'int ff_mjpeg_find_marker(MJpegDecodeContext *s,\n                         const uint8_t **buf_ptr, const uint8_t *buf_end,\n                         const uint8_t **unescaped_buf_ptr,\n                         int *unescaped_buf_size)\n{\n    int start_code;\n    start_code = find_marker(buf_ptr, buf_end);\n    av_fast_padded_malloc(&s->buffer, &s->buffer_size, buf_end - *buf_ptr);\n    if (!s->buffer)\n        return AVERROR(ENOMEM);\n    if (start_code == SOS && !s->ls) {\n        const uint8_t *src = *buf_ptr;\n        uint8_t *dst = s->buffer;\n        while (src < buf_end) {\n            uint8_t x = *(src++);\n            *(dst++) = x;\n            if (s->avctx->codec_id != AV_CODEC_ID_THP) {\n                if (x == 0xff) {\n                    while (src < buf_end && x == 0xff)\n                        x = *(src++);\n                    if (x >= 0xd0 && x <= 0xd7)\n                        *(dst++) = x;\n                    else if (x)\n                        break;\n                }\n            }\n        }\n        *unescaped_buf_ptr  = s->buffer;\n        *unescaped_buf_size = dst - s->buffer;\n        memset(s->buffer + *unescaped_buf_size, 0,\n               FF_INPUT_BUFFER_PADDING_SIZE);\n        av_log(s->avctx, AV_LOG_DEBUG, "escaping removed %td bytes\\n",\n               (buf_end - *buf_ptr) - (dst - s->buffer));\n    } else if (start_code == SOS && s->ls) {\n        const uint8_t *src = *buf_ptr;\n        uint8_t *dst  = s->buffer;\n        int bit_count = 0;\n        int t = 0, b = 0;\n        PutBitContext pb;\n        s->cur_scan++;\n        while (src + t < buf_end) {\n            uint8_t x = src[t++];\n            if (x == 0xff) {\n                while ((src + t < buf_end) && x == 0xff)\n                    x = src[t++];\n                if (x & 0x80) {\n                    t -= 2;\n                    break;\n                }\n            }\n        }\n        bit_count = t * 8;\n        init_put_bits(&pb, dst, t);\n        while (b < t) {\n            uint8_t x = src[b++];\n            put_bits(&pb, 8, x);\n            if (x == 0xFF) {\n                x = src[b++];\n                put_bits(&pb, 7, x);\n                bit_count--;\n            }\n        }\n        flush_put_bits(&pb);\n        *unescaped_buf_ptr  = dst;\n        *unescaped_buf_size = (bit_count + 7) >> 3;\n        memset(s->buffer + *unescaped_buf_size, 0,\n               FF_INPUT_BUFFER_PADDING_SIZE);\n    } else {\n        *unescaped_buf_ptr  = *buf_ptr;\n        *unescaped_buf_size = buf_end - *buf_ptr;\n    }\n    return start_code;\n}', 'int av_frame_ref(AVFrame *dst, const AVFrame *src)\n{\n    int i, ret = 0;\n    dst->format         = src->format;\n    dst->width          = src->width;\n    dst->height         = src->height;\n    dst->channel_layout = src->channel_layout;\n    dst->nb_samples     = src->nb_samples;\n    ret = av_frame_copy_props(dst, src);\n    if (ret < 0)\n        return ret;\n    if (!src->buf[0]) {\n        ret = av_frame_get_buffer(dst, 32);\n        if (ret < 0)\n            return ret;\n        if (src->nb_samples) {\n            int ch = av_get_channel_layout_nb_channels(src->channel_layout);\n            av_samples_copy(dst->extended_data, src->extended_data, 0, 0,\n                            dst->nb_samples, ch, dst->format);\n        } else {\n            av_image_copy(dst->data, dst->linesize, src->data, src->linesize,\n                          dst->format, dst->width, dst->height);\n        }\n        return 0;\n    }\n    for (i = 0; i < FF_ARRAY_ELEMS(src->buf) && src->buf[i]; i++) {\n        dst->buf[i] = av_buffer_ref(src->buf[i]);\n        if (!dst->buf[i]) {\n            ret = AVERROR(ENOMEM);\n            goto fail;\n        }\n    }\n    if (src->extended_buf) {\n        dst->extended_buf = av_mallocz(sizeof(*dst->extended_buf) *\n                                       src->nb_extended_buf);\n        if (!dst->extended_buf) {\n            ret = AVERROR(ENOMEM);\n            goto fail;\n        }\n        dst->nb_extended_buf = src->nb_extended_buf;\n        for (i = 0; i < src->nb_extended_buf; i++) {\n            dst->extended_buf[i] = av_buffer_ref(src->extended_buf[i]);\n            if (!dst->extended_buf[i]) {\n                ret = AVERROR(ENOMEM);\n                goto fail;\n            }\n        }\n    }\n    if (src->extended_data != src->data) {\n        int ch = av_get_channel_layout_nb_channels(src->channel_layout);\n        if (!ch) {\n            ret = AVERROR(EINVAL);\n            goto fail;\n        }\n        dst->extended_data = av_malloc(sizeof(*dst->extended_data) * ch);\n        if (!dst->extended_data) {\n            ret = AVERROR(ENOMEM);\n            goto fail;\n        }\n        memcpy(dst->extended_data, src->extended_data, sizeof(*src->extended_data) * ch);\n    } else\n        dst->extended_data = dst->data;\n    memcpy(dst->data,     src->data,     sizeof(src->data));\n    memcpy(dst->linesize, src->linesize, sizeof(src->linesize));\n    return 0;\nfail:\n    av_frame_unref(dst);\n    return ret;\n}', 'int av_frame_get_buffer(AVFrame *frame, int align)\n{\n    if (frame->format < 0)\n        return AVERROR(EINVAL);\n    if (frame->width > 0 && frame->height > 0)\n        return get_video_buffer(frame, align);\n    else if (frame->nb_samples > 0 && frame->channel_layout)\n        return get_audio_buffer(frame, align);\n    return AVERROR(EINVAL);\n}', 'static int get_audio_buffer(AVFrame *frame, int align)\n{\n    int channels = av_get_channel_layout_nb_channels(frame->channel_layout);\n    int planar   = av_sample_fmt_is_planar(frame->format);\n    int planes   = planar ? channels : 1;\n    int ret, i;\n    if (!frame->linesize[0]) {\n        ret = av_samples_get_buffer_size(&frame->linesize[0], channels,\n                                         frame->nb_samples, frame->format,\n                                         align);\n        if (ret < 0)\n            return ret;\n    }\n    if (planes > AV_NUM_DATA_POINTERS) {\n        frame->extended_data = av_mallocz(planes *\n                                          sizeof(*frame->extended_data));\n        frame->extended_buf  = av_mallocz((planes - AV_NUM_DATA_POINTERS) *\n                                          sizeof(*frame->extended_buf));\n        if (!frame->extended_data || !frame->extended_buf) {\n            av_freep(&frame->extended_data);\n            av_freep(&frame->extended_buf);\n            return AVERROR(ENOMEM);\n        }\n        frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;\n    } else\n        frame->extended_data = frame->data;\n    for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {\n        frame->buf[i] = av_buffer_alloc(frame->linesize[0]);\n        if (!frame->buf[i]) {\n            av_frame_unref(frame);\n            return AVERROR(ENOMEM);\n        }\n        frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;\n    }\n    for (i = 0; i < planes - AV_NUM_DATA_POINTERS; i++) {\n        frame->extended_buf[i] = av_buffer_alloc(frame->linesize[0]);\n        if (!frame->extended_buf[i]) {\n            av_frame_unref(frame);\n            return AVERROR(ENOMEM);\n        }\n        frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;\n    }\n    return 0;\n}', 'int av_samples_get_buffer_size(int *linesize, int nb_channels, int nb_samples,\n                               enum AVSampleFormat sample_fmt, int align)\n{\n    int line_size;\n    int sample_size = av_get_bytes_per_sample(sample_fmt);\n    int planar      = av_sample_fmt_is_planar(sample_fmt);\n    if (!sample_size || nb_samples <= 0 || nb_channels <= 0)\n        return AVERROR(EINVAL);\n    if (!align) {\n        if (nb_samples > INT_MAX - 31)\n            return AVERROR(EINVAL);\n        align = 1;\n        nb_samples = FFALIGN(nb_samples, 32);\n    }\n    if (nb_channels > INT_MAX / align ||\n        (int64_t)nb_channels * nb_samples > (INT_MAX - (align * nb_channels)) / sample_size)\n        return AVERROR(EINVAL);\n    line_size = planar ? FFALIGN(nb_samples * sample_size,               align) :\n                         FFALIGN(nb_samples * sample_size * nb_channels, align);\n    if (linesize)\n        *linesize = line_size;\n    return planar ? line_size * nb_channels : line_size;\n}']