home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / wvis0626.zip / warpvision_20020626.zip / audio / adecode.c next >
C/C++ Source or Header  |  2002-06-19  |  12KB  |  479 lines

  1. /*
  2.  * WarpVision audio
  3.  *
  4.  * Copyleft Alex Strelnikov.
  5.  *
  6.  * WarpVision is free software; you can redistribute it and/or modify
  7.  * it under the terms of the GNU General Public License as published by
  8.  * the Free Software Foundation; either version 2 of the License, or
  9.  * (at your option) any later version.
  10.  *
  11.  * WarpVision is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program; if not, write to the Free Software
  18.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  19.  *
  20. */
  21.  
  22. #define INCL_DOS
  23.  
  24. #include <os2.h>
  25. #include <os2me.h>
  26.  
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30.  
  31. #include <assert.h>
  32.  
  33. #include <sys\types.h>
  34.  
  35. #include <inttypes.h>
  36.  
  37. #include "avcodec.h"
  38. #include "avifmt.h"
  39. #include "stream.h"
  40. #include "demuxer.h"
  41. #include "stheader.h"
  42.  
  43. #include "codecs.h"
  44. #include "dart.h"
  45. #include "adecode.h"
  46.  
  47. #include <mp3.h>
  48.  
  49. #include <a52.h>
  50.  
  51. sample_t *a52_data;
  52. a52_state_t *a52_state;
  53. static int a52_flags;
  54. sample_t a52_level, a52_bias;
  55. int a52_sample_rate;
  56. int a52_bit_rate;
  57.  
  58. #define AUDIO_FRAME_SIZE   65535
  59.  
  60. char *audio_buffer;
  61.  
  62. char *input_audio_buffer;
  63. int input_audio_buffer_len;
  64.  
  65. extern sh_audio_t *sh_audio;
  66.  
  67. int audio_resample_flg = FALSE;
  68. ReSampleContext *audio_resample_ctx;
  69. int audio_header_decoded = FALSE;
  70.  
  71. audio_header_t audio_header;
  72.  
  73. DartStruct DART;
  74.  
  75. extern HMTX dart_cb_mtx;
  76.  
  77. void CalcAVtime(void);
  78.  
  79. int a52_fill_buffer(sh_audio_t *sh_audio)
  80. {
  81.     int length;
  82.  
  83.     input_audio_buffer_len = 0;
  84.  
  85.     // Sync frame
  86.     for(;;)
  87.     {
  88.         while(input_audio_buffer_len < 7)
  89.         {
  90.             int chr;
  91.  
  92.             chr = demux_getc(sh_audio->ds);
  93.  
  94.             if (chr < 0)
  95.                return -1;
  96.  
  97.             input_audio_buffer[input_audio_buffer_len++] = chr;
  98.         }
  99.  
  100.         length = a52_syncinfo(input_audio_buffer,
  101.                               &a52_flags,
  102.                               &a52_sample_rate,
  103.                               &a52_bit_rate);
  104.         // if done
  105.         if(length >= 7 && length <= 3840)
  106.             break;
  107.  
  108.         // resync
  109.         memmove(input_audio_buffer, input_audio_buffer + 1, 6);
  110.         input_audio_buffer_len--;
  111.     }
  112.  
  113.     // read a52 frame
  114.     input_audio_buffer_len = demux_read_data(sh_audio->ds,
  115.                                              input_audio_buffer + 7,
  116.                                              length - 7);
  117.     return length;
  118. }
  119.  
  120. // MP3 decoder buffer callback:
  121. int mp3_fill_buffer(char *buf, int size)
  122. {
  123.   return demux_read_data(sh_audio->ds, buf, size);
  124. }
  125.  
  126. void resync_audio_stream(sh_audio_t *sh_audio)
  127. {
  128.     switch(sh_audio->codec->codec_type)
  129.     {
  130.         case AUDIO_MPEG:
  131.  
  132.             MP3_DecodeFrame(NULL,-2); // resync
  133.             MP3_DecodeFrame(NULL,-2); // resync
  134.             MP3_DecodeFrame(NULL,-2); // resync
  135.  
  136.         break;
  137.  
  138.         case AUDIO_AC3:
  139.             // nope
  140.         break;
  141.     }
  142. }
  143.  
  144. void skip_audio_frame(sh_audio_t *sh_audio)
  145. {
  146.     switch(sh_audio->codec->codec_type)
  147.     {
  148.         case AUDIO_MPEG:
  149.  
  150.             MP3_DecodeFrame(NULL,-2); // skip MPEG frame
  151.  
  152.         break;
  153.  
  154.         case AUDIO_AC3:
  155.  
  156.             a52_fill_buffer(sh_audio); // skip AC3 frame
  157.  
  158.         break;
  159.     }
  160. }
  161.  
  162.  
  163. size_t dartCallback (void *Buffer, size_t BufferSize)
  164. {
  165.     int rc, hr, bsize, usize, len;
  166.     int isamp, osamp;
  167.     DWORD srcsize = 0;
  168.     unsigned char *tmp_buffer;
  169.         
  170.     CalcAVtime();
  171.  
  172.     DosRequestMutexSem(dart_cb_mtx, (ULONG)SEM_INDEFINITE_WAIT);
  173.  
  174.     if (DART.Stopped == TRUE)
  175.         return 0;
  176.  
  177.     audio_header.dart_buffer_size = BufferSize;
  178.  
  179.     bsize = BufferSize;
  180.     usize = 0;
  181.  
  182.     tmp_buffer = (unsigned char *)malloc(AUDIO_FRAME_SIZE);
  183.  
  184.     if (audio_header.audio_buffer_used > 0)
  185.     {
  186.         memmove((BYTE *)Buffer + usize, audio_buffer, audio_header.audio_buffer_used);
  187.         bsize -= audio_header.audio_buffer_used;
  188.         usize += audio_header.audio_buffer_used;
  189.         audio_header.audio_buffer_used = 0;
  190.     }
  191.  
  192.     while(bsize > 0)
  193.     {
  194.       len = audio_decode(sh_audio, FALSE);
  195.  
  196.       if (len <= 0)
  197.           goto Exit;
  198.  
  199.       if (audio_resample_flg > 0)
  200.       {
  201.           isamp = len/(audio_header.channels * 2);
  202.           if (isamp > 0)
  203.           {
  204.               osamp = audio_resample(audio_resample_ctx, (short *)tmp_buffer, (short *)audio_buffer, isamp);
  205.               osamp *= (audio_header.channels * 2);
  206.           }
  207.           else
  208.               osamp = 0;
  209.       }
  210.       else
  211.       {
  212.           memcpy(tmp_buffer, audio_buffer, len);
  213.           osamp = len;
  214.       }
  215.  
  216.       if(osamp == 0)
  217.           goto Exit;
  218.  
  219.       if (osamp <= bsize)
  220.       {
  221.           memcpy((BYTE *)Buffer + usize, tmp_buffer, osamp);
  222.           bsize -= osamp;
  223.           usize += osamp;
  224.           audio_header.audio_buffer_used = 0;
  225.       } else {
  226.           memcpy((BYTE *)Buffer + usize, tmp_buffer, bsize);
  227.           audio_header.audio_buffer_used = osamp - bsize;
  228.           memmove(audio_buffer, tmp_buffer + bsize, audio_header.audio_buffer_used);
  229.           bsize = 0;
  230.       }
  231.  
  232.     }
  233.  
  234. Exit:
  235.  
  236.     free(tmp_buffer);
  237.  
  238.     DosReleaseMutexSem(dart_cb_mtx);
  239.  
  240.     return BufferSize;
  241. }
  242.  
  243.  
  244. int audio_init(sh_audio_t *sh_audio, int AudioDeviceID, int Resample)
  245. {
  246.     int rc;
  247.  
  248.     audio_resample_flg = Resample;
  249.  
  250.     audio_buffer = (UCHAR *)malloc(AUDIO_FRAME_SIZE * 4);
  251.     audio_header.audio_buffer_used = 0;
  252.  
  253.     input_audio_buffer = (UCHAR *)malloc(AUDIO_FRAME_SIZE * 4);
  254.     input_audio_buffer_len = 0;
  255.  
  256.     memset(&audio_header, 0, sizeof(audio_header_t));
  257.  
  258.     switch(sh_audio->codec->codec_type)
  259.     {
  260.         case AUDIO_MPEG:
  261.  
  262.             MP3_Init();
  263.  
  264.             MP3_DecodeFrame(audio_buffer, -1);
  265.  
  266.             audio_header.sample_rate = MP3_samplerate;
  267.             audio_header.bit_rate = MP3_bitrate;
  268. //            audio_header.channels = MP3_channels;
  269.             audio_header.channels = 2;
  270.             audio_header.format = MCI_WAVE_FORMAT_PCM;
  271.             audio_header.bits_per_sample = BPS_16;
  272.             sh_audio->i_bps = MP3_bitrate*(1000/8);
  273.  
  274.         break;
  275.  
  276.         case AUDIO_AC3:
  277.         {
  278.             a52_state = a52_init(0);
  279.  
  280.             if (a52_state == NULL)
  281.                 return FALSE;
  282.  
  283.             if (a52_fill_buffer(sh_audio) < 0)
  284.                 return FALSE;
  285.  
  286.             a52_flags = A52_STEREO;
  287.             a52_flags |= A52_ADJUST_LEVEL;
  288.             a52_level = 1;
  289.             a52_bias = 384;
  290.  
  291.             if (a52_frame(a52_state,
  292.                           input_audio_buffer,
  293.                           &a52_flags, &a52_level, a52_bias) != 0)
  294.                 return FALSE;
  295.  
  296.             audio_header.sample_rate = a52_sample_rate;
  297.             audio_header.bit_rate = a52_bit_rate / 1000;
  298.             audio_header.channels = 2;
  299.             audio_header.format = MCI_WAVE_FORMAT_PCM;
  300.             audio_header.bits_per_sample = BPS_16;
  301.             sh_audio->i_bps = a52_bit_rate*(1000/8);
  302.         }
  303.         break;
  304.  
  305.         case AUDIO_PCM:
  306.             audio_header.sample_rate = sh_audio->wf->nSamplesPerSec;
  307.             audio_header.bit_rate = sh_audio->wf->nAvgBytesPerSec/1000;
  308.             audio_header.channels = sh_audio->wf->nChannels;
  309.             audio_header.format = MCI_WAVE_FORMAT_PCM;
  310.             sh_audio->i_bps = sh_audio->wf->nAvgBytesPerSec;
  311.  
  312.             switch (sh_audio->wf->wBitsPerSample)
  313.             {
  314.                 case 8:
  315.                     audio_header.bits_per_sample = BPS_8;
  316.                 break;
  317.  
  318.                 case 16:
  319.                     audio_header.bits_per_sample = BPS_16;
  320.                 break;
  321.  
  322.                 default:
  323.                     audio_header.bits_per_sample = BPS_16;
  324.                 break;
  325.             }
  326.  
  327.         break;
  328.  
  329.         default:
  330.             // mmmmmm...
  331.         break;
  332.     }
  333.  
  334.     if (audio_resample_flg > 0)
  335.     {
  336.         switch(audio_resample_flg)
  337.         {
  338.             case RESAMPLE_TO_44100:
  339.                 if (audio_header.sample_rate == 48000)
  340.                 {
  341.                     audio_header.resample_rate = 44100;
  342.                 }
  343.             break;
  344.  
  345.             case RESAMPLE_TO_48000:
  346.                 if (audio_header.sample_rate != 48000)
  347.                 {
  348.                     audio_header.resample_rate = 48000;
  349.                 }
  350.             break;
  351.         }
  352.  
  353.         if (audio_header.resample_rate != 0)
  354.         {
  355.             audio_resample_ctx = audio_resample_init(audio_header.channels,
  356.                                                      audio_header.channels,
  357.                                                      audio_header.resample_rate, 
  358.                                                      audio_header.sample_rate);
  359.             printf("Audio: resample %d -> %d\n",
  360.                    audio_header.sample_rate,
  361.                    audio_header.resample_rate);
  362.         }
  363.         else
  364.         {
  365.             audio_resample_flg = RESAMPLE_NONE;
  366.         }
  367.     }
  368.  
  369.     DART.Shareable = FALSE;
  370.  
  371.     if (audio_header.resample_rate != 0)
  372.     {
  373.         rc = dart_init(AudioDeviceID,
  374.                        audio_header.bits_per_sample,
  375.                        audio_header.resample_rate,
  376.                        audio_header.format,
  377.                        audio_header.channels,
  378.                        2,
  379.                        dartCallback);
  380.     }
  381.     else
  382.     {
  383.         rc = dart_init(AudioDeviceID,
  384.                        audio_header.bits_per_sample,
  385.                        audio_header.sample_rate,
  386.                        audio_header.format,
  387.                        audio_header.channels,
  388.                        2,
  389.                        dartCallback);
  390.     }
  391.  
  392.     sh_audio->o_bps = audio_header.channels * audio_header.sample_rate * 2;
  393.  
  394.     return rc;
  395. }
  396.  
  397. int audio_decode(sh_audio_t *sh_audio, int init_flag)
  398. {
  399.     UCHAR *ac3_audio_data = NULL;
  400.     UCHAR compr_audio_data[2048];
  401.     int in_ptr = 0;
  402.     UCHAR *temp_data;
  403.     int len, size, total_len, i, ret;
  404.     int out_size = 0;
  405.     static int read_size = 1024;
  406.  
  407.  Next:
  408.  
  409.     switch(sh_audio->codec->codec_type)
  410.     {
  411.         case AUDIO_MPEG:
  412.         {
  413.             total_len = MP3_DecodeFrame(audio_buffer, -1);
  414.  
  415.             if (total_len == 0)
  416.                 total_len = -1;
  417.         }
  418.  
  419.         break;
  420.  
  421.         case AUDIO_AC3:
  422.         {
  423.             int rc;
  424.  
  425.             if (input_audio_buffer_len == 0)
  426.             {
  427.                 if (a52_fill_buffer(sh_audio) < 0)
  428.                 {
  429.                     total_len = -1;
  430.                     break;
  431.                 }
  432.             }
  433.  
  434.             input_audio_buffer_len = 0;
  435.  
  436.             a52_flags = A52_STEREO;
  437.             a52_flags |= A52_ADJUST_LEVEL;
  438.             a52_level = 1;
  439.             a52_bias = 384;
  440.  
  441.             rc = a52_frame(a52_state,
  442.                            input_audio_buffer,
  443.                            &a52_flags,
  444.                            &a52_level,
  445.                            a52_bias);
  446.             if (rc != 0)
  447.                 total_len = -1;
  448.  
  449.             a52_dynrng(a52_state, NULL, NULL);
  450.  
  451.             total_len = 0;
  452.  
  453.             for (i = 0; i < 6; i++)
  454.             {
  455.                 if (a52_block(a52_state) != 0)
  456.                     break;
  457.  
  458.                 float2s16_2(a52_samples(a52_state), (int8_t *)audio_buffer + total_len);
  459.  
  460.                 total_len += (256 * 4);
  461.             }
  462.         }
  463.  
  464.         break;
  465.  
  466.         case AUDIO_PCM:
  467.         {
  468.             total_len = demux_read_data(sh_audio->ds, audio_buffer, 1024);
  469.  
  470.             if (total_len == 0)
  471.                 total_len = -1;
  472.         }
  473.  
  474.         break;
  475.     }
  476.  
  477.     return total_len;
  478. }
  479.