File: ExportOpus.cpp

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 (891 lines) | stat: -rw-r--r-- 24,115 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
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
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
/**********************************************************************

   SPDX-License-Identifier: GPL-2.0-or-later

   Audacity: A Digital Audio Editor

   ExportOpus.cpp

   Dmitry Vedenko

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

#include "Export.h"

#include <random>
#include <string_view>

#include <ogg/ogg.h>
#include <opus/opus.h>
#include <opus/opus_multistream.h>

#include "wxFileNameWrapper.h"
#include "Mix.h"

#include "MemoryX.h"

#include "Track.h"
#include "Tags.h"

#include "ExportPluginHelpers.h"
#include "ExportOptionsEditor.h"
#include "ExportPluginRegistry.h"

#include "PlainExportOptionsEditor.h"

#include "CodeConversions.h"


namespace
{

TranslatableString GetOpusEncErrorString(int error)
{
   switch (error)
   {
   case OPUS_OK:
      return XO("no error");
   case OPUS_BAD_ARG:
      return XO("invalid argument");
   case OPUS_BUFFER_TOO_SMALL:
      return XO("buffer too small");
   case OPUS_INTERNAL_ERROR:
      return XO("internal error");
   case OPUS_INVALID_PACKET:
      return XO("invalid packet");
   case OPUS_UNIMPLEMENTED:
      return XO("not implemented");
   case OPUS_INVALID_STATE:
      return XO("invalid state");
   case OPUS_ALLOC_FAIL:
      return XO("memory allocation has failed");
   default:
      return XO("Unknown error");
   }
}

[[noreturn]] void FailExport(const TranslatableString& title, int errorCode = 0)
{
   if (errorCode != 0)
   {
      throw ExportException(Verbatim("%s: %s")
                               .Format(title, GetOpusEncErrorString(errorCode))
                               .Translation());
   }

   throw ExportException(title.Translation());
}

/* i18n-hint: kbps abbreviates "thousands of bits per second" */
TranslatableString n_kbps(int n)
{
   return XO("%d kbps").Format(n);
}

enum : int
{
   OPUSOptionIDBitRate = 0,
   OPUSOptionIDQuality,
   OPUSOptionIDFrameDuration,
   OPUSOptionIDVBRMode,
   OPUSOptionIDApplication,
   OPUSOptionIDCutoff
};

namespace VBRMode
{
enum : int
{
   CBR,
   VBR,
   CVBR
};
}

const std::initializer_list<PlainExportOptionsEditor::OptionDesc> OPUSOptions {
   {
      {
         OPUSOptionIDBitRate, XO("Bit Rate"),
         OPUS_AUTO,
         ExportOption::TypeEnum,
         {
            6000,
            8000,
            16000,
            24000,
            32000,
            40000,
            48000,
            64000,
            80000,
            96000,
            128000,
            160000,
            192000,
            256000,
            OPUS_AUTO,
            OPUS_BITRATE_MAX,
         },
         {
            n_kbps( 6 ),
            n_kbps( 8 ),
            n_kbps( 16 ),
            n_kbps( 24 ),
            n_kbps( 32 ),
            n_kbps( 40 ),
            n_kbps( 48 ),
            n_kbps( 64 ),
            n_kbps( 80 ),
            n_kbps( 96 ),
            n_kbps( 128 ),
            n_kbps( 160 ),
            n_kbps( 192 ),
            n_kbps( 256 ),
            XO("Auto"),
            XO("Maximum")
         }
      }, wxT("/FileFormats/OPUS/Bitrate")
   },
   {
      {
         OPUSOptionIDQuality, XO("Quality"),
         10,
         ExportOption::TypeRange,
         { 0, 10 }
      }, wxT("/FileFormats/OPUS/Quality")
   },
   {
      {
         OPUSOptionIDFrameDuration, XO("Frame Duration"),
         200,
         ExportOption::TypeEnum,
         {
            25,
            50,
            100,
            200,
            400,
            600,
         },
         {
            XO("2.5 ms"),
            XO("5 ms"),
            XO("10 ms"),
            XO("20 ms"),
            XO("40 ms"),
            XO("60 ms"),
         }
      }, wxT("/FileFormats/OPUS/FrameDuration")
   },
   {
      {
         OPUSOptionIDVBRMode, XO("VBR Mode"),
         VBRMode::VBR,
         ExportOption::TypeEnum,
         { VBRMode::CBR, VBRMode::VBR, VBRMode::CVBR },
         { XO("Off"), XO("On"), XO("Constrained") }
      }, wxT("/FileFormats/OPUS/VbrMode")
   },
   {
      {
         OPUSOptionIDApplication, XO("Optimize for"),
         OPUS_APPLICATION_AUDIO,
         ExportOption::TypeEnum,
         { OPUS_APPLICATION_VOIP, OPUS_APPLICATION_AUDIO, OPUS_APPLICATION_RESTRICTED_LOWDELAY },
         { XO("Speech"), XO("Audio"), XO("Low Delay") }
      }, wxT("/FileFormats/OPUS/Application")
   },
   {
      {
         OPUSOptionIDCutoff, XO("Cutoff"),
         OPUS_AUTO,
         ExportOption::TypeEnum,
         {
            OPUS_AUTO,
            OPUS_BANDWIDTH_NARROWBAND,
            OPUS_BANDWIDTH_MEDIUMBAND,
            OPUS_BANDWIDTH_WIDEBAND,
            OPUS_BANDWIDTH_SUPERWIDEBAND,
            OPUS_BANDWIDTH_FULLBAND,
         },
         {
            XO("Auto"),
            XO("Narrowband"),
            XO("Mediumband"),
            XO("Wideband"),
            XO("Super Wideband"),
            XO("Fullband")
         }
      }, wxT("/FileFormats/OPUS/Cutoff")
   },
};

constexpr int supportedSampleRates[] = { 8000, 12000, 16000, 24000, 48000 };

bool IsValidSampleRate(int sampleRate) noexcept
{
   for (auto sr : supportedSampleRates)
      if (sr == sampleRate)
         return true;
   return false;
}
}

class OpusExportProcessor final : public ExportProcessor
{
   struct OggPacket final
   {
      using byte_type = std::remove_pointer_t<decltype(ogg_packet::packet)>;
      static_assert(sizeof(byte_type) == 1);

      OggPacket(int64_t packetNo, bool resizable)
          : resizable { resizable }
      {
         packet.packetno = packetNo;
      }

      explicit OggPacket(int64_t packetNo)
          : OggPacket { packetNo, false }
      {
      }

      OggPacket(int64_t packetNo, long size, bool resizable)
          : OggPacket { packetNo, resizable }
      {
         Resize(size);
      }

      void Resize(long size)
      {
         buffer.resize(size);
         packet.packet = buffer.data();
      }

      void Reset() noexcept
      {
         packet.bytes = 0;
      }

      void MarkBOS() noexcept
      {
         packet.b_o_s = 1;
      }

      void MarkEOS() noexcept
      {
         packet.e_o_s = 1;
      }

      void Write(const void* data, const long length)
      {
         const auto nextPos = packet.bytes + length;

         if (nextPos > buffer.size())
         {
            if (resizable)
               Resize(std::max<size_t>(1024, buffer.size() * 2));
            else
               FailExport(
                  XO("Buffer overflow in OGG packet"), OPUS_BUFFER_TOO_SMALL);
         }

         std::copy(
            reinterpret_cast<const byte_type*>(data),
            reinterpret_cast<const byte_type*>(data) + length,
            buffer.data() + packet.bytes);

         packet.bytes = nextPos;
      }

      template<typename IntType>
      void Write(IntType value)
      {
         static_assert(std::is_integral_v<IntType>);
         if constexpr (sizeof (IntType) == 1)
         {
            Write(&value, 1);
         }
         else
         {
            if (IsLittleEndian ())
            {
               Write(&value, sizeof (IntType));
            }
            else
            {
               IntType swapped = SwapIntBytes(value);
               Write(&swapped, sizeof (IntType));
            }
         }
      }

      byte_type* GetBuffer()
      {
         return buffer.data();
      }

      size_t GetBufferSize() const
      {
         return buffer.size();
      }

      ogg_packet packet {};

   private:
      std::vector<byte_type> buffer;
      bool resizable { false };
   };

   struct
   {
      TranslatableString status;
      int32_t sampleRate {};

      double t0 {};
      double t1 {};
      unsigned numChannels {};
      wxFileNameWrapper fName;
      wxFile outFile;
      std::unique_ptr<Mixer> mixer;
      std::unique_ptr<Tags> metadata;

      // Encoder properties
      struct OpusState final
      {
         ~OpusState()
         {
            if (encoder != nullptr)
               opus_multistream_encoder_destroy(encoder);
         }

         OpusMSEncoder* encoder {};

         int32_t frameSize {};
         int32_t sampleRateFactor {};
         uint16_t preskip {};
         uint8_t channelMapping {};
         uint8_t nbStreams {};
         uint8_t nbCoupled {};
         uint8_t streamMap[255] {};
      } opus;

      // Bitstream properties
      struct OggState final
      {
         OggState()
             // Audio always starts in the packet #3.
             // The first one is for opus header, the second one is for tags,
             // both are mandatory
             : audioStreamPacket(2)
         {
            // As per OGG docs - stream serialno should be a random
            // number. It is used to stitch the streams, this doesn't
            // matter much for Audacity
            std::mt19937 gen(std::time(nullptr));
            ogg_stream_init(&stream, gen());
         }

         void PacketIn(const OggPacket& packet)
         {
            ogg_stream_packetin(&stream,
               // C libraries are not always const-correct
               const_cast<ogg_packet*>(&packet.packet));
         }

         void WriteOut(wxFile& outputStream)
         {
            ogg_page page {};

            while (ogg_stream_pageout(&stream, &page))
               WritePage(outputStream, page);
         }

         void Flush(wxFile& outputStream)
         {
            ogg_page page {};

            while (ogg_stream_flush(&stream, &page))
               WritePage(outputStream, page);
         }

         ogg_stream_state stream;

         OggPacket audioStreamPacket;

      private:
         void WritePage(wxFile& outputStream, const ogg_page& page)
         {
            if (
               outputStream.Write(page.header, page.header_len) !=
               page.header_len)
               FailExport(XO("Unable to write OGG page header"));

            if (outputStream.Write(page.body, page.body_len) != page.body_len)
               FailExport(XO("Unable to write OGG page"));
         }
      } ogg;

      std::vector<float> encodeBuffer;
   } context;

   void WriteOpusHeader();
   void WriteTags();

   int32_t GetBestFrameSize(int32_t samplesCount) const noexcept
   {
      static const int32_t multipliers[] = {
         25, 50, 100, 200, 400, 600,
      };

      const auto sampleRate = context.sampleRate;

      for (auto multiplier : multipliers)
      {
         const auto frameSize = multiplier * sampleRate / 10000;

         if (samplesCount <= frameSize)
            return frameSize;
      }

      return 60 * sampleRate / 1000;
   }

public:

   ~OpusExportProcessor();

   bool Initialize(AudacityProject& project,
      const Parameters& parameters,
      const wxFileNameWrapper& filename,
      double t0, double t1, bool selectedOnly,
      double sampleRate, unsigned channels,
      MixerOptions::Downmix* mixerSpec,
      const Tags* tags) override;

   ExportResult Process(ExportProcessorDelegate& delegate) override;

};

class ExportOpus final : public ExportPlugin
{
public:

   ExportOpus();

   int GetFormatCount() const override;
   FormatInfo GetFormatInfo(int) const override;

   std::vector<std::string> GetMimeTypes(int) const override;

   std::unique_ptr<ExportOptionsEditor>
   CreateOptionsEditor(int, ExportOptionsEditor::Listener*) const override;

   std::unique_ptr<ExportProcessor> CreateProcessor(int format) const override;
};

ExportOpus::ExportOpus() = default;

int ExportOpus::GetFormatCount() const
{
   return 1;
}

FormatInfo ExportOpus::GetFormatInfo(int) const
{
   return {
      wxT("Opus"), XO("Opus Files"), { wxT("opus") }, 255, true
   };
}

std::vector<std::string> ExportOpus::GetMimeTypes(int) const
{
   return { "audio/opus" };
}

std::unique_ptr<ExportOptionsEditor>
ExportOpus::CreateOptionsEditor(int, ExportOptionsEditor::Listener* listener) const
{
   return std::make_unique<PlainExportOptionsEditor>(
      OPUSOptions,
      ExportOptionsEditor::SampleRateList { 8000, 12000, 16000, 24000, 48000 },
      listener);
}

std::unique_ptr<ExportProcessor> ExportOpus::CreateProcessor(int) const
{
   return std::make_unique<OpusExportProcessor>();
}


void OpusExportProcessor::WriteOpusHeader()
{
   const auto headerSize =
      // "OpusHead"
      8 +
      // Version number (always 1)
      1 +
      // Channels count
      1 +
      // Preskip
      2 +
      // Input sample rate
      4 +
      // Output gain (always 0)
      2 +
      // Channel mapping
      1 +
      (context.opus.channelMapping == 0 ? 0 :
         (
            // Stream count
            1 +
            // Two channel stream count
            1 +
            // Channel mapping
            context.numChannels));

   OggPacket headerPacket(0, headerSize, false);
   // Header must have beginning-of-stream marker
   headerPacket.MarkBOS();

   headerPacket.Write("OpusHead", 8);
   headerPacket.Write<uint8_t>(1);
   headerPacket.Write<uint8_t>(context.numChannels);
   headerPacket.Write(context.opus.preskip);
   // Should we put the project sample rate here?
   headerPacket.Write(context.sampleRate);
   // Opus docs recommend encoders to use 0 as a gain
   headerPacket.Write<uint16_t>(0);
   headerPacket.Write(context.opus.channelMapping);

   if (context.opus.channelMapping > 0)
   {
      headerPacket.Write(context.opus.nbStreams);
      headerPacket.Write(context.opus.nbCoupled);

      for (int i = 0; i < context.numChannels; ++i)
         headerPacket.Write<uint8_t>(context.opus.streamMap[i]);
   }

   // This is guaranteed by the way we calculate the header size
   assert(headerPacket.packet.bytes == headerSize);

   context.ogg.PacketIn(headerPacket);
   context.ogg.Flush(context.outFile);
}

void OpusExportProcessor::WriteTags()
{
   OggPacket commentsPacket { 1, true };

   commentsPacket.Write("OpusTags", 8);

   const std::string_view vendor { opus_get_version_string() };

   commentsPacket.Write<uint32_t>(vendor.size());
   commentsPacket.Write(vendor.data(), vendor.size());

   commentsPacket.Write<uint32_t>(context.metadata->Count());

   for (const auto& pair : context.metadata->GetRange())
   {
      const auto key = pair.first == TAG_YEAR ? std::string("DATE") :
                                                audacity::ToUTF8(pair.first);

      const auto value = audacity::ToUTF8(pair.second);

      commentsPacket.Write<uint32_t>(key.size() + value.size() + 1);
      commentsPacket.Write(key.data(), key.size());
      commentsPacket.Write("=", 1);
      commentsPacket.Write(value.data(), value.size());
   }

   context.ogg.PacketIn(commentsPacket);
   context.ogg.Flush(context.outFile);
}

OpusExportProcessor::~OpusExportProcessor()
{

}

bool OpusExportProcessor::Initialize(
   AudacityProject& project, const Parameters& parameters,
   const wxFileNameWrapper& fName, double t0, double t1, bool selectionOnly,
   double sampleRate, unsigned numChannels, MixerOptions::Downmix* mixerSpec,
   const Tags* metadata)
{
   context.sampleRate = int32_t(sampleRate);

   if (!IsValidSampleRate(context.sampleRate))
      throw ExportException(XO("Unsupported sample rate").Translation());

   context.t0 = t0;
   context.t1 = t1;
   context.numChannels = numChannels;
   context.fName = fName;

   // Internally the Opus is always in 48k, find out the multiplier for
   // values, that expect 48k sample rate
   context.opus.sampleRateFactor = 48000 / context.sampleRate;

   const auto bitRate = ExportPluginHelpers::GetParameterValue<int>(
      parameters, OPUSOptionIDBitRate, OPUS_AUTO);
   const auto vbrMode = ExportPluginHelpers::GetParameterValue<int>(
      parameters, OPUSOptionIDVBRMode, VBRMode::VBR);
   const int complexity = ExportPluginHelpers::GetParameterValue<int>(
      parameters, OPUSOptionIDQuality, 10);
   const int frameMultiplier = ExportPluginHelpers::GetParameterValue<int>(
      parameters, OPUSOptionIDFrameDuration, 200);
   const int application = ExportPluginHelpers::GetParameterValue<int>(
      parameters, OPUSOptionIDApplication, OPUS_APPLICATION_AUDIO);
   const int cutoff = ExportPluginHelpers::GetParameterValue<int>(
      parameters, OPUSOptionIDCutoff, OPUS_AUTO);

   // Number of samples per frame per channel
   context.opus.frameSize = frameMultiplier * context.sampleRate / 10000;

   context.status = selectionOnly ? XO("Exporting selected audio as Opus") :
                                    XO("Exporting the audio as Opus");

   // Create opus encoder
   int error;

   if (numChannels <= 2)
   {
      context.opus.channelMapping = 0;
      context.opus.nbStreams = 1;
      context.opus.nbCoupled = numChannels - 1;
      context.opus.streamMap[0] = 0;
      context.opus.streamMap[1] = 1;

      context.opus.encoder = opus_multistream_encoder_create(
         sampleRate, numChannels, context.opus.nbStreams,
         context.opus.nbCoupled, context.opus.streamMap, application, &error);
   }
   else
   {
      context.opus.channelMapping = numChannels <= 8 ? 1 : 255;

      int nbStreams {}, nbCoupled {};

      context.opus.encoder = opus_multistream_surround_encoder_create(
         sampleRate, numChannels, context.opus.channelMapping,
         &nbStreams, &nbCoupled,
         context.opus.streamMap, application, &error);

      // opus_multistream_surround_encoder_create is expected to fill
      // stream count with values in [0, 255]
      context.opus.nbStreams = uint8_t(nbStreams);
      context.opus.nbCoupled = uint8_t(nbCoupled);
   }

   if (error != OPUS_OK)
      FailExport(XO("Unable to create Opus encoder"), error);

   error = opus_multistream_encoder_ctl(
      context.opus.encoder, OPUS_SET_BITRATE(bitRate));

   if (error != OPUS_OK)
      FailExport(XO("Unable to set bitrate"), error);

   error = opus_multistream_encoder_ctl(
      context.opus.encoder, OPUS_SET_COMPLEXITY(complexity));

   if (error != OPUS_OK)
      FailExport(XO("Unable to set complexity"), error);

   error = opus_multistream_encoder_ctl(
      context.opus.encoder, OPUS_SET_BANDWIDTH(cutoff));

   if (error != OPUS_OK)
      FailExport(XO("Unable to set bandwidth"), error);

   error = opus_multistream_encoder_ctl(
      context.opus.encoder, OPUS_SET_VBR(vbrMode == VBRMode::CBR ? 0 : 1));

   if (error != OPUS_OK)
      FailExport(XO("Unable to set VBR mode"), error);

   if (vbrMode == VBRMode::CVBR)
   {
      error = opus_multistream_encoder_ctl(
         context.opus.encoder, OPUS_SET_VBR_CONSTRAINT(1));

      if (error != OPUS_OK)
         FailExport(XO("Unable to set CVBR mode"), error);
   }

   // Calculate the encoder latency. This value is needed in header
   // and to flush the encoder
   int lookahead {};
   error = opus_multistream_encoder_ctl(
      context.opus.encoder, OPUS_GET_LOOKAHEAD(&lookahead));

   if (error != OPUS_OK)
      FailExport(XO("Unable to get lookahead"), error);

   // Latency is always in 48k encoded samples
   const auto calculatedPreskip = lookahead * context.opus.sampleRateFactor;
   if (
      calculatedPreskip < 0 ||
      calculatedPreskip >= std::numeric_limits<uint16_t>::max())
      FailExport(XO("Failed to calculate correct preskip"), OPUS_BAD_ARG);
   // It is safe to cast to uint16_t here
   context.opus.preskip = uint16_t(calculatedPreskip);

   // Resize the audio packet so it can contain all the raw data.
   // This is overkill, but should be enough to hold all the data from
   // the encode float
   context.ogg.audioStreamPacket.Resize(
      context.opus.frameSize * sizeof(float) * numChannels);


   // Try to open the file for writing
   if (
      !context.outFile.Create(fName.GetFullPath(), true) ||
      !context.outFile.IsOpened())
   {
      throw ExportException(_("Unable to open target file for writing"));
   }

   WriteOpusHeader();

   context.metadata = std::make_unique<Tags>(
      metadata == nullptr ? Tags::Get(project) : *metadata);

   WriteTags();

   context.mixer = ExportPluginHelpers::CreateMixer(
      project, selectionOnly, t0, t1, numChannels, context.opus.frameSize, true,
      sampleRate, floatSample, mixerSpec);

   return true;
}

ExportResult OpusExportProcessor::Process(ExportProcessorDelegate& delegate)
{
   delegate.SetStatusString(context.status);

   auto exportResult = ExportResult::Success;

   int64_t granulePos = 0;

   int32_t latencyLeft = context.opus.preskip;

   while (exportResult == ExportResult::Success)
   {
      auto samplesThisRun = context.mixer->Process();

      if (samplesThisRun == 0)
         break;

      auto mixedAudioBuffer =
         reinterpret_cast<const float*>(context.mixer->GetBuffer());

      // bestFrameSize <= context.opus.frameSize by design
      auto bestFrameSize = GetBestFrameSize(samplesThisRun);

      if (samplesThisRun < bestFrameSize)
      {
         // Opus expects that the full frame is passed to the encoder, fill missing data with zeroes
         context.encodeBuffer.resize(bestFrameSize * context.numChannels);

         std::copy(
            mixedAudioBuffer, mixedAudioBuffer + samplesThisRun * context.numChannels,
            context.encodeBuffer.begin());

         std::fill(
            context.encodeBuffer.begin() + samplesThisRun * context.numChannels,
            context.encodeBuffer.begin() + bestFrameSize * context.numChannels,
            0);

         mixedAudioBuffer = context.encodeBuffer.data();

         auto zeroesCount = bestFrameSize - int32_t(samplesThisRun);

         if (zeroesCount < latencyLeft)
            samplesThisRun += zeroesCount;
         else
            samplesThisRun += latencyLeft;

         // Reduce the latency by the number of zeroes pushed (potentially
         // removing the need to flush the encoder)
         latencyLeft = std::max(0, latencyLeft - zeroesCount);
      }

      auto result = opus_multistream_encode_float(
         context.opus.encoder, mixedAudioBuffer, bestFrameSize,
         context.ogg.audioStreamPacket.GetBuffer(),
         context.ogg.audioStreamPacket.GetBufferSize());

      if (result < 0)
         FailExport(XO("Failed to encode input buffer"), result);

      // granulePos is the index of the last real sample in the packet at 48k rate
      granulePos += samplesThisRun * context.opus.sampleRateFactor;

      context.ogg.audioStreamPacket.packet.bytes = result;
      context.ogg.audioStreamPacket.packet.granulepos = granulePos;

      if (latencyLeft == 0)
         context.ogg.audioStreamPacket.MarkEOS();

      context.ogg.PacketIn(context.ogg.audioStreamPacket);
      context.ogg.WriteOut(context.outFile);

      context.ogg.audioStreamPacket.packet.packetno++;

      exportResult = ExportPluginHelpers::UpdateProgress(
         delegate, *context.mixer, context.t0, context.t1);
   }

   // Flush the encoder

   while (latencyLeft > 0)
   {
      auto frameSize = GetBestFrameSize(latencyLeft);

      context.encodeBuffer.resize(frameSize * context.numChannels);

      std::fill(
         context.encodeBuffer.begin(),
         context.encodeBuffer.begin() + frameSize * context.numChannels, 0);

      auto samplesOut = std::min(latencyLeft, frameSize);

      auto result = opus_multistream_encode_float(
         context.opus.encoder, context.encodeBuffer.data(),
         frameSize, context.ogg.audioStreamPacket.GetBuffer(),
         context.ogg.audioStreamPacket.GetBufferSize());

      if (result < 0)
         FailExport(XO("Failed to encode input buffer"), result);

      granulePos += samplesOut * context.opus.sampleRateFactor;

      context.ogg.audioStreamPacket.packet.bytes = result;
      context.ogg.audioStreamPacket.packet.granulepos = granulePos;

      if (latencyLeft == samplesOut)
         context.ogg.audioStreamPacket.MarkEOS();

      context.ogg.PacketIn(context.ogg.audioStreamPacket);
      context.ogg.WriteOut(context.outFile);

      context.ogg.audioStreamPacket.packet.packetno++;

      latencyLeft -= samplesOut;
   }

   context.ogg.Flush(context.outFile);

   if (!context.outFile.Close())
      return ExportResult::Error;

   return exportResult;
}


static ExportPluginRegistry::RegisteredPlugin sRegisteredPlugin{ "Opus",
   []{ return std::make_unique< ExportOpus >(); }
};