Audacity 3.2.0
AVFormatContextWrapper.cpp
Go to the documentation of this file.
1/**********************************************************************
2
3 Audacity: A Digital Audio Editor
4
5 AVFormatContextWrapper.cpp
6
7 Dmitry Vedenko
8
9**********************************************************************/
10
12
13#include "FFmpegFunctions.h"
14
17#include "AVStreamWrapper.h"
18
20 : mFFmpeg(ffmpeg)
21{
22}
23
25{
26 return mAVFormatContext;
27}
28
30{
31 return mAVFormatContext;
32}
33
35{
36 if (mAVFormatContext != nullptr)
38}
39
41 const wxString& path,
42 const AVInputFormatWrapper* inputFormat,
44)
45{
46 auto ioContext = mFFmpeg.CreateAVIOContext();
47
48 const auto result = ioContext->Open(path, false);
49
51 return result;
52
53 SetAVIOContext(std::move(ioContext));
54
55 AVDictionary* dict = options.Release();
56
57 /*
58 Documentation for the last argument:
59 "A dictionary filled with AVFormatContext and demuxer-private options.
60 On return this parameter will be destroyed and replaced with a
61 dict containing options that were not found.
62 May be NULL."
63 */
65 &mAVFormatContext, path.c_str(),
66 inputFormat != nullptr ? inputFormat->GetWrappedValue() : nullptr,
67 &dict));
68
69 // Don't leak the replacement dictionary
70 AVDictionaryWrapper cleanup{ mFFmpeg, dict };
71
72 if (rc)
73 {
75 }
76
79
81
83
84 return result;
85}
86
89{
90 auto ioContext = mFFmpeg.CreateAVIOContext();
91
92 const auto result = ioContext->Open(path, true);
93
95 return result;
96
97 SetAVIOContext(std::move(ioContext));
98
99 return result;
100}
101
102std::unique_ptr<AVPacketWrapper> AVFormatContextWrapper::ReadNextPacket()
103{
104 std::unique_ptr<AVPacketWrapper> packet = mFFmpeg.CreateAVPacketWrapper();
105
106 if (mFFmpeg.av_read_frame(mAVFormatContext, packet->GetWrappedValue()) < 0)
107 return {};
108
109 return packet;
110}
111
112std::unique_ptr<AVStreamWrapper> AVFormatContextWrapper::CreateStream()
113{
114 // The complementary deallocation happens in avformat_free_context
116
117 if (stream == nullptr)
118 return {};
119
121
122 return mFFmpeg.CreateAVStreamWrapper(stream, true);
123}
124
127{
128 return mInputFormat.get();
129}
130
133{
134 return mOutputFormat.get();
135}
136
137const AVStreamWrapper*
138AVFormatContextWrapper::GetStream(int index) const noexcept
139{
140 if (index < GetStreamsCount())
141 return GetStreams()[index].get();
142
143 return nullptr;
144}
struct AVDictionary AVDictionary
Definition: FFmpegTypes.h:117
AVDictionary * Release() noexcept
std::unique_ptr< AVOutputFormatWrapper > mOutputFormat
AVFormatContextWrapper(const AVFormatContextWrapper &)=delete
virtual const AVStreamWrapper * GetStream(int index) const noexcept
const AVOutputFormatWrapper * GetOutputFormat() const noexcept
virtual void SetAVIOContext(std::unique_ptr< AVIOContextWrapper > pb) noexcept=0
const FFmpegFunctions & mFFmpeg
AVIOContextWrapper::OpenResult OpenInputContext(const wxString &path, const AVInputFormatWrapper *inputFormat, AVDictionaryWrapper options)
std::unique_ptr< AVPacketWrapper > ReadNextPacket()
AVFormatContext * GetWrappedValue() noexcept
std::unique_ptr< AVStreamWrapper > CreateStream()
const AVInputFormatWrapper * GetInputFormat() const noexcept
std::unique_ptr< AVInputFormatWrapper > mInputFormat
virtual void UpdateStreamList() noexcept=0
virtual AVInputFormat * GetIFormat() const noexcept=0
AVIOContextWrapper::OpenResult OpenOutputContext(const wxString &path)
AVFormatContext * mAVFormatContext
AVInputFormat * GetWrappedValue() noexcept
AVStream *(* avformat_new_stream)(AVFormatContext *s, const AVCodec *c)
int(* avformat_find_stream_info)(AVFormatContext *ic, AVDictionary **options)
void(* avformat_free_context)(AVFormatContext *s)
int(* avformat_open_input)(AVFormatContext **ic_ptr, const char *filename, const AVInputFormat *fmt, AVDictionary **options)
int(* av_read_frame)(AVFormatContext *s, AVPacket *pkt)
std::unique_ptr< AVInputFormatWrapper > CreateAVInputFormatWrapper(AVInputFormat *inputFormat) const
std::unique_ptr< AVIOContextWrapper > CreateAVIOContext() const
std::unique_ptr< AVStreamWrapper > CreateAVStreamWrapper(AVStream *stream, bool forEncoding) const
std::unique_ptr< AVPacketWrapper > CreateAVPacketWrapper() const