49 #include <SDL_thread.h>
61 #define MAX_QUEUE_SIZE (15 * 1024 * 1024)
62 #define MIN_AUDIOQ_SIZE (20 * 16 * 1024)
67 #define SDL_AUDIO_BUFFER_SIZE 1024
70 #define AV_SYNC_THRESHOLD 0.01
72 #define AV_NOSYNC_THRESHOLD 10.0
74 #define FRAME_SKIP_FACTOR 0.05
77 #define SAMPLE_CORRECTION_PERCENT_MAX 10
80 #define AUDIO_DIFF_AVG_NB 20
83 #define SAMPLE_ARRAY_SIZE (2 * 65536)
96 #define VIDEO_PICTURE_QUEUE_SIZE 2
97 #define SUBPICTURE_QUEUE_SIZE 4
264 static char *vfilters =
NULL;
274 #define FF_ALLOC_EVENT (SDL_USEREVENT)
275 #define FF_REFRESH_EVENT (SDL_USEREVENT + 1)
276 #define FF_QUIT_EVENT (SDL_USEREVENT + 2)
291 q->
mutex = SDL_CreateMutex();
292 q->
cond = SDL_CreateCond();
300 SDL_LockMutex(q->
mutex);
310 SDL_UnlockMutex(q->
mutex);
316 SDL_DestroyMutex(q->
mutex);
317 SDL_DestroyCond(q->
cond);
335 SDL_LockMutex(q->
mutex);
346 SDL_CondSignal(q->
cond);
348 SDL_UnlockMutex(q->
mutex);
354 SDL_LockMutex(q->
mutex);
358 SDL_CondSignal(q->
cond);
360 SDL_UnlockMutex(q->
mutex);
369 SDL_LockMutex(q->
mutex);
395 SDL_UnlockMutex(q->
mutex);
400 int x,
int y,
int w,
int h,
int color)
407 SDL_FillRect(screen, &rect, color);
410 #define ALPHA_BLEND(a, oldp, newp, s)\
411 ((((oldp << s) * (255 - (a))) + (newp * (a))) / (255 << s))
413 #define RGBA_IN(r, g, b, a, s)\
415 unsigned int v = ((const uint32_t *)(s))[0];\
416 a = (v >> 24) & 0xff;\
417 r = (v >> 16) & 0xff;\
418 g = (v >> 8) & 0xff;\
422 #define YUVA_IN(y, u, v, a, s, pal)\
424 unsigned int val = ((const uint32_t *)(pal))[*(const uint8_t*)(s)];\
425 a = (val >> 24) & 0xff;\
426 y = (val >> 16) & 0xff;\
427 u = (val >> 8) & 0xff;\
431 #define YUVA_OUT(d, y, u, v, a)\
433 ((uint32_t *)(d))[0] = (a << 24) | (y << 16) | (u << 8) | v;\
441 int wrap, wrap3, width2, skip2;
442 int y, u,
v, a, u1, v1,
a1, w, h;
443 uint8_t *lum, *cb, *cr;
446 int dstx, dsty, dstw, dsth;
448 dstw = av_clip(rect->
w, 0, imgw);
449 dsth = av_clip(rect->
h, 0, imgh);
450 dstx = av_clip(rect->
x, 0, imgw - dstw);
451 dsty = av_clip(rect->
y, 0, imgh - dsth);
456 width2 = ((dstw + 1) >> 1) + (dstx & ~dstw & 1);
461 pal = (
const uint32_t *)rect->
pict.
data[1];
478 for (w = dstw - (dstx & 1); w >= 2; w -= 2) {
505 p += wrap3 - dstw *
BPP;
506 lum += wrap - dstw - dstx;
507 cb += dst->
linesize[1] - width2 - skip2;
508 cr += dst->
linesize[2] - width2 - skip2;
510 for (h = dsth - (dsty & 1); h >= 2; h -= 2) {
535 for (w = dstw - (dstx & 1); w >= 2; w -= 2) {
567 p += -wrap3 + 2 *
BPP;
590 p += wrap3 + (wrap3 - dstw *
BPP);
591 lum += wrap + (wrap - dstw - dstx);
592 cb += dst->
linesize[1] - width2 - skip2;
593 cr += dst->
linesize[2] - width2 - skip2;
611 for (w = dstw - (dstx & 1); w >= 2; w -= 2) {
657 if (vp->picref->video->pixel_aspect.num == 0)
660 aspect_ratio =
av_q2d(vp->picref->video->pixel_aspect);
671 if (aspect_ratio <= 0.0)
673 aspect_ratio *= (float)vp->
width / (
float)vp->
height;
683 SDL_LockYUVOverlay (vp->
bmp);
685 pict.
data[0] = vp->
bmp->pixels[0];
686 pict.
data[1] = vp->
bmp->pixels[2];
687 pict.
data[2] = vp->
bmp->pixels[1];
697 SDL_UnlockYUVOverlay (vp->
bmp);
705 width = ((int)rint(height * aspect_ratio)) & ~1;
706 if (width > is->
width) {
708 height = ((int)rint(width / aspect_ratio)) & ~1;
713 rect.x = is->
xleft + x;
714 rect.y = is->
ytop + y;
717 SDL_DisplayYUVOverlay(vp->
bmp, &rect);
739 int i, i_start, x, y1, y, ys, delay, n, nb_display_channels;
740 int ch, channels, h, h2, bgcolor, fgcolor;
742 int rdft_bits, nb_freq;
744 for (rdft_bits = 1; (1 << rdft_bits) < 2 * s->
height; rdft_bits++)
746 nb_freq = 1 << (rdft_bits - 1);
750 nb_display_channels = channels;
764 delay += 2 * data_used;
765 if (delay < data_used)
771 for (i = 0; i < 1000; i += channels) {
778 if (h < score && (b ^ c) < 0) {
790 bgcolor = SDL_MapRGB(
screen->format, 0x00, 0x00, 0x00);
796 fgcolor = SDL_MapRGB(
screen->format, 0xff, 0xff, 0xff);
799 h = s->
height / nb_display_channels;
802 for (ch = 0; ch < nb_display_channels; ch++) {
804 y1 = s->
ytop + ch * h + (h / 2);
805 for (x = 0; x < s->
width; x++) {
814 s->
xleft + x, ys, 1, y,
822 fgcolor = SDL_MapRGB(
screen->format, 0x00, 0x00, 0xff);
824 for (ch = 1; ch < nb_display_channels; ch++) {
825 y = s->
ytop + ch * h;
832 nb_display_channels=
FFMIN(nb_display_channels, 2);
842 for (ch = 0; ch < nb_display_channels; ch++) {
843 data[ch] = s->
rdft_data + 2 * nb_freq * ch;
845 for (x = 0; x < 2 * nb_freq; x++) {
846 double w = (x-nb_freq) * (1.0 / nb_freq);
855 for (y = 0; y < s->
height; y++) {
856 double w = 1 / sqrt(nb_freq);
857 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]));
858 int b = (nb_display_channels == 2 ) ? sqrt(w * sqrt(data[1][2 * y + 0] * data[1][2 * y + 0]
859 + data[1][2 * y + 1] * data[1][2 * y + 1])) : a;
862 fgcolor = SDL_MapRGB(
screen->format, a, b, (a + b) / 2);
878 int flags = SDL_HWSURFACE | SDL_ASYNCBLIT | SDL_HWACCEL;
882 else flags |= SDL_RESIZABLE;
891 }
else if (is->out_video_filter && is->out_video_filter->inputs[0]) {
892 w = is->out_video_filter->inputs[0]->w;
893 h = is->out_video_filter->inputs[0]->h;
907 #if defined(__APPLE__) && !SDL_VERSION_ATLEAST(1, 2, 14)
909 screen = SDL_SetVideoMode(w, h, 24, flags);
911 screen = SDL_SetVideoMode(w, h, 0, flags);
914 fprintf(stderr,
"SDL: could not set video mode - exiting\n");
944 event.user.data1 = opaque;
947 SDL_PushEvent(&event);
958 int hw_buf_size, bytes_per_sec;
967 pts -= (double)hw_buf_size / bytes_per_sec;
1038 double delay, sync_threshold, diff;
1042 if (delay <= 0 || delay >= 10.0) {
1062 if (diff <= -sync_threshold)
1064 else if (diff >= sync_threshold)
1070 av_dlog(
NULL,
"video: delay=%0.3f pts=%0.3f A-V=%f\n",
1071 delay, frame_current_pts, -diff);
1094 if (time < vp->target_clock)
1109 if (is->
pictq_size > 1 || time > next_target + 0.5) {
1190 static int64_t last_time;
1192 int aqsize, vqsize, sqsize;
1196 if (!last_time || (cur_time - last_time) >= 30000) {
1209 printf(
"%7.2f A-V:%7.3f s:%3.1f aq=%5dKB vq=%5dKB sq=%5dB f=%"PRId64
"/%"PRId64
" \r",
1237 SDL_FreeYUVOverlay(vp->
bmp);
1245 #if !CONFIG_AVFILTER
1280 SDL_FreeYUVOverlay(vp->
bmp);
1287 vp->
width = is->out_video_filter->inputs[0]->w;
1288 vp->
height = is->out_video_filter->inputs[0]->h;
1289 vp->
pix_fmt = is->out_video_filter->inputs[0]->format;
1299 if (!vp->
bmp || vp->
bmp->pitches[0] < vp->
width) {
1302 fprintf(stderr,
"Error: the video system does not support an image\n"
1303 "size of %dx%d pixels. Try using -lowres or -vf \"scale=w:h\"\n"
1304 "to reduce the image size.\n", vp->
width, vp->
height );
1346 vp->
width != is->out_video_filter->inputs[0]->w ||
1347 vp->
height != is->out_video_filter->inputs[0]->h) {
1360 event.user.data1 = is;
1361 SDL_PushEvent(&event);
1380 vp->picref = src_frame->
opaque;
1384 SDL_LockYUVOverlay (vp->
bmp);
1387 pict.
data[0] = vp->
bmp->pixels[0];
1388 pict.
data[1] = vp->
bmp->pixels[2];
1389 pict.
data[2] = vp->
bmp->pixels[1];
1396 pict_src.
data[0] = src_frame->
data[0];
1397 pict_src.
data[1] = src_frame->
data[1];
1398 pict_src.
data[2] = src_frame->
data[2];
1413 fprintf(stderr,
"Cannot initialize the conversion context\n");
1420 SDL_UnlockYUVOverlay(vp->
bmp);
1443 double frame_delay, pts;
1457 frame_delay += src_frame->
repeat_pict * (frame_delay * 0.5);
1557 for (i = 0; i < 4; i ++) {
1562 ref->
data[i] += ((edge * pixel_size) >> hshift) + ((edge * ref->
linesize[i]) >> vshift);
1577 memset(pic->
data, 0,
sizeof(pic->
data));
1587 return input_get_buffer(codec, pic);
1602 static int input_init(
AVFilterContext *ctx,
const char *args,
void *opaque)
1604 FilterPriv *priv = ctx->
priv;
1606 if (!opaque)
return -1;
1609 codec = priv->is->video_st->
codec;
1626 FilterPriv *priv = ctx->
priv;
1632 FilterPriv *priv = link->
src->
priv;
1643 if (priv->use_dr1) {
1648 priv->frame->data, priv->frame->linesize,
1665 FilterPriv *priv = ctx->
priv;
1676 FilterPriv *priv = link->
src->
priv;
1681 link->
time_base = priv->is->video_st->time_base;
1688 .
name =
"avplay_input",
1690 .priv_size =
sizeof(FilterPriv),
1700 .request_frame = input_request_frame,
1701 .config_props = input_config_props, },
1707 char sws_flags_str[128];
1711 snprintf(sws_flags_str,
sizeof(sws_flags_str),
"flags=%d",
sws_flags);
1715 NULL, is, graph)) < 0)
1718 NULL, &avsink_ctx, graph)) < 0)
1746 is->out_video_filter = filt_out;
1768 if ((ret = configure_video_filters(graph, is, vfilters)) < 0)
1770 filt_out = is->out_video_filter;
1774 #if !CONFIG_AVFILTER
1785 av_dlog(
NULL,
"Changing size %dx%d -> %dx%d\n", last_w, last_h,
1789 if ((ret = configure_video_filters(graph, is, vfilters)) < 0)
1791 filt_out = is->out_video_filter;
1797 pts_int = picref->
pts;
1806 "tb:%d/%d pts:%"PRId64
" -> tb:%d/%d pts:%"PRId64
"\n",
1851 int r,
g,
b, y, u,
v, a;
1883 &got_subtitle, pkt);
1885 if (got_subtitle && sp->
sub.
format == 0) {
1917 size = samples_size /
sizeof(short);
1934 int samples_size1,
double pts)
1936 int n, samples_size;
1940 samples_size = samples_size1;
1945 double diff, avg_diff;
1946 int wanted_size, min_size, max_size, nb_samples;
1962 nb_samples = samples_size / n;
1966 if (wanted_size < min_size)
1967 wanted_size = min_size;
1968 else if (wanted_size > max_size)
1969 wanted_size = max_size;
1972 if (wanted_size < samples_size) {
1974 samples_size = wanted_size;
1975 }
else if (wanted_size > samples_size) {
1976 uint8_t *samples_end, *q;
1980 nb = (samples_size - wanted_size);
1981 samples_end = (uint8_t *)samples + samples_size - n;
1982 q = samples_end + n;
1984 memcpy(q, samples_end, n);
1988 samples_size = wanted_size;
1991 av_dlog(
NULL,
"diff=%f adiff=%f sample_diff=%d apts=%0.3f vpts=%0.3f %f\n",
1992 diff, avg_diff, samples_size - samples_size1,
2003 return samples_size;
2012 int n, len1, data_size, got_frame;
2015 int flush_complete = 0;
2019 while (pkt_temp->
size > 0 || (!pkt_temp->
data && new_packet)) {
2036 pkt_temp->
data += len1;
2037 pkt_temp->
size -= len1;
2055 fprintf(stderr,
"Cannot convert %s sample format to %s sample format\n",
2064 const void *ibuf[6] = { is->
frame->
data[0] };
2067 int ostride[6] = { 2 };
2068 int len= data_size/istride[0];
2075 printf(
"av_audio_convert() failed\n");
2081 data_size = len * 2;
2094 static double last_clock;
2095 printf(
"audio: delay=%0.3f clock=%0.3f pts=%0.3f\n",
2107 memset(pkt_temp, 0,
sizeof(*pkt_temp));
2141 if (audio_size < 0) {
2170 SDL_AudioSpec wanted_spec, spec;
2174 if (stream_index < 0 || stream_index >= ic->
nb_streams)
2208 wanted_spec.format = AUDIO_S16SYS;
2209 wanted_spec.channels = avctx->
channels;
2210 wanted_spec.silence = 0;
2213 wanted_spec.userdata = is;
2214 if (SDL_OpenAudio(&wanted_spec, &spec) < 0) {
2215 fprintf(stderr,
"SDL_OpenAudio: %s\n", SDL_GetError());
2266 if (stream_index < 0 || stream_index >= ic->
nb_streams)
2350 return global_video_state && global_video_state->
abort_request;
2362 int pkt_in_play_range = 0;
2365 int orig_nb_streams;
2367 memset(st_index, -1,
sizeof(st_index));
2372 global_video_state = is;
2397 fprintf(stderr,
"%s: could not find codec parameters\n", is->
filename);
2401 for (i = 0; i < orig_nb_streams; i++)
2421 fprintf(stderr,
"%s: could not seek to position %0.3f\n",
2436 st_index[AVMEDIA_TYPE_VIDEO],
2442 (st_index[AVMEDIA_TYPE_AUDIO] >= 0 ?
2443 st_index[AVMEDIA_TYPE_AUDIO] :
2444 st_index[AVMEDIA_TYPE_VIDEO]),
2451 if (st_index[AVMEDIA_TYPE_AUDIO] >= 0) {
2456 if (st_index[AVMEDIA_TYPE_VIDEO] >= 0) {
2465 if (st_index[AVMEDIA_TYPE_SUBTITLE] >= 0) {
2485 #if CONFIG_RTSP_DEMUXER
2494 int64_t seek_target = is->
seek_pos;
2590 global_video_state =
NULL;
2607 event.user.data1 = is;
2608 SDL_PushEvent(&event);
2644 int start_index, stream_index;
2655 stream_index = start_index;
2666 if (stream_index == start_index)
2668 st = ic->
streams[stream_index];
2671 switch (codec_type) {
2694 #if defined(__APPLE__) && SDL_VERSION_ATLEAST(1, 2, 14)
2723 int bgcolor = SDL_MapRGB(
screen->format, 0x00, 0x00, 0x00);
2736 double incr, pos, frac;
2740 SDL_WaitEvent(&event);
2741 switch (event.type) {
2747 switch (event.key.keysym.sym) {
2814 case SDL_MOUSEBUTTONDOWN:
2819 case SDL_MOUSEMOTION:
2820 if (event.type == SDL_MOUSEBUTTONDOWN) {
2823 if (event.motion.state != SDL_PRESSED)
2834 int tns, thh, tmm, tss;
2837 tmm = (tns % 3600) / 60;
2839 frac = x / cur_stream->
width;
2842 mm = (ns % 3600) / 60;
2844 fprintf(stderr,
"Seek to %2.0f%% (%2d:%02d:%02d) of total duration (%2d:%02d:%02d) \n", frac*100,
2845 hh, mm, ss, thh, tmm, tss);
2853 case SDL_VIDEORESIZE:
2855 screen = SDL_SetVideoMode(event.resize.w, event.resize.h, 0,
2856 SDL_HWSURFACE|SDL_RESIZABLE|SDL_ASYNCBLIT|SDL_HWACCEL);
2882 "Option '%s' has been removed, use private format options instead\n", opt);
2901 if (!file_iformat) {
2902 fprintf(stderr,
"Unknown input format: %s\n", arg);
2911 "Option '%s' has been removed, use private format options instead\n", opt);
2917 if (!strcmp(arg,
"audio"))
2919 else if (!strcmp(arg,
"video"))
2921 else if (!strcmp(arg,
"ext"))
2924 fprintf(stderr,
"Unknown value for %s: %s\n", opt, arg);
2966 {
"ss",
HAS_ARG, { (
void*)&
opt_seek },
"seek to a given position in seconds",
"pos" },
2967 {
"t",
HAS_ARG, { (
void*)&
opt_duration },
"play \"duration\" seconds of audio/video",
"duration" },
2994 {
"vf",
OPT_STRING |
HAS_ARG, { (
void*)&vfilters },
"video filters",
"filter list" },
2998 {
"i", 0, {
NULL },
"avconv compatibility dummy option",
""},
3004 printf(
"Simple media player\n");
3020 #if !CONFIG_AVFILTER
3023 printf(
"\nWhile playing:\n"
3025 "f toggle full screen\n"
3027 "a cycle audio channel\n"
3028 "v cycle video channel\n"
3029 "t cycle subtitle channel\n"
3030 "w show audio waves\n"
3031 "s activate frame-step mode\n"
3032 "left/right seek backward/forward 10 seconds\n"
3033 "down/up seek backward/forward 1 minute\n"
3034 "mouse click seek to percentage in file corresponding to fraction of width\n"
3041 fprintf(stderr,
"Argument '%s' provided as input filename, but '%s' was already specified.\n",
3045 if (!strcmp(filename,
"-"))
3077 fprintf(stderr,
"An input file must be specified\n");
3085 flags = SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER;
3086 #if !defined(__MINGW32__) && !defined(__APPLE__)
3087 flags |= SDL_INIT_EVENTTHREAD;
3089 if (SDL_Init (flags)) {
3090 fprintf(stderr,
"Could not initialize SDL - %s\n", SDL_GetError());
3095 #if HAVE_SDL_VIDEO_SIZE
3096 const SDL_VideoInfo *vi = SDL_GetVideoInfo();
3102 SDL_EventState(SDL_ACTIVEEVENT, SDL_IGNORE);
3103 SDL_EventState(SDL_SYSWMEVENT, SDL_IGNORE);
3104 SDL_EventState(SDL_USEREVENT, SDL_IGNORE);
3107 flush_pkt.
data =
"FLUSH";