Skip to content

Commit 873dfb6

Browse files
committed
Added basic wrapper for AVCodecParameters entity
1 parent 356e1c7 commit 873dfb6

File tree

2 files changed

+156
-0
lines changed

2 files changed

+156
-0
lines changed

src/codecparameters.cpp

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
#include "codecparameters.h"
2+
#include "codeccontext.h"
3+
4+
namespace av {
5+
6+
CodecParametersView::CodecParametersView(AVCodecParameters *codecpar)
7+
: FFWrapperPtr<AVCodecParameters>(codecpar)
8+
{
9+
}
10+
11+
bool CodecParametersView::isValid() const
12+
{
13+
return !isNull();
14+
}
15+
16+
void CodecParametersView::copyFrom(CodecParametersView src, OptionalErrorCode ec)
17+
{
18+
clear_if(ec);
19+
20+
if (!m_raw) {
21+
throws_if(ec, Errors::Unallocated);
22+
return;
23+
}
24+
25+
if (!src.m_raw) {
26+
throws_if(ec, Errors::Unallocated);
27+
return;
28+
}
29+
30+
auto const ret = avcodec_parameters_copy(m_raw, src.m_raw);
31+
if (ret) {
32+
throws_if(ec, ret, ffmpeg_category());
33+
}
34+
}
35+
36+
void CodecParametersView::copyTo(CodecParametersView dst, OptionalErrorCode ec) const
37+
{
38+
clear_if(ec);
39+
40+
if (!m_raw) {
41+
throws_if(ec, Errors::Unallocated);
42+
return;
43+
}
44+
45+
if (!dst.m_raw) {
46+
throws_if(ec, Errors::Unallocated);
47+
return;
48+
}
49+
50+
auto const ret = avcodec_parameters_copy(dst.m_raw, m_raw);
51+
if (ret) {
52+
throws_if(ec, ret, ffmpeg_category());
53+
}
54+
}
55+
56+
void CodecParametersView::copyFrom(const CodecContext2 &src, OptionalErrorCode ec)
57+
{
58+
clear_if(ec);
59+
60+
if (!m_raw) {
61+
throws_if(ec, Errors::Unallocated);
62+
return;
63+
}
64+
65+
if (!src.raw()) {
66+
throws_if(ec, Errors::Unallocated);
67+
return;
68+
}
69+
70+
auto const ret = avcodec_parameters_from_context(m_raw, src.raw());
71+
if (ret) {
72+
throws_if(ec, ret, ffmpeg_category());
73+
}
74+
}
75+
76+
void CodecParametersView::copyTo(CodecContext2 &dst, OptionalErrorCode ec) const
77+
{
78+
clear_if(ec);
79+
80+
if (!m_raw) {
81+
throws_if(ec, Errors::Unallocated);
82+
return;
83+
}
84+
85+
if (!dst.raw()) {
86+
throws_if(ec, Errors::Unallocated);
87+
return;
88+
}
89+
90+
auto const ret = avcodec_parameters_to_context(dst.raw(), m_raw);
91+
if (ret) {
92+
throws_if(ec, ret, ffmpeg_category());
93+
}
94+
}
95+
96+
int CodecParametersView::getAudioFrameDuration(int frame_bytes) const
97+
{
98+
if (!m_raw)
99+
return 0;
100+
return av_get_audio_frame_duration2(const_cast<AVCodecParameters*>(m_raw), frame_bytes);
101+
}
102+
103+
104+
105+
CodecParameters::CodecParameters()
106+
: CodecParametersView(avcodec_parameters_alloc())
107+
{
108+
}
109+
110+
CodecParameters::~CodecParameters()
111+
{
112+
avcodec_parameters_free(&m_raw);
113+
}
114+
115+
} // namespace av

src/codecparameters.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#pragma once
2+
3+
#include "averror.h"
4+
#include "ffmpeg.h"
5+
#include "avutils.h"
6+
7+
extern "C" {
8+
#include <libavcodec/codec_par.h>
9+
}
10+
11+
12+
namespace av {
13+
14+
/**
15+
* @brief The CodecParametersView class
16+
*
17+
* Not-owned wrapper for AVCodecParameters. Do not keep for a long time.
18+
*/
19+
class CodecParametersView : public FFWrapperPtr<AVCodecParameters>
20+
{
21+
public:
22+
CodecParametersView(AVCodecParameters *codecpar = nullptr);
23+
bool isValid() const;
24+
25+
void copyFrom(CodecParametersView src, OptionalErrorCode ec = throws());
26+
void copyTo(CodecParametersView dst, OptionalErrorCode ec = throws()) const;
27+
28+
void copyFrom(const class CodecContext2& src, OptionalErrorCode ec = throws());
29+
void copyTo(class CodecContext2& dst, OptionalErrorCode ec = throws()) const;
30+
31+
int getAudioFrameDuration(int frame_bytes) const;
32+
};
33+
34+
class CodecParameters : CodecParametersView, public noncopyable
35+
{
36+
public:
37+
CodecParameters();
38+
~CodecParameters();
39+
};
40+
41+
} // namespace av

0 commit comments

Comments
 (0)