options: Make dynamic dup hack work with new options
[mplayer.git] / libmpdemux / mpeg_hdr.c
blob3e4bbfe31bc316804fcd5f6770e2796cd839b67a
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>
7 #include <string.h>
9 #include "config.h"
10 #include "mpeg_hdr.h"
12 #include "mp_msg.h"
14 static float frameratecode2framerate[16] = {
16 // Official mpeg1/2 framerates: (1-8)
17 24000.0/1001, 24,25,
18 30000.0/1001, 30,50,
19 60000.0/1001, 60,
20 // Xing's 15fps: (9)
21 15,
22 // libmpeg3's "Unofficial economy rates": (10-13)
23 5,10,12,15,
24 // some invalid ones: (14-15)
25 0,0
29 int mp_header_process_sequence_header (mp_mpeg_header_t * picture, const unsigned char * buffer)
31 int width, height;
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);
50 picture->mpeg1 = 1;
51 picture->picture_structure = 3; //FRAME_PICTURE;
52 picture->display_time=100;
53 return 0;
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) )
64 return 1;
66 picture->progressive_sequence = (buffer[1] >> 3) & 1;
67 picture->mpeg1 = 0;
68 return 0;
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;
84 else
85 picture->display_time+=100;
86 } else
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;
94 return 0;
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);
105 return 0;
108 float mpeg12_aspect_info(mp_mpeg_header_t *picture)
110 float aspect = 0.0;
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
116 aspect=4.0/3.0;
117 break;
118 case 3: // PAL/NTSC Widescreen SVCD/DVD 16:9
119 case 6: // (PAL?)/NTSC Widescreen SVCD 16:9
120 aspect=16.0/9.0;
121 break;
122 case 4: // according to ISO-138182-2 Table 6.3
123 aspect=2.21;
124 break;
125 case 1: // VGA 1:1 - do not prescale
126 case 9: // Movie Type ??? / 640x480
127 aspect=0.0;
128 break;
129 default:
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);
137 return aspect;
140 //MPEG4 HEADERS
141 unsigned char mp_getbits(unsigned char *buffer, unsigned int from, unsigned char len)
143 unsigned int n;
144 unsigned char m, u, l, y;
146 n = from / 8;
147 m = from % 8;
148 u = 8 - m;
149 l = (len > u ? len - u : 0);
151 y = (buffer[n] << m);
152 if(8 > len)
153 y >>= (8-len);
154 if(l)
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);
159 return y;
162 static inline unsigned int mp_getbits16(unsigned char *buffer, unsigned int from, unsigned char len)
164 if(len > 8)
165 return (mp_getbits(buffer, from, len - 8) << 8) | mp_getbits(buffer, from + len - 8, 8);
166 else
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);
179 n += 8;
180 } else {
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);
185 return n;
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
193 picture->fps = 0;
194 picture->timeinc_bits = picture->timeinc_resolution = picture->timeinc_unit = 0;
195 n = 9;
196 if(getbits(buffer, n, 1))
197 n += 7;
198 n++;
199 aspect=getbits(buffer, n, 4);
200 n += 4;
201 if(aspect == 0x0f) {
202 aspectw = getbits(buffer, n, 8);
203 n += 8;
204 aspecth = getbits(buffer, n, 8);
205 n += 8;
208 if(getbits(buffer, n, 1)) {
209 n += 4;
210 if(getbits(buffer, n, 1))
211 n += 79;
212 n++;
213 } else n++;
215 n+=3;
217 picture->timeinc_resolution = getbits(buffer, n, 8) << 8;
218 n += 8;
219 picture->timeinc_resolution |= getbits(buffer, n, 8);
220 n += 8;
222 picture->timeinc_bits = 0;
223 v = picture->timeinc_resolution - 1;
224 while(v && (x<16)) {
225 v>>=1;
226 picture->timeinc_bits++;
228 picture->timeinc_bits = (picture->timeinc_bits > 1 ? picture->timeinc_bits : 1);
230 n++; //marker bit
232 if(getbits(buffer, n, 1)) { //fixed_vop_timeinc
233 n++;
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);
243 return 0;
246 void mp4_header_process_vop(mp_mpeg_header_t * picture, unsigned char * buffer)
248 int n;
249 n = 0;
250 picture->picture_type = getbits(buffer, n, 2);
251 n += 2;
252 while(getbits(buffer, n, 1))
253 n++;
254 n++;
255 getbits(buffer, n, 1);
256 n++;
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)
267 len++;
269 x = len + n;
270 while(n < x)
272 m = min(x - n, 8);
273 v |= getbits(buffer, n, m);
274 n += m;
275 if(x - n > 8)
276 v <<= 8;
279 v2 = 1;
280 for(n = 0; n < len; n++)
281 v2 <<= 1;
282 v2 = (v2 - 1) + v;
284 //fprintf(stderr, "READ_GOLOMB(%u), V=2^%u + %u-1 = %u\n", *init, len, v, v2);
285 *init = x;
286 return 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);
297 n += 8;
298 if(picture->aspect_ratio_information == 255)
300 picture->display_picture_width = (getbits(buf, n, 8) << 8) | getbits(buf, n + 8, 8);
301 n += 16;
303 picture->display_picture_height = (getbits(buf, n, 8) << 8) | getbits(buf, n + 8, 8);
304 n += 16;
308 if((overscan=getbits(buf, n++, 1)))
309 n++;
310 if((vsp_color=getbits(buf, n++, 1)))
312 n += 4;
313 if(getbits(buf, n++, 1))
314 n += 24;
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);
324 n += 32;
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);
327 n += 32;
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;
333 if(fixed_fps)
334 picture->fps /= 2;
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);
340 return n;
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;
348 int frame_mbs_only;
350 len = mp_unescape03(buf, len);
352 picture->fps = picture->timeinc_unit = picture->timeinc_resolution = 0;
353 n = 24;
354 read_golomb(buf, &n);
355 if(buf[0] >= 100){
356 if(read_golomb(buf, &n) == 3)
357 n++;
358 read_golomb(buf, &n);
359 read_golomb(buf, &n);
360 n++;
361 if(getbits(buf, n++, 1)){
362 //FIXME scaling matrix
365 read_golomb(buf, &n);
366 v = read_golomb(buf, &n);
367 if(v == 0)
368 read_golomb(buf, &n);
369 else if(v == 1)
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;
384 if(!frame_mbs_only)
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);
397 return n;
400 static int mp_unescape03(unsigned char *buf, int len)
402 unsigned char *dest;
403 int i, j, skip;
405 dest = malloc(len);
406 if(! dest)
407 return 0;
409 j = i = skip = 0;
410 while(i <= len-3)
412 if(buf[i] == 0 && buf[i+1] == 0 && buf[i+2] == 3)
414 dest[j] = dest[j+1] = 0;
415 j += 2;
416 i += 3;
417 skip++;
419 else
421 dest[j] = buf[i];
422 j++;
423 i++;
426 dest[j] = buf[len-2];
427 dest[j+1] = buf[len-1];
428 len -= skip;
429 memcpy(buf, dest, len);
430 free(dest);
432 return len;
435 int mp_vc1_decode_sequence_header(mp_mpeg_header_t * picture, unsigned char * buf, int len)
437 int n, x;
439 len = mp_unescape03(buf, len);
441 picture->display_picture_width = picture->display_picture_height = 0;
442 picture->fps = 0;
443 n = 0;
444 x = getbits(buf, n, 2);
445 n += 2;
446 if(x != 3) //not advanced profile
447 return 0;
449 getbits16(buf, n, 14);
450 n += 14;
451 picture->display_picture_width = getbits16(buf, n, 12) * 2 + 2;
452 n += 12;
453 picture->display_picture_height = getbits16(buf, n, 12) * 2 + 2;
454 n += 12;
455 getbits(buf, n, 6);
456 n += 6;
457 x = getbits(buf, n, 1);
458 n += 1;
459 if(x) //display info
461 getbits16(buf, n, 14);
462 n += 14;
463 getbits16(buf, n, 14);
464 n += 14;
465 if(getbits(buf, n++, 1)) //aspect ratio
467 x = getbits(buf, n, 4);
468 n += 4;
469 if(x == 15)
471 getbits16(buf, n, 16);
472 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);
483 n += 16;
484 picture->fps = (double) (frexp+1) / 32.0;
486 else
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);
492 n += 8;
493 frden = getbits(buf, n, 4);
494 n += 4;
495 if((frden == 1 || frden == 2) && (frnum < 8))
496 picture->fps = frates[frnum] / frdivs[frden];
501 //free(dest);
502 return 1;