示例#1
0
void Parser::setStream()
{
    int error = 0;

    AVCodec *codec = NULL;
    AVCodecID codecID = getCodecID();

    codec = avcodec_find_encoder(codecID);
    fmt_ctx_out->audio_codec = codec;
    stm_out = avformat_new_stream(fmt_ctx_out, codec);

    if (!stm_out)
        throw StreamException() << errno_code(MIR_ERR_OPEN_STREAM_1);

    cdc_ctx_out = stm_out->codec;
    cdc_ctx_out->codec = codec;
    cdc_ctx_out->codec_id = codecID;
    cdc_ctx_out->codec_type = AVMEDIA_TYPE_AUDIO;

    cdc_ctx_out->sample_fmt = getSampleFormat(codecID);

    if (!isVBR)
        cdc_ctx_out->bit_rate = bitRate;
    else
    {
        cdc_ctx_out->rc_max_rate = 0;
        cdc_ctx_out->rc_min_rate = 0;
        cdc_ctx_out->bit_rate_tolerance = bitRate;
        cdc_ctx_out->bit_rate = bitRate;
    }

    cdc_ctx_out->sample_rate = sampleRate;
    cdc_ctx_out->channels = nbChannel;
    cdc_ctx_out->channel_layout = av_get_default_channel_layout(nbChannel);

    error = avcodec_open2(cdc_ctx_out, codec, NULL);

    if (error < 0)
        throw StreamException() << errno_code(MIR_ERR_OPEN_STREAM_2);

    if (codecID == AV_CODEC_ID_PCM_S16LE || codecID == AV_CODEC_ID_MP3)
        cdc_ctx_out->frame_size = av_rescale_rnd(nbSamplesIn,
                                                 cdc_ctx_out->sample_rate, sampleRateIn, AV_ROUND_UP);
    else if (codecID == AV_CODEC_ID_AAC)
    {
        cdc_ctx_out->profile = FF_PROFILE_AAC_LOW;
        cdc_ctx_out->frame_size = 1024;

        // some formats want stream headers to be separate
        if(fmt_ctx_out->oformat->flags & AVFMT_GLOBALHEADER)
            cdc_ctx_out->flags |= CODEC_FLAG_GLOBAL_HEADER;
    }

    cdc_out = codec;

    if (audioFormat == AUDIOFORMAT::arq)
    {
        fmt_ctx_out->oformat->flags |= AVFMT_ALLOW_FLUSH;
    }
}
示例#2
0
XBool XAudioStream::load(const char * filename)
{
	if(m_isLoaded ||
		filename == NULL) return XFalse;
	av_register_all();	
	m_pFormatCtx = NULL;
	if(avformat_open_input(&m_pFormatCtx,filename,NULL,NULL) != 0)
	{
		LogStr("File open error!");
		return XFalse;
	}
	if(avformat_find_stream_info(m_pFormatCtx,NULL) < 0)		//检查视频流信息
	{
		LogStr("can not find stream information!");
		return XFalse;
	}
	m_pAudioCodecCtx = m_pFormatCtx->streams[0]->codec;
	AVCodec *aCodec = avcodec_find_decoder(m_pAudioCodecCtx->codec_id);
	if(aCodec == NULL)
	{//找不到音频解码器
		LogStr("can not find audio decoder information!");
		return XFalse;
	} 
	if(avcodec_open2(m_pAudioCodecCtx,aCodec,NULL) < 0)
	{//找不到音频解码包
		LogStr("can not open audio decoder!");
		return XFalse;
	}
	m_frameDataSum = XEG.getAudioChannelSum() * av_get_bytes_per_sample(getSampleFormat());
	XMem::XDELETE_ARRAY(m_audioBuf);
	m_audioBuf = XMem::createArrayMem<uint8_t>((AVCODEC_MAX_AUDIO_FRAME_SIZE * 3) / 2);
	if(m_audioBuf == NULL) return XFalse;
	//m_pFrame = av_frame_alloc();
	//if(m_pFrame == NULL)
	//{
	//	printf("malloc Frame failed!\n");
	//	return XFalse;
	//}
	m_allFrameSum = m_pFormatCtx->streams[0]->duration;
	av_init_packet(&m_dataPacket);

	m_pSwrContext = swr_alloc();
	if(m_pSwrContext == NULL) return XFalse;
	if(m_pAudioCodecCtx->channel_layout == 0)
	{
		swr_alloc_set_opts(m_pSwrContext,av_get_default_channel_layout(XEG.getAudioChannelSum()),getSampleFormat(),XEG.getAudioSampleRate() * m_speed,
			av_get_default_channel_layout(m_pAudioCodecCtx->channels),m_pAudioCodecCtx->sample_fmt,m_pAudioCodecCtx->sample_rate,0,NULL);
	}else
	{
		swr_alloc_set_opts(m_pSwrContext,av_get_default_channel_layout(XEG.getAudioChannelSum()),getSampleFormat(),XEG.getAudioSampleRate() * m_speed,
			m_pAudioCodecCtx->channel_layout,m_pAudioCodecCtx->sample_fmt,m_pAudioCodecCtx->sample_rate,0,NULL);
	}
	if(swr_init(m_pSwrContext) < 0)
	{
		LogStr("swr_init() fail");
		return XFalse;
	}

	m_isLoaded = XTrue;
	return XTrue;
}
示例#3
0
void XAudioStream::setSpeed(float speed)
{
	if(m_speed == speed || speed <= 0.0f) return;
	m_speed = speed;
	swr_free(&m_pSwrContext);
	m_pSwrContext = swr_alloc();
	if(m_pSwrContext == NULL) return;
	if(m_pAudioCodecCtx->channel_layout == 0)
	{
		swr_alloc_set_opts(m_pSwrContext,av_get_default_channel_layout(XEG.getAudioChannelSum()),getSampleFormat(),XEG.getAudioSampleRate() * m_speed,
			av_get_default_channel_layout(m_pAudioCodecCtx->channels),m_pAudioCodecCtx->sample_fmt,m_pAudioCodecCtx->sample_rate,0,NULL);
	}else
	{
		swr_alloc_set_opts(m_pSwrContext,av_get_default_channel_layout(XEG.getAudioChannelSum()),getSampleFormat(),XEG.getAudioSampleRate() * m_speed,
			m_pAudioCodecCtx->channel_layout,m_pAudioCodecCtx->sample_fmt,m_pAudioCodecCtx->sample_rate,0,NULL);
	}
	if(swr_init(m_pSwrContext) < 0)
	{
		LogStr("swr_init() fail");
		return;
	}
}