ffmpeg学习笔记-音频解码

时间:2023-06-12 10:02:32

在之前的文章已经初步对视频解码有个初步的认识了,接下来来看一看音频解码

音频解码步骤

音频解码与视频解码一样,有者固有的步骤,只要按照步骤来,就能顺利的解码音频

ffmpeg学习笔记-音频解码

以上是ffmpeg的解码流程图,可以看到,无论是音频还是视频,其步骤都是一样的,差别在于在视频处理和音频处理的方法不一样

音频解码步骤

由于音频解码步骤与视频类似,这里就直接使用之前测试ffmpeg生成是否可用的demo进行改进

  • 注册组件av_register_all()
  • 打开输入文件avformat_open_input()
  • 获取输入信息avformat_find_stream_info()
  • 获取解码器avcodec_find_decoder()
  • 打开解码器avcodec_open2()
  • 循环读取一帧av_read_frame()
  • 在循环中判断是否得到数据avcodec_decode_audio4()
  • 对得到的数据进行处理
  • 关闭打开的资源

代码示例(将原音频转为PCM保存在本地)

之前已经对示例进行过详细讲解,这里就不再罗嗦,只列举出native的实现

#include <stdlib.h>
#include <stdio.h>
#include <android/log.h> #include "com_cj5785_ffmpegaudioplayer_SoundPlayer.h" //封装格式
#include "include/libavformat/avformat.h"
//解码
#include "include/libavcodec/avcodec.h"
//像素处理
#include "include/libswscale/swscale.h"
//重采样
#include "include/libswresample/swresample.h" #define MAX_AUDIO_FRME_SIZE 48000 * 4
#define LOGI(FORMAT,...) __android_log_print(4,"cj5785",FORMAT,##__VA_ARGS__);
#define LOGE(FORMAT,...) __android_log_print(6,"cj5785",FORMAT,##__VA_ARGS__); JNIEXPORT void JNICALL Java_com_cj5785_ffmpegaudioplayer_SoundPlayer_sound
(JNIEnv *env, jobject jobj, jstring jstr_input, jstring jstr_output)
{
const char *input = (*env)->GetStringUTFChars(env, jstr_input, NULL);
const char *output = (*env)->GetStringUTFChars(env, jstr_output, NULL); LOGI("%s",input);
//注册组件
av_register_all(); //打开输入文件
AVFormatContext *pFormatCtx = avformat_alloc_context();
if(avformat_open_input(&pFormatCtx, input, NULL, NULL) != 0)
{
LOGE("%s", "打开文件失败!");
return;
} //获取流信息
if(avformat_find_stream_info(pFormatCtx, NULL) < 0)
{
LOGE("%s","获取输入文件信息失败!");
return;
} //对输入流做音视频判断,获取音频流索引位置
int i = 0;
int audio_stream_index = -1;
for(; i < pFormatCtx->nb_streams; i++)
{
//判断是否是音频流
if(pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO)
{
audio_stream_index = i;
break;
}
}
if(audio_stream_index == -1)
{
LOGE("%s", "找不到音频流!");
return;
} //获取解码器
AVCodecContext *codecCtx = pFormatCtx->streams[audio_stream_index]->codec;
AVCodec *codec = avcodec_find_decoder(codecCtx->codec_id);
if(codec == NULL)
{
LOGE("%s", "无法获取解码器");
return;
} //打开解码器
if(avcodec_open2(codecCtx, codec, NULL) < 0)
{
LOGI("%s", "无法打开解码器");
return;
} //压缩数据
AVPacket *packet = (AVPacket *)av_malloc(sizeof(AVPacket));
//解压缩数据
AVFrame *frame = av_frame_alloc();
//将压缩数据转化为16bits 44100Hz PCM 统一音频采样格式与采样率
SwrContext *swrCtx = swr_alloc();
//----------重采样设置参数----------
//输入采样格式
enum AVSampleFormat in_sample_fmt = codecCtx->sample_fmt;
//输出采样格式
enum AVSampleFormat out_sample_fmt = AV_SAMPLE_FMT_S16;
//输入采样率
int in_sample_rate = codecCtx->sample_rate;
//输出采样率
int out_sample_rate = in_sample_rate;
//获取输入的声道布局,根据声道个数获取声道布局 av_get_default_channel_layout(codec->channel_layouts);
uint64_t in_ch_layout = codecCtx->channel_layout;
//输出声道默认为立体声
uint64_t out_ch_layout = AV_CH_LAYOUT_STEREO;
swr_alloc_set_opts(swrCtx,
out_ch_layout, out_sample_fmt, out_sample_rate,
in_ch_layout, in_sample_fmt, in_sample_rate,
0, NULL);
swr_init(swrCtx);
//输出的声道个数
int nb_out_channel = av_get_channel_layout_nb_channels(out_ch_layout);
//----------重采样设置参数----------
//16bits 44100Hz PCM数据
uint8_t *out_buffer = (uint8_t *)av_malloc(MAX_AUDIO_FRME_SIZE);
//打开写入PCM文件
FILE *fpPCM = fopen(output, "wb");
//不断读取压缩数据
int ret, got_frame = 0, frame_count = 0;
while(av_read_frame(pFormatCtx, packet) >= 0)
{
if(packet->stream_index == audio_stream_index)
{
ret = avcodec_decode_audio4(codecCtx, frame, &got_frame, packet);
if(ret < 0)
{
LOGI("%s","解码完成");
}
if(got_frame)
{
LOGI("解码第%d帧", frame_count++);
swr_convert(swrCtx, &out_buffer, MAX_AUDIO_FRME_SIZE, (const uint8_t **)frame->data, frame->nb_samples);
//获取sample的大小
int out_buffer_size = av_samples_get_buffer_size(NULL, nb_out_channel,
frame->nb_samples ,out_sample_fmt, 1);
fwrite(out_buffer, 1, out_buffer_size, fpPCM);
}
}
av_free_packet(packet);
}
fclose(fpPCM);
av_frame_free(&frame);
av_free(out_buffer);
swr_free(&swrCtx);
avcodec_close(codecCtx);
avformat_close_input(&pFormatCtx);
(*env)->ReleaseStringUTFChars(env, jstr_input, input);
(*env)->ReleaseStringUTFChars(env, jstr_output, output);
}

经Adobe Audition软件测试,解码后的文件可以正常播放

本文到此结束,下篇文章将继续介绍音频播放