zoukankan      html  css  js  c++  java
  • avcodec_decode_video2函数

    转自 https://www.xuebuyuan.com/2156374.html

    该函数的作用是实现压缩视频的解码。在avcodec.h中的声明方式如下: 

    int avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture, int *got_picture_ptr, const AVPacket *avpkt);

    待解码的数据保存在avpkt->data中,大小为avpkt->size;解码完成后,picture用于保存输出图像数据。

    该方法的各个参数:

    AVCodecContext *avctx:编解码上下文环境,定义了编解码操作的一些细节;

    AVFrame *picture:输出参数;传递到该方法的对象本身必须在外部由av_frame_alloc()分配空间,而实际解码过后的数据储存区将由AVCodecContext.get_buffer2()分配;AVCodecContext.refcounted_frames表示该frame的引用计数,当这个值为1时,表示有另外一帧将该帧用作参考帧,而且参考帧返回给调用者;当参考完成时,调用者需要调用av_frame_unref()方法解除对该帧的参考;av_frame_is_writable()可以通过返回值是否为1来验证该帧是否可写。

    int *got_picture_ptr:该值为0表明没有图像可以解码,否则表明有图像可以解码;

    const AVPacket *avpkt:输入参数,包含待解码数据。

    int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture, int *got_picture_ptr,
     const AVPacket *avpkt)
    {
        AVCodecInternal *avci = avctx->internal;
        int ret;
        // copy to ensure we do not change avpkt
        AVPacket tmp = *avpkt;
    
        if (!avctx->codec)
            return AVERROR(EINVAL);
        if (avctx->codec->type != AVMEDIA_TYPE_VIDEO) {
            av_log(avctx, AV_LOG_ERROR, "Invalid media type for video
    ");
            return AVERROR(EINVAL);
        }
    
        *got_picture_ptr = 0;
        if ((avctx->coded_width || avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
            return AVERROR(EINVAL);
    
        avcodec_get_frame_defaults(picture);
    
        if (!avctx->refcounted_frames)
            av_frame_unref(&avci->to_free);
    
        if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
            int did_split = av_packet_split_side_data(&tmp);
            apply_param_change(avctx, &tmp);
            avctx->pkt = &tmp;
            if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
                ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
                                             &tmp);
            else {
                ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
                                           &tmp);
                picture->pkt_dts = avpkt->dts;
    
                if(!avctx->has_b_frames){
                    av_frame_set_pkt_pos(picture, avpkt->pos);
                }
                //FIXME these should be under if(!avctx->has_b_frames)
                /* get_buffer is supposed to set frame parameters */
                if (!(avctx->codec->capabilities & CODEC_CAP_DR1)) {
                    if (!picture->sample_aspect_ratio.num)    picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
                    if (!picture->width)                      picture->width               = avctx->width;
                    if (!picture->height)                     picture->height              = avctx->height;
                    if (picture->format == AV_PIX_FMT_NONE)   picture->format              = avctx->pix_fmt;
                }
            }
            add_metadata_from_side_data(avctx, picture);
    
            emms_c(); //needed to avoid an emms_c() call before every return;
    
            avctx->pkt = NULL;
            if (did_split) {
                av_packet_free_side_data(&tmp);
                if(ret == tmp.size)
                    ret = avpkt->size;
            }
    
            if (ret < 0 && picture->data[0])
                av_frame_unref(picture);
    
            if (*got_picture_ptr) {
                if (!avctx->refcounted_frames) {
                    avci->to_free = *picture;
                    avci->to_free.extended_data = avci->to_free.data;
                    memset(picture->buf, 0, sizeof(picture->buf));
                }
    
                avctx->frame_number++;
                av_frame_set_best_effort_timestamp(picture, guess_correct_pts(avctx, picture->pkt_pts, picture->pkt_dts));
            }
        } else
            ret = 0;
    
        /* many decoders assign whole AVFrames, thus overwriting extended_data;
         * make sure it's set correctly */
        picture->extended_data = picture->data;
    
        return ret;
    }

    在该函数中,调用了ret = avctx->codec->decode(avctx, picture, got_picture_ptr, &tmp);实现解码功能。在当前demo中,codec类型为ff_hevc_decoder,decode指针指向的函数为hevc_decode_frame。ff_hevc_decoder的定义如下:

    AVCodec ff_hevc_decoder = {
        .name                  = "hevc",
        .long_name             = NULL_IF_CONFIG_SMALL("HEVC (High Efficiency Video Coding)"),
        .type                  = AVMEDIA_TYPE_VIDEO,
        .id                    = AV_CODEC_ID_HEVC,
        .priv_data_size        = sizeof(HEVCContext),
        .priv_class            = &hevc_decoder_class,
        .init                  = hevc_decode_init,
        .close                 = hevc_decode_free,
        .decode                = hevc_decode_frame,
        .flush                 = hevc_decode_flush,
        .update_thread_context = hevc_update_thread_context,
        .init_thread_copy      = hevc_init_thread_copy,
        .capabilities   = CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_SLICE_THREADS | CODEC_CAP_FRAME_THREADS,
    };

    解码函数:

    static int hevc_decode_frame(AVCodecContext *avctx, void *data, int *got_output,
                                 AVPacket *avpkt)
    {
        int ret;
        HEVCContext *s = avctx->priv_data;
    
        if (!avpkt->size) {
            ret = ff_hevc_output_frame(s, data, 1);
            if (ret < 0)
                return ret;
    
            *got_output = ret;
            return 0;
        }
    
        s->ref = NULL;
        ret = decode_nal_units(s, avpkt->data, avpkt->size);
        if (ret < 0)
            return ret;
    
        /* verify the SEI checksum */
        if (avctx->err_recognition & AV_EF_CRCCHECK && s->is_decoded &&
            avctx->err_recognition & AV_EF_EXPLODE &&
            s->is_md5) {
            ret = verify_md5(s, s->ref->frame);
            if (ret < 0) {
                ff_hevc_unref_frame(s, s->ref, ~0);
                return ret;
            }
        }
        s->is_md5 = 0;
    
        if (s->is_decoded) {
            av_log(avctx, AV_LOG_DEBUG, "Decoded frame with POC %d.
    ", s->poc);
            s->is_decoded = 0;
        }
    
        if (s->output_frame->buf[0]) {
            av_frame_move_ref(data, s->output_frame);
            *got_output = 1;
        }
    
        return avpkt->size;
    }

    熟悉编解码标准的同学都知道,H.264和HEVC都定义了网络抽象层NAL来执行传输层的任务,每一个NAL单元都按照规定保存了某些语法元素。函数decode_nal_units执行了对这些NAL单元进行解析并对NAL的下一层视频编码层VCL进行解码的任务。

  • 相关阅读:
    centos7下配置时间同步服务器
    交换机简单配置(转)
    ubuntu16.04安装docker CE
    docker下使用DB2
    iptables之centos6版本详解
    iptables之centos6版本常用设置
    iptables介绍iptables和netfilter
    git的使用学习(九)搭建git服务器
    js 图片预览
    ps -ef | grep java 查看所有关于java的进程
  • 原文地址:https://www.cnblogs.com/nanqiang/p/11387269.html
Copyright © 2011-2022 走看看