File: AVStreamWrapperImpl.inl

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 (252 lines) | stat: -rw-r--r-- 6,438 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
/**********************************************************************

  Audacity: A Digital Audio Editor

  AVStreamWrapperImpl.inl

  Dmitry Vedenko

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

class AVStreamWrapperImpl : public AVStreamWrapper
{
public:
   AVStreamWrapperImpl(
      const FFmpegFunctions& ffmpeg, AVStream* wrapped, bool forEncoding)
       : AVStreamWrapper(ffmpeg, wrapped)
       , mForEncoding(forEncoding)
   {
   }

   int GetIndex() const noexcept override
   {
      if (mAVStream != nullptr)
         return mAVStream->index;

      return {};
   }

   int GetId() const noexcept override
   {
      if (mAVStream != nullptr)
         return mAVStream->id;

      return {};
   }

   void SetId(int id) noexcept override
   {
      if (mAVStream != nullptr)
         mAVStream->id = id;
   }

   AudacityAVRational GetTimeBase() const noexcept override
   {
      if (mAVStream != nullptr)
         return { mAVStream->time_base.num, mAVStream->time_base.den };

      return {};
   }

   void SetTimeBase(AudacityAVRational time_base) noexcept override
   {
      if (mAVStream == nullptr)
         return;

      mAVStream->time_base.num = time_base.num;
      mAVStream->time_base.den = time_base.den;
   }

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

      return {};
   }

   void SetStartTime(int64_t start_time) noexcept override
   {
      if (mAVStream != nullptr)
         mAVStream->start_time = start_time;
   }

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

      return {};
   }

   void SetDuration(int64_t duration) noexcept override
   {
      if (mAVStream != nullptr)
         mAVStream->duration = duration;
   }

   int64_t GetFramesCount() const noexcept override
   {
      if (mAVStream != nullptr)
         return mAVStream->nb_frames;

      return {};
   }

   void SetFramesCount(int64_t nb_frames) noexcept override
   {
      if (mAVStream != nullptr)
         mAVStream->nb_frames = nb_frames;
   }

   int GetDisposition() const noexcept override
   {
      if (mAVStream != nullptr)
         return mAVStream->disposition;

      return {};
   }

   void SetDisposition(int disposition) noexcept override
   {
      if (mAVStream != nullptr)
         mAVStream->disposition = disposition;
   }

   AVSampleFormatFwd GetDiscard() const noexcept override
   {
      if (mAVStream != nullptr)
         return mAVStream->discard;

      return {};
   }

   void SetDiscard(AVDiscardFwd discard) noexcept override
   {
      if (mAVStream != nullptr)
         mAVStream->discard = static_cast<AVDiscard>(discard);
   }

   AudacityAVRational GetSampleAspectRatio() const noexcept override
   {
      if (mAVStream != nullptr)
         return { mAVStream->sample_aspect_ratio.num,
                  mAVStream->sample_aspect_ratio.den };

      return {};
   }

   void SetSampleAspectRatio(
      AudacityAVRational sample_aspect_ratio) noexcept override
   {
      if (mAVStream == nullptr)
         return;

      mAVStream->sample_aspect_ratio.num = sample_aspect_ratio.num;
      mAVStream->sample_aspect_ratio.den = sample_aspect_ratio.den;
   }

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

      return AVDictionaryWrapper(mFFmpeg);
   }

   void SetMetadata(AVDictionaryWrapper metadata) noexcept override
   {
      if (mAVStream != nullptr) {
         // Destroy any previous metadata
         if (mAVStream->metadata != nullptr)
            mFFmpeg.av_dict_free(&mAVStream->metadata);

         // Does this leak or will the stream destroy the dictionary too?
         // Not stated here: https://ffmpeg.org/doxygen/trunk/structAVStream.html
         // But see: https://ffmpeg.org/doxygen/trunk/libavformat_2utils_8c_source.html#l00635
         // And where that function is called: https://ffmpeg.org/doxygen/trunk/libavformat_2utils_8c.html#af8a54ade3869125802dab7fdcd8688b3
         // So all is well IF this stream has an AVFormatContext owner
         // But note that AVStreamWrapper itself does not free resources
         // in its destructor!
         mAVStream->metadata = metadata.Release();
      }
   }

   bool IsAudio() const noexcept override
   {
      if (mAVStream != nullptr)
#if LIBAVFORMAT_VERSION_MAJOR <= 58
         return mAVStream->codec->codec_type == AVMEDIA_TYPE_AUDIO;
#else
         return mAVStream->codecpar->codec_type == AVMEDIA_TYPE_AUDIO;
#endif

      return {};
   }

   AVCodecIDFwd GetAVCodecID() const noexcept override
   {
      if (mAVStream != nullptr)
#if LIBAVFORMAT_VERSION_MAJOR <= 58
         return mAVStream->codec->codec_id;
#else
         return mAVStream->codecpar->codec_id;
#endif

      return AV_CODEC_ID_NONE;
   }

   std::unique_ptr<AVCodecContextWrapper>
   GetAVCodecContext() const noexcept override
   {
      if (mAVStream == nullptr)
         return {};
      
#if LIBAVFORMAT_VERSION_MAJOR <= 58
      return mFFmpeg.CreateAVCodecContextWrapper(mAVStream->codec);
#else
      // No memory management is involved here!
      auto avcodec = mForEncoding ?
                        mFFmpeg.avcodec_find_encoder(mAVStream->codecpar->codec_id) :
                        mFFmpeg.avcodec_find_decoder(mAVStream->codecpar->codec_id);

      AVCodecContext* codecContext = mFFmpeg.avcodec_alloc_context3(avcodec);

      if (codecContext == nullptr)
         return {};

      auto context = mFFmpeg.CreateAVCodecContextWrapper(codecContext);

      if (!mForEncoding)
      {
         auto ret = mFFmpeg.avcodec_parameters_to_context(
            codecContext, mAVStream->codecpar);

         if (ret < 0)
            return {};
      }
      
      return context;
#endif
   }

   int SetParametersFromContext(AVCodecContextWrapper& context) noexcept
   {
#if LIBAVFORMAT_VERSION_MAJOR <= 58
      return 0;
#else
      return mFFmpeg.avcodec_parameters_from_context(
         mAVStream->codecpar, context.GetWrappedValue());
#endif
   }

private:
   const bool mForEncoding;
};

std::unique_ptr<AVStreamWrapper>
CreateAVStreamWrapper(const FFmpegFunctions& ffmpeg, AVStream* wrapped, bool forEncoding)
{
   return std::make_unique<AVStreamWrapperImpl>(
      ffmpeg, wrapped, forEncoding);
}