27#include <libavformat/avformat.h>
74 virtual int write(
const uint8_t *data,
size_t size)
76 static_cast<void>(data);
77 static_cast<void>(size);
96 virtual int read(uint8_t *data,
size_t size)
98 static_cast<void>(data);
99 static_cast<void>(size);
117 virtual int64_t
seek(int64_t offset,
int whence)
119 static_cast<void>(offset);
120 static_cast<void>(whence);
138 virtual const char*
name()
const {
return ""; }
152class FormatContext :
public FFWrapperPtr<AVFormatContext>,
public noncopyable
154#if AVCPP_CXX_STANDARD >= 20
155 struct _StreamTransform {
156 const FormatContext *_parent{};
157 Stream operator()(AVStream *st)
const {
162 using StreamsView = std::ranges::transform_view<std::span<AVStream*>, _StreamTransform>;
281 [[deprecated(
"Codec is not used by the FFmpeg API. Use addStream() without codec and point configured codec context after")]]
313#if AVCPP_CXX_STANDARD >= 20
921 void writeFrame(AVFrame *frame,
int streamIndex,
OptionalErrorCode ec,
int(*write_proc)(AVFormatContext*,
int,AVFrame*));
925 static int avioInterruptCb(
void *opaque);
926 int avioInterruptCb();
927 void setupInterruptHandling();
928 void resetSocketAccess();
930 void closeCodecContexts();
931 int checkPbError(
int stat);
938 std::shared_ptr<char> m_monitor {
new char};
939 std::chrono::time_point<std::chrono::system_clock> m_lastSocketAccess;
940 int64_t m_socketTimeout = -1;
943 bool m_isOpened =
false;
944 bool m_customIO =
false;
945 bool m_streamsInfoFound =
false;
946 bool m_headerWriten =
false;
947 bool m_substractStartTime =
false;
Error codes, error categories and the OptionalErrorCode convention.
Common helpers: comparisons, value lookup, non-copyable base, string utilities.
Encodes audio samples into packets.
Definition codeccontext.h:1782
One buffer of decoded audio samples.
Definition frame.h:892
Base class for audio and video codec contexts.
Definition codeccontext.h:64
Codec descriptor.
Definition codec.h:32
Array of Dictinaries wrapper.
Definition dictionary.h:808
Implements interface to access to the AVDictionary entity.
Definition dictionary.h:38
void openInput(CustomIO *io, Dictionary &&formatOptions, InputFormat format, OptionalErrorCode ec=throws(), size_t internalBufferSize=CUSTOM_IO_DEFAULT_BUFFER_SIZE)
Open an input on a custom IO object with an explicit demuxer and options, moving the dictionary.
void setInterruptCallback(const AvioInterruptCb &cb)
Install a callback that can cancel blocking IO.
void setFormat(const OutputFormat &format)
Force the muxer used by openOutput().
void writeHeader(Dictionary &&options, OptionalErrorCode ec=throws())
Write the container header with muxer options, moving the dictionary.
void close()
Close the container and release its resources.
StreamsView streams(OptionalErrorCode ec=throws()) const
All streams as a range, for range-based for and std algorithms.
void writeUncodedFrameDirect(class VideoFrame &frame, size_t streamIndex, OptionalErrorCode ec=throws())
Write an uncompressed video frame without intercepting FFmpeg errors.
void writeUncodedFrameDirect(class AudioSamples &frame, size_t streamIndex, OptionalErrorCode ec=throws())
Write uncompressed audio samples without intercepting FFmpeg errors.
void writeUncodedFrame(class AudioSamples &frame, size_t streamIndex, OptionalErrorCode ec=throws())
Write uncompressed audio samples through the muxer.
void openInput(CustomIO *io, Dictionary &formatOptions, OptionalErrorCode ec=throws(), size_t internalBufferSize=CUSTOM_IO_DEFAULT_BUFFER_SIZE)
Open an input on a custom IO object with demuxer options.
Definition formatcontext.h:508
OutputFormat outputFormat() const
The muxer in use.
void setSocketTimeout(int64_t timeout)
Abort network operations that block for too long.
Stream addStream(const class VideoEncoderContext &encCtx, OptionalErrorCode ec=throws())
Add a video stream configured from an encoder context.
void openInput(const std::string &uri, OptionalErrorCode ec=throws())
Open an input and probe its container.
void openOutput(const std::string &uri, OutputFormat format, OptionalErrorCode ec=throws())
Open an output with an explicit muxer.
Stream addStream(const Codec &codec, OptionalErrorCode ec=throws())
Add a stream for the given codec.
void openInput(CustomIO *io, Dictionary &&formatOptions, OptionalErrorCode ec=throws(), size_t internalBufferSize=CUSTOM_IO_DEFAULT_BUFFER_SIZE)
Open an input on a custom IO object with demuxer options, moving the dictionary.
Definition formatcontext.h:523
void openInput(CustomIO *io, Dictionary &formatOptions, InputFormat format, OptionalErrorCode ec=throws(), size_t internalBufferSize=CUSTOM_IO_DEFAULT_BUFFER_SIZE)
Open an input on a custom IO object with an explicit demuxer and options.
void writeUncodedFrame(class VideoFrame &frame, size_t streamIndex, OptionalErrorCode ec=throws())
Write an uncompressed video frame through the muxer.
Stream stream(size_t idx, OptionalErrorCode ec)
Stream by index, reporting an out-of-range index as an error.
InputFormat inputFormat() const
The demuxer in use.
bool initOutput(Dictionary &&options, OptionalErrorCode ec=throws())
Init the output with muxer options, moving the dictionary.
void writePacket(const Packet &pkt, OptionalErrorCode ec=throws())
Write one packet.
void findStreamInfo(DictionaryArray &streamsOptions, OptionalErrorCode ec=throws())
Read the stream parameters with per-stream options.
void openInput(CustomIO *io, InputFormat format, OptionalErrorCode ec=throws(), size_t internalBufferSize=CUSTOM_IO_DEFAULT_BUFFER_SIZE)
Open an input on a custom IO object with an explicit demuxer.
bool openOutput(CustomIO *io, Dictionary &&formatOptions, OutputFormat format, OptionalErrorCode ec=throws(), size_t internalBufferSize=CUSTOM_IO_DEFAULT_BUFFER_SIZE)
Open output on a custom IO object with an explicit format and options, moving the dictionary.
void writeTrailer(OptionalErrorCode ec=throws())
Write the container trailer and close the output.
bool initOutput(Dictionary &options, OptionalErrorCode ec=throws())
Init the output with muxer options instead of writing the header.
bool openOutput(CustomIO *io, Dictionary &&formatOptions, OptionalErrorCode ec=throws(), size_t internalBufferSize=CUSTOM_IO_DEFAULT_BUFFER_SIZE)
Open output on a custom IO object with muxer options, moving the dictionary.
void writeHeader(OptionalErrorCode ec=throws())
Write the container header.
void openOutput(const std::string &uri, OptionalErrorCode ec=throws())
Open an output.
Timestamp duration() const noexcept
Duration of the container.
Timestamp startTime() const noexcept
Start time of the container.
bool isOpened() const
Whether the container is open.
static constexpr size_t CUSTOM_IO_DEFAULT_BUFFER_SIZE
Default size of the buffer avcpp allocates around a custom IO object.
Definition formatcontext.h:485
bool openOutput(CustomIO *io, Dictionary &formatOptions, OptionalErrorCode ec=throws(), size_t internalBufferSize=CUSTOM_IO_DEFAULT_BUFFER_SIZE)
Open output on a custom IO object with explicit muxer options.
void openOutput(const std::string &uri, Dictionary &&options, OutputFormat format, OptionalErrorCode ec=throws())
Open an output with an explicit muxer and options, moving the dictionary.
void openOutput(const std::string &uri, Dictionary &options, OutputFormat format, OptionalErrorCode ec=throws())
Open an output with an explicit muxer and options.
void eventFlagsClear(int flags) noexcept
Acknowledge events.
void substractStartTime(bool enable)
Shift timestamps so the file starts at zero.
void findStreamInfo(DictionaryArray &&streamsOptions, OptionalErrorCode ec=throws())
Read the stream parameters with per-stream options, moving them in.
bool initOutput(OptionalErrorCode ec=throws())
Init output without writing the header.
int eventFlags() const noexcept
Flags to the user to detect events happening on the file.
Stream addStream(const class AudioEncoderContext &encCtx, OptionalErrorCode ec=throws())
Add an audio stream configured from an encoder context.
bool checkUncodedFrameWriting(size_t streamIndex, std::error_code &ec) noexcept
Whether an uncompressed frame can be written to a stream.
void writePacket(OptionalErrorCode ec=throws())
Flush the muxer queue.
bool isOutput() const
Whether this context writes instead of reads.
Stream addStream(OptionalErrorCode ec=throws())
Add a stream to an output container.
void openOutput(const std::string &uri, Dictionary &&options, OptionalErrorCode ec=throws())
Open an output with muxer options, moving the dictionary.
void dump() const
Print the container layout through the FFmpeg log system.
void flush()
Discard buffered packets and internal state.
void setFormat(const InputFormat &format)
Force the demuxer used by openInput().
bool checkUncodedFrameWriting(size_t streamIndex) noexcept
Whether an uncompressed frame can be written to a stream.
void findStreamInfo(OptionalErrorCode ec=throws())
Read the stream parameters of an opened input.
void openOutput(CustomIO *io, OutputFormat format, OptionalErrorCode ec=throws(), size_t internalBufferSize=CUSTOM_IO_DEFAULT_BUFFER_SIZE)
Open output on a custom IO object with an explicit output format.
void writePacketDirect(OptionalErrorCode ec=throws())
Flush the muxer queue without intercepting FFmpeg errors.
bool seekable() const noexcept
Whether the container supports seeking.
void writePacketDirect(const Packet &pkt, OptionalErrorCode ec=throws())
Write one packet without intercepting FFmpeg errors.
void seek(const Timestamp ×tamp, OptionalErrorCode ec=throws())
Seek to a timestamp in any stream.
bool openOutput(CustomIO *io, Dictionary &formatOptions, OutputFormat format, OptionalErrorCode ec=throws(), size_t internalBufferSize=CUSTOM_IO_DEFAULT_BUFFER_SIZE)
Open output on a custom IO object with an explicit output format and muxer options.
size_t streamsCount() const
Number of streams in the container.
Stream stream(size_t idx)
Stream by index.
Packet readPacket(OptionalErrorCode ec=throws())
Read the next packet from any stream.
void writeHeader(Dictionary &options, OptionalErrorCode ec=throws())
Write the container header with muxer options.
void openOutput(CustomIO *io, OptionalErrorCode ec=throws(), size_t internalBufferSize=CUSTOM_IO_DEFAULT_BUFFER_SIZE)
Open output on a custom IO object.
void openOutput(const std::string &uri, Dictionary &options, OptionalErrorCode ec=throws())
Open an output with muxer options.
Carries the caller's choice between exceptions and error codes.
Definition averror.h:96
Compressed data and its metadata.
Definition packet.h:110
One stream inside a container.
Definition stream.h:34
The Timestamp class represents timestamp value and it timebase.
Definition timestamp.h:25
Encodes video frames into packets.
Definition codeccontext.h:1338
One video frame.
Definition frame.h:578
Codec descriptors and lookup by name or id.
AVDictionary wrappers: Dictionary and DictionaryArray.
FFmpeg types re-exported for the public API and small value wrappers.
Root namespace of the library.
Definition audioresampler.h:19
OptionalErrorCode throws()
Helper to construct null OptionalErrorCode object.
Definition averror.h:310
@ Encoding
Compressing frames into packets.
Definition codecparameters.h:33
@ Decoding
Decompressing packets into frames.
Definition codecparameters.h:35
std::function< int()> AvioInterruptCb
Callback that can abort a blocking IO operation.
Definition formatcontext.h:39
Compressed packets and their side data.
Streams inside a container: parameters, time base and disposition.
Interface class to customize IO operations with FormatContext.
Definition formatcontext.h:57
virtual int write(const uint8_t *data, size_t size)
Write part of data to the destination.
Definition formatcontext.h:74
virtual const char * name() const
Name of the I/O.
Definition formatcontext.h:138
virtual int seekable() const
Defines supported types of buffer seeking.
Definition formatcontext.h:131
virtual int read(uint8_t *data, size_t size)
Read part of data from the data source.
Definition formatcontext.h:96
virtual int64_t seek(int64_t offset, int whence)
Seek in stream.
Definition formatcontext.h:117