osl: Introduce volatile columns that need not be freed.
[paraslash.git] / aac_afh.c
1 /*
2  * Copyright (C) 2006-2007 Andre Noll <maan@systemlinux.org>
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_afh.c para_server's aac audio format handler */
12
13 #include "para.h"
14 #include "error.h"
15 #include "string.h"
16 #include "afh.h"
17 #include "afs.h"
18 #include "server.h"
19 #include "aac.h"
20
21 static int aac_find_stsz(unsigned char *buf, size_t buflen, off_t *skip)
22 {
23         int i;
24
25         for (i = 0; i + 16 < buflen; i++) {
26                 unsigned char *p = buf + i;
27                 unsigned sample_count, sample_size;
28
29                 if (p[0] != 's' || p[1] != 't' || p[2] != 's' || p[3] != 'z')
30                         continue;
31                 PARA_DEBUG_LOG("found stsz@%d\n", i);
32                 i += 8;
33                 sample_size = aac_read_int32(buf + i);
34                 PARA_DEBUG_LOG("sample size: %d\n", sample_size);
35                 i += 4;
36                 sample_count = aac_read_int32(buf + i);
37                 i += 4;
38                 PARA_DEBUG_LOG("sample count: %d\n", sample_count);
39                 *skip = i;
40                 return sample_count;
41         }
42         return -E_STSZ;
43 }
44
45 static ssize_t aac_compute_chunk_table(struct afh_info *afhi,
46                 unsigned char *map, size_t numbytes)
47 {
48         int ret, i;
49         size_t sum = 0;
50         off_t skip;
51
52         ret = aac_find_stsz(map, numbytes, &skip);
53         if (ret < 0)
54                 return ret;
55         afhi->chunks_total = ret;
56         PARA_DEBUG_LOG("sz table has %lu entries\n", afhi->chunks_total);
57         afhi->chunk_table = para_malloc((afhi->chunks_total + 1) * sizeof(size_t));
58         for (i = 1; i <= afhi->chunks_total; i++) {
59                 if (skip + 4 > numbytes)
60                         break;
61                 sum += aac_read_int32(map + skip);
62                 afhi->chunk_table[i] = sum;
63                 skip += 4;
64 //              if (i < 10 || i + 10 > afhi->chunks_total)
65 //                      PARA_DEBUG_LOG("offset #%d: %zu\n", i, afhi->chunk_table[i]);
66         }
67         return skip;
68 }
69
70 static int aac_set_chunk_tv(struct afh_info *afhi,
71                 mp4AudioSpecificConfig *mp4ASC, long unsigned *seconds)
72 {
73         float tmp = mp4ASC->sbr_present_flag == 1? 2047 : 1023;
74         struct timeval total;
75         long unsigned ms = 1000.0 * afhi->chunks_total * tmp
76                 / mp4ASC->samplingFrequency;
77
78         if (!mp4ASC->samplingFrequency)
79                 return -E_MP4ASC;
80         ms = 1000.0 * afhi->chunks_total * tmp / mp4ASC->samplingFrequency;
81         ms2tv(ms, &total);
82         tv_divide(afhi->chunks_total, &total, &afhi->chunk_tv);
83         PARA_INFO_LOG("%luHz, %lus (%lu x %lums)\n",
84                 mp4ASC->samplingFrequency, ms / 1000,
85                 afhi->chunks_total, tv2ms(&afhi->chunk_tv));
86         if (ms < 1000)
87                 return -E_MP4ASC;
88         *seconds = ms / 1000;
89         return 1;
90 }
91
92 /*
93  * Init m4a file and write some tech data to given pointers.
94  */
95 static int aac_get_file_info(char *map, size_t numbytes,
96                 struct afh_info *afhi)
97 {
98         int i;
99         size_t skip;
100         ssize_t ret;
101         unsigned long rate = 0, decoder_len;
102         unsigned char channels = 0;
103         mp4AudioSpecificConfig mp4ASC;
104         NeAACDecHandle handle = NULL;
105         unsigned char *umap = (unsigned char *) map;
106
107         ret = aac_find_esds(umap, numbytes, &skip, &decoder_len);
108         if (ret < 0)
109                 goto out;
110         handle = aac_open();
111         ret = -E_AAC_AFH_INIT;
112         if (NeAACDecInit(handle, umap + skip, decoder_len, &rate, &channels))
113                 goto out;
114         if (!channels)
115                 goto out;
116         PARA_DEBUG_LOG("rate: %lu, channels: %d\n", rate, channels);
117         ret = -E_MP4ASC;
118         if (NeAACDecAudioSpecificConfig(umap + skip, numbytes - skip, &mp4ASC))
119                 goto out;
120         if (!mp4ASC.samplingFrequency)
121                 goto out;
122         ret = aac_compute_chunk_table(afhi, umap, numbytes);
123         if (ret < 0)
124                 goto out;
125         skip = ret;
126         ret = aac_set_chunk_tv(afhi, &mp4ASC, &afhi->seconds_total);
127         if (ret < 0)
128                 goto out;
129         ret = aac_find_entry_point(umap + skip, numbytes - skip, &skip);
130         if (ret < 0)
131                 goto out;
132         afhi->chunk_table[0] = ret;
133         for (i = 1; i<= afhi->chunks_total; i++)
134                 afhi->chunk_table[i] += ret;
135         afhi->channels = channels;
136         afhi->frequency = rate;
137         ret = (afhi->chunk_table[afhi->chunks_total] - afhi->chunk_table[0]) * 8; /* bits */
138         ret += (channels * afhi->seconds_total * 500); /* avoid rounding error */
139         afhi->bitrate = ret / (channels * afhi->seconds_total * 1000);
140         sprintf(afhi->info_string, "audio_file_info1:%lu x %lums, "
141                 "%uHz, %d channel%s, %ukb/s\n"
142                 "audio_file_info2:\n"
143                 "audio_file_info3:\n",
144                 afhi->chunks_total, tv2ms(&afhi->chunk_tv),
145                 afhi->frequency, channels, channels == 1? "" : "s", afhi->bitrate
146         );
147         tv_scale(20, &afhi->chunk_tv, &afhi->eof_tv);
148         ret = 1;
149 out:
150         if (handle)
151                 NeAACDecClose(handle);
152         return ret;
153 }
154
155 static const char* aac_suffixes[] = {"m4a", "mp4", NULL};
156 /**
157  * the init function of the aac audio format handler
158  *
159  * \param afh pointer to the struct to initialize
160  */
161 void aac_afh_init(struct audio_format_handler *afh)
162 {
163         afh->get_file_info = aac_get_file_info,
164         afh->suffixes = aac_suffixes;
165 }