home *** CD-ROM | disk | FTP | other *** search
/ The Best of Mecomp Multimedia 2 / MECOMP-CD-II.iso / amiga / tools / libs / mpega_library / developer / demo / mpega_demo.c next >
Encoding:
C/C++ Source or Header  |  1998-06-02  |  9.4 KB  |  323 lines

  1. /*------------------------------------------------------------------------------
  2.  
  3.     File    :   MPEGA_demo.c
  4.  
  5.     Author  :   Stéphane TAVENARD
  6.  
  7.     $VER:   MPEGA_demo.c 1.1  (02/05/1998)
  8.  
  9.     (C) Copyright 1997-1997 Stéphane TAVENARD
  10.         All Rights Reserved
  11.  
  12.     #Rev|   Date   |                      Comment
  13.     ----|----------|--------------------------------------------------------
  14.     0   |25/10/1997| Initial revision                                     ST
  15.     1   |02/05/1998| Added some time features                             ST
  16.  
  17.     ------------------------------------------------------------------------
  18.  
  19.     Demo of how to use MPEGA library
  20.     Use of private bitstream access functions (access to ram buffer)
  21.  
  22. ------------------------------------------------------------------------------*/
  23.  
  24. #include <exec/exec.h>
  25. #include <clib/exec_protos.h>
  26. #include <pragmas/exec_pragmas.h>
  27. #include <dos.h>
  28.  
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <time.h>
  33.  
  34. #include <libraries/mpega.h>
  35. #include <clib/mpega_protos.h>
  36. #include <pragmas/mpega_pragmas.h>
  37.  
  38. static char *Version = "$VER:MPEGA_demo 1.1 (02.05.98) (C)1997-1998 Stéphane TAVENARD";
  39.  
  40. struct Library *MPEGABase = NULL;
  41.  
  42. char *mpega_name = "mpega.library";
  43.  
  44. MPEGA_STREAM *mps = NULL;
  45.  
  46. // Ram buffer
  47. #define MPEGA_BUFFER_SIZE  (256*1024) // in bytes
  48.  
  49. BYTE   *mpega_buffer = NULL;
  50. ULONG  mpega_buffer_offset = 0;
  51. ULONG  mpega_buffer_size = 0;
  52.  
  53. static int break_cleanup( void )
  54. {
  55.    if( mps ) {
  56.       MPEGA_close( mps );
  57.       mps = NULL;
  58.    }
  59.    if( MPEGABase ) {
  60.       CloseLibrary( MPEGABase );
  61.       MPEGABase = NULL;
  62.    }
  63.    return 1;
  64. }
  65.  
  66. static void exit_cleanup( void )
  67. {
  68.    (void)break_cleanup();
  69. }
  70.  
  71. // Here start our own bitstream access routines
  72.  
  73. static ULONG __saveds __asm def_baccess( register __a0 struct Hook  *hook,
  74.                                          register __a2 APTR          handle,
  75.                                          register __a1 MPEGA_ACCESS *access ) {
  76. /*----------------------------------------------------------------------------
  77. */
  78.  
  79.    switch( access->func ) {
  80.  
  81.       case MPEGA_BSFUNC_OPEN:
  82.          // We don't really need stream_name
  83.          // buffer_size indicate the following read access size
  84.  
  85. printf( "bitstream open: filename='%s'\n", access->data.open.stream_name );
  86. printf( "bitstream open: buffer_size=%d\n", access->data.open.buffer_size );
  87.  
  88.          // Some open errors...
  89.          if( !mpega_buffer ) return NULL;
  90.  
  91.          // initialize some variables
  92.          mpega_buffer_offset = 0;
  93.  
  94.          // We know total size, we can set it
  95.          access->data.open.stream_size = mpega_buffer_size;
  96.  
  97.          // Just return a dummy handle (not NULL)
  98.          return 1;
  99.  
  100.       case MPEGA_BSFUNC_CLOSE:
  101.          if( handle ) {
  102.             // Clean up
  103. printf( "bitstream close\n" );
  104.          }
  105.          break;
  106.       case MPEGA_BSFUNC_READ: {
  107.          LONG read_size;
  108.  
  109.          if( !handle ) return 0; // Check valid handle
  110.  
  111.          read_size = mpega_buffer_size - mpega_buffer_offset;
  112.          if( read_size > access->data.read.num_bytes ) read_size = access->data.read.num_bytes;
  113.  
  114.          if( read_size > 0 ) {
  115.             if( !access->data.read.buffer ) return 0;
  116.             // Fill buffer with our MPEG audio data
  117.             memcpy( access->data.read.buffer, &mpega_buffer[ mpega_buffer_offset ], read_size );
  118.             mpega_buffer_offset += read_size;
  119.          }
  120.          else {
  121.             read_size = 0; // End of stream
  122.          }
  123. //printf( "bitstream read: requested %d bytes, read %d\n", access->data.read.num_bytes, read_size );
  124.  
  125.          return (ULONG)read_size;
  126.       }
  127.       case MPEGA_BSFUNC_SEEK:
  128.          if( !handle ) return 0;
  129.  
  130. printf( "bitstream seek: pos = %d\n", access->data.seek.abs_byte_seek_pos );
  131.          if( access->data.seek.abs_byte_seek_pos <= 0 ) mpega_buffer_offset = 0;
  132.          else if( access->data.seek.abs_byte_seek_pos >= mpega_buffer_size ) return 1;
  133.          else mpega_buffer_offset = access->data.seek.abs_byte_seek_pos;
  134.          return 0;
  135.    }
  136.    return 0;
  137. }
  138.  
  139. static struct Hook def_bsaccess_hook = {
  140.    { NULL, NULL }, def_baccess, NULL, NULL
  141. };
  142.  
  143. int output_pcm( WORD channels, WORD *pcm[ 2 ], LONG count, FILE *out_file )
  144. /*---------------------------------------------------------------------------
  145.    Ouput the current decoded PCM to a file
  146.    Return 0 if Ok
  147. */
  148. {
  149. #define PCM_BUFFER_SIZE (MPEGA_MAX_CHANNELS*MPEGA_PCM_SIZE)
  150.    static WORD *pcm_buffer = NULL;
  151.    if( !out_file ) return -1;
  152.  
  153.    if( !pcm_buffer ) {
  154.       pcm_buffer = (WORD *)malloc( PCM_BUFFER_SIZE * sizeof(WORD) );
  155.       if( !pcm_buffer ) return -1;
  156.    }
  157.    if( channels == 2 ) {
  158.       register WORD *pcm0, *pcm1, *pcmLR;
  159.       register LONG i;
  160.  
  161.       pcm0 = pcm[ 0 ];
  162.       pcm1 = pcm[ 1 ];
  163.       pcmLR = pcm_buffer;
  164.       i = count;
  165.       while( i-- ) {
  166.          *pcmLR++ = *pcm0++;
  167.          *pcmLR++ = *pcm1++;
  168.       }
  169.       fwrite( pcm_buffer, 4, count, out_file );
  170.    }
  171.    else {
  172.       fwrite( pcm[ 0 ], 2, count, out_file );
  173.    }
  174.  
  175.    return 0;
  176.  
  177. } /* output_pcm */
  178.  
  179. int main( int argc, char **argv )
  180. {
  181.    char *in_filename;
  182.    FILE *in_file;
  183.    int frame = 0;
  184.    char *out_filename = NULL;
  185.    FILE *out_file = NULL;
  186.    WORD i;
  187.    LONG pcm_count, total_pcm = 0;
  188.    LONG index;
  189.    WORD *pcm[ MPEGA_MAX_CHANNELS ];
  190.    clock_t clk; // #1
  191.    double secs; // #1
  192. //ULONG ms;
  193.  
  194.    static const char *layer_name[] = { "?", "I", "II", "III" };
  195.    static const char *mode_name[] = { "stereo", "j-stereo", "dual", "mono" };
  196.  
  197.    MPEGA_CTRL mpa_ctrl = {
  198.       NULL,    // Bitstream access is default file I/O
  199.       // Layers I & II settings (mono, stereo)
  200.       { FALSE, { 1, 2, 48000 }, { 1, 2, 48000 } },
  201.       // Layer III settings (mono, stereo)
  202.       { FALSE, { 1, 2, 48000 }, { 1, 2, 48000 } },
  203.       0,           // Don't check mpeg validity at start (needed for mux stream)
  204.       2048         // Stream Buffer size
  205.    };
  206.  
  207.  
  208. //printf( "MPEGA_demo: sizeof( MPEGA_CTRL ) = %d\n", sizeof( MPEGA_CTRL ) );
  209. //printf( "MPEGA_demo: sizeof( MPEGA_STREAM ) = %d\n", sizeof( MPEGA_STREAM ) );
  210.  
  211.    onbreak( break_cleanup );
  212.    atexit( exit_cleanup );
  213.  
  214.    if( argc <= 1 ) {
  215.       fprintf( stderr, "%s\n", &Version[ 5 ] );
  216.       fprintf( stderr, "Usage %s <input mpeg audio file> [<output pcm file>]\n", argv[ 0 ] );
  217.       fprintf( stderr, "This is a demo of how to use mpega.library\n" );
  218.       fprintf( stderr, "This also show how to use custom bitstream access\n" );
  219.       exit( 0 );
  220.    }
  221.  
  222.    MPEGABase = OpenLibrary( mpega_name, 0L );
  223.    if( !MPEGABase ) {
  224.       printf( "Unable to open '%s'\n", mpega_name );
  225.       exit( 0 );
  226.    }
  227.  
  228.    in_filename = argv[ 1 ];
  229.    if( argc > 2 ) out_filename = argv[ 2 ];
  230.  
  231.    mpega_buffer = (BYTE *)malloc( MPEGA_BUFFER_SIZE );
  232.    if( !mpega_buffer ) {
  233.       fprintf( stderr, "Can't allocate MPEG buffer\n" );
  234.       exit( 0 );
  235.    }
  236.  
  237.    for( i=0; i<MPEGA_MAX_CHANNELS; i++ ) {
  238.       pcm[ i ] = malloc( MPEGA_PCM_SIZE * sizeof( WORD ) );
  239.       if( !pcm[ i ] ) {
  240.          fprintf( stderr, "Can't allocate PCM buffers\n" );
  241.          exit( 0 );
  242.       }
  243.    }
  244.  
  245.    // Open the output file
  246.    if( out_filename ) {
  247.       out_file = fopen( out_filename, "wb" );
  248.       if( !out_file ) {
  249.          fprintf( stderr, "Can't create output file '%s'\n", out_filename );
  250.          exit( 0 );
  251.       }
  252.    }
  253.  
  254.    // Load the stream into a ram buffer
  255.    in_file = fopen( in_filename, "rb" );
  256.    if( !in_file ) {
  257.       fprintf( stderr, "Unable to open file '%s'\n", in_filename );
  258.       exit( 0 );
  259.    }
  260.    mpega_buffer_size = fread( mpega_buffer, 1, MPEGA_BUFFER_SIZE, in_file );
  261.    fclose( in_file );
  262.    printf( "Read %d bytes from file '%s'\n", mpega_buffer_size, in_filename );
  263.  
  264.    // #1 Begin
  265.    printf( "Test if MPEG Audio sync inside...\n" );
  266.    index = MPEGA_find_sync( mpega_buffer, MPEGA_BUFFER_SIZE );
  267.    if( index >= 0 ) {
  268.       printf( "Ok, found MPEG Audio sync at position %d\n", index );
  269.    }
  270.    else {
  271.       printf( "* Error %d *\n", index );
  272.    }
  273.    // #1 End
  274.  
  275.    // Set our bitstream access routines and open the stream
  276.    mpa_ctrl.bs_access = &def_bsaccess_hook;
  277.  
  278.    mps = MPEGA_open( in_filename, &mpa_ctrl );
  279.    if( !mps ) {
  280.       printf( "Unable to find MPEG Audio stream in file '%s'\n", in_filename );
  281.       exit( 0 );
  282.    }
  283.  
  284.    printf( "\n" );
  285.    printf( "MPEG norm %d Layer %s\n", mps->norm, layer_name[ mps->layer ] );
  286.    printf( "Bitrate: %d kbps\n", mps->bitrate );
  287.    printf( "Frequency: %d Hz\n", mps->frequency );
  288.    printf( "Mode: %d (%s)\n", mps->mode, mode_name[ mps->mode ] );
  289.    printf( "Stream duration: %ld ms\n", mps->ms_duration );
  290.    printf( "\n" );
  291.    printf( "Output decoding parameters\n" );
  292.    printf( "Channels: %d\n", mps->dec_channels );
  293.    printf( "Quality: %d\n", mps->dec_quality );
  294.    printf( "Frequency: %d Hz\n", mps->dec_frequency );
  295.    printf( "\n" );
  296.  
  297.    clk = clock(); // #1
  298.    while( (pcm_count = MPEGA_decode_frame( mps, pcm )) >= 0 ) {
  299. //MPEGA_time( mps, &ms );
  300.       total_pcm += pcm_count;
  301.       if( out_file ) output_pcm( mps->dec_channels, pcm, pcm_count, out_file );
  302.       frame++;
  303.       if( (frame & 31) == 0 ) fprintf( stderr, "{%04d}\r", frame ); fflush( stderr );
  304.    }
  305.    clk = clock() - clk; // #1
  306.    secs = (double)clk / (double)CLK_TCK; // #1
  307.    printf( "time used = %7.3f secs\n", secs ); // #1
  308.    printf( "%ld samples / sec\n", (int)((double)total_pcm / secs) );
  309.    printf( "%7.3f % CPU used on real time\n", ((double)mps->frequency * 100) / ((double)total_pcm / secs) );
  310.  
  311.    fprintf( stderr, "\n" );
  312.  
  313.    fprintf( stderr, "last pcm_count = %d\n", pcm_count );
  314.    fprintf( stderr, "total_pcm = %d\n", total_pcm );
  315.  
  316.    MPEGA_close( mps );
  317.    mps = NULL;
  318.    CloseLibrary( MPEGABase );
  319.    MPEGABase = NULL;
  320.  
  321. }
  322.  
  323.