17 #if defined (AJALinux) || defined (AJAMac)
19 #include <sys/socket.h>
20 #include <netinet/in.h>
21 #include <arpa/inet.h>
23 #pragma warning(disable:4800)
30 uint32_t features = GetFeatures();
38 #define WAIT_RESET_MS 800
40 kipdprintf(
"CNTV2ConfigTs2022::SetupJ2KEncoder channel = %d\n", channel);
66 uint32_t lastFrameCount = J2kGetFrameCounter(channel, 2);
67 uint32_t currentFrameCount = 0;
75 currentFrameCount = J2kGetFrameCounter(channel, 2);
77 if (lastFrameCount != currentFrameCount)
80 lastFrameCount = currentFrameCount;
91 SetEncoderInputEnable( channel,
false,
false );
94 SetEncoderReset( channel,
true );
100 SetEncoderReset( channel,
false );
119 if (!SetupTsForEncode(channel))
123 if (!SetupJ2KForEncode(channel))
178 bool CNTV2ConfigTs2022::SetupJ2KForEncode(
const NTV2Channel channel)
192 uint32_t sb_rmv_c0 = 0;
193 uint32_t sb_rmv_c1 = 0;
194 uint32_t sb_rmv_c2 = 0;
197 uint32_t regulator_type = 0;
199 uint32_t reg_cap = 0;
200 uint32_t clk_fx_freq = 200;
201 uint32_t marker = 0x02;
202 uint32_t guard_bit = 1;
203 uint32_t prog_order = 0;
204 uint32_t fdwt_type = 1;
206 uint32_t num_comp = 3;
207 uint32_t num_levels = 5;
209 uint32_t GOB[8] = {03, 03, 03, 03, 03, 03, 03, 03};
210 uint32_t QS_C0[8] = {12, 13, 14, 15, 16, 16, 16, 16};
211 uint32_t QS_C1[8] = {12, 13, 14, 15, 16, 16, 16, 16};
212 uint32_t QS_C2[8] = {12, 13, 14, 15, 16, 16, 16, 16};
224 uint32_t width = fd.GetRasterWidth();
225 uint32_t height = fd.GetVisibleRasterHeight();
229 uint32_t framesPerSecNum;
230 uint32_t framesPerSecDen;
231 uint32_t fieldsPerSec;
234 if (framesPerSecDen == 1001)
235 framesPerSecDen = 1000;
236 fieldsPerSec = framesPerSecNum/framesPerSecDen;
240 kipdprintf(
"CNTV2ConfigTs2022::SetupJ2KForEncode width=%d, height=%d fpsNum=%d,\n", width, height, fieldsPerSec);
245 bitRateMsb = ((((mbps*1000000)/fieldsPerSec)/8)/9) >> 16;
246 bitRateLsb = ((((mbps*1000000)/fieldsPerSec)/8)/9) & 0xFFFF;
247 rtConstant = (((clk_fx_freq*149000)/200)/fieldsPerSec)/9;
252 bitRateMsb = (((mbps*1000000)/fieldsPerSec)/8) >> 16;
253 bitRateLsb = (((mbps*1000000)/fieldsPerSec)/8) & 0xFFFF;
254 rtConstant = (((clk_fx_freq*149000)/200)/fieldsPerSec);
258 for (uint32_t config=0; config < 3; config++)
260 J2kSetParam(channel, config, 0x00, width);
261 J2kSetParam(channel, config, 0x01, height);
262 J2kSetParam(channel, config, 0x02, bitDepth);
263 J2kSetParam(channel, config, 0x03, guard_bit);
264 J2kSetParam(channel, config, 0x04, subSamp);
265 J2kSetParam(channel, config, 0x05, num_comp);
266 J2kSetParam(channel, config, 0x06, mct);
267 J2kSetParam(channel, config, 0x07, fdwt_type);
268 J2kSetParam(channel, config, 0x08, num_levels);
269 J2kSetParam(channel, config, 0x09, codeBlocksize);
271 J2kSetParam(channel, config, 0x0A, prog_order);
272 J2kSetParam(channel, config, 0x0B, bitRateMsb);
273 J2kSetParam(channel, config, 0x0C, bitRateLsb);
274 J2kSetParam(channel, config, 0x0D, rtConstant);
275 J2kSetParam(channel, config, 0x0E, marker);
276 J2kSetParam(channel, config, 0x0F, ull);
277 J2kSetParam(channel, config, 0x10, sb_rmv_c0);
278 J2kSetParam(channel, config, 0x11, sb_rmv_c1);
279 J2kSetParam(channel, config, 0x12, sb_rmv_c2);
280 J2kSetParam(channel, config, 0x13, regulator_type);
281 J2kSetParam(channel, config, 0x14, Rsiz);
282 J2kSetParam(channel, config, 0x15, reg_cap);
288 if ((height != 1080) && (height != 540))
294 if ((height == 1080) && (num_levels > 4))
300 if ((height == 540) && (num_levels > 3))
313 #Set ULL in active mode
314 opb_write $env [expr 0x10000*$enc+0x1006] 1
316 #Set img height for each kind of tile
317 if {$im_height == 1080} {
318 ipx_jp2k_e_set_param $env $enc 0 0x1 128
319 ipx_jp2k_e_set_param $env $enc 1 0x1 128
320 ipx_jp2k_e_set_param $env $enc 2 0x1 56
322 if {$im_height == 540} {
323 ipx_jp2k_e_set_param $env $enc 0 0x1 64
324 ipx_jp2k_e_set_param $env $enc 1 0x1 64
325 ipx_jp2k_e_set_param $env $enc 2 0x1 28
328 #Specify all kind of tile
329 ipx_jp2k_e_set_param $env $enc 0 0xF 1
330 ipx_jp2k_e_set_param $env $enc 1 0xF 2
331 ipx_jp2k_e_set_param $env $enc 2 0xF 3
334 #ipx_jp2k_e_get_all_status $env $enc 0
335 for {set config 0} {$config<3} {incr config} {
336 for {set lvl 0} {$lvl<7} {incr lvl} {
337 ipx_jp2k_e_set_param $env $enc $config [expr 0x80+$lvl] [lindex $GOB $lvl]
338 ipx_jp2k_e_set_param $env $enc $config [expr 0x88+$lvl] [expr [lindex $QS_C0 $lvl]*0x800]
339 ipx_jp2k_e_set_param $env $enc $config [expr 0x90+$lvl] [expr [lindex $QS_C1 $lvl]*0x800]
340 ipx_jp2k_e_set_param $env $enc $config [expr 0x98+$lvl] [expr [lindex $QS_C2 $lvl]*0x800]
347 for (uint32_t config=0; config < 3; config++)
349 for (uint32_t lvl=0; lvl < 7; lvl++)
351 J2kSetParam(channel, config, 0x80+lvl, GOB[lvl]);
352 J2kSetParam(channel, config, 0x88+lvl, QS_C0[lvl]*0x800);
353 J2kSetParam(channel, config, 0x90+lvl, QS_C1[lvl]*0x800);
354 J2kSetParam(channel, config, 0x98+lvl, QS_C2[lvl]*0x800);
415 bool CNTV2ConfigTs2022::SetupTsForEncode(
const NTV2Channel channel)
418 if (!SetupEncodeTsTimer(channel))
422 if (!SetupEncodeTsMpegJ2kEncap(channel))
426 if (!SetupEncodeTsMpegPcrEncap(channel))
430 if (!SetupEncodeTsMpegAesEncap(channel))
434 if (!SetupEncodeTsAesEncap(channel))
438 if (!SetupEncodeTsJ2KEncoder(channel))
448 bool CNTV2ConfigTs2022::SetupEncodeTsTimer(
const NTV2Channel channel)
450 uint32_t addr = GetIpxTsAddr(channel);
453 kipdprintf(
"CNTV2ConfigTs2022::SetupEncodeTsTimer\n");
458 tsGen = CalculateTsGen(channel);
478 bool CNTV2ConfigTs2022::SetupEncodeTsJ2KEncoder(
const NTV2Channel channel)
481 uint32_t addr = GetIpxTsAddr(channel);
483 kipdprintf(
"CNTV2ConfigTs2022::SetupEncodeTsJ2KEncoder\n");
509 bool CNTV2ConfigTs2022::SetupEncodeTsMpegJ2kEncap(
const NTV2Channel channel)
511 uint32_t addr = GetIpxTsAddr(channel);
513 kipdprintf(
"CNTV2ConfigTs2022::SetupEncodeTsMpegJ2kEncap\n");
515 GenerateTableForMpegJ2kEncap(channel);
518 for (int32_t index=0; index < _transactionCount; index++)
528 bool CNTV2ConfigTs2022::SetupEncodeTsMpegPcrEncap(
const NTV2Channel channel)
530 uint32_t addr = GetIpxTsAddr(channel);
532 kipdprintf(
"CNTV2ConfigTs2022::SetupEncodeTsMpegPcrEncap\n");
534 GenerateTableForMpegPcrEncap(channel);
537 for (int32_t index=0; index < _transactionCount; index++)
547 bool CNTV2ConfigTs2022::SetupEncodeTsMpegAesEncap(
const NTV2Channel channel)
549 uint32_t addr = GetIpxTsAddr(channel);
551 kipdprintf(
"CNTV2ConfigTs2022::SetupEncodeTsMpegAesEncap\n");
553 GenerateTableForMpegAesEncap(channel);
556 for (int32_t index=0; index < _transactionCount; index++)
566 bool CNTV2ConfigTs2022::SetupEncodeTsAesEncap(
const NTV2Channel channel)
568 uint32_t addr = GetIpxTsAddr(channel);
571 uint32_t numAudioChannels = 0;
572 uint32_t audioChannels = 0;
578 if (numAudioChannels)
579 audioChannels = (numAudioChannels/2) - 1;
598 uint32_t CNTV2ConfigTs2022::GetFeatures()
614 bool CNTV2ConfigTs2022::J2kCanAcceptCmd(
const NTV2Channel channel)
617 uint32_t addr = GetIpxJ2KAddr(channel);
630 bool CNTV2ConfigTs2022::J2KGetNextT0Status(
const NTV2Channel channel, uint32_t *pStatus)
633 uint32_t addr = GetIpxJ2KAddr(channel);
641 if (val & (
BIT(10) |
BIT(9))) {
642 printf(
"Overflow / status full 0x%08x\n", val);
654 bool CNTV2ConfigTs2022::GetT0CmdStatus(
const NTV2Channel channel,
const uint32_t cmdId, uint32_t *pStatus ) {
656 static const int MAX_STATUSES_TO_WAIT = 16;
658 while( J2KGetNextT0Status(channel, &val) )
660 if ( ((val >> 16)& 0xff) == cmdId )
668 if (count == MAX_STATUSES_TO_WAIT)
676 void CNTV2ConfigTs2022::J2kSetMode(
const NTV2Channel channel, uint32_t tier, uint32_t mode)
678 uint32_t addr = GetIpxJ2KAddr(channel);
685 void CNTV2ConfigTs2022::J2kSetConfig(
const NTV2Channel channel, uint32_t config)
688 uint32_t addr = GetIpxJ2KAddr(channel);
692 if (!GetT0CmdStatus( channel, 0x01, &val))
694 printf(
"No status received for setconfig\n");
698 if (val >> 24 != 0xf3)
700 printf(
"J2KSetConfig: Expected status 0xf3...... received 0x%08x\n", val);
705 uint32_t CNTV2ConfigTs2022::J2kGetFrameCounter(
const NTV2Channel channel, uint32_t tier)
707 uint32_t addr = GetIpxJ2KAddr(channel);
716 void CNTV2ConfigTs2022::J2kSetParam (
const NTV2Channel channel, uint32_t config, uint32_t param, uint32_t value)
719 uint32_t addr = GetIpxJ2KAddr(channel);
723 while(!J2kCanAcceptCmd(channel))
725 printf(
"J2kSetParam - command fifo full\n");
729 val = 0x70000000 + (param<<16) + (config&0x7)*0x2000 + param;
733 while(!J2kCanAcceptCmd(channel))
735 printf(
"J2kSetParam - command fifo full\n");
738 val = 0x7f000000 + (param<<16) + value;
742 if (!GetT0CmdStatus( channel, param , &val))
744 printf(
"No status received for SetParam\n");
748 if (val >> 24 != 0xf0)
750 printf(
"J2KSetConfig: Expected status 0xf0...... received 0x%08x\n", val);
755 void CNTV2ConfigTs2022::GenerateTableForMpegJ2kEncap(
const NTV2Channel channel)
761 kipdprintf(
"CNTV2ConfigTs2022::GenerateTransactionTableForMpegJ2kEncap\n");
773 streamData.
width = fd.GetRasterWidth();
774 streamData.
height = fd.GetVisibleRasterHeight();
791 _transactionCount = 0;
794 _transactionTable[_transactionCount][0] =
HOST_EN;
795 _transactionTable[_transactionCount++][1] = 0;
810 _transactionTable[_transactionCount++][1] = streamData.
videoPid;
811 kipdprintf(
"Payload Parameters = 0x%x\n", _transactionTable[_transactionCount-1][1]);
814 _transactionTable[_transactionCount++][1] = streamData.
interlaced;
815 kipdprintf(
"Interlaced Video = %i\n", _transactionTable[_transactionCount-1][1]);
818 _transactionTable[_transactionCount++][1] = pes.
calcPatPmtPeriod() | 0x01000000;
819 kipdprintf(
"PAT/PMT Transmission Period = %i (0x%x)\n", _transactionTable[_transactionCount-1][1], _transactionTable[_transactionCount-1][1]);
821 _transactionTable[_transactionCount][0] =
PACKET_RATE;
822 _transactionTable[_transactionCount++][1] = 0;
823 kipdprintf(
"Packet Rate = %i (0x%x)\n", _transactionTable[_transactionCount-1][1], _transactionTable[_transactionCount-1][1]);
827 kipdprintf(
"PTS Offset = 0x%02x J2K TS Offset = 0x%02x auf1 offset = 0x%02x auf2 offset = 0x%02x\n\n",
829 _transactionTable[_transactionCount][0] =
PTS_OFFSET;
830 _transactionTable[_transactionCount++][1] = pes.
_ptsOffset;
832 _transactionTable[_transactionCount++][1] = pes.
_j2kTsOffset;
833 _transactionTable[_transactionCount][0] =
AUF1_OFFSET;
834 _transactionTable[_transactionCount++][1] = pes.
_auf1Offset;
835 _transactionTable[_transactionCount][0] =
AUF2_OFFSET;
836 _transactionTable[_transactionCount++][1] = pes.
_auf2Offset;
838 kipdprintf(
"PES Template Length = %i, Data:\n", length);
839 _transactionTable[_transactionCount][0] =
PES_HDR_LEN;
840 _transactionTable[_transactionCount++][1] = length;
842 for (w1 = 0; w1 < 188; w1++)
845 _transactionTable[_transactionCount++][1] = pes.
_pkt8[w1];
854 kipdprintf(
"Adaptation Template Length = %i, Data:\n", length);
856 _transactionTable[_transactionCount++][1] = length;
857 for (w1 = 0; w1 < 188; w1++)
860 _transactionTable[_transactionCount++][1] = adp.
_pkt32[w1];
869 kipdprintf(
"PAT Template Length = %i, Data:\n", length);
870 for (w1 = 0; w1 < 188; w1++)
873 _transactionTable[_transactionCount++][1] = pat.
_pkt8[w1];
892 kipdprintf(
"PMT Template Length = %i, Data:\n", length);
893 for (w1 = 0; w1 < 188; w1++)
896 _transactionTable[_transactionCount++][1] = pmt.
_pkt8[w1];
901 _transactionTable[_transactionCount][0] =
HOST_EN;
902 _transactionTable[_transactionCount++][1] = 1;
906 void CNTV2ConfigTs2022::GenerateTableForMpegPcrEncap(
const NTV2Channel channel)
911 kipdprintf(
"CNTV2ConfigTs2022::GenerateTableForMpegPcrEncap\n");
917 _transactionCount = 0;
920 _transactionTable[_transactionCount][0] =
HOST_EN;
921 _transactionTable[_transactionCount++][1] = 0;
924 _transactionTable[_transactionCount++][1] = 0;
925 kipdprintf(
"PAT/PMT Transmission Period = %i (0x%x)\n", _transactionTable[_transactionCount-1][1], _transactionTable[_transactionCount-1][1]);
927 _transactionTable[_transactionCount][0] =
PACKET_RATE;
928 _transactionTable[_transactionCount++][1] = 0;
929 kipdprintf(
"Packet Rate = %i (0x%x)\n", _transactionTable[_transactionCount-1][1], _transactionTable[_transactionCount-1][1]);
936 kipdprintf(
"Adaptation Template Length = %i, Data:\n", length);
938 _transactionTable[_transactionCount++][1] = length;
939 for (w1 = 0; w1 < 188; w1++)
942 _transactionTable[_transactionCount++][1] = adp.
_pkt32[w1];
947 _transactionTable[_transactionCount][0] =
HOST_EN;
948 _transactionTable[_transactionCount++][1] = 1;
952 void CNTV2ConfigTs2022::GenerateTableForMpegAesEncap(
const NTV2Channel channel)
958 kipdprintf(
"CNTV2ConfigTs2022::GenerateTableForMpegAesEncap\n");
963 kipdprintf(
"Audio 1 PID = 0x%02x\n\n", audioPid);
965 _transactionCount = 0;
968 _transactionTable[_transactionCount][0] =
HOST_EN;
969 _transactionTable[_transactionCount++][1] = 0;
972 _transactionTable[_transactionCount][0] =
PACKET_RATE;
973 _transactionTable[_transactionCount++][1] = 0;
984 _transactionTable[_transactionCount++][1] = audioPid;
985 kipdprintf(
"Payload Parameters = 0x%x\n", _transactionTable[_transactionCount-1][1]);
987 _transactionTable[_transactionCount][0] =
PACKET_RATE;
988 _transactionTable[_transactionCount++][1] = 0x10000;
989 kipdprintf(
"Packet Rate = %i (0x%x)\n", _transactionTable[_transactionCount-1][1], _transactionTable[_transactionCount-1][1]);
993 kipdprintf(
"PTS Offset = 0x%02x auf1 offset = 0x%02x auf2 offset = 0x%02x\n\n",
995 _transactionTable[_transactionCount][0] =
PTS_OFFSET;
996 _transactionTable[_transactionCount++][1] = pes.
_ptsOffset;
997 _transactionTable[_transactionCount][0] =
AUF1_OFFSET;
998 _transactionTable[_transactionCount++][1] = pes.
_auf1Offset;
999 _transactionTable[_transactionCount][0] =
AUF2_OFFSET;
1000 _transactionTable[_transactionCount++][1] = pes.
_auf2Offset;
1002 kipdprintf(
"PES Template Length = %i, Data:\n", length);
1003 _transactionTable[_transactionCount][0] =
PES_HDR_LEN;
1004 _transactionTable[_transactionCount++][1] = length;
1006 for (w1 = 0; w1 < 188; w1++)
1009 _transactionTable[_transactionCount++][1] = pes.
_pkt8[w1];
1018 kipdprintf(
"Adaptation Template Length = %i, Data:\n", length);
1020 _transactionTable[_transactionCount++][1] = length;
1021 for (w1 = 0; w1 < 188; w1++)
1024 _transactionTable[_transactionCount++][1] = adp.
_pkt32[w1];
1029 _transactionTable[_transactionCount][0] =
HOST_EN;
1030 _transactionTable[_transactionCount++][1] = 1;
1034 uint32_t CNTV2ConfigTs2022::GetIpxJ2KAddr(
const NTV2Channel channel)
1047 uint32_t CNTV2ConfigTs2022::GetIpxTsAddr(
const NTV2Channel channel)
1059 bool CNTV2ConfigTs2022::WriteJ2KConfigReg(
const NTV2Channel channel,
const uint32_t reg,
const uint32_t value)
1075 bool CNTV2ConfigTs2022::ReadJ2KConfigReg(
const NTV2Channel channel,
const uint32_t reg, uint32_t * value)
1091 void CNTV2ConfigTs2022::SetEncoderInputEnable(
const NTV2Channel channel,
bool bEnable,
bool bMDEnable )
1096 uint32_t encoderBit(0), mdBit(0);
1108 uint32_t tmp = (bEnable?encoderBit:0) | (bMDEnable?mdBit:0);
1110 val &= ~(encoderBit|mdBit);
1117 void CNTV2ConfigTs2022::SetEncoderReset(
const NTV2Channel channel,
bool bReset )
1134 int32_t CNTV2ConfigTs2022::CalculateTsGen(
const NTV2Channel channel)
1137 uint32_t audioChannels1;
1143 double ts_bitrate = (((double) mbps + ((
double) audioChannels1 * 1.6)) * 1.2) * 1000000;
1144 double sys_clk = 125e6;
1148 d1 = ts_bitrate / 8.0 / 188.0;
1153 return (int32_t) d1;