Changeset 480


Ignore:
Timestamp:
03/31/12 00:52:54 (12 years ago)
Author:
r2d
Message:

add raw output to mpcdec, not really tested

File:
1 edited

Legend:

Unmodified
Added
Removed
  • libmpc/trunk/mpcdec/mpcdec.c

    r444 r480  
    4040#include <libwaveformat.h>
    4141#include <getopt.h>
     42#include <math.h>
     43#include <byteswap.h>
    4244
    4345#ifdef _MSC_VER
     
    7577    return (t_wav_uint32) !fseek(p_handle, p_position, SEEK_SET);
    7678}
     79
     80#ifdef MPC_FIXED_POINT
     81
     82static int raw_output_int16(FILE * file, short * buff, int cnt, mpc_bool_t reverse_endian)
     83{
     84        int i;
     85        for (i = 0; i < cnt; i++) {
     86                short out = buff[i];
     87                if (reverse_endian)
     88                        out = bswap_16(out);
     89                if (fwrite(&out, sizeof(out), 1, file) != 1)
     90                        return -1;
     91        }
     92        return 0;
     93}
     94
     95#else
     96
     97static int raw_output_float32(FILE * file, float * buff, int cnt, mpc_bool_t reverse_endian)
     98{
     99        int i;
     100        for (i = 0; i < cnt; i++) {
     101                int tmp = nearbyintf(buff[i] * (1 << 15));
     102                short out;
     103                if (tmp > ((1 << 15) - 1))
     104                        tmp = ((1 << 15) - 1);
     105                if (tmp < -(1 << 15))
     106                        tmp = -(1 << 15);
     107                if (reverse_endian)
     108                        tmp = bswap_16((short) tmp);
     109                out = (short)tmp;
     110                if (fwrite(&out, sizeof(out), 1, file) != 1)
     111                        return -1;
     112        }
     113        return 0;
     114}
     115
     116#endif
    77117
    78118static void print_info(mpc_streaminfo * info, char * filename)
     
    109149                        "-c : check the file for stream errors\n"
    110150                        "     (doesn't fully decode, outfile will be ignored)\n"
     151                        "-r : output raw data (left/right) in machine native endian\n"
     152                        "-e : reverse raw data endianness\n"
    111153                        "-h : print this help\n"
    112154            "you can use stdin and stdout as resp. <infile.mpc> and\n"
     
    122164        mpc_status err;
    123165        mpc_bool_t info = MPC_FALSE, is_wav_output = MPC_FALSE, check = MPC_FALSE;
     166        mpc_bool_t is_raw_output = MPC_FALSE, reverse_endian = MPC_FALSE;
    124167    MPC_SAMPLE_FORMAT sample_buffer[MPC_DECODER_BUFFER_LENGTH];
    125     clock_t begin, end, sum; int total_samples; t_wav_output_file wav_output;
     168    clock_t begin, end, sum; int total_samples;
     169        t_wav_output_file wav_output;
     170        t_wav_output_file_callback wavo_fc;
    126171        int c;
    127172
    128173    fprintf(stderr, About);
    129174
    130         while ((c = getopt(argc , argv, "ihc")) != -1) {
     175        while ((c = getopt(argc , argv, "ihcre")) != -1) {
    131176                switch (c) {
    132177                        case 'i':
     
    136181                                check = MPC_TRUE;
    137182                                break;
     183                        case 'r':
     184                                is_raw_output = MPC_TRUE;
     185                                break;
     186                        case 'e':
     187                                reverse_endian = MPC_TRUE;
     188                                break;
    138189                        case 'h':
    139190                                usage(argv[0]);
     
    166217        }
    167218
    168         if (!check)
    169                 is_wav_output = argc - optind > 1;
    170     if(is_wav_output)
     219        if (check) {
     220                is_raw_output = MPC_FALSE;
     221        } else if (argc - optind > 1 && is_raw_output == MPC_FALSE) {
     222                is_wav_output = MPC_TRUE;
     223        };
     224       
     225    if (is_wav_output || is_raw_output)
    171226    {
    172         t_wav_output_file_callback wavo_fc;
    173227        memset(&wav_output, 0, sizeof wav_output);
    174228        wavo_fc.m_seek      = mpc_wav_output_seek;
    175229        wavo_fc.m_write     = mpc_wav_output_write;
    176                 if (strcmp(argv[optind + 1], "-") == 0) {
     230                if (strcmp(argv[optind + 1], "-") == 0 || (is_raw_output && argc - optind <= 1)) {
    177231                        SET_BINARY_MODE(stdout);
    178232                    wavo_fc.m_user_data = stdout;
    179233                } else
    180234                        wavo_fc.m_user_data = fopen(argv[optind + 1], "wb");
    181         if(!wavo_fc.m_user_data) return !MPC_STATUS_OK;
    182         err = waveformat_output_open(&wav_output, wavo_fc, si.channels, 16, 0, si.sample_freq, (t_wav_uint32) si.samples * si.channels);
    183         if(!err) return !MPC_STATUS_OK;
     235        if(!wavo_fc.m_user_data)
     236                        return !MPC_STATUS_OK;
     237               
     238                if (is_wav_output) {
     239                        if (!waveformat_output_open(&wav_output, wavo_fc, si.channels, 16, 0, si.sample_freq, (t_wav_uint32) si.samples * si.channels))
     240                                return !MPC_STATUS_OK;
     241                }
    184242    }
    185243
     
    200258        sum           += end - begin;
    201259
    202                 if(is_wav_output) {
     260                if (is_wav_output || is_raw_output) {
    203261#ifdef MPC_FIXED_POINT
    204262                        mpc_int16_t tmp_buff[MPC_DECODER_BUFFER_LENGTH];
     
    210268                                tmp_buff[i] = tmp;
    211269                        }
    212                         if(waveformat_output_process_int16(&wav_output, tmp_buff, frame.samples * si.channels) < 0)
     270#endif
     271                        if (is_wav_output) {
     272#ifdef MPC_FIXED_POINT
     273                                if(waveformat_output_process_int16(&wav_output, tmp_buff, frame.samples * si.channels) < 0)
    213274#else
    214                         if(waveformat_output_process_float32(&wav_output, sample_buffer, frame.samples * si.channels) < 0)
    215 #endif
    216                 break;
     275                                if(waveformat_output_process_float32(&wav_output, sample_buffer, frame.samples * si.channels) < 0)
     276#endif
     277                                        break;
     278                        }
     279                        if (is_raw_output) {
     280#ifdef MPC_FIXED_POINT
     281                                if (raw_output_int16(wavo_fc.m_user_data, tmp_buff, frame.samples * si.channels, reverse_endian) < 0)
     282#else
     283                                if (raw_output_float32(wavo_fc.m_user_data, sample_buffer, frame.samples * si.channels, reverse_endian) < 0)
     284#endif
     285                                        break;
     286                        }
    217287                }
    218288    }
Note: See TracChangeset for help on using the changeset viewer.