Saturday, August 13, 2011
A very impressive Russian site is trying to recreate most of the game's content for browsing on the web. What impressed me even more is the creator managed to reverse-engineer some of the game's data types before I did. He kindly gave jPSXdec a shout out since it was used heavily to extract nearly everything on the site.
This very old Japanese site I've seen before, but did a good job of documenting the game's content as well.
Friday, August 5, 2011
I figured it would be a bit rough to use this approach. Unfortunately, anything more than this would multiply the amount of work many times.
I've also discovered there are 34 images on the game discs that don't seem to ever appear in the game. They're not particularly interesting, however.
Wednesday, June 1, 2011
While developing jPSXdec for the last 4 years, I've run across three different methods of decoding bitstreams.
If you'd like to learn more about what part this plays in MPEG and PlayStation .STR decoding, check out my thorough document on the subject: PlayStation_STR_format.txt
Approach 1: Brute force
This is the most obvious approach. For each code, peek the next n-bits until the bits match something.
In the worst case, this approach requires 111 conditional checks to identify a bit code. To be honest, I've never actually seen this implemented anywhere besides by me years ago when first learning about bitstream parsing.
Approach 2: Binary tree
I actually ran across this approach implemented in the Serial Experiments Lain PlayStation game. You have a tree of conditionals testing the value of each bit until a match is found.
The branching can be optimized a bit for most leaves: once the length of the bit code is clear, the remaining bits can be used as the index in several small lookup tables. The jPSXdec implementation only requires (in the worst case) 12 branches to determine the longest bit codes.
Approach 3: Array lookup
I believe this type of approach is used in ffmpeg and the Q-gears decoder. Thanks to the unspoken tradition of never documenting anything, I was unable to understand what it was doing. It wasn't until I reverse-engineered the .iki bitstream parsing that I finally saw how this approach works.
At least for MPEG-1 (and PSX STR), you can take advantage of its particular set of variable length bit codes. Only the first code ('
11s') and the end-of-block code ('
10') need special parsing. The rest of the codes fall under one of three groups. The group a code belongs to can be determined by looking at how many initial zeros it has.
- Group one starts with between 1 and 4 zeros (this also includes the escape code
- Group two starts with between 6 and 8 zeros.
- Group three starts with between 9 and 11 zeros.
All codes in their groups:
01 // end-of-block
---- [Group 1] ----
0 00001 // escape code
---- [Group 2] ----
---- [Group 3] ----
Each group has its own lookup table of 256 entries, and each code will be associated with one or more entries in the lookup table. After stripping off the minimum number of zeros in the group, no entry in the group will have more than 8 bits remaining in the bit code. For codes that have 8 bits remaining, its value identifies the associated table index. For the bit codes that have fewer than 8 bits remaining, you have to walk through every combination of the remaining bits to find all associated indexes.
Use 0 for sign bit for now: 001100
Strip off first leading 0: 01100
Find all combinations of remaining bits:
01100+000 = 96 (table index)
01100+001 = 97
01100+010 = 98
01100+011 = 99
01100+100 = 100
01100+101 = 101
01100+110 = 102
01100+111 = 103
Thus bit code 00110s will be associated with table indexes 96-103.
Now each table entry needs three values: the inverse discreet cosine transform (IDCT) run of zero-value alternating current (AC) coefficients, the non-zero AC coefficient value, and the length of the bitstream bits that should be skipped.
Once all three tables are constructed, the following pseudo code will parse your bitstream.
Of course the implementation details can vary, but this gives the idea. The Approach 3 I implemented for jPSXdec requires about 8 conditionals to identify a bit code in the worst case. I've found it to be about 10%-15% faster than the Approach 2 I've been using.