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