diff options
author | Matthias P. Braendli <matthias.braendli@mpb.li> | 2014-02-28 13:18:35 +0100 |
---|---|---|
committer | Matthias P. Braendli <matthias.braendli@mpb.li> | 2014-02-28 13:18:35 +0100 |
commit | 58d7fcd0db89cdc62e9362aa6d63c88843f2837d (patch) | |
tree | c4656b1b871e1fc044c66d06bfa8bfb47e2bedc2 | |
parent | d9f6727654cad16951b0e8ca85c1c8e1263421bf (diff) | |
download | fdk-aac-58d7fcd0db89cdc62e9362aa6d63c88843f2837d.tar.gz fdk-aac-58d7fcd0db89cdc62e9362aa6d63c88843f2837d.tar.bz2 fdk-aac-58d7fcd0db89cdc62e9362aa6d63c88843f2837d.zip |
Add PAD (MOT and Slideshow) encoder from www.csp.it
-rw-r--r-- | Makefile.am | 19 | ||||
-rw-r--r-- | aac-enc-dabplus-zmq.c | 169 | ||||
-rw-r--r-- | configure.ac | 26 | ||||
-rw-r--r-- | contrib/lib_crc.c | 459 | ||||
-rw-r--r-- | contrib/lib_crc.h | 66 | ||||
-rw-r--r-- | mot-encoder.c | 638 |
6 files changed, 1308 insertions, 69 deletions
diff --git a/Makefile.am b/Makefile.am index 5040d3e..ff9e1b9 100644 --- a/Makefile.am +++ b/Makefile.am @@ -33,25 +33,26 @@ lib_LTLIBRARIES = libfdk-aac.la libfdk_aac_la_LDFLAGS = -version-info @FDK_AAC_VERSION@ -no-undefined \ -export-symbols $(top_srcdir)/fdk-aac.sym -if EXAMPLE - aac_enc_LDADD = libfdk-aac.la aac_enc_SOURCES = aac-enc.c wavreader.c aac_enc_dabplus_LDADD = libfdk-aac.la -lfec aac_enc_dabplus_SOURCES = aac-enc-dabplus.c wavreader.c -if HAVE_ZEROMQ_TEST aac_enc_dabplus_zmq_LDADD = libfdk-aac.la -lfec -lzmq -aac_enc_dabplus_zmq_SOURCES = aac-enc-dabplus-zmq.c wavreader.c +aac_enc_dabplus_zmq_SOURCES = aac-enc-dabplus-zmq.c wavreader.c \ + contrib/lib_crc.c + +mot_encoder_CFLAGS = @MAGICKWAND_CPPFLAGS@ -Icontrib +mot_encoder_LDADD = @MAGICKWAND_LDADD@ +mot_encoder_SOURCES = mot-encoder.c contrib/lib_crc.c -bin_PROGRAMS = aac-enc$(EXEEXT) aac-enc-dabplus$(EXEEXT) aac-enc-dabplus-zmq$(EXEEXT) -else -bin_PROGRAMS = aac-enc$(EXEEXT) aac-enc-dabplus$(EXEEXT) -endif +bin_PROGRAMS = aac-enc$(EXEEXT) \ + aac-enc-dabplus$(EXEEXT) \ + aac-enc-dabplus-zmq$(EXEEXT) \ + mot-encoder$(EXEEXT) noinst_HEADERS = wavreader.h -endif AACDEC_SRC = \ libAACdec/src/aacdec_drc.cpp \ diff --git a/aac-enc-dabplus-zmq.c b/aac-enc-dabplus-zmq.c index 9122dc2..add317b 100644 --- a/aac-enc-dabplus-zmq.c +++ b/aac-enc-dabplus-zmq.c @@ -1,19 +1,20 @@ /* ------------------------------------------------------------------ * Copyright (C) 2011 Martin Storsjo - * Copyright (C) 2013 Matthias P. Braendli + * Copyright (C) 2013,2014 Matthias P. Braendli + * Copyright (C) 2014 CSP Innovazione nelle ICT s.c.a r.l. (http://www.csp.it/) * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. * - * http://www.apache.org/licenses/LICENSE-2.0 + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - * express or implied. - * See the License for the specific language governing permissions - * and limitations under the License. + * You should have received a copy of the GNU General Public License + * along with this program If not, see <http://www.gnu.org/licenses/>. * ------------------------------------------------------------------- */ @@ -29,8 +30,23 @@ #include "wavreader.h" #include <fec.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <errno.h> + +#include "contrib/lib_crc.h" + +#define MIN(a,b) (((a)<(b))?(a):(b)) +#define MAX(a,b) (((a)>(b))?(a):(b)) void usage(const char* name) { + fprintf(stderr, "fdk-aac-dabplus HE-AACv2 encoder for DAB+\n\n"); + fprintf(stderr, "With ZeroMQ output for ODR-DabMux\n"); + fprintf(stderr, "and PAD (DLS and MOT Slideshow) by http://www.csp.it\n"); + fprintf(stderr, "\n"); + fprintf(stderr, "http://opendigitalradio.org\n"); + fprintf(stderr, "\nUsage:\n"); fprintf(stderr, "%s [OPTION...]\n", name); fprintf(stderr, " -b, --bitrate={ 8, 16, ..., 192 } Output bitrate in kbps. Must be 8 multiple.\n" @@ -40,7 +56,7 @@ void usage(const char* name) { " -o, --output=URI Output zmq uri. (e.g. 'tcp://*:9000')\n" " -a, --afterburner Turn on AAC encoder quality increaser.\n" //" -m, --message Turn on AAC frame messages.\n" -//" -p, --pad=BYTES Set PAD size in bytes.\n" +" -p, --pad=BYTES Set PAD size in bytes.\n" " -f, --format={ wav, raw } Set input file format (default: wav).\n" " -c, --channels={ 1, 2 } Nb of input channels for raw input (default: 2).\n" " -r, --rate={ 32000, 48000 } Sample rate for raw input (default: 48000).\n" @@ -79,25 +95,35 @@ int main(int argc, char *argv[]) { CHANNEL_MODE mode; AACENC_InfoStruct info = { 0 }; + int pad_fd; + unsigned char pad_buf[128]; + int padlen; + void *zmq_context = zmq_ctx_new(); void *zmq_sock = NULL; const struct option longopts[] = { {"bitrate", required_argument, 0, 'b'}, - {"input", required_argument, 0, 'i'}, - {"output", required_argument, 0, 'o'}, - {"format", required_argument, 0, 'f'}, - {"rate", required_argument, 0, 'r'}, - {"channels", required_argument, 0, 'c'}, - //{"lp", no_argument, 0, 'l'}, - {"afterburner", no_argument, 0, 'a'}, - {"help", no_argument, 0, 'h'}, - {0,0,0,0}, + {"input", required_argument, 0, 'i'}, + {"output", required_argument, 0, 'o'}, + {"format", required_argument, 0, 'f'}, + {"rate", required_argument, 0, 'r'}, + {"channels", required_argument, 0, 'c'}, + {"pad", required_argument, 0, 'p'}, + //{"lp", no_argument, 0, 'l'}, + {"afterburner", no_argument, 0, 'a'}, + {"help", no_argument, 0, 'h'}, + {0,0,0,0}, }; + if (argc == 1) { + usage(argv[0]); + return 0; + } + int index; while(ch != -1) { - ch = getopt_long(argc, argv, "tlhab:c:i:o:r:f:", longopts, &index); + ch = getopt_long(argc, argv, "tlhab:c:i:o:r:f:p:", longopts, &index); switch (ch) { case 'f': if(strcmp(optarg, "raw")==0) { @@ -123,6 +149,9 @@ int main(int argc, char *argv[]) { case 'o': outuri = optarg; break; + case 'p': + padlen = atoi(optarg); + break; case '?': case 'h': usage(argv[0]); @@ -131,9 +160,29 @@ int main(int argc, char *argv[]) { } if(subchannel_index < 1 || subchannel_index > 24) { - fprintf(stderr, "Bad subchannels number: %d, try other bitrate.\n", subchannel_index); + fprintf(stderr, "Bad subchannels number: %d, try other bitrate.\n", + subchannel_index); return 1; } + if(padlen != 0) { + int flags; + if (mkfifo("/tmp/pad.fifo", S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH) != 0) { + if (errno != EEXIST) { + fprintf(stderr, "Can't create pad file: %d!\n", errno); + return 1; + } + } + pad_fd = open("/tmp/pad.fifo", O_RDONLY | O_NONBLOCK); + if (pad_fd == -1) { + fprintf(stderr, "Can't open pad file!\n"); + return 1; + } + flags = fcntl(pad_fd, F_GETFL, 0); + if (fcntl(pad_fd, F_SETFL, flags | O_NONBLOCK)) { + fprintf(stderr, "Can't set non-blocking mode in pad file!\n"); + return 1; + } + } if(raw_input) { if(infile && strcmp(infile, "-")) { @@ -217,7 +266,7 @@ int main(int argc, char *argv[]) { return 1; } if (aacEncoder_SetParam(handle, AACENC_SAMPLERATE, sample_rate) != AACENC_OK) { - fprintf(stderr, "Unable to set the AOT\n"); + fprintf(stderr, "Unable to set the samplerate\n"); return 1; } if (aacEncoder_SetParam(handle, AACENC_CHANNELMODE, mode) != AACENC_OK) { @@ -229,7 +278,7 @@ int main(int argc, char *argv[]) { return 1; } if (aacEncoder_SetParam(handle, AACENC_GRANULE_LENGTH, 960) != AACENC_OK) { - fprintf(stderr, "Unable to set the AOT\n"); + fprintf(stderr, "Unable to set the granule length\n"); return 1; } if (aacEncoder_SetParam(handle, AACENC_TRANSMUX, TT_DABPLUS) != AACENC_OK) { @@ -252,6 +301,10 @@ int main(int argc, char *argv[]) { fprintf(stderr, "Unable to set the afterburner mode\n"); return 1; } + if (aacEncoder_SetParam(handle, AACENC_ANCILLARY_BITRATE, 0) != AACENC_OK) { + fprintf(stderr, "Unable to set the ancillary bitrate\n"); + return 1; + } if (aacEncEncode(handle, NULL, NULL, NULL, NULL) != AACENC_OK) { fprintf(stderr, "Unable to initialize the encoder\n"); return 1; @@ -294,44 +347,74 @@ int main(int argc, char *argv[]) { AACENC_BufDesc in_buf = { 0 }, out_buf = { 0 }; AACENC_InArgs in_args = { 0 }; AACENC_OutArgs out_args = { 0 }; - int in_identifier = IN_AUDIO_DATA; - int in_size, in_elem_size; + int in_identifier[] = {IN_AUDIO_DATA, IN_ANCILLRY_DATA}; + int in_size[2], in_elem_size[2]; int out_identifier = OUT_BITSTREAM_DATA; int out_size, out_elem_size; - int read=0, i; + int pcmread=0, i, ret; int send_error; - void *in_ptr, *out_ptr; + void *in_ptr[2], *out_ptr; AACENC_ERROR err; + // Read data from the PAD fifo + if (padlen != 0) { + ret = read(pad_fd, pad_buf, padlen); + } + else { + ret = 0; + } + + if(ret < 0 && errno == EAGAIN) { + // If this condition passes, there is no data to be read + in_buf.numBufs = 1; // Samples; + } + else if(ret >= 0) { + // Otherwise, you're good to go and buffer should contain "count" bytes. + in_buf.numBufs = 2; // Samples + Data; + } + else { + // Some other error occurred during read. + fprintf(stderr, "Unable to read from PAD!\n"); + break; + } + if(raw_input) { if(fread(input_buf, input_size, 1, in_fh) == 1) { - read = input_size; + pcmread = input_size; } else { fprintf(stderr, "Unable to read from input!\n"); break; } } else { - read = wav_read_data(wav, input_buf, input_size); + pcmread = wav_read_data(wav, input_buf, input_size); } - for (i = 0; i < read/2; i++) { + for (i = 0; i < pcmread/2; i++) { const uint8_t* in = &input_buf[2*i]; convert_buf[i] = in[0] | (in[1] << 8); } - if (read <= 0) { + if (pcmread <= 0) { in_args.numInSamples = -1; } else { - in_ptr = convert_buf; - in_size = read; - in_elem_size = 2; - - in_args.numInSamples = read/2; - in_buf.numBufs = 1; - in_buf.bufs = &in_ptr; - in_buf.bufferIdentifiers = &in_identifier; - in_buf.bufSizes = &in_size; - in_buf.bufElSizes = &in_elem_size; + in_ptr[0] = convert_buf; + in_ptr[1] = pad_buf; + in_size[0] = pcmread; + in_size[1] = padlen; + + in_elem_size[0] = 2; + in_elem_size[1] = sizeof(UCHAR); + + in_args.numInSamples = pcmread/2; + in_args.numAncBytes = padlen; + + //in_buf.numBufs = 2; // Samples + Data + + in_buf.bufs = (void**)&in_ptr; + in_buf.bufferIdentifiers = in_identifier; + in_buf.bufSizes = in_size; + in_buf.bufElSizes = in_elem_size; + } out_ptr = outbuf; out_size = sizeof(outbuf); diff --git a/configure.ac b/configure.ac index 98f4066..5283420 100644 --- a/configure.ac +++ b/configure.ac @@ -7,15 +7,6 @@ AC_CONFIG_MACRO_DIR([m4]) AM_INIT_AUTOMAKE([tar-ustar foreign]) m4_ifdef([AM_SILENT_RULES], [AM_SILENT_RULES([yes])]) -dnl Various options for configure -AC_ARG_ENABLE([example], - [AS_HELP_STRING([--enable-example], - [enable example encoding program (default is no)])], - [example=$enableval], [example=no]) - -dnl Automake conditionals to set -AM_CONDITIONAL(EXAMPLE, test x$example = xyes) - dnl Checks for programs. AC_PROG_CC AC_PROG_CXX @@ -24,15 +15,16 @@ LT_INIT AC_CHECK_LIB([m], [sin]) -# fdk-aac-dabplus-zmq needs ZeroMQ -AC_ARG_ENABLE([zmq], - [AS_HELP_STRING([--enable-zmq], [Enable fdk-aac-dabplus-zmq with ZeroMQ output])] - ) - -AS_IF(test "x$enable_zmq" = "xyes", - AC_CHECK_LIB(zmq, zmq_init, , AC_MSG_ERROR(ZeroMQ libzmq is required))) +AC_CHECK_LIB(zmq, zmq_init, , AC_MSG_ERROR(ZeroMQ libzmq is required)) -AM_CONDITIONAL(HAVE_ZEROMQ_TEST, test "x$enable_zmq" = "xyes") +if pkg-config MagickWand; then + MAGICKWAND_CFLAGS=`pkg-config MagickWand --cflags` + MAGICKWAND_LDADD=`pkg-config MagickWand --libs` + AC_SUBST(MAGICKWAND_CPPFLAGS) + AC_SUBST(MAGICKWAND_LDADD) +else + AC_MSG_ERROR(MagickWand not found) +fi dnl soname version to use dnl goes by ‘current[:revision[:age]]’ with the soname ending up as diff --git a/contrib/lib_crc.c b/contrib/lib_crc.c new file mode 100644 index 0000000..8f71ffb --- /dev/null +++ b/contrib/lib_crc.c @@ -0,0 +1,459 @@ +#include "lib_crc.h"
+
+
+
+ /*******************************************************************\
+ * *
+ * Library : lib_crc *
+ * File : lib_crc.c *
+ * Author : Lammert Bies 1999-2008 *
+ * E-mail : info@lammertbies.nl *
+ * Language : ANSI C *
+ * *
+ * *
+ * Description *
+ * =========== *
+ * *
+ * The file lib_crc.c contains the private and public func- *
+ * tions used for the calculation of CRC-16, CRC-CCITT and *
+ * CRC-32 cyclic redundancy values. *
+ * *
+ * *
+ * Dependencies *
+ * ============ *
+ * *
+ * lib_crc.h CRC definitions and prototypes *
+ * *
+ * *
+ * Modification history *
+ * ==================== *
+ * *
+ * Date Version Comment *
+ * *
+ * 2008-04-20 1.16 Added CRC-CCITT calculation for Kermit *
+ * *
+ * 2007-04-01 1.15 Added CRC16 calculation for Modbus *
+ * *
+ * 2007-03-28 1.14 Added CRC16 routine for Sick devices *
+ * *
+ * 2005-12-17 1.13 Added CRC-CCITT with initial 0x1D0F *
+ * *
+ * 2005-05-14 1.12 Added CRC-CCITT with start value 0 *
+ * *
+ * 2005-02-05 1.11 Fixed bug in CRC-DNP routine *
+ * *
+ * 2005-02-04 1.10 Added CRC-DNP routines *
+ * *
+ * 1999-02-21 1.01 Added FALSE and TRUE mnemonics *
+ * *
+ * 1999-01-22 1.00 Initial source *
+ * *
+ \*******************************************************************/
+
+
+
+ /*******************************************************************\
+ * *
+ * #define P_xxxx *
+ * *
+ * The CRC's are computed using polynomials. The coefficients *
+ * for the algorithms are defined by the following constants. *
+ * *
+ \*******************************************************************/
+
+#define P_16 0xA001
+#define P_32 0xEDB88320L
+#define P_CCITT 0x1021
+#define P_DNP 0xA6BC
+#define P_KERMIT 0x8408
+#define P_SICK 0x8005
+
+
+
+ /*******************************************************************\
+ * *
+ * static int crc_tab...init *
+ * static unsigned ... crc_tab...[] *
+ * *
+ * The algorithms use tables with precalculated values. This *
+ * speeds up the calculation dramaticaly. The first time the *
+ * CRC function is called, the table for that specific calcu- *
+ * lation is set up. The ...init variables are used to deter- *
+ * mine if the initialization has taken place. The calculated *
+ * values are stored in the crc_tab... arrays. *
+ * *
+ * The variables are declared static. This makes them invisi- *
+ * ble for other modules of the program. *
+ * *
+ \*******************************************************************/
+
+static int crc_tab16_init = FALSE;
+static int crc_tab32_init = FALSE;
+static int crc_tabccitt_init = FALSE;
+static int crc_tabdnp_init = FALSE;
+static int crc_tabkermit_init = FALSE;
+
+static unsigned short crc_tab16[256];
+static unsigned long crc_tab32[256];
+static unsigned short crc_tabccitt[256];
+static unsigned short crc_tabdnp[256];
+static unsigned short crc_tabkermit[256];
+
+
+
+ /*******************************************************************\
+ * *
+ * static void init_crc...tab(); *
+ * *
+ * Three local functions are used to initialize the tables *
+ * with values for the algorithm. *
+ * *
+ \*******************************************************************/
+
+static void init_crc16_tab( void );
+static void init_crc32_tab( void );
+static void init_crcccitt_tab( void );
+static void init_crcdnp_tab( void );
+static void init_crckermit_tab( void );
+
+
+
+ /*******************************************************************\
+ * *
+ * unsigned short update_crc_ccitt( unsigned long crc, char c ); *
+ * *
+ * The function update_crc_ccitt calculates a new CRC-CCITT *
+ * value based on the previous value of the CRC and the next *
+ * byte of the data to be checked. *
+ * *
+ \*******************************************************************/
+
+unsigned short update_crc_ccitt( unsigned short crc, char c ) {
+
+ unsigned short tmp, short_c;
+
+ short_c = 0x00ff & (unsigned short) c;
+
+ if ( ! crc_tabccitt_init ) init_crcccitt_tab();
+
+ tmp = (crc >> 8) ^ short_c;
+ crc = (crc << 8) ^ crc_tabccitt[tmp];
+
+ return crc;
+
+} /* update_crc_ccitt */
+
+
+
+ /*******************************************************************\
+ * *
+ * unsigned short update_crc_sick( *
+ * unsigned long crc, char c, char prev_byte ); *
+ * *
+ * The function update_crc_sick calculates a new CRC-SICK *
+ * value based on the previous value of the CRC and the next *
+ * byte of the data to be checked. *
+ * *
+ \*******************************************************************/
+
+unsigned short update_crc_sick( unsigned short crc, char c, char prev_byte ) {
+
+ unsigned short short_c, short_p;
+
+ short_c = 0x00ff & (unsigned short) c;
+ short_p = ( 0x00ff & (unsigned short) prev_byte ) << 8;
+
+ if ( crc & 0x8000 ) crc = ( crc << 1 ) ^ P_SICK;
+ else crc = crc << 1;
+
+ crc &= 0xffff;
+ crc ^= ( short_c | short_p );
+
+ return crc;
+
+} /* update_crc_sick */
+
+
+
+ /*******************************************************************\
+ * *
+ * unsigned short update_crc_16( unsigned short crc, char c ); *
+ * *
+ * The function update_crc_16 calculates a new CRC-16 value *
+ * based on the previous value of the CRC and the next byte *
+ * of the data to be checked. *
+ * *
+ \*******************************************************************/
+
+unsigned short update_crc_16( unsigned short crc, char c ) {
+
+ unsigned short tmp, short_c;
+
+ short_c = 0x00ff & (unsigned short) c;
+
+ if ( ! crc_tab16_init ) init_crc16_tab();
+
+ tmp = crc ^ short_c;
+ crc = (crc >> 8) ^ crc_tab16[ tmp & 0xff ];
+
+ return crc;
+
+} /* update_crc_16 */
+
+
+
+ /*******************************************************************\
+ * *
+ * unsigned short update_crc_kermit( unsigned short crc, char c ); *
+ * *
+ * The function update_crc_kermit calculates a new CRC value *
+ * based on the previous value of the CRC and the next byte *
+ * of the data to be checked. *
+ * *
+ \*******************************************************************/
+
+unsigned short update_crc_kermit( unsigned short crc, char c ) {
+
+ unsigned short tmp, short_c;
+
+ short_c = 0x00ff & (unsigned short) c;
+
+ if ( ! crc_tabkermit_init ) init_crckermit_tab();
+
+ tmp = crc ^ short_c;
+ crc = (crc >> 8) ^ crc_tabkermit[ tmp & 0xff ];
+
+ return crc;
+
+} /* update_crc_kermit */
+
+
+
+ /*******************************************************************\
+ * *
+ * unsigned short update_crc_dnp( unsigned short crc, char c ); *
+ * *
+ * The function update_crc_dnp calculates a new CRC-DNP value *
+ * based on the previous value of the CRC and the next byte *
+ * of the data to be checked. *
+ * *
+ \*******************************************************************/
+
+unsigned short update_crc_dnp( unsigned short crc, char c ) {
+
+ unsigned short tmp, short_c;
+
+ short_c = 0x00ff & (unsigned short) c;
+
+ if ( ! crc_tabdnp_init ) init_crcdnp_tab();
+
+ tmp = crc ^ short_c;
+ crc = (crc >> 8) ^ crc_tabdnp[ tmp & 0xff ];
+
+ return crc;
+
+} /* update_crc_dnp */
+
+
+
+ /*******************************************************************\
+ * *
+ * unsigned long update_crc_32( unsigned long crc, char c ); *
+ * *
+ * The function update_crc_32 calculates a new CRC-32 value *
+ * based on the previous value of the CRC and the next byte *
+ * of the data to be checked. *
+ * *
+ \*******************************************************************/
+
+unsigned long update_crc_32( unsigned long crc, char c ) {
+
+ unsigned long tmp, long_c;
+
+ long_c = 0x000000ffL & (unsigned long) c;
+
+ if ( ! crc_tab32_init ) init_crc32_tab();
+
+ tmp = crc ^ long_c;
+ crc = (crc >> 8) ^ crc_tab32[ tmp & 0xff ];
+
+ return crc;
+
+} /* update_crc_32 */
+
+
+
+ /*******************************************************************\
+ * *
+ * static void init_crc16_tab( void ); *
+ * *
+ * The function init_crc16_tab() is used to fill the array *
+ * for calculation of the CRC-16 with values. *
+ * *
+ \*******************************************************************/
+
+static void init_crc16_tab( void ) {
+
+ int i, j;
+ unsigned short crc, c;
+
+ for (i=0; i<256; i++) {
+
+ crc = 0;
+ c = (unsigned short) i;
+
+ for (j=0; j<8; j++) {
+
+ if ( (crc ^ c) & 0x0001 ) crc = ( crc >> 1 ) ^ P_16;
+ else crc = crc >> 1;
+
+ c = c >> 1;
+ }
+
+ crc_tab16[i] = crc;
+ }
+
+ crc_tab16_init = TRUE;
+
+} /* init_crc16_tab */
+
+
+
+ /*******************************************************************\
+ * *
+ * static void init_crckermit_tab( void ); *
+ * *
+ * The function init_crckermit_tab() is used to fill the array *
+ * for calculation of the CRC Kermit with values. *
+ * *
+ \*******************************************************************/
+
+static void init_crckermit_tab( void ) {
+
+ int i, j;
+ unsigned short crc, c;
+
+ for (i=0; i<256; i++) {
+
+ crc = 0;
+ c = (unsigned short) i;
+
+ for (j=0; j<8; j++) {
+
+ if ( (crc ^ c) & 0x0001 ) crc = ( crc >> 1 ) ^ P_KERMIT;
+ else crc = crc >> 1;
+
+ c = c >> 1;
+ }
+
+ crc_tabkermit[i] = crc;
+ }
+
+ crc_tabkermit_init = TRUE;
+
+} /* init_crckermit_tab */
+
+
+
+ /*******************************************************************\
+ * *
+ * static void init_crcdnp_tab( void ); *
+ * *
+ * The function init_crcdnp_tab() is used to fill the array *
+ * for calculation of the CRC-DNP with values. *
+ * *
+ \*******************************************************************/
+
+static void init_crcdnp_tab( void ) {
+
+ int i, j;
+ unsigned short crc, c;
+
+ for (i=0; i<256; i++) {
+
+ crc = 0;
+ c = (unsigned short) i;
+
+ for (j=0; j<8; j++) {
+
+ if ( (crc ^ c) & 0x0001 ) crc = ( crc >> 1 ) ^ P_DNP;
+ else crc = crc >> 1;
+
+ c = c >> 1;
+ }
+
+ crc_tabdnp[i] = crc;
+ }
+
+ crc_tabdnp_init = TRUE;
+
+} /* init_crcdnp_tab */
+
+
+
+ /*******************************************************************\
+ * *
+ * static void init_crc32_tab( void ); *
+ * *
+ * The function init_crc32_tab() is used to fill the array *
+ * for calculation of the CRC-32 with values. *
+ * *
+ \*******************************************************************/
+
+static void init_crc32_tab( void ) {
+
+ int i, j;
+ unsigned long crc;
+
+ for (i=0; i<256; i++) {
+
+ crc = (unsigned long) i;
+
+ for (j=0; j<8; j++) {
+
+ if ( crc & 0x00000001L ) crc = ( crc >> 1 ) ^ P_32;
+ else crc = crc >> 1;
+ }
+
+ crc_tab32[i] = crc;
+ }
+
+ crc_tab32_init = TRUE;
+
+} /* init_crc32_tab */
+
+
+
+ /*******************************************************************\
+ * *
+ * static void init_crcccitt_tab( void ); *
+ * *
+ * The function init_crcccitt_tab() is used to fill the array *
+ * for calculation of the CRC-CCITT with values. *
+ * *
+ \*******************************************************************/
+
+static void init_crcccitt_tab( void ) {
+
+ int i, j;
+ unsigned short crc, c;
+
+ for (i=0; i<256; i++) {
+
+ crc = 0;
+ c = ((unsigned short) i) << 8;
+
+ for (j=0; j<8; j++) {
+
+ if ( (crc ^ c) & 0x8000 ) crc = ( crc << 1 ) ^ P_CCITT;
+ else crc = crc << 1;
+
+ c = c << 1;
+ }
+
+ crc_tabccitt[i] = crc;
+ }
+
+ crc_tabccitt_init = TRUE;
+
+} /* init_crcccitt_tab */
diff --git a/contrib/lib_crc.h b/contrib/lib_crc.h new file mode 100644 index 0000000..0e559e6 --- /dev/null +++ b/contrib/lib_crc.h @@ -0,0 +1,66 @@ + /*******************************************************************\
+ * *
+ * Library : lib_crc *
+ * File : lib_crc.h *
+ * Author : Lammert Bies 1999-2008 *
+ * E-mail : info@lammertbies.nl *
+ * Language : ANSI C *
+ * *
+ * *
+ * Description *
+ * =========== *
+ * *
+ * The file lib_crc.h contains public definitions and proto- *
+ * types for the CRC functions present in lib_crc.c. *
+ * *
+ * *
+ * Dependencies *
+ * ============ *
+ * *
+ * none *
+ * *
+ * *
+ * Modification history *
+ * ==================== *
+ * *
+ * Date Version Comment *
+ * *
+ * 2008-04-20 1.16 Added CRC-CCITT routine for Kermit *
+ * *
+ * 2007-04-01 1.15 Added CRC16 calculation for Modbus *
+ * *
+ * 2007-03-28 1.14 Added CRC16 routine for Sick devices *
+ * *
+ * 2005-12-17 1.13 Added CRC-CCITT with initial 0x1D0F *
+ * *
+ * 2005-02-14 1.12 Added CRC-CCITT with initial 0x0000 *
+ * *
+ * 2005-02-05 1.11 Fixed bug in CRC-DNP routine *
+ * *
+ * 2005-02-04 1.10 Added CRC-DNP routines *
+ * *
+ * 2005-01-07 1.02 Changes in tst_crc.c *
+ * *
+ * 1999-02-21 1.01 Added FALSE and TRUE mnemonics *
+ * *
+ * 1999-01-22 1.00 Initial source *
+ * *
+ \*******************************************************************/
+
+
+
+#define CRC_VERSION "1.16"
+
+
+
+#define FALSE 0
+#define TRUE 1
+
+
+
+unsigned short update_crc_16( unsigned short crc, char c );
+unsigned long update_crc_32( unsigned long crc, char c );
+unsigned short update_crc_ccitt( unsigned short crc, char c );
+unsigned short update_crc_dnp( unsigned short crc, char c );
+unsigned short update_crc_kermit( unsigned short crc, char c );
+unsigned short update_crc_sick( unsigned short crc, char c, char prev_byte );
diff --git a/mot-encoder.c b/mot-encoder.c new file mode 100644 index 0000000..7325cee --- /dev/null +++ b/mot-encoder.c @@ -0,0 +1,638 @@ +/* + Copyright (C) 2014 CSP Innovazione nelle ICT s.c.a r.l. (http://www.csp.it/) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. + + MotEncoder.c + Generete PAD data for MOT Slideshow and DLS + + Authors: + Sergio Sagliocco <sergio.sagliocco@csp.it> +*/ + +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <string.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <dirent.h> +#include <wand/magick_wand.h> + +#include "lib_crc.h" + +#define MIN(a,b) (((a)<(b))?(a):(b)) +#define MAX(a,b) (((a)>(b))?(a):(b)) + +#define MAXSEGLEN 8179 +#define MAXDLS 129 + +typedef unsigned char UCHAR; +typedef unsigned short int USHORT; + +typedef struct { + // MSC Data Group Header (extension field not supported) + unsigned char extflag; // 1 bit + unsigned char crcflag; // 1 bit + unsigned char segflag; // 1 bit + unsigned char accflag; // 1 bit + unsigned char dgtype; // 4 bits + unsigned char cindex; // 4 bits + unsigned char rindex; // 4 bits + /// Session header - Segment field + unsigned char last; // 1 bit + unsigned short int segnum; // 16 bits + // Session header - User access field + unsigned char rfa; // 3 bits + unsigned char tidflag; // 1 bit + unsigned char lenid; // 4 bits - Fixed to value 2 in this implemntation + unsigned short int tid; // 16 bits + // MSC data group data field + // Mot Segmentation header + unsigned char rcount; // 3 bits + unsigned short int seglen; // 13 bits + // Mot segment + unsigned char* segdata; + // MSC data group CRC + unsigned short int crc; // 16 bits +} MSCDG; +/* + typedef struct { +// MOT HEADER CUSTOMIZED FOR SLIDESHOW APP +unsigned int bodysize; // 28 bits +unsigned short int headsize; // 13 bits +unsigned char ctype; // 6 bits +unsigned char sctype; // 9 bits +unsigned char triggertime[5]; // 0x85 0x00 0x00 0x00 0x00 => NOW +unsigned char contname[14]; // 0xCC 0x0C 0x00 imgXXXX.jpg +} MOTSLIDEHDR; +*/ +int encodeFile (char* fname, int fidx, int padlen); +void createMotHeader(size_t blobsize, int fidx, char* mothdr, int* mothdrlen); +void createMscDG(MSCDG* msc, unsigned short int dgtype, unsigned short int cindex, unsigned short int lastseg, unsigned short int tid, unsigned char* data, unsigned short int datalen); +void packMscDG(unsigned char* mscblob, MSCDG* msc, unsigned short int *bsize); +void writeMotPAD(unsigned char* mscdg, unsigned short int mscdgsize, unsigned short int padlen); +void create_dls_datagroup (char* text, int padlen, unsigned char*** p_dlsdg, int* p_numdg); +void writeDLS(int padlen); + +void usage(char* name) +{ + fprintf(stderr, "DAB MOT encoder\n" + "for slideshow and DLS\n\n" + "By CSP Innovazione nelle ICT s.c.a r.l. (http://www.csp.it/)\n\n" + "Reads image data from the specified directory, and outputs PAD data\n" + "on standard output\n" + "Reads DLS from /tmp/dls.file\n"); + fprintf(stderr, "Usage: %s <dirname>\n", name); +} + +int main (int argc, char *argv[]) +{ + int len,fidx,ret; + struct dirent *pDirent; + DIR *pDir; + char imagepath[128]; + char dlstext[MAXDLS],dlstextprev[MAXDLS]; + int padlen=53; + + + if (argc < 2) { + usage(argv[0]); + return 0; + } + + MagickWandGenesis(); + + fidx=0; + while(1) { + pDir = opendir (argv[1]); + if (pDir == NULL) { + printf ("Cannot open directory '%s'\n", argv[1]); + return 1; + } + if (fidx == 9999) fidx=0; + while ((pDirent = readdir(pDir)) != NULL) { + if (pDirent->d_name[0]!='.') { + sprintf (imagepath,"%s/%s",argv[1],pDirent->d_name); + ret=encodeFile(imagepath, fidx, padlen); + if (ret!=1) { + fprintf(stderr,"Error - Cannot encode file %s\n",pDirent->d_name); + } else { + fidx++; + writeDLS(padlen); + sleep(10); + } + } + } + closedir (pDir); + } + return 1; +} + +int encodeFile (char* fname, int fidx, int padlen) +{ + int fd=0,ret,mothdrlen,nseg,lastseglen,i,last,curseglen; + unsigned char mothdr[32]; + MagickWand *m_wand = NULL; + PixelWand *p_wand = NULL; + size_t blobsize,height,width; + unsigned char *blob = NULL, *curseg = NULL; + MagickBooleanType err; + MSCDG msc; + unsigned char mscblob[8200]; + unsigned short int mscblobsize; + //float aspectRatio; + + + m_wand = NewMagickWand(); + p_wand = NewPixelWand(); + PixelSetColor(p_wand,"black"); + + err = MagickReadImage(m_wand,fname); + if (err==MagickFalse) { + fprintf(stderr,"Error - Unable to load image %s\n",fname); + ret=0; + goto RETURN; + } + + height=MagickGetImageHeight(m_wand); + width=MagickGetImageWidth(m_wand); + //aspectRatio = (width * 1.0)/height; + + fprintf(stderr,"Image: %s (id=%d). Original size: %d x %d. ",fname,fidx,width,height); + + while (height>240 || width>320) { + if (height/240.0 > width/320.0) { + //width = height * aspectRatio; + width = width * 240.0 / height; + height = 240; + } + else { + //height = width * (1.0/aspectRatio); + height = height * 320.0 / width; + width = 320; + } + MagickResizeImage(m_wand,width,height,LanczosFilter,1); + } + + + + height=MagickGetImageHeight(m_wand); + width=MagickGetImageWidth(m_wand); + + MagickBorderImage(m_wand,p_wand,(320-width)/2,(240-height)/2); + + MagickSetImageCompressionQuality(m_wand,75); + MagickSetImageFormat(m_wand,"jpg"); + blob=MagickGetImagesBlob(m_wand, &blobsize); + fprintf(stderr,"Resized to %d x %d. Size after compression %d bytes\n",width,height,blobsize); + + nseg=blobsize / MAXSEGLEN; + lastseglen=blobsize % MAXSEGLEN; + if (lastseglen !=0) nseg++; + + createMotHeader(blobsize,fidx,mothdr,&mothdrlen); + // Create the MSC Data Group C-Structure + createMscDG(&msc,3,0,1,fidx,mothdr,mothdrlen); + // Generate the MSC DG frame (Figure 9 en 300 401) + packMscDG(mscblob,&msc,&mscblobsize); + writeMotPAD(mscblob,mscblobsize,padlen); + + for (i=0;i<nseg;i++) { + curseg=blob+i*MAXSEGLEN; + if (i==nseg-1) { + curseglen=lastseglen; + last=1; + } + else { + curseglen=MAXSEGLEN; + last=0; + } + + createMscDG(&msc,4,i,last,fidx,curseg,curseglen); + packMscDG(mscblob,&msc,&mscblobsize); + writeMotPAD(mscblob,mscblobsize,padlen); + } + + ret=1; + +RETURN: + if (m_wand) { + m_wand = DestroyMagickWand(m_wand); + } + if (blob) { + free(blob); + } + return ret; +} + + +void createMotHeader(size_t blobsize, int fidx, char* mothdr,int* mothdrlen) +{ + int ret; + struct stat s; + char MotHeaderCore[7]= {0x00,0x00,0x00,0x00,0x0D,0x04,0x01}; + char MotHeaderExt[19]= {0x85,0x00,0x00,0x00,0x00,0xcc,0x0c,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; + char cntemp[12]; + int i; + + MotHeaderCore[0]=(blobsize<<4 & 0xFF000000)>>24; + MotHeaderCore[1]=(blobsize<<4 & 0x00FF0000)>>16; + MotHeaderCore[2]=(blobsize<<4 & 0x0000FF00)>>8; + MotHeaderCore[3]=(blobsize<<4 & 0x000000FF); + + sprintf(cntemp,"img%04d.jpg",fidx); + for (i=0; i<sizeof(cntemp); i++) { + MotHeaderExt[8+i]=cntemp[i]; + } + *mothdrlen=26; + for (i=0;i<7;i++) + mothdr[i]=MotHeaderCore[i]; + for (i=0;i<19;i++) + mothdr[7+i]=MotHeaderExt[i]; + + return; +} + +void createMscDG(MSCDG* msc, unsigned short int dgtype, unsigned short int cindex, unsigned short int lastseg, unsigned short int tid, unsigned char* data, unsigned short int datalen) +{ + msc->extflag=0; + msc->crcflag=1; + msc->segflag=1; + msc->accflag=1; + msc->dgtype=dgtype; + msc->cindex=cindex; + msc->rindex=0; + msc->last=lastseg; + msc->segnum=cindex; + msc->rfa=0; + msc->tidflag=1; + msc->lenid=2; + msc->tid=tid; + msc->segdata=data; + msc->rcount=0; + msc->seglen=datalen; + + return; +} + + +void packMscDG(unsigned char* b, MSCDG* msc, unsigned short int* bsize) { + + int i; + unsigned short int crc=0xFFFF; + + b[0]=(msc->extflag<<7)|(msc->crcflag<<6)|(msc->segflag<<5)|(msc->accflag<<4)|msc->dgtype; + b[1]=(msc->cindex<<4)|msc->rindex; + b[2]=(msc->last<<7)|((msc->segnum & 0x7F00)>>8); + b[3]=msc->segnum & 0x00FF; + b[4]=0; + b[4]=(msc->rfa<<5)|(msc->tidflag<<4)|msc->lenid; + b[5]=(msc->tid & 0xFF00)>>8; + b[6]=msc->tid & 0x00FF; + b[7]=(msc->rcount<<5)|((msc->seglen & 0x1F00)>>8); + b[8]=msc->seglen & 0x00FF; + + for (i=0;i<9;i++) + crc = update_crc_ccitt(crc, b[i]); + + for(i=0;i<msc->seglen;i++) { + b[i+9]=(msc->segdata)[i]; + crc = update_crc_ccitt(crc, b[i+9]); + } + + crc=~crc; + b[9+msc->seglen] = (crc & 0xFF00) >> 8; // HI CRC + b[9+msc->seglen+1] = crc & 0x00FF; // LO CRC + + *bsize=9+msc->seglen+1+1; + + //write(1,b,9+msc->seglen+1+1); + +} + + +void writeMotPAD(unsigned char* mscdg, unsigned short int mscdgsize, unsigned short int padlen) { + + unsigned char pad[128]; + int xpadlengthmask,i,j,numseg,lastseglen; + unsigned short int crc; + + + if (padlen==17) + xpadlengthmask=3; + else if (padlen==21) + xpadlengthmask=4; + else if (padlen==29) + xpadlengthmask=5; + else if (padlen==37) + xpadlengthmask=6; + else if (padlen==53) + xpadlengthmask=7; + + + // Write Data Group Length Indicator + crc = 0xffff; + pad[padlen-1]=0x02; // FF-PAD Byte L (CI=1) + pad[padlen-2]=0x20; // FF-PAD Byte L-1 (Variable size X_PAD) + pad[padlen-3]=(xpadlengthmask<<5) | 0x01; // CI => data length = 12 (011) - Application Type=2 (DLS - start of X-PAD data group) + pad[padlen-4]=0x00; // End of CI list + pad[padlen-5]=(mscdgsize & 0x3F00)>>8; // RFA+HI Data group length + pad[padlen-6]=(mscdgsize & 0x00FF); + crc = update_crc_ccitt(crc, pad[padlen-5]); + crc = update_crc_ccitt(crc, pad[padlen-6]); + crc = ~crc; + pad[padlen-7]=(crc & 0xFF00) >> 8; // HI CRC + pad[padlen-8]=(crc & 0x00FF); // LO CRC + for (i=padlen-9;i>=0;i--) { // NULL PADDING + pad[i]=0x00; + } + write(STDOUT_FILENO,pad,padlen); + //fprintf(stderr,"Data Group Length Indicator: "); + //for (i=0;i<padlen;i++) fprintf(stderr,"%02x ",pad[i]); + //fprintf(stderr,"\n"); + + // Write MSC Data Groupsd + numseg=mscdgsize / (padlen-5); + lastseglen=mscdgsize % (padlen-5); + if (lastseglen > 0) { + numseg++; // The last incomplete segment + } + + for (i=0;i<numseg;i++) { + + char* curseg; + int curseglen; + UCHAR firstseg; + + curseg = &mscdg[i*(padlen-5)]; + //fprintf(stderr,"Segment number %d\n",i+1); + + if (i==0) // First segment + firstseg=1; + else + firstseg=0; + + if (i==numseg-1) { //Last segment + if (lastseglen!=0) + curseglen=lastseglen; + else + curseglen=padlen-5; + } else { + curseglen=padlen-5; + } + + pad[padlen-1]=0x02; // FF-PAD Byte L (CI=1) + pad[padlen-2]=0x20; // FF-PAD Byte L-1 (Variable size X_PAD) + if (firstseg==1) + pad[padlen-3]=(xpadlengthmask<<5) | 12; // CI => data length = 12 (011) - Application Type=12 (start of MOT) + else + pad[padlen-3]=(xpadlengthmask<<5) | 13; // CI => data length = 12 (011) - Application Type=13 (MOT) + + pad[padlen-4]=0x00; // End of CI list + + for (j=0; j<curseglen; j++) + pad[padlen-5-j]=curseg[j]; + for (j=padlen-5-curseglen;j>=0;j--) + pad[j]=0x00; + + write(STDOUT_FILENO,pad,padlen); + //fprintf(stderr,"MSC Data Group - Segment %d: ",i); + //for (j=0;j<padlen;j++) fprintf(stderr,"%02x ",pad[j]); + // fprintf(stderr,"\n"); + + } +} + + +void writeDLS(int padlen) { + char dlstext[MAXDLS]; + static char dlstextprev[MAXDLS]; + int dlslen,i; + static unsigned char** dlsdg; + static int numdg=0; + + static int dlsfd=0; + + if (dlsfd!=0) close(dlsfd); + dlsfd=open("/tmp/dls.file",O_RDONLY); + if (dlsfd==-1) { + fprintf(stderr,"Error - Cannot open dls file\n"); + return; + } + + dlslen=read(dlsfd, dlstext, MAXDLS); + dlstext[dlslen]=0x00; + //if (strcmp(dlstext,dlstextprev)!=0) { + create_dls_datagroup(dlstext,padlen,&dlsdg,&numdg); + strcpy(dlstextprev,dlstext); + //} + for (i=0;i<numdg;i++) { + write(STDOUT_FILENO,dlsdg[i],padlen); + } + +} + +void create_dls_datagroup (char* text, int padlen, UCHAR*** p_dlsdg, int* p_numdg) { + + UCHAR dlsseg[8][16]; // max 8 segments, each max 16 chars + UCHAR** dlsdg; // Array od datagroups composing dls text; + + + int numseg; // Number of DSL segments + int lastseglen; // Length of the last segment + int numdg; // Number of data group + int xpadlengthmask; + int i,j,k,z,idx_start_crc,idx_stop_crc; + USHORT dlscrc; + static UCHAR toggle=0; + + if (toggle==0) + toggle=1; + else + toggle=0; + + numseg=strlen(text) / 16; + lastseglen=strlen(text) % 16; + if (padlen-9 >= 16) { + if (lastseglen > 0) numseg++; // The last incomplete segment + numdg=numseg; // The PAD can contain the full segmnet and overhead (9 bytes) + } else { + numdg=numseg*2; // Each 16 char segment span over 2 dg + if (lastseglen > 0) { + numseg++; // The last incomplete segment + if (lastseglen <= padlen-9) + numdg+=1; + else + numdg+=2; + + } + } + *p_numdg=numdg; + fprintf(stderr,"PAD Length: %d\n",padlen); + fprintf(stderr,"DLS text: %s\n",text); + fprintf(stderr,"Number od DLS segments: %d\n",numseg); + fprintf(stderr,"Number od DLS data grupus: %d\n",numdg); + + if (padlen==17) + xpadlengthmask=3; + else if (padlen==21) + xpadlengthmask=4; + else if (padlen==29) + xpadlengthmask=5; + else if (padlen==37) + xpadlengthmask=6; + else if (padlen==53) + xpadlengthmask=7; + + *p_dlsdg = (UCHAR**) malloc(numdg*sizeof(UCHAR*)); + dlsdg=*p_dlsdg; + + i=0; + for (z=0;z<numseg;z++) { + + char* curseg; + int curseglen; + UCHAR firstseg,lastseg; + + curseg = &text[z*16]; + fprintf(stderr,"Segment number %d\n",z+1); + + if (z==0) // First segment + firstseg=1; + else + firstseg=0; + + if (z==numseg-1) { //Last segment + if (lastseglen!=0) + curseglen=lastseglen; + else + curseglen=16; + lastseg=1; + } else { + curseglen=16; + lastseg=0; + } + + if (curseglen<=padlen-9) { // Segment is composed by 1 data group + dlsdg[i]=(UCHAR*) malloc(padlen*sizeof(UCHAR)); + dlsdg[i][padlen-1]=0x02; // FF-PAD Byte L (CI=1) + dlsdg[i][padlen-2]=0x20; // FF-PAD Byte L-1 (Variable size X_PAD) + dlsdg[i][padlen-3]=(xpadlengthmask<<5) | 0x02; // CI => data length = 12 (011) - Application Type=2 (DLS - start of X-PAD data group) + dlsdg[i][padlen-4]=0x00; // End of CI list + + dlsdg[i][padlen-5]=((toggle*8+firstseg*4+lastseg*2+0)<<4) | (curseglen-1); // DLS Prefix (T=1,Only one segment,segment length-1) + + if (firstseg==1) + dlsdg[i][padlen-6]=0x00; // DLS Prefix (Charset standard) + else + dlsdg[i][padlen-6]=z<<4; // DLS SegNum + + idx_start_crc = padlen-5; // CRC start from prefix + for (j=0;j<curseglen;j++) { // DLS text + dlsdg[i][padlen-7-j]=curseg[j]; + } + + idx_stop_crc = padlen-7-curseglen+1; + + dlscrc = 0xffff; + for (j=idx_start_crc;j>=idx_stop_crc;j--) { + dlscrc = update_crc_ccitt(dlscrc, dlsdg[i][j]); + } + + dlscrc = ~dlscrc; + fprintf(stderr,"crc=%x ~crc=%x\n",~dlscrc,dlscrc); + + dlsdg[i][padlen-7-curseglen] = (dlscrc & 0xFF00) >> 8; // HI CRC + dlsdg[i][padlen-7-curseglen-1] = dlscrc & 0x00FF; // LO CRC + + for (j=padlen-7-curseglen-2;j>=0;j--) { // NULL PADDING + dlsdg[i][j]=0x00; + } + fprintf(stderr,"Data group: "); + for (j=0;j<padlen;j++) fprintf(stderr,"%x ",dlsdg[i][j]); + fprintf(stderr,"\n"); + i++; + + } else { // Segment is composed by 2 data groups + + // FIRST DG (NO CRC) + dlscrc = 0xffff; + + dlsdg[i]=(UCHAR*) malloc(padlen*sizeof(UCHAR)); + dlsdg[i][padlen-1]=0x02; // FF-PAD Byte L (CI=1) + dlsdg[i][padlen-2]=0x20; // FF-PAD Byte L-1 (Variable size X_PAD) + dlsdg[i][padlen-3]=(xpadlengthmask<<5) | 0x02; // CI => data length = 12 (011) - Application Type=2 (DLS - start of X-PAD data group) + dlsdg[i][padlen-4]=0x00; // End of CI list + dlsdg[i][padlen-5]=((toggle*8+firstseg*4+lastseg*2+0)<<4) | (curseglen-1); // DLS Prefix (T=1,Only one segment,segment length-1) + + if (firstseg==1) + dlsdg[i][padlen-6]=0x00; // DLS Prefix (Charset standard) + else + dlsdg[i][padlen-6]=(i-1)<<4; // DLS SegNum + + dlscrc = update_crc_ccitt(dlscrc, dlsdg[i][padlen-5]); + dlscrc = update_crc_ccitt(dlscrc, dlsdg[i][padlen-6]); + + for (j=0;j < MIN(curseglen,padlen-7); j++) { // DLS text + dlsdg[i][padlen-7-j]=curseg[j]; + dlscrc = update_crc_ccitt(dlscrc, dlsdg[i][padlen-7-j]); + } + k=j; + if (curseglen==padlen-8) { // end of segment + dlscrc = ~dlscrc; + dlsdg[i][1] = (dlscrc & 0xFF00) >> 8; // HI CRC + fprintf(stderr,"crc=%x ~crc=%x\n",~dlscrc,dlscrc); + } else if (curseglen==padlen-7) { + dlscrc = ~dlscrc; + fprintf(stderr,"crc=%x ~crc=%x\n",~dlscrc,dlscrc); + } + dlsdg[i][0]=0x00; + + fprintf(stderr,"First Data group: "); + for (j=0;j<padlen;j++) fprintf(stderr,"%x ",dlsdg[i][j]); + fprintf(stderr,"\n"); + + // SECOND DG (NO CI, NO PREFIX) + i++; + + dlsdg[i]=(UCHAR*) malloc(padlen*sizeof(UCHAR)); + dlsdg[i][padlen-1]=0x00; // FF-PAD Byte L (CI=0) + dlsdg[i][padlen-2]=0x20; // FF-PAD Byte L-1 (Variable size X_PAD) + if (curseglen==padlen-8) { + dlsdg[i][padlen-3] = dlscrc & 0x00FF; // LO CRC + } else if (curseglen==padlen-7) { + dlsdg[i][padlen-3] = (dlscrc & 0xFF00) >> 8; // HI CRC + dlsdg[i][padlen-4] = dlscrc & 0x00FF; // LO CRC + } else { + for (j=0;j < curseglen-k; j++) { // DLS text + dlsdg[i][padlen-3-j]=curseg[k+j]; + dlscrc = update_crc_ccitt(dlscrc, dlsdg[i][padlen-3-j]); + } + dlscrc = ~dlscrc; + dlsdg[i][padlen-3-curseglen+k]=(dlscrc & 0xFF00) >> 8; // HI CRC + dlsdg[i][padlen-3-curseglen+k-1]=dlscrc & 0x00FF; // LO CRC + } + + fprintf(stderr,"Second Data group: "); + for (j=0;j<padlen;j++) fprintf(stderr,"%x ",dlsdg[i][j]); + fprintf(stderr,"\n"); + fprintf(stderr,"**** crc=%x ~crc=%x\n",~dlscrc,dlscrc); + i++; + } + + } +} + |