From 22f1fce330059ef8a383cf327a023d6a9da5ad3e Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Mon, 15 Feb 2016 02:44:20 +0100 Subject: Include toolame-dab as library --- libtoolame-dab/xpad.c | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 libtoolame-dab/xpad.c (limited to 'libtoolame-dab/xpad.c') diff --git a/libtoolame-dab/xpad.c b/libtoolame-dab/xpad.c new file mode 100644 index 0000000..6c97ea8 --- /dev/null +++ b/libtoolame-dab/xpad.c @@ -0,0 +1,88 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "xpad.h" + +static int xpad_fd = 0; + +/* The F-PAD has to be: + uint16_t fpad = 0x2; // CI flag + + if (xpad_len()) { + fpad |= 1<<13; // variable length X-PAD + } + + which is included by mot-encoder in the file/fifo + it generates + */ + +/* Create and open the desired PAD input fifo + */ +int xpad_init(char* pad_fifo, int pad_len) +{ + if (mkfifo(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; + } + } + + xpad_fd = open(pad_fifo, O_RDONLY | O_NONBLOCK); + if (xpad_fd == -1) { + fprintf(stderr, "Can't open pad file!\n"); + return -1; + } + + int flags = fcntl(xpad_fd, F_GETFL, 0); + if (fcntl(xpad_fd, F_SETFL, flags | O_NONBLOCK)) { + fprintf(stderr, "Can't set non-blocking mode in pad file!\n"); + return -1; + } + + return 0; +} + +int xpad_read_len(uint8_t* buf, int len) +{ + if (xpad_fd == 0) return 0; + + ssize_t num_read = 0; + + while (num_read < len) { + ssize_t r = read(xpad_fd, buf + num_read, len - num_read); + + if(r < 0) { + if (errno == EAGAIN) { + return 0; + } + else { + perror("PAD input read error"); + return -1; + } + } + else if (r == 0) { + // reached end of data + return 0; + } + + num_read += r; + } + +#if XPAD_DEBUG + int i; + for (i = 0; i < len; i++) { + fprintf(stderr, "%02x ", buf[i]); + } + fprintf(stderr, "\n"); +#endif + + return num_read; +} + -- cgit v1.2.3