summaryrefslogtreecommitdiffstats
path: root/TOOLS/lib/Parse/Matroska/Definitions.pm
blob: a73c7b1fc66ac84633e0ae7f4027477989ee7b38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
use 5.008;
use strict;
use warnings;

# ABSTRACT: internal EBML grammar definitions
package Parse::Matroska::Definitions;

use Parse::Matroska::Utils qw{uniq uncamelize};

use Exporter;
our @ISA       = qw{Exporter};
our @EXPORT_OK = qw{elem_by_hexid %EBML_DEFINITION %MATROSKA_DEFINITION};

=head1 SYNOPSIS

    use Parse::Matroska::Definitions qw{elem_by_hexid};
    my $ebml_id = elem_by_hexid('1a45dfa3');
    print "EBML ID $ebml_id->{elid}'s name: $ebml_id->{name}";

=head1 DESCRIPTION

Contains the definition of the EBML grammar as expected in
Matroska files. This module is meant mostly for internal use.

As this was extended from a script in mpv-player, some data
generated is apparently useless for regular module users
but is still relevant to the mpv-player script. Such data
is annotated as being for mpv compatibility.

=head1 NOTE

The API of this module is not yet considered stable.

=head1 GLOBALS

These global variables are considered B<immutable>.

=head2 @Parse::Matroska::Definitions::global_elem_list

A global list of known matroska elements. Useful for
mpv's matroska script, used for generating C headers
that parse matroska.

=head2 %Parse::Matroska::Definitions::global_elem_dict

A global hash of known matroska elements. Used internally
by L</elem_by_hexid($id)>.

=cut

@Parse::Matroska::Definitions::global_elem_list = ();
%Parse::Matroska::Definitions::global_elem_dict = ();

=head2 %EBML_DEFINITION

Optionally-importable hash of known EBML IDs belonging
to the EBML generic grammar.

=head2 %MATROSKA_DEFINITION

Optionally-importable hash of known EBML IDs belonging
to the Matroska-specific grammar.

=cut

our %EBML_DEFINITION = define_ebml();
our %MATROSKA_DEFINITION = define_matroska();

=method elem_by_hexid($id)

Returns an EBML Element Definition corresponding to the provided
hexadecimal string. Returns C<undef> if the element is unknown.

=cut
sub elem_by_hexid {
    my ($elid) = @_;
    return $Parse::Matroska::Definitions::global_elem_dict{$elid};
}

################################################
### Helper functions for document definition ###
################################################

# used by elem when setting the 'valname' key
use constant TYPE_MAP => {
    uint    => 'uint64_t',
    str     => 'char *',
    binary  => 'struct bstr',
    ebml_id => 'uint32_t',
    float   => 'double',
    sint    => 'int64_t',
};

# this will be localized to "MATROSKA" or "EBML" on the elem declarations
our $ELEM_DEFINE_TYPE = undef;

=method elem($name,$elid,$valtype)

NOTE: never call this function yourself; it changes data structures
that are considered immutable outside of this package.

Internal API function that generates the EBML Element Definitions.

This API function returns an array which first element is C<$elid>
and the second is a generated hash. The generated hash is stored
in the @global_elem_list and %global_elem_dict.

The generated hash contains:

=for :list
= name
The EBML Element's name, given through C<$name>.
= elid
The EBML Element's hex id, given through C<$elid>. Used for lookups by L</elem_by_hexid($id)>.
= valtype
The EBML Element's type, given through C<$valtype>, except when C<$valtype> is an arrayref.
= multiple
If C<$name> ends with a C<*>, this is set as true and strips the C<*> from L</name>. Used to
mark elements that may be repeated.
= subelements
An arrayref of elements that may be children of this element, given through C<$valtype> if it
is an arrayref. Sets L</valtype> to C<sub> if there are subelements.
= subids
An arrayref listing all the L</elid>s of subelements, C<uniq>ified.

The following elements are for mpv compatibility:

=for :list
= definename
Name used for generating C #defines.
= fieldname
Name used for generating C struct fields.
= structname
Name used for generating C struct names.
= ebmltype
A pre-#defined constant to describe the element's type.
= valname
Typename used when declaring a struct field referring to this element.

=cut
sub elem {
    my %e = (name => shift, elid => shift, valtype => shift);

    # strip * from name, set 'multiple' if there was one
    $e{multiple} = scalar $e{name} =~ s/\*$//;

    # ELEM_DEFINE_TYPE is either MATROSKA or EBML
    $e{definename} = "${ELEM_DEFINE_TYPE}_ID_".uc($e{name});
    $e{fieldname} = uncamelize $e{name};
    $e{structname} = "ebml_$e{fieldname}";

    if (ref $e{valtype} eq 'HASH') {
        $e{subelements} = $e{valtype};
        $e{subids} = uniq map { $_->{elid} } values %{$e{subelements}};
        $e{valtype} = 'sub';
        $e{ebmltype} = 'EBML_TYPE_SUBELEMENTS';
        $e{valname} = "struct $e{structname}";
    } else {
        $e{ebmltype} = "EBML_TYPE_\U$e{valtype}";
        die "Unrecognized value type $e{valtype}" unless
            defined ($e{valname} = TYPE_MAP->{$e{valtype}});
    }
    my $e = \%e;
    push @Parse::Matroska::Definitions::global_elem_list, $e;
    $Parse::Matroska::Definitions::global_elem_dict{$e{elid}} = $e;
    return ($e{elid}, $e);
}

#############################################
### EBML and Matroska document definitons ###
#############################################

=method define_ebml

Internal function that defines the EBML generic grammar.

Must not be called from outside the package.

=cut
sub define_ebml {
    local $ELEM_DEFINE_TYPE = 'EBML';
    return (
        elem('EBML', '1a45dfa3', {
            elem('EBMLVersion',        '4286', 'uint'),
            elem('EBMLReadVersion',    '42f7', 'uint'),
            elem('EBMLMaxIDLength',    '42f2', 'uint'),
            elem('EBMLMaxSizeLength',  '42f3', 'uint'),
            elem('DocType',            '4282', 'str'),
            elem('DocTypeVersion',     '4287', 'uint'),
            elem('DocTypeReadVersion', '4285', 'uint'),
        }),

        elem('CRC32',      'bf', 'binary'),
        elem('Void',       'ec', 'binary'),
    );
}


=method define_matroska

Internal function that defines the Matroska-specific EBML grammar.

Must not be called from outside the package.

=cut
sub define_matroska {
    local $ELEM_DEFINE_TYPE = 'MATROSKA';
    return (
        elem('Segment', '18538067', {
            elem('SeekHead*', '114d9b74', {
                elem('Seek*', '4dbb', {
                    elem('SeekID',       '53ab', 'ebml_id'),
                    elem('SeekPosition', '53ac', 'uint'),
                }),
            }),

            elem('Info*', '1549a966', {
                elem('SegmentUID',      '73a4', 'binary'),
                elem('PrevUID',       '3cb923', 'binary'),
                elem('NextUID',       '3eb923', 'binary'),
                elem('TimecodeScale', '2ad7b1', 'uint'),
                elem('DateUTC',         '4461', 'sint'),
                elem('Title',           '7ba9', 'str'),
                elem('MuxingApp',       '4d80', 'str'),
                elem('WritingApp',      '5741', 'str'),
                elem('Duration',        '4489', 'float'),
            }),

            elem('Cluster*', '1f43b675', {
                elem('Timecode', 'e7', 'uint'),
                elem('BlockGroup*', 'a0', {
                    elem('Block',           'a1', 'binary'),
                    elem('BlockDuration',   '9b', 'uint'),
                    elem('ReferenceBlock*', 'fb', 'sint'),
                    elem('DiscardPadding',  '75A2', 'sint'),
                }),
                elem('SimpleBlock*', 'a3', 'binary'),
            }),

            elem('Tracks*', '1654ae6b', {
                elem('TrackEntry*', 'ae', {
                    elem('TrackNumber',            'd7', 'uint'),
                    elem('TrackUID',             '73c5', 'uint'),
                    elem('TrackType',              '83', 'uint'),
                    elem('FlagEnabled',            'b9', 'uint'),
                    elem('FlagDefault',            '88', 'uint'),
                    elem('FlagForced',           '55aa', 'uint'),
                    elem('FlagLacing',             '9c', 'uint'),
                    elem('MinCache',             '6de7', 'uint'),
                    elem('MaxCache',             '6df8', 'uint'),
                    elem('DefaultDuration',    '23e383', 'uint'),
                    elem('TrackTimecodeScale', '23314f', 'float'),
                    elem('MaxBlockAdditionID',   '55ee', 'uint'),
                    elem('Name',                 '536e', 'str'),
                    elem('Language',           '22b59c', 'str'),
                    elem('CodecID',                '86', 'str'),
                    elem('CodecPrivate',         '63a2', 'binary'),
                    elem('CodecName',          '258688', 'str'),
                    elem('CodecDecodeAll',         'aa', 'uint'),
                    elem('CodecDelay',           '56AA', 'uint'),
                    elem('SeekPreRoll',          '56BB', 'uint'),
                    elem('Video', 'e0', {
                        elem('FlagInterlaced',  '9a', 'uint'),
                        elem('PixelWidth',      'b0', 'uint'),
                        elem('PixelHeight',     'ba', 'uint'),
                        elem('DisplayWidth',  '54b0', 'uint'),
                        elem('DisplayHeight', '54ba', 'uint'),
                        elem('DisplayUnit',   '54b2', 'uint'),
                        elem('FrameRate',   '2383e3', 'float'),
                        elem('ColourSpace', '2eb524', 'binary'),
                        elem('StereoMode',    '53b8', 'uint'),
                    }),
                    elem('Audio', 'e1', {
                        elem('SamplingFrequency',         'b5', 'float'),
                        elem('OutputSamplingFrequency', '78b5', 'float'),
                        elem('Channels',                  '9f', 'uint'),
                        elem('BitDepth',                '6264', 'uint'),
                    }),
                    elem('ContentEncodings', '6d80', {
                        elem('ContentEncoding*', '6240', {
                            elem('ContentEncodingOrder', '5031', 'uint'),
                            elem('ContentEncodingScope', '5032', 'uint'),
                            elem('ContentEncodingType',  '5033', 'uint'),
                            elem('ContentCompression', '5034', {
                                elem('ContentCompAlgo',     '4254', 'uint'),
                                elem('ContentCompSettings', '4255', 'binary'),
                            }),
                        }),
                    }),
                }),
            }),

            elem('Cues', '1c53bb6b', {
                elem('CuePoint*', 'bb', {
                    elem('CueTime', 'b3', 'uint'),
                    elem('CueTrackPositions*', 'b7', {
                        elem('CueTrack',           'f7', 'uint'),
                        elem('CueClusterPosition', 'f1', 'uint'),
                        elem('CueRelativePosition','f0', 'uint'),
                        elem('CueDuration',        'b2', 'uint'),
                    }),
                }),
            }),

            elem('Attachments', '1941a469', {
                elem('AttachedFile*', '61a7', {
                    elem('FileDescription', '467e', 'str'),
                    elem('FileName',        '466e', 'str'),
                    elem('FileMimeType',    '4660', 'str'),
                    elem('FileData',        '465c', 'binary'),
                    elem('FileUID',         '46ae', 'uint'),
                }),
            }),

            elem('Chapters', '1043a770', {
                elem('EditionEntry*', '45b9', {
                    elem('EditionUID',         '45bc', 'uint'),
                    elem('EditionFlagHidden',  '45bd', 'uint'),
                    elem('EditionFlagDefault', '45db', 'uint'),
                    elem('EditionFlagOrdered', '45dd', 'uint'),
                    elem('ChapterAtom*', 'b6', {
                        elem('ChapterUID',               '73c4', 'uint'),
                        elem('ChapterTimeStart',           '91', 'uint'),
                        elem('ChapterTimeEnd',             '92', 'uint'),
                        elem('ChapterFlagHidden',          '98', 'uint'),
                        elem('ChapterFlagEnabled',       '4598', 'uint'),
                        elem('ChapterSegmentUID',        '6e67', 'binary'),
                        elem('ChapterSegmentEditionUID', '6ebc', 'uint'),
                        elem('ChapterDisplay*', '80', {
                            elem('ChapString',      '85', 'str'),
                            elem('ChapLanguage*', '437c', 'str'),
                            elem('ChapCountry*',  '437e', 'str'),
                        }),
                    }),
                }),
            }),
            elem('Tags*', '1254c367', {
                elem('Tag*', '7373', {
                    elem('Targets', '63c0', {
                        elem('TargetTypeValue',     '68ca', 'uint'),
                        elem('TargetTrackUID',      '63c5', 'uint'),
                        elem('TargetEditionUID',    '63c9', 'uint'),
                        elem('TargetChapterUID',    '63c4', 'uint'),
                        elem('TargetAttachmentUID', '63c6', 'uint'),
                     }),
                    elem('SimpleTag*', '67c8', {
                        elem('TagName',     '45a3', 'str'),
                        elem('TagLanguage', '447a', 'str'),
                        elem('TagString',   '4487', 'str'),
                    }),
                }),
            }),
        }),
    );
}

1;