Handle sender commands in vss_post_select().
[paraslash.git] / aacdec.c
1 /*
2 * Copyright (C) 2006-2008 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 aacdec.c paraslash's aac (m4a) decoder */
12
13 #include "para.h"
14
15 #include "list.h"
16 #include "sched.h"
17 #include "filter.h"
18 #include "error.h"
19 #include "string.h"
20 #include "aac.h"
21
22 /** the output buffer size */
23 #define AAC_OUTBUF_SIZE (32 * 1024)
24
25 /** give up decoding after that many errors */
26 #define MAX_ERRORS 20
27
28 /**
29 * data specific to the aacdec filter
30 *
31 * \sa filter, filter_node
32 */
33 struct private_aacdec_data {
34 /** the return value of aac_open */
35 NeAACDecHandle handle;
36 /** info about the currently decoded frame */
37 NeAACDecFrameInfo frame_info;
38 /** whether this instance of the aac decoder is already initialized */
39 int initialized;
40 /**
41 * return value of aac_find_esds(). Used to call the right aacdec
42 * init function
43 */
44 unsigned long decoder_length;
45 /** number of times the decoder returned an error */
46 unsigned error_count;
47 /** number of bytes already consumed from the imput stream */
48 size_t consumed_total;
49 /** return value of aac_find_entry_point */
50 size_t entry;
51 };
52
53 static ssize_t aacdec(char *input_buffer, size_t len, struct filter_node *fn)
54 {
55 struct private_aacdec_data *padd = fn->private_data;
56 struct filter_chain *fc = fn->fc;
57 int i, ret;
58 unsigned char *p, *outbuffer;
59 unsigned char *inbuf = (unsigned char*)input_buffer;
60 size_t skip, consumed = 0;
61
62 if (fn->loaded > fn->bufsize * 3 / 5)
63 return 0;
64 if (len < 2048 && !*fc->input_error)
65 return 0;
66
67 if (!padd->initialized) {
68 unsigned long rate = 0;
69 unsigned char channels = 0;
70 ret = aac_find_esds(inbuf, len, &skip, &padd->decoder_length);
71 if (ret < 0) {
72 PARA_INFO_LOG("%s\n", para_strerror(-ret));
73 ret = NeAACDecInit(padd->handle, inbuf,
74 len, &rate, &channels);
75 PARA_INFO_LOG("decoder init: %d\n", ret);
76 if (ret < 0) {
77 ret = -E_AACDEC_INIT;
78 goto out;
79 }
80 consumed = ret;
81 } else {
82 PARA_INFO_LOG("decoder len: %lu\n",
83 padd->decoder_length);
84 consumed += skip;
85 p = inbuf + consumed;
86 ret = -E_AACDEC_INIT;
87 if (NeAACDecInit2(padd->handle, p,
88 padd->decoder_length, &rate,
89 &channels) < 0)
90 goto out;
91 }
92 fc->samplerate = rate;
93 fc->channels = channels;
94 PARA_INFO_LOG("rate: %u, channels: %d\n",
95 fc->samplerate, fc->channels);
96 padd->initialized = 1;
97 }
98 if (padd->decoder_length > 0) {
99 consumed = 0;
100 if (!padd->entry) {
101 ret = aac_find_entry_point(inbuf + consumed,
102 len - consumed, &skip);
103 if (ret < 0) {
104 ret = len;
105 goto out;
106 }
107 consumed += skip;
108 padd->entry = ret;
109 PARA_INFO_LOG("entry: %zu\n", padd->entry);
110 }
111 ret = len;
112 if (padd->consumed_total + len < padd->entry)
113 goto out;
114 if (padd->consumed_total < padd->entry)
115 consumed = padd->entry - padd->consumed_total;
116 }
117 for (; consumed < len; consumed++)
118 if ((inbuf[consumed] & 0xfe) == 0x20)
119 break;
120 if (consumed >= len)
121 goto success;
122 p = inbuf + consumed;
123 outbuffer = NeAACDecDecode(padd->handle, &padd->frame_info, p,
124 len - consumed);
125 if (padd->frame_info.error) {
126 ret = -E_AAC_DECODE;
127 if (padd->error_count++ > MAX_ERRORS)
128 goto out;
129 PARA_ERROR_LOG("frame_error: %d, consumed: %zu + %zd + %lu\n",
130 padd->frame_info.error, padd->consumed_total,
131 consumed, padd->frame_info.bytesconsumed);
132 PARA_ERROR_LOG("%s\n", NeAACDecGetErrorMessage(
133 padd->frame_info.error));
134 consumed++; /* catch 21 */
135 goto success;
136 }
137 padd->error_count = 0;
138 consumed += padd->frame_info.bytesconsumed;
139 ret = consumed;
140 if (!padd->frame_info.samples)
141 goto out;
142 ret = -E_AAC_OVERRUN;
143 if (padd->frame_info.samples * 2 + fn->loaded > fn->bufsize)
144 goto out;
145 for (i = 0; i < padd->frame_info.samples; i++) {
146 short *s = (short *)outbuffer;
147 write_int16_host_endian(fn->buf + fn->loaded, s[i]);
148 fn->loaded += 2;
149 }
150 success:
151 ret = consumed;
152 out:
153 if (ret > 0)
154 padd->consumed_total += ret;
155 return ret;
156 }
157
158 static void aacdec_open(struct filter_node *fn)
159 {
160 fn->private_data = para_calloc(sizeof(struct private_aacdec_data));
161 struct private_aacdec_data *padd = fn->private_data;
162
163 fn->bufsize = AAC_OUTBUF_SIZE;
164 fn->buf = para_calloc(fn->bufsize);
165 padd->handle = aac_open();
166 }
167
168 static void aacdec_close(struct filter_node *fn)
169 {
170 struct private_aacdec_data *padd = fn->private_data;
171
172 NeAACDecClose(padd->handle);
173 free(fn->buf);
174 fn->buf = NULL;
175 free(padd);
176 fn->private_data = NULL;
177 }
178
179 /**
180 * the init function of the aacdec filter
181 *
182 * \param f pointer to the filter struct to initialize
183 *
184 * \sa filter::init
185 */
186 void aacdec_init(struct filter *f)
187 {
188 f->open = aacdec_open;
189 f->convert = aacdec;
190 f->close = aacdec_close;
191 }