1 ////////// Codec-specific routines used to interface between "MPlayer"
2 ////////// and the "LIVE555 Streaming Media" libraries:
4 #include "demux_rtp_internal.h"
13 AVCodecParserContext
* h264parserctx
;
17 static unsigned char* parseH264ConfigStr( char const* configStr
,
18 unsigned int& configSize
)
26 if( configStr
== NULL
|| *configStr
== '\0' )
28 psz
= dup
= strdup( configStr
);
30 /* Count the number of comma's */
31 for( psz
= dup
; *psz
!= '\0'; ++psz
)
40 unsigned char *cfg
= new unsigned char[5 * strlen(dup
)];
42 for( i
= 0; i
< i_records
; i
++ )
45 cfg
[configSize
++] = 0x00;
46 cfg
[configSize
++] = 0x00;
47 cfg
[configSize
++] = 0x01;
48 configSize
+= av_base64_decode( (uint8_t*)&cfg
[configSize
],
50 5 * strlen(dup
) - 3 );
54 if( dup
) free( dup
);
60 needVideoFrameRate(demuxer_t
* demuxer
, MediaSubsession
* subsession
); // forward
62 parseQTState_video(QuickTimeGenericRTPSource::QTState
const& qtState
,
63 unsigned& fourcc
); // forward
65 parseQTState_audio(QuickTimeGenericRTPSource::QTState
const& qtState
,
66 unsigned& fourcc
, unsigned& numChannels
); // forward
68 static BITMAPINFOHEADER
* insertVideoExtradata(BITMAPINFOHEADER
*bih
,
69 unsigned char * extraData
,
72 BITMAPINFOHEADER
* original
= bih
;
73 if (!size
|| size
> INT_MAX
- sizeof(BITMAPINFOHEADER
))
75 bih
= (BITMAPINFOHEADER
*)realloc(bih
, sizeof(BITMAPINFOHEADER
) + size
);
78 bih
->biSize
= sizeof(BITMAPINFOHEADER
) + size
;
79 memcpy(bih
+1, extraData
, size
);
83 void rtpCodecInitialize_video(demuxer_t
* demuxer
,
84 MediaSubsession
* subsession
,
87 // Create a dummy video stream header
88 // to make the main MPlayer code happy:
89 sh_video_t
* sh_video
= new_sh_video(demuxer
,0);
91 = (BITMAPINFOHEADER
*)calloc(1,sizeof(BITMAPINFOHEADER
));
92 bih
->biSize
= sizeof(BITMAPINFOHEADER
);
94 demux_stream_t
* d_video
= demuxer
->video
;
95 d_video
->sh
= sh_video
; sh_video
->ds
= d_video
;
97 // Map known video MIME types to the BITMAPINFOHEADER parameters
98 // that this program uses. (Note that not all types need all
99 // of the parameters to be set.)
100 if (strcmp(subsession
->codecName(), "MPV") == 0) {
101 flags
|= RTPSTATE_IS_MPEG12_VIDEO
;
102 } else if (strcmp(subsession
->codecName(), "MP1S") == 0 ||
103 strcmp(subsession
->codecName(), "MP2T") == 0) {
104 flags
|= RTPSTATE_IS_MPEG12_VIDEO
|RTPSTATE_IS_MULTIPLEXED
;
105 } else if (strcmp(subsession
->codecName(), "H263") == 0 ||
106 strcmp(subsession
->codecName(), "H263-2000") == 0 ||
107 strcmp(subsession
->codecName(), "H263-1998") == 0) {
108 bih
->biCompression
= sh_video
->format
109 = mmioFOURCC('H','2','6','3');
110 needVideoFrameRate(demuxer
, subsession
);
111 } else if (strcmp(subsession
->codecName(), "H264") == 0) {
112 bih
->biCompression
= sh_video
->format
113 = mmioFOURCC('H','2','6','4');
114 unsigned int configLen
= 0;
115 unsigned char* configData
116 = parseH264ConfigStr(subsession
->fmtp_spropparametersets(), configLen
);
117 sh_video
->bih
= bih
= insertVideoExtradata(bih
, configData
, configLen
);
119 #ifdef USE_LIBAVCODEC
120 avcodec_register_all();
121 h264parserctx
= av_parser_init(CODEC_ID_H264
);
123 needVideoFrameRate(demuxer
, subsession
);
124 } else if (strcmp(subsession
->codecName(), "H261") == 0) {
125 bih
->biCompression
= sh_video
->format
126 = mmioFOURCC('H','2','6','1');
127 needVideoFrameRate(demuxer
, subsession
);
128 } else if (strcmp(subsession
->codecName(), "JPEG") == 0) {
129 bih
->biCompression
= sh_video
->format
130 = mmioFOURCC('M','J','P','G');
131 needVideoFrameRate(demuxer
, subsession
);
132 } else if (strcmp(subsession
->codecName(), "MP4V-ES") == 0) {
133 bih
->biCompression
= sh_video
->format
134 = mmioFOURCC('m','p','4','v');
135 // For the codec to work correctly, it may need a 'VOL Header' to be
136 // inserted at the front of the data stream. Construct this from the
137 // "config" MIME parameter, which was present (hopefully) in the
138 // session's SDP description:
140 unsigned char* configData
141 = parseGeneralConfigStr(subsession
->fmtp_config(), configLen
);
142 sh_video
->bih
= bih
= insertVideoExtradata(bih
, configData
, configLen
);
143 needVideoFrameRate(demuxer
, subsession
);
144 } else if (strcmp(subsession
->codecName(), "X-QT") == 0 ||
145 strcmp(subsession
->codecName(), "X-QUICKTIME") == 0) {
146 // QuickTime generic RTP format, as described in
147 // http://developer.apple.com/quicktime/icefloe/dispatch026.html
149 // We can't initialize this stream until we've received the first packet
150 // that has QuickTime "sdAtom" information in the header. So, keep
151 // reading packets until we get one:
152 unsigned char* packetData
; unsigned packetDataLen
; float pts
;
153 QuickTimeGenericRTPSource
* qtRTPSource
154 = (QuickTimeGenericRTPSource
*)(subsession
->rtpSource());
157 if (!awaitRTPPacket(demuxer
, demuxer
->video
,
158 packetData
, packetDataLen
, pts
)) {
161 } while (!parseQTState_video(qtRTPSource
->qtState
, fourcc
));
163 bih
->biCompression
= sh_video
->format
= fourcc
;
164 bih
->biWidth
= qtRTPSource
->qtState
.width
;
165 bih
->biHeight
= qtRTPSource
->qtState
.height
;
166 uint8_t *pos
= (uint8_t*)qtRTPSource
->qtState
.sdAtom
+ 86;
167 uint8_t *endpos
= (uint8_t*)qtRTPSource
->qtState
.sdAtom
168 + qtRTPSource
->qtState
.sdAtomSize
;
169 while (pos
+8 < endpos
) {
170 unsigned atomLength
= pos
[0]<<24 | pos
[1]<<16 | pos
[2]<<8 | pos
[3];
171 if (atomLength
== 0 || atomLength
> endpos
-pos
) break;
172 if ((!memcmp(pos
+4, "avcC", 4) && fourcc
==mmioFOURCC('a','v','c','1') ||
173 !memcmp(pos
+4, "esds", 4) ||
174 !memcmp(pos
+4, "SMI ", 4) && fourcc
==mmioFOURCC('S','V','Q','3')) &&
176 sh_video
->bih
= bih
=
177 insertVideoExtradata(bih
, pos
+8, atomLength
-8);
182 needVideoFrameRate(demuxer
, subsession
);
185 "Unknown MPlayer format code for MIME type \"video/%s\"\n",
186 subsession
->codecName());
190 void rtpCodecInitialize_audio(demuxer_t
* demuxer
,
191 MediaSubsession
* subsession
,
194 // Create a dummy audio stream header
195 // to make the main MPlayer code happy:
196 sh_audio_t
* sh_audio
= new_sh_audio(demuxer
,0);
197 WAVEFORMATEX
* wf
= (WAVEFORMATEX
*)calloc(1,sizeof(WAVEFORMATEX
));
199 demux_stream_t
* d_audio
= demuxer
->audio
;
200 d_audio
->sh
= sh_audio
; sh_audio
->ds
= d_audio
;
202 wf
->nChannels
= subsession
->numChannels();
204 // Map known audio MIME types to the WAVEFORMATEX parameters
205 // that this program uses. (Note that not all types need all
206 // of the parameters to be set.)
208 = subsession
->rtpSource()->timestampFrequency(); // by default
209 if (strcmp(subsession
->codecName(), "MPA") == 0 ||
210 strcmp(subsession
->codecName(), "MPA-ROBUST") == 0 ||
211 strcmp(subsession
->codecName(), "X-MP3-DRAFT-00") == 0) {
212 wf
->wFormatTag
= sh_audio
->format
= 0x55;
213 // Note: 0x55 is for layer III, but should work for I,II also
214 wf
->nSamplesPerSec
= 0; // sample rate is deduced from the data
215 } else if (strcmp(subsession
->codecName(), "AC3") == 0) {
216 wf
->wFormatTag
= sh_audio
->format
= 0x2000;
217 wf
->nSamplesPerSec
= 0; // sample rate is deduced from the data
218 } else if (strcmp(subsession
->codecName(), "L16") == 0) {
219 wf
->wFormatTag
= sh_audio
->format
= 0x736f7774; // "twos"
221 wf
->wBitsPerSample
= 16;
223 } else if (strcmp(subsession
->codecName(), "L8") == 0) {
224 wf
->wFormatTag
= sh_audio
->format
= 0x20776172; // "raw "
226 wf
->wBitsPerSample
= 8;
228 } else if (strcmp(subsession
->codecName(), "PCMU") == 0) {
229 wf
->wFormatTag
= sh_audio
->format
= 0x7;
230 wf
->nAvgBytesPerSec
= 8000;
232 wf
->wBitsPerSample
= 8;
234 } else if (strcmp(subsession
->codecName(), "PCMA") == 0) {
235 wf
->wFormatTag
= sh_audio
->format
= 0x6;
236 wf
->nAvgBytesPerSec
= 8000;
238 wf
->wBitsPerSample
= 8;
240 } else if (strcmp(subsession
->codecName(), "AMR") == 0) {
241 wf
->wFormatTag
= sh_audio
->format
= mmioFOURCC('s','a','m','r');
242 } else if (strcmp(subsession
->codecName(), "AMR-WB") == 0) {
243 wf
->wFormatTag
= sh_audio
->format
= mmioFOURCC('s','a','w','b');
244 } else if (strcmp(subsession
->codecName(), "GSM") == 0) {
245 wf
->wFormatTag
= sh_audio
->format
= mmioFOURCC('a','g','s','m');
246 wf
->nAvgBytesPerSec
= 1650;
247 wf
->nBlockAlign
= 33;
248 wf
->wBitsPerSample
= 16;
250 } else if (strcmp(subsession
->codecName(), "QCELP") == 0) {
251 wf
->wFormatTag
= sh_audio
->format
= mmioFOURCC('Q','c','l','p');
252 wf
->nAvgBytesPerSec
= 1750;
253 wf
->nBlockAlign
= 35;
254 wf
->wBitsPerSample
= 16;
256 } else if (strcmp(subsession
->codecName(), "MP4A-LATM") == 0) {
257 wf
->wFormatTag
= sh_audio
->format
= mmioFOURCC('m','p','4','a');
258 // For the codec to work correctly, it needs "AudioSpecificConfig"
259 // data, which is parsed from the "StreamMuxConfig" string that
260 // was present (hopefully) in the SDP description:
261 unsigned codecdata_len
;
263 = parseStreamMuxConfigStr(subsession
->fmtp_config(),
265 sh_audio
->codecdata_len
= codecdata_len
;
266 //faad doesn't understand LATM's data length field, so omit it
267 ((MPEG4LATMAudioRTPSource
*)subsession
->rtpSource())->omitLATMDataLengthField();
268 } else if (strcmp(subsession
->codecName(), "MPEG4-GENERIC") == 0) {
269 wf
->wFormatTag
= sh_audio
->format
= mmioFOURCC('m','p','4','a');
270 // For the codec to work correctly, it needs "AudioSpecificConfig"
271 // data, which was present (hopefully) in the SDP description:
272 unsigned codecdata_len
;
274 = parseGeneralConfigStr(subsession
->fmtp_config(),
276 sh_audio
->codecdata_len
= codecdata_len
;
277 } else if (strcmp(subsession
->codecName(), "X-QT") == 0 ||
278 strcmp(subsession
->codecName(), "X-QUICKTIME") == 0) {
279 // QuickTime generic RTP format, as described in
280 // http://developer.apple.com/quicktime/icefloe/dispatch026.html
282 // We can't initialize this stream until we've received the first packet
283 // that has QuickTime "sdAtom" information in the header. So, keep
284 // reading packets until we get one:
285 unsigned char* packetData
; unsigned packetDataLen
; float pts
;
286 QuickTimeGenericRTPSource
* qtRTPSource
287 = (QuickTimeGenericRTPSource
*)(subsession
->rtpSource());
288 unsigned fourcc
, numChannels
;
290 if (!awaitRTPPacket(demuxer
, demuxer
->audio
,
291 packetData
, packetDataLen
, pts
)) {
294 } while (!parseQTState_audio(qtRTPSource
->qtState
, fourcc
, numChannels
));
296 wf
->wFormatTag
= sh_audio
->format
= fourcc
;
297 wf
->nChannels
= numChannels
;
299 uint8_t *pos
= (uint8_t*)qtRTPSource
->qtState
.sdAtom
+ 52;
300 uint8_t *endpos
= (uint8_t*)qtRTPSource
->qtState
.sdAtom
301 + qtRTPSource
->qtState
.sdAtomSize
;
302 while (pos
+8 < endpos
) {
303 unsigned atomLength
= pos
[0]<<24 | pos
[1]<<16 | pos
[2]<<8 | pos
[3];
304 if (atomLength
== 0 || atomLength
> endpos
-pos
) break;
305 if (!memcmp(pos
+4, "wave", 4) && fourcc
==mmioFOURCC('Q','D','M','2') &&
307 atomLength
<= INT_MAX
) {
308 sh_audio
->codecdata
= (unsigned char*) malloc(atomLength
-8);
309 if (sh_audio
->codecdata
) {
310 memcpy(sh_audio
->codecdata
, pos
+8, atomLength
-8);
311 sh_audio
->codecdata_len
= atomLength
-8;
319 "Unknown MPlayer format code for MIME type \"audio/%s\"\n",
320 subsession
->codecName());
324 static void needVideoFrameRate(demuxer_t
* demuxer
,
325 MediaSubsession
* subsession
) {
326 // For some codecs, MPlayer's decoding software can't (or refuses to :-)
327 // figure out the frame rate by itself, so (unless the user specifies
328 // it manually, using "-fps") we figure it out ourselves here, using the
329 // presentation timestamps in successive packets,
330 extern float force_fps
; if (force_fps
!= 0.0) return; // user used "-fps"
332 demux_stream_t
* d_video
= demuxer
->video
;
333 sh_video_t
* sh_video
= (sh_video_t
*)(d_video
->sh
);
335 // If we already know the subsession's video frame rate, use it:
336 int fps
= (int)(subsession
->videoFPS());
339 sh_video
->frametime
= 1.0f
/fps
;
343 // Keep looking at incoming frames until we see two with different,
344 // non-zero "pts" timestamps:
345 unsigned char* packetData
; unsigned packetDataLen
;
346 float lastPTS
= 0.0, curPTS
;
347 unsigned const maxNumFramesToWaitFor
= 300;
349 for (unsigned i
= 0; i
< maxNumFramesToWaitFor
; ++i
) {
350 if (!awaitRTPPacket(demuxer
, d_video
, packetData
, packetDataLen
, curPTS
)) {
354 if (curPTS
!= lastPTS
&& lastPTS
!= 0.0) {
355 // Use the difference between these two "pts"s to guess the frame rate.
356 // (should really check that there were no missing frames inbetween)#####
357 // Guess the frame rate as an integer. If it's not, use "-fps" instead.
358 fps
= (int)(1/fabs(curPTS
-lastPTS
) + 0.5); // rounding
359 if (fps
== lastfps
) {
360 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
);
362 sh_video
->frametime
=1.0f
/fps
;
365 if (fps
>lastfps
) lastfps
= fps
;
369 fprintf(stderr
, "demux_rtp: Failed to guess the video frame rate\n");
373 parseQTState_video(QuickTimeGenericRTPSource::QTState
const& qtState
,
375 // qtState's "sdAtom" field is supposed to contain a QuickTime video
376 // 'sample description' atom. This atom's name is the 'fourcc' that we want:
377 char const* sdAtom
= qtState
.sdAtom
;
378 if (sdAtom
== NULL
|| qtState
.sdAtomSize
< 2*4) return False
;
380 fourcc
= *(unsigned*)(&sdAtom
[4]); // put in host order
385 parseQTState_audio(QuickTimeGenericRTPSource::QTState
const& qtState
,
386 unsigned& fourcc
, unsigned& numChannels
) {
387 // qtState's "sdAtom" field is supposed to contain a QuickTime audio
388 // 'sample description' atom. This atom's name is the 'fourcc' that we want.
389 // Also, the top half of the 5th word following the atom name should
390 // contain the number of channels ("numChannels") that we want:
391 char const* sdAtom
= qtState
.sdAtom
;
392 if (sdAtom
== NULL
|| qtState
.sdAtomSize
< 7*4) return False
;
394 fourcc
= *(unsigned*)(&sdAtom
[4]); // put in host order
396 char const* word7Ptr
= &sdAtom
[6*4];
397 numChannels
= (word7Ptr
[0]<<8)|(word7Ptr
[1]);