Skip to content
Snippets Groups Projects
utils.c 71.2 KiB
Newer Older
  • Learn to ignore specific revisions
  •         if (avctx->codec->pix_fmts) {
    
                for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
    
                    if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
                        break;
    
                if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE) {
    
                    av_log(avctx, AV_LOG_ERROR, "Specified pix_fmt is not supported\n");
                    ret = AVERROR(EINVAL);
                    goto free_and_end;
                }
    
                if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ420P ||
                    avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ422P ||
                    avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ440P ||
                    avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ444P)
                    avctx->color_range = AVCOL_RANGE_JPEG;
    
            if (avctx->codec->supported_samplerates) {
                for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
                    if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
                        break;
                if (avctx->codec->supported_samplerates[i] == 0) {
                    av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
                    ret = AVERROR(EINVAL);
                    goto free_and_end;
                }
            }
            if (avctx->codec->channel_layouts) {
                if (!avctx->channel_layout) {
                    av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
                } else {
                    for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
                        if (avctx->channel_layout == avctx->codec->channel_layouts[i])
                            break;
                    if (avctx->codec->channel_layouts[i] == 0) {
                        av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
                        ret = AVERROR(EINVAL);
                        goto free_and_end;
                    }
                }
            }
    
            if (avctx->channel_layout && avctx->channels) {
                if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
                    av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
                    ret = AVERROR(EINVAL);
                    goto free_and_end;
                }
    
            } else if (avctx->channel_layout) {
                avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
    
    
            if (!avctx->rc_initial_buffer_occupancy)
                avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3 / 4;
    
    Luca Barbato's avatar
    Luca Barbato committed
        if (avctx->codec->init && !(avctx->active_thread_type & FF_THREAD_FRAME)) {
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
            ret = avctx->codec->init(avctx);
            if (ret < 0) {
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
            }
    
    #if FF_API_AUDIOENC_DELAY
        if (av_codec_is_encoder(avctx->codec))
            avctx->delay = avctx->initial_padding;
    #endif
    
    
        if (av_codec_is_decoder(avctx->codec)) {
            /* validate channel layout from the decoder */
    
            if (avctx->channel_layout) {
                int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
                if (!avctx->channels)
                    avctx->channels = channels;
                else if (channels != avctx->channels) {
                    av_log(avctx, AV_LOG_WARNING,
                           "channel layout does not match number of channels\n");
                    avctx->channel_layout = 0;
                }
    
            if (avctx->channels && avctx->channels < 0 ||
                avctx->channels > FF_SANE_NB_CHANNELS) {
                ret = AVERROR(EINVAL);
                goto free_and_end;
            }
    
    
    #if FF_API_AVCTX_TIMEBASE
            if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
                avctx->time_base = av_inv_q(avctx->framerate);
    #endif
    
        if (!(codec->caps_internal & FF_CODEC_CAP_INIT_THREADSAFE) && codec->init) {
    
            /* Release any user-supplied mutex. */
            if (lockmgr_cb) {
                (*lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
            }
    
        if (options) {
            av_dict_free(options);
            *options = tmp;
        }
    
    
        if (avctx->codec &&
            (avctx->codec->caps_internal & FF_CODEC_CAP_INIT_CLEANUP))
            avctx->codec->close(avctx);
    
    
        if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
            av_opt_free(avctx->priv_data);
        av_opt_free(avctx);
    
    
    #if FF_API_CODED_FRAME
    FF_DISABLE_DEPRECATION_WARNINGS
    
    FF_ENABLE_DEPRECATION_WARNINGS
    #endif
    
        if (avctx->internal) {
            av_frame_free(&avctx->internal->to_free);
    
            av_freep(&avctx->internal->pool);
    
    Luca Barbato's avatar
    Luca Barbato committed
        avctx->codec = NULL;
    
    int ff_alloc_packet(AVPacket *avpkt, int size)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    {
    
        if (size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
    
            AVBufferRef *buf = avpkt->buf;
    
    
            if (avpkt->size < size)
                return AVERROR(EINVAL);
    
            av_init_packet(avpkt);
    
    Luca Barbato's avatar
    Luca Barbato committed
            avpkt->size     = size;
    
            return 0;
        } else {
            return av_new_packet(avpkt, size);
    
    /**
     * Pad last frame with silence.
     */
    static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
    {
        AVFrame *frame = NULL;
        int ret;
    
    
        frame->format         = src->format;
        frame->channel_layout = src->channel_layout;
        frame->nb_samples     = s->frame_size;
        ret = av_frame_get_buffer(frame, 32);
        if (ret < 0)
    
        ret = av_frame_copy_props(frame, src);
        if (ret < 0)
    
            goto fail;
    
        if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
                                   src->nb_samples, s->channels, s->sample_fmt)) < 0)
            goto fail;
        if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
                                          frame->nb_samples - src->nb_samples,
                                          s->channels, s->sample_fmt)) < 0)
            goto fail;
    
        *dst = frame;
    
        return 0;
    
    fail:
    
        av_frame_free(&frame);
    
    int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
                                                  AVPacket *avpkt,
                                                  const AVFrame *frame,
                                                  int *got_packet_ptr)
    {
    
        AVFrame *padded_frame = NULL;
    
        if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY) && !frame) {
    
            av_packet_unref(avpkt);
    
        /* ensure that extended_data is properly set */
        if (frame && !frame->extended_data) {
            if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
                avctx->channels > AV_NUM_DATA_POINTERS) {
                av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
    
    Luca Barbato's avatar
    Luca Barbato committed
                                            "with more than %d channels, but extended_data is not set.\n",
    
                       AV_NUM_DATA_POINTERS);
                return AVERROR(EINVAL);
            }
            av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
    
            tmp = *frame;
            tmp.extended_data = tmp.data;
            frame = &tmp;
        }
    
    
        /* extract audio service type metadata */
        if (frame) {
            AVFrameSideData *sd = av_frame_get_side_data(frame, AV_FRAME_DATA_AUDIO_SERVICE_TYPE);
            if (sd && sd->size >= sizeof(enum AVAudioServiceType))
                avctx->audio_service_type = *(enum AVAudioServiceType*)sd->data;
        }
    
    
        /* check for valid frame size */
        if (frame) {
    
            if (avctx->codec->capabilities & AV_CODEC_CAP_SMALL_LAST_FRAME) {
    
                if (frame->nb_samples > avctx->frame_size)
    
            } else if (!(avctx->codec->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE)) {
    
                if (frame->nb_samples < avctx->frame_size &&
                    !avctx->internal->last_audio_frame) {
                    ret = pad_last_frame(avctx, &padded_frame, frame);
                    if (ret < 0)
                        return ret;
    
                    frame = padded_frame;
                    avctx->internal->last_audio_frame = 1;
                }
    
    
                if (frame->nb_samples != avctx->frame_size) {
                    ret = AVERROR(EINVAL);
                    goto end;
                }
    
        ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
        if (!ret) {
            if (*got_packet_ptr) {
    
                if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY)) {
    
                    if (avpkt->pts == AV_NOPTS_VALUE)
                        avpkt->pts = frame->pts;
                    if (!avpkt->duration)
                        avpkt->duration = ff_samples_to_time_base(avctx,
                                                                  frame->nb_samples);
    
                ret = av_buffer_realloc(&avpkt->buf, avpkt->size);
                if (ret >= 0)
                    avpkt->data = avpkt->buf->data;
    
        if (ret < 0 || !*got_packet_ptr) {
    
            av_packet_unref(avpkt);
    
        /* NOTE: if we add any audio encoders which output non-keyframe packets,
    
    Luca Barbato's avatar
    Luca Barbato committed
         *       this needs to be moved to the encoders, but for now we can do it
         *       here to simplify things */
    
        av_frame_free(&padded_frame);
    
    #if FF_API_AUDIOENC_DELAY
        avctx->delay = avctx->initial_padding;
    #endif
    
    
    int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
                                                  AVPacket *avpkt,
                                                  const AVFrame *frame,
                                                  int *got_packet_ptr)
    {
        int ret;
        int user_packet = !!avpkt->data;
    
    
        if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY) && !frame) {
    
            av_packet_unref(avpkt);
    
    Luca Barbato's avatar
    Luca Barbato committed
            avpkt->size = 0;
    
        }
    
        if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
            return AVERROR(EINVAL);
    
    
        av_assert0(avctx->codec->encode2);
    
        ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
        if (!ret) {
            if (!*got_packet_ptr)
                avpkt->size = 0;
    
            else if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
    
                avpkt->pts = avpkt->dts = frame->pts;
    
                ret = av_buffer_realloc(&avpkt->buf, avpkt->size);
                if (ret >= 0)
                    avpkt->data = avpkt->buf->data;
    
            av_packet_unref(avpkt);
    
    int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
    
                                const AVSubtitle *sub)
    {
        int ret;
    
    Luca Barbato's avatar
    Luca Barbato committed
        if (sub->start_display_time) {
    
            av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
            return -1;
        }
    
    Luca Barbato's avatar
    Luca Barbato committed
        if (sub->num_rects == 0 || !sub->rects)
    
        ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
    
        avctx->frame_number++;
        return ret;
    }
    
    
    static int apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
    
        const uint8_t *data;
        uint32_t flags;
    
        data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
    
        if (!(avctx->codec->capabilities & AV_CODEC_CAP_PARAM_CHANGE)) {
    
            av_log(avctx, AV_LOG_ERROR, "This decoder does not support parameter "
                   "changes, but PARAM_CHANGE side data was sent to it.\n");
            return AVERROR(EINVAL);
        }
    
        if (size < 4)
            goto fail;
    
    
        flags = bytestream_get_le32(&data);
        size -= 4;
    
        if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
    
            if (size < 4)
                goto fail;
    
            avctx->channels = bytestream_get_le32(&data);
            size -= 4;
        }
        if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
            if (size < 8)
    
            avctx->channel_layout = bytestream_get_le64(&data);
            size -= 8;
        }
        if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
    
            if (size < 4)
                goto fail;
    
            avctx->sample_rate = bytestream_get_le32(&data);
            size -= 4;
        }
        if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
            if (size < 8)
    
            avctx->width  = bytestream_get_le32(&data);
            avctx->height = bytestream_get_le32(&data);
            size -= 8;
    
            ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
            if (ret < 0)
                return ret;
    
    
        return 0;
    fail:
        av_log(avctx, AV_LOG_ERROR, "PARAM_CHANGE side data too small.\n");
        return AVERROR_INVALIDDATA;
    
    static int unrefcount_frame(AVCodecInternal *avci, AVFrame *frame)
    {
        int ret;
    
        /* move the original frame to our backup */
        av_frame_unref(avci->to_free);
        av_frame_move_ref(avci->to_free, frame);
    
        /* now copy everything except the AVBufferRefs back
         * note that we make a COPY of the side data, so calling av_frame_free() on
         * the caller's frame will work properly */
        ret = av_frame_copy_props(frame, avci->to_free);
        if (ret < 0)
            return ret;
    
        memcpy(frame->data,     avci->to_free->data,     sizeof(frame->data));
        memcpy(frame->linesize, avci->to_free->linesize, sizeof(frame->linesize));
        if (avci->to_free->extended_data != avci->to_free->data) {
            int planes = av_get_channel_layout_nb_channels(avci->to_free->channel_layout);
            int size   = planes * sizeof(*frame->extended_data);
    
            if (!size) {
                av_frame_unref(frame);
                return AVERROR_BUG;
            }
    
            frame->extended_data = av_malloc(size);
            if (!frame->extended_data) {
                av_frame_unref(frame);
                return AVERROR(ENOMEM);
            }
            memcpy(frame->extended_data, avci->to_free->extended_data,
                   size);
        } else
            frame->extended_data = frame->data;
    
        frame->format         = avci->to_free->format;
        frame->width          = avci->to_free->width;
        frame->height         = avci->to_free->height;
        frame->channel_layout = avci->to_free->channel_layout;
        frame->nb_samples     = avci->to_free->nb_samples;
    
        return 0;
    }
    
    
    int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
    
    Luca Barbato's avatar
    Luca Barbato committed
                                                  int *got_picture_ptr,
                                                  AVPacket *avpkt)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    {
    
        AVCodecInternal *avci = avctx->internal;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        int ret;
    
    Luca Barbato's avatar
    Luca Barbato committed
        *got_picture_ptr = 0;
        if ((avctx->coded_width || avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
    
        avctx->internal->pkt = avpkt;
    
        ret = apply_param_change(avctx, avpkt);
        if (ret < 0) {
            av_log(avctx, AV_LOG_ERROR, "Error applying parameter changes.\n");
            if (avctx->err_recognition & AV_EF_EXPLODE)
                return ret;
        }
    
        if ((avctx->codec->capabilities & AV_CODEC_CAP_DELAY) || avpkt->size ||
            (avctx->active_thread_type & FF_THREAD_FRAME)) {
    
    Luca Barbato's avatar
    Luca Barbato committed
            if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
                ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
                                             avpkt);
    
            else {
                ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
    
    Luca Barbato's avatar
    Luca Barbato committed
                                           avpkt);
    
                if (!(avctx->codec->caps_internal & FF_CODEC_CAP_SETS_PKT_DTS))
                    picture->pkt_dts = avpkt->dts;
    
                /* get_buffer is supposed to set frame parameters */
    
                if (!(avctx->codec->capabilities & AV_CODEC_CAP_DR1)) {
    
                    picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
                    picture->width               = avctx->width;
                    picture->height              = avctx->height;
                    picture->format              = avctx->pix_fmt;
                }
    
    Diego Biurrun's avatar
    Diego Biurrun committed
            emms_c(); //needed to avoid an emms_c() call before every return;
    
            if (*got_picture_ptr) {
                if (!avctx->refcounted_frames) {
    
                    int err = unrefcount_frame(avci, picture);
                    if (err < 0)
                        return err;
    
                avctx->frame_number++;
    
    Luca Barbato's avatar
    Luca Barbato committed
        } else
            ret = 0;
    
    #if FF_API_AVCTX_TIMEBASE
        if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
            avctx->time_base = av_inv_q(avctx->framerate);
    #endif
    
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        return ret;
    }
    
    
    int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
                                                  AVFrame *frame,
                                                  int *got_frame_ptr,
                                                  AVPacket *avpkt)
    {
    
        AVCodecInternal *avci = avctx->internal;
    
        int ret = 0;
    
        *got_frame_ptr = 0;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    
    
        avctx->internal->pkt = avpkt;
    
        if (!avpkt->data && avpkt->size) {
            av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
            return AVERROR(EINVAL);
        }
    
    
        ret = apply_param_change(avctx, avpkt);
        if (ret < 0) {
            av_log(avctx, AV_LOG_ERROR, "Error applying parameter changes.\n");
            if (avctx->err_recognition & AV_EF_EXPLODE)
                return ret;
        }
    
        if ((avctx->codec->capabilities & AV_CODEC_CAP_DELAY) || avpkt->size) {
    
            ret = avctx->codec->decode(avctx, frame, got_frame_ptr, avpkt);
            if (ret >= 0 && *got_frame_ptr) {
                avctx->frame_number++;
                frame->pkt_dts = avpkt->dts;
    
                if (frame->format == AV_SAMPLE_FMT_NONE)
                    frame->format = avctx->sample_fmt;
    
    
                if (!avctx->refcounted_frames) {
    
                    int err = unrefcount_frame(avci, frame);
                    if (err < 0)
                        return err;
    
                av_frame_unref(frame);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        return ret;
    }
    
    
    int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
    
    Luca Barbato's avatar
    Luca Barbato committed
                                 int *got_sub_ptr,
                                 AVPacket *avpkt)
    
        avctx->internal->pkt = avpkt;
    
        *got_sub_ptr = 0;
    
        ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
    
        if (*got_sub_ptr)
            avctx->frame_number++;
        return ret;
    }
    
    
    void avsubtitle_free(AVSubtitle *sub)
    {
        int i;
    
    
    Luca Barbato's avatar
    Luca Barbato committed
        for (i = 0; i < sub->num_rects; i++) {
    
            av_freep(&sub->rects[i]->data[0]);
            av_freep(&sub->rects[i]->data[1]);
            av_freep(&sub->rects[i]->data[2]);
            av_freep(&sub->rects[i]->data[3]);
    
            av_freep(&sub->rects[i]->text);
            av_freep(&sub->rects[i]->ass);
            av_freep(&sub->rects[i]);
    
    
        memset(sub, 0, sizeof(AVSubtitle));
    }
    
    
    av_cold int avcodec_close(AVCodecContext *avctx)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    {
    
        if (avcodec_is_open(avctx)) {
    
            FramePool *pool = avctx->internal->pool;
    
            if (HAVE_THREADS && avctx->internal->thread_ctx)
    
                ff_thread_free(avctx);
            if (avctx->codec && avctx->codec->close)
                avctx->codec->close(avctx);
    
            av_frame_free(&avctx->internal->to_free);
    
            for (i = 0; i < FF_ARRAY_ELEMS(pool->pools); i++)
                av_buffer_pool_uninit(&pool->pools[i]);
            av_freep(&avctx->internal->pool);
    
    
            if (avctx->hwaccel && avctx->hwaccel->uninit)
                avctx->hwaccel->uninit(avctx);
            av_freep(&avctx->internal->hwaccel_priv_data);
    
    
        for (i = 0; i < avctx->nb_coded_side_data; i++)
            av_freep(&avctx->coded_side_data[i].data);
        av_freep(&avctx->coded_side_data);
        avctx->nb_coded_side_data = 0;
    
    
        if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
    
            av_opt_free(avctx->priv_data);
        av_opt_free(avctx);
    
        av_freep(&avctx->priv_data);
    
        if (av_codec_is_encoder(avctx->codec)) {
    
    #if FF_API_CODED_FRAME
    FF_DISABLE_DEPRECATION_WARNINGS
    
    FF_ENABLE_DEPRECATION_WARNINGS
    #endif
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        avctx->codec = NULL;
    
        avctx->active_thread_type = 0;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        return 0;
    }
    
    
    static AVCodec *find_encdec(enum AVCodecID id, int encoder)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    {
    
    Luca Barbato's avatar
    Luca Barbato committed
        AVCodec *p, *experimental = NULL;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        p = first_avcodec;
        while (p) {
    
            if ((encoder ? av_codec_is_encoder(p) : av_codec_is_decoder(p)) &&
                p->id == id) {
    
                if (p->capabilities & AV_CODEC_CAP_EXPERIMENTAL && !experimental) {
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            p = p->next;
        }
    
    AVCodec *avcodec_find_encoder(enum AVCodecID id)
    {
        return find_encdec(id, 1);
    }
    
    
    AVCodec *avcodec_find_encoder_by_name(const char *name)
    {
        AVCodec *p;
    
        p = first_avcodec;
        while (p) {
    
    Luca Barbato's avatar
    Luca Barbato committed
            if (av_codec_is_encoder(p) && strcmp(name, p->name) == 0)
    
                return p;
            p = p->next;
        }
        return NULL;
    }
    
    
    AVCodec *avcodec_find_decoder(enum AVCodecID id)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    {
    
        return find_encdec(id, 0);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    }
    
    AVCodec *avcodec_find_decoder_by_name(const char *name)
    {
        AVCodec *p;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        p = first_avcodec;
        while (p) {
    
    Luca Barbato's avatar
    Luca Barbato committed
            if (av_codec_is_decoder(p) && strcmp(name, p->name) == 0)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                return p;
            p = p->next;
        }
        return NULL;
    }
    
    
    static int get_bit_rate(AVCodecContext *ctx)
    
    Luca Barbato's avatar
    Luca Barbato committed
        switch (ctx->codec_type) {
    
        case AVMEDIA_TYPE_DATA:
        case AVMEDIA_TYPE_SUBTITLE:
        case AVMEDIA_TYPE_ATTACHMENT:
    
            bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
            bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
            break;
        default:
            bit_rate = 0;
            break;
        }
        return bit_rate;
    }
    
    
    size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
    {
        int i, len, ret = 0;
    
    
    #define TAG_PRINT(x)                                              \
        (((x) >= '0' && (x) <= '9') ||                                \
         ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') ||  \
         ((x) == '.' || (x) == ' '))
    
    
        for (i = 0; i < 4; i++) {
            len = snprintf(buf, buf_size,
    
                           TAG_PRINT(codec_tag & 0xFF) ? "%c" : "[%d]", codec_tag & 0xFF);
    
    Luca Barbato's avatar
    Luca Barbato committed
            buf        += len;
            buf_size    = buf_size > len ? buf_size - len : 0;
            ret        += len;
            codec_tag >>= 8;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
    {
        const char *codec_name;
    
        const char *profile = NULL;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        char buf1[32];
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        int bitrate;
    
        AVRational display_aspect_ratio;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    
    
        if (enc->codec)
            p = enc->codec;
        else if (encode)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            p = avcodec_find_encoder(enc->codec_id);
        else
            p = avcodec_find_decoder(enc->codec_id);
    
        if (p) {
            codec_name = p->name;
    
            profile = av_get_profile_name(p, enc->profile);
    
        } else if (enc->codec_id == AV_CODEC_ID_MPEG2TS) {
    
            /* fake mpeg2 transport stream codec (currently not
    
    Luca Barbato's avatar
    Luca Barbato committed
             * registered) */
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        } else {
            /* output avi tags */
    
            char tag_buf[32];
            av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
            snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            codec_name = buf1;
        }
    
    
    Luca Barbato's avatar
    Luca Barbato committed
        switch (enc->codec_type) {
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            snprintf(buf, buf_size,
                     "Video: %s%s",
    
                     codec_name, enc->mb_decision ? " (hq)" : "");
    
            if (profile)
                snprintf(buf + strlen(buf), buf_size - strlen(buf),
                         " (%s)", profile);
    
            if (enc->codec_tag) {
                char tag_buf[32];
                av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
                snprintf(buf + strlen(buf), buf_size - strlen(buf),
                         " [%s / 0x%04X]", tag_buf, enc->codec_tag);
            }
    
    
            av_strlcat(buf, "\n      ", buf_size);
            snprintf(buf + strlen(buf), buf_size - strlen(buf),
                     "%s", enc->pix_fmt == AV_PIX_FMT_NONE ? "none" :
    
                         av_get_pix_fmt_name(enc->pix_fmt));
    
            if (enc->color_range != AVCOL_RANGE_UNSPECIFIED)
                snprintf(buf + strlen(buf), buf_size - strlen(buf), ", %s",
                         av_color_range_name(enc->color_range));
            if (enc->colorspace != AVCOL_SPC_UNSPECIFIED ||
                enc->color_primaries != AVCOL_PRI_UNSPECIFIED ||
                enc->color_trc != AVCOL_TRC_UNSPECIFIED) {
                new_line = 1;
                snprintf(buf + strlen(buf), buf_size - strlen(buf), ", %s/%s/%s",
                         av_color_space_name(enc->colorspace),
                         av_color_primaries_name(enc->color_primaries),
                         av_color_transfer_name(enc->color_trc));
            }
            if (av_log_get_level() >= AV_LOG_DEBUG &&
                enc->chroma_sample_location != AVCHROMA_LOC_UNSPECIFIED)
                snprintf(buf + strlen(buf), buf_size - strlen(buf), ", %s",
                         av_chroma_location_name(enc->chroma_sample_location));
    
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            if (enc->width) {
    
                av_strlcat(buf, new_line ? "\n      " : ", ", buf_size);
    
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                snprintf(buf + strlen(buf), buf_size - strlen(buf),
    
                if (av_log_get_level() >= AV_LOG_VERBOSE &&
                    (enc->width != enc->coded_width ||
                     enc->height != enc->coded_height))
                    snprintf(buf + strlen(buf), buf_size - strlen(buf),
                             " (%dx%d)", enc->coded_width, enc->coded_height);
    
    
                if (enc->sample_aspect_ratio.num) {
    
    Baptiste Coudurier's avatar
    Baptiste Coudurier committed
                    av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
    
    Luca Barbato's avatar
    Luca Barbato committed
                              enc->width * enc->sample_aspect_ratio.num,
                              enc->height * enc->sample_aspect_ratio.den,
                              1024 * 1024);
    
    Baptiste Coudurier's avatar
    Baptiste Coudurier committed
                    snprintf(buf + strlen(buf), buf_size - strlen(buf),
                             " [PAR %d:%d DAR %d:%d]",
                             enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
                             display_aspect_ratio.num, display_aspect_ratio.den);
    
    Luca Barbato's avatar
    Luca Barbato committed
                if (av_log_get_level() >= AV_LOG_DEBUG) {
                    int g = av_gcd(enc->time_base.num, enc->time_base.den);
    
                    snprintf(buf + strlen(buf), buf_size - strlen(buf),
    
    Luca Barbato's avatar
    Luca Barbato committed
                             ", %d/%d",
                             enc->time_base.num / g, enc->time_base.den / g);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            }
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            if (encode) {
                snprintf(buf + strlen(buf), buf_size - strlen(buf),
                         ", q=%d-%d", enc->qmin, enc->qmax);
            }
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            break;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            snprintf(buf, buf_size,
                     "Audio: %s",
                     codec_name);
    
            if (profile)
                snprintf(buf + strlen(buf), buf_size - strlen(buf),
                         " (%s)", profile);
    
            if (enc->codec_tag) {
                char tag_buf[32];
                av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
                snprintf(buf + strlen(buf), buf_size - strlen(buf),
                         " [%s / 0x%04X]", tag_buf, enc->codec_tag);
            }
    
    
            av_strlcat(buf, "\n      ", buf_size);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            if (enc->sample_rate) {
                snprintf(buf + strlen(buf), buf_size - strlen(buf),
    
                         "%d Hz, ", enc->sample_rate);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            }
    
            av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
    
            if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
    
                snprintf(buf + strlen(buf), buf_size - strlen(buf),
    
                         ", %s", av_get_sample_fmt_name(enc->sample_fmt));
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            break;
    
            snprintf(buf, buf_size, "Data: %s", codec_name);
    
            snprintf(buf, buf_size, "Subtitle: %s", codec_name);
    
            snprintf(buf, buf_size, "Attachment: %s", codec_name);
            break;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        default:
    
            snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
            return;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        }
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        if (encode) {
    
            if (enc->flags & AV_CODEC_FLAG_PASS1)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                snprintf(buf + strlen(buf), buf_size - strlen(buf),
                         ", pass 1");
    
            if (enc->flags & AV_CODEC_FLAG_PASS2)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                snprintf(buf + strlen(buf), buf_size - strlen(buf),
                         ", pass 2");
        }
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        if (bitrate != 0) {
    
            snprintf(buf + strlen(buf), buf_size - strlen(buf),
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                     ", %d kb/s", bitrate / 1000);
    
    const char *av_get_profile_name(const AVCodec *codec, int profile)
    {
        const AVProfile *p;
        if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
            return NULL;
    
        for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
            if (p->profile == profile)
                return p->name;
    
        return NULL;
    }
    
    
    Luca Barbato's avatar
    Luca Barbato committed
    unsigned avcodec_version(void)
    
    Nick Kurshev's avatar
    Nick Kurshev committed
    {
    
    Luca Barbato's avatar
    Luca Barbato committed
        return LIBAVCODEC_VERSION_INT;
    
    Nick Kurshev's avatar
    Nick Kurshev committed
    }
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    
    
    const char *avcodec_configuration(void)
    
        return LIBAV_CONFIGURATION;
    
    {
    #define LICENSE_PREFIX "libavcodec license: "
    
        return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
    
    void avcodec_flush_buffers(AVCodecContext *avctx)
    {
    
    Luca Barbato's avatar
    Luca Barbato committed
        if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
    
    Luca Barbato's avatar
    Luca Barbato committed
        else if (avctx->codec->flush)
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
            avctx->codec->flush(avctx);
    
            av_frame_unref(avctx->internal->to_free);
    
    int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
    
    Luca Barbato's avatar
    Luca Barbato committed
        switch (codec_id) {
    
        case AV_CODEC_ID_ADPCM_CT:
        case AV_CODEC_ID_ADPCM_IMA_APC:
        case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
        case AV_CODEC_ID_ADPCM_IMA_WS:
        case AV_CODEC_ID_ADPCM_G722:
        case AV_CODEC_ID_ADPCM_YAMAHA:
    
            return 4;
    
        case AV_CODEC_ID_PCM_ALAW:
        case AV_CODEC_ID_PCM_MULAW:
        case AV_CODEC_ID_PCM_S8:
        case AV_CODEC_ID_PCM_U8:
        case AV_CODEC_ID_PCM_ZORK:
    
        case AV_CODEC_ID_PCM_S16BE:
    
        case AV_CODEC_ID_PCM_S16BE_PLANAR:
    
        case AV_CODEC_ID_PCM_S16LE:
        case AV_CODEC_ID_PCM_S16LE_PLANAR:
        case AV_CODEC_ID_PCM_U16BE:
        case AV_CODEC_ID_PCM_U16LE:
    
        case AV_CODEC_ID_PCM_S24DAUD:
        case AV_CODEC_ID_PCM_S24BE:
        case AV_CODEC_ID_PCM_S24LE:
    
        case AV_CODEC_ID_PCM_S24LE_PLANAR:
    
        case AV_CODEC_ID_PCM_U24BE:
        case AV_CODEC_ID_PCM_U24LE:
    
        case AV_CODEC_ID_PCM_S32BE:
        case AV_CODEC_ID_PCM_S32LE:
    
        case AV_CODEC_ID_PCM_S32LE_PLANAR:
    
        case AV_CODEC_ID_PCM_U32BE:
        case AV_CODEC_ID_PCM_U32LE:
        case AV_CODEC_ID_PCM_F32BE:
        case AV_CODEC_ID_PCM_F32LE:
    
        case AV_CODEC_ID_PCM_F64BE:
        case AV_CODEC_ID_PCM_F64LE:
    
    int av_get_bits_per_sample(enum AVCodecID codec_id)
    
        case AV_CODEC_ID_ADPCM_SBPRO_2:
    
        case AV_CODEC_ID_ADPCM_SBPRO_3:
    
        case AV_CODEC_ID_ADPCM_SBPRO_4:
        case AV_CODEC_ID_ADPCM_IMA_WAV:
        case AV_CODEC_ID_ADPCM_IMA_QT: