From cd4cd23485c84d70497510e14fdd71265b58fb2d Mon Sep 17 00:00:00 2001 From: Marten Richter Date: Sun, 16 Sep 2012 12:40:16 +0200 Subject: [PATCH] Various audio related improvements and bugfixes --- audioomx.cc | 146 ++++++++++++++++++++++++++++++++---------------- audioomx.h | 6 +- player.cc | 4 +- playerlivetv.cc | 2 +- videoomx.cc | 12 ++-- 5 files changed, 109 insertions(+), 61 deletions(-) diff --git a/audioomx.cc b/audioomx.cc index 4b11e80..f4a8aec 100644 --- a/audioomx.cc +++ b/audioomx.cc @@ -474,7 +474,7 @@ int AudioOMX::ChangeAudioDestination() //clock aka omx mutex needs to be locked } -int AudioOMX::ChangeAudioPortConfig() //clock aka omx mutex needs to be locked +int AudioOMX::ChangeAudioPortConfig(bool disport) //clock aka omx mutex needs to be locked { OMX_ERRORTYPE error; //Ok first fidle a working configuration @@ -549,7 +549,17 @@ int AudioOMX::ChangeAudioPortConfig() //clock aka omx mutex needs to be locked if (passthrough) { // TODO coding } else { + //VideoOMX* video=(VideoOMX*)Video::getInstance(); + /*if (disport) { + if (!video->ChangeComponentState(omx_aud_rend, OMX_StateIdle)) { + Log::getInstance()->log("Audio", Log::DEBUG, + "omx_aud_rend ChangeComponentState Execute CAP"); + return 0; + } + video->DisablePort(omx_aud_rend,omx_rend_input_port,false); + DestroyInputBufsOMX(); + }*/ OMX_AUDIO_PARAM_PCMMODETYPE audio_pcm; memset(&audio_pcm, 0, sizeof(audio_pcm)); audio_pcm.nSize = sizeof(audio_pcm); @@ -572,9 +582,19 @@ int AudioOMX::ChangeAudioPortConfig() //clock aka omx mutex needs to be locked Log::getInstance()->log("Audio", Log::DEBUG, "Init OMX_IndexParamAudioPcm failed %x %d", error, omx_rend_input_port); - DeAllocateCodecsOMX(); + //DeAllocateCodecsOMX(); return 0; } + /*if (disport) { + video->EnablePort(omx_aud_rend,omx_rend_input_port,false); + PrepareInputBufsOMX(); + if (!video->ChangeComponentState(omx_aud_rend, OMX_StateExecuting)) { + Log::getInstance()->log("Audio", Log::DEBUG, + "omx_aud_rend ChangeComponentState Execute CAP"); + return 0; + } + + }*/ } } @@ -806,7 +826,7 @@ int AudioOMX::AllocateCodecsOMX() - if (!ChangeAudioPortConfig()){ + if (!ChangeAudioPortConfig(false)){ Log::getInstance()->log("Audio", Log::NOTICE, "Change AudioPortConfig failed"); video->UnlockClock(); DeAllocateCodecsOMX(); @@ -1152,13 +1172,15 @@ int AudioOMX::pause() { paused = true; VideoOMX *vw = (VideoOMX*) Video::getInstance(); vw->LockClock(); - OMX_ERRORTYPE error; - error = OMX_SendCommand(omx_aud_rend, OMX_CommandFlush, - omx_rend_input_port, NULL); - if (error != OMX_ErrorNone) { - Log::getInstance()->log("Audio", Log::DEBUG, - "OMX_Flush rend in failed %x", error); + if (omx_running) { + OMX_ERRORTYPE error; + error = OMX_SendCommand(omx_aud_rend, OMX_CommandFlush, + omx_rend_input_port, NULL); + if (error != OMX_ErrorNone) { + Log::getInstance()->log("Audio", Log::DEBUG, + "OMX_Flush rend in failed %x", error); + } } vw->UnlockClock(); vw->clockPause(); @@ -1244,10 +1266,11 @@ int AudioOMX::test() } #endif -unsigned int AudioOMX::AdvanceMpAudioSync(UCHAR *data,unsigned int size) +unsigned int AudioOMX::AdvanceMpAudioSync(UCHAR *data,unsigned int size,unsigned int *framesize) { if (size<=2) return size; // silly; unsigned int test=0; + *framesize=0; while (test+1>6; + unsigned char frmsize=(code &0x3f); + if (fscod!=0) Log::getInstance()->log("Audio", Log::DEBUG, "warning we only support 48 KHz sampling rate"); + *framesize=frm_size_tab[frmsize]*2; + return test; // probably FrameSync + } test++; } return size; @@ -1364,7 +1400,7 @@ UINT AudioOMX::DeliverMediaPacket(MediaPacket packet, const UCHAR* buffer, cur_input_buf_omx = NULL; } vw->LockClock(); - if (!ChangeAudioPortConfig()) { + if (!ChangeAudioPortConfig(true)) { Log::getInstance()->log("Audio", Log::DEBUG, "Changing audio port config failed", error); } @@ -1418,7 +1454,6 @@ UINT AudioOMX::DeliverMediaPacket(MediaPacket packet, const UCHAR* buffer, input_bufs_omx_mutex.Unlock(); } - if (cur_input_buf_omx->nFilledLen == 0) {//will only be changed on first packet if (packet.synched) { //Log::getInstance()->log("Audio", Log::DEBUG, @@ -1495,59 +1530,72 @@ UINT AudioOMX::DeliverMediaPacket(MediaPacket packet, const UCHAR* buffer, //Log::getInstance()->log("Audio", Log::DEBUG, "libav in %d %d", // framesize, current_context->frame_size); + bool donotdecompress=false; + unsigned int gotframesize=0; if (!decompress_buffer_filled) { // only do this if no old data is present int adv = 0; switch (packet.type) { case MPTYPE_MPEG_AUDIO: case MPTYPE_MPEG_AUDIO_LAYER3: { adv = AdvanceMpAudioSync(incoming_paket_libav.data, - incoming_paket_libav.size); + incoming_paket_libav.size,&gotframesize); } - break; + break; case MPTYPE_AC3: case MPTYPE_AC3_PRE13: { adv = AdvanceAc3AudioSync(incoming_paket_libav.data, - incoming_paket_libav.size); + incoming_paket_libav.size,&gotframesize); } - break; + break; }; if (adv > 0) { incoming_paket_libav.data += adv; haveToCopy -= adv; *samplepos += adv; - if (current_context->frame_size>0) framesize=min(current_context->frame_size,haveToCopy); - else framesize=haveToCopy; + /*if (current_context->frame_size>0) framesize=min(current_context->frame_size,haveToCopy); + else*/ framesize=haveToCopy; //Log::getInstance()->log("Audio", Log::DEBUG,"Advance by %d %d from %d",adv,packet.type,*samplepos ); if (haveToCopy <= 0) { - // Log::getInstance()->log("Audio", Log::DEBUG,"No sync code in packet remove %d",packet.type); - *samplepos=packet.length; - return packet.length; + // Log::getInstance()->log("Audio", Log::DEBUG,"No sync code in packet remove %d",packet.type); + *samplepos=packet.length; + return packet.length; } - break; + + } // - // else Log::getInstance()->log("Audio", Log::DEBUG,"Loop run" ); + + if (gotframesize>0 && gotframesize>haveToCopy) { + donotdecompress=true; + errcount=100; // exit loop + } + // else Log::getInstance()->log("Audio", Log::DEBUG,"Loop run" ); } - int oldcancelstate; - int oldcanceltype; - pthread_testcancel(); - pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldcancelstate); - pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldcanceltype); - libav_mutex.Lock(); - if (!omx_running || !mp23codec_context_libav - || !ac3codec_context_libav) { + if (!donotdecompress) { + int oldcancelstate; + int oldcanceltype; + pthread_testcancel(); + pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldcancelstate); + pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldcanceltype); + libav_mutex.Lock(); + if (!omx_running || !mp23codec_context_libav + || !ac3codec_context_libav) { + libav_mutex.Unlock(); + return *samplepos; + } libav_mutex.Unlock(); - return *samplepos; - } - libav_mutex.Unlock(); - // Log::getInstance()->log("Audio", Log::DEBUG,"libav out"); - len = avcodec_decode_audio4(current_context, decode_frame_libav, - &gotta, &incoming_paket_libav); - //Log::getInstance()->log("Audio", Log::DEBUG, "libav out1"); - pthread_setcancelstate(oldcancelstate, NULL); - pthread_setcanceltype(oldcanceltype, NULL); - pthread_testcancel(); + // Log::getInstance()->log("Audio", Log::DEBUG,"libav out"); + len = avcodec_decode_audio4(current_context, decode_frame_libav, + &gotta, &incoming_paket_libav); + //Log::getInstance()->log("Audio", Log::DEBUG, "libav out1"); + pthread_setcancelstate(oldcancelstate, NULL); + pthread_setcanceltype(oldcanceltype, NULL); + pthread_testcancel(); + } else { + gotta=0; + len=0; + } //Log::getInstance()->log("Audio", Log::DEBUG, "libav out2"); if (!omx_running) { libav_mutex.Unlock(); @@ -1559,8 +1607,8 @@ UINT AudioOMX::DeliverMediaPacket(MediaPacket packet, const UCHAR* buffer, Log::getInstance()->log("Audio", Log::DEBUG,"saved audio: %d",len); haveToCopy -= min(len-decompress_buffer_filled,0); *samplepos += min(len-decompress_buffer_filled,0); - if (current_context->frame_size>0) framesize=min(current_context->frame_size,haveToCopy); - else framesize=haveToCopy; + //if (current_context->frame_size>0) framesize=min(current_context->frame_size,haveToCopy); + /*else*/ framesize=haveToCopy; } else { framesize=haveToCopy; } @@ -1574,8 +1622,8 @@ UINT AudioOMX::DeliverMediaPacket(MediaPacket packet, const UCHAR* buffer, haveToCopy -= len; *samplepos += len; errcount=0; - if (current_context->frame_size>0) framesize=min(current_context->frame_size,haveToCopy); - else framesize=haveToCopy; + /*if (current_context->frame_size>0) framesize=min(current_context->frame_size,haveToCopy); + else*/framesize=haveToCopy; } else { errcount++; framesize=haveToCopy; diff --git a/audioomx.h b/audioomx.h index bd72c56..2bd71c8 100644 --- a/audioomx.h +++ b/audioomx.h @@ -116,8 +116,8 @@ class AudioOMX : public Audio - unsigned int AdvanceAc3AudioSync(UCHAR *data,unsigned int size); - unsigned int AdvanceMpAudioSync(UCHAR *data,unsigned int size); + unsigned int AdvanceAc3AudioSync(UCHAR *data,unsigned int size,unsigned int *framesize); + unsigned int AdvanceMpAudioSync(UCHAR *data,unsigned int size,unsigned int *framesize); void ReturnEmptyOMXBuffer(OMX_BUFFERHEADERTYPE* bulibaver); @@ -143,7 +143,7 @@ class AudioOMX : public Audio int PrepareInputBufsOMX(); int DestroyInputBufsOMX(); - int ChangeAudioPortConfig(); + int ChangeAudioPortConfig(bool disport); int ChangeAudioDestination(); long long correctAudioLatency(long long pts,int addsamples,int srate); diff --git a/player.cc b/player.cc index f2c9d10..1650ed4 100644 --- a/player.cc +++ b/player.cc @@ -93,11 +93,11 @@ int Player::init(bool p_isPesRecording,double framespersecond) unsigned int demux_video_size=2097152; unsigned int demux_audio_size=524288; if (video->supportsh264()) { - demux_video_size*=5*1;//5; + demux_video_size*=5*2;//5; } if (audio->maysupportAc3()) { - demux_audio_size*=2; + //demux_audio_size*=2; } if (!demuxer->init(this, audio, video,teletext, demux_video_size,demux_audio_size,65536, framespersecond, subtitles)) diff --git a/playerlivetv.cc b/playerlivetv.cc index cd48c55..e964489 100644 --- a/playerlivetv.cc +++ b/playerlivetv.cc @@ -81,7 +81,7 @@ int PlayerLiveTV::init() } if (audio->maysupportAc3()) { - demux_audio_size*=2; + //demux_audio_size*=2; } int text_fak=video->getTeletextBufferFaktor(); diff --git a/videoomx.cc b/videoomx.cc index cd4d242..25444c1 100644 --- a/videoomx.cc +++ b/videoomx.cc @@ -2105,7 +2105,7 @@ bool VideoOMX::FrameSkip(long long pts) if ((target_time - current_time) < 0000LL) { //skip a bit more skip = true; // we are too slow skipping = true; - /* Log::getInstance()->log("Video", Log::DEBUG,"Skipping frames2 %lld %lld %d",target_time-current_time,pts,Demuxer::getInstance()->getFrameRate()); +/* Log::getInstance()->log("Video", Log::DEBUG,"Skipping frames2 %lld %lld %d",target_time-current_time,pts,Demuxer::getInstance()->getFrameRate()); Log::getInstance()->log("Video", Log::DEBUG, "skip detail pts: %lld target: %lld sys: %lld off: %lld diff %lld",pts,target_time,current_time,offset, target_time-current_time);*/ } else { @@ -2129,7 +2129,7 @@ void VideoOMX::FrameWaitforDisplay(long long pts) target_time+=offset; //Log::getInstance()->log("Video", Log::DEBUG, "Wait for display pts: %lld tg: %lld sys: %lld off: %lld diff %lld po: %lld",pts,target_time,current_time,offset, // target_time-current_time,playbacktimeoffset); - if ((target_time-current_time)>2000000LL) target_time=current_time+2000000LL; // something is wrong do not wait too long + if ((target_time-current_time)>1000000LL) target_time=current_time+1000000LL; // something is wrong do not wait too long //Log::getInstance()->log("Video", Log::DEBUG, "Wait for display pts: %lld tg: %lld sys: %lld off: %lld diff %lld po: %lld",pts,target_time,current_time,offset, // target_time-current_time,playbacktimeoffset); @@ -2192,15 +2192,15 @@ void VideoOMX::threadMethod() //Log::getInstance()->log("Video", Log::DEBUG, "threadMethod Frameskip"); } else { - // Log::getInstance()->log("Video", Log::DEBUG, - // "prot empty this buffer in"); + Log::getInstance()->log("Video", Log::DEBUG, + "prot empty this buffer in"); OMX_ERRORTYPE error = ProtOMXEmptyThisBuffer(omx_vid_dec, pict); if (error != OMX_ErrorNone) { Log::getInstance()->log("Video", Log::DEBUG, "OMX_EmptyThisBuffer failed %x", error); } - // Log::getInstance()->log("Video", Log::DEBUG, - // "prot empty this buffer out"); + Log::getInstance()->log("Video", Log::DEBUG, + "prot empty this buffer out"); if (deint_first_frame && dodeint) DeinterlaceFix(); if (islast) FrameWaitforDisplay(time); } -- 2.39.5