2 * Copyright (C) 2004-2011 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. */
9 #include <vorbis/codec.h>
16 #include "ogg_afh_common.h"
18 struct private_vorbis_data {
23 static int vorbis_packet_callback(ogg_packet *packet, int packet_num,
24 __a_unused int serial, struct afh_info *afhi, void *private_data)
26 struct private_vorbis_data *pvd = private_data;
28 if (vorbis_synthesis_headerin(&pvd->vi, &pvd->vc, packet) < 0)
30 if (packet_num == 0) {
31 if (pvd->vi.rate == 0)
33 afhi->channels = pvd->vi.channels;
34 afhi->frequency = pvd->vi.rate;
35 afhi->bitrate = pvd->vi.bitrate_nominal / 1000;
36 PARA_DEBUG_LOG("channels: %i, sampling rate: %i, bitrate: %i\n",
37 afhi->channels, afhi->frequency, afhi->bitrate);
41 return 1; /* we also want to have packet #2 */
42 afhi->tags.artist = para_strdup(vorbis_comment_query(&pvd->vc, "artist", 0));
43 afhi->tags.title = para_strdup(vorbis_comment_query(&pvd->vc, "title", 0));
44 afhi->tags.album = para_strdup(vorbis_comment_query(&pvd->vc, "album", 0));
45 afhi->tags.year = para_strdup(vorbis_comment_query(&pvd->vc, "year", 0));
46 afhi->tags.comment = para_strdup(vorbis_comment_query(&pvd->vc, "comment", 0));
50 static int ogg_vorbis_get_file_info(char *map, size_t numbytes, __a_unused int fd,
51 struct afh_info *afhi)
54 struct private_vorbis_data pvd;
55 struct ogg_afh_callback_info vorbis_callback_info = {
56 .packet_callback = vorbis_packet_callback,
60 vorbis_info_init(&pvd.vi);
61 vorbis_comment_init(&pvd.vc);
62 ret = ogg_get_file_info(map, numbytes, afhi, &vorbis_callback_info);
63 vorbis_info_clear(&pvd.vi);
64 vorbis_comment_clear(&pvd.vc);
68 struct vorbis_get_header_data {
74 static void add_ogg_page(ogg_page *og, struct vorbis_get_header_data *vghd)
76 size_t old_len = vghd->len;
77 size_t new_len = vghd->len + og->header_len + og->body_len;
78 char *buf = para_realloc(vghd->buf, new_len), *p = buf + old_len;
80 memcpy(p, og->header, og->header_len);
81 memcpy(p + og->header_len, og->body, og->body_len);
84 PARA_DEBUG_LOG("header/body/old/new: %lu/%lu/%zu/%zu\n",
85 og->header_len, og->body_len, old_len, new_len);
89 * Process the first three ogg packets.
91 * This creates chunk zero (the audio file header) from the first three ogg
92 * packets of the input file with metadata (vorbis comments) stripped off. Page
93 * 0 of the input file always contains only ogg packet #0 while page 1 usually
94 * contains both packets 1 (comments) and 2 (setup). However, we always create
95 * an separate ogg page for each packet to circumvent a bug in older libogg
96 * versions which causes too little data being copied to the second ogg page.
97 * This affects at least Ubuntu Hardy, and there is no real disadvantage in
98 * creating three pages instead of two.
100 static int vorbis_get_header_callback(ogg_packet *packet, int packet_num,
101 int serial, __a_unused struct afh_info *afhi, void *private_data)
104 struct vorbis_get_header_data *vghd = private_data;
106 static unsigned char dummy_packet[] = {
108 'v', 'o', 'r', 'b', 'i', 's',
109 0x06, 0x00, 0x00, 0x00,
110 'd', 'u', 'm', 'm', 'y', '\0',
111 0x00, 0x00, 0x00, 0x00, /* no comment :) */
112 0xff /* framing bit */
115 PARA_DEBUG_LOG("processing ogg packet #%d (%li bytes)\n",
116 packet_num, packet->bytes);
119 if (packet_num == 0) {
120 ogg_stream_init(&vghd->os, serial);
121 ret = -E_OGG_PACKET_IN;
122 ret = ogg_stream_packetin(&vghd->os, packet);
125 ret = -E_OGG_STREAM_FLUSH;
126 if (ogg_stream_flush(&vghd->os, &og) == 0)
128 add_ogg_page(&og, vghd);
131 if (packet_num == 1) {
132 PARA_INFO_LOG("replacing metadata packet (saved %ld bytes)\n",
133 packet->bytes - sizeof(dummy_packet));
134 ogg_packet replacement = *packet;
135 replacement.packet = dummy_packet;
136 replacement.bytes = sizeof(dummy_packet);
137 ret = -E_OGG_PACKET_IN;
138 if (ogg_stream_packetin(&vghd->os, &replacement) < 0)
140 ret = -E_OGG_STREAM_FLUSH;
141 if (ogg_stream_flush(&vghd->os, &og) == 0)
143 add_ogg_page(&og, vghd);
146 ret = -E_OGG_PACKET_IN;
147 if (ogg_stream_packetin(&vghd->os, packet) < 0)
149 ret = -E_OGG_STREAM_FLUSH;
150 if (ogg_stream_flush(&vghd->os, &og) == 0)
152 add_ogg_page(&og, vghd);
155 ogg_stream_clear(&vghd->os);
159 static void vorbis_get_header(void *map, size_t mapsize, char **buf,
163 struct vorbis_get_header_data vghd = {.len = 0};
164 struct ogg_afh_callback_info cb = {
165 .packet_callback = vorbis_get_header_callback,
166 .private_data = &vghd,
169 ret = ogg_get_file_info(map, mapsize, NULL, &cb);
174 PARA_INFO_LOG("created %zu byte ogg/vorbis header chunk\n", *len);
177 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
180 static const char* ogg_suffixes[] = {"ogg", NULL};
183 * The init function of the ogg vorbis audio format handler.
185 * \param afh Pointer to the struct to initialize.
187 void ogg_init(struct audio_format_handler *afh)
189 afh->get_file_info = ogg_vorbis_get_file_info;
190 afh->get_header = vorbis_get_header;
191 afh->suffixes = ogg_suffixes;