Simplify time representation; better in-tree DCP subtitle parser.
[libsub.git] / asdcplib / src / WavFileWriter.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    WavFileWriter.h
28     \version $Id: WavFileWriter.h,v 1.6 2009/04/09 19:16:49 msheby Exp $
29     \brief   demux and write PCM data to WAV file(s)
30 */
31
32 #include <KM_fileio.h>
33 #include <KM_log.h>
34 #include <Wav.h>
35 #include <list>
36
37 #ifndef _WAVFILEWRITER_H_
38 #define _WAVFILEWRITER_H_
39
40
41 //
42 class WavFileElement : public Kumu::FileWriter
43 {
44   ASDCP::PCM::FrameBuffer m_Buf;
45   byte_t* m_p;
46
47   WavFileElement();
48   KM_NO_COPY_CONSTRUCT(WavFileElement);
49
50 public:
51   WavFileElement(ui32_t s) : m_Buf(s), m_p(0)
52   {
53     m_p = m_Buf.Data();
54   }
55
56   ~WavFileElement() {}
57
58   void WriteSample(const byte_t* sample, ui32_t sample_size)
59   {
60     memcpy(m_p, sample, sample_size);
61     m_p += sample_size;
62   }
63
64   ASDCP::Result_t Flush()
65   {
66     ui32_t write_count = 0;
67
68     if ( m_p == m_Buf.Data() )
69       return ASDCP::RESULT_EMPTY_FB;
70
71     ui32_t write_size = m_p - m_Buf.Data();
72     m_p = m_Buf.Data();
73     return Write(m_Buf.RoData(), write_size, &write_count);
74   }
75 };
76
77
78 //
79 class WavFileWriter
80 {
81   ASDCP::PCM::AudioDescriptor m_ADesc;
82   std::list<WavFileElement*>  m_OutFile;
83   ui32_t                      m_ChannelCount;
84   ASDCP_NO_COPY_CONSTRUCT(WavFileWriter);
85
86  public:
87   WavFileWriter() : m_ChannelCount(0) {}
88   ~WavFileWriter()
89     {
90       while ( ! m_OutFile.empty() )
91         {
92           delete m_OutFile.back();
93           m_OutFile.pop_back();
94         }
95     }
96
97   //
98   enum SplitType_t {
99     ST_NONE,   // write all channels to a single WAV file
100     ST_MONO,   // write each channel a separate WAV file
101     ST_STEREO  // write channel pairs to separate WAV files
102   };
103
104   ASDCP::Result_t
105     OpenWrite(ASDCP::PCM::AudioDescriptor &ADesc, const char* file_root, SplitType_t split = ST_NONE)
106     {
107       ASDCP_TEST_NULL_STR(file_root);
108       char filename[Kumu::MaxFilePath];
109       ui32_t file_count = 0;
110       ASDCP::Result_t result = ASDCP::RESULT_OK;
111       m_ADesc = ADesc;
112
113       switch ( split )
114         {
115         case ST_NONE:
116           file_count = 1;
117           m_ChannelCount = m_ADesc.ChannelCount;
118           break;
119
120         case ST_MONO:
121           file_count = m_ADesc.ChannelCount;
122           m_ChannelCount = 1;
123           break;
124
125         case ST_STEREO:
126           if ( m_ADesc.ChannelCount % 2 != 0 )
127             {
128               Kumu::DefaultLogSink().Error("Unable to create 2-channel splits with odd number of input channels.\n");
129               return ASDCP::RESULT_PARAM;
130             }
131
132           file_count = m_ADesc.ChannelCount / 2;
133           m_ChannelCount = 2;
134           break;
135         }
136       assert(file_count && m_ChannelCount);
137
138       ui32_t element_size = ASDCP::PCM::CalcFrameBufferSize(m_ADesc) / file_count;
139
140       for ( ui32_t i = 0; i < file_count && ASDCP_SUCCESS(result); i++ )
141         {
142           snprintf(filename, Kumu::MaxFilePath, "%s_%u.wav", file_root, (i + 1));
143           m_OutFile.push_back(new WavFileElement(element_size));
144           result = m_OutFile.back()->OpenWrite(filename);
145
146           if ( ASDCP_SUCCESS(result) )
147             {
148               ASDCP::PCM::AudioDescriptor tmpDesc = m_ADesc;
149               tmpDesc.ChannelCount = m_ChannelCount;
150               ASDCP::Wav::SimpleWaveHeader Wav(tmpDesc);
151               result = Wav.WriteToFile(*(m_OutFile.back()));
152             }
153         }
154       
155       return result;
156     }
157
158   ASDCP::Result_t
159     WriteFrame(ASDCP::PCM::FrameBuffer& FB)
160     {
161       if ( m_OutFile.empty() )
162         return ASDCP::RESULT_STATE;
163
164       if ( m_OutFile.size() == 1 ) // no de-interleave needed, just write out the frame
165         return m_OutFile.back()->Write(FB.RoData(), FB.Size(), 0);
166  
167       std::list<WavFileElement*>::iterator fi;
168       ui32_t sample_size = m_ADesc.QuantizationBits / 8;
169       const byte_t* p = FB.RoData();
170       const byte_t* end_p = p + FB.Size();
171
172       while ( p < end_p )
173         {
174           for ( fi = m_OutFile.begin(); fi != m_OutFile.end(); fi++ )
175             {
176               for ( ui32_t c = 0; c < m_ChannelCount; c++ )
177                 {
178                   (*fi)->WriteSample(p, sample_size);
179                   p += sample_size;
180                 }
181             }
182         }
183
184       ASDCP::Result_t result = ASDCP::RESULT_OK;
185
186       for ( fi = m_OutFile.begin(); fi != m_OutFile.end() && ASDCP_SUCCESS(result); fi++ )
187         result = (*fi)->Flush();
188
189       return result;
190     }
191 };
192
193
194 #endif // _WAVFILEWRITER_H_
195
196 //
197 // end WavFileWriter.h
198 //