2013-05-06 13:09:40 +00:00
|
|
|
// Copyright (c) 2012- PPSSPP Project.
|
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, version 2.0 or later versions.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License 2.0 for more details.
|
|
|
|
|
|
|
|
// A copy of the GPL 2.0 should have been included with the program.
|
|
|
|
// If not, see http://www.gnu.org/licenses/
|
|
|
|
|
|
|
|
// Official git repository and contact information can be found at
|
|
|
|
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
|
|
|
|
|
|
|
|
#include <map>
|
2013-05-20 00:54:14 +00:00
|
|
|
#include "Core/HLE/HLE.h"
|
2013-06-22 16:08:43 +00:00
|
|
|
#include "Core/HLE/sceMp3.h"
|
2013-05-20 00:54:14 +00:00
|
|
|
#include "Core/HW/MediaEngine.h"
|
|
|
|
#include "Core/Reporting.h"
|
2013-06-22 12:17:31 +00:00
|
|
|
#include "../HW/MediaEngine.h"
|
2013-05-06 13:09:40 +00:00
|
|
|
|
2013-01-20 13:30:16 +00:00
|
|
|
#ifdef USE_FFMPEG
|
|
|
|
#ifndef PRId64
|
2013-06-23 16:01:11 +00:00
|
|
|
#define PRId64 "%llu"
|
2013-01-20 13:30:16 +00:00
|
|
|
#endif
|
2013-06-23 16:01:11 +00:00
|
|
|
|
2013-01-20 13:30:16 +00:00
|
|
|
// Urgh! Why is this needed?
|
|
|
|
#ifdef ANDROID
|
|
|
|
#ifndef UINT64_C
|
|
|
|
#define UINT64_C(c) (c ## ULL)
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
extern "C" {
|
|
|
|
#include <libavutil/opt.h>
|
|
|
|
#include <libavformat/avformat.h>
|
2013-06-23 16:01:11 +00:00
|
|
|
//#include <libavutil/timestamp.h> // iOS build is not happy with this one.
|
2013-01-20 13:30:16 +00:00
|
|
|
#include <libswresample/swresample.h>
|
|
|
|
#include <libavutil/samplefmt.h>
|
|
|
|
}
|
|
|
|
#endif
|
2013-05-06 13:09:40 +00:00
|
|
|
|
2013-01-20 13:30:16 +00:00
|
|
|
static const int MP3_BITRATES[] = {0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320};
|
2013-05-06 13:09:40 +00:00
|
|
|
|
2013-01-20 13:30:16 +00:00
|
|
|
struct Mp3Context {
|
2013-05-06 13:09:40 +00:00
|
|
|
void DoState(PointerWrap &p) {
|
|
|
|
p.Do(mp3StreamStart);
|
|
|
|
p.Do(mp3StreamEnd);
|
|
|
|
p.Do(mp3Buf);
|
|
|
|
p.Do(mp3BufSize);
|
|
|
|
p.Do(mp3PcmBuf);
|
|
|
|
p.Do(mp3PcmBufSize);
|
|
|
|
p.Do(mp3DecodedBytes);
|
|
|
|
p.Do(mp3LoopNum);
|
|
|
|
p.Do(mp3MaxSamples);
|
|
|
|
p.Do(mp3Bitrate);
|
|
|
|
p.Do(mp3Channels);
|
|
|
|
p.Do(mp3SamplingRate);
|
|
|
|
p.Do(mp3Version);
|
|
|
|
p.DoClass(mediaengine);
|
|
|
|
p.DoMarker("Mp3Context");
|
|
|
|
}
|
|
|
|
|
2013-01-20 13:30:16 +00:00
|
|
|
int mp3StreamStart;
|
|
|
|
int mp3StreamEnd;
|
2013-05-06 13:09:40 +00:00
|
|
|
u32 mp3Buf;
|
|
|
|
int mp3BufSize;
|
|
|
|
u32 mp3PcmBuf;
|
|
|
|
int mp3PcmBufSize;
|
|
|
|
|
2013-01-20 13:30:16 +00:00
|
|
|
int readPosition;
|
|
|
|
|
|
|
|
int bufferRead;
|
|
|
|
int bufferWrite;
|
|
|
|
int bufferAvailable;
|
|
|
|
|
2013-05-06 13:09:40 +00:00
|
|
|
int mp3DecodedBytes;
|
|
|
|
int mp3LoopNum;
|
|
|
|
int mp3MaxSamples;
|
2013-01-20 13:30:16 +00:00
|
|
|
MediaEngine *mediaengine;
|
2013-05-06 13:09:40 +00:00
|
|
|
|
|
|
|
int mp3Channels;
|
|
|
|
int mp3Bitrate;
|
|
|
|
int mp3SamplingRate;
|
|
|
|
int mp3Version;
|
2013-05-12 18:13:07 +00:00
|
|
|
#ifdef USE_FFMPEG
|
2013-01-20 13:30:16 +00:00
|
|
|
AVFormatContext *avformat_context;
|
|
|
|
AVIOContext *avio_context;
|
|
|
|
AVCodecContext *decoder_context;
|
|
|
|
SwrContext *resampler_context;
|
|
|
|
int audio_stream_index;
|
2013-05-12 18:13:07 +00:00
|
|
|
#endif
|
2013-05-06 13:09:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static std::map<u32, Mp3Context *> mp3Map;
|
|
|
|
static u32 lastMp3Handle = 0;
|
|
|
|
|
|
|
|
Mp3Context *getMp3Ctx(u32 mp3) {
|
|
|
|
if (mp3Map.find(mp3) == mp3Map.end()) {
|
|
|
|
ERROR_LOG(HLE, "Bad mp3 handle %08x - using last one (%08x) instead", mp3, lastMp3Handle);
|
|
|
|
mp3 = lastMp3Handle;
|
|
|
|
}
|
2013-01-20 13:30:16 +00:00
|
|
|
|
2013-05-06 13:09:40 +00:00
|
|
|
if (mp3Map.find(mp3) == mp3Map.end())
|
|
|
|
return NULL;
|
|
|
|
return mp3Map[mp3];
|
|
|
|
}
|
|
|
|
|
2013-01-20 13:30:16 +00:00
|
|
|
/* MP3 */
|
2013-05-06 13:09:40 +00:00
|
|
|
int sceMp3Decode(u32 mp3, u32 outPcmPtr) {
|
|
|
|
DEBUG_LOG(HLE, "sceMp3Decode(%08x,%08x)", mp3, outPcmPtr);
|
|
|
|
|
|
|
|
Mp3Context *ctx = getMp3Ctx(mp3);
|
|
|
|
if (!ctx) {
|
|
|
|
ERROR_LOG(HLE, "%s: bad mp3 handle %08x", __FUNCTION__, mp3);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nothing to decode
|
2013-01-20 13:30:16 +00:00
|
|
|
if (ctx->bufferAvailable == 0 || ctx->readPosition >= ctx->mp3StreamEnd) {
|
|
|
|
return 0;
|
2013-05-06 13:09:40 +00:00
|
|
|
}
|
2013-01-20 13:30:16 +00:00
|
|
|
int bytesdecoded = 0;
|
2013-05-06 13:09:40 +00:00
|
|
|
|
2013-01-20 13:30:16 +00:00
|
|
|
#ifndef USE_FFMPEG
|
2013-05-06 13:09:40 +00:00
|
|
|
Memory::Memset(ctx->mp3PcmBuf, 0, ctx->mp3PcmBufSize);
|
|
|
|
Memory::Write_U32(ctx->mp3PcmBuf, outPcmPtr);
|
2013-01-20 13:30:16 +00:00
|
|
|
#else
|
|
|
|
|
2013-06-23 16:01:11 +00:00
|
|
|
AVFrame frame;
|
|
|
|
memset(&frame, 0, sizeof(frame));
|
|
|
|
AVPacket packet;
|
|
|
|
memset(&packet, 0, sizeof(packet));
|
2013-06-22 12:17:31 +00:00
|
|
|
int got_frame = 0, ret;
|
2013-01-20 13:30:16 +00:00
|
|
|
static int audio_frame_count = 0;
|
|
|
|
|
2013-06-22 12:17:31 +00:00
|
|
|
while (!got_frame) {
|
2013-01-20 13:30:16 +00:00
|
|
|
if ((ret = av_read_frame(ctx->avformat_context, &packet)) < 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (packet.stream_index == ctx->audio_stream_index) {
|
|
|
|
avcodec_get_frame_defaults(&frame);
|
|
|
|
got_frame = 0;
|
|
|
|
ret = avcodec_decode_audio4(ctx->decoder_context, &frame, &got_frame, &packet);
|
|
|
|
if (ret < 0) {
|
|
|
|
ERROR_LOG(HLE, "avcodec_decode_audio4: Error decoding audio %d", ret);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (got_frame) {
|
2013-06-23 16:01:11 +00:00
|
|
|
//char buf[1024] = "";
|
|
|
|
//av_ts_make_time_string(buf, frame.pts, &ctx->decoder_context->time_base);
|
|
|
|
//DEBUG_LOG(HLE, "audio_frame n:%d nb_samples:%d pts:%s", audio_frame_count++, frame.nb_samples, buf);
|
2013-01-20 13:30:16 +00:00
|
|
|
|
2013-05-12 17:48:12 +00:00
|
|
|
/*
|
2013-01-20 13:30:16 +00:00
|
|
|
u8 *audio_dst_data;
|
|
|
|
int audio_dst_linesize;
|
|
|
|
|
|
|
|
ret = av_samples_alloc(&audio_dst_data, &audio_dst_linesize, frame.channels, frame.nb_samples, (AVSampleFormat)frame.format, 1);
|
|
|
|
if (ret < 0) {
|
|
|
|
ERROR_LOG(HLE, "av_samples_alloc: Could not allocate audio buffer %d", ret);
|
|
|
|
return -1;
|
|
|
|
}
|
2013-05-12 17:48:12 +00:00
|
|
|
*/
|
2013-01-20 13:30:16 +00:00
|
|
|
|
|
|
|
int decoded = av_samples_get_buffer_size(NULL, frame.channels, frame.nb_samples, (AVSampleFormat)frame.format, 1);
|
|
|
|
|
|
|
|
u8* out = Memory::GetPointer(ctx->mp3PcmBuf + bytesdecoded);
|
|
|
|
ret = swr_convert(ctx->resampler_context, &out, frame.nb_samples, (const u8**)frame.extended_data, frame.nb_samples);
|
|
|
|
if (ret < 0) {
|
|
|
|
ERROR_LOG(HLE, "swr_convert: Error while converting %d", ret);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
//av_samples_copy(&audio_dst_data, frame.data, 0, 0, frame.nb_samples, frame.channels, (AVSampleFormat)frame.format);
|
|
|
|
|
|
|
|
//memcpy(Memory::GetPointer(ctx->mp3PcmBuf + bytesdecoded), audio_dst_data, decoded);
|
|
|
|
bytesdecoded += decoded;
|
2013-05-12 17:48:12 +00:00
|
|
|
// av_freep(&audio_dst_data[0]);
|
2013-01-20 13:30:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
av_free_packet(&packet);
|
|
|
|
}
|
|
|
|
Memory::Write_U32(ctx->mp3PcmBuf, outPcmPtr);
|
|
|
|
#endif
|
2013-05-06 13:09:40 +00:00
|
|
|
|
2013-01-20 13:30:16 +00:00
|
|
|
#if 0 && defined(_DEBUG)
|
2013-05-06 13:09:40 +00:00
|
|
|
char fileName[256];
|
2013-01-20 13:30:16 +00:00
|
|
|
sprintf(fileName, "out.wav", mp3);
|
2013-05-06 13:09:40 +00:00
|
|
|
|
2013-01-20 13:30:16 +00:00
|
|
|
FILE * file = fopen(fileName, "a+b");
|
|
|
|
if (file) {
|
|
|
|
if (!Memory::IsValidAddress(ctx->mp3Buf)) {
|
2013-05-06 13:09:40 +00:00
|
|
|
ERROR_LOG(HLE, "sceMp3Decode mp3Buf %08X is not a valid address!", ctx->mp3Buf);
|
|
|
|
}
|
|
|
|
|
2013-01-20 13:30:16 +00:00
|
|
|
//u8 * ptr = Memory::GetPointer(ctx->mp3Buf);
|
|
|
|
fwrite(Memory::GetPointer(ctx->mp3PcmBuf), 1, bytesdecoded, file);
|
|
|
|
|
2013-05-06 13:09:40 +00:00
|
|
|
fclose(file);
|
|
|
|
}
|
2013-01-20 13:30:16 +00:00
|
|
|
#endif
|
2013-05-06 13:09:40 +00:00
|
|
|
|
2013-01-20 13:30:16 +00:00
|
|
|
return bytesdecoded;
|
2013-05-06 13:09:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int sceMp3ResetPlayPosition(u32 mp3) {
|
|
|
|
DEBUG_LOG(HLE, "SceMp3ResetPlayPosition(%08x)", mp3);
|
|
|
|
|
|
|
|
Mp3Context *ctx = getMp3Ctx(mp3);
|
|
|
|
if (!ctx) {
|
|
|
|
ERROR_LOG(HLE, "%s: bad mp3 handle %08x", __FUNCTION__, mp3);
|
|
|
|
return -1;
|
|
|
|
}
|
2013-01-20 13:30:16 +00:00
|
|
|
|
|
|
|
ctx->readPosition = ctx->mp3StreamStart;
|
2013-05-06 13:09:40 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int sceMp3CheckStreamDataNeeded(u32 mp3) {
|
|
|
|
DEBUG_LOG(HLE, "sceMp3CheckStreamDataNeeded(%08x)", mp3);
|
|
|
|
|
|
|
|
Mp3Context *ctx = getMp3Ctx(mp3);
|
|
|
|
if (!ctx) {
|
|
|
|
ERROR_LOG(HLE, "%s: bad mp3 handle %08x", __FUNCTION__, mp3);
|
|
|
|
return -1;
|
|
|
|
}
|
2013-01-20 13:30:16 +00:00
|
|
|
|
|
|
|
return ctx->bufferAvailable != ctx->mp3BufSize && ctx->readPosition < ctx->mp3StreamEnd;
|
|
|
|
}
|
|
|
|
|
|
|
|
int readFunc(void *opaque, uint8_t *buf, int buf_size) {
|
|
|
|
Mp3Context *ctx = static_cast<Mp3Context*>(opaque);
|
|
|
|
|
|
|
|
int res = 0;
|
|
|
|
while (ctx->bufferAvailable && buf_size) {
|
|
|
|
// Maximum bytes we can read
|
|
|
|
int to_read = std::min(ctx->bufferAvailable, buf_size);
|
|
|
|
|
|
|
|
// Don't read past the end if the buffer loops
|
|
|
|
to_read = std::min(ctx->mp3BufSize - ctx->bufferRead, to_read);
|
|
|
|
memcpy(buf + res, Memory::GetCharPointer(ctx->mp3Buf + ctx->bufferRead), to_read);
|
|
|
|
|
|
|
|
ctx->bufferRead += to_read;
|
|
|
|
if (ctx->bufferRead == ctx->mp3BufSize)
|
|
|
|
ctx->bufferRead = 0;
|
|
|
|
ctx->bufferAvailable -= to_read;
|
2013-05-12 18:41:31 +00:00
|
|
|
buf_size -= to_read;
|
2013-01-20 13:30:16 +00:00
|
|
|
res += to_read;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ctx->bufferAvailable == 0) {
|
|
|
|
ctx->bufferRead = 0;
|
|
|
|
ctx->bufferWrite = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0 && defined(_DEBUG)
|
|
|
|
char fileName[256];
|
|
|
|
sprintf(fileName, "out.mp3");
|
|
|
|
|
|
|
|
FILE * file = fopen(fileName, "a+b");
|
|
|
|
if (file) {
|
|
|
|
if (!Memory::IsValidAddress(ctx->mp3Buf)) {
|
|
|
|
ERROR_LOG(HLE, "sceMp3Decode mp3Buf %08X is not a valid address!", ctx->mp3Buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
fwrite(buf, 1, res, file);
|
|
|
|
|
|
|
|
fclose(file);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return res;
|
2013-05-06 13:09:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
u32 sceMp3ReserveMp3Handle(u32 mp3Addr) {
|
|
|
|
DEBUG_LOG(HLE, "sceMp3ReserveMp3Handle(%08x)", mp3Addr);
|
|
|
|
Mp3Context *ctx = new Mp3Context;
|
|
|
|
|
|
|
|
memset(ctx, 0, sizeof(Mp3Context));
|
|
|
|
|
|
|
|
ctx->mp3StreamStart = Memory::Read_U64(mp3Addr);
|
|
|
|
ctx->mp3StreamEnd = Memory::Read_U64(mp3Addr+8);
|
|
|
|
ctx->mp3Buf = Memory::Read_U32(mp3Addr+16);
|
|
|
|
ctx->mp3BufSize = Memory::Read_U32(mp3Addr+20);
|
|
|
|
ctx->mp3PcmBuf = Memory::Read_U32(mp3Addr+24);
|
|
|
|
ctx->mp3PcmBufSize = Memory::Read_U32(mp3Addr+28);
|
|
|
|
|
2013-01-20 13:30:16 +00:00
|
|
|
ctx->readPosition = ctx->mp3StreamStart;
|
2013-05-06 13:09:40 +00:00
|
|
|
ctx->mp3MaxSamples = ctx->mp3PcmBufSize / 4 ;
|
|
|
|
|
2013-05-12 20:29:43 +00:00
|
|
|
ctx->mp3Channels = 2;
|
2013-05-06 13:09:40 +00:00
|
|
|
ctx->mp3Bitrate = 128;
|
2013-05-12 20:29:43 +00:00
|
|
|
ctx->mp3SamplingRate = 44100;
|
2013-05-06 13:09:40 +00:00
|
|
|
|
2013-05-12 18:13:07 +00:00
|
|
|
#ifdef USE_FFMPEG
|
2013-01-20 13:30:16 +00:00
|
|
|
ctx->avformat_context = NULL;
|
|
|
|
ctx->avio_context = NULL;
|
2013-05-12 18:13:07 +00:00
|
|
|
#endif
|
2013-01-20 13:30:16 +00:00
|
|
|
|
2013-05-06 13:09:40 +00:00
|
|
|
mp3Map[mp3Addr] = ctx;
|
|
|
|
return mp3Addr;
|
|
|
|
}
|
|
|
|
|
|
|
|
int sceMp3InitResource() {
|
2013-06-06 00:50:04 +00:00
|
|
|
WARN_LOG(HLE, "UNIMPL: sceMp3InitResource");
|
2013-05-06 13:09:40 +00:00
|
|
|
// Do nothing here
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int sceMp3TermResource() {
|
2013-06-06 00:50:04 +00:00
|
|
|
WARN_LOG(HLE, "UNIMPL: sceMp3TermResource");
|
2013-05-06 13:09:40 +00:00
|
|
|
// Do nothing here
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int sceMp3Init(u32 mp3) {
|
|
|
|
DEBUG_LOG(HLE, "sceMp3Init(%08x)", mp3);
|
|
|
|
|
|
|
|
Mp3Context *ctx = getMp3Ctx(mp3);
|
|
|
|
if (!ctx) {
|
|
|
|
ERROR_LOG(HLE, "%s: bad mp3 handle %08x", __FUNCTION__, mp3);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read in the header and swap the endian
|
|
|
|
int header = Memory::Read_U32(ctx->mp3Buf);
|
|
|
|
header = (header >> 24) |
|
2013-01-20 13:30:16 +00:00
|
|
|
((header<<8) & 0x00FF0000) |
|
|
|
|
((header>>8) & 0x0000FF00) |
|
|
|
|
(header << 24);
|
2013-05-06 13:09:40 +00:00
|
|
|
|
|
|
|
ctx->mp3Version = ((header >> 19) & 0x3);
|
2013-01-20 13:30:16 +00:00
|
|
|
|
2013-05-12 18:13:07 +00:00
|
|
|
#ifdef USE_FFMPEG
|
2013-01-20 13:30:16 +00:00
|
|
|
u8* avio_buffer = static_cast<u8*>(av_malloc(ctx->mp3BufSize));
|
|
|
|
ctx->avio_context = avio_alloc_context(avio_buffer, ctx->mp3BufSize, 0, ctx, readFunc, NULL, NULL);
|
|
|
|
ctx->avformat_context = avformat_alloc_context();
|
|
|
|
ctx->avformat_context->pb = ctx->avio_context;
|
|
|
|
|
|
|
|
int ret;
|
|
|
|
if ((ret = avformat_open_input(&ctx->avformat_context, NULL, av_find_input_format("mp3"), NULL)) < 0) {
|
|
|
|
ERROR_LOG(HLE, "avformat_open_input: Cannot open input %d", ret);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((ret = avformat_find_stream_info(ctx->avformat_context, NULL)) < 0) {
|
|
|
|
ERROR_LOG(HLE, "avformat_find_stream_info: Cannot find stream information %d", ret);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
AVCodec *dec;
|
|
|
|
|
|
|
|
/* select the audio stream */
|
|
|
|
ret = av_find_best_stream(ctx->avformat_context, AVMEDIA_TYPE_AUDIO, -1, -1, &dec, 0);
|
|
|
|
if (ret < 0) {
|
|
|
|
ERROR_LOG(HLE, "av_find_best_stream: Cannot find a audio stream in the input file %d", ret);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
ctx->audio_stream_index = ret;
|
|
|
|
ctx->decoder_context = ctx->avformat_context->streams[ctx->audio_stream_index]->codec;
|
|
|
|
|
|
|
|
/* init the audio decoder */
|
|
|
|
if ((ret = avcodec_open2(ctx->decoder_context, dec, NULL)) < 0) {
|
|
|
|
ERROR_LOG(HLE, "avcodec_open2: Cannot open audio decoder %d", ret);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx->resampler_context = swr_alloc_set_opts(NULL,
|
|
|
|
ctx->decoder_context->channel_layout,
|
|
|
|
AV_SAMPLE_FMT_S16,
|
|
|
|
ctx->decoder_context->sample_rate,
|
|
|
|
ctx->decoder_context->channel_layout,
|
|
|
|
ctx->decoder_context->sample_fmt,
|
|
|
|
ctx->decoder_context->sample_rate,
|
|
|
|
0, NULL);
|
2013-05-12 20:29:43 +00:00
|
|
|
|
|
|
|
// Let's just grab this info from FFMPEG, it seems more reliable than the code above.
|
|
|
|
|
|
|
|
ctx->mp3SamplingRate = ctx->decoder_context->sample_rate;
|
|
|
|
ctx->mp3Channels = ctx->decoder_context->channels;
|
|
|
|
ctx->mp3Bitrate = ctx->decoder_context->bit_rate;
|
|
|
|
|
2013-01-20 13:30:16 +00:00
|
|
|
if (!ctx->resampler_context) {
|
|
|
|
ERROR_LOG(HLE, "Could not allocate resampler context %d", ret);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((ret = swr_init(ctx->resampler_context)) < 0) {
|
|
|
|
ERROR_LOG(HLE, "Failed to initialize the resampling context %d", ret);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
av_dump_format(ctx->avformat_context, 0, "mp3", 0);
|
2013-05-12 18:13:07 +00:00
|
|
|
#endif
|
|
|
|
|
2013-05-06 13:09:40 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int sceMp3GetLoopNum(u32 mp3) {
|
|
|
|
DEBUG_LOG(HLE, "sceMp3GetLoopNum(%08x)", mp3);
|
|
|
|
Mp3Context *ctx = getMp3Ctx(mp3);
|
|
|
|
if (!ctx) {
|
|
|
|
ERROR_LOG(HLE, "%s: bad mp3 handle %08x", __FUNCTION__, mp3);
|
|
|
|
return -1;
|
|
|
|
}
|
2013-01-20 13:30:16 +00:00
|
|
|
|
2013-05-06 13:09:40 +00:00
|
|
|
return ctx->mp3LoopNum;
|
|
|
|
}
|
|
|
|
|
2013-01-20 13:30:16 +00:00
|
|
|
int sceMp3GetMaxOutputSample(u32 mp3)
|
|
|
|
{
|
2013-05-06 13:09:40 +00:00
|
|
|
DEBUG_LOG(HLE, "sceMp3GetMaxOutputSample(%08x)", mp3);
|
|
|
|
Mp3Context *ctx = getMp3Ctx(mp3);
|
|
|
|
if (!ctx) {
|
|
|
|
ERROR_LOG(HLE, "%s: bad mp3 handle %08x", __FUNCTION__, mp3);
|
|
|
|
return -1;
|
|
|
|
}
|
2013-01-20 13:30:16 +00:00
|
|
|
|
2013-05-06 13:09:40 +00:00
|
|
|
return ctx->mp3MaxSamples;
|
|
|
|
}
|
|
|
|
|
|
|
|
int sceMp3GetSumDecodedSample(u32 mp3) {
|
2013-05-20 00:54:14 +00:00
|
|
|
ERROR_LOG_REPORT(HLE, "UNIMPL sceMp3GetSumDecodedSample(%08X)", mp3);
|
2013-05-06 13:09:40 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int sceMp3SetLoopNum(u32 mp3, int loop) {
|
2013-06-22 12:17:31 +00:00
|
|
|
INFO_LOG(HLE, "sceMp3SetLoopNum(%08X, %i)", mp3, loop);
|
2013-05-06 13:09:40 +00:00
|
|
|
|
|
|
|
Mp3Context *ctx = getMp3Ctx(mp3);
|
|
|
|
if (!ctx) {
|
|
|
|
ERROR_LOG(HLE, "%s: bad mp3 handle %08x", __FUNCTION__, mp3);
|
|
|
|
return -1;
|
|
|
|
}
|
2013-01-20 13:30:16 +00:00
|
|
|
|
2013-05-06 13:09:40 +00:00
|
|
|
ctx->mp3LoopNum = loop;
|
2013-01-20 13:30:16 +00:00
|
|
|
|
2013-05-06 13:09:40 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
int sceMp3GetMp3ChannelNum(u32 mp3) {
|
|
|
|
DEBUG_LOG(HLE, "sceMp3GetMp3ChannelNum(%08X)", mp3);
|
|
|
|
|
|
|
|
Mp3Context *ctx = getMp3Ctx(mp3);
|
|
|
|
if (!ctx) {
|
|
|
|
ERROR_LOG(HLE, "%s: bad mp3 handle %08x", __FUNCTION__, mp3);
|
|
|
|
return -1;
|
|
|
|
}
|
2013-01-20 13:30:16 +00:00
|
|
|
|
2013-05-06 13:09:40 +00:00
|
|
|
return ctx->mp3Channels;
|
|
|
|
}
|
|
|
|
int sceMp3GetBitRate(u32 mp3) {
|
|
|
|
DEBUG_LOG(HLE, "sceMp3GetBitRate(%08X)", mp3);
|
|
|
|
|
|
|
|
Mp3Context *ctx = getMp3Ctx(mp3);
|
|
|
|
if (!ctx) {
|
|
|
|
ERROR_LOG(HLE, "%s: bad mp3 handle %08x", __FUNCTION__, mp3);
|
|
|
|
return -1;
|
|
|
|
}
|
2013-01-20 13:30:16 +00:00
|
|
|
|
2013-05-06 13:09:40 +00:00
|
|
|
return ctx->mp3Bitrate;
|
|
|
|
}
|
|
|
|
int sceMp3GetSamplingRate(u32 mp3) {
|
|
|
|
DEBUG_LOG(HLE, "sceMp3GetSamplingRate(%08X)", mp3);
|
|
|
|
|
|
|
|
Mp3Context *ctx = getMp3Ctx(mp3);
|
|
|
|
if (!ctx) {
|
|
|
|
ERROR_LOG(HLE, "%s: bad mp3 handle %08x", __FUNCTION__, mp3);
|
|
|
|
return -1;
|
|
|
|
}
|
2013-01-20 13:30:16 +00:00
|
|
|
|
2013-05-06 13:09:40 +00:00
|
|
|
return ctx->mp3SamplingRate;
|
|
|
|
}
|
2013-05-12 20:29:43 +00:00
|
|
|
|
2013-05-06 13:09:40 +00:00
|
|
|
int sceMp3GetInfoToAddStreamData(u32 mp3, u32 dstPtr, u32 towritePtr, u32 srcposPtr) {
|
2013-06-22 12:17:31 +00:00
|
|
|
INFO_LOG(HLE, "sceMp3GetInfoToAddStreamData(%08X, %08X, %08X, %08X)", mp3, dstPtr, towritePtr, srcposPtr);
|
2013-01-20 13:30:16 +00:00
|
|
|
|
2013-05-06 13:09:40 +00:00
|
|
|
Mp3Context *ctx = getMp3Ctx(mp3);
|
|
|
|
if (!ctx) {
|
|
|
|
ERROR_LOG(HLE, "%s: bad mp3 handle %08x", __FUNCTION__, mp3);
|
|
|
|
return -1;
|
|
|
|
}
|
2013-01-20 13:30:16 +00:00
|
|
|
|
|
|
|
u32 buf, max_write;
|
|
|
|
if (ctx->readPosition < ctx->mp3StreamEnd) {
|
2013-06-22 12:17:31 +00:00
|
|
|
buf = ctx->mp3Buf + ctx->bufferWrite;
|
2013-01-20 13:30:16 +00:00
|
|
|
max_write = std::min(ctx->mp3BufSize - ctx->bufferWrite, ctx->mp3BufSize - ctx->bufferAvailable);
|
|
|
|
} else {
|
|
|
|
buf = 0;
|
|
|
|
max_write = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Memory::IsValidAddress(dstPtr))
|
|
|
|
Memory::Write_U32(buf, dstPtr);
|
|
|
|
if (Memory::IsValidAddress(towritePtr))
|
|
|
|
Memory::Write_U32(max_write, towritePtr);
|
|
|
|
if (Memory::IsValidAddress(srcposPtr))
|
|
|
|
Memory::Write_U32(ctx->readPosition, srcposPtr);
|
|
|
|
|
2013-05-06 13:09:40 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2013-05-12 20:29:43 +00:00
|
|
|
|
|
|
|
int sceMp3NotifyAddStreamData(u32 mp3, int size) {
|
2013-06-22 12:17:31 +00:00
|
|
|
INFO_LOG(HLE, "sceMp3NotifyAddStreamData(%08X, %i)", mp3, size);
|
2013-05-12 20:29:43 +00:00
|
|
|
|
|
|
|
Mp3Context *ctx = getMp3Ctx(mp3);
|
|
|
|
if (!ctx) {
|
|
|
|
ERROR_LOG(HLE, "%s: bad mp3 handle %08x", __FUNCTION__, mp3);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx->readPosition += size;
|
|
|
|
ctx->bufferAvailable += size;
|
|
|
|
ctx->bufferWrite += size;
|
|
|
|
|
|
|
|
if (ctx->bufferWrite == ctx->mp3BufSize)
|
|
|
|
ctx->bufferWrite = 0;
|
|
|
|
|
2013-06-22 12:17:31 +00:00
|
|
|
if (ctx->readPosition >= ctx->mp3StreamEnd && ctx->mp3LoopNum != 0) {
|
|
|
|
ctx->readPosition = ctx->mp3StreamStart;
|
|
|
|
if (ctx->mp3LoopNum > 0)
|
|
|
|
ctx->mp3LoopNum--;
|
|
|
|
}
|
2013-05-12 20:29:43 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-05-06 13:09:40 +00:00
|
|
|
int sceMp3ReleaseMp3Handle(u32 mp3) {
|
|
|
|
DEBUG_LOG(HLE, "sceMp3ReleaseMp3Handle(%08X)", mp3);
|
|
|
|
|
|
|
|
Mp3Context *ctx = getMp3Ctx(mp3);
|
|
|
|
if (!ctx) {
|
|
|
|
ERROR_LOG(HLE, "%s: bad mp3 handle %08x", __FUNCTION__, mp3);
|
|
|
|
return -1;
|
|
|
|
}
|
2013-01-20 13:30:16 +00:00
|
|
|
|
2013-05-12 18:13:07 +00:00
|
|
|
#ifdef USE_FFMPEG
|
2013-01-20 13:30:16 +00:00
|
|
|
av_free(ctx->avio_context->buffer);
|
|
|
|
av_free(ctx->avio_context);
|
2013-05-12 18:13:07 +00:00
|
|
|
#endif
|
2013-05-06 13:09:40 +00:00
|
|
|
mp3Map.erase(mp3Map.find(mp3));
|
2013-01-20 13:30:16 +00:00
|
|
|
|
2013-05-06 13:09:40 +00:00
|
|
|
delete ctx;
|
2013-01-20 13:30:16 +00:00
|
|
|
|
2013-05-06 13:09:40 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 sceMp3EndEntry() {
|
2013-05-20 00:54:14 +00:00
|
|
|
ERROR_LOG_REPORT(HLE, "UNIMPL sceMp3EndEntry(...)");
|
2013-05-06 13:09:40 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 sceMp3StartEntry() {
|
2013-05-20 00:54:14 +00:00
|
|
|
ERROR_LOG_REPORT(HLE, "UNIMPL sceMp3StartEntry(...)");
|
2013-05-06 13:09:40 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 sceMp3GetFrameNum(u32 mp3) {
|
2013-05-20 00:54:14 +00:00
|
|
|
ERROR_LOG_REPORT(HLE, "UNIMPL sceMp3GetFrameNum(%08x)", mp3);
|
2013-05-06 13:09:40 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 sceMp3GetVersion(u32 mp3) {
|
|
|
|
DEBUG_LOG(HLE, "sceMp3GetVersion(%08x)", mp3);
|
|
|
|
Mp3Context *ctx = getMp3Ctx(mp3);
|
|
|
|
if (!ctx) {
|
|
|
|
ERROR_LOG(HLE, "%s: bad mp3 handle %08x", __FUNCTION__, mp3);
|
|
|
|
return -1;
|
|
|
|
}
|
2013-01-20 13:30:16 +00:00
|
|
|
|
2013-05-06 13:09:40 +00:00
|
|
|
return ctx->mp3Version;
|
|
|
|
}
|
|
|
|
|
2013-01-20 13:30:16 +00:00
|
|
|
const HLEFunction sceMp3[] = {
|
2013-05-06 13:09:40 +00:00
|
|
|
{0x07EC321A,WrapU_U<sceMp3ReserveMp3Handle>,"sceMp3ReserveMp3Handle"},
|
|
|
|
{0x0DB149F4,WrapI_UI<sceMp3NotifyAddStreamData>,"sceMp3NotifyAddStreamData"},
|
|
|
|
{0x2A368661,WrapI_U<sceMp3ResetPlayPosition>,"sceMp3ResetPlayPosition"},
|
|
|
|
{0x354D27EA,WrapI_U<sceMp3GetSumDecodedSample>,"sceMp3GetSumDecodedSample"},
|
|
|
|
{0x35750070,WrapI_V<sceMp3InitResource>,"sceMp3InitResource"},
|
|
|
|
{0x3C2FA058,WrapI_V<sceMp3TermResource>,"sceMp3TermResource"},
|
|
|
|
{0x3CEF484F,WrapI_UI<sceMp3SetLoopNum>,"sceMp3SetLoopNum"},
|
|
|
|
{0x44E07129,WrapI_U<sceMp3Init>,"sceMp3Init"},
|
|
|
|
{0x732B042A,WrapU_V<sceMp3EndEntry>,"sceMp3EndEntry"},
|
|
|
|
{0x7F696782,WrapI_U<sceMp3GetMp3ChannelNum>,"sceMp3GetMp3ChannelNum"},
|
|
|
|
{0x87677E40,WrapI_U<sceMp3GetBitRate>,"sceMp3GetBitRate"},
|
|
|
|
{0x87C263D1,WrapI_U<sceMp3GetMaxOutputSample>,"sceMp3GetMaxOutputSample"},
|
|
|
|
{0x8AB81558,WrapU_V<sceMp3StartEntry>,"sceMp3StartEntry"},
|
|
|
|
{0x8F450998,WrapI_U<sceMp3GetSamplingRate>,"sceMp3GetSamplingRate"},
|
|
|
|
{0xA703FE0F,WrapI_UUUU<sceMp3GetInfoToAddStreamData>,"sceMp3GetInfoToAddStreamData"},
|
|
|
|
{0xD021C0FB,WrapI_UU<sceMp3Decode>,"sceMp3Decode"},
|
|
|
|
{0xD0A56296,WrapI_U<sceMp3CheckStreamDataNeeded>,"sceMp3CheckStreamDataNeeded"},
|
|
|
|
{0xD8F54A51,WrapI_U<sceMp3GetLoopNum>,"sceMp3GetLoopNum"},
|
|
|
|
{0xF5478233,WrapI_U<sceMp3ReleaseMp3Handle>,"sceMp3ReleaseMp3Handle"},
|
2013-05-20 02:21:59 +00:00
|
|
|
{0xAE6D2027,WrapU_U<sceMp3GetVersion>,"sceMp3GetVersion"}, // Name is wrong.
|
2013-05-06 13:09:40 +00:00
|
|
|
{0x3548AEC8,WrapU_U<sceMp3GetFrameNum>,"sceMp3GetFrameNum"},
|
2013-05-22 08:19:25 +00:00
|
|
|
{0x0840e808,0,"sceMp3_0840E808"},
|
|
|
|
{0x1b839b83,0,"sceMp3_1B839B83"},
|
|
|
|
{0xe3ee2c81,0,"sceMp3_E3EE2C81"},
|
2013-05-06 13:09:40 +00:00
|
|
|
};
|
|
|
|
|
2013-01-20 13:30:16 +00:00
|
|
|
void Register_sceMp3() {
|
2013-05-06 13:09:40 +00:00
|
|
|
RegisterModule("sceMp3", ARRAY_SIZE(sceMp3), sceMp3);
|
2013-05-12 18:13:07 +00:00
|
|
|
}
|