o Added preliminary support for timed-text wrapping for AS-02. This
[asdcplib.git] / src / DCData_ByteStream_Parser.cpp
1 /*
2 Copyright (c) 2013-2013, 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    AtmosSyncChannel_Mixer.h
28     \version $Id$
29     \brief   AS-DCP library, Digital Cinema Data bytestream essence reader
30 */
31
32 #include "AS_DCP.h"
33 #include "KM_fileio.h"
34 #include "KM_log.h"
35 using Kumu::DefaultLogSink;
36
37 //------------------------------------------------------------------------------------------
38
39 class ASDCP::DCData::BytestreamParser::h__BytestreamParser
40 {
41   ASDCP_NO_COPY_CONSTRUCT(h__BytestreamParser);
42
43 public:
44   DCDataDescriptor  m_DDesc;
45   Kumu::FileReader   m_File;
46
47   h__BytestreamParser()
48   {
49     memset(&m_DDesc, 0, sizeof(m_DDesc));
50     m_DDesc.EditRate = Rational(24,1);
51   }
52
53   ~h__BytestreamParser() {}
54
55   Result_t OpenReadFrame(const char* filename, FrameBuffer& FB)
56   {
57     ASDCP_TEST_NULL_STR(filename);
58     m_File.Close();
59     Result_t result = m_File.OpenRead(filename);
60
61     if ( ASDCP_SUCCESS(result) )
62     {
63         Kumu::fsize_t file_size = m_File.Size();
64
65         if ( FB.Capacity() < file_size )
66         {
67             DefaultLogSink().Error("FrameBuf.Capacity: %u frame length: %u\n", FB.Capacity(), (ui32_t)file_size);
68             return RESULT_SMALLBUF;
69         }
70     }
71
72     ui32_t read_count;
73
74     if ( ASDCP_SUCCESS(result) )
75         result = m_File.Read(FB.Data(), FB.Capacity(), &read_count);
76
77     if ( ASDCP_SUCCESS(result) )
78         FB.Size(read_count);
79
80     return result;
81   }
82 };
83
84
85 //------------------------------------------------------------------------------------------
86
87 ASDCP::DCData::BytestreamParser::BytestreamParser()
88 {
89 }
90
91 ASDCP::DCData::BytestreamParser::~BytestreamParser()
92 {
93 }
94
95 // Opens the stream for reading, parses enough data to provide a complete
96 // set of stream metadata for the MXFWriter below.
97 ASDCP::Result_t
98 ASDCP::DCData::BytestreamParser::OpenReadFrame(const char* filename, FrameBuffer& FB) const
99 {
100   const_cast<ASDCP::DCData::BytestreamParser*>(this)->m_Parser = new h__BytestreamParser;
101   return m_Parser->OpenReadFrame(filename, FB);
102 }
103
104 //
105 ASDCP::Result_t
106 ASDCP::DCData::BytestreamParser::FillDCDataDescriptor(DCDataDescriptor& DDesc) const
107 {
108   if ( m_Parser.empty() )
109     return RESULT_INIT;
110
111   DDesc = m_Parser->m_DDesc;
112   return RESULT_OK;
113 }
114
115
116 //
117 // end DCData_Bytestream_Parser.cpp
118 //
119
120
121