1 | //codecfilter_vorbis.c: |
---|
2 | |
---|
3 | #include "roard.h" |
---|
4 | |
---|
5 | int cf_vorbis_open(CODECFILTER_USERDATA_T * inst, int codec, |
---|
6 | struct roar_stream_server * info, |
---|
7 | struct roar_codecfilter * filter) { |
---|
8 | struct codecfilter_vorbis_inst * self = malloc(sizeof(struct codecfilter_vorbis_inst)); |
---|
9 | |
---|
10 | if ( !self ) |
---|
11 | return -1; |
---|
12 | |
---|
13 | self->current_section = -1; |
---|
14 | self->last_section = -1; |
---|
15 | self->opened = 0; |
---|
16 | self->stream = info; |
---|
17 | // self->outlen = ROAR_OUTPUT_BUFFER_SAMPLES * s->info.channels * s->info.bits / 8; // optimal size |
---|
18 | |
---|
19 | if ( (self->in = fdopen(((struct roar_stream*)info)->fh, "r")) == NULL ) { |
---|
20 | free((void*)self); |
---|
21 | return -1; |
---|
22 | } |
---|
23 | |
---|
24 | *inst = (CODECFILTER_USERDATA_T) self; |
---|
25 | |
---|
26 | ((struct roar_stream*)info)->info.codec = ROAR_CODEC_DEFAULT; |
---|
27 | |
---|
28 | return 0; |
---|
29 | } |
---|
30 | |
---|
31 | int cf_vorbis_close(CODECFILTER_USERDATA_T inst) { |
---|
32 | struct codecfilter_vorbis_inst * self = (struct codecfilter_vorbis_inst *) inst; |
---|
33 | |
---|
34 | if ( !inst ) |
---|
35 | return -1; |
---|
36 | |
---|
37 | ov_clear(&(self->vf)); |
---|
38 | |
---|
39 | free(inst); |
---|
40 | return 0; |
---|
41 | } |
---|
42 | |
---|
43 | int cf_vorbis_read(CODECFILTER_USERDATA_T inst, char * buf, int len) { |
---|
44 | struct codecfilter_vorbis_inst * self = (struct codecfilter_vorbis_inst *) inst; |
---|
45 | long r; |
---|
46 | long todo = len; |
---|
47 | long done = 0; |
---|
48 | |
---|
49 | // printf("cf_vorbis_read(inst=%p, buf=%p, len=%i) = ?\n", inst, buf, len); |
---|
50 | |
---|
51 | self->opened++; |
---|
52 | if ( self->opened == 16 ) { |
---|
53 | //printf("cf_vorbis_read(*): opening...\n"); |
---|
54 | if ( ov_open(self->in, &(self->vf), NULL, 0) < 0 ) { |
---|
55 | free((void*)self); |
---|
56 | return 0; |
---|
57 | } |
---|
58 | } |
---|
59 | |
---|
60 | if ( self->opened < 16 ) { |
---|
61 | return -1; |
---|
62 | } |
---|
63 | |
---|
64 | while (todo) { |
---|
65 | r = ov_read(&(self->vf), buf+done, todo, 0, 2, 1, &(self->current_section)); |
---|
66 | if ( r < 1 ) { |
---|
67 | break; |
---|
68 | } else { |
---|
69 | if ( self->last_section != self->current_section ) |
---|
70 | if ( cf_vorbis_update_stream(self) == -1 ) |
---|
71 | return 0; |
---|
72 | |
---|
73 | self->last_section = self->current_section; |
---|
74 | todo -= r; |
---|
75 | done += r; |
---|
76 | } |
---|
77 | } |
---|
78 | |
---|
79 | //printf("ov_read(*) = %i\n", done); |
---|
80 | |
---|
81 | if ( done == 0 ) { |
---|
82 | // do some EOF handling... |
---|
83 | return 0; |
---|
84 | } else { |
---|
85 | return len; |
---|
86 | } |
---|
87 | } |
---|
88 | |
---|
89 | int cf_vorbis_update_stream (struct codecfilter_vorbis_inst * self) { |
---|
90 | vorbis_info *vi = ov_info(&(self->vf), -1); |
---|
91 | char **ptr = ov_comment(&(self->vf), -1)->user_comments; |
---|
92 | char key[80] = {0}, value[80] = {0}; |
---|
93 | struct roar_stream * s = (struct roar_stream *) self->stream; |
---|
94 | int type; |
---|
95 | int j, h = 0; |
---|
96 | float rpg_track = 0, rpg_album = 0; |
---|
97 | |
---|
98 | s->info.channels = vi->channels; |
---|
99 | s->info.rate = vi->rate; |
---|
100 | s->info.bits = 16; |
---|
101 | s->info.codec = ROAR_CODEC_DEFAULT; |
---|
102 | |
---|
103 | stream_meta_clear(s->id); |
---|
104 | |
---|
105 | while(*ptr){ |
---|
106 | for (j = 0; (*ptr)[j] != 0 && (*ptr)[j] != '='; j++) |
---|
107 | key[j] = (*ptr)[j]; |
---|
108 | key[j] = 0; |
---|
109 | |
---|
110 | for (j++, h = 0; (*ptr)[j] != 0 && (*ptr)[j] != '='; j++) |
---|
111 | value[h++] = (*ptr)[j]; |
---|
112 | value[h] = 0; |
---|
113 | |
---|
114 | type = roar_meta_inttype(key); |
---|
115 | if ( type != -1 ) |
---|
116 | stream_meta_set(s->id, type, "", value); |
---|
117 | |
---|
118 | ROAR_DBG("cf_vorbis_update_stream(*): Meta %-16s: %s", key, value); |
---|
119 | |
---|
120 | if ( strcmp(key, "REPLAYGAIN_TRACK_PEAK") == 0 ) { |
---|
121 | rpg_track = 1/atof(value); |
---|
122 | /* |
---|
123 | } else if ( strcmp(key, "REPLAYGAIN_TRACK_GAIN") == 0 ) { |
---|
124 | rpg_track = powf(10, atof(value)/20); |
---|
125 | */ |
---|
126 | } else if ( strcmp(key, "REPLAYGAIN_ALBUM_PEAK") == 0 ) { |
---|
127 | rpg_album = 1/atof(value); |
---|
128 | /* |
---|
129 | } else if ( strcmp(key, "REPLAYGAIN_ALBUM_GAIN") == 0 ) { |
---|
130 | rpg_album = powf(10, atof(value)/20); |
---|
131 | */ |
---|
132 | } |
---|
133 | |
---|
134 | ++ptr; |
---|
135 | } |
---|
136 | |
---|
137 | if ( rpg_album ) { |
---|
138 | self->stream->mixer.rpg_div = 2718; // = int(exp(1)*1000) |
---|
139 | self->stream->mixer.rpg_mul = (float)rpg_album*2718; |
---|
140 | } else if ( rpg_track ) { |
---|
141 | self->stream->mixer.rpg_div = 2718; |
---|
142 | self->stream->mixer.rpg_mul = (float)rpg_track*2718; |
---|
143 | } |
---|
144 | |
---|
145 | //printf("RPG: mul=%i, div=%i\n", self->stream->mixer.rpg_mul, self->stream->mixer.rpg_div); |
---|
146 | return 0; |
---|
147 | } |
---|
148 | |
---|
149 | //ll |
---|