Scripting in DVD Studio, Part 5: Aspect Ratios and Advanced-ish Math
This time, we'll write a script to detect whether the DVD player is showing in widescreen (16:9 anamorphic) mode or standard (4:3) mode - and automatically play the video in the appropriate format.
I hope that this tutorial will be a practical one for those of you who are putting both regular NTSC-DV content and anamorphic on the same disc, but more importantly I hope that the introduction to bit math might be useful if you need to extract other characteristics from the bitwise SPRMs, like SPRM14 (Video Config), SPRM15 (Audio Config), and SPRM11 (Karaoke).
You'll have to excuse me for getting all technical up in here; "bitwise" just means that the single SPRM stores a lot of pieces of information in one number. Read on for what I promise will be a painless introduction ...
Before we do anything with bitwise math, we need to think quickly about how binary numbers work. The details aren't important - just think about it this way. Deep breath:
| 1. Position | 16 | 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 2. Binary Number | 0 | 0 | 0 | 1 | 0 | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 0 | 0 | 1 | 1 | |
| 3. (multiply by) Value | 32768 | 16384 | 8192 | 4096 | 2048 | 1024 | 512 | 256 | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 | |
| 4. (=) Calculated Value | 0+ | 0+ | 0+ | 4096+ | 0+ | 0+ | 512+ | 256+ | 128+ | 64+ | 32+ | 16+ | 0+ | 0+ | 2+ | 1 | =5105 |
So Row #2 is our binary number. It's just like computer-speak in the movies: it's a series of ones and zeros. Now, for most of our SPRMs (DVD system variables), we're interested in the "real" number that those ones and zeros add up to (Row #4). But for bitwise SPRMs, each individual position in the binary number represents whether something is true or false.
For example, when you look up SPRM 14 (Video Configuration) in the DVD Studio Pro manual, it says that Bit 8 corresponds to Pan-And-Scan mode - so when the DVD player is in pan-and-scan mode, Bit 8 will be set (have a value of 1). For our purposes, we want to know whether the DVD player's aspect ratio is in 4:3 mode or 16:9 mode. The manual tells us that, in 4x3 mode, both Bit 10 and Bit 11 of SPRM 14 are unset (0) - while in 16x9 mode, Bit 10 and Bit 11 of SPRM 14 are both set.
Now remember - just like in the example above, there are a lot of other bits in SPRM 14, and each of them changes the calculated value (line 4) that we're dealing with in DVD Studio. So if we only want to measure Bit 10 and Bit 11, we need to reset all of the other bits. The easiest way to do this is called a bitwise AND operation - essentially, it takes two binary numbers, and multiplies them together at the individual ones-and-zeros level, like this:
| 1. Position | 16 | 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 2. Binary 1 (pretend it's SPRM14) | 0 | 0 | 0 | 1 | 0 | 1 | 1 | 1 | 0 | 1 | 1 | 1 | 0 | 0 | 1 | 1 |
| 3. (AND/multiply) Binary 2 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 4. (=) Calculated Binary | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
See how the AND filtered out everything but the values in bits 10 and 11? Now, in order to test whether bits 10 and 11 are set, we just see if this result has the same numeric value as a binary number with only bits 10 and 11 - which you can calculate like we did in the first example above. Turns out that value is (1024 + 2048) = 3072 ... which is exactly the same as the binary we use to "AND" with in Line 3 above.
Now, as to coding up the example in DVD Studio: Since you've had some practice with coding scripts, let me show you the finished "Test Aspect Ratio" script, which you would simply set as a pre-script to the 4x3 version of your content:

...Stumped? Here are the Script Inspector settings for each step:

This will be the end of my DVD scripting series for the time being - but if you have any questions, or you're interested in other DVD scripting tutorials, please do contact us!
Listed below are 0 links to blogs that reference this entry: Scripting in DVD Studio, Part 5: Aspect Ratios and Advanced-ish Math.
TrackBack URL for this entry: http://www.geniusdv.com/weblog/mt-tb.cgi/871

