Introduce get_header() for ogg/vorbis.
[paraslash.git] / ogg_afh.c
1 /*
2 * Copyright (C) 2004-2011 Andre Noll <maan@systemlinux.org>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file ogg_afh.c Audio format handler for ogg/vorbis files. */
8
9 #include <vorbis/codec.h>
10 #include <regex.h>
11
12 #include "para.h"
13 #include "afh.h"
14 #include "error.h"
15 #include "string.h"
16 #include "ogg_afh_common.h"
17
18 struct private_vorbis_data {
19 vorbis_info vi;
20 vorbis_comment vc;
21 };
22
23 static int vorbis_packet_callback(ogg_packet *packet, int packet_num,
24 __a_unused int serial, struct afh_info *afhi, void *private_data)
25 {
26 struct private_vorbis_data *pvd = private_data;
27
28 if (vorbis_synthesis_headerin(&pvd->vi, &pvd->vc, packet) < 0)
29 return -E_VORBIS;
30 if (packet_num == 0) {
31 if (pvd->vi.rate == 0)
32 return -E_VORBIS;
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);
38 return 1;
39 }
40 if (packet_num == 1)
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));
47 return 0;
48 }
49
50 static int ogg_vorbis_get_file_info(char *map, size_t numbytes, __a_unused int fd,
51 struct afh_info *afhi)
52 {
53 int ret;
54 struct private_vorbis_data pvd;
55 struct ogg_afh_callback_info vorbis_callback_info = {
56 .packet_callback = vorbis_packet_callback,
57 .private_data = &pvd,
58 };
59
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);
65 return ret;
66 }
67
68 struct vorbis_get_header_data {
69 ogg_stream_state os;
70 char *buf;
71 size_t len;
72 };
73
74 static void add_ogg_page(ogg_page *og, struct vorbis_get_header_data *vghd)
75 {
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;
79
80 memcpy(p, og->header, og->header_len);
81 memcpy(p + og->header_len, og->body, og->body_len);
82 vghd->buf = buf;
83 vghd->len = new_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);
86 }
87
88 static int vorbis_get_header_callback(ogg_packet *packet, int packet_num,
89 int serial, __a_unused struct afh_info *afhi, void *private_data)
90 {
91 int ret;
92 struct vorbis_get_header_data *vghd = private_data;
93 ogg_page og;
94 static unsigned char dummy_packet[] = {
95 0x03,
96 'v', 'o', 'r', 'b', 'i', 's',
97 0x06, 0x00, 0x00, 0x00,
98 'd', 'u', 'm', 'm', 'y', '\0',
99 0x00, 0x00, 0x00, 0x00, /* no comment :) */
100 0xff /* framing bit */
101 };
102
103 PARA_DEBUG_LOG("processing ogg packet #%d\n", packet_num);
104 if (packet_num > 2)
105 return 0;
106 if (packet_num == 0) {
107 ogg_stream_init(&vghd->os, serial);
108 ret = -E_OGG_PACKET_IN;
109 ret = ogg_stream_packetin(&vghd->os, packet);
110 if (ret < 0)
111 goto out;
112 ret = -E_OGG_STREAM_FLUSH;
113 if (ogg_stream_flush(&vghd->os, &og) == 0)
114 goto out;
115 add_ogg_page(&og, vghd);
116 return 1;
117 }
118 if (packet_num == 1) {
119 PARA_INFO_LOG("replacing metadata packet\n");
120 ogg_packet replacement = *packet;
121 replacement.packet = dummy_packet;
122 replacement.bytes = sizeof(dummy_packet);
123 ret = ogg_stream_packetin(&vghd->os, &replacement);
124 if (ret >= 0)
125 return 1;
126 ret = -E_OGG_PACKET_IN;
127 goto out;
128 }
129 ret = -E_OGG_PACKET_IN;
130 if (ogg_stream_packetin(&vghd->os, packet) < 0)
131 goto out;
132 ret = -E_OGG_STREAM_FLUSH;
133 if (ogg_stream_flush(&vghd->os, &og) == 0)
134 goto out;
135 add_ogg_page(&og, vghd);
136 ret = 0;
137 out:
138 ogg_stream_clear(&vghd->os);
139 return ret;
140 }
141
142 static void vorbis_get_header(void *map, size_t mapsize, char **buf,
143 size_t *len)
144 {
145 int ret;
146 struct vorbis_get_header_data vghd = {.len = 0};
147 struct ogg_afh_callback_info cb = {
148 .packet_callback = vorbis_get_header_callback,
149 .private_data = &vghd,
150 };
151
152 ret = ogg_get_file_info(map, mapsize, NULL, &cb);
153 if (ret < 0)
154 goto fail;
155 *buf = vghd.buf;
156 *len = vghd.len;
157 PARA_INFO_LOG("created %zu byte ogg vorbis header\n", *len);
158 return;
159 fail:
160 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
161 }
162
163 static const char* ogg_suffixes[] = {"ogg", NULL};
164
165 /**
166 * The init function of the ogg vorbis audio format handler.
167 *
168 * \param afh Pointer to the struct to initialize.
169 */
170 void ogg_init(struct audio_format_handler *afh)
171 {
172 afh->get_file_info = ogg_vorbis_get_file_info;
173 afh->get_header = vorbis_get_header;
174 afh->suffixes = ogg_suffixes;
175 }