Mercurial > mplayer.hg
annotate libmpdemux/demux_rtp_codec.cpp @ 22188:b9fa8b0ddab9
strncat() misuses, may have been exploitable.
Reported by Sascha Sommer.
author | rtogni |
---|---|
date | Sun, 11 Feb 2007 18:00:06 +0000 |
parents | 1dc228327c55 |
children | 7c9d838fc788 |
rev | line source |
---|---|
9250 | 1 ////////// Codec-specific routines used to interface between "MPlayer" |
16572
56a5f69e9b35
"LIVE.COM Streaming Media" is now called "LIVE555 Streaming Media".
rsf
parents:
11398
diff
changeset
|
2 ////////// and the "LIVE555 Streaming Media" libraries: |
9250 | 3 |
4 #include "demux_rtp_internal.h" | |
5 extern "C" { | |
21911
e86bb13ec44b
demux_rtp_codec.cpp:100: `INT_MAX' undeclared (first use this function)
diego
parents:
21871
diff
changeset
|
6 #include <limits.h> |
21983
a6b624360aef
better autodetection of framerate in case of h264; it works correctly with b-frames.
nicodvb
parents:
21911
diff
changeset
|
7 #include <math.h> |
9250 | 8 #include "stheader.h" |
9 } | |
10 | |
9565
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
11 static void |
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
12 needVideoFrameRate(demuxer_t* demuxer, MediaSubsession* subsession); // forward |
9250 | 13 static Boolean |
14 parseQTState_video(QuickTimeGenericRTPSource::QTState const& qtState, | |
15 unsigned& fourcc); // forward | |
16 static Boolean | |
17 parseQTState_audio(QuickTimeGenericRTPSource::QTState const& qtState, | |
18 unsigned& fourcc, unsigned& numChannels); // forward | |
19 | |
20 void rtpCodecInitialize_video(demuxer_t* demuxer, | |
21 MediaSubsession* subsession, | |
22 unsigned& flags) { | |
23 flags = 0; | |
24 // Create a dummy video stream header | |
25 // to make the main MPlayer code happy: | |
26 sh_video_t* sh_video = new_sh_video(demuxer,0); | |
27 BITMAPINFOHEADER* bih | |
28 = (BITMAPINFOHEADER*)calloc(1,sizeof(BITMAPINFOHEADER)); | |
29 bih->biSize = sizeof(BITMAPINFOHEADER); | |
30 sh_video->bih = bih; | |
31 demux_stream_t* d_video = demuxer->video; | |
32 d_video->sh = sh_video; sh_video->ds = d_video; | |
33 | |
34 // Map known video MIME types to the BITMAPINFOHEADER parameters | |
35 // that this program uses. (Note that not all types need all | |
36 // of the parameters to be set.) | |
10478
b0d0aa726417
Added support for the "L16" and "L8" (raw PCM audio) RTP payload formats.
rsf
parents:
9910
diff
changeset
|
37 if (strcmp(subsession->codecName(), "MPV") == 0) { |
9565
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
38 flags |= RTPSTATE_IS_MPEG12_VIDEO; |
10478
b0d0aa726417
Added support for the "L16" and "L8" (raw PCM audio) RTP payload formats.
rsf
parents:
9910
diff
changeset
|
39 } else if (strcmp(subsession->codecName(), "MP1S") == 0 || |
b0d0aa726417
Added support for the "L16" and "L8" (raw PCM audio) RTP payload formats.
rsf
parents:
9910
diff
changeset
|
40 strcmp(subsession->codecName(), "MP2T") == 0) { |
b0d0aa726417
Added support for the "L16" and "L8" (raw PCM audio) RTP payload formats.
rsf
parents:
9910
diff
changeset
|
41 flags |= RTPSTATE_IS_MPEG12_VIDEO|RTPSTATE_IS_MULTIPLEXED; |
9250 | 42 } else if (strcmp(subsession->codecName(), "H263") == 0 || |
43 strcmp(subsession->codecName(), "H263-1998") == 0) { | |
44 bih->biCompression = sh_video->format | |
45 = mmioFOURCC('H','2','6','3'); | |
9565
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
46 needVideoFrameRate(demuxer, subsession); |
19415 | 47 } else if (strcmp(subsession->codecName(), "H264") == 0) { |
48 bih->biCompression = sh_video->format | |
49 = mmioFOURCC('H','2','6','4'); | |
50 needVideoFrameRate(demuxer, subsession); | |
9250 | 51 } else if (strcmp(subsession->codecName(), "H261") == 0) { |
52 bih->biCompression = sh_video->format | |
53 = mmioFOURCC('H','2','6','1'); | |
9565
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
54 needVideoFrameRate(demuxer, subsession); |
9370
88bd19564b64
Motion-JPEG RTP streams can now be played. Some MPEG-4 ES video RTP
arpi
parents:
9250
diff
changeset
|
55 } else if (strcmp(subsession->codecName(), "JPEG") == 0) { |
88bd19564b64
Motion-JPEG RTP streams can now be played. Some MPEG-4 ES video RTP
arpi
parents:
9250
diff
changeset
|
56 bih->biCompression = sh_video->format |
88bd19564b64
Motion-JPEG RTP streams can now be played. Some MPEG-4 ES video RTP
arpi
parents:
9250
diff
changeset
|
57 = mmioFOURCC('M','J','P','G'); |
9565
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
58 needVideoFrameRate(demuxer, subsession); |
9370
88bd19564b64
Motion-JPEG RTP streams can now be played. Some MPEG-4 ES video RTP
arpi
parents:
9250
diff
changeset
|
59 } else if (strcmp(subsession->codecName(), "MP4V-ES") == 0) { |
88bd19564b64
Motion-JPEG RTP streams can now be played. Some MPEG-4 ES video RTP
arpi
parents:
9250
diff
changeset
|
60 bih->biCompression = sh_video->format |
88bd19564b64
Motion-JPEG RTP streams can now be played. Some MPEG-4 ES video RTP
arpi
parents:
9250
diff
changeset
|
61 = mmioFOURCC('m','p','4','v'); |
9565
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
62 // For the codec to work correctly, it may need a 'VOL Header' to be |
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
63 // inserted at the front of the data stream. Construct this from the |
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
64 // "config" MIME parameter, which was present (hopefully) in the |
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
65 // session's SDP description: |
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
66 unsigned configLen; |
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
67 unsigned char* configData |
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
68 = parseGeneralConfigStr(subsession->fmtp_config(), configLen); |
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
69 insertRTPData(demuxer, demuxer->video, configData, configLen); |
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
70 needVideoFrameRate(demuxer, subsession); |
9250 | 71 } else if (strcmp(subsession->codecName(), "X-QT") == 0 || |
72 strcmp(subsession->codecName(), "X-QUICKTIME") == 0) { | |
73 // QuickTime generic RTP format, as described in | |
74 // http://developer.apple.com/quicktime/icefloe/dispatch026.html | |
75 | |
76 // We can't initialize this stream until we've received the first packet | |
77 // that has QuickTime "sdAtom" information in the header. So, keep | |
78 // reading packets until we get one: | |
9565
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
79 unsigned char* packetData; unsigned packetDataLen; float pts; |
9250 | 80 QuickTimeGenericRTPSource* qtRTPSource |
81 = (QuickTimeGenericRTPSource*)(subsession->rtpSource()); | |
82 unsigned fourcc; | |
83 do { | |
9565
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
84 if (!awaitRTPPacket(demuxer, demuxer->video, |
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
85 packetData, packetDataLen, pts)) { |
9250 | 86 return; |
87 } | |
88 } while (!parseQTState_video(qtRTPSource->qtState, fourcc)); | |
89 | |
90 bih->biCompression = sh_video->format = fourcc; | |
21871 | 91 bih->biWidth = qtRTPSource->qtState.width; |
92 bih->biHeight = qtRTPSource->qtState.height; | |
22112
c9214f2c30c4
support for SVQ3 in X-QT; patch by Carl Eugen Hoyos (cehoyos ag or at) approved by Ross
nicodvb
parents:
21983
diff
changeset
|
93 if (bih->biCompression == mmioFOURCC('a','v','c','1') || |
22154
1dc228327c55
when the video codec is mpeg4video copy the content of the esds in extradata; patch by ceyes ag or at
nicodvb
parents:
22112
diff
changeset
|
94 bih->biCompression == mmioFOURCC('m','p','4','v') || |
22112
c9214f2c30c4
support for SVQ3 in X-QT; patch by Carl Eugen Hoyos (cehoyos ag or at) approved by Ross
nicodvb
parents:
21983
diff
changeset
|
95 bih->biCompression == mmioFOURCC('S','V','Q','3')) { |
21871 | 96 uint8_t *pos = (uint8_t*)qtRTPSource->qtState.sdAtom + 86; |
97 uint8_t *endpos = (uint8_t*)qtRTPSource->qtState.sdAtom | |
98 + qtRTPSource->qtState.sdAtomSize; | |
99 while (pos+8 < endpos) { | |
100 unsigned atomLength = pos[0]<<24 | pos[1]<<16 | pos[2]<<8 | pos[3]; | |
101 if (atomLength == 0 || atomLength > endpos-pos) break; | |
22154
1dc228327c55
when the video codec is mpeg4video copy the content of the esds in extradata; patch by ceyes ag or at
nicodvb
parents:
22112
diff
changeset
|
102 if ((!memcmp(pos+4, "avcC", 4) || |
1dc228327c55
when the video codec is mpeg4video copy the content of the esds in extradata; patch by ceyes ag or at
nicodvb
parents:
22112
diff
changeset
|
103 !memcmp(pos+4, "esds", 4) || |
1dc228327c55
when the video codec is mpeg4video copy the content of the esds in extradata; patch by ceyes ag or at
nicodvb
parents:
22112
diff
changeset
|
104 !memcmp(pos+4, "SMI ", 4)) && |
21871 | 105 atomLength > 8 && |
106 atomLength <= INT_MAX-sizeof(BITMAPINFOHEADER)) { | |
107 bih->biSize = sizeof(BITMAPINFOHEADER)+atomLength-8; | |
108 sh_video->bih = bih = (BITMAPINFOHEADER*)realloc(bih, bih->biSize); | |
109 memcpy(bih+1, pos+8, atomLength-8); | |
110 break; | |
111 } | |
112 pos += atomLength; | |
113 } | |
114 needVideoFrameRate(demuxer, subsession); | |
115 } | |
9250 | 116 } else { |
117 fprintf(stderr, | |
118 "Unknown MPlayer format code for MIME type \"video/%s\"\n", | |
119 subsession->codecName()); | |
120 } | |
121 } | |
122 | |
123 void rtpCodecInitialize_audio(demuxer_t* demuxer, | |
124 MediaSubsession* subsession, | |
125 unsigned& flags) { | |
126 flags = 0; | |
127 // Create a dummy audio stream header | |
128 // to make the main MPlayer code happy: | |
129 sh_audio_t* sh_audio = new_sh_audio(demuxer,0); | |
130 WAVEFORMATEX* wf = (WAVEFORMATEX*)calloc(1,sizeof(WAVEFORMATEX)); | |
131 sh_audio->wf = wf; | |
132 demux_stream_t* d_audio = demuxer->audio; | |
133 d_audio->sh = sh_audio; sh_audio->ds = d_audio; | |
134 | |
9565
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
135 wf->nChannels = subsession->numChannels(); |
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
136 |
9250 | 137 // Map known audio MIME types to the WAVEFORMATEX parameters |
138 // that this program uses. (Note that not all types need all | |
139 // of the parameters to be set.) | |
140 wf->nSamplesPerSec | |
141 = subsession->rtpSource()->timestampFrequency(); // by default | |
142 if (strcmp(subsession->codecName(), "MPA") == 0 || | |
143 strcmp(subsession->codecName(), "MPA-ROBUST") == 0 || | |
144 strcmp(subsession->codecName(), "X-MP3-DRAFT-00") == 0) { | |
145 wf->wFormatTag = sh_audio->format = 0x55; | |
146 // Note: 0x55 is for layer III, but should work for I,II also | |
147 wf->nSamplesPerSec = 0; // sample rate is deduced from the data | |
148 } else if (strcmp(subsession->codecName(), "AC3") == 0) { | |
149 wf->wFormatTag = sh_audio->format = 0x2000; | |
150 wf->nSamplesPerSec = 0; // sample rate is deduced from the data | |
10478
b0d0aa726417
Added support for the "L16" and "L8" (raw PCM audio) RTP payload formats.
rsf
parents:
9910
diff
changeset
|
151 } else if (strcmp(subsession->codecName(), "L16") == 0) { |
b0d0aa726417
Added support for the "L16" and "L8" (raw PCM audio) RTP payload formats.
rsf
parents:
9910
diff
changeset
|
152 wf->wFormatTag = sh_audio->format = 0x736f7774; // "twos" |
b0d0aa726417
Added support for the "L16" and "L8" (raw PCM audio) RTP payload formats.
rsf
parents:
9910
diff
changeset
|
153 wf->nBlockAlign = 1; |
b0d0aa726417
Added support for the "L16" and "L8" (raw PCM audio) RTP payload formats.
rsf
parents:
9910
diff
changeset
|
154 wf->wBitsPerSample = 16; |
b0d0aa726417
Added support for the "L16" and "L8" (raw PCM audio) RTP payload formats.
rsf
parents:
9910
diff
changeset
|
155 wf->cbSize = 0; |
b0d0aa726417
Added support for the "L16" and "L8" (raw PCM audio) RTP payload formats.
rsf
parents:
9910
diff
changeset
|
156 } else if (strcmp(subsession->codecName(), "L8") == 0) { |
b0d0aa726417
Added support for the "L16" and "L8" (raw PCM audio) RTP payload formats.
rsf
parents:
9910
diff
changeset
|
157 wf->wFormatTag = sh_audio->format = 0x20776172; // "raw " |
b0d0aa726417
Added support for the "L16" and "L8" (raw PCM audio) RTP payload formats.
rsf
parents:
9910
diff
changeset
|
158 wf->nBlockAlign = 1; |
b0d0aa726417
Added support for the "L16" and "L8" (raw PCM audio) RTP payload formats.
rsf
parents:
9910
diff
changeset
|
159 wf->wBitsPerSample = 8; |
b0d0aa726417
Added support for the "L16" and "L8" (raw PCM audio) RTP payload formats.
rsf
parents:
9910
diff
changeset
|
160 wf->cbSize = 0; |
9250 | 161 } else if (strcmp(subsession->codecName(), "PCMU") == 0) { |
162 wf->wFormatTag = sh_audio->format = 0x7; | |
163 wf->nAvgBytesPerSec = 8000; | |
164 wf->nBlockAlign = 1; | |
165 wf->wBitsPerSample = 8; | |
166 wf->cbSize = 0; | |
167 } else if (strcmp(subsession->codecName(), "PCMA") == 0) { | |
168 wf->wFormatTag = sh_audio->format = 0x6; | |
169 wf->nAvgBytesPerSec = 8000; | |
170 wf->nBlockAlign = 1; | |
171 wf->wBitsPerSample = 8; | |
172 wf->cbSize = 0; | |
173 } else if (strcmp(subsession->codecName(), "GSM") == 0) { | |
174 wf->wFormatTag = sh_audio->format = mmioFOURCC('a','g','s','m'); | |
175 wf->nAvgBytesPerSec = 1650; | |
176 wf->nBlockAlign = 33; | |
177 wf->wBitsPerSample = 16; | |
178 wf->cbSize = 0; | |
179 } else if (strcmp(subsession->codecName(), "QCELP") == 0) { | |
180 wf->wFormatTag = sh_audio->format = mmioFOURCC('Q','c','l','p'); | |
181 wf->nAvgBytesPerSec = 1750; | |
182 wf->nBlockAlign = 35; | |
183 wf->wBitsPerSample = 16; | |
184 wf->cbSize = 0; | |
185 } else if (strcmp(subsession->codecName(), "MP4A-LATM") == 0) { | |
186 wf->wFormatTag = sh_audio->format = mmioFOURCC('m','p','4','a'); | |
187 // For the codec to work correctly, it needs "AudioSpecificConfig" | |
188 // data, which is parsed from the "StreamMuxConfig" string that | |
189 // was present (hopefully) in the SDP description: | |
190 unsigned codecdata_len; | |
191 sh_audio->codecdata | |
192 = parseStreamMuxConfigStr(subsession->fmtp_config(), | |
193 codecdata_len); | |
194 sh_audio->codecdata_len = codecdata_len; | |
9565
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
195 } else if (strcmp(subsession->codecName(), "MPEG4-GENERIC") == 0) { |
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
196 wf->wFormatTag = sh_audio->format = mmioFOURCC('m','p','4','a'); |
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
197 // For the codec to work correctly, it needs "AudioSpecificConfig" |
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
198 // data, which was present (hopefully) in the SDP description: |
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
199 unsigned codecdata_len; |
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
200 sh_audio->codecdata |
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
201 = parseGeneralConfigStr(subsession->fmtp_config(), |
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
202 codecdata_len); |
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
203 sh_audio->codecdata_len = codecdata_len; |
9250 | 204 } else if (strcmp(subsession->codecName(), "X-QT") == 0 || |
205 strcmp(subsession->codecName(), "X-QUICKTIME") == 0) { | |
206 // QuickTime generic RTP format, as described in | |
207 // http://developer.apple.com/quicktime/icefloe/dispatch026.html | |
208 | |
209 // We can't initialize this stream until we've received the first packet | |
210 // that has QuickTime "sdAtom" information in the header. So, keep | |
211 // reading packets until we get one: | |
9565
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
212 unsigned char* packetData; unsigned packetDataLen; float pts; |
9250 | 213 QuickTimeGenericRTPSource* qtRTPSource |
214 = (QuickTimeGenericRTPSource*)(subsession->rtpSource()); | |
215 unsigned fourcc, numChannels; | |
216 do { | |
9565
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
217 if (!awaitRTPPacket(demuxer, demuxer->audio, |
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
218 packetData, packetDataLen, pts)) { |
9250 | 219 return; |
220 } | |
221 } while (!parseQTState_audio(qtRTPSource->qtState, fourcc, numChannels)); | |
222 | |
223 wf->wFormatTag = sh_audio->format = fourcc; | |
224 wf->nChannels = numChannels; | |
225 } else { | |
226 fprintf(stderr, | |
227 "Unknown MPlayer format code for MIME type \"audio/%s\"\n", | |
228 subsession->codecName()); | |
229 } | |
230 } | |
231 | |
9565
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
232 static void needVideoFrameRate(demuxer_t* demuxer, |
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
233 MediaSubsession* subsession) { |
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
234 // For some codecs, MPlayer's decoding software can't (or refuses to :-) |
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
235 // figure out the frame rate by itself, so (unless the user specifies |
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
236 // it manually, using "-fps") we figure it out ourselves here, using the |
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
237 // presentation timestamps in successive packets, |
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
238 extern float force_fps; if (force_fps != 0.0) return; // user used "-fps" |
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
239 |
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
240 demux_stream_t* d_video = demuxer->video; |
9910 | 241 sh_video_t* sh_video = (sh_video_t*)(d_video->sh); |
9565
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
242 |
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
243 // If we already know the subsession's video frame rate, use it: |
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
244 int fps = (int)(subsession->videoFPS()); |
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
245 if (fps != 0) { |
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
246 sh_video->fps = fps; |
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
247 return; |
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
248 } |
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
249 |
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
250 // Keep looking at incoming frames until we see two with different, |
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
251 // non-zero "pts" timestamps: |
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
252 unsigned char* packetData; unsigned packetDataLen; |
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
253 float lastPTS = 0.0, curPTS; |
11397
d0db11b74e82
Increased the threshold for how many incoming frames to look at while guessing
rsf
parents:
10478
diff
changeset
|
254 unsigned const maxNumFramesToWaitFor = 300; |
21983
a6b624360aef
better autodetection of framerate in case of h264; it works correctly with b-frames.
nicodvb
parents:
21911
diff
changeset
|
255 int lastfps = 0; |
9565
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
256 for (unsigned i = 0; i < maxNumFramesToWaitFor; ++i) { |
9910 | 257 if (!awaitRTPPacket(demuxer, d_video, packetData, packetDataLen, curPTS)) { |
258 break; | |
259 } | |
9565
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
260 |
21983
a6b624360aef
better autodetection of framerate in case of h264; it works correctly with b-frames.
nicodvb
parents:
21911
diff
changeset
|
261 if (curPTS != lastPTS && lastPTS != 0.0) { |
9565
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
262 // Use the difference between these two "pts"s to guess the frame rate. |
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
263 // (should really check that there were no missing frames inbetween)##### |
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
264 // Guess the frame rate as an integer. If it's not, use "-fps" instead. |
21983
a6b624360aef
better autodetection of framerate in case of h264; it works correctly with b-frames.
nicodvb
parents:
21911
diff
changeset
|
265 fps = (int)(1/fabs(curPTS-lastPTS) + 0.5); // rounding |
a6b624360aef
better autodetection of framerate in case of h264; it works correctly with b-frames.
nicodvb
parents:
21911
diff
changeset
|
266 if (fps == lastfps) { |
9565
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
267 fprintf(stderr, "demux_rtp: Guessed the video frame rate as %d frames-per-second.\n\t(If this is wrong, use the \"-fps <frame-rate>\" option instead.)\n", fps); |
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
268 sh_video->fps = fps; |
21983
a6b624360aef
better autodetection of framerate in case of h264; it works correctly with b-frames.
nicodvb
parents:
21911
diff
changeset
|
269 sh_video->frametime=1.0f/fps; |
9565
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
270 return; |
21983
a6b624360aef
better autodetection of framerate in case of h264; it works correctly with b-frames.
nicodvb
parents:
21911
diff
changeset
|
271 } |
a6b624360aef
better autodetection of framerate in case of h264; it works correctly with b-frames.
nicodvb
parents:
21911
diff
changeset
|
272 if (fps>lastfps) lastfps = fps; |
9565
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
273 } |
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
274 lastPTS = curPTS; |
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
275 } |
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
276 fprintf(stderr, "demux_rtp: Failed to guess the video frame rate\n"); |
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
277 } |
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
278 |
9250 | 279 static Boolean |
280 parseQTState_video(QuickTimeGenericRTPSource::QTState const& qtState, | |
281 unsigned& fourcc) { | |
282 // qtState's "sdAtom" field is supposed to contain a QuickTime video | |
283 // 'sample description' atom. This atom's name is the 'fourcc' that we want: | |
284 char const* sdAtom = qtState.sdAtom; | |
285 if (sdAtom == NULL || qtState.sdAtomSize < 2*4) return False; | |
286 | |
287 fourcc = *(unsigned*)(&sdAtom[4]); // put in host order | |
288 return True; | |
289 } | |
290 | |
291 static Boolean | |
292 parseQTState_audio(QuickTimeGenericRTPSource::QTState const& qtState, | |
293 unsigned& fourcc, unsigned& numChannels) { | |
294 // qtState's "sdAtom" field is supposed to contain a QuickTime audio | |
295 // 'sample description' atom. This atom's name is the 'fourcc' that we want. | |
296 // Also, the top half of the 5th word following the atom name should | |
297 // contain the number of channels ("numChannels") that we want: | |
298 char const* sdAtom = qtState.sdAtom; | |
299 if (sdAtom == NULL || qtState.sdAtomSize < 7*4) return False; | |
300 | |
301 fourcc = *(unsigned*)(&sdAtom[4]); // put in host order | |
302 | |
303 char const* word7Ptr = &sdAtom[6*4]; | |
304 numChannels = (word7Ptr[0]<<8)|(word7Ptr[1]); | |
305 return True; | |
306 } |