home *** CD-ROM | disk | FTP | other *** search
/ Network PC / Network PC.iso / amiga utilities / graphics / display / mpega / src / mpega_decode.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-11-20  |  11.6 KB  |  382 lines

  1. /*------------------------------------------------------------------------------
  2.  
  3.     File    :    mpega_decode.c
  4.  
  5.     Author  :    Stéphane TAVENARD
  6.  
  7.     $VER:   mpega_decode.c  1.0  (23/06/1995)
  8.  
  9.     (C) Copyright 1995-1995 Stéphane TAVENARD
  10.     All Rights Reserved
  11.  
  12.     #Rev|   Date   |              Comment
  13.     ----|----------|--------------------------------------------------------
  14.     0    |27/05/1995| Initial revision                      ST
  15.     1    |23/06/1995| First release (aminet)                               ST
  16.  
  17.     ------------------------------------------------------------------------
  18.  
  19.     MPEG Audio decode functions
  20.  
  21. ------------------------------------------------------------------------------*/
  22.  
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include <math.h>
  26. #include <stdlib.h>
  27. #include "AudioPort.h"
  28. #include "AIFF.h"
  29. #include "mpega_decode_asm.h"
  30. #include "mpega_decode.h"
  31.  
  32. frame_params *MPEGA_close( frame_params *fr_ps )
  33. {
  34.    if( !fr_ps ) return NULL;
  35.    if( fr_ps->audio_port ) {
  36.       AU_close( fr_ps->audio_port );
  37.       fr_ps->audio_port = NULL;
  38.    }
  39.    if( fr_ps->bs ) BSTR_close( fr_ps->bs );
  40.    fr_ps->bs = NULL;
  41.    if( fr_ps->header ) free( fr_ps->header );
  42.    if( fr_ps->bit_alloc ) free( fr_ps->bit_alloc );
  43.    if( fr_ps->scfsi ) free( fr_ps->scfsi );
  44.    if( fr_ps->scale_index ) free( fr_ps->scale_index );
  45.    if( fr_ps->sample ) free( fr_ps->sample );
  46.    if( fr_ps->fraction ) free( fr_ps->fraction );
  47.    free( fr_ps );
  48.    return NULL;
  49. }
  50.  
  51. frame_params *MPEGA_open( void )
  52. {
  53.    frame_params *fr_ps;
  54.  
  55.    fr_ps = (frame_params *)malloc( sizeof( frame_params ) );
  56.    if( !fr_ps ) return NULL;
  57.    memset( fr_ps, 0, sizeof( frame_params ) );
  58.    fr_ps->header = (layer *)malloc( sizeof( layer ) );
  59.    if( !fr_ps->header ) return MPEGA_close( fr_ps );
  60.    memset( fr_ps->header, 0, sizeof( layer ) );
  61.    fr_ps->bit_alloc = (int *)malloc( sizeof( int ) * 2 * SBLIMIT );
  62.    if( !fr_ps->bit_alloc ) return MPEGA_close( fr_ps );
  63.    fr_ps->scfsi = (int *)malloc( sizeof( int ) * 2 * SBLIMIT );
  64.    if( !fr_ps->scfsi ) return MPEGA_close( fr_ps );
  65.    fr_ps->scale_index = (int *)malloc( sizeof( int ) * 2 * 3 * SBLIMIT );
  66.    if( !fr_ps->scale_index ) return MPEGA_close( fr_ps );
  67.    fr_ps->sample = (int *)malloc( sizeof( int ) * 2 * 3 * SBLIMIT );
  68.    if( !fr_ps->sample ) return MPEGA_close( fr_ps );
  69.    fr_ps->fraction = (INT *)malloc( sizeof( INT ) * 2 * 3 * SBLIMIT );
  70.    if( !fr_ps->fraction ) return MPEGA_close( fr_ps );
  71.  
  72.    /* default values */
  73.    fr_ps->freq_div = 1;
  74.    fr_ps->quality = 2;
  75.    fr_ps->bitstream_buffer_size = BSTR_BUFFER_SIZE;
  76.  
  77.    return fr_ps;
  78. }
  79.  
  80. #define OUT_BUFFER_SIZE  (13824*4)
  81.  
  82. static BOOL stereo_file = FALSE;
  83. static BYTE *l_file_buffer = NULL;
  84. static BYTE *r_file_buffer = NULL;
  85. static BYTE *stereo_file_buffer = NULL;
  86.  
  87. static int init_file( frame_params *fr_ps )
  88. {
  89.    long buffer_byte_size;
  90.  
  91.    if( !fr_ps->out_file_name ) return 1;
  92.  
  93.    if( !fr_ps->out_file ) {
  94.       if( strcmp( fr_ps->out_file_name, "stdout" ) == 0 ) {
  95.      fr_ps->out_file = stdout;
  96.       }
  97.       else {
  98.      fr_ps->out_file = fopen( fr_ps->out_file_name, "w+b" );
  99.      if( !fr_ps->out_file ) {
  100.         fprintf( stderr, "Can't create file '%s'\n", fr_ps->out_file_name );
  101.         return 0;
  102.      }
  103.       }
  104.    }
  105.    if( fr_ps->out_file_type == MPEGA_FILETYPE_AIFF ) {
  106.       if( AIFF_seek_to_sound_data( fr_ps->out_file ) == -1 ) {
  107.      fclose( fr_ps->out_file );
  108.      fr_ps->out_file = NULL;
  109.      return 0;
  110.       }
  111.    }
  112.  
  113.    fr_ps->out_buffer_size = OUT_BUFFER_SIZE;
  114.  
  115.    if( (fr_ps->stereo > 1) && !(fr_ps->mono_forced) ) stereo_file = TRUE;
  116.    else stereo_file = FALSE;
  117.    if( fr_ps->output_8bits ) buffer_byte_size = fr_ps->out_buffer_size;
  118.    else buffer_byte_size = (fr_ps->out_buffer_size)*2;
  119.    fr_ps->out_buffer[ 0 ] = fr_ps->out_buffer[ 1 ] = NULL;
  120.    fr_ps->out_buffer[ 0 ] = (BYTE *)malloc( buffer_byte_size );
  121.    if( !fr_ps->out_buffer[ 0 ] ) exit( 0 );
  122.    if( stereo_file ) {
  123.       fr_ps->out_buffer[ 1 ] = (BYTE *)malloc( buffer_byte_size );
  124.       if( !fr_ps->out_buffer[ 1 ] ) exit( 0 );
  125.       stereo_file_buffer = (BYTE *)malloc( buffer_byte_size * 2 );
  126.       if( !stereo_file_buffer ) exit( 0 );
  127.    }
  128.    l_file_buffer = fr_ps->out_buffer[ 0 ];
  129.    r_file_buffer = fr_ps->out_buffer[ 1 ];
  130.    return 1;
  131. }
  132.  
  133. static int write_file( frame_params *fr_ps )
  134. /*
  135.    WARNING length is not byte length, it's sample length (8 or 16 bits)
  136. */
  137. {
  138.    long buffer_byte_size;
  139.    int write_length;
  140.    register int i;
  141.  
  142.    if( fr_ps->out_write_length > 0 ) {
  143.       if( fr_ps->output_8bits ) buffer_byte_size = fr_ps->out_write_length;
  144.       else buffer_byte_size = fr_ps->out_write_length * 2;
  145.       if( stereo_file ) {
  146.      if( fr_ps->output_8bits ) {
  147.         BYTE *l_sample, *r_sample, *buffer;
  148.  
  149.         buffer = stereo_file_buffer;
  150.         l_sample = l_file_buffer;
  151.         r_sample = r_file_buffer;
  152.         for( i=0; i<fr_ps->out_write_length; i++ ) {
  153.            *buffer++ = *l_sample++;
  154.            *buffer++ = *r_sample++;
  155.         }
  156.      }
  157.      else {
  158.         WORD *l_sample, *r_sample, *buffer;
  159.  
  160.         buffer = (WORD *)stereo_file_buffer;
  161.         l_sample = (WORD *)l_file_buffer;
  162.         r_sample = (WORD *)r_file_buffer;
  163.         for( i=0; i<fr_ps->out_write_length; i++ ) {
  164.            *buffer++ = *l_sample++;
  165.            *buffer++ = *r_sample++;
  166.         }
  167.      }
  168.      write_length = fwrite( stereo_file_buffer, buffer_byte_size*2, 1, fr_ps->out_file );
  169.       }
  170.       else {
  171.      write_length = fwrite( l_file_buffer, buffer_byte_size, 1, fr_ps->out_file );
  172.       }
  173.       if( !fr_ps->output_8bits ) write_length <<= 1;
  174.    }
  175.    else {
  176.       write_length = 0;
  177.       if ( fr_ps->out_file_type == MPEGA_FILETYPE_AIFF ) {
  178.      IFF_AIFF pcm_aiff_data;
  179.  
  180.      pcm_aiff_data.numChannels     = fr_ps->stereo;
  181.      pcm_aiff_data.numSampleFrames     = fr_ps->out_sample_length;
  182.      if( fr_ps->output_8bits ) pcm_aiff_data.sampleSize = 8;
  183.      else pcm_aiff_data.sampleSize     = 16;
  184.      pcm_aiff_data.sampleRate     = fr_ps->out_sample_freq;
  185.      pcm_aiff_data.sampleType     = IFF_ID_SSND;
  186.      pcm_aiff_data.blkAlgn.offset     = 0;
  187.      pcm_aiff_data.blkAlgn.blockSize = 0;
  188.  
  189.      if( AIFF_write_headers( fr_ps->out_file, &pcm_aiff_data ) == -1 ) {
  190.         printf( "Could not write AIFF headers to \"%s\"\n", fr_ps->out_file_name );
  191.      }
  192.       }
  193.       fclose( fr_ps->out_file );
  194.       fr_ps->out_file = NULL;
  195.    }
  196.    fr_ps->out_buffer[ 0 ] = l_file_buffer;
  197.    fr_ps->out_buffer[ 1 ] = r_file_buffer;
  198.    fr_ps->out_write_length = 0;
  199.  
  200.    return write_length;
  201. }
  202.  
  203. static int init_audio( frame_params *fr_ps )
  204. {
  205.    BYTE flags = 0;
  206.  
  207.    if( fr_ps->audio_port ) return;
  208.  
  209.    fr_ps->out_buffer_size = fr_ps->out_sample_freq * 2;    /* 2 secs buffer */
  210.    if( fr_ps->out_buffer_size >= 0x20000L ) {
  211.       fr_ps->out_buffer_size = 0x1FFFEL;
  212.    }
  213.    if( !fr_ps->output_8bits ) {
  214.       flags |= AUF_16BITS;
  215.    }
  216.    if( (fr_ps->stereo > 1) && !(fr_ps->mono_forced) ) flags |= AUF_STEREO;
  217.  
  218.    fr_ps->audio_port = AU_open( flags, fr_ps->out_buffer_size );
  219.  
  220.    if( !fr_ps->audio_port ) {
  221.       fprintf( stderr, "Can't open audio port !\n" );
  222.       exit( 0 );
  223.    }
  224.    fr_ps->audio_port->frequency = fr_ps->out_sample_freq;
  225.    fprintf( stderr, "Audio output " );
  226.    if( flags & AUF_16BITS ) fprintf( stderr, "16-bit " );
  227.    else fprintf( stderr, "8-bit " );
  228.    if( flags & AUF_STEREO ) fprintf( stderr, "stereo " );
  229.    else fprintf( stderr, "mono " );
  230.    fprintf( stderr, "%ldHz ", fr_ps->audio_port->frequency );
  231.  
  232.    fr_ps->audio_port->l_vol = fr_ps->audio_port->r_vol = 64;
  233.    fr_ps->audio_port->filter_on = fr_ps->audio_filter;
  234.    fr_ps->audio_port->flags = AUF_FILTER | AUF_FREQ | AUF_VOL;
  235.    fr_ps->audio_port->mixing_frequency = fr_ps->mixing_frequency;
  236.    if( fr_ps->mixing_frequency > 0 ) {
  237.       fr_ps->audio_port->flags |= AUF_MIXING;
  238.       fprintf( stderr, "mixing at %ldHz\n", fr_ps->mixing_frequency );
  239.    }
  240.    else fprintf( stderr, "\n" );
  241.    fr_ps->audio_port->command = AUC_CONTROL;
  242.    AU_write( fr_ps->audio_port );
  243.    fr_ps->out_buffer[ 0 ] = (BYTE *)fr_ps->audio_port->l_wave;
  244.    fr_ps->out_buffer[ 1 ] = (BYTE *)fr_ps->audio_port->r_wave;
  245.    return 1;
  246. }
  247.  
  248. static int write_audio( frame_params *fr_ps )
  249. {
  250.    int write_length = 0;
  251.  
  252.    if( !fr_ps->audio_port ) return 0;
  253.    if( fr_ps->out_write_length >= 0 ) {
  254.       fr_ps->audio_port->flags &= ~(AUF_FILTER|AUF_FREQ|AUF_VOL);
  255.       if( fr_ps->no_audio_wait ) fr_ps->audio_port->flags |= AUF_NOWAIT;
  256.       fr_ps->audio_port->command = AUC_WRITE;
  257.       fr_ps->audio_port->wave_length = fr_ps->out_write_length;
  258.       AU_write( fr_ps->audio_port );
  259.       fr_ps->out_buffer[ 0 ] = (BYTE *)fr_ps->audio_port->l_wave;
  260.       fr_ps->out_buffer[ 1 ] = (BYTE *)fr_ps->audio_port->r_wave;
  261.       write_length = fr_ps->out_write_length;
  262.    }
  263.  
  264.    fr_ps->out_write_length = 0;
  265.    return write_length;
  266. }
  267.  
  268. static int MPEGA_init_out( frame_params *fr_ps )
  269. {
  270.    if( fr_ps->play ) return init_audio( fr_ps );
  271.    else          return init_file( fr_ps );
  272. }
  273.  
  274. static void MPEGA_read_CRC( frame_params *fr_ps )
  275. {
  276.    int old_crc;
  277.  
  278.    old_crc = BSTR_read_bits( fr_ps->bs, 16);
  279. }
  280.  
  281. int MPEGA_seek_header( frame_params *fr_ps )
  282. /*------------------------------------------
  283.    Seek, read and decode MPEG audio header
  284. */
  285. {
  286.    if( BSTR_end( fr_ps->bs ) ) return 0;
  287.    if( !BSTR_seek_sync( fr_ps->bs, MPEGA_SYNC_WORD, MPEGA_SYNC_WORD_LNGTH ) ) return 0;
  288.    if( !ASM_read_header( fr_ps ) ) return 0;
  289.    ASM_decode_header( fr_ps );
  290.    if( fr_ps->header->error_protection ) MPEGA_read_CRC( fr_ps );
  291.    return 1;
  292. }
  293.  
  294. int MPEGA_init_decode( frame_params *fr_ps )
  295. /*------------------------------------------
  296.    Initialize MPEG Audio decoding (analys of first frame to setup buffers)
  297. */
  298. {
  299.    int scale_size;
  300.  
  301.    if( fr_ps->freq_div <= 0 ) fr_ps->freq_div = 1;
  302.    fr_ps->sub_band_size = SBLIMIT / fr_ps->freq_div;
  303.    ASM_init_decode( fr_ps->sub_band_size, fr_ps->output_8bits, fr_ps->quality );
  304.  
  305.    if( !fr_ps->bs ) {
  306.       fr_ps->bs = BSTR_open( fr_ps->bitstream_name,
  307.                  fr_ps->bitstream_buffer_size, BSTR_MODE_READ );
  308.       if( !fr_ps->bs ) {
  309.      fprintf( stderr, "Can't open file '%s'\n", fr_ps->bitstream_name );
  310.      return 0;
  311.       }
  312.    }
  313.    if( !MPEGA_seek_header( fr_ps ) ) return 0;
  314.    /* Seek to start of bitstream */
  315.    if( !BSTR_rewind( fr_ps->bs ) ) return 0;
  316.  
  317.    fr_ps->out_sample_freq = fr_ps->bitstream_freq / fr_ps->freq_div;
  318.    if( !MPEGA_init_out( fr_ps ) ) return 0;      /* Initialize on first frame */
  319.    if( fr_ps->header->lay == 1 ) scale_size = SCALE_BLOCK;
  320.    else if ( fr_ps->header->lay == 2 ) scale_size = 3 * SCALE_BLOCK;
  321.    else return 0;
  322.  
  323.    fr_ps->out_write_level = fr_ps->out_buffer_size - scale_size * fr_ps->sub_band_size;
  324.    fr_ps->got_bits = 0;
  325.    fr_ps->frame_count = 0;
  326.    fr_ps->out_write_length = 0;
  327.    fr_ps->out_sample_length = 0;
  328.  
  329.    return 1;
  330. }
  331.  
  332.  
  333.  
  334. int MPEGA_write_out( frame_params *fr_ps )
  335. /*----------------------------------------
  336.    Write out_buffer to output media
  337.    -> return written sample count
  338. */
  339. {
  340.    if( fr_ps->play ) return write_audio( fr_ps );
  341.    else          return write_file( fr_ps );
  342. }
  343.  
  344. int MPEGA_decode_frame( frame_params *fr_ps )
  345. /*-------------------------------------------
  346.    Decode one MPEG Audio frame
  347.    -> return 0 if frame can't be decoded or bitstream empty
  348.    -> return out sample length decoded otherwise
  349.      !!! length is not in byte, but in sample count (8 or 16 bits wide)
  350. */
  351. {
  352.    int sample_frame_length = 0;
  353.  
  354.    if( !MPEGA_seek_header( fr_ps ) ) return 0;
  355.  
  356.    switch( fr_ps->header->lay ) {
  357.       case 1: {
  358.      ASM_I_decode_bitalloc( fr_ps );
  359.      ASM_I_decode_scale( fr_ps );
  360.      if( fr_ps->mono_forced ) fr_ps->stereo = 1;
  361.      ASM_I_decode_frame( fr_ps );
  362.      sample_frame_length = fr_ps->sub_band_size * SCALE_BLOCK;
  363.      break;
  364.       }
  365.  
  366.       case 2: {
  367.      ASM_II_decode_bitalloc( fr_ps );
  368.      ASM_II_decode_scale( fr_ps );
  369.      if( fr_ps->mono_forced ) fr_ps->stereo = 1;
  370.      ASM_II_decode_frame( fr_ps );
  371.      sample_frame_length = fr_ps->sub_band_size * 3 * SCALE_BLOCK;
  372.      break;
  373.       }
  374.    }
  375.    fr_ps->frame_count++;
  376.    fr_ps->got_bits = BSTR_seek_tell( fr_ps->bs );
  377.    fr_ps->out_write_length += sample_frame_length;
  378.    fr_ps->out_sample_length += sample_frame_length;
  379.  
  380.    return sample_frame_length;
  381. }
  382.