2 // based on libmpeg2/header.c by Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
14 static float frameratecode2framerate
[16] = {
16 // Official mpeg1/2 framerates: (1-8)
22 // libmpeg3's "Unofficial economy rates": (10-13)
24 // some invalid ones: (14-15)
29 int mp_header_process_sequence_header (mp_mpeg_header_t
* picture
, const unsigned char * buffer
)
33 if ((buffer
[6] & 0x20) != 0x20){
34 fprintf(stderr
, "missing marker bit!\n");
35 return 1; /* missing marker_bit */
38 height
= (buffer
[0] << 16) | (buffer
[1] << 8) | buffer
[2];
40 picture
->display_picture_width
= (height
>> 12);
41 picture
->display_picture_height
= (height
& 0xfff);
43 width
= ((height
>> 12) + 15) & ~15;
44 height
= ((height
& 0xfff) + 15) & ~15;
46 picture
->aspect_ratio_information
= buffer
[3] >> 4;
47 picture
->frame_rate_code
= buffer
[3] & 15;
48 picture
->fps
=frameratecode2framerate
[picture
->frame_rate_code
];
49 picture
->bitrate
= (buffer
[4]<<10)|(buffer
[5]<<2)|(buffer
[6]>>6);
51 picture
->picture_structure
= 3; //FRAME_PICTURE;
52 picture
->display_time
=100;
56 static int header_process_sequence_extension (mp_mpeg_header_t
* picture
,
57 unsigned char * buffer
)
59 /* check chroma format, size extensions, marker bit */
61 if ( ((buffer
[1] & 0x06) == 0x00) ||
62 ((buffer
[1] & 0x01) != 0x00) || (buffer
[2] & 0xe0) ||
63 ((buffer
[3] & 0x01) != 0x01) )
66 picture
->progressive_sequence
= (buffer
[1] >> 3) & 1;
71 static int header_process_picture_coding_extension (mp_mpeg_header_t
* picture
, unsigned char * buffer
)
73 picture
->picture_structure
= buffer
[2] & 3;
74 picture
->top_field_first
= buffer
[3] >> 7;
75 picture
->repeat_first_field
= (buffer
[3] >> 1) & 1;
76 picture
->progressive_frame
= buffer
[4] >> 7;
78 // repeat_first implementation by A'rpi/ESP-team, based on libmpeg3:
79 picture
->display_time
=100;
80 if(picture
->repeat_first_field
){
81 if(picture
->progressive_sequence
){
82 if(picture
->top_field_first
)
83 picture
->display_time
+=200;
85 picture
->display_time
+=100;
87 if(picture
->progressive_frame
){
88 picture
->display_time
+=50;
91 //temopral hack. We calc time on every field, so if we have 2 fields
92 // interlaced we'll end with double time for 1 frame
93 if( picture
->picture_structure
!=3 ) picture
->display_time
/=2;
97 int mp_header_process_extension (mp_mpeg_header_t
* picture
, unsigned char * buffer
)
99 switch (buffer
[0] & 0xf0) {
100 case 0x10: /* sequence extension */
101 return header_process_sequence_extension (picture
, buffer
);
102 case 0x80: /* picture coding extension */
103 return header_process_picture_coding_extension (picture
, buffer
);
108 float mpeg12_aspect_info(mp_mpeg_header_t
*picture
)
112 switch(picture
->aspect_ratio_information
) {
113 case 2: // PAL/NTSC SVCD/DVD 4:3
114 case 8: // PAL VCD 4:3
115 case 12: // NTSC VCD 4:3
118 case 3: // PAL/NTSC Widescreen SVCD/DVD 16:9
119 case 6: // (PAL?)/NTSC Widescreen SVCD 16:9
122 case 4: // according to ISO-138182-2 Table 6.3
125 case 1: // VGA 1:1 - do not prescale
126 case 9: // Movie Type ??? / 640x480
130 mp_msg(MSGT_DECVIDEO
,MSGL_ERR
,"Detected unknown aspect_ratio_information in mpeg sequence header.\n"
131 "Please report the aspect value (%i) along with the movie type (VGA,PAL,NTSC,"
132 "SECAM) and the movie resolution (720x576,352x240,480x480,...) to the MPlayer"
133 " developers, so that we can add support for it!\nAssuming 1:1 aspect for now.\n",
134 picture
->aspect_ratio_information
);
141 unsigned char mp_getbits(unsigned char *buffer
, unsigned int from
, unsigned char len
)
144 unsigned char m
, u
, l
, y
;
149 l
= (len
> u
? len
- u
: 0);
151 y
= (buffer
[n
] << m
);
155 y
|= (buffer
[n
+1] >> (8-l
));
157 //fprintf(stderr, "GETBITS(%d -> %d): bytes=0x%x 0x%x, n=%d, m=%d, l=%d, u=%d, Y=%d\n",
158 // from, (int) len, (int) buffer[n],(int) buffer[n+1], n, (int) m, (int) l, (int) u, (int) y);
162 static inline unsigned int mp_getbits16(unsigned char *buffer
, unsigned int from
, unsigned char len
)
165 return (mp_getbits(buffer
, from
, len
- 8) << 8) | mp_getbits(buffer
, from
+ len
- 8, 8);
167 return mp_getbits(buffer
, from
, len
);
170 #define getbits mp_getbits
171 #define getbits16 mp_getbits16
173 static int read_timeinc(mp_mpeg_header_t
* picture
, unsigned char * buffer
, int n
)
175 if(picture
->timeinc_bits
> 8) {
176 picture
->timeinc_unit
= getbits(buffer
, n
, picture
->timeinc_bits
- 8) << 8;
177 n
+= picture
->timeinc_bits
- 8;
178 picture
->timeinc_unit
|= getbits(buffer
, n
, 8);
181 picture
->timeinc_unit
= getbits(buffer
, n
, picture
->timeinc_bits
);
182 n
+= picture
->timeinc_bits
;
184 //fprintf(stderr, "TIMEINC2: %d, bits: %d\n", picture->timeinc_unit, picture->timeinc_bits);
188 int mp4_header_process_vol(mp_mpeg_header_t
* picture
, unsigned char * buffer
)
190 unsigned int n
, aspect
=0, aspectw
=0, aspecth
=0, x
=1, v
;
192 //begins with 0x0000012x
194 picture
->timeinc_bits
= picture
->timeinc_resolution
= picture
->timeinc_unit
= 0;
196 if(getbits(buffer
, n
, 1))
199 aspect
=getbits(buffer
, n
, 4);
202 aspectw
= getbits(buffer
, n
, 8);
204 aspecth
= getbits(buffer
, n
, 8);
208 if(getbits(buffer
, n
, 1)) {
210 if(getbits(buffer
, n
, 1))
217 picture
->timeinc_resolution
= getbits(buffer
, n
, 8) << 8;
219 picture
->timeinc_resolution
|= getbits(buffer
, n
, 8);
222 picture
->timeinc_bits
= 0;
223 v
= picture
->timeinc_resolution
- 1;
226 picture
->timeinc_bits
++;
228 picture
->timeinc_bits
= (picture
->timeinc_bits
> 1 ? picture
->timeinc_bits
: 1);
232 if(getbits(buffer
, n
, 1)) { //fixed_vop_timeinc
234 n
= read_timeinc(picture
, buffer
, n
);
236 if(picture
->timeinc_unit
)
237 picture
->fps
= (float) picture
->timeinc_resolution
/ (float) picture
->timeinc_unit
;
240 //fprintf(stderr, "ASPECT: %d, PARW=%d, PARH=%d, TIMEINCRESOLUTION: %d, FIXED_TIMEINC: %d (number of bits: %d), FPS: %u\n",
241 // aspect, aspectw, aspecth, picture->timeinc_resolution, picture->timeinc_unit, picture->timeinc_bits, picture->fps);
246 void mp4_header_process_vop(mp_mpeg_header_t
* picture
, unsigned char * buffer
)
250 picture
->picture_type
= getbits(buffer
, n
, 2);
252 while(getbits(buffer
, n
, 1))
255 getbits(buffer
, n
, 1);
257 n
= read_timeinc(picture
, buffer
, n
);
260 #define min(a, b) ((a) <= (b) ? (a) : (b))
262 static unsigned int read_golomb(unsigned char *buffer
, unsigned int *init
)
264 unsigned int x
, v
= 0, v2
= 0, m
, len
= 0, n
= *init
;
266 while(getbits(buffer
, n
++, 1) == 0)
273 v
|= getbits(buffer
, n
, m
);
280 for(n
= 0; n
< len
; n
++)
284 //fprintf(stderr, "READ_GOLOMB(%u), V=2^%u + %u-1 = %u\n", *init, len, v, v2);
290 static int h264_parse_vui(mp_mpeg_header_t
* picture
, unsigned char * buf
, unsigned int n
)
292 unsigned int overscan
, vsp_color
, chroma
, timing
, fixed_fps
;
294 if(getbits(buf
, n
++, 1))
296 picture
->aspect_ratio_information
= getbits(buf
, n
, 8);
298 if(picture
->aspect_ratio_information
== 255)
300 picture
->display_picture_width
= (getbits(buf
, n
, 8) << 8) | getbits(buf
, n
+ 8, 8);
303 picture
->display_picture_height
= (getbits(buf
, n
, 8) << 8) | getbits(buf
, n
+ 8, 8);
308 if((overscan
=getbits(buf
, n
++, 1)))
310 if((vsp_color
=getbits(buf
, n
++, 1)))
313 if(getbits(buf
, n
++, 1))
316 if((chroma
=getbits(buf
, n
++, 1)))
318 read_golomb(buf
, &n
);
319 read_golomb(buf
, &n
);
321 if((timing
=getbits(buf
, n
++, 1)))
323 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);
326 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);
329 fixed_fps
= getbits(buf
, n
, 1);
331 if(picture
->timeinc_unit
> 0 && picture
->timeinc_resolution
> 0)
332 picture
->fps
= (float) picture
->timeinc_resolution
/ (float) picture
->timeinc_unit
;
337 //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,
338 // picture->timeinc_resolution, picture->timeinc_unit, picture->timeinc_unit, fixed_fps);
343 static int mp_unescape03(unsigned char *buf
, int len
);
345 int h264_parse_sps(mp_mpeg_header_t
* picture
, unsigned char * buf
, int len
)
347 unsigned int n
= 0, v
, i
, mbh
;
350 len
= mp_unescape03(buf
, len
);
352 picture
->fps
= picture
->timeinc_unit
= picture
->timeinc_resolution
= 0;
354 read_golomb(buf
, &n
);
356 if(read_golomb(buf
, &n
) == 3)
358 read_golomb(buf
, &n
);
359 read_golomb(buf
, &n
);
361 if(getbits(buf
, n
++, 1)){
362 //FIXME scaling matrix
365 read_golomb(buf
, &n
);
366 v
= read_golomb(buf
, &n
);
368 read_golomb(buf
, &n
);
371 getbits(buf
, n
++, 1);
372 read_golomb(buf
, &n
);
373 read_golomb(buf
, &n
);
374 v
= read_golomb(buf
, &n
);
375 for(i
= 0; i
< v
; i
++)
376 read_golomb(buf
, &n
);
378 read_golomb(buf
, &n
);
379 getbits(buf
, n
++, 1);
380 picture
->display_picture_width
= 16 *(read_golomb(buf
, &n
)+1);
381 mbh
= read_golomb(buf
, &n
)+1;
382 frame_mbs_only
= getbits(buf
, n
++, 1);
383 picture
->display_picture_height
= 16 * (2 - frame_mbs_only
) * mbh
;
385 getbits(buf
, n
++, 1);
386 getbits(buf
, n
++, 1);
387 if(getbits(buf
, n
++, 1))
389 read_golomb(buf
, &n
);
390 read_golomb(buf
, &n
);
391 read_golomb(buf
, &n
);
392 read_golomb(buf
, &n
);
394 if(getbits(buf
, n
++, 1))
395 n
= h264_parse_vui(picture
, buf
, n
);
400 static int mp_unescape03(unsigned char *buf
, int len
)
412 if(buf
[i
] == 0 && buf
[i
+1] == 0 && buf
[i
+2] == 3)
414 dest
[j
] = dest
[j
+1] = 0;
426 dest
[j
] = buf
[len
-2];
427 dest
[j
+1] = buf
[len
-1];
429 memcpy(buf
, dest
, len
);
435 int mp_vc1_decode_sequence_header(mp_mpeg_header_t
* picture
, unsigned char * buf
, int len
)
439 len
= mp_unescape03(buf
, len
);
441 picture
->display_picture_width
= picture
->display_picture_height
= 0;
444 x
= getbits(buf
, n
, 2);
446 if(x
!= 3) //not advanced profile
449 getbits16(buf
, n
, 14);
451 picture
->display_picture_width
= getbits16(buf
, n
, 12) * 2 + 2;
453 picture
->display_picture_height
= getbits16(buf
, n
, 12) * 2 + 2;
457 x
= getbits(buf
, n
, 1);
461 getbits16(buf
, n
, 14);
463 getbits16(buf
, n
, 14);
465 if(getbits(buf
, n
++, 1)) //aspect ratio
467 x
= getbits(buf
, n
, 4);
471 getbits16(buf
, n
, 16);
476 if(getbits(buf
, n
++, 1)) //framerates
478 int frexp
=0, frnum
=0, frden
=0;
480 if(getbits(buf
, n
++, 1))
482 frexp
= getbits16(buf
, n
, 16);
484 picture
->fps
= (double) (frexp
+1) / 32.0;
488 float frates
[] = {0, 24000, 25000, 30000, 50000, 60000, 48000, 72000, 0};
489 float frdivs
[] = {0, 1000, 1001, 0};
491 frnum
= getbits(buf
, n
, 8);
493 frden
= getbits(buf
, n
, 4);
495 if((frden
== 1 || frden
== 2) && (frnum
< 8))
496 picture
->fps
= frates
[frnum
] / frdivs
[frden
];