예제 #1
0
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;
}
예제 #2
0
파일: ImportPCM.cpp 프로젝트: mcanthony/aaf
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;
}