AJA NTV2 SDK  17.0.1.1246
NTV2 SDK 17.0.1.1246
main.cpp
Go to the documentation of this file.
1 /* SPDX-License-Identifier: MIT */
9 // Includes
10 #include "ntv2utils.h"
11 #include "ajatypes.h"
14 #include "ajabase/system/process.h"
15 #include "ajabase/common/common.h"
16 #include "ntv2dolbycapture.h"
17 #include <signal.h>
18 #include <iostream>
19 #include <iomanip>
20 
21 using namespace std;
22 
23 
24 // Globals
25 static bool gGlobalQuit (false); // Set this "true" to exit gracefully
26 
27 
28 static void SignalHandler (int inSignal)
29 {
30  (void) inSignal;
31  gGlobalQuit = true;
32 }
33 
34 
35 int main (int argc, const char ** argv)
36 {
38  char * pDeviceSpec (AJA_NULL); // Device specifier string, if any
39  char * pInputSrcSpec (AJA_NULL); // SDI source spec
40  char * pPixelFormat (AJA_NULL); // Pixel format argument
41  char * pFramesSpec (AJA_NULL); // AutoCirculate frames spec
42  int doRecordAnc (0); // Record anc
43  int doRecordAudio (0); // Record audio
44  int doRecordDolby (0); // Record dolby
45  uint32_t channelNumber (1); // Channel/FrameStore to use
46  int doAudioFilter (0); // Enable anc audio filter?
47  int doFrameData (0); // Output per frame data?
48  int doMultiFormat (0); // Enable multi-format?
50 
51  // Command line option descriptions:
52  const CNTV2DemoCommon::PoptOpts optionsTable [] =
53  {
54  #if !defined(NTV2_DEPRECATE_16_0) // --board option is deprecated!
55  {"board", 'b', POPT_ARG_STRING, &pDeviceSpec, 0, "device to use", "(deprecated)" },
56  #endif
57  {"device", 'd', POPT_ARG_STRING, &pDeviceSpec, 0, "device to use", "index#, serial#, or model" },
58  {"input", 'i', POPT_ARG_STRING, &pInputSrcSpec, 0, "which HDMI input", "?=list" },
59  {"pixelFormat", 'p', POPT_ARG_STRING, &pPixelFormat, 0, "pixel format to use", "'?' or 'list' to list" },
60  {"frames", 0, POPT_ARG_STRING, &pFramesSpec, 0, "frames to AutoCirculate", "num[@min] or min-max" },
61  {"anc", 'a', POPT_ARG_NONE, &doRecordAnc, 0, "record anc to file", AJA_NULL },
62  {"audio", 'f', POPT_ARG_NONE, &doRecordAudio, 0, "record audio to file", AJA_NULL },
63  {"dolby", 'g', POPT_ARG_NONE, &doRecordDolby, 0, "record dolby to file", AJA_NULL },
64  {"audioFilter", 'x', POPT_ARG_NONE, &doAudioFilter, 0, "only capture audio anc", AJA_NULL },
65  {"frameData", 'y', POPT_ARG_NONE, &doFrameData, 0, "show per-frame stats", AJA_NULL },
66  {"channel", 'c', POPT_ARG_INT, &channelNumber, 0, "channel to use", "1-8" },
67  {"multiFormat", 'm', POPT_ARG_NONE, &doMultiFormat, 0, "use multi-format/channel", AJA_NULL },
70  };
71  CNTV2DemoCommon::Popt popt(argc, argv, optionsTable);
72  if (!popt)
73  {cerr << "## ERROR: " << popt.errorStr() << endl; return 2;}
74 
75  const string deviceSpec (pDeviceSpec ? pDeviceSpec : "0");
76  const string inputSourceStr (pInputSrcSpec ? CNTV2DemoCommon::ToLower(string(pInputSrcSpec)) : "");
77  const string pixelFormatStr (pPixelFormat ? pPixelFormat : "");
78  const string framesSpec (pFramesSpec ? pFramesSpec : "");
79  string recordAncFile ("disabled");
80  string recordAudioFile ("disabled");
81  string recordDolbyFile ("disabled");
82 
83  // Device
84  const string legalDevices(CNTV2DemoCommon::GetDeviceStrings());
85  if (deviceSpec == "?" || deviceSpec == "list")
86  {cout << legalDevices << endl; return 0;}
87  if (!CNTV2DemoCommon::IsValidDevice(deviceSpec))
88  {cout << "## ERROR: No such device '" << deviceSpec << "'" << endl << legalDevices; return 1;}
89 
90  DolbyConfig config(deviceSpec);
91 
92  // Channel
93  if ((channelNumber < 1) || (channelNumber > 8))
94  {cerr << "## ERROR: Invalid channel number " << channelNumber << " -- expected 1 thru 8" << endl; return 1;}
95  config.fInputChannel = NTV2Channel(channelNumber - 1);
96 
97  // Input source
98  const string legalSources(CNTV2DemoCommon::GetInputSourceStrings(NTV2_IOKINDS_HDMI, deviceSpec));
99  if (inputSourceStr == "?" || inputSourceStr == "list")
100  {cout << legalSources << endl; return 0;}
101  if (!inputSourceStr.empty())
102  {
105  {cerr << "## ERROR: Input source '" << inputSourceStr << "' not one of:" << endl << legalSources << endl; return 1;}
106  } // if input source specified
107 
108  // Pixel Format
109  const NTV2PixelFormat pixelFormat (pixelFormatStr.empty() ? NTV2_FBF_8BIT_YCBCR : CNTV2DemoCommon::GetPixelFormatFromString(pixelFormatStr));
110  if (pixelFormatStr == "?" || pixelFormatStr == "list")
111  {cout << CNTV2DemoCommon::GetPixelFormatStrings(PIXEL_FORMATS_ALL, deviceSpec) << endl; return 0;}
112  else if (!pixelFormatStr.empty() && !NTV2_IS_VALID_FRAME_BUFFER_FORMAT(pixelFormat))
113  {
114  cerr << "## ERROR: Invalid '--pixelFormat' value '" << pixelFormatStr << "' -- expected values:" << endl
116  return 2;
117  }
118 
119  // AutoCirculate Frames
120  static const string legalFramesSpec("{frameCount}[@{firstFrameNum}] or {firstFrameNum}-{lastFrameNum}");
121  if (!framesSpec.empty())
122  {
123  const string parseResult(config.fFrames.setFromString(framesSpec));
124  if (!parseResult.empty())
125  {cerr << "## ERROR: Bad 'frames' spec '" << framesSpec << "'\n## " << parseResult << endl; return 1;}
126  }
127  if (!config.fFrames.valid())
128  {cerr << "## ERROR: Bad 'frames' spec '" << framesSpec << "'\n## Expected " << legalFramesSpec << endl; return 1;}
129 
130  // Anc Capture
131  if (doRecordAnc)
132  { // User specified an empty filePath -- invent a file name...
133  ostringstream fileName; fileName << "ntv2dolbycapture-" << deviceSpec << "-"
134  << ::NTV2ChannelToString(config.fInputChannel,true) << "-" << AJAProcess::GetPid() << ".anc";
135  recordAncFile = fileName.str();
136  }
137 
138  // Audio Capture
139  if (doRecordAudio)
140  { // User specified an empty filePath -- invent a file name...
141  ostringstream fileName; fileName << "ntv2dolbycapture-" << deviceSpec << "-"
142  << ::NTV2ChannelToString(config.fInputChannel,true) << "-" << AJAProcess::GetPid() << ".raw";
143  recordAudioFile = fileName.str();
144  }
145 
146  // Dolby Capture
147  if (doRecordDolby)
148  { // User specified an empty filePath -- invent a file name...
149  ostringstream fileName; fileName << "ntv2dolbycapture-" << deviceSpec << "-"
150  << ::NTV2ChannelToString(config.fInputChannel,true) << "-" << AJAProcess::GetPid() << ".ec3";
151  recordDolbyFile = fileName.str();
152  }
153 
154  // Instantiate the ntv2dolbycapture object, using the specified AJA device...
155  config.fDeviceSpec = deviceSpec;
156  config.fPixelFormat = pixelFormat;
157  config.fAncDataFilePath = recordAncFile;
158  config.fAudioDataFilePath = recordAudioFile;
159  config.fDolbyDataFilePath = recordDolbyFile;
160  config.fDoAudioFilter = doAudioFilter ? true : false;
161  config.fDoFrameData = doFrameData ? true : false;
162  config.fDoMultiFormat = doMultiFormat ? true : false;
163  config.fWithAnc = doRecordAnc ? true : false;
164  config.fWithAudio = doRecordAudio ? true : false;
165  config.fWithDolby = doRecordDolby ? true : false;
166  //cerr << "Specified Configuration:" << endl << config << endl;
167 
168  ::signal (SIGINT, SignalHandler);
169  #if defined (AJAMac)
170  ::signal (SIGHUP, SignalHandler);
171  ::signal (SIGQUIT, SignalHandler);
172  #endif
173 
174  NTV2DolbyCapture capturer(config);
175 
176  // Initialize the ntv2dolbycapture instance...
177  status = capturer.Init();
178  if (AJA_FAILURE(status))
179  {cout << "## ERROR: Capture initialization failed with status " << status << endl; return 1;}
180 
181  // Run the capturer...
182  capturer.Run();
183 
184  cout << " Capture Capture" << endl
185  << " Frames Frames Buffer" << endl
186  << "Processed Dropped Level" << endl;
187  // Poll its status until stopped...
188  do
189  {
190  ULWord framesProcessed, framesDropped, bufferLevel;
191  capturer.GetACStatus (framesProcessed, framesDropped, bufferLevel);
192  cout << setw(9) << framesProcessed << setw(9) << framesDropped << setw(9) << bufferLevel << "\r" << flush;
193  AJATime::Sleep(2000);
194  } while (!gGlobalQuit); // loop til quit time
195 
196  cout << endl;
197 
198  return AJA_SUCCESS(status) ? 0 : 1;
199 
200 } // main
ntv2dolbycapture.h
Declares the NTV2DolbyCapture class.
CNTV2DemoCommon::GetInputSourceFromString
static NTV2InputSource GetInputSourceFromString(const std::string &inStr)
Returns the NTV2InputSource that matches the given string.
Definition: ntv2democommon.cpp:713
poptOption
Definition: options_popt.h:148
DolbyConfig::fDoAudioFilter
bool fDoAudioFilter
If true, capture only audio anc.
Definition: ntv2dolbycapture.h:37
DolbyConfig::fDeviceSpec
std::string fDeviceSpec
The AJA device to use.
Definition: ntv2dolbycapture.h:29
DolbyConfig::fDolbyDataFilePath
std::string fDolbyDataFilePath
Optional path to Dolby binary data file.
Definition: ntv2dolbycapture.h:32
NTV2Channel
NTV2Channel
These enum values are mostly used to identify a specific Frame Store. They're also commonly used to i...
Definition: ntv2enums.h:1305
AJA_STATUS_SUCCESS
@ AJA_STATUS_SUCCESS
Definition: types.h:368
NTV2DolbyCapture::GetACStatus
virtual void GetACStatus(ULWord &outGoodFrames, ULWord &outDroppedFrames, ULWord &outBufferLevel)
Provides status information about my input (capture) process.
Definition: ntv2dolbycapture.cpp:587
NTV2DolbyCapture::Init
virtual AJAStatus Init(void)
Initializes me and prepares me to Run.
Definition: ntv2dolbycapture.cpp:74
systemtime.h
Declares the AJATime class.
SignalHandler
void SignalHandler(int inSignal)
Definition: main.cpp:26
DolbyConfig::fDoFrameData
bool fDoFrameData
if true, output per frame statistics
Definition: ntv2dolbycapture.h:38
DolbyConfig::fInputChannel
NTV2Channel fInputChannel
The device channel to use.
Definition: ntv2dolbycapture.h:33
DolbyConfig
This class is used to configure an NTV2Capture instance.
Definition: ntv2dolbycapture.h:26
DolbyConfig::fInputSource
NTV2InputSource fInputSource
The device input connector to use.
Definition: ntv2dolbycapture.h:34
NTV2_IOKINDS_HDMI
@ NTV2_IOKINDS_HDMI
Specifies HDMI input/output kinds.
Definition: ntv2enums.h:1252
DolbyConfig::fDoMultiFormat
bool fDoMultiFormat
If true, use multi-format/multi-channel mode, if device supports it; otherwise normal mode.
Definition: ntv2dolbycapture.h:39
NTV2FrameBufferFormat
NTV2FrameBufferFormat
Identifies a particular video frame buffer format. See Device Frame Buffer Formats for details.
Definition: ntv2enums.h:207
main
int main(int argc, const char **argv)
Definition: main.cpp:30
NTV2ACFrameRange::setFromString
std::string setFromString(const std::string &inStr)
Definition: ntv2utils.cpp:4155
POPT_ARG_INT
#define POPT_ARG_INT
Definition: options_popt.h:58
ajatypes.h
Declares the most fundamental data types used by NTV2. Since Windows NT was the first principal devel...
CNTV2DemoCommon::GetDeviceStrings
static std::string GetDeviceStrings(const NTV2DeviceKinds inKinds=NTV2_DEVICEKIND_ALL)
Definition: ntv2democommon.cpp:493
POPT_AUTOHELP
#define POPT_AUTOHELP
Definition: options_popt.h:220
CNTV2DemoCommon::IsValidDevice
static bool IsValidDevice(const std::string &inDeviceSpec)
Definition: ntv2democommon.cpp:433
AJAStatus
AJAStatus
Definition: types.h:365
process.h
Declares the AJAProcess class.
AJATime::Sleep
static void Sleep(const int32_t inMilliseconds)
Suspends execution of the current thread for a given number of milliseconds.
Definition: systemtime.cpp:284
ULWord
uint32_t ULWord
Definition: ajatypes.h:246
CNTV2DemoCommon::ToLower
static std::string ToLower(const std::string &inStr)
Returns the given string after converting it to lower case.
Definition: ntv2democommon.cpp:937
CNTV2DemoCommon::GetPixelFormatFromString
static NTV2FrameBufferFormat GetPixelFormatFromString(const std::string &inStr)
Returns the NTV2FrameBufferFormat that matches the given string.
Definition: ntv2democommon.cpp:660
AJAProcess::GetPid
static uint64_t GetPid()
Definition: process.cpp:35
PIXEL_FORMATS_ALL
@ PIXEL_FORMATS_ALL
Definition: ntv2democommon.h:240
options_popt.h
NTV2_IS_VALID_INPUT_SOURCE
#define NTV2_IS_VALID_INPUT_SOURCE(_inpSrc_)
Definition: ntv2enums.h:1243
NTV2_FBF_8BIT_YCBCR
@ NTV2_FBF_8BIT_YCBCR
See 8-Bit YCbCr Format.
Definition: ntv2enums.h:211
CNTV2DemoCommon::Popt
Definition: ntv2democommon.h:801
ntv2utils.h
Declares numerous NTV2 utility functions.
CNTV2DemoCommon::Popt::errorStr
virtual const std::string & errorStr(void) const
Definition: ntv2democommon.h:809
AJA_SUCCESS
#define AJA_SUCCESS(_status_)
Definition: types.h:357
DolbyConfig::fWithAnc
bool fWithAnc
If true, also capture Anc.
Definition: ntv2dolbycapture.h:40
AJADebug::Open
static AJAStatus Open(bool incrementRefCount=false)
Definition: debug.cpp:44
AJA_NULL
#define AJA_NULL
Definition: ajatypes.h:190
DolbyConfig::fFrames
CNTV2DemoCommon::ACFrameRange fFrames
AutoCirculate frame count or range.
Definition: ntv2dolbycapture.h:35
common.h
Private include file for all ajabase sources.
DolbyConfig::fWithDolby
bool fWithDolby
If true, also capture Dolby.
Definition: ntv2dolbycapture.h:42
CNTV2DemoCommon::GetInputSourceStrings
static std::string GetInputSourceStrings(const NTV2IOKinds inKinds=NTV2_IOKINDS_ALL, const std::string inDeviceSpecifier=std::string())
Definition: ntv2democommon.cpp:685
gGlobalQuit
static bool gGlobalQuit((0))
NTV2DolbyCapture::Run
virtual AJAStatus Run(void)
Runs me.
Definition: ntv2dolbycapture.cpp:349
POPT_TABLEEND
#define POPT_TABLEEND
Definition: options_popt.h:215
true
#define true
Definition: ntv2devicefeatures.h:26
CNTV2DemoCommon::GetPixelFormatStrings
static std::string GetPixelFormatStrings(const NTV2PixelFormatKinds inKinds=PIXEL_FORMATS_ALL, const std::string inDeviceSpecifier=std::string())
Definition: ntv2democommon.cpp:603
DolbyConfig::fWithAudio
bool fWithAudio
If true, also capture Audio.
Definition: ntv2dolbycapture.h:41
NTV2ACFrameRange::valid
bool valid(void) const
Definition: ntv2utils.h:977
NTV2ChannelToString
std::string NTV2ChannelToString(const NTV2Channel inValue, const bool inForRetailDisplay=false)
Definition: ntv2utils.cpp:5759
DolbyConfig::fPixelFormat
NTV2PixelFormat fPixelFormat
Pixel format to use.
Definition: ntv2dolbycapture.h:36
NTV2DolbyCapture
Instances of me capture frames in real time from a video signal provided to an input of an AJA device...
Definition: ntv2dolbycapture.h:72
NTV2_IS_VALID_FRAME_BUFFER_FORMAT
#define NTV2_IS_VALID_FRAME_BUFFER_FORMAT(__s__)
Definition: ntv2enums.h:251
AJA_FAILURE
#define AJA_FAILURE(_status_)
Definition: types.h:358
DolbyConfig::fAudioDataFilePath
std::string fAudioDataFilePath
Optional path to Audio binary data file.
Definition: ntv2dolbycapture.h:31
DolbyConfig::fAncDataFilePath
std::string fAncDataFilePath
Optional path to Anc binary data file.
Definition: ntv2dolbycapture.h:30
POPT_ARG_STRING
#define POPT_ARG_STRING
Definition: options_popt.h:57
POPT_ARG_NONE
#define POPT_ARG_NONE
Definition: options_popt.h:56