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