Sonic and Sega Retro Message Board: Demystifying Cinepak For Sega - Sonic and Sega Retro Message Board

Jump to content

Hey there, Guest!  (Log In · Register) Help
  • 2 Pages +
  • 1
  • 2
    Locked
    Locked Forum

Demystifying Cinepak For Sega For now, a small preview of my research

#1 User is offline Bgvanbur 

Posted 27 August 2011 - 12:37 PM

  • Posts: 128
  • Joined: 02-November 10
  • Gender:Male
  • Location:USA
  • Project: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)
This post has been edited by Bgvanbur: 10 July 2012 - 08:32 AM

#2 User is offline Overlord 

Posted 27 August 2011 - 02:32 PM

  • Substitute Meerkovo IT Chief
  • Posts: 17149
  • Joined: 12-January 03
  • Gender:Male
  • Location:Berkshire, England
  • Project:VGDB
  • Wiki edits:3,204
That's both hilarious AND a really neat technical achievement - my kudos to you, sir.

#3 User is offline Sik 

Posted 27 August 2011 - 04:45 PM

  • Sik is pronounced as "seek", not as "sick".
  • Posts: 6719
  • Joined: 17-March 06
  • Gender:Male
  • Project:being an asshole =P
  • Wiki edits:11

View PostBgvanbur, on 27 August 2011 - 12:37 PM, said:

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 16000 Hz 8-bit PCM (this is fixed). It is not 12.5Mhz/384/2 (16276Hz) as some places suggest (I actually thought this seemed plausible but from my testing I proved it can't be 16276 and must be 16000 or really close to it).
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 User is offline Bgvanbur 

Posted 27 August 2011 - 09:45 PM

  • Posts: 128
  • Joined: 02-November 10
  • Gender:Male
  • Location:USA
  • Project:a disassembly, some small Sega CD projects
Thanks overlord.

View PostSik, on 27 August 2011 - 04:45 PM, said:

View PostBgvanbur, on 27 August 2011 - 12:37 PM, said:

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 16000 Hz 8-bit PCM (this is fixed). It is not 12.5Mhz/384/2 (16276Hz) as some places suggest (I actually thought this seemed plausible but from my testing I proved it can't be 16276 and must be 16000 or really close to it).
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.

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 User is offline Andlabs 

Posted 27 August 2011 - 09:59 PM

  • 「いっきまーす」
  • Posts: 2175
  • Joined: 11-July 08
  • Gender:Male
  • Project:Writing my own MD/Genesis sound driver :D
  • Wiki edits:7,061
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?
This post has been edited by Andlabs: 27 August 2011 - 10:03 PM

#6 User is offline Hendricks 266 

Posted 28 August 2011 - 01:34 AM

  • Posts: 362
  • Joined: 01-June 05
  • Gender:Male
  • Location:United States
  • Wiki edits:58
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 User is offline Sik 

Posted 28 August 2011 - 06:06 AM

  • Sik is pronounced as "seek", not as "sick".
  • Posts: 6719
  • Joined: 17-March 06
  • Gender:Male
  • Project:being an asshole =P
  • Wiki edits:11

View PostAndlabs, on 27 August 2011 - 09:59 PM, said:

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?
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 User is offline Bgvanbur 

Posted 28 August 2011 - 08:23 AM

  • Posts: 128
  • Joined: 02-November 10
  • Gender:Male
  • Location:USA
  • Project:a disassembly, some small Sega CD projects

View PostSik, on 28 August 2011 - 06:06 AM, said:

View PostAndlabs, on 27 August 2011 - 09:59 PM, said:

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?
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.

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:

Posted Image

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.
This post has been edited by Bgvanbur: 28 August 2011 - 02:55 PM

#9 User is offline GerbilSoft 

Posted 28 August 2011 - 05:14 PM

  • RickRotate'd.
  • Posts: 2846
  • Joined: 11-January 03
  • Gender:Male
  • Location:USA
  • Project:Gens/GS
  • Wiki edits:5,000 + one spin
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 User is offline Bgvanbur 

Posted 28 August 2011 - 07:47 PM

  • Posts: 128
  • Joined: 02-November 10
  • Gender:Male
  • Location:USA
  • Project:a disassembly, some small Sega CD projects

View PostGerbilSoft, on 28 August 2011 - 05:14 PM, said:

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.

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:

	;; modify channel 1
	MOVE.B   #0xC0,0xF(A0)
	MOVEQ    #0x14,D0
AudioInitSubCh1Wait0:	
	DBF      D0,AudioInitSubCh1Wait0
	;; envelope (affects amplitude)
#if	REVISION > 19931003
	MOVE.B   #0xB4,0x1(A0)
#else
	MOVE.B   #0xFF,0x1(A0)
#endif
	MOVEQ    #0x14,D0
AudioInitSubCh1Wait1:	
	DBF      D0,AudioInitSubCh1Wait1
	;; set both left and right to equal panning
	MOVE.B   #0xFF,0x3(A0)
	MOVEQ    #0x14,D0
AudioInitSubCh1Wait2:	
	DBF      D0,AudioInitSubCh1Wait2
	;; set sampling (high) to 1/2 base sampling frequency
	;; which is (12.5MHz/384)*0.5
	MOVE.B   #0x4,0x7(A0)
	MOVEQ    #0x14,D0
AudioInitSubCh1Wait3:
	DBF      D0,AudioInitSubCh1Wait3
	;; set sampling to (low) 1/2 base sampling frequency
	CLR.B    0x5(A0)
	MOVEQ    #0x14,D0
AudioInitSubCh1Wait4:	
	DBF      D0,AudioInitSubCh1Wait4
	;; set stop address (high)
	CLR.B    0xB(A0)
	MOVEQ    #0x14,D0
AudioInitSubCh1Wait5:	
	DBF      D0,AudioInitSubCh1Wait5
	;; set stop address (low)
	CLR.B    0x9(A0)
	MOVEQ    #0x14,D0
AudioInitSubCh1Wait6:
	DBF      D0,AudioInitSubCh1Wait6
	CLR.B    0xD(A0)
	MOVEQ    #0x14,D0
AudioInitSubCh1Wait7:
	DBF      D0,AudioInitSubCh1Wait7


#11 User is offline TmEE 

Posted 29 August 2011 - 06:12 AM

  • Master of OPL3-SA2/3
  • Posts: 1726
  • Joined: 06-January 08
  • Gender:Male
  • Location:Estonia, Rapla City
  • Project:T-04YBSC-A !
  • Wiki edits:11
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.
This post has been edited by TmEE: 29 August 2011 - 06:40 AM

#12 User is offline Thousand Pancake 

Posted 31 August 2011 - 05:35 PM

  • Being a food you put milk on and then eat in the morning
  • Posts: 360
  • Joined: 13-May 09

View PostBgvanbur, on 27 August 2011 - 12:37 PM, said:

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).

Nyan Cat Cinepak Demo ISO
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 User is offline Bgvanbur 

Posted 07 September 2011 - 11:00 PM

  • Posts: 128
  • Joined: 02-November 10
  • Gender:Male
  • Location:USA
  • Project:a disassembly, some small Sega CD projects

View PostTmEE, on 29 August 2011 - 06:12 AM, said:

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.

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 User is offline Bgvanbur 

Posted 10 September 2011 - 01:38 AM

  • Posts: 128
  • Joined: 02-November 10
  • Gender:Male
  • Location:USA
  • Project: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 User is offline ICEknight 

Posted 10 September 2011 - 03:21 PM

  • Posts: 11172
  • Joined: 11-January 03
  • Gender:Male
  • Location:Spain
  • Wiki edits:18

View PostBgvanbur, on 10 September 2011 - 01:38 AM, said:

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.

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

  • 2 Pages +
  • 1
  • 2
    Locked
    Locked Forum

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users