avcpp 3.2.1
Wrapper for the FFmpeg that simplify usage from C++ projects.
Loading...
Searching...
No Matches
frame.h
Go to the documentation of this file.
1#pragma once
2
9
10#include <vector>
11
12#include "avcompat.h"
13#include "avcpp/buffer.h"
14#include "avcpp/dictionary.h"
15
16#if AVCPP_CXX_STANDARD >= 20
17#if __has_include(<span>)
18#include <span>
19#endif
20#endif
21
22#include "ffmpeg.h"
23#include "hwcontext.h"
24#include "rational.h"
25#include "timestamp.h"
26#include "pixelformat.h"
27#include "sampleformat.h"
28
29extern "C" {
30#include <libavutil/hwcontext.h>
31#include <libavutil/imgutils.h>
32#include <libavutil/attributes.h>
33}
34
35namespace av
36{
37
38namespace frame
39{
50int64_t get_best_effort_timestamp(const AVFrame* frame);
61uint64_t get_channel_layout(const AVFrame* frame);
68void set_channel_layout(AVFrame* frame, uint64_t layout);
69} // ::av::frame
70
71#if AVCPP_HAS_FRAME_SIDE_DATA
75class FrameSideData : public FFWrapperPtr<AVFrameSideData>
76{
77public:
78 using FFWrapperPtr<AVFrameSideData>::FFWrapperPtr;
79
85 std::string_view name() const noexcept;
91 AVFrameSideDataType type() const noexcept;
97 std::span<const uint8_t> span() const noexcept;
103 std::span<uint8_t> span() noexcept;
104
110 BufferRefView buffer() const noexcept;
111
116 Dictionary metadata() const noexcept;
117
126 static std::string_view name(AVFrameSideDataType type) noexcept;
127
128#if AVCPP_API_HAS_AVSIDEDATADESCRIPTOR
134 std::optional<AVSideDataDescriptor> descriptor() const noexcept;
142 static std::optional<AVSideDataDescriptor> descriptor(AVFrameSideDataType type) noexcept;
143#endif // AVCPP_API_HAS_AVSIDEDATADESCRIPTOR
144
150 bool empty() const noexcept;
156 operator bool() const noexcept;
157};
158#endif // AVCPP_HAS_PKT_SIDE_DATA
159
172class FrameCommon : public FFWrapperPtr<AVFrame>
173{
174public:
178 struct wrap_data {};
184
196 FrameCommon(const AVFrame *frame);
197
198 // Helper ctors to implement move/copy ctors
211
212 ~FrameCommon();
213
219 bool isReferenced() const;
225 int refCount() const;
232 AVFrame* makeRef() const;
233
239 Timestamp pts() const;
249 attribute_deprecated void setPts(int64_t pts, Rational ptsTimeBase);
255 void setPts(const Timestamp &ts);
256
262 const Rational& timeBase() const;
269 void setTimeBase(const Rational &value);
270
276 int streamIndex() const;
283
295 bool isComplete() const;
296
302 bool isValid() const;
303
309 operator bool() const;
310
317 bool isHwFrame() const;
318
329 uint8_t *data(size_t plane = 0);
337 const uint8_t *data(size_t plane = 0) const;
338
346 size_t size(size_t plane) const;
352 size_t size() const;
353
357 void dump() const;
358
359 // You must implement operators in deveritive classes using assignOperator() and moveOperator()
363 void operator=(const FrameCommon&) = delete;
364
370 void swap(FrameCommon &other);
371
372#if AVCPP_HAS_FRAME_SIDE_DATA
379 const FrameSideData sideData(AVFrameSideDataType type) const;
387 FrameSideData sideData(AVFrameSideDataType type);
388
393 std::size_t sideDataCount() const noexcept;
394
400 FrameSideData sideDataIndex(std::size_t index) noexcept;
401
409 ArrayView<AVFrameSideData*, FrameSideData, std::size_t> sideData() noexcept;
415 ArrayView<const AVFrameSideData*, FrameSideData, std::size_t> sideData() const noexcept;
416
417
422 void sideDataRemove(AVFrameSideDataType type) noexcept;
423
431 FrameSideData addSideData(AVFrameSideDataType type, std::span<const uint8_t> data, OptionalErrorCode ec = throws());
432
442 FrameSideData addSideData(AVFrameSideDataType type, std::span<uint8_t> data, wrap_data, OptionalErrorCode ec = throws());
443
453 FrameSideData addSideData(AVFrameSideDataType type, std::span<uint8_t> data, wrap_data_static, OptionalErrorCode ec = throws());
454
463 FrameSideData addSideData(AVFrameSideDataType type, BufferRef buf, OptionalErrorCode ec = throws());
464
472 FrameSideData allocateSideData(AVFrameSideDataType type, std::size_t size, OptionalErrorCode ec = throws());
473#endif
474
475protected:
481 void copyInfoFrom(const FrameCommon& other);
488 void clone(FrameCommon& dst, size_t align = 1) const;
489
490protected:
496 bool m_isComplete {false};
497};
498
507template<typename T>
508class Frame : public FrameCommon
509{
510protected:
518 T& assignOperator(const T &rhs) {
519 if (this == &rhs)
520 return static_cast<T&>(*this);
521 T(rhs).swap(static_cast<T&>(*this));
522 return static_cast<T&>(*this);
523 }
524
532 T& moveOperator(T &&rhs) {
533 if (this == &rhs)
534 return static_cast<T&>(*this);
535 T(std::move(rhs)).swap(static_cast<T&>(*this));
536 return static_cast<T&>(*this);
537 }
538
539public:
541
547 static T null() {
548 return T(nullptr);
549 }
550
558 T clone(size_t align = 1) const {
559 T result;
560 FrameCommon::clone(result, align);
561 return result;
562 }
563};
564
565static_assert(std::is_copy_assignable_v<FrameCommon> == false);
566static_assert(std::is_copy_constructible_v<FrameCommon> == true);
567static_assert(std::is_move_assignable_v<FrameCommon> == false);
568static_assert(std::is_move_constructible_v<FrameCommon> == true);
569
577class VideoFrame : public Frame<VideoFrame>
578{
579public:
581
585 VideoFrame() = default;
594 VideoFrame(PixelFormat pixelFormat, int width, int height, int align = 1);
605 VideoFrame(const uint8_t *data, size_t size, PixelFormat pixelFormat, int width, int height, int align = 1);
606
612 VideoFrame(const VideoFrame &other);
619
636
648 int width() const;
654 int height() const;
655
674
680 bool isKeyFrame() const;
686 void setKeyFrame(bool isKey);
687
693 int quality() const;
700
706 AVPictureType pictureType() const;
713 void setPictureType(AVPictureType type = AV_PICTURE_TYPE_NONE);
714
727
736 size_t bufferSize(int align = 1, OptionalErrorCode ec = throws()) const;
747 bool copyToBuffer(uint8_t *dst, size_t size, int align = 1, OptionalErrorCode ec = throws());
757 bool copyToBuffer(std::vector<uint8_t>& dst, int align = 1, OptionalErrorCode ec = throws());
758
770 VideoFrame clone(size_t align = 1, OptionalErrorCode ec = throws()) const;
771
786 VideoFrame toSystemMemory(PixelFormat format = AV_PIX_FMT_NONE,
787 OptionalErrorCode ec = throws()) const;
788
800 OptionalErrorCode ec = throws()) const;
801
813 int flags = AV_HWFRAME_MAP_READ,
814 OptionalErrorCode ec = throws()) const;
815
821
826 hw::DeviceContext hwDeviceContext() const noexcept;
827
828
843 static VideoFrame wrap(const void *data, size_t size, PixelFormat pixelFormat, int width, int height, int align = 1);
844
845#if AVCPP_CXX_STANDARD >= 20
858 static VideoFrame wrap(std::span<const std::byte> data, PixelFormat pixelFormat, int width, int height, int align = 1);
859
874 static VideoFrame wrap(std::span<const std::byte> data, void (*deleter)(void *opaque, uint8_t *data),
875 void *opaque, PixelFormat pixelFormat, int width, int height, int align = 1);
876#endif
877};
878
879static_assert(std::is_copy_assignable_v<VideoFrame> == true);
880static_assert(std::is_copy_constructible_v<VideoFrame> == true);
881static_assert(std::is_move_assignable_v<VideoFrame> == true);
882static_assert(std::is_move_constructible_v<VideoFrame> == true);
883
891class AudioSamples : public Frame<AudioSamples>
892{
893public:
895
899 AudioSamples() = default;
921 AudioSamples(const uint8_t *data, size_t size,
922 SampleFormat sampleFormat, int samplesCount, uint64_t channelLayout, int sampleRate, int align = SampleFormat::AlignDefault);
923
936
953
965 int init(SampleFormat sampleFormat, int samplesCount, uint64_t channelLayout, int sampleRate, int align = SampleFormat::AlignDefault);
966
978 int samplesCount() const;
984 int channelsCount() const;
990 uint64_t channelsLayout() const;
996 int sampleRate() const;
1004 size_t sampleBitDepth(OptionalErrorCode ec = throws()) const;
1010 bool isPlanar() const;
1011
1018 std::string channelsLayoutString() const;
1019};
1020
1021static_assert(std::is_copy_assignable_v<AudioSamples> == true);
1022static_assert(std::is_copy_constructible_v<AudioSamples> == true);
1023static_assert(std::is_move_assignable_v<AudioSamples> == true);
1024static_assert(std::is_move_constructible_v<AudioSamples> == true);
1025
1026
1027} // ::av
1028
FFmpeg version and API compatibility switches.
Reference counted buffers: BufferRef and Buffer over AVBufferRef.
C-array (FFmpeg) wrapper witch can iterate over collection and represents any element via light-weigh...
Definition avutils.h:862
AudioSamples(SampleFormat sampleFormat, int samplesCount, uint64_t channelLayout, int sampleRate, int align=SampleFormat::AlignDefault)
Allocate an empty buffer with the given layout.
uint64_t channelsLayout() const
Channel layout as a bit mask.
SampleFormat sampleFormat() const
Sample format of the buffer.
AudioSamples & operator=(AudioSamples &&rhs)
Move assignment.
std::string channelsLayoutString() const
Human-readable name of the channel layout.
AudioSamples(const AudioSamples &other)
Copy constructor: references the same sample buffer.
AudioSamples(AudioSamples &&other)
Move constructor.
size_t sampleBitDepth(OptionalErrorCode ec=throws()) const
Number of bits per sample.
int channelsCount() const
Number of audio channels.
int sampleRate() const
Sample rate of the buffer.
bool isPlanar() const
Whether the channels are stored in separate planes.
int samplesCount() const
Number of samples per channel.
AudioSamples(const uint8_t *data, size_t size, SampleFormat sampleFormat, int samplesCount, uint64_t channelLayout, int sampleRate, int align=SampleFormat::AlignDefault)
Copy external sample data into a buffer.
AudioSamples()=default
Create an invalid buffer.
AudioSamples & operator=(const AudioSamples &rhs)
Copy assignment: references the buffer of the other samples.
int init(SampleFormat sampleFormat, int samplesCount, uint64_t channelLayout, int sampleRate, int align=SampleFormat::AlignDefault)
Allocate the buffer storage.
Non-owning view for the nested AVBufferRef.
Definition buffer.h:32
Light weight wrapper for the FFmpeg AVBufferRef functionality.
Definition buffer.h:136
Implements interface to access to the AVDictionary entity.
Definition dictionary.h:38
void sideDataRemove(AVFrameSideDataType type) noexcept
Remove and free all side data instances of the given type.
bool m_isComplete
Completeness flag, set through setComplete().
Definition frame.h:496
std::size_t sideDataCount() const noexcept
Return count of the side data elements.
void setPts(const Timestamp &ts)
Set the timestamp.
FrameCommon()
Create an invalid frame.
void copyInfoFrom(const FrameCommon &other)
Copy the avcpp bookkeeping (time base, stream index, completeness).
void setComplete(bool isComplete)
Mark the frame as complete or incomplete.
Rational m_timeBase
Time base of the timestamps in this frame.
Definition frame.h:492
bool isComplete() const
Whether the frame is marked complete.
FrameSideData sideData(AVFrameSideDataType type)
Mutable access to the side data of the given type.
uint8_t * data(size_t plane=0)
Pointer to one image or audio plane.
int refCount() const
Number of references to the frame buffer.
FrameSideData sideDataIndex(std::size_t index) noexcept
Get side data element by index.
FrameSideData allocateSideData(AVFrameSideDataType type, std::size_t size, OptionalErrorCode ec=throws())
Allocate storage for the packet side data of the given type and return reference to it.
FrameCommon(FrameCommon &&other)
Move constructor.
int streamIndex() const
Index of the stream the frame belongs to.
attribute_deprecated void setPts(int64_t pts, Rational ptsTimeBase)
Set the timestamp and its time base at once.
bool isValid() const
Whether the wrapper holds a frame.
const uint8_t * data(size_t plane=0) const
Pointer to one plane, read-only.
bool isHwFrame() const
Whether the frame lives in device memory.
void dump() const
Print the frame layout through the FFmpeg log system.
const Rational & timeBase() const
Time base of the frame.
AVFrame * makeRef() const
Create an independent reference to the frame.
bool isReferenced() const
Whether the frame data is reference counted.
void swap(FrameCommon &other)
Exchange the contents of two frames.
const FrameSideData sideData(AVFrameSideDataType type) const
Get packet side data of the given type.
void setTimeBase(const Rational &value)
Set the time base.
size_t size() const
Total size of all planes.
void setStreamIndex(int streamIndex)
Set the stream index.
FrameCommon(const AVFrame *frame)
Copy the frame metadata into a new wrapper.
void operator=(const FrameCommon &)=delete
Assignment is deleted: derived classes implement it to avoid slicing.
FrameCommon(const FrameCommon &other)
Copy constructor: references the same buffer.
size_t size(size_t plane) const
Size of one plane.
FrameSideData addSideData(AVFrameSideDataType type, std::span< const uint8_t > data, OptionalErrorCode ec=throws())
Add side data of the given type into packet.
int m_streamIndex
Index of the stream the frame belongs to; -1 when unset.
Definition frame.h:494
void clone(FrameCommon &dst, size_t align=1) const
Make a deep copy of the frame data into another frame object.
Timestamp pts() const
Presentation timestamp.
Simple view for the AVFrameSideData elements.
Definition frame.h:76
std::optional< AVSideDataDescriptor > descriptor() const noexcept
Descriptor of this side data type, when the FFmpeg build provides one.
AVFrameSideDataType type() const noexcept
Identifier of the side data type.
Dictionary metadata() const noexcept
metadata
std::string_view name() const noexcept
Name of the side data type, as FFmpeg spells it.
BufferRefView buffer() const noexcept
The buffer that owns the payload.
std::span< const uint8_t > span() const noexcept
Read-only view of the payload.
bool empty() const noexcept
Whether the entry carries no payload.
CRTP base that gives a frame type its copy and move assignment.
Definition frame.h:509
T & moveOperator(T &&rhs)
Move the other frame into this one.
Definition frame.h:532
FrameCommon()
Create an invalid frame.
T clone(size_t align=1) const
Deep copy of the frame.
Definition frame.h:558
T & assignOperator(const T &rhs)
Copy the other frame into this one.
Definition frame.h:518
static T null()
An invalid frame of this type.
Definition frame.h:547
Carries the caller's choice between exceptions and error codes.
Definition averror.h:96
The PixelFormat class is a simple wrapper for AVPixelFormat that allow to acces it it properties.
Definition pixelformat.h:41
Rational number: a numerator and a denominator.
Definition rational.h:49
The SampleFormat class is a simple proxy class for AVSampleFormat.
Definition sampleformat.h:35
@ AlignDefault
Align each line to the interval FFmpeg prefers.
Definition sampleformat.h:45
The Timestamp class represents timestamp value and it timebase.
Definition timestamp.h:25
VideoFrame()=default
Create an invalid frame.
VideoFrame toHwFrame(const hw::FramesContext &frames, OptionalErrorCode ec=throws()) const
Upload a software frame into hardware memory.
VideoFrame(VideoFrame &&other)
Move constructor.
VideoFrame(const VideoFrame &other)
Copy constructor: references the same frame buffer.
VideoFrame & operator=(VideoFrame &&rhs)
Move assignment.
VideoFrame mapTo(const hw::FramesContext &frames, int flags=AV_HWFRAME_MAP_READ, OptionalErrorCode ec=throws()) const
Map this hardware frame into another hardware frames context.
bool isKeyFrame() const
Whether the frame is a key frame.
PixelFormat adjustFromJpegPixelFormat()
Convert JPEG pixel format to the compatible non-JPEG one.
hw::DeviceContext hwDeviceContext() const noexcept
Device context used by this hardware frame, or an invalid context.
VideoFrame(const uint8_t *data, size_t size, PixelFormat pixelFormat, int width, int height, int align=1)
Copy external image data into a frame.
hw::FramesContext hwFramesContext() const noexcept
Frames context of this hardware frame, or an invalid context.
VideoFrame & operator=(const VideoFrame &rhs)
Copy assignment: references the buffer of the other frame.
void setQuality(int quality)
Attach a quality value to the frame.
static VideoFrame wrap(std::span< const std::byte > data, PixelFormat pixelFormat, int width, int height, int align=1)
Wrap external data into VideoFrame object ready to use with FFmpeg/AvCpp.
static VideoFrame wrap(const void *data, size_t size, PixelFormat pixelFormat, int width, int height, int align=1)
Wrap external data into VideoFrame object ready to use with FFmpeg/AvCpp.
static VideoFrame wrap(std::span< const std::byte > data, void(*deleter)(void *opaque, uint8_t *data), void *opaque, PixelFormat pixelFormat, int width, int height, int align=1)
Wrap external data into VideoFrame object ready to use with FFmpeg/AvCpp.
Rational sampleAspectRatio() const
Pixel aspect ratio of the frame.
bool copyToBuffer(std::vector< uint8_t > &dst, int align=1, OptionalErrorCode ec=throws())
Copy the image into a vector, resizing it as needed.
VideoFrame(PixelFormat pixelFormat, int width, int height, int align=1)
Allocate an empty frame for the given format and size.
void setSampleAspectRatio(const Rational &sampleAspectRatio)
Set the pixel aspect ratio.
PixelFormat adjustToJpegPixelFormat()
Convert non-JPEG pixel format to the compatible JPEG one.
size_t bufferSize(int align=1, OptionalErrorCode ec=throws()) const
Size of the buffer needed to hold the frame in one contiguous block.
VideoFrame toSystemMemory(PixelFormat format=AV_PIX_FMT_NONE, OptionalErrorCode ec=throws()) const
Transfer a hardware frame into system memory.
bool copyToBuffer(uint8_t *dst, size_t size, int align=1, OptionalErrorCode ec=throws())
Copy the image into a caller-provided buffer.
int height() const
Frame height.
int width() const
Frame width.
void setPictureType(AVPictureType type=AV_PICTURE_TYPE_NONE)
Set the picture type.
PixelFormat pixelFormat() const
Pixel format of the frame.
void setKeyFrame(bool isKey)
Mark the frame as a key frame.
VideoFrame clone(size_t align=1, OptionalErrorCode ec=throws()) const
Deep copy of a software frame.
int quality() const
Quality value attached to the frame.
AVPictureType pictureType() const
Picture type assigned by the decoder or expected by the encoder.
Owner of an AVHWFramesContext.
Definition hwcontext.h:242
AVDictionary wrappers: Dictionary and DictionaryArray.
FFmpeg types re-exported for the public API and small value wrappers.
int64_t get_best_effort_timestamp(const AVFrame *frame)
Timestamp FFmpeg considers the best estimate of the presentation time.
void set_channel_layout(AVFrame *frame, uint64_t layout)
Set the channel layout of a frame from a bit mask.
uint64_t get_channel_layout(const AVFrame *frame)
Channel layout of a frame as a bit mask.
Hardware devices, hardware frames and hardware codec configuration.
Root namespace of the library.
Definition audioresampler.h:19
OptionalErrorCode throws()
Helper to construct null OptionalErrorCode object.
Definition averror.h:310
STL namespace.
Pixel formats.
Rational numbers, time bases and arithmetic on them.
Audio sample formats.
Wrap static data, do not owning and free.
Definition frame.h:183
Wrap data and take owning.
Definition frame.h:178
Frame timestamps and their arithmetic.