2005-05-07 07:01:35 -04:00
|
|
|
/* Icecast
|
|
|
|
*
|
|
|
|
* This program is distributed under the GNU General Public License, version 2.
|
|
|
|
* A copy of this license is included with this source.
|
|
|
|
*
|
2015-01-10 13:53:44 -05:00
|
|
|
* Copyright 2000-2004, Jack Moffitt <jack@xiph.org,
|
2005-05-07 07:01:35 -04:00
|
|
|
* Michael Smith <msmith@xiph.org>,
|
|
|
|
* oddsock <oddsock@xiph.org>,
|
|
|
|
* Karl Heyes <karl@xiph.org>
|
|
|
|
* and others (see AUTHORS for details).
|
2022-03-15 19:52:27 -04:00
|
|
|
* Copyright 2014-2022, Philipp "ph3-der-loewe" Schafft <lion@lion.leolix.org>,
|
2005-05-07 07:01:35 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/* Ogg codec handler for FLAC logical streams */
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
2022-03-15 18:33:28 -04:00
|
|
|
#include <stdint.h>
|
2022-03-15 18:53:49 -04:00
|
|
|
#include <stdbool.h>
|
2005-05-07 07:01:35 -04:00
|
|
|
#include <ogg/ogg.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "refbuf.h"
|
2022-03-15 21:01:03 -04:00
|
|
|
#include "metadata_xiph.h"
|
2005-05-07 07:01:35 -04:00
|
|
|
#include "format_ogg.h"
|
|
|
|
#include "client.h"
|
|
|
|
#include "stats.h"
|
|
|
|
|
|
|
|
#define CATMODULE "format-flac"
|
|
|
|
#include "logging.h"
|
|
|
|
|
2022-03-15 18:33:28 -04:00
|
|
|
typedef enum {
|
|
|
|
FLAC_BLOCK_TYPE__ERROR = -1,
|
|
|
|
FLAC_BLOCK_TYPE_STREAMINFO = 0,
|
|
|
|
FLAC_BLOCK_TYPE_PADDING = 1,
|
|
|
|
FLAC_BLOCK_TYPE_APPLICATION = 2,
|
|
|
|
FLAC_BLOCK_TYPE_SEEKTABLE = 3,
|
|
|
|
FLAC_BLOCK_TYPE_VORBIS_COMMENT = 4,
|
|
|
|
FLAC_BLOCK_TYPE_CUESHEET = 5,
|
|
|
|
FLAC_BLOCK_TYPE_PICTURE = 6
|
|
|
|
} flac_block_type_t;
|
|
|
|
|
2022-03-15 18:53:49 -04:00
|
|
|
typedef struct {
|
|
|
|
flac_block_type_t type;
|
|
|
|
bool last;
|
|
|
|
size_t len;
|
|
|
|
const void *data;
|
|
|
|
} flac_block_t;
|
|
|
|
|
|
|
|
/* returns true if parse was ok, false on error */
|
2022-03-15 19:50:28 -04:00
|
|
|
static bool flac_parse_block(flac_block_t *block, const ogg_packet * packet, size_t offset)
|
2022-03-15 18:33:28 -04:00
|
|
|
{
|
|
|
|
uint8_t type;
|
2022-03-15 18:53:49 -04:00
|
|
|
uint32_t len;
|
2022-03-15 18:33:28 -04:00
|
|
|
|
2022-03-15 18:53:49 -04:00
|
|
|
/* check header length */
|
2022-03-15 19:50:28 -04:00
|
|
|
if ((size_t)packet->bytes <= (4 + offset))
|
2022-03-15 18:53:49 -04:00
|
|
|
return false;
|
|
|
|
|
2022-03-15 19:50:28 -04:00
|
|
|
type = packet->packet[offset];
|
2022-03-15 18:53:49 -04:00
|
|
|
|
|
|
|
/* 0xFF is the sync code for a FRAME not a block */
|
|
|
|
if (type == 0xFF)
|
|
|
|
return false;
|
2022-03-15 18:33:28 -04:00
|
|
|
|
2022-03-15 18:53:49 -04:00
|
|
|
memset(block, 0, sizeof(*block));
|
2022-03-15 18:33:28 -04:00
|
|
|
|
2022-03-15 18:53:49 -04:00
|
|
|
if (type & 0x80) {
|
|
|
|
block->last = true;
|
|
|
|
type &= 0x7F;
|
2022-03-15 18:33:28 -04:00
|
|
|
}
|
2022-03-15 18:53:49 -04:00
|
|
|
|
|
|
|
if (type > FLAC_BLOCK_TYPE_PICTURE)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
block->type = type;
|
|
|
|
|
2022-03-15 19:50:28 -04:00
|
|
|
len = (unsigned char)packet->packet[1 + offset];
|
2022-03-15 18:53:49 -04:00
|
|
|
len <<= 8;
|
2022-03-15 19:50:28 -04:00
|
|
|
len |= (unsigned char)packet->packet[2 + offset];
|
2022-03-15 18:53:49 -04:00
|
|
|
len <<= 8;
|
2022-03-15 19:50:28 -04:00
|
|
|
len |= (unsigned char)packet->packet[3 + offset];
|
2022-03-15 18:53:49 -04:00
|
|
|
|
|
|
|
/* check Ogg packet size vs. self-sync size */
|
2022-03-15 19:50:28 -04:00
|
|
|
if ((size_t)packet->bytes != (len + 4 + offset))
|
2022-03-15 18:53:49 -04:00
|
|
|
return false;
|
|
|
|
|
|
|
|
block->len = len;
|
2022-03-15 19:50:28 -04:00
|
|
|
block->data = packet->packet + 4 + offset;
|
2022-03-15 18:53:49 -04:00
|
|
|
|
|
|
|
return true;
|
2022-03-15 18:33:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static const char * flac_block_type_to_name(flac_block_type_t type)
|
|
|
|
{
|
|
|
|
switch (type) {
|
|
|
|
case FLAC_BLOCK_TYPE__ERROR:
|
|
|
|
return "<error>";
|
|
|
|
break;
|
|
|
|
case FLAC_BLOCK_TYPE_STREAMINFO:
|
|
|
|
return "STREAMINFO";
|
|
|
|
break;
|
|
|
|
case FLAC_BLOCK_TYPE_PADDING:
|
|
|
|
return "PADDING";
|
|
|
|
break;
|
|
|
|
case FLAC_BLOCK_TYPE_APPLICATION:
|
|
|
|
return "APPLICATION";
|
|
|
|
break;
|
|
|
|
case FLAC_BLOCK_TYPE_SEEKTABLE:
|
|
|
|
return "SEEKTABLE";
|
|
|
|
break;
|
|
|
|
case FLAC_BLOCK_TYPE_VORBIS_COMMENT:
|
|
|
|
return "VORBIS_COMMENT";
|
|
|
|
break;
|
|
|
|
case FLAC_BLOCK_TYPE_CUESHEET:
|
|
|
|
return "CUESHEET";
|
|
|
|
break;
|
|
|
|
case FLAC_BLOCK_TYPE_PICTURE:
|
|
|
|
return "PICTURE";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return "<unknown>";
|
|
|
|
}
|
2005-05-07 07:01:35 -04:00
|
|
|
|
2022-03-15 21:29:02 -04:00
|
|
|
static void flac_handle_block_streaminfo(format_plugin_t *plugin, ogg_state_t *ogg_info, ogg_codec_t *codec, const flac_block_t *block)
|
|
|
|
{
|
|
|
|
uint32_t raw;
|
|
|
|
uint32_t sample_rate;
|
|
|
|
uint32_t channels;
|
|
|
|
uint32_t bits;
|
|
|
|
|
|
|
|
if (block->len != 34) {
|
|
|
|
ICECAST_LOG_ERROR("Can not parse FLAC header block STREAMINFO");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
raw = metadata_xiph_read_u32be_unaligned(block->data + 10);
|
|
|
|
sample_rate = ((raw >> 12) & 0xfffff) + 0;
|
|
|
|
channels = ((raw >> 9) & 0x7 ) + 1;
|
|
|
|
bits = ((raw >> 4) & 0x1F ) + 1;
|
|
|
|
|
|
|
|
stats_event_args(ogg_info->mount, "audio_samplerate", "%ld", (long int)sample_rate);
|
|
|
|
stats_event_args(ogg_info->mount, "audio_channels", "%ld", (long int)channels);
|
|
|
|
stats_event_args(ogg_info->mount, "audio_bits", "%ld", (long int)bits);
|
|
|
|
}
|
|
|
|
|
2022-03-15 21:01:03 -04:00
|
|
|
static void flac_handle_block(format_plugin_t *plugin, ogg_state_t *ogg_info, ogg_codec_t *codec, const flac_block_t *block)
|
2022-03-15 19:50:28 -04:00
|
|
|
{
|
|
|
|
ICECAST_LOG_DEBUG("Found header of type %s%s with %zu bytes of data", flac_block_type_to_name(block->type), block->last ? "(last)" : "", block->len);
|
2022-03-15 21:01:03 -04:00
|
|
|
|
|
|
|
switch (block->type) {
|
2022-03-15 21:29:02 -04:00
|
|
|
case FLAC_BLOCK_TYPE_STREAMINFO:
|
|
|
|
flac_handle_block_streaminfo(plugin, ogg_info, codec, block);
|
|
|
|
break;
|
2022-03-15 21:01:03 -04:00
|
|
|
case FLAC_BLOCK_TYPE_VORBIS_COMMENT:
|
|
|
|
vorbis_comment_clear(&plugin->vc);
|
|
|
|
vorbis_comment_init(&plugin->vc);
|
|
|
|
if (!metadata_xiph_read_vorbis_comments(&plugin->vc, block->data, block->len)) {
|
|
|
|
ICECAST_LOG_ERROR("Can not parse FLAC header block VORBIS_COMMENT");
|
|
|
|
}
|
|
|
|
ogg_info->log_metadata = 1;
|
|
|
|
break;
|
2022-03-15 21:28:27 -04:00
|
|
|
default:
|
|
|
|
/* no-op */
|
|
|
|
break;
|
2022-03-15 21:01:03 -04:00
|
|
|
}
|
2022-03-15 19:50:28 -04:00
|
|
|
}
|
|
|
|
|
2005-05-07 07:01:35 -04:00
|
|
|
static void flac_codec_free (ogg_state_t *ogg_info, ogg_codec_t *codec)
|
|
|
|
{
|
2014-10-31 04:46:58 -04:00
|
|
|
ICECAST_LOG_DEBUG("freeing FLAC codec");
|
2022-03-15 18:12:18 -04:00
|
|
|
stats_event(ogg_info->mount, "FLAC_version", NULL);
|
|
|
|
ogg_stream_clear(&codec->os);
|
|
|
|
free(codec);
|
2005-05-07 07:01:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Here, we just verify the page is ok and then add it to the queue */
|
2014-12-09 11:08:27 -05:00
|
|
|
static refbuf_t *process_flac_page (ogg_state_t *ogg_info, ogg_codec_t *codec, ogg_page *page, format_plugin_t *plugin)
|
2005-05-07 07:01:35 -04:00
|
|
|
{
|
|
|
|
refbuf_t * refbuf;
|
|
|
|
|
2022-03-15 18:12:18 -04:00
|
|
|
if (codec->headers) {
|
2005-05-07 07:01:35 -04:00
|
|
|
ogg_packet packet;
|
2022-03-15 18:12:18 -04:00
|
|
|
|
|
|
|
if (ogg_stream_pagein(&codec->os, page) < 0) {
|
2006-03-14 21:24:57 -05:00
|
|
|
ogg_info->error = 1;
|
|
|
|
return NULL;
|
|
|
|
}
|
2022-03-15 18:12:18 -04:00
|
|
|
|
|
|
|
while (ogg_stream_packetout(&codec->os, &packet)) {
|
2022-03-15 18:53:49 -04:00
|
|
|
flac_block_t block;
|
2022-03-15 18:14:12 -04:00
|
|
|
|
2022-03-15 19:50:28 -04:00
|
|
|
if (flac_parse_block(&block, &packet, 0)) {
|
2022-03-15 21:01:03 -04:00
|
|
|
flac_handle_block(plugin, ogg_info, codec, &block);
|
2022-03-15 18:53:49 -04:00
|
|
|
|
|
|
|
if (block.last) {
|
2022-03-15 18:14:12 -04:00
|
|
|
codec->headers = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-03-15 18:53:49 -04:00
|
|
|
continue;
|
2005-05-07 07:01:35 -04:00
|
|
|
}
|
2022-03-15 18:12:18 -04:00
|
|
|
|
2005-05-07 07:01:35 -04:00
|
|
|
ogg_info->error = 1;
|
2022-03-15 18:12:18 -04:00
|
|
|
|
2005-05-07 07:01:35 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
2022-03-15 18:12:18 -04:00
|
|
|
|
|
|
|
if (codec->headers) {
|
|
|
|
format_ogg_attach_header(ogg_info, page);
|
2005-05-07 07:01:35 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2022-03-15 18:12:18 -04:00
|
|
|
|
|
|
|
refbuf = make_refbuf_with_page(page);
|
|
|
|
|
2005-05-07 07:01:35 -04:00
|
|
|
return refbuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Check for flac header in logical stream */
|
|
|
|
|
|
|
|
ogg_codec_t *initial_flac_page (format_plugin_t *plugin, ogg_page *page)
|
|
|
|
{
|
|
|
|
ogg_state_t *ogg_info = plugin->_state;
|
2022-03-15 18:12:18 -04:00
|
|
|
ogg_codec_t *codec = calloc(1, sizeof(ogg_codec_t));
|
2005-05-07 07:01:35 -04:00
|
|
|
ogg_packet packet;
|
|
|
|
|
2022-03-15 18:12:18 -04:00
|
|
|
ogg_stream_init(&codec->os, ogg_page_serialno(page));
|
|
|
|
ogg_stream_pagein(&codec->os, page);
|
2005-05-07 07:01:35 -04:00
|
|
|
|
2022-03-15 18:12:18 -04:00
|
|
|
ogg_stream_packetout(&codec->os, &packet);
|
2005-05-07 07:01:35 -04:00
|
|
|
|
2014-10-31 04:46:58 -04:00
|
|
|
ICECAST_LOG_DEBUG("checking for FLAC codec");
|
2005-05-07 07:01:35 -04:00
|
|
|
do
|
|
|
|
{
|
|
|
|
unsigned char *parse = packet.packet;
|
2022-03-15 19:50:28 -04:00
|
|
|
flac_block_t block;
|
2005-05-07 07:01:35 -04:00
|
|
|
|
|
|
|
if (page->header_len + page->body_len != 79)
|
|
|
|
break;
|
|
|
|
if (*parse != 0x7F)
|
|
|
|
break;
|
|
|
|
parse++;
|
2014-11-30 15:32:30 -05:00
|
|
|
if (memcmp(parse, "FLAC", 4) != 0)
|
2005-05-07 07:01:35 -04:00
|
|
|
break;
|
|
|
|
|
2014-10-31 04:46:58 -04:00
|
|
|
ICECAST_LOG_INFO("seen initial FLAC header");
|
2005-05-07 07:01:35 -04:00
|
|
|
|
|
|
|
parse += 4;
|
2022-03-15 18:12:18 -04:00
|
|
|
stats_event_args(ogg_info->mount, "FLAC_version", "%d.%d", parse[0], parse[1]);
|
2005-05-07 07:01:35 -04:00
|
|
|
codec->process_page = process_flac_page;
|
|
|
|
codec->codec_free = flac_codec_free;
|
|
|
|
codec->headers = 1;
|
|
|
|
codec->name = "FLAC";
|
|
|
|
|
2022-03-15 19:50:28 -04:00
|
|
|
if (flac_parse_block(&block, &packet, 13))
|
2022-03-15 21:01:03 -04:00
|
|
|
flac_handle_block(plugin, ogg_info, codec, &block);
|
2022-03-15 19:50:28 -04:00
|
|
|
|
2014-11-30 15:32:30 -05:00
|
|
|
format_ogg_attach_header(ogg_info, page);
|
2005-05-07 07:01:35 -04:00
|
|
|
return codec;
|
|
|
|
} while (0);
|
|
|
|
|
2014-11-30 15:32:30 -05:00
|
|
|
ogg_stream_clear(&codec->os);
|
|
|
|
free(codec);
|
2005-05-07 07:01:35 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|