Subversion Repositories Kolibri OS

Rev

Rev 2349 | Blame | Last modification | View Log | Download | RSS feed

  1.  
  2. #include <stdint.h>
  3. #include <libavcodec/avcodec.h>
  4. #include <libavformat/avformat.h>
  5. #include <libswscale/swscale.h>
  6.  
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <winlib.h>
  10. #include "sound.h"
  11. #include "fplay.h"
  12.  
  13.  
  14. astream_t astream;
  15.  
  16. static SNDBUF hBuff;
  17.  
  18. extern uint8_t *decoder_buffer;
  19.  
  20. extern volatile uint32_t status;
  21.  
  22. extern volatile uint32_t driver_lock;
  23.  
  24.  
  25. static int snd_format;
  26. int sample_rate;
  27.  
  28. int init_audio(int format)
  29. {
  30.     int    err;
  31.     int    version =-1;
  32.     char  *errstr;
  33.  
  34.     mutex_lock(&driver_lock);
  35.  
  36.     if((err = InitSound(&version)) !=0 )
  37.     {
  38.         mutex_unlock(&driver_lock);
  39.         errstr = "Sound service not installed\n\r";
  40.         goto exit_whith_error;
  41.     };
  42.  
  43.     mutex_unlock(&driver_lock);
  44.  
  45.     printf("sound version 0x%x\n", version);
  46.  
  47.     if( (SOUND_VERSION>(version&0xFFFF)) ||
  48.         (SOUND_VERSION<(version >> 16)))
  49.     {
  50.         errstr = "Sound service version mismatch\n\r";
  51.         goto exit_whith_error;
  52.     }
  53.  
  54.     snd_format = format;
  55.  
  56.     create_thread(audio_thread, 0, 163840);
  57.  
  58.     return 1;
  59.  
  60. exit_whith_error:
  61.  
  62.     printf(errstr);
  63.     return 0;
  64. };
  65.  
  66. static uint64_t samples_lost;
  67. static double  audio_delta;
  68.  
  69. double get_master_clock()
  70. {
  71.     double tstamp;
  72.  
  73.     GetTimeStamp(hBuff, &tstamp);
  74.     return tstamp - audio_delta;
  75. };
  76.  
  77. int decode_audio(AVCodecContext  *ctx, queue_t *qa)
  78. {
  79.     AVPacket   pkt;
  80.     AVPacket    pkt_tmp;
  81.  
  82.     int         len;
  83.     int         data_size=0;
  84.  
  85.     if( astream.count > AVCODEC_MAX_AUDIO_FRAME_SIZE*7)
  86.         return 1;
  87.  
  88.     if( get_packet(qa, &pkt) == 0 )
  89.         return 0;
  90.  
  91.  //          __asm__("int3");
  92.  
  93.     pkt_tmp = pkt;
  94.  
  95.     while(pkt_tmp.size > 0)
  96.     {
  97.         data_size = AVCODEC_MAX_AUDIO_FRAME_SIZE;
  98.  
  99.         len = avcodec_decode_audio3(ctx,(int16_t*)decoder_buffer,
  100.                                    &data_size, &pkt_tmp);
  101.  
  102.         if(len >= 0)
  103.         {
  104.             pkt_tmp.data += len;
  105.             pkt_tmp.size -= len;
  106.  
  107.             mutex_lock(&astream.lock);
  108.             memcpy(astream.buffer+astream.count, decoder_buffer, data_size);
  109.             astream.count += data_size;
  110.             mutex_unlock(&astream.lock);
  111.        }
  112.        else pkt_tmp.size = 0;
  113.     }
  114.     av_free_packet(&pkt);
  115.     return 1;
  116. };
  117.  
  118.  
  119. int audio_thread(void *param)
  120. {
  121.     SND_EVENT evnt;
  122.     int       buffsize;
  123.     int      samples;
  124.     int       err;
  125.     char     *errstr;
  126.  
  127.  
  128.     if((err = CreateBuffer(snd_format|PCM_RING,0, &hBuff)) != 0)
  129.     {
  130.         errstr = "Cannot create sound buffer\n\r";
  131.         goto exit_whith_error;
  132.     };
  133.  
  134.     SetVolume(hBuff,-1000,-1000);
  135.  
  136.     if((err = GetBufferSize(hBuff, &buffsize)) != 0)
  137.     {
  138.         errstr = "Cannot get buffer size\n\r";
  139.         goto exit_whith_error;
  140.     };
  141.  
  142.     buffsize = buffsize/2;
  143.  
  144.     samples = buffsize/4;
  145.  
  146.     while( (astream.count < buffsize*2) &&
  147.                (status != 0) )
  148.         yield();
  149.  
  150.     mutex_lock(&astream.lock);
  151.     {
  152.         SetBuffer(hBuff, astream.buffer, 0, buffsize);
  153.         astream.count -= buffsize;
  154.         if(astream.count)
  155.             memcpy(astream.buffer, astream.buffer+buffsize, astream.count);
  156.         mutex_unlock(&astream.lock);
  157.     };
  158.  
  159.     if((err = PlayBuffer(hBuff, 0)) !=0 )
  160.     {
  161.         errstr = "Cannot play buffer\n\r";
  162.         goto exit_whith_error;
  163.     };
  164.  
  165.  
  166. #ifdef BLACK_MAGIC_SOUND
  167.  
  168.     while( status != 0)
  169.     {
  170.         uint32_t  offset;
  171.  
  172.         GetNotify(&evnt);
  173.  
  174.         if(evnt.code != 0xFF000001)
  175.         {
  176.             printf("invalid event code %d\n\r", evnt.code);
  177.             continue;
  178.         }
  179.  
  180.         if(evnt.stream != hBuff)
  181.         {
  182.             printf("invalid stream %x hBuff= %x\n\r",
  183.                     evnt.stream, hBuff);
  184.             continue;
  185.         }
  186.  
  187.         GetTimeStamp(hBuff, &audio_delta);
  188.         samples_lost = audio_delta*sample_rate/1000;
  189.  
  190.         offset = evnt.offset;
  191.  
  192.         mutex_lock(&astream.lock);
  193.         {
  194.             SetBuffer(hBuff, astream.buffer, offset, buffsize);
  195.             astream.count -= buffsize;
  196.             if(astream.count)
  197.                 memcpy(astream.buffer, astream.buffer+buffsize, astream.count);
  198.             mutex_unlock(&astream.lock);
  199.         };
  200.         break;
  201.     };
  202. #endif
  203.  
  204.     printf("initial audio delta %f\n", audio_delta);
  205.  
  206.     while( status != 0)
  207.     {
  208.         uint32_t  offset;
  209.         double    event_stamp, wait_stamp;
  210.         int       too_late = 0;
  211.  
  212.         GetNotify(&evnt);
  213.  
  214.         if(evnt.code != 0xFF000001)
  215.         {
  216.             printf("invalid event code %d\n\r", evnt.code);
  217.             continue;
  218.         }
  219.  
  220.         if(evnt.stream != hBuff)
  221.         {
  222.             printf("invalid stream %x hBuff= %x\n\r",
  223.                     evnt.stream, hBuff);
  224.             continue;
  225.         };
  226.  
  227.         GetTimeStamp(hBuff, &event_stamp);
  228.  
  229.         offset = evnt.offset;
  230.  
  231.         while( (astream.count < buffsize) &&
  232.                (status != 0) )
  233.         {
  234.             yield();
  235.             GetTimeStamp(hBuff, &wait_stamp);
  236.             if( (wait_stamp - event_stamp) >
  237.                  samples*1500/sample_rate )
  238.             {
  239.                 samples_lost+= samples;
  240.                 audio_delta = (double)samples_lost*1000/sample_rate;
  241. //                printf("audio delta %f\n", audio_delta);
  242.                 too_late = 1;
  243.                 break;
  244.             }
  245.         };
  246.  
  247.         if((too_late == 1) || (status == 0))
  248.             continue;
  249.  
  250.         mutex_lock(&astream.lock);
  251.         SetBuffer(hBuff, astream.buffer, offset, buffsize);
  252.         astream.count -= buffsize;
  253.         if(astream.count)
  254.             memcpy(astream.buffer, astream.buffer+buffsize, astream.count);
  255.         mutex_unlock(&astream.lock);
  256.     }
  257.  
  258.     StopBuffer(hBuff);
  259.     DestroyBuffer(hBuff);
  260.  
  261.     return 0;
  262.  
  263. exit_whith_error:
  264.  
  265.     printf(errstr);
  266.     return -1;
  267.  
  268. };
  269.  
  270.