2 * based on libmpeg2/header.c by Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
4 * This file is part of MPlayer.
6 * MPlayer is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * MPlayer is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 #include "libavutil/attributes.h"
31 static float frameratecode2framerate
[16] = {
33 // Official mpeg1/2 framerates: (1-8)
39 // libmpeg3's "Unofficial economy rates": (10-13)
41 // some invalid ones: (14-15)
46 int mp_header_process_sequence_header (mp_mpeg_header_t
* picture
, const unsigned char * buffer
)
50 if ((buffer
[6] & 0x20) != 0x20){
51 fprintf(stderr
, "missing marker bit!\n");
52 return 1; /* missing marker_bit */
55 height
= (buffer
[0] << 16) | (buffer
[1] << 8) | buffer
[2];
57 picture
->display_picture_width
= height
>> 12;
58 picture
->display_picture_height
= height
& 0xfff;
60 picture
->aspect_ratio_information
= buffer
[3] >> 4;
61 picture
->frame_rate_code
= buffer
[3] & 15;
62 picture
->fps
=frameratecode2framerate
[picture
->frame_rate_code
];
63 picture
->bitrate
= (buffer
[4]<<10)|(buffer
[5]<<2)|(buffer
[6]>>6);
65 picture
->picture_structure
= 3; //FRAME_PICTURE;
66 picture
->display_time
=100;
67 picture
->frame_rate_extension_n
= 1;
68 picture
->frame_rate_extension_d
= 1;
72 static int header_process_sequence_extension (mp_mpeg_header_t
* picture
,
73 unsigned char * buffer
)
75 /* check chroma format, size extensions, marker bit */
77 if ( ((buffer
[1] & 0x06) == 0x00) ||
78 ((buffer
[1] & 0x01) != 0x00) || (buffer
[2] & 0xe0) ||
79 ((buffer
[3] & 0x01) != 0x01) )
82 picture
->progressive_sequence
= (buffer
[1] >> 3) & 1;
83 picture
->frame_rate_extension_n
= ((buffer
[5] >> 5) & 3) + 1;
84 picture
->frame_rate_extension_d
= (buffer
[5] & 0x1f) + 1;
90 static int header_process_picture_coding_extension (mp_mpeg_header_t
* picture
, unsigned char * buffer
)
92 picture
->picture_structure
= buffer
[2] & 3;
93 picture
->top_field_first
= buffer
[3] >> 7;
94 picture
->repeat_first_field
= (buffer
[3] >> 1) & 1;
95 picture
->progressive_frame
= buffer
[4] >> 7;
97 // repeat_first implementation by A'rpi/ESP-team, based on libmpeg3:
98 picture
->display_time
=100;
99 if(picture
->repeat_first_field
){
100 if(picture
->progressive_sequence
){
101 if(picture
->top_field_first
)
102 picture
->display_time
+=200;
104 picture
->display_time
+=100;
106 if(picture
->progressive_frame
){
107 picture
->display_time
+=50;
110 //temopral hack. We calc time on every field, so if we have 2 fields
111 // interlaced we'll end with double time for 1 frame
112 if( picture
->picture_structure
!=3 ) picture
->display_time
/=2;
116 int mp_header_process_extension (mp_mpeg_header_t
* picture
, unsigned char * buffer
)
118 switch (buffer
[0] & 0xf0) {
119 case 0x10: /* sequence extension */
120 return header_process_sequence_extension (picture
, buffer
);
121 case 0x80: /* picture coding extension */
122 return header_process_picture_coding_extension (picture
, buffer
);
127 float mpeg12_aspect_info(mp_mpeg_header_t
*picture
)
131 switch(picture
->aspect_ratio_information
) {
132 case 2: // PAL/NTSC SVCD/DVD 4:3
133 case 8: // PAL VCD 4:3
134 case 12: // NTSC VCD 4:3
137 case 3: // PAL/NTSC Widescreen SVCD/DVD 16:9
138 case 6: // (PAL?)/NTSC Widescreen SVCD 16:9
141 case 4: // according to ISO-138182-2 Table 6.3
144 case 1: // VGA 1:1 - do not prescale
145 case 9: // Movie Type ??? / 640x480
149 mp_msg(MSGT_DECVIDEO
,MSGL_ERR
,"Detected unknown aspect_ratio_information in mpeg sequence header.\n"
150 "Please report the aspect value (%i) along with the movie type (VGA,PAL,NTSC,"
151 "SECAM) and the movie resolution (720x576,352x240,480x480,...) to the MPlayer"
152 " developers, so that we can add support for it!\nAssuming 1:1 aspect for now.\n",
153 picture
->aspect_ratio_information
);
160 unsigned char mp_getbits(unsigned char *buffer
, unsigned int from
, unsigned char len
)
163 unsigned char m
, u
, l
, y
;
168 l
= (len
> u
? len
- u
: 0);
170 y
= (buffer
[n
] << m
);
174 y
|= (buffer
[n
+1] >> (8-l
));
176 //fprintf(stderr, "GETBITS(%d -> %d): bytes=0x%x 0x%x, n=%d, m=%d, l=%d, u=%d, Y=%d\n",
177 // from, (int) len, (int) buffer[n],(int) buffer[n+1], n, (int) m, (int) l, (int) u, (int) y);
181 static inline unsigned int mp_getbits16(unsigned char *buffer
, unsigned int from
, unsigned char len
)
184 return (mp_getbits(buffer
, from
, len
- 8) << 8) | mp_getbits(buffer
, from
+ len
- 8, 8);
186 return mp_getbits(buffer
, from
, len
);
189 #define getbits mp_getbits
190 #define getbits16 mp_getbits16
192 static int read_timeinc(mp_mpeg_header_t
* picture
, unsigned char * buffer
, int n
)
194 if(picture
->timeinc_bits
> 8) {
195 picture
->timeinc_unit
= getbits(buffer
, n
, picture
->timeinc_bits
- 8) << 8;
196 n
+= picture
->timeinc_bits
- 8;
197 picture
->timeinc_unit
|= getbits(buffer
, n
, 8);
200 picture
->timeinc_unit
= getbits(buffer
, n
, picture
->timeinc_bits
);
201 n
+= picture
->timeinc_bits
;
203 //fprintf(stderr, "TIMEINC2: %d, bits: %d\n", picture->timeinc_unit, picture->timeinc_bits);
207 int mp4_header_process_vol(mp_mpeg_header_t
* picture
, unsigned char * buffer
)
209 unsigned int n
, aspect
=0, aspectw av_unused
=0, aspecth av_unused
=0, x
=1, v
;
211 //begins with 0x0000012x
213 picture
->timeinc_bits
= picture
->timeinc_resolution
= picture
->timeinc_unit
= 0;
215 if(getbits(buffer
, n
, 1))
218 aspect
=getbits(buffer
, n
, 4);
221 aspectw
= getbits(buffer
, n
, 8);
223 aspecth
= getbits(buffer
, n
, 8);
227 if(getbits(buffer
, n
, 1)) {
229 if(getbits(buffer
, n
, 1))
236 picture
->timeinc_resolution
= getbits(buffer
, n
, 8) << 8;
238 picture
->timeinc_resolution
|= getbits(buffer
, n
, 8);
241 picture
->timeinc_bits
= 0;
242 v
= picture
->timeinc_resolution
- 1;
245 picture
->timeinc_bits
++;
247 picture
->timeinc_bits
= (picture
->timeinc_bits
> 1 ? picture
->timeinc_bits
: 1);
251 if(getbits(buffer
, n
++, 1)) { //fixed_vop_timeinc
252 n
+= read_timeinc(picture
, buffer
, n
);
254 if(picture
->timeinc_unit
)
255 picture
->fps
= (float) picture
->timeinc_resolution
/ (float) picture
->timeinc_unit
;
259 picture
->display_picture_width
= getbits16(buffer
, n
, 13);
262 picture
->display_picture_height
= getbits16(buffer
, n
, 13);
265 //fprintf(stderr, "ASPECT: %d, PARW=%d, PARH=%d, TIMEINCRESOLUTION: %d, FIXED_TIMEINC: %d (number of bits: %d), FPS: %u\n",
266 // aspect, aspectw, aspecth, picture->timeinc_resolution, picture->timeinc_unit, picture->timeinc_bits, picture->fps);
271 void mp4_header_process_vop(mp_mpeg_header_t
* picture
, unsigned char * buffer
)
275 picture
->picture_type
= getbits(buffer
, n
, 2);
277 while(getbits(buffer
, n
, 1))
280 getbits(buffer
, n
, 1);
282 n
+= read_timeinc(picture
, buffer
, n
);
285 #define min(a, b) ((a) <= (b) ? (a) : (b))
287 static unsigned int read_golomb(unsigned char *buffer
, unsigned int *init
)
289 unsigned int x
, v
= 0, v2
= 0, m
, len
= 0, n
= *init
;
291 while(getbits(buffer
, n
++, 1) == 0)
298 v
|= getbits(buffer
, n
, m
);
305 for(n
= 0; n
< len
; n
++)
309 //fprintf(stderr, "READ_GOLOMB(%u), V=2^%u + %u-1 = %u\n", *init, len, v, v2);
314 inline static int read_golomb_s(unsigned char *buffer
, unsigned int *init
)
316 unsigned int v
= read_golomb(buffer
, init
);
317 return (v
& 1) ? ((v
+ 1) >> 1) : -(v
>> 1);
320 static int h264_parse_vui(mp_mpeg_header_t
* picture
, unsigned char * buf
, unsigned int n
)
322 unsigned int overscan
, vsp_color
, chroma
, timing
, fixed_fps
;
324 if(getbits(buf
, n
++, 1))
326 picture
->aspect_ratio_information
= getbits(buf
, n
, 8);
328 if(picture
->aspect_ratio_information
== 255)
330 picture
->display_picture_width
= (getbits(buf
, n
, 8) << 8) | getbits(buf
, n
+ 8, 8);
333 picture
->display_picture_height
= (getbits(buf
, n
, 8) << 8) | getbits(buf
, n
+ 8, 8);
338 if((overscan
=getbits(buf
, n
++, 1)))
340 if((vsp_color
=getbits(buf
, n
++, 1)))
343 if(getbits(buf
, n
++, 1))
346 if((chroma
=getbits(buf
, n
++, 1)))
348 read_golomb(buf
, &n
);
349 read_golomb(buf
, &n
);
351 if((timing
=getbits(buf
, n
++, 1)))
353 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);
356 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);
359 fixed_fps
= getbits(buf
, n
, 1);
361 if(picture
->timeinc_unit
> 0 && picture
->timeinc_resolution
> 0)
362 picture
->fps
= (float) picture
->timeinc_resolution
/ (float) picture
->timeinc_unit
;
367 //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,
368 // picture->timeinc_resolution, picture->timeinc_unit, picture->timeinc_unit, fixed_fps);
373 static int mp_unescape03(unsigned char *buf
, int len
)
385 if(buf
[i
] == 0 && buf
[i
+1] == 0 && buf
[i
+2] == 3)
387 dest
[j
] = dest
[j
+1] = 0;
399 dest
[j
] = buf
[len
-2];
400 dest
[j
+1] = buf
[len
-1];
402 memcpy(buf
, dest
, len
);
408 int h264_parse_sps(mp_mpeg_header_t
* picture
, unsigned char * buf
, int len
)
410 unsigned int n
= 0, v
, i
, k
, mbh
;
413 len
= mp_unescape03(buf
, len
);
415 picture
->fps
= picture
->timeinc_unit
= picture
->timeinc_resolution
= 0;
417 read_golomb(buf
, &n
);
419 if(read_golomb(buf
, &n
) == 3)
421 read_golomb(buf
, &n
);
422 read_golomb(buf
, &n
);
424 if(getbits(buf
, n
++, 1)){
425 for(i
= 0; i
< 8; i
++)
426 { // scaling list is skipped for now
427 if(getbits(buf
, n
++, 1))
430 for(k
= (i
< 6 ? 16 : 64); k
&& v
; k
--)
431 v
= (v
+ read_golomb_s(buf
, &n
)) & 255;
436 read_golomb(buf
, &n
);
437 v
= read_golomb(buf
, &n
);
439 read_golomb(buf
, &n
);
442 getbits(buf
, n
++, 1);
443 read_golomb(buf
, &n
);
444 read_golomb(buf
, &n
);
445 v
= read_golomb(buf
, &n
);
446 for(i
= 0; i
< v
; i
++)
447 read_golomb(buf
, &n
);
449 read_golomb(buf
, &n
);
450 getbits(buf
, n
++, 1);
451 picture
->display_picture_width
= 16 *(read_golomb(buf
, &n
)+1);
452 mbh
= read_golomb(buf
, &n
)+1;
453 frame_mbs_only
= getbits(buf
, n
++, 1);
454 picture
->display_picture_height
= 16 * (2 - frame_mbs_only
) * mbh
;
456 getbits(buf
, n
++, 1);
457 getbits(buf
, n
++, 1);
458 if(getbits(buf
, n
++, 1))
460 read_golomb(buf
, &n
);
461 read_golomb(buf
, &n
);
462 read_golomb(buf
, &n
);
463 read_golomb(buf
, &n
);
465 if(getbits(buf
, n
++, 1))
466 n
= h264_parse_vui(picture
, buf
, n
);
471 int mp_vc1_decode_sequence_header(mp_mpeg_header_t
* picture
, unsigned char * buf
, int len
)
475 len
= mp_unescape03(buf
, len
);
477 picture
->display_picture_width
= picture
->display_picture_height
= 0;
480 x
= getbits(buf
, n
, 2);
482 if(x
!= 3) //not advanced profile
485 getbits16(buf
, n
, 14);
487 picture
->display_picture_width
= getbits16(buf
, n
, 12) * 2 + 2;
489 picture
->display_picture_height
= getbits16(buf
, n
, 12) * 2 + 2;
493 x
= getbits(buf
, n
, 1);
497 getbits16(buf
, n
, 14);
499 getbits16(buf
, n
, 14);
501 if(getbits(buf
, n
++, 1)) //aspect ratio
503 x
= getbits(buf
, n
, 4);
507 getbits16(buf
, n
, 16);
512 if(getbits(buf
, n
++, 1)) //framerates
514 int frexp
=0, frnum
=0, frden
=0;
516 if(getbits(buf
, n
++, 1))
518 frexp
= getbits16(buf
, n
, 16);
520 picture
->fps
= (double) (frexp
+1) / 32.0;
524 float frates
[] = {0, 24000, 25000, 30000, 50000, 60000, 48000, 72000, 0};
525 float frdivs
[] = {0, 1000, 1001, 0};
527 frnum
= getbits(buf
, n
, 8);
529 frden
= getbits(buf
, n
, 4);
531 if((frden
== 1 || frden
== 2) && (frnum
< 8))
532 picture
->fps
= frates
[frnum
] / frdivs
[frden
];