11// SPDX-FileCopyrightText: Copyright 2024 shadPS4 Emulator Project
22// SPDX-License-Identifier: GPL-2.0-or-later
33
4- #include " core/libraries/ajm/ajm_at9.h"
5- #include " core/libraries/ajm/ajm_instance.h"
6- #include " core/libraries/ajm/ajm_mp3.h"
4+ #include " ajm_at9.h"
5+ #include " ajm_instance.h"
6+ #include " ajm_mp3.h"
7+ #include " ajm_result.h"
78
89#include < magic_enum/magic_enum.hpp>
910
1011namespace Libraries ::Ajm {
1112
12- constexpr int ORBIS_AJM_RESULT_NOT_INITIALIZED = 0x00000001 ;
13- constexpr int ORBIS_AJM_RESULT_INVALID_DATA = 0x00000002 ;
14- constexpr int ORBIS_AJM_RESULT_INVALID_PARAMETER = 0x00000004 ;
15- constexpr int ORBIS_AJM_RESULT_PARTIAL_INPUT = 0x00000008 ;
16- constexpr int ORBIS_AJM_RESULT_NOT_ENOUGH_ROOM = 0x00000010 ;
17- constexpr int ORBIS_AJM_RESULT_STREAM_CHANGE = 0x00000020 ;
18- constexpr int ORBIS_AJM_RESULT_TOO_MANY_CHANNELS = 0x00000040 ;
19- constexpr int ORBIS_AJM_RESULT_UNSUPPORTED_FLAG = 0x00000080 ;
20- constexpr int ORBIS_AJM_RESULT_SIDEBAND_TRUNCATED = 0x00000100 ;
21- constexpr int ORBIS_AJM_RESULT_PRIORITY_PASSED = 0x00000200 ;
22- constexpr int ORBIS_AJM_RESULT_CODEC_ERROR = 0x40000000 ;
23- constexpr int ORBIS_AJM_RESULT_FATAL = 0x80000000 ;
24-
2513u8 GetPCMSize (AjmFormatEncoding format) {
2614 switch (format) {
2715 case AjmFormatEncoding::S16:
@@ -60,6 +48,7 @@ void AjmInstance::Reset() {
6048
6149void AjmInstance::ExecuteJob (AjmJob& job) {
6250 const auto control_flags = job.flags .control_flags ;
51+ job.output .p_result ->result = 0 ;
6352 if (True (control_flags & AjmJobControlFlags::Reset)) {
6453 LOG_TRACE (Lib_Ajm, " Resetting instance {}" , job.instance_id );
6554 Reset ();
@@ -91,8 +80,7 @@ void AjmInstance::ExecuteJob(AjmJob& job) {
9180 m_gapless.current .total_samples -= sample_difference;
9281 } else {
9382 LOG_WARNING (Lib_Ajm, " ORBIS_AJM_RESULT_INVALID_PARAMETER" );
94- job.output .p_result ->result = ORBIS_AJM_RESULT_INVALID_PARAMETER;
95- return ;
83+ job.output .p_result ->result |= ORBIS_AJM_RESULT_INVALID_PARAMETER;
9684 }
9785 }
9886
@@ -106,61 +94,59 @@ void AjmInstance::ExecuteJob(AjmJob& job) {
10694 m_gapless.current .skip_samples -= sample_difference;
10795 } else {
10896 LOG_WARNING (Lib_Ajm, " ORBIS_AJM_RESULT_INVALID_PARAMETER" );
109- job.output .p_result ->result = ORBIS_AJM_RESULT_INVALID_PARAMETER;
110- return ;
97+ job.output .p_result ->result |= ORBIS_AJM_RESULT_INVALID_PARAMETER;
11198 }
11299 }
113100 }
114101
115- if (!job.input .buffer .empty () && !job.output .buffers .empty ()) {
116- std::span<u8 > in_buf (job.input .buffer );
117- SparseOutputBuffer out_buf (job.output .buffers );
102+ std::span<u8 > in_buf (job.input .buffer );
103+ SparseOutputBuffer out_buf (job.output .buffers );
104+ auto in_size = in_buf.size ();
105+ auto out_size = out_buf.Size ();
106+ u32 frames_decoded = 0 ;
118107
119- u32 frames_decoded = 0 ;
120- auto in_size = in_buf.size ();
121- auto out_size = out_buf.Size ();
122- while (!in_buf.empty () && !out_buf.IsEmpty () && !m_gapless.IsEnd ()) {
108+ if (!job.input .buffer .empty ()) {
109+ for (;;) {
110+ if (m_flags.gapless_loop && m_gapless.IsEnd ()) {
111+ m_gapless.Reset ();
112+ m_total_samples = 0 ;
113+ }
123114 if (!HasEnoughSpace (out_buf)) {
124- if (job.output .p_mframe == nullptr || frames_decoded == 0 ) {
125- LOG_WARNING (Lib_Ajm, " ORBIS_AJM_RESULT_NOT_ENOUGH_ROOM ({} < {})" ,
126- out_buf.Size (), m_codec->GetNextFrameSize (m_gapless));
127- job.output .p_result ->result = ORBIS_AJM_RESULT_NOT_ENOUGH_ROOM;
128- break ;
129- }
115+ LOG_TRACE (Lib_Ajm, " ORBIS_AJM_RESULT_NOT_ENOUGH_ROOM ({} < {})" , out_buf.Size (),
116+ m_codec->GetNextFrameSize (m_gapless));
117+ job.output .p_result ->result |= ORBIS_AJM_RESULT_NOT_ENOUGH_ROOM;
130118 }
131-
132- const auto [nframes, nsamples, reset] =
133- m_codec->ProcessData (in_buf, out_buf, m_gapless);
134- if (reset) {
119+ if (in_buf.size () < m_codec->GetMinimumInputSize ()) {
120+ job.output .p_result ->result |= ORBIS_AJM_RESULT_PARTIAL_INPUT;
121+ }
122+ if (job.output .p_result ->result != 0 ) {
123+ break ;
124+ }
125+ const auto result = m_codec->ProcessData (in_buf, out_buf, m_gapless);
126+ if (result.is_reset ) {
135127 m_total_samples = 0 ;
128+ } else {
129+ m_total_samples += result.samples_written ;
136130 }
137- if (!nframes) {
138- LOG_WARNING (Lib_Ajm, " ORBIS_AJM_RESULT_NOT_INITIALIZED" );
139- job.output .p_result ->result = ORBIS_AJM_RESULT_NOT_INITIALIZED;
131+ frames_decoded += result.frames_decoded ;
132+ if (result.result != 0 ) {
133+ job.output .p_result ->result |= result.result ;
134+ job.output .p_result ->internal_result = result.internal_result ;
140135 break ;
141136 }
142- frames_decoded += nframes;
143- m_total_samples += nsamples;
144-
145137 if (False (job.flags .run_flags & AjmJobRunFlags::MultipleFrames)) {
146138 break ;
147139 }
148140 }
141+ }
149142
150- const auto total_decoded_samples = m_total_samples;
151- if (m_flags.gapless_loop && m_gapless.IsEnd ()) {
152- in_buf = in_buf.subspan (in_buf.size ());
153- m_gapless.Reset ();
154- m_codec->Reset ();
155- }
156- if (job.output .p_mframe ) {
157- job.output .p_mframe ->num_frames = frames_decoded;
158- }
159- if (job.output .p_stream ) {
160- job.output .p_stream ->input_consumed = in_size - in_buf.size ();
161- job.output .p_stream ->output_written = out_size - out_buf.Size ();
162- job.output .p_stream ->total_decoded_samples = total_decoded_samples;
163- }
143+ if (job.output .p_mframe ) {
144+ job.output .p_mframe ->num_frames = frames_decoded;
145+ }
146+ if (job.output .p_stream ) {
147+ job.output .p_stream ->input_consumed = in_size - in_buf.size ();
148+ job.output .p_stream ->output_written = out_size - out_buf.Size ();
149+ job.output .p_stream ->total_decoded_samples = m_total_samples;
164150 }
165151
166152 if (job.output .p_format != nullptr ) {
@@ -175,6 +161,9 @@ void AjmInstance::ExecuteJob(AjmJob& job) {
175161}
176162
177163bool AjmInstance::HasEnoughSpace (const SparseOutputBuffer& output) const {
164+ if (m_gapless.IsEnd ()) {
165+ return true ;
166+ }
178167 return output.Size () >= m_codec->GetNextFrameSize (m_gapless);
179168}
180169
0 commit comments