2 Copyright (c) 2007-2009, John Hurst
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
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.
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.
27 /*! \file S12MTimecode.cpp
29 \brief AS-DCP library, Timecode PCM essence reader and writer implementation
34 DROP-FRAME NOT SUPPORTED!
38 #ifndef _S12MTIMECODE_H_
39 #define _S12MTIMECODE_H_
46 class S12MTimecode : public Kumu::IArchive
53 S12MTimecode() : m_FrameCount(0), m_FPS(0) {}
55 S12MTimecode(ui32_t frame_count, ui32_t fps) : m_FrameCount(frame_count), m_FPS(fps) {}
57 S12MTimecode(const std::string& tc, ui32_t fps) : m_FrameCount(0), m_FPS(fps)
62 S12MTimecode(const S12MTimecode& rhs) : IArchive(), m_FrameCount(0), m_FPS(0)
65 m_FrameCount = rhs.m_FrameCount;
70 const S12MTimecode& operator=(const S12MTimecode& rhs)
73 m_FrameCount = rhs.m_FrameCount;
77 inline void SetFPS(ui32_t fps) { m_FPS = fps; }
78 inline ui32_t GetFPS() const { return m_FPS; }
80 inline void SetFrames(ui32_t frame_count) { m_FrameCount = frame_count; }
81 inline ui32_t GetFrames() const { return m_FrameCount; }
83 inline bool operator==(const S12MTimecode& rhs) const { return m_FrameCount == rhs.m_FrameCount; }
84 inline bool operator<(const S12MTimecode& rhs) const { return m_FrameCount < rhs.m_FrameCount; }
86 inline const S12MTimecode operator+(const S12MTimecode& rhs){
88 assert(m_FPS == rhs.m_FPS);
89 return S12MTimecode(m_FrameCount + rhs.m_FrameCount, m_FPS);
92 inline const S12MTimecode operator-(const S12MTimecode& rhs){
94 assert(m_FPS == rhs.m_FPS);
95 return S12MTimecode(m_FrameCount + rhs.m_FrameCount, m_FPS);
99 void DecodeString(const std::string& tc)
102 const char* p = tc.c_str();
104 while ( *p != 0 && ! isdigit(*p) )
109 ui32_t hours = atoi(p);
110 ui32_t minutes = atoi(p+3);
111 ui32_t seconds = atoi(p+6);
112 ui32_t frames = atoi(p+9);
114 m_FrameCount = (((((hours * 60) + minutes) * 60) + seconds) * m_FPS)+ frames;
118 const char* EncodeString(char* buf, ui32_t buf_len)
121 ui32_t frames = m_FrameCount % m_FPS;
122 m_FrameCount /= m_FPS;
123 ui32_t seconds = m_FrameCount % 60;
125 ui32_t minutes = m_FrameCount % 60;
126 ui32_t hours = m_FrameCount / 60;
128 snprintf(buf, buf_len, "%02d:%02d:%02d:%02d", hours, minutes, seconds, frames);
133 bool HasValue() const { return (m_FPS > 0); }
134 ui32_t ArchiveLength() const { return sizeof(ui32_t)*2; }
136 bool Archive(Kumu::MemIOWriter* Writer) const
138 if ( ! Writer->WriteUi32BE(m_FPS) ) return false;
139 if ( ! Writer->WriteUi32BE(m_FrameCount) ) return false;
143 bool Unarchive(Kumu::MemIOReader* Reader)
145 if ( ! Reader->ReadUi32BE(&m_FPS) ) return false;
146 if ( ! Reader->ReadUi32BE(&m_FrameCount) ) return false;
154 #endif // _S12MTIMECODE_H_
157 // end S12MTimecode.h