1/**********************************************************************
2
3  Audacity: A Digital Audio Editor
4
5  AVFrameWrapperImpl.inl
6
7  Dmitry Vedenko
8
9**********************************************************************/
10
11class AVFrameWrapperImpl : public AVFrameWrapper
12{
13public:
14   explicit
15   AVFrameWrapperImpl(const FFmpegFunctions& ffmpeg)
16      : AVFrameWrapper(ffmpeg)
17   {
18   }
19
20   int GetNumDataPointers() const noexcept override
21   {
22      return AV_NUM_DATA_POINTERS;
23   }
24
25   uint8_t* GetData(int index) const noexcept override
26   {
27      if (mAVFrame == nullptr)
28         return {};
29
30      if (index < 0 || index >= AV_NUM_DATA_POINTERS)
31         return {};
32
33      return mAVFrame->data[index];
34   }
35
36   int GetLineSize(int index) const noexcept override
37   {
38      if (mAVFrame == nullptr)
39         return {};
40
41      if (index < 0 || index >= AV_NUM_DATA_POINTERS)
42         return {};
43
44      return mAVFrame->linesize[index];
45   }
46
47   uint64_t GetError(int index) const noexcept override
48   {
49      if (mAVFrame == nullptr)
50         return {};
51
52      if (index < 0 || index >= AV_NUM_DATA_POINTERS)
53         return {};
54
55      return mAVFrame->error[index];
56   }
57
58   uint8_t* GetExtendedData(int index) const noexcept override
59   {
60      if (mAVFrame != nullptr)
61         return mAVFrame->extended_data[index];
62
63      return {};
64   }
65
66   int GetWidth() const noexcept override
67   {
68      if (mAVFrame != nullptr)
69         return mAVFrame->width;
70
71      return {};
72   }
73
74   int GetHeight() const noexcept override
75   {
76      if (mAVFrame != nullptr)
77         return mAVFrame->height;
78
79      return {};
80   }
81
82   int GetSamplesCount() const noexcept override
83   {
84      if (mAVFrame != nullptr)
85         return mAVFrame->nb_samples;
86
87      return {};
88   }
89
90   void SetSamplesCount(int count) noexcept override
91   {
92      if (mAVFrame != nullptr)
93         mAVFrame->nb_samples = count;
94   }
95
96   AVSampleFormatFwd GetFormat() const noexcept override
97   {
98      if (mAVFrame != nullptr)
99         return static_cast<AVSampleFormatFwd>(mAVFrame->format);
100
101      return {};
102   }
103
104   void SetFormat(AVSampleFormatFwd format) noexcept override
105   {
106      if (mAVFrame != nullptr)
107         mAVFrame->format = format;
108   }
109
110   int GetKeyFrame() const noexcept override
111   {
112      if (mAVFrame != nullptr)
113         return mAVFrame->key_frame;
114
115      return {};
116   }
117
118   AudacityAVRational GetSampleAspectRatio() const noexcept override
119   {
120      if (mAVFrame != nullptr)
121         return { mAVFrame->sample_aspect_ratio.num,
122               mAVFrame->sample_aspect_ratio.den };
123
124      return {};
125   }
126
127   int64_t GetPresentationTimestamp() const noexcept override
128   {
129      if (mAVFrame != nullptr)
130         return mAVFrame->pts;
131
132      return {};
133   }
134
135   int64_t GetPacketPresentationTimestamp() const noexcept override
136   {
137      if (mAVFrame != nullptr)
138         return mAVFrame->pkt_pts;
139
140      return {};
141   }
142
143   int64_t GetPacketDecompressionTimestamp() const noexcept override
144   {
145      if (mAVFrame != nullptr)
146         return mAVFrame->pkt_dts;
147
148      return {};
149   }
150
151   int GetCodedPictureNumber() const noexcept override
152   {
153      if (mAVFrame != nullptr)
154         return mAVFrame->coded_picture_number;
155
156      return {};
157   }
158
159   int GetDisplayPictureNumber() const noexcept override
160   {
161      if (mAVFrame != nullptr)
162         return mAVFrame->display_picture_number;
163
164      return {};
165   }
166
167   int GetQuality() const noexcept override
168   {
169      if (mAVFrame != nullptr)
170         return mAVFrame->quality;
171
172      return {};
173   }
174
175   void* GetOpaque() const noexcept override
176   {
177      if (mAVFrame != nullptr)
178         return mAVFrame->opaque;
179
180      return {};
181   }
182
183   void SetOpaque(void* opaque) noexcept override
184   {
185      if (mAVFrame != nullptr)
186         mAVFrame->opaque = opaque;
187   }
188
189   int GetRepeatPict() const noexcept override
190   {
191      if (mAVFrame != nullptr)
192         return mAVFrame->repeat_pict;
193
194      return {};
195   }
196
197   int GetInterlacedFrame() const noexcept override
198   {
199      if (mAVFrame != nullptr)
200         return mAVFrame->interlaced_frame;
201
202      return {};
203   }
204
205   int GetTopFieldFirst() const noexcept override
206   {
207      if (mAVFrame != nullptr)
208         return mAVFrame->top_field_first;
209
210      return {};
211   }
212
213   int GetPaletteHasChanged() const noexcept override
214   {
215      if (mAVFrame != nullptr)
216         return mAVFrame->palette_has_changed;
217
218      return {};
219   }
220
221   int64_t GetReorderedOpaque() const noexcept override
222   {
223      if (mAVFrame != nullptr)
224         return mAVFrame->reordered_opaque;
225
226      return {};
227   }
228
229   int GetSampleRate() const noexcept override
230   {
231      if (mAVFrame != nullptr)
232         return mAVFrame->sample_rate;
233
234      return {};
235   }
236
237   uint64_t GetChannelLayout() const noexcept override
238   {
239      if (mAVFrame != nullptr)
240         return mAVFrame->channel_layout;
241
242      return {};
243   }
244
245   void SetChannelLayout(uint64_t layout) noexcept override
246   {
247      if (mAVFrame != nullptr)
248      {
249         mAVFrame->channel_layout = layout;
250#if LIBAVUTIL_VERSION_MAJOR >= 56
251         mAVFrame->channels =
252            mFFmpeg.av_get_channel_layout_nb_channels(layout);
253#endif
254      }
255   }
256
257   int GetSideDataCount() const noexcept override
258   {
259      if (mAVFrame != nullptr)
260         return mAVFrame->nb_side_data;
261
262      return {};
263   }
264
265   int GetFlags() const noexcept override
266   {
267      if (mAVFrame != nullptr)
268         return mAVFrame->flags;
269
270      return {};
271   }
272
273   int64_t GetBestEffortTimestamp() const noexcept override
274   {
275      if (mAVFrame != nullptr)
276         return mAVFrame->best_effort_timestamp;
277
278      return {};
279   }
280
281   int64_t GetPacketPos() const noexcept override
282   {
283      if (mAVFrame != nullptr)
284         return mAVFrame->pkt_pos;
285
286      return {};
287   }
288
289   int64_t GetPacketDuration() const noexcept override
290   {
291      if (mAVFrame != nullptr)
292         return mAVFrame->pkt_duration;
293
294      return {};
295   }
296
297   AVDictionaryWrapper GetMetadata() const noexcept override
298   {
299      if (mAVFrame != nullptr)
300         return AVDictionaryWrapper(mFFmpeg, mAVFrame->metadata);
301
302      return AVDictionaryWrapper(mFFmpeg);
303   }
304
305   int GetDecodeErrorFlags() const noexcept override
306   {
307      if (mAVFrame != nullptr)
308         return mAVFrame->decode_error_flags;
309
310      return {};
311   }
312
313   int GetChannels() const noexcept override
314   {
315      if (mAVFrame != nullptr)
316         return mAVFrame->channels;
317
318      return {};
319   }
320
321   int GetPacketSize() const noexcept override
322   {
323      if (mAVFrame != nullptr)
324         return mAVFrame->pkt_size;
325
326      return {};
327   }
328};
329
330std::unique_ptr<AVFrameWrapper> CreateAVFrameWrapper(const FFmpegFunctions& ffmpeg)
331{
332   return std::make_unique<AVFrameWrapperImpl>(ffmpeg);
333}
334