/*******************************************************************
                       MPEG time code routine
 *******************************************************************/

#define TIMECODE_C
#include "timecode.h"

void read_timecode(MPEG_IO *in, TIMECODE *out);
unsigned int timecode2frame(TIMECODE *in, int fps);

void read_timecode(MPEG_IO *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;
}

unsigned int timecode2frame(TIMECODE *in, int fps)
{
	unsigned int r;

	r = in->hh * 60 * 60 * fps;
	r += in->mm * 60 * fps;
	r += in->ss * fps;
	r += in->ff;

	if(in->drop){
		r -= in->mm * 2;
	}

	return r;
}