Ignore:
Timestamp:
10/06/06 18:51:07 (16 years ago)
Author:
zorg
Message:

Added primitive mpcdec

File:
1 edited

Legend:

Unmodified
Added
Removed
  • libmpcdec/branches/zorg/mpcdec/mpcdec.c

    r68 r70  
    3535#include <assert.h>
    3636#include <time.h>
     37#include <mpcdec/mpcdec.h>
    3738
    38 #include <mpcdec/mpcdec.h>
    39 #include <libwaveformat.h>
     39#ifdef WIN32
     40#include <crtdbg.h>
     41#endif
    4042
    4143static void
    4244usage(const char *exename)
    4345{
    44     printf
    45         ("Usage: %s <infile.mpc> [<outfile.wav>]\nIf <outfile.wav> is not specified, decoder will run in benchmark mode.\n",
    46          exename);
     46    printf("Usage: %s <infile.mpc>\n", exename);
    4747}
    4848
     
    5050main(int argc, char **argv)
    5151{
    52     mpc_reader reader; mpc_streaminfo si; mpc_decoder decoder; wavwriter
     52    mpc_reader reader; mpc_streaminfo si; mpc_decoder* decoder; mpc_status err;
     53    MPC_SAMPLE_FORMAT sample_buffer[MPC_DECODER_BUFFER_LENGTH];
     54    clock_t begin, end, sum; int total_samples;
    5355
    54     if (argc != 2 && argc != 3) {
    55         if (argc > 0)
    56             usage(argv[0]);
     56    if(argc != 2)
     57    {
     58        usage(argv[0]);
    5759        return 0;
    5860    }
    5961
     62    err = mpc_reader_init_stdio(&reader, argv[1]);
     63    if(err < 0) return !MPC_STATUS_OK;
    6064
    61     FILE *input = fopen(argv[1], "rb");
    62     FILE *output = 0;
    63     if (input == 0) {
    64         usage(argv[0]);
    65         printf("Error opening input file: \"%s\"\n", argv[1]);
    66         return 1;
     65    err = mpc_streaminfo_init(&si, &reader);
     66    if(err < 0) return !MPC_STATUS_OK;
     67
     68    err = mpc_decoder_init(&decoder, &reader, &si);
     69    if(err < 0) return !MPC_STATUS_OK;
     70
     71    sum = total_samples = 0;
     72    while(TRUE)
     73    {
     74        mpc_uint32_t samples;
     75        begin = clock();
     76        samples = mpc_decoder_decode(decoder, sample_buffer, 0, 0);
     77        end = clock();
     78        if(samples <= 0) break;
     79        if(!samples) break;
     80        total_samples += samples;
     81        sum           += end - begin;
    6782    }
    6883
    69     if (argc == 3) {
    70         output = fopen(argv[2], "wb");
    71         if (output == 0) {
    72             fclose(input);
    73             usage(argv[0]);
    74             printf("Error opening output file: \"%s\"\n", argv[2]);
    75             return 1;
    76         }
    77     }
     84    printf("%u samples ", total_samples);
     85    total_samples = (mpc_uint32_t) ((double) total_samples * CLOCKS_PER_SEC * 100 / (si.sample_freq * sum));
     86    printf("decoded in %u ms (%u.%02ux)\n",
     87           sum * 1000 / CLOCKS_PER_SEC,
     88           total_samples / 100,
     89           total_samples % 100
     90           );
    7891
    79     /* initialize our reader_data tag the reader will carry around with it */
    80     reader_data data;
    81     data.file = input;
    82     data.seekable = true;
    83     fseek(data.file, 0, SEEK_END);
    84     data.size = ftell(data.file);
    85     fseek(data.file, 0, SEEK_SET);
     92    mpc_decoder_exit(decoder);
     93    mpc_reader_exit_stdio(&reader);
    8694
    87     /* set up an mpc_reader linked to our function implementations */
    88     mpc_decoder decoder;
    89     mpc_reader reader;
    90     reader.read = read_impl;
    91     reader.seek = seek_impl;
    92     reader.tell = tell_impl;
    93     reader.get_size = get_size_impl;
    94     reader.canseek = canseek_impl;
    95     reader.data = &data;
    96 
    97     /* read file's streaminfo data */
    98     mpc_streaminfo info;
    99     mpc_streaminfo_init(&info);
    100     if (mpc_streaminfo_read(&info, &reader) != ERROR_CODE_OK) {
    101         printf("Not a valid musepack file: \"%s\"\n", argv[1]);
    102         return 1;
    103     }
    104 
    105     /* instantiate a decoder with our file reader */
    106     mpc_decoder_setup(&decoder, &reader);
    107     if (!mpc_decoder_initialize(&decoder, &info)) {
    108         printf("Error initializing decoder.\n", argv[1]);
    109         return 1;
    110     }
    111 
    112     /* decode the file */
    113     printf("Decoding from:\n%s\nTo:\n%s\n", argv[1],
    114            output ? argv[2] : "N/A");
    115     WavWriter *wavwriter =
    116         output ? new WavWriter(output, 2, 16, info.sample_freq) : 0;
    117     MPC_SAMPLE_FORMAT sample_buffer[MPC_DECODER_BUFFER_LENGTH];
    118     clock_t begin, end;
    119     begin = clock();
    120     unsigned total_samples = 0;
    121     mpc_bool_t successful = FALSE;
    122     for (;;) {
    123         unsigned status = mpc_decoder_decode(&decoder, sample_buffer, 0, 0);
    124         if (status == (unsigned)(-1)) {
    125             //decode error
    126             printf("Error decoding file.\n");
    127             break;
    128         }
    129         else if (status == 0)   //EOF
    130         {
    131             successful = true;
    132             break;
    133         }
    134         else                    //status>0
    135         {
    136             total_samples += status;
    137             if (wavwriter) {
    138                 if (!wavwriter->
    139                     WriteSamples(sample_buffer, status * /* stereo */ 2)) {
    140                     printf("Write error.\n");
    141                     break;
    142                 }
    143             }
    144         }
    145     }
    146 
    147     end = clock();
    148 
    149     if (wavwriter) {
    150         delete wavwriter;
    151     }
    152 
    153     if (successful) {
    154         printf("\nFinished.\nTotal samples decoded: %u.\n", total_samples);
    155         unsigned ms = (end - begin) * 1000 / CLOCKS_PER_SEC;
    156         unsigned ratio =
    157             (unsigned)((double)total_samples / (double)info.sample_freq /
    158                        ((double)ms / 1000.0) * 100.0);
    159         printf("Time: %u ms (%u.%02ux).\n", ms, ratio / 100, ratio % 100);
    160     }
    161 
     95#ifdef WIN32
     96    assert(_CrtCheckMemory());
     97    _CrtDumpMemoryLeaks();
     98#endif
    16299    return 0;
    163100}
Note: See TracChangeset for help on using the changeset viewer.