Remove unused functions.
[mplayer/glamo.git] / libmpdemux / mpeg_hdr.c
blobd91019ad1342e19f5a6d9c2f0e0e05a263f3f68b
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 picture->frame_rate_extension_n = 1;
71 picture->frame_rate_extension_d = 1;
72 return 0;
75 static int header_process_sequence_extension (mp_mpeg_header_t * picture,
76 unsigned char * buffer)
78 /* check chroma format, size extensions, marker bit */
80 if ( ((buffer[1] & 0x06) == 0x00) ||
81 ((buffer[1] & 0x01) != 0x00) || (buffer[2] & 0xe0) ||
82 ((buffer[3] & 0x01) != 0x01) )
83 return 1;
85 picture->progressive_sequence = (buffer[1] >> 3) & 1;
86 picture->frame_rate_extension_n = ((buffer[5] >> 5) & 3) + 1;
87 picture->frame_rate_extension_d = (buffer[5] & 0x1f) + 1;
89 picture->mpeg1 = 0;
90 return 0;
93 static int header_process_picture_coding_extension (mp_mpeg_header_t * picture, unsigned char * buffer)
95 picture->picture_structure = buffer[2] & 3;
96 picture->top_field_first = buffer[3] >> 7;
97 picture->repeat_first_field = (buffer[3] >> 1) & 1;
98 picture->progressive_frame = buffer[4] >> 7;
100 // repeat_first implementation by A'rpi/ESP-team, based on libmpeg3:
101 picture->display_time=100;
102 if(picture->repeat_first_field){
103 if(picture->progressive_sequence){
104 if(picture->top_field_first)
105 picture->display_time+=200;
106 else
107 picture->display_time+=100;
108 } else
109 if(picture->progressive_frame){
110 picture->display_time+=50;
113 //temopral hack. We calc time on every field, so if we have 2 fields
114 // interlaced we'll end with double time for 1 frame
115 if( picture->picture_structure!=3 ) picture->display_time/=2;
116 return 0;
119 int mp_header_process_extension (mp_mpeg_header_t * picture, unsigned char * buffer)
121 switch (buffer[0] & 0xf0) {
122 case 0x10: /* sequence extension */
123 return header_process_sequence_extension (picture, buffer);
124 case 0x80: /* picture coding extension */
125 return header_process_picture_coding_extension (picture, buffer);
127 return 0;
130 float mpeg12_aspect_info(mp_mpeg_header_t *picture)
132 float aspect = 0.0;
134 switch(picture->aspect_ratio_information) {
135 case 2: // PAL/NTSC SVCD/DVD 4:3
136 case 8: // PAL VCD 4:3
137 case 12: // NTSC VCD 4:3
138 aspect=4.0/3.0;
139 break;
140 case 3: // PAL/NTSC Widescreen SVCD/DVD 16:9
141 case 6: // (PAL?)/NTSC Widescreen SVCD 16:9
142 aspect=16.0/9.0;
143 break;
144 case 4: // according to ISO-138182-2 Table 6.3
145 aspect=2.21;
146 break;
147 case 1: // VGA 1:1 - do not prescale
148 case 9: // Movie Type ??? / 640x480
149 aspect=0.0;
150 break;
151 default:
152 mp_msg(MSGT_DECVIDEO,MSGL_ERR,"Detected unknown aspect_ratio_information in mpeg sequence header.\n"
153 "Please report the aspect value (%i) along with the movie type (VGA,PAL,NTSC,"
154 "SECAM) and the movie resolution (720x576,352x240,480x480,...) to the MPlayer"
155 " developers, so that we can add support for it!\nAssuming 1:1 aspect for now.\n",
156 picture->aspect_ratio_information);
159 return aspect;
162 //MPEG4 HEADERS
163 unsigned char mp_getbits(unsigned char *buffer, unsigned int from, unsigned char len)
165 unsigned int n;
166 unsigned char m, u, l, y;
168 n = from / 8;
169 m = from % 8;
170 u = 8 - m;
171 l = (len > u ? len - u : 0);
173 y = (buffer[n] << m);
174 if(8 > len)
175 y >>= (8-len);
176 if(l)
177 y |= (buffer[n+1] >> (8-l));
179 //fprintf(stderr, "GETBITS(%d -> %d): bytes=0x%x 0x%x, n=%d, m=%d, l=%d, u=%d, Y=%d\n",
180 // from, (int) len, (int) buffer[n],(int) buffer[n+1], n, (int) m, (int) l, (int) u, (int) y);
181 return y;
184 static inline unsigned int mp_getbits16(unsigned char *buffer, unsigned int from, unsigned char len)
186 if(len > 8)
187 return (mp_getbits(buffer, from, len - 8) << 8) | mp_getbits(buffer, from + len - 8, 8);
188 else
189 return mp_getbits(buffer, from, len);
192 #define getbits mp_getbits
193 #define getbits16 mp_getbits16
195 static int read_timeinc(mp_mpeg_header_t * picture, unsigned char * buffer, int n)
197 if(picture->timeinc_bits > 8) {
198 picture->timeinc_unit = getbits(buffer, n, picture->timeinc_bits - 8) << 8;
199 n += picture->timeinc_bits - 8;
200 picture->timeinc_unit |= getbits(buffer, n, 8);
201 n += 8;
202 } else {
203 picture->timeinc_unit = getbits(buffer, n, picture->timeinc_bits);
204 n += picture->timeinc_bits;
206 //fprintf(stderr, "TIMEINC2: %d, bits: %d\n", picture->timeinc_unit, picture->timeinc_bits);
207 return n;
210 int mp4_header_process_vol(mp_mpeg_header_t * picture, unsigned char * buffer)
212 unsigned int n, aspect=0, aspectw=0, aspecth=0, x=1, v;
214 //begins with 0x0000012x
215 picture->fps = 0;
216 picture->timeinc_bits = picture->timeinc_resolution = picture->timeinc_unit = 0;
217 n = 9;
218 if(getbits(buffer, n, 1))
219 n += 7;
220 n++;
221 aspect=getbits(buffer, n, 4);
222 n += 4;
223 if(aspect == 0x0f) {
224 aspectw = getbits(buffer, n, 8);
225 n += 8;
226 aspecth = getbits(buffer, n, 8);
227 n += 8;
230 if(getbits(buffer, n, 1)) {
231 n += 4;
232 if(getbits(buffer, n, 1))
233 n += 79;
234 n++;
235 } else n++;
237 n+=3;
239 picture->timeinc_resolution = getbits(buffer, n, 8) << 8;
240 n += 8;
241 picture->timeinc_resolution |= getbits(buffer, n, 8);
242 n += 8;
244 picture->timeinc_bits = 0;
245 v = picture->timeinc_resolution - 1;
246 while(v && (x<16)) {
247 v>>=1;
248 picture->timeinc_bits++;
250 picture->timeinc_bits = (picture->timeinc_bits > 1 ? picture->timeinc_bits : 1);
252 n++; //marker bit
254 if(getbits(buffer, n, 1)) { //fixed_vop_timeinc
255 n++;
256 n = read_timeinc(picture, buffer, n);
258 if(picture->timeinc_unit)
259 picture->fps = (float) picture->timeinc_resolution / (float) picture->timeinc_unit;
262 //fprintf(stderr, "ASPECT: %d, PARW=%d, PARH=%d, TIMEINCRESOLUTION: %d, FIXED_TIMEINC: %d (number of bits: %d), FPS: %u\n",
263 // aspect, aspectw, aspecth, picture->timeinc_resolution, picture->timeinc_unit, picture->timeinc_bits, picture->fps);
265 return 0;
268 void mp4_header_process_vop(mp_mpeg_header_t * picture, unsigned char * buffer)
270 int n;
271 n = 0;
272 picture->picture_type = getbits(buffer, n, 2);
273 n += 2;
274 while(getbits(buffer, n, 1))
275 n++;
276 n++;
277 getbits(buffer, n, 1);
278 n++;
279 n = read_timeinc(picture, buffer, n);
282 #define min(a, b) ((a) <= (b) ? (a) : (b))
284 static unsigned int read_golomb(unsigned char *buffer, unsigned int *init)
286 unsigned int x, v = 0, v2 = 0, m, len = 0, n = *init;
288 while(getbits(buffer, n++, 1) == 0)
289 len++;
291 x = len + n;
292 while(n < x)
294 m = min(x - n, 8);
295 v |= getbits(buffer, n, m);
296 n += m;
297 if(x - n > 8)
298 v <<= 8;
301 v2 = 1;
302 for(n = 0; n < len; n++)
303 v2 <<= 1;
304 v2 = (v2 - 1) + v;
306 //fprintf(stderr, "READ_GOLOMB(%u), V=2^%u + %u-1 = %u\n", *init, len, v, v2);
307 *init = x;
308 return v2;
311 inline static int read_golomb_s(unsigned char *buffer, unsigned int *init)
313 unsigned int v = read_golomb(buffer, init);
314 return (v & 1) ? ((v + 1) >> 1) : -(v >> 1);
317 static int h264_parse_vui(mp_mpeg_header_t * picture, unsigned char * buf, unsigned int n)
319 unsigned int overscan, vsp_color, chroma, timing, fixed_fps;
321 if(getbits(buf, n++, 1))
323 picture->aspect_ratio_information = getbits(buf, n, 8);
324 n += 8;
325 if(picture->aspect_ratio_information == 255)
327 picture->display_picture_width = (getbits(buf, n, 8) << 8) | getbits(buf, n + 8, 8);
328 n += 16;
330 picture->display_picture_height = (getbits(buf, n, 8) << 8) | getbits(buf, n + 8, 8);
331 n += 16;
335 if((overscan=getbits(buf, n++, 1)))
336 n++;
337 if((vsp_color=getbits(buf, n++, 1)))
339 n += 4;
340 if(getbits(buf, n++, 1))
341 n += 24;
343 if((chroma=getbits(buf, n++, 1)))
345 read_golomb(buf, &n);
346 read_golomb(buf, &n);
348 if((timing=getbits(buf, n++, 1)))
350 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);
351 n += 32;
353 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);
354 n += 32;
356 fixed_fps = getbits(buf, n, 1);
358 if(picture->timeinc_unit > 0 && picture->timeinc_resolution > 0)
359 picture->fps = (float) picture->timeinc_resolution / (float) picture->timeinc_unit;
360 if(fixed_fps)
361 picture->fps /= 2;
364 //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,
365 // picture->timeinc_resolution, picture->timeinc_unit, picture->timeinc_unit, fixed_fps);
367 return n;
370 static int mp_unescape03(unsigned char *buf, int len);
372 int h264_parse_sps(mp_mpeg_header_t * picture, unsigned char * buf, int len)
374 unsigned int n = 0, v, i, k, mbh;
375 int frame_mbs_only;
377 len = mp_unescape03(buf, len);
379 picture->fps = picture->timeinc_unit = picture->timeinc_resolution = 0;
380 n = 24;
381 read_golomb(buf, &n);
382 if(buf[0] >= 100){
383 if(read_golomb(buf, &n) == 3)
384 n++;
385 read_golomb(buf, &n);
386 read_golomb(buf, &n);
387 n++;
388 if(getbits(buf, n++, 1)){
389 for(i = 0; i < 8; i++)
390 { // scaling list is skipped for now
391 if(getbits(buf, n++, 1))
393 v = 8;
394 for(k = (i < 6 ? 16 : 64); k && v; k--)
395 v = (v + read_golomb_s(buf, &n)) & 255;
400 read_golomb(buf, &n);
401 v = read_golomb(buf, &n);
402 if(v == 0)
403 read_golomb(buf, &n);
404 else if(v == 1)
406 getbits(buf, n++, 1);
407 read_golomb(buf, &n);
408 read_golomb(buf, &n);
409 v = read_golomb(buf, &n);
410 for(i = 0; i < v; i++)
411 read_golomb(buf, &n);
413 read_golomb(buf, &n);
414 getbits(buf, n++, 1);
415 picture->display_picture_width = 16 *(read_golomb(buf, &n)+1);
416 mbh = read_golomb(buf, &n)+1;
417 frame_mbs_only = getbits(buf, n++, 1);
418 picture->display_picture_height = 16 * (2 - frame_mbs_only) * mbh;
419 if(!frame_mbs_only)
420 getbits(buf, n++, 1);
421 getbits(buf, n++, 1);
422 if(getbits(buf, n++, 1))
424 read_golomb(buf, &n);
425 read_golomb(buf, &n);
426 read_golomb(buf, &n);
427 read_golomb(buf, &n);
429 if(getbits(buf, n++, 1))
430 n = h264_parse_vui(picture, buf, n);
432 return n;
435 static int mp_unescape03(unsigned char *buf, int len)
437 unsigned char *dest;
438 int i, j, skip;
440 dest = malloc(len);
441 if(! dest)
442 return 0;
444 j = i = skip = 0;
445 while(i <= len-3)
447 if(buf[i] == 0 && buf[i+1] == 0 && buf[i+2] == 3)
449 dest[j] = dest[j+1] = 0;
450 j += 2;
451 i += 3;
452 skip++;
454 else
456 dest[j] = buf[i];
457 j++;
458 i++;
461 dest[j] = buf[len-2];
462 dest[j+1] = buf[len-1];
463 len -= skip;
464 memcpy(buf, dest, len);
465 free(dest);
467 return len;
470 int mp_vc1_decode_sequence_header(mp_mpeg_header_t * picture, unsigned char * buf, int len)
472 int n, x;
474 len = mp_unescape03(buf, len);
476 picture->display_picture_width = picture->display_picture_height = 0;
477 picture->fps = 0;
478 n = 0;
479 x = getbits(buf, n, 2);
480 n += 2;
481 if(x != 3) //not advanced profile
482 return 0;
484 getbits16(buf, n, 14);
485 n += 14;
486 picture->display_picture_width = getbits16(buf, n, 12) * 2 + 2;
487 n += 12;
488 picture->display_picture_height = getbits16(buf, n, 12) * 2 + 2;
489 n += 12;
490 getbits(buf, n, 6);
491 n += 6;
492 x = getbits(buf, n, 1);
493 n += 1;
494 if(x) //display info
496 getbits16(buf, n, 14);
497 n += 14;
498 getbits16(buf, n, 14);
499 n += 14;
500 if(getbits(buf, n++, 1)) //aspect ratio
502 x = getbits(buf, n, 4);
503 n += 4;
504 if(x == 15)
506 getbits16(buf, n, 16);
507 n += 16;
511 if(getbits(buf, n++, 1)) //framerates
513 int frexp=0, frnum=0, frden=0;
515 if(getbits(buf, n++, 1))
517 frexp = getbits16(buf, n, 16);
518 n += 16;
519 picture->fps = (double) (frexp+1) / 32.0;
521 else
523 float frates[] = {0, 24000, 25000, 30000, 50000, 60000, 48000, 72000, 0};
524 float frdivs[] = {0, 1000, 1001, 0};
526 frnum = getbits(buf, n, 8);
527 n += 8;
528 frden = getbits(buf, n, 4);
529 n += 4;
530 if((frden == 1 || frden == 2) && (frnum < 8))
531 picture->fps = frates[frnum] / frdivs[frden];
536 //free(dest);
537 return 1;