File: AVFormatContextWrapper.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 (144 lines) | stat: -rw-r--r-- 3,583 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
/**********************************************************************

  Audacity: A Digital Audio Editor

  AVFormatContextWrapper.cpp

  Dmitry Vedenko

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

#include "AVFormatContextWrapper.h"

#include "FFmpegFunctions.h"

#include "AVInputFormatWrapper.h"
#include "AVOutputFormatWrapper.h"
#include "AVStreamWrapper.h"

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

AVFormatContext* AVFormatContextWrapper::GetWrappedValue() noexcept
{
   return mAVFormatContext;
}

const AVFormatContext* AVFormatContextWrapper::GetWrappedValue() const noexcept
{
   return mAVFormatContext;
}

AVFormatContextWrapper::~AVFormatContextWrapper()
{
   if (mAVFormatContext != nullptr)
      mFFmpeg.avformat_free_context(mAVFormatContext);
}

AVIOContextWrapper::OpenResult AVFormatContextWrapper::OpenInputContext(
   const wxString& path,
   const AVInputFormatWrapper* inputFormat,
   AVDictionaryWrapper options
)
{
   auto ioContext = mFFmpeg.CreateAVIOContext();

   const auto result = ioContext->Open(path, false);

   if (result != AVIOContextWrapper::OpenResult::Success)
      return result;

   SetAVIOContext(std::move(ioContext));

   AVDictionary* dict = options.Release();

   /*
      Documentation for the last argument:
      "A dictionary filled with AVFormatContext and demuxer-private options.
      On return this parameter will be destroyed and replaced with a
      dict containing options that were not found.
      May be NULL."
    */
   int rc = (mFFmpeg.avformat_open_input(
          &mAVFormatContext, path.c_str(),
          inputFormat != nullptr ? inputFormat->GetWrappedValue() : nullptr,
          &dict));

   // Don't leak the replacement dictionary
   AVDictionaryWrapper cleanup{ mFFmpeg, dict };

   if (rc)
   {
      return AVIOContextWrapper::OpenResult::InternalError;
   }

   if (mFFmpeg.avformat_find_stream_info(mAVFormatContext, nullptr) < 0)
      return AVIOContextWrapper::OpenResult::InternalError;

   UpdateStreamList();

   mInputFormat = mFFmpeg.CreateAVInputFormatWrapper(GetIFormat());

   return result;
}

AVIOContextWrapper::OpenResult
AVFormatContextWrapper::OpenOutputContext(const wxString& path)
{
   auto ioContext = mFFmpeg.CreateAVIOContext();

   const auto result = ioContext->Open(path, true);

   if (result != AVIOContextWrapper::OpenResult::Success)
      return result;

   SetAVIOContext(std::move(ioContext));

   return result;
}

std::unique_ptr<AVPacketWrapper> AVFormatContextWrapper::ReadNextPacket()
{
   std::unique_ptr<AVPacketWrapper> packet = mFFmpeg.CreateAVPacketWrapper();

   if (mFFmpeg.av_read_frame(mAVFormatContext, packet->GetWrappedValue()) < 0)
      return {};

   return packet;
}

std::unique_ptr<AVStreamWrapper> AVFormatContextWrapper::CreateStream()
{
   // The complementary deallocation happens in avformat_free_context
   AVStream* stream = mFFmpeg.avformat_new_stream(mAVFormatContext, nullptr);

   if (stream == nullptr)
      return {};

   UpdateStreamList();

   return mFFmpeg.CreateAVStreamWrapper(stream, true);
}

const AVInputFormatWrapper*
AVFormatContextWrapper::GetInputFormat() const noexcept
{
   return mInputFormat.get();
}

const AVOutputFormatWrapper*
AVFormatContextWrapper::GetOutputFormat() const noexcept
{
   return mOutputFormat.get();
}

const AVStreamWrapper*
AVFormatContextWrapper::GetStream(int index) const noexcept
{
   if (index < GetStreamsCount())
      return GetStreams()[index].get();

   return nullptr;
}