2 * codec-specific routines used to interface between MPlayer
3 * and the "LIVE555 Streaming Media" libraries
5 * This file is part of MPlayer.
7 * MPlayer is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * MPlayer is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "demux_rtp_internal.h"
27 #include "libavutil/base64.h"
30 #ifdef CONFIG_LIBAVCODEC
31 AVCodecParserContext
* h264parserctx
;
35 static unsigned char* parseH264ConfigStr( char const* configStr
,
36 unsigned int& configSize
)
44 if( configStr
== NULL
|| *configStr
== '\0' )
46 psz
= dup
= strdup( configStr
);
48 /* Count the number of comma's */
49 for( psz
= dup
; *psz
!= '\0'; ++psz
)
58 unsigned char *cfg
= new unsigned char[5 * strlen(dup
)];
60 for( i
= 0; i
< i_records
; i
++ )
63 cfg
[configSize
++] = 0x00;
64 cfg
[configSize
++] = 0x00;
65 cfg
[configSize
++] = 0x01;
66 configSize
+= av_base64_decode( (uint8_t*)&cfg
[configSize
],
68 5 * strlen(dup
) - 3 );
72 if( dup
) free( dup
);
78 needVideoFrameRate(demuxer_t
* demuxer
, MediaSubsession
* subsession
); // forward
80 parseQTState_video(QuickTimeGenericRTPSource::QTState
const& qtState
,
81 unsigned& fourcc
); // forward
83 parseQTState_audio(QuickTimeGenericRTPSource::QTState
const& qtState
,
84 unsigned& fourcc
, unsigned& numChannels
); // forward
86 static BITMAPINFOHEADER
* insertVideoExtradata(BITMAPINFOHEADER
*bih
,
87 unsigned char * extraData
,
90 BITMAPINFOHEADER
* original
= bih
;
91 if (!size
|| size
> INT_MAX
- sizeof(BITMAPINFOHEADER
))
93 bih
= (BITMAPINFOHEADER
*)realloc(bih
, sizeof(BITMAPINFOHEADER
) + size
);
96 bih
->biSize
= sizeof(BITMAPINFOHEADER
) + size
;
97 memcpy(bih
+1, extraData
, size
);
101 void rtpCodecInitialize_video(demuxer_t
* demuxer
,
102 MediaSubsession
* subsession
,
105 // Create a dummy video stream header
106 // to make the main MPlayer code happy:
107 sh_video_t
* sh_video
= new_sh_video(demuxer
,0);
108 BITMAPINFOHEADER
* bih
109 = (BITMAPINFOHEADER
*)calloc(1,sizeof(BITMAPINFOHEADER
));
110 bih
->biSize
= sizeof(BITMAPINFOHEADER
);
112 demux_stream_t
* d_video
= demuxer
->video
;
113 d_video
->sh
= sh_video
; sh_video
->ds
= d_video
;
115 // Map known video MIME types to the BITMAPINFOHEADER parameters
116 // that this program uses. (Note that not all types need all
117 // of the parameters to be set.)
118 if (strcmp(subsession
->codecName(), "MPV") == 0) {
119 flags
|= RTPSTATE_IS_MPEG12_VIDEO
;
120 } else if (strcmp(subsession
->codecName(), "MP1S") == 0 ||
121 strcmp(subsession
->codecName(), "MP2T") == 0) {
122 flags
|= RTPSTATE_IS_MPEG12_VIDEO
|RTPSTATE_IS_MULTIPLEXED
;
123 } else if (strcmp(subsession
->codecName(), "H263") == 0 ||
124 strcmp(subsession
->codecName(), "H263-2000") == 0 ||
125 strcmp(subsession
->codecName(), "H263-1998") == 0) {
126 bih
->biCompression
= sh_video
->format
127 = mmioFOURCC('H','2','6','3');
128 needVideoFrameRate(demuxer
, subsession
);
129 } else if (strcmp(subsession
->codecName(), "H264") == 0) {
130 bih
->biCompression
= sh_video
->format
131 = mmioFOURCC('H','2','6','4');
132 unsigned int configLen
= 0;
133 unsigned char* configData
134 = parseH264ConfigStr(subsession
->fmtp_spropparametersets(), configLen
);
135 sh_video
->bih
= bih
= insertVideoExtradata(bih
, configData
, configLen
);
137 #ifdef CONFIG_LIBAVCODEC
138 avcodec_register_all();
139 h264parserctx
= av_parser_init(CODEC_ID_H264
);
141 needVideoFrameRate(demuxer
, subsession
);
142 } else if (strcmp(subsession
->codecName(), "H261") == 0) {
143 bih
->biCompression
= sh_video
->format
144 = mmioFOURCC('H','2','6','1');
145 needVideoFrameRate(demuxer
, subsession
);
146 } else if (strcmp(subsession
->codecName(), "JPEG") == 0) {
147 bih
->biCompression
= sh_video
->format
148 = mmioFOURCC('M','J','P','G');
149 needVideoFrameRate(demuxer
, subsession
);
150 } else if (strcmp(subsession
->codecName(), "MP4V-ES") == 0) {
151 bih
->biCompression
= sh_video
->format
152 = mmioFOURCC('m','p','4','v');
153 // For the codec to work correctly, it may need a 'VOL Header' to be
154 // inserted at the front of the data stream. Construct this from the
155 // "config" MIME parameter, which was present (hopefully) in the
156 // session's SDP description:
158 unsigned char* configData
159 = parseGeneralConfigStr(subsession
->fmtp_config(), configLen
);
160 sh_video
->bih
= bih
= insertVideoExtradata(bih
, configData
, configLen
);
161 needVideoFrameRate(demuxer
, subsession
);
162 } else if (strcmp(subsession
->codecName(), "X-QT") == 0 ||
163 strcmp(subsession
->codecName(), "X-QUICKTIME") == 0) {
164 // QuickTime generic RTP format, as described in
165 // http://developer.apple.com/quicktime/icefloe/dispatch026.html
167 // We can't initialize this stream until we've received the first packet
168 // that has QuickTime "sdAtom" information in the header. So, keep
169 // reading packets until we get one:
170 unsigned char* packetData
; unsigned packetDataLen
; float pts
;
171 QuickTimeGenericRTPSource
* qtRTPSource
172 = (QuickTimeGenericRTPSource
*)(subsession
->rtpSource());
175 if (!awaitRTPPacket(demuxer
, demuxer
->video
,
176 packetData
, packetDataLen
, pts
)) {
179 } while (!parseQTState_video(qtRTPSource
->qtState
, fourcc
));
181 bih
->biCompression
= sh_video
->format
= fourcc
;
182 bih
->biWidth
= qtRTPSource
->qtState
.width
;
183 bih
->biHeight
= qtRTPSource
->qtState
.height
;
184 uint8_t *pos
= (uint8_t*)qtRTPSource
->qtState
.sdAtom
+ 86;
185 uint8_t *endpos
= (uint8_t*)qtRTPSource
->qtState
.sdAtom
186 + qtRTPSource
->qtState
.sdAtomSize
;
187 while (pos
+8 < endpos
) {
188 unsigned atomLength
= pos
[0]<<24 | pos
[1]<<16 | pos
[2]<<8 | pos
[3];
189 if (atomLength
== 0 || atomLength
> endpos
-pos
) break;
190 if ((!memcmp(pos
+4, "avcC", 4) && fourcc
==mmioFOURCC('a','v','c','1') ||
191 !memcmp(pos
+4, "esds", 4) ||
192 !memcmp(pos
+4, "SMI ", 4) && fourcc
==mmioFOURCC('S','V','Q','3')) &&
194 sh_video
->bih
= bih
=
195 insertVideoExtradata(bih
, pos
+8, atomLength
-8);
200 needVideoFrameRate(demuxer
, subsession
);
203 "Unknown MPlayer format code for MIME type \"video/%s\"\n",
204 subsession
->codecName());
208 void rtpCodecInitialize_audio(demuxer_t
* demuxer
,
209 MediaSubsession
* subsession
,
212 // Create a dummy audio stream header
213 // to make the main MPlayer code happy:
214 sh_audio_t
* sh_audio
= new_sh_audio(demuxer
,0);
215 WAVEFORMATEX
* wf
= (WAVEFORMATEX
*)calloc(1,sizeof(WAVEFORMATEX
));
217 demux_stream_t
* d_audio
= demuxer
->audio
;
218 d_audio
->sh
= sh_audio
; sh_audio
->ds
= d_audio
;
219 d_audio
->id
= sh_audio
->aid
;
221 wf
->nChannels
= subsession
->numChannels();
223 // Map known audio MIME types to the WAVEFORMATEX parameters
224 // that this program uses. (Note that not all types need all
225 // of the parameters to be set.)
227 = subsession
->rtpSource()->timestampFrequency(); // by default
228 if (strcmp(subsession
->codecName(), "MPA") == 0 ||
229 strcmp(subsession
->codecName(), "MPA-ROBUST") == 0 ||
230 strcmp(subsession
->codecName(), "X-MP3-DRAFT-00") == 0) {
231 wf
->wFormatTag
= sh_audio
->format
= 0x55;
232 // Note: 0x55 is for layer III, but should work for I,II also
233 wf
->nSamplesPerSec
= 0; // sample rate is deduced from the data
234 } else if (strcmp(subsession
->codecName(), "AC3") == 0) {
235 wf
->wFormatTag
= sh_audio
->format
= 0x2000;
236 wf
->nSamplesPerSec
= 0; // sample rate is deduced from the data
237 } else if (strcmp(subsession
->codecName(), "L16") == 0) {
238 wf
->wFormatTag
= sh_audio
->format
= 0x736f7774; // "twos"
240 wf
->wBitsPerSample
= 16;
242 } else if (strcmp(subsession
->codecName(), "L8") == 0) {
243 wf
->wFormatTag
= sh_audio
->format
= 0x20776172; // "raw "
245 wf
->wBitsPerSample
= 8;
247 } else if (strcmp(subsession
->codecName(), "PCMU") == 0) {
248 wf
->wFormatTag
= sh_audio
->format
= 0x7;
249 wf
->nAvgBytesPerSec
= 8000;
251 wf
->wBitsPerSample
= 8;
253 } else if (strcmp(subsession
->codecName(), "PCMA") == 0) {
254 wf
->wFormatTag
= sh_audio
->format
= 0x6;
255 wf
->nAvgBytesPerSec
= 8000;
257 wf
->wBitsPerSample
= 8;
259 } else if (strcmp(subsession
->codecName(), "AMR") == 0) {
260 wf
->wFormatTag
= sh_audio
->format
= mmioFOURCC('s','a','m','r');
261 } else if (strcmp(subsession
->codecName(), "AMR-WB") == 0) {
262 wf
->wFormatTag
= sh_audio
->format
= mmioFOURCC('s','a','w','b');
263 } else if (strcmp(subsession
->codecName(), "GSM") == 0) {
264 wf
->wFormatTag
= sh_audio
->format
= mmioFOURCC('a','g','s','m');
265 wf
->nAvgBytesPerSec
= 1650;
266 wf
->nBlockAlign
= 33;
267 wf
->wBitsPerSample
= 16;
269 } else if (strcmp(subsession
->codecName(), "QCELP") == 0) {
270 wf
->wFormatTag
= sh_audio
->format
= mmioFOURCC('Q','c','l','p');
271 wf
->nAvgBytesPerSec
= 1750;
272 wf
->nBlockAlign
= 35;
273 wf
->wBitsPerSample
= 16;
275 } else if (strcmp(subsession
->codecName(), "MP4A-LATM") == 0) {
276 wf
->wFormatTag
= sh_audio
->format
= mmioFOURCC('m','p','4','a');
277 // For the codec to work correctly, it needs "AudioSpecificConfig"
278 // data, which is parsed from the "StreamMuxConfig" string that
279 // was present (hopefully) in the SDP description:
280 unsigned codecdata_len
;
282 = parseStreamMuxConfigStr(subsession
->fmtp_config(),
284 sh_audio
->codecdata_len
= codecdata_len
;
285 //faad doesn't understand LATM's data length field, so omit it
286 ((MPEG4LATMAudioRTPSource
*)subsession
->rtpSource())->omitLATMDataLengthField();
287 } else if (strcmp(subsession
->codecName(), "MPEG4-GENERIC") == 0) {
288 wf
->wFormatTag
= sh_audio
->format
= mmioFOURCC('m','p','4','a');
289 // For the codec to work correctly, it needs "AudioSpecificConfig"
290 // data, which was present (hopefully) in the SDP description:
291 unsigned codecdata_len
;
293 = parseGeneralConfigStr(subsession
->fmtp_config(),
295 sh_audio
->codecdata_len
= codecdata_len
;
296 } else if (strcmp(subsession
->codecName(), "X-QT") == 0 ||
297 strcmp(subsession
->codecName(), "X-QUICKTIME") == 0) {
298 // QuickTime generic RTP format, as described in
299 // http://developer.apple.com/quicktime/icefloe/dispatch026.html
301 // We can't initialize this stream until we've received the first packet
302 // that has QuickTime "sdAtom" information in the header. So, keep
303 // reading packets until we get one:
304 unsigned char* packetData
; unsigned packetDataLen
; float pts
;
305 QuickTimeGenericRTPSource
* qtRTPSource
306 = (QuickTimeGenericRTPSource
*)(subsession
->rtpSource());
307 unsigned fourcc
, numChannels
;
309 if (!awaitRTPPacket(demuxer
, demuxer
->audio
,
310 packetData
, packetDataLen
, pts
)) {
313 } while (!parseQTState_audio(qtRTPSource
->qtState
, fourcc
, numChannels
));
315 wf
->wFormatTag
= sh_audio
->format
= fourcc
;
316 wf
->nChannels
= numChannels
;
318 uint8_t *pos
= (uint8_t*)qtRTPSource
->qtState
.sdAtom
+ 52;
319 uint8_t *endpos
= (uint8_t*)qtRTPSource
->qtState
.sdAtom
320 + qtRTPSource
->qtState
.sdAtomSize
;
321 while (pos
+8 < endpos
) {
322 unsigned atomLength
= pos
[0]<<24 | pos
[1]<<16 | pos
[2]<<8 | pos
[3];
323 if (atomLength
== 0 || atomLength
> endpos
-pos
) break;
324 if (!memcmp(pos
+4, "wave", 4) && fourcc
==mmioFOURCC('Q','D','M','2') &&
326 atomLength
<= INT_MAX
) {
327 sh_audio
->codecdata
= (unsigned char*) malloc(atomLength
-8);
328 if (sh_audio
->codecdata
) {
329 memcpy(sh_audio
->codecdata
, pos
+8, atomLength
-8);
330 sh_audio
->codecdata_len
= atomLength
-8;
338 "Unknown MPlayer format code for MIME type \"audio/%s\"\n",
339 subsession
->codecName());
343 static void needVideoFrameRate(demuxer_t
* demuxer
,
344 MediaSubsession
* subsession
) {
345 // For some codecs, MPlayer's decoding software can't (or refuses to :-)
346 // figure out the frame rate by itself, so (unless the user specifies
347 // it manually, using "-fps") we figure it out ourselves here, using the
348 // presentation timestamps in successive packets,
349 extern double force_fps
; if (force_fps
!= 0.0) return; // user used "-fps"
351 demux_stream_t
* d_video
= demuxer
->video
;
352 sh_video_t
* sh_video
= (sh_video_t
*)(d_video
->sh
);
354 // If we already know the subsession's video frame rate, use it:
355 int fps
= (int)(subsession
->videoFPS());
358 sh_video
->frametime
= 1.0f
/fps
;
362 // Keep looking at incoming frames until we see two with different,
363 // non-zero "pts" timestamps:
364 unsigned char* packetData
; unsigned packetDataLen
;
365 float lastPTS
= 0.0, curPTS
;
366 unsigned const maxNumFramesToWaitFor
= 300;
368 for (unsigned i
= 0; i
< maxNumFramesToWaitFor
; ++i
) {
369 if (!awaitRTPPacket(demuxer
, d_video
, packetData
, packetDataLen
, curPTS
)) {
373 if (curPTS
!= lastPTS
&& lastPTS
!= 0.0) {
374 // Use the difference between these two "pts"s to guess the frame rate.
375 // (should really check that there were no missing frames inbetween)#####
376 // Guess the frame rate as an integer. If it's not, use "-fps" instead.
377 fps
= (int)(1/fabs(curPTS
-lastPTS
) + 0.5); // rounding
378 if (fps
== lastfps
) {
379 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
);
381 sh_video
->frametime
=1.0f
/fps
;
384 if (fps
>lastfps
) lastfps
= fps
;
388 fprintf(stderr
, "demux_rtp: Failed to guess the video frame rate\n");
392 parseQTState_video(QuickTimeGenericRTPSource::QTState
const& qtState
,
394 // qtState's "sdAtom" field is supposed to contain a QuickTime video
395 // 'sample description' atom. This atom's name is the 'fourcc' that we want:
396 char const* sdAtom
= qtState
.sdAtom
;
397 if (sdAtom
== NULL
|| qtState
.sdAtomSize
< 2*4) return False
;
399 fourcc
= *(unsigned*)(&sdAtom
[4]); // put in host order
404 parseQTState_audio(QuickTimeGenericRTPSource::QTState
const& qtState
,
405 unsigned& fourcc
, unsigned& numChannels
) {
406 // qtState's "sdAtom" field is supposed to contain a QuickTime audio
407 // 'sample description' atom. This atom's name is the 'fourcc' that we want.
408 // Also, the top half of the 5th word following the atom name should
409 // contain the number of channels ("numChannels") that we want:
410 char const* sdAtom
= qtState
.sdAtom
;
411 if (sdAtom
== NULL
|| qtState
.sdAtomSize
< 7*4) return False
;
413 fourcc
= *(unsigned*)(&sdAtom
[4]); // put in host order
415 char const* word7Ptr
= &sdAtom
[6*4];
416 numChannels
= (word7Ptr
[0]<<8)|(word7Ptr
[1]);