File: AVCodecContextWrapperImpl.inl

package info (click to toggle)
audacity 3.7.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 125,252 kB
  • sloc: cpp: 358,238; ansic: 75,458; lisp: 7,761; sh: 3,410; python: 1,503; xml: 1,385; perl: 854; makefile: 122
file content (529 lines) | stat: -rw-r--r-- 13,589 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
/**********************************************************************

  Audacity: A Digital Audio Editor

  AVCodecContextWrapper.inl

  Dmitry Vedenko

**********************************************************************/

template<typename ResultType>
struct Converters
{};

template <> struct Converters<float>
{
   static float Convert(uint8_t value)
   {
      return static_cast<float>((value - 0x80) / static_cast<double>(1u << 7));
   }

   static float Convert(int16_t value)
   {
      return static_cast<float>(value / static_cast<double>(1u << 15));
   }

   static float Convert(int32_t value)
   {
      return static_cast<float>(value / static_cast<double>(1u << 31));
   }

   static float Convert(int64_t value)
   {
      return static_cast<float>(value / static_cast<double>(1ull << 63));
   }

   static float Convert(float value)
   {
      return value;
   }

   static float Convert(double value)
   {
      return static_cast<float>(value);
   }
};

template <>
struct Converters<int16_t>
{
   static int16_t Convert(uint8_t value)
   {
      return (value - 0x80) << 8;
   }

   static int16_t Convert(int16_t value)
   {
      return value;
   }

   static int16_t Convert(int32_t value)
   {
      return Convert(Converters<float>::Convert(value));
   }

   static int16_t Convert(int64_t value)
   {
      return Convert(Converters<float>::Convert(value));
   }

   static int16_t Convert(float value)
   {
      long intValue = lrintf(value * (1 << 15));

      intValue = std::clamp<long>(
         intValue,
         std::numeric_limits<int16_t>::min(),
         std::numeric_limits<int16_t>::max()
      );

      return static_cast<int16_t>(intValue);
   }

   static int16_t Convert(double value)
   {
      long intValue = lrint(value * (1 << 15));

      intValue = std::clamp<long>(
         intValue, std::numeric_limits<int16_t>::min(),
         std::numeric_limits<int16_t>::max());

      return static_cast<int16_t>(intValue);
   }
};

template<typename OutputType, typename InputType>
std::vector<OutputType> Convert(const void* rawData, const size_t dataSize)
{
   const size_t samplesCount = dataSize / sizeof(InputType);

   std::vector<OutputType> output;
   output.reserve(samplesCount);

   const InputType* currentSample = static_cast<const InputType*>(rawData);

   for (int sample = 0; sample < samplesCount; ++sample)
   {
      output.push_back(Converters<OutputType>::Convert(*currentSample));
      ++currentSample;
   }

   return output;
}

class AVCodecContextWrapperImpl : public AVCodecContextWrapper
{
public:
   AVCodecContextWrapperImpl(const FFmpegFunctions& ffmpeg, std::unique_ptr<AVCodecWrapper> codec) noexcept
      : AVCodecContextWrapper(ffmpeg, std::move(codec))
   {
   }

   AVCodecContextWrapperImpl(const FFmpegFunctions& ffmpeg, AVCodecContext* wrapped)
      : AVCodecContextWrapper(ffmpeg, wrapped)
   {
      if(mAVCodecContext == nullptr)
         return;

      if (mFFmpeg.av_codec_is_encoder(mAVCodecContext->codec))
         mAVCodec = mFFmpeg.CreateEncoder(mAVCodecContext->codec_id);
      else
         mAVCodec = mFFmpeg.CreateDecoder(mAVCodecContext->codec_id);
   }

   int GetBitRate() const noexcept override
   {
      if (mAVCodecContext != nullptr)
         // May truncate int64_t to int.  But who uses such high rates, really?
         return mAVCodecContext->bit_rate;

      return {};
   }

   void SetBitRate(int value) noexcept override
   {
      if (mAVCodecContext != nullptr)
         mAVCodecContext->bit_rate = value;
   }

   const AVChannelLayoutWrapper* GetChannelLayout() const noexcept override
   {
      return GetChannelLayoutSafe();
   }

   void SetChannelLayout(const AVChannelLayoutWrapper* layout) noexcept override
   {
      if (mAVCodecContext == nullptr || layout == nullptr)
         return;

      mChannelLayoutWrapper = layout->Clone();
// Clone never returns nullptr
#if HAS_AV_CHANNEL_LAYOUT
      mAVCodecContext->ch_layout = *layout->GetChannelLayout();
#else
      mAVCodecContext->channel_layout = layout->GetLegacyChannelLayout();
      mAVCodecContext->channels       = layout->GetChannelsCount();
#endif
   }

   int GetChannels() const noexcept override
   {
      if (auto layout = GetChannelLayoutSafe(); layout != nullptr)
         return layout->GetChannelsCount();

      return {};
   }

   const AVCodecWrapper* GetCodec() const noexcept override
   {
      if (!mAVCodec && mAVCodecContext && mAVCodecContext->codec)
      {
         if (mFFmpeg.av_codec_is_encoder(mAVCodecContext->codec))
            mAVCodec = mFFmpeg.CreateEncoder(mAVCodecContext->codec_id);
         else
            mAVCodec = mFFmpeg.CreateDecoder(mAVCodecContext->codec_id);
      }

      return mAVCodec.get();
   }

   AVCodecIDFwd GetCodecId() const noexcept override
   {
      if (mAVCodecContext != nullptr)
         return mAVCodecContext->codec_id;

      return {};
   }

   void SetCodecTag(unsigned int tag) noexcept override
   {
      if (mAVCodecContext != nullptr)
         mAVCodecContext->codec_tag = tag;
   }

   unsigned int GetCodecTag() const noexcept override
   {
      if (mAVCodecContext != nullptr)
         return mAVCodecContext->codec_tag;

      return {};
   }

   AVMediaTypeFwd GetCodecType() const noexcept override
   {
      if (mAVCodecContext != nullptr)
         return mAVCodecContext->codec_type;

      return {};
   }

   int GetCompressionLevel() const noexcept override
   {
      if (mAVCodecContext != nullptr)
         return mAVCodecContext->compression_level;

      return {};
   }

   void SetCompressionLevel(int value) noexcept override
   {
      if (mAVCodecContext != nullptr)
         mAVCodecContext->compression_level = value;
   }

   int GetCutoff() const noexcept override
   {
      if (mAVCodecContext != nullptr)
         return mAVCodecContext->cutoff;

      return {};
   }

   void SetCutoff(int value) noexcept override
   {
      if (mAVCodecContext != nullptr)
         mAVCodecContext->cutoff = value;
   }

   int GetFlags() const noexcept override
   {
      if (mAVCodecContext != nullptr)
         return mAVCodecContext->flags;

      return {};
   }

   void SetFlags(int value) noexcept override
   {
      if (mAVCodecContext != nullptr)
         mAVCodecContext->flags = value;
   }

   int GetFlags2() const noexcept override
   {
      if (mAVCodecContext != nullptr)
         return mAVCodecContext->flags2;

      return {};
   }

   void SetFlags2(int value) noexcept override
   {
      if (mAVCodecContext != nullptr)
         mAVCodecContext->flags2 = value;
   }

   int GetFrameNumber() const noexcept override
   {
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(61, 0, 0)
      if (mAVCodecContext != nullptr)
         return mAVCodecContext->frame_number;
#else
      if (mAVCodecContext != nullptr)
         return mAVCodecContext->frame_num;
#endif

      return {};
   }

   int GetFrameSize() const noexcept override
   {
      if (mAVCodecContext != nullptr)
         return mAVCodecContext->frame_size;

      return {};
   }

   void SetFrameSize(int value) noexcept override
   {
      if (mAVCodecContext != nullptr)
         mAVCodecContext->frame_size = value;
   }

   int GetGlobalQuality() const noexcept override
   {
      if (mAVCodecContext != nullptr)
         return mAVCodecContext->global_quality;

      return {};
   }

   void SetGlobalQuality(int value) noexcept override
   {
      if (mAVCodecContext != nullptr)
         mAVCodecContext->global_quality = value;
   }

   int GetProfile() const noexcept override
   {
      if (mAVCodecContext != nullptr)
         return mAVCodecContext->profile;

      return {};
   }

   void SetProfile(int value) noexcept override
   {
      if (mAVCodecContext != nullptr)
         mAVCodecContext->profile = value;
   }

   AVSampleFormatFwd GetSampleFmt() const noexcept override
   {
      if (mAVCodecContext != nullptr)
         return mAVCodecContext->sample_fmt;

      return {};
   }

   void SetSampleFmt(AVSampleFormatFwd value) noexcept override
   {
      if (mAVCodecContext != nullptr)
         mAVCodecContext->sample_fmt = static_cast<AVSampleFormat>(value);
   }

   int GetSampleRate() const noexcept override
   {
      if (mAVCodecContext != nullptr)
         return mAVCodecContext->sample_rate;

      return {};
   }

   void SetSampleRate(int value) noexcept override
   {
      if (mAVCodecContext != nullptr)
         mAVCodecContext->sample_rate = value;
   }

   int GetStrictStdCompliance() const noexcept override
   {
      if (mAVCodecContext != nullptr)
         return mAVCodecContext->strict_std_compliance;

      return {};
   }

   void SetStrictStdCompliance(int value) noexcept override
   {
      if (mAVCodecContext != nullptr)
         mAVCodecContext->strict_std_compliance = value;
   }

   struct AudacityAVRational GetTimeBase() const noexcept override
   {
      if (mAVCodecContext != nullptr)
      {
         return { mAVCodecContext->time_base.num,
                  mAVCodecContext->time_base.den };
      }

      return {};
   }

   void SetTimeBase(struct AudacityAVRational value) noexcept override
   {
      if (mAVCodecContext != nullptr)
      {
         mAVCodecContext->time_base.num = value.num;
         mAVCodecContext->time_base.den = value.den;
      };
   }

   sampleFormat GetPreferredAudacitySampleFormat() const noexcept override
   {
      if (mAVCodecContext == nullptr)
         return int16Sample;

      switch (mAVCodecContext->sample_fmt)
      {
      case AV_SAMPLE_FMT_U8:
      case AV_SAMPLE_FMT_U8P:
      case AV_SAMPLE_FMT_S16:
      case AV_SAMPLE_FMT_S16P:
         return int16Sample;
      default:
         return floatSample;
      }
      return floatSample;
   }

   std::vector<int16_t> DecodeAudioPacketInt16(const AVPacketWrapper* packet) override
   {
      if (mAVCodecContext == nullptr)
         return {};

      const auto rawData = DecodeAudioPacket(packet);

      switch (mAVCodecContext->sample_fmt)
      {
      case AV_SAMPLE_FMT_U8:
      case AV_SAMPLE_FMT_U8P:
         return Convert<int16_t, uint8_t>(rawData.data(), rawData.size());
      case AV_SAMPLE_FMT_S16:
      case AV_SAMPLE_FMT_S16P:
         return Convert<int16_t, int16_t>(rawData.data(), rawData.size());
      case AV_SAMPLE_FMT_S32:
      case AV_SAMPLE_FMT_S32P:
         return Convert<int16_t, int32_t>(rawData.data(), rawData.size());
      case AV_SAMPLE_FMT_FLT:
      case AV_SAMPLE_FMT_FLTP:
         return Convert<int16_t, float>(rawData.data(), rawData.size());
      case AV_SAMPLE_FMT_DBL:
      case AV_SAMPLE_FMT_DBLP:
         return Convert<int16_t, double>(rawData.data(), rawData.size());
#if LIBAVFORMAT_VERSION_MAJOR >= 58
      case AV_SAMPLE_FMT_S64:
      case AV_SAMPLE_FMT_S64P:
         return Convert<int16_t, int64_t>(rawData.data(), rawData.size());
#endif
      default:
         return {};
      }
   }

   std::vector<float> DecodeAudioPacketFloat(const AVPacketWrapper* packet) override
   {
      if (mAVCodecContext == nullptr)
         return {};

      const auto rawData = DecodeAudioPacket(packet);

      switch (mAVCodecContext->sample_fmt)
      {
      case AV_SAMPLE_FMT_U8:
      case AV_SAMPLE_FMT_U8P:
         return Convert<float, uint8_t>(rawData.data(), rawData.size());
      case AV_SAMPLE_FMT_S16:
      case AV_SAMPLE_FMT_S16P:
         return Convert<float, int16_t>(rawData.data(), rawData.size());
      case AV_SAMPLE_FMT_S32:
      case AV_SAMPLE_FMT_S32P:
         return Convert<float, int32_t>(rawData.data(), rawData.size());
      case AV_SAMPLE_FMT_FLT:
      case AV_SAMPLE_FMT_FLTP:
         return Convert<float, float>(rawData.data(), rawData.size());
      case AV_SAMPLE_FMT_DBL:
      case AV_SAMPLE_FMT_DBLP:
         return Convert<float, double>(rawData.data(), rawData.size());
#if LIBAVFORMAT_VERSION_MAJOR >= 58
      case AV_SAMPLE_FMT_S64:
      case AV_SAMPLE_FMT_S64P:
         return Convert<float, int64_t>(rawData.data(), rawData.size());
#endif
      default:
         return {};
      }
   }

   int Open( const AVCodecWrapper *codec, AVDictionaryWrapper *options )
   override
   {
      if (mAVCodecContext == nullptr)
         return {};

      AVDictionary *dictionary = options ? options->Release() : nullptr;

      int result = mFFmpeg.avcodec_open2(mAVCodecContext,
         codec ? codec->GetWrappedValue() : nullptr,
         dictionary ? &dictionary : nullptr);

      if (options)
         *options = AVDictionaryWrapper{ mFFmpeg, dictionary };

      return result;
   }
   const AVChannelLayoutWrapper* GetChannelLayoutSafe() const noexcept
   {
      if (mAVCodecContext == nullptr)
         return nullptr;

      if (mChannelLayoutWrapper == nullptr)
      {
#if HAS_AV_CHANNEL_LAYOUT
         mChannelLayoutWrapper =
            mFFmpeg.CreateAVChannelLayout(&mAVCodecContext->ch_layout);
#else
         mChannelLayoutWrapper = mFFmpeg.CreateLegacyChannelLayout(
            mAVCodecContext->channel_layout, mAVCodecContext->channels);
#endif
      }

      return mChannelLayoutWrapper.get();
   }

   mutable std::unique_ptr<AVChannelLayoutWrapper> mChannelLayoutWrapper;
};

std::unique_ptr<AVCodecContextWrapper> CreateAVCodecContextWrapperFromCodec(
   const FFmpegFunctions& fns, std::unique_ptr<AVCodecWrapper> codec)
{
   return std::make_unique<AVCodecContextWrapperImpl>(fns, std::move(codec));
}

std::unique_ptr<AVCodecContextWrapper> CreateAVCodecContextWrapper(
   const FFmpegFunctions& fns, AVCodecContext* context)
{
   return std::make_unique<AVCodecContextWrapperImpl>(fns, context);
}