format_vorbis.c 5.6 KB
Newer Older
Jack Moffitt's avatar
Jack Moffitt committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* format_vorbis.c
**
** format plugin for vorbis
**
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <ogg/ogg.h>
#include <vorbis/codec.h>

#include "refbuf.h"

Michael Smith's avatar
Michael Smith committed
16
#include "stats.h"
Jack Moffitt's avatar
Jack Moffitt committed
17 18
#include "format.h"

19 20 21 22
#define CATMODULE "format-vorbis"
#include "log.h"
#include "logging.h"

Michael Smith's avatar
Michael Smith committed
23 24
#define MAX_HEADER_PAGES 10

Jack Moffitt's avatar
Jack Moffitt committed
25 26 27
typedef struct _vstate_tag
{
	ogg_sync_state oy;
Jack Moffitt's avatar
Jack Moffitt committed
28 29 30 31
	ogg_stream_state os;
	vorbis_info vi;
	vorbis_comment vc;

Jack Moffitt's avatar
Jack Moffitt committed
32 33 34
	ogg_page og;
	unsigned long serialno;
	int header;
Michael Smith's avatar
Michael Smith committed
35
	refbuf_t *headbuf[MAX_HEADER_PAGES];
Jack Moffitt's avatar
Jack Moffitt committed
36
	int packets;
Jack Moffitt's avatar
Jack Moffitt committed
37 38
} vstate_t;

39 40 41 42 43
static void format_vorbis_free_plugin(format_plugin_t *self);
static int format_vorbis_get_buffer(format_plugin_t *self, char *data, 
        unsigned long len, refbuf_t **buffer);
static refbuf_queue_t *format_vorbis_get_predata(format_plugin_t *self);
static void *format_vorbis_create_client_data(format_plugin_t *self);
Jack Moffitt's avatar
Jack Moffitt committed
44 45 46 47 48 49 50

format_plugin_t *format_vorbis_get_plugin(void)
{
	format_plugin_t *plugin;
	vstate_t *state;

	plugin = (format_plugin_t *)malloc(sizeof(format_plugin_t));
51

Jack Moffitt's avatar
Jack Moffitt committed
52 53 54 55
	plugin->type = FORMAT_TYPE_VORBIS;
	plugin->has_predata = 1;
	plugin->get_buffer = format_vorbis_get_buffer;
	plugin->get_predata = format_vorbis_get_predata;
56 57
    plugin->write_buf_to_client = format_generic_write_buf_to_client;
    plugin->create_client_data = format_vorbis_create_client_data;
58
	plugin->free_plugin = format_vorbis_free_plugin;
59
    plugin->format_description = "Ogg Vorbis";
Jack Moffitt's avatar
Jack Moffitt committed
60 61 62 63 64 65 66 67 68

	state = (vstate_t *)calloc(1, sizeof(vstate_t));
	ogg_sync_init(&state->oy);

	plugin->_state = (void *)state;

	return plugin;
}

69 70 71 72 73 74 75 76 77
void format_vorbis_free_plugin(format_plugin_t *self)
{
	int i;
	vstate_t *state = (vstate_t *)self->_state;

	/* free memory associated with this plugin instance */

	/* free state memory */
	ogg_sync_clear(&state->oy);
Jack Moffitt's avatar
Jack Moffitt committed
78 79 80
	ogg_stream_clear(&state->os);
	vorbis_comment_clear(&state->vc);
	vorbis_info_clear(&state->vi);
81
	
Michael Smith's avatar
Michael Smith committed
82
	for (i = 0; i < MAX_HEADER_PAGES; i++) {
83 84 85 86 87 88 89 90 91 92 93 94
		if (state->headbuf[i]) {
			refbuf_release(state->headbuf[i]);
			state->headbuf[i] = NULL;
		}
	}

	free(state);

	/* free the plugin instance */
	free(self);
}

Michael Smith's avatar
Michael Smith committed
95
int format_vorbis_get_buffer(format_plugin_t *self, char *data, unsigned long len, refbuf_t **buffer)
Jack Moffitt's avatar
Jack Moffitt committed
96
{
Michael Smith's avatar
Michael Smith committed
97
	char *buf;
Jack Moffitt's avatar
Jack Moffitt committed
98 99 100
	int i, result;
	ogg_packet op;
	char *tag;
Michael Smith's avatar
Michael Smith committed
101
    refbuf_t *refbuf;
Jack Moffitt's avatar
Jack Moffitt committed
102 103 104 105
	vstate_t *state = (vstate_t *)self->_state;

	if (data) {
		/* write the data to the buffer */
Michael Smith's avatar
Michael Smith committed
106 107
		buf = ogg_sync_buffer(&state->oy, len);
        memcpy(buf, data, len);
Jack Moffitt's avatar
Jack Moffitt committed
108 109 110 111 112 113 114 115 116 117 118 119
		ogg_sync_wrote(&state->oy, len);
	}

	refbuf = NULL;
	if (ogg_sync_pageout(&state->oy, &state->og) == 1) {
		refbuf = refbuf_new(state->og.header_len + state->og.body_len);
		memcpy(refbuf->data, state->og.header, state->og.header_len);
		memcpy(&refbuf->data[state->og.header_len], state->og.body, state->og.body_len);

		if (state->serialno != ogg_page_serialno(&state->og)) {
			/* this is a new logical bitstream */
			state->header = 0;
Jack Moffitt's avatar
Jack Moffitt committed
120 121 122
			state->packets = 0;

			/* release old headers, stream state, vorbis data */
Michael Smith's avatar
Michael Smith committed
123
			for (i = 0; i < MAX_HEADER_PAGES; i++) {
Jack Moffitt's avatar
Jack Moffitt committed
124 125 126 127 128
				if (state->headbuf[i]) {
					refbuf_release(state->headbuf[i]);
					state->headbuf[i] = NULL;
				}
			}
129 130 131 132
            /* Clear old stuff. Rarely but occasionally needed. */
			ogg_stream_clear(&state->os);
			vorbis_comment_clear(&state->vc);
			vorbis_info_clear(&state->vi);
Jack Moffitt's avatar
Jack Moffitt committed
133

Jack Moffitt's avatar
Jack Moffitt committed
134
			state->serialno = ogg_page_serialno(&state->og);
Jack Moffitt's avatar
Jack Moffitt committed
135 136 137
			ogg_stream_init(&state->os, state->serialno);
			vorbis_info_init(&state->vi);
			vorbis_comment_init(&state->vc);
Jack Moffitt's avatar
Jack Moffitt committed
138 139 140
		}

		if (state->header >= 0) {
Michael Smith's avatar
Michael Smith committed
141 142 143 144 145
            /* FIXME: In some streams (non-vorbis ogg streams), this could get
             * extras pages beyond the header. We need to collect the pages
             * here anyway, but they may have to be discarded later.
             */
			if (ogg_page_granulepos(&state->og) <= 0) {
Jack Moffitt's avatar
Jack Moffitt committed
146 147
				state->header++;
			} else {
Jack Moffitt's avatar
Jack Moffitt committed
148 149 150 151 152
				/* we're done caching headers */
				state->header = -1;

				/* put known comments in the stats */
				tag = vorbis_comment_query(&state->vc, "TITLE", 0);
153 154
				if (tag) stats_event(self->mount, "title", tag);
				else stats_event(self->mount, "title", "unknown");
Jack Moffitt's avatar
Jack Moffitt committed
155
				tag = vorbis_comment_query(&state->vc, "ARTIST", 0);
156 157
				if (tag) stats_event(self->mount, "artist", tag);
				else stats_event(self->mount, "artist", "unknown");
Jack Moffitt's avatar
Jack Moffitt committed
158 159 160 161 162

				/* don't need these now */
				ogg_stream_clear(&state->os);
				vorbis_comment_clear(&state->vc);
				vorbis_info_clear(&state->vi);
Jack Moffitt's avatar
Jack Moffitt committed
163 164 165
			}
		}

Jack Moffitt's avatar
Jack Moffitt committed
166
		/* cache header pages */
167
		if (state->header > 0 && state->packets < 3) {
Michael Smith's avatar
Michael Smith committed
168 169
            if(state->header > MAX_HEADER_PAGES) {
                refbuf_release(refbuf);
170 171
                ERROR1("Bad vorbis input: header is more than %d pages long", MAX_HEADER_PAGES);

Michael Smith's avatar
Michael Smith committed
172 173
                return -1;
            }
Jack Moffitt's avatar
Jack Moffitt committed
174 175
			refbuf_addref(refbuf);
			state->headbuf[state->header - 1] = refbuf;
Jack Moffitt's avatar
Jack Moffitt committed
176

177
			if (state->packets >= 0 && state->packets < 3) {
Jack Moffitt's avatar
Jack Moffitt committed
178
				ogg_stream_pagein(&state->os, &state->og);
179
				while (state->packets < 3) {
Jack Moffitt's avatar
Jack Moffitt committed
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
					result = ogg_stream_packetout(&state->os, &op);
					if (result == 0) break; /* need more data */
					if (result < 0) {
						state->packets = -1;
						break;
					}

					state->packets++;

					if (vorbis_synthesis_headerin(&state->vi, &state->vc, &op) < 0) {
						state->packets = -1;
						break;
					}
				}
			}
Jack Moffitt's avatar
Jack Moffitt committed
195 196 197
		}
	}

Michael Smith's avatar
Michael Smith committed
198 199
    *buffer = refbuf;
	return 0;
Jack Moffitt's avatar
Jack Moffitt committed
200 201 202 203 204 205 206 207 208
}

refbuf_queue_t *format_vorbis_get_predata(format_plugin_t *self)
{
	refbuf_queue_t *queue;
	int i;
	vstate_t *state = (vstate_t *)self->_state;

	queue = NULL;
Michael Smith's avatar
Michael Smith committed
209
	for (i = 0; i < MAX_HEADER_PAGES; i++) {
Jack Moffitt's avatar
Jack Moffitt committed
210 211 212 213 214 215 216 217 218 219 220
		if (state->headbuf[i]) {
			refbuf_addref(state->headbuf[i]);
			refbuf_queue_add(&queue, state->headbuf[i]);
		} else {
			break;
		}
	}

	return queue;
}

221 222 223
static void *format_vorbis_create_client_data(format_plugin_t *self) {
    return NULL;
}
Jack Moffitt's avatar
Jack Moffitt committed
224 225