Merge commit 'meins/next' into next
[paraslash.git] / mp3dec_filter.c
1 /*
2  * Copyright (C) 2005-2009 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file mp3dec_filter.c Paraslash's mp3 decoder. */
8
9 #include <mad.h>
10 #include <regex.h>
11
12 #include "para.h"
13 #include "mp3dec_filter.cmdline.h"
14 #include "list.h"
15 #include "sched.h"
16 #include "ggo.h"
17 #include "filter.h"
18 #include "error.h"
19 #include "string.h"
20
21 /** Convert a sample value from libmad to a signed short. */
22 #define MAD_TO_SHORT(f) (f) >= MAD_F_ONE? SHRT_MAX :\
23         (f) <= -MAD_F_ONE? -SHRT_MAX : (signed short) ((f) >> (MAD_F_FRACBITS - 15))
24
25 /** Data specific to the mp3dec filter. */
26 struct private_mp3dec_data {
27         /** Information on the current mp3 stream. */
28         struct mad_stream stream;
29         /** Information about the frame which is currently decoded. */
30         struct mad_frame frame;
31         /** Contains the PCM output. */
32         struct mad_synth synth;
33 };
34
35 static ssize_t mp3dec(char *inbuffer, size_t len, struct filter_node *fn)
36 {
37         int i, ret;
38         struct private_mp3dec_data *pmd = fn->private_data;
39         size_t copy = PARA_MIN(len, (size_t)4096);
40
41         if (fn->loaded + 16384 > fn->bufsize)
42                 return 0;
43         mad_stream_buffer(&pmd->stream, (unsigned char *) inbuffer, copy);
44         pmd->stream.error = 0;
45 next_frame:
46         ret = mad_header_decode(&pmd->frame.header, &pmd->stream);
47         if (ret < 0) {
48                 if (pmd->stream.error != MAD_ERROR_BUFLEN &&
49                         pmd->stream.error != MAD_ERROR_LOSTSYNC)
50                         PARA_DEBUG_LOG("header decode: %s\n",
51                                 mad_stream_errorstr(&pmd->stream));
52                 goto out;
53         }
54         fn->fc->samplerate = pmd->frame.header.samplerate;
55         fn->fc->channels = MAD_NCHANNELS(&pmd->frame.header);
56         ret = mad_frame_decode(&pmd->frame, &pmd->stream);
57         if (ret) {
58                 if (MAD_RECOVERABLE(pmd->stream.error) ||
59                         pmd->stream.error == MAD_ERROR_BUFLEN) {
60                         PARA_DEBUG_LOG("frame decode: %s\n",
61                                 mad_stream_errorstr(&pmd->stream));
62                         goto out;
63                 }
64                 PARA_ERROR_LOG("frame decode: %s\n",
65                         mad_stream_errorstr(&pmd->stream));
66                 return -E_MAD_FRAME_DECODE;
67         }
68         mad_synth_frame(&pmd->synth, &pmd->frame);
69
70         for (i = 0; i < pmd->synth.pcm.length; i++) {
71                 int s = MAD_TO_SHORT(pmd->synth.pcm.samples[0][i]);
72                 write_int16_host_endian(fn->buf + fn->loaded, s);
73                 fn->loaded += 2;
74                 if (MAD_NCHANNELS(&pmd->frame.header) == 2) { /* stereo */
75                         s = MAD_TO_SHORT(pmd->synth.pcm.samples[1][i]);
76                         write_int16_host_endian(fn->buf + fn->loaded, s);
77                         fn->loaded += 2;
78                 }
79                 if (fn->loaded != fn->bufsize) /* output buffer not full */
80                         continue;
81                 PARA_ERROR_LOG("output buffer full: %zd\n", fn->loaded);
82                         return -E_MP3DEC_OVERRUN;
83         }
84         if (fn->loaded + 16384 <= fn->bufsize)
85                 goto next_frame;
86 out:
87         if (pmd->stream.next_frame) { /* we still have some data */
88                 size_t off = pmd->stream.bufend - pmd->stream.next_frame;
89                 if (fn->loaded + 16384 <= fn->bufsize && off > 2048)
90                         goto next_frame;
91                 return copy - off;
92         }
93         return copy;
94 }
95
96 static void mp3dec_close(struct filter_node *fn)
97 {
98         struct private_mp3dec_data *pmd = fn->private_data;
99
100         mad_synth_finish(&pmd->synth);
101         mad_frame_finish(&pmd->frame);
102         mad_stream_finish(&pmd->stream);
103
104         free(fn->buf);
105         fn->buf = NULL;
106         free(pmd);
107         fn->private_data = NULL;
108 }
109
110 static void mp3dec_open(struct filter_node *fn)
111 {
112         struct private_mp3dec_data *pmd = para_calloc(sizeof(*pmd));
113         struct mp3dec_filter_args_info *mp3_conf = fn->conf;
114
115         fn->private_data = pmd;
116         mad_stream_init(&pmd->stream);
117         mad_frame_init(&pmd->frame);
118         mad_synth_init(&pmd->synth);
119         fn->loaded = 0;
120         fn->bufsize = mp3_conf->bufsize_arg * 1024;
121         fn->buf = para_calloc(fn->bufsize);
122         if (mp3_conf->ignore_crc_given)
123                 mad_stream_options(&pmd->stream, MAD_OPTION_IGNORECRC);
124 }
125
126 static int mp3dec_parse_config(int argc, char **argv, void **config)
127 {
128         int ret;
129         struct mp3dec_filter_args_info *mp3_conf;
130
131         mp3_conf = para_calloc(sizeof(*mp3_conf));
132         ret = -E_MP3DEC_SYNTAX;
133         if (mp3dec_cmdline_parser(argc, argv, mp3_conf))
134                 goto err;
135         ret = -ERRNO_TO_PARA_ERROR(EINVAL);
136         if (mp3_conf->bufsize_arg < 32)
137                 goto err;
138         if (mp3_conf->bufsize_arg >= INT_MAX / 1024)
139                 goto err;
140         *config = mp3_conf;
141         return 1;
142 err:
143         free(mp3_conf);
144         return ret;
145 }
146
147 /**
148  * The init function of the mp3dec filter.
149  *
150  * \param f Pointer to the filter struct to initialize.
151  *
152  * \sa filter::init.
153  */
154 void mp3dec_filter_init(struct filter *f)
155 {
156         struct mp3dec_filter_args_info dummy;
157
158         mp3dec_cmdline_parser_init(&dummy);
159         f->open = mp3dec_open;
160         f->convert = mp3dec;
161         f->close = mp3dec_close;
162         f->parse_config = mp3dec_parse_config;
163         f->help = (struct ggo_help) {
164                 .short_help = mp3dec_filter_args_info_help,
165                 .detailed_help = mp3dec_filter_args_info_detailed_help
166         };
167 }