Ignore:
Timestamp:
10/06/06 17:14:05 (18 years ago)
Author:
zorg
Message:

Separated public interface from private headers
Use opaque objects whenever possible
Some (useless?) cosmetics on libmpcdec
Remove sv5-6 outdated support
Added libwavformat for upcoming mpcdec
New layout
Work in progress...

Location:
libmpcdec/branches/zorg/mpcdec
Files:
1 added
1 moved

Legend:

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

    r63 r68  
    3232  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    3333*/
    34 
    3534#include <stdio.h>
    3635#include <assert.h>
     
    3837
    3938#include <mpcdec/mpcdec.h>
    40 
    41 /*
    42   The data bundle we pass around with our reader to store file
    43   position and size etc.
    44 */
    45 typedef struct reader_data_t {
    46     FILE *file;
    47     long size;
    48     mpc_bool_t seekable;
    49 } reader_data;
    50 
    51 /*
    52   Our implementations of the mpc_reader callback functions.
    53 */
    54 mpc_int32_t
    55 read_impl(void *data, void *ptr, mpc_int32_t size)
    56 {
    57     reader_data *d = (reader_data *) data;
    58     return fread(ptr, 1, size, d->file);
    59 }
    60 
    61 mpc_bool_t
    62 seek_impl(void *data, mpc_int32_t offset)
    63 {
    64     reader_data *d = (reader_data *) data;
    65     return d->seekable ? !fseek(d->file, offset, SEEK_SET) : false;
    66 }
    67 
    68 mpc_int32_t
    69 tell_impl(void *data)
    70 {
    71     reader_data *d = (reader_data *) data;
    72     return ftell(d->file);
    73 }
    74 
    75 mpc_int32_t
    76 get_size_impl(void *data)
    77 {
    78     reader_data *d = (reader_data *) data;
    79     return d->size;
    80 }
    81 
    82 mpc_bool_t
    83 canseek_impl(void *data)
    84 {
    85     reader_data *d = (reader_data *) data;
    86     return d->seekable;
    87 }
    88 
    89 #define WFX_SIZE (2+2+4+4+2+2)
    90 
    91 #ifdef MPC_FIXED_POINT
    92 static int
    93 shift_signed(MPC_SAMPLE_FORMAT val, int shift)
    94 {
    95     if (shift > 0)
    96         val <<= shift;
    97     else if (shift < 0)
    98         val >>= -shift;
    99     return (int)val;
    100 }
    101 #endif
    102 
    103 class WavWriter {
    104   public:
    105     WavWriter(FILE * p_output, unsigned p_nch, unsigned p_bps,
    106               unsigned p_srate)
    107     : m_file(p_output), m_nch(p_nch), m_bps(p_bps), m_srate(p_srate) {
    108         assert(m_bps == 16 || m_bps == 24);
    109 
    110         WriteRaw("RIFF", 4);
    111         WriteDword(0);          //fix this in destructor
    112 
    113         WriteRaw("WAVE", 4);
    114         WriteRaw("fmt ", 4);
    115         WriteDword(WFX_SIZE);
    116 
    117         WriteWord(1);           //WAVE_FORMAT_PCM
    118         WriteWord(m_nch);
    119         WriteDword(m_srate);
    120         WriteDword(m_srate * m_nch * (m_bps >> 3));
    121         WriteWord(m_nch * (m_bps >> 3));
    122         WriteWord(m_bps);
    123         /*
    124            WORD  wFormatTag;
    125            WORD  nChannels;
    126            DWORD nSamplesPerSec;
    127            DWORD nAvgBytesPerSec;
    128            WORD  nBlockAlign;
    129            WORD  wBitsPerSample;
    130          */
    131         WriteRaw("data", 4);
    132         WriteDword(0);          //fix this in destructor
    133 
    134         m_data_bytes_written = 0;
    135     } mpc_bool_t WriteSamples(const MPC_SAMPLE_FORMAT * p_buffer, unsigned p_size) {
    136         unsigned n;
    137         int clip_min = -1 << (m_bps - 1),
    138             clip_max = (1 << (m_bps - 1)) - 1, float_scale = 1 << (m_bps - 1);
    139         for (n = 0; n < p_size; n++) {
    140             int val;
    141 #ifdef MPC_FIXED_POINT
    142             val =
    143                 shift_signed(p_buffer[n],
    144                              m_bps - MPC_FIXED_POINT_SCALE_SHIFT);
    145 #else
    146             val = (int)(p_buffer[n] * float_scale);
    147 #endif
    148             if (val < clip_min)
    149                 val = clip_min;
    150             else if (val > clip_max)
    151                 val = clip_max;
    152             if (!WriteInt(val, m_bps))
    153                 return false;
    154         }
    155         m_data_bytes_written += p_size * (m_bps >> 3);
    156         return true;
    157     }
    158 
    159     ~WavWriter() {
    160         if (m_data_bytes_written & 1) {
    161             char blah = 0;
    162             WriteRaw(&blah, 1);
    163             m_data_bytes_written++;
    164         }
    165         Seek(4);
    166         WriteDword((unsigned long)(m_data_bytes_written + 4 + 8 + WFX_SIZE +
    167                                    8));
    168         Seek(8 + 4 + 8 + WFX_SIZE + 4);
    169         WriteDword(m_data_bytes_written);
    170     }
    171 
    172   private:
    173 
    174     mpc_bool_t Seek(unsigned p_offset) {
    175         return !fseek(m_file, p_offset, SEEK_SET);
    176     }
    177 
    178     mpc_bool_t WriteRaw(const void *p_buffer, unsigned p_bytes) {
    179         return fwrite(p_buffer, 1, p_bytes, m_file) == p_bytes;
    180     }
    181 
    182     mpc_bool_t WriteDword(unsigned long p_val) {
    183         return WriteInt(p_val, 32);
    184     }
    185     mpc_bool_t WriteWord(unsigned short p_val) {
    186         return WriteInt(p_val, 16);
    187     }
    188 
    189     // write a little-endian number properly
    190     mpc_bool_t WriteInt(unsigned int p_val, unsigned p_width_bits) {
    191         unsigned char temp;
    192         unsigned shift = 0;
    193         assert((p_width_bits % 8) == 0);
    194         do {
    195             temp = (unsigned char)((p_val >> shift) & 0xFF);
    196             if (!WriteRaw(&temp, 1))
    197                 return false;
    198             shift += 8;
    199         } while (shift < p_width_bits);
    200         return true;
    201     }
    202 
    203     unsigned m_nch, m_bps, m_srate;
    204     FILE *m_file;
    205     unsigned m_data_bytes_written;
    206 };
    207 
     39#include <libwaveformat.h>
    20840
    20941static void
     
    21850main(int argc, char **argv)
    21951{
     52    mpc_reader reader; mpc_streaminfo si; mpc_decoder decoder; wavwriter
     53
    22054    if (argc != 2 && argc != 3) {
    22155        if (argc > 0)
     
    22357        return 0;
    22458    }
     59
    22560
    22661    FILE *input = fopen(argv[1], "rb");
Note: See TracChangeset for help on using the changeset viewer.