Eliminate warnings about copy constructors not getting initialized.
[asdcplib.git] / src / S12MTimecode.h
1 /*
2 Copyright (c) 2007, 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   ui32_t m_FrameCount;
49   ui32_t m_FPS;
50
51 public:
52   S12MTimecode() : m_FrameCount(0), m_FPS(0) {}
53
54   S12MTimecode(ui32_t frame_count, ui32_t fps) : m_FrameCount(frame_count), m_FPS(fps) {}
55
56   S12MTimecode(const std::string& tc, ui32_t fps) : m_FrameCount(0), m_FPS(fps)
57   {
58     DecodeString(tc);
59   }
60
61   S12MTimecode(const S12MTimecode& rhs) : IArchive(), m_FrameCount(0), m_FPS(0)
62   {
63     m_FPS = rhs.m_FPS;
64     m_FrameCount = rhs.m_FrameCount;
65   }
66
67   ~S12MTimecode() {}
68
69   const S12MTimecode& operator=(const S12MTimecode& rhs)
70   {
71     assert(m_FPS != 0);
72     m_FrameCount = rhs.m_FrameCount;
73     return *this;
74   }
75
76   inline void   SetFPS(ui32_t fps) { m_FPS = fps; }
77   inline ui32_t GetFPS() const { return m_FPS; }
78
79   inline void   SetFrames(ui32_t frame_count) { m_FrameCount = frame_count; }
80   inline ui32_t GetFrames() const { return m_FrameCount; }
81
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; }
84
85   inline const S12MTimecode operator+(const S12MTimecode& rhs){
86     assert(m_FPS > 0);
87     assert(m_FPS == rhs.m_FPS);
88     return S12MTimecode(m_FrameCount + rhs.m_FrameCount, m_FPS);
89   }
90
91   inline const S12MTimecode operator-(const S12MTimecode& rhs){
92     assert(m_FPS > 0);
93     assert(m_FPS == rhs.m_FPS);
94     return S12MTimecode(m_FrameCount + rhs.m_FrameCount, m_FPS);
95   }
96
97
98   void DecodeString(const std::string& tc)
99   {
100     assert(m_FPS > 0);
101     const char* p = tc.c_str();
102
103     while ( *p != 0 && ! isdigit(*p) )
104       p++;
105
106     if ( *p != 0 )
107       {
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);
112
113         m_FrameCount = (((((hours * 60) + minutes) * 60) + seconds) * m_FPS)+ frames;
114       }
115   }
116
117   const char* EncodeString(char* buf, ui32_t buf_len)
118   {
119     assert(m_FPS > 0);
120     ui32_t frames = m_FrameCount % m_FPS;
121     m_FrameCount /= m_FPS;
122     ui32_t seconds = m_FrameCount % 60;
123     m_FrameCount /= 60;
124     ui32_t minutes = m_FrameCount % 60;
125     ui32_t hours = m_FrameCount / 60;
126
127     snprintf(buf, buf_len, "%02d:%02d:%02d:%02d", hours, minutes, seconds, frames);
128     return buf;
129   }
130
131   // IArchive
132   bool HasValue() const { return (m_FPS > 0); }
133   ui32_t ArchiveLength() const { return sizeof(ui32_t)*2; }
134
135   bool Archive(Kumu::MemIOWriter* Writer) const
136   {
137     if ( ! Writer->WriteUi32BE(m_FPS) ) return false;
138     if ( ! Writer->WriteUi32BE(m_FrameCount) ) return false;
139     return true;
140   }
141
142   bool Unarchive(Kumu::MemIOReader* Reader)
143   {
144     if ( ! Reader->ReadUi32BE(&m_FPS) ) return false;
145     if ( ! Reader->ReadUi32BE(&m_FrameCount) ) return false;
146     return true;
147   }
148 };
149
150
151 } // namespace ASDCP
152
153 #endif // _S12MTIMECODE_H_
154
155 //
156 // end S12MTimecode.h
157 //