hack fix for Ogg sequence headers being wrong
[schroedinger/research-port.git] / tools / dump_packets.c
blobe80f0040fac4519183a36c3bf01636ccc97cc606
2 #ifdef HAVE_CONFIG_H
3 #include "config.h"
4 #endif
6 #include <schroedinger/schro.h>
7 #include <schroedinger/schrobitstream.h>
8 #include <schroedinger/schrounpack.h>
9 #include <string.h>
10 #include <stdio.h>
12 #if 0
13 /* Used for checking bitstream bugs */
14 #define MARKER() \
15 do { \
16 g_print(" marker: %d\n", schro_unpack_decode_uint(&unpack)); \
17 }while(0)
18 #else
19 #define MARKER()
20 #endif
22 static void handle_packet(unsigned char *data, int size);
24 char *fn = "output.drc";
26 int
27 main (int argc, char *argv[])
29 FILE *file;
31 if (argc > 1) {
32 fn = argv[1];
35 file = fopen (fn, "r");
36 if (file == NULL) {
37 printf("cannot open %s\n", fn);
38 return 1;
41 while (1) {
42 unsigned char *packet;
43 unsigned char header[13];
44 int n;
45 int size;
47 n = fread (header, 1, 13, file);
48 if (n == 0) {
49 return 0;
51 if (n < 13) {
52 printf("truncated header\n");
53 return 1;
56 if (header[0] != 'B' || header[1] != 'B' || header[2] != 'C' ||
57 header[3] != 'D') {
58 printf("expected BBCD header\n");
59 return 1;
62 size = (header[5]<<24) | (header[6]<<16) | (header[7]<<8) | (header[8]);
63 if (size == 0) {
64 size = 13;
66 if (size < 13) {
67 printf("packet too small (%d < 13)\n", size);
68 return 1;
70 if (size > 16*1024*1024) {
71 printf("packet too large? (%d > 16777216)\n", size);
72 return 1;
75 packet = malloc (size);
76 memcpy (packet, header, 13);
77 n = fread (packet + 13, 1, size - 13, file);
78 if (n < size - 13) {
79 printf("truncated packet (%d < %d)\n", n, size-13);
80 exit(1);
83 handle_packet (packet, size);
85 free(packet);
88 return 0;
91 static void
92 dump_hex (unsigned char *data, int length, char *prefix)
94 int i;
95 for(i=0;i<length;i++){
96 if ((i&0xf) == 0) {
97 printf("%s0x%04x: ", prefix, i);
99 printf("%02x ", data[i]);
100 if ((i&0xf) == 0xf) {
101 printf("\n");
104 if ((i&0xf) != 0xf) {
105 printf("\n");
109 static void
110 handle_packet(unsigned char *data, int size)
112 SchroUnpack unpack;
113 const char *parse_code;
114 int next;
115 int prev;
117 if (memcmp (data, "KW-DIRAC", 8) == 0) {
118 printf("KW-DIRAC header\n");
119 return;
121 if (memcmp (data, "BBCD", 4) != 0) {
122 printf("non-Dirac packet\n");
123 dump_hex (data, MIN(size, 100), " ");
124 return;
127 switch (data[4]) {
128 case SCHRO_PARSE_CODE_SEQUENCE_HEADER:
129 parse_code = "access unit header";
130 break;
131 case SCHRO_PARSE_CODE_AUXILIARY_DATA:
132 parse_code = "auxiliary data";
133 break;
134 case SCHRO_PARSE_CODE_INTRA_REF:
135 parse_code = "intra ref";
136 break;
137 case SCHRO_PARSE_CODE_INTRA_NON_REF:
138 parse_code = "intra non-ref";
139 break;
140 case SCHRO_PARSE_CODE_INTER_REF_1:
141 parse_code = "inter ref 1";
142 break;
143 case SCHRO_PARSE_CODE_INTER_REF_2:
144 parse_code = "inter ref 2";
145 break;
146 case SCHRO_PARSE_CODE_INTER_NON_REF_1:
147 parse_code = "inter non-ref 1";
148 break;
149 case SCHRO_PARSE_CODE_INTER_NON_REF_2:
150 parse_code = "inter non-ref 2";
151 break;
152 case SCHRO_PARSE_CODE_END_OF_SEQUENCE:
153 parse_code = "end of sequence";
154 break;
155 case SCHRO_PARSE_CODE_LD_INTRA_REF:
156 parse_code = "low-delay intra ref";
157 break;
158 case SCHRO_PARSE_CODE_LD_INTRA_NON_REF:
159 parse_code = "low-delay intra non-ref";
160 break;
161 case SCHRO_PARSE_CODE_INTRA_REF_NOARITH:
162 parse_code = "intra ref noarith";
163 break;
164 case SCHRO_PARSE_CODE_INTRA_NON_REF_NOARITH:
165 parse_code = "intra non-ref noarith";
166 break;
167 case SCHRO_PARSE_CODE_INTER_REF_1_NOARITH:
168 parse_code = "inter ref 1 noarith";
169 break;
170 case SCHRO_PARSE_CODE_INTER_REF_2_NOARITH:
171 parse_code = "inter ref 2 noarith";
172 break;
173 case SCHRO_PARSE_CODE_INTER_NON_REF_1_NOARITH:
174 parse_code = "inter non-ref 1 noarith";
175 break;
176 case SCHRO_PARSE_CODE_INTER_NON_REF_2_NOARITH:
177 parse_code = "inter non-ref 2 noarith";
178 break;
179 case SCHRO_PARSE_CODE_PADDING:
180 parse_code = "padding";
181 break;
182 default:
183 parse_code = "unknown";
184 break;
186 printf("Parse code: %s (0x%02x)\n", parse_code, data[4]);
188 schro_unpack_init_with_data (&unpack, data + 5, size - 5, 1);
190 next = schro_unpack_decode_bits (&unpack, 32);
191 prev = schro_unpack_decode_bits (&unpack, 32);
193 printf(" offset to next: %d\n", next);
194 printf(" offset to prev: %d\n", prev);
196 if (data[4] == SCHRO_PARSE_CODE_SEQUENCE_HEADER) {
197 int bit;
199 /* parse parameters */
200 printf(" version.major: %d\n", schro_unpack_decode_uint(&unpack));
201 printf(" version.minor: %d\n", schro_unpack_decode_uint(&unpack));
202 printf(" profile: %d\n", schro_unpack_decode_uint(&unpack));
203 printf(" level: %d\n", schro_unpack_decode_uint(&unpack));
205 /* sequence parameters */
206 printf(" video_format: %d\n", schro_unpack_decode_uint(&unpack));
208 bit = schro_unpack_decode_bit(&unpack);
209 printf(" custom dimensions flag: %s\n", bit ? "yes" : "no");
210 if (bit) {
211 printf(" width: %d\n", schro_unpack_decode_uint(&unpack));
212 printf(" height: %d\n", schro_unpack_decode_uint(&unpack));
215 bit = schro_unpack_decode_bit(&unpack);
216 printf(" chroma format flag: %s\n", bit ? "yes" : "no");
217 if (bit) {
218 printf(" chroma format: %d\n", schro_unpack_decode_uint(&unpack));
221 bit = schro_unpack_decode_bit(&unpack);
222 printf(" custom_scan_format_flag: %s\n", bit ? "yes" : "no");
223 if (bit) {
224 printf(" source sampling: %d\n", schro_unpack_decode_uint(&unpack));
227 MARKER();
229 bit = schro_unpack_decode_bit(&unpack);
230 printf(" frame rate flag: %s\n", bit ? "yes" : "no");
231 if (bit) {
232 int index = schro_unpack_decode_uint(&unpack);
233 printf(" frame rate index: %d\n", index);
234 if (index == 0) {
235 printf(" frame rate numerator: %d\n",
236 schro_unpack_decode_uint(&unpack));
237 printf(" frame rate demoninator: %d\n",
238 schro_unpack_decode_uint(&unpack));
242 MARKER();
244 bit = schro_unpack_decode_bit(&unpack);
245 printf(" aspect ratio flag: %s\n", bit ? "yes" : "no");
246 if (bit) {
247 int index = schro_unpack_decode_uint(&unpack);
248 printf(" aspect ratio index: %d\n", index);
249 if (index == 0) {
250 printf(" aspect ratio numerator: %d\n",
251 schro_unpack_decode_uint(&unpack));
252 printf(" aspect ratio demoninator: %d\n",
253 schro_unpack_decode_uint(&unpack));
257 MARKER();
259 bit = schro_unpack_decode_bit(&unpack);
260 printf(" clean area flag: %s\n", bit ? "yes" : "no");
261 if (bit) {
262 printf(" clean width: %d\n", schro_unpack_decode_uint(&unpack));
263 printf(" clean height: %d\n", schro_unpack_decode_uint(&unpack));
264 printf(" left offset: %d\n", schro_unpack_decode_uint(&unpack));
265 printf(" top offset: %d\n", schro_unpack_decode_uint(&unpack));
268 MARKER();
270 bit = schro_unpack_decode_bit(&unpack);
271 printf(" signal range flag: %s\n", bit ? "yes" : "no");
272 if (bit) {
273 int index = schro_unpack_decode_uint(&unpack);
274 printf(" signal range index: %d\n", index);
275 if (index == 0) {
276 printf(" luma offset: %d\n", schro_unpack_decode_uint(&unpack));
277 printf(" luma excursion: %d\n", schro_unpack_decode_uint(&unpack));
278 printf(" chroma offset: %d\n", schro_unpack_decode_uint(&unpack));
279 printf(" chroma excursion: %d\n", schro_unpack_decode_uint(&unpack));
283 MARKER();
285 bit = schro_unpack_decode_bit(&unpack);
286 printf(" colour spec flag: %s\n", bit ? "yes" : "no");
287 if (bit) {
288 int index = schro_unpack_decode_uint(&unpack);
289 printf(" colour spec index: %d\n", index);
290 if (index == 0) {
291 bit = schro_unpack_decode_bit(&unpack);
292 printf(" colour primaries flag: %s\n", bit ? "yes" : "no");
293 if (bit) {
294 printf(" colour primaries: %d\n",
295 schro_unpack_decode_uint(&unpack));
297 bit = schro_unpack_decode_bit(&unpack);
298 printf(" colour matrix flag: %s\n", bit ? "yes" : "no");
299 if (bit) {
300 printf(" colour matrix: %d\n", schro_unpack_decode_uint(&unpack));
302 bit = schro_unpack_decode_bit(&unpack);
303 printf(" transfer function flag: %s\n", bit ? "yes" : "no");
304 if (bit) {
305 printf(" transfer function: %d\n",
306 schro_unpack_decode_uint(&unpack));
311 printf(" interlaced_coding: %d\n", schro_unpack_decode_uint(&unpack));
313 MARKER();
315 } else if (SCHRO_PARSE_CODE_IS_PICTURE(data[4])) {
316 int num_refs = SCHRO_PARSE_CODE_NUM_REFS(data[4]);
317 int bit;
318 int n;
319 int i;
320 int lowdelay = SCHRO_PARSE_CODE_IS_LOW_DELAY(data[4]);
322 printf(" num refs: %d\n", num_refs);
324 schro_unpack_byte_sync(&unpack);
325 printf(" picture_number: %u\n", schro_unpack_decode_bits(&unpack, 32));
326 if (num_refs > 0) {
327 printf(" ref1_offset: %d\n", schro_unpack_decode_sint(&unpack));
329 if (num_refs > 1) {
330 printf(" ref2_offset: %d\n", schro_unpack_decode_sint(&unpack));
332 if (SCHRO_PARSE_CODE_IS_REFERENCE(data[4])) {
333 int r = schro_unpack_decode_sint(&unpack);
334 if (r == 0) {
335 printf(" retire: none\n");
336 } else {
337 printf(" retire: %d\n", r);
341 if (num_refs > 0) {
342 int index;
344 schro_unpack_byte_sync(&unpack);
345 index = schro_unpack_decode_uint(&unpack);
347 printf(" block parameters index: %d\n", index);
348 if (index == 0) {
349 printf(" luma block width: %d\n", schro_unpack_decode_uint(&unpack));
350 printf(" luma block height: %d\n", schro_unpack_decode_uint(&unpack));
351 printf(" horiz luma block sep: %d\n", schro_unpack_decode_uint(&unpack));
352 printf(" vert luma block sep: %d\n", schro_unpack_decode_uint(&unpack));
355 MARKER();
357 printf(" motion vector precision bits: %d\n", schro_unpack_decode_uint(&unpack));
359 MARKER();
361 bit = schro_unpack_decode_bit(&unpack);
362 printf(" using global motion flag: %s\n", bit ? "yes" : "no");
363 if (bit) {
364 for(i=0;i<num_refs;i++){
365 printf(" global motion ref%d:\n", i+1);
366 bit = schro_unpack_decode_bit(&unpack);
367 printf(" non-zero pan/tilt flag: %s\n", bit ? "yes" : "no");
368 if (bit) {
369 printf(" pan %d\n", schro_unpack_decode_sint(&unpack));
370 printf(" tilt %d\n", schro_unpack_decode_sint(&unpack));
372 bit = schro_unpack_decode_bit(&unpack);
373 printf(" non-zero zoom rot shear flag: %s\n", bit ? "yes" : "no");
374 if (bit) {
375 printf(" exponent %d\n", schro_unpack_decode_uint(&unpack));
376 printf(" A11 %d\n", schro_unpack_decode_sint(&unpack));
377 printf(" A12 %d\n", schro_unpack_decode_sint(&unpack));
378 printf(" A21 %d\n", schro_unpack_decode_sint(&unpack));
379 printf(" A22 %d\n", schro_unpack_decode_sint(&unpack));
381 bit = schro_unpack_decode_bit(&unpack);
382 printf(" non-zero perspective flag: %s\n", bit ? "yes" : "no");
383 if (bit) {
384 printf(" exponent %d\n", schro_unpack_decode_uint(&unpack));
385 printf(" perspective_x %d\n", schro_unpack_decode_sint(&unpack));
386 printf(" perspective_y %d\n", schro_unpack_decode_sint(&unpack));
391 MARKER();
393 printf(" picture prediction mode: %d\n", schro_unpack_decode_uint(&unpack));
395 bit = schro_unpack_decode_bit(&unpack);
396 printf(" non-default weights flag: %s\n", bit ? "yes" : "no");
397 if (bit) {
398 printf(" picture weight precision: %d\n",
399 schro_unpack_decode_uint(&unpack));
400 for(i=0;i<num_refs;i++){
401 printf(" picture weight ref%d: %d\n", i+1,
402 schro_unpack_decode_sint(&unpack));
406 MARKER();
408 schro_unpack_byte_sync(&unpack);
410 n = schro_unpack_decode_uint(&unpack);
411 printf(" superblock split data length: %d\n", n);
412 schro_unpack_byte_sync (&unpack);
413 schro_unpack_skip_bits (&unpack, n*8);
415 n = schro_unpack_decode_uint(&unpack);
416 printf(" prediction modes data length: %d\n", n);
417 schro_unpack_byte_sync (&unpack);
418 schro_unpack_skip_bits (&unpack, n*8);
420 n = schro_unpack_decode_uint(&unpack);
421 printf(" vector data (ref1,x) length: %d\n", n);
422 schro_unpack_byte_sync (&unpack);
423 schro_unpack_skip_bits (&unpack, n*8);
425 n = schro_unpack_decode_uint(&unpack);
426 printf(" vector data (ref1,y) length: %d\n", n);
427 schro_unpack_byte_sync (&unpack);
428 schro_unpack_skip_bits (&unpack, n*8);
430 if (num_refs>1) {
431 n = schro_unpack_decode_uint(&unpack);
432 printf(" vector data (ref2,x) length: %d\n", n);
433 schro_unpack_byte_sync (&unpack);
434 schro_unpack_skip_bits (&unpack, n*8);
436 n = schro_unpack_decode_uint(&unpack);
437 printf(" vector data (ref2,y) length: %d\n", n);
438 schro_unpack_byte_sync (&unpack);
439 schro_unpack_skip_bits (&unpack, n*8);
442 n = schro_unpack_decode_uint(&unpack);
443 printf(" DC data (y) length: %d\n", n);
444 schro_unpack_byte_sync (&unpack);
445 schro_unpack_skip_bits (&unpack, n*8);
447 n = schro_unpack_decode_uint(&unpack);
448 printf(" DC data (u) length: %d\n", n);
449 schro_unpack_byte_sync (&unpack);
450 schro_unpack_skip_bits (&unpack, n*8);
452 n = schro_unpack_decode_uint(&unpack);
453 printf(" DC data (v) length: %d\n", n);
454 schro_unpack_byte_sync (&unpack);
455 schro_unpack_skip_bits (&unpack, n*8);
459 schro_unpack_byte_sync (&unpack);
460 if (num_refs == 0) {
461 bit = 0;
462 } else {
463 bit = schro_unpack_decode_bit (&unpack);
464 printf(" zero residual: %s\n", bit ? "yes" : "no");
466 if (!bit) {
467 int depth;
468 int j;
470 printf(" wavelet index: %d\n", schro_unpack_decode_uint(&unpack));
472 depth = schro_unpack_decode_uint(&unpack);
473 printf(" transform depth: %d\n", depth);
475 if (!lowdelay) {
476 bit = schro_unpack_decode_bit (&unpack);
477 printf(" spatial partition flag: %s\n", bit ? "yes" : "no");
478 if (bit) {
479 for(i=0;i<depth+1;i++){
480 printf(" number of codeblocks depth=%d\n", i);
481 printf(" horizontal codeblocks: %d\n",
482 schro_unpack_decode_uint(&unpack));
483 printf(" vertical codeblocks: %d\n",
484 schro_unpack_decode_uint(&unpack));
486 printf(" codeblock mode index: %d\n", schro_unpack_decode_uint(&unpack));
489 schro_unpack_byte_sync (&unpack);
490 for(j=0;j<3;j++){
491 printf(" component %d:\n",j);
492 printf(" comp subband length quantiser_index\n");
493 for(i=0;i<1+depth*3;i++){
494 int length;
496 if(unpack.overrun) {
497 printf(" PAST END\n");
498 continue;
501 length = schro_unpack_decode_uint(&unpack);
502 if (length > 0) {
503 printf(" %4d %4d: %6d %3d\n", j, i, length,
504 schro_unpack_decode_uint(&unpack));
505 schro_unpack_byte_sync(&unpack);
506 schro_unpack_skip_bits (&unpack, length*8);
507 } else {
508 printf(" %4d %4d: %6d\n", j, i, length);
509 schro_unpack_byte_sync(&unpack);
513 } else {
514 int slice_x;
515 int slice_y;
516 int slice_bytes_numerator;
517 int slice_bytes_denominator;
519 slice_x = schro_unpack_decode_uint(&unpack);
520 slice_y = schro_unpack_decode_uint(&unpack);
521 slice_bytes_numerator = schro_unpack_decode_uint(&unpack);
522 slice_bytes_denominator = schro_unpack_decode_uint(&unpack);
524 printf(" n_horiz_slices: %d\n", slice_x);
525 printf(" n_horiz_slices: %d\n", slice_y);
526 printf(" slice_bytes_numerator: %d\n", slice_bytes_numerator);
527 printf(" slice_bytes_denominator: %d\n", slice_bytes_denominator);
529 bit = schro_unpack_decode_bit (&unpack);
530 printf(" encode_quant_matrix: %s\n", bit ? "yes" : "no");
531 if (bit) {
532 for(i=0;i<1+depth*3;i++){
533 printf(" %2d: %d\n", i, schro_unpack_decode_uint(&unpack));
537 schro_unpack_byte_sync (&unpack);
540 } else if (data[4] == SCHRO_PARSE_CODE_AUXILIARY_DATA) {
541 int length = next - 14;
542 int code = data[13];
543 switch (code) {
544 case 0:
545 printf(" code: 0 (invalid)\n");
546 break;
547 case 1:
548 printf(" code: 1 (encoder implementation/version)\n");
549 printf(" string: %.*s\n", length, data + 14);
550 break;
551 case 2:
552 printf(" code: 2 (SMPTE 12M timecode)\n");
553 break;
554 case 3:
556 int i;
557 printf(" code: 3 (MD5 checksum)\n");
558 printf(" checksum: ");
559 for(i=0;i<16;i++){
560 printf("%02x", data[14+i]);
562 printf("\n");
564 break;
565 case 4:
567 int bitrate;
568 printf(" code: %d (bitrate)\n", code);
569 bitrate = (data[14]<<24);
570 bitrate |= (data[15]<<16);
571 bitrate |= (data[16]<<8);
572 bitrate |= (data[17]<<0);
573 printf(" bitrate: %d\n", bitrate);
575 break;
576 default:
577 printf(" code: %d (unknown)\n", code);
578 dump_hex (data + 14, length, " ");
579 break;
582 schro_unpack_skip_bits (&unpack, (1 + length)*8);
583 } else if (data[4] == SCHRO_PARSE_CODE_PADDING) {
584 int length = next - 13;
585 schro_unpack_skip_bits (&unpack, length*8);
588 schro_unpack_byte_sync (&unpack);
590 printf("offset %d\n", schro_unpack_get_bits_read (&unpack)/8);
591 dump_hex (unpack.data, MIN(data + size - unpack.data, 100), " ");