Recently I've run across a couple of groups trying to replace the video of a PlayStation 1 game. As we have to do the same thing with S.E. Lain, I thought I would share some insight on how it might be done with the highest quality output.
Since there are many things that go into encoding mpeg type images (DCT + quantization being the least of it), that task is best left to programs that know how to do it well, such as ffmpeg. It also allows you to tweak those options, and use its advanced features to get the best results.
So here is how you could do it, broken down into 6 ridiculously complicated steps.
- Convert your video into yuv4mpeg2 video (or perhaps an AVI with a YCbCr codec), but using the PSX specific rgb->YCbCr conversion.
- Feed the YCbCr video into ffmpeg at a mpeg1 allowed fps and create a mpeg1 movie with only I-frames (-intra). This should be done at a variety of different quality levels (-qscale from 1 to n).
- Parse the I-frames out of the movie, and parse each frame's macro-blocks
- Convert the mpeg1 VLCs to PSX VLCs
- Do this for every mpeg1 quality and pick the one that fits best within the amount of space available to each frame
- Multiplex the frames and construct all the sectors, including the correct frame headers and the sectors' ECE/EDC codes
If the new video is mostly just the old video with some changes (e.g. subtitles) then quality can improved tremendously with this variation.
- Use some method to determine which macro blocks to replace (manually picking them, or performing a diff on lossless video data, or a fuzzy diff on lossy video data)
- Only replace those macro blocks with new ones of the same qscale
- If the replaced data makes the frame too big, then either replace the entire frame with an ffmpeg created frame that will fit (qscale will probably be bigger), or remove some quality in the frame to make it smaller
- Multiplex the frames and construct all the sectors
Alternatively have ffmpeg write an AVI using MJPG codec, then parse the JPEG frames and convert the JPEG VLCs to PSX VLCs (I'm not sure how to get ffmpeg to produce quality variations with the MJPG codec).
Encoding video by the steps above will likely have better results than if you used Sony's official PS1 SDK video encoder (and will also reduce the likelihood of serious legal trouble and C&D letters).
Here is a possible ffmpeg command-line for step 2 above.
ffmpeg -i source.y4m -vcodec mpeg1video -r 30 -qscale # \
-trellis 1 -intra -dc 10 -dct faan -debug dct_coeff \
-vstats_file qscale#.log out-qscale#.m1v
-i source.y4m : yuv4mpeg2 input file
-vcodec mpeg1video : write mpeg1
-r 30 : use 30 frames/sec
(just to use something
compatible with mpeg1)
-qscale # : chosen qscale
-trellis 1 : trellis quantization
does this even apply?
-intra : only write I-frames
-dc 10 : use 10 bits of precision
for the DC values?
(not sure if this is helpful)
-dct faan : use a floating-point
for best quality
-debug dct_coeff : forgot what this does
-vstats_file qscale#.log : write a log of useful info
out-qscale#.m1v : output file, video only