簡體   English   中英

FFmpeg h265 編碼->解碼,C++

[英]FFmpeg h265 encode->decode, c++

嗨,我已經有一些適用於 h264 的工作編碼和直接解碼代碼。 我正在嘗試修改代碼以使用 h265,但我在解碼方面做錯了,因為我總是收到錯誤消息:

[hevc @ 0x78eca0] PPS id out of range: 0
[hevc @ 0x78eca0] Error parsing NAL unit #0.

解碼初始化:

decoder = avcodec_find_decoder(AV_CODEC_ID_H265);

ctx = avcodec_alloc_context3(decoder);
ctx->extradata = NULL;
ctx->width = 400;
ctx->height = 256;
ctx->pix_fmt = AV_PIX_FMT_YUV420P;

avcodec_open2(ctx,decoder,NULL);

編碼,似乎沒問題,因為我只處理小視頻,所以我只能得到一個 nal :

x265_nal* nals;
unsigned int i_nals;

int ret = x265_encoder_encode(m_x265Encoder, &nals, &i_nals, m_picIn, m_picOut);

AVPacket avpkt;
av_init_packet(&avpkt);
avpkt.size = nals[0].sizeBytes;
avpkt.data = nals[0].payload;
AVFrame* frame = avcodec_alloc_frame();

int got=0;
avcodec_decode_video2(ctx,frame,&got,&avpkt);

有人可以幫我解決這個問題嗎?

邁克

您確定在實際壓縮圖片之前將SPS,PPS,VPS NAL單元傳遞給了解碼器嗎?好像解碼器有圖片但還沒有PPS。

實際上我找到了一個可行的解決方案,它不是最好的代碼,主要用於測試,最終 x265 只是在 CPU 上變慢,無法跟上直播:

    AVFrame* avpic = av_frame_alloc();
    avpic->pts = msg.TimeStampCapture;
    avpic->format = AV_PIX_FMT_YUV420P;
    avpic->width = _dstFrameWidth;
    avpic->height = _dstFrameHeight;

    AVPicture tmppic;
    avpicture_fill(&tmppic, pSrc, _rawFormat, _srcFrameWidth, _srcFrameHeight);
    int err_sws = sws_scale(pSWSContext, tmppic.data, tmppic.linesize, 0, _srcFrameHeight, avpic->data, avpic->linesize);

    int ret = av_image_alloc(avpic->data, avpic->linesize, avpic->width, avpic->height,AV_PIX_FMT_YUV420P,8 );

    // Using scale Context to create avpic fore video2 encode
    SwsContext* pSWSContext = sws_getContext(_srcFrameWidth, _srcFrameHeight, _rawFormat, _dstFrameWidth, _dstFrameHeight, AV_PIX_FMT_YUV420P, SWS_BILINEAR , 0, 0, 0);

    sws_freeContext(pSWSContext);
    int got_packet;
    AVPacket pkt;
    av_init_packet(&pkt);
    pkt.data = NULL;    // packet data will be allocated by the encoder
    pkt.size = 0;

    ret = avcodec_encode_video2(_ectx,&pkt,avpic,&got_packet);
    //count ++;
    msg.pts = pkt.pts;
    msg.dts = pkt.dts;
    msg.count = count++;
    msg.flags = pkt.flags;


    nEncodedSize = pkt.size;


    /// Decoder test
    AVPacket avpkt;
    av_init_packet(&avpkt);
    avpkt.data = pkt.data;
    avpkt.size = pkt.size;
    avpkt.pts = pkt.pts;
    avpkt.dts = pkt.dts;
    avpkt.flags = pkt.flags;
    avpkt.stream_index = pkt.stream_index;
    AVFrame* frame = av_frame_alloc();


    int got_picture = 0;
    avcodec_decode_video2(m_cc,frame,&got_picture,&avpkt)

    av_frame_free(&avpic);
    av_packet_unref(&pkt);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM