Writ

From XiphWiki
Revision as of 21:55, 25 October 2004 by 60.25.122.54 (talk)
Jump to navigation Jump to search

Introduction

Ogg Writ is a text phrase codec. While its primary purpose is to embed subtitles or captions in a Theora stream, its design makes it useful for many other purposes. It could provide lyrics to song encoded in Vorbis, a transcript to a political debate encoded in Speex, or even incorporate a live chat session as part of a continuous video stream.

One of the unique aspects of Writ is its discontinuous nature, that is, unlike other Ogg codecs the granules for which seperate packets effect may overlap. See the Granules and Muxing section below for how this works.


SVN

Current Ogg Writ development is on Xiph SVN as /trunk/writ/. It's being developed to use libogg2, so you'll need both to work on it. The reference encoder and decoder are available as part of the py-ogg2 package which is available on Xiph SVN as /trunk/py-ogg2/.


This is a (near final) working draft of the spec
Writ has been designed so that encoders/decoders can support a bare minimum and be fully compatable with future subversions. Each subversion adds a new feature, some building on others, adding a new header packet and likely a new field to each body packet.

Decoders should ignore header packets beyond what they were written to support and also ignore extra fields in data packets beyond their current version. This allows new features to be added without requiring that all software, or even most software, to support them.

We will be conservative about adding future subversions.

Header Packet 0 (BOS, 16 bytes):
 0x00                                   ( 8 bit Header 0)
 "writ" (LSB 0x74697277)                (32 bit codec identification)
 version                                ( 8 bit unsigned int, 0 = Alpha)
 subversion                             ( 8 bit unsigned int)
 granulerate_numerator                  (32 bit unsigned int)
 granulerate_denominator                (32 bit unsigned int)

Data Packet (each):
 0xFF                                   ( 8 bit 0xFF = data packet)
 granule_start                          (64 bit signed integer)
 granule_duration                       (32 bit unsigned integer)
 text_length                            ( 8 bit unsigned integer)
 text_string                            (variable-length UTF-8 string)


<B>Subversion 1 adds multiple language support</B>

Header Packet 1 (Language Definition, 8+ bytes) :
 0x01                                   ( 8 bit Header 1)
 "writ" (LSB 0x74697277)                (32 bit codec identification)
 num_languages                          ( 8 bit unsigned int)
 [repeated 1+num_languages times] :
   language_length                      ( 8 bit unsigned int)
   language_string                      (0+language_length rfc3066)
   language_desc_length                 ( 8 bit unsigned int)
   language_desc_string                 (0+language_desc_length UTF-8)

Data Packet (each):
 0xFF                                   ( 8 bit 0xFF = data packet)
 granule_start                          (64 bit signed integer)
 granule_duration                       (32 bit unsigned integer)
 [repeated num_languages times] :
   text_length                          ( 8 bit unsigned integer)
   text_string                          (variable-length UTF-8 string)


<B>Subversion 2 adds text window support</B>

Header Packet 2 (Window Definition, 10+ bytes) :
 0x02                                   ( 8 bit Header 2)
 "writ" (LSB 0x74697277)                (32 bit codec identification)
 location_scale_x                       (16 bit unsigned int)
 location_scale_y                       (16 bit unsigned int)
 num_windows                            ( 8 bit unsigned int)
 [if (window_num > 0) repeated window_num times] :
   location_x                           (variable length, see below)
   location_y                           (variable length, see below)
   location_width                       (variable length, see below)
   location_height                      (variable length, see below)
   alignment_x                          ( 2 bit alignment, see below)
   alignment_y                          ( 2 bit alignment, see below)

Data Packet (each):
 0xFF                                   ( 8 bit 0xFF = data packet)
 granule_start                          (64 bit signed integer)
 granule_duration                       (32 bit unsigned integer)
 [repeated num_languages times] :
   text_length                          ( 8 bit unsigned integer)
   text_string                          (variable-length UTF-8 string)
 [if (window_num > 1)] :
   window_id                            ( 8 bit unsigned integer)


<B>Example Stream</B>
 Header Packet 0
  version 0
  subversion 2
  granulenum 1
  granuledom 1
 \x00writ\x00\x02\x01\x00\x00\x00\x01\x00\x00\x00

 Header Packet 1
  num_languages 2
   Language 0:
    language en
    language_desc English
   Language 1:
    language es
    language_desc Spanish
 \x01writ\x01\x02en\x07English\x02es\x07Spanish

 Header Packet 2
  location_scale_x 4000 (12 bits)
  location_scale_y 270  ( 9 bits)
  num_windows 2
   Window 0:
    location_x 1
    location_y 2
    location_width 3
    location_height 1
    alignment_x 3 (Full)
    alignment_y 3 (Full)
   Window 1:
    location_x 5
    location_y 6
    location_width 7
    location_height 1
    alignment_x 3 (Full)
    alignment_y 3 (Full)
 \x02writ\xa0\x0f\x0e\x01\x02\x01\x20\x60\x00\x02\x7c\x01\x18\x38\x80\x00\x0f

 Phrase Packet:
  granule_start 5
  granule_duration 10
  Language 0: "Hello World!"
  Language 1: "Hola, Mundo!"
  window_id 0
 \xff\x05\x00\x00\x00\x00\x00\x00\x00\x0a\x00\x00\x00\x0cHello World!\x0cHola, Mundo!\x00

 Phrase Packet:
  granule_start 12
  granule_duration 15
  Language 0: "It's a beautiful day to be born."
  Language 1: "Es un día hermoso para que se llevará."
  window_id 1
 \xff\x0c\x00\x00\x00\x00\x00\x00\x00\x0f\x00\x00\x00\x20It's a beautiful day to be born.\x26Es un d\xeda hermoso para que se llevar\xe1.\x01


Granules and Muxing

Granulepos in Writ (as well as future discontinuous codecs) will be by start time, not end time, that the data in a given page is tagged for. This greatly simplifies this specification (see the old method below).

All Writ phrases will be provided at and given the granulepos of their start time, ordered by their start time within the logical bitstream.

Phrase packets with long durations should be repeated in the logical bitstream at regular intervals to ensure that a player seeking to the middle of their duration will still see them. These packet copies will be identical to their original, including the start and duration fields, the granulepos of the page they reside on will be incremented for each copy to place it forward on the logical bitstream.

No two phrases can start on the same granule. On decoding, each packets' start granule is checked against already known packets. If a match is found the new packet is ignored. This prevents phrase copies from being interpreted as new phrases.

Seeking Example


Here is a timeline (granule numbers at top, read down) of a sample stream:

                        <- Granules ->
0000000000111111111122222222223333333333444444444455555555556666666666
0123456789012345678901234567890123456789012345678901234567890123456789
 ___________  ____________  ____________  ____________  _____________
|_Vorbis____||_Vorbis_____||_Vorbis_____||_Vorbis_____||_Vorbis______|
 ____________________   ____________________________________
|_A____________>_____| |_D____________>______________>______|
     _________      ___    __________     ___________
    |_B_______|    |_C_|  |_E________|   |_F_________|

 (note: these have been seperated vertically for easy viewing only)

Packet  Granule Description
 V H0   0       Vorbis Header 0x01 (page by itself)
 W H0   0       Writ Header 0 (page by itself)
 V H1   0       Vorbis Header 0x03
 V H2   0       Vorbis Header 0x05
 W H1   0       Writ Header 1 (Language Defs)
 W H2   0       Writ Header 2 (Window Defs)
 W A    0       Writ Phrase A
 W B    4       Writ Phrase B
 V      12      Vorbis 0-12
 W A    15      Writ Phrase A
 W C    19      Writ Phrase C
 W D    23      Writ Phrase D
 V      26      Vorbis 13-26
 W E    26      Writ Phrase E
 W D    38      Writ Phrase D
 V      40      Vorbis 27-40
 W F    41      Writ Phrase F
 W D    53      Writ Phrase D (EOF)
 V      54      Vorbis 41-54
 V      69      Vorbis 55-69 (EOF)

Player begins decoding at beginning of stream. It reads the BOS pages for both codecs, then receives a non-BOS page. At this point it knows that it has two bitstreams to decode and has resolved that one is Writ and the other Vorbis. It'll continue processing the headers for both.

Next it's going to find two Writ packets (phrases A and B) and toss them into libwrit. Then it'll get to the first Vorbis data page. It now has data from both bitstreams, and it knows (from the granulepos on the Vorbis page) that it has enough data to run until 12. If there were any Writ packets before 12 they would have appeared first.

At around granule 9 the listener seeks forward to 24. This will cause a rapid seek through the file to find the first page with a granulepos greater than the seek position and begin decoding at that point.

It'll find a Vorbis packet containing 13-26 (and not use 13-23) and Writ phrase E. Again, having data from both bitstreams it can begin playing. D would normally appear at granule 24 but is not known about yet. The player knows that this is only enough to decode until 26 so, knowing enough to prebuffer, continues reading the file as it plays the media.

The next packet it finds is Writ phrase D, and passing it to libwrit, is found that the current granulepos is within the duration. It is thus displayed immediatly, as it's prebuffered, without waiting for granulepos 38. It'll keep reading (because the maximum decoded Vorbis is still 26) and find a Vorbis packet with a 40 granulepos.

As it nears 38 it'll read the file again and find Writ phrase F, which takes it out to 41. Vorbis only goes until 40, so it'll have to keep reading until the next Vorbis packet.

Next it'll find Writ phrase D, which will be ignored by libwrit because phrase D is already known (matches start granule of earlier D), and the EOF on that page marks this as the last of the Writ stream.

It'll continue reading for the next Vorbis data and find the packet for granule 54, followed by the Vorbis packet for granule 69. With that it's EOS, EOF, finished.

This is of course a simplistic example, Writ and Vorbis will rarely have granules which equal the same amount of time. Each bitstream has its' own granule -> time mapping which is calculated when muxing concurrent bitstreams within the file. So if there are 44100 Vorbis granules per second and only 4 Writ granules per second, pages would be ordered as W25 V297892 W31 V385932 W39 W41 V463057 etc. The logic used in the above example works after this granule-time mapping is calculated.


Ongoing Discussion

  • How does this get "encoded" and "merged"?
    • <purple_haese> The muxing rule is pages are arranged in ascending order by the timestamp that is represented by their granulepos.
  • For what reason is the 0x00 and 0xFF byte at the beginning of header and data packet respectively?
    • <xiphmont> If, after a seek, I hand your codec a header packet, what does the codec do?
    • <xiphmont> It does *nothing*. If I haven't told it to reset, the header is not data, *it must ignore the header*.
    • <xiphmont> this eliminates a huge raft of special cases in Ogg seeking.


"The Old Way"

The section below is for historical purposes only!

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
2003/08/17
  In a lengthy discussion with Monty and Derf the decidion to change the
  behavior of discontinuous bitstreams in Ogg, or rather, extend the
  current Ogg specification to handle discontinuous codecs, was made.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

The Ogg granulepos of each page is equal to the expiration of the text, packets are ordered by expiration time and may overlap. So, at or before text A is to be displayed, the following sequence is included:

Physical        Text    Text    Text
Location        Packet  Start   Expire  (text expire = page granulepos)
---------------------------------------
00              B       04      14
00              D       19      23
00              C       09      24
00              F       27      34
00              E       26      37
00              G       35      47
00              H       42      54
00              A       00      59
51              I       51      66

So B, D, C, F, E, G, and H are all defined before A, building a FIFO (first in first out) buffer in the player. Encoders should limit the extend of this behavior to reduce nessesary buffer size on the player side by prematurly expiring captions and recreating them periodically.

The screen should not be updated with the new captions until they've all been processed to prevent "flicker". New caption data to the same position will scroll the previous data upwards with no line breaks seperating them (unless present in text). http://www.sec66.com http://www.funasia.cn http://www.funasia.cn http://www.haishun.net http://haishun11.51.net http://fireworks.garrywa.com http://global.garrywa.com/index1.htm http://www.haishun.net http://www.fm360.net http://www.tjwatch.com http://www.conjhost.com http://fireworks.garrywa.com/fireworks/fireworks.htm http://www.feilun.com.cn http://www.3333111.com http://www.3333222.com http://www.3333555.com http://www.3333666.com http://www.3333999.com http://www.5555111.com http://www.5555222.com http://www.5555333.com http://www.5555666.com http://www.3333000.com http://www.5555000.com http://www.5555888.com http://www.5555999.com http://www.6666000.com http://www.6666111.com http://www.6666333.com http://www.6666555.com http://www.7777000.com http://www.7777111.com http://www.7777333.com http://www.7777555.com http://www.7777666.com http://www.7777999.com http://www.8888000.com http://www.8888111.com http://www.8888222.com http://www.8888555.com http://www.8888666.com http://www.8888999.com http://www.9999000.com http://www.9999111.com http://www.9999555.com http://www.9999333.com http://www.9999666.com http://www.9999888.com http://www.1110000.com http://www.2220000.com http://www.3330000.com http://www.5550000.com http://www.6660000.com http://www.8880000.com http://www.1030000.com http://www.1060000.com http://www.1080000.com http://www.1180000.com http://www.1380000.com http://www.1680000.com http://www.1580000.com http://www.1780000.com http://www.1980000.com http://www.3110000.com http://www.3220000.com http://www.3550000.com http://www.3560000.com http://www.3160000.com http://www.3180000.com http://www.3260000.com http://www.3580000.com http://www.3660000.com http://www.3380000.com http://www.3880000.com http://www.5110000.com http://www.5220000.com http://www.5330000.com http://www.5660000.com http://www.5880000.com http://www.5160000.com http://www.5180000.com http://www.5680000.com http://www.5260000.com http://www.5280000.com http://www.5360000.com http://www.5380000.com http://www.6110000.com http://www.6220000.com http://www.6330000.com http://www.6550000.com http://www.6770000.com http://www.6780000.com http://www.6880000.com http://www.3690000.com http://www.2580000.com http://www.1470000.com http://www.6160000.com http://www.6260000.com http://www.6280000.com http://www.6360000.com http://www.6380000.com http://www.6560000.com http://www.6580000.com http://www.6390000.com http://www.6610000.com http://www.6630000.com http://www.6650000.com http://www.6680000.com http://www.6690000.com http://www.3450000.com http://www.4560000.com http://www.7890000.com http://www.8660000.com http://www.88810000.com http://www.88820000.com http://www.88830000.com http://www.88850000.com http://www.88870000.com http://www.88890000.com http://www.88818888.com http://www.88828888.com http://www.88838888.com http://www.88858888.com http://www.88868888.com http://www.88878888.com http://www.88898888.com http://www.88188888.com http://www.88288888.com http://www.88388888.com http://www.88588888.com http://www.88688888.com http://www.88788888.com http://www.88988888.com http://www.8188888.com http://www.8288888.com http://www.8388888.com http://www.8988888.com http://www.81888888.com http://www.82888888.com http://www.83888888.com http://www.84888888.com http://www.85888888.com http://www.86888888.com http://www.87888888.com http://www.89888888.com http://www.88881888.com http://www.88882888.com http://www.88883888.com http://www.88885888.com http://www.88886888.com http://www.88887888.com http://www.88889888.com http://www.8888818.com http://www.8888858.com http://www.8888878.com http://www.8888898.com http://www.88888828.com http://www.88888838.com http://www.88888858.com http://www.88888868.com http://www.88888878.com http://www.88888898.com http://www.13188888888.com http://www.11338888.com http://www.11558888.com http://www.11668888.com http://www.11778888.com http://www.11888888.com http://www.11998888.com http://www.22118888.com http://www.22338888.com http://www.22558888.com http://www.22668888.com http://www.22778888.com http://www.22888888.com http://www.22998888.com http://www.33118888.com http://www.33558888.com http://www.33668888.com http://www.33778888.com http://www.33888888.com http://www.33998888.com http://ww.55118888.com http://www.55338888.com http://www.55668888.com http://www.55778888.com http://www.55888888.com http://www.55998888.com http://www.66118888.com http://www.66228888.com http://www.66338888.com http://www.66558888.com http://www.66888888.com http://www.66998888.com http://www.77118888.com http://www.77338888.com http://www.77558888.com http://www.77668888.com http://www.77888888.com http://www.77998888.com http://www.88118888.com http://www.88228888.com http://www.88338888.com http://www.88558888.com http://www.88668888.com http://www.88778888.com http://www.88998888.com http://www.99118888.com http://www.99338888.com http://www.99558888.com http://www.99668888.com http://www.99778888.com http://www.99888888.com http://www.conjhost.com

http://www.108888.com http://www.208888.com http://www.308888.com http://www.408888.com http://www.508888.com http://www.708888.com http://www.300888.com http://www.500888.com http://www.700888.com http://www.900888.com http://www.106666.com http://www.206666.com http://www.1006666.com http://www.2006666.com http://www.306666.com http://www.3006666.com http://www.506666.com http://www.606666.com http://www.5006666.com http://www.6006666.com http://www.706666.com http://www.7006666.com http://www.806666.com http://www.8006666.com http://www.906666.com http://www.9006666.com http://www.0001111.com http://www.00001111.com http://www.0003333.com http://www.00005555.com http://www.0005555.com http://www.0006666.com http://www.00007777.com http://www.0008888.com http://www.1236666.com http://www.3456666.com http://www.13088888888.com http://www.13288888888.com http://www.13588888888.com http://www.13688888888.com http://www.13788888888.com http://www.13888888888.com http://www.02088888888.com http://www.02288888888.com http://www.02188888888.com http://www.02388888888.com http://www.02588888888.com http://www.02788888888.com http://www.3698888.com http://www.1478888.com http://www.888777888.com http://www.8899888888.com http://www.8877888888.com http://www.8866888888.com http://www.8855888888.com http://www.8833888888.com http://www.8822888888.com http://www.8811888888.com http://www.7799888888.com http://www.7788888888.com http://www.7766888888.com http://www.7755888888.com http://www.7733888888.com http://www.7722888888.com http://www.6666888888.com http://www.6655888888.com http://www.6633888888.com http://www.6622888888.com http://www.6611888888.com http://www.5599888888.com http://www.5588888888.com http://www.5577888888.com http://www.5566888888.com http://www.5555888888.com http://www.5533888888.com http://www.5522888888.com http://www.5511888888.com http://www.3399888888.com http://www.3377888888.com http://www.3366888888.com http://www.3311888888.com http://www.2299888888.com http://www.2277888888.com http://www.2266888888.com http://www.2255888888.com http://www.2233888888.com http://www.3355888888.com http://www.3333888888.com http://www.3322888888.com http://www.2211888888.com http://www.1199888888.com http://www.1188888888.com http://www.1177888888.com http://www.1166888888.com http://www.1155888888.com http://www.1133888888.com http://www.1122888888.com http://www.999999888.com http://www.999777888.com http://www.999666999.com http://www.999555888.com http://www.999333888.com http://www.999222888.com http://www.999111888.com http://www.888888999.com http://www.888999888.com http://www.888555888.com http://www.888333888.com http://www.888222888.com http://www.888111888.com http://www.777777888.com http://www.777999888.com http://www.777666888.com http://www.777555888.com http://www.777333888.com http://www.777222888.com http://www.777111888.com http://www.666999888.com http://www.666666888.com ttp://www.666333888.com http://www.666222888.com http://www.666111888.com http://www.555999888.com http://www.555777888.com http://www.555555888.com http://www.555333888.com http://www.555222888.com http://www.555111888.com http://www.1111166666.com http://www.1111199999.com http://www.1111188888.com http://www.2222228888.com http://www.2221118888.com http://www.1119998888.com http://www.1118888888.com http://www.1117778888.com http://www.1116668888.com http://www.1115558888.com http://www.1113338888.com http://www.1112228888.com http://www.111166666.com http://www.111155555.com http://www.111133333.com http://www.111122222.com http://www.333888888.com http://www.333777888.com http://www.333555888.com http://www.333333888.com http://www.333111888.com http://www.3331118888.com http://www.2229998888.com http://www.2228888888.com http://www.2226668888.com http://www.2225558888.com http://www.1111133333.com http://www.999998888.com http://www.777778888.com http://www.666668888.com http://www.555558888.com http://www.333338888.com http://www.222228888.com http://www.111118888.com