]> git.tuebingen.mpg.de Git - paraslash.git/blob - amp_filter.c
mp3dec: Only proceed decoding if at least 16K are available.
[paraslash.git] / amp_filter.c
1 /*
2  * Copyright (C) 2008 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file amp_filter.c Paraslash's amplify filter. */
8
9 #include "para.h"
10 #include "amp_filter.cmdline.h"
11 #include "list.h"
12 #include "sched.h"
13 #include "filter.h"
14 #include "string.h"
15 #include "error.h"
16
17 /** The size of the output data buffer. */
18 #define AMP_CHUNK_SIZE 40960
19
20 extern char *stat_item_values[NUM_STAT_ITEMS];
21
22 /** Data specific to the amplify filter. */
23 struct private_amp_data {
24         /** Points to the configuration data for this instance of this filter. */
25         struct amp_filter_args_info *conf;
26         /** Amplification factor. */
27         unsigned amp;
28 };
29
30 static ssize_t amp_convert(char *inbuf, size_t inbuf_len, struct filter_node *fn)
31 {
32         size_t i, length = PARA_MIN((inbuf_len / 2) * 2,
33                 (fn->bufsize - fn->loaded) / 2 * 2);
34         struct private_amp_data *pad = fn->private_data;
35         int16_t *ip = (int16_t *)inbuf, *op = (int16_t *)(fn->buf + fn->loaded);
36
37         if (!length)
38                 return 0;
39         for (i = 0; i < length / 2; i++) {
40                 int x = (PARA_ABS(*ip) * (64 + pad->amp)) >> 6;
41                 *op++ = *ip++ > 0? PARA_MIN(x, 32767) : PARA_MAX(-x, -32768);
42         }
43         fn->loaded += length;
44         return length;
45 }
46
47 static void amp_close(struct filter_node *fn)
48 {
49         free(fn->private_data);
50         free(fn->buf);
51 }
52
53 static int amp_parse_config(int argc, char **argv, void **config)
54 {
55         struct amp_filter_args_info *amp_conf = para_calloc(sizeof(*amp_conf));
56         int ret = -E_AMP_SYNTAX;
57
58         if (amp_cmdline_parser(argc, argv, amp_conf))
59                 goto err;
60         ret = -ERRNO_TO_PARA_ERROR(EINVAL);
61         if (amp_conf->amp_arg < 0)
62                 goto err;
63         PARA_NOTICE_LOG("amplification: %u (scaling factor: %1.2f)\n",
64                 amp_conf->amp_arg, amp_conf->amp_arg / 64.0 + 1.0);
65         *config = amp_conf;
66         return 1;
67 err:
68         free(amp_conf);
69         return ret;
70 }
71
72 static void amp_open(struct filter_node *fn)
73 {
74         struct private_amp_data *pad = para_calloc(sizeof(*pad));
75
76         pad->conf = fn->conf;
77         fn->private_data = pad;
78         if (!pad->conf->amp_given && stat_item_values[SI_AMPLIFICATION]) {
79                 int i = SI_AMPLIFICATION;
80                 char *s = stat_item_values[i] + strlen(status_item_list[i]) + 1;
81                 sscanf(s, "%u", &pad->amp);
82         } else
83                 pad->amp = pad->conf->amp_arg;
84         fn->bufsize = AMP_CHUNK_SIZE;
85         fn->buf = para_malloc(fn->bufsize);
86 }
87
88 /**
89  * The init function of the amplify filter.
90  *
91  * \param f Pointer to the struct to initialize.
92  */
93 void amp_filter_init(struct filter *f)
94 {
95         f->open = amp_open;
96         f->close = amp_close;
97         f->convert = amp_convert;
98         f->print_help = amp_cmdline_parser_print_help;
99         f->parse_config = amp_parse_config;
100 }