StormByte C++ Library: Multimedia module 0.0.9999
StormByte-Multimedia is a StormByte library module for parsing configuration files
Loading...
Searching...
No Matches
ffmpeg.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/logger/log.hxx>
51
52#include <atomic>
53#include <condition_variable>
54#include <cstddef>
55#include <cstdint>
56#include <deque>
57#include <memory>
58#include <mutex>
59#include <optional>
60#include <string>
61#include <string_view>
62
64 class Host;
65 class Pipe;
66 class Pumper;
67 class Worker;
68}
69
71 class Filter;
72}
73
75 class Filters;
76 class Route;
77}
78
84 class ProcessTwoPasses;
85
256 friend class StormByte::Multimedia::Backend::Pipeline::Detail::Worker::Filter;
257 friend class ProcessTwoPasses;
258
259 public:
264
268 FFmpeg(const FFmpeg& other) = delete;
269
273 FFmpeg(FFmpeg&& other) noexcept = delete;
274
279 virtual ~FFmpeg() noexcept;
280
284 FFmpeg& operator=(const FFmpeg& other) = delete;
285
289 FFmpeg& operator=(FFmpeg&& other) noexcept = delete;
290
294
299
304 virtual enum StormByte::Multimedia::Type Media() const noexcept = 0;
305
310 std::string Name() const noexcept;
311
316 inline const std::string& Leaf() const noexcept {
317 return m_name;
318 }
319
324 inline const Pipeline::Kinds& Receives() const noexcept {
325 return m_receives;
326 }
327
332 inline const Pipeline::Kinds& Produces() const noexcept {
333 return m_produces;
334 }
335
339
344
351 virtual void Clean() noexcept = 0;
352
359 virtual void Setup() noexcept = 0;
360
375 virtual void Process(const Pipeline::Frame& frame) noexcept;
376
384 virtual void Process(const Pipeline::Packet& packet) noexcept;
385
393 virtual class Report Report() const noexcept;
394
399 Pipeline::State Status() const noexcept;
400
405 bool Failed() const noexcept;
406
411 const std::optional<std::string>& Error() const noexcept;
412
421 virtual std::size_t InputCeiling() const noexcept;
422
426
427 protected:
432
445 FFmpeg(std::shared_ptr<StormByte::Logger::Log> log,
446 std::string name, Pipeline::Kinds receives, Pipeline::Kinds produces) noexcept;
447
451
461 void Log(StormByte::Logger::Level level, std::string_view message) noexcept;
462
467 void Fail(std::string reason) noexcept;
468
480 void Hold(std::uint8_t n) noexcept;
481
489 void Release() noexcept;
490
495 bool Held() const noexcept;
496
501 std::uint8_t HeldFor() const noexcept;
502
510 virtual void Eof() noexcept;
511
523 virtual void LastChance(const Pipeline::Frame& frame) noexcept;
524
529 virtual void LastChance(const Pipeline::Packet& packet) noexcept;
530
545 const StormByte::Multimedia::FFmpeg::AVFrame& AVFrame() const noexcept;
546
561 const StormByte::Multimedia::FFmpeg::AVPacket& AVPacket() const noexcept;
562
581 void Save(StormByte::Multimedia::FFmpeg::AVFrame&& incoming) noexcept;
582
594 void Save(StormByte::Multimedia::FFmpeg::AVPacket&& incoming) noexcept;
595
596 private:
597 class Surface;
598
602 void Open() noexcept;
603
608 void Work(Pipeline::Item::PointerType item) noexcept;
609
614 void Finish() noexcept;
615
619 void Park() noexcept;
620
624 void CallLastChance() noexcept;
625
629 void Emit(Pipeline::Item::PointerType item) noexcept;
630
635 void Wait() noexcept;
636
640 void Launch() noexcept;
641
645 void Halt() noexcept;
646
650 void Stop() noexcept;
651
655 bool Stopping() const noexcept;
656
660 Backend::Pipeline::Host& Face() noexcept;
661
665 std::condition_variable& Wake() noexcept;
666
670 void CloseHoppers() noexcept;
671
676 void RecordWork(std::int64_t microseconds) noexcept;
677
681 void DumpWork() noexcept;
682
689 bool MeasuringTwoPass() const noexcept;
690
691 std::shared_ptr<StormByte::Logger::Log> m_log;
692 std::string m_name;
693 Pipeline::Kinds m_receives;
694 Pipeline::Kinds m_produces;
695
696 private:
697 std::condition_variable m_wake;
698 std::unique_ptr<Backend::Pipeline::Pipe> m_pipe;
699
700 protected:
705 Backend::Pipeline::Pipe& pipe() noexcept;
706
711 const Backend::Pipeline::Pipe& pipe() const noexcept;
712
713 std::unique_ptr<Surface> m_surface;
714 std::unique_ptr<Backend::Pipeline::Pumper> m_pumper;
715 std::mutex m_wait;
716 std::optional<std::string> m_error;
718 std::uint64_t m_workN;
719 std::int64_t m_workMin;
720 std::int64_t m_workMax;
721 std::int64_t m_lastWork;
722 Pipeline::Item::PointerType m_current;
723 std::deque<Pipeline::Item::PointerType> m_queue;
724 std::uint8_t m_hold;
725 std::uint8_t m_heldFor;
726 std::shared_ptr<const Pipeline::Plan> m_plan;
728 };
729
754 public:
758 Process(const Process& other) = delete;
759
763 Process(Process&& other) noexcept = delete;
764
768 ~Process() noexcept override = default;
769
773 Process& operator=(const Process& other) = delete;
774
778 Process& operator=(Process&& other) noexcept = delete;
779
780 protected:
786 Process(std::shared_ptr<StormByte::Logger::Log> log, std::string name) noexcept;
787
795 Process(std::shared_ptr<StormByte::Logger::Log> log, std::string name,
796 Pipeline::Kinds receives, Pipeline::Kinds produces) noexcept;
797 };
798
813 friend class FFmpeg;
814
815 public:
819 ProcessTwoPasses(const ProcessTwoPasses& other) = delete;
820
824 ProcessTwoPasses(ProcessTwoPasses&& other) noexcept = delete;
825
829 ~ProcessTwoPasses() noexcept override = default;
830
834 ProcessTwoPasses& operator=(const ProcessTwoPasses& other) = delete;
835
839 ProcessTwoPasses& operator=(ProcessTwoPasses&& other) noexcept = delete;
840
841 protected:
847 ProcessTwoPasses(std::shared_ptr<StormByte::Logger::Log> log, std::string name) noexcept;
848
856 ProcessTwoPasses(std::shared_ptr<StormByte::Logger::Log> log, std::string name,
857 Pipeline::Kinds receives, Pipeline::Kinds produces) noexcept;
858
865 bool Measuring() const noexcept;
866
871 virtual void Measure(const Pipeline::Frame& frame) noexcept = 0;
872
880 virtual void Measured() noexcept;
881
882 private:
886 void EnterMeasure() noexcept;
887
891 void LeaveMeasure() noexcept;
892
897 void BindMeasure(StormByte::Multimedia::Pipeline::Filters* owner) noexcept;
898
906 void MeasureSourceClosed() noexcept;
907
914 void DrainMeasure() noexcept;
915
916 bool m_measuring = false;
917 std::atomic<bool> m_measureClosed{false};
918 std::atomic<bool> m_measureDrained{false};
919 StormByte::Multimedia::Pipeline::Filters* m_measureOwner = nullptr;
920 };
921
931 public:
935 Packet(const Packet& other) = delete;
936
940 Packet(Packet&& other) noexcept = delete;
941
945 ~Packet() noexcept override = default;
946
950 Packet& operator=(const Packet& other) = delete;
951
955 Packet& operator=(Packet&& other) noexcept = delete;
956
957 protected:
963 Packet(std::shared_ptr<StormByte::Logger::Log> log, std::string name) noexcept;
964
972 Packet(std::shared_ptr<StormByte::Logger::Log> log, std::string name,
973 Pipeline::Kinds receives, Pipeline::Kinds produces) noexcept;
974 };
975
1004 public:
1008 Analytics(const Analytics& other) = delete;
1009
1013 Analytics(Analytics&& other) noexcept = delete;
1014
1018 ~Analytics() noexcept override = default;
1019
1023 Analytics& operator=(const Analytics& other) = delete;
1024
1028 Analytics& operator=(Analytics&& other) noexcept = delete;
1029
1030 protected:
1038 Analytics(std::shared_ptr<StormByte::Logger::Log> log, std::string name) noexcept;
1039
1047 Analytics(std::shared_ptr<StormByte::Logger::Log> log, std::string name,
1048 Pipeline::Kinds receives, Pipeline::Kinds produces) noexcept;
1049
1054 void Save(StormByte::Multimedia::FFmpeg::AVFrame&& incoming) noexcept = delete;
1055
1060 void Save(StormByte::Multimedia::FFmpeg::AVPacket&& incoming) noexcept = delete;
1061
1066 void Hold(std::uint8_t n) noexcept = delete;
1067
1071 void Release() noexcept = delete;
1072
1076 bool Held() const noexcept = delete;
1077
1081 std::uint8_t HeldFor() const noexcept = delete;
1082
1087 void LastChance(const Pipeline::Frame& frame) noexcept override {
1088 (void)frame;
1089 }
1090
1095 void LastChance(const Pipeline::Packet& packet) noexcept override {
1096 (void)packet;
1097 }
1098 };
1099}
void Save(StormByte::Multimedia::FFmpeg::AVFrame &&incoming) noexcept=delete
Deleted.
Analytics(const Analytics &other)=delete
Copy is not allowed.
void LastChance(const Pipeline::Packet &packet) noexcept override
Unused on Analytics.
Definition ffmpeg.hxx:1095
void LastChance(const Pipeline::Frame &frame) noexcept override
Unused on Analytics.
Definition ffmpeg.hxx:1087
void Hold(std::uint8_t n) noexcept=delete
Deleted.
bool Held() const noexcept=delete
Deleted.
Analytics(Analytics &&other) noexcept=delete
Move is not allowed.
std::uint8_t HeldFor() const noexcept=delete
Deleted.
~Analytics() noexcept override=default
Releases only filter resources.
const Pipeline::Kinds & Produces() const noexcept
Kinds this filter emits.
Definition ffmpeg.hxx:332
virtual void Clean() noexcept=0
Drops per-run state.
virtual void Eof() noexcept
Filter-visible end-of-input hook.
Pipeline::Item::PointerType m_current
Unit of this Work call.
Definition ffmpeg.hxx:722
std::uint8_t m_hold
Ceiling; 0 = not Held.
Definition ffmpeg.hxx:724
std::unique_ptr< Surface > m_surface
Host for Pumper and Worker.
Definition ffmpeg.hxx:713
const std::optional< std::string > & Error() const noexcept
Failure text.
virtual class Report Report() const noexcept
Measurement, if any.
std::int64_t m_workMax
Slowest Process, us.
Definition ffmpeg.hxx:720
std::shared_ptr< const Pipeline::Plan > m_plan
Job plan; Filters copies it.
Definition ffmpeg.hxx:726
virtual void Setup() noexcept=0
Acquires per-run state.
std::uint8_t HeldFor() const noexcept
Units currently parked in the Hold queue.
Backend::Pipeline::Pipe & pipe() noexcept
In / out hoppers of this filter.
std::uint64_t m_workN
Timed Process calls.
Definition ffmpeg.hxx:718
void Save(StormByte::Multimedia::FFmpeg::AVFrame &&incoming) noexcept
Replaces the media of the current frame.
bool m_exhausted
Source Ended(); unused on Through.
Definition ffmpeg.hxx:717
std::int64_t m_workMin
Fastest Process, us.
Definition ffmpeg.hxx:719
Pipeline::State Status() const noexcept
Current lifecycle value.
void Fail(std::string reason) noexcept
Marks a hard error and wakes the filter.
bool Failed() const noexcept
Whether this filter has failed.
void Log(StormByte::Logger::Level level, std::string_view message) noexcept
Writes one log line on the scoped module logger.
virtual void Process(const Pipeline::Frame &frame) noexcept
Frame body.
bool Held() const noexcept
Whether this filter is inside a Hold.
std::deque< Pipeline::Item::PointerType > m_queue
Thread-local Hold.
Definition ffmpeg.hxx:723
friend class ProcessTwoPasses
Definition ffmpeg.hxx:257
const StormByte::Multimedia::FFmpeg::AVPacket & AVPacket() const noexcept
Borrowed RAII handle of the current packet.
virtual std::size_t InputCeiling() const noexcept
Ceiling of this filter's input hopper.
void Release() noexcept
Ends the Hold and replays parked units through Process, then Emits them.
void Hold(std::uint8_t n) noexcept
Starts a Hold of at most n units.
std::mutex m_wait
Mutex for m_wake.
Definition ffmpeg.hxx:715
FFmpeg(const FFmpeg &other)=delete
Copy is not allowed.
virtual void LastChance(const Pipeline::Frame &frame) noexcept
Last look before the Hold must end.
int m_origin
Plan track In(); -1 until bound.
Definition ffmpeg.hxx:727
std::uint8_t m_heldFor
Parked count.
Definition ffmpeg.hxx:725
std::unique_ptr< Backend::Pipeline::Pumper > m_pumper
Thread and State.
Definition ffmpeg.hxx:714
std::int64_t m_lastWork
Last Process, us.
Definition ffmpeg.hxx:721
const Pipeline::Kinds & Receives() const noexcept
Kinds this filter consumes.
Definition ffmpeg.hxx:324
virtual enum StormByte::Multimedia::Type Media() const noexcept=0
Media this filter handles.
std::optional< std::string > m_error
Fail message.
Definition ffmpeg.hxx:716
const std::string & Leaf() const noexcept
Leaf token used in reports (vmaf).
Definition ffmpeg.hxx:316
std::string Name() const noexcept
Media()/name computed on each call.
FFmpeg(FFmpeg &&other) noexcept=delete
Move is not allowed.
const StormByte::Multimedia::FFmpeg::AVFrame & AVFrame() const noexcept
Borrowed RAII handle of the current frame.
Mutates compressed packets.
Definition ffmpeg.hxx:930
Packet(const Packet &other)=delete
Copy is not allowed.
~Packet() noexcept override=default
Releases only filter resources.
Packet(Packet &&other) noexcept=delete
Move is not allowed.
Process that measures the origin before it rewrites.
Definition ffmpeg.hxx:811
virtual void Measure(const Pipeline::Frame &frame) noexcept=0
First pass.
ProcessTwoPasses(const ProcessTwoPasses &other)=delete
Copy is not allowed.
ProcessTwoPasses(ProcessTwoPasses &&other) noexcept=delete
Move is not allowed.
virtual void Measured() noexcept
After measure EoF, before the second read.
~ProcessTwoPasses() noexcept override=default
Releases only filter resources.
bool Measuring() const noexcept
Whether the tube is in the measure pass.
friend class FFmpeg
Definition ffmpeg.hxx:813
friend class StormByte::Multimedia::Pipeline::Filters
Definition ffmpeg.hxx:812
Process(Process &&other) noexcept=delete
Move is not allowed.
Process(const Process &other)=delete
Copy is not allowed.
~Process() noexcept override=default
Releases only filter resources.
Optional facade: Between stretches, Add filters, Close.
Definition filters.hxx:102
One decoded access unit.
Definition frame.hxx:81
Facade shared by Frame and Packet.
Definition item.hxx:96
Bitmask of Kind.
Definition typedefs.hxx:148
One compressed access unit.
Definition packet.hxx:80
Closed job intention: source File, destination container and tracks that enter the tube.
Definition plan.hxx:76
Multimedia-owned pipeline stages and unit holders.
Definition AVFormatContext.hxx:52
Definition AVFormatContext.hxx:52
Forward so this RAII type can friend the filter base.
Definition AVFrame.hxx:63
Demux / decode / filter / encode / mux types.
State
Lifecycle of the stage thread.
Definition typedefs.hxx:211
@ Route
Route
Definition typedefs.hxx:110
@ Filter
Filter::FFmpeg
Definition typedefs.hxx:109
Public multimedia types: codecs, containers, streams and files.
Type
Kind of media stream or codec.
Definition type.hxx:55
#define STORMBYTE_MULTIMEDIA_PUBLIC
Imported symbol.
Definition visibility.h:47