if((played_frame)>=(max-2))
if((played_frame)>=(max-1))
void GUI_PlayAvi(void){    uint32_t  time_e, time_a = 0;    uint32_t err = 0, acc = 0;    uint32_t max;    uint32_t framelen,flags;    AVDMGenericVideoStream *filter;    uint8_t *buffer;    vids = 0, auds = 0, dauds = 0;    // check we got everything...    if (!avifileinfo)	return;  if((curframe+1)>=  	avifileinfo->nb_frames-1)  {  	 printf("No frame left\n");  	 return;   }    if (avifileinfo->fps1000 == 0)	return;    if (playing)      {	  stop_req = 1;	  return;      }	uint32_t played_frame=0;      uint32_t remaining=avifileinfo->nb_frames-curframe;    if(guiOutputDisplay)    {			filter=getLastVideoFilter(curframe,remaining);			if(mode_preview)			{				editorKillPreview ();				//video_body->desactivateCache();				 UI_setPreviewToggleStatus( 0 );				 mode_preview=0;			}	}		else	{			filter=getFirstVideoFilter(curframe,remaining );	}    max=filter->getInfo()->nb_frames;    // compute how much a frame lasts in ms    one_frame = (uint32_t) floor(10000000 / filter->getInfo()->fps1000);    err = one_frame % 10;    one_frame /= 10;    buffer=new uint8_t [filter->getInfo()->height*filter->getInfo()->width*3];    // go to RealTime...        printf(" One frame : %lu, err=%lu ms\n", one_frame, err);    // read frame in chunk  //  if (!video_body->getUncompressedFrame(curframe + 1, rdr_decomp_buffer))    if(!filter->getFrameNumberNoAlloc(1,&framelen,buffer,&flags))      {	  printf("\n cannot read frame!\n");	  return;      }    	curframe++;	played_frame++;    // prepare 1st frame    stop_req = 0;    playing = 1;#ifdef HAVE_AUDIO    ComputePreload();#endif     renderResize(filter->getInfo()->width,filter->getInfo()->height);     renderStartPlaying();// reset timer reference    resetTime();    do      {	  	vids++;			  	renderUpdateImage(buffer);     	if(mode_preview&&!guiOutputDisplay)     	{        	      		editorUpdatePreview(played_frame);      	}	  update_status_bar(flags);	  if (time_a == 0)	      time_a = getTime(0);	  // mark !	  //printf("\n Rendering %lu frame\n",curframe);	  // read frame in chunk//	if (!video_body->getUncompressedFrame(curframe + 1, rdr_decomp_buffer))	if((played_frame)>=(max-2))	{		printf("\n End met (%lu  / %lu )\n",played_frame,max);		goto abort_play;	}  if(!filter->getFrameNumberNoAlloc(played_frame+1,&framelen,buffer,&flags))    {		printf("\n cannot read frame!\n");  		goto abort_play;	    }	curframe++;	played_frame++;#ifdef HAVE_AUDIO	  FillAudio();#endif	  time_e = getTime(1);	  acc += err;	  if (acc > 10)	    {		acc -= 10;		time_a++;	    }	  time_a += one_frame;	  // delta a is next frame time	  // time is is current time	  delta = time_a - time_e;	  if (delta <= 0)	    {		//if(delta<-19)  // allow 19 ms late without warning...		// tick seems to be ~ 18 ms		//printf("\n Late ....,due : %lu ms / found : %lu \n",		//                                              time_a,time_e); 		// a call to whatever sleep function will last at leat 10 ms		// give some time to GTK                		  } else	    {		// a call to whatever sleep function will last at leat 10 ms		// give some time to GTK                				if (delta > 10)		    GUI_Sleep(delta - 10);	    }     	//      		 uint16_t  p;            if(mode_preview)		      		p=3;       	  else          		p=1;	     	for(;p>0;p--)    			UI_purge();      }    while (!stop_req);abort_play:		// ___________________________________    // Flush buffer       // go back to normal display mode    //____________________________________    playing = 0;	  delete [] buffer;	   renderStopPlaying();	   renderResize(avifileinfo->width ,  avifileinfo->height);	   getFirstVideoFilter( );	   video_body->getUncompressedFrame(curframe, rdr_decomp_buffer,&flags);	   renderUpdateImage(rdr_decomp_buffer);	   renderRefresh();     	   update_status_bar(flags);	   if(mode_preview)	   {		editorUpdatePreview(curframe);	   }#ifdef HAVE_AUDIO    if (currentaudiostream)      {	  if (wavbuf)	      free(wavbuf);	  deleteAudioFilter();	  currentaudiostream->endDecompress();	  AVDM_AudioClose();      }#endif    // done.};