StormByte C++ Library: Multimedia module 0.0.9999
StormByte-Multimedia is a StormByte library module for parsing configuration files
Loading...
Searching...
No Matches
encoder.hxx
Go to the documentation of this file.
1/*
2 * Copyright (C) 2024-2026 David C. Manuelda (StormBytePP)
3 *
4 * This file is part of StormByte-Multimedia.
5 *
6 * StormByte-Multimedia original source is dual-licensed:
7 *
8 * 1. GNU Lesser General Public License v3.0 (or later)
9 * You may redistribute and/or modify this file under the terms of the
10 * GNU Lesser General Public License as published by the Free Software
11 * Foundation, either version 3 of the License, or (at your option)
12 * any later version.
13 *
14 * 2. Commercial license
15 * Alternatively, this file may be used under the terms of a commercial
16 * license agreement with the copyright holder
17 * (David C. Manuelda <StormByte@gmail.com>).
18 *
19 * Both licenses apply only to original StormByte-Multimedia source in this
20 * file. Third-party components — including FFmpeg and embedded trained data —
21 * remain under their own licenses and are not covered by the commercial grant.
22 *
23 * Neither license grants any patent rights. Any patent licenses required
24 * to use this software or third-party components must be obtained separately
25 * from the patent holders.
26 *
27 * StormByte-Multimedia is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU Lesser General Public License for more details.
31 *
32 * You should have received a copy of the GNU Lesser General Public License
33 * version 3 along with StormByte-Multimedia. If not, see
34 * <https://www.gnu.org/licenses/lgpl-3.0.html>.
35 *
36 * SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-StormByte-Commercial
37 */
38
39#pragma once
40
41#include <StormByte/buffer/fifo.hxx>
42#include <StormByte/logger/log.hxx>
50
51#include <cstddef>
52#include <cstdint>
53#include <map>
54#include <memory>
55#include <optional>
56#include <string>
57#include <vector>
58
66 class Encoder;
67 class Packet;
68}
69
71 class Encode;
72}
73
81 class Encoder;
82 class Frame;
83 class Muxer;
84
104 friend class Backend::Pipeline::Encoder;
105 friend class Backend::Pipeline::Detail::Worker::Encode;
106 friend class Muxer;
107
108 public:
120 Encoder(std::shared_ptr<StormByte::Logger::Log> log,
121 int output_index, const Codec& codec) noexcept;
122
127 Encoder(const Encoder& other) = delete;
128
133 Encoder(Encoder&& other) noexcept = delete;
134
138 ~Encoder() noexcept override;
139
145 Encoder& operator=(const Encoder& other) = delete;
146
152 Encoder& operator=(Encoder&& other) noexcept = delete;
153
167 explicit operator bool() const noexcept;
168
173 inline int Index() const noexcept {
174 return m_index;
175 }
176
181 std::size_t InputCeiling() const noexcept override {
182 return Ceiling;
183 }
184
189 inline const Codec& Destination() const noexcept {
190 return *m_codec;
191 }
192
197 bool Opened() const noexcept;
198
203 std::optional<int> AudioChannels() const noexcept;
204
209 std::optional<int> AudioSampleRate() const noexcept;
210
215 std::optional<int> AudioFrameSize() const noexcept;
216
221 std::optional<int> AudioSampleFormat() const noexcept;
222
236 inline const std::string& EncoderTag() const noexcept {
237 return m_encoderTag;
238 }
239
253 inline const std::optional<std::string>& Implementation() const noexcept {
254 return m_implementation;
255 }
256
261 void Implementation(std::string name) noexcept;
262
267 inline const Features& Require() const noexcept {
268 return m_require;
269 }
270
275 inline void Require(Features features) noexcept {
276 m_require = features;
277 }
278
283 inline const Features& Capabilities() const noexcept {
284 return m_capabilities;
285 }
286
300 inline void CRF(int value) noexcept {
301 m_crf = value;
302 }
303
308 inline const std::optional<int>& CRF() const noexcept {
309 return m_crf;
310 }
311
316 inline void BitRate(std::int64_t bits_per_second) noexcept {
317 m_bitRate = bits_per_second;
318 }
319
324 inline const std::optional<std::int64_t>& BitRate() const noexcept {
325 return m_bitRate;
326 }
327
332 inline void MaxBitRate(std::int64_t bits_per_second) noexcept {
333 m_maxBitRate = bits_per_second;
334 }
335
340 inline const std::optional<std::int64_t>& MaxBitRate() const noexcept {
341 return m_maxBitRate;
342 }
343
348 void Preset(std::string name) noexcept;
349
354 inline const std::optional<std::string>& Preset() const noexcept {
355 return m_preset;
356 }
357
362 void Tune(std::string name) noexcept;
363
368 inline const std::optional<std::string>& Tune() const noexcept {
369 return m_tune;
370 }
371
385 inline const std::map<std::string, std::string>& FineTune() const noexcept {
386 return m_fineTune;
387 }
388
393 inline void FineTune(std::map<std::string, std::string> options) noexcept {
394 m_fineTune = std::move(options);
395 }
396
401 private:
407 using Step::Log;
408
414 std::string Label() const noexcept override;
415
439 Packet::PointerType Wrap(
440 enum StormByte::Multimedia::Type type, int index,
441 StormByte::Buffer::FIFO payload,
442 std::optional<Property::Duration> pts,
443 std::optional<Property::Duration> dts,
444 std::optional<Property::Duration> duration,
445 bool keyFrame,
446 std::vector<SideData> attachments,
447 std::unique_ptr<Backend::Pipeline::Packet> backend) noexcept;
448
454 bool MuxBindStream(void* avStream) noexcept;
455
461 void* FrameHandle(Frame& frame) noexcept;
462
468 const void* FrameHandle(const Frame& frame) noexcept;
469
474 void Emit(Packet::PointerType packet) noexcept;
475
476 static constexpr std::size_t Ceiling = 64;
477 int m_index;
478 const Codec* m_codec;
479 std::string m_encoderTag;
480 std::optional<std::string> m_implementation;
481 Features m_require;
482 Features m_capabilities;
483 std::optional<int> m_crf;
484 std::optional<std::int64_t> m_bitRate;
485 std::optional<std::int64_t> m_maxBitRate;
486 std::optional<std::string> m_preset;
487 std::optional<std::string> m_tune;
488 std::map<std::string, std::string> m_fineTune;
489 std::unique_ptr<Backend::Pipeline::Encoder> m_backend;
490 std::optional<std::uint64_t> m_serial;
491 std::uint64_t m_part;
492 Join m_join{*this};
493 };
494}
Immutable codec identity owned by Registry.
Definition codec.hxx:58
Bitmask of Feature flags.
Definition features.hxx:56
Encodes frames of one output track into packets.
Definition encoder.hxx:103
void Tune(std::string name) noexcept
Content tune.
Encoder(Encoder &&other) noexcept=delete
Move constructor.
void Preset(std::string name) noexcept
Encoder preset.
const std::optional< int > & CRF() const noexcept
CRF/CQ, if set.
Definition encoder.hxx:308
void FineTune(std::map< std::string, std::string > options) noexcept
Replaces the vendor dict (before open).
Definition encoder.hxx:393
Encoder(const Encoder &other)=delete
Copy constructor.
Encoder(std::shared_ptr< StormByte::Logger::Log > log, int output_index, const Codec &codec) noexcept
Encoder for output track output_index and destination codec.
const Features & Capabilities() const noexcept
Features of the row selected at open.
Definition encoder.hxx:283
const std::optional< std::string > & Tune() const noexcept
Content tune, if set.
Definition encoder.hxx:368
const std::optional< std::string > & Preset() const noexcept
Preset, if set.
Definition encoder.hxx:354
void CRF(int value) noexcept
Constant quality (CRF/CQ).
Definition encoder.hxx:300
const std::map< std::string, std::string > & FineTune() const noexcept
Vendor leftovers.
Definition encoder.hxx:385
const std::optional< std::int64_t > & MaxBitRate() const noexcept
VBV ceiling, if set.
Definition encoder.hxx:340
~Encoder() noexcept override
Destructor.
bool Opened() const noexcept
Whether the backend finished Open().
void Implementation(std::string name) noexcept
Pins an FFmpeg encoder name.
const Features & Require() const noexcept
Extra features the caller demands besides Frame HDR.
Definition encoder.hxx:267
const std::optional< std::string > & Implementation() const noexcept
Pinned FFmpeg encoder name, if any.
Definition encoder.hxx:253
void Require(Features features) noexcept
Replaces the extra feature mask (before open).
Definition encoder.hxx:275
void BitRate(std::int64_t bits_per_second) noexcept
Target bitrate in bits per second.
Definition encoder.hxx:316
void MaxBitRate(std::int64_t bits_per_second) noexcept
VBV ceiling in bits per second.
Definition encoder.hxx:332
const Codec & Destination() const noexcept
Destination codec.
Definition encoder.hxx:189
const std::optional< std::int64_t > & BitRate() const noexcept
Target bitrate, if set.
Definition encoder.hxx:324
std::size_t InputCeiling() const noexcept override
Ceiling of the encoder input hopper.
Definition encoder.hxx:181
One decoded access unit.
Definition frame.hxx:76
Writes interleaved packets to a destination container.
Definition muxer.hxx:150
One compressed access unit.
Definition packet.hxx:76
One side-data blob attached to a Frame or Packet.
Definition side_data.hxx:90
One stage with a Backend::Pipeline::Pipe (in / out).
Definition step.hxx:114
Multimedia-owned pipeline stages and unit holders.
Demux / decode / filter / encode / mux types.
@ Frame
StormByte::Multimedia::Pipeline::Frame
@ Packet
StormByte::Multimedia::Pipeline::Packet
Type
Kind of media stream or codec.
Definition type.hxx:55
#define STORMBYTE_MULTIMEDIA_PUBLIC
Imported symbol.
Definition visibility.h:47