don't click here

Demystifying Cinepak For Sega

Discussion in 'Technical Discussion' started by Bgvanbur, Aug 27, 2011.

  1. Bgvanbur

    Bgvanbur

    Tech Member
    128
    1
    0
    USA
    a disassembly, some small Sega CD projects
    I have been researching heavily into the Cinepak For Sega (as used on the Sega CD).

    First off, many places that talk about Cinepak for Sega and it seems like they make up things with little or no evidence. Cinepak uses 16276 Hz 8-bit PCM (this is fixed) but the code thinks it is 16000 Hz (so it plays faster than the code expects). It has a max of 61 unique colors in one frame since it only uses one plane with the possibility of 1 to 4 palettes. It is not 128 or 256 colors as many places suggest (including Sega Mega CD - Sega Retro). It is double buffered in both the Word RAM and the VDP RAM (so when seeing if a tile has changed, you need to check if was the same from two frames ago, and do not check it against the last frame). There is no necessity for ordered dithering to be used, but it is typically used since ordered dithering typically produces smaller frame data due to the way 4x2 or 2x2 pixel blocks are encoded and reused (and better compression means you can have a larger picture or faster frame rate). When two palettes are used, the palette map uses two bits to encode each palette for a tile. And many places claim a movie is or is not Cinepak For Sega and are wrong.

    I have been working on disassemblies of both the MAIN and SUB CPU code, a detailed list of Cinepak games and notes about each one, a Cinepak encoder, and a Cinepak decoder. The MAIN code went through a couple of revisions but not much changed between revisions. The SUB code went through several revisions and had much larger changes (which makes sense since it was more complex than the MAIN code). I plan to release all of these as they get more refined and finalized.

    For now, I am excited to provide my Nyan Cat Cinepak Demo (it uses unmodified Cinepak revision 1.2 with some small startup MAIN/SUB code to start the movie). I was real happy when this finally worked in Gens (and worked first try on hardware last night).

    Collection of files released in this thread:

    Nyan Cat Cinepak Demo ISO
    Bad Apple Cinepak Demo ISO and source
    Glenn Miller In The Mood Cinepak Demo ISO and source
    Nyan Cat Cinepak Demo ISO and source
    Star Wars Chess Reencoded Cinepak Demo ISO and source
    Cinepak 68k Disassemblies
    Cinepak Movie Reseach (ods)
    Cinepak Movie Research (xls)
     
  2. Overlord

    Overlord

    Now playable in Smash Bros Ultimate Moderator
    19,218
    965
    93
    Long-term happiness
    That's both hilarious AND a really neat technical achievement - my kudos to you, sir.
     
  3. Sik

    Sik

    Sik is pronounced as "seek", not as "sick". Tech Member
    6,718
    1
    0
    being an asshole =P
    The PCM hardware on the MCD plays at 32000 Hz, so 16000 Hz (half that) seems more than logical. Make what you want out of that.
     
  4. Bgvanbur

    Bgvanbur

    Tech Member
    128
    1
    0
    USA
    a disassembly, some small Sega CD projects
    Thanks overlord.

    Yeah most places mention 32000 Hz as the base PCM rate, but the discussion at SpriteMinds seemed correct since they seem to know what they were talking about (SpriteMind.Net :: View topic - Small Sega CD PCM question: sample rates). And I assumed all the people reporting 32000 Hz were just rounding the rate to a nice number. But it does indeed seem to be 32000 Hz (and Cinepak plays with at half the base rate, so 16000 Hz).

    So when I originally tried 16276 Hz, since the sample rate wasn't right about one minute in I would get serious audio glitches as the audio chunks were filling faster than they should have been. So I tried the 16000 Hz, and the whole 3.5 minute demo played with no audio glitches so the audio chunks are being properly buffered at the right speed.
     
  5. Andlabs

    Andlabs

    「いっきまーす」 Wiki Sysop
    2,175
    1
    0
    Writing my own MD/Genesis sound driver :D
    Nice job with the research!

    The Sega Retro console pages need to be rewritten to be more accurate. I did the Mega Drive one, I think the Saturn and 8-bit ones were handled, and I was in the middle of the 32X one and need to continue. Perhaps the Mega CD should be done first, since that hardware is more complicated than 32X hardware.

    One thing I cannot find proof of is at what frequency the PCM chip runs at though everyone thinks it's 32000hz; it's the one thing separating me (and all of us) from these Sonic CD PCM sample rips. Has anyone checked the hardware?
     
  6. If the emulators can play the audio back, then perhaps you can find that information there. Perhaps I'm talking out of my ass.
     
  7. Sik

    Sik

    Sik is pronounced as "seek", not as "sick". Tech Member
    6,718
    1
    0
    being an asshole =P
    Well, Sega docs say 32000 Hz, and it seems to be like that so far... Also audio frequencies are the one place where you really don't want to be off even by 1 Hz.
     
  8. Bgvanbur

    Bgvanbur

    Tech Member
    128
    1
    0
    USA
    a disassembly, some small Sega CD projects
    I reviewed the Sega CD manual, and in the overview it reports the PCM as 32KHz twice. But in the PCM part is the following that looks like 31.9 KHz:

    [​IMG]

    And if I look at the first long movie of Ecco 2, the video imagewise is (0x1D5B0+0x28)/600 seconds long with 3207468 bytes of pcm data, which means it has 16000.0 bytes/second for audio. So obviously this Cinepak file was targeting 16000 Hz audio. This also has no problems when I put it in my Cinepak move only ISO.

    I made a PCM test iso (PCMTEST.ISO) that has low peak and high peak every 0x4000 bytes in the PCM data. The I recorded from my actual Model 2 US Sega CD and got the following wave file (PCMTEST.WAV, 8bit, 22050 Hz). Looking at the peaks, I get 0x4000 bytes / ( (0xa756-0x50a2)bytes / 22050 Hz ) = 16276.2 Hz.

    The more I look, the more I get confused as to which frequency is correct. Ugh.
     
  9. GerbilSoft

    GerbilSoft

    RickRotate'd. Administrator
    2,971
    76
    28
    USA
    rom-properties
    I'm thinking that while the Sega CD does use 32,552.4 Hz as its native frequency, most games used 32,000 Hz for ease of development. There's an "address step" register that lets you specify a fractional increment, which lets you use basically any frequency lower than 32,552.4 Hz. (I don't know offhand how to calculate the appropriate value for 16,000 Hz, but the manual probably indicates how to do it.)

    Of course, using a frequency that isn't an integral ratio of the base frequency might result in some aliasing, but I don't think it's too noticeable on the Sega CD.
     
  10. Bgvanbur

    Bgvanbur

    Tech Member
    128
    1
    0
    USA
    a disassembly, some small Sega CD projects
    Cinepak SUBPLAY.BIN code uses FDH/FDL=0x0400, which according to the Sega CD development docs says it should playback at half the base frequency. Here is the only part of the SUBPLAY.BIN disassembly that modifies the PCM control registers and how it sets up the first PCM channel:

    Code (Text):
    1.     ;; modify channel 1
    2.     MOVE.B   #0xC0,0xF(A0)
    3.     MOVEQ    #0x14,D0
    4. AudioInitSubCh1Wait0:  
    5.     DBF      D0,AudioInitSubCh1Wait0
    6.     ;; envelope (affects amplitude)
    7. #if REVISION > 19931003
    8.     MOVE.B   #0xB4,0x1(A0)
    9. #else
    10.     MOVE.B   #0xFF,0x1(A0)
    11. #endif
    12.     MOVEQ    #0x14,D0
    13. AudioInitSubCh1Wait1:  
    14.     DBF      D0,AudioInitSubCh1Wait1
    15.     ;; set both left and right to equal panning
    16.     MOVE.B   #0xFF,0x3(A0)
    17.     MOVEQ    #0x14,D0
    18. AudioInitSubCh1Wait2:  
    19.     DBF      D0,AudioInitSubCh1Wait2
    20.     ;; set sampling (high) to 1/2 base sampling frequency
    21.     ;; which is (12.5MHz/384)*0.5
    22.     MOVE.B   #0x4,0x7(A0)
    23.     MOVEQ    #0x14,D0
    24. AudioInitSubCh1Wait3:
    25.     DBF      D0,AudioInitSubCh1Wait3
    26.     ;; set sampling to (low) 1/2 base sampling frequency
    27.     CLR.B    0x5(A0)
    28.     MOVEQ    #0x14,D0
    29. AudioInitSubCh1Wait4:  
    30.     DBF      D0,AudioInitSubCh1Wait4
    31.     ;; set stop address (high)
    32.     CLR.B    0xB(A0)
    33.     MOVEQ    #0x14,D0
    34. AudioInitSubCh1Wait5:  
    35.     DBF      D0,AudioInitSubCh1Wait5
    36.     ;; set stop address (low)
    37.     CLR.B    0x9(A0)
    38.     MOVEQ    #0x14,D0
    39. AudioInitSubCh1Wait6:
    40.     DBF      D0,AudioInitSubCh1Wait6
    41.     CLR.B    0xD(A0)
    42.     MOVEQ    #0x14,D0
    43. AudioInitSubCh1Wait7:
    44.     DBF      D0,AudioInitSubCh1Wait7
     
  11. TmEE

    TmEE

    Master of OPL3-SA2/3 Tech Member
    1,726
    2
    18
    Estonia, Rapla City
    T-04YBSC-A !
    Clock for sound is exactly 1250000Hz, master clock of 50MHz divided by 4, and sample rate is derived from it. 32552Hz is a nice number as it gives a divisor of 384.
     
  12. Thousand Pancake

    Thousand Pancake

    Being a food you put milk on and then eat in the m Member
    360
    0
    0
    I think I'm starting to get sick of the flying poptart.

    Please give this forum a tongue sticking out emoticon. Preferably today. :v:
     
  13. Bgvanbur

    Bgvanbur

    Tech Member
    128
    1
    0
    USA
    a disassembly, some small Sega CD projects
    Now after further dissassembly I found that Cinepak determines the current time by taking the current audio ticks based on the PCM data position and divides it by 16000 (and then this time is used to advance the image frames). This is why 16000 seemed to be the correct playback frequency, but it is just bad calculation in the Cinepak code that makes it appear the PCM data should be played back at 16000 when the audio playback is really at 16276 Hz.
     
  14. Bgvanbur

    Bgvanbur

    Tech Member
    128
    1
    0
    USA
    a disassembly, some small Sega CD projects
    I have made a spreadsheet containing detailed information on 4260 Cinepak For Sega files (does not include Tomcat Alley, Surgical Strike and Wirehead files since they are a modified format that I have not investigate yet, and does not contain 2 Joe Montana's NFL Football and 1 Crime Patrol files due to errors in these files, and may not include any games that I missed).

    OpenDocument Spreadsheet: cinepakfiles.ods
    Excel Spreadsheet: cinepakfiles.xls

    Some interesting notes:
    • Each file always uses a fixed number of palettes (2, 3, or 4) despite sometimes up to 72% of the palettes not being used.
    • Files use framerates between 1 and 30. The early files used small video size and large framerates, as time went on framerates went down to increase video size.
    • Tile data is compressed between 31% and 63%.
    • These files never reuse tiles even though the code supports this (as utilized in my Nyan Cat demo).
    • Only one file has no images.
    • 168 files have no audio.
    • The longest playing Cinepak file is 5:53 and there are many single and double frame files.
    • Every file always fills the 4x4 and 4x2 tables to full capacity even if not all the entries are used.
    • The max unique colors in one file is 461 (out of a possible 512 colors).
    • The cinepak files are sometimes single files or grouped in large files, and use a variety of naming conventions and file extensions (and even use extensions used by other games for other video formats).
     
  15. ICEknight

    ICEknight

    Researcher Researcher
    So Mega-CD FMVs could have looked 72% better than they did? Those lazy devs...
     
  16. Bgvanbur

    Bgvanbur

    Tech Member
    128
    1
    0
    USA
    a disassembly, some small Sega CD projects
    Not really. Each unused palette only wastes 32 bytes. Since they used a fixed number of palettes, a frame with less palettes would just mean that frame didn't need as many palettes as other frames. This wouldn't really affect FMV quality.

    The real deficiency is the lack of reusing tiles from previous frames. (For example if I reencode the Star Wars Chess cinepak files, I get around 50% reduction in size by reusing tiles since it has most of the outer tiles not changing.) For movies like this, this huge amount of extra bandwidth could have been used for better color quality, better framerate, or better video size.
     
  17. Bgvanbur

    Bgvanbur

    Tech Member
    128
    1
    0
    USA
    a disassembly, some small Sega CD projects
    I am releasing the source for several Cinepak demos. All the source files are inside the ISO itself. I build these on Linux using PERL, ffmpeg, and SCDTools (mentioned in more detail below). The convert.sh script is used to convert the original movie into a Sega CD ISO with the Cinepak demo. The SCDMAKE.CFG file controls how SCDMAKE will make the ISO. MAINPLAY.BIN and SUBPLAY.BIN are the Cinepak code for the MAIN and SUB CPUs respectively. IP.ASM and SP.ASM are the intial MAIN and SUB programs respectively.

    SCDTools is a suite of scripts I made that are located at Sourceforge. My scdasm script wraps my favorite 68k assembler for easy calling and not wondering about all the various options that typically get passed. My scdwav2pcm script converts 8-bit PCM wave audio files to the sega pcm format. My scdimg2tile generates Sega VDP tile data/maps from images, determines Sega palettes for a set of images, and can perform a few dithering methods (such as 2x2 ordered dithering and 4x2 ordered dithering that is really handy for Cinepak). My scdcinepakencodeframe script can encode a frame's tile data into the compressed Cinepak tile data. My scdcinepakencode script encodes an entire Cinepak file from palette files, cinepak compressed data, and pcm data. My scdmake script makes the Sega CD ISO (controls all the Sega fields, the PVD fields, supports Joliet, supports directories, and supports making an include file detailing file starts and sizes to make easy file usage in the SUB code). My scdmoviedecode will decode Cinepak movie files and some Sonic CD movie files (it decodes it into a format easy to use for scdcinepakencodeframe and scdcinepakencode. My scdwrite script is how I make Sega CD ISOs on Linux.

    And now the links for the Cinepak demos with the source:
    BADAPPLESRC.ZIP: Bad apple video, uses greyscale and the video is mostly monochrome and a lot of tile reuse and I did not use any dithering.
    GMITMSRC.ZIP: Glenn Miller - In the mood, it is live action and has very little tile reuse, uses 4x2 ordered dithering.
    NYANCATSRC.ZIP: The initial Cinepak demo I made, has a custom encoding script to allow non-uniform frame durations and allows for looping the 12 frames.
    X6ATNEWSRC.ZIP: The Star Wars Chess movie of AT vs C3PO, decoded and reencoded with my cinepak encoder (and since it support tile reuse the resulting file is much smaller than the original).

    I still am working on cleaning up my Cinepak disassemblies for public release.
     
  18. Bgvanbur

    Bgvanbur

    Tech Member
    128
    1
    0
    USA
    a disassembly, some small Sega CD projects
    Finally releasing the source to the MAINPLAY and SUBPLAY Cinepak for Sega modules.

    CINEPAKSRC.ZIP

    It has MAINPLAY.ASM and SUBPLAY.ASM, the source for both Cinepak modules that supports multiple revisions using preprocessor directives. I have also included assembly sources for each revision for those who cannot use or cannot be bothered to use gpp. The MAINPLAY.ASM is very well documented and properly assembles to the original binary for all but one revision. The SUBPLAY is also very well documented except for a few places, and properly assembles to the original binary for over half of the revisions (there is more complex variations in the revisions for SUBPLAY).

    It also has all the original binaries I extracted from various Cinepak for Sega games (note: some Cinepak games I was unable to easily determine the Cinepak module locations).

    It also has DEMYST.PFT, a document that documents the Cinepak for Sega format, some of the revision information, some of the problems with it, how to use the modules, and a brief section about a few games (don't really feel like finishing this section of the document).

    I doubt I will work on Cinepak for Sega disassembly or research much more beyond this. Though I intend to make use of it in any homebrew games I make.
     
  19. Chilly Willy

    Chilly Willy

    Tech Member
    751
    11
    18
    Doom 32X
    Fantastic! Thank you for your efforts, and for releasing the code.
    :)
     
  20. Will this likely work on hardware? :O