LibADX - A Dreamcast K:OS ADX Decoding Library

If you have any questions on programming, this is the place to ask them, whether you're a newbie or an experienced programmer. Discussion on programming in general is also welcome. We will help you with programming homework, but we will not do your work for you! Any porting requests must be made in Developmental Ideas.
User avatar
PH3NOM
DC Developer
DC Developer
Posts: 576
https://www.artistsworkshop.eu/meble-kuchenne-na-wymiar-warszawa-gdzie-zamowic/
Joined: Fri Jun 18, 2010 9:29 pm
Has thanked: 0
Been thanked: 5 times

LibADX - A Dreamcast K:OS ADX Decoding Library

Post by PH3NOM »

LibADX - A Dreamcast K:OS ADX Decoding Library
LibADX (c)2012 Josh PH3NOM Pearson
decoder algorithm based on adx2wav (c) BERO 2001

LibADX is a library for decoding ADX audio files using the
Kallisti:OS development environment, intended for use only
on the Sega Dreamcast game console.

LibADX features full implementation of the ADX looping function.
The functions available include play, pause, stop, restart.

This library is completely free to modify and or redistribute.

LibADX is a static library, build the library with the makefile:
LibADX/LibADX/Makefile

Then build the example player:
LibADX/Makefile

The example player uses a hard-coded file name, so make sure
to include a "sample.adx" on the root of the /cd/, or modify
the source libADXplay.c to load a different file.
Attachments
LibADX.tar.gz
LibADX v.0.1 - A Dreamcast ADX Library for KOS
(6.64 KiB) Downloaded 165 times
Last edited by PH3NOM on Mon Feb 13, 2012 11:09 pm, edited 1 time in total.
User avatar
BlueCrab
The Crabby Overlord
The Crabby Overlord
Posts: 5658
Joined: Mon May 27, 2002 11:31 am
Location: Sailing the Skies of Arcadia
Has thanked: 9 times
Been thanked: 69 times
Contact:

Re: LibADX - A Dreamcast K:OS ADX Decoding Library

Post by BlueCrab »

Just to let you know, PH3NOM, you're more than welcome to upload any Dreamcast-related stuff as an attachment on a post here, and I'd encourage you to do so. It'd probably be a good idea too, just in case the files were to get deleted from megaupload or something like that. It'd be a shame to lose stuff due to that kind of thing happening.

Also, just as a minor nit-pick, there's no : in KallistiOS or in its abbreviation. :wink:
User avatar
Basil
Insane DCEmu
Insane DCEmu
Posts: 200
Joined: Wed Apr 09, 2008 9:04 am
Has thanked: 13 times
Been thanked: 0
Contact:

Re: LibADX - A Dreamcast K:OS ADX Decoding Library

Post by Basil »

Thank you, i read someone was asking for it.
Is there a legal way to encode to adx ?
User avatar
mankrip
DCEmu Ex-Mod
DCEmu Ex-Mod
Posts: 3712
Joined: Sun Nov 04, 2001 5:12 pm
Has thanked: 0
Been thanked: 0
Contact:

Re: LibADX - A Dreamcast K:OS ADX Decoding Library

Post by mankrip »

Haven't looked at it, but I guess it runs on the SH4. Did the official games use the sound processor for decoding ADX files?
Ph'nglui mglw'nafh mankrip Hell's end wgah'nagl fhtagn.
==-=-=-=-=-=-=-=-=-=-==
Dev blog / Twitter / YouTube
Image
User avatar
PH3NOM
DC Developer
DC Developer
Posts: 576
Joined: Fri Jun 18, 2010 9:29 pm
Has thanked: 0
Been thanked: 5 times

Re: LibADX - A Dreamcast K:OS ADX Decoding Library

Post by PH3NOM »

Basil wrote:Thank you, i read someone was asking for it.
Is there a legal way to encode to adx ?
FFMpeg should handle that just fine.
Although, looking at the source, that encoder does not support encoding loop information.
mankrip wrote:Haven't looked at it, but I guess it runs on the SH4. Did the official games use the sound processor for decoding ADX files?
The ADX decoder consumes 1% of the Dreamcast's CPU.
http://www.segatech.com/technical/critools/index.html

I think the hard part with decoding ADX on the AICA would be streaming the encoded samples to the AICA.
It would be easy to upload an entire ADX to SRAM then decode it, but most ADX's will be to big to fit into SRAM, so it would need to be streamed somehow.
If anyone has some ideas on how to make this work, I would be interested to hear.
User avatar
Basil
Insane DCEmu
Insane DCEmu
Posts: 200
Joined: Wed Apr 09, 2008 9:04 am
Has thanked: 13 times
Been thanked: 0
Contact:

Re: LibADX - A Dreamcast K:OS ADX Decoding Library

Post by Basil »

FFMpeg should handle that just fine.
Can you give me a tutorial ?
User avatar
Neoblast
DC Developer
DC Developer
Posts: 314
Joined: Sat Dec 01, 2007 8:51 am
Has thanked: 3 times
Been thanked: 1 time

Re: LibADX - A Dreamcast K:OS ADX Decoding Library

Post by Neoblast »

Wow, now that is a sserious surprise.
This might end some issues with audio formats.

Hum one question, when this is loaded, does it take over the whole sound system or is it possible to load any other formats through other libs ?
Chilly Willy
DC Developer
DC Developer
Posts: 414
Joined: Thu Aug 20, 2009 11:00 am
Has thanked: 0
Been thanked: 2 times

Re: LibADX - A Dreamcast K:OS ADX Decoding Library

Post by Chilly Willy »

Basil wrote:
FFMpeg should handle that just fine.
Can you give me a tutorial ?
From a terminal

Code: Select all

ffmpeg -i input.wav -acodec adpcm_adx output.wav
That will take an input wav and encode it as ADX and store it in a wav file. The input file can be a bunch of different formats... whatever ffmpeg is capable of decoding, be it ogg or mp3 or plain pcm. The output should be stored in a wav file - the header will have the compression type set in it. That is the simplest form of transcoding, and will have the same settings as the input file - if the input was mono and 44kHz, so will the output. If the input was 22kHz and stereo, so will the output.

To change the audio before encoding, you can use filters in ffmpeg, or a completely different app to pipe the data to ffmpeg. Here's how you use sox to pipe data to ffmpeg

Code: Select all

sox -v 0.90 BadAppleEn.ogg -t wav -c 2 -r 22050 - | ffmpeg -i - -acodec adpcm_adx output.wav
That will have sox decode BadAppleEn.ogg into a wav at 90% volume with two channels and 22kHz sample rate that will be piped into ffmpeg which will then encode it to a wav with ADX encoding. If you needed it to be 8kHz mono 8-bit, you would do

Code: Select all

sox -v 0.90 BadAppleEn.ogg -t wav -c 1 -r 8000 -b 8 - | ffmpeg -i - -acodec adpcm_adx output.wav
EDIT: Found an issue with my instructions... you can't actually store an ADX stream in a wave. Weird, but ffmpeg won't do that. In fact, I'm having trouble finding an output format that WILL store an ADX stream. What good is ADX encoding if you can't save the encoded data? It won't even save it as raw data. :|

EDIT 2: Couldn't find anything on saving the output of ffmpeg when encoding as ADX. I guess for now it's wav2adx or nothing. :cry:
User avatar
PH3NOM
DC Developer
DC Developer
Posts: 576
Joined: Fri Jun 18, 2010 9:29 pm
Has thanked: 0
Been thanked: 5 times

Re: LibADX - A Dreamcast K:OS ADX Decoding Library

Post by PH3NOM »

BlueCrab wrote:Just to let you know, PH3NOM, you're more than welcome to upload any Dreamcast-related stuff as an attachment on a post here, and I'd encourage you to do so. It'd probably be a good idea too, just in case the files were to get deleted from megaupload or something like that. It'd be a shame to lose stuff due to that kind of thing happening.

Also, just as a minor nit-pick, there's no : in KallistiOS or in its abbreviation. :wink:
Funny, I thought I saw it abbreviated as K:OS somewhere...

And, stupid question; Now that megaupload is in limbo, how exactly to I upload files as attachments here?
User avatar
BlueCrab
The Crabby Overlord
The Crabby Overlord
Posts: 5658
Joined: Mon May 27, 2002 11:31 am
Location: Sailing the Skies of Arcadia
Has thanked: 9 times
Been thanked: 69 times
Contact:

Re: LibADX - A Dreamcast K:OS ADX Decoding Library

Post by BlueCrab »

PH3NOM wrote:
BlueCrab wrote:Just to let you know, PH3NOM, you're more than welcome to upload any Dreamcast-related stuff as an attachment on a post here, and I'd encourage you to do so. It'd probably be a good idea too, just in case the files were to get deleted from megaupload or something like that. It'd be a shame to lose stuff due to that kind of thing happening.

Also, just as a minor nit-pick, there's no : in KallistiOS or in its abbreviation. :wink:
Funny, I thought I saw it abbreviated as K:OS somewhere...

And, stupid question; Now that megaupload is in limbo, how exactly to I upload files as attachments here?
You have to use the full "Post a reply" or "New topic" page to see the option, not the quick reply box at the bottom of the thread. Once you're on the full post editor, you should see a box labeled "Upload attachment" right underneath the Preview/Submit/Save/Cancel buttons. Pick your file, hit the add a file button, and you should be good to go.

You also should be able to upload an attachment if you just edit a post as well. The upload box should be in the same place.

If you don't see it, let me know, and I'll figure out why its not showing up.
Ayla
DC Developer
DC Developer
Posts: 142
Joined: Thu Apr 03, 2008 7:01 am
Has thanked: 0
Been thanked: 4 times
Contact:

Re: LibADX - A Dreamcast K:OS ADX Decoding Library

Post by Ayla »

Could you re-upload it plz?
User avatar
PH3NOM
DC Developer
DC Developer
Posts: 576
Joined: Fri Jun 18, 2010 9:29 pm
Has thanked: 0
Been thanked: 5 times

Re: LibADX - A Dreamcast K:OS ADX Decoding Library

Post by PH3NOM »

Ayla wrote:Could you re-upload it plz?
Original post updated with attachment...
User avatar
Newbie
Insane DCEmu
Insane DCEmu
Posts: 171
Joined: Sat Jul 27, 2013 1:16 pm
Has thanked: 0
Been thanked: 0

Re: LibADX - A Dreamcast K:OS ADX Decoding Library

Post by Newbie »

Is anybody have tested how time (% cycle) this ADX decoding lib takes on SH4 CPU ?
User avatar
PH3NOM
DC Developer
DC Developer
Posts: 576
Joined: Fri Jun 18, 2010 9:29 pm
Has thanked: 0
Been thanked: 5 times

Re: LibADX - A Dreamcast K:OS ADX Decoding Library

Post by PH3NOM »

Newbie wrote:Is anybody have tested how time (% cycle) this ADX decoding lib takes on SH4 CPU ?
Good question, I have not made any concrete benchmarks of SH4 time consumed.

One thing I can say, is since I released that lib I have made an attempt to get the lib running on the ARM instead of the SH4.

The ARM is too slow to run the ADX decode algorithm in stereo real-time, although it was fast enough to decode mono real time. In porting the lib to the arm, I realized some deficiencies in the decoder implementation by the original author bero, that had the routine copying back and forth between temporary buffers each chunk in a stereo stream. As an attempt to optimize for the ARM, I removed the need for temporary buffer storage by adding a strided version of the chunk decoder.

The optimizations to the lib can be seen in my latest release of Dreamcast Media Center:
viewtopic.php?f=72&t=102594
User avatar
Newbie
Insane DCEmu
Insane DCEmu
Posts: 171
Joined: Sat Jul 27, 2013 1:16 pm
Has thanked: 0
Been thanked: 0

Re: LibADX - A Dreamcast K:OS ADX Decoding Library

Post by Newbie »

SH4 on DC is a 200 MHZ CPU so the CPU time slice is very little.

To manage getting % of CPU time slice, we need a very accurate timer which could operate in nano (10E-9) seconds perhaps, because MHZ frequency means 10E-6 seconds period ... I am not sure.

Besides, the fact that the decoder runs on a separate thread make the thing difficult.

I tried to use the classic "timer_us_gettime64()" function by time subtraction concept.

I run the code without calling the decoder and get the execution time of the main routine then i make the same test calling the decode routine.

The result is not accurate but it seems that it takes 0 ms, probably a fraction of a ms that is not an integer ....

I am working on a object oriented version of your code to use it in my projects so i have make some modifications.

By the way, i have a thread question :)

I am not sure that all threads created by "thd_create" are disposed.
I could track them as they become zombie threads as their state is STATE_ZOMBIE (in thread.h).
But after ... the pointer returned by "thd_create" is not accurate to verify the dispose fact because it seems to be never null ....

Thanks for help.
User avatar
BlueCrab
The Crabby Overlord
The Crabby Overlord
Posts: 5658
Joined: Mon May 27, 2002 11:31 am
Location: Sailing the Skies of Arcadia
Has thanked: 9 times
Been thanked: 69 times
Contact:

Re: LibADX - A Dreamcast K:OS ADX Decoding Library

Post by BlueCrab »

Newbie wrote:By the way, i have a thread question :)

I am not sure that all threads created by "thd_create" are disposed.
I could track them as they become zombie threads as their state is STATE_ZOMBIE (in thread.h).
But after ... the pointer returned by "thd_create" is not accurate to verify the dispose fact because it seems to be never null ....

Thanks for help.
In the future, please make a new thread for unrelated questions. I'll let it slide and answer your question this time... :wink:

The pointers never get changed unless you change them. The thread code doesn't know where all your pointers are and thus can't overwrite them. Also, if you're using a relatively new version of KOS, you're going to need to make sure to either thd_join or thd_detach all your threads, otherwise they will sit around wasting memory forever (which is obviously bad).
User avatar
Newbie
Insane DCEmu
Insane DCEmu
Posts: 171
Joined: Sat Jul 27, 2013 1:16 pm
Has thanked: 0
Been thanked: 0

Re: LibADX - A Dreamcast K:OS ADX Decoding Library

Post by Newbie »

No problem i move it :)
User avatar
Newbie
Insane DCEmu
Insane DCEmu
Posts: 171
Joined: Sat Jul 27, 2013 1:16 pm
Has thanked: 0
Been thanked: 0

Re: LibADX - A Dreamcast K:OS ADX Decoding Library

Post by Newbie »

I found in the last version of LibADX the code line in adx_to_pcm_stride below :

Code: Select all

++out; // Add pointer stride to allow stereo interleave (C) PH3NOM
What is it useful for ?
User avatar
Newbie
Insane DCEmu
Insane DCEmu
Posts: 171
Joined: Sat Jul 27, 2013 1:16 pm
Has thanked: 0
Been thanked: 0

Re: LibADX - A Dreamcast K:OS ADX Decoding Library

Post by Newbie »

Using timer_us_gettime64(); which is in micro seconds, i found about 3 micro seconds for decoding.
But i'm not sure my method is accurate ...
User avatar
PH3NOM
DC Developer
DC Developer
Posts: 576
Joined: Fri Jun 18, 2010 9:29 pm
Has thanked: 0
Been thanked: 5 times

Re: LibADX - A Dreamcast K:OS ADX Decoding Library

Post by PH3NOM »

Newbie wrote:I found in the last version of LibADX the code line in adx_to_pcm_stride below :

Code: Select all

++out; // Add pointer stride to allow stereo interleave (C) PH3NOM
What is it useful for ?
The reason I did that is to place the samples in their correct position in the stereo sample buffer, as stereo pcm samples are stored interleaved.

Not only does this place the samples in the correct position, but it also removes the need to use a intermediate buffer as was being done previously:

Code: Select all

    	    short tmpbuf[32*2];
		    adx_to_pcm(tmpbuf,adx_buf,prev);
		    adx_to_pcm(tmpbuf+32,adx_buf+ADX_Info.chunk_size,prev+1);
		    for(i=0;i<32;i++) {
			    outbuf[i*2]   = tmpbuf[i];
			    outbuf[i*2+1] = tmpbuf[i+32];
		    }
Now, we use intelligent pointers to prevent using a intermediate buffer:

Code: Select all

	           short *ptr = pcm_buf->buf+pcm_buf->size;
	           adx_to_pcm_stride(ptr,sfa_ptr,prev);
	           adx_to_pcm_stride(ptr+1,sfa_ptr+ADX_Info.chunk_size,prev+1);
The new lib is being utilized by the function DCMC_LibADX_Decode in libdcmc/dcmc-audio.c if you want to see how to decode adx bitstream using the new lib.
Newbie wrote:Using timer_us_gettime64(); which is in micro seconds, i found about 3 micro seconds for decoding.
But i'm not sure my method is accurate ...
It sounds like you are measuring time inside your main function? Would be best to measure speed within the adx library code, if you are measuring in microseconds.

But, it sounds like the overall performance impact of the lib is negligible.

Also, worth noting, the lib only uses integer computations, no floating point operations are used, so the Sh4's FPU is not burdened and could be executing other computations in the mean time...

My benchmarks showed LibADX to be much, much faster than using MP3... I am curious if you can compare using LibADX vs the LibMP3 that comes with KOS, using the methods you have used to record time...
Post Reply