1 ////////// Codec-specific routines used to interface between "MPlayer"
2 ////////// and the "LIVE555 Streaming Media" libraries:
4 #include "demux_rtp_internal.h"
12 needVideoFrameRate(demuxer_t
* demuxer
, MediaSubsession
* subsession
); // forward
14 parseQTState_video(QuickTimeGenericRTPSource::QTState
const& qtState
,
15 unsigned& fourcc
); // forward
17 parseQTState_audio(QuickTimeGenericRTPSource::QTState
const& qtState
,
18 unsigned& fourcc
, unsigned& numChannels
); // forward
20 void rtpCodecInitialize_video(demuxer_t
* demuxer
,
21 MediaSubsession
* subsession
,
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);
28 = (BITMAPINFOHEADER
*)calloc(1,sizeof(BITMAPINFOHEADER
));
29 bih
->biSize
= sizeof(BITMAPINFOHEADER
);
31 demux_stream_t
* d_video
= demuxer
->video
;
32 d_video
->sh
= sh_video
; sh_video
->ds
= d_video
;
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.)
37 if (strcmp(subsession
->codecName(), "MPV") == 0) {
38 flags
|= RTPSTATE_IS_MPEG12_VIDEO
;
39 } else if (strcmp(subsession
->codecName(), "MP1S") == 0 ||
40 strcmp(subsession
->codecName(), "MP2T") == 0) {
41 flags
|= RTPSTATE_IS_MPEG12_VIDEO
|RTPSTATE_IS_MULTIPLEXED
;
42 } else if (strcmp(subsession
->codecName(), "H263") == 0 ||
43 strcmp(subsession
->codecName(), "H263-2000") == 0 ||
44 strcmp(subsession
->codecName(), "H263-1998") == 0) {
45 bih
->biCompression
= sh_video
->format
46 = mmioFOURCC('H','2','6','3');
47 needVideoFrameRate(demuxer
, subsession
);
48 } else if (strcmp(subsession
->codecName(), "H264") == 0) {
49 bih
->biCompression
= sh_video
->format
50 = mmioFOURCC('H','2','6','4');
51 needVideoFrameRate(demuxer
, subsession
);
52 } else if (strcmp(subsession
->codecName(), "H261") == 0) {
53 bih
->biCompression
= sh_video
->format
54 = mmioFOURCC('H','2','6','1');
55 needVideoFrameRate(demuxer
, subsession
);
56 } else if (strcmp(subsession
->codecName(), "JPEG") == 0) {
57 bih
->biCompression
= sh_video
->format
58 = mmioFOURCC('M','J','P','G');
59 needVideoFrameRate(demuxer
, subsession
);
60 } else if (strcmp(subsession
->codecName(), "MP4V-ES") == 0) {
61 bih
->biCompression
= sh_video
->format
62 = mmioFOURCC('m','p','4','v');
63 // For the codec to work correctly, it may need a 'VOL Header' to be
64 // inserted at the front of the data stream. Construct this from the
65 // "config" MIME parameter, which was present (hopefully) in the
66 // session's SDP description:
68 unsigned char* configData
69 = parseGeneralConfigStr(subsession
->fmtp_config(), configLen
);
70 insertRTPData(demuxer
, demuxer
->video
, configData
, configLen
);
71 needVideoFrameRate(demuxer
, subsession
);
72 } else if (strcmp(subsession
->codecName(), "X-QT") == 0 ||
73 strcmp(subsession
->codecName(), "X-QUICKTIME") == 0) {
74 // QuickTime generic RTP format, as described in
75 // http://developer.apple.com/quicktime/icefloe/dispatch026.html
77 // We can't initialize this stream until we've received the first packet
78 // that has QuickTime "sdAtom" information in the header. So, keep
79 // reading packets until we get one:
80 unsigned char* packetData
; unsigned packetDataLen
; float pts
;
81 QuickTimeGenericRTPSource
* qtRTPSource
82 = (QuickTimeGenericRTPSource
*)(subsession
->rtpSource());
85 if (!awaitRTPPacket(demuxer
, demuxer
->video
,
86 packetData
, packetDataLen
, pts
)) {
89 } while (!parseQTState_video(qtRTPSource
->qtState
, fourcc
));
91 bih
->biCompression
= sh_video
->format
= fourcc
;
92 bih
->biWidth
= qtRTPSource
->qtState
.width
;
93 bih
->biHeight
= qtRTPSource
->qtState
.height
;
94 if (bih
->biCompression
== mmioFOURCC('a','v','c','1') ||
95 bih
->biCompression
== mmioFOURCC('m','p','4','v') ||
96 bih
->biCompression
== mmioFOURCC('S','V','Q','3')) {
97 uint8_t *pos
= (uint8_t*)qtRTPSource
->qtState
.sdAtom
+ 86;
98 uint8_t *endpos
= (uint8_t*)qtRTPSource
->qtState
.sdAtom
99 + qtRTPSource
->qtState
.sdAtomSize
;
100 while (pos
+8 < endpos
) {
101 unsigned atomLength
= pos
[0]<<24 | pos
[1]<<16 | pos
[2]<<8 | pos
[3];
102 if (atomLength
== 0 || atomLength
> endpos
-pos
) break;
103 if ((!memcmp(pos
+4, "avcC", 4) ||
104 !memcmp(pos
+4, "esds", 4) ||
105 !memcmp(pos
+4, "SMI ", 4)) &&
107 atomLength
<= INT_MAX
-sizeof(BITMAPINFOHEADER
)) {
108 bih
->biSize
= sizeof(BITMAPINFOHEADER
)+atomLength
-8;
109 sh_video
->bih
= bih
= (BITMAPINFOHEADER
*)realloc(bih
, bih
->biSize
);
110 memcpy(bih
+1, pos
+8, atomLength
-8);
115 needVideoFrameRate(demuxer
, subsession
);
119 "Unknown MPlayer format code for MIME type \"video/%s\"\n",
120 subsession
->codecName());
124 void rtpCodecInitialize_audio(demuxer_t
* demuxer
,
125 MediaSubsession
* subsession
,
128 // Create a dummy audio stream header
129 // to make the main MPlayer code happy:
130 sh_audio_t
* sh_audio
= new_sh_audio(demuxer
,0);
131 WAVEFORMATEX
* wf
= (WAVEFORMATEX
*)calloc(1,sizeof(WAVEFORMATEX
));
133 demux_stream_t
* d_audio
= demuxer
->audio
;
134 d_audio
->sh
= sh_audio
; sh_audio
->ds
= d_audio
;
136 wf
->nChannels
= subsession
->numChannels();
138 // Map known audio MIME types to the WAVEFORMATEX parameters
139 // that this program uses. (Note that not all types need all
140 // of the parameters to be set.)
142 = subsession
->rtpSource()->timestampFrequency(); // by default
143 if (strcmp(subsession
->codecName(), "MPA") == 0 ||
144 strcmp(subsession
->codecName(), "MPA-ROBUST") == 0 ||
145 strcmp(subsession
->codecName(), "X-MP3-DRAFT-00") == 0) {
146 wf
->wFormatTag
= sh_audio
->format
= 0x55;
147 // Note: 0x55 is for layer III, but should work for I,II also
148 wf
->nSamplesPerSec
= 0; // sample rate is deduced from the data
149 } else if (strcmp(subsession
->codecName(), "AC3") == 0) {
150 wf
->wFormatTag
= sh_audio
->format
= 0x2000;
151 wf
->nSamplesPerSec
= 0; // sample rate is deduced from the data
152 } else if (strcmp(subsession
->codecName(), "L16") == 0) {
153 wf
->wFormatTag
= sh_audio
->format
= 0x736f7774; // "twos"
155 wf
->wBitsPerSample
= 16;
157 } else if (strcmp(subsession
->codecName(), "L8") == 0) {
158 wf
->wFormatTag
= sh_audio
->format
= 0x20776172; // "raw "
160 wf
->wBitsPerSample
= 8;
162 } else if (strcmp(subsession
->codecName(), "PCMU") == 0) {
163 wf
->wFormatTag
= sh_audio
->format
= 0x7;
164 wf
->nAvgBytesPerSec
= 8000;
166 wf
->wBitsPerSample
= 8;
168 } else if (strcmp(subsession
->codecName(), "PCMA") == 0) {
169 wf
->wFormatTag
= sh_audio
->format
= 0x6;
170 wf
->nAvgBytesPerSec
= 8000;
172 wf
->wBitsPerSample
= 8;
174 } else if (strcmp(subsession
->codecName(), "GSM") == 0) {
175 wf
->wFormatTag
= sh_audio
->format
= mmioFOURCC('a','g','s','m');
176 wf
->nAvgBytesPerSec
= 1650;
177 wf
->nBlockAlign
= 33;
178 wf
->wBitsPerSample
= 16;
180 } else if (strcmp(subsession
->codecName(), "QCELP") == 0) {
181 wf
->wFormatTag
= sh_audio
->format
= mmioFOURCC('Q','c','l','p');
182 wf
->nAvgBytesPerSec
= 1750;
183 wf
->nBlockAlign
= 35;
184 wf
->wBitsPerSample
= 16;
186 } else if (strcmp(subsession
->codecName(), "MP4A-LATM") == 0) {
187 wf
->wFormatTag
= sh_audio
->format
= mmioFOURCC('m','p','4','a');
188 // For the codec to work correctly, it needs "AudioSpecificConfig"
189 // data, which is parsed from the "StreamMuxConfig" string that
190 // was present (hopefully) in the SDP description:
191 unsigned codecdata_len
;
193 = parseStreamMuxConfigStr(subsession
->fmtp_config(),
195 sh_audio
->codecdata_len
= codecdata_len
;
196 //faad doesn't understand LATM's data length field, so omit it
197 ((MPEG4LATMAudioRTPSource
*)subsession
->rtpSource())->omitLATMDataLengthField();
198 } else if (strcmp(subsession
->codecName(), "MPEG4-GENERIC") == 0) {
199 wf
->wFormatTag
= sh_audio
->format
= mmioFOURCC('m','p','4','a');
200 // For the codec to work correctly, it needs "AudioSpecificConfig"
201 // data, which was present (hopefully) in the SDP description:
202 unsigned codecdata_len
;
204 = parseGeneralConfigStr(subsession
->fmtp_config(),
206 sh_audio
->codecdata_len
= codecdata_len
;
207 } else if (strcmp(subsession
->codecName(), "X-QT") == 0 ||
208 strcmp(subsession
->codecName(), "X-QUICKTIME") == 0) {
209 // QuickTime generic RTP format, as described in
210 // http://developer.apple.com/quicktime/icefloe/dispatch026.html
212 // We can't initialize this stream until we've received the first packet
213 // that has QuickTime "sdAtom" information in the header. So, keep
214 // reading packets until we get one:
215 unsigned char* packetData
; unsigned packetDataLen
; float pts
;
216 QuickTimeGenericRTPSource
* qtRTPSource
217 = (QuickTimeGenericRTPSource
*)(subsession
->rtpSource());
218 unsigned fourcc
, numChannels
;
220 if (!awaitRTPPacket(demuxer
, demuxer
->audio
,
221 packetData
, packetDataLen
, pts
)) {
224 } while (!parseQTState_audio(qtRTPSource
->qtState
, fourcc
, numChannels
));
226 wf
->wFormatTag
= sh_audio
->format
= fourcc
;
227 wf
->nChannels
= numChannels
;
230 "Unknown MPlayer format code for MIME type \"audio/%s\"\n",
231 subsession
->codecName());
235 static void needVideoFrameRate(demuxer_t
* demuxer
,
236 MediaSubsession
* subsession
) {
237 // For some codecs, MPlayer's decoding software can't (or refuses to :-)
238 // figure out the frame rate by itself, so (unless the user specifies
239 // it manually, using "-fps") we figure it out ourselves here, using the
240 // presentation timestamps in successive packets,
241 extern float force_fps
; if (force_fps
!= 0.0) return; // user used "-fps"
243 demux_stream_t
* d_video
= demuxer
->video
;
244 sh_video_t
* sh_video
= (sh_video_t
*)(d_video
->sh
);
246 // If we already know the subsession's video frame rate, use it:
247 int fps
= (int)(subsession
->videoFPS());
253 // Keep looking at incoming frames until we see two with different,
254 // non-zero "pts" timestamps:
255 unsigned char* packetData
; unsigned packetDataLen
;
256 float lastPTS
= 0.0, curPTS
;
257 unsigned const maxNumFramesToWaitFor
= 300;
259 for (unsigned i
= 0; i
< maxNumFramesToWaitFor
; ++i
) {
260 if (!awaitRTPPacket(demuxer
, d_video
, packetData
, packetDataLen
, curPTS
)) {
264 if (curPTS
!= lastPTS
&& lastPTS
!= 0.0) {
265 // Use the difference between these two "pts"s to guess the frame rate.
266 // (should really check that there were no missing frames inbetween)#####
267 // Guess the frame rate as an integer. If it's not, use "-fps" instead.
268 fps
= (int)(1/fabs(curPTS
-lastPTS
) + 0.5); // rounding
269 if (fps
== lastfps
) {
270 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
);
272 sh_video
->frametime
=1.0f
/fps
;
275 if (fps
>lastfps
) lastfps
= fps
;
279 fprintf(stderr
, "demux_rtp: Failed to guess the video frame rate\n");
283 parseQTState_video(QuickTimeGenericRTPSource::QTState
const& qtState
,
285 // qtState's "sdAtom" field is supposed to contain a QuickTime video
286 // 'sample description' atom. This atom's name is the 'fourcc' that we want:
287 char const* sdAtom
= qtState
.sdAtom
;
288 if (sdAtom
== NULL
|| qtState
.sdAtomSize
< 2*4) return False
;
290 fourcc
= *(unsigned*)(&sdAtom
[4]); // put in host order
295 parseQTState_audio(QuickTimeGenericRTPSource::QTState
const& qtState
,
296 unsigned& fourcc
, unsigned& numChannels
) {
297 // qtState's "sdAtom" field is supposed to contain a QuickTime audio
298 // 'sample description' atom. This atom's name is the 'fourcc' that we want.
299 // Also, the top half of the 5th word following the atom name should
300 // contain the number of channels ("numChannels") that we want:
301 char const* sdAtom
= qtState
.sdAtom
;
302 if (sdAtom
== NULL
|| qtState
.sdAtomSize
< 7*4) return False
;
304 fourcc
= *(unsigned*)(&sdAtom
[4]); // put in host order
306 char const* word7Ptr
= &sdAtom
[6*4];
307 numChannels
= (word7Ptr
[0]<<8)|(word7Ptr
[1]);