@@ -28,8 +28,6 @@ static constexpr std::array<std::array<s32, 15>, 2> BitrateTable = {{
28
28
29
29
static constexpr std::array<s32, 2 > UnkTable = {0x48 , 0x90 };
30
30
31
- SwrContext* swr_context{};
32
-
33
31
static AVSampleFormat AjmToAVSampleFormat (AjmFormatEncoding format) {
34
32
switch (format) {
35
33
case AjmFormatEncoding::S16:
@@ -49,26 +47,28 @@ AVFrame* AjmMp3Decoder::ConvertAudioFrame(AVFrame* frame) {
49
47
return frame;
50
48
}
51
49
52
- auto pcm16_frame = av_frame_clone (frame);
53
- pcm16_frame->format = format;
50
+ AVFrame* new_frame = av_frame_alloc ();
51
+ new_frame->pts = frame->pts ;
52
+ new_frame->pkt_dts = frame->pkt_dts < 0 ? 0 : frame->pkt_dts ;
53
+ new_frame->format = format;
54
+ new_frame->ch_layout = frame->ch_layout ;
55
+ new_frame->sample_rate = frame->sample_rate ;
54
56
55
- if (swr_context) {
56
- swr_free (&swr_context);
57
- swr_context = nullptr ;
58
- }
59
57
AVChannelLayout in_ch_layout = frame->ch_layout ;
60
- AVChannelLayout out_ch_layout = pcm16_frame ->ch_layout ;
61
- swr_alloc_set_opts2 (&swr_context , &out_ch_layout, AVSampleFormat (pcm16_frame ->format ),
58
+ AVChannelLayout out_ch_layout = new_frame ->ch_layout ;
59
+ swr_alloc_set_opts2 (&m_swr_context , &out_ch_layout, AVSampleFormat (new_frame ->format ),
62
60
frame->sample_rate , &in_ch_layout, AVSampleFormat (frame->format ),
63
61
frame->sample_rate , 0 , nullptr );
64
- swr_init (swr_context );
65
- const auto res = swr_convert_frame (swr_context, pcm16_frame , frame);
62
+ swr_init (m_swr_context );
63
+ const auto res = swr_convert_frame (m_swr_context, new_frame , frame);
66
64
if (res < 0 ) {
67
65
LOG_ERROR (Lib_AvPlayer, " Could not convert to S16: {}" , av_err2str (res));
66
+ av_frame_free (&new_frame);
67
+ av_frame_free (&frame);
68
68
return nullptr ;
69
69
}
70
70
av_frame_free (&frame);
71
- return pcm16_frame ;
71
+ return new_frame ;
72
72
}
73
73
74
74
AjmMp3Decoder::AjmMp3Decoder (AjmFormatEncoding format)
@@ -78,6 +78,7 @@ AjmMp3Decoder::AjmMp3Decoder(AjmFormatEncoding format)
78
78
}
79
79
80
80
AjmMp3Decoder::~AjmMp3Decoder () {
81
+ swr_free (&m_swr_context);
81
82
avcodec_free_context (&m_codec_context);
82
83
}
83
84
@@ -108,6 +109,11 @@ std::tuple<u32, u32> AjmMp3Decoder::ProcessData(std::span<u8>& in_buf, SparseOut
108
109
u32 frames_decoded = 0 ;
109
110
u32 samples_decoded = 0 ;
110
111
112
+ auto max_samples =
113
+ max_samples_per_channel.has_value ()
114
+ ? max_samples_per_channel.value () * m_codec_context->ch_layout .nb_channels
115
+ : std::numeric_limits<u32 >::max ();
116
+
111
117
if (pkt->size ) {
112
118
// Send the packet with the compressed data to the decoder
113
119
pkt->pts = m_parser->pts ;
@@ -121,6 +127,7 @@ std::tuple<u32, u32> AjmMp3Decoder::ProcessData(std::span<u8>& in_buf, SparseOut
121
127
AVFrame* frame = av_frame_alloc ();
122
128
ret = avcodec_receive_frame (m_codec_context, frame);
123
129
if (ret == AVERROR (EAGAIN) || ret == AVERROR_EOF) {
130
+ av_frame_free (&frame);
124
131
break ;
125
132
} else if (ret < 0 ) {
126
133
UNREACHABLE_MSG (" Error during decoding" );
@@ -135,11 +142,6 @@ std::tuple<u32, u32> AjmMp3Decoder::ProcessData(std::span<u8>& in_buf, SparseOut
135
142
gapless.skipped_samples += skipped_samples;
136
143
}
137
144
138
- const auto max_samples =
139
- max_samples_per_channel.has_value ()
140
- ? max_samples_per_channel.value () * frame->ch_layout .nb_channels
141
- : std::numeric_limits<u32 >::max ();
142
-
143
145
switch (m_format) {
144
146
case AjmFormatEncoding::S16:
145
147
samples_decoded +=
@@ -157,6 +159,8 @@ std::tuple<u32, u32> AjmMp3Decoder::ProcessData(std::span<u8>& in_buf, SparseOut
157
159
UNREACHABLE ();
158
160
}
159
161
162
+ max_samples -= samples_decoded;
163
+
160
164
av_frame_free (&frame);
161
165
}
162
166
}
0 commit comments