net.c: Combine remote_name() and __get_sock_name().
[paraslash.git] / aac_common.c
1 /*
2  * Copyright (C) 2006 Andre Noll <maan@tuebingen.mpg.de>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6 /*
7  * based in parts on libfaad, Copyright (C) 2003-2005 M. Bakker,
8  * Ahead Software AG
9  */
10
11 /** \file aac_common.c Common functions of aac_afh and aadcec. */
12
13 #include "para.h"
14 #include "aac.h"
15 #include "error.h"
16
17 /**
18  * Get a new libfaad decoder handle.
19  *
20  * \return The handle returned by NeAACDecOpen().
21  */
22 NeAACDecHandle aac_open(void)
23 {
24         NeAACDecHandle h = NeAACDecOpen();
25         NeAACDecConfigurationPtr c = NeAACDecGetCurrentConfiguration(h);
26
27         c->defObjectType = LC;
28         c->outputFormat = FAAD_FMT_16BIT;
29         c->downMatrix = 0;
30         NeAACDecSetConfiguration(h, c);
31         return h;
32 }
33
34 static unsigned long aac_read_decoder_length(unsigned char *buf, int *description_len)
35 {
36         uint8_t b;
37         uint8_t numBytes = 0;
38         unsigned long length = 0;
39
40         do {
41                 b = buf[numBytes];
42                 numBytes++;
43                 length = (length << 7) | (b & 0x7F);
44         } while
45                 ((b & 0x80) && numBytes < 4);
46         *description_len = numBytes;
47         return length;
48 }
49
50 /**
51  * search for the position and the length of the decoder configuration
52  *
53  * \param buf buffer to seach
54  * \param buflen length of \a buf
55  * \param skip Upon succesful return, this contains the offset in \a buf where
56  * the decoder config starts.
57  * \param decoder_length result pointer that is filled in with the length of
58  * the decoder configuration on success.
59  *
60  * \return positive on success, negative on errors
61  */
62 int aac_find_esds(unsigned char *buf, size_t buflen, size_t *skip,
63                 unsigned long *decoder_length)
64 {
65         size_t i;
66
67         for (i = 0; i + 4 < buflen; i++) {
68                 unsigned char *p = buf + i;
69                 int description_len;
70
71                 if (p[0] != 'e' || p[1] != 's' || p[2] != 'd' || p[3] != 's')
72                         continue;
73                 i += 8;
74                 p = buf + i;
75                 PARA_INFO_LOG("found esds@%zu, next: %x\n", i, *p);
76                 if (*p == 3)
77                         i += 8;
78                 else
79                         i += 6;
80                 p = buf + i;
81                 PARA_INFO_LOG("next: %x\n", *p);
82                 if (*p != 4)
83                         continue;
84                 i += 18;
85                 p = buf + i;
86                 PARA_INFO_LOG("next: %x\n", *p);
87                 if (*p != 5)
88                         continue;
89                 i++;
90                 p = buf + i;
91                 *decoder_length = aac_read_decoder_length(p, &description_len);
92                 PARA_INFO_LOG("decoder length: %lu\n", *decoder_length);
93                 i += description_len;
94                 *skip = i;
95                 return 1;
96         }
97         return -E_ESDS;
98 }
99
100 /**
101  * search for the first entry in the stco table
102  *
103  * \param buf buffer to seach
104  * \param buflen length of \a buf
105  * \param skip Upon succesful return, this contains the number
106  * of bytes to skip from the input buffer.
107  *
108  * \return the position of the first entry in the table on success,
109  * -E_STCO on errors.
110  */
111 ssize_t aac_find_entry_point(unsigned char *buf, size_t buflen, size_t *skip)
112 {
113         ssize_t ret;
114         size_t i;
115
116         for (i = 0; i + 20 < buflen; i++) {
117                 unsigned char *p = buf + i;
118
119                 if (p[0] != 's' || p[1] != 't' || p[2] != 'c' || p[3] != 'o')
120                         continue;
121                 PARA_INFO_LOG("found stco@%zu\n", i);
122                 i += 12;
123                 ret = aac_read_int32(buf + i); /* first offset */
124                 i += 4;
125                 PARA_INFO_LOG("entry point: %zd\n", ret);
126                 *skip = i;
127                 return ret;
128         }
129         PARA_WARNING_LOG("stco not found, buflen: %zu\n", buflen);
130         return -E_STCO;
131 }