string.c: Fix typos in comment.
[paraslash.git] / flacdec_filter.c
index 40246f24b625c3a5236df47149bb9179633eb5f9..6a3a8effaf83b3b9e333e429762f0ff2d3938afe 100644 (file)
@@ -1,8 +1,4 @@
-/*
- * Copyright (C) 2011-2012 Andre Noll <maan@systemlinux.org>
- *
- * Licensed under the GPL v2. For licencing details see COPYING.
- */
+/* Copyright (C) 2011 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
 
 /** \file flacdec_filter.c The flac decoder. */
 
@@ -12,7 +8,6 @@
 #include "para.h"
 #include "list.h"
 #include "sched.h"
-#include "ggo.h"
 #include "buffer_tree.h"
 #include "filter.h"
 #include "error.h"
@@ -25,7 +20,7 @@ struct private_flacdec_data {
         * We can not consume directly what was copied by the read callback
         * because we might need to feed unconsumend bytes to the decoder again
         * after the read callback ran out of data and returned ABORT. So we
-        * track how many bytes are unconsumed so far.
+        * track how many bytes have been fed to libflac but are unconsumed so far.
         */
        size_t unconsumed;
 };
@@ -63,6 +58,16 @@ static FLAC__StreamDecoderReadStatus read_cb(
        }
        if (*bytes > 0)
                return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
+       /**
+       * Nothing was copied. If the input queue of the btrn is smaller than
+       * the minimal input queue size, our parent must have been gone, so
+       * we're not going to get more input. Since our remaining data is not
+       * sufficient do decode a single frame, we have an EOF condition.
+       */
+       if (btr_get_input_queue_size(btrn) < fn->min_iqs) {
+               assert(btr_no_parent(btrn));
+               return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
+       }
        /*
         * We are kind of screwed here. Returning CONTINUE with a byte count of
         * zero leads to an endless loop, so we must return either EOF or
@@ -144,7 +149,7 @@ static FLAC__StreamDecoderWriteStatus write_cb(
                        write_int16_host_endian(outbuffer + 4 * k + 2, right);
                }
        }
-       btr_add_output(outbuffer, n * 4, btrn);
+       btr_add_output(outbuffer, n * channels * 2, btrn);
        flac_consume(fn);
        return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
 }
@@ -200,9 +205,9 @@ static bool output_queue_full(struct btr_node *btrn)
        return btr_get_output_queue_size(btrn) > FLACDEC_MAX_OUTPUT_SIZE;
 }
 
-static void flacdec_pre_select(struct sched *s, struct task *t)
+static void flacdec_pre_select(struct sched *s, void *context)
 {
-       struct filter_node *fn = container_of(t, struct filter_node, task);
+       struct filter_node *fn = context;
        struct private_flacdec_data *pfd = fn->private_data;
        struct btr_node *btrn = fn->btrn;
        int ret;
@@ -216,15 +221,16 @@ static void flacdec_pre_select(struct sched *s, struct task *t)
                return sched_min_delay(s);
 }
 
-static void flacdec_post_select(__a_unused struct sched *s, struct task *t)
+static int flacdec_post_select(__a_unused struct sched *s, void *context)
 {
-       struct filter_node *fn = container_of(t, struct filter_node, task);
+       struct filter_node *fn = context;
        struct private_flacdec_data *pfd = fn->private_data;
        struct btr_node *btrn = fn->btrn;
        int ret;
+       FLAC__StreamDecoderState state;
 
        if (output_queue_full(btrn))
-               return;
+               return 0;
        ret = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL);
        if (ret < 0 && ret != -E_BTR_EOF) /* fatal error */
                goto out;
@@ -234,40 +240,46 @@ static void flacdec_post_select(__a_unused struct sched *s, struct task *t)
                ret = flacdec_init(fn);
                goto out;
        }
-       pfd->unconsumed = 0;
-       for (;;) {
-               if (output_queue_full(btrn)) {
-                       pfd->have_more = true;
-                       break;
-               }
-               pfd->have_more = false;
-               FLAC__StreamDecoderState state;
-               FLAC__stream_decoder_process_single(pfd->decoder);
-               state = FLAC__stream_decoder_get_state(pfd->decoder);
-               //PARA_CRIT_LOG("state: %s\n", FLAC__stream_decoder_get_resolved_state_string(pfd->decoder));
-               ret = -E_FLACDEC_EOF;
-               if (state == FLAC__STREAM_DECODER_END_OF_STREAM)
-                       goto out;
-               if (state == FLAC__STREAM_DECODER_ABORTED) {
-                       FLAC__stream_decoder_flush(pfd->decoder);
-                       fn->min_iqs = pfd->unconsumed + 1;
-                       break;
-               }
-               fn->min_iqs = 0;
+       if (output_queue_full(btrn)) {
+               pfd->have_more = true;
+               ret = 1;
+               goto out;
+       }
+       pfd->have_more = false;
+       FLAC__stream_decoder_process_single(pfd->decoder);
+       state = FLAC__stream_decoder_get_state(pfd->decoder);
+       ret = -E_FLACDEC_EOF;
+       if (state == FLAC__STREAM_DECODER_END_OF_STREAM)
+               goto out;
+       if (state == FLAC__STREAM_DECODER_ABORTED) {
+               FLAC__stream_decoder_flush(pfd->decoder);
+               pfd->unconsumed = 0; /* feed unconsumed bytes again */
+               fn->min_iqs = btr_get_input_queue_size(btrn) + 1;
+               ret = 1;
+               goto out;
        }
+       pfd->have_more = true;
+       fn->min_iqs = 0;
        ret = 1;
 out:
-       t->error = ret;
        if (ret < 0)
-               btr_remove_node(btrn);
+               btr_remove_node(&fn->btrn);
+       return ret;
 }
 
 static void flacdec_close(struct filter_node *fn)
 {
-       struct private_flacdec_data *pfd = fn->private_data;
+       struct private_flacdec_data *pfd;
 
-       FLAC__stream_decoder_finish(pfd->decoder);
-       FLAC__stream_decoder_delete(pfd->decoder);
+       if (!fn)
+               return;
+       pfd = fn->private_data;
+       if (!pfd)
+               return;
+       if (pfd->decoder) {
+               FLAC__stream_decoder_finish(pfd->decoder);
+               FLAC__stream_decoder_delete(pfd->decoder);
+       }
        free(pfd);
        fn->private_data = NULL;
 }
@@ -279,18 +291,10 @@ static void flacdec_open(struct filter_node *fn)
        fn->min_iqs = 0;
 }
 
-/**
- * The init function of the flacdec filter.
- *
- * \param f Pointer to the filter struct to initialize.
- *
- * \sa filter::init.
- */
-void flacdec_filter_init(struct filter *f)
-{
-       f->open = flacdec_open;
-       f->close = flacdec_close;
-       f->pre_select = flacdec_pre_select;
-       f->post_select = flacdec_post_select;
-       f->execute = flacdec_execute;
-}
+const struct filter lsg_filter_cmd_com_flacdec_user_data = {
+       .open = flacdec_open,
+       .close = flacdec_close,
+       .pre_select = flacdec_pre_select,
+       .post_select = flacdec_post_select,
+       .execute = flacdec_execute,
+};