2 Copyright (c) 2007, 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
52 S12MTimecode() : m_FrameCount(0), m_FPS(0) {}
54 S12MTimecode(ui32_t frame_count, ui32_t fps) : m_FrameCount(frame_count), m_FPS(fps) {}
56 S12MTimecode(const std::string& tc, ui32_t fps) : m_FrameCount(0), m_FPS(fps)
61 S12MTimecode(const S12MTimecode& rhs) : m_FrameCount(0), m_FPS(0)
64 m_FrameCount = rhs.m_FrameCount;
69 const S12MTimecode& operator=(const S12MTimecode& rhs)
72 m_FrameCount = rhs.m_FrameCount;
76 inline void SetFPS(ui32_t fps) { m_FPS = fps; }
77 inline ui32_t GetFPS() const { return m_FPS; }
79 inline void SetFrames(ui32_t frame_count) { m_FrameCount = frame_count; }
80 inline ui32_t GetFrames() const { return m_FrameCount; }
82 inline bool operator==(const S12MTimecode& rhs) const { return m_FrameCount == rhs.m_FrameCount; }
83 inline bool operator<(const S12MTimecode& rhs) const { return m_FrameCount < rhs.m_FrameCount; }
85 inline const S12MTimecode operator+(const S12MTimecode& rhs){
87 assert(m_FPS == rhs.m_FPS);
88 return S12MTimecode(m_FrameCount + rhs.m_FrameCount, m_FPS);
91 inline const S12MTimecode operator-(const S12MTimecode& rhs){
93 assert(m_FPS == rhs.m_FPS);
94 return S12MTimecode(m_FrameCount + rhs.m_FrameCount, m_FPS);
98 void DecodeString(const std::string& tc)
101 const char* p = tc.c_str();
103 while ( *p != 0 && ! isdigit(*p) )
108 ui32_t hours = atoi(p);
109 ui32_t minutes = atoi(p+3);
110 ui32_t seconds = atoi(p+6);
111 ui32_t frames = atoi(p+9);
113 m_FrameCount = (((((hours * 60) + minutes) * 60) + seconds) * m_FPS)+ frames;
117 const char* EncodeString(char* buf, ui32_t buf_len)
120 ui32_t frames = m_FrameCount % m_FPS;
121 m_FrameCount /= m_FPS;
122 ui32_t seconds = m_FrameCount % 60;
124 ui32_t minutes = m_FrameCount % 60;
125 ui32_t hours = m_FrameCount / 60;
127 snprintf(buf, buf_len, "%02d:%02d:%02d:%02d", hours, minutes, seconds, frames);
132 bool HasValue() const { return (m_FPS > 0); }
133 ui32_t ArchiveLength() const { return sizeof(ui32_t)*2; }
135 bool Archive(Kumu::MemIOWriter* Writer) const
137 if ( ! Writer->WriteUi32BE(m_FPS) ) return false;
138 if ( ! Writer->WriteUi32BE(m_FrameCount) ) return false;
142 bool Unarchive(Kumu::MemIOReader* Reader)
144 if ( ! Reader->ReadUi32BE(&m_FPS) ) return false;
145 if ( ! Reader->ReadUi32BE(&m_FrameCount) ) return false;
153 #endif // _S12MTIMECODE_H_
156 // end S12MTimecode.h