File: AVIOContextWrapper.cpp

package info (click to toggle)
audacity 3.7.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • 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 (127 lines) | stat: -rw-r--r-- 3,061 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
/**********************************************************************

  Audacity: A Digital Audio Editor

  AVIOContextWrapper.cpp

  Dmitry Vedenko

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

#include "AVIOContextWrapper.h"

#include "FFmpegFunctions.h"

#define AVSEEK_FORCE 0x20000
#define AVSEEK_SIZE 0x10000

constexpr int BufferSize = 32 * 1024;

AVIOContextWrapper::AVIOContextWrapper(
   const FFmpegFunctions& ffmpeg) noexcept
    : mFFmpeg(ffmpeg)
{
}

AVIOContext* AVIOContextWrapper::GetWrappedValue() noexcept
{
   return mAVIOContext;
}

const AVIOContext* AVIOContextWrapper::GetWrappedValue() const noexcept
{
   return mAVIOContext;
}

AVIOContextWrapper::~AVIOContextWrapper()
{
   if (mAVIOContext == nullptr)
      return;

   if (mFFmpeg.avio_context_free != nullptr)
      mFFmpeg.avio_context_free(&mAVIOContext);
   else
      mFFmpeg.av_free(mAVIOContext);
}

AVIOContextWrapper::OpenResult
AVIOContextWrapper::Open(const wxString& fileName, bool forWriting)
{
   auto pFile = std::make_unique<wxFile>();
   if (!pFile->Open(fileName, forWriting ? wxFile::write : wxFile::read))
      return OpenResult::FileOpenFailed;

   unsigned char* buffer =
      static_cast<unsigned char*>(mFFmpeg.av_malloc(BufferSize));

   if (buffer == nullptr)
      return OpenResult::NoMemory;

   /*
    "The buffer must be allocated with av_malloc() and friends. It may be freed
    and replaced with a new buffer by libavformat. AVIOContext.buffer holds the
    buffer currently in use, which must be later freed with av_free()."

    See ~AVIOContextWrapperImpl() for the deallocation
    */
   mAVIOContext = mFFmpeg.avio_alloc_context(
      buffer, BufferSize,
      static_cast<int>(forWriting),
      this,
      FileRead, FileWrite, FileSeek
   );

   if (mAVIOContext == nullptr) {
      mFFmpeg.av_free(buffer);
      return OpenResult::NoMemory;
   }

   mpFile = move(pFile);

   return OpenResult::Success;
}

int AVIOContextWrapper::FileRead(void* opaque, uint8_t* buf, int size)
{
   AVIOContextWrapper* wrapper = static_cast<AVIOContextWrapper*>(opaque);
   
   if (wrapper == nullptr)
      return AUDACITY_AVERROR(EINVAL);
   
   return wrapper->Read(buf, size);
}

int AVIOContextWrapper::FileWrite(void* opaque, const uint8_t* buf, int size)
{
   AVIOContextWrapper* wrapper = static_cast<AVIOContextWrapper*>(opaque);
   if (!(wrapper && wrapper->mpFile))
      return {};
   return wrapper->mpFile->Write(buf, size);
}

int64_t AVIOContextWrapper::FileSeek(void* opaque, int64_t pos, int whence)
{
   AVIOContextWrapper* wrapper = static_cast<AVIOContextWrapper*>(opaque);
   if (!(wrapper && wrapper->mpFile))
      return {};

   wxSeekMode mode = wxFromStart;

   switch (whence & ~AVSEEK_FORCE)
   {
   case (SEEK_SET):
      mode = wxFromStart;
      break;
   case (SEEK_CUR):
      mode = wxFromCurrent;
      break;
   case (SEEK_END):
      mode = wxFromEnd;
      break;
   case (AVSEEK_SIZE):
      return wrapper->mpFile->Length();
   }


   return wrapper->mpFile->Seek(pos, mode);
}