Implemented J2K desc to/from MD
[asdcplib.git] / src / Wav.h
1 /*
2 Copyright (c) 2005-2009, John Hurst
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
8 1. Redistributions of source code must retain the above copyright
9    notice, this list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright
11    notice, this list of conditions and the following disclaimer in the
12    documentation and/or other materials provided with the distribution.
13 3. The name of the author may not be used to endorse or promote products
14    derived from this software without specific prior written permission.
15
16 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 /*! \file    Wav.h
28     \version $Id$
29     \brief   Wave file common elements
30 */
31
32 #ifndef _WAV_H_
33 #define _WAV_H_
34
35 #include <KM_fileio.h>
36 #include <AS_DCP.h>
37
38 namespace ASDCP
39 {
40   //
41   class fourcc
42     {
43     private:
44       byte_t data[4];
45
46     public:
47       inline fourcc() { memset( data, 0, 4 ); }
48       inline fourcc( const char* v )   { memcpy( this->data, v, 4 ); }
49       inline fourcc( const byte_t* v ) { memcpy( this->data, v, 4 ); }
50       inline fourcc& operator=(const fourcc & s) { memcpy( this->data, s.data, 4); return *this; }
51       inline bool operator==(const fourcc &rhs)  { return memcmp(data, rhs.data, 4) == 0 ? true : false; }
52       inline bool operator!=(const fourcc &rhs)  { return memcmp(data, rhs.data, 4) != 0 ? true : false; }
53     };
54
55   namespace AIFF
56     {
57       const fourcc FCC_FORM("FORM");
58       const fourcc FCC_AIFF("AIFF");
59       const fourcc FCC_COMM("COMM");
60       const fourcc FCC_SSND("SSND");
61
62       class SimpleAIFFHeader
63         {
64         public:
65           ui16_t  numChannels;
66           ui32_t  numSampleFrames;
67           ui16_t  sampleSize;
68           byte_t  sampleRate[10]; // 80-bit IEEE 754 float
69           ui32_t  data_len;
70
71           SimpleAIFFHeader() :
72             numChannels(0), numSampleFrames(0), sampleSize(0), data_len(0) {
73             memset(sampleRate, 0, 10);
74           }
75           
76           Result_t  ReadFromBuffer(const byte_t* buf, ui32_t buf_len, ui32_t* data_start);
77           Result_t  ReadFromFile(const Kumu::FileReader& InFile, ui32_t* data_start);
78           void      FillADesc(ASDCP::PCM::AudioDescriptor& ADesc, Rational PictureRate) const;
79         };
80
81     } // namespace AIFF
82
83   namespace Wav
84     {
85       const ui32_t MaxWavHeader = 1024*32; // must find "data" within this space or no happy
86
87       const fourcc FCC_RIFF("RIFF");
88       const fourcc FCC_WAVE("WAVE");
89       const fourcc FCC_fmt_("fmt ");
90       const fourcc FCC_data("data");
91
92       const ui16_t ASDCP_WAVE_FORMAT_PCM = 1;
93       const ui16_t ASDCP_WAVE_FORMAT_EXTENSIBLE = 65534;
94
95       //
96       class SimpleWaveHeader
97         {
98         public:
99           ui16_t        format;
100           ui16_t        nchannels;
101           ui32_t        samplespersec;
102           ui32_t        avgbps;
103           ui16_t        blockalign;
104           ui16_t        bitspersample;
105           ui16_t        cbsize;
106           ui32_t        data_len;
107
108           SimpleWaveHeader() :
109             format(0), nchannels(0), samplespersec(0), avgbps(0),
110             blockalign(0), bitspersample(0), cbsize(0), data_len(0) {}
111       
112           SimpleWaveHeader(ASDCP::PCM::AudioDescriptor& ADesc);
113           
114           Result_t  ReadFromBuffer(const byte_t* buf, ui32_t buf_len, ui32_t* data_start);
115           Result_t  ReadFromFile(const Kumu::FileReader& InFile, ui32_t* data_start);
116           Result_t  WriteToFile(Kumu::FileWriter& OutFile) const;
117           void      FillADesc(ASDCP::PCM::AudioDescriptor& ADesc, Rational PictureRate) const;
118         };
119
120     } // namespace Wav
121
122   namespace RF64
123     {
124       const fourcc FCC_RF64("RF64");
125       const fourcc FCC_ds64("ds64");
126
127
128       static const ui32_t MAX_RIFF_LEN = 0xFFFFFFFF;
129       static const ui32_t DS64_HEADER_LEN = 28;
130       static const ui32_t SIMPLE_RF64_HEADER_LEN = 82;
131       //
132       class SimpleRF64Header
133         {
134         public:
135           ui16_t        format;
136           ui16_t        nchannels;
137           ui32_t        samplespersec;
138           ui32_t        avgbps;
139           ui16_t        blockalign;
140           ui16_t        bitspersample;
141           ui16_t        cbsize;
142           ui64_t        data_len;
143
144           SimpleRF64Header() :
145             format(0), nchannels(0), samplespersec(0), avgbps(0),
146             blockalign(0), bitspersample(0), cbsize(0), data_len(0) {}
147
148           SimpleRF64Header(ASDCP::PCM::AudioDescriptor& ADesc);
149
150           Result_t  ReadFromBuffer(const byte_t* buf, ui32_t buf_len, ui32_t* data_start);
151           Result_t  ReadFromFile(const Kumu::FileReader& InFile, ui32_t* data_start);
152           Result_t  WriteToFile(Kumu::FileWriter& OutFile) const;
153           void      FillADesc(ASDCP::PCM::AudioDescriptor& ADesc, Rational PictureRate) const;
154
155     private:
156       static const ui64_t SAMPLE_COUNT = 0;
157       static const ui32_t TABLE_LEN = 0;
158         };
159
160     } // namespace RF64
161 } // namespace ASDCP
162
163 #endif // _WAV_H_
164
165 //
166 // end Wav.h
167 //