aprintf("Left: %lu\n",rbuf-cbuf);
int Mrequant_frame(uint8_t *in, uint32_t len,uint8_t *out, uint32_t *lenout){		// recoding	mean_write=out;;	mean_wrotten=0;	mean_read=in;	mean_read_available=len;	while(mean_read_available+(rbuf-cbuf)>10)	{		// get next start code prefix		found = 0;		while (!found)		{		    if (!byte_stuff) {			LOCK(3)		    } else {			LOCK(6)			if ( (cbuf[0] == 0) && (cbuf[1] == 0) && (cbuf[2] == 0) && (cbuf[3] == 0) && (cbuf[4] == 0) && (cbuf[5] == 0) ) { SEEKR(1) }		    }		    if ( (cbuf[0] == 0) && (cbuf[1] == 0) && (cbuf[2] == 1) ) found = 1; // start code !		    else { COPY(1) } // continue search		}		COPY(3)				// get start code		LOCK(1)		ID = cbuf[0];		COPY(1)		if (ID == 0x00) // pic header		{			LOCK(4)			picture_coding_type = (cbuf[1] >> 3) & 0x7;			if (picture_coding_type < 1 || picture_coding_type > 3)			{				DEBF("illegal picture_coding_type: %i\n", picture_coding_type);				validPicHeader = 0;			}			else			{				validPicHeader = 1;				cbuf[1] |= 0x7; cbuf[2] = 0xFF; cbuf[3] |= 0xF8; // vbv_delay is now 0xFFFF			}			COPY(4)		}		else if (ID == 0xB3) // seq header		{			if(!validSeqHeader)				printf("Seq header\n");			LOCK(8)			horizontal_size_value = (cbuf[0] << 4) | (cbuf[1] >> 4);			vertical_size_value = ((cbuf[1] & 0xF) << 8) | cbuf[2];			if (	horizontal_size_value > 720 || horizontal_size_value < 352				||  vertical_size_value > 576 || vertical_size_value < 480				|| (horizontal_size_value & 0xF) || (vertical_size_value & 0xF))			{				DEBF("illegal size, hori: %i verti: %i\n", horizontal_size_value, vertical_size_value);				validSeqHeader = 0;			}			else				validSeqHeader = 1;			COPY(8)		}		else if (ID == 0xB5) // extension		{			LOCK(1)			if ((cbuf[0] >> 4) == 0x8) // pic coding ext			{				LOCK(5)				f_code[0][0] = (cbuf[0] & 0xF) - 1;				f_code[0][1] = (cbuf[1] >> 4) - 1;				f_code[1][0] = (cbuf[1] & 0xF) - 1;				f_code[1][1] = (cbuf[2] >> 4) - 1;								intra_dc_precision = (cbuf[2] >> 2) & 0x3;				picture_structure = cbuf[2] & 0x3;				frame_pred_frame_dct = (cbuf[3] >> 6) & 0x1;				concealment_motion_vectors = (cbuf[3] >> 5) & 0x1;				q_scale_type = (cbuf[3] >> 4) & 0x1;				intra_vlc_format = (cbuf[3] >> 3) & 0x1;				alternate_scan = (cbuf[3] >> 2) & 0x1;								if (	(f_code[0][0] > 8 && f_code[0][0] < 14)					||  (f_code[0][1] > 8 && f_code[0][1] < 14)					||  (f_code[1][0] > 8 && f_code[1][0] < 14)					||  (f_code[1][1] > 8 && f_code[1][1] < 14)					||  picture_structure == 0)				{					DEBF("illegal ext, f_code[0][0]: %i f_code[0][1]: %i f_code[1][0]: %i f_code[1][1]: %i picture_structure:%i\n",							f_code[0][0], f_code[0][1], f_code[1][0], f_code[1][1], picture_structure);					validExtHeader = 0;				}				else					validExtHeader = 1;				COPY(5)			}			else			{				COPY(1)			}		}		else if (ID == 0xB8) // gop header		{			LOCK(4)			COPY(4)		}		else if ((ID >= 0x01) && (ID <= 0xAF) && validPicHeader && validSeqHeader && validExtHeader) // slice		{			uint8 *outTemp = wbuf, *inTemp = cbuf;//			printf("Processing slice %d\n",ID);			quant_corr = (((inbytecnt - (rbuf - cbuf)) / fact_x) - (outbytecnt + (wbuf - owbuf))) / REACT_DELAY;						if 	(		((picture_coding_type == B_TYPE) && (quant_corr < 2.5f)) // don't recompress if we're in advance!					||	((picture_coding_type == P_TYPE) && (quant_corr < -2.5f))					||	((picture_coding_type == I_TYPE) && (quant_corr < -5.0f))				)			{				uint8 *nsc = cbuf;				int fsc = 0, toLock;								// lock all the slice				while (!fsc)				{					toLock = nsc - cbuf + 3;					LOCK(toLock)						if ( (nsc[0] == 0) && (nsc[1] == 0) && (nsc[2] == 1) ) fsc = 1; // start code !					else nsc++; // continue search				}								// init error				sliceError = 0;							// init bit buffer				inbitbuf = 0; inbitcnt = 0;				outbitbuf = 0; outbitcnt = BITS_IN_BUF;								// get 32 bits				Refill_bits();				Refill_bits();				Refill_bits();				Refill_bits();							// begin bit level recoding				mpeg2_slice(ID);				flush_read_buffer();				flush_write_buffer();				// end bit level recoding								/*LOGF("type: %s code: %02i in : %6i out : %6i diff : %6i fact: %2.2f\n",				(picture_coding_type == I_TYPE ? "I_TYPE" : (picture_coding_type == P_TYPE ? "P_TYPE" : "B_TYPE")),				ID,  cbuf - inTemp, wbuf - outTemp, (wbuf - outTemp) - (cbuf - inTemp), (float)(cbuf - inTemp) / (float)(wbuf - outTemp));*/								if ((wbuf - outTemp > cbuf - inTemp) || (sliceError > MAX_ERRORS)) // yes that might happen, rarely				{#ifndef NDEBUG					if (sliceError > MAX_ERRORS)					{						DEBF("sliceError (%i) > MAX_ERRORS (%i)\n", sliceError, MAX_ERRORS);					}#endif									/*LOGF("*** slice bigger than before !! (type: %s code: %i in : %i out : %i diff : %i)\n",					(picture_coding_type == I_TYPE ? "I_TYPE" : (picture_coding_type == P_TYPE ? "P_TYPE" : "B_TYPE")),					ID, cbuf - inTemp, wbuf - outTemp, (wbuf - outTemp) - (cbuf - inTemp));*/									// in this case, we'll just use the original slice !					memcpy(outTemp, inTemp, cbuf - inTemp);					wbuf = outTemp + (cbuf - inTemp);										// adjust outbytecnt					outbytecnt -= (wbuf - outTemp) - (cbuf - inTemp);				}				#ifdef STAT				switch(picture_coding_type)				{					case I_TYPE:						ori_i += cbuf - inTemp;						new_i += (wbuf - outTemp > cbuf - inTemp) ? (cbuf - inTemp) : (wbuf - outTemp);						cnt_i ++;						break;											case P_TYPE:						ori_p += cbuf - inTemp;						new_p += (wbuf - outTemp > cbuf - inTemp) ? (cbuf - inTemp) : (wbuf - outTemp);						cnt_p ++;						break;											case B_TYPE:						ori_b += cbuf - inTemp;						new_b += (wbuf - outTemp > cbuf - inTemp) ? (cbuf - inTemp) : (wbuf - outTemp);						cnt_b ++;						break;											default:						assert(0);						break;				}#endif			}		}		#ifndef NDEBUG		if ((ID >= 0x01) && (ID <= 0xAF) && (!validPicHeader || !validSeqHeader || !validExtHeader))		{			if (!validPicHeader) DEBF("missing pic header (%02X)\n", ID);			if (!validSeqHeader) DEBF("missing seq header (%02X)\n", ID);			if (!validExtHeader) DEBF("missing ext header (%02X)\n", ID);		}#endif				if (wbuf - owbuf > MIN_WRITE) { WRITE }	}	// Flush incoming & outgoing buffers_req_exit:		WRITE;	FORCE_LOCK();	assert(!mean_available);		*lenout=mean_wrotten;	return 1;}