encoder/{vorbis,opus}: merge code to new class OggStream
This commit is contained in:
parent
9e3f843291
commit
038d216f18
@ -695,10 +695,10 @@ ENCODER_LIBS = \
|
||||
$(OPUS_LIBS) \
|
||||
$(VORBISENC_LIBS)
|
||||
|
||||
libencoder_plugins_a_SOURCES =
|
||||
|
||||
libencoder_plugins_a_SOURCES += src/encoder_list.c
|
||||
libencoder_plugins_a_SOURCES += src/encoder/null_encoder.c
|
||||
libencoder_plugins_a_SOURCES = \
|
||||
src/encoder/OggStream.hxx \
|
||||
src/encoder/null_encoder.c \
|
||||
src/encoder_list.c
|
||||
|
||||
if ENABLE_WAVE_ENCODER
|
||||
libencoder_plugins_a_SOURCES += src/encoder/wave_encoder.c
|
||||
|
128
src/encoder/OggStream.hxx
Normal file
128
src/encoder/OggStream.hxx
Normal file
@ -0,0 +1,128 @@
|
||||
/*
|
||||
* Copyright (C) 2003-2012 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
|
@ -19,6 +19,7 @@
|
||||
|
||||
#include "config.h"
|
||||
#include "OpusEncoderPlugin.hxx"
|
||||
#include "OggStream.hxx"
|
||||
|
||||
extern "C" {
|
||||
#include "encoder_api.h"
|
||||
@ -59,11 +60,9 @@ struct opus_encoder {
|
||||
|
||||
unsigned char buffer2[1275 * 3 + 7];
|
||||
|
||||
ogg_stream_state os;
|
||||
OggStream stream;
|
||||
|
||||
ogg_int64_t packetno;
|
||||
|
||||
bool flush;
|
||||
};
|
||||
|
||||
gcc_const
|
||||
@ -196,13 +195,9 @@ opus_encoder_open(struct encoder *_encoder,
|
||||
encoder->buffer_position = 0;
|
||||
encoder->buffer = (unsigned char *)g_malloc(encoder->buffer_size);
|
||||
|
||||
ogg_stream_init(&encoder->os, g_random_int());
|
||||
encoder->stream.Initialize(g_random_int());
|
||||
encoder->packetno = 0;
|
||||
|
||||
/* set "flush" to true, so the caller gets the full headers on
|
||||
the first read() */
|
||||
encoder->flush = true;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -211,7 +206,7 @@ opus_encoder_close(struct encoder *_encoder)
|
||||
{
|
||||
struct opus_encoder *encoder = (struct opus_encoder *)_encoder;
|
||||
|
||||
ogg_stream_clear(&encoder->os);
|
||||
encoder->stream.Deinitialize();
|
||||
g_free(encoder->buffer);
|
||||
opus_encoder_destroy(encoder->enc);
|
||||
}
|
||||
@ -247,7 +242,7 @@ opus_encoder_do_encode(struct opus_encoder *encoder, bool eos,
|
||||
packet.e_o_s = eos;
|
||||
packet.granulepos = 0; // TODO
|
||||
packet.packetno = encoder->packetno++;
|
||||
ogg_stream_packetin(&encoder->os, &packet);
|
||||
encoder->stream.PacketIn(packet);
|
||||
|
||||
encoder->buffer_position = 0;
|
||||
|
||||
@ -259,7 +254,7 @@ opus_encoder_end(struct encoder *_encoder, GError **error_r)
|
||||
{
|
||||
struct opus_encoder *encoder = (struct opus_encoder *)_encoder;
|
||||
|
||||
encoder->flush = true;
|
||||
encoder->stream.Flush();
|
||||
|
||||
memset(encoder->buffer + encoder->buffer_position, 0,
|
||||
encoder->buffer_size - encoder->buffer_position);
|
||||
@ -273,7 +268,7 @@ opus_encoder_flush(struct encoder *_encoder, G_GNUC_UNUSED GError **error)
|
||||
{
|
||||
struct opus_encoder *encoder = (struct opus_encoder *)_encoder;
|
||||
|
||||
encoder->flush = true;
|
||||
encoder->stream.Flush();
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -327,9 +322,8 @@ opus_encoder_generate_head(struct opus_encoder *encoder)
|
||||
packet.e_o_s = false;
|
||||
packet.granulepos = 0;
|
||||
packet.packetno = encoder->packetno++;
|
||||
ogg_stream_packetin(&encoder->os, &packet);
|
||||
|
||||
encoder->flush = true;
|
||||
encoder->stream.PacketIn(packet);
|
||||
encoder->stream.Flush();
|
||||
}
|
||||
|
||||
static void
|
||||
@ -352,47 +346,23 @@ opus_encoder_generate_tags(struct opus_encoder *encoder)
|
||||
packet.e_o_s = false;
|
||||
packet.granulepos = 0;
|
||||
packet.packetno = encoder->packetno++;
|
||||
ogg_stream_packetin(&encoder->os, &packet);
|
||||
encoder->stream.PacketIn(packet);
|
||||
encoder->stream.Flush();
|
||||
|
||||
g_free(comments);
|
||||
|
||||
encoder->flush = true;
|
||||
}
|
||||
|
||||
static size_t
|
||||
opus_encoder_read(struct encoder *_encoder, void *_dest, size_t length)
|
||||
opus_encoder_read(struct encoder *_encoder, void *dest, size_t length)
|
||||
{
|
||||
struct opus_encoder *encoder = (struct opus_encoder *)_encoder;
|
||||
unsigned char *dest = (unsigned char *)_dest;
|
||||
|
||||
if (encoder->packetno == 0)
|
||||
opus_encoder_generate_head(encoder);
|
||||
else if (encoder->packetno == 1)
|
||||
opus_encoder_generate_tags(encoder);
|
||||
|
||||
ogg_page page;
|
||||
int result = ogg_stream_pageout(&encoder->os, &page);
|
||||
if (result == 0 && encoder->flush) {
|
||||
encoder->flush = false;
|
||||
result = ogg_stream_flush(&encoder->os, &page);
|
||||
|
||||
}
|
||||
|
||||
if (result == 0)
|
||||
return 0;
|
||||
|
||||
assert(page.header_len > 0 || page.body_len > 0);
|
||||
|
||||
size_t nbytes = (size_t)page.header_len + (size_t)page.body_len;
|
||||
|
||||
if (nbytes > length)
|
||||
/* XXX better error handling */
|
||||
MPD_ERROR("buffer too small");
|
||||
|
||||
memcpy(dest, page.header, page.header_len);
|
||||
memcpy(dest + page.header_len, page.body, page.body_len);
|
||||
|
||||
return nbytes;
|
||||
return encoder->stream.PageOut(dest, length);
|
||||
}
|
||||
|
||||
static const char *
|
||||
|
@ -19,6 +19,7 @@
|
||||
|
||||
#include "config.h"
|
||||
#include "VorbisEncoderPlugin.hxx"
|
||||
#include "OggStream.hxx"
|
||||
|
||||
extern "C" {
|
||||
#include "encoder_api.h"
|
||||
@ -49,13 +50,11 @@ struct vorbis_encoder {
|
||||
|
||||
struct audio_format audio_format;
|
||||
|
||||
ogg_stream_state os;
|
||||
|
||||
vorbis_dsp_state vd;
|
||||
vorbis_block vb;
|
||||
vorbis_info vi;
|
||||
|
||||
bool flush;
|
||||
OggStream stream;
|
||||
};
|
||||
|
||||
static inline GQuark
|
||||
@ -177,7 +176,7 @@ vorbis_encoder_reinit(struct vorbis_encoder *encoder, GError **error)
|
||||
|
||||
vorbis_analysis_init(&encoder->vd, &encoder->vi);
|
||||
vorbis_block_init(&encoder->vd, &encoder->vb);
|
||||
ogg_stream_init(&encoder->os, g_random_int());
|
||||
encoder->stream.Initialize(g_random_int());
|
||||
|
||||
return true;
|
||||
}
|
||||
@ -190,9 +189,9 @@ vorbis_encoder_headerout(struct vorbis_encoder *encoder, vorbis_comment *vc)
|
||||
vorbis_analysis_headerout(&encoder->vd, vc,
|
||||
&packet, &comments, &codebooks);
|
||||
|
||||
ogg_stream_packetin(&encoder->os, &packet);
|
||||
ogg_stream_packetin(&encoder->os, &comments);
|
||||
ogg_stream_packetin(&encoder->os, &codebooks);
|
||||
encoder->stream.PacketIn(packet);
|
||||
encoder->stream.PacketIn(comments);
|
||||
encoder->stream.PacketIn(codebooks);
|
||||
}
|
||||
|
||||
static void
|
||||
@ -221,17 +220,13 @@ vorbis_encoder_open(struct encoder *_encoder,
|
||||
|
||||
vorbis_encoder_send_header(encoder);
|
||||
|
||||
/* set "flush" to true, so the caller gets the full headers on
|
||||
the first read() */
|
||||
encoder->flush = true;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static void
|
||||
vorbis_encoder_clear(struct vorbis_encoder *encoder)
|
||||
{
|
||||
ogg_stream_clear(&encoder->os);
|
||||
encoder->stream.Deinitialize();
|
||||
vorbis_block_clear(&encoder->vb);
|
||||
vorbis_dsp_clear(&encoder->vd);
|
||||
vorbis_info_clear(&encoder->vi);
|
||||
@ -254,7 +249,7 @@ vorbis_encoder_blockout(struct vorbis_encoder *encoder)
|
||||
|
||||
ogg_packet packet;
|
||||
while (vorbis_bitrate_flushpacket(&encoder->vd, &packet))
|
||||
ogg_stream_packetin(&encoder->os, &packet);
|
||||
encoder->stream.PacketIn(packet);
|
||||
}
|
||||
}
|
||||
|
||||
@ -263,7 +258,7 @@ vorbis_encoder_flush(struct encoder *_encoder, G_GNUC_UNUSED GError **error)
|
||||
{
|
||||
struct vorbis_encoder *encoder = (struct vorbis_encoder *)_encoder;
|
||||
|
||||
encoder->flush = true;
|
||||
encoder->stream.Flush();
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -282,7 +277,7 @@ vorbis_encoder_pre_tag(struct encoder *_encoder, G_GNUC_UNUSED GError **error)
|
||||
vorbis_analysis_init(&encoder->vd, &encoder->vi);
|
||||
vorbis_block_init(&encoder->vd, &encoder->vb);
|
||||
|
||||
encoder->flush = true;
|
||||
encoder->stream.Flush();
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -311,18 +306,13 @@ vorbis_encoder_tag(struct encoder *_encoder, const struct tag *tag,
|
||||
|
||||
/* reset ogg_stream_state and begin a new stream */
|
||||
|
||||
ogg_stream_reset_serialno(&encoder->os, g_random_int());
|
||||
encoder->stream.Reinitialize(g_random_int());
|
||||
|
||||
/* send that vorbis_comment to the ogg_stream_state */
|
||||
|
||||
vorbis_encoder_headerout(encoder, &comment);
|
||||
vorbis_comment_clear(&comment);
|
||||
|
||||
/* the next vorbis_encoder_read() call should flush the
|
||||
ogg_stream_state */
|
||||
|
||||
encoder->flush = true;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -359,34 +349,11 @@ vorbis_encoder_write(struct encoder *_encoder,
|
||||
}
|
||||
|
||||
static size_t
|
||||
vorbis_encoder_read(struct encoder *_encoder, void *_dest, size_t length)
|
||||
vorbis_encoder_read(struct encoder *_encoder, void *dest, size_t length)
|
||||
{
|
||||
struct vorbis_encoder *encoder = (struct vorbis_encoder *)_encoder;
|
||||
unsigned char *dest = (unsigned char *)_dest;
|
||||
|
||||
ogg_page page;
|
||||
int ret = ogg_stream_pageout(&encoder->os, &page);
|
||||
if (ret == 0 && encoder->flush) {
|
||||
encoder->flush = false;
|
||||
ret = ogg_stream_flush(&encoder->os, &page);
|
||||
|
||||
}
|
||||
|
||||
if (ret == 0)
|
||||
return 0;
|
||||
|
||||
assert(page.header_len > 0 || page.body_len > 0);
|
||||
|
||||
size_t nbytes = (size_t)page.header_len + (size_t)page.body_len;
|
||||
|
||||
if (nbytes > length)
|
||||
/* XXX better error handling */
|
||||
MPD_ERROR("buffer too small");
|
||||
|
||||
memcpy(dest, page.header, page.header_len);
|
||||
memcpy(dest + page.header_len, page.body, page.body_len);
|
||||
|
||||
return nbytes;
|
||||
return encoder->stream.PageOut(dest, length);
|
||||
}
|
||||
|
||||
static const char *
|
||||
|
Loading…
Reference in New Issue
Block a user