本人刚开始学习ffmpeg,写此博客作为笔记,也希望能帮助其他刚入门的童鞋!ffmpeg源码下载和编译本文就直接跳过,源码doc/example路径下有很多demo,可以为我们演示ffmpeg的api如何使用,对我们理解ffmpeg的api有很大帮助。本文将介绍decode_audio.c如何编译使用。
代码主体框架:
1、根据Codec ID查找编码器(Codec)
2、根据编码器 ID查找解析器(Parse)。注:解析器用于解封装,解封装后的数据放到AVPacket中
3、根据Codec创建AVCodecContext。
4、循环读取输入文件,送入解析器解封装出AVPacket,然后再解码出AVFrame数据。
inputFile ----(解封装)–> AVPacket —(解码)—>AVFrame —(保存pcm数据)–> File.pcm
注:音频中一个AVFrame可能包含多个音频帧。而视频则一个AVFrame对应一帧图像。
因测试方便,我修改了部分代码用于适配FLAC格式的音频文件。代码如下
/*
* Copyright (c) 2001 Fabrice Bellard
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/**
* @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>
#define AUDIO_INBUF_SIZE 20480
#define AUDIO_REFILL_THRESH 4096
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) {
fprintf(stderr, "Error submitting the packet to the decoder\n");
exit(1);
}
/* read all the output frames (in general there may be any number of them */
while (ret >= 0) {
ret = avcodec_receive_frame(dec_ctx, frame);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
return;
else if (ret < 0) {
fprintf(stderr, "Error during decoding\n");
exit(1);
}
data_size = av_get_bytes_per_sample(dec_ctx->sample_fmt);
if (data_size < 0) {
/* This should not occur, checking just for paranoia */
fprintf(stderr, "Failed to calculate data size\n");
exit(1);
}
printf("+++++ FLC-DBG: data_size = %d +++++\n", data_size); //单通道样本(sample)大小
printf("+++++ FLC-DBG: frame->nb_samples = %d +++++\n", frame->nb_samples); //帧个数
printf("+++++ FLC-DBG: dec_ctx->channels = %d +++++\n", dec_ctx->channels); //通道数
printf("+++++ FLC-DBG: frame->linesize[0] = %d +++++\n", frame->linesize[0]); //frame->data[0]数据长度(Bytes)
printf("+++++ FLC-DBG: frame->linesize[1] = %d +++++\n", frame->linesize[1]);
//FLAC格式的文件,解码后数据都放在frame->data[0]中,长度可根据frame->linesize[0]来获取。
//原始代码不同声道数据存在不同的frame->data[i]中。
// for (i = 0; i < frame->nb_samples; i++)
// for (ch = 0; ch < dec_ctx->channels; ch++)
// fwrite(frame->data[ch] + data_size*i, 1, data_size, outfile);
//仅保存单通道数据。
//for (i = 0; i < frame->nb_samples; i++)
// fwrite(frame->data[0] + i * data_size*dec_ctx->channels, 1, data_size, outfile);
//保存全部通道
fwrite(frame->data[0], 1, frame->linesize[0], outfile);
}
}
int main(int argc, char **argv)
{
const char *outfilename, *filename;
const AVCodec *codec;
AVCodecContext *c= 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;
if (argc <= 2) {
fprintf(stderr, "Usage: %s <input file> <output file>\n", argv[0]);
exit(0);
}
filename = argv[1];
outfilename = argv[2];
pkt = av_packet_alloc();
/* find the MPEG audio decoder */
//此处修改为FLAC的codec id。
//codec = avcodec_find_decoder(AV_CODEC_ID_MP2);
codec = avcodec_find_decoder(AV_CODEC_ID_FLAC);
if (!codec) {
fprintf(stderr, "Codec not found\n");
exit(1);
}
parser = av_parser_init(codec->id);
if (!parser) {
fprintf(stderr, "Parser not found\n");
exit(1);
}
c = avcodec_alloc_context3(codec);
if (!c) {
fprintf(stderr, "Could not allocate audio codec context\n");
exit(1);
}
/* open it */
if (avcodec_open2(c, codec, NULL) < 0) {
fprintf(stderr, "Could not open codec\n");
exit(1);
}
f = fopen(filename, "rb");
if (!f) {
fprintf(stderr, "Could not open %s\n", filename);
exit(1);
}
outfile = fopen(outfilename, "wb");
if (!outfile) {
av_free(c);
exit(1);
}
/* decode until eof */
data = inbuf;
data_size = fread(inbuf, 1, AUDIO_INBUF_SIZE, f);
while (data_size > 0) {
if (!decoded_frame) {
if (!(decoded_frame = av_frame_alloc())) {
fprintf(stderr, "Could not allocate audio frame\n");
exit(1);
}
}
ret = av_parser_parse2(parser, c, &pkt->data, &pkt->size,
data, data_size,
AV_NOPTS_VALUE, AV_NOPTS_VALUE, 0);
if (ret < 0) {
fprintf(stderr, "Error while parsing\n");
exit(1);
}
data += ret;
data_size -= ret;
printf("\n+++++ FLC-DBG: pkt->data:%p, pkt->size:%d +++++\n",
pkt->data, pkt->size);
if (pkt->size)
decode(c, pkt, decoded_frame, outfile);
if (data_size < AUDIO_REFILL_THRESH) {
memmove(inbuf, data, data_size);
data = inbuf;
len = fread(data + data_size, 1,
AUDIO_INBUF_SIZE - data_size, f);
if (len > 0)
data_size += len;
}
}
/* flush the decoder */
pkt->data = NULL;
pkt->size = 0;
decode(c, pkt, decoded_frame, outfile);
fclose(outfile);
fclose(f);
avcodec_free_context(&c);
av_parser_close(parser);
av_frame_free(&decoded_frame);
av_packet_free(&pkt);
return 0;
}
使用方法
./decode_audio ~/Music/yesterdayOnceMore.flac ~/Music/output.pcm
查看编码后的数据,使用Audacity,选择导入原始数据。
配置原始数据的采样率、位宽、声道数。
点击播放按钮,试听我们解码出来的文件吧
整个工程的下载地址:
https://download.csdn.net/download/lyy901135/11296755
注:Makefile中需要对头文件和库的路径进行适配。