1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
|
/*************************************************************************
OpusEncoder.h - sub encoder base class for Opus in an Ogg container
-------------------
begin : Thu Jan 03 2013
copyright : (C) 2013 by Thomas Eschenbacher
email : Thomas.Eschenbacher@gmx.de
***************************************************************************/
/***************************************************************************
* *
* 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. *
* *
***************************************************************************/
#ifndef OPUS_ENCODER_H
#define OPUS_ENCODER_H
#ifdef HAVE_OGG_OPUS
#include "config.h"
#include <ogg/ogg.h>
#include <opus/opus.h>
#include <opus/opus_multistream.h>
#include "libkwave/FileInfo.h"
#include "libkwave/MultiTrackSink.h"
#include "libkwave/VorbisCommentMap.h"
#include "libkwave/modules/SampleBuffer.h"
#include "OggSubEncoder.h"
#include "OpusHeader.h"
class QIODevice;
class QWidget;
namespace Kwave
{
class ChannelMixer;
class FileInfo;
class MultiTrackReader;
class StreamObject;
class OpusEncoder: public Kwave::OggSubEncoder
{
public:
/**
* Constructor
*/
OpusEncoder();
/** Destructor */
~OpusEncoder() override;
/**
* parse the header of the stream and initialize the decoder
* @param widget a QWidget to be used as parent for error messages
* @param info reference to a FileInfo to fill
* @param src MultiTrackReader used as source of the audio data
* @return true if succeeded, false if failed
*/
virtual bool open(QWidget *widget,
const Kwave::FileInfo &info,
Kwave::MultiTrackReader &src) override;
/**
* write the header information
* @param dst a QIODevice that receives the raw data
* @return true if succeeded, false if failed
*/
bool writeHeader(QIODevice &dst) override;
/**
* encode received ogg data
* @param src MultiTrackReader used as source of the audio data
* @param dst a QIODevice that receives the raw data
* @return true if succeeded, false if failed
*/
virtual bool encode(Kwave::MultiTrackReader &src,
QIODevice &dst) override;
/**
* finished the encoding, clean up
*/
void close() override;
private:
/**
* set up the downmixing mode
* @param widget a QWidget to be used as parent for error messages
* @param tracks number of tracks
* @param bitrate in bits/sec or -1 for "auto"
* @return true if succeeded or false if failed/canceled
*/
bool setupDownMix(QWidget *widget, unsigned int tracks, int bitrate);
/**
* determine the bitrate to use for encoding
* @param widget a QWidget to be used as parent for error messages
* @param tracks number of tracks
* @return true if succeeded or false if failed/canceled
*/
bool setupBitrate(QWidget *widget, unsigned int tracks);
/**
* determine the sample rate to use for encoding
* @param widget a QWidget to be used as parent for error messages
* @param tracks number of tracks
* @param rate sample rate of the original in samples/sec
* @return true if succeeded or false if failed/canceled
*/
bool setupCodingRate(QWidget *widget, unsigned int tracks, double rate);
/**
* set up the encoder, including packet size and channel mapping
* @param widget a QWidget to be used as parent for error messages
* @param tracks number of tracks
* @param rate sample rate of the original in samples/sec
* @return true if succeeded or false if failed/canceled
*/
bool setupEncoder(QWidget *widget, unsigned int tracks, double rate);
/**
* set up the bitrate mode, e.g. VBR, CBR etc
* @param widget a QWidget to be used as parent for error messages
* @return true if succeeded or false if failed/canceled
*/
bool setupBitrateMode(QWidget *widget);
/**
* write the Opus header into the Ogg stream
* @param dst a QIODevice that receives the raw data
* @return true if succeeded or false if failed/canceled
*/
bool writeOpusHeader(QIODevice &dst);
/**
* write the Opus tags into the Ogg stream
* @param dst a QIODevice that receives the raw data
* @return true if succeeded or false if failed/canceled
*/
bool writeOpusTags(QIODevice &dst);
/**
* Write the current ogg page to the destination IO device
* @param dst a QIODevice that receives the raw data
* @return true if succeeded or false if failed/canceled
*/
bool writeOggPage(QIODevice &dst);
/**
* Fill the input buffer of the encoder with samples.
* @param src MultiTrackReader used as source of the audio data
* @return number of samples read
*/
unsigned int fillInBuffer(Kwave::MultiTrackReader &src);
private:
/** map for translating Opus comments to Kwave FileInfo */
Kwave::VorbisCommentMap m_comments_map;
/** file info, set in open(...) */
Kwave::FileInfo m_info;
/** take physical pages, weld into a logical stream of packets */
ogg_stream_state m_os;
/** one Ogg bitstream page. Opus packets are inside */
ogg_page m_og;
/** one raw packet of data for decode */
ogg_packet m_op;
/**
* downmix mode: off, automatic, mono or stereo
*/
enum {
DOWNMIX_OFF = -1, /**< no downmixing */
DOWNMIX_AUTO = 0, /**< automatic, based on bitrate */
DOWNMIX_MONO = 1, /**< downmix to mono */
DOWNMIX_STEREO = 2 /**< downmix to stereo */
} m_downmix;
/** bitrate in bits/sec */
int m_bitrate;
/** encoding sample rate in bits/sec */
int m_coding_rate;
/** number of tracks used for encoding, after downmixing */
unsigned int m_encoder_channels;
/** channel mixer (if downmixing is required) */
Kwave::ChannelMixer *m_channel_mixer;
/** sample rate converter (if needed) */
Kwave::StreamObject *m_rate_converter;
/** frame size in samples */
unsigned int m_frame_size;
/** number of samples to pad at the end to compensate preskip */
unsigned int m_extra_out;
/** Opus header, including channel map */
Kwave::opus_header_t m_opus_header;
/** maximum number of bytes per frame */
unsigned int m_max_frame_bytes;
/** buffer for one packet */
unsigned char *m_packet_buffer;
/** the Opus multistream encoder */
OpusMSEncoder *m_encoder;
/** input buffer of the encoder */
float *m_encoder_input;
/**
* end of the queue that possibly consists of a channel mixer (in
* case of downmixing), a rate converter (if needed) and a buffer
* (if one of the two objects mentioned before is present).
*/
Kwave::StreamObject *m_last_queue_element;
/** multi track buffer, for blockwise reading from the source device */
Kwave::MultiTrackSink<Kwave::SampleBuffer, true> *m_buffer;
};
}
#endif /* HAVE_OGG_OPUS */
#endif /* OPUS_ENCODER_H */
//***************************************************************************
//***************************************************************************
|