Advertisement
atreyu187

Dreamcast Guides : DCRes SFD Downsampling Tutorial

Jan 6th, 2013
1,489
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 21.29 KB | None | 0 0
  1. ----------------------- Page 1-----------------------
  2.  
  3. How to downsample SFD files
  4. by TuxTheWise
  5.  
  6. Introduction
  7. SFD is the video format most used in Dreamcast. It consists in usual MPEG 1 video
  8. multiplexed with (except for header change) ADX audio.
  9.  
  10. Multiplexed means that video and audio were separated into blocks and mixed
  11. together into a single file, what is used in practically every movie format. The
  12. multiplex system of SFD is the same used in MPEG 1 (and 2?) movies.
  13.  
  14. Thus to demultiplex a movie means to get the movie file and reconstruct the audio
  15. and video in separated files again.
  16.  
  17. Note that this guide has much more information than you need, so it's up to you to
  18. filter it.
  19.  
  20. There are examples at the end of this document, so check them if anything gets too
  21. obscure.
  22.  
  23. About the programs
  24. - The programs will have indication about where to get them when they're
  25. mentioned for the first time.
  26. - Katana SDK means that the program is included as tool in Katana SDK (lol?).
  27. Nowadays you can get it in a lot of places (including snesorama). I also included a
  28. copy on this pack.
  29. - AutoIt Scripts need AutoIt Script (lol?) to run. You can get the interpreter here:
  30. http://www.autoitscript.com/. If I provide a script, just save it with .au3 extension
  31. and double-click it (if your instalation has associated the .au3 extension to run).
  32.  
  33. Step 1: Demultiplexing a SFD
  34. Any MPEG demultiplexer should be able to handle SFD files.
  35.  
  36. You can go with TMPGEnc (there is a free version in
  37. http://www.tmpgenc.net/en/download.html). Select File -> MPEG Tools... -> De-
  38. multiplex tab (Simple De-multiplex won't work with SFD) -> Browse. Select your SFD
  39. file. There should be an audio_stream and a video_stream (ignore the others).
  40. Double-click each one of streams and save them whenever you want. Use .sfa
  41. extension for audio and .mpv extension for video.
  42.  
  43. 1
  44.  
  45. ----------------------- Page 2-----------------------
  46.  
  47. You can also use demux.exe (it's somewhere around this pack). I like this one
  48. because it's command line tool, what makes batch demultiplexing very easy. Use
  49. this syntax:
  50.  
  51. demux.exe video.sfd output
  52.  
  53. It should create "output_c0.m2v" (rename it to .mpv) and "output_e0.m2a"
  54. (rename it to .sfa).
  55.  
  56. If you want to batch multiplex, put demux.exe and demux.au3 (both can be found
  57. around on this pack) in your SFD directory, execute the script and it will create a
  58. "montar.bat". Execute the batch and it will demultiplex every SFD of a directory.
  59. Very useful, I use it all the time.
  60.  
  61. Intermission: DC Movie Maker - The easy and crap way
  62. DC Movie Maker is the official SFD tool included in the Katana SDK. It can encode
  63. videos and audio (of a variety of formats) and multiplex them into a SFD file.
  64.  
  65. I don't recommend using it for encoding media because it only encodes MPEG using
  66. CBR (constant bitrate). VBR (variable bitrate) offers more quality with reduced size,
  67. because it adapts the bitrate according to the scene you're encoding. As an
  68. example, you'll get a much smaller file encoding a credits video using VBR than using
  69. CBR, because there are a lot of steady areas.
  70.  
  71. Additionally, I believe DC Movie Maker MPEG-1 encoder doesn't have a very good
  72. quality. I haven't tested it too much though.
  73.  
  74. About audio encoding, I have nothing to say about it.
  75.  
  76. DC Movie Maker may be useful for multiplexing audio and video though.
  77.  
  78. If you're thinking about using it, I have some commentaries. Note that if you don't
  79. want to use it you can just skip to the next chapter.
  80.  
  81. 1) If you use a .mpv file as video input, DC Movie Maker will think it's not a ready
  82. MPEG video stream and it will re-encode it (even if it's ready!). Use it if you want to
  83. downsample a video.
  84. 2) If you use a .m1v file as video input, DC Movie Maker will think it's a ready MPEG
  85. video stream and won't change it, it will only multiplex it into a SFD file.
  86. 3) If you use a .sfa file as audio input, DC Movie Maker will understand that it's a
  87. ready audio stream for SFD and won't change it, it will only multiplex it into a SFD
  88.  
  89. 2
  90.  
  91. ----------------------- Page 3-----------------------
  92.  
  93. file.
  94. 4) If you use other format (like .wav) as audio input, DC Movie Maker will encode it
  95. and multiplex it into a SFD file.
  96.  
  97. Step 2: Working with video
  98. To downsample the video, you'll need an encoder. Like I said, you could use DC
  99. Movie Maker, but you'll get very poor results. I recommend two encoders:
  100.  
  101. - TMPGEnc. It's freeware, it supports VBR encoding, and it outputs good results.
  102.  
  103. To encode a video into a lower quality, at the bottom of screen select your
  104. input.mpv (.mpv is not shown as a supported format, but load it anyway). Select "ES
  105. (Video Only)" as Stream Format, and click the Setting button right below it. Select
  106. "2-pass VBR(VBR)" as Rate control mode, and click the Setting button at its side.
  107. Here, adjust the bitrates values (discussion about it will be made later), confirm the
  108. options. Adjust the output file if needed and start the encoding with the giant Start
  109. button at the top. Wait until it ends and you'll have your downsampled video.
  110.  
  111. - Mainconcept MPEG Encoder (developer website: http://www.mainconcept.com).
  112. It's a commercial, very expensive codec. In my option, it presents the best results,
  113. noticeable most in dark scenes. It's also very fast, with native support for special
  114. instructions (SSE2 and above) and multi-core. You can't go wrong wasting all these
  115. hundred dollars.
  116.  
  117. Since only a few people have access to this software, I'm not telling you how to use
  118. it. It's pretty much the same of TMPGEnc, so you shouldn't have problems.
  119.  
  120. Note that batch support in both of these softwares aren't that good if you need to
  121. adjust space and try a lot of different bitrates. I don't have an easy solution to give
  122. you.
  123.  
  124. Step 3 (optional): Downsampling Audio
  125. Before going on with that, note that audio is a very important part of video, and it
  126. only uses a small amount of data compared to video. It means that the general rule
  127. is "don't touch the audio" unless you have a very limited space.
  128.  
  129. For example, imagine a SFD with video encoded in 4800kbps CBR and audio being
  130. 44khz stereo (397kbps). Well, audio in this case uses only ~7.6% of the space in the
  131. file. If you mono the audio (198kbps), you'll lose a lot of quality in audio but your
  132. video will only be ~3,8% smaller.
  133.  
  134. 3
  135.  
  136. ----------------------- Page 4-----------------------
  137.  
  138. Anyway, let's teach you how to work with audio.
  139.  
  140. Decoding .sfa
  141.  
  142. Rename it to .adx, open it with foobar2000 (freeware, download it in
  143. http://www.foobar2000.org/). Right-click the file in playlist, click Convert and use
  144. the appropriate format (WAV).
  145.  
  146. Note: .sfa and .adx have differences in header, but foobar2000 can handle it.
  147.  
  148. Note: If foobar2000 doesn't recognize ADX, you'll have to install the correct plugin
  149. for it (available on foobar2000's website).
  150.  
  151. Note: There are a lot of crap old decoders for ADX, so be sure to use a good one
  152. (like foobar2000). Probably you'll also get good results with ffmpeg (free, pre-
  153. compiled Windows version here: http://ffmpeg.arrozcru.org/autobuilds/).
  154.  
  155. Modifying audio
  156.  
  157. You can convert the decoded audio to mono and/or downsample it to save space. I
  158. like GoldWave (commercial, simple and useful, official site website here:
  159. http://www.goldwave.com/), but you can also try Audacity (free, available here:
  160. http://audacity.sourceforge.net/)
  161.  
  162. I'm not teaching you how to use these programs, you should be able to figure it out
  163. by yourself.
  164.  
  165. Encoding Audio Back
  166.  
  167. Use "adxencd.exe" (Katana SDK) to encode your .wav into a .adx file:
  168.  
  169. adxencd.exe input.wav
  170.  
  171. The parameters (number of channels and frequency) are given by the .wav used as
  172. source.
  173.  
  174. Note: You'll find a tool to batch encode ADX files somew here around this pack.
  175.  
  176. Converting ADX to SFA
  177.  
  178. Use ADXConverter.exe (included in Darkfalz's DC Tools, it can be found somewhere
  179. around this pack) to convert the ADX files to SFA. It has native support for batch
  180.  
  181. 4
  182.  
  183. ----------------------- Page 5-----------------------
  184.  
  185. conversion.
  186.  
  187. Final step: Multiplexing files back to a SFD file
  188.  
  189. You can use DC Movie Maker to multiplex the files. If you want to use it, check its
  190. chapter above.
  191.  
  192. I like using sfdmux.exe (hard to find wrapper for official sfdmux.dll, made by Zench. I
  193. found it here: http://forum.xentax.com/viewtopic.php?f=18&t=3084 but you can
  194. found a copy around this pack. It's called by command line so you can easily batch
  195. multiplex files.
  196.  
  197. Well, at this point, you must have your video .m1v (modified or not) and your audio
  198. .sfa (modified or not). To multiplex then into a SFD use:
  199.  
  200. sfdmux.exe -V=video.m1v -A=audio.sfa -S=output.sfd
  201.  
  202. and you're done.
  203.  
  204. You can use criar.au3 (included around this pack) to automatize the process. It will
  205. create a "montar.bat" to multiplex all the .m1v of a directory, assuming that the .sfa
  206. has the same base name.
  207.  
  208. Extra: About Downsampling and Bitrates
  209.  
  210. The main reason for downsampling SFD is to save space. But there are situations
  211. when you need to downsample movies to make them play right.
  212.  
  213. Videos at the "slowest" part of the disc cannot be higher than ~3600kbps CBR, else
  214. reader won't read it fast enough (GDs are read faster than CDs). This bitrate limit is
  215. higher at the "fastest" area of the disc (I remember testing ~5000kbps CBR without
  216. problem). Some games (e.g: Spawn, Star Wars Jedi Power Battles) have videos
  217. encoded in a very high bitrate (~9000kbps), and there is no way to play it right on a
  218. CD. So they'll need to be downsampled even if you have enough space in the disc.
  219.  
  220. Before choosing your new bitrate, don't forget to check the one of the original
  221. video. You can see it after select a SFD or M1V or MPV at the De-multiplex tab of
  222. TMPGEnc.
  223.  
  224. Now about minimum and maximum bitrates choices. For minimum, you can always
  225. use 0~300. For maximum, just use your good sense. For example, if my average
  226.  
  227. 5
  228.  
  229. ----------------------- Page 6-----------------------
  230.  
  231. bitrate is 3600kbps, I'd choose a maximum bitrate of 4000~4400kbps.
  232.  
  233. Oh, and don't forget that re-encoding always make you lose quality, even if you use
  234. bitrates higher than the original ones.
  235.  
  236. About dimension reducing
  237.  
  238. Resizing your video is a bad idea, unless you need to use an incredible low bitrate.
  239. General advice: don't use it. Video may lose too much quality, it may lose aspect
  240. ratio, the game may not play it right, etc.
  241.  
  242. I've never tried this, at least at the time that this document was written.
  243.  
  244. Math prediction
  245.  
  246. VBR encoding may be a pain in the ass when you want your videos to have a
  247. determined size. Like the name says, it uses a variable bitrate, so the final file size
  248. can't be predicted with precision like you could do with CBR. It will force you to re-
  249. encode your videos a lot of times until you find a bitrate set that gives you the size
  250. that you want.
  251.  
  252. If you use a too high bitrate, it won't fit on a CD. If you use a too low bitrate, quality
  253. will be worse than it could be. What to do then?
  254.  
  255. Imagine that you need a .m1v video to have 20mb. You use an average bitrate of
  256. 1000kbps and get a result of 10mb. Well, using the good sense, if you double the
  257. average bitrate (1000kbps -> 2000kbps) you should also double the video size (10mb
  258. -> 20mb) and solve your problem. Result won't be exactly the double because of
  259. VBR variations, but the double bitrate is your best next shot to try. But what if your
  260. numbers are too complicated? And you actually want to have the size of the SFD
  261. file, not of the single video stream. So how to consider all that to find your next
  262. bitrate try? You can manually guess the bitrates, but believe me, it will take dozens
  263. of encoding.
  264.  
  265. Well, this chapter will show you how to estimate the next bitrate try based on your
  266. last try, so you'll reduce the number of times you need to encode until find the right
  267. size. Don't worry if you don't fully understand it now, it will be clearer in third
  268. example at the final of this document.
  269.  
  270. 6
  271.  
  272. ----------------------- Page 7-----------------------
  273.  
  274. Facts:
  275.  
  276. 1) The space your final video must have is T.
  277. 2) The size of the SFD of the last encoding with bitrate B* is S*.
  278. 3) The size of your .sfa is A.
  279. 4) The next bitrate you need to try is B (that's what you want to estimate).
  280. 5) When multiplexing a file, the final size will be ~1.0157 * (Size of video + Size of
  281. Audio). This is an experimental value and it works very fine.
  282.  
  283. (S*/1.0157) - A = Size of video stream (should be obvious if you think a little).
  284.  
  285. Using simple proportion:
  286.  
  287. B* / B = [(S*/1.0157) - A] / [(T/1.0157) - A]
  288.  
  289. And with some algebra we get:
  290.  
  291. B = B*(T - kA)
  292. ----------
  293. S* - kA
  294.  
  295. where:
  296.  
  297. k = 1.0157
  298. T = Size that the video must have.
  299. A = .sfa (audio stream) file size.
  300. S* = Size obtained on the last encoding.
  301. B* = Bitrate used on the last encoding.
  302.  
  303. As I told you, things will get clearer at the correct example later.
  304.  
  305. Note that you can use the formula for a bunch of files, just replace the parameters
  306. for the sum of them (except for bitrate of course). Obviously, you'll have to use the
  307. same bitrate for all the videos in order to do that.
  308.  
  309. Example 1: Downsampling Sonic Adventure Opening without change the audio
  310.  
  311. File information:
  312.  
  313. SAH448_6.SFD
  314. 49491968 bytes
  315. Video bitrate: 3200kbps CBR
  316.  
  317. 7
  318.  
  319. ----------------------- Page 8-----------------------
  320.  
  321. Work Directory: H:\Tutorial SFD\
  322.  
  323. Plan: Downsample it to an 2000kbps (average) bitrate using VBR.
  324.  
  325. 1)
  326.  
  327. demux.exe SAH448_6.SFD lol
  328. rename lol_e0.m2v SAH448_6.MPV
  329. rename lol_c0.m2a SAH448_6.SFA
  330.  
  331. Result:
  332.  
  333. SAH448_6.MPV - 43461666 bytes
  334. SAH448_6.SFA - 5299268 bytes
  335.  
  336. 2)
  337.  
  338. In TMPGEnc:
  339.  
  340. - "H:\Tutorial SFD\SAH448_6.MPV" as video source.
  341. - "H:\Tutorial SFD\SAH448_6.m1v" as output filename.
  342. - Select "ES (Video Only)".
  343. - Setting -> Select "2-pass VBR(VBR)" -> Setting.
  344. - Average bitrate = 2000kbits/sec; Maximum bitrate = 2600kbits/sec; Minimum
  345. bitrate = 0.
  346. - OK -> OK -> Start.
  347.  
  348. Result:
  349.  
  350. SAH448_6.m1v - 27126950 bytes.
  351.  
  352. 3)
  353.  
  354. sfdmux.exe -V=SAH448_6.m1v -A=SAH448_6.sfa -S=SAH448_
  355. 6.SFD
  356.  
  357. Result:
  358.  
  359. SAH448_6.SFD - 33144832 bytes (~33% smaller).
  360.  
  361. You can test it, it will play correctly in the game.
  362.  
  363. 8
  364.  
  365. ----------------------- Page 9-----------------------
  366.  
  367. Example 2: Downsampling Sonic Adventure Opening, but now monoizing the
  368. audio
  369.  
  370. Same file information and working directory.
  371.  
  372. Plan: Downsample the video to an 2000kbps (average) bitrate using VBR.Make audio
  373. mono.
  374.  
  375. Steps 1 and 2 are exactly the same of the last example.
  376.  
  377. 3) rename SAH448_6.SFA SAH448_6.ADX
  378.  
  379. 4) Open SAH448_6.ADX on foobar2000 (it should play). Right-click playlist entry ->
  380. Convert -> ... -> Output format = wAV -> OK.
  381.  
  382. 5) Open SAH448_6.WAV in GoldWave. File -> Save As -> Select PCM signed 16bit,
  383. mono in Attributes -> Save (overwrite previous .wav).
  384.  
  385. 6) adxencd.exe SAH448_6.wav
  386.  
  387. Result:
  388.  
  389. SAH448_6.adx - 2649294 bytes
  390.  
  391. 7) Open ADXConverter.exe. "H:\tutorial SFD\" in the main field -> Mark ADX to SFA
  392. -> Mark Delete ADX -> Convert.
  393.  
  394. Result:
  395.  
  396. SAH448_6.sfa - 2649546 bytes
  397.  
  398. 8) Do step 3 of last example.
  399.  
  400. Result:
  401.  
  402. SAH448_6.SFD - 30455808 bytes (~38% smaller).
  403.  
  404. You can test it, it will play correctly in the game.
  405.  
  406. Note: We have monoized the audio. It means that we lost a lot of quality in audio
  407. but gained only ~5% in size compared to the video with the downsampled audio.
  408. That's why you should avoid changing the audio.
  409.  
  410. 9
  411.  
  412. ----------------------- Page 10-----------------------
  413.  
  414. Example 3: Math prediction
  415.  
  416. Note: Everytime I encode the video again, it's clear that I'm using the original video
  417. as source! Don't be dumb and use your previous result as source.
  418.  
  419. Imagine the result of the last example:
  420.  
  421. SAH448_6.SFD - 30455808 bytes
  422. Average video bitrate: 2000kbps.
  423.  
  424. But you actually had 40000000 bytes free, so you can use a higher bitrate to
  425. improve the quality. What bitrate will you use now? Let's use our prediction formula
  426. (in fact my formula, but that's cool):
  427.  
  428. Using:
  429.  
  430. k = 1.0157
  431. B* = 2000
  432. T = 40000000
  433. A = 2649546 (size of the monoized .sfa)
  434. S* = 30455808
  435.  
  436. and applying on the formula, we get B ~ 2688 kbps.
  437.  
  438. Encoding the video now with 0/2688/3000 and multiplexing files we get:
  439.  
  440. SAH448_6.SFD - 39890944 bytes
  441.  
  442. That is incredible close to the result we wanted (only 0.2% smaller). We could stop
  443. here, but let's suppose that the result was not so close as we wanted. We could do
  444. the same process again to find our next bitrate try. Using:
  445.  
  446. k = 1.0157
  447. B* = 2688
  448. T = 40000000
  449. A = 2649546 (size of the monoized .sfa)
  450. S* = 39890944
  451.  
  452. We'll get B ~ 2695. If we encode again using 0/2695/3000 and multiplex we get:
  453.  
  454. SAH448_6.SFD - 39979008 bytes
  455.  
  456. 10
  457.  
  458. ----------------------- Page 11-----------------------
  459.  
  460. Even closer! Cool right?
  461.  
  462. Note that this formula will never converge to the exact result because of VBR
  463. variations. It should however be floating around the exact value.
  464.  
  465. Also note that the maximum bitrate actually affects the final size of file, so you
  466. should maintain it fix once you gets close to the result you want.
  467.  
  468. This formula converges pretty fast and gives you very useful tips when you have a
  469. restricted space for video. Use it!
  470.  
  471. Blue Stinger Anomally
  472.  
  473. This "anomally" also happens in Flag To Flag, and it may happen in other old games
  474. (all Dreamcast games are old now, but you got the idea) that use SFD videos. The old
  475. SFD multiplexed format uses additional header space and you need special attention
  476. to work with them.
  477.  
  478. If you get an error message before demultiplexing it, it's probably the case.
  479.  
  480. Before demultiplexing:
  481.  
  482. Find the first "00 00 01 BA" ocorrence (I think it's always in the 0x2000 offset) using
  483. your favorite Hex Editor (like Free Hex Editor Neo, freeware, available here:
  484. http://www.hhdsoftware.com/Products/home/hex-editor-free.html). Remove
  485. everything above it (yeah, rip them off), and save the changes.
  486.  
  487. Now you should be able to demultiplex the streams without problems.
  488.  
  489. After multiplexing it back:
  490.  
  491. Use SFD-Fixer (included somewhere on this pack) the program should be self
  492. explainable. In the "Original video" field select the original file (lol?), not the one
  493. that you modified in the hex editor.
  494.  
  495. After that the video should play fine in the game.
  496.  
  497. Sonic Adventure 2 Anomally
  498.  
  499. 11
  500.  
  501. ----------------------- Page 12-----------------------
  502.  
  503. Videos in Sonic Adventure 2 have multiple audio streams (Japanese and English
  504. audio). Actually it's the only game I've ever seen with this characteristic.
  505.  
  506. You'll be able to demultiplex the audio fine, just remember that you'll get two audio
  507. streams.
  508.  
  509. To multiplex a SFD with the two audio streams back, use:
  510.  
  511. sfdmux.exe -V=video.m1v -A=audio1.sfa -A=audio2.sfa -
  512. S=output.sfd
  513.  
  514. Note that parameters order in sfdmux.exe matters!
  515.  
  516. Also note that you can't multiplex multiple audio streams using DC Movie Maker.
  517.  
  518. If you don't add the two audio streams, the game will freeze when it tries to play the
  519. video with English audio.
  520.  
  521. Final words
  522.  
  523. Wow, it got longer than expected. I hope it can be useful for someone.
  524.  
  525. Let a comment if you have questions, just be sure to read the full guide before
  526. asking stuff. I don't like to answer questions quoting parts of my own guide.
  527.  
  528. 12
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement