AJA NTV2 SDK  17.0.1.1246
NTV2 SDK 17.0.1.1246
ntv2demohevccommon.cpp
Go to the documentation of this file.
1 /* SPDX-License-Identifier: MIT */
8 #include "ntv2demohevccommon.h"
9 #include "ntv2devicescanner.h"
10 #include "ntv2devicefeatures.h"
11 #include "ntv2utils.h"
12 #include <assert.h>
13 #include <map>
14 #include <sys/stat.h>
15 
16 
17 using namespace std;
18 
19 
20 #if defined (MSWindows)
21  #define FILENO_FUNCTION _fileno
22 #else
23  #define FILENO_FUNCTION fileno
24 #endif
25 
26 
28 : mHevcFd (NULL),
29  mEncFd (NULL),
30  mAiffFd (NULL),
31  mYuvFd (NULL),
32  mRawFd (NULL),
33  mHevcFileFrameCount (0),
34  mMaxHevcFrames (0),
35  mEncFileFrameCount (0),
36  mMaxEncFrames (0),
37  mAiffFileFrameCount (0),
38  mMaxAiffFrames (0),
39  mAiffTotalSize (0),
40  mAiffNumSamples (0),
41  mAiffNumChannels (0),
42  mAiffWriteBuffer (0),
43  mYuvFileSize (0),
44  mYuvFrameWidth (0),
45  mYuvFrameHeight (0),
46  mYuvNumTotalFrames (0),
47  mYuvFrameSize (0),
48  mRawFileFrameCount (0),
49  mMaxRawFrames (0)
50 
51 {
52 }
53 
54 
56 {
57  // Just in case
58  CloseHevcFile();
59  CloseEncFile();
60  CloseAiffFile();
62 
63 }
64 
66 {
67  switch (pixelFormat)
68  {
75  default: break;
76  }
77 
79 }
80 
81 
83 {
84  switch (frameRate)
85  {
96  default: break;
97  }
98 
99  return AJA_FrameRate_Unknown;
100 }
101 
102 
103 AJAStatus CNTV2DemoHevcCommon::CreateHevcFile(const string & inFileName, uint32_t maxFrames)
104 {
105  // Close file if already open
106  if (mHevcFd != NULL)
107  {
108  CloseHevcFile();
109  }
110 
111  // Open binary output
112  mHevcFd = fopen(inFileName.c_str(), "wb");
113  if (mHevcFd == NULL)
114  {
115  return AJA_STATUS_OPEN;
116  }
117  mMaxHevcFrames = maxFrames;
118 
119  return AJA_STATUS_SUCCESS;
120 }
121 
122 
124 {
125  if (mHevcFd != NULL)
126  {
127  fclose(mHevcFd);
128  mHevcFd = NULL;
129  mMaxHevcFrames = 0;
130  }
131 }
132 
133 
134 void CNTV2DemoHevcCommon::WriteHevcData(void* pBuffer, uint32_t bufferSize)
135 {
136  if ((mHevcFd == NULL) ||
137  (pBuffer == NULL) ||
138  (bufferSize == 0) ||
139  (mMaxHevcFrames == 0)) return;
140 
141  if((mMaxHevcFrames != 0xffffffff) && (mHevcFileFrameCount > mMaxHevcFrames))
142  {
143  fseek (mHevcFd, 0, SEEK_SET);
144  mHevcFileFrameCount = 0;
145  }
146 
147  fwrite(pBuffer, 1, bufferSize, mHevcFd);
148  mHevcFileFrameCount++;
149 }
150 
151 
152 AJAStatus CNTV2DemoHevcCommon::CreateEncFile(const string & inFileName, uint32_t maxFrames)
153 {
154  // Close file if already open
155  if (mEncFd != NULL)
156  {
157  CloseEncFile();
158  }
159 
160  // Open text output
161  mEncFd = fopen(inFileName.c_str(), "w");
162  if (mEncFd == NULL)
163  {
164  return AJA_STATUS_OPEN;
165  }
166  mMaxEncFrames = maxFrames;
167 
168  return AJA_STATUS_SUCCESS;
169 }
170 
171 
173 {
174  if (mEncFd != NULL)
175  {
176  fclose(mEncFd);
177  mEncFd = NULL;
178  mMaxEncFrames = 0;
179  }
180 }
181 
182 
183 void CNTV2DemoHevcCommon::WriteEncData(void* pBuffer, uint32_t bufferSize)
184 {
185  if ((mEncFd == NULL) ||
186  (pBuffer == NULL) ||
187  (bufferSize < sizeof(HevcEncodedData)) ||
188  (mMaxEncFrames == 0)) return;
189 
190  if((mMaxEncFrames != 0xffffffff) && (mEncFileFrameCount > mMaxEncFrames))
191  {
192  fseek (mEncFd, 0, SEEK_SET);
193  mEncFileFrameCount = 0;
194  }
195 
196  HevcEncodedData* pEncData = (HevcEncodedData*)pBuffer;
197 
198  const char* pPicType = "Unknown";
199  switch(pEncData->pictureType)
200  {
201  case 0: pPicType = "I-frame"; break;
202  case 1: pPicType = "P-frame"; break;
203  case 2: pPicType = "B-frame"; break;
204  default: break;
205  }
206  const char* pIdrState = "Unknown";
207  switch(pEncData->esIdrType)
208  {
209  case 0: pIdrState = "false"; break;
210  case 1: pIdrState = "true"; break;
211  case 3: pIdrState = "command"; break;
212  default: break;
213  }
214 
215  long long unsigned int offset = (((uint64_t)pEncData->esOffsetHigh)<<32) + (uint64_t)pEncData->esOffsetLow;
216  long long int pts = ((((uint64_t)pEncData->ptsValueHigh)<<32) + (uint64_t)pEncData->ptsValueLow);
217  long long int dts = ((((uint64_t)pEncData->dtsValueHigh)<<32) + (uint64_t)pEncData->dtsValueLow);
218 
219  fprintf(mEncFd, "Serial number: %d Picture type: %s IDR state: %s Last frame: %s\n",
220  pEncData->serialNumber, pPicType, pIdrState, pEncData->esLastFrame?"Y":"N");
221  fprintf(mEncFd, "Frame offset: 0x%0llx Frame Size: %d\n",
222  offset, pEncData->esSize);
223  fprintf(mEncFd, "PTS: %lld DTS: %lld\n",
224  pts, dts);
225  fprintf(mEncFd, "Temporal ID: %d NAL offset: 0x%x\n",
226  pEncData->temporalId, pEncData->nalOffset);
227  fprintf(mEncFd, "CPB data size: %d Additional data: %d\n\n",
228  pEncData->cpbValue, pEncData->numAdditionalData);
229 
230  mEncFileFrameCount++;
231 }
232 
233 
234 AJAStatus CNTV2DemoHevcCommon::CreateAiffFile(const string & inFileName, uint32_t numChannels, uint32_t maxFrames, uint32_t bufferSize)
235 {
236  // Close file if already open
237  if (mAiffFd != NULL)
238  {
239  CloseAiffFile();
240  }
241 
242  // Open binary output
243  mAiffFd = fopen(inFileName.c_str(), "wb");
244  if (mAiffFd == NULL)
245  {
246  return AJA_STATUS_OPEN;
247  }
248 
249  // Allocate copy buffer
250  mAiffWriteBuffer = new uint8_t [bufferSize];
251  if (mAiffWriteBuffer == NULL)
252  {
253  return AJA_STATUS_OPEN;
254  }
255 
256  // Initialize channels and counts for header
257  mAiffTotalSize = 0;
258  mAiffNumSamples = 0;
259  mAiffNumChannels = numChannels;
260  mMaxAiffFrames = maxFrames;
261 
262  // Write AIFF header with 0 sizes
263  WriteAiffHeader ();
264 
265  return AJA_STATUS_SUCCESS;
266 }
267 
268 
270 {
271  if (mAiffFd != NULL)
272  {
273  // Rewrite the header with final sizes
274  WriteAiffHeader ();
275  fclose(mAiffFd);
276  mAiffFd = NULL;
277  mMaxAiffFrames = 0;
278  }
279 
280  if (mAiffWriteBuffer != NULL)
281  {
282  delete [] mAiffWriteBuffer;
283  mAiffWriteBuffer = NULL;
284  }
285 }
286 
287 
289 {
290  if (mAiffFd == NULL) return;
291 
292  fseek (mAiffFd, 0, SEEK_SET);
293 
294  // Write the form chunk
295  fprintf (mAiffFd,"FORM");
296  fputc ((int)((mAiffTotalSize >> 24) & 0xff), mAiffFd); // Total file size
297  fputc ((int)((mAiffTotalSize >> 16) & 0xff), mAiffFd);
298  fputc ((int)((mAiffTotalSize >> 8) & 0xff), mAiffFd);
299  fputc ((int)((mAiffTotalSize >> 0) & 0xff), mAiffFd);
300  fprintf (mAiffFd,"AIFF");
301 
302  mAiffTotalSize = 4;
303 
304  // Write the common chunk
305  fprintf (mAiffFd,"COMM");
306  fputc (0, mAiffFd); // Chunk size
307  fputc (0, mAiffFd);
308  fputc (0, mAiffFd);
309  fputc (18, mAiffFd);
310  fputc(0, mAiffFd); // Number of channels
311  fputc (mAiffNumChannels, mAiffFd);
312  fputc ((int)((mAiffNumSamples >> 24) & 0xff), mAiffFd); // Number of samples
313  fputc ((int)((mAiffNumSamples >> 16) & 0xff), mAiffFd);
314  fputc ((int)((mAiffNumSamples >> 8) & 0xff), mAiffFd);
315  fputc ((int)((mAiffNumSamples >> 0) & 0xff), mAiffFd);
316  fputc (0, mAiffFd); // 16 bit
317  fputc (16, mAiffFd);
318  fputc (0x40, mAiffFd); // 48000 kHz (10 byte sample rate) */
319  fputc (0x0e, mAiffFd);
320  fputc (0xbb, mAiffFd);
321  fputc (0x80, mAiffFd);
322  fputc (0, mAiffFd);
323  fputc (0, mAiffFd);
324  fputc (0, mAiffFd);
325  fputc (0, mAiffFd);
326  fputc (0, mAiffFd);
327  fputc (0, mAiffFd);
328 
329  mAiffTotalSize += 26;
330 
331  // Compute SSND chunk size
332  uint32_t chunkSize = 2*mAiffNumChannels*mAiffNumSamples + 8;
333 
334  // Write the sound data chunk
335  fprintf (mAiffFd,"SSND");
336  fputc ((int)((chunkSize >> 24) & 0xff), mAiffFd); // Chunk size
337  fputc ((int)((chunkSize >> 16) & 0xff), mAiffFd);
338  fputc ((int)((chunkSize >> 8) & 0xff), mAiffFd);
339  fputc ((int)((chunkSize >> 0) & 0xff), mAiffFd);
340  fputc (0, mAiffFd); // Audio offset
341  fputc (0, mAiffFd);
342  fputc (0, mAiffFd);
343  fputc (0, mAiffFd);
344  fputc (0, mAiffFd); // Audio block size
345  fputc (0, mAiffFd);
346  fputc (0, mAiffFd);
347  fputc (0, mAiffFd);
348 
349  mAiffTotalSize += 16;
350 
351  mAiffNumSamples = 0;
352 }
353 
354 
355 void CNTV2DemoHevcCommon::WriteAiffData(void* pBuffer, uint32_t numChannels, uint32_t numSamples)
356 {
357  uint8_t* pAudio = (uint8_t*)pBuffer;
358  uint8_t* pAiff = mAiffWriteBuffer;
359 
360  if ((mAiffFd == NULL) ||
361  (pAiff == NULL) ||
362  (numChannels == 0) ||
363  (numSamples == 0) ||
364  (mMaxAiffFrames == 0)) return;
365 
366  if((mMaxAiffFrames != 0xffffffff) && (mAiffFileFrameCount > mMaxAiffFrames))
367  {
368  fseek (mAiffFd, 0, SEEK_SET);
369  mAiffFileFrameCount = 0;
370  }
371 
372  // Copy audio from AJA buffer to AIFF buffer
373  for (uint32_t is = 0; is < numSamples; is++)
374  {
375  for (uint32_t ic = 0; ic < numChannels; ic++)
376  {
377  if (ic < mAiffNumChannels)
378  {
379  *pAiff++ = *(pAudio+3);
380  *pAiff++ = *(pAudio+2);
381  }
382  pAudio += 4;
383  }
384  }
385 
386  // Compute AIFF audio size
387  uint32_t audioSize = 2*mAiffNumChannels*numSamples;
388 
389  // Write audio to AIFF file
390  fwrite(mAiffWriteBuffer, 1, audioSize, mAiffFd);
391 
392  // Increment AIFF header counts
393  mAiffNumSamples += numSamples;
394  mAiffTotalSize += audioSize;
395 
396  mAiffFileFrameCount++;
397 }
398 
399 
400 AJAStatus CNTV2DemoHevcCommon::OpenYuv420File(const string & inFileName, const uint32_t width, const uint32_t height)
401 {
402  struct stat fileStat;
403 
404  // Close file if already open
405  if (mYuvFd != NULL)
406  {
407  CloseYuv420File();
408  }
409 
410  // Open binary output
411  mYuvFd = fopen(inFileName.c_str(), "rb");
412  if (mYuvFd == NULL)
413  {
414  return AJA_STATUS_OPEN;
415  }
416 
417  // Get the size of the file using fstat
418  fstat(FILENO_FUNCTION(mYuvFd), &fileStat);
419  mYuvFileSize = fileStat.st_size;
420 
421  // Now save width, height and calculate frame size and number of frames
422  mYuvFrameWidth = width;
423  mYuvFrameHeight = height;
424  mYuvFrameSize = (width * height) + ((width * height) / 2);
425  mYuvNumTotalFrames = uint32_t(mYuvFileSize / mYuvFrameSize);
426 
427  return AJA_STATUS_SUCCESS;
428 }
429 
430 
432 {
433  if (mYuvFd != NULL)
434  {
435  fclose(mYuvFd);
436  mYuvFd = NULL;
437  mYuvFileSize = 0;
438  mYuvFrameWidth = 0;
439  mYuvFrameHeight = 0;
440  mYuvFrameSize = 0;
441  mYuvNumTotalFrames = 0;
442  }
443 }
444 
445 
446 AJAStatus CNTV2DemoHevcCommon::ReadYuv420Frame(void* pBuffer, uint32_t numFrame)
447 {
448  size_t result;
449 
450  if (numFrame > mYuvNumTotalFrames)
451  {
452  return AJA_STATUS_RANGE;
453  }
454 
455  // Seek to the frame
456  //fseek(mYuvFd, (mYuvFrameSize * numFrame), SEEK_SET);
457 
458  // Read the Y plane
459  result = fread(pBuffer, 1, mYuvFrameSize, mYuvFd);
460  if (result != (mYuvFrameSize))
461  return AJA_STATUS_FAIL;
462 
463  return AJA_STATUS_SUCCESS;
464 }
465 
466 
467 AJAStatus CNTV2DemoHevcCommon::ConvertYuv420FrameToNV12(void* pSrcBuffer, void* pDstBuffer, uint32_t bufferSize)
468 {
469  // Sanity check make sure they pass in a proper sized buffer
470  if (bufferSize != mYuvFrameSize)
471  return AJA_STATUS_FAIL;
472 
473  // Copy over Y plane
474  memcpy(pDstBuffer, pSrcBuffer, (mYuvFrameWidth * mYuvFrameHeight));
475 
476  char * srcCbPtr;
477  char * srcCrPtr;
478  char * destCbCrPtr;
479 
480  srcCbPtr = (char*)pSrcBuffer + (mYuvFrameWidth * mYuvFrameHeight);
481  srcCrPtr = (char*)pSrcBuffer + (mYuvFrameWidth * mYuvFrameHeight) + ((mYuvFrameWidth * mYuvFrameHeight) / 4);
482  destCbCrPtr = (char*)pDstBuffer + (mYuvFrameWidth * mYuvFrameHeight);
483 
484  // Now interleave Cb and Cr planes
485  for (uint32_t i = 0; i< (mYuvFrameWidth * mYuvFrameHeight) / 4; i++)
486  {
487  *destCbCrPtr++ = *srcCbPtr++;
488  *destCbCrPtr++ = *srcCrPtr++;
489  }
490 
491  return AJA_STATUS_SUCCESS;
492 }
493 
494 
495 AJAStatus CNTV2DemoHevcCommon::CreateRawFile(const string & inFileName, uint32_t maxFrames)
496 {
497  // Close file if already open
498  if (mRawFd != NULL)
499  {
500  CloseRawFile();
501  }
502 
503  // Open binary output
504  mRawFd = fopen(inFileName.c_str(), "wb");
505  if (mRawFd == NULL)
506  {
507  return AJA_STATUS_OPEN;
508  }
509  mMaxRawFrames = maxFrames;
510 
511  return AJA_STATUS_SUCCESS;
512 }
513 
514 
516 {
517  if (mRawFd != NULL)
518  {
519  fclose(mRawFd);
520  mRawFd = NULL;
521  mMaxRawFrames = 0;
522  }
523 }
524 
525 
526 void CNTV2DemoHevcCommon::WriteRawData(void* pBuffer, uint32_t bufferSize)
527 {
528  if ((mRawFd == NULL) ||
529  (pBuffer == NULL) ||
530  (bufferSize == 0) ||
531  (mMaxRawFrames == 0)) return;
532 
533  if((mMaxRawFrames != 0xffffffff) && (mRawFileFrameCount > mMaxRawFrames))
534  {
535  fseek (mRawFd, 0, SEEK_SET);
536  mRawFileFrameCount = 0;
537  }
538 
539  fwrite(pBuffer, 1, bufferSize, mRawFd);
540  mRawFileFrameCount++;
541 }
542 
543 
544 uint32_t CNTV2DemoHevcCommon::AlignDataBuffer(void* pBuffer, uint32_t bufferSize, uint32_t dataSize, uint32_t alignBytes, uint8_t fill)
545 {
546  if ((pBuffer == NULL) ||
547  (bufferSize == 0) ||
548  (dataSize == 0)) return 0;
549 
550  if (alignBytes == 0) return dataSize;
551 
552  // round up to aligned size
553  uint32_t alignSize = ((dataSize - 1)/alignBytes + 1)*alignBytes;
554  if (alignSize > bufferSize) return dataSize;
555 
556  // fill to aligned size
557  uint8_t* pData = (uint8_t*)pBuffer;
558  for (uint32_t i = dataSize; i < alignSize; i++)
559  {
560  pData[i] = fill;
561  }
562 
563  return alignSize;
564 }
565 
566 
568 {
569  if (sdiFormat == NTV2_FORMAT_UNKNOWN)
570  return AJA_STATUS_FAIL;
571 
572  switch (sdiFormat)
573  {
575  videoFormat = NTV2_FORMAT_1080p_2398;
576  if (quad) videoFormat = NTV2_FORMAT_4x1920x1080p_2398;
577  break;
579  videoFormat = NTV2_FORMAT_1080p_2400;
580  if (quad) videoFormat = NTV2_FORMAT_4x1920x1080p_2400;
581  break;
583  videoFormat = NTV2_FORMAT_1080p_2500;
584  if (quad) videoFormat = NTV2_FORMAT_4x1920x1080p_2500;
585  break;
587  videoFormat = NTV2_FORMAT_1080p_2997;
588  if (quad) videoFormat = NTV2_FORMAT_4x1920x1080p_2997;
589  break;
591  videoFormat = NTV2_FORMAT_1080p_3000;
592  if (quad) videoFormat = NTV2_FORMAT_4x1920x1080p_3000;
593  break;
596  videoFormat = NTV2_FORMAT_1080p_5000_A;
597  if (quad) videoFormat = NTV2_FORMAT_4x1920x1080p_5000;
598  break;
601  videoFormat = NTV2_FORMAT_1080p_5994_A;
602  if (quad) videoFormat = NTV2_FORMAT_4x1920x1080p_5994;
603  break;
606  videoFormat = NTV2_FORMAT_1080p_6000_A;
607  if (quad) videoFormat = NTV2_FORMAT_4x1920x1080p_6000;
608  break;
609  default:
610  videoFormat = sdiFormat;
611  break;
612  }
613 
614  return AJA_STATUS_SUCCESS;
615 }
616 
617 
618 AJAStatus CNTV2DemoHevcCommon::SetupHEVC (CNTV2m31 * pM31, M31VideoPreset preset, M31Channel encodeChannel, bool multiStream, bool withInfo)
619 {
620  HevcMainState mainState;
621  HevcEncodeMode encodeMode;
622  HevcVinState vInState;
623  HevcEhState ehState;
624 
625  if (multiStream)
626  {
627  pM31->GetMainState(&mainState, &encodeMode);
628  if ((mainState != Hevc_MainState_Encode) || (encodeMode != Hevc_EncodeMode_Multiple))
629  {
630  // Here we need to start up the M31 so we reset the part then go into the init state
631  if (!pM31->Reset())
632  { cerr << "## ERROR: Reset of M31 failed" << endl; return AJA_STATUS_INITIALIZE; }
633 
634  // After a reset we should be in the boot state so lets check this
635  pM31->GetMainState(&mainState);
636  if (mainState != Hevc_MainState_Boot)
637  { cerr << "## ERROR: Not in boot state after reset" << endl; return AJA_STATUS_INITIALIZE; }
638 
639  // Now go to the init state
640  if (!pM31->ChangeMainState(Hevc_MainState_Init, Hevc_EncodeMode_Multiple))
641  { cerr << "## ERROR: ChangeMainState to init failed" << endl; return AJA_STATUS_INITIALIZE; }
642 
643  pM31->GetMainState(&mainState);
644  if (mainState != Hevc_MainState_Init)
645  { cerr << "## ERROR: Not in init state after change" << endl; return AJA_STATUS_INITIALIZE; }
646 
647  // Now lets configure the device for a given preset. First we must clear out all of the params which
648  // is necessary since the param space is basically uninitialized memory.
649  pM31->ClearAllParams();
650 
651  // Load and set common params for all channels
652  if (!pM31->SetupCommonParams(preset, M31_CH0))
653  { cerr << "## ERROR: SetCommonParams failed ch0 " << endl; return AJA_STATUS_INITIALIZE; }
654 
655  // Change state to encode
656  if (!pM31->ChangeMainState(Hevc_MainState_Encode, Hevc_EncodeMode_Multiple))
657  { cerr << "## ERROR: ChangeMainState to encode failed" << endl; return AJA_STATUS_INITIALIZE; }
658 
659  pM31->GetMainState(&mainState);
660  if (mainState != Hevc_MainState_Encode)
661  { cerr << "## ERROR: Not in encode state after change" << endl; return AJA_STATUS_INITIALIZE; }
662  }
663 
664  // Write out stream params
665  if (!pM31->SetupVIParams(preset, encodeChannel))
666  { cerr << "## ERROR: SetupVIParams failed" << endl; return AJA_STATUS_INITIALIZE; }
667  if (!pM31->SetupVInParams(preset, encodeChannel))
668  { cerr << "## ERROR: SetupVinParams failed" << endl; return AJA_STATUS_INITIALIZE; }
669  if (!pM31->SetupVAParams(preset, encodeChannel))
670  { cerr << "## ERROR: SetupVAParams failed" << endl; return AJA_STATUS_INITIALIZE; }
671  if (!pM31->SetupEHParams(preset, encodeChannel))
672  { cerr << "## ERROR: SetupEHParams failed" << endl; return AJA_STATUS_INITIALIZE; }
673 
674  if (withInfo)
675  {
676  // Enable picture information
677  if (!pM31->mpM31VInParam->SetPTSMode(M31_PTSModeHost, (M31VirtualChannel)encodeChannel))
678  { cerr << "## ERROR: SetPTSMode failed" << endl; return AJA_STATUS_INITIALIZE; }
679  }
680 
681  // Now that we have setup the M31 lets change the VIn and EH states for channel 0 to start
682  if (!pM31->ChangeVInState(Hevc_VinState_Start, encodeChannel))
683  { cerr << "## ERROR: ChangeVInState failed" << endl; return AJA_STATUS_INITIALIZE; }
684 
685  pM31->GetVInState(&vInState, encodeChannel);
686  if (vInState != Hevc_VinState_Start)
687  {cout << "## VIn didn't start = '" << vInState << endl; }
688 
689  if (!pM31->ChangeEHState(Hevc_EhState_Start, encodeChannel))
690  { cerr << "## ERROR: ChangeEHState failed" << endl; return AJA_STATUS_INITIALIZE; }
691 
692  pM31->GetEHState(&ehState, encodeChannel);
693  if (ehState != Hevc_EhState_Start)
694  { cout << "## EH didn't start = '" << ehState << endl; }
695  }
696  else
697  {
698  // if we are in the init state assume that last stop was good
699  // otherwise reset the codec
700  pM31->GetMainState(&mainState, &encodeMode);
701  if ((mainState != Hevc_MainState_Init) || (encodeMode != Hevc_EncodeMode_Single))
702  {
703  // Here we need to start up the M31 so we reset the part then go into the init state
704  if (!pM31->Reset())
705  { cerr << "## ERROR: Reset of M31 failed" << endl; return AJA_STATUS_INITIALIZE; }
706 
707  // After a reset we should be in the boot state so lets check this
708  pM31->GetMainState(&mainState);
709  if (mainState != Hevc_MainState_Boot)
710  { cerr << "## ERROR: Not in boot state after reset" << endl; return AJA_STATUS_INITIALIZE; }
711 
712  // Now go to the init state
713  if (!pM31->ChangeMainState(Hevc_MainState_Init, Hevc_EncodeMode_Single))
714  { cerr << "## ERROR: ChangeMainState to init failed" << endl; return AJA_STATUS_INITIALIZE; }
715 
716  pM31->GetMainState(&mainState);
717  if (mainState != Hevc_MainState_Init)
718  { cerr << "## ERROR: Not in init state after change" << endl; return AJA_STATUS_INITIALIZE; }
719  }
720 
721  // Now lets configure the device for a given preset. First we must clear out all of the params which
722  // is necessary since the param space is basically uninitialized memory.
723  pM31->ClearAllParams();
724 
725  // Now load params for M31 preset into local structures in CNTV2m31
726  if (!pM31->LoadAllParams(preset))
727  { cerr << "## ERROR: LoadAllPresets failed" << endl; return AJA_STATUS_INITIALIZE; }
728 
729  // Write out all of the params to each of the 4 physical channels
730  if (!pM31->SetAllParams(M31_CH0))
731  { cerr << "## ERROR: SetVideoPreset failed ch0 " << endl; return AJA_STATUS_INITIALIZE; }
732 
733  if (!pM31->SetAllParams(M31_CH1))
734  { cerr << "## ERROR: SetVideoPreset failed ch1 " << endl; return AJA_STATUS_INITIALIZE; }
735 
736  if (!pM31->SetAllParams(M31_CH2))
737  { cerr << "## ERROR: SetVideoPreset failed ch1 " << endl; return AJA_STATUS_INITIALIZE; }
738 
739  if (!pM31->SetAllParams(M31_CH3))
740  { cerr << "## ERROR: SetVideoPreset failed ch1 " << endl; return AJA_STATUS_INITIALIZE; }
741 
742  // Here is where you can alter params sent to the M31 because all of these structures are public
743 // pM31->mpM31EHParam->SetFrameNumInGOP(16, (M31VirtualChannel)encodeChannel);
744 // pM31->mpM31EHParam->SetIpPeriod(4, (M31VirtualChannel)encodeChannel);
745 // pM31->mpM31EHParam->SetClosedGOP(1, (M31VirtualChannel)encodeChannel);
746 // pM31->mpM31EHParam->SetGOPHierarchy(0, (M31VirtualChannel)encodeChannel);
747 // pM31->mpM31EHParam->SetBitRate(10000, (M31VirtualChannel)encodeChannel);
748 
749  if (withInfo)
750  {
751  // Enable picture information
752  if (!pM31->mpM31VInParam->SetPTSMode(M31_PTSModeHost, (M31VirtualChannel)M31_CH0))
753  { cerr << "## ERROR: SetPTSMode failed" << endl; return AJA_STATUS_INITIALIZE; }
754  }
755 
756  // Change state to encode
757  if (!pM31->ChangeMainState(Hevc_MainState_Encode, Hevc_EncodeMode_Single))
758  { cerr << "## ERROR: ChangeMainState to encode failed" << endl; return AJA_STATUS_INITIALIZE; }
759 
760  pM31->GetMainState(&mainState);
761  if (mainState != Hevc_MainState_Encode)
762  { cerr << "## ERROR: Not in encode state after change" << endl; return AJA_STATUS_INITIALIZE; }
763 
764  // Now that we have setup the M31 lets change the VIn and EH states for channel 0 to start
765  if (!pM31->ChangeVInState(Hevc_VinState_Start, 0x01))
766  { cerr << "## ERROR: ChangeVInState failed" << endl; return AJA_STATUS_INITIALIZE; }
767 
768  pM31->GetVInState(&vInState, M31_CH0);
769  if (vInState != Hevc_VinState_Start)
770  {cout << "## VIn didn't start = '" << vInState << endl; }
771 
772  if (!pM31->ChangeEHState(Hevc_EhState_Start, 0x01))
773  { cerr << "## ERROR: ChangeEHState failed" << endl; return AJA_STATUS_INITIALIZE; }
774 
775  pM31->GetEHState(&ehState, M31_CH0);
776  if (ehState != Hevc_EhState_Start)
777  {cout << "## EH didn't start = '" << ehState << endl; }
778  }
779 
780  return AJA_STATUS_SUCCESS;
781 }
782 
783 
CNTV2DemoHevcCommon::WriteAiffData
void WriteAiffData(void *pBuffer, uint32_t numChannels, uint32_t numSamples)
Definition: ntv2demohevccommon.cpp:355
CNTV2DemoHevcCommon::WriteRawData
void WriteRawData(void *pBuffer, uint32_t bufferSize)
Definition: ntv2demohevccommon.cpp:526
NTV2_FORMAT_4x1920x1080p_6000
@ NTV2_FORMAT_4x1920x1080p_6000
Definition: ntv2enums.h:580
AJA_PixelFormat_YCBCR10_422PL
@ AJA_PixelFormat_YCBCR10_422PL
Definition: videotypes.h:157
NTV2_FBF_10BIT_YCBCR_420PL2
@ NTV2_FBF_10BIT_YCBCR_420PL2
10-Bit 4:2:0 2-Plane YCbCr
Definition: ntv2enums.h:239
HevcVinState
HevcVinState
Definition: ntv2publicinterface.h:9410
Hevc_MainState_Boot
@ Hevc_MainState_Boot
Definition: ntv2publicinterface.h:9375
HevcEncodedData::esIdrType
ULWord esIdrType
Definition: ntv2publicinterface.h:9617
AJA_FrameRate_3000
@ AJA_FrameRate_3000
Definition: videotypes.h:223
CNTV2DemoHevcCommon::GetAJAPixelFormat
AJA_PixelFormat GetAJAPixelFormat(NTV2FrameBufferFormat pixelFormat)
Definition: ntv2demohevccommon.cpp:65
Hevc_EncodeMode_Single
@ Hevc_EncodeMode_Single
Definition: ntv2publicinterface.h:9386
ntv2devicefeatures.h
Declares device capability functions.
CNTV2DemoHevcCommon::CNTV2DemoHevcCommon
CNTV2DemoHevcCommon()
Definition: ntv2demohevccommon.cpp:27
NTV2_FRAMERATE_6000
@ NTV2_FRAMERATE_6000
60 frames per second
Definition: ntv2enums.h:399
NULL
#define NULL
Definition: ntv2caption608types.h:19
AJA_STATUS_SUCCESS
@ AJA_STATUS_SUCCESS
Definition: types.h:368
CNTV2DemoHevcCommon::WriteHevcData
void WriteHevcData(void *pBuffer, uint32_t bufferSize)
Definition: ntv2demohevccommon.cpp:134
AJA_FrameRate_Unknown
@ AJA_FrameRate_Unknown
Definition: videotypes.h:212
ntv2demohevccommon.h
This file contains some structures, constants, classes and functions that are used in some of the hev...
NTV2_FRAMERATE_2997
@ NTV2_FRAMERATE_2997
Fractional rate of 30,000 frames per 1,001 seconds.
Definition: ntv2enums.h:403
M31_CH2
@ M31_CH2
Definition: ntv2m31enums.h:229
AJA_PixelFormat_YCbCr8
@ AJA_PixelFormat_YCbCr8
Definition: videotypes.h:125
CNTV2DemoHevcCommon::CreateRawFile
AJAStatus CreateRawFile(const std::string &inFileName, uint32_t maxFrames)
Definition: ntv2demohevccommon.cpp:495
AJA_FrameRate_2997
@ AJA_FrameRate_2997
Definition: videotypes.h:222
CNTV2DemoHevcCommon::ConvertYuv420FrameToNV12
AJAStatus ConvertYuv420FrameToNV12(void *pSrcBuffer, void *pDstBuffer, uint32_t bufferSize)
Definition: ntv2demohevccommon.cpp:467
AJA_PixelFormat_YCBCR8_422PL
@ AJA_PixelFormat_YCBCR8_422PL
Definition: videotypes.h:159
CNTV2DemoHevcCommon::CloseHevcFile
void CloseHevcFile(void)
Definition: ntv2demohevccommon.cpp:123
Hevc_VinState_Start
@ Hevc_VinState_Start
Definition: ntv2publicinterface.h:9414
CNTV2DemoHevcCommon::WriteEncData
void WriteEncData(void *pBuffer, uint32_t bufferSize)
Definition: ntv2demohevccommon.cpp:183
HevcEncodedData::esSize
ULWord esSize
Definition: ntv2publicinterface.h:9608
NTV2_FORMAT_4x1920x1080p_2997
@ NTV2_FORMAT_4x1920x1080p_2997
Definition: ntv2enums.h:570
NTV2_FORMAT_4x1920x1080p_2500
@ NTV2_FORMAT_4x1920x1080p_2500
Definition: ntv2enums.h:563
NTV2FrameBufferFormat
NTV2FrameBufferFormat
Identifies a particular video frame buffer format. See Device Frame Buffer Formats for details.
Definition: ntv2enums.h:207
CNTV2DemoHevcCommon::CloseRawFile
void CloseRawFile(void)
Definition: ntv2demohevccommon.cpp:515
CNTV2DemoHevcCommon::DetermineInputFormat
AJAStatus DetermineInputFormat(NTV2VideoFormat sdiFormat, bool quad, NTV2VideoFormat &videoFormat)
Definition: ntv2demohevccommon.cpp:567
AJA_FrameRate_2400
@ AJA_FrameRate_2400
Definition: videotypes.h:220
M31_PTSModeHost
@ M31_PTSModeHost
Definition: ntv2m31enums.h:327
FILENO_FUNCTION
#define FILENO_FUNCTION
Definition: ntv2demohevccommon.cpp:23
NTV2_FRAMERATE_2500
@ NTV2_FRAMERATE_2500
25 frames per second
Definition: ntv2enums.h:404
HevcEncodedData::numAdditionalData
ULWord numAdditionalData
Definition: ntv2publicinterface.h:9635
NTV2FrameRate
NTV2FrameRate
Identifies a particular video frame rate.
Definition: ntv2enums.h:396
NTV2_FRAMERATE_4800
@ NTV2_FRAMERATE_4800
48 frames per second
Definition: ntv2enums.h:408
M31_CH1
@ M31_CH1
Definition: ntv2m31enums.h:228
HevcEncodedData::serialNumber
ULWord serialNumber
Definition: ntv2publicinterface.h:9605
Hevc_MainState_Init
@ Hevc_MainState_Init
Definition: ntv2publicinterface.h:9376
AJA_FrameRate
AJA_FrameRate
Definition: videotypes.h:210
NTV2_FRAMERATE_2400
@ NTV2_FRAMERATE_2400
24 frames per second
Definition: ntv2enums.h:405
NTV2_FBF_8BIT_YCBCR_420PL2
@ NTV2_FBF_8BIT_YCBCR_420PL2
8-Bit 4:2:0 2-Plane YCbCr
Definition: ntv2enums.h:241
M31_CH0
@ M31_CH0
Definition: ntv2m31enums.h:227
M31_CH3
@ M31_CH3
Definition: ntv2m31enums.h:230
AJAStatus
AJAStatus
Definition: types.h:365
AJA_FrameRate_4795
@ AJA_FrameRate_4795
Definition: videotypes.h:224
AJA_FrameRate_2500
@ AJA_FrameRate_2500
Definition: videotypes.h:221
NTV2_FORMAT_1080p_5994_B
@ NTV2_FORMAT_1080p_5994_B
Definition: ntv2enums.h:529
NTV2_FRAMERATE_2398
@ NTV2_FRAMERATE_2398
Fractional rate of 24,000 frames per 1,001 seconds.
Definition: ntv2enums.h:406
AJA_STATUS_FAIL
@ AJA_STATUS_FAIL
Definition: types.h:369
HevcEncodedData::temporalId
ULWord temporalId
Definition: ntv2publicinterface.h:9616
CNTV2DemoHevcCommon::CloseYuv420File
void CloseYuv420File(void)
Definition: ntv2demohevccommon.cpp:431
ntv2devicescanner.h
Declares the CNTV2DeviceScanner class.
HevcEncodedData::esOffsetLow
ULWord esOffsetLow
Definition: ntv2publicinterface.h:9606
NTV2_FORMAT_1080p_2997
@ NTV2_FORMAT_1080p_2997
Definition: ntv2enums.h:518
AJA_PixelFormat_YCBCR10_420PL
@ AJA_PixelFormat_YCBCR10_420PL
Definition: videotypes.h:156
NTV2_FORMAT_1080p_3000
@ NTV2_FORMAT_1080p_3000
Definition: ntv2enums.h:519
CNTV2DemoHevcCommon::CloseAiffFile
void CloseAiffFile(void)
Definition: ntv2demohevccommon.cpp:269
NTV2_FORMAT_1080p_2500
@ NTV2_FORMAT_1080p_2500
Definition: ntv2enums.h:520
Hevc_MainState_Encode
@ Hevc_MainState_Encode
Definition: ntv2publicinterface.h:9377
AJA_FrameRate_2398
@ AJA_FrameRate_2398
Definition: videotypes.h:219
HevcEncodedData::ptsValueHigh
ULWord ptsValueHigh
Definition: ntv2publicinterface.h:9610
AJA_FrameRate_5000
@ AJA_FrameRate_5000
Definition: videotypes.h:226
HevcEncodedData::ptsValueLow
ULWord ptsValueLow
Definition: ntv2publicinterface.h:9609
CNTV2DemoHevcCommon::WriteAiffHeader
void WriteAiffHeader(void)
Definition: ntv2demohevccommon.cpp:288
NTV2_FORMAT_4x1920x1080p_3000
@ NTV2_FORMAT_4x1920x1080p_3000
Definition: ntv2enums.h:571
NTV2_FBF_8BIT_YCBCR
@ NTV2_FBF_8BIT_YCBCR
See 8-Bit YCbCr Format.
Definition: ntv2enums.h:211
ntv2utils.h
Declares numerous NTV2 utility functions.
NTV2_FORMAT_1080p_5000_B
@ NTV2_FORMAT_1080p_5000_B
Definition: ntv2enums.h:528
HevcEhState
HevcEhState
Definition: ntv2publicinterface.h:9419
HevcEncodedData::nalOffset
ULWord nalOffset
Definition: ntv2publicinterface.h:9619
HevcEncodedData::pictureType
ULWord pictureType
Definition: ntv2publicinterface.h:9618
NTV2_FBF_10BIT_YCBCR_422PL2
@ NTV2_FBF_10BIT_YCBCR_422PL2
10-Bit 4:2:2 2-Plane YCbCr
Definition: ntv2enums.h:240
CNTV2DemoHevcCommon::CreateEncFile
AJAStatus CreateEncFile(const std::string &inFileName, uint32_t maxFrames)
Definition: ntv2demohevccommon.cpp:152
M31VideoPreset
M31VideoPreset
Definition: ntv2m31enums.h:12
NTV2_FORMAT_1080p_6000_A
@ NTV2_FORMAT_1080p_6000_A
Definition: ntv2enums.h:535
AJA_FrameRate_5994
@ AJA_FrameRate_5994
Definition: videotypes.h:227
NTV2_FORMAT_1080p_2398
@ NTV2_FORMAT_1080p_2398
Definition: ntv2enums.h:521
AJA_FrameRate_4800
@ AJA_FrameRate_4800
Definition: videotypes.h:225
AJA_STATUS_RANGE
@ AJA_STATUS_RANGE
Definition: types.h:372
HevcEncodedData
Definition: ntv2publicinterface.h:9603
NTV2_FORMAT_UNKNOWN
@ NTV2_FORMAT_UNKNOWN
Definition: ntv2enums.h:498
CNTV2DemoHevcCommon::~CNTV2DemoHevcCommon
~CNTV2DemoHevcCommon()
Definition: ntv2demohevccommon.cpp:55
HevcEncodedData::dtsValueLow
ULWord dtsValueLow
Definition: ntv2publicinterface.h:9611
NTV2_FBF_10BIT_YCBCR
@ NTV2_FBF_10BIT_YCBCR
See 10-Bit YCbCr Format.
Definition: ntv2enums.h:210
HevcEncodeMode
HevcEncodeMode
Definition: ntv2publicinterface.h:9383
AJA_STATUS_INITIALIZE
@ AJA_STATUS_INITIALIZE
Definition: types.h:373
M31VirtualChannel
M31VirtualChannel
Definition: ntv2m31enums.h:184
NTV2_FRAMERATE_5994
@ NTV2_FRAMERATE_5994
Fractional rate of 60,000 frames per 1,001 seconds.
Definition: ntv2enums.h:401
HevcEncodedData::cpbValue
ULWord cpbValue
Definition: ntv2publicinterface.h:9620
AJA_PixelFormat_Unknown
@ AJA_PixelFormat_Unknown
Definition: videotypes.h:123
NTV2_FORMAT_4x1920x1080p_5994
@ NTV2_FORMAT_4x1920x1080p_5994
Definition: ntv2enums.h:579
AJA_PixelFormat_YCbCr10
@ AJA_PixelFormat_YCbCr10
Definition: videotypes.h:124
Hevc_EhState_Start
@ Hevc_EhState_Start
Definition: ntv2publicinterface.h:9423
HevcEncodedData::dtsValueHigh
ULWord dtsValueHigh
Definition: ntv2publicinterface.h:9612
NTV2_FORMAT_1080p_5000_A
@ NTV2_FORMAT_1080p_5000_A
Definition: ntv2enums.h:533
NTV2VideoFormat
enum _NTV2VideoFormat NTV2VideoFormat
Identifies a particular video format.
NTV2_FORMAT_4x1920x1080p_5000
@ NTV2_FORMAT_4x1920x1080p_5000
Definition: ntv2enums.h:578
NTV2_FORMAT_4x1920x1080p_2398
@ NTV2_FORMAT_4x1920x1080p_2398
Definition: ntv2enums.h:561
CNTV2DemoHevcCommon::CreateAiffFile
AJAStatus CreateAiffFile(const std::string &inFileName, uint32_t numChannels, uint32_t maxFrames, uint32_t bufferSize)
Definition: ntv2demohevccommon.cpp:234
NTV2_FORMAT_1080p_6000_B
@ NTV2_FORMAT_1080p_6000_B
Definition: ntv2enums.h:530
M31Channel
M31Channel
Definition: ntv2m31enums.h:225
NTV2_FORMAT_1080p_5994_A
@ NTV2_FORMAT_1080p_5994_A
Definition: ntv2enums.h:534
HevcEncodedData::esOffsetHigh
ULWord esOffsetHigh
Definition: ntv2publicinterface.h:9607
NTV2_FBF_8BIT_YCBCR_422PL2
@ NTV2_FBF_8BIT_YCBCR_422PL2
8-Bit 4:2:2 2-Plane YCbCr
Definition: ntv2enums.h:242
AJA_FrameRate_6000
@ AJA_FrameRate_6000
Definition: videotypes.h:228
CNTV2DemoHevcCommon::CloseEncFile
void CloseEncFile(void)
Definition: ntv2demohevccommon.cpp:172
AJA_PixelFormat
AJA_PixelFormat
Definition: videotypes.h:121
CNTV2DemoHevcCommon::CreateHevcFile
AJAStatus CreateHevcFile(const std::string &inFileName, uint32_t maxFrames)
Definition: ntv2demohevccommon.cpp:103
Hevc_EncodeMode_Multiple
@ Hevc_EncodeMode_Multiple
Definition: ntv2publicinterface.h:9387
AJA_STATUS_OPEN
@ AJA_STATUS_OPEN
Definition: types.h:375
NTV2_FRAMERATE_5000
@ NTV2_FRAMERATE_5000
50 frames per second
Definition: ntv2enums.h:407
CNTV2DemoHevcCommon::AlignDataBuffer
uint32_t AlignDataBuffer(void *pBuffer, uint32_t bufferSize, uint32_t dataSize, uint32_t alignBytes, uint8_t fill)
Definition: ntv2demohevccommon.cpp:544
NTV2_FRAMERATE_4795
@ NTV2_FRAMERATE_4795
Fractional rate of 48,000 frames per 1,001 seconds.
Definition: ntv2enums.h:409
NTV2_FRAMERATE_3000
@ NTV2_FRAMERATE_3000
30 frames per second
Definition: ntv2enums.h:402
CNTV2DemoHevcCommon::GetAJAFrameRate
AJA_FrameRate GetAJAFrameRate(NTV2FrameRate frameRate)
Definition: ntv2demohevccommon.cpp:82
CNTV2DemoHevcCommon::ReadYuv420Frame
AJAStatus ReadYuv420Frame(void *pBuffer, uint32_t numFrame)
Definition: ntv2demohevccommon.cpp:446
HevcEncodedData::esLastFrame
ULWord esLastFrame
Definition: ntv2publicinterface.h:9626
NTV2_FORMAT_4x1920x1080p_2400
@ NTV2_FORMAT_4x1920x1080p_2400
Definition: ntv2enums.h:562
NTV2_FORMAT_1080p_2400
@ NTV2_FORMAT_1080p_2400
Definition: ntv2enums.h:522
CNTV2DemoHevcCommon::OpenYuv420File
AJAStatus OpenYuv420File(const std::string &inFileName, const uint32_t width, const uint32_t height)
Definition: ntv2demohevccommon.cpp:400
AJA_PixelFormat_YCBCR8_420PL
@ AJA_PixelFormat_YCBCR8_420PL
Definition: videotypes.h:158
CNTV2DemoHevcCommon::SetupHEVC
AJAStatus SetupHEVC(CNTV2m31 *pM31, M31VideoPreset preset, M31Channel encodeChannel, bool multiStream, bool withInfo)
Definition: ntv2demohevccommon.cpp:618
HevcMainState
HevcMainState
Definition: ntv2publicinterface.h:9372