"Fossies" - the Fresh Open Source Software Archive

Member "quicktime4linux-2.3/vbraudio.c" (9 Jan 2007, 4848 Bytes) of package /linux/privat/old/quicktime4linux-2.3-src.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 // Utility functions for vbr audio
    2 
    3 #include "funcprotos.h"
    4 #include "quicktime.h"
    5 
    6 
    7 
    8 // Maximum samples to store in output buffer
    9 #define MAX_VBR_BUFFER 0x200000
   10 
   11 
   12 
   13 void quicktime_init_vbr(quicktime_vbr_t *ptr, int channels)
   14 {
   15     ptr->channels = channels;
   16     if(!ptr->output_buffer)
   17     {
   18         int i;
   19         ptr->output_buffer = calloc(channels, sizeof(double*));
   20         for(i = 0; i < channels; i++)
   21             ptr->output_buffer[i] = calloc(MAX_VBR_BUFFER, sizeof(double));
   22     }
   23 }
   24 
   25 void quicktime_clear_vbr(quicktime_vbr_t *ptr)
   26 {
   27     int i;
   28 
   29     if(ptr->output_buffer)
   30     {
   31         for(i = 0; i < ptr->channels; i++)
   32             free(ptr->output_buffer[i]);
   33         free(ptr->output_buffer);
   34     }
   35 
   36     if(ptr->input_buffer)
   37     {
   38         free(ptr->input_buffer);
   39     }
   40 }
   41 
   42 void quicktime_vbr_set_channels(quicktime_vbr_t *ptr, int channels)
   43 {
   44     ptr->channels = channels;
   45 }
   46 
   47 int64_t quicktime_vbr_end(quicktime_vbr_t *ptr)
   48 {
   49     return ptr->buffer_end;
   50 }
   51 
   52 unsigned char* quicktime_vbr_input(quicktime_vbr_t *ptr)
   53 {
   54     return ptr->input_buffer;
   55 }
   56 
   57 int quicktime_vbr_input_size(quicktime_vbr_t *ptr)
   58 {
   59     return ptr->input_size;
   60 }
   61 
   62 static int limit_samples(int samples)
   63 {
   64     if(samples > MAX_VBR_BUFFER)
   65     {
   66         fprintf(stderr, 
   67             "quicktime_align_vbr: can't decode more than %p samples at a time.\n",
   68             MAX_VBR_BUFFER);
   69         return 1;
   70     }
   71     return 0;
   72 }
   73 
   74 int quicktime_align_vbr(quicktime_audio_map_t *atrack, 
   75     int samples)
   76 {
   77     quicktime_vbr_t *ptr = &atrack->vbr;
   78     int64_t start_position = atrack->current_position;
   79 
   80     if(limit_samples(samples)) return 1;
   81 
   82 // Desired start point is outside existing range.  Reposition buffer pointer
   83 // to start time of nearest frame.
   84     if(start_position < ptr->buffer_end - ptr->buffer_size ||
   85         start_position > ptr->buffer_end)
   86     {
   87         int64_t start_time = start_position;
   88         ptr->sample = quicktime_time_to_sample(&atrack->track->mdia.minf.stbl.stts,
   89             &start_time);
   90         ptr->buffer_end = start_time;
   91         ptr->buffer_size = 0;
   92     }
   93 
   94     return 0;
   95 }
   96 
   97 int quicktime_read_vbr(quicktime_t *file,
   98     quicktime_audio_map_t *atrack)
   99 {
  100     quicktime_vbr_t *vbr = &atrack->vbr;
  101     quicktime_trak_t *trak = atrack->track;
  102     int64_t offset = quicktime_sample_to_offset(file, 
  103         trak, 
  104         vbr->sample);
  105     int size = quicktime_sample_size(trak, vbr->sample);
  106     int new_allocation = vbr->input_size + size;
  107     int result = 0;
  108 
  109     if(vbr->input_allocation < new_allocation)
  110     {
  111         vbr->input_buffer = realloc(vbr->input_buffer, new_allocation);
  112         vbr->input_allocation = new_allocation;
  113     }
  114 
  115 
  116     quicktime_set_position(file, offset);
  117     result = !quicktime_read_data(file, vbr->input_buffer + vbr->input_size, size);
  118     vbr->input_size += size;
  119     vbr->sample++;
  120     return result;
  121 }
  122 
  123 void quicktime_shift_vbr(quicktime_audio_map_t *atrack, int bytes)
  124 {
  125     quicktime_vbr_t *vbr = &atrack->vbr;
  126     if(bytes >= vbr->input_size)
  127     {
  128         vbr->input_size = 0;
  129     }
  130     else
  131     {
  132         int i, j;
  133         for(i = 0, j = bytes; j < vbr->input_size; i++, j++)
  134             vbr->input_buffer[i] = vbr->input_buffer[j];
  135         vbr->input_size -= bytes;
  136     }
  137 }
  138 
  139 void quicktime_store_vbr_float(quicktime_audio_map_t *atrack,
  140     float *samples,
  141     int sample_count)
  142 {
  143     int i, j;
  144     quicktime_vbr_t *vbr = &atrack->vbr;
  145     for(i = 0; i < sample_count; i++)
  146     {
  147         for(j = 0; j < vbr->channels; j++)
  148         {
  149             vbr->output_buffer[j][vbr->buffer_ptr] = 
  150                 samples[i * vbr->channels + j];
  151         }
  152         vbr->buffer_ptr++;
  153         if(vbr->buffer_ptr >= MAX_VBR_BUFFER)
  154             vbr->buffer_ptr = 0;
  155     }
  156     vbr->buffer_end += sample_count;
  157     vbr->buffer_size += sample_count;
  158     if(vbr->buffer_size > MAX_VBR_BUFFER) vbr->buffer_size = MAX_VBR_BUFFER;
  159 }
  160 
  161 void quicktime_store_vbr_int16(quicktime_audio_map_t *atrack,
  162     int16_t *samples,
  163     int sample_count)
  164 {
  165     int i, j;
  166     quicktime_vbr_t *vbr = &atrack->vbr;
  167     for(i = 0; i < sample_count; i++)
  168     {
  169         for(j = 0; j < vbr->channels; j++)
  170         {
  171             vbr->output_buffer[j][vbr->buffer_ptr] = 
  172                 (float)samples[i * vbr->channels + j] / 32768.0;
  173         }
  174         vbr->buffer_ptr++;
  175         if(vbr->buffer_ptr >= MAX_VBR_BUFFER)
  176             vbr->buffer_ptr = 0;
  177     }
  178     vbr->buffer_end += sample_count;
  179     vbr->buffer_size += sample_count;
  180     if(vbr->buffer_size > MAX_VBR_BUFFER) vbr->buffer_size = MAX_VBR_BUFFER;
  181 }
  182 
  183 void quicktime_copy_vbr_float(quicktime_vbr_t *vbr,
  184     int64_t start_position, 
  185     int samples,
  186     float *output, 
  187     int channel)
  188 {
  189     int i, j;
  190     int input_ptr = vbr->buffer_ptr - 
  191         (vbr->buffer_end - start_position);
  192     while(input_ptr < 0) input_ptr += MAX_VBR_BUFFER;
  193 
  194     for(i = 0; i < samples; i++)
  195     {
  196         output[i] = vbr->output_buffer[channel][input_ptr++];
  197         if(input_ptr >= MAX_VBR_BUFFER)
  198             input_ptr = 0;
  199     }
  200 }
  201 
  202 
  203 void quicktime_copy_vbr_int16(quicktime_vbr_t *vbr,
  204     int64_t start_position, 
  205     int samples,
  206     int16_t *output, 
  207     int channel)
  208 {
  209     int i, j;
  210     int input_ptr = vbr->buffer_ptr - 
  211         (vbr->buffer_end - start_position);
  212     while(input_ptr < 0) input_ptr += MAX_VBR_BUFFER;
  213     for(i = 0; i < samples; i++)
  214     {
  215         output[i] = (int)(vbr->output_buffer[channel][input_ptr++] * 32767);
  216         if(input_ptr >= MAX_VBR_BUFFER)
  217             input_ptr = 0;
  218     }
  219 }
  220 
  221 
  222