音視頻 day23 視頻解碼 H264

1. h264的壓縮編碼文件,播放器能直接播放嗎?

  • 大部分狀況下能夠,由於播放器基本都集成了 h264 解碼器。(相似 AAC 的原理)

2. 繪圖解釋 h264 解碼的過程?(必須能默出來)

image.png

3. h264 解碼的過程,若是邊框尺寸不是 16 的倍數,可能會出問題嗎?

  • 對,可能會被轉換尺寸,不會按照要求

4. h264 解碼主要源代碼以下:

  • ffmpegs.h
#ifndef FFMPEGS_H
#define FFMPEGS_H

extern "C" {
#include <libavutil/avutil.h>
}

typedef struct {
    const char *filename;
    int width;
    int height;
    AVPixelFormat pixFmt;
    int fps;
} VideoDecodeSpec;

class FFmpegs {
public:
    FFmpegs();

    static void h264Decode(const char *inFilename, VideoDecodeSpec &out);
};

#endif // FFMPEGS_H

複製代碼
  • ffmpegs.m
#include "ffmpegs.h"
#include <QDebug>
#include <QFile>

extern "C" {
#include <libavcodec/avcodec.h>
#include <libavutil/avutil.h>
#include <libavutil/imgutils.h>
}

#define ERROR_BUF(ret) \ char errbuf[1024]; \ av_strerror(ret, errbuf, sizeof (errbuf));

// 輸入緩衝區的大小
#define IN_DATA_SIZE 4096

FFmpegs::FFmpegs() {

}

static int frameIdx = 0;

static int decode(AVCodecContext *ctx, AVPacket *pkt, AVFrame *frame, QFile &outFile) {
    // 發送壓縮數據到解碼器
    int ret = avcodec_send_packet(ctx, pkt);
    if (ret < 0) {
        ERROR_BUF(ret);
        qDebug() << "avcodec_send_packet error" << errbuf;
        return ret;
    }

    while (true) {
        // 獲取解碼後的數據
        ret = avcodec_receive_frame(ctx, frame);
        if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
            return 0;
        } else if (ret < 0) {
            ERROR_BUF(ret);
            qDebug() << "avcodec_receive_frame error" << errbuf;
            return ret;
        }

        qDebug() << "解碼出第" << ++frameIdx << "幀";

        // 將解碼後的數據寫入文件
        // 寫入Y平面
        outFile.write((char *) frame->data[0],
                      frame->linesize[0] * ctx->height);
        // 寫入U平面
        outFile.write((char *) frame->data[1],
                      frame->linesize[1] * (ctx->height >> 1));
        // 寫入V平面
        outFile.write((char *) frame->data[2],
                      frame->linesize[2] * (ctx->height >> 1));

        qDebug() << "寫入Y平面" << " linesize "  << frame->linesize[0] << " 總大小 " << (frame->linesize[0] * ctx->height );
        qDebug() << "寫入U平面" << " linesize "  << frame->linesize[1] << " 總大小 " << (frame->linesize[1] * ctx->height >> 1);
        qDebug() << "寫入V平面" << " linesize "  << frame->linesize[2] << " 總大小 " << (frame->linesize[2] * ctx->height >> 1);

        /* * frame->data[0] 0xd08c400 0x8c400 * frame->data[1] 0xd0d79c0 0xd79c0 * frame->data[2] 0xd0ea780 0xea780 * * frame->data[1] - frame->data[0] = 308672 = y平面的大小 * frame->data[2] - frame->data[1] = 77248 = u平面的大小 * * y平面的大小 640x480*1 = 307200 * u平面的大小 640x480*0.25 = 76800 * v平面的大小 640x480*0.25 */

// // 將解碼後的數據寫入文件(460800)
// int imgSize = av_image_get_buffer_size(ctx->pix_fmt, ctx->width, ctx->height, 1);
// // outFile.write((char *) frame->data[0], frame->linesize[0]);
// outFile.write((char *) frame->data[0], imgSize);
    }
}

void FFmpegs::h264Decode(const char *inFilename, VideoDecodeSpec &out) {
    // 返回結果
    int ret = 0;

    // 用來存放讀取的輸入文件數據(h264)
    // 加上AV_INPUT_BUFFER_PADDING_SIZE是爲了防止某些優化過的reader一次性讀取過多致使越界
    char inDataArray[IN_DATA_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];
    char *inData = inDataArray;

    // 每次從輸入文件中讀取的長度(h264)
    // 輸入緩衝區中,剩下的等待進行解碼的有效數據長度
    int inLen;
    // 是否已經讀取到了輸入文件的尾部
    int inEnd = 0;

    // 文件
    QFile inFile(inFilename);
    QFile outFile(out.filename);

    // 解碼器
    AVCodec *codec = nullptr;
    // 上下文
    AVCodecContext *ctx = nullptr;
    // 解析器上下文
    AVCodecParserContext *parserCtx = nullptr;

    // 存放解碼前的數據(h264)
    AVPacket *pkt = nullptr;
    // 存放解碼後的數據(yuv)
    AVFrame *frame = nullptr;

    // 獲取解碼器
    // codec = avcodec_find_decoder_by_name("h264");
    codec = avcodec_find_decoder(AV_CODEC_ID_H264);
    if (!codec) {
        qDebug() << "decoder not found";
        return;
    }

    // 初始化解析器上下文
    parserCtx = av_parser_init(codec->id);
    if (!parserCtx) {
        qDebug() << "av_parser_init error";
        return;
    }

    // 建立上下文
    ctx = avcodec_alloc_context3(codec);
    if (!ctx) {
        qDebug() << "avcodec_alloc_context3 error";
        goto end;
    }

    // 建立AVPacket
    pkt = av_packet_alloc();
    if (!pkt) {
        qDebug() << "av_packet_alloc error";
        goto end;
    }

    // 建立AVFrame
    frame = av_frame_alloc();
    if (!frame) {
        qDebug() << "av_frame_alloc error";
        goto end;
    }

    // 打開解碼器
    ret = avcodec_open2(ctx, codec, nullptr);
    if (ret < 0) {
        ERROR_BUF(ret);
        qDebug() << "avcodec_open2 error" << errbuf;
        goto end;
    }

    // 打開文件
    if (!inFile.open(QFile::ReadOnly)) {
        qDebug() << "file open error:" << inFilename;
        goto end;
    }
    if (!outFile.open(QFile::WriteOnly)) {
        qDebug() << "file open error:" << out.filename;
        goto end;
    }

    // 讀取文件數據
    do {
        inLen = inFile.read(inDataArray, IN_DATA_SIZE);
        // 設置是否到了文件尾部
        inEnd = !inLen;

        // 讓inData指向數組的首元素
        inData = inDataArray;

        // 只要輸入緩衝區中還有等待進行解碼的數據
        while (inLen > 0 || inEnd) {
            // 到了文件尾部(雖然沒有讀取任何數據,但也要調用av_parser_parse2,修復bug)

            // 通過解析器解析
            ret = av_parser_parse2(parserCtx, ctx,
                                   &pkt->data, &pkt->size,
                                   (uint8_t *) inData, inLen,
                                   AV_NOPTS_VALUE, AV_NOPTS_VALUE, 0);

            if (ret < 0) {
                ERROR_BUF(ret);
                qDebug() << "av_parser_parse2 error" << errbuf;
                goto end;
            }

            // 跳過已經解析過的數據
            inData += ret;
            // 減去已經解析過的數據大小
            inLen -= ret;

            qDebug() << inEnd << pkt->size << ret;

            // 解碼
            if (pkt->size > 0 && decode(ctx, pkt, frame, outFile) < 0) {
                goto end;
            }

            // 若是到了文件尾部
            if (inEnd) break;
        }
    } while (!inEnd);

    // 刷新緩衝區
    // pkt->data = nullptr;
    // pkt->size = 0;
    // decode(ctx, pkt, frame, outFile);
    decode(ctx, nullptr, frame, outFile);

    // 賦值輸出參數
    out.width = ctx->width;
    out.height = ctx->height;
    out.pixFmt = ctx->pix_fmt;
    // 用framerate.num獲取幀率,並非time_base.den
    out.fps = ctx->framerate.num;

end:
    inFile.close();
    outFile.close();
    av_packet_free(&pkt);
    av_frame_free(&frame);
    av_parser_close(parserCtx);
    avcodec_free_context(&ctx);

// bug fix
// https://patchwork.ffmpeg.org/project/ffmpeg/patch/tencent_609A2E9F73AB634ED670392DD89A63400008@qq.com/

//
// while ((inLen = inFile.read(inDataArray, IN_DATA_SIZE)) > 0)
// while (inLen > 0) {
// // 通過解析器解析
// ret = av_parser_parse2(parserCtx, ctx,
// &pkt->data, &pkt->size,
// (uint8_t *) inData, inLen,
// AV_NOPTS_VALUE, AV_NOPTS_VALUE, 0);

// if (ret < 0) {
// ERROR_BUF(ret);
// qDebug() << "av_parser_parse2 error" << errbuf;
// goto end;
// }

// // 跳過已經解析過的數據
// inData += ret;
// // 減去已經解析過的數據大小
// inLen -= ret;

// // 解碼
// if (pkt->size > 0 && decode(ctx, pkt, frame, outFile) < 0) {
// goto end;
// }
// }
// }
}

複製代碼
相關文章
相關標籤/搜索