annotate libmpdemux/demux_rtp_codec.cpp @ 22829:9ade9f0ebfc0

Handle proper compilation on Mac OS X. Fix data alignment bug (the original purpose of the code is unclear, though). patch by Marc Hoffman, mmh pleasantst com
author diego
date Fri, 30 Mar 2007 22:09:13 +0000
parents 09f97d0161ba
children d7f4da5e9ee4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
9250
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
1 ////////// Codec-specific routines used to interface between "MPlayer"
16572
56a5f69e9b35 "LIVE.COM Streaming Media" is now called "LIVE555 Streaming Media".
rsf
parents: 11398
diff changeset
2 ////////// and the "LIVE555 Streaming Media" libraries:
9250
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
3
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
4 #include "demux_rtp_internal.h"
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
5 extern "C" {
21911
e86bb13ec44b demux_rtp_codec.cpp:100: `INT_MAX' undeclared (first use this function)
diego
parents: 21871
diff changeset
6 #include <limits.h>
21983
a6b624360aef better autodetection of framerate in case of h264; it works correctly with b-frames.
nicodvb
parents: 21911
diff changeset
7 #include <math.h>
9250
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
8 #include "stheader.h"
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
9 }
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
10
9565
e74916774667 Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents: 9370
diff changeset
11 static void
e74916774667 Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents: 9370
diff changeset
12 needVideoFrameRate(demuxer_t* demuxer, MediaSubsession* subsession); // forward
9250
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
13 static Boolean
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
14 parseQTState_video(QuickTimeGenericRTPSource::QTState const& qtState,
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
15 unsigned& fourcc); // forward
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
16 static Boolean
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
17 parseQTState_audio(QuickTimeGenericRTPSource::QTState const& qtState,
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
18 unsigned& fourcc, unsigned& numChannels); // forward
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
19
22278
ac8ac9e3761d insert extradata in sh_video->bih+1 instead of pushing it to demuxer->video as separate packet; patch by C.E.Hoyos
nicodvb
parents: 22235
diff changeset
20 static BITMAPINFOHEADER * insertVideoExtradata(BITMAPINFOHEADER *bih,
ac8ac9e3761d insert extradata in sh_video->bih+1 instead of pushing it to demuxer->video as separate packet; patch by C.E.Hoyos
nicodvb
parents: 22235
diff changeset
21 unsigned char * extraData,
ac8ac9e3761d insert extradata in sh_video->bih+1 instead of pushing it to demuxer->video as separate packet; patch by C.E.Hoyos
nicodvb
parents: 22235
diff changeset
22 unsigned size)
ac8ac9e3761d insert extradata in sh_video->bih+1 instead of pushing it to demuxer->video as separate packet; patch by C.E.Hoyos
nicodvb
parents: 22235
diff changeset
23 {
ac8ac9e3761d insert extradata in sh_video->bih+1 instead of pushing it to demuxer->video as separate packet; patch by C.E.Hoyos
nicodvb
parents: 22235
diff changeset
24 BITMAPINFOHEADER * original = bih;
ac8ac9e3761d insert extradata in sh_video->bih+1 instead of pushing it to demuxer->video as separate packet; patch by C.E.Hoyos
nicodvb
parents: 22235
diff changeset
25 if (!size || size > INT_MAX - sizeof(BITMAPINFOHEADER))
ac8ac9e3761d insert extradata in sh_video->bih+1 instead of pushing it to demuxer->video as separate packet; patch by C.E.Hoyos
nicodvb
parents: 22235
diff changeset
26 return bih;
ac8ac9e3761d insert extradata in sh_video->bih+1 instead of pushing it to demuxer->video as separate packet; patch by C.E.Hoyos
nicodvb
parents: 22235
diff changeset
27 bih = (BITMAPINFOHEADER*)realloc(bih, sizeof(BITMAPINFOHEADER) + size);
ac8ac9e3761d insert extradata in sh_video->bih+1 instead of pushing it to demuxer->video as separate packet; patch by C.E.Hoyos
nicodvb
parents: 22235
diff changeset
28 if (!bih)
ac8ac9e3761d insert extradata in sh_video->bih+1 instead of pushing it to demuxer->video as separate packet; patch by C.E.Hoyos
nicodvb
parents: 22235
diff changeset
29 return original;
ac8ac9e3761d insert extradata in sh_video->bih+1 instead of pushing it to demuxer->video as separate packet; patch by C.E.Hoyos
nicodvb
parents: 22235
diff changeset
30 bih->biSize = sizeof(BITMAPINFOHEADER) + size;
ac8ac9e3761d insert extradata in sh_video->bih+1 instead of pushing it to demuxer->video as separate packet; patch by C.E.Hoyos
nicodvb
parents: 22235
diff changeset
31 memcpy(bih+1, extraData, size);
ac8ac9e3761d insert extradata in sh_video->bih+1 instead of pushing it to demuxer->video as separate packet; patch by C.E.Hoyos
nicodvb
parents: 22235
diff changeset
32 return bih;
ac8ac9e3761d insert extradata in sh_video->bih+1 instead of pushing it to demuxer->video as separate packet; patch by C.E.Hoyos
nicodvb
parents: 22235
diff changeset
33 }
ac8ac9e3761d insert extradata in sh_video->bih+1 instead of pushing it to demuxer->video as separate packet; patch by C.E.Hoyos
nicodvb
parents: 22235
diff changeset
34
9250
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
35 void rtpCodecInitialize_video(demuxer_t* demuxer,
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
36 MediaSubsession* subsession,
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
37 unsigned& flags) {
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
38 flags = 0;
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
39 // Create a dummy video stream header
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
40 // to make the main MPlayer code happy:
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
41 sh_video_t* sh_video = new_sh_video(demuxer,0);
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
42 BITMAPINFOHEADER* bih
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
43 = (BITMAPINFOHEADER*)calloc(1,sizeof(BITMAPINFOHEADER));
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
44 bih->biSize = sizeof(BITMAPINFOHEADER);
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
45 sh_video->bih = bih;
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
46 demux_stream_t* d_video = demuxer->video;
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
47 d_video->sh = sh_video; sh_video->ds = d_video;
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
48
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
49 // Map known video MIME types to the BITMAPINFOHEADER parameters
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
50 // that this program uses. (Note that not all types need all
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
51 // of the parameters to be set.)
10478
b0d0aa726417 Added support for the "L16" and "L8" (raw PCM audio) RTP payload formats.
rsf
parents: 9910
diff changeset
52 if (strcmp(subsession->codecName(), "MPV") == 0) {
9565
e74916774667 Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents: 9370
diff changeset
53 flags |= RTPSTATE_IS_MPEG12_VIDEO;
10478
b0d0aa726417 Added support for the "L16" and "L8" (raw PCM audio) RTP payload formats.
rsf
parents: 9910
diff changeset
54 } else if (strcmp(subsession->codecName(), "MP1S") == 0 ||
b0d0aa726417 Added support for the "L16" and "L8" (raw PCM audio) RTP payload formats.
rsf
parents: 9910
diff changeset
55 strcmp(subsession->codecName(), "MP2T") == 0) {
b0d0aa726417 Added support for the "L16" and "L8" (raw PCM audio) RTP payload formats.
rsf
parents: 9910
diff changeset
56 flags |= RTPSTATE_IS_MPEG12_VIDEO|RTPSTATE_IS_MULTIPLEXED;
9250
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
57 } else if (strcmp(subsession->codecName(), "H263") == 0 ||
22211
7c9d838fc788 support for H263-2000; patch by Carl Eugen Hoyos
nicodvb
parents: 22154
diff changeset
58 strcmp(subsession->codecName(), "H263-2000") == 0 ||
9250
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
59 strcmp(subsession->codecName(), "H263-1998") == 0) {
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
60 bih->biCompression = sh_video->format
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
61 = mmioFOURCC('H','2','6','3');
9565
e74916774667 Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents: 9370
diff changeset
62 needVideoFrameRate(demuxer, subsession);
19415
95ebc082c918 Simplistic attempt to make H.264 over RTSP work.
reimar
parents: 16572
diff changeset
63 } else if (strcmp(subsession->codecName(), "H264") == 0) {
95ebc082c918 Simplistic attempt to make H.264 over RTSP work.
reimar
parents: 16572
diff changeset
64 bih->biCompression = sh_video->format
95ebc082c918 Simplistic attempt to make H.264 over RTSP work.
reimar
parents: 16572
diff changeset
65 = mmioFOURCC('H','2','6','4');
95ebc082c918 Simplistic attempt to make H.264 over RTSP work.
reimar
parents: 16572
diff changeset
66 needVideoFrameRate(demuxer, subsession);
9250
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
67 } else if (strcmp(subsession->codecName(), "H261") == 0) {
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
68 bih->biCompression = sh_video->format
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
69 = mmioFOURCC('H','2','6','1');
9565
e74916774667 Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents: 9370
diff changeset
70 needVideoFrameRate(demuxer, subsession);
9370
88bd19564b64 Motion-JPEG RTP streams can now be played. Some MPEG-4 ES video RTP
arpi
parents: 9250
diff changeset
71 } 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
72 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
73 = mmioFOURCC('M','J','P','G');
9565
e74916774667 Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents: 9370
diff changeset
74 needVideoFrameRate(demuxer, subsession);
9370
88bd19564b64 Motion-JPEG RTP streams can now be played. Some MPEG-4 ES video RTP
arpi
parents: 9250
diff changeset
75 } 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
76 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
77 = mmioFOURCC('m','p','4','v');
9565
e74916774667 Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents: 9370
diff changeset
78 // 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
79 // 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
80 // "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
81 // session's SDP description:
e74916774667 Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents: 9370
diff changeset
82 unsigned configLen;
e74916774667 Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents: 9370
diff changeset
83 unsigned char* configData
e74916774667 Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents: 9370
diff changeset
84 = parseGeneralConfigStr(subsession->fmtp_config(), configLen);
22278
ac8ac9e3761d insert extradata in sh_video->bih+1 instead of pushing it to demuxer->video as separate packet; patch by C.E.Hoyos
nicodvb
parents: 22235
diff changeset
85 sh_video->bih = bih = insertVideoExtradata(bih, configData, configLen);
9565
e74916774667 Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents: 9370
diff changeset
86 needVideoFrameRate(demuxer, subsession);
9250
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
87 } else if (strcmp(subsession->codecName(), "X-QT") == 0 ||
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
88 strcmp(subsession->codecName(), "X-QUICKTIME") == 0) {
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
89 // QuickTime generic RTP format, as described in
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
90 // http://developer.apple.com/quicktime/icefloe/dispatch026.html
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
91
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
92 // We can't initialize this stream until we've received the first packet
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
93 // that has QuickTime "sdAtom" information in the header. So, keep
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
94 // 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
95 unsigned char* packetData; unsigned packetDataLen; float pts;
9250
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
96 QuickTimeGenericRTPSource* qtRTPSource
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
97 = (QuickTimeGenericRTPSource*)(subsession->rtpSource());
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
98 unsigned fourcc;
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
99 do {
9565
e74916774667 Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents: 9370
diff changeset
100 if (!awaitRTPPacket(demuxer, demuxer->video,
e74916774667 Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents: 9370
diff changeset
101 packetData, packetDataLen, pts)) {
9250
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
102 return;
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
103 }
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
104 } while (!parseQTState_video(qtRTPSource->qtState, fourcc));
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
105
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
106 bih->biCompression = sh_video->format = fourcc;
21871
8f5c762ca6d9 add support for avc1 in X-QT over RTSP
gpoirier
parents: 19415
diff changeset
107 bih->biWidth = qtRTPSource->qtState.width;
8f5c762ca6d9 add support for avc1 in X-QT over RTSP
gpoirier
parents: 19415
diff changeset
108 bih->biHeight = qtRTPSource->qtState.height;
8f5c762ca6d9 add support for avc1 in X-QT over RTSP
gpoirier
parents: 19415
diff changeset
109 uint8_t *pos = (uint8_t*)qtRTPSource->qtState.sdAtom + 86;
8f5c762ca6d9 add support for avc1 in X-QT over RTSP
gpoirier
parents: 19415
diff changeset
110 uint8_t *endpos = (uint8_t*)qtRTPSource->qtState.sdAtom
8f5c762ca6d9 add support for avc1 in X-QT over RTSP
gpoirier
parents: 19415
diff changeset
111 + qtRTPSource->qtState.sdAtomSize;
8f5c762ca6d9 add support for avc1 in X-QT over RTSP
gpoirier
parents: 19415
diff changeset
112 while (pos+8 < endpos) {
8f5c762ca6d9 add support for avc1 in X-QT over RTSP
gpoirier
parents: 19415
diff changeset
113 unsigned atomLength = pos[0]<<24 | pos[1]<<16 | pos[2]<<8 | pos[3];
8f5c762ca6d9 add support for avc1 in X-QT over RTSP
gpoirier
parents: 19415
diff changeset
114 if (atomLength == 0 || atomLength > endpos-pos) break;
22809
09f97d0161ba Handle X-QT extradata in a slightly more correct way
cehoyos
parents: 22756
diff changeset
115 if ((!memcmp(pos+4, "avcC", 4) && fourcc==mmioFOURCC('a','v','c','1') ||
22154
1dc228327c55 when the video codec is mpeg4video copy the content of the esds in extradata; patch by ceyes ag or at
nicodvb
parents: 22112
diff changeset
116 !memcmp(pos+4, "esds", 4) ||
22809
09f97d0161ba Handle X-QT extradata in a slightly more correct way
cehoyos
parents: 22756
diff changeset
117 !memcmp(pos+4, "SMI ", 4) && fourcc==mmioFOURCC('S','V','Q','3')) &&
22278
ac8ac9e3761d insert extradata in sh_video->bih+1 instead of pushing it to demuxer->video as separate packet; patch by C.E.Hoyos
nicodvb
parents: 22235
diff changeset
118 atomLength > 8) {
ac8ac9e3761d insert extradata in sh_video->bih+1 instead of pushing it to demuxer->video as separate packet; patch by C.E.Hoyos
nicodvb
parents: 22235
diff changeset
119 sh_video->bih = bih =
ac8ac9e3761d insert extradata in sh_video->bih+1 instead of pushing it to demuxer->video as separate packet; patch by C.E.Hoyos
nicodvb
parents: 22235
diff changeset
120 insertVideoExtradata(bih, pos+8, atomLength-8);
21871
8f5c762ca6d9 add support for avc1 in X-QT over RTSP
gpoirier
parents: 19415
diff changeset
121 break;
8f5c762ca6d9 add support for avc1 in X-QT over RTSP
gpoirier
parents: 19415
diff changeset
122 }
8f5c762ca6d9 add support for avc1 in X-QT over RTSP
gpoirier
parents: 19415
diff changeset
123 pos += atomLength;
8f5c762ca6d9 add support for avc1 in X-QT over RTSP
gpoirier
parents: 19415
diff changeset
124 }
22756
3d6a64f3d28f Every X-QT stream needs video frame rate (not just avc, mpeg4 and svq3)
cehoyos
parents: 22463
diff changeset
125 needVideoFrameRate(demuxer, subsession);
9250
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
126 } else {
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
127 fprintf(stderr,
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
128 "Unknown MPlayer format code for MIME type \"video/%s\"\n",
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
129 subsession->codecName());
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
130 }
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
131 }
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
132
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
133 void rtpCodecInitialize_audio(demuxer_t* demuxer,
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
134 MediaSubsession* subsession,
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
135 unsigned& flags) {
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
136 flags = 0;
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
137 // Create a dummy audio stream header
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
138 // to make the main MPlayer code happy:
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
139 sh_audio_t* sh_audio = new_sh_audio(demuxer,0);
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
140 WAVEFORMATEX* wf = (WAVEFORMATEX*)calloc(1,sizeof(WAVEFORMATEX));
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
141 sh_audio->wf = wf;
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
142 demux_stream_t* d_audio = demuxer->audio;
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
143 d_audio->sh = sh_audio; sh_audio->ds = d_audio;
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
144
9565
e74916774667 Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents: 9370
diff changeset
145 wf->nChannels = subsession->numChannels();
e74916774667 Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents: 9370
diff changeset
146
9250
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
147 // Map known audio MIME types to the WAVEFORMATEX parameters
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
148 // that this program uses. (Note that not all types need all
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
149 // of the parameters to be set.)
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
150 wf->nSamplesPerSec
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
151 = subsession->rtpSource()->timestampFrequency(); // by default
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
152 if (strcmp(subsession->codecName(), "MPA") == 0 ||
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
153 strcmp(subsession->codecName(), "MPA-ROBUST") == 0 ||
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
154 strcmp(subsession->codecName(), "X-MP3-DRAFT-00") == 0) {
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
155 wf->wFormatTag = sh_audio->format = 0x55;
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
156 // Note: 0x55 is for layer III, but should work for I,II also
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
157 wf->nSamplesPerSec = 0; // sample rate is deduced from the data
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
158 } else if (strcmp(subsession->codecName(), "AC3") == 0) {
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
159 wf->wFormatTag = sh_audio->format = 0x2000;
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
160 wf->nSamplesPerSec = 0; // sample rate is deduced from the data
10478
b0d0aa726417 Added support for the "L16" and "L8" (raw PCM audio) RTP payload formats.
rsf
parents: 9910
diff changeset
161 } else if (strcmp(subsession->codecName(), "L16") == 0) {
b0d0aa726417 Added support for the "L16" and "L8" (raw PCM audio) RTP payload formats.
rsf
parents: 9910
diff changeset
162 wf->wFormatTag = sh_audio->format = 0x736f7774; // "twos"
b0d0aa726417 Added support for the "L16" and "L8" (raw PCM audio) RTP payload formats.
rsf
parents: 9910
diff changeset
163 wf->nBlockAlign = 1;
b0d0aa726417 Added support for the "L16" and "L8" (raw PCM audio) RTP payload formats.
rsf
parents: 9910
diff changeset
164 wf->wBitsPerSample = 16;
b0d0aa726417 Added support for the "L16" and "L8" (raw PCM audio) RTP payload formats.
rsf
parents: 9910
diff changeset
165 wf->cbSize = 0;
b0d0aa726417 Added support for the "L16" and "L8" (raw PCM audio) RTP payload formats.
rsf
parents: 9910
diff changeset
166 } else if (strcmp(subsession->codecName(), "L8") == 0) {
b0d0aa726417 Added support for the "L16" and "L8" (raw PCM audio) RTP payload formats.
rsf
parents: 9910
diff changeset
167 wf->wFormatTag = sh_audio->format = 0x20776172; // "raw "
b0d0aa726417 Added support for the "L16" and "L8" (raw PCM audio) RTP payload formats.
rsf
parents: 9910
diff changeset
168 wf->nBlockAlign = 1;
b0d0aa726417 Added support for the "L16" and "L8" (raw PCM audio) RTP payload formats.
rsf
parents: 9910
diff changeset
169 wf->wBitsPerSample = 8;
b0d0aa726417 Added support for the "L16" and "L8" (raw PCM audio) RTP payload formats.
rsf
parents: 9910
diff changeset
170 wf->cbSize = 0;
9250
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
171 } else if (strcmp(subsession->codecName(), "PCMU") == 0) {
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
172 wf->wFormatTag = sh_audio->format = 0x7;
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
173 wf->nAvgBytesPerSec = 8000;
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
174 wf->nBlockAlign = 1;
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
175 wf->wBitsPerSample = 8;
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
176 wf->cbSize = 0;
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
177 } else if (strcmp(subsession->codecName(), "PCMA") == 0) {
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
178 wf->wFormatTag = sh_audio->format = 0x6;
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
179 wf->nAvgBytesPerSec = 8000;
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
180 wf->nBlockAlign = 1;
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
181 wf->wBitsPerSample = 8;
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
182 wf->cbSize = 0;
22463
979b2aa16e80 support for AMR; it works inserting in the first byte of the demux_packet
nicodvb
parents: 22354
diff changeset
183 } else if (strcmp(subsession->codecName(), "AMR") == 0) {
979b2aa16e80 support for AMR; it works inserting in the first byte of the demux_packet
nicodvb
parents: 22354
diff changeset
184 wf->wFormatTag = sh_audio->format = mmioFOURCC('s','a','m','r');
979b2aa16e80 support for AMR; it works inserting in the first byte of the demux_packet
nicodvb
parents: 22354
diff changeset
185 } else if (strcmp(subsession->codecName(), "AMR-WB") == 0) {
979b2aa16e80 support for AMR; it works inserting in the first byte of the demux_packet
nicodvb
parents: 22354
diff changeset
186 wf->wFormatTag = sh_audio->format = mmioFOURCC('s','a','w','b');
9250
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
187 } else if (strcmp(subsession->codecName(), "GSM") == 0) {
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
188 wf->wFormatTag = sh_audio->format = mmioFOURCC('a','g','s','m');
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
189 wf->nAvgBytesPerSec = 1650;
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
190 wf->nBlockAlign = 33;
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
191 wf->wBitsPerSample = 16;
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
192 wf->cbSize = 0;
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
193 } else if (strcmp(subsession->codecName(), "QCELP") == 0) {
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
194 wf->wFormatTag = sh_audio->format = mmioFOURCC('Q','c','l','p');
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
195 wf->nAvgBytesPerSec = 1750;
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
196 wf->nBlockAlign = 35;
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
197 wf->wBitsPerSample = 16;
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
198 wf->cbSize = 0;
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
199 } else if (strcmp(subsession->codecName(), "MP4A-LATM") == 0) {
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
200 wf->wFormatTag = sh_audio->format = mmioFOURCC('m','p','4','a');
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
201 // For the codec to work correctly, it needs "AudioSpecificConfig"
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
202 // data, which is parsed from the "StreamMuxConfig" string that
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
203 // was present (hopefully) in the SDP description:
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
204 unsigned codecdata_len;
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
205 sh_audio->codecdata
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
206 = parseStreamMuxConfigStr(subsession->fmtp_config(),
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
207 codecdata_len);
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
208 sh_audio->codecdata_len = codecdata_len;
22235
583926af08ac omit length field of AAC-LATM audio streams; fixes decoding by faad. Patch by Carl Eugen Hoyos (cehoyos ag or at)
nicodvb
parents: 22211
diff changeset
209 //faad doesn't understand LATM's data length field, so omit it
583926af08ac omit length field of AAC-LATM audio streams; fixes decoding by faad. Patch by Carl Eugen Hoyos (cehoyos ag or at)
nicodvb
parents: 22211
diff changeset
210 ((MPEG4LATMAudioRTPSource*)subsession->rtpSource())->omitLATMDataLengthField();
9565
e74916774667 Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents: 9370
diff changeset
211 } 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
212 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
213 // 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
214 // 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
215 unsigned codecdata_len;
e74916774667 Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents: 9370
diff changeset
216 sh_audio->codecdata
e74916774667 Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents: 9370
diff changeset
217 = parseGeneralConfigStr(subsession->fmtp_config(),
e74916774667 Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents: 9370
diff changeset
218 codecdata_len);
e74916774667 Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents: 9370
diff changeset
219 sh_audio->codecdata_len = codecdata_len;
9250
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
220 } else if (strcmp(subsession->codecName(), "X-QT") == 0 ||
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
221 strcmp(subsession->codecName(), "X-QUICKTIME") == 0) {
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
222 // QuickTime generic RTP format, as described in
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
223 // http://developer.apple.com/quicktime/icefloe/dispatch026.html
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
224
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
225 // We can't initialize this stream until we've received the first packet
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
226 // that has QuickTime "sdAtom" information in the header. So, keep
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
227 // 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
228 unsigned char* packetData; unsigned packetDataLen; float pts;
9250
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
229 QuickTimeGenericRTPSource* qtRTPSource
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
230 = (QuickTimeGenericRTPSource*)(subsession->rtpSource());
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
231 unsigned fourcc, numChannels;
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
232 do {
9565
e74916774667 Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents: 9370
diff changeset
233 if (!awaitRTPPacket(demuxer, demuxer->audio,
e74916774667 Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents: 9370
diff changeset
234 packetData, packetDataLen, pts)) {
9250
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
235 return;
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
236 }
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
237 } while (!parseQTState_audio(qtRTPSource->qtState, fourcc, numChannels));
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
238
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
239 wf->wFormatTag = sh_audio->format = fourcc;
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
240 wf->nChannels = numChannels;
22336
760c74c078ce copy the content of QDM2 atom as extradata for ffqdm2 playback; patch by Carl Eugen Hoyos
nicodvb
parents: 22278
diff changeset
241
760c74c078ce copy the content of QDM2 atom as extradata for ffqdm2 playback; patch by Carl Eugen Hoyos
nicodvb
parents: 22278
diff changeset
242 uint8_t *pos = (uint8_t*)qtRTPSource->qtState.sdAtom + 52;
760c74c078ce copy the content of QDM2 atom as extradata for ffqdm2 playback; patch by Carl Eugen Hoyos
nicodvb
parents: 22278
diff changeset
243 uint8_t *endpos = (uint8_t*)qtRTPSource->qtState.sdAtom
760c74c078ce copy the content of QDM2 atom as extradata for ffqdm2 playback; patch by Carl Eugen Hoyos
nicodvb
parents: 22278
diff changeset
244 + qtRTPSource->qtState.sdAtomSize;
760c74c078ce copy the content of QDM2 atom as extradata for ffqdm2 playback; patch by Carl Eugen Hoyos
nicodvb
parents: 22278
diff changeset
245 while (pos+8 < endpos) {
760c74c078ce copy the content of QDM2 atom as extradata for ffqdm2 playback; patch by Carl Eugen Hoyos
nicodvb
parents: 22278
diff changeset
246 unsigned atomLength = pos[0]<<24 | pos[1]<<16 | pos[2]<<8 | pos[3];
760c74c078ce copy the content of QDM2 atom as extradata for ffqdm2 playback; patch by Carl Eugen Hoyos
nicodvb
parents: 22278
diff changeset
247 if (atomLength == 0 || atomLength > endpos-pos) break;
760c74c078ce copy the content of QDM2 atom as extradata for ffqdm2 playback; patch by Carl Eugen Hoyos
nicodvb
parents: 22278
diff changeset
248 if (!memcmp(pos+4, "wave", 4) && fourcc==mmioFOURCC('Q','D','M','2') &&
760c74c078ce copy the content of QDM2 atom as extradata for ffqdm2 playback; patch by Carl Eugen Hoyos
nicodvb
parents: 22278
diff changeset
249 atomLength > 8 &&
760c74c078ce copy the content of QDM2 atom as extradata for ffqdm2 playback; patch by Carl Eugen Hoyos
nicodvb
parents: 22278
diff changeset
250 atomLength <= INT_MAX) {
760c74c078ce copy the content of QDM2 atom as extradata for ffqdm2 playback; patch by Carl Eugen Hoyos
nicodvb
parents: 22278
diff changeset
251 sh_audio->codecdata = (unsigned char*) malloc(atomLength-8);
760c74c078ce copy the content of QDM2 atom as extradata for ffqdm2 playback; patch by Carl Eugen Hoyos
nicodvb
parents: 22278
diff changeset
252 if (sh_audio->codecdata) {
760c74c078ce copy the content of QDM2 atom as extradata for ffqdm2 playback; patch by Carl Eugen Hoyos
nicodvb
parents: 22278
diff changeset
253 memcpy(sh_audio->codecdata, pos+8, atomLength-8);
760c74c078ce copy the content of QDM2 atom as extradata for ffqdm2 playback; patch by Carl Eugen Hoyos
nicodvb
parents: 22278
diff changeset
254 sh_audio->codecdata_len = atomLength-8;
760c74c078ce copy the content of QDM2 atom as extradata for ffqdm2 playback; patch by Carl Eugen Hoyos
nicodvb
parents: 22278
diff changeset
255 }
760c74c078ce copy the content of QDM2 atom as extradata for ffqdm2 playback; patch by Carl Eugen Hoyos
nicodvb
parents: 22278
diff changeset
256 break;
760c74c078ce copy the content of QDM2 atom as extradata for ffqdm2 playback; patch by Carl Eugen Hoyos
nicodvb
parents: 22278
diff changeset
257 }
760c74c078ce copy the content of QDM2 atom as extradata for ffqdm2 playback; patch by Carl Eugen Hoyos
nicodvb
parents: 22278
diff changeset
258 pos += atomLength;
760c74c078ce copy the content of QDM2 atom as extradata for ffqdm2 playback; patch by Carl Eugen Hoyos
nicodvb
parents: 22278
diff changeset
259 }
9250
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
260 } else {
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
261 fprintf(stderr,
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
262 "Unknown MPlayer format code for MIME type \"audio/%s\"\n",
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
263 subsession->codecName());
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
264 }
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
265 }
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
266
9565
e74916774667 Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents: 9370
diff changeset
267 static void needVideoFrameRate(demuxer_t* demuxer,
e74916774667 Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents: 9370
diff changeset
268 MediaSubsession* subsession) {
e74916774667 Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents: 9370
diff changeset
269 // 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
270 // 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
271 // 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
272 // presentation timestamps in successive packets,
e74916774667 Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents: 9370
diff changeset
273 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
274
e74916774667 Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents: 9370
diff changeset
275 demux_stream_t* d_video = demuxer->video;
9910
41ed68e3a034 Minor code cleanup.
rsf
parents: 9565
diff changeset
276 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
277
e74916774667 Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents: 9370
diff changeset
278 // 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
279 int fps = (int)(subsession->videoFPS());
e74916774667 Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents: 9370
diff changeset
280 if (fps != 0) {
e74916774667 Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents: 9370
diff changeset
281 sh_video->fps = fps;
22354
b465e5be1a53 assign missing frametime as 1.0/fps; patch by Carl Eigen Hoyos
nicodvb
parents: 22336
diff changeset
282 sh_video->frametime = 1.0f/fps;
9565
e74916774667 Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents: 9370
diff changeset
283 return;
e74916774667 Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents: 9370
diff changeset
284 }
e74916774667 Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents: 9370
diff changeset
285
e74916774667 Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents: 9370
diff changeset
286 // 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
287 // non-zero "pts" timestamps:
e74916774667 Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents: 9370
diff changeset
288 unsigned char* packetData; unsigned packetDataLen;
e74916774667 Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents: 9370
diff changeset
289 float lastPTS = 0.0, curPTS;
11397
d0db11b74e82 Increased the threshold for how many incoming frames to look at while guessing
rsf
parents: 10478
diff changeset
290 unsigned const maxNumFramesToWaitFor = 300;
21983
a6b624360aef better autodetection of framerate in case of h264; it works correctly with b-frames.
nicodvb
parents: 21911
diff changeset
291 int lastfps = 0;
9565
e74916774667 Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents: 9370
diff changeset
292 for (unsigned i = 0; i < maxNumFramesToWaitFor; ++i) {
9910
41ed68e3a034 Minor code cleanup.
rsf
parents: 9565
diff changeset
293 if (!awaitRTPPacket(demuxer, d_video, packetData, packetDataLen, curPTS)) {
41ed68e3a034 Minor code cleanup.
rsf
parents: 9565
diff changeset
294 break;
41ed68e3a034 Minor code cleanup.
rsf
parents: 9565
diff changeset
295 }
9565
e74916774667 Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents: 9370
diff changeset
296
21983
a6b624360aef better autodetection of framerate in case of h264; it works correctly with b-frames.
nicodvb
parents: 21911
diff changeset
297 if (curPTS != lastPTS && lastPTS != 0.0) {
9565
e74916774667 Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents: 9370
diff changeset
298 // 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
299 // (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
300 // Guess the frame rate as an integer. If it's not, use "-fps" instead.
21983
a6b624360aef better autodetection of framerate in case of h264; it works correctly with b-frames.
nicodvb
parents: 21911
diff changeset
301 fps = (int)(1/fabs(curPTS-lastPTS) + 0.5); // rounding
a6b624360aef better autodetection of framerate in case of h264; it works correctly with b-frames.
nicodvb
parents: 21911
diff changeset
302 if (fps == lastfps) {
9565
e74916774667 Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents: 9370
diff changeset
303 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
304 sh_video->fps = fps;
21983
a6b624360aef better autodetection of framerate in case of h264; it works correctly with b-frames.
nicodvb
parents: 21911
diff changeset
305 sh_video->frametime=1.0f/fps;
9565
e74916774667 Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents: 9370
diff changeset
306 return;
21983
a6b624360aef better autodetection of framerate in case of h264; it works correctly with b-frames.
nicodvb
parents: 21911
diff changeset
307 }
a6b624360aef better autodetection of framerate in case of h264; it works correctly with b-frames.
nicodvb
parents: 21911
diff changeset
308 if (fps>lastfps) lastfps = fps;
9565
e74916774667 Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents: 9370
diff changeset
309 }
e74916774667 Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents: 9370
diff changeset
310 lastPTS = curPTS;
e74916774667 Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents: 9370
diff changeset
311 }
e74916774667 Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents: 9370
diff changeset
312 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
313 }
e74916774667 Improved RTP packet buffering, by relying on the underlying OS's UDP
rsf
parents: 9370
diff changeset
314
9250
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
315 static Boolean
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
316 parseQTState_video(QuickTimeGenericRTPSource::QTState const& qtState,
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
317 unsigned& fourcc) {
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
318 // qtState's "sdAtom" field is supposed to contain a QuickTime video
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
319 // 'sample description' atom. This atom's name is the 'fourcc' that we want:
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
320 char const* sdAtom = qtState.sdAtom;
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
321 if (sdAtom == NULL || qtState.sdAtomSize < 2*4) return False;
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
322
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
323 fourcc = *(unsigned*)(&sdAtom[4]); // put in host order
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
324 return True;
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
325 }
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
326
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
327 static Boolean
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
328 parseQTState_audio(QuickTimeGenericRTPSource::QTState const& qtState,
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
329 unsigned& fourcc, unsigned& numChannels) {
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
330 // qtState's "sdAtom" field is supposed to contain a QuickTime audio
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
331 // 'sample description' atom. This atom's name is the 'fourcc' that we want.
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
332 // Also, the top half of the 5th word following the atom name should
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
333 // contain the number of channels ("numChannels") that we want:
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
334 char const* sdAtom = qtState.sdAtom;
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
335 if (sdAtom == NULL || qtState.sdAtomSize < 7*4) return False;
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
336
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
337 fourcc = *(unsigned*)(&sdAtom[4]); // put in host order
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
338
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
339 char const* word7Ptr = &sdAtom[6*4];
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
340 numChannels = (word7Ptr[0]<<8)|(word7Ptr[1]);
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
341 return True;
bb490ffeebf5 Restruct by Ross Finlayson <finlayson@live.com>
bertrand
parents:
diff changeset
342 }