2 // based on libmpeg2/header.c by Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
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,
19 // libmpeg3's "Unofficial economy rates": (10-13)
20 5*10000,10*10000,12*10000,15*10000,
21 // some invalid ones: (14-15)
26 int mp_header_process_sequence_header (mp_mpeg_header_t
* picture
, unsigned char * buffer
)
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);
48 picture
->picture_structure
= 3; //FRAME_PICTURE;
49 picture
->display_time
=100;
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) )
63 picture
->progressive_sequence
= (buffer
[1] >> 3) & 1;
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;
82 picture
->display_time
+=100;
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;
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
);
107 unsigned char mp_getbits(unsigned char *buffer
, unsigned int from
, unsigned char len
)
110 unsigned char m
, u
, l
, y
;
115 l
= (len
> u
? len
- u
: 0);
117 y
= (buffer
[n
] << m
);
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);
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);
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);
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
151 picture
->timeinc_bits
= picture
->timeinc_resolution
= picture
->timeinc_unit
= 0;
153 if(getbits(buffer
, n
, 1))
156 aspect
=getbits(buffer
, n
, 4);
159 aspectw
= getbits(buffer
, n
, 8);
161 aspecth
= getbits(buffer
, n
, 8);
165 if(getbits(buffer
, n
, 1)) {
167 if(getbits(buffer
, n
, 1))
174 picture
->timeinc_resolution
= getbits(buffer
, n
, 8) << 8;
176 picture
->timeinc_resolution
|= getbits(buffer
, n
, 8);
179 picture
->timeinc_bits
= 0;
180 v
= picture
->timeinc_resolution
- 1;
183 picture
->timeinc_bits
++;
185 picture
->timeinc_bits
= (picture
->timeinc_bits
> 1 ? picture
->timeinc_bits
: 1);
189 if(getbits(buffer
, n
, 1)) { //fixed_vop_timeinc
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);
203 void mp4_header_process_vop(mp_mpeg_header_t
* picture
, unsigned char * buffer
)
207 picture
->picture_type
= getbits(buffer
, n
, 2);
209 while(getbits(buffer
, n
, 1))
212 getbits(buffer
, n
, 1);
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)
230 v
|= getbits(buffer
, n
, m
);
237 for(n
= 0; n
< len
; n
++)
241 //fprintf(stderr, "READ_GOLOMB(%u), V=2^%u + %u-1 = %u\n", *init, len, v, 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);
255 if(picture
->aspect_ratio_information
== 255)
257 picture
->display_picture_width
= (getbits(buf
, n
, 8) << 8) | getbits(buf
, n
+ 8, 8);
260 picture
->display_picture_height
= (getbits(buf
, n
, 8) << 8) | getbits(buf
, n
+ 8, 8);
265 if((overscan
=getbits(buf
, n
++, 1)))
267 if((vsp_color
=getbits(buf
, n
++, 1)))
270 if(getbits(buf
, n
++, 1))
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);
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);
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);
298 int h264_parse_sps(mp_mpeg_header_t
* picture
, unsigned char * buf
, int len
)
300 unsigned int n
= 0, v
, i
, j
, mbh
;
304 dest
= (unsigned char*) malloc(len
);
310 if(buf
[i
] == 0 && buf
[i
+1] == 0 && buf
[i
+2] == 3)
312 dest
[j
] = dest
[j
+1] = 0;
323 dest
[j
] = buf
[len
-2];
324 dest
[j
+1] = buf
[len
-1];
329 picture
->fps
= picture
->timeinc_unit
= picture
->timeinc_resolution
= 0;
331 read_golomb(buf
, &n
);
333 if(read_golomb(buf
, &n
) == 3)
335 read_golomb(buf
, &n
);
336 read_golomb(buf
, &n
);
338 if(getbits(buf
, n
++, 1)){
339 //FIXME scaling matrix
342 read_golomb(buf
, &n
);
343 v
= read_golomb(buf
, &n
);
345 read_golomb(buf
, &n
);
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
;
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
);