+++ /dev/null
-/*
- * Copyright (C) 2005-2008 Andre Noll <maan@systemlinux.org>
- *
- * Licensed under the GPL v2. For licencing details see COPYING.
- */
-
-/** \file mp3dec.c Paraslash's mp3 decoder. */
-
-#include "para.h"
-#include "list.h"
-#include "sched.h"
-#include "filter.h"
-#include "error.h"
-#include <mad.h>
-#include "string.h"
-
-/** The output buffer size. */
-#define MP3_OUTBUF_SIZE (128 * 1024)
-
-/** Convert a sample value from libmad to a signed short. */
-#define MAD_TO_SHORT(f) (f) >= MAD_F_ONE? SHRT_MAX :\
- (f) <= -MAD_F_ONE? -SHRT_MAX : (signed short) ((f) >> (MAD_F_FRACBITS - 15))
-
-/** Data specific to the mp3dec filter. */
-struct private_mp3dec_data {
- /** Information on the current mp3 stream. */
- struct mad_stream stream;
- /** Information about the frame which is currently decoded. */
- struct mad_frame frame;
- /** Contains the PCM output. */
- struct mad_synth synth;
-};
-
-static ssize_t mp3dec(char *inbuffer, size_t len, struct filter_node *fn)
-{
- int i, ret;
- struct private_mp3dec_data *pmd = fn->private_data;
- size_t copy = PARA_MIN(len, 4096);
-
- if (fn->loaded > fn->bufsize * 4 / 5)
- return 0;
- mad_stream_buffer(&pmd->stream, (unsigned char *) inbuffer, copy);
- pmd->stream.error = 0;
-next_frame:
- ret = mad_header_decode(&pmd->frame.header, &pmd->stream);
- if (ret < 0) {
- if (pmd->stream.error != MAD_ERROR_BUFLEN &&
- pmd->stream.error != MAD_ERROR_LOSTSYNC)
- PARA_DEBUG_LOG("header decode: %s\n",
- mad_stream_errorstr(&pmd->stream));
- goto out;
- }
- fn->fc->samplerate = pmd->frame.header.samplerate;
- fn->fc->channels = MAD_NCHANNELS(&pmd->frame.header);
- ret = mad_frame_decode(&pmd->frame, &pmd->stream);
- if (ret) {
- if (MAD_RECOVERABLE(pmd->stream.error) ||
- pmd->stream.error == MAD_ERROR_BUFLEN) {
- PARA_DEBUG_LOG("frame decode: %s\n",
- mad_stream_errorstr(&pmd->stream));
- goto out;
- }
- PARA_ERROR_LOG("frame decode: %s\n",
- mad_stream_errorstr(&pmd->stream));
- return -E_MAD_FRAME_DECODE;
- }
- mad_synth_frame(&pmd->synth, &pmd->frame);
-
- for (i = 0; i < pmd->synth.pcm.length; i++) {
- int s = MAD_TO_SHORT(pmd->synth.pcm.samples[0][i]);
- write_int16_host_endian(fn->buf + fn->loaded, s);
- fn->loaded += 2;
- if (MAD_NCHANNELS(&pmd->frame.header) == 2) { /* stereo */
- s = MAD_TO_SHORT(pmd->synth.pcm.samples[1][i]);
- write_int16_host_endian(fn->buf + fn->loaded, s);
- fn->loaded += 2;
- }
- if (fn->loaded != fn->bufsize) /* output buffer not full */
- continue;
- PARA_ERROR_LOG("output buffer full: %zd\n", fn->loaded);
- return -E_MP3DEC_OVERRUN;
- }
- if (fn->loaded <= fn->bufsize * 4 / 5)
- goto next_frame;
-out:
- if (pmd->stream.next_frame) { /* we still have some data */
- size_t off = pmd->stream.bufend - pmd->stream.next_frame;
-// PARA_INFO_LOG("off: %zd, rate: %u, returning %zd\n", off,
-// fn->fc->samplerate, copy - off);
- return copy - off;
- }
- return copy;
-}
-
-static void mp3dec_close(struct filter_node *fn)
-{
- struct private_mp3dec_data *pmd = fn->private_data;
-
- mad_synth_finish(&pmd->synth);
- mad_frame_finish(&pmd->frame);
- mad_stream_finish(&pmd->stream);
-
- free(fn->buf);
- fn->buf = NULL;
- free(pmd);
- fn->private_data = NULL;
-}
-
-static void mp3dec_open(struct filter_node *fn)
-{
- struct private_mp3dec_data *pmd = para_calloc(sizeof(*pmd));
-
- fn->private_data = pmd;
- mad_stream_init(&pmd->stream);
- mad_frame_init(&pmd->frame);
- mad_synth_init(&pmd->synth);
- fn->loaded = 0;
- fn->bufsize = MP3_OUTBUF_SIZE;
- fn->buf = para_calloc(fn->bufsize);
-}
-
-/**
- * The init function of the mp3dec filter.
- *
- * \param f Pointer to the filter struct to initialize.
- *
- * \sa filter::init.
- */
-void mp3dec_filter_init(struct filter *f)
-{
- f->open = mp3dec_open;
- f->convert = mp3dec;
- f->close = mp3dec_close;
-}