Skip to content

Commit

Permalink
更新代码细节20210526
Browse files Browse the repository at this point in the history
  • Loading branch information
JackeyLea committed May 26, 2021
1 parent c74c3d4 commit 379c3e8
Show file tree
Hide file tree
Showing 4 changed files with 308 additions and 317 deletions.
3 changes: 2 additions & 1 deletion 15.audio_decode_mp32pcm/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

int main()
{
const char inFileName[] = "test.mp3";
const char inFileName[] = "/home/jackey/Music/test.mp3";
const char outFileName[] = "test.pcm";
FILE *file=fopen(outFileName,"w+b");
if(!file){
Expand Down Expand Up @@ -61,6 +61,7 @@ int main()
printf("Cannot alloc codec context.\n");
return -1;
}
codecCtx->pkt_timebase = fmtCtx->streams[aStreamIndex]->time_base;

if(avcodec_open2(codecCtx,codec,NULL)<0){
printf("Cannot open audio codec.\n");
Expand Down
191 changes: 93 additions & 98 deletions 16.audio_decode_swr_mp32pcm/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,119 +23,114 @@ int main()

AVFormatContext *fmtCtx =avformat_alloc_context();
AVCodecContext *codecCtx = NULL;
AVPacket *pkt=av_packet_alloc();
AVFrame *frame = av_frame_alloc();

int aStreamIndex = -1;

if(avformat_open_input(&fmtCtx,inFileName,NULL,NULL)<0){
printf("Cannot open input file.\n");
return -1;
}
if(avformat_find_stream_info(fmtCtx,NULL)<0){
printf("Cannot find any stream in file.\n");
return -1;
}
do{
if(avformat_open_input(&fmtCtx,inFileName,NULL,NULL)<0){
printf("Cannot open input file.\n");
break;
}
if(avformat_find_stream_info(fmtCtx,NULL)<0){
printf("Cannot find any stream in file.\n");
break;
}

av_dump_format(fmtCtx,0,inFileName,0);
av_dump_format(fmtCtx,0,inFileName,0);

for(size_t i=0;i<fmtCtx->nb_streams;i++){
if(fmtCtx->streams[i]->codecpar->codec_type==AVMEDIA_TYPE_AUDIO){
aStreamIndex=(int)i;
for(size_t i=0;i<fmtCtx->nb_streams;i++){
if(fmtCtx->streams[i]->codecpar->codec_type==AVMEDIA_TYPE_AUDIO){
aStreamIndex=(int)i;
break;
}
}
if(aStreamIndex==-1){
printf("Cannot find audio stream.\n");
break;
}
}
if(aStreamIndex==-1){
printf("Cannot find audio stream.\n");
return -1;
}

AVCodecParameters *aCodecPara = fmtCtx->streams[aStreamIndex]->codecpar;
AVCodec *codec = avcodec_find_decoder(aCodecPara->codec_id);
if(!codec){
printf("Cannot find any codec for audio.\n");
return -1;
}
codecCtx = avcodec_alloc_context3(codec);
if(avcodec_parameters_to_context(codecCtx,aCodecPara)<0){
printf("Cannot alloc codec context.\n");
return -1;
}

if(avcodec_open2(codecCtx,codec,NULL)<0){
printf("Cannot open audio codec.\n");
return -1;
}
AVCodecParameters *aCodecPara = fmtCtx->streams[aStreamIndex]->codecpar;
AVCodec *codec = avcodec_find_decoder(aCodecPara->codec_id);
if(!codec){
printf("Cannot find any codec for audio.\n");
break;
}
codecCtx = avcodec_alloc_context3(codec);
if(avcodec_parameters_to_context(codecCtx,aCodecPara)<0){
printf("Cannot alloc codec context.\n");
break;
}
codecCtx->pkt_timebase=fmtCtx->streams[aStreamIndex]->time_base;

AVPacket *pkt=av_packet_alloc();
AVFrame *frame = av_frame_alloc();
if(avcodec_open2(codecCtx,codec,NULL)<0){
printf("Cannot open audio codec.\n");
break;
}

//设置转码参数
uint64_t out_channel_layout = codecCtx->channel_layout;
enum AVSampleFormat out_sample_fmt = AV_SAMPLE_FMT_S16;
int out_sample_rate = codecCtx->sample_rate;
int out_channels = av_get_channel_layout_nb_channels(out_channel_layout);
printf("out rate : %d , out_channel is: %d\n",out_sample_rate,out_channels);

//uint8_t *audio_out_buffer[2];
//audio_out_buffer[0]= (uint8_t*)av_malloc(MAX_AUDIO_FRAME_SIZE*2);
//audio_out_buffer[1] =(uint8_t*)av_malloc(MAX_AUDIO_FRAME_SIZE*2);
uint8_t *audio_out_buffer = (uint8_t*)av_malloc(MAX_AUDIO_FRAME_SIZE*2);

SwrContext *swr_ctx = swr_alloc_set_opts(NULL,
out_channel_layout,
out_sample_fmt,
out_sample_rate,
codecCtx->channel_layout,
codecCtx->sample_fmt,
codecCtx->sample_rate,
0,NULL);
swr_init(swr_ctx);


while(av_read_frame(fmtCtx,pkt)>=0){
if(pkt->stream_index==aStreamIndex){
if(avcodec_send_packet(codecCtx,pkt)>=0){
while(avcodec_receive_frame(codecCtx,frame)>=0){
/*
Planar(平面),其数据格式排列方式为 (特别记住,该处是以点nb_samples采样点来交错,不是以字节交错):
LLLLLLRRRRRRLLLLLLRRRRRRLLLLLLRRRRRRL...(每个LLLLLLRRRRRR为一个音频帧)
而不带P的数据格式(即交错排列)排列方式为:
LRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRL...(每个LR为一个音频样本)
*/
if(av_sample_fmt_is_planar(codecCtx->sample_fmt)){
int len = swr_convert(swr_ctx,
&audio_out_buffer,
MAX_AUDIO_FRAME_SIZE*2,
(const uint8_t**)frame->data,
frame->nb_samples);
if(len<=0){
continue;
//设置转码参数
uint64_t out_channel_layout = codecCtx->channel_layout;
enum AVSampleFormat out_sample_fmt = AV_SAMPLE_FMT_S16;
int out_sample_rate = codecCtx->sample_rate;
int out_channels = av_get_channel_layout_nb_channels(out_channel_layout);

uint8_t *audio_out_buffer = (uint8_t*)av_malloc(MAX_AUDIO_FRAME_SIZE*2);

SwrContext *swr_ctx = swr_alloc_set_opts(NULL,
out_channel_layout,
out_sample_fmt,
out_sample_rate,
codecCtx->channel_layout,
codecCtx->sample_fmt,
codecCtx->sample_rate,
0,NULL);
swr_init(swr_ctx);

while(av_read_frame(fmtCtx,pkt)>=0){
if(pkt->stream_index==aStreamIndex){
if(avcodec_send_packet(codecCtx,pkt)>=0){
while(avcodec_receive_frame(codecCtx,frame)>=0){
/*
Planar(平面),其数据格式排列方式为 (特别记住,该处是以点nb_samples采样点来交错,不是以字节交错):
LLLLLLRRRRRRLLLLLLRRRRRRLLLLLLRRRRRRL...(每个LLLLLLRRRRRR为一个音频帧)
而不带P的数据格式(即交错排列)排列方式为:
LRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRL...(每个LR为一个音频样本)
*/
if(av_sample_fmt_is_planar(codecCtx->sample_fmt)){
int len = swr_convert(swr_ctx,
&audio_out_buffer,
MAX_AUDIO_FRAME_SIZE*2,
(const uint8_t**)frame->data,
frame->nb_samples);
if(len<=0){
continue;
}

int dst_bufsize = av_samples_get_buffer_size(0,
out_channels,
len,
out_sample_fmt,
1);

//int numBytes =av_get_bytes_per_sample(out_sample_fmt);
//printf("number bytes is: %d.\n",numBytes);

fwrite(audio_out_buffer,1,dst_bufsize,file);

//pcm播放时是LRLRLR格式,所以要交错保存数据
// for(int i=0;i<frame->nb_samples;i++){
// for(int ch=0;ch<2;ch++){
// fwrite((char*)audio_out_buffer[ch]+numBytes*i,1,numBytes,file);
// }
// }
}
printf("convert length is: %d.\n",len);

int dst_bufsize = av_samples_get_buffer_size(0,
out_channels,
len,
out_sample_fmt,
1);
printf("buffer size is: %d.\n",dst_bufsize);

//int numBytes =av_get_bytes_per_sample(out_sample_fmt);
//printf("number bytes is: %d.\n",numBytes);

fwrite(audio_out_buffer,1,dst_bufsize,file);

//pcm播放时是LRLRLR格式,所以要交错保存数据
// for(int i=0;i<frame->nb_samples;i++){
// for(int ch=0;ch<2;ch++){
// fwrite((char*)audio_out_buffer[ch]+numBytes*i,1,numBytes,file);
// }
// }
}
}
}
av_packet_unref(pkt);
}
av_packet_unref(pkt);
}
}while(0);

av_frame_free(&frame);
av_packet_free(&pkt);
Expand Down
Loading

0 comments on commit 379c3e8

Please sign in to comment.