@@ -18,19 +18,13 @@ class Atrac3Audio : public AudioDecoder {
18
18
Atrac3Audio (PSPAudioType audioType, int channels, size_t blockAlign, const uint8_t *extraData, size_t extraDataSize)
19
19
: audioType_(audioType), channels_(channels) {
20
20
blockAlign_ = (int )blockAlign;
21
- if (audioType == PSP_CODEC_AT3PLUS) {
22
- at3pCtx_ = atrac3p_alloc (channels, &blockAlign_);
23
- if (at3pCtx_) {
24
- codecOpen_ = true ;
25
- } else {
26
- ERROR_LOG (Log::ME, " Failed to open atrac3+ context! (channels=%d blockAlign=%d ed=%d)" , channels, (int )blockAlign, (int )extraDataSize);
27
- }
28
- } else if (audioType_ == PSP_CODEC_AT3) {
21
+ if (audioType_ == PSP_CODEC_AT3) {
29
22
at3Ctx_ = atrac3_alloc (channels, &blockAlign_, extraData, (int )extraDataSize);
30
23
if (at3Ctx_) {
31
24
codecOpen_ = true ;
32
25
} else {
33
26
ERROR_LOG (Log::ME, " Failed to open atrac3 context! !channels=%d blockAlign=%d ed=%d)" , channels, (int )blockAlign, (int )extraDataSize);
27
+ codecFailed_ = true ;
34
28
}
35
29
}
36
30
for (int i = 0 ; i < 2 ; i++) {
@@ -63,17 +57,34 @@ class Atrac3Audio : public AudioDecoder {
63
57
}
64
58
65
59
bool Decode (const uint8_t *inbuf, int inbytes, int *inbytesConsumed, int outputChannels, int16_t *outbuf, int *outSamples) override {
60
+ if (outSamples)
61
+ *outSamples = 0 ;
62
+ if (inbytesConsumed)
63
+ *inbytesConsumed = 0 ;
66
64
if (!codecOpen_) {
67
- WARN_LOG_N_TIMES (codecNotOpen, 5 , Log::ME, " Atrac3Audio:Decode: Codec not open, not decoding" );
68
- if (outSamples)
69
- *outSamples = 0 ;
70
- if (inbytesConsumed)
71
- *inbytesConsumed = 0 ;
72
- return false ;
65
+ // We delay the codecOpen until the first decode, so the setChannels call from MediaEngine::getAudioSamples
66
+ // can take effect. Note, we don't do this with Atrac3, just Atrac3+.
67
+ if (codecFailed_) {
68
+ return false ;
69
+ }
70
+ if (audioType_ == PSP_CODEC_AT3PLUS) {
71
+ at3pCtx_ = atrac3p_alloc (channels_, &blockAlign_);
72
+ if (at3pCtx_) {
73
+ codecOpen_ = true ;
74
+ } else {
75
+ ERROR_LOG (Log::ME, " Failed to open atrac3+ context! (channels=%d blockAlign=%d)" , channels_, (int )blockAlign_);
76
+ codecFailed_ = true ;
77
+ }
78
+ }
79
+ if (!codecOpen_) {
80
+ WARN_LOG_N_TIMES (codecNotOpen, 5 , Log::ME, " Atrac3Audio:Decode: Codec not open, not decoding" );
81
+ return false ;
82
+ }
73
83
}
74
84
if (inbytes != blockAlign_ && blockAlign_ != 0 ) {
75
85
WARN_LOG (Log::ME, " Atrac3Audio::Decode: Unexpected block align %d (expected %d). %s" , inbytes, blockAlign_, at3pCtx_ ? " Atrac3+" : " Atrac3" );
76
86
}
87
+
77
88
blockAlign_ = inbytes;
78
89
// We just call the decode function directly without going through the whole packet machinery.
79
90
int result;
@@ -125,7 +136,16 @@ class Atrac3Audio : public AudioDecoder {
125
136
}
126
137
127
138
void SetChannels (int channels) override {
128
- // Hmm. ignore for now.
139
+ _dbg_assert_ (audioType_ == PSPAudioType::PSP_CODEC_AT3PLUS);
140
+ if (audioType_ == PSPAudioType::PSP_CODEC_AT3PLUS) {
141
+ if (!at3pCtx_) {
142
+ // Codec shouldn't be open yet.
143
+ _dbg_assert_ (!codecOpen_);
144
+ channels_ = 1 ;
145
+ } else {
146
+ // This will come every packet, just ignore it.
147
+ }
148
+ }
129
149
}
130
150
131
151
PSPAudioType GetAudioType () const override { return audioType_; }
@@ -141,6 +161,7 @@ class Atrac3Audio : public AudioDecoder {
141
161
float *buffers_[2 ]{};
142
162
143
163
bool codecOpen_ = false ;
164
+ bool codecFailed_ = false ;
144
165
145
166
PSPAudioType audioType_;
146
167
};
0 commit comments