FFmpeg 解码 avcodec_find_decoder AVCodecContext

avcodec_find_decoder

(1)解码模块第一步:获取解码器 avcodec_find_decoder()FFmpeg的解码器编码器都存在avcodec的结构体中
(2) 解码前保证用到的解码器已经注册好 avcodec_register_all();(过期)
(3)通过解封装之后,从avstream里面获取CodecID ,通过CodecID来查找decoder AVCodec *avcodec_find_decoder(enum AVCodecID id)
AVCodec 存放的是解码器格式的配置信息
(4)通过解码器名字找到解码器 AVCodec *avcodec_find_decoder_by_name(const char
*name);
如:avcodec_find_decoder_by_name(“h264_mediacodec”);

AVCodecContext

(1) AVCodecContext *avcodec_alloc_context3(const AVCodec *codec) 空间申请

(2)void avcodec_free_context(AVCodecContext **avctx); 释放

(3)int avcodec_open2(AVCodecContext *avctx, const AVCodec
*codec, AVDictionary **options) 打开 options动态设置 多线程解码设置
• /libavcodec/options_table.h
• int thread_count CPU数量设置
• time_base 时间基数
(4)avcodec_parameters_to_context(codec, p); 把avstream中的参数复制到codec中

  //解码器初始化
    AVCodecContext *vc = avcodec_alloc_context3(codec);
    vc->thread_count = 1;
    avcodec_parameters_to_context(vc, ic->streams[videoStream]->codecpar);
    //打开解码器
    re = avcodec_open2(vc,0, 0);
    if (re != 0) {
         NSLog(@"avcodec_open2 video failed!");
         return;
    }

    ////////////////////////////////////////////////////
    //音频解码器
    AVCodec *acodec = avcodec_find_decoder(ic->streams[audioStream]->codecpar->codec_id);
    //硬解码
    //    codec = avcodec_find_decoder_by_name("h264_mediacodec");
    if (!acodec) {
        NSLog(@"avcodec_find_decoder_by_name audio failed!");
        return ;
    }
    //解码器初始化
    AVCodecContext *ac = avcodec_alloc_context3(acodec);
    ac->thread_count = 1;
    avcodec_parameters_to_context(ac, ic->streams[audioStream]->codecpar);
    //打开解码器
    re = avcodec_open2(ac,0, 0);
    if (re != 0) {
        NSLog(@"avcodec_open2 audio failed!");
        return;
    }

AVFrame

AVFrame用于存放解码后的数据
• AVFrame *frame = av_frame_alloc() 分配内存空间

• void av_frame_free(AVFrame **frame) 释放内存空间

• int av_frame_ref(AVFrame *dst, const AVFrame *src) 引用计数+1

• AVFrame *av_frame_clone(const AVFrame *src) 重新创建一个空间 引用计数+1 一般不用比较耗时

• void av_frame_unref(AVFrame *frame) 引用计数减一

//////////

• uint8_t *data[AV_NUM_DATA_POINTERS]

• int linesize[AV_NUM_DATA_POINTERS]
视频一行的数据 音频一个通道数据大小 存放的目的主要是为了按字节对齐左右
自己做冲采样可以按行做对比
image.png

• int width, height(宽高);int nb_samples (单通道的样本数量)

• int64_t pts ;int64_t pkt_dts

• int sample_rate;uint64_t channel_layout;int channels

• int format; //AVPixelFormat AVSampleFormat 像素格式


avcodec_send_packet

• int avcodec_send_packet(AVCodecContext *avctx, const AVPacket *avpkt)
把avpkt放入解码队列的缓存中 avcodec_send_packet 调用后 会创建一个 avpkt对象,会把实际的数据 引用数据+1 或者copy一份

avcodec_receive_frame

• int avcodec_receive_frame(AVCodecContex t *avctx, AVFrame *frame) (传同一个对象的时候 会清掉上一个) 从解码成功的数据中取出一个 frame 解码的时候前几帧的可能获取失败 导致播放的时候视频最后的几帧没有播放(解决方案 结尾处的avcodec_send_packet avpkt传NULL 一直avcodec_receive_frame,直到取不出帧为止)
avcodec_send_packet 和 avcodec_receive_frame 不是一一对应的

        //测试音视频解码
        AVCodecContext *cc = vc;
        if (pkt->stream_index == audioStream) {
            cc = ac;
        }

        //发送的线程中解码
        re = avcodec_send_packet(cc, pkt);\
        //解码
        av_packet_unref(pkt);

        if (re != 0) {
            NSLog(@"avcodec_send_packet audio failed!");
            continue;
        }
        for (; ; ) {//保证能搜到全部的解码数据
            re = avcodec_receive_frame(cc, frame);
            if (re != 0) {
//                NSLog(@"avcodec_receive_frame audio failed!");
                break;
            }
            NSLog(@"frame->pts %lld",frame->pts);
        }
        //最后一针的时候 avcodec_send_packet(cc, NULL)

猜你喜欢

转载自blog.csdn.net/u010141160/article/details/81563751