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  Packet();
69  Packet(const Packet &packet, OptionalErrorCode ec);
70  Packet(const Packet &packet);
71  Packet(Packet &&packet);
72  explicit Packet(const AVPacket *packet, OptionalErrorCode ec = throws());
73  explicit Packet(const std::vector<uint8_t> &data);
74 
75  Packet(const uint8_t *data, size_t size, bool doAllign = true);
76  // data must be allocated with av_malloc() family
77  Packet(uint8_t *data, size_t size, wrap_data, OptionalErrorCode ec = throws());
78  Packet(uint8_t *data, size_t size, wrap_data_static, OptionalErrorCode ec = throws());
79 
80 #if AVCPP_CXX_STANDARD >= 20
81  explicit Packet(std::span<const uint8_t> data, bool doAllign = true);
82  // data must be allocated with av_malloc() family
83  Packet(std::span<uint8_t> data, wrap_data, OptionalErrorCode ec = throws());
84  Packet(std::span<uint8_t> data, wrap_data_static, OptionalErrorCode ec = throws());
85 #endif
86 
87  ~Packet();
88 
89  bool setData(const std::vector<uint8_t> &newData, OptionalErrorCode ec = throws());
90  bool setData(const uint8_t *newData, size_t size, OptionalErrorCode ec = throws());
91 
92  const uint8_t* data() const;
93  uint8_t* data();
94 
95 #if AVCPP_CXX_STANDARD >= 20
96  bool setData(std::span<const uint8_t> newData, OptionalErrorCode ec = throws());
97  std::span<const uint8_t> span() const;
98  std::span<uint8_t> span();
99 #endif
100 
101  Timestamp pts() const;
102  Timestamp dts() const;
103  Timestamp ts() const;
104  size_t size() const;
105 
113  attribute_deprecated void setPts(int64_t pts, const Rational &tsTimeBase = Rational(0, 0));
114  attribute_deprecated void setDts(int64_t dts, const Rational &tsTimeBase = Rational(0, 0));
115 
116  void setPts(const Timestamp &pts);
117  void setDts(const Timestamp &dts);
118 
119  int streamIndex() const;
120  bool isKeyPacket() const;
121  int duration() const;
122  bool isComplete() const;
123  bool isNull() const;
124 
125  void setStreamIndex(int idx);
126  void setKeyPacket(bool keyPacket);
127  void setDuration(int duration, const Rational &durationTimeBase = Rational(0, 0));
128  void setComplete(bool complete);
129 
130  // Flags
131  int flags() const;
132  void setFlags(int flags);
133  void addFlags(int flags);
134  void clearFlags(int flags);
135 
136 #if AVCPP_HAS_AVFORMAT
137  void dump(const Stream & st, bool dumpPayload = false) const;
138 #endif // if AVCPP_HAS_AVFORMAT
139 
140  const Rational& timeBase() const { return m_timeBase; }
141  void setTimeBase(const Rational &value);
142 
143 #if AVCPP_HAS_PKT_SIDE_DATA
150  std::span<const uint8_t> sideData(AVPacketSideDataType type) const;
151  std::span<uint8_t> sideData(AVPacketSideDataType type);
152 
157  std::size_t sideDataCount() const noexcept;
158 
164  PacketSideData sideDataIndex(std::size_t index) noexcept;
165 
173  ArrayView<AVPacketSideData, PacketSideData, std::size_t> sideData() noexcept;
174  ArrayView<const AVPacketSideData, PacketSideData, std::size_t> sideData() const noexcept;
175 
176  void freeSideData() noexcept;
177 
184  void addSideData(AVPacketSideDataType type, std::span<const uint8_t> data, OptionalErrorCode ec = throws());
185 
194  void addSideData(AVPacketSideDataType type, std::span<uint8_t> data, wrap_data, OptionalErrorCode ec = throws());
195 
203  std::span<uint8_t> allocateSideData(AVPacketSideDataType type, std::size_t size, OptionalErrorCode ec = throws());
204 #endif
205 
206  bool isReferenced() const;
207  int refCount() const;
208 
209 #if AVCPP_API_AVCODEC_NEW_INIT_PACKET
210  AVPacket* makeRef(OptionalErrorCode ec) const;
211 #else
212  AVPacket makeRef(OptionalErrorCode ec = throws()) const;
213 #endif
214 
215  Packet clone(OptionalErrorCode ec = throws()) const;
216 
217  Packet &operator=(const Packet &rhs);
218  Packet &operator=(Packet &&rhs);
219 
220 #if AVCPP_API_AVCODEC_NEW_INIT_PACKET
221  Packet &operator=(const AVPacket *rhs);
222 #else
223  Packet &operator=(const AVPacket &rhs);
224 #endif
225 
226  void swap(Packet &other);
227 
228  operator bool() const { return isComplete(); }
229 
230 private:
231  bool m_completeFlag;
232  Rational m_timeBase;
233 };
234 
235 
236 
237 } // ::av
238 
Definition: averror.h:66
Definition: packet.h:52
const Rational & timeBase() const
Definition: packet.h:140
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:14
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