Rewrote header, updated source.

+alphabranch


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@32786 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Stephan Aßmus
2009-08-28 14:50:09 +00:00
parent cb3f273dd2
commit 224f4fcd63
2 changed files with 131 additions and 142 deletions
+82 -93
View File
@@ -1,119 +1,108 @@
/***************************************************************** /*
* Copyright 2009, Haiku, Inc. All rights reserved.
File: TimeCode.h * Distributed under the terms of the MIT License.
*/
Description: Time code handling functions and class #ifndef _TIME_CODE_H
Copyright 1998-, Be Incorporated. All Rights Reserved.
*****************************************************************/
#if !defined(_TIME_CODE_H)
#define _TIME_CODE_H #define _TIME_CODE_H
#include <SupportDefs.h> #include <SupportDefs.h>
/* Time code is always in the form HH:MM:SS:FF, it's the definition of FF that varies */ // Time code is always in the form HH:MM:SS:FF, it's the definition of "FF"
// that varies
enum timecode_type { enum timecode_type {
B_TIMECODE_DEFAULT, B_TIMECODE_DEFAULT,
B_TIMECODE_100, B_TIMECODE_100,
B_TIMECODE_75, /* CD */ B_TIMECODE_75, // CD
B_TIMECODE_30, /* MIDI */ B_TIMECODE_30, // MIDI
B_TIMECODE_30_DROP_2, /* NTSC */ B_TIMECODE_30_DROP_2, // NTSC
B_TIMECODE_30_DROP_4, /* Brazil */ B_TIMECODE_30_DROP_4, // Brazil
B_TIMECODE_25, /* PAL */ B_TIMECODE_25, // PAL
B_TIMECODE_24, /* Film */ B_TIMECODE_24, // Film
B_TIMECODE_18 /* Super8 */ B_TIMECODE_18 // Super8
}; };
struct timecode_info { struct timecode_info {
timecode_type type; timecode_type type;
int drop_frames; int drop_frames;
int every_nth; int every_nth;
int except_nth; int except_nth;
int fps_div; int fps_div;
char name[32]; /* for popup menu etc */ char name[32]; // For popup menus and such
char format[32]; /* for sprintf(fmt, h, m, s, f) */ char format[32]; // For sprintf(fmt, h, m, s, f)
char _reserved_[64];
char _reserved_[64];
}; };
status_t us_to_timecode(bigtime_t micros, int * hours, int * minutes, int * seconds, int * frames, const timecode_info * code = NULL);
status_t timecode_to_us(int hours, int minutes, int seconds, int frames, bigtime_t * micros, const timecode_info * code = NULL);
status_t frames_to_timecode(int32 l_frames, int * hours, int * minutes, int * seconds, int * frames, const timecode_info * code = NULL); status_t us_to_timecode(bigtime_t micros, int* hours, int* minutes,
status_t timecode_to_frames(int hours, int minutes, int seconds, int frames, int32 * l_frames, const timecode_info * code = NULL); int* seconds, int* frames, const timecode_info* code = NULL);
status_t get_timecode_description(timecode_type type, timecode_info * out_timecode);
status_t timecode_to_us(int hours, int minutes, int seconds, int frames,
bigtime_t* micros, const timecode_info* code = NULL);
status_t frames_to_timecode(int32 l_frames, int* hours, int* minutes,
int* seconds, int* frames, const timecode_info* code = NULL);
status_t timecode_to_frames(int hours, int minutes, int seconds, int frames,
int32* lFrames, const timecode_info* code = NULL);
status_t get_timecode_description(timecode_type type,
timecode_info* _timecode);
status_t count_timecodes(); status_t count_timecodes();
/* we may want a set_default_timecode, too -- but that's bad from a thread standpoint */
class BTimeCode { class BTimeCode {
public: public:
BTimeCode(); BTimeCode();
BTimeCode( BTimeCode(bigtime_t microSeconds,
bigtime_t us, timecode_type type = B_TIMECODE_DEFAULT);
timecode_type type = B_TIMECODE_DEFAULT); BTimeCode(const BTimeCode& other);
BTimeCode( BTimeCode(int hours, int minutes, int seconds,
const BTimeCode & clone); int frames,
BTimeCode( timecode_type type = B_TIMECODE_DEFAULT);
int hours, ~BTimeCode();
int minutes,
int seconds,
int frames,
timecode_type type = B_TIMECODE_DEFAULT);
~BTimeCode();
void SetData( void SetData(int hours, int minutes, int seconds,
int hours, int frames);
int minutes, status_t SetType(timecode_type type);
int seconds, void SetMicroseconds(bigtime_t microSeconds);
int frames); void SetLinearFrames(int32 linearFrames);
status_t SetType(
timecode_type type);
void SetMicroseconds(
bigtime_t us);
void SetLinearFrames(
int32 linear_frames);
BTimeCode & operator=( BTimeCode& operator=(const BTimeCode& other);
const BTimeCode & clone); bool operator==(const BTimeCode& other) const;
bool operator==( bool operator<(const BTimeCode& other) const;
const BTimeCode & other) const;
bool operator<(
const BTimeCode & other) const;
BTimeCode & operator+=( BTimeCode& operator+=(const BTimeCode& other);
const BTimeCode & other); BTimeCode& operator-=(const BTimeCode& other);
BTimeCode & operator-=(
const BTimeCode & other);
BTimeCode operator+( BTimeCode operator+(const BTimeCode& other) const;
const BTimeCode & other) const; BTimeCode operator-(const BTimeCode& other) const;
BTimeCode operator-(
const BTimeCode & other) const;
int Hours() const; int Hours() const;
int Minutes() const; int Minutes() const;
int Seconds() const; int Seconds() const;
int Frames() const; int Frames() const;
timecode_type Type() const; timecode_type Type() const;
void GetData( void GetData(int* _hours, int* _minutes,
int * out_hours, int* _seconds, int* _frames,
int * out_minutes, timecode_type* _type = NULL) const;
int * out_seconds,
int * out_frames,
timecode_type * out_type = NULL) const;
bigtime_t Microseconds() const; bigtime_t Microseconds() const;
int32 LinearFrames() const; int32 LinearFrames() const;
void GetString(
char * str) const; /* at least 24 bytes */ // Make sure the passed buffer is at least 24 bytes large.
void GetString(char* string) const;
private: private:
int m_hours; int fHours;
int m_minutes; int fMinutes;
int m_seconds; int fSeconds;
int m_frames; int fFrames;
timecode_info m_info; timecode_info fInfo;
}; };
#endif // _TIME_CODE_H
#endif /* _TIME_CODE_H */
+49 -49
View File
@@ -12,7 +12,7 @@ status_t us_to_timecode(bigtime_t micros, int * hours, int * minutes, int * seco
int32 l_frames; int32 l_frames;
CALLED(); CALLED();
if (code) { if (code) {
// We have a valid timecode_info // We have a valid timecode_info
switch (code->type) { switch (code->type) {
@@ -26,7 +26,7 @@ status_t us_to_timecode(bigtime_t micros, int * hours, int * minutes, int * seco
default: default:
l_frames = (((micros % 1000000) * code->fps_div) / 1000000) + (micros / 1000000 * code->fps_div); l_frames = (((micros % 1000000) * code->fps_div) / 1000000) + (micros / 1000000 * code->fps_div);
break; break;
}; };
} else { } else {
// Convert us to frames // Convert us to frames
l_frames = int32((((micros % 1000000) * 29.97) / 1000000) + (micros / 1000000 * 29.97)); l_frames = int32((((micros % 1000000) * 29.97) / 1000000) + (micros / 1000000 * 29.97));
@@ -38,11 +38,11 @@ status_t us_to_timecode(bigtime_t micros, int * hours, int * minutes, int * seco
status_t timecode_to_us(int hours, int minutes, int seconds, int frames, bigtime_t * micros, const timecode_info * code) status_t timecode_to_us(int hours, int minutes, int seconds, int frames, bigtime_t * micros, const timecode_info * code)
{ {
int32 l_frames; int32 l_frames;
CALLED(); CALLED();
if (timecode_to_frames(hours, minutes, seconds, frames, &l_frames, code) == B_OK) { if (timecode_to_frames(hours, minutes, seconds, frames, &l_frames, code) == B_OK) {
if (code) { if (code) {
// We have a valid timecode_info // We have a valid timecode_info
switch (code->type) { switch (code->type) {
@@ -56,7 +56,7 @@ status_t timecode_to_us(int hours, int minutes, int seconds, int frames, bigtime
default: default:
*micros = l_frames * 1000000 / code->fps_div; *micros = l_frames * 1000000 / code->fps_div;
break; break;
}; };
} else { } else {
*micros = bigtime_t(l_frames * ((1000000 / 29.97) + 0.5)); *micros = bigtime_t(l_frames * ((1000000 / 29.97) + 0.5));
@@ -96,11 +96,11 @@ status_t frames_to_timecode(int32 l_frames, int * hours, int * minutes, int * se
while (extra_frames != extra_frames2) { while (extra_frames != extra_frames2) {
l_frames += extra_frames2 - extra_frames; l_frames += extra_frames2 - extra_frames;
extra_frames = extra_frames2; extra_frames = extra_frames2;
total_mins = l_frames / fps_div / 60; total_mins = l_frames / fps_div / 60;
extra_frames2 = code->drop_frames*(total_mins/code->every_nth) - code->drop_frames*(total_mins/code->except_nth); extra_frames2 = code->drop_frames*(total_mins/code->every_nth) - code->drop_frames*(total_mins/code->except_nth);
} }
// l_frames should now include all adjusted frames // l_frames should now include all adjusted frames
} }
} else { } else {
@@ -119,12 +119,12 @@ status_t frames_to_timecode(int32 l_frames, int * hours, int * minutes, int * se
while (extra_frames != extra_frames2) { while (extra_frames != extra_frames2) {
l_frames += extra_frames2 - extra_frames; l_frames += extra_frames2 - extra_frames;
extra_frames = extra_frames2; extra_frames = extra_frames2;
total_mins = l_frames / fps_div / 60; total_mins = l_frames / fps_div / 60;
extra_frames2 = 2*(total_mins) - 2*(total_mins/10); extra_frames2 = 2*(total_mins) - 2*(total_mins/10);
} }
} }
// convert frames to seconds leaving the remainder as frames // convert frames to seconds leaving the remainder as frames
*seconds = l_frames / fps_div; // DIV *seconds = l_frames / fps_div; // DIV
*frames = l_frames % fps_div; // MOD *frames = l_frames % fps_div; // MOD
@@ -146,7 +146,7 @@ status_t timecode_to_frames(int hours, int minutes, int seconds, int frames, int
int32 extra_frames; int32 extra_frames;
CALLED(); CALLED();
if (code) { if (code) {
// We have a valid timecode_info // We have a valid timecode_info
fps_div = code->fps_div; fps_div = code->fps_div;
@@ -158,7 +158,7 @@ status_t timecode_to_frames(int hours, int minutes, int seconds, int frames, int
// Adjust "every_nth" minute we lose "drop_frames" "except_nth" minute // Adjust "every_nth" minute we lose "drop_frames" "except_nth" minute
if (code->every_nth > 0) { if (code->every_nth > 0) {
extra_frames = code->drop_frames*(total_mins/code->every_nth) - code->drop_frames*(total_mins/code->except_nth); extra_frames = code->drop_frames*(total_mins/code->every_nth) - code->drop_frames*(total_mins/code->except_nth);
*l_frames = *l_frames - extra_frames; *l_frames = *l_frames - extra_frames;
} }
} else { } else {
@@ -172,7 +172,7 @@ status_t timecode_to_frames(int hours, int minutes, int seconds, int frames, int
*l_frames = *l_frames - extra_frames; *l_frames = *l_frames - extra_frames;
} }
return B_OK; return B_OK;
} }
@@ -190,7 +190,7 @@ status_t get_timecode_description(timecode_type type, timecode_info * out_timeco
strncpy(out_timecode->name,"100 FPS",31); strncpy(out_timecode->name,"100 FPS",31);
out_timecode->fps_div = 100; out_timecode->fps_div = 100;
break; break;
case B_TIMECODE_75: // CD case B_TIMECODE_75: // CD
strncpy(out_timecode->name,"CD",31); strncpy(out_timecode->name,"CD",31);
out_timecode->fps_div = 75; out_timecode->fps_div = 75;
break; break;
@@ -232,7 +232,7 @@ status_t get_timecode_description(timecode_type type, timecode_info * out_timeco
out_timecode->except_nth = 10; // except every 10 minutes out_timecode->except_nth = 10; // except every 10 minutes
break; break;
} }
return B_OK; return B_OK;
} }
@@ -298,10 +298,10 @@ BTimeCode::SetData(int hours,
int frames) int frames)
{ {
CALLED(); CALLED();
m_hours = hours; fHours = hours;
m_minutes = minutes; fMinutes = minutes;
m_seconds = seconds; fSeconds = seconds;
m_frames = frames; fFrames = frames;
} }
@@ -309,8 +309,8 @@ status_t
BTimeCode::SetType(timecode_type type) BTimeCode::SetType(timecode_type type)
{ {
CALLED(); CALLED();
return get_timecode_description(type, &m_info); return get_timecode_description(type, &fInfo);
} }
@@ -318,8 +318,8 @@ void
BTimeCode::SetMicroseconds(bigtime_t us) BTimeCode::SetMicroseconds(bigtime_t us)
{ {
CALLED(); CALLED();
us_to_timecode(us, &m_hours, &m_minutes, &m_seconds, &m_frames, &m_info); us_to_timecode(us, &fHours, &fMinutes, &fSeconds, &fFrames, &fInfo);
} }
@@ -327,8 +327,8 @@ void
BTimeCode::SetLinearFrames(int32 linear_frames) BTimeCode::SetLinearFrames(int32 linear_frames)
{ {
CALLED(); CALLED();
frames_to_timecode(linear_frames, &m_hours, &m_minutes, &m_seconds, &m_frames, &m_info); frames_to_timecode(linear_frames, &fHours, &fMinutes, &fSeconds, &fFrames, &fInfo);
} }
@@ -336,12 +336,12 @@ BTimeCode &
BTimeCode::operator=(const BTimeCode &clone) BTimeCode::operator=(const BTimeCode &clone)
{ {
CALLED(); CALLED();
m_hours = clone.Hours(); fHours = clone.Hours();
m_minutes = clone.Minutes(); fMinutes = clone.Minutes();
m_seconds = clone.Seconds(); fSeconds = clone.Seconds();
m_frames = clone.Frames(); fFrames = clone.Frames();
SetType(clone.Type()); SetType(clone.Type());
return *this; return *this;
} }
@@ -368,9 +368,9 @@ BTimeCode &
BTimeCode::operator+=(const BTimeCode &other) BTimeCode::operator+=(const BTimeCode &other)
{ {
CALLED(); CALLED();
this->SetLinearFrames(this->LinearFrames() + other.LinearFrames()); this->SetLinearFrames(this->LinearFrames() + other.LinearFrames());
return *this; return *this;
} }
@@ -379,9 +379,9 @@ BTimeCode &
BTimeCode::operator-=(const BTimeCode &other) BTimeCode::operator-=(const BTimeCode &other)
{ {
CALLED(); CALLED();
this->SetLinearFrames(this->LinearFrames() - other.LinearFrames()); this->SetLinearFrames(this->LinearFrames() - other.LinearFrames());
return *this; return *this;
} }
@@ -411,7 +411,7 @@ BTimeCode::Hours() const
{ {
CALLED(); CALLED();
return m_hours; return fHours;
} }
@@ -420,7 +420,7 @@ BTimeCode::Minutes() const
{ {
CALLED(); CALLED();
return m_minutes; return fMinutes;
} }
@@ -429,7 +429,7 @@ BTimeCode::Seconds() const
{ {
CALLED(); CALLED();
return m_seconds; return fSeconds;
} }
@@ -438,7 +438,7 @@ BTimeCode::Frames() const
{ {
CALLED(); CALLED();
return m_frames; return fFrames;
} }
@@ -447,7 +447,7 @@ BTimeCode::Type() const
{ {
CALLED(); CALLED();
return m_info.type; return fInfo.type;
} }
@@ -459,11 +459,11 @@ BTimeCode::GetData(int *out_hours,
timecode_type *out_type) const timecode_type *out_type) const
{ {
CALLED(); CALLED();
*out_hours = m_hours; *out_hours = fHours;
*out_minutes = m_minutes; *out_minutes = fMinutes;
*out_seconds = m_seconds; *out_seconds = fSeconds;
*out_frames = m_frames; *out_frames = fFrames;
*out_type = m_info.type; *out_type = fInfo.type;
} }
@@ -474,10 +474,10 @@ BTimeCode::Microseconds() const
CALLED(); CALLED();
if (timecode_to_us(m_hours,m_minutes,m_seconds,m_frames, &us, &m_info) == B_OK) { if (timecode_to_us(fHours,fMinutes,fSeconds,fFrames, &us, &fInfo) == B_OK) {
return us; return us;
} }
return 0; return 0;
} }
@@ -488,12 +488,12 @@ BTimeCode::LinearFrames() const
int32 linear_frames; int32 linear_frames;
CALLED(); CALLED();
if (timecode_to_frames(m_hours,m_minutes,m_seconds,m_frames,&linear_frames,&m_info) == B_OK) { if (timecode_to_frames(fHours,fMinutes,fSeconds,fFrames,&linear_frames,&fInfo) == B_OK) {
return linear_frames; return linear_frames;
} }
return 0; return 0;
} }
@@ -501,5 +501,5 @@ void
BTimeCode::GetString(char *str) const BTimeCode::GetString(char *str) const
{ {
CALLED(); CALLED();
sprintf(str,m_info.format, m_hours, m_minutes, m_seconds, m_frames); sprintf(str,fInfo.format, fHours, fMinutes, fSeconds, fFrames);
} }