audiod.c: Don't log to /dev/null.
[paraslash.git] / mp3dec.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.c Paraslash's mp3 decoder. */
8
9 #include "para.h"
10 #include "list.h"
11 #include "sched.h"
12 #include "filter.h"
13 #include "error.h"
14 #include <mad.h>
15 #include "string.h"
16
17 /** The output buffer size. */
18 #define MP3_OUTBUF_SIZE (128 * 1024)
19
20 /** Convert a sample value from libmad to a signed short. */
21 #define MAD_TO_SHORT(f) (f) >= MAD_F_ONE? SHRT_MAX :\
22         (f) <= -MAD_F_ONE? -SHRT_MAX : (signed short) ((f) >> (MAD_F_FRACBITS - 15))
23
24 /** Data specific to the mp3dec filter. */
25 struct private_mp3dec_data {
26         /** Information on the current mp3 stream. */
27         struct mad_stream stream;
28         /** Information about the frame which is currently decoded. */
29         struct mad_frame frame;
30         /** Contains the PCM output. */
31         struct mad_synth synth;
32 };
33
34 static ssize_t mp3dec(char *inbuffer, size_t len, struct filter_node *fn)
35 {
36         int i, ret;
37         struct private_mp3dec_data *pmd = fn->private_data;
38         size_t copy = PARA_MIN(len, 4096);
39
40         if (fn->loaded > fn->bufsize * 4 / 5)
41                 return 0;
42         mad_stream_buffer(&pmd->stream, (unsigned char *) inbuffer, copy);
43         pmd->stream.error = 0;
44 next_frame:
45         ret = mad_header_decode(&pmd->frame.header, &pmd->stream);
46         if (ret < 0)
47                 goto out;
48         fn->fc->samplerate = pmd->frame.header.samplerate;
49         fn->fc->channels = MAD_NCHANNELS(&pmd->frame.header);
50         ret = mad_frame_decode(&pmd->frame, &pmd->stream);
51         if (ret) {
52                 if (MAD_RECOVERABLE(pmd->stream.error) || pmd->stream.error == MAD_ERROR_BUFLEN)
53                         goto out;
54                 PARA_ERROR_LOG("fatal: ret = %d, loaded = %zd\n", ret, fn->loaded);
55                 return -E_MAD_FRAME_DECODE;
56         }
57         mad_synth_frame(&pmd->synth, &pmd->frame);
58
59         for (i = 0; i < pmd->synth.pcm.length; i++) {
60                 int s = MAD_TO_SHORT(pmd->synth.pcm.samples[0][i]);
61                 write_int16_host_endian(fn->buf + fn->loaded, s);
62                 fn->loaded += 2;
63                 if (MAD_NCHANNELS(&pmd->frame.header) == 2) { /* stereo */
64                         s = MAD_TO_SHORT(pmd->synth.pcm.samples[1][i]);
65                         write_int16_host_endian(fn->buf + fn->loaded, s);
66                         fn->loaded += 2;
67                 }
68                 if (fn->loaded != fn->bufsize) /* output buffer not full */
69                         continue;
70                 PARA_ERROR_LOG("output buffer full: %zd\n", fn->loaded);
71                         return -E_MP3DEC_OVERRUN;
72         }
73         if (fn->loaded <= fn->bufsize * 4 / 5)
74                 goto next_frame;
75 out:
76         if (pmd->stream.next_frame) { /* we still have some data */
77                 size_t off = pmd->stream.bufend - pmd->stream.next_frame;
78 //              PARA_INFO_LOG("off: %zd, rate: %u, returning %zd\n", off,
79 //                      fn->fc->samplerate, copy - off);
80                 return copy - off;
81         }
82         return copy;
83 }
84
85 static void mp3dec_close(struct filter_node *fn)
86 {
87         struct private_mp3dec_data *pmd = fn->private_data;
88
89         mad_synth_finish(&pmd->synth);
90         mad_frame_finish(&pmd->frame);
91         mad_stream_finish(&pmd->stream);
92
93         free(fn->buf);
94         fn->buf = NULL;
95         free(pmd);
96         fn->private_data = NULL;
97 }
98
99 static void mp3dec_open(struct filter_node *fn)
100 {
101         fn->private_data = para_calloc(sizeof(struct private_mp3dec_data));
102         struct private_mp3dec_data *pmd = fn->private_data;
103
104         mad_stream_init(&pmd->stream);
105         mad_frame_init(&pmd->frame);
106         mad_synth_init(&pmd->synth);
107         fn->loaded = 0;
108         fn->bufsize = MP3_OUTBUF_SIZE;
109         fn->buf = para_calloc(fn->bufsize);
110 }
111
112 /**
113  * The init function of the mp3dec filter.
114  *
115  * \param f Pointer to the filter struct to initialize.
116  *
117  * \sa filter::init.
118  */
119 void mp3dec_init(struct filter *f)
120 {
121         f->open = mp3dec_open;
122         f->convert = mp3dec;
123         f->close = mp3dec_close;
124 }