Bump patch version post tag.
[asdcplib.git] / src / S12MTimecode.h
1 /*
2 Copyright (c) 2007-2014, 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    S12MTimecode.cpp
28     \version $Id$       
29     \brief   AS-DCP library, Timecode PCM essence reader and writer implementation
30 */
31
32 /*
33
34   DROP-FRAME NOT SUPPORTED!
35
36 */
37
38 #ifndef _S12MTIMECODE_H_
39 #define _S12MTIMECODE_H_
40
41 #include "KM_util.h"
42 #include "KM_memio.h"
43
44 namespace ASDCP {
45
46  class S12MTimecode : public Kumu::IArchive
47 {
48  protected:
49   ui32_t m_FrameCount;
50   ui32_t m_FPS;
51
52 public:
53   S12MTimecode() : m_FrameCount(0), m_FPS(0) {}
54
55   S12MTimecode(ui32_t frame_count, ui32_t fps) : m_FrameCount(frame_count), m_FPS(fps) {}
56
57   S12MTimecode(const std::string& tc, ui32_t fps) : m_FrameCount(0), m_FPS(fps)
58   {
59     DecodeString(tc);
60   }
61
62   S12MTimecode(const S12MTimecode& rhs) : IArchive(), m_FrameCount(0), m_FPS(0)
63   {
64     m_FPS = rhs.m_FPS;
65     m_FrameCount = rhs.m_FrameCount;
66   }
67
68   ~S12MTimecode() {}
69
70   const S12MTimecode& operator=(const S12MTimecode& rhs)
71   {
72     assert(m_FPS != 0);
73     m_FrameCount = rhs.m_FrameCount;
74     return *this;
75   }
76
77   inline void   SetFPS(ui32_t fps) { m_FPS = fps; }
78   inline ui32_t GetFPS() const { return m_FPS; }
79
80   inline void   SetFrames(ui32_t frame_count) { m_FrameCount = frame_count; }
81   inline ui32_t GetFrames() const { return m_FrameCount; }
82
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; }
85
86   inline const S12MTimecode operator+(const S12MTimecode& rhs){
87     assert(m_FPS > 0);
88     assert(m_FPS == rhs.m_FPS);
89     return S12MTimecode(m_FrameCount + rhs.m_FrameCount, m_FPS);
90   }
91
92   inline const S12MTimecode operator-(const S12MTimecode& rhs){
93     assert(m_FPS > 0);
94     assert(m_FPS == rhs.m_FPS);
95     return S12MTimecode(m_FrameCount + rhs.m_FrameCount, m_FPS);
96   }
97
98
99   void DecodeString(const std::string& tc)
100   {
101     assert(m_FPS > 0);
102     const char* p = tc.c_str();
103
104     while ( *p != 0 && ! isdigit(*p) )
105       p++;
106
107     if ( *p != 0 )
108       {
109         ui32_t hours = strtol(p, 0, 10);
110         ui32_t minutes = strtol(p+3, 0, 10);
111         ui32_t seconds = strtol(p+6, 0, 10);
112         ui32_t frames = strtol(p+9, 0, 10);
113
114         m_FrameCount = (((((hours * 60) + minutes) * 60) + seconds) * m_FPS)+ frames;
115       }
116   }
117
118   const char* EncodeString(char* buf, ui32_t buf_len)
119   {
120     assert(m_FPS > 0);
121     ui32_t frames = m_FrameCount % m_FPS;
122     m_FrameCount /= m_FPS;
123     ui32_t seconds = m_FrameCount % 60;
124     m_FrameCount /= 60;
125     ui32_t minutes = m_FrameCount % 60;
126     ui32_t hours = m_FrameCount / 60;
127
128     snprintf(buf, buf_len, "%02d:%02d:%02d:%02d", hours, minutes, seconds, frames);
129     return buf;
130   }
131
132   // IArchive
133   bool HasValue() const { return (m_FPS > 0); }
134   ui32_t ArchiveLength() const { return sizeof(ui32_t)*2; }
135
136   bool Archive(Kumu::MemIOWriter* Writer) const
137   {
138     if ( ! Writer->WriteUi32BE(m_FPS) ) return false;
139     if ( ! Writer->WriteUi32BE(m_FrameCount) ) return false;
140     return true;
141   }
142
143   bool Unarchive(Kumu::MemIOReader* Reader)
144   {
145     if ( ! Reader->ReadUi32BE(&m_FPS) ) return false;
146     if ( ! Reader->ReadUi32BE(&m_FrameCount) ) return false;
147     return true;
148   }
149 };
150
151
152 } // namespace ASDCP
153
154 #endif // _S12MTIMECODE_H_
155
156 //
157 // end S12MTimecode.h
158 //