h264-reader 0.8.0

Reader for H264 bitstream syntax
Documentation
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
use crate::nal::sei::HeaderType;
use crate::nal::sei::SeiMessage;

#[derive(Debug)]
pub enum ItuTT35Error {
    NotEnoughData { expected: usize, actual: usize },
}

#[derive(Debug, PartialEq)]
pub enum ItuTT35 {
    Japan,
    Albania,
    Algeria,
    AmericanSamoa,
    GermanyFederalRepublicOf(u8),
    Anguilla,
    AntiguaandBarbuda,
    Argentina,
    AscensionseeSHelena,
    Australia,
    Austria,
    Bahamas,
    Bahrain,
    Bangladesh,
    Barbados,
    Belgium,
    Belize,
    BeninRepublicOf,
    Bermudas,
    BhutanKingdomOf,
    Bolivia,
    Botswana,
    Brazil,
    BritishAntarcticTerritory,
    BritishIndianOceanTerritory,
    BritishVirginIslands,
    BruneiDarussalam,
    Bulgaria,
    MyanmarUnionOf,
    Burundi,
    Byelorussia,
    Cameroon,
    Canada,
    CapeVerde,
    CaymanIslands,
    CentralAfricanRepublic,
    Chad,
    Chile,
    China,
    Colombia,
    Comoros,
    Congo,
    CookIslands,
    CostaRica,
    Cuba,
    Cyprus,
    CzechandSlovakFederalRepublic,
    Cambodia,
    DemocraticPeoplesRepublicOfKorea,
    Denmark,
    Djibouti,
    DominicanRepublic,
    Dominica,
    Ecuador,
    Egypt,
    ElSalvador,
    EquatorialGuinea,
    Ethiopia,
    FalklandIslands,
    Fiji,
    Finland,
    France,
    FrenchPolynesia,
    FrenchSouthernAndAntarcticLands,
    Gabon,
    Gambia,
    Angola,
    Ghana,
    Gibraltar,
    Greece,
    Grenada,
    Guam,
    Guatemala,
    Guernsey,
    Guinea,
    GuineaBissau,
    Guayana,
    Haiti,
    Honduras,
    Hongkong,
    HungaryRepublicOf,
    Iceland,
    India,
    Indonesia,
    IranIslamicRepublicOf,
    Iraq,
    Ireland,
    Israel,
    Italy,
    CotedIvoire,
    Jamaica,
    Afghanistan,
    Jersey,
    Jordan,
    Kenya,
    Kiribati,
    KoreaRepublicOf,
    Kuwait,
    LaoPeoplesDemocraticRepublic,
    Lebanon,
    Lesotho,
    Liberia,
    Libya,
    Liechtenstein,
    Luxembourg,
    Macau,
    Madagascar,
    Malaysia,
    Malawi,
    Maldives,
    Mali,
    Malta,
    Mauritania,
    Mauritius,
    Mexico,
    Monaco,
    Mongolia,
    Montserrat,
    Morocco,
    Mozambique,
    Nauru,
    Nepal,
    Netherlands,
    NetherlandsAntilles,
    NewCaledonia,
    NewZealand,
    Nicaragua,
    Niger,
    Nigeria,
    Norway,
    Oman,
    Pakistan,
    Panama,
    PapuaNewGuinea,
    Paraguay,
    Peru,
    Philippines,
    PolandRepublicOf,
    Portugal,
    PuertoRico,
    Qatar,
    Romania,
    Rwanda,
    SaintKittsAndNevis,
    SaintCroix,
    SaintHelenaAndAscension,
    SaintLucia,
    SanMarino,
    SaintThomas,
    SaoTomeAndPrincipe,
    SaintVincentAndTheGrenadines,
    SaudiArabia,
    Senegal,
    Seychelles,
    SierraLeone,
    Singapore,
    SolomonIslands,
    Somalia,
    SouthAfrica,
    Spain,
    SriLanka,
    Sudan,
    Suriname,
    Swaziland,
    Sweden,
    Switzerland,
    Syria,
    Tanzania,
    Thailand,
    Togo,
    Tonga,
    TrinidadAndTobago,
    Tunisia,
    Turkey,
    TurksAndCaicosIslands,
    Tuvalu,
    Uganda,
    Ukraine,
    UnitedArabEmirates,
    UnitedKingdom,
    UnitedStates,
    BurkinaFaso,
    Uruguay,
    USSR,
    Vanuatu,
    VaticanCityState,
    Venezuela,
    VietNam,
    WallisAndFutuna,
    WesternSamoa,
    YemenRepublicOf(u8),
    Yugoslavia,
    Zaire,
    Zambia,
    Zimbabwe,
    Unknown(u8),
    Extended(u8),
}
impl ItuTT35 {
    pub fn read<'a>(msg: &SeiMessage<'a>) -> Result<(ItuTT35, &'a [u8]), ItuTT35Error> {
        assert_eq!(msg.payload_type, HeaderType::UserDataRegisteredItuTT35);
        if msg.payload.is_empty() {
            return Err(ItuTT35Error::NotEnoughData {
                expected: 1,
                actual: 0,
            });
        }
        let buf = msg.payload;
        let itu_t_t35_country_code = buf[0];
        Ok(match itu_t_t35_country_code {
            0b0000_0000 => (ItuTT35::Japan, &buf[1..]),
            0b0000_0001 => (ItuTT35::Albania, &buf[1..]),
            0b0000_0010 => (ItuTT35::Algeria, &buf[1..]),
            0b0000_0011 => (ItuTT35::AmericanSamoa, &buf[1..]),
            0b0000_0100 => (
                ItuTT35::GermanyFederalRepublicOf(itu_t_t35_country_code),
                &buf[1..],
            ),
            0b0000_0101 => (ItuTT35::Anguilla, &buf[1..]),
            0b0000_0110 => (ItuTT35::AntiguaandBarbuda, &buf[1..]),
            0b0000_0111 => (ItuTT35::Argentina, &buf[1..]),
            0b0000_1000 => (ItuTT35::AscensionseeSHelena, &buf[1..]),
            0b0000_1001 => (ItuTT35::Australia, &buf[1..]),
            0b0000_1010 => (ItuTT35::Austria, &buf[1..]),
            0b0000_1011 => (ItuTT35::Bahamas, &buf[1..]),
            0b0000_1100 => (ItuTT35::Bahrain, &buf[1..]),
            0b0000_1101 => (ItuTT35::Bangladesh, &buf[1..]),
            0b0000_1110 => (ItuTT35::Barbados, &buf[1..]),
            0b0000_1111 => (ItuTT35::Belgium, &buf[1..]),
            0b0001_0000 => (ItuTT35::Belize, &buf[1..]),
            0b0001_0001 => (ItuTT35::BeninRepublicOf, &buf[1..]),
            0b0001_0010 => (ItuTT35::Bermudas, &buf[1..]),
            0b0001_0011 => (ItuTT35::BhutanKingdomOf, &buf[1..]),
            0b0001_0100 => (ItuTT35::Bolivia, &buf[1..]),
            0b0001_0101 => (ItuTT35::Botswana, &buf[1..]),
            0b0001_0110 => (ItuTT35::Brazil, &buf[1..]),
            0b0001_0111 => (ItuTT35::BritishAntarcticTerritory, &buf[1..]),
            0b0001_1000 => (ItuTT35::BritishIndianOceanTerritory, &buf[1..]),
            0b0001_1001 => (ItuTT35::BritishVirginIslands, &buf[1..]),
            0b0001_1010 => (ItuTT35::BruneiDarussalam, &buf[1..]),
            0b0001_1011 => (ItuTT35::Bulgaria, &buf[1..]),
            0b0001_1100 => (ItuTT35::MyanmarUnionOf, &buf[1..]),
            0b0001_1101 => (ItuTT35::Burundi, &buf[1..]),
            0b0001_1110 => (ItuTT35::Byelorussia, &buf[1..]),
            0b0001_1111 => (ItuTT35::Cameroon, &buf[1..]),
            0b0010_0000 => (ItuTT35::Canada, &buf[1..]),
            0b0010_0001 => (ItuTT35::CapeVerde, &buf[1..]),
            0b0010_0010 => (ItuTT35::CaymanIslands, &buf[1..]),
            0b0010_0011 => (ItuTT35::CentralAfricanRepublic, &buf[1..]),
            0b0010_0100 => (ItuTT35::Chad, &buf[1..]),
            0b0010_0101 => (ItuTT35::Chile, &buf[1..]),
            0b0010_0110 => (ItuTT35::China, &buf[1..]),
            0b0010_0111 => (ItuTT35::Colombia, &buf[1..]),
            0b0010_1000 => (ItuTT35::Comoros, &buf[1..]),
            0b0010_1001 => (ItuTT35::Congo, &buf[1..]),
            0b0010_1010 => (ItuTT35::CookIslands, &buf[1..]),
            0b0010_1011 => (ItuTT35::CostaRica, &buf[1..]),
            0b0010_1100 => (ItuTT35::Cuba, &buf[1..]),
            0b0010_1101 => (ItuTT35::Cyprus, &buf[1..]),
            0b0010_1110 => (ItuTT35::CzechandSlovakFederalRepublic, &buf[1..]),
            0b0010_1111 => (ItuTT35::Cambodia, &buf[1..]),
            0b0011_0000 => (ItuTT35::DemocraticPeoplesRepublicOfKorea, &buf[1..]),
            0b0011_0001 => (ItuTT35::Denmark, &buf[1..]),
            0b0011_0010 => (ItuTT35::Djibouti, &buf[1..]),
            0b0011_0011 => (ItuTT35::DominicanRepublic, &buf[1..]),
            0b0011_0100 => (ItuTT35::Dominica, &buf[1..]),
            0b0011_0101 => (ItuTT35::Ecuador, &buf[1..]),
            0b0011_0110 => (ItuTT35::Egypt, &buf[1..]),
            0b0011_0111 => (ItuTT35::ElSalvador, &buf[1..]),
            0b0011_1000 => (ItuTT35::EquatorialGuinea, &buf[1..]),
            0b0011_1001 => (ItuTT35::Ethiopia, &buf[1..]),
            0b0011_1010 => (ItuTT35::FalklandIslands, &buf[1..]),
            0b0011_1011 => (ItuTT35::Fiji, &buf[1..]),
            0b0011_1100 => (ItuTT35::Finland, &buf[1..]),
            0b0011_1101 => (ItuTT35::France, &buf[1..]),
            0b0011_1110 => (ItuTT35::FrenchPolynesia, &buf[1..]),
            0b0011_1111 => (ItuTT35::FrenchSouthernAndAntarcticLands, &buf[1..]),
            0b0100_0000 => (ItuTT35::Gabon, &buf[1..]),
            0b0100_0001 => (ItuTT35::Gambia, &buf[1..]),
            0b0100_0010 => (
                ItuTT35::GermanyFederalRepublicOf(itu_t_t35_country_code),
                &buf[1..],
            ),
            0b0100_0011 => (ItuTT35::Angola, &buf[1..]),
            0b0100_0100 => (ItuTT35::Ghana, &buf[1..]),
            0b0100_0101 => (ItuTT35::Gibraltar, &buf[1..]),
            0b0100_0110 => (ItuTT35::Greece, &buf[1..]),
            0b0100_0111 => (ItuTT35::Grenada, &buf[1..]),
            0b0100_1000 => (ItuTT35::Guam, &buf[1..]),
            0b0100_1001 => (ItuTT35::Guatemala, &buf[1..]),
            0b0100_1010 => (ItuTT35::Guernsey, &buf[1..]),
            0b0100_1011 => (ItuTT35::Guinea, &buf[1..]),
            0b0100_1100 => (ItuTT35::GuineaBissau, &buf[1..]),
            0b0100_1101 => (ItuTT35::Guayana, &buf[1..]),
            0b0100_1110 => (ItuTT35::Haiti, &buf[1..]),
            0b0100_1111 => (ItuTT35::Honduras, &buf[1..]),
            0b0101_0000 => (ItuTT35::Hongkong, &buf[1..]),
            0b0101_0001 => (ItuTT35::HungaryRepublicOf, &buf[1..]),
            0b0101_0010 => (ItuTT35::Iceland, &buf[1..]),
            0b0101_0011 => (ItuTT35::India, &buf[1..]),
            0b0101_0100 => (ItuTT35::Indonesia, &buf[1..]),
            0b0101_0101 => (ItuTT35::IranIslamicRepublicOf, &buf[1..]),
            0b0101_0110 => (ItuTT35::Iraq, &buf[1..]),
            0b0101_0111 => (ItuTT35::Ireland, &buf[1..]),
            0b0101_1000 => (ItuTT35::Israel, &buf[1..]),
            0b0101_1001 => (ItuTT35::Italy, &buf[1..]),
            0b0101_1010 => (ItuTT35::CotedIvoire, &buf[1..]),
            0b0101_1011 => (ItuTT35::Jamaica, &buf[1..]),
            0b0101_1100 => (ItuTT35::Afghanistan, &buf[1..]),
            0b0101_1101 => (ItuTT35::Jersey, &buf[1..]),
            0b0101_1110 => (ItuTT35::Jordan, &buf[1..]),
            0b0101_1111 => (ItuTT35::Kenya, &buf[1..]),
            0b0110_0000 => (ItuTT35::Kiribati, &buf[1..]),
            0b0110_0001 => (ItuTT35::KoreaRepublicOf, &buf[1..]),
            0b0110_0010 => (ItuTT35::Kuwait, &buf[1..]),
            0b0110_0011 => (ItuTT35::LaoPeoplesDemocraticRepublic, &buf[1..]),
            0b0110_0100 => (ItuTT35::Lebanon, &buf[1..]),
            0b0110_0101 => (ItuTT35::Lesotho, &buf[1..]),
            0b0110_0110 => (ItuTT35::Liberia, &buf[1..]),
            0b0110_0111 => (ItuTT35::Libya, &buf[1..]),
            0b0110_1000 => (ItuTT35::Liechtenstein, &buf[1..]),
            0b0110_1001 => (ItuTT35::Luxembourg, &buf[1..]),
            0b0110_1010 => (ItuTT35::Macau, &buf[1..]),
            0b0110_1011 => (ItuTT35::Madagascar, &buf[1..]),
            0b0110_1100 => (ItuTT35::Malaysia, &buf[1..]),
            0b0110_1101 => (ItuTT35::Malawi, &buf[1..]),
            0b0110_1110 => (ItuTT35::Maldives, &buf[1..]),
            0b0110_1111 => (ItuTT35::Mali, &buf[1..]),
            0b0111_0000 => (ItuTT35::Malta, &buf[1..]),
            0b1111_0001 => (ItuTT35::Mauritania, &buf[1..]),
            0b0111_0010 => (ItuTT35::Mauritius, &buf[1..]),
            0b0111_0011 => (ItuTT35::Mexico, &buf[1..]),
            0b0111_0100 => (ItuTT35::Monaco, &buf[1..]),
            0b0111_0101 => (ItuTT35::Mongolia, &buf[1..]),
            0b0111_0110 => (ItuTT35::Montserrat, &buf[1..]),
            0b0111_0111 => (ItuTT35::Morocco, &buf[1..]),
            0b0111_1000 => (ItuTT35::Mozambique, &buf[1..]),
            0b0111_1001 => (ItuTT35::Nauru, &buf[1..]),
            0b0111_1010 => (ItuTT35::Nepal, &buf[1..]),
            0b0111_1011 => (ItuTT35::Netherlands, &buf[1..]),
            0b0111_1100 => (ItuTT35::NetherlandsAntilles, &buf[1..]),
            0b0111_1101 => (ItuTT35::NewCaledonia, &buf[1..]),
            0b0111_1110 => (ItuTT35::NewZealand, &buf[1..]),
            0b0111_1111 => (ItuTT35::Nicaragua, &buf[1..]),
            0b1000_0000 => (ItuTT35::Niger, &buf[1..]),
            0b1000_0001 => (ItuTT35::Nigeria, &buf[1..]),
            0b1000_0010 => (ItuTT35::Norway, &buf[1..]),
            0b1000_0011 => (ItuTT35::Oman, &buf[1..]),
            0b1000_0100 => (ItuTT35::Pakistan, &buf[1..]),
            0b1000_0101 => (ItuTT35::Panama, &buf[1..]),
            0b1000_0110 => (ItuTT35::PapuaNewGuinea, &buf[1..]),
            0b1000_0111 => (ItuTT35::Paraguay, &buf[1..]),
            0b1000_1000 => (ItuTT35::Peru, &buf[1..]),
            0b1000_1001 => (ItuTT35::Philippines, &buf[1..]),
            0b1000_1010 => (ItuTT35::PolandRepublicOf, &buf[1..]),
            0b1000_1011 => (ItuTT35::Portugal, &buf[1..]),
            0b1000_1100 => (ItuTT35::PuertoRico, &buf[1..]),
            0b1000_1101 => (ItuTT35::Qatar, &buf[1..]),
            0b1000_1110 => (ItuTT35::Romania, &buf[1..]),
            0b1000_1111 => (ItuTT35::Rwanda, &buf[1..]),
            0b1001_0000 => (ItuTT35::SaintKittsAndNevis, &buf[1..]),
            0b1001_0001 => (ItuTT35::SaintCroix, &buf[1..]),
            0b1001_0010 => (ItuTT35::SaintHelenaAndAscension, &buf[1..]),
            0b1001_0011 => (ItuTT35::SaintLucia, &buf[1..]),
            0b1001_0100 => (ItuTT35::SanMarino, &buf[1..]),
            0b1001_0101 => (ItuTT35::SaintThomas, &buf[1..]),
            0b1001_0110 => (ItuTT35::SaoTomeAndPrincipe, &buf[1..]),
            0b1001_0111 => (ItuTT35::SaintVincentAndTheGrenadines, &buf[1..]),
            0b1001_1000 => (ItuTT35::SaudiArabia, &buf[1..]),
            0b1001_1001 => (ItuTT35::Senegal, &buf[1..]),
            0b1001_1010 => (ItuTT35::Seychelles, &buf[1..]),
            0b1001_1011 => (ItuTT35::SierraLeone, &buf[1..]),
            0b1001_1100 => (ItuTT35::Singapore, &buf[1..]),
            0b1001_1101 => (ItuTT35::SolomonIslands, &buf[1..]),
            0b1001_1110 => (ItuTT35::Somalia, &buf[1..]),
            0b1001_1111 => (ItuTT35::SouthAfrica, &buf[1..]),
            0b1010_0000 => (ItuTT35::Spain, &buf[1..]),
            0b1010_0001 => (ItuTT35::SriLanka, &buf[1..]),
            0b1010_0010 => (ItuTT35::Sudan, &buf[1..]),
            0b1010_0011 => (ItuTT35::Suriname, &buf[1..]),
            0b1010_0100 => (ItuTT35::Swaziland, &buf[1..]),
            0b1010_0101 => (ItuTT35::Sweden, &buf[1..]),
            0b1010_0110 => (ItuTT35::Switzerland, &buf[1..]),
            0b1010_0111 => (ItuTT35::Syria, &buf[1..]),
            0b1010_1000 => (ItuTT35::Tanzania, &buf[1..]),
            0b1010_1001 => (ItuTT35::Thailand, &buf[1..]),
            0b1010_1010 => (ItuTT35::Togo, &buf[1..]),
            0b1010_1011 => (ItuTT35::Tonga, &buf[1..]),
            0b1010_1100 => (ItuTT35::TrinidadAndTobago, &buf[1..]),
            0b1010_1101 => (ItuTT35::Tunisia, &buf[1..]),
            0b1010_1110 => (ItuTT35::Turkey, &buf[1..]),
            0b1010_1111 => (ItuTT35::TurksAndCaicosIslands, &buf[1..]),
            0b1011_0000 => (ItuTT35::Tuvalu, &buf[1..]),
            0b1011_0001 => (ItuTT35::Uganda, &buf[1..]),
            0b1011_0010 => (ItuTT35::Ukraine, &buf[1..]),
            0b1011_0011 => (ItuTT35::UnitedArabEmirates, &buf[1..]),
            0b1011_0100 => (ItuTT35::UnitedKingdom, &buf[1..]),
            0b1011_0101 => (ItuTT35::UnitedStates, &buf[1..]),
            0b1011_0110 => (ItuTT35::BurkinaFaso, &buf[1..]),
            0b1011_0111 => (ItuTT35::Uruguay, &buf[1..]),
            0b1011_1000 => (ItuTT35::USSR, &buf[1..]),
            0b1011_1001 => (ItuTT35::Vanuatu, &buf[1..]),
            0b1011_1010 => (ItuTT35::VaticanCityState, &buf[1..]),
            0b1011_1011 => (ItuTT35::Venezuela, &buf[1..]),
            0b1011_1100 => (ItuTT35::VietNam, &buf[1..]),
            0b1011_1101 => (ItuTT35::WallisAndFutuna, &buf[1..]),
            0b1011_1110 => (ItuTT35::WesternSamoa, &buf[1..]),
            0b1011_1111 => (ItuTT35::YemenRepublicOf(itu_t_t35_country_code), &buf[1..]),
            0b1100_0000 => (ItuTT35::YemenRepublicOf(itu_t_t35_country_code), &buf[1..]),
            0b1100_0001 => (ItuTT35::Yugoslavia, &buf[1..]),
            0b1100_0010 => (ItuTT35::Zaire, &buf[1..]),
            0b1100_0011 => (ItuTT35::Zambia, &buf[1..]),
            0b1100_0100 => (ItuTT35::Zimbabwe, &buf[1..]),
            0b1111_1111 => {
                if buf.len() < 2 {
                    return Err(ItuTT35Error::NotEnoughData {
                        expected: 2,
                        actual: buf.len(),
                    });
                }
                (ItuTT35::Extended(buf[1]), &buf[1..])
            }
            _ => (ItuTT35::Unknown(itu_t_t35_country_code), &buf[1..]),
        })
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn parse() {
        let msg = SeiMessage {
            payload_type: HeaderType::UserDataRegisteredItuTT35,
            payload: &[0b1011_0100, 0x00],
        };
        assert_eq!(
            ItuTT35::read(&msg).unwrap(),
            (ItuTT35::UnitedKingdom, &[0x00][..])
        );
    }
}