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