⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 audio_ffmpeg.cpp

📁 完整的RTP RTSP代码库
💻 CPP
📖 第 1 页 / 共 2 页
字号:
  for (int i = 0; i < queue_cnt; i++) {    // extract mode field + quality bit & set the follow bit    //    if (i > 0) payloadHeader[i] |= 0x80;#if 0    payloadHeader[i + 1] = (*(uint8_t*)m_audioQueue[i]->GetData() & 0x7C) | 0x80;#else    payloadHeader[i + 1] = (*(uint8_t*)m_audioQueue[i]->GetData() & 0x78) | 0x84;#endif    // body of the frame    iov[i + 1].iov_base = (uint8_t*)m_audioQueue[i]->GetData() + 1;    iov[i + 1].iov_len  = m_audioQueue[i]->GetDataLength() - 1;    //    printf("data len is %d\n", iov[i + 1].iov_len);  }  // clear the follow bit in the last TOC entry  payloadHeader[queue_cnt] &= 0x7F;  iov[0].iov_base = payloadHeader;  iov[0].iov_len  = queue_cnt + 1;  //  printf("hdr  len is %d\n\n", queue_cnt + 1);  return true;}bool ffmpeg_get_audio_rtp_info (CAudioProfile *pConfig,			      MediaType *audioFrameType,			      uint32_t *audioTimeScale,			      uint8_t *audioPayloadNumber,			      uint8_t *audioPayloadBytesPerPacket,			      uint8_t *audioPayloadBytesPerFrame,			      uint8_t *audioQueueMaxCount,				audio_set_rtp_payload_f *audio_set_rtp_payload,			      audio_set_rtp_header_f *audio_set_header,			      audio_set_rtp_jumbo_frame_f *audio_set_jumbo,			      void **ud){  const char *encodingName = pConfig->GetStringValue(CFG_AUDIO_ENCODING);  if (!strcasecmp(encodingName, AUDIO_ENCODING_MP3)) {    *audioFrameType = MP3AUDIOFRAME;    if (pConfig->GetBoolValue(CFG_RTP_USE_MP3_PAYLOAD_14)) {      *audioPayloadNumber = 14;      *audioTimeScale = 90000;    } else {      *audioPayloadNumber = 97;      *audioTimeScale = pConfig->GetIntegerValue(CFG_AUDIO_SAMPLE_RATE);    }    *audioPayloadBytesPerPacket = 4;    *audioPayloadBytesPerFrame = 0;    *audioQueueMaxCount = 8;    *audio_set_header = ffmpeg_set_rtp_header;    *audio_set_jumbo = ffmpeg_set_rtp_jumbo;    *ud = malloc(4);    memset(*ud, 0, 4);    return true;  }  if (!strcasecmp(encodingName, AUDIO_ENCODING_AMR)) {    *audioTimeScale = pConfig->GetIntegerValue(CFG_AUDIO_SAMPLE_RATE);    if (*audioTimeScale == 8000) {      *audioFrameType = AMRNBAUDIOFRAME;    } else {      *audioFrameType = AMRWBAUDIOFRAME;    }    *audioPayloadNumber = 97;    *audioPayloadBytesPerPacket = 4;    *audioPayloadBytesPerFrame = 0;    *audioQueueMaxCount = 5;    *audio_set_rtp_payload = ffmpeg_amr_set_rtp_payload;    *ud = malloc(10);    memset(*ud, 0, 10);    return true;  }  if (strcasecmp(encodingName, AUDIO_ENCODING_ALAW) == 0 ||      strcasecmp(encodingName, AUDIO_ENCODING_ULAW) == 0) {    if (strcasecmp(encodingName, AUDIO_ENCODING_ALAW) == 0) {      *audioFrameType = ALAWAUDIOFRAME;      *audioPayloadNumber = 8;    } else {      *audioFrameType = ULAWAUDIOFRAME;      *audioPayloadNumber = 0;    }    *audioTimeScale = 8000;    *audioPayloadBytesPerPacket = 0;    *audioPayloadBytesPerFrame = 0;    *audioQueueMaxCount = 1;    *audio_set_header = NULL;    *audio_set_jumbo = NULL;    *ud = NULL;    return true;  }  return false;	    }CFfmpegAudioEncoder::CFfmpegAudioEncoder(CAudioProfile *ap,					 CAudioEncoder *next, 					 u_int8_t srcChannels,					 u_int32_t srcSampleRate,					 uint16_t mtu,					 bool realTime) :  CAudioEncoder(ap, next, srcChannels, srcSampleRate, mtu, realTime){	m_FrameBuffer = NULL;	m_codec = NULL;	m_avctx = NULL;}bool CFfmpegAudioEncoder::Init (void){  const char *encoding = Profile()->GetStringValue(CFG_AUDIO_ENCODING);  avcodec_init();  avcodec_register_all();  if (strcasecmp(encoding,AUDIO_ENCODING_MP3) == 0) {    m_codec = avcodec_find_encoder(CODEC_ID_MP2);    m_media_frame = MP3AUDIOFRAME;  } else if (strcasecmp(encoding, AUDIO_ENCODING_ALAW) == 0) {    m_codec = avcodec_find_encoder(CODEC_ID_PCM_ALAW);    m_media_frame = ALAWAUDIOFRAME;  } else if (strcasecmp(encoding, AUDIO_ENCODING_ULAW) == 0) {    m_codec = avcodec_find_encoder(CODEC_ID_PCM_MULAW);    m_media_frame = ULAWAUDIOFRAME;  } else if (strcasecmp(encoding, AUDIO_ENCODING_AMR) == 0) {#ifdef MAY_HAVE_AMR_CODEC    uint32_t samplingRate = Profile()->GetIntegerValue(CFG_AUDIO_SAMPLE_RATE);    if (samplingRate == 8000) {      m_codec = avcodec_find_encoder(CODEC_ID_AMR_NB);      m_media_frame= AMRNBAUDIOFRAME;    } else {      m_codec = avcodec_find_encoder(CODEC_ID_AMR_WB);      m_media_frame = AMRWBAUDIOFRAME;    }#endif  }      if (m_codec == NULL) {    error_message("Couldn't find audio codec");    return false;  }  m_avctx = avcodec_alloc_context();  m_frame = avcodec_alloc_frame();  m_avctx->codec_type = CODEC_TYPE_AUDIO;  switch (m_media_frame) {  case MP3AUDIOFRAME:    m_avctx->codec_id = CODEC_ID_MP2;    m_samplesPerFrame =       MP4AV_Mp3GetSamplingWindow(Profile()->GetIntegerValue(CFG_AUDIO_SAMPLE_RATE));    m_FrameMaxSize = (u_int)(1.25 * m_samplesPerFrame) + 7200;    break;  case ALAWAUDIOFRAME:    m_avctx->codec_id = CODEC_ID_PCM_ALAW;    m_samplesPerFrame = 160;    m_FrameMaxSize = m_samplesPerFrame / 2;    break;  case ULAWAUDIOFRAME:    m_avctx->codec_id = CODEC_ID_PCM_MULAW;    m_samplesPerFrame = 160;    m_FrameMaxSize = m_samplesPerFrame / 2;    break;  case AMRNBAUDIOFRAME:    m_avctx->codec_id = CODEC_ID_AMR_NB;    m_samplesPerFrame =       MP4AV_AmrGetSamplingWindow(8000);    m_FrameMaxSize = 64;    break;  case AMRWBAUDIOFRAME:    m_avctx->codec_id = CODEC_ID_AMR_WB;    m_samplesPerFrame =       MP4AV_AmrGetSamplingWindow(16000);    m_FrameMaxSize = 64;    break;  }  m_avctx->bit_rate = Profile()->GetIntegerValue(CFG_AUDIO_BIT_RATE);  m_avctx->sample_rate = Profile()->GetIntegerValue(CFG_AUDIO_SAMPLE_RATE);  m_avctx->channels = Profile()->GetIntegerValue(CFG_AUDIO_CHANNELS);  ffmpeg_interface_lock();  if (avcodec_open(m_avctx, m_codec) < 0) {    ffmpeg_interface_unlock();    error_message("Couldn't open ffmpeg codec");    return false;  }  ffmpeg_interface_unlock();  m_FrameBufferSize = 2 * m_FrameMaxSize;  m_FrameBufferLength = 0;  m_FrameBuffer = (u_int8_t*)malloc(m_FrameBufferSize);  if (!m_FrameBuffer) {    return false;  }  Initialize();  return true;}u_int32_t CFfmpegAudioEncoder::GetSamplesPerFrame(){	return m_samplesPerFrame;}bool CFfmpegAudioEncoder::EncodeSamples(	int16_t* pSamples, 	u_int32_t numSamplesPerChannel,	u_int8_t numChannels){	if (numChannels != 1 && numChannels != 2) {		return false;	// invalid numChannels	}	u_int32_t DataLength = 0;	if (pSamples != NULL) { 	  DataLength = 	    avcodec_encode_audio(m_avctx,				 m_FrameBuffer,				 m_FrameBufferSize,				 pSamples);	  //debug_message("ffmpeg encode %u", DataLength);	} else {	  return false;	}	m_FrameBufferLength += DataLength;	//	error_message("audio -return from ffmpeg_encode_buffer is %d %d", mp3DataLength, m_mp3FrameBufferLength);	return (DataLength >= 0);}bool CFfmpegAudioEncoder::GetEncodedFrame(	u_int8_t** ppBuffer, 	u_int32_t* pBufferLength,	u_int32_t* pNumSamplesPerChannel){	const u_int8_t* frame;	u_int32_t frameLength;	if (m_FrameBufferLength == 0) return false;	// I'm not sure we actually need all this code; however, 	// it doesn't hurt.  It's copied from the lame interface	if (m_media_frame == MP3AUDIOFRAME) {	  if (!MP4AV_Mp3GetNextFrame(m_FrameBuffer, m_FrameBufferLength, 				     &frame, &frameLength)) {	    //debug_message("Can't find frame header - len %d", m_mp3FrameBufferLength);	    return false;	  }	  	  // check if we have all the bytes for the MP3 frame	} else if ((m_media_frame == AMRNBAUDIOFRAME) ||		   (m_media_frame == AMRWBAUDIOFRAME)) {	  	  frame = m_FrameBuffer;	  if (!MP4AV_AmrGetNextFrame(m_FrameBuffer, m_FrameBufferLength,				     &frameLength,				     m_media_frame == AMRNBAUDIOFRAME)) {	    return false;	  }	} else {	  frameLength = m_FrameBufferLength;	  frame = m_FrameBuffer;	}#if 0	error_message("buffer %d size %d", 		      m_FrameBufferLength,frameLength);#endif		if (frameLength > m_FrameBufferLength) {	  //debug_message("Not enough in buffer - %d %d", m_mp3FrameBufferLength, mp3FrameLength);	  return false;	}	// need a buffer for this MP3 frame	*ppBuffer = (u_int8_t*)malloc(frameLength);	if (*ppBuffer == NULL) {	  error_message("Cannot alloc memory");		return false;	}	// copy the MP3 frame	memcpy(*ppBuffer, frame, frameLength);	*pBufferLength = frameLength;	// shift what remains in the buffer down	memcpy(m_FrameBuffer, 		frame + frameLength, 		m_FrameBufferLength - frameLength);	//	error_message("vers %d layer %d", MP4AV_Mp3GetHdrVersion(MP4AV_Mp3HeaderFromBytes(*ppBuffer)),	//      MP4AV_Mp3GetHdrLayer(MP4AV_Mp3HeaderFromBytes(*ppBuffer)));	m_FrameBufferLength -= frameLength;	*pNumSamplesPerChannel = m_samplesPerFrame;	return true;}void CFfmpegAudioEncoder::StopEncoder(void){  ffmpeg_interface_lock();  avcodec_close(m_avctx);  ffmpeg_interface_unlock();  m_avctx = NULL;  free(m_FrameBuffer);  m_FrameBuffer = NULL;}void InitFFmpegAudio (void){  AddAudioEncoderTable(&ffmpeg_audio_encoder_table);  AddAudioEncoderTable(&ffmpeg_alaw_audio_encoder_table);  AddAudioEncoderTable(&ffmpeg_ulaw_audio_encoder_table);#ifdef MAY_HAVE_AMR_CODEC  avcodec_init();  avcodec_register_all();  bool have_amr_nb = avcodec_find_encoder(CODEC_ID_AMR_NB) != NULL;  bool have_amr_wb = avcodec_find_encoder(CODEC_ID_AMR_WB) != NULL;  if (have_amr_nb == false && have_amr_wb == false) return;  if (have_amr_nb && have_amr_wb == false) {    ffmpeg_amr_audio_encoder_table.num_sample_rates = 1;  } else if (have_amr_nb == false && have_amr_wb) {    ffmpeg_amr_audio_encoder_table.num_sample_rates = 1;    ffmpeg_amr_audio_encoder_table.sample_rates =       &ffmpeg_amr_sample_rates[1];  }  AddAudioEncoderTable(&ffmpeg_amr_audio_encoder_table);#endif}    #endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -