comparison libmpdemux/demux_rtp_codec.cpp @ 9250:bb490ffeebf5

Restruct by Ross Finlayson <finlayson@live.com> The code now supports 'QuickTime generic' RTP streams (the "X-QT" MIME type), which - thanks to the QuickTime codecs - makes it possible to play more QuickTime RTP streams.
author bertrand
date Mon, 03 Feb 2003 10:27:50 +0000
parents
children 88bd19564b64
comparison
equal deleted inserted replaced
9249:8546faeb0112 9250:bb490ffeebf5
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
9 static Boolean
10 parseQTState_video(QuickTimeGenericRTPSource::QTState const& qtState,
11 unsigned& fourcc); // forward
12 static Boolean
13 parseQTState_audio(QuickTimeGenericRTPSource::QTState const& qtState,
14 unsigned& fourcc, unsigned& numChannels); // forward
15
16 void rtpCodecInitialize_video(demuxer_t* demuxer,
17 MediaSubsession* subsession,
18 unsigned& flags) {
19 flags = 0;
20 // Create a dummy video stream header
21 // to make the main MPlayer code happy:
22 sh_video_t* sh_video = new_sh_video(demuxer,0);
23 BITMAPINFOHEADER* bih
24 = (BITMAPINFOHEADER*)calloc(1,sizeof(BITMAPINFOHEADER));
25 bih->biSize = sizeof(BITMAPINFOHEADER);
26 sh_video->bih = bih;
27 demux_stream_t* d_video = demuxer->video;
28 d_video->sh = sh_video; sh_video->ds = d_video;
29
30 // If we happen to know the subsession's video frame rate, set it,
31 // so that the user doesn't have to give the "-fps" option instead.
32 int fps = (int)(subsession->videoFPS());
33 if (fps != 0) sh_video->fps = fps;
34
35 // Map known video MIME types to the BITMAPINFOHEADER parameters
36 // that this program uses. (Note that not all types need all
37 // of the parameters to be set.)
38 if (strcmp(subsession->codecName(), "MPV") == 0 ||
39 strcmp(subsession->codecName(), "MP1S") == 0 ||
40 strcmp(subsession->codecName(), "MP2T") == 0) {
41 flags |= RTPSTATE_IS_MPEG;
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');
46 } else if (strcmp(subsession->codecName(), "H261") == 0) {
47 bih->biCompression = sh_video->format
48 = mmioFOURCC('H','2','6','1');
49 } else if (strcmp(subsession->codecName(), "X-QT") == 0 ||
50 strcmp(subsession->codecName(), "X-QUICKTIME") == 0) {
51 // QuickTime generic RTP format, as described in
52 // http://developer.apple.com/quicktime/icefloe/dispatch026.html
53
54 // We can't initialize this stream until we've received the first packet
55 // that has QuickTime "sdAtom" information in the header. So, keep
56 // reading packets until we get one:
57 unsigned char* packetData; unsigned packetDataLen;
58 QuickTimeGenericRTPSource* qtRTPSource
59 = (QuickTimeGenericRTPSource*)(subsession->rtpSource());
60 unsigned fourcc;
61 do {
62 if (!awaitRTPPacket(demuxer, 0 /*video*/, packetData, packetDataLen)) {
63 return;
64 }
65 } while (!parseQTState_video(qtRTPSource->qtState, fourcc));
66
67 bih->biCompression = sh_video->format = fourcc;
68 } else {
69 fprintf(stderr,
70 "Unknown MPlayer format code for MIME type \"video/%s\"\n",
71 subsession->codecName());
72 }
73 }
74
75 void rtpCodecInitialize_audio(demuxer_t* demuxer,
76 MediaSubsession* subsession,
77 unsigned& flags) {
78 flags = 0;
79 // Create a dummy audio stream header
80 // to make the main MPlayer code happy:
81 sh_audio_t* sh_audio = new_sh_audio(demuxer,0);
82 WAVEFORMATEX* wf = (WAVEFORMATEX*)calloc(1,sizeof(WAVEFORMATEX));
83 sh_audio->wf = wf;
84 demux_stream_t* d_audio = demuxer->audio;
85 d_audio->sh = sh_audio; sh_audio->ds = d_audio;
86
87 // Map known audio MIME types to the WAVEFORMATEX parameters
88 // that this program uses. (Note that not all types need all
89 // of the parameters to be set.)
90 wf->nSamplesPerSec
91 = subsession->rtpSource()->timestampFrequency(); // by default
92 if (strcmp(subsession->codecName(), "MPA") == 0 ||
93 strcmp(subsession->codecName(), "MPA-ROBUST") == 0 ||
94 strcmp(subsession->codecName(), "X-MP3-DRAFT-00") == 0) {
95 wf->wFormatTag = sh_audio->format = 0x55;
96 // Note: 0x55 is for layer III, but should work for I,II also
97 wf->nSamplesPerSec = 0; // sample rate is deduced from the data
98 flags |= RTPSTATE_IS_MPEG;
99 } else if (strcmp(subsession->codecName(), "AC3") == 0) {
100 wf->wFormatTag = sh_audio->format = 0x2000;
101 wf->nSamplesPerSec = 0; // sample rate is deduced from the data
102 } else if (strcmp(subsession->codecName(), "PCMU") == 0) {
103 wf->wFormatTag = sh_audio->format = 0x7;
104 wf->nChannels = 1;
105 wf->nAvgBytesPerSec = 8000;
106 wf->nBlockAlign = 1;
107 wf->wBitsPerSample = 8;
108 wf->cbSize = 0;
109 } else if (strcmp(subsession->codecName(), "PCMA") == 0) {
110 wf->wFormatTag = sh_audio->format = 0x6;
111 wf->nChannels = 1;
112 wf->nAvgBytesPerSec = 8000;
113 wf->nBlockAlign = 1;
114 wf->wBitsPerSample = 8;
115 wf->cbSize = 0;
116 } else if (strcmp(subsession->codecName(), "GSM") == 0) {
117 wf->wFormatTag = sh_audio->format = mmioFOURCC('a','g','s','m');
118 wf->nChannels = 1;
119 wf->nAvgBytesPerSec = 1650;
120 wf->nBlockAlign = 33;
121 wf->wBitsPerSample = 16;
122 wf->cbSize = 0;
123 } else if (strcmp(subsession->codecName(), "QCELP") == 0) {
124 wf->wFormatTag = sh_audio->format = mmioFOURCC('Q','c','l','p');
125 // The following settings for QCELP don't quite work right #####
126 wf->nChannels = 1;
127 wf->nAvgBytesPerSec = 1750;
128 wf->nBlockAlign = 35;
129 wf->wBitsPerSample = 16;
130 wf->cbSize = 0;
131 } else if (strcmp(subsession->codecName(), "MP4A-LATM") == 0) {
132 wf->wFormatTag = sh_audio->format = mmioFOURCC('m','p','4','a');
133 #ifndef HAVE_FAAD
134 fprintf(stderr, "WARNING: Playing MPEG-4 (AAC) Audio requires the \"faad\" library!\n");
135 #endif
136 #if (LIVEMEDIA_LIBRARY_VERSION_INT < 1042761600)
137 fprintf(stderr, "WARNING: This audio stream might not play correctly. Please upgrade to version \"2003.01.17\" or later of the \"LIVE.COM Streaming Media\" libraries.\n");
138 #else
139 // For the codec to work correctly, it needs "AudioSpecificConfig"
140 // data, which is parsed from the "StreamMuxConfig" string that
141 // was present (hopefully) in the SDP description:
142 unsigned codecdata_len;
143 sh_audio->codecdata
144 = parseStreamMuxConfigStr(subsession->fmtp_config(),
145 codecdata_len);
146 sh_audio->codecdata_len = codecdata_len;
147 #endif
148 flags |= RTPSTATE_IS_MPEG;
149 } else if (strcmp(subsession->codecName(), "X-QT") == 0 ||
150 strcmp(subsession->codecName(), "X-QUICKTIME") == 0) {
151 // QuickTime generic RTP format, as described in
152 // http://developer.apple.com/quicktime/icefloe/dispatch026.html
153
154 // We can't initialize this stream until we've received the first packet
155 // that has QuickTime "sdAtom" information in the header. So, keep
156 // reading packets until we get one:
157 unsigned char* packetData; unsigned packetDataLen;
158 QuickTimeGenericRTPSource* qtRTPSource
159 = (QuickTimeGenericRTPSource*)(subsession->rtpSource());
160 unsigned fourcc, numChannels;
161 do {
162 if (!awaitRTPPacket(demuxer, 1 /*audio*/, packetData, packetDataLen)) {
163 return;
164 }
165 } while (!parseQTState_audio(qtRTPSource->qtState, fourcc, numChannels));
166
167 wf->wFormatTag = sh_audio->format = fourcc;
168 wf->nChannels = numChannels;
169 } else {
170 fprintf(stderr,
171 "Unknown MPlayer format code for MIME type \"audio/%s\"\n",
172 subsession->codecName());
173 }
174 }
175
176 static Boolean
177 parseQTState_video(QuickTimeGenericRTPSource::QTState const& qtState,
178 unsigned& fourcc) {
179 // qtState's "sdAtom" field is supposed to contain a QuickTime video
180 // 'sample description' atom. This atom's name is the 'fourcc' that we want:
181 char const* sdAtom = qtState.sdAtom;
182 if (sdAtom == NULL || qtState.sdAtomSize < 2*4) return False;
183
184 fourcc = *(unsigned*)(&sdAtom[4]); // put in host order
185 return True;
186 }
187
188 static Boolean
189 parseQTState_audio(QuickTimeGenericRTPSource::QTState const& qtState,
190 unsigned& fourcc, unsigned& numChannels) {
191 // qtState's "sdAtom" field is supposed to contain a QuickTime audio
192 // 'sample description' atom. This atom's name is the 'fourcc' that we want.
193 // Also, the top half of the 5th word following the atom name should
194 // contain the number of channels ("numChannels") that we want:
195 char const* sdAtom = qtState.sdAtom;
196 if (sdAtom == NULL || qtState.sdAtomSize < 7*4) return False;
197
198 fourcc = *(unsigned*)(&sdAtom[4]); // put in host order
199
200 char const* word7Ptr = &sdAtom[6*4];
201 numChannels = (word7Ptr[0]<<8)|(word7Ptr[1]);
202 return True;
203 }