2 * Copyright (C) 2004 Andre Noll <maan@tuebingen.mpg.de>
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 {
24 * Vorbis uses three header packets, all of which are required: the
25 * identification header, the comments header, and the setup header.
27 * The identification header identifies the bitstream as Vorbis. It contains
28 * the Vorbis version and simple audio characteristics of the stream such as
29 * sample rate and number of channels.
31 * The comment header includes user text comments (tags) and a vendor string
32 * for the application/library that produced the bitstream.
34 * The setup header includes extensive CODEC setup information as well as the
35 * complete VQ and Huffman codebooks needed for decoding.
37 static int vorbis_packet_callback(ogg_packet *packet, int packet_num,
38 __a_unused int serial, struct afh_info *afhi, void *private_data)
40 struct private_vorbis_data *pvd = private_data;
42 if (vorbis_synthesis_headerin(&pvd->vi, &pvd->vc, packet) < 0)
44 if (packet_num == 0) {
45 if (pvd->vi.rate == 0)
47 afhi->channels = pvd->vi.channels;
48 afhi->frequency = pvd->vi.rate;
49 afhi->bitrate = pvd->vi.bitrate_nominal / 1000;
50 PARA_DEBUG_LOG("channels: %i, sampling rate: %i, bitrate: %i\n",
51 afhi->channels, afhi->frequency, afhi->bitrate);
55 return 1; /* we also want to have packet #2 */
56 afhi->tags.artist = para_strdup(vorbis_comment_query(&pvd->vc, "artist", 0));
57 afhi->tags.title = para_strdup(vorbis_comment_query(&pvd->vc, "title", 0));
58 afhi->tags.album = para_strdup(vorbis_comment_query(&pvd->vc, "album", 0));
59 afhi->tags.year = para_strdup(vorbis_comment_query(&pvd->vc, "year", 0));
60 afhi->tags.comment = para_strdup(vorbis_comment_query(&pvd->vc, "comment", 0));
64 static int ogg_vorbis_get_file_info(char *map, size_t numbytes, __a_unused int fd,
65 struct afh_info *afhi)
68 struct private_vorbis_data pvd;
69 struct oac_callback_info vorbis_callback_info = {
70 .packet_callback = vorbis_packet_callback,
74 vorbis_info_init(&pvd.vi);
75 vorbis_comment_init(&pvd.vc);
76 ret = oac_get_file_info(map, numbytes, afhi, &vorbis_callback_info);
77 vorbis_info_clear(&pvd.vi);
78 vorbis_comment_clear(&pvd.vc);
82 static int vorbis_get_header_callback(ogg_packet *packet, int packet_num,
83 int serial, __a_unused struct afh_info *afhi, void *private_data)
86 struct oac_custom_header *h = private_data;
87 static unsigned char dummy_packet[] = {
89 'v', 'o', 'r', 'b', 'i', 's',
90 0x06, 0x00, 0x00, 0x00,
91 'd', 'u', 'm', 'm', 'y', '\0',
92 0x00, 0x00, 0x00, 0x00, /* no comment :) */
93 0xff /* framing bit */
96 PARA_DEBUG_LOG("processing ogg packet #%d\n", packet_num);
97 if (packet_num == 0) {
98 oac_custom_header_init(serial, h);
99 ret = oac_custom_header_append(packet, h);
102 oac_custom_header_flush(h);
105 if (packet_num == 1) {
106 ogg_packet replacement = *packet;
107 PARA_INFO_LOG("replacing metadata packet\n");
108 replacement.packet = dummy_packet;
109 replacement.bytes = sizeof(dummy_packet);
110 ret = oac_custom_header_append(&replacement, h);
111 return ret < 0? ret : 1;
113 assert(packet_num == 2);
114 ret = oac_custom_header_append(packet, h);
117 oac_custom_header_flush(h);
121 static void vorbis_get_header(void *map, size_t mapsize, char **buf,
125 struct oac_custom_header *h = oac_custom_header_new();
126 struct oac_callback_info cb = {
127 .packet_callback = vorbis_get_header_callback,
131 ret = oac_get_file_info(map, mapsize, NULL, &cb);
132 *len = oac_custom_header_get(buf, h);
134 PARA_ERROR_LOG("could not create ogg/vorbis header: %s\n",
135 para_strerror(-ret));
140 PARA_INFO_LOG("created %zu byte ogg vorbis header\n", *len);
143 static int vorbis_make_meta_packet(struct taginfo *tags, ogg_packet *result)
148 vorbis_comment_init(&vc);
149 vorbis_comment_add_tag(&vc, "artist", tags->artist);
150 vorbis_comment_add_tag(&vc, "title", tags->title);
151 vorbis_comment_add_tag(&vc, "album", tags->album);
152 vorbis_comment_add_tag(&vc, "year", tags->year);
153 vorbis_comment_add_tag(&vc, "comment", tags->comment);
154 ret = vorbis_commentheader_out(&vc, result);
155 vorbis_comment_clear(&vc);
157 return -E_VORBIS_COMMENTHEADER;
161 static int vorbis_rewrite_tags(const char *map, size_t mapsize,
162 struct taginfo *tags, int output_fd,
163 __a_unused const char *filename)
168 ret = vorbis_make_meta_packet(tags, &packet);
171 ret = oac_rewrite_tags(map, mapsize, output_fd, (char *)packet.packet,
177 static const char * const ogg_suffixes[] = {"ogg", NULL};
180 * The init function of the ogg vorbis audio format handler.
182 * \param afh Pointer to the struct to initialize.
184 void ogg_afh_init(struct audio_format_handler *afh)
186 afh->get_file_info = ogg_vorbis_get_file_info;
187 afh->get_header = vorbis_get_header;
188 afh->suffixes = ogg_suffixes;
189 afh->rewrite_tags = vorbis_rewrite_tags;