debian/: delegate handling of mplayer.conf to dpkg.
[mplayer/glamo.git] / libmpdemux / mpeg_hdr.c
blobc07cd6bf16ce6aeb6494337768a637f4b5a08657
1 /*
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.
21 #include <inttypes.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
26 #include "config.h"
27 #include "mpeg_hdr.h"
29 #include "mp_msg.h"
31 static float frameratecode2framerate[16] = {
33 // Official mpeg1/2 framerates: (1-8)
34 24000.0/1001, 24,25,
35 30000.0/1001, 30,50,
36 60000.0/1001, 60,
37 // Xing's 15fps: (9)
38 15,
39 // libmpeg3's "Unofficial economy rates": (10-13)
40 5,10,12,15,
41 // some invalid ones: (14-15)
42 0,0
46 int mp_header_process_sequence_header (mp_mpeg_header_t * picture, const unsigned char * buffer)
48 int width, height;
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 width = ((height >> 12) + 15) & ~15;
61 height = ((height & 0xfff) + 15) & ~15;
63 picture->aspect_ratio_information = buffer[3] >> 4;
64 picture->frame_rate_code = buffer[3] & 15;
65 picture->fps=frameratecode2framerate[picture->frame_rate_code];
66 picture->bitrate = (buffer[4]<<10)|(buffer[5]<<2)|(buffer[6]>>6);
67 picture->mpeg1 = 1;
68 picture->picture_structure = 3; //FRAME_PICTURE;
69 picture->display_time=100;
70 return 0;
73 static int header_process_sequence_extension (mp_mpeg_header_t * picture,
74 unsigned char * buffer)
76 /* check chroma format, size extensions, marker bit */
78 if ( ((buffer[1] & 0x06) == 0x00) ||
79 ((buffer[1] & 0x01) != 0x00) || (buffer[2] & 0xe0) ||
80 ((buffer[3] & 0x01) != 0x01) )
81 return 1;
83 picture->progressive_sequence = (buffer[1] >> 3) & 1;
84 picture->mpeg1 = 0;
85 return 0;
88 static int header_process_picture_coding_extension (mp_mpeg_header_t * picture, unsigned char * buffer)
90 picture->picture_structure = buffer[2] & 3;
91 picture->top_field_first = buffer[3] >> 7;
92 picture->repeat_first_field = (buffer[3] >> 1) & 1;
93 picture->progressive_frame = buffer[4] >> 7;
95 // repeat_first implementation by A'rpi/ESP-team, based on libmpeg3:
96 picture->display_time=100;
97 if(picture->repeat_first_field){
98 if(picture->progressive_sequence){
99 if(picture->top_field_first)
100 picture->display_time+=200;
101 else
102 picture->display_time+=100;
103 } else
104 if(picture->progressive_frame){
105 picture->display_time+=50;
108 //temopral hack. We calc time on every field, so if we have 2 fields
109 // interlaced we'll end with double time for 1 frame
110 if( picture->picture_structure!=3 ) picture->display_time/=2;
111 return 0;
114 int mp_header_process_extension (mp_mpeg_header_t * picture, unsigned char * buffer)
116 switch (buffer[0] & 0xf0) {
117 case 0x10: /* sequence extension */
118 return header_process_sequence_extension (picture, buffer);
119 case 0x80: /* picture coding extension */
120 return header_process_picture_coding_extension (picture, buffer);
122 return 0;
125 float mpeg12_aspect_info(mp_mpeg_header_t *picture)
127 float aspect = 0.0;
129 switch(picture->aspect_ratio_information) {
130 case 2: // PAL/NTSC SVCD/DVD 4:3
131 case 8: // PAL VCD 4:3
132 case 12: // NTSC VCD 4:3
133 aspect=4.0/3.0;
134 break;
135 case 3: // PAL/NTSC Widescreen SVCD/DVD 16:9
136 case 6: // (PAL?)/NTSC Widescreen SVCD 16:9
137 aspect=16.0/9.0;
138 break;
139 case 4: // according to ISO-138182-2 Table 6.3
140 aspect=2.21;
141 break;
142 case 1: // VGA 1:1 - do not prescale
143 case 9: // Movie Type ??? / 640x480
144 aspect=0.0;
145 break;
146 default:
147 mp_msg(MSGT_DECVIDEO,MSGL_ERR,"Detected unknown aspect_ratio_information in mpeg sequence header.\n"
148 "Please report the aspect value (%i) along with the movie type (VGA,PAL,NTSC,"
149 "SECAM) and the movie resolution (720x576,352x240,480x480,...) to the MPlayer"
150 " developers, so that we can add support for it!\nAssuming 1:1 aspect for now.\n",
151 picture->aspect_ratio_information);
154 return aspect;
157 //MPEG4 HEADERS
158 unsigned char mp_getbits(unsigned char *buffer, unsigned int from, unsigned char len)
160 unsigned int n;
161 unsigned char m, u, l, y;
163 n = from / 8;
164 m = from % 8;
165 u = 8 - m;
166 l = (len > u ? len - u : 0);
168 y = (buffer[n] << m);
169 if(8 > len)
170 y >>= (8-len);
171 if(l)
172 y |= (buffer[n+1] >> (8-l));
174 //fprintf(stderr, "GETBITS(%d -> %d): bytes=0x%x 0x%x, n=%d, m=%d, l=%d, u=%d, Y=%d\n",
175 // from, (int) len, (int) buffer[n],(int) buffer[n+1], n, (int) m, (int) l, (int) u, (int) y);
176 return y;
179 static inline unsigned int mp_getbits16(unsigned char *buffer, unsigned int from, unsigned char len)
181 if(len > 8)
182 return (mp_getbits(buffer, from, len - 8) << 8) | mp_getbits(buffer, from + len - 8, 8);
183 else
184 return mp_getbits(buffer, from, len);
187 #define getbits mp_getbits
188 #define getbits16 mp_getbits16
190 static int read_timeinc(mp_mpeg_header_t * picture, unsigned char * buffer, int n)
192 if(picture->timeinc_bits > 8) {
193 picture->timeinc_unit = getbits(buffer, n, picture->timeinc_bits - 8) << 8;
194 n += picture->timeinc_bits - 8;
195 picture->timeinc_unit |= getbits(buffer, n, 8);
196 n += 8;
197 } else {
198 picture->timeinc_unit = getbits(buffer, n, picture->timeinc_bits);
199 n += picture->timeinc_bits;
201 //fprintf(stderr, "TIMEINC2: %d, bits: %d\n", picture->timeinc_unit, picture->timeinc_bits);
202 return n;
205 int mp4_header_process_vol(mp_mpeg_header_t * picture, unsigned char * buffer)
207 unsigned int n, aspect=0, aspectw=0, aspecth=0, x=1, v;
209 //begins with 0x0000012x
210 picture->fps = 0;
211 picture->timeinc_bits = picture->timeinc_resolution = picture->timeinc_unit = 0;
212 n = 9;
213 if(getbits(buffer, n, 1))
214 n += 7;
215 n++;
216 aspect=getbits(buffer, n, 4);
217 n += 4;
218 if(aspect == 0x0f) {
219 aspectw = getbits(buffer, n, 8);
220 n += 8;
221 aspecth = getbits(buffer, n, 8);
222 n += 8;
225 if(getbits(buffer, n, 1)) {
226 n += 4;
227 if(getbits(buffer, n, 1))
228 n += 79;
229 n++;
230 } else n++;
232 n+=3;
234 picture->timeinc_resolution = getbits(buffer, n, 8) << 8;
235 n += 8;
236 picture->timeinc_resolution |= getbits(buffer, n, 8);
237 n += 8;
239 picture->timeinc_bits = 0;
240 v = picture->timeinc_resolution - 1;
241 while(v && (x<16)) {
242 v>>=1;
243 picture->timeinc_bits++;
245 picture->timeinc_bits = (picture->timeinc_bits > 1 ? picture->timeinc_bits : 1);
247 n++; //marker bit
249 if(getbits(buffer, n, 1)) { //fixed_vop_timeinc
250 n++;
251 n = read_timeinc(picture, buffer, n);
253 if(picture->timeinc_unit)
254 picture->fps = (float) picture->timeinc_resolution / (float) picture->timeinc_unit;
257 //fprintf(stderr, "ASPECT: %d, PARW=%d, PARH=%d, TIMEINCRESOLUTION: %d, FIXED_TIMEINC: %d (number of bits: %d), FPS: %u\n",
258 // aspect, aspectw, aspecth, picture->timeinc_resolution, picture->timeinc_unit, picture->timeinc_bits, picture->fps);
260 return 0;
263 void mp4_header_process_vop(mp_mpeg_header_t * picture, unsigned char * buffer)
265 int n;
266 n = 0;
267 picture->picture_type = getbits(buffer, n, 2);
268 n += 2;
269 while(getbits(buffer, n, 1))
270 n++;
271 n++;
272 getbits(buffer, n, 1);
273 n++;
274 n = read_timeinc(picture, buffer, n);
277 #define min(a, b) ((a) <= (b) ? (a) : (b))
279 static unsigned int read_golomb(unsigned char *buffer, unsigned int *init)
281 unsigned int x, v = 0, v2 = 0, m, len = 0, n = *init;
283 while(getbits(buffer, n++, 1) == 0)
284 len++;
286 x = len + n;
287 while(n < x)
289 m = min(x - n, 8);
290 v |= getbits(buffer, n, m);
291 n += m;
292 if(x - n > 8)
293 v <<= 8;
296 v2 = 1;
297 for(n = 0; n < len; n++)
298 v2 <<= 1;
299 v2 = (v2 - 1) + v;
301 //fprintf(stderr, "READ_GOLOMB(%u), V=2^%u + %u-1 = %u\n", *init, len, v, v2);
302 *init = x;
303 return v2;
306 inline static int read_golomb_s(unsigned char *buffer, unsigned int *init)
308 unsigned int v = read_golomb(buffer, init);
309 return (v & 1) ? ((v + 1) >> 1) : -(v >> 1);
312 static int h264_parse_vui(mp_mpeg_header_t * picture, unsigned char * buf, unsigned int n)
314 unsigned int overscan, vsp_color, chroma, timing, fixed_fps;
316 if(getbits(buf, n++, 1))
318 picture->aspect_ratio_information = getbits(buf, n, 8);
319 n += 8;
320 if(picture->aspect_ratio_information == 255)
322 picture->display_picture_width = (getbits(buf, n, 8) << 8) | getbits(buf, n + 8, 8);
323 n += 16;
325 picture->display_picture_height = (getbits(buf, n, 8) << 8) | getbits(buf, n + 8, 8);
326 n += 16;
330 if((overscan=getbits(buf, n++, 1)))
331 n++;
332 if((vsp_color=getbits(buf, n++, 1)))
334 n += 4;
335 if(getbits(buf, n++, 1))
336 n += 24;
338 if((chroma=getbits(buf, n++, 1)))
340 read_golomb(buf, &n);
341 read_golomb(buf, &n);
343 if((timing=getbits(buf, n++, 1)))
345 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);
346 n += 32;
348 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);
349 n += 32;
351 fixed_fps = getbits(buf, n, 1);
353 if(picture->timeinc_unit > 0 && picture->timeinc_resolution > 0)
354 picture->fps = (float) picture->timeinc_resolution / (float) picture->timeinc_unit;
355 if(fixed_fps)
356 picture->fps /= 2;
359 //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,
360 // picture->timeinc_resolution, picture->timeinc_unit, picture->timeinc_unit, fixed_fps);
362 return n;
365 static int mp_unescape03(unsigned char *buf, int len);
367 int h264_parse_sps(mp_mpeg_header_t * picture, unsigned char * buf, int len)
369 unsigned int n = 0, v, i, k, mbh;
370 int frame_mbs_only;
372 len = mp_unescape03(buf, len);
374 picture->fps = picture->timeinc_unit = picture->timeinc_resolution = 0;
375 n = 24;
376 read_golomb(buf, &n);
377 if(buf[0] >= 100){
378 if(read_golomb(buf, &n) == 3)
379 n++;
380 read_golomb(buf, &n);
381 read_golomb(buf, &n);
382 n++;
383 if(getbits(buf, n++, 1)){
384 for(i = 0; i < 8; i++)
385 { // scaling list is skipped for now
386 if(getbits(buf, n++, 1))
388 v = 8;
389 for(k = (i < 6 ? 16 : 64); k && v; k--)
390 v = (v + read_golomb_s(buf, &n)) & 255;
395 read_golomb(buf, &n);
396 v = read_golomb(buf, &n);
397 if(v == 0)
398 read_golomb(buf, &n);
399 else if(v == 1)
401 getbits(buf, n++, 1);
402 read_golomb(buf, &n);
403 read_golomb(buf, &n);
404 v = read_golomb(buf, &n);
405 for(i = 0; i < v; i++)
406 read_golomb(buf, &n);
408 read_golomb(buf, &n);
409 getbits(buf, n++, 1);
410 picture->display_picture_width = 16 *(read_golomb(buf, &n)+1);
411 mbh = read_golomb(buf, &n)+1;
412 frame_mbs_only = getbits(buf, n++, 1);
413 picture->display_picture_height = 16 * (2 - frame_mbs_only) * mbh;
414 if(!frame_mbs_only)
415 getbits(buf, n++, 1);
416 getbits(buf, n++, 1);
417 if(getbits(buf, n++, 1))
419 read_golomb(buf, &n);
420 read_golomb(buf, &n);
421 read_golomb(buf, &n);
422 read_golomb(buf, &n);
424 if(getbits(buf, n++, 1))
425 n = h264_parse_vui(picture, buf, n);
427 return n;
430 static int mp_unescape03(unsigned char *buf, int len)
432 unsigned char *dest;
433 int i, j, skip;
435 dest = malloc(len);
436 if(! dest)
437 return 0;
439 j = i = skip = 0;
440 while(i <= len-3)
442 if(buf[i] == 0 && buf[i+1] == 0 && buf[i+2] == 3)
444 dest[j] = dest[j+1] = 0;
445 j += 2;
446 i += 3;
447 skip++;
449 else
451 dest[j] = buf[i];
452 j++;
453 i++;
456 dest[j] = buf[len-2];
457 dest[j+1] = buf[len-1];
458 len -= skip;
459 memcpy(buf, dest, len);
460 free(dest);
462 return len;
465 int mp_vc1_decode_sequence_header(mp_mpeg_header_t * picture, unsigned char * buf, int len)
467 int n, x;
469 len = mp_unescape03(buf, len);
471 picture->display_picture_width = picture->display_picture_height = 0;
472 picture->fps = 0;
473 n = 0;
474 x = getbits(buf, n, 2);
475 n += 2;
476 if(x != 3) //not advanced profile
477 return 0;
479 getbits16(buf, n, 14);
480 n += 14;
481 picture->display_picture_width = getbits16(buf, n, 12) * 2 + 2;
482 n += 12;
483 picture->display_picture_height = getbits16(buf, n, 12) * 2 + 2;
484 n += 12;
485 getbits(buf, n, 6);
486 n += 6;
487 x = getbits(buf, n, 1);
488 n += 1;
489 if(x) //display info
491 getbits16(buf, n, 14);
492 n += 14;
493 getbits16(buf, n, 14);
494 n += 14;
495 if(getbits(buf, n++, 1)) //aspect ratio
497 x = getbits(buf, n, 4);
498 n += 4;
499 if(x == 15)
501 getbits16(buf, n, 16);
502 n += 16;
506 if(getbits(buf, n++, 1)) //framerates
508 int frexp=0, frnum=0, frden=0;
510 if(getbits(buf, n++, 1))
512 frexp = getbits16(buf, n, 16);
513 n += 16;
514 picture->fps = (double) (frexp+1) / 32.0;
516 else
518 float frates[] = {0, 24000, 25000, 30000, 50000, 60000, 48000, 72000, 0};
519 float frdivs[] = {0, 1000, 1001, 0};
521 frnum = getbits(buf, n, 8);
522 n += 8;
523 frden = getbits(buf, n, 4);
524 n += 4;
525 if((frden == 1 || frden == 2) && (frnum < 8))
526 picture->fps = frates[frnum] / frdivs[frden];
531 //free(dest);
532 return 1;