1 /* Copyright (C) 2004 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
3 /** \file ogg_afh.c Audio format handler for ogg/vorbis files. */
5 #include <vorbis/codec.h>
12 #include "ogg_afh_common.h"
14 struct private_vorbis_data
{
20 * Vorbis uses three header packets, all of which are required: the
21 * identification header, the comments header, and the setup header.
23 * The identification header identifies the bitstream as Vorbis. It contains
24 * the Vorbis version and simple audio characteristics of the stream such as
25 * sample rate and number of channels.
27 * The comment header includes user text comments (tags) and a vendor string
28 * for the application/library that produced the bitstream.
30 * The setup header includes extensive CODEC setup information as well as the
31 * complete VQ and Huffman codebooks needed for decoding.
33 static int vorbis_packet_callback(ogg_packet
*packet
, int packet_num
,
34 __a_unused
int serial
, struct afh_info
*afhi
, void *private_data
)
36 struct private_vorbis_data
*pvd
= private_data
;
38 if (vorbis_synthesis_headerin(&pvd
->vi
, &pvd
->vc
, packet
) < 0)
40 if (packet_num
== 0) {
41 if (pvd
->vi
.rate
== 0)
43 afhi
->channels
= pvd
->vi
.channels
;
44 afhi
->frequency
= pvd
->vi
.rate
;
45 afhi
->bitrate
= pvd
->vi
.bitrate_nominal
/ 1000;
46 PARA_DEBUG_LOG("channels: %i, sampling rate: %i, bitrate: %i\n",
47 afhi
->channels
, afhi
->frequency
, afhi
->bitrate
);
51 return 1; /* we also want to have packet #2 */
52 afhi
->tags
.artist
= para_strdup(vorbis_comment_query(&pvd
->vc
, "artist", 0));
53 afhi
->tags
.title
= para_strdup(vorbis_comment_query(&pvd
->vc
, "title", 0));
54 afhi
->tags
.album
= para_strdup(vorbis_comment_query(&pvd
->vc
, "album", 0));
55 afhi
->tags
.year
= para_strdup(vorbis_comment_query(&pvd
->vc
, "year", 0));
56 afhi
->tags
.comment
= para_strdup(vorbis_comment_query(&pvd
->vc
, "comment", 0));
60 static int ogg_vorbis_get_file_info(char *map
, size_t numbytes
, __a_unused
int fd
,
61 struct afh_info
*afhi
)
64 struct private_vorbis_data pvd
;
65 struct oac_callback_info vorbis_callback_info
= {
66 .packet_callback
= vorbis_packet_callback
,
70 vorbis_info_init(&pvd
.vi
);
71 vorbis_comment_init(&pvd
.vc
);
72 ret
= oac_get_file_info(map
, numbytes
, afhi
, &vorbis_callback_info
);
73 vorbis_info_clear(&pvd
.vi
);
74 vorbis_comment_clear(&pvd
.vc
);
78 static int vorbis_get_header_callback(ogg_packet
*packet
, int packet_num
,
79 int serial
, __a_unused
struct afh_info
*afhi
, void *private_data
)
82 struct oac_custom_header
*h
= private_data
;
83 static unsigned char dummy_packet
[] = {
85 'v', 'o', 'r', 'b', 'i', 's',
86 0x06, 0x00, 0x00, 0x00,
87 'd', 'u', 'm', 'm', 'y', '\0',
88 0x00, 0x00, 0x00, 0x00, /* no comment :) */
89 0xff /* framing bit */
92 PARA_DEBUG_LOG("processing ogg packet #%d\n", packet_num
);
93 if (packet_num
== 0) {
94 oac_custom_header_init(serial
, h
);
95 ret
= oac_custom_header_append(packet
, h
);
98 oac_custom_header_flush(h
);
101 if (packet_num
== 1) {
102 ogg_packet replacement
= *packet
;
103 PARA_INFO_LOG("replacing metadata packet\n");
104 replacement
.packet
= dummy_packet
;
105 replacement
.bytes
= sizeof(dummy_packet
);
106 ret
= oac_custom_header_append(&replacement
, h
);
107 return ret
< 0? ret
: 1;
109 assert(packet_num
== 2);
110 ret
= oac_custom_header_append(packet
, h
);
113 oac_custom_header_flush(h
);
117 static void vorbis_get_header(void *map
, size_t mapsize
, char **buf
,
121 struct oac_custom_header
*h
= oac_custom_header_new();
122 struct oac_callback_info cb
= {
123 .packet_callback
= vorbis_get_header_callback
,
127 ret
= oac_get_file_info(map
, mapsize
, NULL
, &cb
);
128 *len
= oac_custom_header_get(buf
, h
);
130 PARA_ERROR_LOG("could not create ogg/vorbis header: %s\n",
131 para_strerror(-ret
));
136 PARA_INFO_LOG("created %zu byte ogg vorbis header\n", *len
);
139 static int vorbis_make_meta_packet(struct taginfo
*tags
, ogg_packet
*result
)
144 vorbis_comment_init(&vc
);
145 vorbis_comment_add_tag(&vc
, "artist", tags
->artist
);
146 vorbis_comment_add_tag(&vc
, "title", tags
->title
);
147 vorbis_comment_add_tag(&vc
, "album", tags
->album
);
148 vorbis_comment_add_tag(&vc
, "year", tags
->year
);
149 vorbis_comment_add_tag(&vc
, "comment", tags
->comment
);
150 ret
= vorbis_commentheader_out(&vc
, result
);
151 vorbis_comment_clear(&vc
);
153 return -E_VORBIS_COMMENTHEADER
;
157 static int vorbis_rewrite_tags(const char *map
, size_t mapsize
,
158 struct taginfo
*tags
, int output_fd
,
159 __a_unused
const char *filename
)
164 ret
= vorbis_make_meta_packet(tags
, &packet
);
167 ret
= oac_rewrite_tags(map
, mapsize
, output_fd
, (char *)packet
.packet
,
173 static const char * const ogg_suffixes
[] = {"ogg", NULL
};
176 * The ogg vorbis audio format handler.
178 * It should actually be called vorbis because the ogg container format is also
179 * employed for the speex and flac codecs. Both the ogg and the vorbis
180 * libraries must be installed to get this audio format handler.
182 const struct audio_format_handler ogg_afh
= {
183 .get_file_info
= ogg_vorbis_get_file_info
,
184 .get_header
= vorbis_get_header
,
185 .suffixes
= ogg_suffixes
,
186 .rewrite_tags
= vorbis_rewrite_tags