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 ogg_afh_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
= ogg_get_file_info(map
, numbytes
, afhi
, &vorbis_callback_info
);
77 vorbis_info_clear(&pvd
.vi
);
78 vorbis_comment_clear(&pvd
.vc
);
82 struct vorbis_get_header_data
{
88 static void add_ogg_page(ogg_page
*og
, struct vorbis_get_header_data
*vghd
)
90 size_t old_len
= vghd
->len
;
91 size_t new_len
= vghd
->len
+ og
->header_len
+ og
->body_len
;
92 char *buf
= para_realloc(vghd
->buf
, new_len
), *p
= buf
+ old_len
;
94 memcpy(p
, og
->header
, og
->header_len
);
95 memcpy(p
+ og
->header_len
, og
->body
, og
->body_len
);
98 PARA_DEBUG_LOG("header/body/old/new: %lu/%lu/%zu/%zu\n",
99 og
->header_len
, og
->body_len
, old_len
, new_len
);
102 static int vorbis_get_header_callback(ogg_packet
*packet
, int packet_num
,
103 int serial
, __a_unused
struct afh_info
*afhi
, void *private_data
)
106 struct vorbis_get_header_data
*vghd
= private_data
;
108 static unsigned char dummy_packet
[] = {
110 'v', 'o', 'r', 'b', 'i', 's',
111 0x06, 0x00, 0x00, 0x00,
112 'd', 'u', 'm', 'm', 'y', '\0',
113 0x00, 0x00, 0x00, 0x00, /* no comment :) */
114 0xff /* framing bit */
117 PARA_DEBUG_LOG("processing ogg packet #%d\n", packet_num
);
120 if (packet_num
== 0) {
121 ogg_stream_init(&vghd
->os
, serial
);
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 ogg_packet replacement
= *packet
;
133 PARA_INFO_LOG("replacing metadata packet\n");
134 replacement
.packet
= dummy_packet
;
135 replacement
.bytes
= sizeof(dummy_packet
);
136 ret
= ogg_stream_packetin(&vghd
->os
, &replacement
);
139 ret
= -E_OGG_PACKET_IN
;
142 ret
= -E_OGG_PACKET_IN
;
143 if (ogg_stream_packetin(&vghd
->os
, packet
) < 0)
145 while (ogg_stream_flush(&vghd
->os
, &og
))
146 add_ogg_page(&og
, vghd
);
149 ogg_stream_clear(&vghd
->os
);
153 static void vorbis_get_header(void *map
, size_t mapsize
, char **buf
,
157 struct vorbis_get_header_data vghd
= {.len
= 0};
158 struct ogg_afh_callback_info cb
= {
159 .packet_callback
= vorbis_get_header_callback
,
160 .private_data
= &vghd
,
163 ret
= ogg_get_file_info(map
, mapsize
, NULL
, &cb
);
168 PARA_INFO_LOG("created %zu byte ogg vorbis header\n", *len
);
171 PARA_ERROR_LOG("%s\n", para_strerror(-ret
));
174 static int vorbis_make_meta_packet(struct taginfo
*tags
, ogg_packet
*result
)
179 vorbis_comment_init(&vc
);
180 vorbis_comment_add_tag(&vc
, "artist", tags
->artist
);
181 vorbis_comment_add_tag(&vc
, "title", tags
->title
);
182 vorbis_comment_add_tag(&vc
, "album", tags
->album
);
183 vorbis_comment_add_tag(&vc
, "year", tags
->year
);
184 vorbis_comment_add_tag(&vc
, "comment", tags
->comment
);
185 ret
= vorbis_commentheader_out(&vc
, result
);
186 vorbis_comment_clear(&vc
);
188 return -E_VORBIS_COMMENTHEADER
;
192 static int vorbis_rewrite_tags(const char *map
, size_t mapsize
,
193 struct taginfo
*tags
, int output_fd
,
194 __a_unused
const char *filename
)
199 ret
= vorbis_make_meta_packet(tags
, &packet
);
202 ret
= ogg_rewrite_tags(map
, mapsize
, output_fd
, (char *)packet
.packet
,
208 static const char * const ogg_suffixes
[] = {"ogg", NULL
};
211 * The init function of the ogg vorbis audio format handler.
213 * \param afh Pointer to the struct to initialize.
215 void ogg_init(struct audio_format_handler
*afh
)
217 afh
->get_file_info
= ogg_vorbis_get_file_info
;
218 afh
->get_header
= vorbis_get_header
;
219 afh
->suffixes
= ogg_suffixes
;
220 afh
->rewrite_tags
= vorbis_rewrite_tags
;