File: AVFormatContextWrapperImpl.inl

package info (click to toggle)
audacity 3.2.4%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 106,704 kB
  • sloc: cpp: 277,038; ansic: 73,623; lisp: 7,761; python: 3,305; sh: 2,715; perl: 821; xml: 275; makefile: 119
file content (586 lines) | stat: -rw-r--r-- 15,557 bytes parent folder | download | duplicates (2)
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
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
/**********************************************************************

  Audacity: A Digital Audio Editor

  AVFormatContextWrapperImpl.inl

  Dmitry Vedenko

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

//! Some checks for correct overlaying.
// There is no AVDictionaryWrapper.inl, so put it here
static_assert(
   sizeof(AudacityAVDictionaryEntry) == sizeof(AVDictionaryEntry) &&
   offsetof(AudacityAVDictionaryEntry, key) == offsetof(AVDictionaryEntry, key)
      &&
   offsetof(AudacityAVDictionaryEntry, value) == offsetof(AVDictionaryEntry,  value),
   "AudacityAVDictionaryEntry does not safely overlay AVDictionaryEntry"
);

//! More sanity checks of macro constants
static_assert(
   AV_DICT_MATCH_CASE == DICT_MATCH_CASE
   && AV_DICT_IGNORE_SUFFIX == DICT_IGNORE_SUFFIX
   && AV_NOPTS_VALUE == AUDACITY_AV_NOPTS_VALUE
   && AV_TIME_BASE == AUDACITY_AV_TIME_BASE
   && FF_QP2LAMBDA == AUDACITY_FF_QP2LAMBDA
   && FF_PROFILE_AAC_LOW == AUDACITY_FF_PROFILE_AAC_LOW
   && AVFMT_NOFILE == AUDACITY_AVFMT_NOFILE
   && AVFMT_GLOBALHEADER == AUDACITY_AVFMT_GLOBALHEADER
   && FF_COMPLIANCE_EXPERIMENTAL == AUDACITY_FF_COMPLIANCE_EXPERIMENTAL
   && AV_SAMPLE_FMT_U8 == static_cast<int>(AUDACITY_AV_SAMPLE_FMT_U8)
   && AV_SAMPLE_FMT_U8P == static_cast<int>(AUDACITY_AV_SAMPLE_FMT_U8P)
   && AV_SAMPLE_FMT_U8P == static_cast<int>(AUDACITY_AV_SAMPLE_FMT_U8P)
   && AV_SAMPLE_FMT_S16P == static_cast<int>(AUDACITY_AV_SAMPLE_FMT_S16P)
   && AV_SAMPLE_FMT_S32 == static_cast<int>(AUDACITY_AV_SAMPLE_FMT_S32)
   && AV_SAMPLE_FMT_S32P == static_cast<int>(AUDACITY_AV_SAMPLE_FMT_S32P)
   && AV_SAMPLE_FMT_FLT == static_cast<int>(AUDACITY_AV_SAMPLE_FMT_FLT)
   && AV_SAMPLE_FMT_FLTP == static_cast<int>(AUDACITY_AV_SAMPLE_FMT_FLTP)
,
   "FFmpeg constants don't match"
);

class AVFormatContextWrapperImpl : public AVFormatContextWrapper
{
public:
   AVFormatContextWrapperImpl(const FFmpegFunctions& ffmpeg)
      : AVFormatContextWrapper(ffmpeg)
   {
      mAVFormatContext = mFFmpeg.avformat_alloc_context();
   }

   AVInputFormat* GetIFormat() const noexcept override
   {
      if (mAVFormatContext != nullptr)
#if LIBAVFORMAT_VERSION_MAJOR <= 58
         return mAVFormatContext->iformat;
#else
         return const_cast<AVInputFormat*>(mAVFormatContext->iformat);
#endif

      return {};
   }

   AVOutputFormat* GetOFormat() const noexcept override
   {
      if (mAVFormatContext != nullptr)
#if LIBAVFORMAT_VERSION_MAJOR <= 58
         return mAVFormatContext->oformat;
#else
         return const_cast<AVOutputFormat*>(mAVFormatContext->oformat);
#endif

      return {};
   }

   void SetOutputFormat(std::unique_ptr<AVOutputFormatWrapper> oformat) noexcept override
   {
      if (mAVFormatContext != nullptr)
      {
         mAVFormatContext->oformat = const_cast<AVOutputFormat*>(oformat->GetWrappedValue());
         mOutputFormat = std::move(oformat);
      }
   }

   AVIOContextWrapper* GetAVIOContext() const noexcept override
   {
      return mAVIOContext.get();
   }

   void SetAVIOContext(std::unique_ptr<AVIOContextWrapper> pb) noexcept override
   {
      if (mAVFormatContext != nullptr)
      {
         mAVIOContext = std::move(pb);
         mAVFormatContext->pb = mAVIOContext->GetWrappedValue();
      }
   }

   int GetCtxFlags() const noexcept override
   {
      if (mAVFormatContext != nullptr)
         return mAVFormatContext->ctx_flags;

      return {};
   }

   unsigned int GetStreamsCount() const noexcept override
   {
      if (mAVFormatContext != nullptr)
         return mAVFormatContext->nb_streams;

      return {};
   }

   const AVFormatContextWrapper::StreamsList& GetStreams() const noexcept override
   {
      return mStreams;
   }

   const char* GetFilename() const noexcept override
   {
      if (mAVFormatContext != nullptr)
#if LIBAVFORMAT_VERSION_MAJOR <= 58
         return mAVFormatContext->filename;
#else
         return mAVFormatContext->url;
#endif

      return {};
   }

   void SetFilename(const char* filename) noexcept override
   {
      if (mAVFormatContext == nullptr)
         return;
#if LIBAVFORMAT_VERSION_MAJOR <= 58
      const size_t len =
         std::min(sizeof(mAVFormatContext->filename) - 1, std::strlen(filename));

      std::copy(filename, filename + len, mAVFormatContext->filename);

      mAVFormatContext->filename[len] = '\0';
#else
      mAVFormatContext->url = mFFmpeg.av_strdup(filename);
#endif
   }

   int64_t GetStartTime() const noexcept override
   {
      if (mAVFormatContext != nullptr)
         return mAVFormatContext->start_time;

      return {};
   }

   int64_t GetDuration() const noexcept override
   {
      if (mAVFormatContext != nullptr)
         return mAVFormatContext->duration;

      return {};
   }

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

      return {};
   }

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

   unsigned int GetPacketSize() const noexcept override
   {
      if (mAVFormatContext != nullptr)
         return mAVFormatContext->packet_size;

      return {};
   }

   void SetPacketSize(unsigned int packet_size) noexcept override
   {
      if (mAVFormatContext != nullptr)
         mAVFormatContext->packet_size = packet_size;
   }

   int GetMaxDelay() const noexcept override
   {
      if (mAVFormatContext != nullptr)
         return mAVFormatContext->max_delay;

      return {};
   }

   void SetMaxDelay(int max_delay) noexcept override
   {
      if (mAVFormatContext != nullptr)
         mAVFormatContext->max_delay = max_delay;
   }

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

      return {};
   }

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

   unsigned int GetProbeSize() const noexcept override
   {
      if (mAVFormatContext != nullptr)
         return mAVFormatContext->probesize;

      return {};
   }

   void SetProbeSize(unsigned int probesize) noexcept override
   {
      if (mAVFormatContext != nullptr)
         mAVFormatContext->probesize = probesize;
   }

   int GetMaxAnalyzeDuration() const noexcept override
   {
      if (mAVFormatContext != nullptr)
         return mAVFormatContext->max_analyze_duration;

      return {};
   }

   void SetMaxAnalyzeDuration(int max_analyze_duration) noexcept override
   {
      if (mAVFormatContext != nullptr)
         mAVFormatContext->max_analyze_duration = max_analyze_duration;
   }

   AVCodecIDFwd GetAudioCodecId() const noexcept override
   {
      if (mAVFormatContext != nullptr)
         return mAVFormatContext->audio_codec_id;

      return {};
   }

   void SetAudioCodecId(AVCodecIDFwd audio_codec_id) noexcept override
   {
      if (mAVFormatContext != nullptr)
         mAVFormatContext->audio_codec_id = static_cast<AVCodecID>(audio_codec_id);
   }

   unsigned int GetMaxIndexSize() const noexcept override
   {
      if (mAVFormatContext != nullptr)
         return mAVFormatContext->max_index_size;

      return {};
   }

   void SetMaxIndexSize(unsigned int max_index_size) noexcept override
   {
      if (mAVFormatContext != nullptr)
         mAVFormatContext->max_index_size = max_index_size;
   }

   AVDictionaryWrapper GetMetadata() const noexcept override
   {
      if (mAVFormatContext != nullptr)
         return AVDictionaryWrapper(mFFmpeg, mAVFormatContext->metadata);

      return AVDictionaryWrapper(mFFmpeg);
   }

   void SetMetadata(AVDictionaryWrapper metadata) noexcept override
   {
      if (mAVFormatContext == nullptr)
         return;

      if (mAVFormatContext->metadata != nullptr)
         mFFmpeg.av_dict_free(&mAVFormatContext->metadata);

      // This Release() doesn't leak:
      /* */https://ffmpeg.org/doxygen/2.8/structAVFormatContext.html#a3019a56080ed2e3297ff25bc2ff88adf */
      mAVFormatContext->metadata = metadata.Release();
   }

   int64_t GetStartTimeRealtime() const noexcept override
   {
      if (mAVFormatContext != nullptr)
         return mAVFormatContext->start_time_realtime;

      return {};
   }

   void SetStartTimeRealtime(int64_t start_time_realtime) noexcept override
   {
      if (mAVFormatContext != nullptr)
         mAVFormatContext->start_time_realtime = start_time_realtime;
   }

   int GetFpsProbeSize() const noexcept override
   {
      if (mAVFormatContext != nullptr)
         return mAVFormatContext->fps_probe_size;

      return {};
   }

   void SetFpsProbeSize(int fps_probe_size) noexcept override
   {
      if (mAVFormatContext != nullptr)
         mAVFormatContext->fps_probe_size = fps_probe_size;
   }

   int GetErrorRecognition() const noexcept override
   {
      if (mAVFormatContext != nullptr)
         return mAVFormatContext->error_recognition;

      return {};
   }

   void SetErrorRecognition(int error_recognition) noexcept override
   {
      if (mAVFormatContext != nullptr)
         mAVFormatContext->error_recognition = error_recognition;
   }

   int64_t GetMaxInterleaveDelta() const noexcept override
   {
      if (mAVFormatContext != nullptr)
         return mAVFormatContext->max_interleave_delta;

      return {};
   }

   void SetMaxInterleaveDelta(int64_t max_interleave_delta) noexcept override
   {
      if (mAVFormatContext != nullptr)
         mAVFormatContext->max_interleave_delta = max_interleave_delta;
   }

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

      return {};
   }

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

   int GetAudioPreload() const noexcept override
   {
      if (mAVFormatContext != nullptr)
         return mAVFormatContext->audio_preload;

      return {};
   }

   void SetAudioPreload(int audio_preload) noexcept override
   {
      if (mAVFormatContext != nullptr)
         mAVFormatContext->audio_preload = audio_preload;
   }

   int GetMaxChunkDuration() const noexcept override
   {
      if (mAVFormatContext != nullptr)
         return mAVFormatContext->max_chunk_duration;

      return {};
   }

   void SetMaxChunkDuration(int max_chunk_duration) noexcept override
   {
      if (mAVFormatContext != nullptr)
         mAVFormatContext->max_chunk_duration = max_chunk_duration;
   }

   int GetMaxChunkSize() const noexcept override
   {
      if (mAVFormatContext != nullptr)
         return mAVFormatContext->max_chunk_size;

      return {};
   }

   void SetMaxChunkSize(int max_chunk_size) noexcept override
   {
      if (mAVFormatContext != nullptr)
         mAVFormatContext->max_chunk_size = max_chunk_size;
   }

   int GetUseWallclockAsTimestamps() const noexcept override
   {
      if (mAVFormatContext != nullptr)
         return mAVFormatContext->use_wallclock_as_timestamps;

      return {};
   }

   void SetUseWallclockAsTimestamps(
      int use_wallclock_as_timestamps) noexcept override
   {
      if (mAVFormatContext != nullptr)
         mAVFormatContext->use_wallclock_as_timestamps = use_wallclock_as_timestamps;
   }

   int GetAvoidNegativeTs() const noexcept override
   {
      if (mAVFormatContext != nullptr)
         return mAVFormatContext->avoid_negative_ts;

      return {};
   }

   void SetAvoidNegativeTs(int avoid_negative_ts) noexcept override
   {
      if (mAVFormatContext != nullptr)
         mAVFormatContext->avoid_negative_ts = avoid_negative_ts;
   }

   int GetAvioFlags() const noexcept override
   {
      if (mAVFormatContext != nullptr)
         return mAVFormatContext->avio_flags;

      return {};
   }

   void SetAvioFlags(int avio_flags) noexcept override
   {
      if (mAVFormatContext != nullptr)
         mAVFormatContext->avio_flags = avio_flags;
   }

   int64_t GetSkipInitialBytes() const noexcept override
   {
      if (mAVFormatContext != nullptr)
         return mAVFormatContext->skip_initial_bytes;

      return {};
   }

   void SetSkipInitialBytes(int64_t skip_initial_bytes) noexcept override
   {
      if (mAVFormatContext != nullptr)
         mAVFormatContext->skip_initial_bytes = skip_initial_bytes;
   }

   unsigned int GetCorrectTsOverflow() const noexcept override
   {
      if (mAVFormatContext != nullptr)
         return mAVFormatContext->correct_ts_overflow;

      return {};
   }

   void SetCorrectTsOverflow(unsigned int correct_ts_overflow) noexcept override
   {
      if (mAVFormatContext != nullptr)
         mAVFormatContext->correct_ts_overflow = correct_ts_overflow;
   }

   int GetSeek2any() const noexcept override
   {
      if (mAVFormatContext != nullptr)
         return mAVFormatContext->seek2any;

      return {};
   }

   void SetSeek2any(int seek2any) noexcept override
   {
      if (mAVFormatContext != nullptr)
         mAVFormatContext->seek2any = seek2any;
   }

   int GetFlushPackets() const noexcept override
   {
      if (mAVFormatContext != nullptr)
         return mAVFormatContext->flush_packets;

      return {};
   }

   void SetFlushPackets(int flush_packets) noexcept override
   {
      if (mAVFormatContext != nullptr)
         mAVFormatContext->flush_packets = flush_packets;
   }

   int GetProbeScore() const noexcept override
   {
      if (mAVFormatContext != nullptr)
         return mAVFormatContext->probe_score;

      return {};
   }

   int GetFormatProbeSize() const noexcept override
   {
      if (mAVFormatContext != nullptr)
         return mAVFormatContext->format_probesize;

      return {};
   }

   void SetFormatProbeSize(int format_probesize) noexcept override
   {
      if (mAVFormatContext != nullptr)
         mAVFormatContext->format_probesize = format_probesize;
   }

   AVCodecWrapper* GetAudioCodec() const noexcept override
   {
      return mForcedAudioCodec.get();
   }

   void SetAudioCodec(std::unique_ptr<AVCodecWrapper> audio_codec) noexcept override
   {
      if (mAVFormatContext == nullptr)
         return;

      mAVFormatContext->audio_codec = const_cast<AVCodec*>(audio_codec->GetWrappedValue());
      mForcedAudioCodec = move(audio_codec);
   }

   void* GetOpaque() const noexcept override
   {
      if (mAVFormatContext != nullptr)
         return mAVFormatContext->opaque;

      return {};
   }

   void SetOpaque(void* opaque) noexcept override
   {
      if (mAVFormatContext != nullptr)
         mAVFormatContext->opaque = opaque;
   }

   int64_t GetOutputTsOffset() const noexcept override
   {
      if (mAVFormatContext != nullptr)
         return mAVFormatContext->output_ts_offset;

      return {};
   }

   void SetOutputTsOffset(int64_t output_ts_offset) noexcept override
   {
      if (mAVFormatContext != nullptr)
         mAVFormatContext->output_ts_offset = output_ts_offset;
   }

   void UpdateStreamList() noexcept override
   {
      mStreams.clear();

      for (int i = 0; i < mAVFormatContext->nb_streams; ++i)
         mStreams.emplace_back(mFFmpeg.CreateAVStreamWrapper(mAVFormatContext->streams[i], false));
   }
};

std::unique_ptr<AVFormatContextWrapper> CreateAVFormatContextWrapper (const FFmpegFunctions& ffmpeg)
{
   return std::make_unique<AVFormatContextWrapperImpl>(ffmpeg);
}