Mercurial > mplayer.hg
annotate libmpdemux/demux_rtp_codec.cpp @ 10447:3eab06376d24
added a faq entry about encoding ASF files
Please check it and correct me if something is not right
author | attila |
---|---|
date | Sun, 20 Jul 2003 13:51:04 +0000 |
parents | 41ed68e3a034 |
children | b0d0aa726417 |
rev | line source |
---|---|
9250 | 1 ////////// Codec-specific routines used to interface between "MPlayer" |
2 ////////// and the "LIVE.COM Streaming Media" libraries: | |
3 | |
4 #include "demux_rtp_internal.h" | |
5 extern "C" { | |
6 #include "stheader.h" | |
7 } | |
8 | |
9565
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
9 static void |
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
10 needVideoFrameRate(demuxer_t* demuxer, MediaSubsession* subsession); // forward |
9250 | 11 static Boolean |
12 parseQTState_video(QuickTimeGenericRTPSource::QTState const& qtState, | |
13 unsigned& fourcc); // forward | |
14 static Boolean | |
15 parseQTState_audio(QuickTimeGenericRTPSource::QTState const& qtState, | |
16 unsigned& fourcc, unsigned& numChannels); // forward | |
17 | |
18 void rtpCodecInitialize_video(demuxer_t* demuxer, | |
19 MediaSubsession* subsession, | |
20 unsigned& flags) { | |
21 flags = 0; | |
22 // Create a dummy video stream header | |
23 // to make the main MPlayer code happy: | |
24 sh_video_t* sh_video = new_sh_video(demuxer,0); | |
25 BITMAPINFOHEADER* bih | |
26 = (BITMAPINFOHEADER*)calloc(1,sizeof(BITMAPINFOHEADER)); | |
27 bih->biSize = sizeof(BITMAPINFOHEADER); | |
28 sh_video->bih = bih; | |
29 demux_stream_t* d_video = demuxer->video; | |
30 d_video->sh = sh_video; sh_video->ds = d_video; | |
31 | |
32 // Map known video MIME types to the BITMAPINFOHEADER parameters | |
33 // that this program uses. (Note that not all types need all | |
34 // of the parameters to be set.) | |
35 if (strcmp(subsession->codecName(), "MPV") == 0 || | |
36 strcmp(subsession->codecName(), "MP1S") == 0 || | |
37 strcmp(subsession->codecName(), "MP2T") == 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; |
9250 | 39 } else if (strcmp(subsession->codecName(), "H263") == 0 || |
40 strcmp(subsession->codecName(), "H263-1998") == 0) { | |
41 bih->biCompression = sh_video->format | |
42 = mmioFOURCC('H','2','6','3'); | |
9565
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
43 needVideoFrameRate(demuxer, subsession); |
9250 | 44 } else if (strcmp(subsession->codecName(), "H261") == 0) { |
45 bih->biCompression = sh_video->format | |
46 = mmioFOURCC('H','2','6','1'); | |
9565
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
47 needVideoFrameRate(demuxer, subsession); |
9370
88bd19564b64
Motion-JPEG RTP streams can now be played. Some MPEG-4 ES video RTP
arpi
parents:
9250
diff
changeset
|
48 } 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
|
49 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
|
50 = mmioFOURCC('M','J','P','G'); |
9565
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
51 needVideoFrameRate(demuxer, subsession); |
9370
88bd19564b64
Motion-JPEG RTP streams can now be played. Some MPEG-4 ES video RTP
arpi
parents:
9250
diff
changeset
|
52 } 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
|
53 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
|
54 = mmioFOURCC('m','p','4','v'); |
9565
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
55 // 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
|
56 // 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
|
57 // "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
|
58 // session's SDP description: |
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
59 unsigned configLen; |
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
60 unsigned char* configData |
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
61 = parseGeneralConfigStr(subsession->fmtp_config(), configLen); |
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
62 insertRTPData(demuxer, demuxer->video, configData, configLen); |
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
63 needVideoFrameRate(demuxer, subsession); |
9250 | 64 } else if (strcmp(subsession->codecName(), "X-QT") == 0 || |
65 strcmp(subsession->codecName(), "X-QUICKTIME") == 0) { | |
66 // QuickTime generic RTP format, as described in | |
67 // http://developer.apple.com/quicktime/icefloe/dispatch026.html | |
68 | |
69 // We can't initialize this stream until we've received the first packet | |
70 // that has QuickTime "sdAtom" information in the header. So, keep | |
71 // 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
|
72 unsigned char* packetData; unsigned packetDataLen; float pts; |
9250 | 73 QuickTimeGenericRTPSource* qtRTPSource |
74 = (QuickTimeGenericRTPSource*)(subsession->rtpSource()); | |
75 unsigned fourcc; | |
76 do { | |
9565
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
77 if (!awaitRTPPacket(demuxer, demuxer->video, |
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
78 packetData, packetDataLen, pts)) { |
9250 | 79 return; |
80 } | |
81 } while (!parseQTState_video(qtRTPSource->qtState, fourcc)); | |
82 | |
83 bih->biCompression = sh_video->format = fourcc; | |
84 } else { | |
85 fprintf(stderr, | |
86 "Unknown MPlayer format code for MIME type \"video/%s\"\n", | |
87 subsession->codecName()); | |
88 } | |
89 } | |
90 | |
91 void rtpCodecInitialize_audio(demuxer_t* demuxer, | |
92 MediaSubsession* subsession, | |
93 unsigned& flags) { | |
94 flags = 0; | |
95 // Create a dummy audio stream header | |
96 // to make the main MPlayer code happy: | |
97 sh_audio_t* sh_audio = new_sh_audio(demuxer,0); | |
98 WAVEFORMATEX* wf = (WAVEFORMATEX*)calloc(1,sizeof(WAVEFORMATEX)); | |
99 sh_audio->wf = wf; | |
100 demux_stream_t* d_audio = demuxer->audio; | |
101 d_audio->sh = sh_audio; sh_audio->ds = d_audio; | |
102 | |
9565
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
103 wf->nChannels = subsession->numChannels(); |
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
104 |
9250 | 105 // Map known audio MIME types to the WAVEFORMATEX parameters |
106 // that this program uses. (Note that not all types need all | |
107 // of the parameters to be set.) | |
108 wf->nSamplesPerSec | |
109 = subsession->rtpSource()->timestampFrequency(); // by default | |
110 if (strcmp(subsession->codecName(), "MPA") == 0 || | |
111 strcmp(subsession->codecName(), "MPA-ROBUST") == 0 || | |
112 strcmp(subsession->codecName(), "X-MP3-DRAFT-00") == 0) { | |
113 wf->wFormatTag = sh_audio->format = 0x55; | |
114 // Note: 0x55 is for layer III, but should work for I,II also | |
115 wf->nSamplesPerSec = 0; // sample rate is deduced from the data | |
116 } else if (strcmp(subsession->codecName(), "AC3") == 0) { | |
117 wf->wFormatTag = sh_audio->format = 0x2000; | |
118 wf->nSamplesPerSec = 0; // sample rate is deduced from the data | |
119 } else if (strcmp(subsession->codecName(), "PCMU") == 0) { | |
120 wf->wFormatTag = sh_audio->format = 0x7; | |
121 wf->nAvgBytesPerSec = 8000; | |
122 wf->nBlockAlign = 1; | |
123 wf->wBitsPerSample = 8; | |
124 wf->cbSize = 0; | |
125 } else if (strcmp(subsession->codecName(), "PCMA") == 0) { | |
126 wf->wFormatTag = sh_audio->format = 0x6; | |
127 wf->nAvgBytesPerSec = 8000; | |
128 wf->nBlockAlign = 1; | |
129 wf->wBitsPerSample = 8; | |
130 wf->cbSize = 0; | |
131 } else if (strcmp(subsession->codecName(), "GSM") == 0) { | |
132 wf->wFormatTag = sh_audio->format = mmioFOURCC('a','g','s','m'); | |
133 wf->nAvgBytesPerSec = 1650; | |
134 wf->nBlockAlign = 33; | |
135 wf->wBitsPerSample = 16; | |
136 wf->cbSize = 0; | |
137 } else if (strcmp(subsession->codecName(), "QCELP") == 0) { | |
138 wf->wFormatTag = sh_audio->format = mmioFOURCC('Q','c','l','p'); | |
139 wf->nAvgBytesPerSec = 1750; | |
140 wf->nBlockAlign = 35; | |
141 wf->wBitsPerSample = 16; | |
142 wf->cbSize = 0; | |
143 } else if (strcmp(subsession->codecName(), "MP4A-LATM") == 0) { | |
144 wf->wFormatTag = sh_audio->format = mmioFOURCC('m','p','4','a'); | |
145 // For the codec to work correctly, it needs "AudioSpecificConfig" | |
146 // data, which is parsed from the "StreamMuxConfig" string that | |
147 // was present (hopefully) in the SDP description: | |
148 unsigned codecdata_len; | |
149 sh_audio->codecdata | |
150 = parseStreamMuxConfigStr(subsession->fmtp_config(), | |
151 codecdata_len); | |
152 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
|
153 } 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
|
154 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
|
155 // 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
|
156 // 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
|
157 unsigned codecdata_len; |
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
158 sh_audio->codecdata |
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
159 = parseGeneralConfigStr(subsession->fmtp_config(), |
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
160 codecdata_len); |
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
161 sh_audio->codecdata_len = codecdata_len; |
9250 | 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 | |
166 | |
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: | |
9565
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
170 unsigned char* packetData; unsigned packetDataLen; float pts; |
9250 | 171 QuickTimeGenericRTPSource* qtRTPSource |
172 = (QuickTimeGenericRTPSource*)(subsession->rtpSource()); | |
173 unsigned fourcc, numChannels; | |
174 do { | |
9565
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
175 if (!awaitRTPPacket(demuxer, demuxer->audio, |
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
176 packetData, packetDataLen, pts)) { |
9250 | 177 return; |
178 } | |
179 } while (!parseQTState_audio(qtRTPSource->qtState, fourcc, numChannels)); | |
180 | |
181 wf->wFormatTag = sh_audio->format = fourcc; | |
182 wf->nChannels = numChannels; | |
183 } else { | |
184 fprintf(stderr, | |
185 "Unknown MPlayer format code for MIME type \"audio/%s\"\n", | |
186 subsession->codecName()); | |
187 } | |
188 } | |
189 | |
9565
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
190 static void needVideoFrameRate(demuxer_t* demuxer, |
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
191 MediaSubsession* subsession) { |
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
192 // 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
|
193 // 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
|
194 // 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
|
195 // presentation timestamps in successive packets, |
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
196 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
|
197 |
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
198 demux_stream_t* d_video = demuxer->video; |
9910 | 199 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
|
200 |
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
201 // 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
|
202 int fps = (int)(subsession->videoFPS()); |
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
203 if (fps != 0) { |
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
204 sh_video->fps = fps; |
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
205 return; |
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
206 } |
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
207 |
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
208 // 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
|
209 // non-zero "pts" timestamps: |
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
210 unsigned char* packetData; unsigned packetDataLen; |
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
211 float lastPTS = 0.0, curPTS; |
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
212 unsigned const maxNumFramesToWaitFor = 100; |
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
213 for (unsigned i = 0; i < maxNumFramesToWaitFor; ++i) { |
9910 | 214 if (!awaitRTPPacket(demuxer, d_video, packetData, packetDataLen, curPTS)) { |
215 break; | |
216 } | |
9565
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
217 |
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
218 if (curPTS > lastPTS && lastPTS != 0.0) { |
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
219 // 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
|
220 // (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
|
221 // Guess the frame rate as an integer. If it's not, use "-fps" instead. |
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
222 fps = (int)(1/(curPTS-lastPTS) + 0.5); // rounding |
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
223 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
|
224 sh_video->fps = fps; |
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
225 return; |
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
226 } |
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
227 lastPTS = curPTS; |
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
228 } |
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
229 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
|
230 } |
e74916774667
Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents:
9370
diff
changeset
|
231 |
9250 | 232 static Boolean |
233 parseQTState_video(QuickTimeGenericRTPSource::QTState const& qtState, | |
234 unsigned& fourcc) { | |
235 // qtState's "sdAtom" field is supposed to contain a QuickTime video | |
236 // 'sample description' atom. This atom's name is the 'fourcc' that we want: | |
237 char const* sdAtom = qtState.sdAtom; | |
238 if (sdAtom == NULL || qtState.sdAtomSize < 2*4) return False; | |
239 | |
240 fourcc = *(unsigned*)(&sdAtom[4]); // put in host order | |
241 return True; | |
242 } | |
243 | |
244 static Boolean | |
245 parseQTState_audio(QuickTimeGenericRTPSource::QTState const& qtState, | |
246 unsigned& fourcc, unsigned& numChannels) { | |
247 // qtState's "sdAtom" field is supposed to contain a QuickTime audio | |
248 // 'sample description' atom. This atom's name is the 'fourcc' that we want. | |
249 // Also, the top half of the 5th word following the atom name should | |
250 // contain the number of channels ("numChannels") that we want: | |
251 char const* sdAtom = qtState.sdAtom; | |
252 if (sdAtom == NULL || qtState.sdAtomSize < 7*4) return False; | |
253 | |
254 fourcc = *(unsigned*)(&sdAtom[4]); // put in host order | |
255 | |
256 char const* word7Ptr = &sdAtom[6*4]; | |
257 numChannels = (word7Ptr[0]<<8)|(word7Ptr[1]); | |
258 return True; | |
259 } |