File: ogg_encoder.cpp

package info (click to toggle)
snapcast 0.31.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,012 kB
  • sloc: cpp: 37,729; python: 2,543; sh: 455; makefile: 16
file content (280 lines) | stat: -rw-r--r-- 9,605 bytes parent folder | download | duplicates (2)
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/***
    This file is part of snapcast
    Copyright (C) 2014-2024  Johannes Pohl

    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 3 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, see <http://www.gnu.org/licenses/>.
***/

// prototype/interface header file
#include "ogg_encoder.hpp"

// local headers
#include "common/aixlog.hpp"
#include "common/snap_exception.hpp"
#include "common/str_compat.hpp"
#include "common/utils/string_utils.hpp"

// standard headers
#include <cstring>
#include <iostream>


using namespace std;

namespace encoder
{

static constexpr auto LOG_TAG = "OggEnc";

OggEncoder::OggEncoder(const std::string& codecOptions) : Encoder(codecOptions), lastGranulepos_(0)
{
}


OggEncoder::~OggEncoder()
{
    ogg_stream_clear(&os_);
    vorbis_block_clear(&vb_);
    vorbis_dsp_clear(&vd_);
    vorbis_comment_clear(&vc_);
    vorbis_info_clear(&vi_);
}


std::string OggEncoder::getAvailableOptions() const
{
    return "VBR:[-0.1 - 1.0]";
}


std::string OggEncoder::getDefaultOptions() const
{
    return "VBR:0.9";
}


std::string OggEncoder::name() const
{
    return "ogg";
}


void OggEncoder::encode(const msg::PcmChunk& chunk)
{
    double res = 0;
    // LOG(TRACE, LOG_TAG) << "payload: " << chunk->payloadSize << "\tframes: " << chunk->getFrameCount() << "\tduration: " <<
    // chunk->duration<chronos::msec>().count()
    // << "\n";
    int frames = chunk.getFrameCount();
    float** buffer = vorbis_analysis_buffer(&vd_, frames);

    /* uninterleave samples */
    for (size_t channel = 0; channel < sampleFormat_.channels(); ++channel)
    {
        if (sampleFormat_.sampleSize() == 1)
        {
            const auto* chunkBuffer = reinterpret_cast<int8_t*>(chunk.payload);
            for (int i = 0; i < frames; i++)
                buffer[channel][i] = chunkBuffer[sampleFormat_.channels() * i + channel] / 128.f;
        }
        else if (sampleFormat_.sampleSize() == 2)
        {
            const auto* chunkBuffer = reinterpret_cast<int16_t*>(chunk.payload);
            for (int i = 0; i < frames; i++)
                buffer[channel][i] = chunkBuffer[sampleFormat_.channels() * i + channel] / 32768.f;
        }
        else if (sampleFormat_.sampleSize() == 4)
        {
            const auto* chunkBuffer = reinterpret_cast<int32_t*>(chunk.payload);
            for (int i = 0; i < frames; i++)
                buffer[channel][i] = chunkBuffer[sampleFormat_.channels() * i + channel] / 2147483648.f;
        }
    }

    /* tell the library how much we actually submitted */
    vorbis_analysis_wrote(&vd_, frames);

    auto oggChunk = make_shared<msg::PcmChunk>(chunk.format, 0);

    /* vorbis does some data preanalysis, then divvies up blocks for
    more involved (potentially parallel) processing.  Get a single
    block for encoding now */
    size_t pos = 0;
    while (vorbis_analysis_blockout(&vd_, &vb_) == 1)
    {
        /* analysis, assume we want to use bitrate management */
        vorbis_analysis(&vb_, nullptr);
        vorbis_bitrate_addblock(&vb_);

        while (vorbis_bitrate_flushpacket(&vd_, &op_) != 0)
        {
            /* weld the packet into the bitstream */
            ogg_stream_packetin(&os_, &op_);

            /* write out pages (if any) */
            while (true)
            {
                int result = ogg_stream_flush(&os_, &og_);
                if (result == 0)
                    break;
                res = os_.granulepos - lastGranulepos_;

                size_t nextLen = pos + og_.header_len + og_.body_len;
                // make chunk larger
                if (oggChunk->payloadSize < nextLen)
                    oggChunk->payload = static_cast<char*>(realloc(oggChunk->payload, nextLen));

                memcpy(oggChunk->payload + pos, og_.header, og_.header_len);
                pos += og_.header_len;
                memcpy(oggChunk->payload + pos, og_.body, og_.body_len);
                pos += og_.body_len;

                if (ogg_page_eos(&og_) != 0)
                    break;
            }
        }
    }

    if (res > 0)
    {
        res /= sampleFormat_.msRate();
        // LOG(INFO, LOG_TAG) << "res: " << res << "\n";
        lastGranulepos_ = os_.granulepos;
        // make oggChunk smaller
        oggChunk->payload = static_cast<char*>(realloc(oggChunk->payload, pos));
        oggChunk->payloadSize = pos;
        encoded_callback_(*this, oggChunk, res);
    }
}


void OggEncoder::initEncoder()
{
    if (codecOptions_.find(':') == string::npos)
        throw SnapException("Invalid codec options: \"" + codecOptions_ + "\"");
    string mode = utils::string::trim_copy(codecOptions_.substr(0, codecOptions_.find(':')));
    if (mode != "VBR")
        throw SnapException("Unsupported codec mode: \"" + mode + R"(". Available: "VBR")");

    string qual = utils::string::trim_copy(codecOptions_.substr(codecOptions_.find(':') + 1));
    double quality = 1.0;
    try
    {
        quality = cpt::stod(qual);
    }
    catch (...)
    {
        throw SnapException("Invalid codec option: \"" + codecOptions_ + "\"");
    }
    if ((quality < -0.1) || (quality > 1.0))
    {
        throw SnapException("compression level has to be between -0.1 and 1.0");
    }

    LOG(INFO, LOG_TAG) << "Init - quality: " << quality << "\n";

    /********** Encode setup ************/
    vorbis_info_init(&vi_);

    /* choose an encoding mode.  A few possibilities commented out, one
     actually used: */

    /*********************************************************************
    Encoding using a VBR quality mode.  The usable range is -.1
    (lowest quality, smallest file) to 1. (highest quality, largest file).
    Example quality mode .4: 44kHz stereo coupled, roughly 128kbps VBR

    ret = vorbis_encode_init_vbr(&vi,2,44100,.4);

    ---------------------------------------------------------------------

    Encoding using an average bitrate mode (ABR).
    example: 44kHz stereo coupled, average 128kbps VBR

    ret = vorbis_encode_init(&vi,2,44100,-1,128000,-1);

    ---------------------------------------------------------------------

    Encode using a quality mode, but select that quality mode by asking for
    an approximate bitrate.  This is not ABR, it is true VBR, but selected
    using the bitrate interface, and then turning bitrate management off:

    ret = ( vorbis_encode_setup_managed(&vi,2,44100,-1,128000,-1) ||
               vorbis_encode_ctl(&vi,OV_ECTL_RATEMANAGE2_SET,NULL) ||
               vorbis_encode_setup_init(&vi));

    *********************************************************************/

    int ret = vorbis_encode_init_vbr(&vi_, sampleFormat_.channels(), sampleFormat_.rate(), quality);

    /* do not continue if setup failed; this can happen if we ask for a
     mode that libVorbis does not support (eg, too low a bitrate, etc,
     will return 'OV_EIMPL') */

    if (ret != 0)
        throw SnapException("failed to init encoder");

    /* add a comment */
    vorbis_comment_init(&vc_);
    vorbis_comment_add_tag(&vc_, "TITLE", "SnapStream");
    vorbis_comment_add_tag(&vc_, "VERSION", VERSION);
    vorbis_comment_add_tag(&vc_, "SAMPLE_FORMAT", sampleFormat_.toString().c_str());

    /* set up the analysis state and auxiliary encoding storage */
    vorbis_analysis_init(&vd_, &vi_);
    vorbis_block_init(&vd_, &vb_);

    /* set up our packet->stream encoder */
    /* pick a random serial number; that way we can more likely build
     chained streams just by concatenation */
    srand(time(nullptr));
    ogg_stream_init(&os_, rand());

    /* Vorbis streams begin with three headers; the initial header (with
     most of the codec setup parameters) which is mandated by the Ogg
     bitstream spec.  The second header holds any comment fields.  The
     third header holds the bitstream codebook.  We merely need to
     make the headers, then pass them to libvorbis one at a time;
     libvorbis handles the additional Ogg bitstream constraints */

    ogg_packet header;
    ogg_packet header_comm;
    ogg_packet header_code;

    vorbis_analysis_headerout(&vd_, &vc_, &header, &header_comm, &header_code);
    ogg_stream_packetin(&os_, &header);
    ogg_stream_packetin(&os_, &header_comm);
    ogg_stream_packetin(&os_, &header_code);

    /* This ensures the actual
     * audio data will start on a new page, as per spec
     */
    size_t pos(0);
    headerChunk_.reset(new msg::CodecHeader("ogg"));
    while (true)
    {
        int result = ogg_stream_flush(&os_, &og_);
        if (result == 0)
            break;
        headerChunk_->payloadSize += og_.header_len + og_.body_len;
        headerChunk_->payload = static_cast<char*>(realloc(headerChunk_->payload, headerChunk_->payloadSize));
        LOG(DEBUG, LOG_TAG) << "HeadLen: " << og_.header_len << ", bodyLen: " << og_.body_len << ", result: " << result << "\n";
        memcpy(headerChunk_->payload + pos, og_.header, og_.header_len);
        pos += og_.header_len;
        memcpy(headerChunk_->payload + pos, og_.body, og_.body_len);
        pos += og_.body_len;
    }
}

} // namespace encoder