File: ImportOGG.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 (371 lines) | stat: -rw-r--r-- 11,300 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
/**********************************************************************

  Audacity: A Digital Audio Editor

  ImportOGG.cpp

  Joshua Haberman
  Leland Lucius

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

\class ImportFileHandle
\brief An ImportFileHandle for data

  The Ogg format supports multiple logical bitstreams that can be chained
  within the physical bitstream. The sampling rate and number of channels
  can vary between these logical bitstreams. For the moment, we'll ignore
  all but the first logical bitstream.

  Ogg also allows for an arbitrary number of channels. Luckily, so does
  Audacity. We'll call the first channel LeftChannel, the second
  RightChannel, and all others after it MonoChannel.

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

\class OGGImportPlugin
\brief An ImportPlugin for OGG data

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

#include "Import.h"
#include "Tags.h"


#define DESC XO("Ogg Vorbis files")

static const auto exts = {
   wxT("ogg")
};

#include <wx/log.h>
#include <wx/setup.h> // see next comment
/* ffile.h must be included AFTER at least one other wx header that includes
 * wx/setup.h, otherwise #ifdefs erroneously collapse it to nothing. This is
 * a bug in wxWidgets (ffile.h should itself include wx/setup.h), and it
 * was a bitch to track down. */
#include <wx/ffile.h>

#include <vorbis/vorbisfile.h>

#include "WaveTrack.h"
#include "ImportPlugin.h"
#include "ImportProgressListener.h"
#include "ImportUtils.h"

class OggImportPlugin final : public ImportPlugin
{
public:
   OggImportPlugin()
   :  ImportPlugin( FileExtensions( exts.begin(), exts.end() ) )
   {
   }

   ~OggImportPlugin() { }

   wxString GetPluginStringID() override { return wxT("liboggvorbis"); }
   TranslatableString GetPluginFormatDescription() override;
   std::unique_ptr<ImportFileHandle> Open(
      const FilePath &Filename, AudacityProject*) override;
};


class OggImportFileHandle final : public ImportFileHandleEx
{
public:
   OggImportFileHandle(const FilePath & filename,
                       std::unique_ptr<wxFFile> &&file,
                       std::unique_ptr<OggVorbis_File> &&vorbisFile)
   :  ImportFileHandleEx(filename),
      mFile(std::move(file)),
      mVorbisFile(std::move(vorbisFile))
      , mStreamUsage{ static_cast<size_t>(mVorbisFile->links) }
   {
      for (int i = 0; i < mVorbisFile->links; i++)
      {
         auto strinfo = XO("Index[%02x] Version[%d], Channels[%d], Rate[%ld]")
            .Format(
               (unsigned int) i,
               mVorbisFile->vi[i].version,
               mVorbisFile->vi[i].channels,
               mVorbisFile->vi[i].rate);
         mStreamInfo.push_back(strinfo);
         mStreamUsage[i] = 0;
      }

   }
   ~OggImportFileHandle();

   TranslatableString GetFileDescription() override;
   ByteCount GetFileUncompressedBytes() override;
   void Import(
      ImportProgressListener& progressListener, WaveTrackFactory* trackFactory,
      TrackHolders& outTracks, Tags* tags,
      std::optional<LibFileFormats::AcidizerTags>& outAcidTags) override;

   wxInt32 GetStreamCount() override
   {
      if (mVorbisFile)
         return mVorbisFile->links;
      else
         return 0;
   }

   const TranslatableStrings &GetStreamInfo() override
   {
      return mStreamInfo;
   }

   void SetStreamUsage(wxInt32 StreamID, bool Use) override
   {
      if (mVorbisFile)
      {
         if (StreamID < mVorbisFile->links)
            mStreamUsage[StreamID] = (Use ? 1 : 0);
      }
   }

private:
   std::unique_ptr<wxFFile> mFile;
   std::unique_ptr<OggVorbis_File> mVorbisFile;

   ArrayOf<int> mStreamUsage;
   TranslatableStrings mStreamInfo;
   std::vector<TrackListHolder> mStreams;
};


TranslatableString OggImportPlugin::GetPluginFormatDescription()
{
    return DESC;
}

std::unique_ptr<ImportFileHandle> OggImportPlugin::Open(
   const FilePath &filename, AudacityProject*)
{
   // Suppress some compiler warnings about unused global variables in the library header
   wxUnusedVar(OV_CALLBACKS_DEFAULT);
   wxUnusedVar(OV_CALLBACKS_NOCLOSE);
   wxUnusedVar(OV_CALLBACKS_STREAMONLY);
   wxUnusedVar(OV_CALLBACKS_STREAMONLY_NOCLOSE);

   auto vorbisFile = std::make_unique<OggVorbis_File>();
   auto file = std::make_unique<wxFFile>(filename, wxT("rb"));

   if (!file->IsOpened()) {
      // No need for a message box, it's done automatically (but how?)
      return nullptr;
   }

   int err = ov_open(file->fp(), vorbisFile.get(), NULL, 0);

   if (err < 0) {
      TranslatableString message;

      switch (err) {
         case OV_EREAD:
            message = XO("Media read error");
            break;
         case OV_ENOTVORBIS:
            message = XO("Not an Ogg Vorbis file");
            break;
         case OV_EVERSION:
            message = XO("Vorbis version mismatch");
            break;
         case OV_EBADHEADER:
            message = XO("Invalid Vorbis bitstream header");
            break;
         case OV_EFAULT:
            message = XO("Internal logic fault");
            break;
      }

      // what to do with message?
      return nullptr;
   }

   return std::make_unique<OggImportFileHandle>(filename, std::move(file), std::move(vorbisFile));
}

static Importer::RegisteredImportPlugin registered{ "OGG",
   std::make_unique< OggImportPlugin >()
};

TranslatableString OggImportFileHandle::GetFileDescription()
{
   return DESC;
}

auto OggImportFileHandle::GetFileUncompressedBytes() -> ByteCount
{
   // TODO:
   return 0;
}

void OggImportFileHandle::Import(
   ImportProgressListener& progressListener, WaveTrackFactory* trackFactory,
   TrackHolders& outTracks, Tags* tags,
   std::optional<LibFileFormats::AcidizerTags>&)
{
   BeginImport();

   outTracks.clear();

   wxASSERT(mFile->IsOpened());

   //Number of streams used may be less than mVorbisFile->links,
   //but this way bitstream matches array index.
   mStreams.reserve(mVorbisFile->links);

   for(int i = 0; i < mVorbisFile->links; ++i)
   {
      //Stream is not used
      if (mStreamUsage[i] == 0)
      {
         //This is just a padding to keep bitstream number and
         //array indices matched.
         mStreams.push_back({});
         continue;
      }

      vorbis_info *vi = ov_info(mVorbisFile.get(), i);

      // The format agrees with what is always passed to Append() below
      auto tracks = trackFactory->CreateMany(vi->channels, int16Sample, vi->rate);

      mStreams.push_back(tracks);
   }

   /* The number of bytes to get from the codec in each run */
#define CODEC_TRANSFER_SIZE 4096u

   /* The number of samples to read between calls to the callback.
    * Balance between responsiveness of the GUI and throughput of import. */
#define SAMPLES_PER_CALLBACK 100000

   long bytesRead = 0;
   {
      ArrayOf<short> mainBuffer{ CODEC_TRANSFER_SIZE };

      /* determine endianness (clever trick courtesy of Nicholas Devillard,
       * (http://www.eso.org/~ndevilla/endian/) */
      int testvar = 1, endian;
      if (*(char *)&testvar)
         endian = 0;  // little endian
      else
         endian = 1;  // big endian

      /* number of samples currently in each channel's buffer */
      long samplesRead = 0;
      int bitstream = 0;
      int samplesSinceLastCallback = 0;

      // You would think that the stream would already be seeked to 0, and
      // indeed it is if the file is legit.  But I had several ogg files on
      // my hard drive that have malformed headers, and this added call
      // causes them to be read correctly.  Otherwise they have lots of
      // zeros inserted at the beginning
      ov_pcm_seek(mVorbisFile.get(), 0);

      do {
         /* get data from the decoder */
         bytesRead = ov_read(mVorbisFile.get(), (char *)mainBuffer.get(),
            CODEC_TRANSFER_SIZE,
            endian,
            2,    // word length (2 for 16 bit samples)
            1,    // signed
            &bitstream);

         if (bytesRead == OV_HOLE) {
            wxFileName ff(GetFilename());
            wxLogError(wxT("Ogg Vorbis importer: file %s is malformed, ov_read() reported a hole"),
               ff.GetFullName());
            /* http://lists.xiph.org/pipermail/vorbis-dev/2001-February/003223.html
             * is the justification for doing this - best effort for malformed file,
             * hence the message.
             */
            continue;
         }
         else if (bytesRead < 0) {
            /* Malformed Ogg Vorbis file. */
            /* TODO: Return some sort of meaningful error. */
            wxLogError(wxT("Ogg Vorbis importer: ov_read() returned error %i"),
               bytesRead);
            break;
         }

         samplesRead = bytesRead / mVorbisFile->vi[bitstream].channels / sizeof(short);

         if (mStreamUsage[bitstream] != 0)
         {
            /* give the data to the wavetracks */
            unsigned chn = 0;
            ImportUtils::ForEachChannel(**std::next(mStreams.begin(), bitstream), [&](auto& channel)
            {
               channel.AppendBuffer(
                  (char *)(mainBuffer.get() + chn),
                  int16Sample,
                  samplesRead,
                  mVorbisFile->vi[bitstream].channels,
                  int16Sample
               );
               ++chn;
            });
         }

         samplesSinceLastCallback += samplesRead;
         if (samplesSinceLastCallback > SAMPLES_PER_CALLBACK) {
            const auto timeTotal = ov_time_total(mVorbisFile.get(), bitstream);
            if(timeTotal > 0)
               progressListener.OnImportProgress(ov_time_tell(mVorbisFile.get()) / timeTotal);
            samplesSinceLastCallback -= SAMPLES_PER_CALLBACK;
         }
      } while (!IsCancelled() && !IsStopped() && bytesRead != 0);
   }

   if (bytesRead < 0)
   {
      progressListener.OnImportResult(ImportProgressListener::ImportResult::Error);
      return;
   }

   if(IsCancelled())
   {
      progressListener.OnImportResult(ImportProgressListener::ImportResult::Cancelled);
      return;
   }

   for (auto& stream : mStreams)
   {
      ImportUtils::FinalizeImport(outTracks, std::move(*stream));
   }
   mStreams.clear();

   //\todo { Extract comments from each stream? }
   if (mVorbisFile->vc[0].comments > 0) {
      tags->Clear();
      for (int c = 0; c < mVorbisFile->vc[0].comments; c++) {
         wxString comment = UTF8CTOWX(mVorbisFile->vc[0].user_comments[c]);
         wxString name = comment.BeforeFirst(wxT('='));
         wxString value = comment.AfterFirst(wxT('='));
         if (name.Upper() == wxT("DATE") && !tags->HasTag(TAG_YEAR)) {
            long val;
            if (value.length() == 4 && value.ToLong(&val)) {
               name = TAG_YEAR;
            }
         }
         tags->SetTag(name, value);
      }
   }

   progressListener.OnImportResult(IsStopped()
                                   ? ImportProgressListener::ImportResult::Stopped
                                   : ImportProgressListener::ImportResult::Success);
}

OggImportFileHandle::~OggImportFileHandle()
{
   ov_clear(mVorbisFile.get());
   mFile->Detach();    // so that it doesn't try to close the file (ov_clear()
                       // did that already)
}