shout_ogg.c 7.18 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/* the Music Player Daemon (MPD)
 * Copyright (C) 2003-2007 by Warren Dukes (warren.dukes@gmail.com)
 * This project's homepage is: http://www.musicpd.org
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

19
#include "shout_plugin.h"
20

21
#include <vorbis/vorbisenc.h>
22
#include <stdlib.h>
23

24
struct ogg_vorbis_data {
25 26 27 28 29 30 31 32 33 34 35
	ogg_stream_state os;
	ogg_page og;
	ogg_packet op;
	ogg_packet header_main;
	ogg_packet header_comments;
	ogg_packet header_codebooks;

	vorbis_dsp_state vd;
	vorbis_block vb;
	vorbis_info vi;
	vorbis_comment vc;
36
};
37

38
static void add_tag(struct ogg_vorbis_data *od, const char *name, char *value)
39 40
{
	if (value) {
41 42 43 44
		union {
			const char *in;
			char *out;
		} u = { .in = name };
45
		vorbis_comment_add_tag(&od->vc, u.out, value);
46 47 48
	}
}

49
static void copy_tag_to_vorbis_comment(struct shout_data *sd)
50
{
51
	struct ogg_vorbis_data *od = (struct ogg_vorbis_data *)sd->encoder_data;
52

53 54 55 56 57 58
	if (sd->tag) {
		int i;

		for (i = 0; i < sd->tag->numOfItems; i++) {
			switch (sd->tag->items[i]->type) {
			case TAG_ITEM_ARTIST:
59
				add_tag(od, "ARTIST", sd->tag->items[i]->value);
60 61
				break;
			case TAG_ITEM_ALBUM:
62
				add_tag(od, "ALBUM", sd->tag->items[i]->value);
63 64
				break;
			case TAG_ITEM_TITLE:
65
				add_tag(od, "TITLE", sd->tag->items[i]->value);
66
				break;
67

68 69 70 71 72 73 74
			default:
				break;
			}
		}
	}
}

75
static int copy_ogg_buffer_to_shout_buffer(ogg_page *og,
76
					   struct shout_buffer *buf)
77
{
78
	if ((size_t)og->header_len + (size_t)og->body_len > sizeof(buf->data)) {
79
		g_warning("%s: not enough buffer space!\n", __func__);
80 81 82
		return -1;
	}

83 84 85
	memcpy(buf->data, og->header, og->header_len);
	memcpy(buf->data + og->header_len, og->body, og->body_len);
	buf->len = og->header_len + og->body_len;
86 87 88 89 90 91

	return 0;
}

static int flush_ogg_buffer(struct shout_data *sd)
{
92 93
	struct shout_buffer *buf = &sd->buf;
	struct ogg_vorbis_data *od = (struct ogg_vorbis_data *)sd->encoder_data;
94 95
	int ret = 0;

96 97
	if (ogg_stream_flush(&od->os, &od->og))
		ret = copy_ogg_buffer_to_shout_buffer(&od->og, buf);
98 99 100 101

	return ret;
}

102
static int send_ogg_vorbis_header(struct shout_data *sd)
103
{
104
	struct ogg_vorbis_data *od = (struct ogg_vorbis_data *)sd->encoder_data;
105 106 107 108 109

	vorbis_analysis_headerout(&od->vd, &od->vc,
				  &od->header_main,
				  &od->header_comments,
				  &od->header_codebooks);
110

111 112 113
	ogg_stream_packetin(&od->os, &od->header_main);
	ogg_stream_packetin(&od->os, &od->header_comments);
	ogg_stream_packetin(&od->os, &od->header_codebooks);
114 115

	return flush_ogg_buffer(sd);
116 117
}

118
static void finish_encoder(struct ogg_vorbis_data *od)
119
{
120
	vorbis_analysis_wrote(&od->vd, 0);
121

122 123 124 125 126
	while (vorbis_analysis_blockout(&od->vd, &od->vb) == 1) {
		vorbis_analysis(&od->vb, NULL);
		vorbis_bitrate_addblock(&od->vb);
		while (vorbis_bitrate_flushpacket(&od->vd, &od->op)) {
			ogg_stream_packetin(&od->os, &od->op);
127 128 129 130
		}
	}
}

131
static int shout_ogg_encoder_clear_encoder(struct shout_data *sd)
132
{
133
	struct ogg_vorbis_data *od = (struct ogg_vorbis_data *)sd->encoder_data;
134
	int ret;
135

136 137 138
	finish_encoder(od);
	if ((ret = ogg_stream_pageout(&od->os, &od->og)))
		copy_ogg_buffer_to_shout_buffer(&od->og, &sd->buf);
139

140 141 142 143 144
	vorbis_comment_clear(&od->vc);
	ogg_stream_clear(&od->os);
	vorbis_block_clear(&od->vb);
	vorbis_dsp_clear(&od->vd);
	vorbis_info_clear(&od->vi);
145 146

	return ret;
147 148
}

149 150
static void shout_ogg_encoder_finish(struct shout_data *sd)
{
151
	struct ogg_vorbis_data *od = (struct ogg_vorbis_data *)sd->encoder_data;
152 153 154 155 156 157 158 159 160

	if (od) {
		free(od);
		sd->encoder_data = NULL;
	}
}

static int shout_ogg_encoder_init(struct shout_data *sd)
{
161
	struct ogg_vorbis_data *od = g_new(struct ogg_vorbis_data, 1);
162 163 164 165 166 167

	sd->encoder_data = od;

	return 0;
}

168
static int reinit_encoder(struct shout_data *sd)
169
{
170
	struct ogg_vorbis_data *od = (struct ogg_vorbis_data *)sd->encoder_data;
171 172

	vorbis_info_init(&od->vi);
173 174

	if (sd->quality >= -1.0) {
175
		if (0 != vorbis_encode_init_vbr(&od->vi,
176
						sd->audio_format.channels,
177
						sd->audio_format.sample_rate,
178
						sd->quality * 0.1)) {
179
			g_warning("error initializing vorbis vbr\n");
180
			vorbis_info_clear(&od->vi);
181 182 183
			return -1;
		}
	} else {
184
		if (0 != vorbis_encode_init(&od->vi,
185
					    sd->audio_format.channels,
186
					    sd->audio_format.sample_rate, -1.0,
187
					    sd->bitrate * 1000, -1.0)) {
188
			g_warning("error initializing vorbis encoder\n");
189
			vorbis_info_clear(&od->vi);
190 191 192 193
			return -1;
		}
	}

194 195 196 197
	vorbis_analysis_init(&od->vd, &od->vi);
	vorbis_block_init(&od->vd, &od->vb);
	ogg_stream_init(&od->os, rand());
	vorbis_comment_init(&od->vc);
198 199 200 201

	return 0;
}

202
static int shout_ogg_encoder_init_encoder(struct shout_data *sd)
203 204 205 206 207
{
	if (reinit_encoder(sd))
		return -1;

	if (send_ogg_vorbis_header(sd)) {
208
		g_warning("error sending ogg vorbis header for shout\n");
209 210 211 212 213 214
		return -1;
	}

	return 0;
}

215
static int shout_ogg_encoder_send_metadata(struct shout_data *sd,
216 217
					   G_GNUC_UNUSED char * song,
					   G_GNUC_UNUSED size_t size)
218
{
219
	struct ogg_vorbis_data *od = (struct ogg_vorbis_data *)sd->encoder_data;
220

221
	shout_ogg_encoder_clear_encoder(sd);
222
	if (reinit_encoder(sd))
223 224 225 226
		return 0;

	copy_tag_to_vorbis_comment(sd);

227 228 229 230
	vorbis_analysis_headerout(&od->vd, &od->vc,
				  &od->header_main,
				  &od->header_comments,
				  &od->header_codebooks);
231

232 233 234
	ogg_stream_packetin(&od->os, &od->header_main);
	ogg_stream_packetin(&od->os, &od->header_comments);
	ogg_stream_packetin(&od->os, &od->header_codebooks);
235

236
	flush_ogg_buffer(sd);
237 238 239 240

	return 0;
}

241 242 243 244 245 246 247 248 249
static void
pcm16_to_ogg_buffer(float **dest, const int16_t *src,
		    unsigned num_samples, unsigned num_channels)
{
	for (unsigned i = 0; i < num_samples; i++)
		for (unsigned j = 0; j < num_channels; j++)
			dest[j][i] = *src++ / 32768.0;
}

250 251 252
static int
shout_ogg_encoder_encode(struct shout_data *sd,
			 const void *chunk, size_t size)
253
{
254
	struct shout_buffer *buf = &sd->buf;
255
	unsigned int samples;
256
	struct ogg_vorbis_data *od = (struct ogg_vorbis_data *)sd->encoder_data;
257

258
	samples = size / audio_format_frame_size(&sd->audio_format);
259 260 261

	/* this is for only 16-bit audio */

262 263 264
	pcm16_to_ogg_buffer(vorbis_analysis_buffer(&od->vd, samples),
			    (const int16_t *)chunk,
			    samples, sd->audio_format.channels);
265

266
	vorbis_analysis_wrote(&od->vd, samples);
267

268 269 270
	while (1 == vorbis_analysis_blockout(&od->vd, &od->vb)) {
		vorbis_analysis(&od->vb, NULL);
		vorbis_bitrate_addblock(&od->vb);
271

272 273
		while (vorbis_bitrate_flushpacket(&od->vd, &od->op)) {
			ogg_stream_packetin(&od->os, &od->op);
274 275
		}
	}
276

277 278
	if (ogg_stream_pageout(&od->os, &od->og))
		copy_ogg_buffer_to_shout_buffer(&od->og, buf);
279 280

	return 0;
281 282
}

283
const struct shout_encoder_plugin shout_ogg_encoder = {
284 285 286 287 288 289 290 291 292 293
	"ogg",
	SHOUT_FORMAT_VORBIS,

	shout_ogg_encoder_clear_encoder,
	shout_ogg_encoder_encode,
	shout_ogg_encoder_finish,
	shout_ogg_encoder_init,
	shout_ogg_encoder_init_encoder,
	shout_ogg_encoder_send_metadata,
};