FFmpeg读取音频

展开阅读全文

ffmpeg音频重采样、编码

01-21

我看网上的例子都是这样的(如下)。我有个问题:如果输入的nb_samples = 1151,输出的codec_ctx->frame_size = 2048;这样的话fifo需要写入两次后才能读出一次。如果输入的格式是平面格式,那么fifo中的数据格式就是(假设声道1的数据是1,声道2的数据是2):11111...(1151个1)22222...(1151个2)1111...(1151个1)2222...(1151个2);然后读出的数据格式就应该是,声道1:111111...2222..(1151个1,897个2);声道2:222...1111..22.(254个2,1151个1,x个2)。rn这样的话读出的数据肯定是不对的啊!! 那应该怎么处理呢?rn[code=c]rn//Resamping of the audiornrn m_pReSmpCtx = av_audio_resample_init(m_pAudioSt->codec->channels,m_audioFmt.in_channels,\rn m_pAudioSt->codec->sample_rate,m_audioFmt.in_sample_rate,\rn SAMPLE_FMT_S16,SAMPLE_FMT_S16,16,10,0,1.0);rnrn int32_t samples = delen / ((m_pAudioSt->codec->channels) * 2);rn int32_t resamplenum = 0;rn int32_t resamplesize = 0;rn int16_t *resamplebuff = new int16_t[MAX_AUDIO_FRAME_SIZE];rn resamplenum = audio_resample(m_pReSmpCtx,resamplebuff,(int16_t*)debuff,samples);rn resamplesize = resamplenum * 2 * (m_pAudioSt->codec->channels);rnrn if(CODEC_TYPE_IPMR == pkt.codecType)rn delete []debuff;rn rnrn av_fifo_realloc2(m_pFifo,av_fifo_size(m_pFifo)+resamplesize);rn av_fifo_generic_write(m_pFifo,resamplebuff,resamplesize,NULL);rn// av_fifo_write(m_pFifo,(uint8_t*)resamplebuff,resamplesize);rnrn delete []resamplebuff;rnrnrn while(av_fifo_size(m_pFifo) >= m_audioSmpSize)rn uint8_t *readbuff = new uint8_t[m_audioSmpSize];rn uint8_t *enbuff = new uint8_t[MAX_AUDIO_FRAME_SIZE];rn int32_t ensize = MAX_AUDIO_FRAME_SIZE;rn av_fifo_generic_read(m_pFifo,readbuff,m_audioSmpSize,NULL);rn AVPacket packet;rn av_init_packet(&packet);rn packet.size = avcodec_encode_audio(m_pAudioSt->codec,enbuff,ensize,(short *)readbuff);rn packet.flags |= AV_PKT_FLAG_KEY;rn packet.data = enbuff;rn packet.stream_index = m_pAudioSt->index;rn if (packet.size > 0 )rn if (av_write_frame(m_pFmtCtx,&packet) != 0)rn ret = RECORD_AUDIO_PLUGIN | WRITE_FRAME_ERR;rn delete []readbuff;rn delete []enbuff;rn return ret;rn rn rn delete []readbuff;rn delete []enbuff;rn av_free_packet(&packet);rnrn rn[/code] 论坛

没有更多推荐了,返回首页