2 * Copyright (C) 2004-2009 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file ogg_afh.c Audio format handler for ogg vorbis files. */
11 #include <vorbis/codec.h>
18 /* Taken from decoder_example.c of libvorbis-1.2.3. */
19 static int read_vorbis_comment(ogg_sync_state *oss, ogg_stream_state *stream,
20 vorbis_info *vi, vorbis_comment *vc)
28 int ret = ogg_sync_pageout(oss, &page);
30 break; /* Need more data */
34 * We can ignore any errors here as they'll also become
35 * apparent at packetout.
37 ogg_stream_pagein(stream, &page);
39 ret = ogg_stream_packetout(stream, &packet);
43 return -E_STREAM_PACKETOUT;
44 ret = vorbis_synthesis_headerin(vi, vc,
55 static int read_vorbis_info(ogg_sync_state *oss, struct afh_info *afhi)
60 ogg_stream_state stream;
65 vorbis_info_init(&vi);
66 vorbis_comment_init(&vc);
68 ret = -E_SYNC_PAGEOUT;
69 if (ogg_sync_pageout(oss, &page) != 1)
72 ret = ogg_page_serialno(&page);
73 ogg_stream_init(&stream, ret);
75 ret = -E_STREAM_PAGEIN;
76 if (ogg_stream_pagein(&stream, &page) < 0)
79 ret = -E_STREAM_PACKETOUT;
80 if (ogg_stream_packetout(&stream, &packet) != 1)
84 if (vorbis_synthesis_headerin(&vi, &vc, &packet) < 0)
88 afhi->channels = vi.channels;
89 afhi->frequency = vi.rate;
90 afhi->bitrate = vi.bitrate_nominal / 1000;
91 PARA_DEBUG_LOG("channels: %i, sampling rate: %i, bitrate: %i\n",
92 afhi->channels, afhi->frequency, afhi->bitrate);
93 ret = read_vorbis_comment(oss, &stream, &vi, &vc);
96 taginfo = make_taginfo(
97 vorbis_comment_query(&vc, "title", 0),
98 vorbis_comment_query(&vc, "artist", 0),
99 vorbis_comment_query(&vc, "album", 0),
100 vorbis_comment_query(&vc, "year", 0),
101 vorbis_comment_query(&vc, "comment", 0)
103 PARA_DEBUG_LOG("tag info: %s\n", taginfo);
104 afhi->info_string = make_message("%s:\n%s",
105 status_item_list[SI_AUDIO_FILE_INFO], taginfo);
108 afhi->header_offset = 0;
109 afhi->header_len = oss->returned;
112 vorbis_info_clear(&vi);
113 vorbis_comment_clear(&vc);
114 ogg_stream_clear(&stream);
118 static void set_chunk_tv(int num_frames, int num_chunks, int frequency,
119 struct timeval *result)
121 uint64_t x = (uint64_t)num_frames * 1000 * 1000
122 / frequency / num_chunks;
124 result->tv_sec = x / 1000 / 1000;
125 result->tv_usec = x % (1000 * 1000);
126 PARA_INFO_LOG("%d chunks, chunk time: %lums\n", num_chunks,
130 /* Write tech data to given audio format handler struct. */
131 static int ogg_get_file_info(char *map, size_t numbytes, __a_unused int fd,
132 struct afh_info *afhi)
138 int ret, i, j, frames_per_chunk, ct_size;
139 long long unsigned num_frames = 0;
141 afhi->info_string = NULL;
144 buf = ogg_sync_buffer(&oss, len);
147 memcpy(buf, map, len);
149 if (ogg_sync_wrote(&oss, len) < 0)
151 ret = read_vorbis_info(&oss, afhi);
156 /* count ogg packages and get duration of the file */
157 for (i = 0; ogg_sync_pageseek(&oss, &op) > 0; i++)
158 num_frames = ogg_page_granulepos(&op);
159 PARA_INFO_LOG("%d pages, %llu frames\n", i, num_frames);
163 afhi->seconds_total = num_frames / afhi->frequency;
164 /* use roughly one page per chunk */
165 frames_per_chunk = num_frames / i;
166 PARA_INFO_LOG("%lu seconds, %d frames/chunk\n",
167 afhi->seconds_total, frames_per_chunk);
169 afhi->chunk_table = para_malloc(ct_size * sizeof(uint32_t));
170 afhi->chunk_table[0] = 0;
171 afhi->chunk_table[1] = afhi->header_len;
172 oss.returned = afhi->header_len;
174 for (i = 0, j = 1; ogg_sync_pageseek(&oss, &op) > 0; i++) {
175 int granule = ogg_page_granulepos(&op);
177 while (granule > j * frames_per_chunk) {
181 afhi->chunk_table = para_realloc(
183 ct_size * sizeof(uint32_t));
185 afhi->chunk_table[j] = oss.returned;
188 afhi->chunks_total = j;
189 set_chunk_tv(num_frames, j, afhi->frequency, &afhi->chunk_tv);
190 tv_scale(3, &afhi->chunk_tv, &afhi->eof_tv);
193 ogg_sync_clear(&oss);
197 static const char* ogg_suffixes[] = {"ogg", NULL};
200 * The init function of the ogg vorbis audio format handler.
202 * \param afh Pointer to the struct to initialize.
204 void ogg_init(struct audio_format_handler *afh)
206 afh->get_file_info = ogg_get_file_info,
207 afh->suffixes = ogg_suffixes;