summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--aac-enc-dabplus-zmq.c867
-rw-r--r--aac-enc-dabplus.c698
-rw-r--r--libAACenc/src/bitenc.cpp2
-rw-r--r--mot-encoder.c1060
-rw-r--r--wavreader.c220
5 files changed, 1424 insertions, 1423 deletions
diff --git a/aac-enc-dabplus-zmq.c b/aac-enc-dabplus-zmq.c
index febf3eb..febbdd9 100644
--- a/aac-enc-dabplus-zmq.c
+++ b/aac-enc-dabplus-zmq.c
@@ -44,31 +44,31 @@
#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://rd.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"
-//" -d, --data=FILENAME Set data filename.\n"
-//" -g, --fs-bug Turn on FS bug mitigation.\n"
-" -i, --input=FILENAME Input filename (default: stdin).\n"
-" -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"
-" -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"
-//" -t, --type=TYPE Set data type (dls|pad|packet|dg).\n"
-//" -v, --verbose=LEVEL Set verbosity level.\n"
-//" -V, --version Print version and exit.\n"
-//" --mi=[ 0, ... ] Set AAC frame messages interval in milliseconds.\n"
-//" --ma=[ 0, ... ] Set AAC frame messages attack time in milliseconds.\n"
-//" -l, --lp Set frame size to 1024 instead of 960.\n"
+ 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://rd.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"
+//" -d, --data=FILENAME Set data filename.\n"
+//" -g, --fs-bug Turn on FS bug mitigation.\n"
+" -i, --input=FILENAME Input filename (default: stdin).\n"
+" -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"
+" -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"
+//" -t, --type=TYPE Set data type (dls|pad|packet|dg).\n"
+//" -v, --verbose=LEVEL Set verbosity level.\n"
+//" -V, --version Print version and exit.\n"
+//" --mi=[ 0, ... ] Set AAC frame messages interval in milliseconds.\n"
+//" --ma=[ 0, ... ] Set AAC frame messages attack time in milliseconds.\n"
+//" -l, --lp Set frame size to 1024 instead of 960.\n"
"\n"
"Only the tcp:// zeromq transport has been tested until now.\n"
@@ -82,420 +82,421 @@ void usage(const char* name) {
#define optional_argument 2
int main(int argc, char *argv[]) {
- int subchannel_index = 8; //64kbps subchannel
- int ch=0;
- const char *infile = NULL;
- const char *outuri = NULL;
- FILE *in_fh;
- void *wav;
- int wav_format, bits_per_sample, sample_rate=48000, channels=2;
- uint8_t* input_buf;
- int16_t* convert_buf;
- void *rs_handler = NULL;
- int aot = AOT_DABPLUS_AAC_LC;
- int afterburner = 0, raw_input=0;
- HANDLE_AACENCODER handle;
- 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'},
- {"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:p:", longopts, &index);
- switch (ch) {
- case 'f':
- if(strcmp(optarg, "raw")==0) {
- raw_input = 1;
- } else if(strcmp(optarg, "wav")!=0)
- usage(argv[0]);
- break;
- case 'a':
- afterburner = 1;
- break;
- case 'b':
- subchannel_index = atoi(optarg) / 8;
- break;
- case 'c':
- channels = atoi(optarg);
- break;
- case 'r':
- sample_rate = atoi(optarg);
- break;
- case 'i':
- infile = optarg;
- break;
- case 'o':
- outuri = optarg;
- break;
- case 'p':
- padlen = atoi(optarg);
- break;
- case '?':
- case 'h':
- usage(argv[0]);
- return 1;
- }
- }
-
- if(subchannel_index < 1 || subchannel_index > 24) {
- 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, "-")) {
- in_fh = fopen(infile, "rb");
- if(!in_fh) {
- fprintf(stderr, "Can't open input file!\n");
- return 1;
- }
- } else {
- in_fh = stdin;
- }
- } else {
- wav = wav_read_open(infile);
- if (!wav) {
- fprintf(stderr, "Unable to open wav file %s\n", infile);
- return 1;
- }
- if (!wav_get_header(wav, &wav_format, &channels, &sample_rate, &bits_per_sample, NULL)) {
- fprintf(stderr, "Bad wav file %s\n", infile);
- return 1;
- }
- if (wav_format != 1) {
- fprintf(stderr, "Unsupported WAV format %d\n", wav_format);
- return 1;
- }
- if (bits_per_sample != 16) {
- fprintf(stderr, "Unsupported WAV sample depth %d\n", bits_per_sample);
- return 1;
- }
- if (channels > 2) {
- fprintf(stderr, "Unsupported WAV channels %d\n", channels);
- return 1;
- }
- }
-
- if (outuri) {
- zmq_sock = zmq_socket(zmq_context, ZMQ_PUB);
- if (zmq_sock == NULL) {
- fprintf(stderr, "Error occurred during zmq_socket: %s\n", zmq_strerror(errno));
- return 2;
- }
- if (zmq_connect(zmq_sock, outuri) != 0) {
- fprintf(stderr, "Error occurred during zmq_connect: %s\n", zmq_strerror(errno));
- return 2;
- }
- } else {
- fprintf(stderr, "Output URI not defined\n");
- return 1;
- }
-
-
- switch (channels) {
- case 1: mode = MODE_1; break;
- case 2: mode = MODE_2; break;
- default:
- fprintf(stderr, "Unsupported channels number %d\n", channels);
- return 1;
- }
-
-
- if (aacEncOpen(&handle, 0x01|0x02|0x04, channels) != AACENC_OK) {
- fprintf(stderr, "Unable to open encoder\n");
- return 1;
- }
-
-
- if(channels == 2 && subchannel_index <= 6)
- aot = AOT_DABPLUS_PS;
- else if((channels == 1 && subchannel_index <= 8) || subchannel_index <= 10)
- aot = AOT_DABPLUS_SBR;
-
- fprintf(stderr, "Using %d subchannels. AAC type: %s%s%s. channels=%d, sample_rate=%d\n",
- subchannel_index,
- aot == AOT_DABPLUS_PS ? "HE-AAC v2" : "",
- aot == AOT_DABPLUS_SBR ? "HE-AAC" : "",
- aot == AOT_DABPLUS_AAC_LC ? "AAC-LC" : "",
- channels, sample_rate);
-
- if (aacEncoder_SetParam(handle, AACENC_AOT, aot) != AACENC_OK) {
- fprintf(stderr, "Unable to set the AOT\n");
- return 1;
- }
- if (aacEncoder_SetParam(handle, AACENC_SAMPLERATE, sample_rate) != AACENC_OK) {
- fprintf(stderr, "Unable to set the samplerate\n");
- return 1;
- }
- if (aacEncoder_SetParam(handle, AACENC_CHANNELMODE, mode) != AACENC_OK) {
- fprintf(stderr, "Unable to set the channel mode\n");
- return 1;
- }
- if (aacEncoder_SetParam(handle, AACENC_CHANNELORDER, 1) != AACENC_OK) {
- fprintf(stderr, "Unable to set the wav channel order\n");
- return 1;
- }
- if (aacEncoder_SetParam(handle, AACENC_GRANULE_LENGTH, 960) != AACENC_OK) {
- fprintf(stderr, "Unable to set the granule length\n");
- return 1;
- }
- if (aacEncoder_SetParam(handle, AACENC_TRANSMUX, TT_DABPLUS) != AACENC_OK) {
- fprintf(stderr, "Unable to set the RAW transmux\n");
- return 1;
- }
-
- /*if (aacEncoder_SetParam(handle, AACENC_BITRATEMODE, 7 *AACENC_BR_MODE_SFR*) != AACENC_OK) {
- fprintf(stderr, "Unable to set the bitrate mode\n");
- return 1;
- }*/
-
-
- fprintf(stderr, "AAC bitrate set to: %d\n", subchannel_index*8000);
- if (aacEncoder_SetParam(handle, AACENC_BITRATE, subchannel_index*8000) != AACENC_OK) {
- fprintf(stderr, "Unable to set the bitrate\n");
- return 1;
- }
- if (aacEncoder_SetParam(handle, AACENC_AFTERBURNER, afterburner) != AACENC_OK) {
- 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;
- }
- if (aacEncInfo(handle, &info) != AACENC_OK) {
- fprintf(stderr, "Unable to get the encoder info\n");
- return 1;
- }
-
- fprintf(stderr, "DAB+ Encoding: framelen=%d\n", info.frameLength);
-
- int input_size = channels*2*info.frameLength;
- input_buf = (uint8_t*) malloc(input_size);
- convert_buf = (int16_t*) malloc(input_size);
-
- /* symsize=8, gfpoly=0x11d, fcr=0, prim=1, nroots=10, pad=135 */
- rs_handler = init_rs_char(8, 0x11d, 0, 1, 10, 135);
- if (rs_handler == NULL) {
- perror("init_rs_char failed");
- return 0;
- }
+ int subchannel_index = 8; //64kbps subchannel
+ int ch=0;
+ const char *infile = NULL;
+ const char *outuri = NULL;
+ FILE *in_fh;
+ void *wav;
+ int wav_format, bits_per_sample, sample_rate=48000, channels=2;
+ uint8_t* input_buf;
+ int16_t* convert_buf;
+ void *rs_handler = NULL;
+ int aot = AOT_DABPLUS_AAC_LC;
+ int afterburner = 0, raw_input=0;
+ HANDLE_AACENCODER handle;
+ 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'},
+ {"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:p:", longopts, &index);
+ switch (ch) {
+ case 'f':
+ if(strcmp(optarg, "raw")==0) {
+ raw_input = 1;
+ } else if(strcmp(optarg, "wav")!=0)
+ usage(argv[0]);
+ break;
+ case 'a':
+ afterburner = 1;
+ break;
+ case 'b':
+ subchannel_index = atoi(optarg) / 8;
+ break;
+ case 'c':
+ channels = atoi(optarg);
+ break;
+ case 'r':
+ sample_rate = atoi(optarg);
+ break;
+ case 'i':
+ infile = optarg;
+ break;
+ case 'o':
+ outuri = optarg;
+ break;
+ case 'p':
+ padlen = atoi(optarg);
+ break;
+ case '?':
+ case 'h':
+ usage(argv[0]);
+ return 1;
+ }
+ }
+
+ if(subchannel_index < 1 || subchannel_index > 24) {
+ 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, "-")) {
+ in_fh = fopen(infile, "rb");
+ if(!in_fh) {
+ fprintf(stderr, "Can't open input file!\n");
+ return 1;
+ }
+ } else {
+ in_fh = stdin;
+ }
+ } else {
+ wav = wav_read_open(infile);
+ if (!wav) {
+ fprintf(stderr, "Unable to open wav file %s\n", infile);
+ return 1;
+ }
+ if (!wav_get_header(wav, &wav_format, &channels, &sample_rate, &bits_per_sample, NULL)) {
+ fprintf(stderr, "Bad wav file %s\n", infile);
+ return 1;
+ }
+ if (wav_format != 1) {
+ fprintf(stderr, "Unsupported WAV format %d\n", wav_format);
+ return 1;
+ }
+ if (bits_per_sample != 16) {
+ fprintf(stderr, "Unsupported WAV sample depth %d\n", bits_per_sample);
+ return 1;
+ }
+ if (channels > 2) {
+ fprintf(stderr, "Unsupported WAV channels %d\n", channels);
+ return 1;
+ }
+ }
+
+ if (outuri) {
+ zmq_sock = zmq_socket(zmq_context, ZMQ_PUB);
+ if (zmq_sock == NULL) {
+ fprintf(stderr, "Error occurred during zmq_socket: %s\n", zmq_strerror(errno));
+ return 2;
+ }
+ if (zmq_connect(zmq_sock, outuri) != 0) {
+ fprintf(stderr, "Error occurred during zmq_connect: %s\n", zmq_strerror(errno));
+ return 2;
+ }
+ } else {
+ fprintf(stderr, "Output URI not defined\n");
+ return 1;
+ }
+
+
+ switch (channels) {
+ case 1: mode = MODE_1; break;
+ case 2: mode = MODE_2; break;
+ default:
+ fprintf(stderr, "Unsupported channels number %d\n", channels);
+ return 1;
+ }
+
+
+ if (aacEncOpen(&handle, 0x01|0x02|0x04, channels) != AACENC_OK) {
+ fprintf(stderr, "Unable to open encoder\n");
+ return 1;
+ }
+
+
+ if(channels == 2 && subchannel_index <= 6)
+ aot = AOT_DABPLUS_PS;
+ else if((channels == 1 && subchannel_index <= 8) || subchannel_index <= 10)
+ aot = AOT_DABPLUS_SBR;
+
+ fprintf(stderr, "Using %d subchannels. AAC type: %s%s%s. channels=%d, sample_rate=%d\n",
+ subchannel_index,
+ aot == AOT_DABPLUS_PS ? "HE-AAC v2" : "",
+ aot == AOT_DABPLUS_SBR ? "HE-AAC" : "",
+ aot == AOT_DABPLUS_AAC_LC ? "AAC-LC" : "",
+ channels, sample_rate);
+
+ if (aacEncoder_SetParam(handle, AACENC_AOT, aot) != AACENC_OK) {
+ fprintf(stderr, "Unable to set the AOT\n");
+ return 1;
+ }
+ if (aacEncoder_SetParam(handle, AACENC_SAMPLERATE, sample_rate) != AACENC_OK) {
+ fprintf(stderr, "Unable to set the samplerate\n");
+ return 1;
+ }
+ if (aacEncoder_SetParam(handle, AACENC_CHANNELMODE, mode) != AACENC_OK) {
+ fprintf(stderr, "Unable to set the channel mode\n");
+ return 1;
+ }
+ if (aacEncoder_SetParam(handle, AACENC_CHANNELORDER, 1) != AACENC_OK) {
+ fprintf(stderr, "Unable to set the wav channel order\n");
+ return 1;
+ }
+ if (aacEncoder_SetParam(handle, AACENC_GRANULE_LENGTH, 960) != AACENC_OK) {
+ fprintf(stderr, "Unable to set the granule length\n");
+ return 1;
+ }
+ if (aacEncoder_SetParam(handle, AACENC_TRANSMUX, TT_DABPLUS) != AACENC_OK) {
+ fprintf(stderr, "Unable to set the RAW transmux\n");
+ return 1;
+ }
+
+ /*if (aacEncoder_SetParam(handle, AACENC_BITRATEMODE, 7 *AACENC_BR_MODE_SFR*) != AACENC_OK) {
+ fprintf(stderr, "Unable to set the bitrate mode\n");
+ return 1;
+ }*/
+
+
+ fprintf(stderr, "AAC bitrate set to: %d\n", subchannel_index*8000);
+ if (aacEncoder_SetParam(handle, AACENC_BITRATE, subchannel_index*8000) != AACENC_OK) {
+ fprintf(stderr, "Unable to set the bitrate\n");
+ return 1;
+ }
+ if (aacEncoder_SetParam(handle, AACENC_AFTERBURNER, afterburner) != AACENC_OK) {
+ 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;
+ }
+ if (aacEncInfo(handle, &info) != AACENC_OK) {
+ fprintf(stderr, "Unable to get the encoder info\n");
+ return 1;
+ }
+
+ fprintf(stderr, "DAB+ Encoding: framelen=%d\n", info.frameLength);
+
+ int input_size = channels*2*info.frameLength;
+ input_buf = (uint8_t*) malloc(input_size);
+ convert_buf = (int16_t*) malloc(input_size);
+
+ /* symsize=8, gfpoly=0x11d, fcr=0, prim=1, nroots=10, pad=135 */
+ rs_handler = init_rs_char(8, 0x11d, 0, 1, 10, 135);
+ if (rs_handler == NULL) {
+ perror("init_rs_char failed");
+ return 0;
+ }
int loops = 0;
int outbuf_size = subchannel_index*120;
- uint8_t outbuf[20480];
-
- if(outbuf_size % 5 != 0) {
- fprintf(stderr, "(outbuf_size mod 5) = %d\n", outbuf_size % 5);
- }
-
- fprintf(stderr, "outbuf_size: %d\n", outbuf_size);
- //outbuf_size += (4 * subchannel_index * (8*8)/8) - outbuf_size/5;
- fprintf(stderr, "outbuf_size: %d\n", outbuf_size);
-
- int frame=0;
- int send_error_count = 0;
- while (1) {
- memset(outbuf, 0x00, outbuf_size);
-
- AACENC_BufDesc in_buf = { 0 }, out_buf = { 0 };
- AACENC_InArgs in_args = { 0 };
- AACENC_OutArgs out_args = { 0 };
- 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 pcmread=0, i, ret;
- int send_error;
- 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");
+ uint8_t outbuf[20480];
+
+ if(outbuf_size % 5 != 0) {
+ fprintf(stderr, "(outbuf_size mod 5) = %d\n", outbuf_size % 5);
+ }
+
+ fprintf(stderr, "outbuf_size: %d\n", outbuf_size);
+ //outbuf_size += (4 * subchannel_index * (8*8)/8) - outbuf_size/5;
+ fprintf(stderr, "outbuf_size: %d\n", outbuf_size);
+
+ int frame=0;
+ int send_error_count = 0;
+ while (1) {
+ memset(outbuf, 0x00, outbuf_size);
+
+ AACENC_BufDesc in_buf = { 0 }, out_buf = { 0 };
+ AACENC_InArgs in_args = { 0 };
+ AACENC_OutArgs out_args = { 0 };
+ 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 pcmread=0, i, ret;
+ int send_error;
+ 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) {
- pcmread = input_size;
- } else {
- fprintf(stderr, "Unable to read from input!\n");
- break;
- }
- } else {
- pcmread = wav_read_data(wav, input_buf, input_size);
- }
-
- for (i = 0; i < pcmread/2; i++) {
- const uint8_t* in = &input_buf[2*i];
- convert_buf[i] = in[0] | (in[1] << 8);
- }
-
- if (pcmread <= 0) {
- in_args.numInSamples = -1;
- } else {
- 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);
- out_elem_size = 1;
- out_buf.numBufs = 1;
- out_buf.bufs = &out_ptr;
- out_buf.bufferIdentifiers = &out_identifier;
- out_buf.bufSizes = &out_size;
- out_buf.bufElSizes = &out_elem_size;
-
- if ((err = aacEncEncode(handle, &in_buf, &out_buf, &in_args, &out_args)) != AACENC_OK) {
- if (err == AACENC_ENCODE_EOF)
- break;
- fprintf(stderr, "Encoding failed\n");
- return 1;
- }
- if (out_args.numOutBytes == 0)
- continue;
+ }
+
+ if(raw_input) {
+ if(fread(input_buf, input_size, 1, in_fh) == 1) {
+ pcmread = input_size;
+ } else {
+ fprintf(stderr, "Unable to read from input!\n");
+ break;
+ }
+ } else {
+ pcmread = wav_read_data(wav, input_buf, input_size);
+ }
+
+ for (i = 0; i < pcmread/2; i++) {
+ const uint8_t* in = &input_buf[2*i];
+ convert_buf[i] = in[0] | (in[1] << 8);
+ }
+
+ if (pcmread <= 0) {
+ in_args.numInSamples = -1;
+ } else {
+ 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);
+ out_elem_size = 1;
+ out_buf.numBufs = 1;
+ out_buf.bufs = &out_ptr;
+ out_buf.bufferIdentifiers = &out_identifier;
+ out_buf.bufSizes = &out_size;
+ out_buf.bufElSizes = &out_elem_size;
+
+ if ((err = aacEncEncode(handle, &in_buf, &out_buf, &in_args, &out_args)) != AACENC_OK) {
+ if (err == AACENC_ENCODE_EOF)
+ break;
+ fprintf(stderr, "Encoding failed\n");
+ return 1;
+ }
+ if (out_args.numOutBytes == 0)
+ continue;
#if 0
- unsigned char au_start[6];
- unsigned char* sfbuf = outbuf;
- au_start[0] = 6;
- au_start[1] = (*(sfbuf + 3) << 4) + ((*(sfbuf + 4)) >> 4);
- au_start[2] = ((*(sfbuf + 4) & 0x0f) << 8) + *(sfbuf + 5);
- fprintf (stderr, "au_start[0] = %d\n", au_start[0]);
- fprintf (stderr, "au_start[1] = %d\n", au_start[1]);
- fprintf (stderr, "au_start[2] = %d\n", au_start[2]);
+ unsigned char au_start[6];
+ unsigned char* sfbuf = outbuf;
+ au_start[0] = 6;
+ au_start[1] = (*(sfbuf + 3) << 4) + ((*(sfbuf + 4)) >> 4);
+ au_start[2] = ((*(sfbuf + 4) & 0x0f) << 8) + *(sfbuf + 5);
+ fprintf (stderr, "au_start[0] = %d\n", au_start[0]);
+ fprintf (stderr, "au_start[1] = %d\n", au_start[1]);
+ fprintf (stderr, "au_start[2] = %d\n", au_start[2]);
#endif
- int row, col;
- unsigned char buf_to_rs_enc[110];
- unsigned char rs_enc[10];
- for(row=0; row < subchannel_index; row++) {
- for(col=0;col < 110; col++) {
- buf_to_rs_enc[col] = outbuf[subchannel_index * col + row];
- }
-
- encode_rs_char(rs_handler, buf_to_rs_enc, rs_enc);
-
- for(col=110; col<120; col++) {
- outbuf[subchannel_index * col + row] = rs_enc[col-110];
- assert(subchannel_index * col + row < outbuf_size);
- }
- }
-
- send_error = zmq_send(zmq_sock, outbuf, outbuf_size, ZMQ_DONTWAIT);
- if (send_error < 0) {
- fprintf(stderr, "ZeroMQ send failed! %s\n", zmq_strerror(errno));
- send_error_count ++;
- }
-
- if (send_error_count > 10)
- {
- fprintf(stderr, "ZeroMQ send failed ten times, aborting!\n");
- break;
- }
- //fwrite(outbuf, 1, /*out_args.numOutBytes*/ outbuf_size, out_fh);
- //fprintf(stderr, "Written %d/%d bytes!\n", out_args.numOutBytes + row*10, outbuf_size);
- if(out_args.numOutBytes + row*10 == outbuf_size)
- fprintf(stderr, ".");
-
-// if(frame > 10)
-// break;
- frame++;
- }
- free(input_buf);
- free(convert_buf);
- if(raw_input) {
- fclose(in_fh);
- } else {
- wav_read_close(wav);
- }
- zmq_close(zmq_sock);
- free_rs_char(rs_handler);
-
- aacEncClose(&handle);
-
- zmq_ctx_term(zmq_context);
- return 0;
+ int row, col;
+ unsigned char buf_to_rs_enc[110];
+ unsigned char rs_enc[10];
+ for(row=0; row < subchannel_index; row++) {
+ for(col=0;col < 110; col++) {
+ buf_to_rs_enc[col] = outbuf[subchannel_index * col + row];
+ }
+
+ encode_rs_char(rs_handler, buf_to_rs_enc, rs_enc);
+
+ for(col=110; col<120; col++) {
+ outbuf[subchannel_index * col + row] = rs_enc[col-110];
+ assert(subchannel_index * col + row < outbuf_size);
+ }
+ }
+
+ send_error = zmq_send(zmq_sock, outbuf, outbuf_size, ZMQ_DONTWAIT);
+ if (send_error < 0) {
+ fprintf(stderr, "ZeroMQ send failed! %s\n", zmq_strerror(errno));
+ send_error_count ++;
+ }
+
+ if (send_error_count > 10)
+ {
+ fprintf(stderr, "ZeroMQ send failed ten times, aborting!\n");
+ break;
+ }
+ //fwrite(outbuf, 1, /*out_args.numOutBytes*/ outbuf_size, out_fh);
+ //fprintf(stderr, "Written %d/%d bytes!\n", out_args.numOutBytes + row*10, outbuf_size);
+ if(out_args.numOutBytes + row*10 == outbuf_size)
+ fprintf(stderr, ".");
+
+// if(frame > 10)
+// break;
+ frame++;
+ }
+ free(input_buf);
+ free(convert_buf);
+ if(raw_input) {
+ fclose(in_fh);
+ } else {
+ wav_read_close(wav);
+ }
+ zmq_close(zmq_sock);
+ free_rs_char(rs_handler);
+
+ aacEncClose(&handle);
+
+ zmq_ctx_term(zmq_context);
+ return 0;
}
diff --git a/aac-enc-dabplus.c b/aac-enc-dabplus.c
index 7516a15..cd86053 100644
--- a/aac-enc-dabplus.c
+++ b/aac-enc-dabplus.c
@@ -5,7 +5,7 @@
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -29,26 +29,26 @@
#include <fec.h>
void usage(const char* name) {
- fprintf(stderr, "%s [OPTION...]\n", name);
- fprintf(stderr,
-" -b, --bitrate={ 8, 16, ..., 192 } Output bitrate in kbps. Must be 8 multiple.\n"
-//" -d, --data=FILENAME Set data filename.\n"
-//" -g, --fs-bug Turn on FS bug mitigation.\n"
-" -i, --input=FILENAME Input filename (default: stdin).\n"
-" -o, --output=FILENAME Output filename (default: stdout).\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"
-" -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"
-//" -t, --type=TYPE Set data type (dls|pad|packet|dg).\n"
-//" -v, --verbose=LEVEL Set verbosity level.\n"
-//" -V, --version Print version and exit.\n"
-//" --mi=[ 0, ... ] Set AAC frame messages interval in milliseconds.\n"
-//" --ma=[ 0, ... ] Set AAC frame messages attack time in milliseconds.\n"
-//" -t, --adts Set ADTS output format (for debugging).\n"
-//" -l, --lp Set frame size to 1024 instead of 960.\n"
+ fprintf(stderr, "%s [OPTION...]\n", name);
+ fprintf(stderr,
+" -b, --bitrate={ 8, 16, ..., 192 } Output bitrate in kbps. Must be 8 multiple.\n"
+//" -d, --data=FILENAME Set data filename.\n"
+//" -g, --fs-bug Turn on FS bug mitigation.\n"
+" -i, --input=FILENAME Input filename (default: stdin).\n"
+" -o, --output=FILENAME Output filename (default: stdout).\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"
+" -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"
+//" -t, --type=TYPE Set data type (dls|pad|packet|dg).\n"
+//" -v, --verbose=LEVEL Set verbosity level.\n"
+//" -V, --version Print version and exit.\n"
+//" --mi=[ 0, ... ] Set AAC frame messages interval in milliseconds.\n"
+//" --ma=[ 0, ... ] Set AAC frame messages attack time in milliseconds.\n"
+//" -t, --adts Set ADTS output format (for debugging).\n"
+//" -l, --lp Set frame size to 1024 instead of 960.\n"
);
@@ -71,8 +71,8 @@ int FindSRIndex(int sr)
{
int i;
for (i = 0; i < 16; i++) {
- if (sr == mpeg4audio_sample_rates[i])
- return i;
+ if (sr == mpeg4audio_sample_rates[i])
+ return i;
}
return 16 - 1;
}
@@ -84,341 +84,341 @@ void adts_hdr_up(char *buff, int size)
/* frame length, 13 bits */
buff[3] &= 0xFC;
- buff[3] |= ((len >> 11) & 0x03); /* 2b: aac_frame_length */
- buff[4] = len >> 3; /* 8b: aac_frame_length */
- buff[5] = (len << 5) & 0xE0; /* 3b: aac_frame_length */
+ buff[3] |= ((len >> 11) & 0x03); /* 2b: aac_frame_length */
+ buff[4] = len >> 3; /* 8b: aac_frame_length */
+ buff[5] = (len << 5) & 0xE0; /* 3b: aac_frame_length */
/* buffer fullness, 11 bits */
- buff[5] |= ((buffer_fullness >> 6) & 0x1F); /* 5b: adts_buffer_fullness */
- buff[6] = (buffer_fullness << 2) & 0xFC; /* 6b: adts_buffer_fullness */
- /* 2b: num_raw_data_blocks */
+ buff[5] |= ((buffer_fullness >> 6) & 0x1F); /* 5b: adts_buffer_fullness */
+ buff[6] = (buffer_fullness << 2) & 0xFC; /* 6b: adts_buffer_fullness */
+ /* 2b: num_raw_data_blocks */
}
int main(int argc, char *argv[]) {
- int subchannel_index = 8; //64kbps subchannel
- int ch=0;
- const char *infile, *outfile;
- FILE *in_fh, *out_fh;
- void *wav;
- int wav_format, bits_per_sample, sample_rate=48000, channels=2;
- uint8_t* input_buf;
- int16_t* convert_buf;
- void *rs_handler = NULL;
- int aot = AOT_DABPLUS_AAC_LC;
- int afterburner = 0, raw_input=0;
- HANDLE_AACENCODER handle;
- CHANNEL_MODE mode;
- AACENC_InfoStruct info = { 0 };
-
- 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'},
- //{"adts", no_argument, 0, 't'},
- {"afterburner", no_argument, 0, 'a'},
- {"help", no_argument, 0, 'h'},
- {0,0,0,0},
- };
-
- int index;
- while(ch != -1) {
- ch = getopt_long(argc, argv, "tlhab:c:i:o:r:f:", longopts, &index);
- switch (ch) {
- case 'f':
- if(strcmp(optarg, "raw")==0) {
- raw_input = 1;
- } else if(strcmp(optarg, "wav")!=0)
- usage(argv[0]);
- break;
- case 'a':
- afterburner = 1;
- break;
- case 'b':
- subchannel_index = atoi(optarg) / 8;
- break;
- case 'c':
- channels = atoi(optarg);
- break;
- case 'r':
- sample_rate = atoi(optarg);
- break;
- case 'i':
- infile = optarg;
- break;
- case 'o':
- outfile = optarg;
- break;
- case '?':
- case 'h':
- usage(argv[0]);
- return 1;
- }
- }
-
- if(subchannel_index < 1 || subchannel_index > 24) {
- fprintf(stderr, "Bad subchannels number: %d, try other bitrate.\n", subchannel_index);
- return 1;
- }
-
- if(raw_input) {
- if(infile && strcmp(infile, "-")) {
- in_fh = fopen(infile, "rb");
- if(!in_fh) {
- fprintf(stderr, "Can't open input file!\n");
- return 1;
- }
- } else {
- in_fh = stdin;
- }
- } else {
- wav = wav_read_open(infile);
- if (!wav) {
- fprintf(stderr, "Unable to open wav file %s\n", infile);
- return 1;
- }
- if (!wav_get_header(wav, &wav_format, &channels, &sample_rate, &bits_per_sample, NULL)) {
- fprintf(stderr, "Bad wav file %s\n", infile);
- return 1;
- }
- if (wav_format != 1) {
- fprintf(stderr, "Unsupported WAV format %d\n", wav_format);
- return 1;
- }
- if (bits_per_sample != 16) {
- fprintf(stderr, "Unsupported WAV sample depth %d\n", bits_per_sample);
- return 1;
- }
- if (channels > 2) {
- fprintf(stderr, "Unsupported WAV channels %d\n", channels);
- return 1;
- }
- }
-
- if(outfile && strcmp(outfile, "-")) {
- out_fh = fopen(outfile, "wb");
- if(!out_fh) {
- fprintf(stderr, "Can't open output file!\n");
- return 1;
- }
- } else {
- out_fh = stdout;
- }
-
-
- switch (channels) {
- case 1: mode = MODE_1; break;
- case 2: mode = MODE_2; break;
- default:
- fprintf(stderr, "Unsupported channels number %d\n", channels);
- return 1;
- }
-
-
- if (aacEncOpen(&handle, 0x01|0x02|0x04, channels) != AACENC_OK) {
- fprintf(stderr, "Unable to open encoder\n");
- return 1;
- }
-
-
- if(channels == 2 && subchannel_index <= 6)
- aot = AOT_DABPLUS_PS;
- else if((channels == 1 && subchannel_index <= 8) || subchannel_index <= 10)
- aot = AOT_DABPLUS_SBR;
-
- fprintf(stderr, "Using %d subchannels. AAC type: %s%s%s. channels=%d, sample_rate=%d\n",
- subchannel_index,
- aot == AOT_DABPLUS_PS ? "HE-AAC v2" : "",
- aot == AOT_DABPLUS_SBR ? "HE-AAC" : "",
- aot == AOT_DABPLUS_AAC_LC ? "AAC-LC" : "",
- channels, sample_rate);
-
- if (aacEncoder_SetParam(handle, AACENC_AOT, aot) != AACENC_OK) {
- fprintf(stderr, "Unable to set the AOT\n");
- return 1;
- }
- if (aacEncoder_SetParam(handle, AACENC_SAMPLERATE, sample_rate) != AACENC_OK) {
- fprintf(stderr, "Unable to set the AOT\n");
- return 1;
- }
- if (aacEncoder_SetParam(handle, AACENC_CHANNELMODE, mode) != AACENC_OK) {
- fprintf(stderr, "Unable to set the channel mode\n");
- return 1;
- }
- if (aacEncoder_SetParam(handle, AACENC_CHANNELORDER, 1) != AACENC_OK) {
- fprintf(stderr, "Unable to set the wav channel order\n");
- return 1;
- }
- if (aacEncoder_SetParam(handle, AACENC_GRANULE_LENGTH, 960) != AACENC_OK) {
- fprintf(stderr, "Unable to set the AOT\n");
- return 1;
- }
- if (aacEncoder_SetParam(handle, AACENC_TRANSMUX, TT_DABPLUS) != AACENC_OK) {
- fprintf(stderr, "Unable to set the RAW transmux\n");
- return 1;
- }
-
- /*if (aacEncoder_SetParam(handle, AACENC_BITRATEMODE, 7 *AACENC_BR_MODE_SFR*) != AACENC_OK) {
- fprintf(stderr, "Unable to set the bitrate mode\n");
- return 1;
- }*/
-
-
- fprintf(stderr, "AAC bitrate set to: %d\n", subchannel_index*8000);
- if (aacEncoder_SetParam(handle, AACENC_BITRATE, subchannel_index*8000) != AACENC_OK) {
- fprintf(stderr, "Unable to set the bitrate\n");
- return 1;
- }
- if (aacEncoder_SetParam(handle, AACENC_AFTERBURNER, afterburner) != AACENC_OK) {
- fprintf(stderr, "Unable to set the afterburner mode\n");
- return 1;
- }
- if (aacEncEncode(handle, NULL, NULL, NULL, NULL) != AACENC_OK) {
- fprintf(stderr, "Unable to initialize the encoder\n");
- return 1;
- }
- if (aacEncInfo(handle, &info) != AACENC_OK) {
- fprintf(stderr, "Unable to get the encoder info\n");
- return 1;
- }
-
- fprintf(stderr, "DAB+ Encoding: framelen=%d\n", info.frameLength);
-
- int input_size = channels*2*info.frameLength;
- input_buf = (uint8_t*) malloc(input_size);
- convert_buf = (int16_t*) malloc(input_size);
-
- /* symsize=8, gfpoly=0x11d, fcr=0, prim=1, nroots=10, pad=135 */
- rs_handler = init_rs_char(8, 0x11d, 0, 1, 10, 135);
- if (rs_handler == NULL) {
- perror("init_rs_char failed");
- return 0;
- }
+ int subchannel_index = 8; //64kbps subchannel
+ int ch=0;
+ const char *infile, *outfile;
+ FILE *in_fh, *out_fh;
+ void *wav;
+ int wav_format, bits_per_sample, sample_rate=48000, channels=2;
+ uint8_t* input_buf;
+ int16_t* convert_buf;
+ void *rs_handler = NULL;
+ int aot = AOT_DABPLUS_AAC_LC;
+ int afterburner = 0, raw_input=0;
+ HANDLE_AACENCODER handle;
+ CHANNEL_MODE mode;
+ AACENC_InfoStruct info = { 0 };
+
+ 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'},
+ //{"adts", no_argument, 0, 't'},
+ {"afterburner", no_argument, 0, 'a'},
+ {"help", no_argument, 0, 'h'},
+ {0,0,0,0},
+ };
+
+ int index;
+ while(ch != -1) {
+ ch = getopt_long(argc, argv, "tlhab:c:i:o:r:f:", longopts, &index);
+ switch (ch) {
+ case 'f':
+ if(strcmp(optarg, "raw")==0) {
+ raw_input = 1;
+ } else if(strcmp(optarg, "wav")!=0)
+ usage(argv[0]);
+ break;
+ case 'a':
+ afterburner = 1;
+ break;
+ case 'b':
+ subchannel_index = atoi(optarg) / 8;
+ break;
+ case 'c':
+ channels = atoi(optarg);
+ break;
+ case 'r':
+ sample_rate = atoi(optarg);
+ break;
+ case 'i':
+ infile = optarg;
+ break;
+ case 'o':
+ outfile = optarg;
+ break;
+ case '?':
+ case 'h':
+ usage(argv[0]);
+ return 1;
+ }
+ }
+
+ if(subchannel_index < 1 || subchannel_index > 24) {
+ fprintf(stderr, "Bad subchannels number: %d, try other bitrate.\n", subchannel_index);
+ return 1;
+ }
+
+ if(raw_input) {
+ if(infile && strcmp(infile, "-")) {
+ in_fh = fopen(infile, "rb");
+ if(!in_fh) {
+ fprintf(stderr, "Can't open input file!\n");
+ return 1;
+ }
+ } else {
+ in_fh = stdin;
+ }
+ } else {
+ wav = wav_read_open(infile);
+ if (!wav) {
+ fprintf(stderr, "Unable to open wav file %s\n", infile);
+ return 1;
+ }
+ if (!wav_get_header(wav, &wav_format, &channels, &sample_rate, &bits_per_sample, NULL)) {
+ fprintf(stderr, "Bad wav file %s\n", infile);
+ return 1;
+ }
+ if (wav_format != 1) {
+ fprintf(stderr, "Unsupported WAV format %d\n", wav_format);
+ return 1;
+ }
+ if (bits_per_sample != 16) {
+ fprintf(stderr, "Unsupported WAV sample depth %d\n", bits_per_sample);
+ return 1;
+ }
+ if (channels > 2) {
+ fprintf(stderr, "Unsupported WAV channels %d\n", channels);
+ return 1;
+ }
+ }
+
+ if(outfile && strcmp(outfile, "-")) {
+ out_fh = fopen(outfile, "wb");
+ if(!out_fh) {
+ fprintf(stderr, "Can't open output file!\n");
+ return 1;
+ }
+ } else {
+ out_fh = stdout;
+ }
+
+
+ switch (channels) {
+ case 1: mode = MODE_1; break;
+ case 2: mode = MODE_2; break;
+ default:
+ fprintf(stderr, "Unsupported channels number %d\n", channels);
+ return 1;
+ }
+
+
+ if (aacEncOpen(&handle, 0x01|0x02|0x04, channels) != AACENC_OK) {
+ fprintf(stderr, "Unable to open encoder\n");
+ return 1;
+ }
+
+
+ if(channels == 2 && subchannel_index <= 6)
+ aot = AOT_DABPLUS_PS;
+ else if((channels == 1 && subchannel_index <= 8) || subchannel_index <= 10)
+ aot = AOT_DABPLUS_SBR;
+
+ fprintf(stderr, "Using %d subchannels. AAC type: %s%s%s. channels=%d, sample_rate=%d\n",
+ subchannel_index,
+ aot == AOT_DABPLUS_PS ? "HE-AAC v2" : "",
+ aot == AOT_DABPLUS_SBR ? "HE-AAC" : "",
+ aot == AOT_DABPLUS_AAC_LC ? "AAC-LC" : "",
+ channels, sample_rate);
+
+ if (aacEncoder_SetParam(handle, AACENC_AOT, aot) != AACENC_OK) {
+ fprintf(stderr, "Unable to set the AOT\n");
+ return 1;
+ }
+ if (aacEncoder_SetParam(handle, AACENC_SAMPLERATE, sample_rate) != AACENC_OK) {
+ fprintf(stderr, "Unable to set the AOT\n");
+ return 1;
+ }
+ if (aacEncoder_SetParam(handle, AACENC_CHANNELMODE, mode) != AACENC_OK) {
+ fprintf(stderr, "Unable to set the channel mode\n");
+ return 1;
+ }
+ if (aacEncoder_SetParam(handle, AACENC_CHANNELORDER, 1) != AACENC_OK) {
+ fprintf(stderr, "Unable to set the wav channel order\n");
+ return 1;
+ }
+ if (aacEncoder_SetParam(handle, AACENC_GRANULE_LENGTH, 960) != AACENC_OK) {
+ fprintf(stderr, "Unable to set the AOT\n");
+ return 1;
+ }
+ if (aacEncoder_SetParam(handle, AACENC_TRANSMUX, TT_DABPLUS) != AACENC_OK) {
+ fprintf(stderr, "Unable to set the RAW transmux\n");
+ return 1;
+ }
+
+ /*if (aacEncoder_SetParam(handle, AACENC_BITRATEMODE, 7 *AACENC_BR_MODE_SFR*) != AACENC_OK) {
+ fprintf(stderr, "Unable to set the bitrate mode\n");
+ return 1;
+ }*/
+
+
+ fprintf(stderr, "AAC bitrate set to: %d\n", subchannel_index*8000);
+ if (aacEncoder_SetParam(handle, AACENC_BITRATE, subchannel_index*8000) != AACENC_OK) {
+ fprintf(stderr, "Unable to set the bitrate\n");
+ return 1;
+ }
+ if (aacEncoder_SetParam(handle, AACENC_AFTERBURNER, afterburner) != AACENC_OK) {
+ fprintf(stderr, "Unable to set the afterburner mode\n");
+ return 1;
+ }
+ if (aacEncEncode(handle, NULL, NULL, NULL, NULL) != AACENC_OK) {
+ fprintf(stderr, "Unable to initialize the encoder\n");
+ return 1;
+ }
+ if (aacEncInfo(handle, &info) != AACENC_OK) {
+ fprintf(stderr, "Unable to get the encoder info\n");
+ return 1;
+ }
+
+ fprintf(stderr, "DAB+ Encoding: framelen=%d\n", info.frameLength);
+
+ int input_size = channels*2*info.frameLength;
+ input_buf = (uint8_t*) malloc(input_size);
+ convert_buf = (int16_t*) malloc(input_size);
+
+ /* symsize=8, gfpoly=0x11d, fcr=0, prim=1, nroots=10, pad=135 */
+ rs_handler = init_rs_char(8, 0x11d, 0, 1, 10, 135);
+ if (rs_handler == NULL) {
+ perror("init_rs_char failed");
+ return 0;
+ }
int loops = 0;
int outbuf_size = subchannel_index*120;
- uint8_t outbuf[20480];
-
- if(outbuf_size % 5 != 0) {
- fprintf(stderr, "(outbuf_size mod 5) = %d\n", outbuf_size % 5);
- }
-
- fprintf(stderr, "outbuf_size: %d\n", outbuf_size);
- //outbuf_size += (4 * subchannel_index * (8*8)/8) - outbuf_size/5;
- fprintf(stderr, "outbuf_size: %d\n", outbuf_size);
-
- int frame=0;
- while (1) {
- memset(outbuf, 0x00, outbuf_size);
-
- 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 out_identifier = OUT_BITSTREAM_DATA;
- int out_size, out_elem_size;
- int read=0, i;
- void *in_ptr, *out_ptr;
- AACENC_ERROR err;
-
- if(raw_input) {
- if(fread(input_buf, input_size, 1, in_fh) == 1) {
- read = input_size;
- } else {
- fprintf(stderr, "Unable to read from input!\n");
- break;
- }
- } else {
- read = wav_read_data(wav, input_buf, input_size);
- }
-
- for (i = 0; i < read/2; i++) {
- const uint8_t* in = &input_buf[2*i];
- convert_buf[i] = in[0] | (in[1] << 8);
- }
-
- if (read <= 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;
- }
- out_ptr = outbuf;
- out_size = sizeof(outbuf);
- out_elem_size = 1;
- out_buf.numBufs = 1;
- out_buf.bufs = &out_ptr;
- out_buf.bufferIdentifiers = &out_identifier;
- out_buf.bufSizes = &out_size;
- out_buf.bufElSizes = &out_elem_size;
-
- if ((err = aacEncEncode(handle, &in_buf, &out_buf, &in_args, &out_args)) != AACENC_OK) {
- if (err == AACENC_ENCODE_EOF)
- break;
- fprintf(stderr, "Encoding failed\n");
- return 1;
- }
- if (out_args.numOutBytes == 0)
- continue;
+ uint8_t outbuf[20480];
+
+ if(outbuf_size % 5 != 0) {
+ fprintf(stderr, "(outbuf_size mod 5) = %d\n", outbuf_size % 5);
+ }
+
+ fprintf(stderr, "outbuf_size: %d\n", outbuf_size);
+ //outbuf_size += (4 * subchannel_index * (8*8)/8) - outbuf_size/5;
+ fprintf(stderr, "outbuf_size: %d\n", outbuf_size);
+
+ int frame=0;
+ while (1) {
+ memset(outbuf, 0x00, outbuf_size);
+
+ 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 out_identifier = OUT_BITSTREAM_DATA;
+ int out_size, out_elem_size;
+ int read=0, i;
+ void *in_ptr, *out_ptr;
+ AACENC_ERROR err;
+
+ if(raw_input) {
+ if(fread(input_buf, input_size, 1, in_fh) == 1) {
+ read = input_size;
+ } else {
+ fprintf(stderr, "Unable to read from input!\n");
+ break;
+ }
+ } else {
+ read = wav_read_data(wav, input_buf, input_size);
+ }
+
+ for (i = 0; i < read/2; i++) {
+ const uint8_t* in = &input_buf[2*i];
+ convert_buf[i] = in[0] | (in[1] << 8);
+ }
+
+ if (read <= 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;
+ }
+ out_ptr = outbuf;
+ out_size = sizeof(outbuf);
+ out_elem_size = 1;
+ out_buf.numBufs = 1;
+ out_buf.bufs = &out_ptr;
+ out_buf.bufferIdentifiers = &out_identifier;
+ out_buf.bufSizes = &out_size;
+ out_buf.bufElSizes = &out_elem_size;
+
+ if ((err = aacEncEncode(handle, &in_buf, &out_buf, &in_args, &out_args)) != AACENC_OK) {
+ if (err == AACENC_ENCODE_EOF)
+ break;
+ fprintf(stderr, "Encoding failed\n");
+ return 1;
+ }
+ if (out_args.numOutBytes == 0)
+ continue;
#if 0
- unsigned char au_start[6];
- unsigned char* sfbuf = outbuf;
- au_start[0] = 6;
- au_start[1] = (*(sfbuf + 3) << 4) + ((*(sfbuf + 4)) >> 4);
- au_start[2] = ((*(sfbuf + 4) & 0x0f) << 8) + *(sfbuf + 5);
- fprintf (stderr, "au_start[0] = %d\n", au_start[0]);
- fprintf (stderr, "au_start[1] = %d\n", au_start[1]);
- fprintf (stderr, "au_start[2] = %d\n", au_start[2]);
+ unsigned char au_start[6];
+ unsigned char* sfbuf = outbuf;
+ au_start[0] = 6;
+ au_start[1] = (*(sfbuf + 3) << 4) + ((*(sfbuf + 4)) >> 4);
+ au_start[2] = ((*(sfbuf + 4) & 0x0f) << 8) + *(sfbuf + 5);
+ fprintf (stderr, "au_start[0] = %d\n", au_start[0]);
+ fprintf (stderr, "au_start[1] = %d\n", au_start[1]);
+ fprintf (stderr, "au_start[2] = %d\n", au_start[2]);
#endif
- int row, col;
- char buf_to_rs_enc[110];
- char rs_enc[10];
- for(row=0; row < subchannel_index; row++) {
- for(col=0;col < 110; col++) {
- buf_to_rs_enc[col] = outbuf[subchannel_index * col + row];
- }
-
- encode_rs_char(rs_handler, buf_to_rs_enc, rs_enc);
-
- for(col=110; col<120; col++) {
- outbuf[subchannel_index * col + row] = rs_enc[col-110];
- assert(subchannel_index * col + row < outbuf_size);
- }
- }
-
- fwrite(outbuf, 1, /*out_args.numOutBytes*/ outbuf_size, out_fh);
- //fprintf(stderr, "Written %d/%d bytes!\n", out_args.numOutBytes + row*10, outbuf_size);
- if(out_args.numOutBytes + row*10 == outbuf_size)
- fprintf(stderr, ".");
-
-// if(frame > 10)
-// break;
- frame++;
- }
- free(input_buf);
- free(convert_buf);
- if(raw_input) {
- fclose(in_fh);
- } else {
- wav_read_close(wav);
- }
- fclose(out_fh);
- free_rs_char(rs_handler);
-
- aacEncClose(&handle);
-
- return 0;
+ int row, col;
+ char buf_to_rs_enc[110];
+ char rs_enc[10];
+ for(row=0; row < subchannel_index; row++) {
+ for(col=0;col < 110; col++) {
+ buf_to_rs_enc[col] = outbuf[subchannel_index * col + row];
+ }
+
+ encode_rs_char(rs_handler, buf_to_rs_enc, rs_enc);
+
+ for(col=110; col<120; col++) {
+ outbuf[subchannel_index * col + row] = rs_enc[col-110];
+ assert(subchannel_index * col + row < outbuf_size);
+ }
+ }
+
+ fwrite(outbuf, 1, /*out_args.numOutBytes*/ outbuf_size, out_fh);
+ //fprintf(stderr, "Written %d/%d bytes!\n", out_args.numOutBytes + row*10, outbuf_size);
+ if(out_args.numOutBytes + row*10 == outbuf_size)
+ fprintf(stderr, ".");
+
+// if(frame > 10)
+// break;
+ frame++;
+ }
+ free(input_buf);
+ free(convert_buf);
+ if(raw_input) {
+ fclose(in_fh);
+ } else {
+ wav_read_close(wav);
+ }
+ fclose(out_fh);
+ free_rs_char(rs_handler);
+
+ aacEncClose(&handle);
+
+ return 0;
}
diff --git a/libAACenc/src/bitenc.cpp b/libAACenc/src/bitenc.cpp
index cd68398..6f2c151 100644
--- a/libAACenc/src/bitenc.cpp
+++ b/libAACenc/src/bitenc.cpp
@@ -749,7 +749,7 @@ static INT FDKaacEnc_writeDataStreamElement( HANDLE_TRANSPORTENC hTpEnc,
#define MAX_DSE_DATA_BYTES ( 510 )
INT dseBitsUsed = 0;
- fprintf(stderr, "FDKaacEnc_writeDataStreamElement() dataPayloadBytes=%d\n", dataPayloadBytes);
+ //fprintf(stderr, "FDKaacEnc_writeDataStreamElement() dataPayloadBytes=%d\n", dataPayloadBytes);
while (dataPayloadBytes > 0)
{
int esc_count = -1;
diff --git a/mot-encoder.c b/mot-encoder.c
index d33514d..a60a39a 100644
--- a/mot-encoder.c
+++ b/mot-encoder.c
@@ -43,40 +43,40 @@ 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
+ // 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
+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);
@@ -89,550 +89,550 @@ 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://rd.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);
+ fprintf(stderr, "DAB MOT encoder\n"
+ "for slideshow and DLS\n\n"
+ "By CSP Innovazione nelle ICT s.c.a r.l. (http://rd.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 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;
+ 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;
+ 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;
+ 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;
+ 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;
+ 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;
+ 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<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]);
- }
+ 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
+ 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;
+ *bsize=9+msc->seglen+1+1;
- //write(1,b,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");
-
- }
+ 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);
- }
+ 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++;
- }
-
- }
+ 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++;
+ }
+
+ }
}
diff --git a/wavreader.c b/wavreader.c
index f672001..c62a885 100644
--- a/wavreader.c
+++ b/wavreader.c
@@ -25,139 +25,139 @@
#define TAG(a, b, c, d) (((a) << 24) | ((b) << 16) | ((c) << 8) | (d))
struct wav_reader {
- FILE *wav;
- uint32_t data_length;
+ FILE *wav;
+ uint32_t data_length;
- int format;
- int sample_rate;
- int bits_per_sample;
- int channels;
- int byte_rate;
- int block_align;
+ int format;
+ int sample_rate;
+ int bits_per_sample;
+ int channels;
+ int byte_rate;
+ int block_align;
};
static uint32_t read_tag(struct wav_reader* wr) {
- uint32_t tag = 0;
- tag = (tag << 8) | fgetc(wr->wav);
- tag = (tag << 8) | fgetc(wr->wav);
- tag = (tag << 8) | fgetc(wr->wav);
- tag = (tag << 8) | fgetc(wr->wav);
- return tag;
+ uint32_t tag = 0;
+ tag = (tag << 8) | fgetc(wr->wav);
+ tag = (tag << 8) | fgetc(wr->wav);
+ tag = (tag << 8) | fgetc(wr->wav);
+ tag = (tag << 8) | fgetc(wr->wav);
+ return tag;
}
static uint32_t read_int32(struct wav_reader* wr) {
- uint32_t value = 0;
- value |= fgetc(wr->wav) << 0;
- value |= fgetc(wr->wav) << 8;
- value |= fgetc(wr->wav) << 16;
- value |= fgetc(wr->wav) << 24;
- return value;
+ uint32_t value = 0;
+ value |= fgetc(wr->wav) << 0;
+ value |= fgetc(wr->wav) << 8;
+ value |= fgetc(wr->wav) << 16;
+ value |= fgetc(wr->wav) << 24;
+ return value;
}
static uint16_t read_int16(struct wav_reader* wr) {
- uint16_t value = 0;
- value |= fgetc(wr->wav) << 0;
- value |= fgetc(wr->wav) << 8;
- return value;
+ uint16_t value = 0;
+ value |= fgetc(wr->wav) << 0;
+ value |= fgetc(wr->wav) << 8;
+ return value;
}
void* wav_read_open(const char *filename) {
- struct wav_reader* wr = (struct wav_reader*) malloc(sizeof(*wr));
- long data_pos = 0;
- memset(wr, 0, sizeof(*wr));
+ struct wav_reader* wr = (struct wav_reader*) malloc(sizeof(*wr));
+ long data_pos = 0;
+ memset(wr, 0, sizeof(*wr));
- wr->wav = fopen(filename, "rb");
- if (wr->wav == NULL) {
- free(wr);
- return NULL;
- }
+ wr->wav = fopen(filename, "rb");
+ if (wr->wav == NULL) {
+ free(wr);
+ return NULL;
+ }
- while (1) {
- uint32_t tag, tag2, length;
- tag = read_tag(wr);
- if (feof(wr->wav))
- break;
- length = read_int32(wr);
- if (tag != TAG('R', 'I', 'F', 'F') || length < 4) {
- fseek(wr->wav, length, SEEK_CUR);
- continue;
- }
- tag2 = read_tag(wr);
- length -= 4;
- if (tag2 != TAG('W', 'A', 'V', 'E')) {
- fseek(wr->wav, length, SEEK_CUR);
- continue;
- }
- // RIFF chunk found, iterate through it
- while (length >= 8) {
- uint32_t subtag, sublength;
- subtag = read_tag(wr);
- if (feof(wr->wav))
- break;
- sublength = read_int32(wr);
- length -= 8;
- if (length < sublength)
- break;
- if (subtag == TAG('f', 'm', 't', ' ')) {
- if (sublength < 16) {
- // Insufficient data for 'fmt '
- break;
- }
- wr->format = read_int16(wr);
- wr->channels = read_int16(wr);
- wr->sample_rate = read_int32(wr);
- wr->byte_rate = read_int32(wr);
- wr->block_align = read_int16(wr);
- wr->bits_per_sample = read_int16(wr);
- fseek(wr->wav, sublength - 16, SEEK_CUR);
- } else if (subtag == TAG('d', 'a', 't', 'a')) {
- data_pos = ftell(wr->wav);
- wr->data_length = sublength;
- fseek(wr->wav, sublength, SEEK_CUR);
- } else {
- fseek(wr->wav, sublength, SEEK_CUR);
- }
- length -= sublength;
- }
- if (length > 0) {
- // Bad chunk?
- fseek(wr->wav, length, SEEK_CUR);
- }
- }
- fseek(wr->wav, data_pos, SEEK_SET);
- return wr;
+ while (1) {
+ uint32_t tag, tag2, length;
+ tag = read_tag(wr);
+ if (feof(wr->wav))
+ break;
+ length = read_int32(wr);
+ if (tag != TAG('R', 'I', 'F', 'F') || length < 4) {
+ fseek(wr->wav, length, SEEK_CUR);
+ continue;
+ }
+ tag2 = read_tag(wr);
+ length -= 4;
+ if (tag2 != TAG('W', 'A', 'V', 'E')) {
+ fseek(wr->wav, length, SEEK_CUR);
+ continue;
+ }
+ // RIFF chunk found, iterate through it
+ while (length >= 8) {
+ uint32_t subtag, sublength;
+ subtag = read_tag(wr);
+ if (feof(wr->wav))
+ break;
+ sublength = read_int32(wr);
+ length -= 8;
+ if (length < sublength)
+ break;
+ if (subtag == TAG('f', 'm', 't', ' ')) {
+ if (sublength < 16) {
+ // Insufficient data for 'fmt '
+ break;
+ }
+ wr->format = read_int16(wr);
+ wr->channels = read_int16(wr);
+ wr->sample_rate = read_int32(wr);
+ wr->byte_rate = read_int32(wr);
+ wr->block_align = read_int16(wr);
+ wr->bits_per_sample = read_int16(wr);
+ fseek(wr->wav, sublength - 16, SEEK_CUR);
+ } else if (subtag == TAG('d', 'a', 't', 'a')) {
+ data_pos = ftell(wr->wav);
+ wr->data_length = sublength;
+ fseek(wr->wav, sublength, SEEK_CUR);
+ } else {
+ fseek(wr->wav, sublength, SEEK_CUR);
+ }
+ length -= sublength;
+ }
+ if (length > 0) {
+ // Bad chunk?
+ fseek(wr->wav, length, SEEK_CUR);
+ }
+ }
+ fseek(wr->wav, data_pos, SEEK_SET);
+ return wr;
}
void wav_read_close(void* obj) {
- struct wav_reader* wr = (struct wav_reader*) obj;
- fclose(wr->wav);
- free(wr);
+ struct wav_reader* wr = (struct wav_reader*) obj;
+ fclose(wr->wav);
+ free(wr);
}
int wav_get_header(void* obj, int* format, int* channels, int* sample_rate, int* bits_per_sample, unsigned int* data_length) {
- struct wav_reader* wr = (struct wav_reader*) obj;
- if (format)
- *format = wr->format;
- if (channels)
- *channels = wr->channels;
- if (sample_rate)
- *sample_rate = wr->sample_rate;
- if (bits_per_sample)
- *bits_per_sample = wr->bits_per_sample;
- if (data_length)
- *data_length = wr->data_length;
- return wr->format && wr->sample_rate;
+ struct wav_reader* wr = (struct wav_reader*) obj;
+ if (format)
+ *format = wr->format;
+ if (channels)
+ *channels = wr->channels;
+ if (sample_rate)
+ *sample_rate = wr->sample_rate;
+ if (bits_per_sample)
+ *bits_per_sample = wr->bits_per_sample;
+ if (data_length)
+ *data_length = wr->data_length;
+ return wr->format && wr->sample_rate;
}
int wav_read_data(void* obj, unsigned char* data, unsigned int length) {
- struct wav_reader* wr = (struct wav_reader*) obj;
- int n;
- if (wr->wav == NULL)
- return -1;
- if (length > wr->data_length)
- length = wr->data_length;
- n = fread(data, 1, length, wr->wav);
- wr->data_length -= length;
- return n;
+ struct wav_reader* wr = (struct wav_reader*) obj;
+ int n;
+ if (wr->wav == NULL)
+ return -1;
+ if (length > wr->data_length)
+ length = wr->data_length;
+ n = fread(data, 1, length, wr->wav);
+ wr->data_length -= length;
+ return n;
}