| 1 | /* BoCA - BonkEnc Component Architecture |
| 2 | * Copyright (C) 2007-2021 Robert Kausch <[email protected]> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License as |
| 6 | * published by the Free Software Foundation, either version 2 of |
| 7 | * the License, or (at your option) any later version. |
| 8 | * |
| 9 | * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR |
| 10 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED |
| 11 | * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. */ |
| 12 | |
| 13 | #include <smooth.h> |
| 14 | #include <smooth/dll.h> |
| 15 | |
| 16 | #include <smooth/io/drivers/driver_ansi.h> |
| 17 | |
| 18 | #include <mfapi.h> |
| 19 | #include <propvarutil.h> |
| 20 | |
| 21 | #include "mediafoundation.h" |
| 22 | #include "mfbytestream.h" |
| 23 | |
| 24 | using namespace smooth::IO; |
| 25 | |
| 26 | const String &BoCA::DecoderMediaFoundation::GetComponentSpecs() |
| 27 | { |
| 28 | static String componentSpecs; |
| 29 | |
| 30 | if (mfplatdll != NIL && mfreadwritedll != NIL) |
| 31 | { |
| 32 | componentSpecs = " \ |
| 33 | \ |
| 34 | <?xml version=\"1.0\" encoding=\"UTF-8\"?> \ |
| 35 | <component> \ |
| 36 | <name>Media Foundation Decoder</name> \ |
| 37 | <version>1.0</version> \ |
| 38 | <id>mediafoundation-dec</id> \ |
| 39 | <type>decoder</type> \ |
| 40 | <precede>wma-dec</precede> \ |
| 41 | <format> \ |
| 42 | <name>Windows Media Audio Files</name> \ |
| 43 | <extension>wma</extension> \ |
| 44 | <tag id=\"wma-tag\" mode=\"other\">WMA Metadata</tag> \ |
| 45 | </format> \ |
| 46 | </component> \ |
| 47 | \ |
| 48 | "; |
| 49 | } |
| 50 | |
| 51 | return componentSpecs; |
| 52 | } |
| 53 | |
| 54 | Void smooth::AttachDLL(Void *instance) |
| 55 | { |
| 56 | LoadMFPlatDLL(); |
| 57 | LoadMFReadWriteDLL(); |
| 58 | |
| 59 | /* Register initialization and cleanup handlers. |
| 60 | */ |
| 61 | if (mfplatdll != NIL && mfreadwritedll != NIL) |
| 62 | { |
| 63 | BoCA::Engine *engine = BoCA::Engine::Get(); |
| 64 | |
| 65 | engine->onInitialize.Connect(&BoCA::DecoderMediaFoundation::Initialize); |
| 66 | engine->onCleanup.Connect(&BoCA::DecoderMediaFoundation::Cleanup); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | Void smooth::DetachDLL() |
| 71 | { |
| 72 | /* Unregister initialization and cleanup handlers. |
| 73 | */ |
| 74 | if (mfplatdll != NIL && mfreadwritedll != NIL) |
| 75 | { |
| 76 | BoCA::Engine *engine = BoCA::Engine::Get(); |
| 77 | |
| 78 | engine->onInitialize.Disconnect(&BoCA::DecoderMediaFoundation::Initialize); |
| 79 | engine->onCleanup.Disconnect(&BoCA::DecoderMediaFoundation::Cleanup); |
| 80 | } |
| 81 | |
| 82 | FreeMFPlatDLL(); |
| 83 | FreeMFReadWriteDLL(); |
| 84 | } |
| 85 | |
| 86 | Void BoCA::DecoderMediaFoundation::Initialize() |
| 87 | { |
| 88 | /* Init COM and Media Foundation. |
| 89 | */ |
| 90 | CoInitialize(NIL); |
| 91 | |
| 92 | ex_MFStartup(MF_VERSION, MFSTARTUP_FULL); |
| 93 | } |
| 94 | |
| 95 | Void BoCA::DecoderMediaFoundation::Cleanup() |
| 96 | { |
| 97 | /* Uninit Media Foundation and COM. |
| 98 | */ |
| 99 | ex_MFShutdown(); |
| 100 | |
| 101 | CoUninitialize(); |
| 102 | } |
| 103 | |
| 104 | Bool BoCA::DecoderMediaFoundation::CanOpenStream(const String &streamURI) |
| 105 | { |
| 106 | InStream in(STREAM_FILE, streamURI, IS_READ); |
| 107 | UnsignedInt64 magic = in.InputNumber(8); |
| 108 | |
| 109 | return (magic == 0x11CF668E75B22630); |
| 110 | } |
| 111 | |
| 112 | Error BoCA::DecoderMediaFoundation::GetStreamInfo(const String &streamURI, Track &track) |
| 113 | { |
| 114 | DriverANSI driver(File(streamURI), IS_READ); |
| 115 | |
| 116 | /* Wrap stream in an IMFByteStream interface and create source reader. |
| 117 | */ |
| 118 | IMFByteStream *byteStream = new MFByteStream(&driver); |
| 119 | HRESULT hr = ex_MFCreateSourceReaderFromByteStream(byteStream, NULL, &reader); |
| 120 | |
| 121 | byteStream->Release(); |
| 122 | |
| 123 | if (FAILED(hr)) return Error(); |
| 124 | |
| 125 | /* Select first audio stream and get input media type. |
| 126 | */ |
| 127 | IMFMediaType *inputType = NULL; |
| 128 | |
| 129 | reader->SetStreamSelection(MF_SOURCE_READER_FIRST_AUDIO_STREAM, TRUE); |
| 130 | reader->GetNativeMediaType(MF_SOURCE_READER_FIRST_AUDIO_STREAM, 0, &inputType); |
| 131 | |
| 132 | /* See if this is a lossless file. |
| 133 | */ |
| 134 | GUID subtype; |
| 135 | |
| 136 | inputType->GetGUID(MF_MT_SUBTYPE, &subtype); |
| 137 | |
| 138 | if (subtype == MFAudioFormat_PCM || |
| 139 | subtype == MFAudioFormat_Float || |
| 140 | subtype == MFAudioFormat_WMAudio_Lossless) track.lossless = True; |
| 141 | else track.lossless = False; |
| 142 | |
| 143 | /* Fill format descriptor with input type. |
| 144 | */ |
| 145 | WAVEFORMATEX wfx = { }; |
| 146 | |
| 147 | wfx.wFormatTag = WAVE_FORMAT_PCM; |
| 148 | wfx.nChannels = MFGetAttributeUINT32(inputType, MF_MT_AUDIO_NUM_CHANNELS, 0); |
| 149 | wfx.nSamplesPerSec = MFGetAttributeUINT32(inputType, MF_MT_AUDIO_SAMPLES_PER_SECOND, 0); |
| 150 | wfx.wBitsPerSample = MFGetAttributeUINT32(inputType, MF_MT_AUDIO_BITS_PER_SAMPLE, 16); |
| 151 | wfx.nBlockAlign = wfx.nChannels * (wfx.wBitsPerSample / 8); |
| 152 | wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign; |
| 153 | |
| 154 | inputType->Release(); |
| 155 | |
| 156 | /* Create output media type. |
| 157 | */ |
| 158 | IMFMediaType *outputType = NULL; |
| 159 | |
| 160 | ex_MFCreateMediaType(&outputType); |
| 161 | ex_MFInitMediaTypeFromWaveFormatEx(outputType, &wfx, sizeof(wfx)); |
| 162 | |
| 163 | /* Set output type and get resulting media type. |
| 164 | */ |
| 165 | reader->SetCurrentMediaType(MF_SOURCE_READER_FIRST_AUDIO_STREAM, NULL, outputType); |
| 166 | reader->GetCurrentMediaType(MF_SOURCE_READER_FIRST_AUDIO_STREAM, &mediaType); |
| 167 | |
| 168 | outputType->Release(); |
| 169 | |
| 170 | /* Fill format from media type. |
| 171 | */ |
| 172 | Format format = track.GetFormat(); |
| 173 | |
| 174 | format.rate = MFGetAttributeUINT32(mediaType, MF_MT_AUDIO_SAMPLES_PER_SECOND, 0); |
| 175 | format.bits = MFGetAttributeUINT32(mediaType, MF_MT_AUDIO_BITS_PER_SAMPLE, 16); |
| 176 | format.channels = MFGetAttributeUINT32(mediaType, MF_MT_AUDIO_NUM_CHANNELS, 0); |
| 177 | |
| 178 | track.SetFormat(format); |
| 179 | |
| 180 | mediaType->Release(); |
| 181 | |
| 182 | /* Get track duration. |
| 183 | */ |
| 184 | PROPVARIANT duration; |
| 185 | |
| 186 | if (!FAILED(reader->GetPresentationAttribute(MF_SOURCE_READER_MEDIASOURCE, MF_PD_DURATION, &duration))) |
| 187 | { |
| 188 | /* Set approxLength, because WMA duration is not always 100% accurate. |
| 189 | */ |
| 190 | track.length = -1; |
| 191 | track.approxLength = Math::Round(Float(duration.hVal.QuadPart) / 1e7 * format.rate); |
| 192 | track.fileSize = driver.GetSize(); |
| 193 | } |
| 194 | |
| 195 | driver.Close(); |
| 196 | |
| 197 | /* Read tags. |
| 198 | */ |
| 199 | AS::Registry &boca = AS::Registry::Get(); |
| 200 | AS::TaggerComponent *tagger = (AS::TaggerComponent *) boca.CreateComponentByID("wma-tag"); |
| 201 | |
| 202 | if (tagger != NIL) |
| 203 | { |
| 204 | tagger->SetConfiguration(GetConfiguration()); |
| 205 | tagger->ParseStreamInfo(streamURI, track); |
| 206 | |
| 207 | boca.DeleteComponent(tagger); |
| 208 | } |
| 209 | |
| 210 | return Success(); |
| 211 | } |
| 212 | |
| 213 | BoCA::DecoderMediaFoundation::DecoderMediaFoundation() |
| 214 | { |
| 215 | reader = NIL; |
| 216 | mediaType = NIL; |
| 217 | } |
| 218 | |
| 219 | BoCA::DecoderMediaFoundation::~DecoderMediaFoundation() |
| 220 | { |
| 221 | } |
| 222 | |
| 223 | Bool BoCA::DecoderMediaFoundation::Activate() |
| 224 | { |
| 225 | /* Wrap stream in an IMFByteStream interface and create source reader. |
| 226 | */ |
| 227 | IMFByteStream *byteStream = new MFByteStream(driver); |
| 228 | HRESULT hr = ex_MFCreateSourceReaderFromByteStream(byteStream, NULL, &reader); |
| 229 | |
| 230 | byteStream->Release(); |
| 231 | |
| 232 | if (FAILED(hr)) return False; |
| 233 | |
| 234 | /* Select first audio stream and get input media type. |
| 235 | */ |
| 236 | IMFMediaType *inputType = NULL; |
| 237 | |
| 238 | reader->SetStreamSelection(MF_SOURCE_READER_FIRST_AUDIO_STREAM, TRUE); |
| 239 | reader->GetNativeMediaType(MF_SOURCE_READER_FIRST_AUDIO_STREAM, 0, &inputType); |
| 240 | |
| 241 | /* Fill format descriptor with input type. |
| 242 | */ |
| 243 | WAVEFORMATEX wfx = { }; |
| 244 | |
| 245 | wfx.wFormatTag = WAVE_FORMAT_PCM; |
| 246 | wfx.nChannels = MFGetAttributeUINT32(inputType, MF_MT_AUDIO_NUM_CHANNELS, 0); |
| 247 | wfx.nSamplesPerSec = MFGetAttributeUINT32(inputType, MF_MT_AUDIO_SAMPLES_PER_SECOND, 0); |
| 248 | wfx.wBitsPerSample = MFGetAttributeUINT32(inputType, MF_MT_AUDIO_BITS_PER_SAMPLE, 16); |
| 249 | wfx.nBlockAlign = wfx.nChannels * (wfx.wBitsPerSample / 8); |
| 250 | wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign; |
| 251 | |
| 252 | inputType->Release(); |
| 253 | |
| 254 | /* Create output media type. |
| 255 | */ |
| 256 | IMFMediaType *outputType = NULL; |
| 257 | |
| 258 | ex_MFCreateMediaType(&outputType); |
| 259 | ex_MFInitMediaTypeFromWaveFormatEx(outputType, &wfx, sizeof(wfx)); |
| 260 | |
| 261 | /* Set output type and get resulting media type. |
| 262 | */ |
| 263 | reader->SetCurrentMediaType(MF_SOURCE_READER_FIRST_AUDIO_STREAM, NULL, outputType); |
| 264 | reader->GetCurrentMediaType(MF_SOURCE_READER_FIRST_AUDIO_STREAM, &mediaType); |
| 265 | |
| 266 | outputType->Release(); |
| 267 | |
| 268 | return True; |
| 269 | } |
| 270 | |
| 271 | Bool BoCA::DecoderMediaFoundation::Deactivate() |
| 272 | { |
| 273 | mediaType->Release(); |
| 274 | reader->Release(); |
| 275 | |
| 276 | return True; |
| 277 | } |
| 278 | |
| 279 | Bool BoCA::DecoderMediaFoundation::Seek(Int64 samplePosition) |
| 280 | { |
| 281 | /* Seek to the requested position. |
| 282 | */ |
| 283 | PROPVARIANT position; |
| 284 | |
| 285 | InitPropVariantFromInt64(Float(samplePosition) * 1e7 / track.GetFormat().rate, &position); |
| 286 | |
| 287 | HRESULT hr = reader->SetCurrentPosition(GUID_NULL, position); |
| 288 | |
| 289 | if (FAILED(hr)) return False; |
| 290 | |
| 291 | return True; |
| 292 | } |
| 293 | |
| 294 | Int BoCA::DecoderMediaFoundation::ReadData(Buffer<UnsignedByte> &data) |
| 295 | { |
| 296 | /* Read next sample. |
| 297 | */ |
| 298 | IMFSample *sample = NULL; |
| 299 | DWORD streamFlags = 0; |
| 300 | HRESULT hr = reader->ReadSample(MF_SOURCE_READER_FIRST_AUDIO_STREAM, 0, NULL, &streamFlags, NULL, &sample); |
| 301 | |
| 302 | if (FAILED(hr)) return -1; |
| 303 | |
| 304 | if (streamFlags & MF_SOURCE_READERF_ENDOFSTREAM) return -1; |
| 305 | |
| 306 | /* Copy sample to data buffer. |
| 307 | */ |
| 308 | IMFMediaBuffer *buffer = NULL; |
| 309 | |
| 310 | sample->ConvertToContiguousBuffer(&buffer); |
| 311 | |
| 312 | BYTE *bytes = NULL; |
| 313 | DWORD length = 0; |
| 314 | |
| 315 | buffer->Lock(&bytes, NULL, &length); |
| 316 | |
| 317 | data.Resize(length); |
| 318 | |
| 319 | memcpy(data, bytes, length); |
| 320 | |
| 321 | buffer->Unlock(); |
| 322 | buffer->Release(); |
| 323 | |
| 324 | sample->Release(); |
| 325 | |
| 326 | /* Update inBytes to indicate progress. |
| 327 | */ |
| 328 | if (track.approxLength > 0) inBytes += track.fileSize * data.Size() / (track.approxLength * track.GetFormat().channels * (track.GetFormat().bits / 8)); |
| 329 | |
| 330 | return data.Size(); |
| 331 | } |