2 * Copyright (C) 2004-2010 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>
19 /* Taken from decoder_example.c of libvorbis-1.2.3. */
20 static int read_vorbis_comment(ogg_sync_state *oss, ogg_stream_state *stream,
21 vorbis_info *vi, vorbis_comment *vc)
29 int ret = ogg_sync_pageout(oss, &page);
31 break; /* Need more data */
35 * We can ignore any errors here as they'll also become
36 * apparent at packetout.
38 ogg_stream_pagein(stream, &page);
40 ret = ogg_stream_packetout(stream, &packet);
44 return -E_STREAM_PACKETOUT;
45 ret = vorbis_synthesis_headerin(vi, vc,
56 static int read_vorbis_info(ogg_sync_state *oss, struct afh_info *afhi)
61 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 afhi->tags.artist = para_strdup(vorbis_comment_query(&vc, "artist", 0));
97 afhi->tags.title = para_strdup(vorbis_comment_query(&vc, "title", 0));
98 afhi->tags.album = para_strdup(vorbis_comment_query(&vc, "album", 0));
99 afhi->tags.year = para_strdup(vorbis_comment_query(&vc, "year", 0));
100 afhi->tags.comment = para_strdup(vorbis_comment_query(&vc, "comment", 0));
102 afhi->header_offset = 0;
103 afhi->header_len = oss->returned;
106 vorbis_info_clear(&vi);
107 vorbis_comment_clear(&vc);
108 //ogg_stream_clear(&stream);
112 static void set_chunk_tv(int num_frames, int num_chunks, int frequency,
113 struct timeval *result)
115 uint64_t x = (uint64_t)num_frames * 1000 * 1000
116 / frequency / num_chunks;
118 result->tv_sec = x / 1000 / 1000;
119 result->tv_usec = x % (1000 * 1000);
120 PARA_INFO_LOG("%d chunks, chunk time: %lums\n", num_chunks,
124 /* Write tech data to given audio format handler struct. */
125 static int ogg_get_file_info(char *map, size_t numbytes, __a_unused int fd,
126 struct afh_info *afhi)
132 int ret, i, j, frames_per_chunk, ct_size;
133 long long unsigned num_frames = 0;
137 buf = ogg_sync_buffer(&oss, len);
140 memcpy(buf, map, len);
142 if (ogg_sync_wrote(&oss, len) < 0)
144 ret = read_vorbis_info(&oss, afhi);
149 /* count ogg packages and get duration of the file */
150 for (i = 0; ogg_sync_pageseek(&oss, &op) > 0; i++)
151 num_frames = ogg_page_granulepos(&op);
152 PARA_INFO_LOG("%d pages, %llu frames\n", i, num_frames);
156 afhi->seconds_total = num_frames / afhi->frequency;
157 /* use roughly one page per chunk */
158 frames_per_chunk = num_frames / i;
159 PARA_INFO_LOG("%lu seconds, %d frames/chunk\n",
160 afhi->seconds_total, frames_per_chunk);
162 afhi->chunk_table = para_malloc(ct_size * sizeof(uint32_t));
163 afhi->chunk_table[0] = 0;
164 afhi->chunk_table[1] = afhi->header_len;
165 oss.returned = afhi->header_len;
167 for (i = 0, j = 1; ogg_sync_pageseek(&oss, &op) > 0; i++) {
168 int granule = ogg_page_granulepos(&op);
170 while (granule > j * frames_per_chunk) {
174 afhi->chunk_table = para_realloc(
176 ct_size * sizeof(uint32_t));
178 afhi->chunk_table[j] = oss.returned;
181 afhi->chunks_total = j;
182 set_chunk_tv(num_frames, j, afhi->frequency, &afhi->chunk_tv);
185 ogg_sync_clear(&oss);
189 static const char* ogg_suffixes[] = {"ogg", NULL};
192 * The init function of the ogg vorbis audio format handler.
194 * \param afh Pointer to the struct to initialize.
196 void ogg_init(struct audio_format_handler *afh)
198 afh->get_file_info = ogg_get_file_info,
199 afh->suffixes = ogg_suffixes;