{de,en}coder/{vorbis,flac,opus}: move several libraries to lib/xiph/

This commit is contained in:
Max Kellermann
2016-05-02 23:11:07 +02:00
parent b27a41812f
commit ed6c6296eb
15 changed files with 20 additions and 22 deletions

View File

@@ -0,0 +1,43 @@
/*
* Copyright 2003-2016 The Music Player Daemon Project
* 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.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "OggSerial.hxx"
#include "system/Clock.hxx"
#include "Compiler.h"
#include <atomic>
static std::atomic_uint next_ogg_serial;
int
GenerateOggSerial()
{
unsigned serial = ++next_ogg_serial;
if (gcc_unlikely(serial < 16)) {
/* first-time initialization: seed with a clock value,
which is random enough for our use */
/* this code is not race-free, but good enough */
const unsigned seed = MonotonicClockMS();
next_ogg_serial = serial = seed;
}
return serial;
}

View File

@@ -0,0 +1,29 @@
/*
* Copyright 2003-2016 The Music Player Daemon Project
* 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.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifndef MPD_OGG_SERIAL_HXX
#define MPD_OGG_SERIAL_HXX
/**
* Generate the next pseudo-random Ogg serial.
*/
int
GenerateOggSerial();
#endif

128
src/lib/xiph/OggStream.hxx Normal file
View File

@@ -0,0 +1,128 @@
/*
* Copyright 2003-2016 The Music Player Daemon Project
* 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.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifndef MPD_OGG_STREAM_HXX
#define MPD_OGG_STREAM_HXX
#include "check.h"
#include <ogg/ogg.h>
#include <assert.h>
#include <string.h>
#include <stdint.h>
class OggStream {
ogg_stream_state state;
bool flush;
#ifndef NDEBUG
bool initialized;
#endif
public:
#ifndef NDEBUG
OggStream():initialized(false) {}
~OggStream() {
assert(!initialized);
}
#endif
void Initialize(int serialno) {
assert(!initialized);
ogg_stream_init(&state, serialno);
/* set "flush" to true, so the caller gets the full
headers on the first read() */
flush = true;
#ifndef NDEBUG
initialized = true;
#endif
}
void Reinitialize(int serialno) {
assert(initialized);
ogg_stream_reset_serialno(&state, serialno);
/* set "flush" to true, so the caller gets the full
headers on the first read() */
flush = true;
}
void Deinitialize() {
assert(initialized);
ogg_stream_clear(&state);
#ifndef NDEBUG
initialized = false;
#endif
}
void Flush() {
assert(initialized);
flush = true;
}
void PacketIn(const ogg_packet &packet) {
assert(initialized);
ogg_stream_packetin(&state,
const_cast<ogg_packet *>(&packet));
}
bool PageOut(ogg_page &page) {
int result = ogg_stream_pageout(&state, &page);
if (result == 0 && flush) {
flush = false;
result = ogg_stream_flush(&state, &page);
}
return result != 0;
}
size_t PageOut(void *_buffer, size_t size) {
ogg_page page;
if (!PageOut(page))
return 0;
assert(page.header_len > 0 || page.body_len > 0);
size_t header_len = (size_t)page.header_len;
size_t body_len = (size_t)page.body_len;
assert(header_len <= size);
if (header_len + body_len > size)
/* TODO: better overflow handling */
body_len = size - header_len;
uint8_t *buffer = (uint8_t *)_buffer;
memcpy(buffer, page.header, header_len);
memcpy(buffer + header_len, page.body, body_len);
return header_len + body_len;
}
};
#endif

View File

@@ -0,0 +1,77 @@
/*
* Copyright 2003-2016 The Music Player Daemon Project
* 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.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifndef MPD_OGG_SYNC_STATE_HXX
#define MPD_OGG_SYNC_STATE_HXX
#include "check.h"
#include "OggUtil.hxx"
#include <ogg/ogg.h>
#include <stddef.h>
/**
* Wrapper for an ogg_sync_state.
*/
class OggSyncState {
ogg_sync_state oy;
Reader &reader;
public:
explicit OggSyncState(Reader &_reader)
:reader(_reader) {
ogg_sync_init(&oy);
}
~OggSyncState() {
ogg_sync_clear(&oy);
}
void Reset() {
ogg_sync_reset(&oy);
}
bool Feed(size_t size) {
return OggFeed(oy, reader, size);
}
bool ExpectPage(ogg_page &page) {
return OggExpectPage(oy, page, reader);
}
bool ExpectFirstPage(ogg_stream_state &os) {
return OggExpectFirstPage(oy, os, reader);
}
bool ExpectPageIn(ogg_stream_state &os) {
return OggExpectPageIn(oy, os, reader);
}
bool ExpectPageSeek(ogg_page &page) {
return OggExpectPageSeek(oy, page, reader);
}
bool ExpectPageSeekIn(ogg_stream_state &os) {
return OggExpectPageSeekIn(oy, os, reader);
}
};
#endif

111
src/lib/xiph/OggUtil.cxx Normal file
View File

@@ -0,0 +1,111 @@
/*
* Copyright 2003-2016 The Music Player Daemon Project
* 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.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "config.h"
#include "OggUtil.hxx"
#include "fs/io/Reader.hxx"
bool
OggFeed(ogg_sync_state &oy, Reader &reader, size_t size)
{
char *buffer = ogg_sync_buffer(&oy, size);
if (buffer == nullptr)
return false;
size_t nbytes = reader.Read(buffer, size);
if (nbytes == 0)
return false;
ogg_sync_wrote(&oy, nbytes);
return true;
}
bool
OggExpectPage(ogg_sync_state &oy, ogg_page &page, Reader &reader)
{
while (true) {
int r = ogg_sync_pageout(&oy, &page);
if (r != 0)
return r > 0;
if (!OggFeed(oy, reader, 1024))
return false;
}
}
bool
OggExpectFirstPage(ogg_sync_state &oy, ogg_stream_state &os, Reader &reader)
{
ogg_page page;
if (!OggExpectPage(oy, page, reader))
return false;
ogg_stream_init(&os, ogg_page_serialno(&page));
ogg_stream_pagein(&os, &page);
return true;
}
bool
OggExpectPageIn(ogg_sync_state &oy, ogg_stream_state &os, Reader &reader)
{
ogg_page page;
if (!OggExpectPage(oy, page, reader))
return false;
ogg_stream_pagein(&os, &page);
return true;
}
bool
OggExpectPageSeek(ogg_sync_state &oy, ogg_page &page, Reader &reader)
{
size_t remaining_skipped = 32768;
while (true) {
int r = ogg_sync_pageseek(&oy, &page);
if (r > 0)
return true;
if (r < 0) {
/* skipped -r bytes */
size_t nbytes = -r;
if (nbytes > remaining_skipped)
/* still no ogg page - we lost our
patience, abort */
return false;
remaining_skipped -= nbytes;
continue;
}
if (!OggFeed(oy, reader, 1024))
return false;
}
}
bool
OggExpectPageSeekIn(ogg_sync_state &oy, ogg_stream_state &os, Reader &reader)
{
ogg_page page;
if (!OggExpectPageSeek(oy, page, reader))
return false;
ogg_stream_pagein(&os, &page);
return true;
}

80
src/lib/xiph/OggUtil.hxx Normal file
View File

@@ -0,0 +1,80 @@
/*
* Copyright 2003-2016 The Music Player Daemon Project
* 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.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifndef MPD_OGG_UTIL_HXX
#define MPD_OGG_UTIL_HXX
#include "check.h"
#include <ogg/ogg.h>
#include <stddef.h>
class Reader;
/**
* Feed data from the #InputStream into the #ogg_sync_state.
*
* @return false on error or end-of-file
*/
bool
OggFeed(ogg_sync_state &oy, Reader &reader, size_t size);
/**
* Feed into the #ogg_sync_state until a page gets available. Garbage
* data at the beginning is considered a fatal error.
*
* @return true if a page is available
*/
bool
OggExpectPage(ogg_sync_state &oy, ogg_page &page, Reader &reader);
/**
* Combines OggExpectPage(), ogg_stream_init() and
* ogg_stream_pagein().
*
* @return true if the stream was initialized and the first page was
* delivered to it
*/
bool
OggExpectFirstPage(ogg_sync_state &oy, ogg_stream_state &os, Reader &reader);
/**
* Combines OggExpectPage() and ogg_stream_pagein().
*
* @return true if a page was delivered to the stream
*/
bool
OggExpectPageIn(ogg_sync_state &oy, ogg_stream_state &os, Reader &reader);
/**
* Like OggExpectPage(), but allow skipping garbage (after seeking).
*/
bool
OggExpectPageSeek(ogg_sync_state &oy, ogg_page &page, Reader &reader);
/**
* Combines OggExpectPageSeek() and ogg_stream_pagein().
*
* @return true if a page was delivered to the stream
*/
bool
OggExpectPageSeekIn(ogg_sync_state &oy, ogg_stream_state &os, Reader &reader);
#endif

View File

@@ -0,0 +1,110 @@
/*
* Copyright 2003-2016 The Music Player Daemon Project
* 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.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "config.h"
#include "VorbisComments.hxx"
#include "XiphTags.hxx"
#include "tag/TagTable.hxx"
#include "tag/TagHandler.hxx"
#include "tag/TagBuilder.hxx"
#include "tag/VorbisComment.hxx"
#include "tag/ReplayGain.hxx"
#include "ReplayGainInfo.hxx"
#include "util/DivideString.hxx"
bool
vorbis_comments_to_replay_gain(ReplayGainInfo &rgi, char **comments)
{
rgi.Clear();
bool found = false;
while (*comments) {
if (ParseReplayGainVorbis(rgi, *comments))
found = true;
comments++;
}
return found;
}
/**
* Check if the comment's name equals the passed name, and if so, copy
* the comment value into the tag.
*/
static bool
vorbis_copy_comment(const char *comment,
const char *name, TagType tag_type,
const TagHandler &handler, void *handler_ctx)
{
const char *value;
value = vorbis_comment_value(comment, name);
if (value != nullptr) {
tag_handler_invoke_tag(handler, handler_ctx, tag_type, value);
return true;
}
return false;
}
static void
vorbis_scan_comment(const char *comment,
const TagHandler &handler, void *handler_ctx)
{
if (handler.pair != nullptr) {
const DivideString split(comment, '=');
if (split.IsDefined() && !split.IsEmpty())
tag_handler_invoke_pair(handler, handler_ctx,
split.GetFirst(),
split.GetSecond());
}
for (const struct tag_table *i = xiph_tags; i->name != nullptr; ++i)
if (vorbis_copy_comment(comment, i->name, i->type,
handler, handler_ctx))
return;
for (unsigned i = 0; i < TAG_NUM_OF_ITEM_TYPES; ++i)
if (vorbis_copy_comment(comment,
tag_item_names[i], TagType(i),
handler, handler_ctx))
return;
}
void
vorbis_comments_scan(char **comments,
const TagHandler &handler, void *handler_ctx)
{
while (*comments)
vorbis_scan_comment(*comments++,
handler, handler_ctx);
}
Tag *
vorbis_comments_to_tag(char **comments)
{
TagBuilder tag_builder;
vorbis_comments_scan(comments, add_tag_handler, &tag_builder);
return tag_builder.IsEmpty()
? nullptr
: tag_builder.CommitNew();
}

View File

@@ -0,0 +1,39 @@
/*
* Copyright 2003-2016 The Music Player Daemon Project
* 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.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifndef MPD_VORBIS_COMMENTS_HXX
#define MPD_VORBIS_COMMENTS_HXX
#include "check.h"
struct ReplayGainInfo;
struct TagHandler;
struct Tag;
bool
vorbis_comments_to_replay_gain(ReplayGainInfo &rgi, char **comments);
void
vorbis_comments_scan(char **comments,
const TagHandler &handler, void *handler_ctx);
Tag *
vorbis_comments_to_tag(char **comments);
#endif