/*******************************************************************
                       MPEG time code routine
 *******************************************************************/

#define TIMECODE_C
#include "timecode.h"

void read_timecode(VIDEO_STREAM *in, TIMECODE *out);
__int64 timecode2frame(TIMECODE *in, int fps);

void read_timecode(VIDEO_STREAM *in, TIMECODE *out)
{
	out->drop = get_bits(in, 1);
	out->hh = get_bits(in, 5);
	out->mm = get_bits(in, 6);
	out->padding = get_bits(in, 1);
	out->ss = get_bits(in, 6);
	out->ff = get_bits(in, 6);

	return;
}

__int64 timecode2frame(TIMECODE *in, int fps)
{
	__int64 r,w;

	r = in->hh * 60 * 60 * fps;
	r += in->mm * 60 * fps;
	r += in->ss * fps;
	r += in->ff;

	if(in->drop){
		w = in->hh * 60 + in->mm;
		r -= 2 * w;
		r += 2 * (w/10);
	}

	return r;
}