wma: make ->ms_stereo local to wma_decode_block().
[paraslash.git] / aacdec_filter.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 aacdec_filter.c paraslash's aac (m4a) decoder. */
12
13 #include <regex.h>
14 #include <neaacdec.h>
15
16 #include "para.h"
17 #include "portable_io.h"
18 #include "list.h"
19 #include "sched.h"
20 #include "buffer_tree.h"
21 #include "filter.h"
22 #include "error.h"
23 #include "string.h"
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         /** whether this instance of the aac decoder is already initialized */
37         bool initialized;
38         /** number of times the decoder returned an error */
39         unsigned error_count;
40         /** number of bytes already consumed from the imput stream */
41         size_t consumed_total;
42         /** The number of channels of the current stream. */
43         unsigned int channels;
44         /** Current sample rate in Hz. */
45         unsigned int sample_rate;
46 };
47
48 static int aacdec_execute(struct btr_node *btrn, const char *cmd, char **result)
49 {
50         struct filter_node *fn = btr_context(btrn);
51         struct private_aacdec_data *padd = fn->private_data;
52
53         return decoder_execute(cmd, padd->sample_rate, padd->channels, result);
54 }
55
56 static void aacdec_open(struct filter_node *fn)
57 {
58         NeAACDecConfigurationPtr c;
59         struct private_aacdec_data *padd = para_calloc(sizeof(*padd));
60
61         padd->handle = NeAACDecOpen();
62         c = NeAACDecGetCurrentConfiguration(padd->handle);
63         c->defObjectType = LC;
64         c->outputFormat = FAAD_FMT_16BIT;
65         c->downMatrix = 0;
66         NeAACDecSetConfiguration(padd->handle, c);
67
68         fn->private_data = padd;
69         fn->min_iqs = 2048;
70 }
71
72 static void aacdec_close(struct filter_node *fn)
73 {
74         struct private_aacdec_data *padd = fn->private_data;
75
76         NeAACDecClose(padd->handle);
77         free(padd);
78         fn->private_data = NULL;
79 }
80
81 static int aacdec_post_select(__a_unused struct sched *s, void *context)
82 {
83         struct filter_node *fn = context;
84         struct btr_node *btrn = fn->btrn;
85         struct private_aacdec_data *padd = fn->private_data;
86         int i, ret;
87         char *inbuf, *outbuf, *btrbuf;
88         size_t len, consumed, loaded = 0;
89         NeAACDecFrameInfo frame_info;
90
91 next_buffer:
92         ret = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL);
93         if (ret < 0)
94                 goto err;
95         if (ret == 0)
96                 return 0;
97         btr_merge(btrn, fn->min_iqs);
98         len = btr_next_buffer(btrn, &inbuf);
99         len = PARA_MIN(len, (size_t)8192);
100         consumed = 0;
101         if (!padd->initialized) {
102                 unsigned long rate = 0;
103                 unsigned char channels = 0;
104                 ret = NeAACDecInit(padd->handle, (unsigned char *)inbuf,
105                         len, &rate, &channels);
106                 PARA_INFO_LOG("decoder init: %d\n", ret);
107                 if (ret < 0) {
108                         ret = -E_AACDEC_INIT;
109                         goto err;
110                 }
111                 consumed = ret;
112                 padd->sample_rate = rate;
113                 padd->channels = channels;
114                 PARA_INFO_LOG("rate: %u, channels: %u\n",
115                         padd->sample_rate, padd->channels);
116                 padd->initialized = true;
117         }
118         if (consumed >= len)
119                 goto success;
120         //PARA_CRIT_LOG("consumed: %zu (%zu + %zu), have: %zu\n", padd->consumed_total + consumed,
121         //      padd->consumed_total, consumed, len - consumed);
122         outbuf = NeAACDecDecode(padd->handle, &frame_info,
123                 (unsigned char *)inbuf + consumed, len - consumed);
124         if (frame_info.error) {
125                 int err = frame_info.error;
126                 ret = -E_AAC_DECODE;
127                 if (padd->error_count++ > MAX_ERRORS)
128                         goto err;
129                 PARA_NOTICE_LOG("error #%u: (%s)\n", padd->error_count,
130                         NeAACDecGetErrorMessage(err));
131                 PARA_NOTICE_LOG("consumed (total, buffer, frame): "
132                         "%zu, %zu, %lu\n",
133                         padd->consumed_total, consumed,
134                         frame_info.bytesconsumed);
135                 consumed++; /* just eat one byte and hope for the best */
136                 goto success;
137         }
138         padd->error_count = 0;
139         //PARA_CRIT_LOG("decoder ate %lu\n", frame_info.bytesconsumed);
140         consumed += frame_info.bytesconsumed;
141         if (!frame_info.samples)
142                 goto success;
143         btrbuf = para_malloc(2 * frame_info.samples);
144         for (i = 0; i < frame_info.samples; i++) {
145                 short sh = ((short *)outbuf)[i];
146                 write_int16_host_endian(btrbuf + loaded, sh);
147                 loaded += 2;
148         }
149         btr_add_output(btrbuf, loaded, btrn);
150 success:
151         btr_consume(btrn, consumed);
152         padd->consumed_total += consumed;
153         if (loaded == 0)
154                 goto next_buffer;
155         return 1;
156 err:
157         assert(ret < 0);
158         btr_remove_node(&fn->btrn);
159         return ret;
160 }
161
162 const struct filter lsg_filter_cmd_com_aacdec_user_data = {
163         .open = aacdec_open,
164         .close = aacdec_close,
165         .pre_select = generic_filter_pre_select,
166         .post_select = aacdec_post_select,
167         .execute = aacdec_execute
168 };