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