avcpp  2.0
Wrapper for the FFmpeg that simplify usage from C++ projects.
packet.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "avcompat.h"
4 
5 #include <vector>
6 #include <type_traits>
7 
8 #if AVCPP_CXX_STANDARD >= 20
9 #include <span>
10 #endif
11 
12 #include "ffmpeg.h"
13 #include "avutils.h"
14 #include "rational.h"
15 #include "stream.h"
16 #include "averror.h"
17 #include "timestamp.h"
18 
19 extern "C" {
20 #include <libavutil/attributes.h>
21 }
22 
23 namespace av {
24 
25 #if AVCPP_HAS_PKT_SIDE_DATA
29 class PacketSideData : public FFWrapperRef<AVPacketSideData>
30 {
31 public:
33 
34  std::string_view name() const noexcept;
35  AVPacketSideDataType type() const noexcept;
36  std::span<const uint8_t> span() const noexcept;
37  std::span<uint8_t> span() noexcept;
38 
39  static std::string_view name(AVPacketSideDataType type);
40 
41  bool empty() const noexcept;
42  operator bool() const noexcept;
43 };
44 #endif // AVCPP_HAS_PKT_SIDE_DATA
45 
46 class Packet :
47 #if AVCPP_API_AVCODEC_NEW_INIT_PACKET
48  public FFWrapperPtr<AVPacket>
49 #else
50  public FFWrapperRef<AVPacket>
51 #endif
52 {
53 private:
54  // if deepCopy true - make deep copy, instead - reference is created
55  void initFromAVPacket(const AVPacket *avpacket, bool deepCopy, OptionalErrorCode ec);
56 
57 public:
61  struct wrap_data {};
66  struct wrap_data_static {};
67 
68 #if AVCPP_API_AVCODEC_NEW_INIT_PACKET
69  explicit Packet(std::nullptr_t); // explicit nullptr packet. There is no extra internal checks, so be careful
70 #endif
71 
72  Packet();
73  Packet(const Packet &packet, OptionalErrorCode ec);
74  Packet(const Packet &packet);
75  Packet(Packet &&packet);
76  explicit Packet(const AVPacket *packet, OptionalErrorCode ec = throws());
77  explicit Packet(const std::vector<uint8_t> &data);
78 
79  Packet(const uint8_t *data, size_t size, bool doAllign = true);
80  // data must be allocated with av_malloc() family
81  Packet(uint8_t *data, size_t size, wrap_data, OptionalErrorCode ec = throws());
82  Packet(uint8_t *data, size_t size, wrap_data_static, OptionalErrorCode ec = throws());
83 
84 #if AVCPP_CXX_STANDARD >= 20
85  explicit Packet(std::span<const uint8_t> data, bool doAllign = true);
86  // data must be allocated with av_malloc() family
87  Packet(std::span<uint8_t> data, wrap_data, OptionalErrorCode ec = throws());
88  Packet(std::span<uint8_t> data, wrap_data_static, OptionalErrorCode ec = throws());
89 #endif
90 
91  ~Packet();
92 
93  bool setData(const std::vector<uint8_t> &newData, OptionalErrorCode ec = throws());
94  bool setData(const uint8_t *newData, size_t size, OptionalErrorCode ec = throws());
95 
96  const uint8_t* data() const;
97  uint8_t* data();
98 
99 #if AVCPP_CXX_STANDARD >= 20
100  bool setData(std::span<const uint8_t> newData, OptionalErrorCode ec = throws());
101  std::span<const uint8_t> span() const;
102  std::span<uint8_t> span();
103 #endif
104 
105  Timestamp pts() const;
106  Timestamp dts() const;
107  Timestamp ts() const;
108  size_t size() const;
109 
117  attribute_deprecated void setPts(int64_t pts, const Rational &tsTimeBase = Rational(0, 0));
118  attribute_deprecated void setDts(int64_t dts, const Rational &tsTimeBase = Rational(0, 0));
119 
120  void setPts(const Timestamp &pts);
121  void setDts(const Timestamp &dts);
122 
123  int streamIndex() const;
124  bool isKeyPacket() const;
125  int duration() const;
126  bool isComplete() const;
127  bool isNull() const;
128 
129  void setStreamIndex(int idx);
130  void setKeyPacket(bool keyPacket);
131  void setDuration(int duration, const Rational &durationTimeBase = Rational(0, 0));
132  void setComplete(bool complete);
133 
134  // Flags
135  int flags() const;
136  void setFlags(int flags);
137  void addFlags(int flags);
138  void clearFlags(int flags);
139 
140 #if AVCPP_HAS_AVFORMAT
141  void dump(const Stream & st, bool dumpPayload = false) const;
142 #endif // if AVCPP_HAS_AVFORMAT
143 
144  const Rational& timeBase() const { return m_timeBase; }
145  void setTimeBase(const Rational &value);
146 
147 #if AVCPP_HAS_PKT_SIDE_DATA
154  std::span<const uint8_t> sideData(AVPacketSideDataType type) const;
155  std::span<uint8_t> sideData(AVPacketSideDataType type);
156 
161  std::size_t sideDataCount() const noexcept;
162 
168  PacketSideData sideDataIndex(std::size_t index) noexcept;
169 
177  ArrayView<AVPacketSideData, PacketSideData, std::size_t> sideData() noexcept;
178  ArrayView<const AVPacketSideData, PacketSideData, std::size_t> sideData() const noexcept;
179 
180  void freeSideData() noexcept;
181 
188  void addSideData(AVPacketSideDataType type, std::span<const uint8_t> data, OptionalErrorCode ec = throws());
189 
198  void addSideData(AVPacketSideDataType type, std::span<uint8_t> data, wrap_data, OptionalErrorCode ec = throws());
199 
207  std::span<uint8_t> allocateSideData(AVPacketSideDataType type, std::size_t size, OptionalErrorCode ec = throws());
208 #endif
209 
210  bool isReferenced() const;
211  int refCount() const;
212 
213 #if AVCPP_API_AVCODEC_NEW_INIT_PACKET
214  AVPacket* makeRef(OptionalErrorCode ec) const;
215 #else
216  AVPacket makeRef(OptionalErrorCode ec = throws()) const;
217 #endif
218 
219  Packet clone(OptionalErrorCode ec = throws()) const;
220 
221  Packet &operator=(const Packet &rhs);
222  Packet &operator=(Packet &&rhs);
223 
224 #if AVCPP_API_AVCODEC_NEW_INIT_PACKET
225  Packet &operator=(const AVPacket *rhs);
226 #else
227  Packet &operator=(const AVPacket &rhs);
228 #endif
229 
230  void swap(Packet &other);
231 
232  operator bool() const { return isComplete(); }
233 
234 private:
235  bool m_completeFlag;
236  Rational m_timeBase;
237 };
238 
239 
240 
241 } // ::av
242 
Definition: averror.h:66
Definition: packet.h:52
const Rational & timeBase() const
Definition: packet.h:144
bool setData(const std::vector< uint8_t > &newData, OptionalErrorCode ec=throws())
Packet(const std::vector< uint8_t > &data)
Definition: rational.h:26
The Timestamp class represents timestamp value and it timebase.
Definition: timestamp.h:18
Definition: audioresampler.cpp:8
OptionalErrorCode throws()
Helper to construct null OptionalErrorCode object.
Definition: averror.h:181
Definition: averror.h:230
Definition: ffmpeg.h:22
Definition: ffmpeg.h:62
Wrap static data, do not owning and free.
Definition: packet.h:66
Wrap data and take owning.
Definition: packet.h:61