Пример #1
0
static HRESULT CreateAAFFile(aafWChar * pFileName, bool comp_enable)
{
	IAAFFile*					pFile = NULL;
	IAAFHeader*					pHeader = NULL;
	IAAFHeader2*				pHeader2 = NULL;
	IAAFDictionary*				pDictionary = NULL;
	IAAFMob*					pMob = NULL;
	IAAFMasterMob*				pMasterMob = NULL;
	IAAFEssenceAccess*			pEssenceAccess = NULL;
	aafMobID_t					masterMobID;
	aafProductIdentification_t	ProductInfo;
	IAAFClassDef				*pCDMasterMob = NULL;
	IAAFDataDef					*pPictureDef = NULL;
	aafUInt32					samplesWritten, bytesWritten;


	// Delete any previous test file before continuing...
	char cFileName[FILENAME_MAX];
	convert(cFileName, sizeof(cFileName), pFileName);
	remove(cFileName);

	cout << "Creating file " << cFileName << " using WriteSamples with " <<
		(comp_enable ? "CompressionEnable" : "CompressionDisable") << endl;

	aafProductVersion_t ver = {1, 0, 0, 0, kAAFVersionBeta};
	ProductInfo.companyName = const_cast<wchar_t *>(L"none");
	ProductInfo.productName = const_cast<wchar_t *>(L"AAF SDK");
	ProductInfo.productVersion = &ver;
	ProductInfo.productVersionString = const_cast<wchar_t *>(L"1.0.0.0 Beta");
	ProductInfo.productID = NIL_UID;
	ProductInfo.platform = NULL;		// Set by SDK when saving

	// select the file kind
	const aafUID_t*			fileKind = &kAAFFileKind_DontCare;
	if( FormatMXF )			fileKind = &kAAFFileKind_AafKlvBinary;
	else if( FormatSS512 )	fileKind = &kAAFFileKind_Aaf512Binary;
	else					fileKind = &kAAFFileKind_Aaf4KBinary;

	// Create a new AAF file
	check(AAFFileOpenNewModifyEx(pFileName, fileKind, 0, &ProductInfo, &pFile));
	check(pFile->GetHeader(&pHeader));

	// Set the operational pattern
	check(pHeader->QueryInterface(IID_IAAFHeader2, (void **)&pHeader2));
	check(pHeader2->SetOperationalPattern(kAAFOpDef_Atom));

	// Get the AAF Dictionary from the file
	check(pHeader->GetDictionary(&pDictionary));

	/* Lookup class definitions for the objects we want to create. */
	check(pDictionary->LookupClassDef(AUID_AAFMasterMob, &pCDMasterMob));

	/* Lookup any necessary data definitions. */
	check(pDictionary->LookupDataDef(kAAFDataDef_Picture, &pPictureDef));

	/* Create a Mastermob */

	// Get a Master MOB Interface
	check(pCDMasterMob->CreateInstance(IID_IAAFMasterMob, (IUnknown **)&pMasterMob));

	// Get a Mob interface and set its variables.
	check(pMasterMob->QueryInterface(IID_IAAFMob, (void **)&pMob));
	check(pMob->GetMobID(&masterMobID));
	if (input_video == NULL)
	{
		check(pMob->SetName(L"DNX_color_bars"));
	}
	else
	{
		check(pMob->SetName(pFileName));
	}

	// Add Mobs to the Header
	check(pHeader->AddMob(pMob));

	/* Create the Essence Data specifying the codec, container, edit rate and sample rate */
	check(pMasterMob->CreateEssence(
		1,			// Slot ID within MasterMob
		pPictureDef,			// MediaKind
		UseDNX? kAAFCodecDNxHD : kAAFCodecVC3,			// codecID
		editRate,				// edit rate
		editRate,				// sample rate
		comp_enable ? kAAFCompressionEnable : kAAFCompressionDisable,
		NULL,					// No Locator used
		ContainerAAF,			// Essence embedded in AAF file
		&pEssenceAccess));		//

	// Set the codec flavour for desired video format
	switch(ComprID)
	{
	case 1235:
		pEssenceAccess->SetEssenceCodecFlavour( kAAFCodecFlavour_VC3_1235 );
		break;
	case 1238:
		pEssenceAccess->SetEssenceCodecFlavour( kAAFCodecFlavour_VC3_1238 );
		break;
	case 1237:
		pEssenceAccess->SetEssenceCodecFlavour( kAAFCodecFlavour_VC3_1237 );
		break;
	case 1241:
		pEssenceAccess->SetEssenceCodecFlavour( kAAFCodecFlavour_VC3_1241 );
		break;
	case 1243:
		pEssenceAccess->SetEssenceCodecFlavour( kAAFCodecFlavour_VC3_1243 );
		break;
	case 1244:
		pEssenceAccess->SetEssenceCodecFlavour( kAAFCodecFlavour_VC3_1244 );
		break;
	case 1242:
		pEssenceAccess->SetEssenceCodecFlavour( kAAFCodecFlavour_VC3_1242 );
		break;
	case 1250:
		pEssenceAccess->SetEssenceCodecFlavour( kAAFCodecFlavour_VC3_1250 );
		break;
	case 1251:
		pEssenceAccess->SetEssenceCodecFlavour( kAAFCodecFlavour_VC3_1251 );
		break;
	case 1252:
		pEssenceAccess->SetEssenceCodecFlavour( kAAFCodecFlavour_VC3_1252 );
		break;
	case 1253:
		pEssenceAccess->SetEssenceCodecFlavour( kAAFCodecFlavour_VC3_1253 );
		break;
	default:
		break;
	}

	switch( input_type )
	{
	case P_UYVY:
	case BarsUYVY:
	default:
		layout = UseDNX?Layout_P_UYVY:Layout_UYVYM;
		break;

	case UYVY:
		layout = UseDNX?Layout_UYVY:Layout_Unknown;
		break;

	case BGRAI:
	case BarsRGB:
		layout = UseDNX?Layout_BGRAI:Layout_Unknown;
		break;

	case BGRA:
		layout = UseDNX?Layout_BGRA:Layout_Unknown;
		break;
	}

	// Set up the information about the format of the data
	IAAFEssenceFormat *  fmtTemplate=NULL;
	check(pEssenceAccess->GetEmptyFileFormat (&fmtTemplate));
	
	if( UseLegacyUIDs )
		check(fmtTemplate->AddFormatSpecifier (kAAFLegacyAUIDs, sizeof(UseLegacyUIDs), (aafDataBuffer_t)&UseLegacyUIDs));

	if( UseDNX )
		check(fmtTemplate->AddFormatSpecifier (kAAFNumThreads, sizeof(nCodecThreads), (aafDataBuffer_t)&nCodecThreads));

	if( UseDNX )
		check(fmtTemplate->AddFormatSpecifier (kAAFBufferLayout, sizeof(layout), (aafDataBuffer_t)&layout));

	// codec will automatically SetEssenceKey() for MXF if !UseLegacyUIDs
	// codec will automatically SetEssenceKey() for Avid legacy if UseLegacyUIDs

	// future: if other kAAFEssenceElementKey, set it here

	// future: if other kAAFPhysicalTrackNum, set it here

	// Put the format specifiers into the codec
	check(pEssenceAccess->PutFileFormat( fmtTemplate ));

	// For fun, print the name of the selected codec flavour
	aafWChar codec_name[128] = L"";
	check(pEssenceAccess->GetCodecName(sizeof(codec_name), codec_name));
	printf("  using codec flavour \"%ls\"\n", codec_name);

	// Write the video samples
	int total_samples = 0;
	if( input_type==Raw )
	{
		// Read one frame of raw VC3 video at a time, repeat last frame until end
		FILE * ip=fopen( input_video,"rb" );
		if(!ip) return false;

		aafUInt32  nwant = GetBytesPerEditUnit(ComprID); 
		unsigned char* raw_buff = new unsigned char [ nwant ];

		size_t nread=0;

		while( Duration-- )
		{
			// keep on trying to read
			if( !feof( ip ) ) nread=fread( raw_buff, 1, nwant, ip );

			// abandon if partial frame (complete frame or zero is ok
			if ( nread!=nwant && nread!= 0 ) { perror(input_video); break; }

			samplesWritten=0;
			check(pEssenceAccess->WriteSamples(1, nwant, raw_buff, &samplesWritten, &bytesWritten));
			total_samples += samplesWritten;
		}
		delete[] raw_buff;
		fclose( ip );
	}
	else if( input_type==P_UYVY || input_type==UYVYI || input_type==UYVYM )
	{
		// Load a single frame of uncompressed
		aafUInt32  nwant = GetLinesPerField(ComprID)*(IsInterlaced(ComprID)?2:1)*(GetPixelsPerLine(ComprID)/2)*(UseDNX?5:8); 
	
		unsigned char* video_buffer = new unsigned char [ nwant ];

		// int LoadXYUV( BufferLayout_t target, byte *video_buffer, int len, int H, int V, const char *input_video, 
		//				 bool isLittleEndian=true, bool isBigEndian=false, bool isP_UYVY=false );

		LoadXYUV( UseDNX?Layout_P_UYVY:Layout_UYVYM, 
			      video_buffer, nwant, 
				  GetPixelsPerLine(ComprID), GetLinesPerField(ComprID)*(IsInterlaced(ComprID)?2:1), 
				  input_video,
				  input_type==UYVYI, input_type==UYVYM, input_type==P_UYVY );

		while( Duration-- )
		{
			samplesWritten=0;
			check(pEssenceAccess->WriteSamples(1, nwant, video_buffer, &samplesWritten, &bytesWritten));
			total_samples += samplesWritten;
		}
		delete[] video_buffer;
	}
	else if( input_type==UYVY )
	{
		// Load a single frame of uncompressed
		aafUInt32  nwant = GetLinesPerField(ComprID)*(IsInterlaced(ComprID)?2:1)*(GetPixelsPerLine(ComprID)/2)*(UseDNX?4:8); 
	
		unsigned char* video_buffer = new unsigned char [ nwant ];

		// int LoadXYUV( BufferLayout_t target, byte *video_buffer, int len, int H, int V, const char *input_video, 
		//				 bool isLittleEndian=true, bool isBigEndian=false, bool isP_UYVY=false );

		LoadXYUV( UseDNX?Layout_UYVY:Layout_UYVYM, 
			      video_buffer, nwant, 
				  GetPixelsPerLine(ComprID), GetLinesPerField(ComprID)*(IsInterlaced(ComprID)?2:1), 
				  input_video,
				  false, false, false );

		while( Duration-- )
		{
			samplesWritten=0;
			check(pEssenceAccess->WriteSamples(1, nwant, video_buffer, &samplesWritten, &bytesWritten));
			total_samples += samplesWritten;
		}
		delete[] video_buffer;
	}
	else if( input_type==BGRAI )
	{
		// never get here with VC3Codec

		// Load a single frame of uncompressed
		aafUInt32  nwant = GetLinesPerField(ComprID)*(IsInterlaced(ComprID)?2:1)*GetPixelsPerLine(ComprID)*8; 
	
		unsigned char* video_buffer = new unsigned char [ nwant ];

		// int LoadXRGB( BufferLayout_t target, byte *video_buffer, int len, int H, int V, const char *input_video, 
		//				 bool isLittleEndian=true, bool isBigEndian=false, bool hasA=false, bool isRGB=false );

		LoadXRGB( Layout_BGRAI, 
			      video_buffer, nwant, 
				  GetPixelsPerLine(ComprID), GetLinesPerField(ComprID)*(IsInterlaced(ComprID)?2:1), 
				  input_video,
				  input_type==BGRAI, false, true, false );

		while( Duration-- )
		{
			samplesWritten=0;
			check(pEssenceAccess->WriteSamples(1, nwant, video_buffer, &samplesWritten, &bytesWritten));
			total_samples += samplesWritten;
		}
		delete[] video_buffer;
	}
	else if( input_type==BGRA )
	{
		// never get here with VC3Codec

		// Load a single frame of uncompressed
		aafUInt32  nwant = GetLinesPerField(ComprID)*(IsInterlaced(ComprID)?2:1)*GetPixelsPerLine(ComprID)*8; 
	
		unsigned char* video_buffer = new unsigned char [ nwant ];

		// int LoadXRGB( BufferLayout_t target, byte *video_buffer, int len, int H, int V, const char *input_video, 
		//				 bool isLittleEndian=true, bool isBigEndian=false, bool hasA=false, bool isRGB=false );

		LoadXRGB( Layout_BGRA, 
			      video_buffer, nwant, 
				  GetPixelsPerLine(ComprID), GetLinesPerField(ComprID)*(IsInterlaced(ComprID)?2:1), 
				  input_video,
				  input_type==BGRAI, false, true, false );

		while( Duration-- )
		{
			samplesWritten=0;
			check(pEssenceAccess->WriteSamples(1, nwant, video_buffer, &samplesWritten, &bytesWritten));
			total_samples += samplesWritten;
		}
		delete[] video_buffer;
	}
	else if( input_type==BarsRGB )		// using generated rgb colo(u)r bars
	{
		// never get here with VC3Codec

		// Create a frame of colour bars
		aafUInt32  nwant = GetLinesPerField(ComprID)*(IsInterlaced(ComprID)?2:1)*GetPixelsPerLine(ComprID)*8; 
		unsigned char* video_buffer = new unsigned char [ nwant ];

		LoadXBars( Layout_BGRAI, video_buffer, nwant, GetPixelsPerLine(ComprID), GetLinesPerField(ComprID)*(IsInterlaced(ComprID)?2:1) );

		while( Duration-- )
		{
			samplesWritten=0;
			check(pEssenceAccess->WriteSamples(1, nwant, video_buffer, &samplesWritten, &bytesWritten));
			total_samples += samplesWritten;
		}
		delete[] video_buffer;
	}
	else // if( input_type==BarsUYVY || input_video == NULL || others... )		// using generated component colo(u)r bars
	{
		// Create a frame of colour bars
		aafUInt32  nwant = GetLinesPerField(ComprID)*(IsInterlaced(ComprID)?2:1)*(GetPixelsPerLine(ComprID)/2)*(UseDNX?5:8); 
		unsigned char* video_buffer = new unsigned char [ nwant ];

		LoadXBars( UseDNX?Layout_P_UYVY:Layout_UYVYM, video_buffer, nwant, GetPixelsPerLine(ComprID), GetLinesPerField(ComprID)*(IsInterlaced(ComprID)?2:1) );

		while( Duration-- )
		{
			samplesWritten=0;
			check(pEssenceAccess->WriteSamples(1, nwant, video_buffer, &samplesWritten, &bytesWritten));
			total_samples += samplesWritten;
		}
		delete[] video_buffer;
	}

	printf("Completed Write\n");

	/* Set the essence to indicate that you have finished writing the samples */
	check(pEssenceAccess->CompleteWrite());
	check(pHeader2->UpdateEssenceContainers());

	pEssenceAccess->Release();

	pMob->Release();
	pMasterMob->Release();

	pPictureDef->Release();
	pCDMasterMob->Release();

	pDictionary->Release();
	pHeader->Release();
	pHeader2->Release();

	/* Save the AAF file */
	pFile->Save();

	/* Close the AAF file */
	pFile->Close();
	pFile->Release();

	return moduleErrorTmp;
}