Chris Robinson | b3fab14 | 2007-04-24 05:28:14 -0700 | [diff] [blame] | 1 | /* |
| 2 | * MPEG Splitter Filter |
| 3 | * |
| 4 | * Copyright 2003 Robert Shearman |
| 5 | * Copyright 2004-2005 Christian Costa |
| 6 | * Copyright 2007 Chris Robinson |
Maarten Lankhorst | 2734e63 | 2008-04-09 12:07:14 -0700 | [diff] [blame] | 7 | * Copyright 2008 Maarten Lankhorst |
Chris Robinson | b3fab14 | 2007-04-24 05:28:14 -0700 | [diff] [blame] | 8 | * |
| 9 | * This library is free software; you can redistribute it and/or |
| 10 | * modify it under the terms of the GNU Lesser General Public |
| 11 | * License as published by the Free Software Foundation; either |
| 12 | * version 2.1 of the License, or (at your option) any later version. |
| 13 | * |
| 14 | * This library is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 | * Lesser General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU Lesser General Public |
| 20 | * License along with this library; if not, write to the Free Software |
| 21 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA |
| 22 | */ |
| 23 | |
| 24 | #include <assert.h> |
| 25 | #include <math.h> |
| 26 | |
| 27 | #include "quartz_private.h" |
| 28 | #include "control_private.h" |
| 29 | #include "pin.h" |
| 30 | |
| 31 | #include "uuids.h" |
| 32 | #include "mmreg.h" |
Chris Robinson | b3fab14 | 2007-04-24 05:28:14 -0700 | [diff] [blame] | 33 | #include "mmsystem.h" |
| 34 | |
| 35 | #include "winternl.h" |
| 36 | |
| 37 | #include "wine/unicode.h" |
| 38 | #include "wine/debug.h" |
| 39 | |
| 40 | #include "parser.h" |
| 41 | |
| 42 | WINE_DEFAULT_DEBUG_CHANNEL(quartz); |
| 43 | |
| 44 | #define SEQUENCE_HEADER_CODE 0xB3 |
| 45 | #define PACK_START_CODE 0xBA |
| 46 | |
| 47 | #define SYSTEM_START_CODE 0xBB |
| 48 | #define AUDIO_ELEMENTARY_STREAM 0xC0 |
| 49 | #define VIDEO_ELEMENTARY_STREAM 0xE0 |
| 50 | |
| 51 | #define MPEG_SYSTEM_HEADER 3 |
| 52 | #define MPEG_VIDEO_HEADER 2 |
| 53 | #define MPEG_AUDIO_HEADER 1 |
| 54 | #define MPEG_NO_HEADER 0 |
| 55 | |
Maarten Lankhorst | c1a4acd | 2008-04-18 22:21:59 -0700 | [diff] [blame] | 56 | #define SEEK_INTERVAL (ULONGLONG)(10 * 10000000) /* Add an entry every 10 seconds */ |
Maarten Lankhorst | 24cac93 | 2008-04-08 12:28:09 -0700 | [diff] [blame] | 57 | |
| 58 | struct seek_entry { |
| 59 | ULONGLONG bytepos; |
| 60 | ULONGLONG timepos; |
| 61 | }; |
| 62 | |
Chris Robinson | b3fab14 | 2007-04-24 05:28:14 -0700 | [diff] [blame] | 63 | typedef struct MPEGSplitterImpl |
| 64 | { |
| 65 | ParserImpl Parser; |
Chris Robinson | b3fab14 | 2007-04-24 05:28:14 -0700 | [diff] [blame] | 66 | LONGLONG EndOfFile; |
Maarten Lankhorst | b0c6a34 | 2008-03-20 22:33:47 -0700 | [diff] [blame] | 67 | LONGLONG duration; |
| 68 | LONGLONG position; |
Maarten Lankhorst | 3a39805 | 2008-04-22 13:57:11 -0700 | [diff] [blame] | 69 | DWORD begin_offset; |
| 70 | BYTE header[4]; |
| 71 | |
| 72 | /* Whether we just seeked (or started playing) */ |
Maarten Lankhorst | b1b7524 | 2008-04-01 14:42:32 -0700 | [diff] [blame] | 73 | BOOL seek; |
Maarten Lankhorst | 3a39805 | 2008-04-22 13:57:11 -0700 | [diff] [blame] | 74 | |
| 75 | /* Seeking cache */ |
Maarten Lankhorst | 24cac93 | 2008-04-08 12:28:09 -0700 | [diff] [blame] | 76 | ULONG seek_entries; |
| 77 | struct seek_entry *seektable; |
Chris Robinson | b3fab14 | 2007-04-24 05:28:14 -0700 | [diff] [blame] | 78 | } MPEGSplitterImpl; |
| 79 | |
| 80 | static int MPEGSplitter_head_check(const BYTE *header) |
| 81 | { |
| 82 | /* If this is a possible start code, check for a system or video header */ |
| 83 | if (header[0] == 0 && header[1] == 0 && header[2] == 1) |
| 84 | { |
| 85 | /* Check if we got a system or elementary stream start code */ |
| 86 | if (header[3] == PACK_START_CODE || |
| 87 | header[3] == VIDEO_ELEMENTARY_STREAM || |
| 88 | header[3] == AUDIO_ELEMENTARY_STREAM) |
| 89 | return MPEG_SYSTEM_HEADER; |
| 90 | |
| 91 | /* Check for a MPEG video sequence start code */ |
| 92 | if (header[3] == SEQUENCE_HEADER_CODE) |
| 93 | return MPEG_VIDEO_HEADER; |
| 94 | } |
| 95 | |
| 96 | /* This should give a good guess if we have an MPEG audio header */ |
| 97 | if(header[0] == 0xff && ((header[1]>>5)&0x7) == 0x7 && |
| 98 | ((header[1]>>1)&0x3) != 0 && ((header[2]>>4)&0xf) != 0xf && |
| 99 | ((header[2]>>2)&0x3) != 0x3) |
| 100 | return MPEG_AUDIO_HEADER; |
| 101 | |
| 102 | /* Nothing yet.. */ |
| 103 | return MPEG_NO_HEADER; |
| 104 | } |
| 105 | |
Maarten Lankhorst | b0c6a34 | 2008-03-20 22:33:47 -0700 | [diff] [blame] | 106 | static const WCHAR wszAudioStream[] = {'A','u','d','i','o',0}; |
| 107 | static const WCHAR wszVideoStream[] = {'V','i','d','e','o',0}; |
| 108 | |
| 109 | static const DWORD freqs[10] = { 44100, 48000, 32000, 22050, 24000, 16000, 11025, 12000, 8000, 0 }; |
| 110 | |
| 111 | static const DWORD tabsel_123[2][3][16] = { |
| 112 | { {0,32,64,96,128,160,192,224,256,288,320,352,384,416,448,}, |
| 113 | {0,32,48,56, 64, 80, 96,112,128,160,192,224,256,320,384,}, |
| 114 | {0,32,40,48, 56, 64, 80, 96,112,128,160,192,224,256,320,} }, |
| 115 | |
| 116 | { {0,32,48,56,64,80,96,112,128,144,160,176,192,224,256,}, |
| 117 | {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,}, |
| 118 | {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,} } |
| 119 | }; |
| 120 | |
Maarten Lankhorst | b0c6a34 | 2008-03-20 22:33:47 -0700 | [diff] [blame] | 121 | static HRESULT parse_header(BYTE *header, LONGLONG *plen, LONGLONG *pduration) |
| 122 | { |
Maarten Lankhorst | 3a39805 | 2008-04-22 13:57:11 -0700 | [diff] [blame] | 123 | LONGLONG duration; |
Maarten Lankhorst | b0c6a34 | 2008-03-20 22:33:47 -0700 | [diff] [blame] | 124 | |
Andrew Talbot | 523552f | 2008-04-23 22:13:04 +0100 | [diff] [blame] | 125 | int bitrate_index, freq_index, lsf = 1, mpeg1, layer, padding, bitrate, length; |
Maarten Lankhorst | b0c6a34 | 2008-03-20 22:33:47 -0700 | [diff] [blame] | 126 | |
| 127 | if (!(header[0] == 0xff && ((header[1]>>5)&0x7) == 0x7 && |
| 128 | ((header[1]>>1)&0x3) != 0 && ((header[2]>>4)&0xf) != 0xf && |
| 129 | ((header[2]>>2)&0x3) != 0x3)) |
| 130 | { |
Maarten Lankhorst | b1b7524 | 2008-04-01 14:42:32 -0700 | [diff] [blame] | 131 | FIXME("Not a valid header: %02x:%02x\n", header[0], header[1]); |
Maarten Lankhorst | b0c6a34 | 2008-03-20 22:33:47 -0700 | [diff] [blame] | 132 | return E_INVALIDARG; |
| 133 | } |
| 134 | |
| 135 | mpeg1 = (header[1]>>4)&0x1; |
| 136 | if (mpeg1) |
| 137 | lsf = ((header[1]>>3)&0x1)^1; |
| 138 | |
| 139 | layer = 4-((header[1]>>1)&0x3); |
| 140 | bitrate_index = ((header[2]>>4)&0xf); |
| 141 | freq_index = ((header[2]>>2)&0x3) + (mpeg1?(lsf*3):6); |
| 142 | padding = ((header[2]>>1)&0x1); |
Maarten Lankhorst | b0c6a34 | 2008-03-20 22:33:47 -0700 | [diff] [blame] | 143 | |
| 144 | bitrate = tabsel_123[lsf][layer-1][bitrate_index] * 1000; |
Maarten Lankhorst | b1b7524 | 2008-04-01 14:42:32 -0700 | [diff] [blame] | 145 | if (!bitrate || layer != 3) |
| 146 | { |
Maarten Lankhorst | 4bc4457 | 2008-04-07 16:11:36 -0700 | [diff] [blame] | 147 | FIXME("Not a valid header: %02x:%02x:%02x:%02x\n", header[0], header[1], header[2], header[3]); |
Maarten Lankhorst | b1b7524 | 2008-04-01 14:42:32 -0700 | [diff] [blame] | 148 | return E_INVALIDARG; |
| 149 | } |
| 150 | |
| 151 | |
Maarten Lankhorst | b0c6a34 | 2008-03-20 22:33:47 -0700 | [diff] [blame] | 152 | if (layer == 3 || layer == 2) |
| 153 | length = 144 * bitrate / freqs[freq_index] + padding; |
| 154 | else |
| 155 | length = 4 * (12 * bitrate / freqs[freq_index] + padding); |
| 156 | |
| 157 | duration = (ULONGLONG)10000000 * (ULONGLONG)(length) / (ULONGLONG)(bitrate/8); |
| 158 | *plen = length; |
Maarten Lankhorst | 3a39805 | 2008-04-22 13:57:11 -0700 | [diff] [blame] | 159 | if (pduration) |
| 160 | *pduration += duration; |
Maarten Lankhorst | b0c6a34 | 2008-03-20 22:33:47 -0700 | [diff] [blame] | 161 | return S_OK; |
| 162 | } |
| 163 | |
Maarten Lankhorst | 3a39805 | 2008-04-22 13:57:11 -0700 | [diff] [blame] | 164 | static HRESULT FillBuffer(MPEGSplitterImpl *This, IMediaSample *pCurrentSample) |
Maarten Lankhorst | b0c6a34 | 2008-03-20 22:33:47 -0700 | [diff] [blame] | 165 | { |
| 166 | Parser_OutputPin * pOutputPin = (Parser_OutputPin*)This->Parser.ppPins[1]; |
| 167 | LONGLONG length = 0; |
Maarten Lankhorst | 3a39805 | 2008-04-22 13:57:11 -0700 | [diff] [blame] | 168 | LONGLONG pos = BYTES_FROM_MEDIATIME(This->Parser.pInputPin->rtNext); |
| 169 | LONGLONG time = This->position; |
| 170 | HRESULT hr; |
| 171 | BYTE *fbuf = NULL; |
| 172 | DWORD len = IMediaSample_GetActualDataLength(pCurrentSample); |
Maarten Lankhorst | b0c6a34 | 2008-03-20 22:33:47 -0700 | [diff] [blame] | 173 | |
Maarten Lankhorst | 3a39805 | 2008-04-22 13:57:11 -0700 | [diff] [blame] | 174 | TRACE("Source length: %u\n", len); |
| 175 | IMediaSample_GetPointer(pCurrentSample, &fbuf); |
Maarten Lankhorst | b0c6a34 | 2008-03-20 22:33:47 -0700 | [diff] [blame] | 176 | |
| 177 | /* Find the next valid header.. it <SHOULD> be right here */ |
Maarten Lankhorst | 3a39805 | 2008-04-22 13:57:11 -0700 | [diff] [blame] | 178 | assert(parse_header(fbuf, &length, &This->position) == S_OK); |
Maarten Lankhorst | 3a39805 | 2008-04-22 13:57:11 -0700 | [diff] [blame] | 179 | IMediaSample_SetActualDataLength(pCurrentSample, length); |
Maarten Lankhorst | b0c6a34 | 2008-03-20 22:33:47 -0700 | [diff] [blame] | 180 | |
Maarten Lankhorst | 0f73bba | 2008-04-30 14:34:07 -0700 | [diff] [blame] | 181 | /* Queue the next sample */ |
Maarten Lankhorst | 3a39805 | 2008-04-22 13:57:11 -0700 | [diff] [blame] | 182 | if (length + 4 == len) |
Maarten Lankhorst | b0c6a34 | 2008-03-20 22:33:47 -0700 | [diff] [blame] | 183 | { |
Maarten Lankhorst | 3a39805 | 2008-04-22 13:57:11 -0700 | [diff] [blame] | 184 | PullPin *pin = This->Parser.pInputPin; |
| 185 | LONGLONG stop = BYTES_FROM_MEDIATIME(pin->rtStop); |
| 186 | |
| 187 | hr = S_OK; |
| 188 | memcpy(This->header, fbuf + length, 4); |
| 189 | while (FAILED(hr = parse_header(This->header, &length, NULL))) |
| 190 | { |
| 191 | memmove(This->header, This->header+1, 3); |
| 192 | if (pos + 4 >= stop) |
| 193 | break; |
| 194 | IAsyncReader_SyncRead(pin->pReader, ++pos, 1, This->header + 3); |
| 195 | } |
| 196 | pin->rtNext = MEDIATIME_FROM_BYTES(pos); |
| 197 | |
| 198 | if (SUCCEEDED(hr)) |
| 199 | { |
| 200 | /* Remove 4 for the last header, which should hopefully work */ |
| 201 | IMediaSample *sample = NULL; |
| 202 | LONGLONG rtSampleStart = pin->rtNext - MEDIATIME_FROM_BYTES(4); |
| 203 | LONGLONG rtSampleStop = rtSampleStart + MEDIATIME_FROM_BYTES(length + 4); |
| 204 | |
Maarten Lankhorst | 3a39805 | 2008-04-22 13:57:11 -0700 | [diff] [blame] | 205 | if (rtSampleStop > pin->rtStop) |
| 206 | rtSampleStop = MEDIATIME_FROM_BYTES(ALIGNUP(BYTES_FROM_MEDIATIME(pin->rtStop), pin->cbAlign)); |
| 207 | |
Juan Lang | e73f31a | 2009-08-27 09:04:50 -0700 | [diff] [blame] | 208 | hr = IMemAllocator_GetBuffer(pin->pAlloc, &sample, NULL, NULL, 0); |
Maarten Lankhorst | 3a39805 | 2008-04-22 13:57:11 -0700 | [diff] [blame] | 209 | if (SUCCEEDED(hr)) |
Juan Lang | e73f31a | 2009-08-27 09:04:50 -0700 | [diff] [blame] | 210 | { |
| 211 | IMediaSample_SetTime(sample, &rtSampleStart, &rtSampleStop); |
| 212 | IMediaSample_SetPreroll(sample, 0); |
| 213 | IMediaSample_SetDiscontinuity(sample, 0); |
| 214 | IMediaSample_SetSyncPoint(sample, 1); |
| 215 | pin->rtCurrent = rtSampleStart; |
| 216 | pin->rtNext = rtSampleStop; |
Maarten Lankhorst | 3a39805 | 2008-04-22 13:57:11 -0700 | [diff] [blame] | 217 | hr = IAsyncReader_Request(pin->pReader, sample, 0); |
Juan Lang | e73f31a | 2009-08-27 09:04:50 -0700 | [diff] [blame] | 218 | } |
Maarten Lankhorst | 3a39805 | 2008-04-22 13:57:11 -0700 | [diff] [blame] | 219 | if (FAILED(hr)) |
| 220 | FIXME("o_Ox%08x\n", hr); |
| 221 | } |
Maarten Lankhorst | b0c6a34 | 2008-03-20 22:33:47 -0700 | [diff] [blame] | 222 | } |
Maarten Lankhorst | 3a39805 | 2008-04-22 13:57:11 -0700 | [diff] [blame] | 223 | /* If not, we're presumably at the end of file */ |
| 224 | |
| 225 | TRACE("Media time : %u.%03u\n", (DWORD)(This->position/10000000), (DWORD)((This->position/10000)%1000)); |
Maarten Lankhorst | b0c6a34 | 2008-03-20 22:33:47 -0700 | [diff] [blame] | 226 | |
Maarten Lankhorst | b1b7524 | 2008-04-01 14:42:32 -0700 | [diff] [blame] | 227 | IMediaSample_SetTime(pCurrentSample, &time, &This->position); |
Maarten Lankhorst | b0c6a34 | 2008-03-20 22:33:47 -0700 | [diff] [blame] | 228 | |
Aric Stewart | 5c1409b | 2010-10-05 14:37:56 -0500 | [diff] [blame] | 229 | hr = BaseOutputPinImpl_Deliver((BaseOutputPin*)&pOutputPin->pin, pCurrentSample); |
Maarten Lankhorst | b1b7524 | 2008-04-01 14:42:32 -0700 | [diff] [blame] | 230 | |
Maarten Lankhorst | 4bc4457 | 2008-04-07 16:11:36 -0700 | [diff] [blame] | 231 | if (hr != S_OK) |
Maarten Lankhorst | b1b7524 | 2008-04-01 14:42:32 -0700 | [diff] [blame] | 232 | { |
Maarten Lankhorst | 4bc4457 | 2008-04-07 16:11:36 -0700 | [diff] [blame] | 233 | if (hr != S_FALSE) |
| 234 | TRACE("Error sending sample (%x)\n", hr); |
| 235 | else |
Maarten Lankhorst | 3a39805 | 2008-04-22 13:57:11 -0700 | [diff] [blame] | 236 | TRACE("S_FALSE (%d), holding\n", IMediaSample_GetActualDataLength(pCurrentSample)); |
Maarten Lankhorst | b1b7524 | 2008-04-01 14:42:32 -0700 | [diff] [blame] | 237 | } |
Maarten Lankhorst | 4bc4457 | 2008-04-07 16:11:36 -0700 | [diff] [blame] | 238 | |
Maarten Lankhorst | b0c6a34 | 2008-03-20 22:33:47 -0700 | [diff] [blame] | 239 | return hr; |
| 240 | } |
| 241 | |
Chris Robinson | b3fab14 | 2007-04-24 05:28:14 -0700 | [diff] [blame] | 242 | |
Maarten Lankhorst | a022467 | 2008-04-18 19:30:25 -0700 | [diff] [blame] | 243 | static HRESULT MPEGSplitter_process_sample(LPVOID iface, IMediaSample * pSample, DWORD_PTR cookie) |
Chris Robinson | b3fab14 | 2007-04-24 05:28:14 -0700 | [diff] [blame] | 244 | { |
Michael Stefaniuc | cc7fc4a | 2009-01-29 11:14:55 +0100 | [diff] [blame] | 245 | MPEGSplitterImpl *This = iface; |
Maarten Lankhorst | b0c6a34 | 2008-03-20 22:33:47 -0700 | [diff] [blame] | 246 | BYTE *pbSrcStream; |
Chris Robinson | b3fab14 | 2007-04-24 05:28:14 -0700 | [diff] [blame] | 247 | DWORD cbSrcStream = 0; |
Maarten Lankhorst | 98f0b5f | 2008-05-09 16:38:29 -0700 | [diff] [blame] | 248 | REFERENCE_TIME tStart, tStop, tAviStart = This->position; |
Maarten Lankhorst | b0c6a34 | 2008-03-20 22:33:47 -0700 | [diff] [blame] | 249 | HRESULT hr; |
Chris Robinson | b3fab14 | 2007-04-24 05:28:14 -0700 | [diff] [blame] | 250 | |
Chris Robinson | b3fab14 | 2007-04-24 05:28:14 -0700 | [diff] [blame] | 251 | hr = IMediaSample_GetTime(pSample, &tStart, &tStop); |
| 252 | if (SUCCEEDED(hr)) |
| 253 | { |
| 254 | cbSrcStream = IMediaSample_GetActualDataLength(pSample); |
| 255 | hr = IMediaSample_GetPointer(pSample, &pbSrcStream); |
| 256 | } |
| 257 | |
Francois Gouget | 633ee95 | 2008-05-06 20:01:59 +0200 | [diff] [blame] | 258 | /* Flush occurring */ |
Maarten Lankhorst | 3a39805 | 2008-04-22 13:57:11 -0700 | [diff] [blame] | 259 | if (cbSrcStream == 0) |
| 260 | { |
| 261 | FIXME(".. Why do I need you?\n"); |
| 262 | return S_OK; |
| 263 | } |
| 264 | |
Chris Robinson | b3fab14 | 2007-04-24 05:28:14 -0700 | [diff] [blame] | 265 | /* trace removed for performance reasons */ |
| 266 | /* TRACE("(%p), %llu -> %llu\n", pSample, tStart, tStop); */ |
| 267 | |
Maarten Lankhorst | b0c6a34 | 2008-03-20 22:33:47 -0700 | [diff] [blame] | 268 | /* Now, try to find a new header */ |
Maarten Lankhorst | 3a39805 | 2008-04-22 13:57:11 -0700 | [diff] [blame] | 269 | hr = FillBuffer(This, pSample); |
| 270 | if (hr != S_OK) |
Chris Robinson | b3fab14 | 2007-04-24 05:28:14 -0700 | [diff] [blame] | 271 | { |
Maarten Lankhorst | 3a39805 | 2008-04-22 13:57:11 -0700 | [diff] [blame] | 272 | WARN("Failed with hres: %08x!\n", hr); |
Maarten Lankhorst | 2734e63 | 2008-04-09 12:07:14 -0700 | [diff] [blame] | 273 | |
Maarten Lankhorst | 0f73bba | 2008-04-30 14:34:07 -0700 | [diff] [blame] | 274 | /* Unset progression if denied! */ |
| 275 | if (hr == VFW_E_WRONG_STATE || hr == S_FALSE) |
Maarten Lankhorst | 714e807 | 2008-03-28 13:10:20 -0700 | [diff] [blame] | 276 | { |
Maarten Lankhorst | 0f73bba | 2008-04-30 14:34:07 -0700 | [diff] [blame] | 277 | memcpy(This->header, pbSrcStream, 4); |
| 278 | This->Parser.pInputPin->rtCurrent = tStart; |
Maarten Lankhorst | 98f0b5f | 2008-05-09 16:38:29 -0700 | [diff] [blame] | 279 | This->position = tAviStart; |
Maarten Lankhorst | 714e807 | 2008-03-28 13:10:20 -0700 | [diff] [blame] | 280 | } |
Chris Robinson | b3fab14 | 2007-04-24 05:28:14 -0700 | [diff] [blame] | 281 | } |
| 282 | |
Maarten Lankhorst | 2734e63 | 2008-04-09 12:07:14 -0700 | [diff] [blame] | 283 | if (BYTES_FROM_MEDIATIME(tStop) >= This->EndOfFile || This->position >= This->Parser.mediaSeeking.llStop) |
Chris Robinson | b3fab14 | 2007-04-24 05:28:14 -0700 | [diff] [blame] | 284 | { |
Andrew Talbot | a19ff5f | 2008-11-04 22:22:53 +0000 | [diff] [blame] | 285 | unsigned int i; |
Chris Robinson | b3fab14 | 2007-04-24 05:28:14 -0700 | [diff] [blame] | 286 | |
Maarten Lankhorst | b0c6a34 | 2008-03-20 22:33:47 -0700 | [diff] [blame] | 287 | TRACE("End of file reached\n"); |
Chris Robinson | b3fab14 | 2007-04-24 05:28:14 -0700 | [diff] [blame] | 288 | |
Chris Robinson | b3fab14 | 2007-04-24 05:28:14 -0700 | [diff] [blame] | 289 | for (i = 0; i < This->Parser.cStreams; i++) |
| 290 | { |
| 291 | IPin* ppin; |
Chris Robinson | b3fab14 | 2007-04-24 05:28:14 -0700 | [diff] [blame] | 292 | |
| 293 | hr = IPin_ConnectedTo(This->Parser.ppPins[i+1], &ppin); |
| 294 | if (SUCCEEDED(hr)) |
| 295 | { |
| 296 | hr = IPin_EndOfStream(ppin); |
| 297 | IPin_Release(ppin); |
| 298 | } |
| 299 | if (FAILED(hr)) |
Andrew Talbot | a19ff5f | 2008-11-04 22:22:53 +0000 | [diff] [blame] | 300 | WARN("Error sending EndOfStream to pin %u (%x)\n", i, hr); |
Chris Robinson | b3fab14 | 2007-04-24 05:28:14 -0700 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | /* Force the pullpin thread to stop */ |
| 304 | hr = S_FALSE; |
| 305 | } |
| 306 | |
| 307 | return hr; |
| 308 | } |
| 309 | |
| 310 | |
| 311 | static HRESULT MPEGSplitter_query_accept(LPVOID iface, const AM_MEDIA_TYPE *pmt) |
| 312 | { |
| 313 | if (!IsEqualIID(&pmt->majortype, &MEDIATYPE_Stream)) |
| 314 | return S_FALSE; |
| 315 | |
| 316 | if (IsEqualIID(&pmt->subtype, &MEDIASUBTYPE_MPEG1Audio)) |
| 317 | return S_OK; |
| 318 | |
| 319 | if (IsEqualIID(&pmt->subtype, &MEDIASUBTYPE_MPEG1Video)) |
| 320 | FIXME("MPEG-1 video streams not yet supported.\n"); |
| 321 | else if (IsEqualIID(&pmt->subtype, &MEDIASUBTYPE_MPEG1System)) |
| 322 | FIXME("MPEG-1 system streams not yet supported.\n"); |
| 323 | else if (IsEqualIID(&pmt->subtype, &MEDIASUBTYPE_MPEG1VideoCD)) |
| 324 | FIXME("MPEG-1 VideoCD streams not yet supported.\n"); |
| 325 | |
| 326 | return S_FALSE; |
| 327 | } |
| 328 | |
| 329 | |
Chris Robinson | b3fab14 | 2007-04-24 05:28:14 -0700 | [diff] [blame] | 330 | static HRESULT MPEGSplitter_init_audio(MPEGSplitterImpl *This, const BYTE *header, PIN_INFO *ppiOutput, AM_MEDIA_TYPE *pamt) |
| 331 | { |
Chris Robinson | b3fab14 | 2007-04-24 05:28:14 -0700 | [diff] [blame] | 332 | WAVEFORMATEX *format; |
| 333 | int bitrate_index; |
| 334 | int freq_index; |
| 335 | int mode_ext; |
| 336 | int emphasis; |
Chris Robinson | b3fab14 | 2007-04-24 05:28:14 -0700 | [diff] [blame] | 337 | int lsf = 1; |
| 338 | int mpeg1; |
| 339 | int layer; |
| 340 | int mode; |
| 341 | |
| 342 | ZeroMemory(pamt, sizeof(*pamt)); |
| 343 | ppiOutput->dir = PINDIR_OUTPUT; |
| 344 | ppiOutput->pFilter = (IBaseFilter*)This; |
| 345 | wsprintfW(ppiOutput->achName, wszAudioStream); |
| 346 | |
Andrew Talbot | 442f29a | 2008-03-12 20:32:07 +0000 | [diff] [blame] | 347 | pamt->formattype = FORMAT_WaveFormatEx; |
| 348 | pamt->majortype = MEDIATYPE_Audio; |
| 349 | pamt->subtype = MEDIASUBTYPE_MPEG1AudioPayload; |
Chris Robinson | b3fab14 | 2007-04-24 05:28:14 -0700 | [diff] [blame] | 350 | |
| 351 | pamt->lSampleSize = 0; |
Maarten Lankhorst | b0c6a34 | 2008-03-20 22:33:47 -0700 | [diff] [blame] | 352 | pamt->bFixedSizeSamples = FALSE; |
Chris Robinson | b3fab14 | 2007-04-24 05:28:14 -0700 | [diff] [blame] | 353 | pamt->bTemporalCompression = 0; |
| 354 | |
| 355 | mpeg1 = (header[1]>>4)&0x1; |
| 356 | if (mpeg1) |
| 357 | lsf = ((header[1]>>3)&0x1)^1; |
| 358 | |
| 359 | layer = 4-((header[1]>>1)&0x3); |
| 360 | bitrate_index = ((header[2]>>4)&0xf); |
| 361 | freq_index = ((header[2]>>2)&0x3) + (mpeg1?(lsf*3):6); |
Chris Robinson | b3fab14 | 2007-04-24 05:28:14 -0700 | [diff] [blame] | 362 | mode = ((header[3]>>6)&0x3); |
| 363 | mode_ext = ((header[3]>>4)&0x3); |
| 364 | emphasis = ((header[3]>>0)&0x3); |
| 365 | |
Maarten Lankhorst | b0c6a34 | 2008-03-20 22:33:47 -0700 | [diff] [blame] | 366 | if (!bitrate_index) |
| 367 | { |
| 368 | /* Set to highest bitrate so samples will fit in for sure */ |
| 369 | FIXME("Variable-bitrate audio not fully supported.\n"); |
| 370 | bitrate_index = 15; |
| 371 | } |
| 372 | |
Chris Robinson | b3fab14 | 2007-04-24 05:28:14 -0700 | [diff] [blame] | 373 | pamt->cbFormat = ((layer==3)? sizeof(MPEGLAYER3WAVEFORMAT) : |
| 374 | sizeof(MPEG1WAVEFORMAT)); |
| 375 | pamt->pbFormat = CoTaskMemAlloc(pamt->cbFormat); |
| 376 | if (!pamt->pbFormat) |
| 377 | return E_OUTOFMEMORY; |
| 378 | ZeroMemory(pamt->pbFormat, pamt->cbFormat); |
| 379 | format = (WAVEFORMATEX*)pamt->pbFormat; |
| 380 | |
| 381 | format->wFormatTag = ((layer == 3) ? WAVE_FORMAT_MPEGLAYER3 : |
| 382 | WAVE_FORMAT_MPEG); |
| 383 | format->nChannels = ((mode == 3) ? 1 : 2); |
| 384 | format->nSamplesPerSec = freqs[freq_index]; |
| 385 | format->nAvgBytesPerSec = tabsel_123[lsf][layer-1][bitrate_index] * 1000 / 8; |
Chris Robinson | b3fab14 | 2007-04-24 05:28:14 -0700 | [diff] [blame] | 386 | |
| 387 | if (layer == 3) |
| 388 | format->nBlockAlign = format->nAvgBytesPerSec * 8 * 144 / |
Maarten Lankhorst | b0c6a34 | 2008-03-20 22:33:47 -0700 | [diff] [blame] | 389 | (format->nSamplesPerSec<<lsf) + 1; |
Chris Robinson | b3fab14 | 2007-04-24 05:28:14 -0700 | [diff] [blame] | 390 | else if (layer == 2) |
| 391 | format->nBlockAlign = format->nAvgBytesPerSec * 8 * 144 / |
Maarten Lankhorst | b0c6a34 | 2008-03-20 22:33:47 -0700 | [diff] [blame] | 392 | format->nSamplesPerSec + 1; |
Chris Robinson | b3fab14 | 2007-04-24 05:28:14 -0700 | [diff] [blame] | 393 | else |
Maarten Lankhorst | b0c6a34 | 2008-03-20 22:33:47 -0700 | [diff] [blame] | 394 | format->nBlockAlign = 4 * (format->nAvgBytesPerSec * 8 * 12 / format->nSamplesPerSec + 1); |
Chris Robinson | b3fab14 | 2007-04-24 05:28:14 -0700 | [diff] [blame] | 395 | |
| 396 | format->wBitsPerSample = 0; |
| 397 | |
| 398 | if (layer == 3) |
| 399 | { |
| 400 | MPEGLAYER3WAVEFORMAT *mp3format = (MPEGLAYER3WAVEFORMAT*)format; |
| 401 | |
| 402 | format->cbSize = MPEGLAYER3_WFX_EXTRA_BYTES; |
| 403 | |
| 404 | mp3format->wID = MPEGLAYER3_ID_MPEG; |
Maarten Lankhorst | b0c6a34 | 2008-03-20 22:33:47 -0700 | [diff] [blame] | 405 | mp3format->fdwFlags = MPEGLAYER3_FLAG_PADDING_ON; |
Chris Robinson | b3fab14 | 2007-04-24 05:28:14 -0700 | [diff] [blame] | 406 | mp3format->nBlockSize = format->nBlockAlign; |
| 407 | mp3format->nFramesPerBlock = 1; |
| 408 | |
| 409 | /* Beware the evil magic numbers. This struct is apparently horribly |
| 410 | * under-documented, and the only references I could find had it being |
| 411 | * set to this with no real explanation. It works fine though, so I'm |
| 412 | * not complaining (yet). |
| 413 | */ |
| 414 | mp3format->nCodecDelay = 1393; |
| 415 | } |
| 416 | else |
| 417 | { |
| 418 | MPEG1WAVEFORMAT *mpgformat = (MPEG1WAVEFORMAT*)format; |
| 419 | |
| 420 | format->cbSize = 22; |
| 421 | |
| 422 | mpgformat->fwHeadLayer = ((layer == 1) ? ACM_MPEG_LAYER1 : |
| 423 | ((layer == 2) ? ACM_MPEG_LAYER2 : |
| 424 | ACM_MPEG_LAYER3)); |
| 425 | mpgformat->dwHeadBitrate = format->nAvgBytesPerSec * 8; |
| 426 | mpgformat->fwHeadMode = ((mode == 3) ? ACM_MPEG_SINGLECHANNEL : |
| 427 | ((mode == 2) ? ACM_MPEG_DUALCHANNEL : |
| 428 | ((mode == 1) ? ACM_MPEG_JOINTSTEREO : |
| 429 | ACM_MPEG_STEREO))); |
| 430 | mpgformat->fwHeadModeExt = ((mode == 1) ? 0x0F : (1<<mode_ext)); |
| 431 | mpgformat->wHeadEmphasis = emphasis + 1; |
| 432 | mpgformat->fwHeadFlags = ACM_MPEG_ID_MPEG1; |
| 433 | } |
| 434 | pamt->subtype.Data1 = format->wFormatTag; |
| 435 | |
| 436 | TRACE("MPEG audio stream detected:\n" |
| 437 | "\tLayer %d (%#x)\n" |
| 438 | "\tFrequency: %d\n" |
| 439 | "\tChannels: %d (%d)\n" |
| 440 | "\tBytesPerSec: %d\n", |
| 441 | layer, format->wFormatTag, format->nSamplesPerSec, |
| 442 | format->nChannels, mode, format->nAvgBytesPerSec); |
| 443 | |
| 444 | dump_AM_MEDIA_TYPE(pamt); |
| 445 | |
| 446 | return S_OK; |
| 447 | } |
| 448 | |
| 449 | |
Maarten Lankhorst | 3a39805 | 2008-04-22 13:57:11 -0700 | [diff] [blame] | 450 | static HRESULT MPEGSplitter_pre_connect(IPin *iface, IPin *pConnectPin, ALLOCATOR_PROPERTIES *props) |
Chris Robinson | b3fab14 | 2007-04-24 05:28:14 -0700 | [diff] [blame] | 451 | { |
| 452 | PullPin *pPin = (PullPin *)iface; |
| 453 | MPEGSplitterImpl *This = (MPEGSplitterImpl*)pPin->pin.pinInfo.pFilter; |
Chris Robinson | b3fab14 | 2007-04-24 05:28:14 -0700 | [diff] [blame] | 454 | HRESULT hr; |
| 455 | LONGLONG pos = 0; /* in bytes */ |
Maarten Lankhorst | 6c1d089 | 2008-03-13 20:24:58 -0700 | [diff] [blame] | 456 | BYTE header[10]; |
Chris Robinson | b3fab14 | 2007-04-24 05:28:14 -0700 | [diff] [blame] | 457 | int streamtype = 0; |
| 458 | LONGLONG total, avail; |
| 459 | AM_MEDIA_TYPE amt; |
| 460 | PIN_INFO piOutput; |
| 461 | |
| 462 | IAsyncReader_Length(pPin->pReader, &total, &avail); |
| 463 | This->EndOfFile = total; |
| 464 | |
Maarten Lankhorst | 6c1d089 | 2008-03-13 20:24:58 -0700 | [diff] [blame] | 465 | hr = IAsyncReader_SyncRead(pPin->pReader, pos, 4, header); |
Chris Robinson | b3fab14 | 2007-04-24 05:28:14 -0700 | [diff] [blame] | 466 | if (SUCCEEDED(hr)) |
| 467 | pos += 4; |
| 468 | |
Maarten Lankhorst | 6c1d089 | 2008-03-13 20:24:58 -0700 | [diff] [blame] | 469 | /* Skip ID3 v2 tag, if any */ |
Rob Shearman | a35431a | 2008-08-17 18:33:17 +0100 | [diff] [blame] | 470 | if (SUCCEEDED(hr) && !memcmp("ID3", header, 3)) |
Maarten Lankhorst | 6c1d089 | 2008-03-13 20:24:58 -0700 | [diff] [blame] | 471 | do { |
| 472 | UINT length; |
| 473 | hr = IAsyncReader_SyncRead(pPin->pReader, pos, 6, header + 4); |
| 474 | if (FAILED(hr)) |
| 475 | break; |
| 476 | pos += 6; |
| 477 | TRACE("Found ID3 v2.%d.%d\n", header[3], header[4]); |
| 478 | length = (header[6] & 0x7F) << 21; |
| 479 | length += (header[7] & 0x7F) << 14; |
| 480 | length += (header[8] & 0x7F) << 7; |
| 481 | length += (header[9] & 0x7F); |
| 482 | TRACE("Length: %u\n", length); |
| 483 | pos += length; |
| 484 | |
| 485 | /* Read the real header for the mpeg splitter */ |
| 486 | hr = IAsyncReader_SyncRead(pPin->pReader, pos, 4, header); |
| 487 | if (SUCCEEDED(hr)) |
| 488 | pos += 4; |
| 489 | TRACE("%x:%x:%x:%x\n", header[0], header[1], header[2], header[3]); |
| 490 | } while (0); |
| 491 | |
Chris Robinson | b3fab14 | 2007-04-24 05:28:14 -0700 | [diff] [blame] | 492 | while(SUCCEEDED(hr) && !(streamtype=MPEGSplitter_head_check(header))) |
| 493 | { |
Maarten Lankhorst | 6c1d089 | 2008-03-13 20:24:58 -0700 | [diff] [blame] | 494 | TRACE("%x:%x:%x:%x\n", header[0], header[1], header[2], header[3]); |
Chris Robinson | b3fab14 | 2007-04-24 05:28:14 -0700 | [diff] [blame] | 495 | /* No valid header yet; shift by a byte and check again */ |
Maarten Lankhorst | b0c6a34 | 2008-03-20 22:33:47 -0700 | [diff] [blame] | 496 | memmove(header, header+1, 3); |
Maarten Lankhorst | 6c1d089 | 2008-03-13 20:24:58 -0700 | [diff] [blame] | 497 | hr = IAsyncReader_SyncRead(pPin->pReader, pos++, 1, header + 3); |
Chris Robinson | b3fab14 | 2007-04-24 05:28:14 -0700 | [diff] [blame] | 498 | } |
| 499 | if (FAILED(hr)) |
| 500 | return hr; |
Maarten Lankhorst | b0c6a34 | 2008-03-20 22:33:47 -0700 | [diff] [blame] | 501 | pos -= 4; |
Maarten Lankhorst | 3a39805 | 2008-04-22 13:57:11 -0700 | [diff] [blame] | 502 | This->begin_offset = pos; |
| 503 | memcpy(This->header, header, 4); |
Chris Robinson | b3fab14 | 2007-04-24 05:28:14 -0700 | [diff] [blame] | 504 | |
Maarten Lankhorst | 24cac93 | 2008-04-08 12:28:09 -0700 | [diff] [blame] | 505 | This->seektable[0].bytepos = pos; |
| 506 | This->seektable[0].timepos = 0; |
| 507 | |
Chris Robinson | b3fab14 | 2007-04-24 05:28:14 -0700 | [diff] [blame] | 508 | switch(streamtype) |
| 509 | { |
| 510 | case MPEG_AUDIO_HEADER: |
Maarten Lankhorst | b0c6a34 | 2008-03-20 22:33:47 -0700 | [diff] [blame] | 511 | { |
| 512 | LONGLONG duration = 0; |
Maarten Lankhorst | 24cac93 | 2008-04-08 12:28:09 -0700 | [diff] [blame] | 513 | DWORD last_entry = 0; |
| 514 | |
Maarten Lankhorst | b0c6a34 | 2008-03-20 22:33:47 -0700 | [diff] [blame] | 515 | DWORD ticks = GetTickCount(); |
| 516 | |
Chris Robinson | b3fab14 | 2007-04-24 05:28:14 -0700 | [diff] [blame] | 517 | hr = MPEGSplitter_init_audio(This, header, &piOutput, &amt); |
| 518 | if (SUCCEEDED(hr)) |
| 519 | { |
| 520 | WAVEFORMATEX *format = (WAVEFORMATEX*)amt.pbFormat; |
| 521 | |
Maarten Lankhorst | 3a39805 | 2008-04-22 13:57:11 -0700 | [diff] [blame] | 522 | props->cbAlign = 1; |
| 523 | props->cbPrefix = 0; |
Chris Robinson | b3fab14 | 2007-04-24 05:28:14 -0700 | [diff] [blame] | 524 | /* Make the output buffer a multiple of the frame size */ |
Maarten Lankhorst | 3a39805 | 2008-04-22 13:57:11 -0700 | [diff] [blame] | 525 | props->cbBuffer = 0x4000 / format->nBlockAlign * |
Chris Robinson | b3fab14 | 2007-04-24 05:28:14 -0700 | [diff] [blame] | 526 | format->nBlockAlign; |
Maarten Lankhorst | 0f73bba | 2008-04-30 14:34:07 -0700 | [diff] [blame] | 527 | props->cBuffers = 3; |
Maarten Lankhorst | 3a39805 | 2008-04-22 13:57:11 -0700 | [diff] [blame] | 528 | hr = Parser_AddPin(&(This->Parser), &piOutput, props, &amt); |
Chris Robinson | b3fab14 | 2007-04-24 05:28:14 -0700 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | if (FAILED(hr)) |
| 532 | { |
| 533 | if (amt.pbFormat) |
| 534 | CoTaskMemFree(amt.pbFormat); |
| 535 | ERR("Could not create pin for MPEG audio stream (%x)\n", hr); |
Maarten Lankhorst | b0c6a34 | 2008-03-20 22:33:47 -0700 | [diff] [blame] | 536 | break; |
Chris Robinson | b3fab14 | 2007-04-24 05:28:14 -0700 | [diff] [blame] | 537 | } |
Maarten Lankhorst | b0c6a34 | 2008-03-20 22:33:47 -0700 | [diff] [blame] | 538 | |
| 539 | /* Check for idv1 tag, and remove it from stream if found */ |
| 540 | hr = IAsyncReader_SyncRead(pPin->pReader, This->EndOfFile-128, 3, header+4); |
| 541 | if (FAILED(hr)) |
| 542 | break; |
| 543 | if (!strncmp((char*)header+4, "TAG", 3)) |
| 544 | This->EndOfFile -= 128; |
Maarten Lankhorst | 4bc4457 | 2008-04-07 16:11:36 -0700 | [diff] [blame] | 545 | This->Parser.pInputPin->rtStop = MEDIATIME_FROM_BYTES(This->EndOfFile); |
Maarten Lankhorst | 3a39805 | 2008-04-22 13:57:11 -0700 | [diff] [blame] | 546 | This->Parser.pInputPin->rtStart = This->Parser.pInputPin->rtCurrent = MEDIATIME_FROM_BYTES(This->begin_offset); |
Maarten Lankhorst | b0c6a34 | 2008-03-20 22:33:47 -0700 | [diff] [blame] | 547 | |
Austin English | 313a85f | 2008-04-10 21:17:14 -0500 | [diff] [blame] | 548 | /* http://mpgedit.org/mpgedit/mpeg_format/mpeghdr.htm has a whole read up on audio headers */ |
Maarten Lankhorst | 6fd90f1 | 2008-03-30 21:18:49 -0700 | [diff] [blame] | 549 | while (pos + 3 < This->EndOfFile) |
Maarten Lankhorst | b0c6a34 | 2008-03-20 22:33:47 -0700 | [diff] [blame] | 550 | { |
| 551 | LONGLONG length = 0; |
Maarten Lankhorst | 714e807 | 2008-03-28 13:10:20 -0700 | [diff] [blame] | 552 | hr = IAsyncReader_SyncRead(pPin->pReader, pos, 4, header); |
Maarten Lankhorst | 6fd90f1 | 2008-03-30 21:18:49 -0700 | [diff] [blame] | 553 | if (hr != S_OK) |
| 554 | break; |
Maarten Lankhorst | b0c6a34 | 2008-03-20 22:33:47 -0700 | [diff] [blame] | 555 | while (parse_header(header, &length, &duration)) |
| 556 | { |
| 557 | /* No valid header yet; shift by a byte and check again */ |
| 558 | memmove(header, header+1, 3); |
| 559 | hr = IAsyncReader_SyncRead(pPin->pReader, pos++, 1, header + 3); |
Maarten Lankhorst | 6fd90f1 | 2008-03-30 21:18:49 -0700 | [diff] [blame] | 560 | if (hr != S_OK || This->EndOfFile - pos < 4) |
Maarten Lankhorst | b0c6a34 | 2008-03-20 22:33:47 -0700 | [diff] [blame] | 561 | break; |
| 562 | } |
Maarten Lankhorst | 714e807 | 2008-03-28 13:10:20 -0700 | [diff] [blame] | 563 | pos += length; |
Maarten Lankhorst | 24cac93 | 2008-04-08 12:28:09 -0700 | [diff] [blame] | 564 | |
| 565 | if (This->seektable && (duration / SEEK_INTERVAL) > last_entry) |
| 566 | { |
| 567 | if (last_entry + 1 > duration / SEEK_INTERVAL) |
| 568 | { |
| 569 | ERR("Somehow skipped %d interval lengths instead of 1\n", (DWORD)(duration/SEEK_INTERVAL) - (last_entry + 1)); |
| 570 | } |
| 571 | ++last_entry; |
| 572 | |
| 573 | TRACE("Entry: %u\n", last_entry); |
| 574 | if (last_entry >= This->seek_entries) |
| 575 | { |
| 576 | This->seek_entries += 64; |
| 577 | This->seektable = CoTaskMemRealloc(This->seektable, (This->seek_entries)*sizeof(struct seek_entry)); |
| 578 | } |
| 579 | This->seektable[last_entry].bytepos = pos; |
| 580 | This->seektable[last_entry].timepos = duration; |
| 581 | } |
| 582 | |
Maarten Lankhorst | 714e807 | 2008-03-28 13:10:20 -0700 | [diff] [blame] | 583 | TRACE("Pos: %x%08x/%x%08x\n", (DWORD)(pos >> 32), (DWORD)pos, (DWORD)(This->EndOfFile>>32), (DWORD)This->EndOfFile); |
Maarten Lankhorst | b0c6a34 | 2008-03-20 22:33:47 -0700 | [diff] [blame] | 584 | } |
| 585 | hr = S_OK; |
Alexandre Julliard | 6af76b3 | 2008-03-26 18:01:09 +0100 | [diff] [blame] | 586 | TRACE("Duration: %d seconds\n", (DWORD)(duration / 10000000)); |
Maarten Lankhorst | b0c6a34 | 2008-03-20 22:33:47 -0700 | [diff] [blame] | 587 | TRACE("Parsing took %u ms\n", GetTickCount() - ticks); |
Maarten Lankhorst | b1b7524 | 2008-04-01 14:42:32 -0700 | [diff] [blame] | 588 | This->duration = duration; |
Maarten Lankhorst | 2734e63 | 2008-04-09 12:07:14 -0700 | [diff] [blame] | 589 | |
| 590 | This->Parser.mediaSeeking.llCurrent = 0; |
| 591 | This->Parser.mediaSeeking.llDuration = duration; |
| 592 | This->Parser.mediaSeeking.llStop = duration; |
Chris Robinson | b3fab14 | 2007-04-24 05:28:14 -0700 | [diff] [blame] | 593 | break; |
Maarten Lankhorst | b0c6a34 | 2008-03-20 22:33:47 -0700 | [diff] [blame] | 594 | } |
Chris Robinson | b3fab14 | 2007-04-24 05:28:14 -0700 | [diff] [blame] | 595 | case MPEG_VIDEO_HEADER: |
| 596 | FIXME("MPEG video processing not yet supported!\n"); |
| 597 | hr = E_FAIL; |
| 598 | break; |
| 599 | case MPEG_SYSTEM_HEADER: |
| 600 | FIXME("MPEG system streams not yet supported!\n"); |
| 601 | hr = E_FAIL; |
| 602 | break; |
| 603 | |
| 604 | default: |
| 605 | break; |
| 606 | } |
Maarten Lankhorst | b0c6a34 | 2008-03-20 22:33:47 -0700 | [diff] [blame] | 607 | This->position = 0; |
Chris Robinson | b3fab14 | 2007-04-24 05:28:14 -0700 | [diff] [blame] | 608 | |
| 609 | return hr; |
| 610 | } |
| 611 | |
| 612 | static HRESULT MPEGSplitter_cleanup(LPVOID iface) |
| 613 | { |
Michael Stefaniuc | cc7fc4a | 2009-01-29 11:14:55 +0100 | [diff] [blame] | 614 | MPEGSplitterImpl *This = iface; |
Chris Robinson | b3fab14 | 2007-04-24 05:28:14 -0700 | [diff] [blame] | 615 | |
Maarten Lankhorst | 0f73bba | 2008-04-30 14:34:07 -0700 | [diff] [blame] | 616 | TRACE("(%p)\n", This); |
Chris Robinson | b3fab14 | 2007-04-24 05:28:14 -0700 | [diff] [blame] | 617 | |
| 618 | return S_OK; |
| 619 | } |
| 620 | |
Maarten Lankhorst | 553fe85 | 2008-04-01 14:43:09 -0700 | [diff] [blame] | 621 | static HRESULT MPEGSplitter_seek(IBaseFilter *iface) |
| 622 | { |
| 623 | MPEGSplitterImpl *This = (MPEGSplitterImpl*)iface; |
| 624 | PullPin *pPin = This->Parser.pInputPin; |
| 625 | LONGLONG newpos, timepos, bytepos; |
| 626 | HRESULT hr = S_OK; |
| 627 | BYTE header[4]; |
| 628 | |
Maarten Lankhorst | 553fe85 | 2008-04-01 14:43:09 -0700 | [diff] [blame] | 629 | newpos = This->Parser.mediaSeeking.llCurrent; |
| 630 | |
| 631 | if (newpos > This->duration) |
| 632 | { |
Maarten Lankhorst | 4bc4457 | 2008-04-07 16:11:36 -0700 | [diff] [blame] | 633 | WARN("Requesting position %x%08x beyond end of stream %x%08x\n", (DWORD)(newpos>>32), (DWORD)newpos, (DWORD)(This->duration>>32), (DWORD)This->duration); |
Maarten Lankhorst | 553fe85 | 2008-04-01 14:43:09 -0700 | [diff] [blame] | 634 | return E_INVALIDARG; |
| 635 | } |
| 636 | |
| 637 | if (This->position/1000000 == newpos/1000000) |
| 638 | { |
Maarten Lankhorst | 4bc4457 | 2008-04-07 16:11:36 -0700 | [diff] [blame] | 639 | TRACE("Requesting position %x%08x same as current position %x%08x\n", (DWORD)(newpos>>32), (DWORD)newpos, (DWORD)(This->position>>32), (DWORD)This->position); |
Maarten Lankhorst | 553fe85 | 2008-04-01 14:43:09 -0700 | [diff] [blame] | 640 | return S_OK; |
| 641 | } |
| 642 | |
Maarten Lankhorst | 24cac93 | 2008-04-08 12:28:09 -0700 | [diff] [blame] | 643 | /* Position, cached */ |
| 644 | bytepos = This->seektable[newpos / SEEK_INTERVAL].bytepos; |
| 645 | timepos = This->seektable[newpos / SEEK_INTERVAL].timepos; |
| 646 | |
Maarten Lankhorst | 553fe85 | 2008-04-01 14:43:09 -0700 | [diff] [blame] | 647 | hr = IAsyncReader_SyncRead(pPin->pReader, bytepos, 4, header); |
Maarten Lankhorst | 3a39805 | 2008-04-22 13:57:11 -0700 | [diff] [blame] | 648 | while (bytepos + 3 < This->EndOfFile) |
Maarten Lankhorst | 553fe85 | 2008-04-01 14:43:09 -0700 | [diff] [blame] | 649 | { |
| 650 | LONGLONG length = 0; |
| 651 | hr = IAsyncReader_SyncRead(pPin->pReader, bytepos, 4, header); |
Maarten Lankhorst | 3a39805 | 2008-04-22 13:57:11 -0700 | [diff] [blame] | 652 | if (hr != S_OK || timepos >= newpos) |
Maarten Lankhorst | 24cac93 | 2008-04-08 12:28:09 -0700 | [diff] [blame] | 653 | break; |
| 654 | |
| 655 | while (parse_header(header, &length, &timepos) && bytepos + 3 < This->EndOfFile) |
Maarten Lankhorst | 553fe85 | 2008-04-01 14:43:09 -0700 | [diff] [blame] | 656 | { |
| 657 | /* No valid header yet; shift by a byte and check again */ |
| 658 | memmove(header, header+1, 3); |
| 659 | hr = IAsyncReader_SyncRead(pPin->pReader, ++bytepos, 1, header + 3); |
Maarten Lankhorst | 24cac93 | 2008-04-08 12:28:09 -0700 | [diff] [blame] | 660 | if (hr != S_OK) |
Maarten Lankhorst | 553fe85 | 2008-04-01 14:43:09 -0700 | [diff] [blame] | 661 | break; |
| 662 | } |
| 663 | bytepos += length; |
| 664 | TRACE("Pos: %x%08x/%x%08x\n", (DWORD)(bytepos >> 32), (DWORD)bytepos, (DWORD)(This->EndOfFile>>32), (DWORD)This->EndOfFile); |
Maarten Lankhorst | 553fe85 | 2008-04-01 14:43:09 -0700 | [diff] [blame] | 665 | } |
Maarten Lankhorst | 4bc4457 | 2008-04-07 16:11:36 -0700 | [diff] [blame] | 666 | |
Maarten Lankhorst | 553fe85 | 2008-04-01 14:43:09 -0700 | [diff] [blame] | 667 | if (SUCCEEDED(hr)) |
| 668 | { |
Maarten Lankhorst | 553fe85 | 2008-04-01 14:43:09 -0700 | [diff] [blame] | 669 | PullPin *pin = This->Parser.pInputPin; |
Maarten Lankhorst | 4bc4457 | 2008-04-07 16:11:36 -0700 | [diff] [blame] | 670 | IPin *victim = NULL; |
Maarten Lankhorst | 553fe85 | 2008-04-01 14:43:09 -0700 | [diff] [blame] | 671 | |
Maarten Lankhorst | 4bc4457 | 2008-04-07 16:11:36 -0700 | [diff] [blame] | 672 | TRACE("Moving sound to %08u bytes!\n", (DWORD)bytepos); |
Maarten Lankhorst | 553fe85 | 2008-04-01 14:43:09 -0700 | [diff] [blame] | 673 | |
Maarten Lankhorst | 2734e63 | 2008-04-09 12:07:14 -0700 | [diff] [blame] | 674 | EnterCriticalSection(&pin->thread_lock); |
Maarten Lankhorst | 553fe85 | 2008-04-01 14:43:09 -0700 | [diff] [blame] | 675 | IPin_BeginFlush((IPin *)pin); |
Maarten Lankhorst | 553fe85 | 2008-04-01 14:43:09 -0700 | [diff] [blame] | 676 | |
Maarten Lankhorst | 4bc4457 | 2008-04-07 16:11:36 -0700 | [diff] [blame] | 677 | /* Make sure this is done while stopped, BeginFlush takes care of this */ |
Aric Stewart | 1d42659 | 2010-10-07 14:47:33 -0500 | [diff] [blame^] | 678 | EnterCriticalSection(&This->Parser.filter.csFilter); |
Maarten Lankhorst | 3a39805 | 2008-04-22 13:57:11 -0700 | [diff] [blame] | 679 | memcpy(This->header, header, 4); |
Maarten Lankhorst | 4bc4457 | 2008-04-07 16:11:36 -0700 | [diff] [blame] | 680 | IPin_ConnectedTo(This->Parser.ppPins[1], &victim); |
| 681 | if (victim) |
| 682 | { |
| 683 | IPin_NewSegment(victim, newpos, This->duration, pin->dRate); |
| 684 | IPin_Release(victim); |
| 685 | } |
| 686 | |
Maarten Lankhorst | 553fe85 | 2008-04-01 14:43:09 -0700 | [diff] [blame] | 687 | pin->rtStart = pin->rtCurrent = MEDIATIME_FROM_BYTES(bytepos); |
| 688 | pin->rtStop = MEDIATIME_FROM_BYTES((REFERENCE_TIME)This->EndOfFile); |
| 689 | This->seek = TRUE; |
Maarten Lankhorst | 553fe85 | 2008-04-01 14:43:09 -0700 | [diff] [blame] | 690 | This->position = newpos; |
Aric Stewart | 1d42659 | 2010-10-07 14:47:33 -0500 | [diff] [blame^] | 691 | LeaveCriticalSection(&This->Parser.filter.csFilter); |
Maarten Lankhorst | 553fe85 | 2008-04-01 14:43:09 -0700 | [diff] [blame] | 692 | |
Maarten Lankhorst | 4bc4457 | 2008-04-07 16:11:36 -0700 | [diff] [blame] | 693 | TRACE("Done flushing\n"); |
| 694 | IPin_EndFlush((IPin *)pin); |
Maarten Lankhorst | 2734e63 | 2008-04-09 12:07:14 -0700 | [diff] [blame] | 695 | LeaveCriticalSection(&pin->thread_lock); |
Maarten Lankhorst | 553fe85 | 2008-04-01 14:43:09 -0700 | [diff] [blame] | 696 | } |
| 697 | return hr; |
| 698 | } |
Maarten Lankhorst | b1b7524 | 2008-04-01 14:42:32 -0700 | [diff] [blame] | 699 | |
Maarten Lankhorst | 3a39805 | 2008-04-22 13:57:11 -0700 | [diff] [blame] | 700 | static HRESULT MPEGSplitter_disconnect(LPVOID iface) |
Maarten Lankhorst | 6165d87 | 2008-04-15 20:39:36 -0700 | [diff] [blame] | 701 | { |
| 702 | /* TODO: Find memory leaks etc */ |
| 703 | return S_OK; |
| 704 | } |
| 705 | |
Maarten Lankhorst | 3a39805 | 2008-04-22 13:57:11 -0700 | [diff] [blame] | 706 | static HRESULT MPEGSplitter_first_request(LPVOID iface) |
| 707 | { |
Michael Stefaniuc | cc7fc4a | 2009-01-29 11:14:55 +0100 | [diff] [blame] | 708 | MPEGSplitterImpl *This = iface; |
Maarten Lankhorst | 3a39805 | 2008-04-22 13:57:11 -0700 | [diff] [blame] | 709 | PullPin *pin = This->Parser.pInputPin; |
| 710 | HRESULT hr; |
| 711 | LONGLONG length; |
| 712 | IMediaSample *sample; |
| 713 | |
| 714 | TRACE("Seeking? %d\n", This->seek); |
| 715 | assert(parse_header(This->header, &length, NULL) == S_OK); |
| 716 | |
| 717 | if (pin->rtCurrent >= pin->rtStop) |
| 718 | { |
| 719 | /* Last sample has already been queued, request nothing more */ |
| 720 | FIXME("Done!\n"); |
| 721 | return S_OK; |
| 722 | } |
| 723 | |
| 724 | hr = IMemAllocator_GetBuffer(pin->pAlloc, &sample, NULL, NULL, 0); |
| 725 | |
| 726 | pin->rtNext = pin->rtCurrent; |
| 727 | if (SUCCEEDED(hr)) |
| 728 | { |
| 729 | LONGLONG rtSampleStart = pin->rtNext; |
| 730 | /* Add 4 for the next header, which should hopefully work */ |
| 731 | LONGLONG rtSampleStop = rtSampleStart + MEDIATIME_FROM_BYTES(length + 4); |
| 732 | |
| 733 | if (rtSampleStop > pin->rtStop) |
| 734 | rtSampleStop = MEDIATIME_FROM_BYTES(ALIGNUP(BYTES_FROM_MEDIATIME(pin->rtStop), pin->cbAlign)); |
| 735 | |
| 736 | hr = IMediaSample_SetTime(sample, &rtSampleStart, &rtSampleStop); |
| 737 | |
| 738 | pin->rtCurrent = pin->rtNext; |
| 739 | pin->rtNext = rtSampleStop; |
| 740 | |
| 741 | IMediaSample_SetPreroll(sample, FALSE); |
| 742 | IMediaSample_SetDiscontinuity(sample, This->seek); |
| 743 | IMediaSample_SetSyncPoint(sample, 1); |
| 744 | This->seek = 0; |
| 745 | |
| 746 | hr = IAsyncReader_Request(pin->pReader, sample, 0); |
| 747 | } |
| 748 | if (FAILED(hr)) |
| 749 | ERR("Horsemen of the apocalypse came to bring error 0x%08x\n", hr); |
| 750 | |
| 751 | return hr; |
| 752 | } |
| 753 | |
Maarten Lankhorst | 1f136a5 | 2008-04-23 11:23:43 -0700 | [diff] [blame] | 754 | static const IBaseFilterVtbl MPEGSplitter_Vtbl = |
| 755 | { |
| 756 | Parser_QueryInterface, |
| 757 | Parser_AddRef, |
| 758 | Parser_Release, |
| 759 | Parser_GetClassID, |
| 760 | Parser_Stop, |
| 761 | Parser_Pause, |
| 762 | Parser_Run, |
| 763 | Parser_GetState, |
| 764 | Parser_SetSyncSource, |
| 765 | Parser_GetSyncSource, |
| 766 | Parser_EnumPins, |
| 767 | Parser_FindPin, |
| 768 | Parser_QueryFilterInfo, |
| 769 | Parser_JoinFilterGraph, |
| 770 | Parser_QueryVendorInfo |
| 771 | }; |
| 772 | |
Chris Robinson | b3fab14 | 2007-04-24 05:28:14 -0700 | [diff] [blame] | 773 | HRESULT MPEGSplitter_create(IUnknown * pUnkOuter, LPVOID * ppv) |
| 774 | { |
| 775 | MPEGSplitterImpl *This; |
| 776 | HRESULT hr = E_FAIL; |
| 777 | |
| 778 | TRACE("(%p, %p)\n", pUnkOuter, ppv); |
| 779 | |
| 780 | *ppv = NULL; |
| 781 | |
| 782 | if (pUnkOuter) |
| 783 | return CLASS_E_NOAGGREGATION; |
| 784 | |
| 785 | This = CoTaskMemAlloc(sizeof(MPEGSplitterImpl)); |
| 786 | if (!This) |
| 787 | return E_OUTOFMEMORY; |
| 788 | |
| 789 | ZeroMemory(This, sizeof(MPEGSplitterImpl)); |
Maarten Lankhorst | 24cac93 | 2008-04-08 12:28:09 -0700 | [diff] [blame] | 790 | This->seektable = CoTaskMemAlloc(sizeof(struct seek_entry) * 64); |
| 791 | if (!This->seektable) |
| 792 | { |
| 793 | CoTaskMemFree(This); |
| 794 | return E_OUTOFMEMORY; |
| 795 | } |
| 796 | This->seek_entries = 64; |
| 797 | |
Maarten Lankhorst | 512ee92 | 2008-04-25 14:59:05 -0700 | [diff] [blame] | 798 | hr = Parser_Create(&(This->Parser), &MPEGSplitter_Vtbl, &CLSID_MPEG1Splitter, MPEGSplitter_process_sample, MPEGSplitter_query_accept, MPEGSplitter_pre_connect, MPEGSplitter_cleanup, MPEGSplitter_disconnect, MPEGSplitter_first_request, NULL, NULL, MPEGSplitter_seek, NULL); |
Chris Robinson | b3fab14 | 2007-04-24 05:28:14 -0700 | [diff] [blame] | 799 | if (FAILED(hr)) |
| 800 | { |
| 801 | CoTaskMemFree(This); |
| 802 | return hr; |
| 803 | } |
Maarten Lankhorst | 3a39805 | 2008-04-22 13:57:11 -0700 | [diff] [blame] | 804 | This->seek = 1; |
Chris Robinson | b3fab14 | 2007-04-24 05:28:14 -0700 | [diff] [blame] | 805 | |
| 806 | /* Note: This memory is managed by the parser filter once created */ |
Michael Stefaniuc | cc7fc4a | 2009-01-29 11:14:55 +0100 | [diff] [blame] | 807 | *ppv = This; |
Chris Robinson | b3fab14 | 2007-04-24 05:28:14 -0700 | [diff] [blame] | 808 | |
| 809 | return hr; |
| 810 | } |