Theora Specification
Xiph.org Foundation
November 3, 2008
Contents
1 Introduction 1
1.1 VP3 and Theora . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 Video Formats . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.3 Classification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.4 Assumptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.5 Codec Setup and Probability Model . . . . . . . . . . . . . . . . 2
1.6 Format Conformance . . . . . . . . . . . . . . . . . . . . . . . . . 3
2 Coded Video Structure 5
2.1 Frame Layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.2 Picture Region . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.3 Blocks and Super Blocks . . . . . . . . . . . . . . . . . . . . . . . 7
2.4 Macro Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.5 Coding Modes and Prediction . . . . . . . . . . . . . . . . . . . . 11
2.6 DCT Coefficients . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
3 Decoding Overview 13
3.1 Decoder Configuration . . . . . . . . . . . . . . . . . . . . . . . . 13
3.1.1 Global Configuration . . . . . . . . . . . . . . . . . . . . . 13
3.1.2 Quantization Matrices . . . . . . . . . . . . . . . . . . . . 13
3.1.3 Huffman Codebooks . . . . . . . . . . . . . . . . . . . . . 14
3.2 High-Level Decode Process . . . . . . . . . . . . . . . . . . . . . 15
3.2.1 Decoder Setup . . . . . . . . . . . . . . . . . . . . . . . . 15
3.2.2 Decode Procedure . . . . . . . . . . . . . . . . . . . . . . 15
4 Video Formats 21
4.1 Color Space Conventions . . . . . . . . . . . . . . . . . . . . . . . 21
4.2 Color Space Conversions and Parameters . . . . . . . . . . . . . 22
4.3 Available Color Spaces . . . . . . . . . . . . . . . . . . . . . . . . 24
4.3.1 Rec. 470M (Rec. ITU-R BT.470-6 System M/NTSC with
Rec. ITU-R BT.601-5) . . . . . . . . . . . . . . . . . . . . 24
4.3.2 Rec. 470BG (Rec. ITU-R BT.470-6 Systems B and G with
Rec. ITU-R BT.601-5) . . . . . . . . . . . . . . . . . . . . 25
4.4 Pixel Formats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
i
ii CONTENTS
4.4.1 4:4:4 Subsampling . . . . . . . . . . . . . . . . . . . . . . 27
4.4.2 4:2:2 Subsampling . . . . . . . . . . . . . . . . . . . . . . 27
4.4.3 4:2:0 Subsampling . . . . . . . . . . . . . . . . . . . . . . 28
4.4.4 Subsampling and the Picture Region . . . . . . . . . . . . 29
5 Bitpacking Convention 33
5.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
5.1.1 Octets and Bytes . . . . . . . . . . . . . . . . . . . . . . . 33
5.1.2 Words and Byte Order . . . . . . . . . . . . . . . . . . . . 33
5.1.3 Bit Order . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
5.2 Coding Bits into Bytes . . . . . . . . . . . . . . . . . . . . . . . . 34
5.2.1 Signedness . . . . . . . . . . . . . . . . . . . . . . . . . . 34
5.2.2 Encoding Example . . . . . . . . . . . . . . . . . . . . . . 35
5.2.3 Decoding Example . . . . . . . . . . . . . . . . . . . . . . 36
5.2.4 End-of-Packet Alignment . . . . . . . . . . . . . . . . . . 37
5.2.5 Reading Zero Bit Integers . . . . . . . . . . . . . . . . . . 37
6 Bitstream Headers 39
6.1 Common Header Decode . . . . . . . . . . . . . . . . . . . . . . . 39
6.2 Identification Header Decode . . . . . . . . . . . . . . . . . . . . 40
6.3 Comment Header . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
6.3.1 Comment Length Decode . . . . . . . . . . . . . . . . . . 46
6.3.2 Comment Header Decode . . . . . . . . . . . . . . . . . . 47
6.3.3 User Comment Format . . . . . . . . . . . . . . . . . . . . 48
6.4 Setup Header . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
6.4.1 Loop Filter Limit Table Decode . . . . . . . . . . . . . . . 49
6.4.2 Quantization Parameters Decode . . . . . . . . . . . . . . 50
6.4.3 Computing a Quantization Matrix . . . . . . . . . . . . . 53
6.4.4 DCT Token Huffman Tables . . . . . . . . . . . . . . . . . 56
6.4.5 Setup Header Decode . . . . . . . . . . . . . . . . . . . . 58
7 Frame Decode 61
7.1 Frame Header Decode . . . . . . . . . . . . . . . . . . . . . . . . 61
7.2 Run-Length Encoded Bit Strings . . . . . . . . . . . . . . . . . . 63
7.2.1 Long-Run Bit String Decode . . . . . . . . . . . . . . . . 63
7.2.2 Short-Run Bit String Decode . . . . . . . . . . . . . . . . 65
7.3 Coded Block Flags Decode . . . . . . . . . . . . . . . . . . . . . 66
7.4 Macro Block Coding Modes . . . . . . . . . . . . . . . . . . . . . 68
7.5 Motion Vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
7.5.1 Motion Vector Decode . . . . . . . . . . . . . . . . . . . . 71
7.5.2 Macro Block Motion Vector Decode . . . . . . . . . . . . 73
7.6 Block-Level qi Decode . . . . . . . . . . . . . . . . . . . . . . . . 79
7.7 DCT Coefficients . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
7.7.1 EOB Token Decode . . . . . . . . . . . . . . . . . . . . . 83
7.7.2 Coefficient Token Decode . . . . . . . . . . . . . . . . . . 85
7.7.3 DCT Coefficient Decode . . . . . . . . . . . . . . . . . . . 93
CONTENTS iii
7.8 Undoing DC Prediction . . . . . . . . . . . . . . . . . . . . . . . 96
7.8.1 Computing the DC Predictor . . . . . . . . . . . . . . . . 96
7.8.2 Inverting the DC Prediction Process . . . . . . . . . . . . 100
7.9 Reconstruction . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
7.9.1 Predictors . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
7.9.2 Dequantization . . . . . . . . . . . . . . . . . . . . . . . . 107
7.9.3 The Inverse DCT . . . . . . . . . . . . . . . . . . . . . . . 109
7.9.4 The Complete Reconstruction Algorithm . . . . . . . . . 118
7.10 Loop Filtering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
7.10.1 Horizontal Filter . . . . . . . . . . . . . . . . . . . . . . . 125
7.10.2 Vertical Filter . . . . . . . . . . . . . . . . . . . . . . . . . 127
7.10.3 Complete Loop Filter . . . . . . . . . . . . . . . . . . . . 128
7.11 Complete Frame Decode . . . . . . . . . . . . . . . . . . . . . . . 132
A Ogg Bitstream Encapsulation 139
A.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
A.1.1 MIME type . . . . . . . . . . . . . . . . . . . . . . . . . . 139
A.2 Embedding in a logical bitstream . . . . . . . . . . . . . . . . . . 140
A.2.1 Headers . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
A.2.2 Frame data . . . . . . . . . . . . . . . . . . . . . . . . . . 140
A.2.3 Granule position . . . . . . . . . . . . . . . . . . . . . . . 140
A.3 Multiplexed stream mapping . . . . . . . . . . . . . . . . . . . . 141
A.3.1 Chained streams . . . . . . . . . . . . . . . . . . . . . . . 141
A.3.2 Grouped streams . . . . . . . . . . . . . . . . . . . . . . . 141
B VP3 143
B.1 VP3 Compatibility . . . . . . . . . . . . . . . . . . . . . . . . . . 143
B.2 Loop Filter Limit Values . . . . . . . . . . . . . . . . . . . . . . . 144
B.3 Quantization Parameters . . . . . . . . . . . . . . . . . . . . . . . 144
B.4 Huffman Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
C Colophon 187