fprintf(out,"%c %010llX",Type[ftype],lastGop-4); if(ftype==1) { if(gop_seen) abspos=lastAbsGop;
{ image_start=lastGop-4; image_absStart=lastAbsGop-4; }  fprintf(out,"%c %010llX",Type[ftype],image_start); if(ftype==1) {
uint8_t indexMpeg(char *mpeg,char *file,uint8_t audioid){	static const char Type[5]={'X','I','P','B','D'};	FILE *out=NULL;	uint32_t val;	uint32_t hnt;;//	uint32_t in;	uint64_t pos;	uint64_t abspos;	uint32_t w,h,fps ;	uint32_t nb_iframe=0;//	uint32_t nb_pframe=0;//	uint32_t nb_bframe=0;//	uint32_t first=0;	uint32_t total_frame=0;	uint32_t seq_found=0;	uint64_t lastI=0;	uint32_t update=0;	uint8_t  gop_seen=0;	uint64_t lastGop=0;	uint64_t lastAbsGop=0;	    uint8_t streamid,audiostreamid=0x00;	mParser *parser;	ADM_mpegDemuxer *demuxer;	audiostreamid=audioid;	parser=new mParser();	printf("audio Id: %x\n",audiostreamid);	if(!parser->open(mpeg))		{				GUI_Alert("Error reading mpeg !");				delete parser;				return 0;		}		uint8_t token;	parser->sync(&token);	// first identify stream type (ES/PS)	delete parser;	switch(token)		{				case 0xb3: // video es									printf("Video Elementary stream");									demuxer=new ADM_mpegDemuxerElementaryStream;									break;				case 0xba : // program stream ?	(vob/mpeg/VCD)									printf("Program stream");									demuxer=new ADM_mpegDemuxerProgramStream(0xe0,audiostreamid);									break;				default:								printf("\n unrecognized stream\n");								return 0;														}	// ok now we have it.	demuxer->open(mpeg);	out=fopen(file,"wt");	if(!out)	{			printf("\n Error : cannot open index !");			delete demuxer;			return 0;	}	fprintf(out,"IDXM ");	if(token==0xb3) fprintf(out,"E XX\n");	else   fprintf(out,"P XX\n");	fprintf(out,"000000000000\n");	fprintf(out,"1\n");	fprintf(out,"%s\n",mpeg);	DIA_working *work;	uint32_t	gop_forward=0;	work=new DIA_working("Indexing mpeg");	while(1)	{				val=0;				hnt=0;				// sync on the video stream is done by demuxer, now get on the infos				//				if(!demuxer->sync(&streamid)) break;				demuxer->getpos(&pos);				abspos=demuxer->getAbsPos();				update++;			//	printf("\n sync found : %d",streamid);				if(update>100)					{						if(work->update(demuxer->getAbsPos()>>16,demuxer->getSize()>>16))						{							// abort;							goto stop_found;						}						update=0;					}				switch(streamid)					{					/* Useless apparently						  	case 0xB9: // sequence end                                        		printf("\n End seq\n");                                        		goto stop_found;                                        		break;					*/					case 0xB3: // sequence start						if(seq_found)						{						 	demuxer->forward(8);							break;						}						seq_found=1;					        val=demuxer->read32i();						w=val>>20;					       	w=((w+15)&~15);					        h= (((val>>8) & 0xfff)+15)& ~15;					      	fps= FPS[val & 0xf];						demuxer->forward(4);						printf("\n %ld x %ld at %ld fps\n",w,h,fps);						fprintf(out,"%ld %ld %ld\n",w,h,fps);						break;					case 0xb8: // GOP						//	printf("GOP\n");						uint32_t gop;							demuxer->forward(3);							gop=demuxer->read8i();						// x Gop broken x x x x x 						gop>>=6; // skip padding						gop&=1;						gop_seen=1;						// Memorize gop position						// as it will be the real start of the						// next I Frame						lastGop=pos;						lastAbsGop=abspos;						// 25 bits = time code#if 0						if(gop)						{							printf("** Closed gop***\n");							// we will change B frame to P frame							// until we reach the next I/P							gop_forward=2;						}#endif						// 						break;					case 0x00 : // picture				                //  printf("PIC\n");						uint8_t ftype;						uint8_t temporal_ref;												total_frame++;						val=demuxer->read16i();						temporal_ref=val>>6;						ftype=7 & (val>>3);#if 0						if(gop_forward==2 && temporal_ref!=0 && ftype==1)						{							printf("Fake closed gop!\n");							gop_forward=0;						}#endif						if(ftype==1 || ftype==2) // I or P							if(gop_forward) gop_forward--;						if(ftype==3 && gop_forward)							ftype=2; // change B frame to P frame						switch(ftype)						{						case 2:// P 						case 3:// B						case 4:							gop_seen=0;							if(total_frame==1)							{								total_frame=0;								continue;							}						case 1:// printf(" Pic : I\n");																				//	printf("\n%c",Type[ftype]);																					if(total_frame!=1)							{								fprintf(out," %llu\n",pos-lastI-4);							}							if(!gop_seen)								fprintf(out,"%c %010llX",Type[ftype],pos-4);							else								fprintf(out,"%c %010llX",Type[ftype],lastGop-4);														          						if(ftype==1) // I frame							{								if(gop_seen) abspos=lastAbsGop;								fprintf(out," %010llX %10lx",								abspos-4,demuxer->getOtherSize()); 								}							if(gop_seen)							{								lastI=lastGop-4;								gop_seen=0;							}							else															lastI=pos-4;							nb_iframe++;							break;						}						break;					default:					   break;					}		}stop_found://	 	fprintf(out," %d\n\n",demuxer->getSize()-lastI-4);	 	fprintf(out," 0 \n\n");       printf("\n end of stream...\n");	  // update # of frames	    fseek(out,0,SEEK_SET);	  	fprintf(out,"IDXM ");		if(token==0xb3) fprintf(out,"E %02x\n",audiostreamid);			else   fprintf(out,"P %02x\n",audiostreamid);		fprintf(out,"%012lX\n",nb_iframe);		fclose(out);		delete work;	  printf("\n Total : %lu frames\n",total_frame);	  if(demuxer->getPTSDelta()!=0)	  {	  	char string[1000];		sprintf(string,"We have a shift of %d ms between audio and video\n",demuxer->getPTSDelta());	  	GUI_Alert(string);	  }	  delete demuxer;	  return 1;}