Bink2: Intra Block — Chroma

January 18th, 2014

Chroma block coding is similar to luma but with some changes since there are only 4 blocks coded here.

Thus, CBP is coded as two nibbles (real CBP and VLC switch) and it does not try to reuse nibbles from last CBP in code.

There are only 4 DCs here but they are decoded the same way.

AC block decoding is completely the same.

Bink2: Intra Block — Luma

January 18th, 2014

Intra luma block in Bink2 contains the following elements: CBP, quantiser, DCs and ACs.

CBP is coded as 32-bit bitmask depending on the previous CBP value. Internally top half is coded depending on bottom one and the whole bitmask is coded in nibbles starting from LSB.
Lower half decoding depends on the control bits:

  • 11 — simply return last CBP
  • 10 — use low 16 bit from last CBP
  • 0 — decode 4 low nibbles of CBP. Initial nibble value is set to (last_cbp >> 4) & 0xF, if the next bit is 1 then don’t change it, otherwise read new value from the bitstream (4 bits of course).

Now we can use these low 16 bits of CBP to restore high 16 bits. This is also done by nibbles and decoding depends on them (why nibbles? Because blocks are coded in groups of four).


pat4 = (last_cbp >> 20) & 0xF;
ref = cbp;
for (i = 0; i < 4; i++) {   if (!ones_count[ref & 0xF]) {    pat4 = 0;   } else if (ones_count[ref & 0xF] || getbit()) {    pat4 = 0;    for (bit = 1; bit < 0x10; bit <<= 1)     if ((ref & bit) && getbit())      pat4 |= bit;   } else {    pat4 &= ref & 0xF;   }   cbp |= pat4 << 16 + i * 4;   ref >>= 4
}

Essentially it decides what set bits to copy from the first part. And top 16 bits are not really a coded block pattern, it just tells decoder to use an alternative set of VLC codes in AC decoding.

Quantiser is coded with static VLC (plus sign bit for nonzero value) as a difference to the previous quantiser.

Quantisation table for DC: 4 4 4 4 4 6 7 8 10 12 16 24 32 48 64 128

16 DCs (coded with the same scheme as motion vector described in the previous post)

16 blocks of AC coefficients coded in groups of four. Each AC block is coded as (value, skip) pair where value is coded with static VLC that gives small levels (0-3) or number of bits for raw value to read. Skips have one peculiarity too: value coded with static VLC defines either skip (for values 0-10), escape value (when you got you need to read 6 bits with real skip value), end of block value and that the following 8 AC coefficients won’t have skip values coded after them.

Scan order is strange, here are first 8 indices from it: 0, 2, 1, 8, 9, 17, 10, 16, 24, 3, 18, 25, 32, 11, 33

Bink2: Frame structure

January 16th, 2014

Frames consist of ordinary macroblocks in 420 format with optional alpha. Bitstream is packed into 32-bit little-endian words.

Every macroblock is prefixed by 2-bit code determining its type.

Type 0 (intra block). Decode intra block.

Type 1 (skip block). Simply skip block.

Type 2 (motion block). Decode motion vectors and copy block with ½-pel precision for luma and alpha and ¼-pel precision for chroma. There is no residue coding.

Type 3 (inter block). Decode motion vectors and copy block with ½-pel precision for luma and alpha and ¼-pel precision for chroma. Now decode and add residue.

Motion vector differences are coded this way: motion vector element size in bits (3 bits, if read value is 7 then read additional 2 bits, so total is up to 7+3=10 bits per MV element); four motion vector elements; sign bits for non-zero motion vector elements.

Side note: looks like this codec employs this scheme (bit size in 3+2 scheme, fixed-size values, signs for nonzero values) for other elements too, e.g. DCs.

Luma hpel filter looks something like (A - 4*B + 19*C - 4*D + E + 1) >> 5
Chroma qpel: ¼ — (6*A + 2*B + 1) >> 3, ½ — (A + B + 1) >> 1

Next: intra macroblock decoding — luma.

Bink2: RE Notes

January 16th, 2014

Since I obviously have nothing better to do with my time I looked at Bink2 again. In the (hopefully) following blog posts I’ll try to document bitstream format; general codec design was presented much earlier.

At least container format remains the same (except that it uses e.g. KB2f or KB2g instead of BIKi).

My Stomach’s Guide to Sweden

January 15th, 2014

Sweden is awesome country and its food is plain but decent and heart-warming. And everything gets even better in late December.

There’s no Christmas in Sweden, they have Jul — it’s less Christian in nature but significantly better. As an Ukrainian I totally approve Swedish version because the national Ukrainian animal and products from it are well celebrated.

IMG_3397Yes, that one

Sweden has really good dairy products (I’d especially recommend cheeses and filmjölk), surprisingly good selection of meat (including horse, deer, reindeer and elk), outstanding herring (really, nothing beats fried fresh strömming) and other fish products. And oh so many variations of candies everywhere. And drinks.

IMG_2395Berries

In jul it all gets even better since special food appears — traditional julskinka, sylta (and especially julpressylta), marzipan pigs, special versions of prästost and other cheese… And of course julmust.

One can try traditional game of mine — try different variations of everything. For example, drinks (Trocadero, julmust/påskmust or filmjölk), cheese or even köttbullar (I’ve finally found köttbullar made from deer meat for example). Or at least look at designs of marzipan pigs, every konditori makes their own.

Fun fact — in Gävle they like unofficial symbol of the city so much that they have marzipan goats along with pigs.

And another fun fact — there is a drink from Norrland called Portello and it has unlicensed clones. The real Portello is produced by The Norrland Brewery (aka Vasa Bryggeri) but some other breweries have drinks with similar taste but different name. Guttsta Källa produces Ortello and Mora Bryggeri produces Candelo, though it’s a trollish brewery that makes Rio Cola with familiar Cuba Cola design, too lazy to produce Julmust and Påskmust (see picture) and less popular drinks like Guldus or Haiwa are all renamed too.

IMG_3414Mora Bryggeri — no julmust or påskmust

IMG_4016The Reference Drinks

I’ll continue exploring the wonderful world of Swedish food at every possible occasion.

And some fun things to try:

  • julmust with semlor (they are always available in Norrland, it seems);
  • fried gravad lax;
  • filmjölk with berries (I mix lingon, blueberries and raspberries — they are available frozen all year round);
  • julskinka and Wastgöta Kloster cheese on tunnbröd;
  • Swedish apples;
  • chewing candies;
  • and of course Trocadero from any brewery in Norrland!

Internetless

December 6th, 2013

I still don’t have Internet connection at home (for two months and counting) and not sure if I get any this year.

So here’s the list of rants I’d have written, had I had access:

  • SD cards as modern floppies — similarly ubiquitous and slow;
  • Swiss cheeses — some are OK, most are slightly sticky, coated in something very smelly and named “chas”;
  • WMV9 pre-RTM P-frames decoding: how it should be decoded and why our decoder is still wrong sometimes;
  • Bink2 decoder progress report (since I’ve not started working on it yet).

Do not stay tuned.

On Some Smaller Railway Details

September 12th, 2013

When I visited VDD13 (they’ve finally made it right — with Trocadero and surströmming, hopefully they’ll keep the level in the future) I could make some additional observations on the rail that finally lead to this post.

First, I like to talk about catenary constructions. They usually come in two variations — a single post with a special support for the wires or two posts with a horizontal construction between them that supports the wires.
It might be hard to believe but they can be æsthetically appealing too.

The top on my list is Sweden (and Netherlands since they seem to employ the same construction). The poles are made from lattice and thus are nice and horizontal supporting constructions are always made as trapezoids.

Runner-up is Switzerland — they also have nice lattice constructions but they seem a bit compressed to me.

Germany has the same poles but for wider ranges they usually have only a wire between poles from which the wire-supporting constructions hang.

Most of the other contries have simple round masts or H-shaped beams that are not interesting, though I must admit French ones have nice wire support constructions reminding of violin bow.

And on the very bottom of the list is Denmark with its large ugly ?-shaped masts in the colour of rust.

And now to the second thing — toilets. I usually try to avoid them but sometimes I feel I have to visit one onboard. So here’s a comparison of that essential thing.

Ukraine — toilets on so-called “InterCity+” should not be that bad, toilets in older carriages are better not be visited at all (and since they dump contents onto tracks directly, toilet rooms are locked long before stations and after them).

Germany — on InterCity trains toilets are decent (or at least tolerable), on ICE they are too small even in the first class. Even on regional trains and trams it seems to be bigger.

France — on TGV first class they are even smaller that on ICE, in the second class even a person smaller than me has problems fitting inside.

Sweden — those people really care. The spaciest toilet rooms on trains I can remember (especially on Reginatåg).

Switzerland — the toilet on Rhätische Bahn regional trains seemed quite good even if those are narrow-gauge trains.

Moral of the story — Swedish trains and railways are the best (and if you have doubts you’re reading the wrong blog).

Voxware Codecs and Tags

August 10th, 2013

If you look at the registry of WAV formats you can see this:


0x0069 WAVE_FORMAT_VOXWARE_BYTE_ALIGNED Voxware, Inc.
0x0070 WAVE_FORMAT_VOXWARE_AC8 Voxware, Inc.
0x0071 WAVE_FORMAT_VOXWARE_AC10 Voxware, Inc.
0x0072 WAVE_FORMAT_VOXWARE_AC16 Voxware, Inc.
0x0073 WAVE_FORMAT_VOXWARE_AC20 Voxware, Inc.
0x0074 WAVE_FORMAT_VOXWARE_RT24 Voxware, Inc.
0x0075 WAVE_FORMAT_VOXWARE_RT29 Voxware, Inc.
0x0076 WAVE_FORMAT_VOXWARE_RT29HW Voxware, Inc.
0x0077 WAVE_FORMAT_VOXWARE_VR12 Voxware, Inc.
0x0078 WAVE_FORMAT_VOXWARE_VR18 Voxware, Inc.
0x0079 WAVE_FORMAT_VOXWARE_TQ40 Voxware, Inc.
0x007A WAVE_FORMAT_VOXWARE_SC3 Voxware, Inc.
0x007B WAVE_FORMAT_VOXWARE_SC3 Voxware, Inc.
0x0081 WAVE_FORMAT_VOXWARE_TQ60 Voxware, Inc.

In reality there’s one codec with several variations (MetaSound) and a family of low-bitrate MetaVoice codecs. And it doesn’t really matter what ID you’ll use — codec extradata contains real tag used to distinguish one codec from another. That’s why we can have 0x0075 format reserved for Voxware RT29 speech codec but used by MetaSound instead.

Here’s the list of internal tags:

  • VOXa — MetaVoice RT24, 8 kHz, mono, 2.4kbps
  • VOXb — MetaVoice VR12, 8 kHz, mono, 1.2kbps (variable bitrate)
  • VOXc — MetaVoice VR15, 8 kHz, mono, 2.4kbps (variable bitrate)
  • VOXg — MetaVoice RT29HQ, 8 kHz, mono, 2.98kbps (called high-quality for some reason)
  • VOXh — MetaVoice RT28, 8 kHz, mono, 2.8kbps
  • VOXi — MetaSound AC08, 8 kHz, mono, 8kbps
  • VOXj — MetaSound AC10, 11 kHz, mono, 10kbps
  • VOXk — MetaSound AC16, 16 kHz, mono, 16kbps
  • VOXL — MetaSound AC24, 22 kHz, mono, 24kbps
  • VOXq-VOXz — MetaSound mono and stereo, various formats
  • VX01 — MetaVoice SC3, 8 kHz, mono, 3.2kbps (embedded)
  • VX02 — MetaVoice SC6, 8 kHz, mono, 6.4kbps (embedded)
  • VX03 — MetaSound, 8 kHz, mono, 6kbps
  • VX04 — MetaSound, 8 kHz, stereo, 12kbps

So, maybe RT29 does not exist and it should be RT28 instead; obviously RT29HW is a typo for RT29HQ and the second SC3 should be SC6 in the registry (and unfortunately there’s no information about TQ40/TQ60). But who is going to correct WAVE formats list because of facts?

P.S. It would be nice to receive samples for all MetaSound modes (encoder is still available and should work on older Windows systems).

A Quest Continues

June 28th, 2013

Well, after some distraction as writing semi-working On2 AVC decoder (it turned out that On2 has introduced some special modes there that differ only on signal reconstruction stage, too lazy to RE them) and recovering after heat wave I’ve returned to the VoxWare ElenrilSound decoder.

I hate parametric codecs — no matter how you screw calculations you’ll still get some output but it won’t be useful for debugging. At least I can use MPlayer2 + binary codec loader + gdb combination to extract runtime information from the reference decoder.

Now I’m trying to make at least one mode work properly, 16kHz@16kbps mono (aka VOXk) for now. Stereo reconstruction might be trickier so I’ll leave it for later but at least most modes differ only by the tables they use. So (in theory) I’ll need to make at least this mode work, add tables for other modes, fix stereo decoding, look at 8kHz@6kbps mode, curse and forget about it.

Good news — bit allocation works properly and bits are read exactly as in the reference decoder. Bad news — reconstructed output is not even close to the expected one, so the work continues…

How I imagine a perfect computer (for me)

June 8th, 2013

Of course this interests nobody but I wanted to rant about it for a long time.

General principles:

  1. Compact size — I like to be able to fit all of my computers on the desk, any size comparable with power supply unit size would do. Laptops are fine too.
  2. Silent — no damned fans.
  3. An ability to use normal storage, not 16MB SSB soldered onboard.
  4. No x86 CPU.
  5. If it’s a laptop it should be able to work for 10 hours with battery.

Display:

  1. 4:3 aspect ratio. If displays nowadays are made for movie-watchers then it’s a sad world. Too much of vertical space is eaten by various toolbars, menu bars and such.
  2. sane resolution. Again, 1920×1080 may be ideal for movie-watchers but I prefer it to be either VGA-based (i.e. multiple of 640×480 or 800×600) or power of two based. And whoever thought about 1366×768 should burn in hell!

Performance — if Libav compiles in ten minutes on dual core system then it’s fast enough for me.

ARM-based laptops are almost good for that, especially performance wise. There’s just one big “but” — they are almost all are for Android or chromebooks. And Baidu has never intended those systems for any real usage. Playing games — fine, browsing — passable (though Firefox 3 on my old PowerPC MacMini with 512 MB RAM gives much better experience than Chrome on tablet with 1GB RAM), editing texts (code) — absolute fail. I can live without a numpad on keyboard (it’s a legacy for accountants and their calculators after all) but not having even “delete” key (there’s only backspace) is pathetic.

So I live with a faint hope that there will be a computer good enough for me.