From d58099780dcd5c5260e9e5609f1ee0b1da247546 Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Tue, 11 Feb 2014 15:07:22 +0100 Subject: add zmq code --- Makefile | 10 ++++----- bitstream.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++------- common.h | 2 ++ toolame.c | 2 ++ 4 files changed, 69 insertions(+), 12 deletions(-) diff --git a/Makefile b/Makefile index 6b99f89..b8f7212 100644 --- a/Makefile +++ b/Makefile @@ -28,16 +28,16 @@ OBJ = $(c_sources:.c=.o) #Uncomment this if you want to do some profiling/debugging #PG = -g -pg -PG = -fomit-frame-pointer +PG = -g -fomit-frame-pointer -# Optimize flag. 3 is about as high as you can sanely go with GCC3.2. -OPTIM = -O3 +# Optimize flag. +OPTIM = -O2 # These flags are pretty much mandatory REQUIRED = -DNDEBUG -DINLINE=inline #pick your architecture -ARCH = -march=pentium +ARCH = -march=native #Possible x86 architectures #gcc3.2 => i386, i486, i586, i686, pentium, pentium-mmx # pentiumpro, pentium2, pentium3, pentium4, k6, k6-2, k6-3, @@ -60,7 +60,7 @@ CC_SWITCHES = $(OPTIM) $(REQUIRED) $(ARCH) $(PG) $(TWEAKS) $(WARNINGS) $(NEW_02L PGM = toolame -LIBS = -lm +LIBS = -lm -lzmq #nick burch's OS/2 fix gagravarr@SoftHome.net UNAME = $(shell uname) diff --git a/bitstream.c b/bitstream.c index fae619f..02f1515 100644 --- a/bitstream.c +++ b/bitstream.c @@ -1,5 +1,7 @@ #include #include +#include +#include #include "common.h" #include "mem.h" #include "bitstream.h" @@ -104,23 +106,74 @@ void empty_buffer (Bit_stream_struc * bs, int minimum) { register int i; - for (i = bs->buf_size - 1; i >= minimum; i--) - fwrite (&bs->buf[i], sizeof (unsigned char), 1, bs->pt); + if (bs->pt) { + for (i = bs->buf_size - 1; i >= minimum; i--) + fwrite (&bs->buf[i], sizeof (unsigned char), 1, bs->pt); - fflush (bs->pt); /* NEW SS to assist in debugging */ + fflush (bs->pt); /* NEW SS to assist in debugging */ - for (i = minimum - 1; i >= 0; i--) - bs->buf[bs->buf_size - minimum + i] = bs->buf[i]; + for (i = minimum - 1; i >= 0; i--) + bs->buf[bs->buf_size - minimum + i] = bs->buf[i]; - bs->buf_byte_idx = bs->buf_size - 1 - minimum; - bs->buf_bit_idx = 8; + bs->buf_byte_idx = bs->buf_size - 1 - minimum; + bs->buf_bit_idx = 8; + } + + if (bs->zmq_sock) { + unsigned char outbuf[bs->zmq_framesize]; + + int j = 0; + for (i = bs->buf_size - 1; i >= minimum; i--) { + outbuf[j++] = bs->buf[i]; + if (j >= bs->zmq_framesize) + break; + } + + if (j < bs->zmq_framesize) { + fprintf(stderr, "not enough data in buffer ! j=%d, req'd %d", + j, bs->zmq_framesize); + } + + int send_error = zmq_send(bs->zmq_sock, outbuf, bs->zmq_framesize, + ZMQ_DONTWAIT); + + if (send_error < 0) { + fprintf(stderr, "ZeroMQ send failed! %s\n", zmq_strerror(errno)); + } + + for (i = minimum - 1; i >= 0; i--) + bs->buf[bs->buf_size - minimum + i] = bs->buf[i]; + + bs->buf_byte_idx = bs->buf_size - 1 - minimum; + bs->buf_bit_idx = 8; + } } +static void *zmq_context; + /* open the device to write the bit stream into it */ void open_bit_stream_w (Bit_stream_struc * bs, char *bs_filenam, int size) { + bs->zmq_sock = NULL; + if (bs_filenam[0] == '-') bs->pt = stdout; + else if (strncmp(bs_filenam, "tcp://", 4) == 0) { + zmq_context = zmq_ctx_new(); + bs->zmq_sock = zmq_socket(zmq_context, ZMQ_PUB); + if (bs->zmq_sock == NULL) { + fprintf(stderr, "Error occurred during zmq_socket: %s\n", + zmq_strerror(errno)); + abort(); + } + if (zmq_connect(bs->zmq_sock, bs_filenam) != 0) { + fprintf(stderr, "Error occurred during zmq_connect: %s\n", + zmq_strerror(errno)); + abort(); + } + + bs->pt = NULL; + } else if ((bs->pt = fopen (bs_filenam, "wb")) == NULL) { fprintf (stderr, "Could not create \"%s\".\n", bs_filenam); exit (1); diff --git a/common.h b/common.h index bc43324..c4fd303 100644 --- a/common.h +++ b/common.h @@ -138,6 +138,8 @@ frame_info; typedef struct bit_stream_struc { FILE *pt; /* pointer to bit stream device */ + void *zmq_sock; /* zmq socket */ + int zmq_framesize; /* zmq frame size */ unsigned char *buf; /* bit stream buffer */ int buf_size; /* size of buffer (in number of bytes) */ long totbit; /* bit counter of bit stream */ diff --git a/toolame.c b/toolame.c index b1c7f20..b8fb776 100644 --- a/toolame.c +++ b/toolame.c @@ -905,6 +905,8 @@ void parse_args (int argc, char **argv, frame_info * frame, int *psy, if ((header->bitrate_index = BitrateIndex (brate, header->version)) < 0) err = 1; + bs.zmq_framesize = 3 * brate; + /* All options are hunky dory, open the input audio file and return to the main drag */ open_bit_stream_w (&bs, outPath, BUFFER_SIZE); -- cgit v1.2.3