Subversion Repositories Kolibri OS

Rev

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