mp3dec: Fix EOF condition.
[paraslash.git] / wav_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 wav_filter.c A filter that inserts a wave header. */
8
9 #include <regex.h>
10 #include <stdbool.h>
11
12 #include "para.h"
13 #include "error.h"
14 #include "list.h"
15 #include "sched.h"
16 #include "ggo.h"
17 #include "buffer_tree.h"
18 #include "filter.h"
19 #include "string.h"
20 #include "portable_io.h"
21
22 /** size of the output buffer */
23 #define WAV_OUTBUF_SIZE 81920
24 /** a wav header is always 44 bytes */
25 #define WAV_HEADER_LEN 44
26 /** always write 16 bit header */
27 #define BITS 16
28
29 static void make_wav_header(unsigned int channels, unsigned int samplerate,
30                 struct filter_node *fn)
31 {
32
33         char *headbuf = fn->buf;
34         unsigned int size = 0x7fffffff;
35         int bytespersec = channels * samplerate * BITS / 8;
36         int align = channels * BITS / 8;
37
38         PARA_DEBUG_LOG("writing wave header: %d channels, %d KHz\n", channels, samplerate);
39         memset(headbuf, 0, WAV_HEADER_LEN);
40         memcpy(headbuf, "RIFF", 4);
41         write_u32(headbuf + 4, size - 8);
42         memcpy(headbuf + 8, "WAVE", 4);
43         memcpy(headbuf + 12, "fmt ", 4);
44         write_u32(headbuf + 16, 16); /* 16 + extra format bytes (zero) */
45         write_u16(headbuf + 20, 1);     /* format (1 == PCM/uncompressed) */
46         write_u16(headbuf + 22, channels);
47         write_u32(headbuf + 24, samplerate);
48         write_u32(headbuf + 28, bytespersec);
49         write_u16(headbuf + 32, align); /* number of bytes per sample slice */
50         write_u16(headbuf + 34, BITS); /* significant bits per sample */
51         memcpy(headbuf + 36, "data", 4); /* chunk ID */
52         write_u32(headbuf + 40, size - 44); /* chunk size */
53 }
54
55 static ssize_t wav_convert(char *inbuf, size_t len, struct filter_node *fn)
56 {
57         size_t copy;
58         int *bof = fn->private_data;
59
60         if (*bof) {
61                 if (!len)
62                         return 0;
63                 if (!fn->fc->channels || !fn->fc->samplerate) {
64                         PARA_ERROR_LOG("%s\n", para_strerror(E_WAV_BAD_FC));
65                         return -E_WAV_BAD_FC;
66                 }
67                 make_wav_header(fn->fc->channels, fn->fc->samplerate, fn);
68                 fn->loaded = WAV_HEADER_LEN;
69                 *bof = 0;
70 //              return 0;
71         }
72         copy = PARA_MIN(len, fn->bufsize - fn->loaded);
73         memmove(fn->buf + fn->loaded, inbuf, copy);
74         fn->loaded += copy;
75 //      PARA_DEBUG_LOG("len = %d, copy = %d\n", len, copy);
76         return copy;
77 }
78
79 static void wav_close(struct filter_node *fn)
80 {
81         free(fn->buf);
82         fn->buf = NULL;
83         free(fn->private_data);
84         fn->private_data = NULL;
85 }
86
87 static void wav_open(struct filter_node *fn)
88 {
89         int *bof;
90
91         fn->bufsize = WAV_OUTBUF_SIZE;
92         fn->buf = para_malloc(fn->bufsize);
93         fn->private_data = para_malloc(sizeof(int));
94         bof = fn->private_data;
95         fn->loaded = 0;
96         *bof = 1;
97         PARA_INFO_LOG("wav filter node: %p, output buffer: %p, loaded: %zd\n",
98                 fn, fn->buf, fn->loaded);
99 }
100
101 /**
102  * the init function of the wav filter
103  *
104  * \param f struct to initialize
105  */
106 void wav_filter_init(struct filter *f)
107 {
108         f->convert = wav_convert;
109         f->close = wav_close;
110         f->open = wav_open;
111 }