tremor uses integer types
[mplayer/glamo.git] / libmpdemux / mpeg_hdr.c
bloba31bd32f84693b1f5f3c4a5e00c9e263d0ea4248
2 // based on libmpeg2/header.c by Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
4 #include <inttypes.h>
5 #include <stdio.h>
6 #include <stdlib.h>
8 #include "config.h"
9 #include "mpeg_hdr.h"
11 static int frameratecode2framerate[16] = {
13 // Official mpeg1/2 framerates: (1-8)
14 24000*10000/1001, 24*10000,25*10000,
15 30000*10000/1001, 30*10000,50*10000,
16 60000*10000/1001, 60*10000,
17 // Xing's 15fps: (9)
18 15*10000,
19 // libmpeg3's "Unofficial economy rates": (10-13)
20 5*10000,10*10000,12*10000,15*10000,
21 // some invalid ones: (14-15)
22 0,0
26 int mp_header_process_sequence_header (mp_mpeg_header_t * picture, unsigned char * buffer)
28 int width, height;
30 if ((buffer[6] & 0x20) != 0x20){
31 fprintf(stderr, "missing marker bit!\n");
32 return 1; /* missing marker_bit */
35 height = (buffer[0] << 16) | (buffer[1] << 8) | buffer[2];
37 picture->display_picture_width = (height >> 12);
38 picture->display_picture_height = (height & 0xfff);
40 width = ((height >> 12) + 15) & ~15;
41 height = ((height & 0xfff) + 15) & ~15;
43 picture->aspect_ratio_information = buffer[3] >> 4;
44 picture->frame_rate_code = buffer[3] & 15;
45 picture->fps=frameratecode2framerate[picture->frame_rate_code];
46 picture->bitrate = (buffer[4]<<10)|(buffer[5]<<2)|(buffer[6]>>6);
47 picture->mpeg1 = 1;
48 picture->picture_structure = 3; //FRAME_PICTURE;
49 picture->display_time=100;
50 return 0;
53 static int header_process_sequence_extension (mp_mpeg_header_t * picture,
54 unsigned char * buffer)
56 /* check chroma format, size extensions, marker bit */
58 if ( ((buffer[1] & 0x06) == 0x00) ||
59 ((buffer[1] & 0x01) != 0x00) || (buffer[2] & 0xe0) ||
60 ((buffer[3] & 0x01) != 0x01) )
61 return 1;
63 picture->progressive_sequence = (buffer[1] >> 3) & 1;
64 picture->mpeg1 = 0;
65 return 0;
68 static int header_process_picture_coding_extension (mp_mpeg_header_t * picture, unsigned char * buffer)
70 picture->picture_structure = buffer[2] & 3;
71 picture->top_field_first = buffer[3] >> 7;
72 picture->repeat_first_field = (buffer[3] >> 1) & 1;
73 picture->progressive_frame = buffer[4] >> 7;
75 // repeat_first implementation by A'rpi/ESP-team, based on libmpeg3:
76 picture->display_time=100;
77 if(picture->repeat_first_field){
78 if(picture->progressive_sequence){
79 if(picture->top_field_first)
80 picture->display_time+=200;
81 else
82 picture->display_time+=100;
83 } else
84 if(picture->progressive_frame){
85 picture->display_time+=50;
88 //temopral hack. We calc time on every field, so if we have 2 fields
89 // interlaced we'll end with double time for 1 frame
90 if( picture->picture_structure!=3 ) picture->display_time/=2;
91 return 0;
94 int mp_header_process_extension (mp_mpeg_header_t * picture, unsigned char * buffer)
96 switch (buffer[0] & 0xf0) {
97 case 0x10: /* sequence extension */
98 return header_process_sequence_extension (picture, buffer);
99 case 0x80: /* picture coding extension */
100 return header_process_picture_coding_extension (picture, buffer);
102 return 0;
106 //MPEG4 HEADERS
107 unsigned char mp_getbits(unsigned char *buffer, unsigned int from, unsigned char len)
109 unsigned int n;
110 unsigned char m, u, l, y;
112 n = from / 8;
113 m = from % 8;
114 u = 8 - m;
115 l = (len > u ? len - u : 0);
117 y = (buffer[n] << m);
118 if(8 > len)
119 y >>= (8-len);
120 if(l)
121 y |= (buffer[n+1] >> (8-l));
123 //fprintf(stderr, "GETBITS(%d -> %d): bytes=0x%x 0x%x, n=%d, m=%d, l=%d, u=%d, Y=%d\n",
124 // from, (int) len, (int) buffer[n],(int) buffer[n+1], n, (int) m, (int) l, (int) u, (int) y);
125 return y;
128 #define getbits mp_getbits
130 static int read_timeinc(mp_mpeg_header_t * picture, unsigned char * buffer, int n)
132 if(picture->timeinc_bits > 8) {
133 picture->timeinc_unit = getbits(buffer, n, picture->timeinc_bits - 8) << 8;
134 n += picture->timeinc_bits - 8;
135 picture->timeinc_unit |= getbits(buffer, n, 8);
136 n += 8;
137 } else {
138 picture->timeinc_unit = getbits(buffer, n, picture->timeinc_bits);
139 n += picture->timeinc_bits;
141 //fprintf(stderr, "TIMEINC2: %d, bits: %d\n", picture->timeinc_unit, picture->timeinc_bits);
142 return n;
145 int mp4_header_process_vol(mp_mpeg_header_t * picture, unsigned char * buffer)
147 unsigned int n, aspect=0, aspectw=0, aspecth=0, x=1, v;
149 //begins with 0x0000012x
150 picture->fps = 0;
151 picture->timeinc_bits = picture->timeinc_resolution = picture->timeinc_unit = 0;
152 n = 9;
153 if(getbits(buffer, n, 1))
154 n += 7;
155 n++;
156 aspect=getbits(buffer, n, 4);
157 n += 4;
158 if(aspect == 0x0f) {
159 aspectw = getbits(buffer, n, 8);
160 n += 8;
161 aspecth = getbits(buffer, n, 8);
162 n += 8;
165 if(getbits(buffer, n, 1)) {
166 n += 4;
167 if(getbits(buffer, n, 1))
168 n += 79;
169 n++;
170 } else n++;
172 n+=3;
174 picture->timeinc_resolution = getbits(buffer, n, 8) << 8;
175 n += 8;
176 picture->timeinc_resolution |= getbits(buffer, n, 8);
177 n += 8;
179 picture->timeinc_bits = 0;
180 v = picture->timeinc_resolution - 1;
181 while(v && (x<16)) {
182 v>>=1;
183 picture->timeinc_bits++;
185 picture->timeinc_bits = (picture->timeinc_bits > 1 ? picture->timeinc_bits : 1);
187 n++; //marker bit
189 if(getbits(buffer, n, 1)) { //fixed_vop_timeinc
190 n++;
191 n = read_timeinc(picture, buffer, n);
193 if(picture->timeinc_unit)
194 picture->fps = (picture->timeinc_resolution * 10000) / picture->timeinc_unit;
197 //fprintf(stderr, "ASPECT: %d, PARW=%d, PARH=%d, TIMEINCRESOLUTION: %d, FIXED_TIMEINC: %d (number of bits: %d), FPS: %u\n",
198 // aspect, aspectw, aspecth, picture->timeinc_resolution, picture->timeinc_unit, picture->timeinc_bits, picture->fps);
200 return 0;
203 void mp4_header_process_vop(mp_mpeg_header_t * picture, unsigned char * buffer)
205 int n;
206 n = 0;
207 picture->picture_type = getbits(buffer, n, 2);
208 n += 2;
209 while(getbits(buffer, n, 1))
210 n++;
211 n++;
212 getbits(buffer, n, 1);
213 n++;
214 n = read_timeinc(picture, buffer, n);
217 #define min(a, b) ((a) <= (b) ? (a) : (b))
219 static unsigned int read_golomb(unsigned char *buffer, unsigned int *init)
221 unsigned int x, v = 0, v2 = 0, m, len = 0, n = *init;
223 while(getbits(buffer, n++, 1) == 0)
224 len++;
226 x = len + n;
227 while(n < x)
229 m = min(x - n, 8);
230 v |= getbits(buffer, n, m);
231 n += m;
232 if(x - n > 8)
233 v <<= 8;
236 v2 = 1;
237 for(n = 0; n < len; n++)
238 v2 <<= 1;
239 v2 = (v2 - 1) + v;
241 //fprintf(stderr, "READ_GOLOMB(%u), V=2^%u + %u-1 = %u\n", *init, len, v, v2);
242 *init = x;
243 return v2;
247 static int h264_parse_vui(mp_mpeg_header_t * picture, unsigned char * buf, unsigned int n)
249 unsigned int overscan, vsp_color, chroma, timing, fixed_fps;
251 if(getbits(buf, n++, 1))
253 picture->aspect_ratio_information = getbits(buf, n, 8);
254 n += 8;
255 if(picture->aspect_ratio_information == 255)
257 picture->display_picture_width = (getbits(buf, n, 8) << 8) | getbits(buf, n + 8, 8);
258 n += 16;
260 picture->display_picture_height = (getbits(buf, n, 8) << 8) | getbits(buf, n + 8, 8);
261 n += 16;
265 if((overscan=getbits(buf, n++, 1)))
266 n++;
267 if((vsp_color=getbits(buf, n++, 1)))
269 n += 4;
270 if(getbits(buf, n++, 1))
271 n += 24;
273 if((chroma=getbits(buf, n++, 1)))
275 read_golomb(buf, &n);
276 read_golomb(buf, &n);
278 if((timing=getbits(buf, n++, 1)))
280 picture->timeinc_unit = (getbits(buf, n, 8) << 24) | (getbits(buf, n+8, 8) << 16) | (getbits(buf, n+16, 8) << 8) | getbits(buf, n+24, 8);
281 n += 32;
283 picture->timeinc_resolution = (getbits(buf, n, 8) << 24) | (getbits(buf, n+8, 8) << 16) | (getbits(buf, n+16, 8) << 8) | getbits(buf, n+24, 8);
284 n += 32;
286 fixed_fps = getbits(buf, n, 1);
288 if(picture->timeinc_unit > 0 && picture->timeinc_resolution > 0)
289 picture->fps = ((uint64_t)picture->timeinc_resolution * 10000) / picture->timeinc_unit;
292 //fprintf(stderr, "H264_PARSE_VUI, OVESCAN=%u, VSP_COLOR=%u, CHROMA=%u, TIMING=%u, DISPW=%u, DISPH=%u, TIMERES=%u, TIMEINC=%u, FIXED_FPS=%u\n", overscan, vsp_color, chroma, timing, picture->display_picture_width, picture->display_picture_height,
293 // picture->timeinc_resolution, picture->timeinc_unit, picture->timeinc_unit, fixed_fps);
295 return n;
298 int h264_parse_sps(mp_mpeg_header_t * picture, unsigned char * buf, int len)
300 unsigned int n = 0, v, i, j, mbh;
301 unsigned char *dest;
302 int frame_mbs_only;
304 dest = (unsigned char*) malloc(len);
305 if(! dest)
306 return 0;
307 j = i = 0;
308 while(i <= len-3)
310 if(buf[i] == 0 && buf[i+1] == 0 && buf[i+2] == 3)
312 dest[j] = dest[j+1] = 0;
313 j += 2;
314 i += 3;
316 else
318 dest[j] = buf[i];
319 j++;
320 i++;
323 dest[j] = buf[len-2];
324 dest[j+1] = buf[len-1];
325 j += 2;
326 len = j+1;
327 buf = dest;
329 picture->fps = picture->timeinc_unit = picture->timeinc_resolution = 0;
330 n = 24;
331 read_golomb(buf, &n);
332 if(buf[0] >= 100){
333 if(read_golomb(buf, &n) == 3)
334 n++;
335 read_golomb(buf, &n);
336 read_golomb(buf, &n);
337 n++;
338 if(getbits(buf, n++, 1)){
339 //FIXME scaling matrix
342 read_golomb(buf, &n);
343 v = read_golomb(buf, &n);
344 if(v == 0)
345 read_golomb(buf, &n);
346 else if(v == 1)
348 getbits(buf, n++, 1);
349 read_golomb(buf, &n);
350 read_golomb(buf, &n);
351 v = read_golomb(buf, &n);
352 for(i = 0; i < v; i++)
353 read_golomb(buf, &n);
355 read_golomb(buf, &n);
356 getbits(buf, n++, 1);
357 picture->display_picture_width = 16 *(read_golomb(buf, &n)+1);
358 mbh = read_golomb(buf, &n)+1;
359 frame_mbs_only = getbits(buf, n++, 1);
360 picture->display_picture_height = 16 * (2 - frame_mbs_only) * mbh;
361 if(!frame_mbs_only)
362 getbits(buf, n++, 1);
363 getbits(buf, n++, 1);
364 if(getbits(buf, n++, 1))
366 read_golomb(buf, &n);
367 read_golomb(buf, &n);
368 read_golomb(buf, &n);
369 read_golomb(buf, &n);
371 if(getbits(buf, n++, 1))
372 n = h264_parse_vui(picture, buf, n);
374 free(dest);
375 return n;