Ffmpeg & Decklink

Ask software engineering and SDK questions for developers working on Mac OS X, Windows or Linux.
  • Author
  • Message
Offline

John Wake

  • Posts: 3
  • Joined: Mon Nov 17, 2014 12:33 pm

Ffmpeg & Decklink

PostMon Nov 17, 2014 12:53 pm

Hi there

I've got some problem during sending audio bytes to Decklink device extreme HD.
The output sound is messy and not clear.

Kindly check the following codes:

Code: Select all
HRESULT ffmpeg::GetAudioBuffer(_Inout_ uint8_t* pAudioBuffer, _Inout_ int& pAudioBufferSize)
{
   HRESULT hr = S_OK;
   size_t i = 0;
   int gotFrame = 0;
   if (av_read_frame(avFormatCtx, avPacket) >= 0)
   {
      // Is this a packet from the video stream
      if (avPacket->stream_index == videoStreamIndex)
      {
         //Nop...we will use it and unuse openCV
      }
      else if (avPacket->stream_index == audioStreamIndex)
      {
         auto ret = avcodec_decode_audio4(audioCodec.pCodecCtx, audioCodec.pFrame, &gotFrame, avPacket);
         if (ret < 0)
         {
            OutputDebugString(L"Error in decoding audio frame");
            return hr;
         }

         if (gotFrame > 0)
         {
            int outputSamples = swr_convert(
               this->audio_convert_ctx,
               &pAudioBuffer,
               MAX_AUDIO_FRAME_SIZE,
               (const uint8_t **) audioCodec.pFrame->data,
               audioCodec.pFrame->nb_samples);

            pAudioBufferSize = av_samples_get_buffer_size(
               NULL,
               this->audio_out_channels_layout,
               this->out_nb_samples,
               this->out_sample_fmt,
               1);
         }

         //free the packet
         av_free_packet(avPacket);
      }
      else
      {
         //free the packet
         av_free_packet(avPacket);
      }
   }

   return hr;
}



Code: Select all

       auto pAudioBuffer = new uint8_t[192000 * 2];
        int size = 0;
   f.GetAudioBuffer(pAudioBuffer, size);

   // Schedule one-frame of audio tone
   m_deckLinkOutput->ScheduleAudioSamples(
         pAudioBuffer,
         size,
         0,
         bmdAudioSampleRate48kHz,
         NULL);

   delete [] pAudioBuffer;

   if (samplesWritten != size)
   {
      OutputDebugString(std::to_wstring(samplesWritten).c_str());
      OutputDebugString(std::to_wstring(size).c_str());
   }



Please help me on this issue.
Any help is really appropriated.
Offline

chasapis.christos

  • Posts: 17
  • Joined: Thu Jun 19, 2014 3:31 pm
  • Location: Greece

Re: Ffmpeg & Decklink

PostSat Nov 22, 2014 12:36 pm

Dear John i have already answear that at your's PM.
I didn't read your's code but you must have in mind that the BMD uses Interleaved Audio VS avlib (ffmpeg) that uses Planar Audio.

There is the avresample lib for that (#include <libavresample/avresample.h> or you can change the plannar to interleaved with your code).


Let's say that we have a AVCodecContext named as Actx and a AVAudioResample as avr
From my source code the code is :

Code: Select all
 
            AVSampleFormat resample_sample_fmt = AV_SAMPLE_FMT_S16;
            int resample_sample_rate = 48000;
            int output_channels = 2;
            av_opt_set_int(avr,"in_channel_layout", Actx->channel_layout, 0); 
            av_opt_set_int(avr,"out_channel_layout", AV_CH_LAYOUT_STEREO, 0);   
            av_opt_set_int(avr,"in_sample_fmt", aframe->format, 0);
            av_opt_set_int(avr,"out_sample_fmt", resample_sample_fmt, 0);
            av_opt_set_int(avr,"in_sample_rate", Actx->sample_rate, 0);
            av_opt_set_int(avr,"out_sample_rate", resample_sample_rate, 0);


Then you should call the :
1.) avresample_open(avr)
2.) av_get_bytes_per_sample();
3.) av_samples_get_buffer_size();
4.) avresample_convert();

The next code if from my working source code :

Code: Select all
           
            int osize = av_get_bytes_per_sample(resample_sample_fmt);
            nb_samples = aframe->nb_samples;
            out_size = av_samples_get_buffer_size(&out_linesize,output_channels, nb_samples,resample_sample_fmt, 0);
            uint8_t * audio_buf1;
            audio_buf1 = (uint8_t*)malloc(sizeof(uint8_t)*buffersize);
            uint8_t * pointer_to_audio_buf1;
            pointer_to_audio_buf1 = audio_buf1;
            out_samples = avresample_convert(avr, &audio_buf1 /*(uint8_t**)audio_buf1*/, aframe->nb_samples /*out_linesize*/, nb_samples, aframe->data, aframe->linesize[0], aframe->nb_samples);
            //printf(" out_samples::%d out_linesize::%d  in_linesize::%d\n",  out_samples,out_linesize, aframe->linesize[0]);


Mine code to convert the Planar to Interleved (without change the Sample rate and the channel mapping) is :

Code: Select all
   
            #define CHASAPIS
            #ifdef CHASAPIS
            uint8_t audio_buf1[buffersize];
            if (av_sample_fmt_is_planar((AVSampleFormat)(aframe->format))){
                /* chasapis from s16p to s16 algorithm */
                int internal_counter = 0;
                for (int counter=0; counter < buffersize; counter+=4){
                    audio_buf1[counter]=aframe->data[0][internal_counter++];///ch1 1st byte
                    audio_buf1[1+counter]=aframe->data[0][internal_counter++];///ch1 2nd byte
                }
                for (int counter=2; counter < buffersize; counter+=4){
                    audio_buf1[counter]=aframe->data[0][internal_counter++];///ch2 1st byte
                    audio_buf1[1+counter]=aframe->data[0][internal_counter++];///ch2 2nd byte
                }
            } else {
                printf("non planar\n");
                for (int counter=0; counter < buffersize; counter++)  audio_buf1[counter]=aframe->data[0][counter];
            }

        #endif // CHASAPIS


Everybody can feel free to contact me via email, skype, facebook, linked in.
I am trying to make a team!!
http://www.chasapis.com
Last edited by chasapis.christos on Fri Nov 28, 2014 3:19 pm, edited 2 times in total.
Offline

John Wake

  • Posts: 3
  • Joined: Mon Nov 17, 2014 12:33 pm

Re: Ffmpeg & Decklink

PostMon Nov 24, 2014 11:41 am

We fixed the problem.
Really appreciated.

Regards
John

Return to Software Developers

Who is online

Users browsing this forum: No registered users and 10 guests