static HRESULT OpenAAFFile(aafWChar * pFileName, bool comp_enable) { IAAFFile* pFile = NULL; IAAFHeader* pHeader = NULL; IAAFDictionary* pDictionary = NULL; IAAFMob* pMob = NULL; IAAFEssenceAccess* pEssenceAccess = NULL; IEnumAAFMobs* pMobIter = NULL; aafNumSlots_t numMobs, numSlots; aafSearchCrit_t criteria; aafMobID_t mobID; IAAFDataDef *pPictureDef = NULL; IAAFMobSlot* pMobSlot = NULL; // Open an AAF file check(AAFFileOpenExistingRead (pFileName, 0, &pFile)); check(pFile->GetHeader(&pHeader)); // Open raw video output file FILE *output; const char *output_file = comp_enable ? "raw.uyvy" : "raw.mjpeg"; if ((output = fopen(output_file, "wb")) == NULL) { perror(output_file); exit(1); } // Get the AAF Dictionary from the file check(pHeader->GetDictionary(&pDictionary)); /* Lookup any necessary data definitions. */ check(pDictionary->LookupDataDef(kAAFDataDef_Picture, &pPictureDef)); /* Check number of Mobs in file */ check(pHeader->CountMobs(kAAFMasterMob, &numMobs)); if (numMobs == 0) return 0; printf("Found %d Master Mobs\n", numMobs); criteria.searchTag = kAAFByMobKind; criteria.tags.mobKind = kAAFMasterMob; check(pHeader->GetMobs(&criteria, &pMobIter)); while (AAFRESULT_SUCCESS == pMobIter->NextOne(&pMob)) { char mobIDstr[256]; char mobName[256]; aafWChar namebuf[1204]; IAAFTimelineMobSlot* pTimelineMobSlot = NULL; IAAFDataDef *pDataDef = NULL; IEnumAAFMobSlots* pMobSlotIter = NULL; check(pMob->GetMobID (&mobID)); check(pMob->GetName (namebuf, sizeof(namebuf))); convert(mobName, sizeof(mobName), namebuf); MobIDtoString(mobID, mobIDstr); printf(" MasterMob Name = '%s'\n", mobName); printf(" (mobID %s)\n", mobIDstr); // Get the number of slots check(pMob->CountSlots(&numSlots)); // Iterating through all Mob Slots check(pMob->GetSlots(&pMobSlotIter)); while(AAFRESULT_SUCCESS == pMobSlotIter->NextOne(&pMobSlot)) { // Check to see if it is a Video Timeline Mob Slot HRESULT hr; hr = pMobSlot->QueryInterface(IID_IAAFTimelineMobSlot,(void **) &pTimelineMobSlot); if (FAILED(hr)) { pMobSlot->Release(); pMobSlot = NULL; continue; } check(pMobSlot->GetDataDef(&pDataDef)); // Check that we have a picture data def aafBool bIsPictureKind = kAAFFalse; check(pDataDef->IsPictureKind(&bIsPictureKind)); if (kAAFTrue != bIsPictureKind) { pTimelineMobSlot->Release(); pTimelineMobSlot = NULL; pDataDef->Release(); pDataDef = NULL; continue; // skip non-picture data defs } aafUInt32 MobSlotID; IAAFMasterMob* pMasterMob = NULL; // Prepare to get video data: first get MobSlotID check(pMobSlot->GetSlotID(&MobSlotID)); // Then get a Master Mob interface check(pMob->QueryInterface(IID_IAAFMasterMob, (void **)&pMasterMob)); printf(" Opening slot %d with %s\n", MobSlotID, comp_enable ? "CompressionEnable" : "CompressionDisable"); // Open the Essence Data check(pMasterMob->OpenEssence(MobSlotID, NULL, kAAFMediaOpenReadOnly, comp_enable ? kAAFCompressionEnable : kAAFCompressionDisable, &pEssenceAccess)); // First put the codec into YUV pixel format IAAFEssenceFormat* pSetFormat = NULL; aafInt32 YUV_pixel = kAAFColorSpaceYUV; check(pEssenceAccess->GetEmptyFileFormat(&pSetFormat)); check(pSetFormat->AddFormatSpecifier(kAAFPixelFormat, 4, (unsigned char *) &YUV_pixel)); check(pEssenceAccess->PutFileFormat(pSetFormat)); pSetFormat->Release(); // Get the information about the format of the video data. // The pFormat object must be setup with each specifier you // wish to access, otherwise you get AAFRESULT_FORMAT_NOT_FOUND. aafRational_t sampleRate; aafUInt32 maxSampleSize; aafRect_t storedRect; aafFrameLayout_t frameLayout; IAAFEssenceFormat *fmtTemplate = NULL; IAAFEssenceFormat* pFormat = NULL; check(pEssenceAccess->GetEmptyFileFormat (&fmtTemplate)); check(fmtTemplate->AddFormatSpecifier(kAAFSampleRate, 0, NULL)); check(fmtTemplate->AddFormatSpecifier(kAAFMaxSampleBytes, 0, NULL)); check(fmtTemplate->AddFormatSpecifier(kAAFStoredRect, 0, NULL)); check(fmtTemplate->AddFormatSpecifier(kAAFFrameLayout, 0, NULL)); check(pEssenceAccess->GetFileFormat(fmtTemplate, &pFormat)); fmtTemplate->Release(); fmtTemplate = NULL; aafInt32 fmtBytesRead; check(pFormat->GetFormatSpecifier(kAAFSampleRate, sizeof(sampleRate), (aafDataBuffer_t)&sampleRate, &fmtBytesRead)); check(pFormat->GetFormatSpecifier(kAAFMaxSampleBytes, sizeof(maxSampleSize), (aafDataBuffer_t)&maxSampleSize, &fmtBytesRead)); check(pFormat->GetFormatSpecifier(kAAFStoredRect, sizeof(storedRect), (aafDataBuffer_t)&storedRect, &fmtBytesRead)); check(pFormat->GetFormatSpecifier(kAAFFrameLayout, sizeof(frameLayout), (aafDataBuffer_t)&frameLayout, &fmtBytesRead)); pFormat->Release(); pFormat = NULL; // Get the sample count which is in terms of EditRate aafLength_t sampleCount; check(pEssenceAccess->CountSamples(pPictureDef, &sampleCount)); const char *frameLayoutStr = ""; switch (frameLayout) { case kAAFFullFrame: frameLayoutStr = "FullFrame"; break; case kAAFOneField: frameLayoutStr = "OneField"; break; case kAAFSeparateFields: frameLayoutStr = "SeparateFields"; break; case kAAFMixedFields: frameLayoutStr = "MixedFields"; break; default: break; } printf("\tSlotID %u: SampleRate=%d/%d MaxSampleBytes=%u StoredRect=%dx%d\n", MobSlotID, sampleRate.numerator, sampleRate.denominator, maxSampleSize, storedRect.xSize, storedRect.ySize); printf("\t\tFrameLayout=%s CountSamples=%"AAFFMT64"d\n", frameLayoutStr, sampleCount); // Buffer to receive samples from ReadSamples aafUInt8 *dataBuf = new aafUInt8[maxSampleSize]; // Buffer to recombine separated fields into interleaved fields aafUInt8 *recombined_buf = new aafUInt8[maxSampleSize]; // Read samples until no more are available aafUInt32 samplesRead, actualBytesRead, total_samples = 0; while (true) { hr = (pEssenceAccess->ReadSamples( 1, // number of samples to read maxSampleSize, // maximum buffer size dataBuf, // output buffer for audio data &samplesRead, // number of samples read &actualBytesRead)); // number of bytes read if (hr == AAFRESULT_EOF) break; else check(hr); aaf_assert(actualBytesRead != 0, "actualBytesRead != 0"); total_samples += samplesRead; aafUInt8* saveBuf = dataBuf; if (comp_enable && frameLayout == kAAFSeparateFields) { // recombine fields into uncompressed frame when decompressing recombine_fields(dataBuf, recombined_buf, storedRect.xSize, storedRect.ySize); saveBuf = recombined_buf; } // Write out video if ( fwrite(saveBuf, maxSampleSize, 1, output) != 1 ) { perror(output_file); return 1; } } printf("\tTotal samples=%u - written to %s\n", total_samples, output_file); delete [] dataBuf; delete [] recombined_buf; pEssenceAccess->Release(); pEssenceAccess = NULL; pMasterMob->Release(); pMasterMob = NULL; } pMobSlotIter->Release(); pMobSlotIter = NULL; pMob->Release(); pMob = NULL; } pMobIter->Release(); pMobIter = NULL; return moduleErrorTmp; }
static HRESULT ReadAAFFile(const aafWChar * pFileName, testType_t testType) { IAAFFile * pFile = NULL; IAAFHeader * pHeader = NULL; IAAFDictionary* pDictionary = NULL; IAAFEssenceAccess* pEssenceAccess = NULL; IAAFEssenceMultiAccess* pMultiEssence = NULL; IAAFEssenceFormat *fmtTemplate = NULL; IEnumAAFMobs* pMobIter = NULL; IAAFMob* pMob = NULL; IAAFMasterMob* pMasterMob = NULL; IAAFEssenceFormat* pFormat = NULL; aafNumSlots_t numMobs, numSlots; aafSearchCrit_t criteria; aafRational_t readSampleRate; aafMobID_t mobID; aafWChar namebuf[1204]; unsigned char AAFDataBuf[4096]; aafUInt32 AAFBytesRead, samplesRead; FILE* pWavFile = NULL; unsigned char WAVDataBuf[4096], *dataPtr; size_t WAVBytesRead; aafUInt32 dataOffset, dataLen; aafUInt16 bitsPerSample, numCh; check(AAFFileOpenExistingRead ( pFileName, 0, &pFile)); check(pFile->GetHeader(&pHeader)); // Get the AAF Dictionary so that we can create valid AAF objects. check(pHeader->GetDictionary(&pDictionary)); // Here we check on the number of mobs in the file. // Get the number of master mobs in the file (should be one) check(pHeader->CountMobs(kAAFMasterMob, &numMobs)); // ** causes leak if (1 == numMobs ) { printf("Found %d Master Mobs\n", numMobs); criteria.searchTag = kAAFByMobKind; criteria.tags.mobKind = kAAFMasterMob; check(pHeader->GetMobs(&criteria, &pMobIter)); while(AAFRESULT_SUCCESS == pMobIter->NextOne(&pMob)) { char mobIDstr[256]; char mobName[256]; check(pMob->GetMobID (&mobID)); check(pMob->GetName (namebuf, sizeof(namebuf))); convert(mobName, sizeof(mobName), namebuf); MobIDtoString(mobID, mobIDstr); printf(" MasterMob Name = '%s'\n", mobName); printf(" (mobID %s)\n", mobIDstr); // Make sure we have two slots check(pMob->CountSlots(&numSlots)); if (2 == numSlots) { // The essence data is in SlotID 1 // Get a Master Mob interface check(pMob->QueryInterface(IID_IAAFMasterMob, (void **)&pMasterMob)); // Open the Essence Data check(pMasterMob->OpenEssence( 1, // SlotID 1 NULL, // mediaCriteria (Don't care) kAAFMediaOpenReadOnly, // Open mode kAAFCompressionDisable,// Compress disabled &pEssenceAccess)); // Open and read the Wave file (for comparison) pWavFile = fopen("Laser.wav", "r"); if (pWavFile) { // read in the essence data WAVBytesRead = fread(WAVDataBuf, sizeof(unsigned char), sizeof(WAVDataBuf), pWavFile); fclose(pWavFile); pWavFile = NULL; check(loadWAVEHeader(WAVDataBuf, &bitsPerSample, &numCh, &readSampleRate, &dataOffset, &dataLen)); dataPtr = WAVDataBuf + dataOffset; aafUInt32 sampleBits; aafInt32 bytesRead; check(pEssenceAccess->GetEmptyFileFormat (&fmtTemplate)); check(fmtTemplate->AddFormatSpecifier (kAAFAudioSampleBits, 0, NULL)); check(pEssenceAccess->GetFileFormat (fmtTemplate, &pFormat)); fmtTemplate->Release(); fmtTemplate = NULL; check(pFormat->GetFormatSpecifier (kAAFAudioSampleBits, sizeof(sampleBits), (aafDataBuffer_t)&sampleBits, &bytesRead)); pFormat->Release(); pFormat = NULL; if(sampleBits != bitsPerSample) { printf("***Wrong sample size read ( was %d , should be %d)\n", sampleBits, bitsPerSample); } // Read the Data from the AAF file if(testType == testStandardCalls) { check(pEssenceAccess->ReadSamples( dataLen, //!!! Hardcoded // Number of Samples sizeof(AAFDataBuf), // Maximum buffer size AAFDataBuf, // Buffer for the data &samplesRead, // Actual number of samples read &AAFBytesRead)); // Actual number of bytes read } else if(testType == testMultiCalls) { aafmMultiXfer_t xfer; aafmMultiResult_t result; check(pEssenceAccess->QueryInterface(IID_IAAFEssenceMultiAccess, (void **)&pMultiEssence)); xfer.numSamples = dataLen; //!!! Hardcoded // Number of Samples xfer.buflen = sizeof(AAFDataBuf); xfer.buffer = AAFDataBuf; result.bytesXfered = 0; check(pMultiEssence->ReadMultiSamples(1, &xfer, &result)); samplesRead = result.samplesXfered; AAFBytesRead = result.bytesXfered; pMultiEssence->Release(); pMultiEssence = NULL; } // Now compare the data read from the AAF file to the actual WAV file if (dataLen != AAFBytesRead) { printf("***Wrong number of bytes read (was %u , should be %zu)\n", AAFBytesRead, WAVBytesRead); } if (memcmp( dataPtr, AAFDataBuf, dataLen) != 0) { printf("*** Data Read is different than the data in the WAV file ***\n"); } } else { printf("***Failed to open Wave file Laser.wav for comparison\n"); } } else { printf("***Wrong number of slots in the Master Mob (was %d should be %d)\n", numSlots, 2); } if (pMasterMob) { pMasterMob->Release(); pMasterMob = NULL; } pMob->Release(); pMob = NULL; if (pEssenceAccess) { pEssenceAccess->Release(); pEssenceAccess = NULL; } } // while pMobIter->NextOne pMobIter->Release(); pMobIter = NULL; } else { printf("***Wrong number of Master mobs in the file (was %d should be %d)\n", numMobs, 1); } printf("--------\n"); cleanup: // Cleanup and return if (pWavFile) fclose(pWavFile); if (pMultiEssence) pMultiEssence->Release(); pMultiEssence=NULL; if(fmtTemplate) { fmtTemplate->Release(); fmtTemplate = NULL; } if (pEssenceAccess) { pEssenceAccess->Release(); pEssenceAccess = NULL; } if (pDictionary) pDictionary->Release(); pDictionary=NULL; if (pHeader) pHeader->Release(); pHeader=NULL; if (pMobIter) pMobIter->Release(); pMobIter=NULL; if (pFormat) pFormat->Release(); pFormat=NULL; if (pFile) { pFile->Close(); pFile->Release(); pFile=NULL; } return moduleErrorTmp; }
static HRESULT OpenAAFFile(aafWChar * pFileName) { IAAFFile* pFile = NULL; IAAFHeader* pHeader = NULL; IAAFDictionary* pDictionary = NULL; IAAFMob* pMob = NULL; IAAFEssenceAccess* pEssenceAccess = NULL; IAAFEssenceFormat* pFormat = NULL; IEnumAAFMobs* pMobIter = NULL; aafNumSlots_t numMobs, numSlots; aafSearchCrit_t criteria; aafMobID_t mobID; IAAFDataDef *pSoundDef = NULL; IAAFMobSlot* pMobSlot = NULL; // Open an AAF file check(AAFFileOpenExistingRead (pFileName, 0, &pFile)); check(pFile->GetHeader(&pHeader)); // Open raw audio output file FILE *output; const char *output_file = "raw.pcm"; if ((output = fopen(output_file, "wb")) == NULL) { perror(output_file); exit(1); } // Get the AAF Dictionary from the file check(pHeader->GetDictionary(&pDictionary)); /* Lookup any necessary data definitions. */ check(pDictionary->LookupDataDef(kAAFDataDef_Sound, &pSoundDef)); /* Check number of Mobs in file */ check(pHeader->CountMobs(kAAFMasterMob, &numMobs)); if (numMobs == 0) { printf("No Master Mobs found in AAF file\n"); return 0; } printf("Found %d Master Mobs\n", numMobs); criteria.searchTag = kAAFByMobKind; criteria.tags.mobKind = kAAFMasterMob; check(pHeader->GetMobs(&criteria, &pMobIter)); while (AAFRESULT_SUCCESS == pMobIter->NextOne(&pMob)) { char mobIDstr[256]; aafWChar namebuf[1204]; IAAFTimelineMobSlot* pTimelineMobSlot = NULL; IAAFDataDef *pDataDef = NULL; IAAFEssenceFormat *fmtTemplate = NULL; unsigned char *dataBuff = NULL; IEnumAAFMobSlots* pMobSlotIter = NULL; check(pMob->GetMobID (&mobID)); check(pMob->GetName (namebuf, sizeof(namebuf))); MobIDtoString(mobID, mobIDstr); printf(" MasterMob Name = '%ls'\n", namebuf); printf(" (mobID %s)\n", mobIDstr); // Get the number of slots check(pMob->CountSlots(&numSlots)); // Iterating through all Mob Slots check(pMob->GetSlots(&pMobSlotIter)); while(AAFRESULT_SUCCESS == pMobSlotIter->NextOne(&pMobSlot)) { // Check to see if it is an Audio Timeline Mob Slot HRESULT hr; aafUInt32 MobSlotID; hr=pMobSlot->QueryInterface(IID_IAAFTimelineMobSlot,(void **) &pTimelineMobSlot); if (SUCCEEDED(hr)) { check(pMobSlot->GetDataDef(&pDataDef)); // Check that we have a sound file by examining its data definition aafBool bIsSoundKind = kAAFFalse; check(pDataDef->IsSoundKind(&bIsSoundKind)); if (kAAFTrue == bIsSoundKind) { IAAFMasterMob* pMasterMob = NULL; // Prepare to get audio data: first get MobSlotID check(pMobSlot->GetSlotID(&MobSlotID)); // Then get a Master Mob interface check(pMob->QueryInterface(IID_IAAFMasterMob, (void **)&pMasterMob)); // Open the Essence Data check(pMasterMob->OpenEssence(MobSlotID, NULL, kAAFMediaOpenReadOnly, kAAFCompressionDisable, &pEssenceAccess)); // Get the information about the format of the audio data. // The pFormat object must be setup with each specifier you // wish to access, otherwise you get AAFRESULT_FORMAT_NOT_FOUND. aafUInt32 audioSampleBits; aafRational_t sampleRate; aafUInt32 numChannels; aafUInt32 maxSampleBytes; check(pEssenceAccess->GetEmptyFileFormat(&fmtTemplate)); check(fmtTemplate->AddFormatSpecifier(kAAFAudioSampleBits, 0, NULL)); check(fmtTemplate->AddFormatSpecifier(kAAFSampleRate, 0, NULL)); check(fmtTemplate->AddFormatSpecifier(kAAFNumChannels, 0, NULL)); check(fmtTemplate->AddFormatSpecifier(kAAFMaxSampleBytes, 0, NULL)); check(pEssenceAccess->GetFileFormat(fmtTemplate, &pFormat)); fmtTemplate->Release(); fmtTemplate = NULL; aafInt32 fmtBytesRead; check(pFormat->GetFormatSpecifier(kAAFAudioSampleBits, sizeof(audioSampleBits), (aafDataBuffer_t)&audioSampleBits, &fmtBytesRead)); check(pFormat->GetFormatSpecifier(kAAFSampleRate, sizeof(sampleRate), (aafDataBuffer_t)&sampleRate, &fmtBytesRead)); check(pFormat->GetFormatSpecifier(kAAFNumChannels, sizeof(numChannels), (aafDataBuffer_t)&numChannels, &fmtBytesRead)); check(pFormat->GetFormatSpecifier(kAAFMaxSampleBytes, sizeof(maxSampleBytes), (aafDataBuffer_t)&maxSampleBytes, &fmtBytesRead)); pFormat->Release(); pFormat = NULL; // Get the sample count which is in terms of EditRate aafLength_t sampleCount; check(pEssenceAccess->CountSamples(pSoundDef, &sampleCount)); printf("\tSlotID %u: SampleBits=%d SampleRate=%d/%d NumChannels=%d MaxSampleBytes=%u\n", MobSlotID, audioSampleBits, sampleRate.numerator, sampleRate.denominator, numChannels, maxSampleBytes); printf("\t\tCountSamples=%"AAFFMT64"d\n", sampleCount); // Set a suitable buffer size dataBuff = new unsigned char[maxSampleBytes]; // Read samples until no more are available aafUInt32 samplesRead, actualBytesRead, total_samples = 0; while (true) { hr = (pEssenceAccess->ReadSamples( 1, // number of samples to read maxSampleBytes, // maximum buffer size dataBuff, // output buffer for audio data &samplesRead, // number of samples read &actualBytesRead)); // number of bytes read if (hr == AAFRESULT_EOF) break; else check(hr); if (actualBytesRead!=0) { total_samples += samplesRead; // Write out samples if ( fwrite(dataBuff, maxSampleBytes, 1, output) != 1 ) { perror(output_file); return 1; } } } printf("\tTotal samples = %u (written to %s)\n", total_samples, output_file); delete [] dataBuff; dataBuff = NULL; pEssenceAccess->Release(); pEssenceAccess = NULL; pMasterMob->Release(); pMasterMob = NULL; } pTimelineMobSlot->Release(); pTimelineMobSlot = NULL; pDataDef->Release(); pDataDef = NULL; } pMobSlot->Release(); pMobSlot = NULL; } pMobSlotIter->Release(); pMobSlotIter = NULL; pMob->Release(); pMob = NULL; } pMobIter->Release(); pMobIter = NULL; return moduleErrorTmp; }