qq_44906983 2019-10-11 22:41 采纳率: 0%
浏览 331

解码出的pcm有问题,我要怎么修改增加代码?

#include
#include
#include

#define __STDC_CONSTANT_MACROS

#ifdef _WIN32
//Windows
extern "C"
{
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libswresample/swresample.h"
};
#else
//Linux...
#ifdef __cplusplus
extern "C"
{
#endif
#include
#include
#include
#ifdef __cplusplus
};
#endif
#endif

#define MAX_AUDIO_FRAME_SIZE 192000 // 1 second of 48khz 32bit audio

int main(int argc, char* argv[])
{
AVFormatContext *pFormatCtx;
int i, audioStream;
AVCodecContext *pCodecCtx;
AVCodec *pCodec;
AVPacket *packet;
uint8_t *out_buffer;
AVFrame *pFrame;
int ret;
uint32_t len = 0;
int got_picture;
int index = 0;
int64_t in_channel_layout;
struct SwrContext *au_convert_ctx;

FILE *pFile=fopen("output.pcm", "wb");
char url[]="output.aac";

av_register_all();
avformat_network_init();
pFormatCtx = avformat_alloc_context();
//Open
if(avformat_open_input(&pFormatCtx,url,NULL,NULL)!=0){
    printf("Couldn't open input stream.\n");
    return -1;
}
// Retrieve stream information
if(avformat_find_stream_info(pFormatCtx,NULL)<0){
    printf("Couldn't find stream information.\n");
    return -1;
}
// Dump valid information onto standard error
av_dump_format(pFormatCtx, 0, url, false);

// Find the first audio stream
audioStream=-1;
for(i=0; i < pFormatCtx->nb_streams; i++)
    if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_AUDIO){
        audioStream=i;
        break;
    }

if(audioStream==-1){
    printf("Didn't find a audio stream.\n");
    return -1;
}

// Get a pointer to the codec context for the audio stream
pCodecCtx=pFormatCtx->streams[audioStream]->codec;

// Find the decoder for the audio stream
pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
if(pCodec==NULL){
    printf("Codec not found.\n");
    return -1;
}

// Open codec
if(avcodec_open2(pCodecCtx, pCodec,NULL)<0){
    printf("Could not open codec.\n");
    return -1;
}

packet=(AVPacket *)av_malloc(sizeof(AVPacket));
av_init_packet(packet);

//Out Audio Param
uint64_t out_channel_layout=AV_CH_LAYOUT_STEREO;
//nb_samples: AAC-1024 MP3-1152
int out_nb_samples=pCodecCtx->frame_size;
AVSampleFormat out_sample_fmt=AV_SAMPLE_FMT_S16;
int out_sample_rate=44100;
int out_channels=av_get_channel_layout_nb_channels(out_channel_layout);
//Out Buffer Size
int out_buffer_size=av_samples_get_buffer_size(NULL,out_channels ,out_nb_samples,out_sample_fmt, 1);

out_buffer=(uint8_t *)av_malloc(MAX_AUDIO_FRAME_SIZE*2);
pFrame=av_frame_alloc();

//FIX:Some Codec's Context Information is missing
in_channel_layout=av_get_default_channel_layout(pCodecCtx->channels);
//Swr
au_convert_ctx = swr_alloc();
au_convert_ctx=swr_alloc_set_opts(au_convert_ctx,out_channel_layout, out_sample_fmt, out_sample_rate,
    in_channel_layout,pCodecCtx->sample_fmt , pCodecCtx->sample_rate,0, NULL);
swr_init(au_convert_ctx);

while(av_read_frame(pFormatCtx, packet)>=0){
    if(packet->stream_index==audioStream){

        ret = avcodec_decode_audio4( pCodecCtx, pFrame,&got_picture, packet);
        if ( ret < 0 ) {
            printf("Error in decoding audio frame.\n");
            return -1;
        }
        if ( got_picture > 0 ){
            swr_convert(au_convert_ctx,&out_buffer, MAX_AUDIO_FRAME_SIZE,(const uint8_t **)pFrame->data , pFrame->nb_samples);

            printf("index:%5d\t pts:%lld\t packet size:%d\n",index,packet->pts,packet->size);
            //Write PCM
            fwrite(out_buffer, 1, out_buffer_size, pFile);
            index++;
        }
    }
    av_free_packet(packet);
}

swr_free(&au_convert_ctx);

fclose(pFile);

av_free(out_buffer);
// Close the codec
avcodec_close(pCodecCtx);
// Close the video file
avformat_close_input(&pFormatCtx);

return 0;

}

  • 写回答

1条回答 默认 最新

  • dabocaiqq 2019-10-12 09:21
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)