paraslash 0.7.3
[paraslash.git] / mp3dec_filter.c
index 6982f264a8d866970cc80e960d79628a41c74101..d40df85edbe24b9dc54f01fc9305a1575c022507 100644 (file)
@@ -1,20 +1,15 @@
-/*
- * Copyright (C) 2005-2011 Andre Noll <maan@systemlinux.org>
- *
- * Licensed under the GPL v2. For licencing details see COPYING.
- */
+/* Copyright (C) 2005 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
 
 /** \file mp3dec_filter.c Paraslash's mp3 decoder. */
 
 #include <mad.h>
 #include <regex.h>
-#include <stdbool.h>
+#include <lopsub.h>
 
+#include "filter_cmd.lsg.h"
 #include "para.h"
-#include "mp3dec_filter.cmdline.h"
 #include "list.h"
 #include "sched.h"
-#include "ggo.h"
 #include "buffer_tree.h"
 #include "filter.h"
 #include "error.h"
@@ -50,16 +45,18 @@ static int handle_decode_error(struct private_mp3dec_data *pmd)
        return 0;
 }
 
-static size_t used_mad_buffer_bytes(struct mad_stream *s, size_t max)
+static void mp3dec_consume(struct btr_node *btrn, struct mad_stream *s,
+               size_t max)
 {
-       size_t rv;
+       size_t used;
 
        if (!s->next_frame)
-               return max;
-       /* we still have some data */
-       rv = s->next_frame - s->buffer;
-       assert(rv <= max);
-       return rv;
+               used = max;
+       else { /* we still have some data */
+               used = s->next_frame - s->buffer;
+               assert(used <= max);
+       }
+       btr_consume(btrn, used);
 }
 
 static void mp3dec_close(struct filter_node *fn)
@@ -74,50 +71,52 @@ static void mp3dec_close(struct filter_node *fn)
        fn->private_data = NULL;
 }
 
-static void mp3dec_post_select(__a_unused struct sched *s, struct task *t)
+#define MP3DEC_MAX_FRAME 8192
+
+static int mp3dec_post_monitor(__a_unused struct sched *s, void *context)
 {
-       struct filter_node *fn = container_of(t, struct filter_node, task);
+       struct filter_node *fn = context;
        int i, ret;
        struct private_mp3dec_data *pmd = fn->private_data;
        struct btr_node *btrn = fn->btrn;
-       size_t loaded = 0, used, len, iqs;
+       size_t loaded = 0, len, iqs;
        char *inbuffer, *outbuffer;
 
 next_buffer:
        pmd->stream.error = 0;
-       t->error = 0;
        iqs = btr_get_input_queue_size(btrn);
        ret = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL);
        if (ret < 0)
                goto err;
        if (ret == 0)
-               return;
+               return 0;
        btr_merge(btrn, fn->min_iqs);
        len = btr_next_buffer(btrn, &inbuffer);
        /*
-        * Decode at most 8K in one go to give the post_select() functions of
+        * Decode at most 8K in one go to give the post_monitor() functions of
         * other buffer tree nodes a chance to run. This is necessary to avoid
         * buffer underruns on slow machines.
         */
-       len = PARA_MIN(len, (size_t)8192);
+       len = PARA_MIN(len, (size_t)MP3DEC_MAX_FRAME);
        mad_stream_buffer(&pmd->stream, (unsigned char *)inbuffer, len);
 next_frame:
        ret = mad_header_decode(&pmd->frame.header, &pmd->stream);
        if (ret < 0) {
-               used = used_mad_buffer_bytes(&pmd->stream, len);
-               btr_consume(btrn, used);
+               mp3dec_consume(btrn, &pmd->stream, len);
                if (pmd->stream.error == MAD_ERROR_BUFLEN) {
                        if (len == iqs && btr_no_parent(btrn)) {
-                               ret = -E_MP3DEC_EOF;
+                               ret = -E_EOF;
                                goto err;
                        }
                        fn->min_iqs += 100;
+                       ret = -E_MP3DEC_CORRUPT;
+                       if (fn->min_iqs > MP3DEC_MAX_FRAME)
+                               goto err;
                }
                if (loaded == 0)
                        goto next_buffer;
-               return;
+               return 0;
        }
-       fn->min_iqs = 0;
        pmd->sample_rate = pmd->frame.header.samplerate;
        pmd->channels = MAD_NCHANNELS(&pmd->frame.header);
 decode:
@@ -127,16 +126,25 @@ decode:
                if (ret < 0)
                        goto err;
                mad_stream_sync(&pmd->stream);
-               if (pmd->stream.error == MAD_ERROR_BUFLEN)
-                       return;
+               if (pmd->stream.error == MAD_ERROR_BUFLEN) {
+                       ret = -E_EOF;
+                       if (len == iqs && btr_no_parent(btrn))
+                               goto err;
+                       fn->min_iqs += 100;
+                       ret = -E_MP3DEC_CORRUPT;
+                       if (fn->min_iqs > MP3DEC_MAX_FRAME)
+                               goto err;
+                       mp3dec_consume(btrn, &pmd->stream, len);
+                       return 0;
+               }
                if (pmd->stream.error != MAD_ERROR_BADDATAPTR)
                        goto decode;
-               used = used_mad_buffer_bytes(&pmd->stream, len);
-               btr_consume(btrn, used);
-               return;
+               mp3dec_consume(btrn, &pmd->stream, len);
+               return 0;
        }
+       fn->min_iqs = 0;
        mad_synth_frame(&pmd->synth, &pmd->frame);
-       outbuffer = para_malloc(pmd->synth.pcm.length * 2 * pmd->channels);
+       outbuffer = arr_alloc(pmd->synth.pcm.length, 2 * pmd->channels);
        loaded = 0;
        for (i = 0; i < pmd->synth.pcm.length; i++) {
                int sample = MAD_TO_SHORT(pmd->synth.pcm.samples[0][i]);
@@ -152,39 +160,22 @@ decode:
        goto next_frame;
 err:
        assert(ret < 0);
-       t->error = ret;
-       btr_remove_node(btrn);
+       btr_remove_node(&fn->btrn);
+       return ret;
 }
 
 static void mp3dec_open(struct filter_node *fn)
 {
-       struct private_mp3dec_data *pmd = para_calloc(sizeof(*pmd));
-       struct mp3dec_filter_args_info *mp3_conf = fn->conf;
+       struct private_mp3dec_data *pmd = zalloc(sizeof(*pmd));
 
        fn->private_data = pmd;
        mad_stream_init(&pmd->stream);
        mad_frame_init(&pmd->frame);
        mad_synth_init(&pmd->synth);
-       if (mp3_conf->ignore_crc_given)
+       if (FILTER_CMD_OPT_GIVEN(MP3DEC, IGNORE_CRC, fn->lpr))
                mad_stream_options(&pmd->stream, MAD_OPTION_IGNORECRC);
 }
 
-static int mp3dec_parse_config(int argc, char **argv, void **config)
-{
-       int ret;
-       struct mp3dec_filter_args_info *mp3_conf;
-
-       mp3_conf = para_calloc(sizeof(*mp3_conf));
-       ret = -E_MP3DEC_SYNTAX;
-       if (mp3dec_cmdline_parser(argc, argv, mp3_conf))
-               goto err;
-       *config = mp3_conf;
-       return 1;
-err:
-       free(mp3_conf);
-       return ret;
-}
-
 static int mp3dec_execute(struct btr_node *btrn, const char *cmd, char **result)
 {
        struct filter_node *fn = btr_context(btrn);
@@ -193,31 +184,10 @@ static int mp3dec_execute(struct btr_node *btrn, const char *cmd, char **result)
        return decoder_execute(cmd, pmd->sample_rate, pmd->channels, result);
 }
 
-static void mp3dec_free_config(void *conf)
-{
-       mp3dec_cmdline_parser_free(conf);
-}
-/**
- * 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)
-{
-       struct mp3dec_filter_args_info dummy;
-
-       mp3dec_cmdline_parser_init(&dummy);
-       f->open = mp3dec_open;
-       f->close = mp3dec_close;
-       f->parse_config = mp3dec_parse_config;
-       f->free_config = mp3dec_free_config;
-       f->pre_select = generic_filter_pre_select;
-       f->post_select = mp3dec_post_select;
-       f->execute = mp3dec_execute;
-       f->help = (struct ggo_help) {
-               .short_help = mp3dec_filter_args_info_help,
-               .detailed_help = mp3dec_filter_args_info_detailed_help
-       };
-}
+const struct filter lsg_filter_cmd_com_mp3dec_user_data = {
+       .open = mp3dec_open,
+       .close = mp3dec_close,
+       .pre_monitor = generic_filter_pre_monitor,
+       .post_monitor = mp3dec_post_monitor,
+       .execute = mp3dec_execute,
+};