ffmpeg_sample解讀_decode_audio


title: ffmpeg_sample解讀_decode_audio
date: 2020-10-21 10:15:02
tags: [讀書筆記]
typora-copy-images-to: ./imgs
typora-root-url: ./imgs


概括

Ffmpeg 項目中的 實例解讀. 把他移到安卓項目中來開發了.

這個項目是解碼音頻數據,輸入aac格式的文件.最后生成pcm格式的文件

總結. 就是每次從文件中讀取一部分數據(20480),然后把這部分數據解析成packet. 然后在送入解碼器解碼成frame.最后寫到文件中,然后在繼續從文件中讀取數據,解析packet.生成frame.

附一個網上的圖,他講的也很好.附上源連接

博客:http://www.lxweimin.com/p/d77718947e21
1

源碼講解


/**
 * @file
 * audio decoding with libavcodec API example
 *
 * @example decode_audio.c
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <libavutil/frame.h>
#include <libavutil/mem.h>

#include <libavcodec/avcodec.h>
#include "../macro.h"

#define AUDIO_INBUF_SIZE 20480
#define AUDIO_REFILL_THRESH 4096

static int get_format_from_sample_fmt(const char **fmt,
                                      enum AVSampleFormat sample_fmt) {
    int i;
    struct sample_fmt_entry {
        enum AVSampleFormat sample_fmt;
        const char *fmt_be, *fmt_le;
    } sample_fmt_entries[] = {
            {AV_SAMPLE_FMT_U8,  "u8",    "u8"},
            {AV_SAMPLE_FMT_S16, "s16be", "s16le"},
            {AV_SAMPLE_FMT_S32, "s32be", "s32le"},
            {AV_SAMPLE_FMT_FLT, "f32be", "f32le"},
            {AV_SAMPLE_FMT_DBL, "f64be", "f64le"},
    };
    *fmt = NULL;

    for (i = 0; i < FF_ARRAY_ELEMS(sample_fmt_entries); i++) {
        struct sample_fmt_entry *entry = &sample_fmt_entries[i];
        if (sample_fmt == entry->sample_fmt) {
            *fmt = AV_NE(entry->fmt_be, entry->fmt_le);
            return 0;
        }
    }

    LOGE(stderr,
         "sample format %s is not supported as output format\n",
         av_get_sample_fmt_name(sample_fmt));
    return -1;
}

static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame,
                   FILE *outfile) {
    int i, ch;
    int ret, data_size;

    //把文件解析的數據發送到解碼器中
    /* send the packet with the compressed data to the decoder */
    ret = avcodec_send_packet(dec_ctx, pkt);
    if (ret < 0) {// 這里出錯
        LOGE(stderr, "Error submitting the packet to the decoder\n%s", ret);
        exit(1);
    }

    /* read all the output frames (in general there may be any number of them */
    while (ret >= 0) {
        //獲取解碼出的frame .一個packet可能會解碼出許多的frame.
        ret = avcodec_receive_frame(dec_ctx, frame);
        if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
            return;//出錯或者到了末尾
        else if (ret < 0) {
            LOGE(stderr, "Error during decoding\n");
            exit(1);
        }
        //根據采樣位數拿到采樣格式拿到采樣位數
        data_size = av_get_bytes_per_sample(dec_ctx->sample_fmt);
        LOGE("采樣位數 %d", data_size);
        if (data_size < 0) {//4
            /* This should not occur, checking just for paranoia */
            LOGE(stderr, "Failed to calculate data size\n");
            exit(1);
        }
        for (i = 0; i < frame->nb_samples; i++)//每個聲道的樣本數 nb_samples 1021 channels 2
            for (ch = 0; ch < dec_ctx->channels; ch++) //所有的通道
                fwrite(frame->data[ch] + data_size * i, 1, data_size, outfile);
    }
}

/**
 * 解碼音頻文件 輸出 pcm 源文件
 * @param argc
 * @param argv
 * @return
 avcodec_find_decoder:根據指定的AVCodecID查找注冊的解碼器。
av_parser_init:初始化AVCodecParserContext。
avcodec_alloc_context3:為AVCodecContext分配內存。
avcodec_open2:打開解碼器。
av_parser_parse2:解析獲得一個Packet。
avcodec_send_packet:將AVPacket壓縮數據給解碼器。
avcodec_receive_frame:獲取到解碼后的AVFrame數據。
av_get_bytes_per_sample: 獲取每個sample中的字節數
 輸入 /storage/emulated/0/1.aac
 輸出 /storage/emulated/0/1.pcm
 播放 ffplay -f f32le -ac 2 -ar 48000 /storage/emulated/0/1.pcm
 博客:http://www.lxweimin.com/p/d77718947e21
 */
int decode_audio_main(int argc, char **argv) {
    const char *outfilename, *filename;
    const AVCodec *codec;
    AVCodecContext *codecContext = NULL;
    AVCodecParserContext *parser = NULL;
    int len, ret;
    FILE *f, *outfile;
    uint8_t inbuf[AUDIO_INBUF_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];// 數據加偏移,防止一次讀到末尾
    uint8_t *data;
    size_t data_size;
    AVPacket *pkt;
    AVFrame *decoded_frame = NULL;
    enum AVSampleFormat sfmt;
    int n_channels = 0;
    const char *fmt;

    if (argc <= 2) {
        LOGE(stderr, "Usage: %s <input file> <output file>\n", argv[0]);
        exit(0);
    }
    filename = argv[1];
    outfilename = argv[2];
    //初始化一個packet,用來接收解封裝后的數據
    pkt = av_packet_alloc();

    /* find the MPEG audio decoder */
    //找到解碼器,指定類型的音頻解碼器 這里指的AAC 因為輸入文件是aac
    codec = avcodec_find_decoder(AV_CODEC_ID_AAC);
    if (!codec) {
        LOGE(stderr, "Codec not found\n");
        exit(1);
    }
    //通過解碼器找到paser上下文
//    AVCodecParser:用于解析輸入的數據流并把它分成一幀一幀的壓縮編碼數據。比較形象的說法就是把長長的一段連續的數據“切割”成一段段的數據
    parser = av_parser_init(codec->id);
    if (!parser) {
        LOGE(stderr, "Parser not found\n");
        exit(1);
    }

    //c創建解碼器上下文,用給定的解碼器. 初始化默認參數
    codecContext = avcodec_alloc_context3(codec);
    if (!codecContext) {
        LOGE(stderr, "Could not allocate audio codec context\n");
        exit(1);
    }

    /* open it *///打開解碼器上下文.我理解就是通過解碼器進行合適的參數設置
    if (avcodec_open2(codecContext, codec, NULL) < 0) {
        LOGE(stderr, "Could not open codec\n");
        exit(1);
    }
    //打開讀入文件,指定讀入方式
    f = fopen(filename, "rb");
    if (!f) {
        LOGE(stderr, "Could not open %s\n", filename);
        exit(1);
    }
    //寫出的文件
    outfile = fopen(outfilename, "wb");
    if (!outfile) {
        av_free(codecContext);
        exit(1);
    }

    /* decode until eof */
    data = inbuf;
    //從f中讀取20k的數據到inbuf中,返回讀了多少進來
    data_size = fread(inbuf, 1, AUDIO_INBUF_SIZE, f);
    LOGE("data_size is %d ", data_size);

    while (data_size > 0) {
        if (!decoded_frame) {
            //分配1幀用來解析數據,這是存放解碼后的數據
            if (!(decoded_frame = av_frame_alloc())) {
                LOGE(stderr, "Could not allocate audio frame\n");
                exit(1);
            }
        }

        //把從文件中讀取到的20k的數據的data中轉到packet中,最后更新data和data.size,此時數據在packet中,
        //最后三個參數是pts. dts pos ret 表示解析了多少數據
        ret = av_parser_parse2(parser, codecContext, &pkt->data, &pkt->size,
                               data, data_size,
                               AV_NOPTS_VALUE, AV_NOPTS_VALUE, 0);
        if (ret < 0) {
            LOGE(stderr, "Error while parsing\n");
            exit(1);
        }
        //這里表示已經從data中解析了多少數據到packet中
        data += ret;
        data_size -= ret; //表示已經解析了多少數據

        if (pkt->size)
            //把解析出的數據解碼后放到frame里,然后輸出
            decode(codecContext, pkt, decoded_frame, outfile);

        //這里表示data剩余的數據已經小于 4096這個閾值.需要繼續從文件中讀取一部分數據.
        if (data_size < AUDIO_REFILL_THRESH) {
            // 從data中拷貝 data_size 的數據到inbuf 中,也就是把data剩余的數據留下.已經解析的都刪掉.
            memmove(inbuf, data, data_size);
            data = inbuf;
            //在繼續從文件中讀入數據.因為data此時不是完全空的,所以要減去已用的空間
            len = fread(data + data_size, 1,
                        AUDIO_INBUF_SIZE - data_size, f);
            if (len > 0)
                data_size += len; //表示從文件中讀取完成后,還有多少可以解析的數據
        }
    }

    //到這里文件的數據都寫完了. 需要刷新解碼器把未完成的packet都解碼出來
    /* flush the decoder */
    pkt->data = NULL;
    pkt->size = 0;
    decode(codecContext, pkt, decoded_frame, outfile);

    //接下來是打印格式 .因為pcm只有音頻數據.無法播放.需要指定相關數據
    /* print output pcm infomations, because there have no metadata of pcm */
    sfmt = codecContext->sample_fmt;

    if (av_sample_fmt_is_planar(sfmt)) {
        const char *packed = av_get_sample_fmt_name(sfmt);
        LOGE("Warning: the sample format the decoder produced is planar "
             "(%s). This example will output the first channel only.\n",
             packed ? packed : "?");
        sfmt = av_get_packed_sample_fmt(sfmt);
    }

    n_channels = codecContext->channels;
    if ((ret = get_format_from_sample_fmt(&fmt, sfmt)) < 0)
        goto end;

    LOGE("Play the output audio file with the command:\n"
         "ffplay -f %s -ac %d -ar %d %s\n",
         fmt, n_channels, codecContext->sample_rate,
         outfilename);
    //-f 格式, -ac 通道數  -ar pcm采樣率
    //ffplay -f f32le -ac 2 -ar 48000 /storage/emulated/0/1.pcm
    end:
    fclose(outfile);
    fclose(f);

    avcodec_free_context(&codecContext);
    av_parser_close(parser);
    av_frame_free(&decoded_frame);
    av_packet_free(&pkt);

    return 0;
}

本文由博客群發一文多發等運營工具平臺 OpenWrite 發布

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。