server: Deplete user list on exit.
[paraslash.git] / ogg_afh.c
1 /* Copyright (C) 2004 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
2
3 /** \file ogg_afh.c Audio format handler for ogg/vorbis files. */
4
5 #include <vorbis/codec.h>
6 #include <regex.h>
7
8 #include "para.h"
9 #include "afh.h"
10 #include "error.h"
11 #include "string.h"
12 #include "ogg_afh_common.h"
13
14 struct private_vorbis_data {
15 vorbis_info vi;
16 vorbis_comment vc;
17 };
18
19 /*
20 * Vorbis uses three header packets, all of which are required: the
21 * identification header, the comments header, and the setup header.
22 *
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.
26 *
27 * The comment header includes user text comments (tags) and a vendor string
28 * for the application/library that produced the bitstream.
29 *
30 * The setup header includes extensive CODEC setup information as well as the
31 * complete VQ and Huffman codebooks needed for decoding.
32 */
33 static int vorbis_packet_callback(ogg_packet *packet, int packet_num,
34 __a_unused int serial, struct afh_info *afhi, void *private_data)
35 {
36 struct private_vorbis_data *pvd = private_data;
37
38 if (vorbis_synthesis_headerin(&pvd->vi, &pvd->vc, packet) < 0)
39 return -E_VORBIS;
40 if (packet_num == 0) {
41 if (pvd->vi.rate == 0)
42 return -E_VORBIS;
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);
48 return 1;
49 }
50 if (packet_num == 1)
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));
57 return 0;
58 }
59
60 static int ogg_vorbis_get_file_info(char *map, size_t numbytes, __a_unused int fd,
61 struct afh_info *afhi)
62 {
63 int ret;
64 struct private_vorbis_data pvd;
65 struct oac_callback_info vorbis_callback_info = {
66 .packet_callback = vorbis_packet_callback,
67 .private_data = &pvd,
68 };
69
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);
75 return ret;
76 }
77
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)
80 {
81 int ret;
82 struct oac_custom_header *h = private_data;
83 static unsigned char dummy_packet[] = {
84 0x03,
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 */
90 };
91
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);
96 if (ret < 0)
97 return ret;
98 oac_custom_header_flush(h);
99 return 1;
100 }
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;
108 }
109 assert(packet_num == 2);
110 ret = oac_custom_header_append(packet, h);
111 if (ret < 0)
112 return ret;
113 oac_custom_header_flush(h);
114 return 0;
115 }
116
117 static void vorbis_get_header(void *map, size_t mapsize, char **buf,
118 size_t *len)
119 {
120 int ret;
121 struct oac_custom_header *h = oac_custom_header_new();
122 struct oac_callback_info cb = {
123 .packet_callback = vorbis_get_header_callback,
124 .private_data = h,
125 };
126
127 ret = oac_get_file_info(map, mapsize, NULL, &cb);
128 *len = oac_custom_header_get(buf, h);
129 if (ret < 0) {
130 PARA_ERROR_LOG("could not create ogg/vorbis header: %s\n",
131 para_strerror(-ret));
132 free(*buf);
133 *buf = NULL;
134 *len = 0;
135 } else
136 PARA_INFO_LOG("created %zu byte ogg vorbis header\n", *len);
137 }
138
139 static int vorbis_make_meta_packet(struct taginfo *tags, ogg_packet *result)
140 {
141 vorbis_comment vc;
142 int ret;
143
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);
152 if (ret != 0)
153 return -E_VORBIS_COMMENTHEADER;
154 return 1;
155 }
156
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)
160 {
161 int ret;
162 ogg_packet packet;
163
164 ret = vorbis_make_meta_packet(tags, &packet);
165 if (ret < 0)
166 return ret;
167 ret = oac_rewrite_tags(map, mapsize, output_fd, (char *)packet.packet,
168 packet.bytes);
169 free(packet.packet);
170 return ret;
171 }
172
173 static const char * const ogg_suffixes[] = {"ogg", NULL};
174
175 /**
176 * The init function of the ogg vorbis audio format handler.
177 *
178 * \param afh Pointer to the struct to initialize.
179 */
180 void ogg_afh_init(struct audio_format_handler *afh)
181 {
182 afh->get_file_info = ogg_vorbis_get_file_info;
183 afh->get_header = vorbis_get_header;
184 afh->suffixes = ogg_suffixes;
185 afh->rewrite_tags = vorbis_rewrite_tags;
186 }