52 #include <SDL_thread.h>
63 #define MAX_QUEUE_SIZE (15 * 1024 * 1024)
64 #define MIN_AUDIOQ_SIZE (20 * 16 * 1024)
69 #define SDL_AUDIO_BUFFER_SIZE 1024
72 #define AV_SYNC_THRESHOLD 0.01
74 #define AV_NOSYNC_THRESHOLD 10.0
76 #define FRAME_SKIP_FACTOR 0.05
79 #define SAMPLE_CORRECTION_PERCENT_MAX 10
82 #define AUDIO_DIFF_AVG_NB 20
85 #define SAMPLE_ARRAY_SIZE (2 * 65536)
98 #define VIDEO_PICTURE_QUEUE_SIZE 2
99 #define SUBPICTURE_QUEUE_SIZE 4
271 static char *vfilters =
NULL;
281 #define FF_ALLOC_EVENT (SDL_USEREVENT)
282 #define FF_REFRESH_EVENT (SDL_USEREVENT + 1)
283 #define FF_QUIT_EVENT (SDL_USEREVENT + 2)
293 q->
mutex = SDL_CreateMutex();
294 q->
cond = SDL_CreateCond();
302 SDL_LockMutex(q->
mutex);
312 SDL_UnlockMutex(q->
mutex);
318 SDL_DestroyMutex(q->
mutex);
319 SDL_DestroyCond(q->
cond);
337 SDL_LockMutex(q->
mutex);
348 SDL_CondSignal(q->
cond);
350 SDL_UnlockMutex(q->
mutex);
356 SDL_LockMutex(q->
mutex);
360 SDL_CondSignal(q->
cond);
362 SDL_UnlockMutex(q->
mutex);
371 SDL_LockMutex(q->
mutex);
397 SDL_UnlockMutex(q->
mutex);
402 int x,
int y,
int w,
int h,
int color)
409 SDL_FillRect(screen, &rect, color);
412 #define ALPHA_BLEND(a, oldp, newp, s)\
413 ((((oldp << s) * (255 - (a))) + (newp * (a))) / (255 << s))
415 #define RGBA_IN(r, g, b, a, s)\
417 unsigned int v = ((const uint32_t *)(s))[0];\
418 a = (v >> 24) & 0xff;\
419 r = (v >> 16) & 0xff;\
420 g = (v >> 8) & 0xff;\
424 #define YUVA_IN(y, u, v, a, s, pal)\
426 unsigned int val = ((const uint32_t *)(pal))[*(const uint8_t*)(s)];\
427 a = (val >> 24) & 0xff;\
428 y = (val >> 16) & 0xff;\
429 u = (val >> 8) & 0xff;\
433 #define YUVA_OUT(d, y, u, v, a)\
435 ((uint32_t *)(d))[0] = (a << 24) | (y << 16) | (u << 8) | v;\
443 int wrap, wrap3, width2, skip2;
444 int y, u, v,
a, u1, v1,
a1, w, h;
448 int dstx, dsty, dstw, dsth;
450 dstw = av_clip(rect->
w, 0, imgw);
451 dsth = av_clip(rect->
h, 0, imgh);
452 dstx = av_clip(rect->
x, 0, imgw - dstw);
453 dsty = av_clip(rect->
y, 0, imgh - dsth);
458 width2 = ((dstw + 1) >> 1) + (dstx & ~dstw & 1);
463 pal = (
const uint32_t *)rect->
pict.
data[1];
480 for (w = dstw - (dstx & 1); w >= 2; w -= 2) {
507 p += wrap3 - dstw *
BPP;
508 lum += wrap - dstw - dstx;
509 cb += dst->
linesize[1] - width2 - skip2;
510 cr += dst->
linesize[2] - width2 - skip2;
512 for (h = dsth - (dsty & 1); h >= 2; h -= 2) {
537 for (w = dstw - (dstx & 1); w >= 2; w -= 2) {
569 p += -wrap3 + 2 *
BPP;
592 p += wrap3 + (wrap3 - dstw *
BPP);
593 lum += wrap + (wrap - dstw - dstx);
594 cb += dst->
linesize[1] - width2 - skip2;
595 cr += dst->
linesize[2] - width2 - skip2;
613 for (w = dstw - (dstx & 1); w >= 2; w -= 2) {
659 if (vp->picref->video->pixel_aspect.num == 0)
662 aspect_ratio =
av_q2d(vp->picref->video->pixel_aspect);
673 if (aspect_ratio <= 0.0)
675 aspect_ratio *= (float)vp->
width / (
float)vp->
height;
685 SDL_LockYUVOverlay (vp->
bmp);
687 pict.
data[0] = vp->
bmp->pixels[0];
688 pict.
data[1] = vp->
bmp->pixels[2];
689 pict.
data[2] = vp->
bmp->pixels[1];
699 SDL_UnlockYUVOverlay (vp->
bmp);
707 width = ((int)
rint(height * aspect_ratio)) & ~1;
708 if (width > is->
width) {
710 height = ((int)
rint(width / aspect_ratio)) & ~1;
715 rect.x = is->
xleft + x;
716 rect.y = is->
ytop + y;
719 SDL_DisplayYUVOverlay(vp->
bmp, &rect);
741 int i, i_start, x, y1, y, ys, delay, n, nb_display_channels;
742 int ch, channels, h, h2, bgcolor, fgcolor;
744 int rdft_bits, nb_freq;
746 for (rdft_bits = 1; (1 << rdft_bits) < 2 * s->
height; rdft_bits++)
748 nb_freq = 1 << (rdft_bits - 1);
752 nb_display_channels = channels;
766 delay += 2 * data_used;
767 if (delay < data_used)
773 for (i = 0; i < 1000; i += channels) {
780 if (h < score && (b ^ c) < 0) {
792 bgcolor = SDL_MapRGB(
screen->format, 0x00, 0x00, 0x00);
798 fgcolor = SDL_MapRGB(
screen->format, 0xff, 0xff, 0xff);
801 h = s->
height / nb_display_channels;
804 for (ch = 0; ch < nb_display_channels; ch++) {
806 y1 = s->
ytop + ch * h + (h / 2);
807 for (x = 0; x < s->
width; x++) {
816 s->
xleft + x, ys, 1, y,
824 fgcolor = SDL_MapRGB(
screen->format, 0x00, 0x00, 0xff);
826 for (ch = 1; ch < nb_display_channels; ch++) {
827 y = s->
ytop + ch * h;
834 nb_display_channels=
FFMIN(nb_display_channels, 2);
844 for (ch = 0; ch < nb_display_channels; ch++) {
845 data[ch] = s->
rdft_data + 2 * nb_freq * ch;
847 for (x = 0; x < 2 * nb_freq; x++) {
848 double w = (x-nb_freq) * (1.0 / nb_freq);
857 for (y = 0; y < s->
height; y++) {
858 double w = 1 / sqrt(nb_freq);
859 int a = sqrt(w * sqrt(data[0][2 * y + 0] * data[0][2 * y + 0] + data[0][2 * y + 1] * data[0][2 * y + 1]));
860 int b = (nb_display_channels == 2 ) ? sqrt(w * sqrt(data[1][2 * y + 0] * data[1][2 * y + 0]
861 + data[1][2 * y + 1] * data[1][2 * y + 1])) : a;
864 fgcolor = SDL_MapRGB(
screen->format, a, b, (a + b) / 2);
880 int flags = SDL_HWSURFACE | SDL_ASYNCBLIT | SDL_HWACCEL;
884 else flags |= SDL_RESIZABLE;
893 }
else if (is->out_video_filter && is->out_video_filter->inputs[0]) {
894 w = is->out_video_filter->inputs[0]->w;
895 h = is->out_video_filter->inputs[0]->h;
909 #if defined(__APPLE__) && !SDL_VERSION_ATLEAST(1, 2, 14)
911 screen = SDL_SetVideoMode(w, h, 24, flags);
913 screen = SDL_SetVideoMode(w, h, 0, flags);
916 fprintf(stderr,
"SDL: could not set video mode - exiting\n");
946 event.user.data1 = opaque;
949 SDL_PushEvent(&event);
960 int hw_buf_size, bytes_per_sec;
969 pts -= (double)hw_buf_size / bytes_per_sec;
1040 double delay, sync_threshold, diff;
1044 if (delay <= 0 || delay >= 10.0) {
1064 if (diff <= -sync_threshold)
1066 else if (diff >= sync_threshold)
1072 av_dlog(
NULL,
"video: delay=%0.3f pts=%0.3f A-V=%f\n",
1073 delay, frame_current_pts, -diff);
1096 if (time < vp->target_clock)
1111 if (is->
pictq_size > 1 || time > next_target + 0.5) {
1192 static int64_t last_time;
1194 int aqsize, vqsize, sqsize;
1198 if (!last_time || (cur_time - last_time) >= 30000) {
1211 printf(
"%7.2f A-V:%7.3f s:%3.1f aq=%5dKB vq=%5dKB sq=%5dB f=%"PRId64
"/%"PRId64
" \r",
1236 SDL_FreeYUVOverlay(vp->
bmp);
1244 #if !CONFIG_AVFILTER
1279 SDL_FreeYUVOverlay(vp->
bmp);
1284 vp->
width = is->out_video_filter->inputs[0]->w;
1285 vp->
height = is->out_video_filter->inputs[0]->h;
1286 vp->
pix_fmt = is->out_video_filter->inputs[0]->format;
1296 if (!vp->
bmp || vp->
bmp->pitches[0] < vp->
width) {
1299 fprintf(stderr,
"Error: the video system does not support an image\n"
1300 "size of %dx%d pixels. Try using -vf \"scale=w:h\"\n"
1301 "to reduce the image size.\n", vp->
width, vp->
height );
1341 vp->
width != is->out_video_filter->inputs[0]->w ||
1342 vp->
height != is->out_video_filter->inputs[0]->h) {
1355 event.user.data1 = is;
1356 SDL_PushEvent(&event);
1374 vp->picref = src_frame->
opaque;
1378 SDL_LockYUVOverlay (vp->
bmp);
1380 pict.
data[0] = vp->
bmp->pixels[0];
1381 pict.
data[1] = vp->
bmp->pixels[2];
1382 pict.
data[2] = vp->
bmp->pixels[1];
1389 pict_src.
data[0] = src_frame->
data[0];
1390 pict_src.
data[1] = src_frame->
data[1];
1391 pict_src.
data[2] = src_frame->
data[2];
1406 fprintf(stderr,
"Cannot initialize the conversion context\n");
1413 SDL_UnlockYUVOverlay(vp->
bmp);
1434 double frame_delay, pts;
1448 frame_delay += src_frame->
repeat_pict * (frame_delay * 0.5);
1512 char sws_flags_str[128];
1513 char buffersrc_args[256];
1518 snprintf(sws_flags_str,
sizeof(sws_flags_str),
"flags=%"PRId64,
sws_flags);
1521 snprintf(buffersrc_args,
sizeof(buffersrc_args),
"%d:%d:%d:%d:%d:%d:%d",
1529 "src", buffersrc_args,
NULL,
1539 "format",
"yuv420p",
NULL, graph)) < 0)
1569 is->in_video_filter = filt_src;
1570 is->out_video_filter = filt_out;
1576 codec->
opaque = &is->buffer_pool;
1600 if ((ret = configure_video_filters(graph, is, vfilters)) < 0)
1602 filt_in = is->in_video_filter;
1603 filt_out = is->out_video_filter;
1626 av_dlog(
NULL,
"Changing size %dx%d -> %dx%d\n", last_w, last_h,
1630 if ((ret = configure_video_filters(graph, is, vfilters)) < 0)
1632 filt_in = is->in_video_filter;
1633 filt_out = is->out_video_filter;
1638 frame->
pts = pts_int;
1666 pts_int = picref->
pts;
1675 "tb:%d/%d pts:%"PRId64
" -> tb:%d/%d pts:%"PRId64
"\n",
1712 int r,
g,
b, y, u, v,
a;
1744 &got_subtitle, pkt);
1746 if (got_subtitle && sp->
sub.
format == 0) {
1778 size = samples_size /
sizeof(short);
1795 int samples_size1,
double pts)
1797 int n, samples_size;
1801 samples_size = samples_size1;
1806 double diff, avg_diff;
1807 int wanted_size, min_size, max_size,
nb_samples;
1823 nb_samples = samples_size / n;
1827 if (wanted_size < min_size)
1828 wanted_size = min_size;
1829 else if (wanted_size > max_size)
1830 wanted_size = max_size;
1833 if (wanted_size < samples_size) {
1835 samples_size = wanted_size;
1836 }
else if (wanted_size > samples_size) {
1841 nb = (samples_size - wanted_size);
1842 samples_end = (
uint8_t *)samples + samples_size - n;
1843 q = samples_end + n;
1845 memcpy(q, samples_end, n);
1849 samples_size = wanted_size;
1852 av_dlog(
NULL,
"diff=%f adiff=%f sample_diff=%d apts=%0.3f vpts=%0.3f %f\n",
1853 diff, avg_diff, samples_size - samples_size1,
1864 return samples_size;
1873 int n, len1, data_size, got_frame;
1876 int flush_complete = 0;
1880 while (pkt_temp->
size > 0 || (!pkt_temp->
data && new_packet)) {
1881 int resample_changed, audio_resample;
1899 pkt_temp->
data += len1;
1900 pkt_temp->
size -= len1;
1920 if ((!is->
avr && audio_resample) || resample_changed) {
1924 else if (audio_resample) {
1927 fprintf(stderr,
"error allocating AVAudioResampleContext\n");
1931 if (audio_resample) {
1940 fprintf(stderr,
"error initializing libavresample\n");
1949 if (audio_resample) {
1951 int out_samples, out_size, out_linesize;
1966 out_linesize, nb_samples,
1970 if (out_samples < 0) {
1971 fprintf(stderr,
"avresample_convert() failed\n");
1988 static double last_clock;
1989 printf(
"audio: delay=%0.3f clock=%0.3f pts=%0.3f\n",
2001 memset(pkt_temp, 0,
sizeof(*pkt_temp));
2037 if (audio_size < 0) {
2066 SDL_AudioSpec wanted_spec, spec;
2070 if (stream_index < 0 || stream_index >= ic->
nb_streams)
2105 fprintf(stderr,
"unable to guess channel layout\n");
2114 wanted_spec.format = AUDIO_S16SYS;
2117 wanted_spec.silence = 0;
2120 wanted_spec.userdata = is;
2121 if (SDL_OpenAudio(&wanted_spec, &spec) < 0) {
2122 fprintf(stderr,
"SDL_OpenAudio: %s\n", SDL_GetError());
2176 if (stream_index < 0 || stream_index >= ic->
nb_streams)
2262 return global_video_state && global_video_state->
abort_request;
2274 int pkt_in_play_range = 0;
2277 int orig_nb_streams;
2279 memset(st_index, -1,
sizeof(st_index));
2284 global_video_state = is;
2309 fprintf(stderr,
"%s: could not find codec parameters\n", is->
filename);
2313 for (i = 0; i < orig_nb_streams; i++)
2333 fprintf(stderr,
"%s: could not seek to position %0.3f\n",
2348 st_index[AVMEDIA_TYPE_VIDEO],
2354 (st_index[AVMEDIA_TYPE_AUDIO] >= 0 ?
2355 st_index[AVMEDIA_TYPE_AUDIO] :
2356 st_index[AVMEDIA_TYPE_VIDEO]),
2363 if (st_index[AVMEDIA_TYPE_AUDIO] >= 0) {
2368 if (st_index[AVMEDIA_TYPE_VIDEO] >= 0) {
2377 if (st_index[AVMEDIA_TYPE_SUBTITLE] >= 0) {
2382 fprintf(stderr,
"%s: could not open codecs\n", is->
filename);
2397 #if CONFIG_RTSP_DEMUXER
2406 int64_t seek_target = is->
seek_pos;
2407 int64_t seek_min = is->
seek_rel > 0 ? seek_target - is->
seek_rel + 2: INT64_MIN;
2408 int64_t seek_max = is->
seek_rel < 0 ? seek_target - is->
seek_rel - 2: INT64_MAX;
2414 fprintf(stderr,
"%s: error while seeking\n", is->
ic->
filename);
2503 global_video_state =
NULL;
2520 event.user.data1 = is;
2521 SDL_PushEvent(&event);
2557 int start_index, stream_index;
2568 stream_index = start_index;
2579 if (stream_index == start_index)
2581 st = ic->
streams[stream_index];
2584 switch (codec_type) {
2606 #if defined(__APPLE__) && SDL_VERSION_ATLEAST(1, 2, 14)
2636 int bgcolor = SDL_MapRGB(
screen->format, 0x00, 0x00, 0x00);
2649 double incr, pos, frac;
2653 SDL_WaitEvent(&event);
2654 switch (event.type) {
2660 switch (event.key.keysym.sym) {
2727 case SDL_MOUSEBUTTONDOWN:
2732 case SDL_MOUSEMOTION:
2733 if (event.type == SDL_MOUSEBUTTONDOWN) {
2736 if (event.motion.state != SDL_PRESSED)
2747 int tns, thh, tmm, tss;
2750 tmm = (tns % 3600) / 60;
2752 frac = x / cur_stream->
width;
2755 mm = (ns % 3600) / 60;
2757 fprintf(stderr,
"Seek to %2.0f%% (%2d:%02d:%02d) of total duration (%2d:%02d:%02d) \n", frac*100,
2758 hh, mm, ss, thh, tmm, tss);
2766 case SDL_VIDEORESIZE:
2768 screen = SDL_SetVideoMode(event.resize.w, event.resize.h, 0,
2769 SDL_HWSURFACE|SDL_RESIZABLE|SDL_ASYNCBLIT|SDL_HWACCEL);
2795 "Option '%s' has been removed, use private format options instead\n", opt);
2799 static int opt_width(
void *optctx,
const char *opt,
const char *arg)
2805 static int opt_height(
void *optctx,
const char *opt,
const char *arg)
2811 static int opt_format(
void *optctx,
const char *opt,
const char *arg)
2814 if (!file_iformat) {
2815 fprintf(stderr,
"Unknown input format: %s\n", arg);
2824 "Option '%s' has been removed, use private format options instead\n", opt);
2828 static int opt_sync(
void *optctx,
const char *opt,
const char *arg)
2830 if (!strcmp(arg,
"audio"))
2832 else if (!strcmp(arg,
"video"))
2834 else if (!strcmp(arg,
"ext"))
2837 fprintf(stderr,
"Unknown value for %s: %s\n", opt, arg);
2843 static int opt_seek(
void *optctx,
const char *opt,
const char *arg)
2855 static int opt_debug(
void *optctx,
const char *opt,
const char *arg)
2862 static int opt_vismv(
void *optctx,
const char *opt,
const char *arg)
2870 {
"x",
HAS_ARG, { .func_arg =
opt_width },
"force displayed width",
"width" },
2871 {
"y",
HAS_ARG, { .func_arg =
opt_height },
"force displayed height",
"height" },
2879 {
"ss",
HAS_ARG, { .func_arg =
opt_seek },
"seek to a given position in seconds",
"pos" },
2880 {
"t",
HAS_ARG, { .func_arg =
opt_duration },
"play \"duration\" seconds of audio/video",
"duration" },
2897 {
"sync",
HAS_ARG |
OPT_EXPERT, { .func_arg =
opt_sync },
"set audio-video sync. type (type=audio/video/ext)",
"type" },
2910 {
"i", 0, {
NULL },
"avconv compatibility dummy option",
""},
2916 printf(
"Simple media player\n");
2917 printf(
"usage: %s [options] input_file\n",
program_name);
2930 #if !CONFIG_AVFILTER
2933 printf(
"\nWhile playing:\n"
2935 "f toggle full screen\n"
2937 "a cycle audio channel\n"
2938 "v cycle video channel\n"
2939 "t cycle subtitle channel\n"
2940 "w show audio waves\n"
2941 "s activate frame-step mode\n"
2942 "left/right seek backward/forward 10 seconds\n"
2943 "down/up seek backward/forward 1 minute\n"
2944 "mouse click seek to percentage in file corresponding to fraction of width\n"
2951 fprintf(stderr,
"Argument '%s' provided as input filename, but '%s' was already specified.\n",
2955 if (!strcmp(filename,
"-"))
2987 fprintf(stderr,
"An input file must be specified\n");
2988 fprintf(stderr,
"Use -h to get full help or, even better, run 'man %s'\n",
program_name);
2995 flags = SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER;
2996 #if !defined(__MINGW32__) && !defined(__APPLE__)
2997 flags |= SDL_INIT_EVENTTHREAD;
2999 if (SDL_Init (flags)) {
3000 fprintf(stderr,
"Could not initialize SDL - %s\n", SDL_GetError());
3005 #if HAVE_SDL_VIDEO_SIZE
3006 const SDL_VideoInfo *vi = SDL_GetVideoInfo();
3012 SDL_EventState(SDL_ACTIVEEVENT, SDL_IGNORE);
3013 SDL_EventState(SDL_SYSWMEVENT, SDL_IGNORE);
3014 SDL_EventState(SDL_USEREVENT, SDL_IGNORE);
3017 flush_pkt.
data =
"FLUSH";