Mercurial > mplayer.hg
annotate libmpdemux/mpeg_hdr.c @ 36965:5fb43d5efdca
configure: add CONFIG_CABAC
fix ffmpeg linking
author | michael |
---|---|
date | Tue, 25 Mar 2014 00:15:02 +0000 |
parents | e0c6b271d55b |
children |
rev | line source |
---|---|
29238
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
24784
diff
changeset
|
1 /* |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
24784
diff
changeset
|
2 * based on libmpeg2/header.c by Aaron Holtzman <aholtzma@ess.engr.uvic.ca> |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
24784
diff
changeset
|
3 * |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
24784
diff
changeset
|
4 * This file is part of MPlayer. |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
24784
diff
changeset
|
5 * |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
24784
diff
changeset
|
6 * MPlayer is free software; you can redistribute it and/or modify |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
24784
diff
changeset
|
7 * it under the terms of the GNU General Public License as published by |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
24784
diff
changeset
|
8 * the Free Software Foundation; either version 2 of the License, or |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
24784
diff
changeset
|
9 * (at your option) any later version. |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
24784
diff
changeset
|
10 * |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
24784
diff
changeset
|
11 * MPlayer is distributed in the hope that it will be useful, |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
24784
diff
changeset
|
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
24784
diff
changeset
|
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
24784
diff
changeset
|
14 * GNU General Public License for more details. |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
24784
diff
changeset
|
15 * |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
24784
diff
changeset
|
16 * You should have received a copy of the GNU General Public License along |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
24784
diff
changeset
|
17 * with MPlayer; if not, write to the Free Software Foundation, Inc., |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
24784
diff
changeset
|
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
24784
diff
changeset
|
19 */ |
2565 | 20 |
15225 | 21 #include <inttypes.h> |
2565 | 22 #include <stdio.h> |
14886 | 23 #include <stdlib.h> |
21947 | 24 #include <string.h> |
2565 | 25 |
26 #include "config.h" | |
27 #include "mpeg_hdr.h" | |
28 | |
18398
a1375e440e92
COSMETICS: moved code to parse mpeg1/2 A/R to mpeg_hdr.c
nicodvb
parents:
17952
diff
changeset
|
29 #include "mp_msg.h" |
a1375e440e92
COSMETICS: moved code to parse mpeg1/2 A/R to mpeg_hdr.c
nicodvb
parents:
17952
diff
changeset
|
30 |
16184
04dd5945fab8
100l to whoever wrote this crap using 1/10000 units. it caused framerates to get trashed from 30000/1001 to 2997/100, etc.!
rfelker
parents:
15618
diff
changeset
|
31 static float frameratecode2framerate[16] = { |
2565 | 32 0, |
5441 | 33 // Official mpeg1/2 framerates: (1-8) |
16184
04dd5945fab8
100l to whoever wrote this crap using 1/10000 units. it caused framerates to get trashed from 30000/1001 to 2997/100, etc.!
rfelker
parents:
15618
diff
changeset
|
34 24000.0/1001, 24,25, |
04dd5945fab8
100l to whoever wrote this crap using 1/10000 units. it caused framerates to get trashed from 30000/1001 to 2997/100, etc.!
rfelker
parents:
15618
diff
changeset
|
35 30000.0/1001, 30,50, |
04dd5945fab8
100l to whoever wrote this crap using 1/10000 units. it caused framerates to get trashed from 30000/1001 to 2997/100, etc.!
rfelker
parents:
15618
diff
changeset
|
36 60000.0/1001, 60, |
5441 | 37 // Xing's 15fps: (9) |
16184
04dd5945fab8
100l to whoever wrote this crap using 1/10000 units. it caused framerates to get trashed from 30000/1001 to 2997/100, etc.!
rfelker
parents:
15618
diff
changeset
|
38 15, |
5441 | 39 // libmpeg3's "Unofficial economy rates": (10-13) |
16184
04dd5945fab8
100l to whoever wrote this crap using 1/10000 units. it caused framerates to get trashed from 30000/1001 to 2997/100, etc.!
rfelker
parents:
15618
diff
changeset
|
40 5,10,12,15, |
5441 | 41 // some invalid ones: (14-15) |
42 0,0 | |
2565 | 43 }; |
44 | |
45 | |
24784
328d1b36952a
Mark constant argument of mp_header_process_sequence_header as such.
diego
parents:
21953
diff
changeset
|
46 int mp_header_process_sequence_header (mp_mpeg_header_t * picture, const unsigned char * buffer) |
2565 | 47 { |
33339
f3c1141dfea7
Remove some useless code that calculates something
reimar
parents:
32347
diff
changeset
|
48 int height; |
2565 | 49 |
50 if ((buffer[6] & 0x20) != 0x20){ | |
14477
92553e3c8f01
automatic fps calculation for mpeg4 in raw stream/mpeg-ts
nicodvb
parents:
12755
diff
changeset
|
51 fprintf(stderr, "missing marker bit!\n"); |
2565 | 52 return 1; /* missing marker_bit */ |
53 } | |
54 | |
55 height = (buffer[0] << 16) | (buffer[1] << 8) | buffer[2]; | |
56 | |
33339
f3c1141dfea7
Remove some useless code that calculates something
reimar
parents:
32347
diff
changeset
|
57 picture->display_picture_width = height >> 12; |
f3c1141dfea7
Remove some useless code that calculates something
reimar
parents:
32347
diff
changeset
|
58 picture->display_picture_height = height & 0xfff; |
2565 | 59 |
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); | |
64 picture->mpeg1 = 1; | |
65 picture->picture_structure = 3; //FRAME_PICTURE; | |
66 picture->display_time=100; | |
30640
34e3d7d697d9
Initialize frame rate extension fields in mp_header_process_sequence_header
reimar
parents:
30639
diff
changeset
|
67 picture->frame_rate_extension_n = 1; |
34e3d7d697d9
Initialize frame rate extension fields in mp_header_process_sequence_header
reimar
parents:
30639
diff
changeset
|
68 picture->frame_rate_extension_d = 1; |
2565 | 69 return 0; |
70 } | |
71 | |
72 static int header_process_sequence_extension (mp_mpeg_header_t * picture, | |
34159 | 73 const unsigned char * buffer) |
2565 | 74 { |
75 /* check chroma format, size extensions, marker bit */ | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29238
diff
changeset
|
76 |
12562 | 77 if ( ((buffer[1] & 0x06) == 0x00) || |
78 ((buffer[1] & 0x01) != 0x00) || (buffer[2] & 0xe0) || | |
79 ((buffer[3] & 0x01) != 0x01) ) | |
2565 | 80 return 1; |
81 | |
82 picture->progressive_sequence = (buffer[1] >> 3) & 1; | |
30639
102ae81b5dc8
Parse and use the information from the frame rate extension header for MPEG-2.
reimar
parents:
29452
diff
changeset
|
83 picture->frame_rate_extension_n = ((buffer[5] >> 5) & 3) + 1; |
102ae81b5dc8
Parse and use the information from the frame rate extension header for MPEG-2.
reimar
parents:
29452
diff
changeset
|
84 picture->frame_rate_extension_d = (buffer[5] & 0x1f) + 1; |
102ae81b5dc8
Parse and use the information from the frame rate extension header for MPEG-2.
reimar
parents:
29452
diff
changeset
|
85 |
2565 | 86 picture->mpeg1 = 0; |
87 return 0; | |
88 } | |
89 | |
34159 | 90 static int header_process_picture_coding_extension (mp_mpeg_header_t * picture, const unsigned char * buffer) |
2565 | 91 { |
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; | |
96 | |
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; | |
103 else | |
104 picture->display_time+=100; | |
105 } else | |
106 if(picture->progressive_frame){ | |
107 picture->display_time+=50; | |
108 } | |
109 } | |
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; | |
113 return 0; | |
114 } | |
115 | |
34159 | 116 int mp_header_process_extension (mp_mpeg_header_t * picture, const unsigned char * buffer) |
2565 | 117 { |
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); | |
123 } | |
124 return 0; | |
125 } | |
126 | |
18398
a1375e440e92
COSMETICS: moved code to parse mpeg1/2 A/R to mpeg_hdr.c
nicodvb
parents:
17952
diff
changeset
|
127 float mpeg12_aspect_info(mp_mpeg_header_t *picture) |
a1375e440e92
COSMETICS: moved code to parse mpeg1/2 A/R to mpeg_hdr.c
nicodvb
parents:
17952
diff
changeset
|
128 { |
a1375e440e92
COSMETICS: moved code to parse mpeg1/2 A/R to mpeg_hdr.c
nicodvb
parents:
17952
diff
changeset
|
129 float aspect = 0.0; |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29238
diff
changeset
|
130 |
18398
a1375e440e92
COSMETICS: moved code to parse mpeg1/2 A/R to mpeg_hdr.c
nicodvb
parents:
17952
diff
changeset
|
131 switch(picture->aspect_ratio_information) { |
a1375e440e92
COSMETICS: moved code to parse mpeg1/2 A/R to mpeg_hdr.c
nicodvb
parents:
17952
diff
changeset
|
132 case 2: // PAL/NTSC SVCD/DVD 4:3 |
a1375e440e92
COSMETICS: moved code to parse mpeg1/2 A/R to mpeg_hdr.c
nicodvb
parents:
17952
diff
changeset
|
133 case 8: // PAL VCD 4:3 |
a1375e440e92
COSMETICS: moved code to parse mpeg1/2 A/R to mpeg_hdr.c
nicodvb
parents:
17952
diff
changeset
|
134 case 12: // NTSC VCD 4:3 |
a1375e440e92
COSMETICS: moved code to parse mpeg1/2 A/R to mpeg_hdr.c
nicodvb
parents:
17952
diff
changeset
|
135 aspect=4.0/3.0; |
a1375e440e92
COSMETICS: moved code to parse mpeg1/2 A/R to mpeg_hdr.c
nicodvb
parents:
17952
diff
changeset
|
136 break; |
a1375e440e92
COSMETICS: moved code to parse mpeg1/2 A/R to mpeg_hdr.c
nicodvb
parents:
17952
diff
changeset
|
137 case 3: // PAL/NTSC Widescreen SVCD/DVD 16:9 |
a1375e440e92
COSMETICS: moved code to parse mpeg1/2 A/R to mpeg_hdr.c
nicodvb
parents:
17952
diff
changeset
|
138 case 6: // (PAL?)/NTSC Widescreen SVCD 16:9 |
a1375e440e92
COSMETICS: moved code to parse mpeg1/2 A/R to mpeg_hdr.c
nicodvb
parents:
17952
diff
changeset
|
139 aspect=16.0/9.0; |
a1375e440e92
COSMETICS: moved code to parse mpeg1/2 A/R to mpeg_hdr.c
nicodvb
parents:
17952
diff
changeset
|
140 break; |
a1375e440e92
COSMETICS: moved code to parse mpeg1/2 A/R to mpeg_hdr.c
nicodvb
parents:
17952
diff
changeset
|
141 case 4: // according to ISO-138182-2 Table 6.3 |
a1375e440e92
COSMETICS: moved code to parse mpeg1/2 A/R to mpeg_hdr.c
nicodvb
parents:
17952
diff
changeset
|
142 aspect=2.21; |
a1375e440e92
COSMETICS: moved code to parse mpeg1/2 A/R to mpeg_hdr.c
nicodvb
parents:
17952
diff
changeset
|
143 break; |
a1375e440e92
COSMETICS: moved code to parse mpeg1/2 A/R to mpeg_hdr.c
nicodvb
parents:
17952
diff
changeset
|
144 case 1: // VGA 1:1 - do not prescale |
a1375e440e92
COSMETICS: moved code to parse mpeg1/2 A/R to mpeg_hdr.c
nicodvb
parents:
17952
diff
changeset
|
145 case 9: // Movie Type ??? / 640x480 |
a1375e440e92
COSMETICS: moved code to parse mpeg1/2 A/R to mpeg_hdr.c
nicodvb
parents:
17952
diff
changeset
|
146 aspect=0.0; |
a1375e440e92
COSMETICS: moved code to parse mpeg1/2 A/R to mpeg_hdr.c
nicodvb
parents:
17952
diff
changeset
|
147 break; |
a1375e440e92
COSMETICS: moved code to parse mpeg1/2 A/R to mpeg_hdr.c
nicodvb
parents:
17952
diff
changeset
|
148 default: |
a1375e440e92
COSMETICS: moved code to parse mpeg1/2 A/R to mpeg_hdr.c
nicodvb
parents:
17952
diff
changeset
|
149 mp_msg(MSGT_DECVIDEO,MSGL_ERR,"Detected unknown aspect_ratio_information in mpeg sequence header.\n" |
a1375e440e92
COSMETICS: moved code to parse mpeg1/2 A/R to mpeg_hdr.c
nicodvb
parents:
17952
diff
changeset
|
150 "Please report the aspect value (%i) along with the movie type (VGA,PAL,NTSC," |
a1375e440e92
COSMETICS: moved code to parse mpeg1/2 A/R to mpeg_hdr.c
nicodvb
parents:
17952
diff
changeset
|
151 "SECAM) and the movie resolution (720x576,352x240,480x480,...) to the MPlayer" |
a1375e440e92
COSMETICS: moved code to parse mpeg1/2 A/R to mpeg_hdr.c
nicodvb
parents:
17952
diff
changeset
|
152 " developers, so that we can add support for it!\nAssuming 1:1 aspect for now.\n", |
a1375e440e92
COSMETICS: moved code to parse mpeg1/2 A/R to mpeg_hdr.c
nicodvb
parents:
17952
diff
changeset
|
153 picture->aspect_ratio_information); |
a1375e440e92
COSMETICS: moved code to parse mpeg1/2 A/R to mpeg_hdr.c
nicodvb
parents:
17952
diff
changeset
|
154 } |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29238
diff
changeset
|
155 |
18398
a1375e440e92
COSMETICS: moved code to parse mpeg1/2 A/R to mpeg_hdr.c
nicodvb
parents:
17952
diff
changeset
|
156 return aspect; |
a1375e440e92
COSMETICS: moved code to parse mpeg1/2 A/R to mpeg_hdr.c
nicodvb
parents:
17952
diff
changeset
|
157 } |
14477
92553e3c8f01
automatic fps calculation for mpeg4 in raw stream/mpeg-ts
nicodvb
parents:
12755
diff
changeset
|
158 |
92553e3c8f01
automatic fps calculation for mpeg4 in raw stream/mpeg-ts
nicodvb
parents:
12755
diff
changeset
|
159 //MPEG4 HEADERS |
34159 | 160 unsigned char mp_getbits(const unsigned char *buffer, unsigned int from, unsigned char len) |
14477
92553e3c8f01
automatic fps calculation for mpeg4 in raw stream/mpeg-ts
nicodvb
parents:
12755
diff
changeset
|
161 { |
92553e3c8f01
automatic fps calculation for mpeg4 in raw stream/mpeg-ts
nicodvb
parents:
12755
diff
changeset
|
162 unsigned int n; |
92553e3c8f01
automatic fps calculation for mpeg4 in raw stream/mpeg-ts
nicodvb
parents:
12755
diff
changeset
|
163 unsigned char m, u, l, y; |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29238
diff
changeset
|
164 |
14477
92553e3c8f01
automatic fps calculation for mpeg4 in raw stream/mpeg-ts
nicodvb
parents:
12755
diff
changeset
|
165 n = from / 8; |
92553e3c8f01
automatic fps calculation for mpeg4 in raw stream/mpeg-ts
nicodvb
parents:
12755
diff
changeset
|
166 m = from % 8; |
92553e3c8f01
automatic fps calculation for mpeg4 in raw stream/mpeg-ts
nicodvb
parents:
12755
diff
changeset
|
167 u = 8 - m; |
92553e3c8f01
automatic fps calculation for mpeg4 in raw stream/mpeg-ts
nicodvb
parents:
12755
diff
changeset
|
168 l = (len > u ? len - u : 0); |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29238
diff
changeset
|
169 |
14477
92553e3c8f01
automatic fps calculation for mpeg4 in raw stream/mpeg-ts
nicodvb
parents:
12755
diff
changeset
|
170 y = (buffer[n] << m); |
92553e3c8f01
automatic fps calculation for mpeg4 in raw stream/mpeg-ts
nicodvb
parents:
12755
diff
changeset
|
171 if(8 > len) |
92553e3c8f01
automatic fps calculation for mpeg4 in raw stream/mpeg-ts
nicodvb
parents:
12755
diff
changeset
|
172 y >>= (8-len); |
92553e3c8f01
automatic fps calculation for mpeg4 in raw stream/mpeg-ts
nicodvb
parents:
12755
diff
changeset
|
173 if(l) |
92553e3c8f01
automatic fps calculation for mpeg4 in raw stream/mpeg-ts
nicodvb
parents:
12755
diff
changeset
|
174 y |= (buffer[n+1] >> (8-l)); |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29238
diff
changeset
|
175 |
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29238
diff
changeset
|
176 //fprintf(stderr, "GETBITS(%d -> %d): bytes=0x%x 0x%x, n=%d, m=%d, l=%d, u=%d, Y=%d\n", |
14477
92553e3c8f01
automatic fps calculation for mpeg4 in raw stream/mpeg-ts
nicodvb
parents:
12755
diff
changeset
|
177 // from, (int) len, (int) buffer[n],(int) buffer[n+1], n, (int) m, (int) l, (int) u, (int) y); |
92553e3c8f01
automatic fps calculation for mpeg4 in raw stream/mpeg-ts
nicodvb
parents:
12755
diff
changeset
|
178 return y; |
92553e3c8f01
automatic fps calculation for mpeg4 in raw stream/mpeg-ts
nicodvb
parents:
12755
diff
changeset
|
179 } |
92553e3c8f01
automatic fps calculation for mpeg4 in raw stream/mpeg-ts
nicodvb
parents:
12755
diff
changeset
|
180 |
34159 | 181 static inline unsigned int mp_getbits16(const unsigned char *buffer, unsigned int from, unsigned char len) |
21947 | 182 { |
183 if(len > 8) | |
184 return (mp_getbits(buffer, from, len - 8) << 8) | mp_getbits(buffer, from + len - 8, 8); | |
185 else | |
186 return mp_getbits(buffer, from, len); | |
187 } | |
188 | |
14967 | 189 #define getbits mp_getbits |
21947 | 190 #define getbits16 mp_getbits16 |
14967 | 191 |
34159 | 192 static int read_timeinc(mp_mpeg_header_t * picture, const unsigned char * buffer, int n) |
14477
92553e3c8f01
automatic fps calculation for mpeg4 in raw stream/mpeg-ts
nicodvb
parents:
12755
diff
changeset
|
193 { |
92553e3c8f01
automatic fps calculation for mpeg4 in raw stream/mpeg-ts
nicodvb
parents:
12755
diff
changeset
|
194 if(picture->timeinc_bits > 8) { |
92553e3c8f01
automatic fps calculation for mpeg4 in raw stream/mpeg-ts
nicodvb
parents:
12755
diff
changeset
|
195 picture->timeinc_unit = getbits(buffer, n, picture->timeinc_bits - 8) << 8; |
92553e3c8f01
automatic fps calculation for mpeg4 in raw stream/mpeg-ts
nicodvb
parents:
12755
diff
changeset
|
196 n += picture->timeinc_bits - 8; |
92553e3c8f01
automatic fps calculation for mpeg4 in raw stream/mpeg-ts
nicodvb
parents:
12755
diff
changeset
|
197 picture->timeinc_unit |= getbits(buffer, n, 8); |
92553e3c8f01
automatic fps calculation for mpeg4 in raw stream/mpeg-ts
nicodvb
parents:
12755
diff
changeset
|
198 n += 8; |
92553e3c8f01
automatic fps calculation for mpeg4 in raw stream/mpeg-ts
nicodvb
parents:
12755
diff
changeset
|
199 } else { |
92553e3c8f01
automatic fps calculation for mpeg4 in raw stream/mpeg-ts
nicodvb
parents:
12755
diff
changeset
|
200 picture->timeinc_unit = getbits(buffer, n, picture->timeinc_bits); |
92553e3c8f01
automatic fps calculation for mpeg4 in raw stream/mpeg-ts
nicodvb
parents:
12755
diff
changeset
|
201 n += picture->timeinc_bits; |
92553e3c8f01
automatic fps calculation for mpeg4 in raw stream/mpeg-ts
nicodvb
parents:
12755
diff
changeset
|
202 } |
92553e3c8f01
automatic fps calculation for mpeg4 in raw stream/mpeg-ts
nicodvb
parents:
12755
diff
changeset
|
203 //fprintf(stderr, "TIMEINC2: %d, bits: %d\n", picture->timeinc_unit, picture->timeinc_bits); |
92553e3c8f01
automatic fps calculation for mpeg4 in raw stream/mpeg-ts
nicodvb
parents:
12755
diff
changeset
|
204 return n; |
92553e3c8f01
automatic fps calculation for mpeg4 in raw stream/mpeg-ts
nicodvb
parents:
12755
diff
changeset
|
205 } |
92553e3c8f01
automatic fps calculation for mpeg4 in raw stream/mpeg-ts
nicodvb
parents:
12755
diff
changeset
|
206 |
34159 | 207 int mp4_header_process_vol(mp_mpeg_header_t * picture, const unsigned char * buffer) |
14477
92553e3c8f01
automatic fps calculation for mpeg4 in raw stream/mpeg-ts
nicodvb
parents:
12755
diff
changeset
|
208 { |
34160 | 209 unsigned int n, aspect, x=1, v; |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29238
diff
changeset
|
210 |
14477
92553e3c8f01
automatic fps calculation for mpeg4 in raw stream/mpeg-ts
nicodvb
parents:
12755
diff
changeset
|
211 //begins with 0x0000012x |
92553e3c8f01
automatic fps calculation for mpeg4 in raw stream/mpeg-ts
nicodvb
parents:
12755
diff
changeset
|
212 picture->fps = 0; |
92553e3c8f01
automatic fps calculation for mpeg4 in raw stream/mpeg-ts
nicodvb
parents:
12755
diff
changeset
|
213 picture->timeinc_bits = picture->timeinc_resolution = picture->timeinc_unit = 0; |
92553e3c8f01
automatic fps calculation for mpeg4 in raw stream/mpeg-ts
nicodvb
parents:
12755
diff
changeset
|
214 n = 9; |
92553e3c8f01
automatic fps calculation for mpeg4 in raw stream/mpeg-ts
nicodvb
parents:
12755
diff
changeset
|
215 if(getbits(buffer, n, 1)) |
92553e3c8f01
automatic fps calculation for mpeg4 in raw stream/mpeg-ts
nicodvb
parents:
12755
diff
changeset
|
216 n += 7; |
92553e3c8f01
automatic fps calculation for mpeg4 in raw stream/mpeg-ts
nicodvb
parents:
12755
diff
changeset
|
217 n++; |
92553e3c8f01
automatic fps calculation for mpeg4 in raw stream/mpeg-ts
nicodvb
parents:
12755
diff
changeset
|
218 aspect=getbits(buffer, n, 4); |
92553e3c8f01
automatic fps calculation for mpeg4 in raw stream/mpeg-ts
nicodvb
parents:
12755
diff
changeset
|
219 n += 4; |
92553e3c8f01
automatic fps calculation for mpeg4 in raw stream/mpeg-ts
nicodvb
parents:
12755
diff
changeset
|
220 if(aspect == 0x0f) { |
34160 | 221 // custom aspect w and h, 8 bit each |
222 n += 16; | |
14477
92553e3c8f01
automatic fps calculation for mpeg4 in raw stream/mpeg-ts
nicodvb
parents:
12755
diff
changeset
|
223 } |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29238
diff
changeset
|
224 |
14477
92553e3c8f01
automatic fps calculation for mpeg4 in raw stream/mpeg-ts
nicodvb
parents:
12755
diff
changeset
|
225 if(getbits(buffer, n, 1)) { |
92553e3c8f01
automatic fps calculation for mpeg4 in raw stream/mpeg-ts
nicodvb
parents:
12755
diff
changeset
|
226 n += 4; |
92553e3c8f01
automatic fps calculation for mpeg4 in raw stream/mpeg-ts
nicodvb
parents:
12755
diff
changeset
|
227 if(getbits(buffer, n, 1)) |
92553e3c8f01
automatic fps calculation for mpeg4 in raw stream/mpeg-ts
nicodvb
parents:
12755
diff
changeset
|
228 n += 79; |
92553e3c8f01
automatic fps calculation for mpeg4 in raw stream/mpeg-ts
nicodvb
parents:
12755
diff
changeset
|
229 n++; |
92553e3c8f01
automatic fps calculation for mpeg4 in raw stream/mpeg-ts
nicodvb
parents:
12755
diff
changeset
|
230 } else n++; |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29238
diff
changeset
|
231 |
14477
92553e3c8f01
automatic fps calculation for mpeg4 in raw stream/mpeg-ts
nicodvb
parents:
12755
diff
changeset
|
232 n+=3; |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29238
diff
changeset
|
233 |
14477
92553e3c8f01
automatic fps calculation for mpeg4 in raw stream/mpeg-ts
nicodvb
parents:
12755
diff
changeset
|
234 picture->timeinc_resolution = getbits(buffer, n, 8) << 8; |
92553e3c8f01
automatic fps calculation for mpeg4 in raw stream/mpeg-ts
nicodvb
parents:
12755
diff
changeset
|
235 n += 8; |
92553e3c8f01
automatic fps calculation for mpeg4 in raw stream/mpeg-ts
nicodvb
parents:
12755
diff
changeset
|
236 picture->timeinc_resolution |= getbits(buffer, n, 8); |
92553e3c8f01
automatic fps calculation for mpeg4 in raw stream/mpeg-ts
nicodvb
parents:
12755
diff
changeset
|
237 n += 8; |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29238
diff
changeset
|
238 |
14477
92553e3c8f01
automatic fps calculation for mpeg4 in raw stream/mpeg-ts
nicodvb
parents:
12755
diff
changeset
|
239 picture->timeinc_bits = 0; |
92553e3c8f01
automatic fps calculation for mpeg4 in raw stream/mpeg-ts
nicodvb
parents:
12755
diff
changeset
|
240 v = picture->timeinc_resolution - 1; |
92553e3c8f01
automatic fps calculation for mpeg4 in raw stream/mpeg-ts
nicodvb
parents:
12755
diff
changeset
|
241 while(v && (x<16)) { |
92553e3c8f01
automatic fps calculation for mpeg4 in raw stream/mpeg-ts
nicodvb
parents:
12755
diff
changeset
|
242 v>>=1; |
92553e3c8f01
automatic fps calculation for mpeg4 in raw stream/mpeg-ts
nicodvb
parents:
12755
diff
changeset
|
243 picture->timeinc_bits++; |
92553e3c8f01
automatic fps calculation for mpeg4 in raw stream/mpeg-ts
nicodvb
parents:
12755
diff
changeset
|
244 } |
92553e3c8f01
automatic fps calculation for mpeg4 in raw stream/mpeg-ts
nicodvb
parents:
12755
diff
changeset
|
245 picture->timeinc_bits = (picture->timeinc_bits > 1 ? picture->timeinc_bits : 1); |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29238
diff
changeset
|
246 |
14477
92553e3c8f01
automatic fps calculation for mpeg4 in raw stream/mpeg-ts
nicodvb
parents:
12755
diff
changeset
|
247 n++; //marker bit |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29238
diff
changeset
|
248 |
30861
1f3b39209b2d
Fix value of bit counter at end of functions to prepare for future patches.
cehoyos
parents:
30640
diff
changeset
|
249 if(getbits(buffer, n++, 1)) { //fixed_vop_timeinc |
1f3b39209b2d
Fix value of bit counter at end of functions to prepare for future patches.
cehoyos
parents:
30640
diff
changeset
|
250 n += read_timeinc(picture, buffer, n); |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29238
diff
changeset
|
251 |
14477
92553e3c8f01
automatic fps calculation for mpeg4 in raw stream/mpeg-ts
nicodvb
parents:
12755
diff
changeset
|
252 if(picture->timeinc_unit) |
16319
798d9be2337f
multiplying fps by 10000 is no more necessary (when determining mp4v and h264 framerate)
nicodvb
parents:
16184
diff
changeset
|
253 picture->fps = (float) picture->timeinc_resolution / (float) picture->timeinc_unit; |
14477
92553e3c8f01
automatic fps calculation for mpeg4 in raw stream/mpeg-ts
nicodvb
parents:
12755
diff
changeset
|
254 } |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29238
diff
changeset
|
255 |
30862
2a8e5cea0c8c
Calculate width and height in mp4_header_process_vop().
cehoyos
parents:
30861
diff
changeset
|
256 n++; //marker bit |
2a8e5cea0c8c
Calculate width and height in mp4_header_process_vop().
cehoyos
parents:
30861
diff
changeset
|
257 picture->display_picture_width = getbits16(buffer, n, 13); |
2a8e5cea0c8c
Calculate width and height in mp4_header_process_vop().
cehoyos
parents:
30861
diff
changeset
|
258 n += 13; |
2a8e5cea0c8c
Calculate width and height in mp4_header_process_vop().
cehoyos
parents:
30861
diff
changeset
|
259 n++; //marker bit |
2a8e5cea0c8c
Calculate width and height in mp4_header_process_vop().
cehoyos
parents:
30861
diff
changeset
|
260 picture->display_picture_height = getbits16(buffer, n, 13); |
2a8e5cea0c8c
Calculate width and height in mp4_header_process_vop().
cehoyos
parents:
30861
diff
changeset
|
261 n += 13; |
2a8e5cea0c8c
Calculate width and height in mp4_header_process_vop().
cehoyos
parents:
30861
diff
changeset
|
262 |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29238
diff
changeset
|
263 //fprintf(stderr, "ASPECT: %d, PARW=%d, PARH=%d, TIMEINCRESOLUTION: %d, FIXED_TIMEINC: %d (number of bits: %d), FPS: %u\n", |
14477
92553e3c8f01
automatic fps calculation for mpeg4 in raw stream/mpeg-ts
nicodvb
parents:
12755
diff
changeset
|
264 // aspect, aspectw, aspecth, picture->timeinc_resolution, picture->timeinc_unit, picture->timeinc_bits, picture->fps); |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29238
diff
changeset
|
265 |
14477
92553e3c8f01
automatic fps calculation for mpeg4 in raw stream/mpeg-ts
nicodvb
parents:
12755
diff
changeset
|
266 return 0; |
92553e3c8f01
automatic fps calculation for mpeg4 in raw stream/mpeg-ts
nicodvb
parents:
12755
diff
changeset
|
267 } |
92553e3c8f01
automatic fps calculation for mpeg4 in raw stream/mpeg-ts
nicodvb
parents:
12755
diff
changeset
|
268 |
34159 | 269 void mp4_header_process_vop(mp_mpeg_header_t * picture, const unsigned char * buffer) |
14477
92553e3c8f01
automatic fps calculation for mpeg4 in raw stream/mpeg-ts
nicodvb
parents:
12755
diff
changeset
|
270 { |
92553e3c8f01
automatic fps calculation for mpeg4 in raw stream/mpeg-ts
nicodvb
parents:
12755
diff
changeset
|
271 int n; |
92553e3c8f01
automatic fps calculation for mpeg4 in raw stream/mpeg-ts
nicodvb
parents:
12755
diff
changeset
|
272 n = 0; |
92553e3c8f01
automatic fps calculation for mpeg4 in raw stream/mpeg-ts
nicodvb
parents:
12755
diff
changeset
|
273 picture->picture_type = getbits(buffer, n, 2); |
92553e3c8f01
automatic fps calculation for mpeg4 in raw stream/mpeg-ts
nicodvb
parents:
12755
diff
changeset
|
274 n += 2; |
92553e3c8f01
automatic fps calculation for mpeg4 in raw stream/mpeg-ts
nicodvb
parents:
12755
diff
changeset
|
275 while(getbits(buffer, n, 1)) |
92553e3c8f01
automatic fps calculation for mpeg4 in raw stream/mpeg-ts
nicodvb
parents:
12755
diff
changeset
|
276 n++; |
92553e3c8f01
automatic fps calculation for mpeg4 in raw stream/mpeg-ts
nicodvb
parents:
12755
diff
changeset
|
277 n++; |
92553e3c8f01
automatic fps calculation for mpeg4 in raw stream/mpeg-ts
nicodvb
parents:
12755
diff
changeset
|
278 getbits(buffer, n, 1); |
92553e3c8f01
automatic fps calculation for mpeg4 in raw stream/mpeg-ts
nicodvb
parents:
12755
diff
changeset
|
279 n++; |
30861
1f3b39209b2d
Fix value of bit counter at end of functions to prepare for future patches.
cehoyos
parents:
30640
diff
changeset
|
280 n += read_timeinc(picture, buffer, n); |
14477
92553e3c8f01
automatic fps calculation for mpeg4 in raw stream/mpeg-ts
nicodvb
parents:
12755
diff
changeset
|
281 } |
14798
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
282 |
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
283 #define min(a, b) ((a) <= (b) ? (a) : (b)) |
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
284 |
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
285 static unsigned int read_golomb(unsigned char *buffer, unsigned int *init) |
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
286 { |
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
287 unsigned int x, v = 0, v2 = 0, m, len = 0, n = *init; |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29238
diff
changeset
|
288 |
14798
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
289 while(getbits(buffer, n++, 1) == 0) |
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
290 len++; |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29238
diff
changeset
|
291 |
14798
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
292 x = len + n; |
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
293 while(n < x) |
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
294 { |
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
295 m = min(x - n, 8); |
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
296 v |= getbits(buffer, n, m); |
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
297 n += m; |
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
298 if(x - n > 8) |
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
299 v <<= 8; |
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
300 } |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29238
diff
changeset
|
301 |
14798
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
302 v2 = 1; |
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
303 for(n = 0; n < len; n++) |
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
304 v2 <<= 1; |
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
305 v2 = (v2 - 1) + v; |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29238
diff
changeset
|
306 |
14798
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
307 //fprintf(stderr, "READ_GOLOMB(%u), V=2^%u + %u-1 = %u\n", *init, len, v, v2); |
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
308 *init = x; |
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
309 return v2; |
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
310 } |
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
311 |
34492
1495455e6d22
Move static keyword to the beginning of function declarations.
diego
parents:
34160
diff
changeset
|
312 static inline int read_golomb_s(unsigned char *buffer, unsigned int *init) |
29452
e8d71beb79b9
Fix H.264 SPS parsing in case of scaling list present.
cehoyos
parents:
29263
diff
changeset
|
313 { |
e8d71beb79b9
Fix H.264 SPS parsing in case of scaling list present.
cehoyos
parents:
29263
diff
changeset
|
314 unsigned int v = read_golomb(buffer, init); |
e8d71beb79b9
Fix H.264 SPS parsing in case of scaling list present.
cehoyos
parents:
29263
diff
changeset
|
315 return (v & 1) ? ((v + 1) >> 1) : -(v >> 1); |
e8d71beb79b9
Fix H.264 SPS parsing in case of scaling list present.
cehoyos
parents:
29263
diff
changeset
|
316 } |
14798
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
317 |
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
318 static int h264_parse_vui(mp_mpeg_header_t * picture, unsigned char * buf, unsigned int n) |
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
319 { |
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
320 unsigned int overscan, vsp_color, chroma, timing, fixed_fps; |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29238
diff
changeset
|
321 |
14798
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
322 if(getbits(buf, n++, 1)) |
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
323 { |
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
324 picture->aspect_ratio_information = getbits(buf, n, 8); |
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
325 n += 8; |
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
326 if(picture->aspect_ratio_information == 255) |
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
327 { |
34632
e0c6b271d55b
Do not parse the extended aspect info into the fields
reimar
parents:
34492
diff
changeset
|
328 // aspect numerator and denominator |
14798
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
329 n += 16; |
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
330 n += 16; |
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
331 } |
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
332 } |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29238
diff
changeset
|
333 |
14887 | 334 if((overscan=getbits(buf, n++, 1))) |
14798
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
335 n++; |
14887 | 336 if((vsp_color=getbits(buf, n++, 1))) |
14798
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
337 { |
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
338 n += 4; |
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
339 if(getbits(buf, n++, 1)) |
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
340 n += 24; |
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
341 } |
14887 | 342 if((chroma=getbits(buf, n++, 1))) |
14798
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
343 { |
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
344 read_golomb(buf, &n); |
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
345 read_golomb(buf, &n); |
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
346 } |
14887 | 347 if((timing=getbits(buf, n++, 1))) |
14798
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
348 { |
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
349 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); |
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
350 n += 32; |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29238
diff
changeset
|
351 |
14798
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
352 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); |
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
353 n += 32; |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29238
diff
changeset
|
354 |
14798
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
355 fixed_fps = getbits(buf, n, 1); |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29238
diff
changeset
|
356 |
14798
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
357 if(picture->timeinc_unit > 0 && picture->timeinc_resolution > 0) |
16319
798d9be2337f
multiplying fps by 10000 is no more necessary (when determining mp4v and h264 framerate)
nicodvb
parents:
16184
diff
changeset
|
358 picture->fps = (float) picture->timeinc_resolution / (float) picture->timeinc_unit; |
17952
7ad7d20cfadc
H264: when fixed_fps is set the framerate is expressed in fields per second, so it must be halved
nicodvb
parents:
16319
diff
changeset
|
359 if(fixed_fps) |
7ad7d20cfadc
H264: when fixed_fps is set the framerate is expressed in fields per second, so it must be halved
nicodvb
parents:
16319
diff
changeset
|
360 picture->fps /= 2; |
14798
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
361 } |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29238
diff
changeset
|
362 |
14798
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
363 //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, |
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
364 // picture->timeinc_resolution, picture->timeinc_unit, picture->timeinc_unit, fixed_fps); |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29238
diff
changeset
|
365 |
14798
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
366 return n; |
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
367 } |
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
368 |
34158
562cd6805eac
Stop h264_parse_sps and mp_vc1_decode_sequence_header from corrupting the
reimar
parents:
33339
diff
changeset
|
369 static int mp_unescape03(uint8_t *dest, const uint8_t *buf, int len) |
32347
6c6a59af9513
Move mp_unescape03 function around to avoid forward declaration.
diego
parents:
30862
diff
changeset
|
370 { |
6c6a59af9513
Move mp_unescape03 function around to avoid forward declaration.
diego
parents:
30862
diff
changeset
|
371 int i, j, skip; |
6c6a59af9513
Move mp_unescape03 function around to avoid forward declaration.
diego
parents:
30862
diff
changeset
|
372 |
6c6a59af9513
Move mp_unescape03 function around to avoid forward declaration.
diego
parents:
30862
diff
changeset
|
373 if(! dest) |
6c6a59af9513
Move mp_unescape03 function around to avoid forward declaration.
diego
parents:
30862
diff
changeset
|
374 return 0; |
6c6a59af9513
Move mp_unescape03 function around to avoid forward declaration.
diego
parents:
30862
diff
changeset
|
375 |
6c6a59af9513
Move mp_unescape03 function around to avoid forward declaration.
diego
parents:
30862
diff
changeset
|
376 j = i = skip = 0; |
6c6a59af9513
Move mp_unescape03 function around to avoid forward declaration.
diego
parents:
30862
diff
changeset
|
377 while(i <= len-3) |
6c6a59af9513
Move mp_unescape03 function around to avoid forward declaration.
diego
parents:
30862
diff
changeset
|
378 { |
6c6a59af9513
Move mp_unescape03 function around to avoid forward declaration.
diego
parents:
30862
diff
changeset
|
379 if(buf[i] == 0 && buf[i+1] == 0 && buf[i+2] == 3) |
6c6a59af9513
Move mp_unescape03 function around to avoid forward declaration.
diego
parents:
30862
diff
changeset
|
380 { |
6c6a59af9513
Move mp_unescape03 function around to avoid forward declaration.
diego
parents:
30862
diff
changeset
|
381 dest[j] = dest[j+1] = 0; |
6c6a59af9513
Move mp_unescape03 function around to avoid forward declaration.
diego
parents:
30862
diff
changeset
|
382 j += 2; |
6c6a59af9513
Move mp_unescape03 function around to avoid forward declaration.
diego
parents:
30862
diff
changeset
|
383 i += 3; |
6c6a59af9513
Move mp_unescape03 function around to avoid forward declaration.
diego
parents:
30862
diff
changeset
|
384 skip++; |
6c6a59af9513
Move mp_unescape03 function around to avoid forward declaration.
diego
parents:
30862
diff
changeset
|
385 } |
6c6a59af9513
Move mp_unescape03 function around to avoid forward declaration.
diego
parents:
30862
diff
changeset
|
386 else |
6c6a59af9513
Move mp_unescape03 function around to avoid forward declaration.
diego
parents:
30862
diff
changeset
|
387 { |
6c6a59af9513
Move mp_unescape03 function around to avoid forward declaration.
diego
parents:
30862
diff
changeset
|
388 dest[j] = buf[i]; |
6c6a59af9513
Move mp_unescape03 function around to avoid forward declaration.
diego
parents:
30862
diff
changeset
|
389 j++; |
6c6a59af9513
Move mp_unescape03 function around to avoid forward declaration.
diego
parents:
30862
diff
changeset
|
390 i++; |
6c6a59af9513
Move mp_unescape03 function around to avoid forward declaration.
diego
parents:
30862
diff
changeset
|
391 } |
6c6a59af9513
Move mp_unescape03 function around to avoid forward declaration.
diego
parents:
30862
diff
changeset
|
392 } |
6c6a59af9513
Move mp_unescape03 function around to avoid forward declaration.
diego
parents:
30862
diff
changeset
|
393 dest[j] = buf[len-2]; |
6c6a59af9513
Move mp_unescape03 function around to avoid forward declaration.
diego
parents:
30862
diff
changeset
|
394 dest[j+1] = buf[len-1]; |
6c6a59af9513
Move mp_unescape03 function around to avoid forward declaration.
diego
parents:
30862
diff
changeset
|
395 len -= skip; |
6c6a59af9513
Move mp_unescape03 function around to avoid forward declaration.
diego
parents:
30862
diff
changeset
|
396 |
6c6a59af9513
Move mp_unescape03 function around to avoid forward declaration.
diego
parents:
30862
diff
changeset
|
397 return len; |
6c6a59af9513
Move mp_unescape03 function around to avoid forward declaration.
diego
parents:
30862
diff
changeset
|
398 } |
21953 | 399 |
34158
562cd6805eac
Stop h264_parse_sps and mp_vc1_decode_sequence_header from corrupting the
reimar
parents:
33339
diff
changeset
|
400 int h264_parse_sps(mp_mpeg_header_t * picture, const unsigned char * inbuf, int len) |
14798
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
401 { |
29452
e8d71beb79b9
Fix H.264 SPS parsing in case of scaling list present.
cehoyos
parents:
29263
diff
changeset
|
402 unsigned int n = 0, v, i, k, mbh; |
15073 | 403 int frame_mbs_only; |
34158
562cd6805eac
Stop h264_parse_sps and mp_vc1_decode_sequence_header from corrupting the
reimar
parents:
33339
diff
changeset
|
404 uint8_t *buf = malloc(len); |
14798
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
405 |
34158
562cd6805eac
Stop h264_parse_sps and mp_vc1_decode_sequence_header from corrupting the
reimar
parents:
33339
diff
changeset
|
406 len = mp_unescape03(buf, inbuf, len); |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29238
diff
changeset
|
407 |
14798
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
408 picture->fps = picture->timeinc_unit = picture->timeinc_resolution = 0; |
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
409 n = 24; |
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
410 read_golomb(buf, &n); |
15618 | 411 if(buf[0] >= 100){ |
412 if(read_golomb(buf, &n) == 3) | |
413 n++; | |
414 read_golomb(buf, &n); | |
415 read_golomb(buf, &n); | |
416 n++; | |
417 if(getbits(buf, n++, 1)){ | |
29452
e8d71beb79b9
Fix H.264 SPS parsing in case of scaling list present.
cehoyos
parents:
29263
diff
changeset
|
418 for(i = 0; i < 8; i++) |
e8d71beb79b9
Fix H.264 SPS parsing in case of scaling list present.
cehoyos
parents:
29263
diff
changeset
|
419 { // scaling list is skipped for now |
e8d71beb79b9
Fix H.264 SPS parsing in case of scaling list present.
cehoyos
parents:
29263
diff
changeset
|
420 if(getbits(buf, n++, 1)) |
e8d71beb79b9
Fix H.264 SPS parsing in case of scaling list present.
cehoyos
parents:
29263
diff
changeset
|
421 { |
e8d71beb79b9
Fix H.264 SPS parsing in case of scaling list present.
cehoyos
parents:
29263
diff
changeset
|
422 v = 8; |
e8d71beb79b9
Fix H.264 SPS parsing in case of scaling list present.
cehoyos
parents:
29263
diff
changeset
|
423 for(k = (i < 6 ? 16 : 64); k && v; k--) |
e8d71beb79b9
Fix H.264 SPS parsing in case of scaling list present.
cehoyos
parents:
29263
diff
changeset
|
424 v = (v + read_golomb_s(buf, &n)) & 255; |
e8d71beb79b9
Fix H.264 SPS parsing in case of scaling list present.
cehoyos
parents:
29263
diff
changeset
|
425 } |
e8d71beb79b9
Fix H.264 SPS parsing in case of scaling list present.
cehoyos
parents:
29263
diff
changeset
|
426 } |
15618 | 427 } |
428 } | |
14798
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
429 read_golomb(buf, &n); |
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
430 v = read_golomb(buf, &n); |
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
431 if(v == 0) |
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
432 read_golomb(buf, &n); |
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
433 else if(v == 1) |
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
434 { |
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
435 getbits(buf, n++, 1); |
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
436 read_golomb(buf, &n); |
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
437 read_golomb(buf, &n); |
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
438 v = read_golomb(buf, &n); |
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
439 for(i = 0; i < v; i++) |
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
440 read_golomb(buf, &n); |
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
441 } |
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
442 read_golomb(buf, &n); |
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
443 getbits(buf, n++, 1); |
15073 | 444 picture->display_picture_width = 16 *(read_golomb(buf, &n)+1); |
445 mbh = read_golomb(buf, &n)+1; | |
446 frame_mbs_only = getbits(buf, n++, 1); | |
447 picture->display_picture_height = 16 * (2 - frame_mbs_only) * mbh; | |
448 if(!frame_mbs_only) | |
14798
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
449 getbits(buf, n++, 1); |
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
450 getbits(buf, n++, 1); |
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
451 if(getbits(buf, n++, 1)) |
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
452 { |
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
453 read_golomb(buf, &n); |
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
454 read_golomb(buf, &n); |
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
455 read_golomb(buf, &n); |
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
456 read_golomb(buf, &n); |
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
457 } |
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
458 if(getbits(buf, n++, 1)) |
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
459 n = h264_parse_vui(picture, buf, n); |
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
460 |
34158
562cd6805eac
Stop h264_parse_sps and mp_vc1_decode_sequence_header from corrupting the
reimar
parents:
33339
diff
changeset
|
461 free(buf); |
562cd6805eac
Stop h264_parse_sps and mp_vc1_decode_sequence_header from corrupting the
reimar
parents:
33339
diff
changeset
|
462 |
14798
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
463 return n; |
0bd50330e688
framerate autodetection for H264 in raw/ts streams
nicodvb
parents:
14477
diff
changeset
|
464 } |
21947 | 465 |
34158
562cd6805eac
Stop h264_parse_sps and mp_vc1_decode_sequence_header from corrupting the
reimar
parents:
33339
diff
changeset
|
466 int mp_vc1_decode_sequence_header(mp_mpeg_header_t * picture, const unsigned char * inbuf, int len) |
21947 | 467 { |
468 int n, x; | |
34158
562cd6805eac
Stop h264_parse_sps and mp_vc1_decode_sequence_header from corrupting the
reimar
parents:
33339
diff
changeset
|
469 uint8_t *buf = malloc(len); |
21947 | 470 |
34158
562cd6805eac
Stop h264_parse_sps and mp_vc1_decode_sequence_header from corrupting the
reimar
parents:
33339
diff
changeset
|
471 len = mp_unescape03(buf, inbuf, len); |
21947 | 472 |
473 picture->display_picture_width = picture->display_picture_height = 0; | |
474 picture->fps = 0; | |
475 n = 0; | |
476 x = getbits(buf, n, 2); | |
477 n += 2; | |
478 if(x != 3) //not advanced profile | |
34158
562cd6805eac
Stop h264_parse_sps and mp_vc1_decode_sequence_header from corrupting the
reimar
parents:
33339
diff
changeset
|
479 goto err_out; |
21947 | 480 |
481 getbits16(buf, n, 14); | |
482 n += 14; | |
483 picture->display_picture_width = getbits16(buf, n, 12) * 2 + 2; | |
484 n += 12; | |
485 picture->display_picture_height = getbits16(buf, n, 12) * 2 + 2; | |
486 n += 12; | |
487 getbits(buf, n, 6); | |
488 n += 6; | |
489 x = getbits(buf, n, 1); | |
490 n += 1; | |
491 if(x) //display info | |
492 { | |
493 getbits16(buf, n, 14); | |
494 n += 14; | |
495 getbits16(buf, n, 14); | |
496 n += 14; | |
497 if(getbits(buf, n++, 1)) //aspect ratio | |
498 { | |
499 x = getbits(buf, n, 4); | |
500 n += 4; | |
501 if(x == 15) | |
502 { | |
503 getbits16(buf, n, 16); | |
504 n += 16; | |
505 } | |
506 } | |
507 | |
508 if(getbits(buf, n++, 1)) //framerates | |
509 { | |
510 int frexp=0, frnum=0, frden=0; | |
511 | |
512 if(getbits(buf, n++, 1)) | |
513 { | |
514 frexp = getbits16(buf, n, 16); | |
515 n += 16; | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29238
diff
changeset
|
516 picture->fps = (double) (frexp+1) / 32.0; |
21947 | 517 } |
518 else | |
519 { | |
520 float frates[] = {0, 24000, 25000, 30000, 50000, 60000, 48000, 72000, 0}; | |
521 float frdivs[] = {0, 1000, 1001, 0}; | |
522 | |
523 frnum = getbits(buf, n, 8); | |
524 n += 8; | |
525 frden = getbits(buf, n, 4); | |
526 n += 4; | |
527 if((frden == 1 || frden == 2) && (frnum < 8)) | |
528 picture->fps = frates[frnum] / frdivs[frden]; | |
529 } | |
530 } | |
531 } | |
532 | |
34158
562cd6805eac
Stop h264_parse_sps and mp_vc1_decode_sequence_header from corrupting the
reimar
parents:
33339
diff
changeset
|
533 free(buf); |
21947 | 534 return 1; |
34158
562cd6805eac
Stop h264_parse_sps and mp_vc1_decode_sequence_header from corrupting the
reimar
parents:
33339
diff
changeset
|
535 |
562cd6805eac
Stop h264_parse_sps and mp_vc1_decode_sequence_header from corrupting the
reimar
parents:
33339
diff
changeset
|
536 err_out: |
562cd6805eac
Stop h264_parse_sps and mp_vc1_decode_sequence_header from corrupting the
reimar
parents:
33339
diff
changeset
|
537 free(buf); |
562cd6805eac
Stop h264_parse_sps and mp_vc1_decode_sequence_header from corrupting the
reimar
parents:
33339
diff
changeset
|
538 return 0; |
21947 | 539 } |