Skip to content

Commit d344b45

Browse files
committed
intial commit
Signed-off-by: Stephen Rhodes <sr99622@gmail.com>
0 parents  commit d344b45

47 files changed

Lines changed: 11320 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Clock.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#include "Clock.h"
2+
3+
Clock::Clock()
4+
{
5+
6+
}
7+
8+
double Clock::get_clock()
9+
{
10+
if (*queue_serial != serial)
11+
return NAN;
12+
if (paused) {
13+
return pts;
14+
}
15+
else {
16+
double time = av_gettime_relative() / 1000000.0;
17+
return pts_drift + time - (time - last_updated) * (1.0 - speed);
18+
}
19+
}
20+
21+
void Clock::set_clock_at(double pts, int serial, double time)
22+
{
23+
this->pts = pts;
24+
this->last_updated = time;
25+
this->pts_drift = this->pts - time;
26+
this->serial = serial;
27+
}
28+
29+
void Clock::set_clock(double pts, int serial)
30+
{
31+
double time = av_gettime_relative() / 1000000.0;
32+
set_clock_at(pts, serial, time);
33+
}
34+
35+
void Clock::set_clock_speed(double speed)
36+
{
37+
set_clock(get_clock(), serial);
38+
this->speed = speed;
39+
}
40+
41+
void Clock::init_clock(int* queue_serial)
42+
{
43+
speed = 1.0;
44+
paused = 0;
45+
this->queue_serial = queue_serial;
46+
set_clock(NAN, -1);
47+
}
48+
49+
void Clock::sync_clock_to_slave(Clock* slave)
50+
{
51+
double clock = get_clock();
52+
double slave_clock = slave->get_clock();
53+
if (!isnan(slave_clock) && (isnan(clock) || fabs(clock - slave_clock) > AV_NOSYNC_THRESHOLD))
54+
set_clock(slave_clock, slave->serial);
55+
}

Clock.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#pragma once
2+
3+
extern "C" {
4+
#include "libavutil/time.h"
5+
#include "libavutil/mathematics.h"
6+
}
7+
8+
#define AV_NOSYNC_THRESHOLD 10.0
9+
10+
class Clock
11+
{
12+
13+
public:
14+
Clock();
15+
double get_clock();
16+
void set_clock_at(double pts, int serial, double time);
17+
void set_clock(double pts, int serial);
18+
void set_clock_speed(double speed);
19+
void init_clock(int* queue_serial);
20+
void sync_clock_to_slave(Clock* slave);
21+
22+
double pts;
23+
double pts_drift;
24+
double last_updated;
25+
double speed;
26+
int serial;
27+
int paused;
28+
int* queue_serial;
29+
};
30+

CommandOptions.cpp

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
#include "CommandOptions.h"
2+
3+
CommandOptions::CommandOptions()
4+
{
5+
}
6+
7+
int CommandOptions::opt_frame_size(void* optctx, const char* opt, const char* arg)
8+
{
9+
av_log(NULL, AV_LOG_WARNING, "Option -s is deprecated, use -video_size.\n");
10+
return opt_default(NULL, "video_size", arg);
11+
}
12+
13+
int CommandOptions::opt_width(void* optctx, const char* opt, const char* arg)
14+
{
15+
screen_width = parse_number_or_die(opt, arg, OPT_INT64, 1, INT_MAX);
16+
return 0;
17+
}
18+
19+
int CommandOptions::opt_height(void* optctx, const char* opt, const char* arg)
20+
{
21+
screen_height = parse_number_or_die(opt, arg, OPT_INT64, 1, INT_MAX);
22+
return 0;
23+
}
24+
25+
int CommandOptions::opt_format(void* optctx, const char* opt, const char* arg)
26+
{
27+
file_iformat = av_find_input_format(arg);
28+
if (!file_iformat) {
29+
av_log(NULL, AV_LOG_FATAL, "Unknown input format: %s\n", arg);
30+
return AVERROR(EINVAL);
31+
}
32+
return 0;
33+
}
34+
35+
int CommandOptions::opt_frame_pix_fmt(void* optctx, const char* opt, const char* arg)
36+
{
37+
av_log(NULL, AV_LOG_WARNING, "Option -pix_fmt is deprecated, use -pixel_format.\n");
38+
return opt_default(NULL, "pixel_format", arg);
39+
}
40+
41+
int CommandOptions::opt_sync(void* optctx, const char* opt, const char* arg)
42+
{
43+
if (!strcmp(arg, "audio"))
44+
av_sync_type = AV_SYNC_AUDIO_MASTER;
45+
else if (!strcmp(arg, "video"))
46+
av_sync_type = AV_SYNC_VIDEO_MASTER;
47+
else if (!strcmp(arg, "ext"))
48+
av_sync_type = AV_SYNC_EXTERNAL_CLOCK;
49+
else {
50+
av_log(NULL, AV_LOG_ERROR, "Unknown value for %s: %s\n", opt, arg);
51+
exit(1);
52+
}
53+
return 0;
54+
}
55+
56+
int CommandOptions::opt_seek(void* optctx, const char* opt, const char* arg)
57+
{
58+
start_time = parse_time_or_die(opt, arg, 1);
59+
return 0;
60+
}
61+
62+
int CommandOptions::opt_duration(void* optctx, const char* opt, const char* arg)
63+
{
64+
duration = parse_time_or_die(opt, arg, 1);
65+
return 0;
66+
}
67+
68+
int CommandOptions::opt_show_mode(void* optctx, const char* opt, const char* arg)
69+
{
70+
show_mode = (ShowMode)(!strcmp(arg, "video") ? SHOW_MODE_VIDEO :
71+
//!strcmp(arg, "waves") ? SHOW_MODE_WAVES :
72+
!strcmp(arg, "rdft") ? SHOW_MODE_RDFT :
73+
parse_number_or_die(opt, arg, OPT_INT, 0, SHOW_MODE_NB - 1));
74+
return 0;
75+
}
76+
77+
void CommandOptions::opt_input_file(void* optctx, const char* filename)
78+
{
79+
if (input_filename) {
80+
av_log(NULL, AV_LOG_FATAL,
81+
"Argument '%s' provided as input filename, but '%s' was already specified.\n",
82+
filename, input_filename);
83+
exit(1);
84+
}
85+
if (!strcmp(filename, "-"))
86+
filename = "pipe:";
87+
input_filename = filename;
88+
}
89+
90+
int CommandOptions::opt_codec(void* optctx, const char* opt, const char* arg)
91+
{
92+
const char* spec = strchr(opt, ':');
93+
if (!spec) {
94+
av_log(NULL, AV_LOG_ERROR,
95+
"No media specifier was specified in '%s' in option '%s'\n",
96+
arg, opt);
97+
return AVERROR(EINVAL);
98+
}
99+
spec++;
100+
switch (spec[0]) {
101+
case 'a': audio_codec_name = arg; break;
102+
case 's': subtitle_codec_name = arg; break;
103+
case 'v': video_codec_name = arg; break;
104+
default:
105+
av_log(NULL, AV_LOG_ERROR,
106+
"Invalid media specifier '%s' in option '%s'\n", spec, opt);
107+
return AVERROR(EINVAL);
108+
}
109+
return 0;
110+
}
111+
112+
#if CONFIG_AVFILTER
113+
int CommandOptions::opt_add_vfilter(void* optctx, const char* opt, const char* arg)
114+
{
115+
GROW_ARRAY(vfilters_list, nb_vfilters);
116+
vfilters_list[nb_vfilters - 1] = arg;
117+
return 0;
118+
}
119+
#endif
120+
121+
void CommandOptions::show_usage(void)
122+
{
123+
av_log(NULL, AV_LOG_INFO, "Simple media player\n");
124+
av_log(NULL, AV_LOG_INFO, "usage: %s [options] input_file\n", program_name);
125+
av_log(NULL, AV_LOG_INFO, "\n");
126+
}
127+

CommandOptions.h

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
#pragma once
2+
3+
extern"C" {
4+
#include "cmdutils.h"
5+
#include "libavutil/opt.h"
6+
}
7+
8+
#include <SDL.h>
9+
10+
//#include "VideoState.h"
11+
12+
//#define CO ((CommandOptions*)command_options)
13+
14+
enum ShowMode {
15+
SHOW_MODE_NONE = -1,
16+
SHOW_MODE_VIDEO = 0,
17+
SHOW_MODE_RDFT,
18+
SHOW_MODE_NB
19+
};
20+
21+
enum SyncMode {
22+
AV_SYNC_AUDIO_MASTER, // default choice
23+
AV_SYNC_VIDEO_MASTER,
24+
AV_SYNC_EXTERNAL_CLOCK, // synchronize to an external clock
25+
};
26+
27+
28+
#define SAMPLE_ARRAY_SIZE (8 * 65536)
29+
#define USE_ONEPASS_SUBTITLE_RENDER 1
30+
#define SDL_AUDIO_MIN_BUFFER_SIZE 512
31+
#define SDL_AUDIO_MAX_CALLBACKS_PER_SEC 30
32+
#define AUDIO_DIFF_AVG_NB 20
33+
#define SAMPLE_CORRECTION_PERCENT_MAX 10
34+
#define EXTERNAL_CLOCK_MIN_FRAMES 2
35+
#define EXTERNAL_CLOCK_MAX_FRAMES 10
36+
#define EXTERNAL_CLOCK_SPEED_MIN 0.900
37+
#define EXTERNAL_CLOCK_SPEED_MAX 1.010
38+
#define EXTERNAL_CLOCK_SPEED_STEP 0.001
39+
40+
class CommandOptions
41+
{
42+
public:
43+
CommandOptions();
44+
45+
static void show_usage(void);
46+
47+
static int opt_frame_size(void* optctx, const char* opt, const char* arg);
48+
static int opt_width(void* optctx, const char* opt, const char* arg);
49+
static int opt_height(void* optctx, const char* opt, const char* arg);
50+
static int opt_format(void* optctx, const char* opt, const char* arg);
51+
static int opt_frame_pix_fmt(void* optctx, const char* opt, const char* arg);
52+
static int opt_sync(void* optctx, const char* opt, const char* arg);
53+
static int opt_seek(void* optctx, const char* opt, const char* arg);
54+
static int opt_duration(void* optctx, const char* opt, const char* arg);
55+
static int opt_show_mode(void* optctx, const char* opt, const char* arg);
56+
static void opt_input_file(void* optctx, const char* filename);
57+
static int opt_codec(void* optctx, const char* opt, const char* arg);
58+
59+
#if CONFIG_AVFILTER
60+
static int opt_add_vfilter(void* optctx, const char* opt, const char* arg);
61+
#endif
62+
63+
inline static int dummy;
64+
65+
inline static AVInputFormat* file_iformat;
66+
inline static const char* input_filename;
67+
inline static const char* window_title;
68+
inline static int default_width = 640;
69+
inline static int default_height = 480;
70+
inline static int screen_width = 0;
71+
inline static int screen_height = 0;
72+
inline static int screen_left = SDL_WINDOWPOS_CENTERED;
73+
inline static int screen_top = SDL_WINDOWPOS_CENTERED;
74+
inline static int audio_disable;
75+
inline static int video_disable;
76+
inline static int subtitle_disable;
77+
inline static const char* wanted_stream_spec[AVMEDIA_TYPE_NB] = { 0 };
78+
inline static int seek_by_bytes = -1;
79+
inline static float seek_interval = 10;
80+
inline static int display_disable;
81+
inline static int borderless;
82+
inline static int startup_volume = 100;
83+
inline static int show_status = 1;
84+
inline static int av_sync_type = AV_SYNC_AUDIO_MASTER;
85+
inline static int64_t start_time = AV_NOPTS_VALUE;
86+
inline static int64_t duration = AV_NOPTS_VALUE;
87+
inline static int fast = 0;
88+
inline static int genpts = 0;
89+
inline static int lowres = 0;
90+
inline static int decoder_reorder_pts = -1;
91+
inline static int autoexit;
92+
inline static int exit_on_keydown;
93+
inline static int exit_on_mousedown;
94+
inline static int loop = 1;
95+
inline static int framedrop = -1;
96+
inline static int infinite_buffer = -1;
97+
inline static enum ShowMode show_mode = SHOW_MODE_NONE;
98+
inline static const char* audio_codec_name;
99+
inline static const char* subtitle_codec_name;
100+
inline static const char* video_codec_name;
101+
inline static double rdftspeed = 0.02;
102+
inline static int64_t cursor_last_shown;
103+
inline static int cursor_hidden = 0;
104+
#if CONFIG_AVFILTER
105+
inline static const char** vfilters_list = NULL;
106+
inline static int nb_vfilters = 0;
107+
inline static char* afilters = NULL;
108+
#endif
109+
inline static int autorotate = 1;
110+
inline static int find_stream_info = 1;
111+
inline static int filter_nbthreads = 0;
112+
113+
/* current context */
114+
inline static int is_full_screen;
115+
inline static int64_t audio_callback_time;
116+
117+
118+
inline static const OptionDef options[] = {
119+
CMDUTILS_COMMON_OPTIONS
120+
{ "x", HAS_ARG, {.func_arg = opt_width }, "force displayed width", "width" },
121+
{ "y", HAS_ARG, {.func_arg = opt_height }, "force displayed height", "height" },
122+
{ "s", HAS_ARG | OPT_VIDEO, {.func_arg = opt_frame_size }, "set frame size (WxH or abbreviation)", "size" },
123+
{ "fs", OPT_BOOL, { &is_full_screen }, "force full screen" },
124+
{ "an", OPT_BOOL, { &audio_disable }, "disable audio" },
125+
{ "vn", OPT_BOOL, { &video_disable }, "disable video" },
126+
{ "sn", OPT_BOOL, { &subtitle_disable }, "disable subtitling" },
127+
{ "ast", OPT_STRING | HAS_ARG | OPT_EXPERT, { &wanted_stream_spec[AVMEDIA_TYPE_AUDIO] }, "select desired audio stream", "stream_specifier" },
128+
{ "vst", OPT_STRING | HAS_ARG | OPT_EXPERT, { &wanted_stream_spec[AVMEDIA_TYPE_VIDEO] }, "select desired video stream", "stream_specifier" },
129+
{ "sst", OPT_STRING | HAS_ARG | OPT_EXPERT, { &wanted_stream_spec[AVMEDIA_TYPE_SUBTITLE] }, "select desired subtitle stream", "stream_specifier" },
130+
{ "ss", HAS_ARG, {.func_arg = opt_seek }, "seek to a given position in seconds", "pos" },
131+
{ "t", HAS_ARG, {.func_arg = opt_duration }, "play \"duration\" seconds of audio/video", "duration" },
132+
{ "bytes", OPT_INT | HAS_ARG, { &seek_by_bytes }, "seek by bytes 0=off 1=on -1=auto", "val" },
133+
{ "seek_interval", OPT_FLOAT | HAS_ARG, { &seek_interval }, "set seek interval for left/right keys, in seconds", "seconds" },
134+
{ "nodisp", OPT_BOOL, { &display_disable }, "disable graphical display" },
135+
{ "noborder", OPT_BOOL, { &borderless }, "borderless window" },
136+
{ "volume", OPT_INT | HAS_ARG, { &startup_volume}, "set startup volume 0=min 100=max", "volume" },
137+
{ "f", HAS_ARG, {.func_arg = opt_format }, "force format", "fmt" },
138+
{ "pix_fmt", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {.func_arg = opt_frame_pix_fmt }, "set pixel format", "format" },
139+
{ "stats", OPT_BOOL | OPT_EXPERT, { &show_status }, "show status", "" },
140+
{ "fast", OPT_BOOL | OPT_EXPERT, { &fast }, "non spec compliant optimizations", "" },
141+
{ "genpts", OPT_BOOL | OPT_EXPERT, { &genpts }, "generate pts", "" },
142+
{ "drp", OPT_INT | HAS_ARG | OPT_EXPERT, { &decoder_reorder_pts }, "let decoder reorder pts 0=off 1=on -1=auto", ""},
143+
{ "lowres", OPT_INT | HAS_ARG | OPT_EXPERT, { &lowres }, "", "" },
144+
{ "sync", HAS_ARG | OPT_EXPERT, {.func_arg = opt_sync }, "set audio-video sync. type (type=audio/video/ext)", "type" },
145+
{ "autoexit", OPT_BOOL | OPT_EXPERT, { &autoexit }, "exit at the end", "" },
146+
{ "exitonkeydown", OPT_BOOL | OPT_EXPERT, { &exit_on_keydown }, "exit on key down", "" },
147+
{ "exitonmousedown", OPT_BOOL | OPT_EXPERT, { &exit_on_mousedown }, "exit on mouse down", "" },
148+
{ "loop", OPT_INT | HAS_ARG | OPT_EXPERT, { &loop }, "set number of times the playback shall be looped", "loop count" },
149+
{ "framedrop", OPT_BOOL | OPT_EXPERT, { &framedrop }, "drop frames when cpu is too slow", "" },
150+
{ "infbuf", OPT_BOOL | OPT_EXPERT, { &infinite_buffer }, "don't limit the input buffer size (useful with realtime streams)", "" },
151+
{ "window_title", OPT_STRING | HAS_ARG, { &window_title }, "set window title", "window title" },
152+
{ "left", OPT_INT | HAS_ARG | OPT_EXPERT, { &screen_left }, "set the x position for the left of the window", "x pos" },
153+
{ "top", OPT_INT | HAS_ARG | OPT_EXPERT, { &screen_top }, "set the y position for the top of the window", "y pos" },
154+
#if CONFIG_AVFILTER
155+
{ "vf", OPT_EXPERT | HAS_ARG, {.func_arg = opt_add_vfilter }, "set video filters", "filter_graph" },
156+
{ "af", OPT_STRING | HAS_ARG, { &afilters }, "set audio filters", "filter_graph" },
157+
#endif
158+
//{ "rdftspeed", OPT_INT | HAS_ARG | OPT_AUDIO | OPT_EXPERT, { &rdftspeed }, "rdft speed", "msecs" },
159+
{ "showmode", HAS_ARG, {.func_arg = opt_show_mode}, "select show mode (0 = video, 1 = waves, 2 = RDFT)", "mode" },
160+
{ "default", HAS_ARG | OPT_AUDIO | OPT_VIDEO | OPT_EXPERT, {.func_arg = opt_default }, "generic catch all option", "" },
161+
{ "i", OPT_BOOL, { &dummy}, "read specified file", "input_file"},
162+
{ "codec", HAS_ARG, {.func_arg = opt_codec}, "force decoder", "decoder_name" },
163+
{ "acodec", HAS_ARG | OPT_STRING | OPT_EXPERT, { &audio_codec_name }, "force audio decoder", "decoder_name" },
164+
{ "scodec", HAS_ARG | OPT_STRING | OPT_EXPERT, { &subtitle_codec_name }, "force subtitle decoder", "decoder_name" },
165+
{ "vcodec", HAS_ARG | OPT_STRING | OPT_EXPERT, { &video_codec_name }, "force video decoder", "decoder_name" },
166+
{ "autorotate", OPT_BOOL, { &autorotate }, "automatically rotate video", "" },
167+
{ "find_stream_info", OPT_BOOL | OPT_INPUT | OPT_EXPERT, { &find_stream_info },
168+
"read and decode the streams to fill missing information with heuristics" },
169+
{ "filter_threads", HAS_ARG | OPT_INT | OPT_EXPERT, { &filter_nbthreads }, "number of filter threads per graph" },
170+
{ NULL, },
171+
};
172+
};
173+

0 commit comments

Comments
 (0)