1 
2 #include "dirac_parse.h"
3 #include <string.h>
4 
5 #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
6 
7 typedef struct _Unpack Unpack;
8 
9 struct _Unpack
10 {
11   unsigned char *data;
12   int n_bits_left;
13   int index;
14   int guard_bit;
15 };
16 
17 static void schro_unpack_init_with_data (Unpack * unpack, unsigned char *data,
18     int n_bytes, unsigned int guard_bit);
19 
20 static unsigned int schro_unpack_decode_bit (Unpack * unpack);
21 static unsigned int schro_unpack_decode_uint (Unpack * unpack);
22 
23 
24 void schro_video_format_set_std_video_format (DiracSequenceHeader * format,
25     int index);
26 void schro_video_format_set_std_frame_rate (DiracSequenceHeader * format,
27     int index);
28 void schro_video_format_set_std_aspect_ratio (DiracSequenceHeader * format,
29     int index);
30 void schro_video_format_set_std_signal_range (DiracSequenceHeader * format,
31     int index);
32 void schro_video_format_set_std_colour_spec (DiracSequenceHeader * format,
33     int index);
34 
35 int
dirac_sequence_header_parse(DiracSequenceHeader * header,unsigned char * data,int n_bytes)36 dirac_sequence_header_parse (DiracSequenceHeader * header,
37     unsigned char *data, int n_bytes)
38 {
39   int bit;
40   int index;
41   Unpack _unpack;
42   Unpack *unpack = &_unpack;
43   int major_version;
44   int minor_version;
45   int profile;
46   int level;
47 
48   memset (header, 0, sizeof (*header));
49 
50   schro_unpack_init_with_data (unpack, data, n_bytes, 1);
51 
52   /* parse parameters */
53   major_version = schro_unpack_decode_uint (unpack);
54   minor_version = schro_unpack_decode_uint (unpack);
55   profile = schro_unpack_decode_uint (unpack);
56   level = schro_unpack_decode_uint (unpack);
57 
58   /* base video header */
59   index = schro_unpack_decode_uint (unpack);
60   schro_video_format_set_std_video_format (header, index);
61 
62   header->major_version = major_version;
63   header->minor_version = minor_version;
64   header->profile = profile;
65   header->level = level;
66 
67   /* source parameters */
68   /* frame dimensions */
69   bit = schro_unpack_decode_bit (unpack);
70   if (bit) {
71     header->width = schro_unpack_decode_uint (unpack);
72     header->height = schro_unpack_decode_uint (unpack);
73   }
74 
75   /* chroma header */
76   bit = schro_unpack_decode_bit (unpack);
77   if (bit) {
78     header->chroma_format = schro_unpack_decode_uint (unpack);
79   }
80 
81   /* scan header */
82   bit = schro_unpack_decode_bit (unpack);
83   if (bit) {
84     header->interlaced = schro_unpack_decode_uint (unpack);
85   }
86 
87   /* frame rate */
88   bit = schro_unpack_decode_bit (unpack);
89   if (bit) {
90     index = schro_unpack_decode_uint (unpack);
91     if (index == 0) {
92       header->frame_rate_numerator = schro_unpack_decode_uint (unpack);
93       header->frame_rate_denominator = schro_unpack_decode_uint (unpack);
94     } else {
95       schro_video_format_set_std_frame_rate (header, index);
96     }
97   }
98 
99   /* aspect ratio */
100   bit = schro_unpack_decode_bit (unpack);
101   if (bit) {
102     index = schro_unpack_decode_uint (unpack);
103     if (index == 0) {
104       header->aspect_ratio_numerator = schro_unpack_decode_uint (unpack);
105       header->aspect_ratio_denominator = schro_unpack_decode_uint (unpack);
106     } else {
107       schro_video_format_set_std_aspect_ratio (header, index);
108     }
109   }
110 
111   /* clean area */
112   bit = schro_unpack_decode_bit (unpack);
113   if (bit) {
114     header->clean_width = schro_unpack_decode_uint (unpack);
115     header->clean_height = schro_unpack_decode_uint (unpack);
116     header->left_offset = schro_unpack_decode_uint (unpack);
117     header->top_offset = schro_unpack_decode_uint (unpack);
118   }
119 
120   /* signal range */
121   bit = schro_unpack_decode_bit (unpack);
122   if (bit) {
123     index = schro_unpack_decode_uint (unpack);
124     if (index == 0) {
125       header->luma_offset = schro_unpack_decode_uint (unpack);
126       header->luma_excursion = schro_unpack_decode_uint (unpack);
127       header->chroma_offset = schro_unpack_decode_uint (unpack);
128       header->chroma_excursion = schro_unpack_decode_uint (unpack);
129     } else {
130       schro_video_format_set_std_signal_range (header, index);
131     }
132   }
133 
134   /* colour spec */
135   bit = schro_unpack_decode_bit (unpack);
136   if (bit) {
137     index = schro_unpack_decode_uint (unpack);
138     schro_video_format_set_std_colour_spec (header, index);
139     if (index == 0) {
140       /* colour primaries */
141       bit = schro_unpack_decode_bit (unpack);
142       if (bit) {
143         header->colour_primaries = schro_unpack_decode_uint (unpack);
144       }
145       /* colour matrix */
146       bit = schro_unpack_decode_bit (unpack);
147       if (bit) {
148         header->colour_matrix = schro_unpack_decode_uint (unpack);
149       }
150       /* transfer function */
151       bit = schro_unpack_decode_bit (unpack);
152       if (bit) {
153         header->transfer_function = schro_unpack_decode_uint (unpack);
154       }
155     }
156   }
157 
158   header->interlaced_coding = schro_unpack_decode_uint (unpack);
159 
160   return 1;
161 }
162 
163 /* standard stuff */
164 
165 static const DiracSequenceHeader schro_video_formats[] = {
166   {0, 0, 0, 0,
167         0,                      /* custom */
168         640, 480, SCHRO_CHROMA_420,
169         FALSE, FALSE,
170         24000, 1001, 1, 1,
171         640, 480, 0, 0,
172         0, 255, 128, 255,
173       0, 0, 0},
174   {0, 0, 0, 0,
175         1,                      /* QSIF525 */
176         176, 120, SCHRO_CHROMA_420,
177         FALSE, FALSE,
178         15000, 1001, 10, 11,
179         176, 120, 0, 0,
180         0, 255, 128, 255,
181       1, 1, 0},
182   {0, 0, 0, 0,
183         2,                      /* QCIF */
184         176, 144, SCHRO_CHROMA_420,
185         FALSE, TRUE,
186         25, 2, 12, 11,
187         176, 144, 0, 0,
188         0, 255, 128, 255,
189       2, 1, 0},
190   {0, 0, 0, 0,
191         3,                      /* SIF525 */
192         352, 240, SCHRO_CHROMA_420,
193         FALSE, FALSE,
194         15000, 1001, 10, 11,
195         352, 240, 0, 0,
196         0, 255, 128, 255,
197       1, 1, 0},
198   {0, 0, 0, 0,
199         4,                      /* CIF */
200         352, 288, SCHRO_CHROMA_420,
201         FALSE, TRUE,
202         25, 2, 12, 11,
203         352, 288, 0, 0,
204         0, 255, 128, 255,
205       2, 1, 0},
206   {0, 0, 0, 0,
207         5,                      /* 4SIF525 */
208         704, 480, SCHRO_CHROMA_420,
209         FALSE, FALSE,
210         15000, 1001, 10, 11,
211         704, 480, 0, 0,
212         0, 255, 128, 255,
213       1, 1, 0},
214   {0, 0, 0, 0,
215         6,                      /* 4CIF */
216         704, 576, SCHRO_CHROMA_420,
217         FALSE, TRUE,
218         25, 2, 12, 11,
219         704, 576, 0, 0,
220         0, 255, 128, 255,
221       2, 1, 0},
222   {0, 0, 0, 0,
223         7,                      /* SD480I-60 */
224         720, 480, SCHRO_CHROMA_422,
225         TRUE, FALSE,
226         30000, 1001, 10, 11,
227         704, 480, 8, 0,
228         64, 876, 512, 896,
229       1, 1, 0},
230   {0, 0, 0, 0,
231         8,                      /* SD576I-50 */
232         720, 576, SCHRO_CHROMA_422,
233         TRUE, TRUE,
234         25, 1, 12, 11,
235         704, 576, 8, 0,
236         64, 876, 512, 896,
237       2, 1, 0},
238   {0, 0, 0, 0,
239         9,                      /* HD720P-60 */
240         1280, 720, SCHRO_CHROMA_422,
241         FALSE, TRUE,
242         60000, 1001, 1, 1,
243         1280, 720, 0, 0,
244         64, 876, 512, 896,
245       0, 0, 0},
246   {0, 0, 0, 0,
247         10,                     /* HD720P-50 */
248         1280, 720, SCHRO_CHROMA_422,
249         FALSE, TRUE,
250         50, 1, 1, 1,
251         1280, 720, 0, 0,
252         64, 876, 512, 896,
253       0, 0, 0},
254   {0, 0, 0, 0,
255         11,                     /* HD1080I-60 */
256         1920, 1080, SCHRO_CHROMA_422,
257         TRUE, TRUE,
258         30000, 1001, 1, 1,
259         1920, 1080, 0, 0,
260         64, 876, 512, 896,
261       0, 0, 0},
262   {0, 0, 0, 0,
263         12,                     /* HD1080I-50 */
264         1920, 1080, SCHRO_CHROMA_422,
265         TRUE, TRUE,
266         25, 1, 1, 1,
267         1920, 1080, 0, 0,
268         64, 876, 512, 896,
269       0, 0, 0},
270   {0, 0, 0, 0,
271         13,                     /* HD1080P-60 */
272         1920, 1080, SCHRO_CHROMA_422,
273         FALSE, TRUE,
274         60000, 1001, 1, 1,
275         1920, 1080, 0, 0,
276         64, 876, 512, 896,
277       0, 0, 0},
278   {0, 0, 0, 0,
279         14,                     /* HD1080P-50 */
280         1920, 1080, SCHRO_CHROMA_422,
281         FALSE, TRUE,
282         50, 1, 1, 1,
283         1920, 1080, 0, 0,
284         64, 876, 512, 896,
285       0, 0, 0},
286   {0, 0, 0, 0,
287         15,                     /* DC2K */
288         2048, 1080, SCHRO_CHROMA_444,
289         FALSE, TRUE,
290         24, 1, 1, 1,
291         2048, 1080, 0, 0,
292         256, 3504, 2048, 3584,
293       3, 0, 0},
294   {0, 0, 0, 0,
295         16,                     /* DC4K */
296         4096, 2160, SCHRO_CHROMA_444,
297         FALSE, TRUE,
298         24, 1, 1, 1,
299         2048, 1536, 0, 0,
300         256, 3504, 2048, 3584,
301       3, 0, 0},
302 };
303 
304 void
schro_video_format_set_std_video_format(DiracSequenceHeader * format,int index)305 schro_video_format_set_std_video_format (DiracSequenceHeader * format,
306     int index)
307 {
308 
309   if (index < 0 || index >= ARRAY_SIZE (schro_video_formats)) {
310     return;
311   }
312 
313   memcpy (format, schro_video_formats + index, sizeof (DiracSequenceHeader));
314 }
315 
316 typedef struct _SchroFrameRate SchroFrameRate;
317 struct _SchroFrameRate
318 {
319   int numerator;
320   int denominator;
321 };
322 
323 static const SchroFrameRate schro_frame_rates[] = {
324   {0, 0},
325   {24000, 1001},
326   {24, 1},
327   {25, 1},
328   {30000, 1001},
329   {30, 1},
330   {50, 1},
331   {60000, 1001},
332   {60, 1},
333   {15000, 1001},
334   {25, 2}
335 };
336 
337 void
schro_video_format_set_std_frame_rate(DiracSequenceHeader * format,int index)338 schro_video_format_set_std_frame_rate (DiracSequenceHeader * format, int index)
339 {
340   if (index < 1 || index >= ARRAY_SIZE (schro_frame_rates)) {
341     return;
342   }
343 
344   format->frame_rate_numerator = schro_frame_rates[index].numerator;
345   format->frame_rate_denominator = schro_frame_rates[index].denominator;
346 }
347 
348 typedef struct _SchroPixelAspectRatio SchroPixelAspectRatio;
349 struct _SchroPixelAspectRatio
350 {
351   int numerator;
352   int denominator;
353 };
354 
355 static const SchroPixelAspectRatio schro_aspect_ratios[] = {
356   {0, 0},
357   {1, 1},
358   {10, 11},
359   {12, 11},
360   {40, 33},
361   {16, 11},
362   {4, 3}
363 };
364 
365 void
schro_video_format_set_std_aspect_ratio(DiracSequenceHeader * format,int index)366 schro_video_format_set_std_aspect_ratio (DiracSequenceHeader * format,
367     int index)
368 {
369   if (index < 1 || index >= ARRAY_SIZE (schro_aspect_ratios)) {
370     return;
371   }
372 
373   format->aspect_ratio_numerator = schro_aspect_ratios[index].numerator;
374   format->aspect_ratio_denominator = schro_aspect_ratios[index].denominator;
375 
376 }
377 
378 typedef struct _SchroSignalRangeStruct SchroSignalRangeStruct;
379 struct _SchroSignalRangeStruct
380 {
381   int luma_offset;
382   int luma_excursion;
383   int chroma_offset;
384   int chroma_excursion;
385 };
386 
387 static const SchroSignalRangeStruct schro_signal_ranges[] = {
388   {0, 0, 0, 0},
389   {0, 255, 128, 255},
390   {16, 219, 128, 224},
391   {64, 876, 512, 896},
392   {256, 3504, 2048, 3584}
393 };
394 
395 void
schro_video_format_set_std_signal_range(DiracSequenceHeader * format,int i)396 schro_video_format_set_std_signal_range (DiracSequenceHeader * format, int i)
397 {
398   if (i < 1 || i >= ARRAY_SIZE (schro_signal_ranges)) {
399     return;
400   }
401 
402   format->luma_offset = schro_signal_ranges[i].luma_offset;
403   format->luma_excursion = schro_signal_ranges[i].luma_excursion;
404   format->chroma_offset = schro_signal_ranges[i].chroma_offset;
405   format->chroma_excursion = schro_signal_ranges[i].chroma_excursion;
406 }
407 
408 typedef struct _SchroColourSpecStruct SchroColourSpecStruct;
409 struct _SchroColourSpecStruct
410 {
411   int colour_primaries;
412   int colour_matrix;
413   int transfer_function;
414 };
415 
416 static const SchroColourSpecStruct schro_colour_specs[] = {
417   {                             /* Custom */
418         SCHRO_COLOUR_PRIMARY_HDTV,
419         SCHRO_COLOUR_MATRIX_HDTV,
420       SCHRO_TRANSFER_CHAR_TV_GAMMA},
421   {                             /* SDTV 525 */
422         SCHRO_COLOUR_PRIMARY_SDTV_525,
423         SCHRO_COLOUR_MATRIX_SDTV,
424       SCHRO_TRANSFER_CHAR_TV_GAMMA},
425   {                             /* SDTV 625 */
426         SCHRO_COLOUR_PRIMARY_SDTV_625,
427         SCHRO_COLOUR_MATRIX_SDTV,
428       SCHRO_TRANSFER_CHAR_TV_GAMMA},
429   {                             /* HDTV */
430         SCHRO_COLOUR_PRIMARY_HDTV,
431         SCHRO_COLOUR_MATRIX_HDTV,
432       SCHRO_TRANSFER_CHAR_TV_GAMMA},
433   {                             /* Cinema */
434         SCHRO_COLOUR_PRIMARY_CINEMA,
435         SCHRO_COLOUR_MATRIX_HDTV,
436       SCHRO_TRANSFER_CHAR_TV_GAMMA}
437 };
438 
439 void
schro_video_format_set_std_colour_spec(DiracSequenceHeader * format,int i)440 schro_video_format_set_std_colour_spec (DiracSequenceHeader * format, int i)
441 {
442   if (i < 0 || i >= ARRAY_SIZE (schro_colour_specs)) {
443     return;
444   }
445 
446   format->colour_primaries = schro_colour_specs[i].colour_primaries;
447   format->colour_matrix = schro_colour_specs[i].colour_matrix;
448   format->transfer_function = schro_colour_specs[i].transfer_function;
449 }
450 
451 
452 /* unpack */
453 
454 static void
schro_unpack_init_with_data(Unpack * unpack,unsigned char * data,int n_bytes,unsigned int guard_bit)455 schro_unpack_init_with_data (Unpack * unpack, unsigned char *data,
456     int n_bytes, unsigned int guard_bit)
457 {
458   memset (unpack, 0, sizeof (Unpack));
459 
460   unpack->data = data;
461   unpack->n_bits_left = 8 * n_bytes;
462   unpack->guard_bit = guard_bit;
463 }
464 
465 static unsigned int
schro_unpack_decode_bit(Unpack * unpack)466 schro_unpack_decode_bit (Unpack * unpack)
467 {
468   int bit;
469 
470   if (unpack->n_bits_left < 1) {
471     return unpack->guard_bit;
472   }
473   bit = (unpack->data[unpack->index >> 3] >> (7 - (unpack->index & 7))) & 1;
474   unpack->index++;
475   unpack->n_bits_left--;
476 
477   return bit;
478 }
479 
480 static unsigned int
schro_unpack_decode_uint(Unpack * unpack)481 schro_unpack_decode_uint (Unpack * unpack)
482 {
483   int count;
484   int value;
485 
486   count = 0;
487   value = 0;
488   while (!schro_unpack_decode_bit (unpack)) {
489     count++;
490     value <<= 1;
491     value |= schro_unpack_decode_bit (unpack);
492   }
493 
494   return (1 << count) - 1 + value;
495 }
496