AJA NTV2 SDK  17.1.1.1245
NTV2 SDK 17.1.1.1245
dpxfileio.cpp
Go to the documentation of this file.
1 /* SPDX-License-Identifier: MIT */
8 #include <stdio.h>
9 
10 #include "dpxfileio.h"
11 #include "ajabase/system/file_io.h"
12 
13 using std::string;
14 using std::vector;
15 
16 
18  : mPathSet (false),
19  mLoopMode (true),
20  mPauseMode (false),
21  mFileCount (0),
22  mCurrentIndex (0)
23 {
24 } // constructor
25 
26 
28 {
29  mFileList.clear();
30 } // destructor
31 
32 
33 uint32_t AJADPXFileIO::GetFileCount () const
34 {
35  return mFileCount;
36 } // GetFileCount
37 
38 
39 vector<string> & AJADPXFileIO::GetFileList ()
40 {
41  return mFileList;
42 } // GetFileList
43 
44 
45 uint32_t AJADPXFileIO::GetIndex () const
46 {
47  return mCurrentIndex;
48 } // GetIndex
49 
50 
52 {
53  return mLoopMode;
54 } // GetLoopMode
55 
56 
58 {
59  return mPauseMode;
60 } // GetPauseMode
61 
62 
63 string AJADPXFileIO::GetPath () const
64 {
65  return mPath;
66 } // GetPath
67 
68 
69 AJAStatus AJADPXFileIO::Read (const uint32_t index)
70 {
71  AJA_UNUSED(index);
72 
73  AJAFileIO file;
75 
76  // Check that we've been initialzed
77  if (!mPathSet)
78  return AJA_STATUS_INITIALIZE;
79 
80  // Check that the index is in range of the vector
81  if (mCurrentIndex >= mFileCount)
82  return AJA_STATUS_RANGE;
83 
84  // Get the name of the next file to open, then do so
85  string fileName = mFileList [mCurrentIndex];
86  status = file.Open (fileName, eAJAReadOnly, eAJAUnbuffered);
87 
88  // Read the header from the file
89  if (AJA_STATUS_SUCCESS == status)
90  {
91  uint32_t bytesRead = file.Read ((uint8_t*)&GetHdr(), uint32_t(GetHdrSize()));
92  if (bytesRead != GetHdrSize())
93  status = AJA_STATUS_IO;
94  }
95 
96  // Done with the file
97  file.Close ();
98 
99  return status;
100 }
101 
102 
103 AJAStatus AJADPXFileIO::Read (uint8_t & buffer,
104  const uint32_t bufferSize,
105  uint32_t & index)
106 {
107  AJA_UNUSED(bufferSize);
108 
109  AJAFileIO file;
110  AJAStatus status = AJA_STATUS_SUCCESS;
111 
112  // Check that we've been initialzed
113  if (!mPathSet)
114  return AJA_STATUS_INITIALIZE;
115 
116  // Check that the index is in range of the vector
117  if (mCurrentIndex >= mFileCount)
118  return AJA_STATUS_RANGE;
119 
120  // Get the name of the next file to open, then do so
121  string fileName = mFileList [mCurrentIndex];
122  status = file.Open (fileName, eAJAReadOnly, eAJAUnbuffered);
123 
124  // Read the header from the file
125  if (AJA_STATUS_SUCCESS == status)
126  {
127  uint32_t bytesRead = file.Read ((uint8_t*)&GetHdr(), uint32_t(GetHdrSize()));
128  if (bytesRead != GetHdrSize())
129  status = AJA_STATUS_IO;
130  }
131 
132  // Sanity check the "magic number"
133  if (AJA_STATUS_SUCCESS == status)
134  {
135  if (!DPX_VALID( &GetHdr() ))
136  status = AJA_STATUS_UNSUPPORTED;
137  }
138 
139  // Seek to the image payload
140  if (AJA_STATUS_SUCCESS == status)
141  {
142  status = file.Seek (get_fi_image_offset(), eAJASeekSet);
143  }
144 
145  // Read the payload
146  if (AJA_STATUS_SUCCESS == status)
147  {
148  uint32_t bytesRead = file.Read ((uint8_t*)&buffer, uint32_t(get_ii_image_size()));
149  if (bytesRead != get_ii_image_size())
150  status = AJA_STATUS_IO;
151  }
152 
153  // Done with the file
154  file.Close ();
155 
156  // Report the current index in the sequence
157  index = mCurrentIndex;
158 
159  // Don't update index if paused
160  if (mPauseMode)
161  return status;
162 
163  // Loop playback if active
164  if (mLoopMode && (mCurrentIndex + 1 >= mFileCount))
165  {
166  mCurrentIndex = 0;
167  }
168  else
169  {
170  // Bump the index even if there was an error, so we skip the bad file
171  mCurrentIndex++;
172  }
173 
174  return status;
175 } // Read
176 
177 
178 void AJADPXFileIO::SetFileList (vector<string> & list)
179 {
180  mFileList.clear();
181 
182  mFileList = list;
183 } // SetFileList
184 
185 
186 AJAStatus AJADPXFileIO::SetIndex (const uint32_t & index)
187 {
188  if (index && (index > mFileCount - 1))
189  return AJA_STATUS_RANGE;
190 
191  mCurrentIndex = index;
192 
193  return AJA_STATUS_SUCCESS;
194 } // SetIndex
195 
196 
198 {
199  mLoopMode = mode;
200 } // SetLoopMode
201 
202 
204 {
205  mPauseMode = mode;
206 } // SetPauseMode
207 
208 
209 AJAStatus AJADPXFileIO::SetPath (const string & path)
210 {
211  AJAFileIO file;
212 
213  mPath = path;
214  mFileList.clear();
215 
216  // Construct a list of all the DPX files in the current location
217  AJAStatus status = file.ReadDirectory (mPath, "*.dpx", mFileList);
218  if (AJA_STATUS_SUCCESS != status)
219  {
220  mFileList.clear();
221  return status;
222  }
223 
224  mFileCount = uint32_t(mFileList.size());
225  mCurrentIndex = 0;
226 
227  mPathSet = true;
228 
229  return AJA_STATUS_SUCCESS;
230 } // SetPath
231 
232 
233 AJAStatus AJADPXFileIO::Write (const uint8_t & buffer,
234  const uint32_t bufferSize,
235  const uint32_t & index) const
236 {
237  string fileName;
238  AJAFileIO file;
239  AJAStatus status = AJA_STATUS_SUCCESS;
240 
241  // Check that we've been initialzed
242  if (!mPathSet)
243  return AJA_STATUS_INITIALIZE;
244 
245  // Construct the file name
246  char asciiSequence[9];
247  ajasnprintf (asciiSequence, 9, "%.8d", index);
248 
249  fileName = mPath + "/" + asciiSequence + ".DPX";
250 
251  // Open the file or return an error code
252  status = file.Open (fileName, eAJAWriteOnly, eAJAUnbuffered);
253  if (AJA_STATUS_SUCCESS != status)
254  return status;
255 
256  // Write the DPX header to the file
257  size_t headerSize = GetHdrSize();
258  uint32_t bytesWritten = file.Write ((const uint8_t*)&GetHdr(), uint32_t(headerSize));
259  if (bytesWritten != headerSize)
260  {
261  status = AJA_STATUS_IO;
262  }
263 
264  // Write the DPX payload to the file
265  bytesWritten = file.Write ((uint8_t*)&buffer, bufferSize);
266  if (bytesWritten != bufferSize)
267  {
268  status = AJA_STATUS_IO;
269  }
270 
271  // Finished with this file
272  file.Close ();
273 
274  return status;
275 } // Write
276 
AJAFileIO::Write
uint32_t Write(const uint8_t *pBuffer, const uint32_t length) const
Definition: file_io.cpp:380
AJADPXFileIO::GetPath
AJA_EXPORT std::string GetPath() const
Returns the current path to the DPX files to be read.
Definition: dpxfileio.cpp:63
AJADPXFileIO::GetIndex
AJA_EXPORT uint32_t GetIndex() const
Returns the index in the file list of the next file to be read.
Definition: dpxfileio.cpp:45
eAJAWriteOnly
@ eAJAWriteOnly
Definition: file_io.h:32
AJA_STATUS_SUCCESS
@ AJA_STATUS_SUCCESS
Definition: types.h:381
AJADPXFileIO::SetIndex
AJA_EXPORT AJAStatus SetIndex(const uint32_t &index)
Specifies the index in the file list of the next file to be read.
Definition: dpxfileio.cpp:186
DpxHdr::GetHdr
const DPX_header_t & GetHdr(void) const
Definition: dpx_hdr.h:463
AJADPXFileIO::SetPauseMode
AJA_EXPORT void SetPauseMode(bool mode)
Specifies the setting of the pause control. If true, pause mode will take effect. If false,...
Definition: dpxfileio.cpp:203
eAJAReadOnly
@ eAJAReadOnly
Definition: file_io.h:31
AJA_UNUSED
#define AJA_UNUSED(_x_)
Definition: types.h:424
DpxHdr::get_ii_image_size
size_t get_ii_image_size()
Definition: dpx_hdr.h:318
AJADPXFileIO::SetLoopMode
AJA_EXPORT void SetLoopMode(bool mode)
Specifies the setting of the loop play control. If true, the last file of a sequence is followed by t...
Definition: dpxfileio.cpp:197
AJAFileIO::Open
AJAStatus Open(const std::string &fileName, const int flags, const int properties)
Definition: file_io.cpp:201
AJAStatus
AJAStatus
Definition: types.h:378
AJADPXFileIO::GetFileCount
AJA_EXPORT uint32_t GetFileCount() const
Returns the number of DPX files in the location specified by the path.
Definition: dpxfileio.cpp:33
AJAFileIO::Seek
AJAStatus Seek(const int64_t distance, const AJAFileSetFlag flag) const
Definition: file_io.cpp:545
dpxfileio.h
Declaration of the AJADPXFileIO class, for low level file I/O.
eAJAUnbuffered
@ eAJAUnbuffered
Definition: file_io.h:40
AJADPXFileIO::Read
AJA_EXPORT AJAStatus Read(const uint32_t inIndex)
Read only the header from the specified file in the DPX sequence.
Definition: dpxfileio.cpp:69
AJADPXFileIO::SetFileList
void SetFileList(std::vector< std::string > &list)
Specifies an ordered list of DPX files to read.
Definition: dpxfileio.cpp:178
DpxHdr::get_fi_image_offset
size_t get_fi_image_offset() const
Definition: dpx_hdr.cpp:197
DpxHdr::GetHdrSize
size_t GetHdrSize(void) const
Definition: dpx_hdr.h:473
AJA_STATUS_UNSUPPORTED
@ AJA_STATUS_UNSUPPORTED
Definition: types.h:394
AJA_STATUS_IO
@ AJA_STATUS_IO
Definition: types.h:389
AJA_STATUS_RANGE
@ AJA_STATUS_RANGE
Definition: types.h:385
AJADPXFileIO::SetPath
AJA_EXPORT AJAStatus SetPath(const std::string &inPath)
Change the path to the DPX files to be read.
Definition: dpxfileio.cpp:209
AJAFileIO::Read
uint32_t Read(uint8_t *pBuffer, const uint32_t length)
Definition: file_io.cpp:328
file_io.h
Declares the AJAFileIO class.
AJA_STATUS_INITIALIZE
@ AJA_STATUS_INITIALIZE
Definition: types.h:386
false
#define false
Definition: ntv2devicefeatures.h:25
AJADPXFileIO::GetLoopMode
AJA_EXPORT bool GetLoopMode() const
Returns the current setting of the loop play control.
Definition: dpxfileio.cpp:51
AJADPXFileIO::GetFileList
AJA_EXPORT std::vector< std::string > & GetFileList()
Returns the list of DPX files that were found at the destination.
Definition: dpxfileio.cpp:39
AJAFileIO::Close
AJAStatus Close()
Definition: file_io.cpp:281
true
#define true
Definition: ntv2devicefeatures.h:26
AJAFileIO
Definition: file_io.h:64
DPX_VALID
#define DPX_VALID(p)
Definition: dpx_hdr.h:30
eAJASeekSet
@ eAJASeekSet
Definition: file_io.h:47
AJAFileIO::ReadDirectory
static AJAStatus ReadDirectory(const std::string &directory, const std::string &filePattern, std::vector< std::string > &fileContainer)
Definition: file_io.cpp:805
AJADPXFileIO::GetPauseMode
AJA_EXPORT bool GetPauseMode() const
Returns the current setting of the pause control.
Definition: dpxfileio.cpp:57
AJADPXFileIO::Write
AJA_EXPORT AJAStatus Write(const uint8_t &inBuffer, const uint32_t inBufferSize, const uint32_t &inIndex) const
Write a DPX file.
Definition: dpxfileio.cpp:233
AJADPXFileIO::AJADPXFileIO
AJA_EXPORT AJADPXFileIO()
Constructs a DPX file IO object.
Definition: dpxfileio.cpp:17
AJADPXFileIO::~AJADPXFileIO
virtual AJA_EXPORT ~AJADPXFileIO()
Definition: dpxfileio.cpp:27