Bug fixing in chroma block matching
[schroedinger/research-port.git] / tools / dump_gop.c
blobf03da5c9c59dd88886417dcad41d1e5cf163dd6c
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 printf(" marker: %d\n", schro_unpack_decode_uint(&unpack)); \
17 }while(0)
18 #else
19 #define MARKER()
20 #endif
23 static void handle_packet(unsigned char *data, int size);
25 char *fn = "output.drc";
27 int
28 main (int argc, char *argv[])
30 FILE *file;
32 if (argc > 1) {
33 fn = argv[1];
36 file = fopen (fn, "r");
37 if (file == NULL) {
38 printf("cannot open %s\n", fn);
39 return 1;
42 while (1) {
43 unsigned char *packet;
44 unsigned char header[13];
45 int n;
46 int size;
48 n = fread (header, 1, 13, file);
49 if (n == 0) {
50 return 0;
52 if (n < 13) {
53 printf("truncated header\n");
54 return 1;
57 if (header[0] != 'B' || header[1] != 'B' || header[2] != 'C' ||
58 header[3] != 'D') {
59 printf("expected BBCD header\n");
60 return 1;
63 size = (header[5]<<24) | (header[6]<<16) | (header[7]<<8) | (header[8]);
64 if (size == 0) {
65 size = 13;
67 if (size < 13) {
68 printf("packet too small (%d < 13)\n", size);
69 return 1;
71 if (size > 16*1024*1024) {
72 printf("packet too large? (%d > 16777216)\n", size);
73 return 1;
76 packet = malloc (size);
77 memcpy (packet, header, 13);
78 n = fread (packet + 13, 1, size - 13, file);
79 if (n < size - 13) {
80 printf("truncated packet (%d < %d)\n", n, size-13);
81 exit(1);
84 handle_packet (packet, size);
86 free(packet);
89 return 0;
92 static void
93 dump_hex (unsigned char *data, int length, char *prefix)
95 int i;
96 for(i=0;i<length;i++){
97 if ((i&0xf) == 0) {
98 printf("%s0x%04x: ", prefix, i);
100 printf("%02x ", data[i]);
101 if ((i&0xf) == 0xf) {
102 printf("\n");
105 if ((i&0xf) != 0xf) {
106 printf("\n");
110 static void
111 handle_packet (unsigned char *data, int size)
113 SchroUnpack unpack;
114 const char *parse_code;
115 int next;
116 int prev;
118 if (memcmp (data, "BBCD", 4) != 0) {
119 printf("non-Dirac packet\n");
120 dump_hex (data, MIN(size, 100), " ");
121 return;
124 switch (data[4]) {
125 case SCHRO_PARSE_CODE_SEQUENCE_HEADER:
126 parse_code = "access unit header";
127 break;
128 case SCHRO_PARSE_CODE_AUXILIARY_DATA:
129 parse_code = "auxiliary data";
130 break;
131 case SCHRO_PARSE_CODE_INTRA_REF:
132 parse_code = "intra ref";
133 break;
134 case SCHRO_PARSE_CODE_INTRA_NON_REF:
135 parse_code = "intra non-ref";
136 break;
137 case SCHRO_PARSE_CODE_INTER_REF_1:
138 parse_code = "inter ref 1";
139 break;
140 case SCHRO_PARSE_CODE_INTER_REF_2:
141 parse_code = "inter ref 2";
142 break;
143 case SCHRO_PARSE_CODE_INTER_NON_REF_1:
144 parse_code = "inter non-ref 1";
145 break;
146 case SCHRO_PARSE_CODE_INTER_NON_REF_2:
147 parse_code = "inter non-ref 2";
148 break;
149 case SCHRO_PARSE_CODE_END_OF_SEQUENCE:
150 parse_code = "end of sequence";
151 break;
152 case SCHRO_PARSE_CODE_LD_INTRA_REF:
153 parse_code = "low-delay intra ref";
154 break;
155 case SCHRO_PARSE_CODE_LD_INTRA_NON_REF:
156 parse_code = "low-delay intra non-ref";
157 break;
158 case SCHRO_PARSE_CODE_INTRA_REF_NOARITH:
159 parse_code = "intra ref noarith";
160 break;
161 case SCHRO_PARSE_CODE_INTRA_NON_REF_NOARITH:
162 parse_code = "intra non-ref noarith";
163 break;
164 case SCHRO_PARSE_CODE_INTER_REF_1_NOARITH:
165 parse_code = "inter ref 1 noarith";
166 break;
167 case SCHRO_PARSE_CODE_INTER_REF_2_NOARITH:
168 parse_code = "inter ref 2 noarith";
169 break;
170 case SCHRO_PARSE_CODE_INTER_NON_REF_1_NOARITH:
171 parse_code = "inter non-ref 1 noarith";
172 break;
173 case SCHRO_PARSE_CODE_INTER_NON_REF_2_NOARITH:
174 parse_code = "inter non-ref 2 noarith";
175 break;
176 default:
177 parse_code = "unknown";
178 break;
180 schro_unpack_init_with_data (&unpack, data + 5, size - 5, 1);
182 next = schro_unpack_decode_bits (&unpack, 32);
183 prev = schro_unpack_decode_bits (&unpack, 32);
185 if (data[4] == SCHRO_PARSE_CODE_SEQUENCE_HEADER) {
186 printf("AU\n");
187 printf("pictur: ");
188 printf(" ");
189 printf(" ");
190 printf(" ref1 ");
191 printf(" ref2 ");
192 printf("retire ");
193 printf(" size \n");
194 } else if (SCHRO_PARSE_CODE_IS_PICTURE(data[4])) {
195 int num_refs = SCHRO_PARSE_CODE_NUM_REFS(data[4]);
196 int pic_num;
197 char ref_chars[3] = { 'I', 'P', 'B' };
199 schro_unpack_byte_sync(&unpack);
200 pic_num = schro_unpack_decode_bits(&unpack, 32);
201 printf("%6d: ", pic_num);
202 if (SCHRO_PARSE_CODE_IS_REFERENCE(data[4])) {
203 printf("ref ");
204 } else {
205 printf(" ");
208 printf("%c ", ref_chars[num_refs]);
210 if (num_refs > 0) {
211 printf("%6d ", pic_num + schro_unpack_decode_sint(&unpack));
212 } else {
213 printf(" ");
215 if (num_refs > 1) {
216 printf("%6d ", pic_num + schro_unpack_decode_sint(&unpack));
217 } else {
218 printf(" ");
221 if (SCHRO_PARSE_CODE_IS_REFERENCE(data[4])) {
222 int r = schro_unpack_decode_sint(&unpack);
223 if (r == 0) {
224 printf(" none ");
225 } else {
226 printf("%6d ", pic_num + r);
228 } else {
229 printf(" ");
231 printf(" %8d\n", next);
232 } else if (data[4] == SCHRO_PARSE_CODE_AUXILIARY_DATA) {
235 schro_unpack_byte_sync (&unpack);