StormByte C++ Library: Multimedia module 0.0.9999
StormByte-Multimedia is a StormByte library module for parsing configuration files
Loading...
Searching...
No Matches
AVEncoder.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
47
48#include <cstdint>
49#include <map>
50#include <string>
51#include <vector>
52
59 class AVFormatContext;
60 class AVFrame;
61 class AVPacket;
62 class AVSubtitle;
63
68 class STORMBYTE_MULTIMEDIA_PUBLIC AVEncoder: public AVPointer<::AVCodecContext> {
69 public:
73 AVEncoder(const AVEncoder&) = delete;
74
79 AVEncoder(AVEncoder&& other) noexcept = default;
80
84 ~AVEncoder() noexcept override;
85
90 AVEncoder& operator=(const AVEncoder&) = delete;
91
97 AVEncoder& operator=(AVEncoder&& other) noexcept = default;
98
107 static ExpectedAVEncoder Open(AVCodec* codec, const AVCodecParameters& params, const AVFormatContext& fmt, int stream_index) noexcept;
108
118 static ExpectedAVEncoder Open(AVCodec* codec, const AVCodecParameters& params, int stream_index,
119 const std::map<std::string, std::string>& options, AVRational time_base) noexcept;
120
130
138
147
152 int StreamIndex() const noexcept;
153
158 AVRational TimeBase() const noexcept;
159
164 bool IsSubtitle() const noexcept;
165
169 void Flush() noexcept;
170
176
181 explicit operator bool() const noexcept;
182
187 int CodecId() const noexcept;
188
193 int CodecType() const noexcept;
194
199 int SampleFmt() const noexcept;
200
205 int FrameSize() const noexcept;
206
211 int Channels() const noexcept;
212
217 int SampleRate() const noexcept;
218
224
229 bool HasBFrames() const noexcept;
230
235 AVRational FrameRate() const noexcept;
236
241 const ::AVCodecContext* Context() const noexcept;
242
243 private:
244 int m_stream_index = -1;
245 FFmpeg::AVBSFPipeline m_bsf_pipeline;
246 std::map<std::int64_t, std::vector<std::uint8_t>> m_hdrPlusT35;
247
252 explicit AVEncoder(AVCodecContext* ctx) noexcept;
253
257 void Free() noexcept override;
258
259 using AVPointer<::AVCodecContext>::Get;
260 };
261
262 extern template class STORMBYTE_MULTIMEDIA_PUBLIC AVPointer<::AVCodecContext>;
263}
Ordered chain of bitstream filters applied to packets.
Definition AVBSFPipeline.hxx:57
RAII AVChannelLayout (av_channel_layout_copy / uninit).
Definition AVChannelLayout.hxx:66
Deep-copying RAII wrapper for AVCodecParameters.
Definition AVCodecParameters.hxx:59
AVRational FrameRate() const noexcept
Video framerate guessed by the encoder.
static ExpectedAVEncoder Open(AVCodec *codec, const AVCodecParameters &params, const AVFormatContext &fmt, int stream_index) noexcept
Opens an encoder; may attach BSF from fmt.
int FrameSize() const noexcept
Encoder frame size in samples.
int Channels() const noexcept
Audio channel count.
int StreamIndex() const noexcept
Stream index this encoder was opened for.
AVEncoder(AVEncoder &&other) noexcept=default
Move constructor.
int SampleRate() const noexcept
Audio sample rate in Hz.
bool HasBFrames() const noexcept
Whether the codec uses B-frames.
AVEncoder(const AVEncoder &)=delete
Copy constructor (deleted).
int CodecId() const noexcept
Opened codec id.
FFmpeg::OperationResult ReceivePacket(AVPacket &pkt) noexcept
Receives an encoded packet (after BSF).
int CodecType() const noexcept
Media type of the opened codec.
const ::AVCodecContext * Context() const noexcept
Opened codec context for mux stream bind.
~AVEncoder() noexcept override
Destructor.
void Flush() noexcept
Flushes encoder and BSF.
FFmpeg::OperationResult SetEof() noexcept
Signals EOF to encoder and BSF.
int SampleFmt() const noexcept
Audio sample format.
AVChannelLayout ChannelLayout() const noexcept
Channel layout owned by the context.
AVRational TimeBase() const noexcept
Encoder time base.
bool IsSubtitle() const noexcept
true if the opened codec is a subtitle encoder.
FFmpeg::OperationResult SendFrame(AVFrame &frame) noexcept
Sends a frame to the encoder.
FFmpeg::OperationResult EncodeSubtitle(AVSubtitle &sub, AVPacket &pkt) noexcept
Encodes one text subtitle (avcodec_encode_subtitle).
RAII input format context (demuxer).
Definition AVFormatContext.hxx:70
RAII owner of a libav AVFrame.
Definition AVFrame.hxx:98
RAII owner of a libav AVPacket.
Definition AVPacket.hxx:83
constexpr const std::decay_t< ::AVCodecContext > * Get() const noexcept
Definition AVPointer.hxx:118
RAII wrapper for AVSubtitle (avsubtitle_free).
Definition AVSubtitle.hxx:66
Pair {num, den} matching libavutil AVRational.
Definition av_rational.hxx:62
Incomplete FFmpeg types for RAII headers.
Private RAII wrappers over libav*.
OperationResult
Result of send/receive style FFmpeg calls.
Definition typedefs.hxx:56
StormByte::Expected< AVEncoder, FFmpeg::EncoderError > ExpectedAVEncoder
Open encoder.
Definition typedefs.hxx:71
#define STORMBYTE_MULTIMEDIA_PUBLIC
Imported symbol.
Definition visibility.h:47