Skip to content

Commit 0495b49

Browse files
committed
Use av::CodecParametersView
1 parent 873dfb6 commit 0495b49

File tree

5 files changed

+30
-13
lines changed

5 files changed

+30
-13
lines changed

src/codeccontext.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "packet.h"
1010
#include "dictionary.h"
1111
#include "codec.h"
12+
#include "codecparameters.h"
1213

1314
#include "codeccontext.h"
1415

@@ -311,7 +312,7 @@ CodecContext2::CodecContext2(const Stream &st, const Codec &codec, Direction dir
311312
#else
312313
m_raw = avcodec_alloc_context3(c.raw());
313314
if (m_raw) {
314-
avcodec_parameters_to_context(m_raw, st.raw()->codecpar);
315+
st.codecParameters().copyTo(*this);
315316
}
316317
#endif
317318

@@ -397,8 +398,9 @@ void CodecContext2::setCodec(const Codec &codec, bool resetDefaults, Direction d
397398
FF_ENABLE_DEPRECATION_WARNINGS
398399
#else
399400
// TBD: need a check
400-
if (m_stream.isValid())
401-
avcodec_parameters_from_context(m_stream.raw()->codecpar, m_raw);
401+
if (m_stream.isValid()) {
402+
m_stream.codecParameters().copyFrom(*this);
403+
}
402404
#endif
403405
}
404406

@@ -507,9 +509,9 @@ void CodecContext2::copyContextFrom(const CodecContext2 &other, OptionalErrorCod
507509
throws_if(ec, stat, ffmpeg_category());
508510
FF_ENABLE_DEPRECATION_WARNINGS
509511
#else
510-
AVCodecParameters params{};
511-
avcodec_parameters_from_context(&params, other.m_raw);
512-
avcodec_parameters_to_context(m_raw, &params);
512+
CodecParameters params;
513+
params.copyFrom(other);
514+
params.copyTo(*this);
513515
#endif
514516
m_raw->codec_tag = 0;
515517
}

src/codecparameters.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class CodecParametersView : public FFWrapperPtr<AVCodecParameters>
3131
int getAudioFrameDuration(int frame_bytes) const;
3232
};
3333

34-
class CodecParameters : CodecParametersView, public noncopyable
34+
class CodecParameters : public CodecParametersView, public noncopyable
3535
{
3636
public:
3737
CodecParameters();

src/formatcontext.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include "formatcontext.h"
99
#include "codeccontext.h"
10+
#include "codecparameters.h"
1011

1112
using namespace std;
1213

@@ -901,12 +902,10 @@ Stream FormatContext::addStream(const CodecContext2 &ctx, OptionalErrorCode ec)
901902
return Stream();
902903
}
903904

904-
auto ret = avcodec_parameters_from_context(st->codecpar, ctx.raw());
905-
if (ret < 0) {
906-
throws_if(ec, ret, ffmpeg_category());
907-
}
905+
auto stream = Stream(m_monitor, st, Direction::Encoding);
906+
stream.codecParameters().copyFrom(ctx, ec);
908907

909-
return Stream(m_monitor, st, Direction::Encoding);
908+
return stream;
910909
}
911910

912911
void FormatContext::writeTrailer(OptionalErrorCode ec)

src/stream.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,18 @@ void Stream::setAverageFrameRate(const Rational &frameRate)
122122
RAW_SET2(isValid(), avg_frame_rate, frameRate.getValue());
123123
}
124124

125+
CodecParametersView Stream::codecParameters() const
126+
{
127+
return m_raw ? m_raw->codecpar : nullptr;
128+
}
129+
130+
void Stream::setCodecParameters(CodecParametersView codecpar, OptionalErrorCode ec)
131+
{
132+
if (m_raw && m_raw->codecpar) {
133+
codecpar.copyTo(m_raw->codecpar, ec);
134+
}
135+
}
136+
125137
int Stream::eventFlags() const noexcept
126138
{
127139
if (!isValid() || m_direction != Direction::Decoding)
@@ -153,7 +165,7 @@ void Stream::setupEncodingParameters(const VideoEncoderContext &ctx, OptionalErr
153165
return;
154166
}
155167

156-
avcodec_parameters_from_context(m_raw->codecpar, ctx.raw());
168+
codecParameters().copyFrom(ctx);
157169
}
158170

159171
} // ::av

src/stream.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "ffmpeg.h"
77
#include "rational.h"
88
#include "timestamp.h"
9+
#include "codecparameters.h"
910

1011
namespace av
1112
{
@@ -56,6 +57,9 @@ class Stream : public FFWrapperPtr<AVStream>
5657
void setSampleAspectRatio(const Rational &aspectRatio);
5758
void setAverageFrameRate(const Rational &frameRate);
5859

60+
CodecParametersView codecParameters() const;
61+
void setCodecParameters(CodecParametersView codecpar, OptionalErrorCode ec = throws());
62+
5963
/**
6064
* Flags to the user to detect events happening on the stream.
6165
* A combination of AVSTREAM_EVENT_FLAG_*. Must be cleared by the user.

0 commit comments

Comments
 (0)