summaryrefslogtreecommitdiffstats
path: root/libtoolame-dab/xpad.c
blob: 6c97ea840278ab64f186ad8c553eafa26efcfc6d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <assert.h>
#include <errno.h>

#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;
}