we always compute the inverse mdct, so get rid of the inverse bit.
[paraslash.git] / prebuffer_filter.c
1 /*
2  * Copyright (C) 2009 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file prebuffer_filter.c Paraslash's prebuffering filter. */
8
9 #include <regex.h>
10
11 #include "para.h"
12 #include "prebuffer_filter.cmdline.h"
13 #include "list.h"
14 #include "sched.h"
15 #include "ggo.h"
16 #include "filter.h"
17 #include "string.h"
18 #include "error.h"
19
20 /** Data specific to the prebuffer filter. */
21 struct private_prebuffer_data {
22         /** The configuration data for this instance of the filter. */
23         struct prebuffer_filter_args_info *conf;
24         /** Number of bytes prebuffered or -1 if no longer prebuffering. */
25         int prebuffered;
26         /** End of prebuffering period. */
27         struct timeval barrier;
28 };
29
30 static ssize_t prebuffer_convert(char *inbuf, size_t inbuf_len,
31                 struct filter_node *fn)
32 {
33         struct private_prebuffer_data *ppd = fn->private_data;
34         struct prebuffer_filter_args_info *conf = ppd->conf;
35
36         if (inbuf_len == 0) {
37                 if (*fn->fc->input_error < 0 && ppd->prebuffered >= 0)
38                         goto prebuffer_end;
39                 return 0;
40         }
41         if (ppd->prebuffered < 0) {
42                 size_t copy = PARA_MIN(inbuf_len, fn->bufsize - fn->loaded);
43                 memcpy(fn->buf + fn->loaded, inbuf, copy);
44                 fn->loaded += copy;
45                 return copy;
46         }
47         if (ppd->prebuffered + inbuf_len > fn->bufsize) {
48                 fn->bufsize = PARA_MAX(2 * fn->bufsize,
49                         ppd->prebuffered + inbuf_len);
50                 fn->buf = para_realloc(fn->buf, fn->bufsize);
51         }
52         memcpy(fn->buf + ppd->prebuffered, inbuf, inbuf_len);
53         if (ppd->prebuffered == 0) {
54                 struct timeval tv;
55                 PARA_INFO_LOG("prebuffer period %dms\n",
56                         conf->duration_arg);
57                 ms2tv(conf->duration_arg, &tv);
58                 tv_add(&tv, now, &ppd->barrier);
59         }
60         ppd->prebuffered += inbuf_len;
61         PARA_DEBUG_LOG("%d bytes prebuffered\n", ppd->prebuffered);
62         if (*fn->fc->input_error >= 0) {
63                 struct timeval diff;
64                 if (tv_diff(now, &ppd->barrier, &diff) < 0)
65                         goto out;
66                 if (ppd->prebuffered < conf->size_arg)
67                         goto out;
68         }
69 prebuffer_end:
70         fn->loaded = ppd->prebuffered;
71         ppd->prebuffered = -1;
72 out:
73         return inbuf_len;
74 }
75
76 static void prebuffer_close(struct filter_node *fn)
77 {
78         free(fn->private_data);
79         free(fn->buf);
80 }
81
82 static int prebuffer_parse_config(int argc, char **argv, void **config)
83 {
84         struct prebuffer_filter_args_info *prebuffer_conf
85                 = para_calloc(sizeof(*prebuffer_conf));
86         int ret = -E_PREBUFFER_SYNTAX;
87
88         if (prebuffer_cmdline_parser(argc, argv, prebuffer_conf))
89                 goto err;
90         ret = -ERRNO_TO_PARA_ERROR(EINVAL);
91         if (prebuffer_conf->duration_arg < 0)
92                 goto err;
93         if (prebuffer_conf->size_arg < 0)
94                 goto err;
95         PARA_NOTICE_LOG("prebuffering %ims, %i bytes\n",
96                 prebuffer_conf->duration_arg, prebuffer_conf->size_arg);
97         *config = prebuffer_conf;
98         return 1;
99 err:
100         free(prebuffer_conf);
101         return ret;
102 }
103
104 static void prebuffer_open(struct filter_node *fn)
105 {
106         struct private_prebuffer_data *ppd = para_calloc(sizeof(*ppd));
107
108         ppd->conf = fn->conf;
109         fn->private_data = ppd;
110         fn->bufsize = 8192; /* gets increased on demand */
111         fn->buf = para_malloc(fn->bufsize);
112 }
113
114 /**
115  * The init function of the prebuffer filter.
116  *
117  * \param f Pointer to the struct to initialize.
118  */
119 void prebuffer_filter_init(struct filter *f)
120 {
121         struct prebuffer_filter_args_info dummy;
122
123         prebuffer_cmdline_parser_init(&dummy);
124         f->open = prebuffer_open;
125         f->close = prebuffer_close;
126         f->convert = prebuffer_convert;
127         f->parse_config = prebuffer_parse_config;
128         f->help = (struct ggo_help) {
129                 .short_help = prebuffer_filter_args_info_help,
130                 .detailed_help = prebuffer_filter_args_info_detailed_help
131         };
132 }