"Fossies" - the Fresh Open Source Software Archive

Member "quicktime4linux-2.3/rawaudio.c" (9 Jan 2007, 7950 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 #include "quicktime.h"
    2 #include "rawaudio.h"
    3 
    4 typedef struct
    5 {
    6     char *work_buffer;
    7     long buffer_size;
    8 } quicktime_rawaudio_codec_t;
    9 
   10 /* =================================== private for rawaudio */
   11 
   12 int rawaudio_byte_order(void)
   13 {                /* 1 if little endian */
   14     int16_t byteordertest;
   15     int byteorder;
   16 
   17     byteordertest = 0x0001;
   18     byteorder = *((unsigned char *)&byteordertest);
   19     return byteorder;
   20 }
   21 
   22 int rawaudio_swap_bytes(char *buffer, long samples, int channels, int bits)
   23 {
   24     long i;
   25     char byte1, byte2, byte3;
   26     char *buffer1, *buffer2, *buffer3;
   27 
   28     if(!rawaudio_byte_order()) return 0;
   29 
   30     switch(bits)
   31     {
   32         case 8:
   33             break;
   34 
   35         case 16:
   36             buffer1 = buffer;
   37             buffer2 = buffer + 1;
   38             while(i < samples * 2)
   39             {
   40                 byte1 = buffer2[i];
   41                 buffer2[i] = buffer1[i];
   42                 buffer1[i] = byte1;
   43                 i += 2;
   44             }
   45             break;
   46 
   47         case 24:
   48             buffer1 = buffer;
   49             buffer2 = buffer + 2;
   50             while(i < samples * 3)
   51             {
   52                 byte1 = buffer2[i];
   53                 buffer2[i] = buffer1[i];
   54                 buffer1[i] = byte1;
   55                 i += 3;
   56             }
   57             break;
   58 
   59         default:
   60             break;
   61     }
   62     return 0;
   63 }
   64 
   65 static int get_work_buffer(quicktime_t *file, int track, long bytes)
   66 {
   67     quicktime_rawaudio_codec_t *codec = ((quicktime_codec_t*)file->atracks[track].codec)->priv;
   68 
   69     if(codec->work_buffer && codec->buffer_size != bytes)
   70     {
   71         free(codec->work_buffer);
   72         codec->work_buffer = 0;
   73     }
   74     
   75     if(!codec->work_buffer) 
   76     {
   77         codec->buffer_size = bytes;
   78         if(!(codec->work_buffer = malloc(bytes))) return 1;
   79     }
   80     return 0;
   81 }
   82 
   83 /* =================================== public for rawaudio */
   84 
   85 static int quicktime_delete_codec_rawaudio(quicktime_audio_map_t *atrack)
   86 {
   87     quicktime_rawaudio_codec_t *codec = ((quicktime_codec_t*)atrack->codec)->priv;
   88 
   89     if(codec->work_buffer) free(codec->work_buffer);
   90     codec->work_buffer = 0;
   91     codec->buffer_size = 0;
   92     free(codec);
   93     return 0;
   94 }
   95 
   96 static int quicktime_decode_rawaudio(quicktime_t *file, 
   97                     int16_t *output_i, 
   98                     float *output_f, 
   99                     long samples, 
  100                     int track, 
  101                     int channel)
  102 {
  103     int result = 0;
  104     long i, j;
  105     quicktime_audio_map_t *track_map = &(file->atracks[track]);
  106     quicktime_rawaudio_codec_t *codec = ((quicktime_codec_t*)track_map->codec)->priv;
  107     int step = file->atracks[track].channels * quicktime_audio_bits(file, track) / 8;
  108 
  109     get_work_buffer(file, track, samples * step);
  110     result = !quicktime_read_audio(file, codec->work_buffer, samples, track);
  111 // Undo increment since this is done in codecs.c
  112     track_map->current_position -= samples;
  113 
  114     switch(quicktime_audio_bits(file, track))
  115     {
  116         case 8:
  117             if(output_i && !result)
  118             {
  119                 for(i = 0, j = 0; i < samples; i++)
  120                 {
  121                     output_i[i] = ((int16_t)((unsigned char)codec->work_buffer[j]) << 8);
  122                     j += step;
  123                     output_i[i] -= 0x8000;
  124                 }
  125             }
  126             else
  127             if(output_f && !result)
  128             {
  129                 for(i = 0, j = 0; i < samples; i++)
  130                 {
  131                     output_f[i] = (float)((unsigned char)codec->work_buffer[j]) - 0x80;
  132                     output_f[i] /= 0x7f;
  133                     j += step;
  134                 }
  135             }
  136             break;
  137 
  138         case 16:
  139             if(output_i && !result)
  140             {
  141                 for(i = 0, j = 0; i < samples; i++)
  142                 {
  143                     output_i[i] = (int16_t)(codec->work_buffer[j]) << 8 |
  144                                     (unsigned char)(codec->work_buffer[j + 1]);
  145                     j += step;
  146                     output_i[i] -= 0x8000;
  147                 }
  148             }
  149             else
  150             if(output_f && !result)
  151             {
  152                 for(i = 0, j = 0; i < samples; i++)
  153                 {
  154                     output_f[i] = (float)((int16_t)(codec->work_buffer[j]) << 8 |
  155                                     (unsigned char)(codec->work_buffer[j + 1])) - 0x8000;
  156                     output_f[i] /= 0x7fff;
  157                     j += step;
  158                 }
  159             }
  160             break;
  161 
  162         case 24:
  163             if(output_i && !result)
  164             {
  165                 for(i = 0, j = 0; i < samples; i++)
  166                 {
  167                     output_i[i] = ((int16_t)(codec->work_buffer[j]) << 8) | 
  168                                     (unsigned char)(codec->work_buffer[j + 1]);
  169                     output_i[i] -= 0x8000;
  170                     j += step;
  171                 }
  172             }
  173             else
  174             if(output_f && !result)
  175             {
  176                 for(i = 0, j = 0; i < samples; i++)
  177                 {
  178                     output_f[i] = (float)(((int)(codec->work_buffer[j]) << 16) | 
  179                                     ((unsigned int)(codec->work_buffer[j + 1]) << 8) |
  180                                     (unsigned char)(codec->work_buffer[j + 2])) - 0x800000;
  181                     output_f[i] /= 0x7fffff;
  182                     j += step;
  183                 }
  184             }
  185             break;
  186 
  187         default:
  188             break;
  189     }
  190 /*printf("quicktime_decode_rawaudio 2\n"); */
  191 
  192     return result;
  193 }
  194 
  195 #define CLAMP(x, y, z) ((x) = ((x) <  (y) ? (y) : ((x) > (z) ? (z) : (x))))
  196 
  197 static int quicktime_encode_rawaudio(quicktime_t *file, 
  198                             int16_t **input_i, 
  199                             float **input_f, 
  200                             int track, 
  201                             long samples)
  202 {
  203     int result = 0;
  204     long i, j, offset;
  205     quicktime_audio_map_t *track_map = &(file->atracks[track]);
  206     quicktime_rawaudio_codec_t *codec = ((quicktime_codec_t*)track_map->codec)->priv;
  207     int step = file->atracks[track].channels * quicktime_audio_bits(file, track) / 8;
  208     int sample;
  209     float sample_f;
  210 
  211     get_work_buffer(file, track, samples * step);
  212 
  213     if(input_i)
  214     {
  215         for(i = 0; i < track_map->channels; i++)
  216         {
  217             switch(quicktime_audio_bits(file, track))
  218             {
  219                 case 8:
  220                     for(j = 0; j < samples; j++)
  221                     {
  222                         sample = input_i[i][j] >> 8;
  223                         sample += 0x80;
  224                         codec->work_buffer[j * step + i] = sample;
  225                     }
  226                     break;
  227                 case 16:
  228                     for(j = 0; j < samples; j++)
  229                     {
  230                         sample = input_i[i][j];
  231                         sample += 0x8000;
  232                         codec->work_buffer[j * step + i * 2] = ((unsigned int)sample & 0xff00) >> 8;
  233                         codec->work_buffer[j * step + i * 2 + 1] = ((unsigned int)sample) & 0xff;
  234                     }
  235                     break;
  236                 case 24:
  237                     for(j = 0; j < samples; j++)
  238                     {
  239                         sample = input_i[i][j];
  240                         sample += 0x8000;
  241                         codec->work_buffer[j * step + i * 3] = ((unsigned int)sample & 0xff00) >> 8;
  242                         codec->work_buffer[j * step + i * 3 + 1] = ((unsigned int)sample & 0xff);
  243                         codec->work_buffer[j * step + i * 3 + 2] = 0;
  244                     }
  245                     break;
  246             }
  247         }
  248     }
  249     else
  250     {
  251         for(i = 0; i < track_map->channels; i++)
  252         {
  253             switch(quicktime_audio_bits(file, track))
  254             {
  255                 case 8:
  256                     for(j = 0; j < samples; j++)
  257                     {
  258                         sample_f = input_f[i][j];
  259                         if(sample_f < 0)
  260                             sample = (int)(sample_f * 0x7f - 0.5);
  261                         else
  262                             sample = (int)(sample_f * 0x7f + 0.5);
  263                         CLAMP(sample, -0x7f, 0x7f);
  264                         sample += 0x80;
  265                         codec->work_buffer[j * step + i] = sample;
  266                     }
  267                     break;
  268                 case 16:
  269                     for(j = 0; j < samples; j++)
  270                     {
  271                         sample_f = input_f[i][j];
  272                         if(sample_f < 0)
  273                             sample = (int)(sample_f * 0x7fff - 0.5);
  274                         else
  275                             sample = (int)(sample_f * 0x7fff + 0.5);
  276                         CLAMP(sample, -0x7fff, 0x7fff);
  277                         sample += 0x8000;
  278                         codec->work_buffer[j * step + i * 2] = ((unsigned int)sample & 0xff00) >> 8;
  279                         codec->work_buffer[j * step + i * 2 + 1] = ((unsigned int)sample) & 0xff;
  280                     }
  281                     break;
  282                 case 24:
  283                     for(j = 0; j < samples; j++)
  284                     {
  285                         sample_f = input_f[i][j];
  286                         if(sample_f < 0)
  287                             sample = (int)(sample_f * 0x7fffff - 0.5);
  288                         else
  289                             sample = (int)(sample_f * 0x7fffff + 0.5);
  290                         CLAMP(sample, -0x7fffff, 0x7fffff);
  291                         sample += 0x800000;
  292                         codec->work_buffer[j * step + i * 3] = ((unsigned int)sample & 0xff0000) >> 16;
  293                         codec->work_buffer[j * step + i * 3 + 1] = ((unsigned int)sample & 0xff00) >> 8;
  294                         codec->work_buffer[j * step + i * 3 + 2] = ((unsigned int)sample) & 0xff;
  295                     }
  296                     break;
  297             }
  298         }
  299     }
  300 
  301     result = quicktime_write_audio(file, codec->work_buffer, samples, track);
  302     return result;
  303 }
  304 
  305 
  306 void quicktime_init_codec_rawaudio(quicktime_audio_map_t *atrack)
  307 {
  308     quicktime_codec_t *codec_base = (quicktime_codec_t*)atrack->codec;
  309     quicktime_rawaudio_codec_t *codec;
  310 
  311 /* Init public items */
  312     codec_base->priv = calloc(1, sizeof(quicktime_rawaudio_codec_t));
  313     codec_base->delete_acodec = quicktime_delete_codec_rawaudio;
  314     codec_base->decode_video = 0;
  315     codec_base->encode_video = 0;
  316     codec_base->decode_audio = quicktime_decode_rawaudio;
  317     codec_base->encode_audio = quicktime_encode_rawaudio;
  318     codec_base->fourcc = QUICKTIME_RAW;
  319     codec_base->title = "8 bit unsigned";
  320     codec_base->desc = "8 bit unsigned for video";
  321     codec_base->wav_id = 0x01;
  322 
  323 /* Init private items */
  324     codec = codec_base->priv;
  325 }