]> git.sesse.net Git - ffmpeg/blob - ffplay.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / ffplay.c
1 /*
2  * Copyright (c) 2003 Fabrice Bellard
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 /**
22  * @file
23  * simple media player based on the FFmpeg libraries
24  */
25
26 #include "config.h"
27 #include <inttypes.h>
28 #include <math.h>
29 #include <limits.h>
30 #include <signal.h>
31 #include "libavutil/avstring.h"
32 #include "libavutil/colorspace.h"
33 #include "libavutil/mathematics.h"
34 #include "libavutil/pixdesc.h"
35 #include "libavutil/imgutils.h"
36 #include "libavutil/dict.h"
37 #include "libavutil/parseutils.h"
38 #include "libavutil/samplefmt.h"
39 #include "libavutil/avassert.h"
40 #include "libavutil/time.h"
41 #include "libavformat/avformat.h"
42 #include "libavdevice/avdevice.h"
43 #include "libswscale/swscale.h"
44 #include "libavutil/opt.h"
45 #include "libavcodec/avfft.h"
46 #include "libswresample/swresample.h"
47
48 #if CONFIG_AVFILTER
49 # include "libavfilter/avcodec.h"
50 # include "libavfilter/avfilter.h"
51 # include "libavfilter/avfiltergraph.h"
52 # include "libavfilter/buffersink.h"
53 # include "libavfilter/buffersrc.h"
54 #endif
55
56 #include <SDL.h>
57 #include <SDL_thread.h>
58
59 #include "cmdutils.h"
60
61 #include <assert.h>
62
63 const char program_name[] = "ffplay";
64 const int program_birth_year = 2003;
65
66 #define MAX_QUEUE_SIZE (15 * 1024 * 1024)
67 #define MIN_FRAMES 5
68
69 /* SDL audio buffer size, in samples. Should be small to have precise
70    A/V sync as SDL does not have hardware buffer fullness info. */
71 #define SDL_AUDIO_BUFFER_SIZE 1024
72
73 /* no AV sync correction is done if below the AV sync threshold */
74 #define AV_SYNC_THRESHOLD 0.01
75 /* no AV correction is done if too big error */
76 #define AV_NOSYNC_THRESHOLD 10.0
77
78 /* maximum audio speed change to get correct sync */
79 #define SAMPLE_CORRECTION_PERCENT_MAX 10
80
81 /* external clock speed adjustment constants for realtime sources based on buffer fullness */
82 #define EXTERNAL_CLOCK_SPEED_MIN  0.900
83 #define EXTERNAL_CLOCK_SPEED_MAX  1.010
84 #define EXTERNAL_CLOCK_SPEED_STEP 0.001
85
86 /* we use about AUDIO_DIFF_AVG_NB A-V differences to make the average */
87 #define AUDIO_DIFF_AVG_NB   20
88
89 /* polls for possible required screen refresh at least this often, should be less than 1/fps */
90 #define REFRESH_RATE 0.01
91
92 /* NOTE: the size must be big enough to compensate the hardware audio buffersize size */
93 /* TODO: We assume that a decoded and resampled frame fits into this buffer */
94 #define SAMPLE_ARRAY_SIZE (8 * 65536)
95
96 #define CURSOR_HIDE_DELAY 1000000
97
98 static int64_t sws_flags = SWS_BICUBIC;
99
100 typedef struct MyAVPacketList {
101     AVPacket pkt;
102     struct MyAVPacketList *next;
103     int serial;
104 } MyAVPacketList;
105
106 typedef struct PacketQueue {
107     MyAVPacketList *first_pkt, *last_pkt;
108     int nb_packets;
109     int size;
110     int abort_request;
111     int serial;
112     SDL_mutex *mutex;
113     SDL_cond *cond;
114 } PacketQueue;
115
116 #define VIDEO_PICTURE_QUEUE_SIZE 4
117 #define SUBPICTURE_QUEUE_SIZE 4
118
119 typedef struct VideoPicture {
120     double pts;             // presentation timestamp for this picture
121     int64_t pos;            // byte position in file
122     SDL_Overlay *bmp;
123     int width, height; /* source height & width */
124     int allocated;
125     int reallocate;
126     int serial;
127
128     AVRational sar;
129 } VideoPicture;
130
131 typedef struct SubPicture {
132     double pts; /* presentation time stamp for this picture */
133     AVSubtitle sub;
134 } SubPicture;
135
136 typedef struct AudioParams {
137     int freq;
138     int channels;
139     int64_t channel_layout;
140     enum AVSampleFormat fmt;
141 } AudioParams;
142
143 enum {
144     AV_SYNC_AUDIO_MASTER, /* default choice */
145     AV_SYNC_VIDEO_MASTER,
146     AV_SYNC_EXTERNAL_CLOCK, /* synchronize to an external clock */
147 };
148
149 typedef struct VideoState {
150     SDL_Thread *read_tid;
151     SDL_Thread *video_tid;
152     AVInputFormat *iformat;
153     int no_background;
154     int abort_request;
155     int force_refresh;
156     int paused;
157     int last_paused;
158     int queue_attachments_req;
159     int seek_req;
160     int seek_flags;
161     int64_t seek_pos;
162     int64_t seek_rel;
163     int read_pause_return;
164     AVFormatContext *ic;
165     int realtime;
166
167     int audio_stream;
168
169     int av_sync_type;
170     double external_clock;                   ///< external clock base
171     double external_clock_drift;             ///< external clock base - time (av_gettime) at which we updated external_clock
172     int64_t external_clock_time;             ///< last reference time
173     double external_clock_speed;             ///< speed of the external clock
174
175     double audio_clock;
176     int audio_clock_serial;
177     double audio_diff_cum; /* used for AV difference average computation */
178     double audio_diff_avg_coef;
179     double audio_diff_threshold;
180     int audio_diff_avg_count;
181     AVStream *audio_st;
182     PacketQueue audioq;
183     int audio_hw_buf_size;
184     uint8_t silence_buf[SDL_AUDIO_BUFFER_SIZE];
185     uint8_t *audio_buf;
186     uint8_t *audio_buf1;
187     unsigned int audio_buf_size; /* in bytes */
188     unsigned int audio_buf1_size;
189     int audio_buf_index; /* in bytes */
190     int audio_write_buf_size;
191     AVPacket audio_pkt_temp;
192     AVPacket audio_pkt;
193     int audio_pkt_temp_serial;
194     int audio_last_serial;
195     struct AudioParams audio_src;
196 #if CONFIG_AVFILTER
197     struct AudioParams audio_filter_src;
198 #endif
199     struct AudioParams audio_tgt;
200     struct SwrContext *swr_ctx;
201     double audio_current_pts;
202     double audio_current_pts_drift;
203     int frame_drops_early;
204     int frame_drops_late;
205     AVFrame *frame;
206
207     enum ShowMode {
208         SHOW_MODE_NONE = -1, SHOW_MODE_VIDEO = 0, SHOW_MODE_WAVES, SHOW_MODE_RDFT, SHOW_MODE_NB
209     } show_mode;
210     int16_t sample_array[SAMPLE_ARRAY_SIZE];
211     int sample_array_index;
212     int last_i_start;
213     RDFTContext *rdft;
214     int rdft_bits;
215     FFTSample *rdft_data;
216     int xpos;
217     double last_vis_time;
218
219     SDL_Thread *subtitle_tid;
220     int subtitle_stream;
221     int subtitle_stream_changed;
222     AVStream *subtitle_st;
223     PacketQueue subtitleq;
224     SubPicture subpq[SUBPICTURE_QUEUE_SIZE];
225     int subpq_size, subpq_rindex, subpq_windex;
226     SDL_mutex *subpq_mutex;
227     SDL_cond *subpq_cond;
228
229     double frame_timer;
230     double frame_last_pts;
231     double frame_last_duration;
232     double frame_last_dropped_pts;
233     double frame_last_returned_time;
234     double frame_last_filter_delay;
235     int64_t frame_last_dropped_pos;
236     int video_stream;
237     AVStream *video_st;
238     PacketQueue videoq;
239     double video_current_pts;       // current displayed pts
240     double video_current_pts_drift; // video_current_pts - time (av_gettime) at which we updated video_current_pts - used to have running video pts
241     int64_t video_current_pos;      // current displayed file pos
242     double max_frame_duration;      // maximum duration of a frame - above this, we consider the jump a timestamp discontinuity
243     int video_clock_serial;
244     VideoPicture pictq[VIDEO_PICTURE_QUEUE_SIZE];
245     int pictq_size, pictq_rindex, pictq_windex;
246     SDL_mutex *pictq_mutex;
247     SDL_cond *pictq_cond;
248 #if !CONFIG_AVFILTER
249     struct SwsContext *img_convert_ctx;
250 #endif
251     SDL_Rect last_display_rect;
252
253     char filename[1024];
254     int width, height, xleft, ytop;
255     int step;
256
257 #if CONFIG_AVFILTER
258     AVFilterContext *in_video_filter;   // the first filter in the video chain
259     AVFilterContext *out_video_filter;  // the last filter in the video chain
260     AVFilterContext *in_audio_filter;   // the first filter in the audio chain
261     AVFilterContext *out_audio_filter;  // the last filter in the audio chain
262     AVFilterGraph *agraph;              // audio filter graph
263 #endif
264
265     int last_video_stream, last_audio_stream, last_subtitle_stream;
266
267     SDL_cond *continue_read_thread;
268 } VideoState;
269
270 /* options specified by the user */
271 static AVInputFormat *file_iformat;
272 static const char *input_filename;
273 static const char *window_title;
274 static int fs_screen_width;
275 static int fs_screen_height;
276 static int default_width  = 640;
277 static int default_height = 480;
278 static int screen_width  = 0;
279 static int screen_height = 0;
280 static int audio_disable;
281 static int video_disable;
282 static int subtitle_disable;
283 static int wanted_stream[AVMEDIA_TYPE_NB] = {
284     [AVMEDIA_TYPE_AUDIO]    = -1,
285     [AVMEDIA_TYPE_VIDEO]    = -1,
286     [AVMEDIA_TYPE_SUBTITLE] = -1,
287 };
288 static int seek_by_bytes = -1;
289 static int display_disable;
290 static int show_status = 1;
291 static int av_sync_type = AV_SYNC_AUDIO_MASTER;
292 static int64_t start_time = AV_NOPTS_VALUE;
293 static int64_t duration = AV_NOPTS_VALUE;
294 static int workaround_bugs = 1;
295 static int fast = 0;
296 static int genpts = 0;
297 static int lowres = 0;
298 static int idct = FF_IDCT_AUTO;
299 static int error_concealment = 3;
300 static int decoder_reorder_pts = -1;
301 static int autoexit;
302 static int exit_on_keydown;
303 static int exit_on_mousedown;
304 static int loop = 1;
305 static int framedrop = -1;
306 static int infinite_buffer = -1;
307 static enum ShowMode show_mode = SHOW_MODE_NONE;
308 static const char *audio_codec_name;
309 static const char *subtitle_codec_name;
310 static const char *video_codec_name;
311 double rdftspeed = 0.02;
312 static int64_t cursor_last_shown;
313 static int cursor_hidden = 0;
314 #if CONFIG_AVFILTER
315 static char *vfilters = NULL;
316 static char *afilters = NULL;
317 #endif
318
319 /* current context */
320 static int is_full_screen;
321 static int64_t audio_callback_time;
322
323 static AVPacket flush_pkt;
324
325 #define FF_ALLOC_EVENT   (SDL_USEREVENT)
326 #define FF_QUIT_EVENT    (SDL_USEREVENT + 2)
327
328 static SDL_Surface *screen;
329
330 static inline
331 int cmp_audio_fmts(enum AVSampleFormat fmt1, int64_t channel_count1,
332                    enum AVSampleFormat fmt2, int64_t channel_count2)
333 {
334     /* If channel count == 1, planar and non-planar formats are the same */
335     if (channel_count1 == 1 && channel_count2 == 1)
336         return av_get_packed_sample_fmt(fmt1) != av_get_packed_sample_fmt(fmt2);
337     else
338         return channel_count1 != channel_count2 || fmt1 != fmt2;
339 }
340
341 static inline
342 int64_t get_valid_channel_layout(int64_t channel_layout, int channels)
343 {
344     if (channel_layout && av_get_channel_layout_nb_channels(channel_layout) == channels)
345         return channel_layout;
346     else
347         return 0;
348 }
349
350 static int packet_queue_put(PacketQueue *q, AVPacket *pkt);
351
352 static int packet_queue_put_private(PacketQueue *q, AVPacket *pkt)
353 {
354     MyAVPacketList *pkt1;
355
356     if (q->abort_request)
357        return -1;
358
359     pkt1 = av_malloc(sizeof(MyAVPacketList));
360     if (!pkt1)
361         return -1;
362     pkt1->pkt = *pkt;
363     pkt1->next = NULL;
364     if (pkt == &flush_pkt)
365         q->serial++;
366     pkt1->serial = q->serial;
367
368     if (!q->last_pkt)
369         q->first_pkt = pkt1;
370     else
371         q->last_pkt->next = pkt1;
372     q->last_pkt = pkt1;
373     q->nb_packets++;
374     q->size += pkt1->pkt.size + sizeof(*pkt1);
375     /* XXX: should duplicate packet data in DV case */
376     SDL_CondSignal(q->cond);
377     return 0;
378 }
379
380 static int packet_queue_put(PacketQueue *q, AVPacket *pkt)
381 {
382     int ret;
383
384     /* duplicate the packet */
385     if (pkt != &flush_pkt && av_dup_packet(pkt) < 0)
386         return -1;
387
388     SDL_LockMutex(q->mutex);
389     ret = packet_queue_put_private(q, pkt);
390     SDL_UnlockMutex(q->mutex);
391
392     if (pkt != &flush_pkt && ret < 0)
393         av_free_packet(pkt);
394
395     return ret;
396 }
397
398 /* packet queue handling */
399 static void packet_queue_init(PacketQueue *q)
400 {
401     memset(q, 0, sizeof(PacketQueue));
402     q->mutex = SDL_CreateMutex();
403     q->cond = SDL_CreateCond();
404     q->abort_request = 1;
405 }
406
407 static void packet_queue_flush(PacketQueue *q)
408 {
409     MyAVPacketList *pkt, *pkt1;
410
411     SDL_LockMutex(q->mutex);
412     for (pkt = q->first_pkt; pkt != NULL; pkt = pkt1) {
413         pkt1 = pkt->next;
414         av_free_packet(&pkt->pkt);
415         av_freep(&pkt);
416     }
417     q->last_pkt = NULL;
418     q->first_pkt = NULL;
419     q->nb_packets = 0;
420     q->size = 0;
421     SDL_UnlockMutex(q->mutex);
422 }
423
424 static void packet_queue_destroy(PacketQueue *q)
425 {
426     packet_queue_flush(q);
427     SDL_DestroyMutex(q->mutex);
428     SDL_DestroyCond(q->cond);
429 }
430
431 static void packet_queue_abort(PacketQueue *q)
432 {
433     SDL_LockMutex(q->mutex);
434
435     q->abort_request = 1;
436
437     SDL_CondSignal(q->cond);
438
439     SDL_UnlockMutex(q->mutex);
440 }
441
442 static void packet_queue_start(PacketQueue *q)
443 {
444     SDL_LockMutex(q->mutex);
445     q->abort_request = 0;
446     packet_queue_put_private(q, &flush_pkt);
447     SDL_UnlockMutex(q->mutex);
448 }
449
450 /* return < 0 if aborted, 0 if no packet and > 0 if packet.  */
451 static int packet_queue_get(PacketQueue *q, AVPacket *pkt, int block, int *serial)
452 {
453     MyAVPacketList *pkt1;
454     int ret;
455
456     SDL_LockMutex(q->mutex);
457
458     for (;;) {
459         if (q->abort_request) {
460             ret = -1;
461             break;
462         }
463
464         pkt1 = q->first_pkt;
465         if (pkt1) {
466             q->first_pkt = pkt1->next;
467             if (!q->first_pkt)
468                 q->last_pkt = NULL;
469             q->nb_packets--;
470             q->size -= pkt1->pkt.size + sizeof(*pkt1);
471             *pkt = pkt1->pkt;
472             if (serial)
473                 *serial = pkt1->serial;
474             av_free(pkt1);
475             ret = 1;
476             break;
477         } else if (!block) {
478             ret = 0;
479             break;
480         } else {
481             SDL_CondWait(q->cond, q->mutex);
482         }
483     }
484     SDL_UnlockMutex(q->mutex);
485     return ret;
486 }
487
488 static inline void fill_rectangle(SDL_Surface *screen,
489                                   int x, int y, int w, int h, int color, int update)
490 {
491     SDL_Rect rect;
492     rect.x = x;
493     rect.y = y;
494     rect.w = w;
495     rect.h = h;
496     SDL_FillRect(screen, &rect, color);
497     if (update && w > 0 && h > 0)
498         SDL_UpdateRect(screen, x, y, w, h);
499 }
500
501 /* draw only the border of a rectangle */
502 static void fill_border(int xleft, int ytop, int width, int height, int x, int y, int w, int h, int color, int update)
503 {
504     int w1, w2, h1, h2;
505
506     /* fill the background */
507     w1 = x;
508     if (w1 < 0)
509         w1 = 0;
510     w2 = width - (x + w);
511     if (w2 < 0)
512         w2 = 0;
513     h1 = y;
514     if (h1 < 0)
515         h1 = 0;
516     h2 = height - (y + h);
517     if (h2 < 0)
518         h2 = 0;
519     fill_rectangle(screen,
520                    xleft, ytop,
521                    w1, height,
522                    color, update);
523     fill_rectangle(screen,
524                    xleft + width - w2, ytop,
525                    w2, height,
526                    color, update);
527     fill_rectangle(screen,
528                    xleft + w1, ytop,
529                    width - w1 - w2, h1,
530                    color, update);
531     fill_rectangle(screen,
532                    xleft + w1, ytop + height - h2,
533                    width - w1 - w2, h2,
534                    color, update);
535 }
536
537 #define ALPHA_BLEND(a, oldp, newp, s)\
538 ((((oldp << s) * (255 - (a))) + (newp * (a))) / (255 << s))
539
540 #define RGBA_IN(r, g, b, a, s)\
541 {\
542     unsigned int v = ((const uint32_t *)(s))[0];\
543     a = (v >> 24) & 0xff;\
544     r = (v >> 16) & 0xff;\
545     g = (v >> 8) & 0xff;\
546     b = v & 0xff;\
547 }
548
549 #define YUVA_IN(y, u, v, a, s, pal)\
550 {\
551     unsigned int val = ((const uint32_t *)(pal))[*(const uint8_t*)(s)];\
552     a = (val >> 24) & 0xff;\
553     y = (val >> 16) & 0xff;\
554     u = (val >> 8) & 0xff;\
555     v = val & 0xff;\
556 }
557
558 #define YUVA_OUT(d, y, u, v, a)\
559 {\
560     ((uint32_t *)(d))[0] = (a << 24) | (y << 16) | (u << 8) | v;\
561 }
562
563
564 #define BPP 1
565
566 static void blend_subrect(AVPicture *dst, const AVSubtitleRect *rect, int imgw, int imgh)
567 {
568     int wrap, wrap3, width2, skip2;
569     int y, u, v, a, u1, v1, a1, w, h;
570     uint8_t *lum, *cb, *cr;
571     const uint8_t *p;
572     const uint32_t *pal;
573     int dstx, dsty, dstw, dsth;
574
575     dstw = av_clip(rect->w, 0, imgw);
576     dsth = av_clip(rect->h, 0, imgh);
577     dstx = av_clip(rect->x, 0, imgw - dstw);
578     dsty = av_clip(rect->y, 0, imgh - dsth);
579     lum = dst->data[0] + dsty * dst->linesize[0];
580     cb  = dst->data[1] + (dsty >> 1) * dst->linesize[1];
581     cr  = dst->data[2] + (dsty >> 1) * dst->linesize[2];
582
583     width2 = ((dstw + 1) >> 1) + (dstx & ~dstw & 1);
584     skip2 = dstx >> 1;
585     wrap = dst->linesize[0];
586     wrap3 = rect->pict.linesize[0];
587     p = rect->pict.data[0];
588     pal = (const uint32_t *)rect->pict.data[1];  /* Now in YCrCb! */
589
590     if (dsty & 1) {
591         lum += dstx;
592         cb += skip2;
593         cr += skip2;
594
595         if (dstx & 1) {
596             YUVA_IN(y, u, v, a, p, pal);
597             lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
598             cb[0] = ALPHA_BLEND(a >> 2, cb[0], u, 0);
599             cr[0] = ALPHA_BLEND(a >> 2, cr[0], v, 0);
600             cb++;
601             cr++;
602             lum++;
603             p += BPP;
604         }
605         for (w = dstw - (dstx & 1); w >= 2; w -= 2) {
606             YUVA_IN(y, u, v, a, p, pal);
607             u1 = u;
608             v1 = v;
609             a1 = a;
610             lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
611
612             YUVA_IN(y, u, v, a, p + BPP, pal);
613             u1 += u;
614             v1 += v;
615             a1 += a;
616             lum[1] = ALPHA_BLEND(a, lum[1], y, 0);
617             cb[0] = ALPHA_BLEND(a1 >> 2, cb[0], u1, 1);
618             cr[0] = ALPHA_BLEND(a1 >> 2, cr[0], v1, 1);
619             cb++;
620             cr++;
621             p += 2 * BPP;
622             lum += 2;
623         }
624         if (w) {
625             YUVA_IN(y, u, v, a, p, pal);
626             lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
627             cb[0] = ALPHA_BLEND(a >> 2, cb[0], u, 0);
628             cr[0] = ALPHA_BLEND(a >> 2, cr[0], v, 0);
629             p++;
630             lum++;
631         }
632         p += wrap3 - dstw * BPP;
633         lum += wrap - dstw - dstx;
634         cb += dst->linesize[1] - width2 - skip2;
635         cr += dst->linesize[2] - width2 - skip2;
636     }
637     for (h = dsth - (dsty & 1); h >= 2; h -= 2) {
638         lum += dstx;
639         cb += skip2;
640         cr += skip2;
641
642         if (dstx & 1) {
643             YUVA_IN(y, u, v, a, p, pal);
644             u1 = u;
645             v1 = v;
646             a1 = a;
647             lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
648             p += wrap3;
649             lum += wrap;
650             YUVA_IN(y, u, v, a, p, pal);
651             u1 += u;
652             v1 += v;
653             a1 += a;
654             lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
655             cb[0] = ALPHA_BLEND(a1 >> 2, cb[0], u1, 1);
656             cr[0] = ALPHA_BLEND(a1 >> 2, cr[0], v1, 1);
657             cb++;
658             cr++;
659             p += -wrap3 + BPP;
660             lum += -wrap + 1;
661         }
662         for (w = dstw - (dstx & 1); w >= 2; w -= 2) {
663             YUVA_IN(y, u, v, a, p, pal);
664             u1 = u;
665             v1 = v;
666             a1 = a;
667             lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
668
669             YUVA_IN(y, u, v, a, p + BPP, pal);
670             u1 += u;
671             v1 += v;
672             a1 += a;
673             lum[1] = ALPHA_BLEND(a, lum[1], y, 0);
674             p += wrap3;
675             lum += wrap;
676
677             YUVA_IN(y, u, v, a, p, pal);
678             u1 += u;
679             v1 += v;
680             a1 += a;
681             lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
682
683             YUVA_IN(y, u, v, a, p + BPP, pal);
684             u1 += u;
685             v1 += v;
686             a1 += a;
687             lum[1] = ALPHA_BLEND(a, lum[1], y, 0);
688
689             cb[0] = ALPHA_BLEND(a1 >> 2, cb[0], u1, 2);
690             cr[0] = ALPHA_BLEND(a1 >> 2, cr[0], v1, 2);
691
692             cb++;
693             cr++;
694             p += -wrap3 + 2 * BPP;
695             lum += -wrap + 2;
696         }
697         if (w) {
698             YUVA_IN(y, u, v, a, p, pal);
699             u1 = u;
700             v1 = v;
701             a1 = a;
702             lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
703             p += wrap3;
704             lum += wrap;
705             YUVA_IN(y, u, v, a, p, pal);
706             u1 += u;
707             v1 += v;
708             a1 += a;
709             lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
710             cb[0] = ALPHA_BLEND(a1 >> 2, cb[0], u1, 1);
711             cr[0] = ALPHA_BLEND(a1 >> 2, cr[0], v1, 1);
712             cb++;
713             cr++;
714             p += -wrap3 + BPP;
715             lum += -wrap + 1;
716         }
717         p += wrap3 + (wrap3 - dstw * BPP);
718         lum += wrap + (wrap - dstw - dstx);
719         cb += dst->linesize[1] - width2 - skip2;
720         cr += dst->linesize[2] - width2 - skip2;
721     }
722     /* handle odd height */
723     if (h) {
724         lum += dstx;
725         cb += skip2;
726         cr += skip2;
727
728         if (dstx & 1) {
729             YUVA_IN(y, u, v, a, p, pal);
730             lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
731             cb[0] = ALPHA_BLEND(a >> 2, cb[0], u, 0);
732             cr[0] = ALPHA_BLEND(a >> 2, cr[0], v, 0);
733             cb++;
734             cr++;
735             lum++;
736             p += BPP;
737         }
738         for (w = dstw - (dstx & 1); w >= 2; w -= 2) {
739             YUVA_IN(y, u, v, a, p, pal);
740             u1 = u;
741             v1 = v;
742             a1 = a;
743             lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
744
745             YUVA_IN(y, u, v, a, p + BPP, pal);
746             u1 += u;
747             v1 += v;
748             a1 += a;
749             lum[1] = ALPHA_BLEND(a, lum[1], y, 0);
750             cb[0] = ALPHA_BLEND(a1 >> 2, cb[0], u, 1);
751             cr[0] = ALPHA_BLEND(a1 >> 2, cr[0], v, 1);
752             cb++;
753             cr++;
754             p += 2 * BPP;
755             lum += 2;
756         }
757         if (w) {
758             YUVA_IN(y, u, v, a, p, pal);
759             lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
760             cb[0] = ALPHA_BLEND(a >> 2, cb[0], u, 0);
761             cr[0] = ALPHA_BLEND(a >> 2, cr[0], v, 0);
762         }
763     }
764 }
765
766 static void free_subpicture(SubPicture *sp)
767 {
768     avsubtitle_free(&sp->sub);
769 }
770
771 static void calculate_display_rect(SDL_Rect *rect, int scr_xleft, int scr_ytop, int scr_width, int scr_height, VideoPicture *vp)
772 {
773     float aspect_ratio;
774     int width, height, x, y;
775
776     if (vp->sar.num == 0)
777         aspect_ratio = 0;
778     else
779         aspect_ratio = av_q2d(vp->sar);
780
781     if (aspect_ratio <= 0.0)
782         aspect_ratio = 1.0;
783     aspect_ratio *= (float)vp->width / (float)vp->height;
784
785     /* XXX: we suppose the screen has a 1.0 pixel ratio */
786     height = scr_height;
787     width = ((int)rint(height * aspect_ratio)) & ~1;
788     if (width > scr_width) {
789         width = scr_width;
790         height = ((int)rint(width / aspect_ratio)) & ~1;
791     }
792     x = (scr_width - width) / 2;
793     y = (scr_height - height) / 2;
794     rect->x = scr_xleft + x;
795     rect->y = scr_ytop  + y;
796     rect->w = FFMAX(width,  1);
797     rect->h = FFMAX(height, 1);
798 }
799
800 static void video_image_display(VideoState *is)
801 {
802     VideoPicture *vp;
803     SubPicture *sp;
804     AVPicture pict;
805     SDL_Rect rect;
806     int i;
807
808     vp = &is->pictq[is->pictq_rindex];
809     if (vp->bmp) {
810         if (is->subtitle_st) {
811             if (is->subpq_size > 0) {
812                 sp = &is->subpq[is->subpq_rindex];
813
814                 if (vp->pts >= sp->pts + ((float) sp->sub.start_display_time / 1000)) {
815                     SDL_LockYUVOverlay (vp->bmp);
816
817                     pict.data[0] = vp->bmp->pixels[0];
818                     pict.data[1] = vp->bmp->pixels[2];
819                     pict.data[2] = vp->bmp->pixels[1];
820
821                     pict.linesize[0] = vp->bmp->pitches[0];
822                     pict.linesize[1] = vp->bmp->pitches[2];
823                     pict.linesize[2] = vp->bmp->pitches[1];
824
825                     for (i = 0; i < sp->sub.num_rects; i++)
826                         blend_subrect(&pict, sp->sub.rects[i],
827                                       vp->bmp->w, vp->bmp->h);
828
829                     SDL_UnlockYUVOverlay (vp->bmp);
830                 }
831             }
832         }
833
834         calculate_display_rect(&rect, is->xleft, is->ytop, is->width, is->height, vp);
835
836         SDL_DisplayYUVOverlay(vp->bmp, &rect);
837
838         if (rect.x != is->last_display_rect.x || rect.y != is->last_display_rect.y || rect.w != is->last_display_rect.w || rect.h != is->last_display_rect.h || is->force_refresh) {
839             int bgcolor = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00);
840             fill_border(is->xleft, is->ytop, is->width, is->height, rect.x, rect.y, rect.w, rect.h, bgcolor, 1);
841             is->last_display_rect = rect;
842         }
843     }
844 }
845
846 static inline int compute_mod(int a, int b)
847 {
848     return a < 0 ? a%b + b : a%b;
849 }
850
851 static void video_audio_display(VideoState *s)
852 {
853     int i, i_start, x, y1, y, ys, delay, n, nb_display_channels;
854     int ch, channels, h, h2, bgcolor, fgcolor;
855     int64_t time_diff;
856     int rdft_bits, nb_freq;
857
858     for (rdft_bits = 1; (1 << rdft_bits) < 2 * s->height; rdft_bits++)
859         ;
860     nb_freq = 1 << (rdft_bits - 1);
861
862     /* compute display index : center on currently output samples */
863     channels = s->audio_tgt.channels;
864     nb_display_channels = channels;
865     if (!s->paused) {
866         int data_used= s->show_mode == SHOW_MODE_WAVES ? s->width : (2*nb_freq);
867         n = 2 * channels;
868         delay = s->audio_write_buf_size;
869         delay /= n;
870
871         /* to be more precise, we take into account the time spent since
872            the last buffer computation */
873         if (audio_callback_time) {
874             time_diff = av_gettime() - audio_callback_time;
875             delay -= (time_diff * s->audio_tgt.freq) / 1000000;
876         }
877
878         delay += 2 * data_used;
879         if (delay < data_used)
880             delay = data_used;
881
882         i_start= x = compute_mod(s->sample_array_index - delay * channels, SAMPLE_ARRAY_SIZE);
883         if (s->show_mode == SHOW_MODE_WAVES) {
884             h = INT_MIN;
885             for (i = 0; i < 1000; i += channels) {
886                 int idx = (SAMPLE_ARRAY_SIZE + x - i) % SAMPLE_ARRAY_SIZE;
887                 int a = s->sample_array[idx];
888                 int b = s->sample_array[(idx + 4 * channels) % SAMPLE_ARRAY_SIZE];
889                 int c = s->sample_array[(idx + 5 * channels) % SAMPLE_ARRAY_SIZE];
890                 int d = s->sample_array[(idx + 9 * channels) % SAMPLE_ARRAY_SIZE];
891                 int score = a - d;
892                 if (h < score && (b ^ c) < 0) {
893                     h = score;
894                     i_start = idx;
895                 }
896             }
897         }
898
899         s->last_i_start = i_start;
900     } else {
901         i_start = s->last_i_start;
902     }
903
904     bgcolor = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00);
905     if (s->show_mode == SHOW_MODE_WAVES) {
906         fill_rectangle(screen,
907                        s->xleft, s->ytop, s->width, s->height,
908                        bgcolor, 0);
909
910         fgcolor = SDL_MapRGB(screen->format, 0xff, 0xff, 0xff);
911
912         /* total height for one channel */
913         h = s->height / nb_display_channels;
914         /* graph height / 2 */
915         h2 = (h * 9) / 20;
916         for (ch = 0; ch < nb_display_channels; ch++) {
917             i = i_start + ch;
918             y1 = s->ytop + ch * h + (h / 2); /* position of center line */
919             for (x = 0; x < s->width; x++) {
920                 y = (s->sample_array[i] * h2) >> 15;
921                 if (y < 0) {
922                     y = -y;
923                     ys = y1 - y;
924                 } else {
925                     ys = y1;
926                 }
927                 fill_rectangle(screen,
928                                s->xleft + x, ys, 1, y,
929                                fgcolor, 0);
930                 i += channels;
931                 if (i >= SAMPLE_ARRAY_SIZE)
932                     i -= SAMPLE_ARRAY_SIZE;
933             }
934         }
935
936         fgcolor = SDL_MapRGB(screen->format, 0x00, 0x00, 0xff);
937
938         for (ch = 1; ch < nb_display_channels; ch++) {
939             y = s->ytop + ch * h;
940             fill_rectangle(screen,
941                            s->xleft, y, s->width, 1,
942                            fgcolor, 0);
943         }
944         SDL_UpdateRect(screen, s->xleft, s->ytop, s->width, s->height);
945     } else {
946         nb_display_channels= FFMIN(nb_display_channels, 2);
947         if (rdft_bits != s->rdft_bits) {
948             av_rdft_end(s->rdft);
949             av_free(s->rdft_data);
950             s->rdft = av_rdft_init(rdft_bits, DFT_R2C);
951             s->rdft_bits = rdft_bits;
952             s->rdft_data = av_malloc(4 * nb_freq * sizeof(*s->rdft_data));
953         }
954         {
955             FFTSample *data[2];
956             for (ch = 0; ch < nb_display_channels; ch++) {
957                 data[ch] = s->rdft_data + 2 * nb_freq * ch;
958                 i = i_start + ch;
959                 for (x = 0; x < 2 * nb_freq; x++) {
960                     double w = (x-nb_freq) * (1.0 / nb_freq);
961                     data[ch][x] = s->sample_array[i] * (1.0 - w * w);
962                     i += channels;
963                     if (i >= SAMPLE_ARRAY_SIZE)
964                         i -= SAMPLE_ARRAY_SIZE;
965                 }
966                 av_rdft_calc(s->rdft, data[ch]);
967             }
968             // least efficient way to do this, we should of course directly access it but its more than fast enough
969             for (y = 0; y < s->height; y++) {
970                 double w = 1 / sqrt(nb_freq);
971                 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]));
972                 int b = (nb_display_channels == 2 ) ? sqrt(w * sqrt(data[1][2 * y + 0] * data[1][2 * y + 0]
973                        + data[1][2 * y + 1] * data[1][2 * y + 1])) : a;
974                 a = FFMIN(a, 255);
975                 b = FFMIN(b, 255);
976                 fgcolor = SDL_MapRGB(screen->format, a, b, (a + b) / 2);
977
978                 fill_rectangle(screen,
979                             s->xpos, s->height-y, 1, 1,
980                             fgcolor, 0);
981             }
982         }
983         SDL_UpdateRect(screen, s->xpos, s->ytop, 1, s->height);
984         if (!s->paused)
985             s->xpos++;
986         if (s->xpos >= s->width)
987             s->xpos= s->xleft;
988     }
989 }
990
991 static void stream_close(VideoState *is)
992 {
993     VideoPicture *vp;
994     int i;
995     /* XXX: use a special url_shutdown call to abort parse cleanly */
996     is->abort_request = 1;
997     SDL_WaitThread(is->read_tid, NULL);
998     packet_queue_destroy(&is->videoq);
999     packet_queue_destroy(&is->audioq);
1000     packet_queue_destroy(&is->subtitleq);
1001
1002     /* free all pictures */
1003     for (i = 0; i < VIDEO_PICTURE_QUEUE_SIZE; i++) {
1004         vp = &is->pictq[i];
1005         if (vp->bmp) {
1006             SDL_FreeYUVOverlay(vp->bmp);
1007             vp->bmp = NULL;
1008         }
1009     }
1010     SDL_DestroyMutex(is->pictq_mutex);
1011     SDL_DestroyCond(is->pictq_cond);
1012     SDL_DestroyMutex(is->subpq_mutex);
1013     SDL_DestroyCond(is->subpq_cond);
1014     SDL_DestroyCond(is->continue_read_thread);
1015 #if !CONFIG_AVFILTER
1016     sws_freeContext(is->img_convert_ctx);
1017 #endif
1018     av_free(is);
1019 }
1020
1021 static void do_exit(VideoState *is)
1022 {
1023     if (is) {
1024         stream_close(is);
1025     }
1026     av_lockmgr_register(NULL);
1027     uninit_opts();
1028 #if CONFIG_AVFILTER
1029     avfilter_uninit();
1030     av_freep(&vfilters);
1031 #endif
1032     avformat_network_deinit();
1033     if (show_status)
1034         printf("\n");
1035     SDL_Quit();
1036     av_log(NULL, AV_LOG_QUIET, "%s", "");
1037     exit(0);
1038 }
1039
1040 static void sigterm_handler(int sig)
1041 {
1042     exit(123);
1043 }
1044
1045 static int video_open(VideoState *is, int force_set_video_mode, VideoPicture *vp)
1046 {
1047     int flags = SDL_HWSURFACE | SDL_ASYNCBLIT | SDL_HWACCEL;
1048     int w,h;
1049     SDL_Rect rect;
1050
1051     if (is_full_screen) flags |= SDL_FULLSCREEN;
1052     else                flags |= SDL_RESIZABLE;
1053
1054     if (vp && vp->width) {
1055         calculate_display_rect(&rect, 0, 0, INT_MAX, vp->height, vp);
1056         default_width  = rect.w;
1057         default_height = rect.h;
1058     }
1059
1060     if (is_full_screen && fs_screen_width) {
1061         w = fs_screen_width;
1062         h = fs_screen_height;
1063     } else if (!is_full_screen && screen_width) {
1064         w = screen_width;
1065         h = screen_height;
1066     } else {
1067         w = default_width;
1068         h = default_height;
1069     }
1070     if (screen && is->width == screen->w && screen->w == w
1071        && is->height== screen->h && screen->h == h && !force_set_video_mode)
1072         return 0;
1073     screen = SDL_SetVideoMode(w, h, 0, flags);
1074     if (!screen) {
1075         fprintf(stderr, "SDL: could not set video mode - exiting\n");
1076         do_exit(is);
1077     }
1078     if (!window_title)
1079         window_title = input_filename;
1080     SDL_WM_SetCaption(window_title, window_title);
1081
1082     is->width  = screen->w;
1083     is->height = screen->h;
1084
1085     return 0;
1086 }
1087
1088 /* display the current picture, if any */
1089 static void video_display(VideoState *is)
1090 {
1091     if (!screen)
1092         video_open(is, 0, NULL);
1093     if (is->audio_st && is->show_mode != SHOW_MODE_VIDEO)
1094         video_audio_display(is);
1095     else if (is->video_st)
1096         video_image_display(is);
1097 }
1098
1099 /* get the current audio clock value */
1100 static double get_audio_clock(VideoState *is)
1101 {
1102     if (is->audio_clock_serial != is->audioq.serial)
1103         return NAN;
1104     if (is->paused) {
1105         return is->audio_current_pts;
1106     } else {
1107         return is->audio_current_pts_drift + av_gettime() / 1000000.0;
1108     }
1109 }
1110
1111 /* get the current video clock value */
1112 static double get_video_clock(VideoState *is)
1113 {
1114     if (is->video_clock_serial != is->videoq.serial)
1115         return NAN;
1116     if (is->paused) {
1117         return is->video_current_pts;
1118     } else {
1119         return is->video_current_pts_drift + av_gettime() / 1000000.0;
1120     }
1121 }
1122
1123 /* get the current external clock value */
1124 static double get_external_clock(VideoState *is)
1125 {
1126     if (is->paused) {
1127         return is->external_clock;
1128     } else {
1129         double time = av_gettime() / 1000000.0;
1130         return is->external_clock_drift + time - (time - is->external_clock_time / 1000000.0) * (1.0 - is->external_clock_speed);
1131     }
1132 }
1133
1134 static int get_master_sync_type(VideoState *is) {
1135     if (is->av_sync_type == AV_SYNC_VIDEO_MASTER) {
1136         if (is->video_st)
1137             return AV_SYNC_VIDEO_MASTER;
1138         else
1139             return AV_SYNC_AUDIO_MASTER;
1140     } else if (is->av_sync_type == AV_SYNC_AUDIO_MASTER) {
1141         if (is->audio_st)
1142             return AV_SYNC_AUDIO_MASTER;
1143         else
1144             return AV_SYNC_EXTERNAL_CLOCK;
1145     } else {
1146         return AV_SYNC_EXTERNAL_CLOCK;
1147     }
1148 }
1149
1150 /* get the current master clock value */
1151 static double get_master_clock(VideoState *is)
1152 {
1153     double val;
1154
1155     switch (get_master_sync_type(is)) {
1156         case AV_SYNC_VIDEO_MASTER:
1157             val = get_video_clock(is);
1158             break;
1159         case AV_SYNC_AUDIO_MASTER:
1160             val = get_audio_clock(is);
1161             break;
1162         default:
1163             val = get_external_clock(is);
1164             break;
1165     }
1166     return val;
1167 }
1168
1169 static void update_external_clock_pts(VideoState *is, double pts)
1170 {
1171    is->external_clock_time = av_gettime();
1172    is->external_clock = pts;
1173    is->external_clock_drift = pts - is->external_clock_time / 1000000.0;
1174 }
1175
1176 static void check_external_clock_sync(VideoState *is, double pts) {
1177     double ext_clock = get_external_clock(is);
1178     if (isnan(ext_clock) || fabs(ext_clock - pts) > AV_NOSYNC_THRESHOLD) {
1179         update_external_clock_pts(is, pts);
1180     }
1181 }
1182
1183 static void update_external_clock_speed(VideoState *is, double speed) {
1184     update_external_clock_pts(is, get_external_clock(is));
1185     is->external_clock_speed = speed;
1186 }
1187
1188 static void check_external_clock_speed(VideoState *is) {
1189    if (is->video_stream >= 0 && is->videoq.nb_packets <= MIN_FRAMES / 2 ||
1190        is->audio_stream >= 0 && is->audioq.nb_packets <= MIN_FRAMES / 2) {
1191        update_external_clock_speed(is, FFMAX(EXTERNAL_CLOCK_SPEED_MIN, is->external_clock_speed - EXTERNAL_CLOCK_SPEED_STEP));
1192    } else if ((is->video_stream < 0 || is->videoq.nb_packets > MIN_FRAMES * 2) &&
1193               (is->audio_stream < 0 || is->audioq.nb_packets > MIN_FRAMES * 2)) {
1194        update_external_clock_speed(is, FFMIN(EXTERNAL_CLOCK_SPEED_MAX, is->external_clock_speed + EXTERNAL_CLOCK_SPEED_STEP));
1195    } else {
1196        double speed = is->external_clock_speed;
1197        if (speed != 1.0)
1198            update_external_clock_speed(is, speed + EXTERNAL_CLOCK_SPEED_STEP * (1.0 - speed) / fabs(1.0 - speed));
1199    }
1200 }
1201
1202 /* seek in the stream */
1203 static void stream_seek(VideoState *is, int64_t pos, int64_t rel, int seek_by_bytes)
1204 {
1205     if (!is->seek_req) {
1206         is->seek_pos = pos;
1207         is->seek_rel = rel;
1208         is->seek_flags &= ~AVSEEK_FLAG_BYTE;
1209         if (seek_by_bytes)
1210             is->seek_flags |= AVSEEK_FLAG_BYTE;
1211         is->seek_req = 1;
1212         SDL_CondSignal(is->continue_read_thread);
1213     }
1214 }
1215
1216 /* pause or resume the video */
1217 static void stream_toggle_pause(VideoState *is)
1218 {
1219     if (is->paused) {
1220         is->frame_timer += av_gettime() / 1000000.0 + is->video_current_pts_drift - is->video_current_pts;
1221         if (is->read_pause_return != AVERROR(ENOSYS)) {
1222             is->video_current_pts = is->video_current_pts_drift + av_gettime() / 1000000.0;
1223         }
1224         is->video_current_pts_drift = is->video_current_pts - av_gettime() / 1000000.0;
1225     }
1226     update_external_clock_pts(is, get_external_clock(is));
1227     is->paused = !is->paused;
1228 }
1229
1230 static void toggle_pause(VideoState *is)
1231 {
1232     stream_toggle_pause(is);
1233     is->step = 0;
1234 }
1235
1236 static void step_to_next_frame(VideoState *is)
1237 {
1238     /* if the stream is paused unpause it, then step */
1239     if (is->paused)
1240         stream_toggle_pause(is);
1241     is->step = 1;
1242 }
1243
1244 static double compute_target_delay(double delay, VideoState *is)
1245 {
1246     double sync_threshold, diff;
1247
1248     /* update delay to follow master synchronisation source */
1249     if (get_master_sync_type(is) != AV_SYNC_VIDEO_MASTER) {
1250         /* if video is slave, we try to correct big delays by
1251            duplicating or deleting a frame */
1252         diff = get_video_clock(is) - get_master_clock(is);
1253
1254         /* skip or repeat frame. We take into account the
1255            delay to compute the threshold. I still don't know
1256            if it is the best guess */
1257         sync_threshold = FFMAX(AV_SYNC_THRESHOLD, delay);
1258         if (!isnan(diff) && fabs(diff) < AV_NOSYNC_THRESHOLD) {
1259             if (diff <= -sync_threshold)
1260                 delay = 0;
1261             else if (diff >= sync_threshold)
1262                 delay = 2 * delay;
1263         }
1264     }
1265
1266     av_dlog(NULL, "video: delay=%0.3f A-V=%f\n",
1267             delay, -diff);
1268
1269     return delay;
1270 }
1271
1272 static void pictq_next_picture(VideoState *is) {
1273     /* update queue size and signal for next picture */
1274     if (++is->pictq_rindex == VIDEO_PICTURE_QUEUE_SIZE)
1275         is->pictq_rindex = 0;
1276
1277     SDL_LockMutex(is->pictq_mutex);
1278     is->pictq_size--;
1279     SDL_CondSignal(is->pictq_cond);
1280     SDL_UnlockMutex(is->pictq_mutex);
1281 }
1282
1283 static int pictq_prev_picture(VideoState *is) {
1284     VideoPicture *prevvp;
1285     int ret = 0;
1286     /* update queue size and signal for the previous picture */
1287     prevvp = &is->pictq[(is->pictq_rindex + VIDEO_PICTURE_QUEUE_SIZE - 1) % VIDEO_PICTURE_QUEUE_SIZE];
1288     if (prevvp->allocated && prevvp->serial == is->videoq.serial) {
1289         SDL_LockMutex(is->pictq_mutex);
1290         if (is->pictq_size < VIDEO_PICTURE_QUEUE_SIZE - 1) {
1291             if (--is->pictq_rindex == -1)
1292                 is->pictq_rindex = VIDEO_PICTURE_QUEUE_SIZE - 1;
1293             is->pictq_size++;
1294             ret = 1;
1295         }
1296         SDL_CondSignal(is->pictq_cond);
1297         SDL_UnlockMutex(is->pictq_mutex);
1298     }
1299     return ret;
1300 }
1301
1302 static void update_video_pts(VideoState *is, double pts, int64_t pos, int serial) {
1303     double time = av_gettime() / 1000000.0;
1304     /* update current video pts */
1305     is->video_current_pts = pts;
1306     is->video_current_pts_drift = is->video_current_pts - time;
1307     is->video_current_pos = pos;
1308     is->frame_last_pts = pts;
1309     is->video_clock_serial = serial;
1310     if (is->videoq.serial == serial)
1311         check_external_clock_sync(is, is->video_current_pts);
1312 }
1313
1314 /* called to display each frame */
1315 static void video_refresh(void *opaque, double *remaining_time)
1316 {
1317     VideoState *is = opaque;
1318     VideoPicture *vp;
1319     double time;
1320
1321     SubPicture *sp, *sp2;
1322
1323     if (!is->paused && get_master_sync_type(is) == AV_SYNC_EXTERNAL_CLOCK && is->realtime)
1324         check_external_clock_speed(is);
1325
1326     if (!display_disable && is->show_mode != SHOW_MODE_VIDEO && is->audio_st) {
1327         time = av_gettime() / 1000000.0;
1328         if (is->force_refresh || is->last_vis_time + rdftspeed < time) {
1329             video_display(is);
1330             is->last_vis_time = time;
1331         }
1332         *remaining_time = FFMIN(*remaining_time, is->last_vis_time + rdftspeed - time);
1333     }
1334
1335     if (is->video_st) {
1336         int redisplay = 0;
1337         if (is->force_refresh)
1338             redisplay = pictq_prev_picture(is);
1339 retry:
1340         if (is->pictq_size == 0) {
1341             SDL_LockMutex(is->pictq_mutex);
1342             if (is->frame_last_dropped_pts != AV_NOPTS_VALUE && is->frame_last_dropped_pts > is->frame_last_pts) {
1343                 update_video_pts(is, is->frame_last_dropped_pts, is->frame_last_dropped_pos, 0);
1344                 is->frame_last_dropped_pts = AV_NOPTS_VALUE;
1345             }
1346             SDL_UnlockMutex(is->pictq_mutex);
1347             // nothing to do, no picture to display in the queue
1348         } else {
1349             double last_duration, duration, delay;
1350             /* dequeue the picture */
1351             vp = &is->pictq[is->pictq_rindex];
1352
1353             if (vp->serial != is->videoq.serial) {
1354                 pictq_next_picture(is);
1355                 redisplay = 0;
1356                 goto retry;
1357             }
1358
1359             if (is->paused)
1360                 goto display;
1361
1362             /* compute nominal last_duration */
1363             last_duration = vp->pts - is->frame_last_pts;
1364             if (last_duration > 0 && last_duration < is->max_frame_duration) {
1365                 /* if duration of the last frame was sane, update last_duration in video state */
1366                 is->frame_last_duration = last_duration;
1367             }
1368             delay = compute_target_delay(is->frame_last_duration, is);
1369
1370             time= av_gettime()/1000000.0;
1371             if (time < is->frame_timer + delay) {
1372                 *remaining_time = FFMIN(is->frame_timer + delay - time, *remaining_time);
1373                 return;
1374             }
1375
1376             if (delay > 0)
1377                 is->frame_timer += delay * FFMAX(1, floor((time-is->frame_timer) / delay));
1378
1379             SDL_LockMutex(is->pictq_mutex);
1380             update_video_pts(is, vp->pts, vp->pos, vp->serial);
1381             SDL_UnlockMutex(is->pictq_mutex);
1382
1383             if (is->pictq_size > 1) {
1384                 VideoPicture *nextvp = &is->pictq[(is->pictq_rindex + 1) % VIDEO_PICTURE_QUEUE_SIZE];
1385                 duration = nextvp->pts - vp->pts;
1386                 if(!is->step && (redisplay || framedrop>0 || (framedrop && get_master_sync_type(is) != AV_SYNC_VIDEO_MASTER)) && time > is->frame_timer + duration){
1387                     if (!redisplay)
1388                         is->frame_drops_late++;
1389                     pictq_next_picture(is);
1390                     redisplay = 0;
1391                     goto retry;
1392                 }
1393             }
1394
1395             if (is->subtitle_st) {
1396                 if (is->subtitle_stream_changed) {
1397                     SDL_LockMutex(is->subpq_mutex);
1398
1399                     while (is->subpq_size) {
1400                         free_subpicture(&is->subpq[is->subpq_rindex]);
1401
1402                         /* update queue size and signal for next picture */
1403                         if (++is->subpq_rindex == SUBPICTURE_QUEUE_SIZE)
1404                             is->subpq_rindex = 0;
1405
1406                         is->subpq_size--;
1407                     }
1408                     is->subtitle_stream_changed = 0;
1409
1410                     SDL_CondSignal(is->subpq_cond);
1411                     SDL_UnlockMutex(is->subpq_mutex);
1412                 } else {
1413                     if (is->subpq_size > 0) {
1414                         sp = &is->subpq[is->subpq_rindex];
1415
1416                         if (is->subpq_size > 1)
1417                             sp2 = &is->subpq[(is->subpq_rindex + 1) % SUBPICTURE_QUEUE_SIZE];
1418                         else
1419                             sp2 = NULL;
1420
1421                         if ((is->video_current_pts > (sp->pts + ((float) sp->sub.end_display_time / 1000)))
1422                                 || (sp2 && is->video_current_pts > (sp2->pts + ((float) sp2->sub.start_display_time / 1000))))
1423                         {
1424                             free_subpicture(sp);
1425
1426                             /* update queue size and signal for next picture */
1427                             if (++is->subpq_rindex == SUBPICTURE_QUEUE_SIZE)
1428                                 is->subpq_rindex = 0;
1429
1430                             SDL_LockMutex(is->subpq_mutex);
1431                             is->subpq_size--;
1432                             SDL_CondSignal(is->subpq_cond);
1433                             SDL_UnlockMutex(is->subpq_mutex);
1434                         }
1435                     }
1436                 }
1437             }
1438
1439 display:
1440             /* display picture */
1441             if (!display_disable && is->show_mode == SHOW_MODE_VIDEO)
1442                 video_display(is);
1443
1444             pictq_next_picture(is);
1445
1446             if (is->step && !is->paused)
1447                 stream_toggle_pause(is);
1448         }
1449     }
1450     is->force_refresh = 0;
1451     if (show_status) {
1452         static int64_t last_time;
1453         int64_t cur_time;
1454         int aqsize, vqsize, sqsize;
1455         double av_diff;
1456
1457         cur_time = av_gettime();
1458         if (!last_time || (cur_time - last_time) >= 30000) {
1459             aqsize = 0;
1460             vqsize = 0;
1461             sqsize = 0;
1462             if (is->audio_st)
1463                 aqsize = is->audioq.size;
1464             if (is->video_st)
1465                 vqsize = is->videoq.size;
1466             if (is->subtitle_st)
1467                 sqsize = is->subtitleq.size;
1468             av_diff = 0;
1469             if (is->audio_st && is->video_st)
1470                 av_diff = get_audio_clock(is) - get_video_clock(is);
1471             printf("%7.2f A-V:%7.3f fd=%4d aq=%5dKB vq=%5dKB sq=%5dB f=%"PRId64"/%"PRId64"   \r",
1472                    get_master_clock(is),
1473                    av_diff,
1474                    is->frame_drops_early + is->frame_drops_late,
1475                    aqsize / 1024,
1476                    vqsize / 1024,
1477                    sqsize,
1478                    is->video_st ? is->video_st->codec->pts_correction_num_faulty_dts : 0,
1479                    is->video_st ? is->video_st->codec->pts_correction_num_faulty_pts : 0);
1480             fflush(stdout);
1481             last_time = cur_time;
1482         }
1483     }
1484 }
1485
1486 /* allocate a picture (needs to do that in main thread to avoid
1487    potential locking problems */
1488 static void alloc_picture(VideoState *is)
1489 {
1490     VideoPicture *vp;
1491
1492     vp = &is->pictq[is->pictq_windex];
1493
1494     if (vp->bmp)
1495         SDL_FreeYUVOverlay(vp->bmp);
1496
1497     video_open(is, 0, vp);
1498
1499     vp->bmp = SDL_CreateYUVOverlay(vp->width, vp->height,
1500                                    SDL_YV12_OVERLAY,
1501                                    screen);
1502     if (!vp->bmp || vp->bmp->pitches[0] < vp->width) {
1503         /* SDL allocates a buffer smaller than requested if the video
1504          * overlay hardware is unable to support the requested size. */
1505         fprintf(stderr, "Error: the video system does not support an image\n"
1506                         "size of %dx%d pixels. Try using -lowres or -vf \"scale=w:h\"\n"
1507                         "to reduce the image size.\n", vp->width, vp->height );
1508         do_exit(is);
1509     }
1510
1511     SDL_LockMutex(is->pictq_mutex);
1512     vp->allocated = 1;
1513     SDL_CondSignal(is->pictq_cond);
1514     SDL_UnlockMutex(is->pictq_mutex);
1515 }
1516
1517 static void duplicate_right_border_pixels(SDL_Overlay *bmp) {
1518     int i, width, height;
1519     Uint8 *p, *maxp;
1520     for (i = 0; i < 3; i++) {
1521         width  = bmp->w;
1522         height = bmp->h;
1523         if (i > 0) {
1524             width  >>= 1;
1525             height >>= 1;
1526         }
1527         if (bmp->pitches[i] > width) {
1528             maxp = bmp->pixels[i] + bmp->pitches[i] * height - 1;
1529             for (p = bmp->pixels[i] + width - 1; p < maxp; p += bmp->pitches[i])
1530                 *(p+1) = *p;
1531         }
1532     }
1533 }
1534
1535 static int queue_picture(VideoState *is, AVFrame *src_frame, double pts, int64_t pos, int serial)
1536 {
1537     VideoPicture *vp;
1538
1539 #if defined(DEBUG_SYNC) && 0
1540     printf("frame_type=%c pts=%0.3f\n",
1541            av_get_picture_type_char(src_frame->pict_type), pts);
1542 #endif
1543
1544     /* wait until we have space to put a new picture */
1545     SDL_LockMutex(is->pictq_mutex);
1546
1547     /* keep the last already displayed picture in the queue */
1548     while (is->pictq_size >= VIDEO_PICTURE_QUEUE_SIZE - 2 &&
1549            !is->videoq.abort_request) {
1550         SDL_CondWait(is->pictq_cond, is->pictq_mutex);
1551     }
1552     SDL_UnlockMutex(is->pictq_mutex);
1553
1554     if (is->videoq.abort_request)
1555         return -1;
1556
1557     vp = &is->pictq[is->pictq_windex];
1558
1559 #if CONFIG_AVFILTER
1560     vp->sar = src_frame->sample_aspect_ratio;
1561 #else
1562     vp->sar = av_guess_sample_aspect_ratio(is->ic, is->video_st, src_frame);
1563 #endif
1564
1565     /* alloc or resize hardware picture buffer */
1566     if (!vp->bmp || vp->reallocate || !vp->allocated ||
1567         vp->width  != src_frame->width ||
1568         vp->height != src_frame->height) {
1569         SDL_Event event;
1570
1571         vp->allocated  = 0;
1572         vp->reallocate = 0;
1573         vp->width = src_frame->width;
1574         vp->height = src_frame->height;
1575
1576         /* the allocation must be done in the main thread to avoid
1577            locking problems. */
1578         event.type = FF_ALLOC_EVENT;
1579         event.user.data1 = is;
1580         SDL_PushEvent(&event);
1581
1582         /* wait until the picture is allocated */
1583         SDL_LockMutex(is->pictq_mutex);
1584         while (!vp->allocated && !is->videoq.abort_request) {
1585             SDL_CondWait(is->pictq_cond, is->pictq_mutex);
1586         }
1587         /* if the queue is aborted, we have to pop the pending ALLOC event or wait for the allocation to complete */
1588         if (is->videoq.abort_request && SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_EVENTMASK(FF_ALLOC_EVENT)) != 1) {
1589             while (!vp->allocated) {
1590                 SDL_CondWait(is->pictq_cond, is->pictq_mutex);
1591             }
1592         }
1593         SDL_UnlockMutex(is->pictq_mutex);
1594
1595         if (is->videoq.abort_request)
1596             return -1;
1597     }
1598
1599     /* if the frame is not skipped, then display it */
1600     if (vp->bmp) {
1601         AVPicture pict = { { 0 } };
1602
1603         /* get a pointer on the bitmap */
1604         SDL_LockYUVOverlay (vp->bmp);
1605
1606         pict.data[0] = vp->bmp->pixels[0];
1607         pict.data[1] = vp->bmp->pixels[2];
1608         pict.data[2] = vp->bmp->pixels[1];
1609
1610         pict.linesize[0] = vp->bmp->pitches[0];
1611         pict.linesize[1] = vp->bmp->pitches[2];
1612         pict.linesize[2] = vp->bmp->pitches[1];
1613
1614 #if CONFIG_AVFILTER
1615         // FIXME use direct rendering
1616         av_picture_copy(&pict, (AVPicture *)src_frame,
1617                         src_frame->format, vp->width, vp->height);
1618 #else
1619         av_opt_get_int(sws_opts, "sws_flags", 0, &sws_flags);
1620         is->img_convert_ctx = sws_getCachedContext(is->img_convert_ctx,
1621             vp->width, vp->height, src_frame->format, vp->width, vp->height,
1622             AV_PIX_FMT_YUV420P, sws_flags, NULL, NULL, NULL);
1623         if (is->img_convert_ctx == NULL) {
1624             fprintf(stderr, "Cannot initialize the conversion context\n");
1625             exit(1);
1626         }
1627         sws_scale(is->img_convert_ctx, src_frame->data, src_frame->linesize,
1628                   0, vp->height, pict.data, pict.linesize);
1629 #endif
1630         /* workaround SDL PITCH_WORKAROUND */
1631         duplicate_right_border_pixels(vp->bmp);
1632         /* update the bitmap content */
1633         SDL_UnlockYUVOverlay(vp->bmp);
1634
1635         vp->pts = pts;
1636         vp->pos = pos;
1637         vp->serial = serial;
1638
1639         /* now we can update the picture count */
1640         if (++is->pictq_windex == VIDEO_PICTURE_QUEUE_SIZE)
1641             is->pictq_windex = 0;
1642         SDL_LockMutex(is->pictq_mutex);
1643         is->pictq_size++;
1644         SDL_UnlockMutex(is->pictq_mutex);
1645     }
1646     return 0;
1647 }
1648
1649 static int get_video_frame(VideoState *is, AVFrame *frame, int64_t *pts, AVPacket *pkt, int *serial)
1650 {
1651     int got_picture;
1652
1653     if (packet_queue_get(&is->videoq, pkt, 1, serial) < 0)
1654         return -1;
1655
1656     if (pkt->data == flush_pkt.data) {
1657         avcodec_flush_buffers(is->video_st->codec);
1658
1659         SDL_LockMutex(is->pictq_mutex);
1660         // Make sure there are no long delay timers (ideally we should just flush the queue but that's harder)
1661         while (is->pictq_size && !is->videoq.abort_request) {
1662             SDL_CondWait(is->pictq_cond, is->pictq_mutex);
1663         }
1664         is->video_current_pos = -1;
1665         is->frame_last_pts = AV_NOPTS_VALUE;
1666         is->frame_last_duration = 0;
1667         is->frame_timer = (double)av_gettime() / 1000000.0;
1668         is->frame_last_dropped_pts = AV_NOPTS_VALUE;
1669         SDL_UnlockMutex(is->pictq_mutex);
1670         return 0;
1671     }
1672
1673     if(avcodec_decode_video2(is->video_st->codec, frame, &got_picture, pkt) < 0)
1674         return 0;
1675
1676     if (got_picture) {
1677         int ret = 1;
1678
1679         if (decoder_reorder_pts == -1) {
1680             *pts = av_frame_get_best_effort_timestamp(frame);
1681         } else if (decoder_reorder_pts) {
1682             *pts = frame->pkt_pts;
1683         } else {
1684             *pts = frame->pkt_dts;
1685         }
1686
1687         if (*pts == AV_NOPTS_VALUE) {
1688             *pts = 0;
1689         }
1690
1691         if (framedrop>0 || (framedrop && get_master_sync_type(is) != AV_SYNC_VIDEO_MASTER)) {
1692             SDL_LockMutex(is->pictq_mutex);
1693             if (is->frame_last_pts != AV_NOPTS_VALUE && *pts) {
1694                 double clockdiff = get_video_clock(is) - get_master_clock(is);
1695                 double dpts = av_q2d(is->video_st->time_base) * *pts;
1696                 double ptsdiff = dpts - is->frame_last_pts;
1697                 if (!isnan(clockdiff) && fabs(clockdiff) < AV_NOSYNC_THRESHOLD &&
1698                      ptsdiff > 0 && ptsdiff < AV_NOSYNC_THRESHOLD &&
1699                      clockdiff + ptsdiff - is->frame_last_filter_delay < 0) {
1700                     is->frame_last_dropped_pos = pkt->pos;
1701                     is->frame_last_dropped_pts = dpts;
1702                     is->frame_drops_early++;
1703                     ret = 0;
1704                 }
1705             }
1706             SDL_UnlockMutex(is->pictq_mutex);
1707         }
1708
1709         return ret;
1710     }
1711     return 0;
1712 }
1713
1714 #if CONFIG_AVFILTER
1715 static int configure_filtergraph(AVFilterGraph *graph, const char *filtergraph,
1716                                  AVFilterContext *source_ctx, AVFilterContext *sink_ctx)
1717 {
1718     int ret;
1719     AVFilterInOut *outputs = NULL, *inputs = NULL;
1720
1721     if (filtergraph) {
1722         outputs = avfilter_inout_alloc();
1723         inputs  = avfilter_inout_alloc();
1724         if (!outputs || !inputs) {
1725             ret = AVERROR(ENOMEM);
1726             goto fail;
1727         }
1728
1729         outputs->name       = av_strdup("in");
1730         outputs->filter_ctx = source_ctx;
1731         outputs->pad_idx    = 0;
1732         outputs->next       = NULL;
1733
1734         inputs->name        = av_strdup("out");
1735         inputs->filter_ctx  = sink_ctx;
1736         inputs->pad_idx     = 0;
1737         inputs->next        = NULL;
1738
1739         if ((ret = avfilter_graph_parse(graph, filtergraph, &inputs, &outputs, NULL)) < 0)
1740             goto fail;
1741     } else {
1742         if ((ret = avfilter_link(source_ctx, 0, sink_ctx, 0)) < 0)
1743             goto fail;
1744     }
1745
1746     ret = avfilter_graph_config(graph, NULL);
1747 fail:
1748     avfilter_inout_free(&outputs);
1749     avfilter_inout_free(&inputs);
1750     return ret;
1751 }
1752
1753 static int configure_video_filters(AVFilterGraph *graph, VideoState *is, const char *vfilters, AVFrame *frame)
1754 {
1755     static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE };
1756     char sws_flags_str[128];
1757     char buffersrc_args[256];
1758     int ret;
1759     AVBufferSinkParams *buffersink_params = av_buffersink_params_alloc();
1760     AVFilterContext *filt_src = NULL, *filt_out = NULL, *filt_crop;
1761     AVCodecContext *codec = is->video_st->codec;
1762
1763     if (!buffersink_params)
1764         return AVERROR(ENOMEM);
1765
1766     av_opt_get_int(sws_opts, "sws_flags", 0, &sws_flags);
1767     snprintf(sws_flags_str, sizeof(sws_flags_str), "flags=%"PRId64, sws_flags);
1768     graph->scale_sws_opts = av_strdup(sws_flags_str);
1769
1770     snprintf(buffersrc_args, sizeof(buffersrc_args),
1771              "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d",
1772              frame->width, frame->height, frame->format,
1773              is->video_st->time_base.num, is->video_st->time_base.den,
1774              codec->sample_aspect_ratio.num, FFMAX(codec->sample_aspect_ratio.den, 1));
1775
1776     if ((ret = avfilter_graph_create_filter(&filt_src,
1777                                             avfilter_get_by_name("buffer"),
1778                                             "ffplay_buffer", buffersrc_args, NULL,
1779                                             graph)) < 0)
1780         goto fail;
1781
1782     buffersink_params->pixel_fmts = pix_fmts;
1783     ret = avfilter_graph_create_filter(&filt_out,
1784                                        avfilter_get_by_name("buffersink"),
1785                                        "ffplay_buffersink", NULL, buffersink_params, graph);
1786     if (ret < 0)
1787         goto fail;
1788
1789     /* SDL YUV code is not handling odd width/height for some driver
1790      * combinations, therefore we crop the picture to an even width/height. */
1791     if ((ret = avfilter_graph_create_filter(&filt_crop,
1792                                             avfilter_get_by_name("crop"),
1793                                             "ffplay_crop", "floor(in_w/2)*2:floor(in_h/2)*2", NULL, graph)) < 0)
1794         goto fail;
1795     if ((ret = avfilter_link(filt_crop, 0, filt_out, 0)) < 0)
1796         goto fail;
1797
1798     if ((ret = configure_filtergraph(graph, vfilters, filt_src, filt_crop)) < 0)
1799         goto fail;
1800
1801     is->in_video_filter  = filt_src;
1802     is->out_video_filter = filt_out;
1803
1804 fail:
1805     av_freep(&buffersink_params);
1806     return ret;
1807 }
1808
1809 static int configure_audio_filters(VideoState *is, const char *afilters, int force_output_format)
1810 {
1811     static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE };
1812     int sample_rates[2] = { 0, -1 };
1813     int64_t channel_layouts[2] = { 0, -1 };
1814     int channels[2] = { 0, -1 };
1815     AVFilterContext *filt_asrc = NULL, *filt_asink = NULL;
1816     char asrc_args[256];
1817     AVABufferSinkParams *asink_params = NULL;
1818     int ret;
1819
1820     avfilter_graph_free(&is->agraph);
1821     if (!(is->agraph = avfilter_graph_alloc()))
1822         return AVERROR(ENOMEM);
1823
1824     ret = snprintf(asrc_args, sizeof(asrc_args),
1825                    "sample_rate=%d:sample_fmt=%s:channels=%d",
1826                    is->audio_filter_src.freq, av_get_sample_fmt_name(is->audio_filter_src.fmt),
1827                    is->audio_filter_src.channels);
1828     if (is->audio_filter_src.channel_layout)
1829         snprintf(asrc_args + ret, sizeof(asrc_args) - ret,
1830                  ":channel_layout=0x%"PRIx64,  is->audio_filter_src.channel_layout);
1831
1832     ret = avfilter_graph_create_filter(&filt_asrc,
1833                                        avfilter_get_by_name("abuffer"), "ffplay_abuffer",
1834                                        asrc_args, NULL, is->agraph);
1835     if (ret < 0)
1836         goto end;
1837
1838     if (!(asink_params = av_abuffersink_params_alloc())) {
1839         ret = AVERROR(ENOMEM);
1840         goto end;
1841     }
1842     asink_params->sample_fmts = sample_fmts;
1843
1844     asink_params->all_channel_counts = 1;
1845     if (force_output_format) {
1846         channel_layouts[0] = is->audio_tgt.channel_layout;
1847         asink_params->channel_layouts = channel_layouts;
1848         asink_params->all_channel_counts = 0;
1849         channels[0] = is->audio_tgt.channels;
1850         asink_params->channel_counts = channels;
1851         asink_params->all_channel_counts = 0;
1852         sample_rates[0] = is->audio_tgt.freq;
1853         asink_params->sample_rates = sample_rates;
1854     }
1855
1856     ret = avfilter_graph_create_filter(&filt_asink,
1857                                        avfilter_get_by_name("abuffersink"), "ffplay_abuffersink",
1858                                        NULL, asink_params, is->agraph);
1859     if (ret < 0)
1860         goto end;
1861
1862     if ((ret = configure_filtergraph(is->agraph, afilters, filt_asrc, filt_asink)) < 0)
1863         goto end;
1864
1865     is->in_audio_filter  = filt_asrc;
1866     is->out_audio_filter = filt_asink;
1867
1868 end:
1869     av_freep(&asink_params);
1870     if (ret < 0)
1871         avfilter_graph_free(&is->agraph);
1872     return ret;
1873 }
1874 #endif  /* CONFIG_AVFILTER */
1875
1876 static int video_thread(void *arg)
1877 {
1878     AVPacket pkt = { 0 };
1879     VideoState *is = arg;
1880     AVFrame *frame = av_frame_alloc();
1881     int64_t pts_int = AV_NOPTS_VALUE, pos = -1;
1882     double pts;
1883     int ret;
1884     int serial = 0;
1885
1886 #if CONFIG_AVFILTER
1887     AVFilterGraph *graph = avfilter_graph_alloc();
1888     AVFilterContext *filt_out = NULL, *filt_in = NULL;
1889     int last_w = 0;
1890     int last_h = 0;
1891     enum AVPixelFormat last_format = -2;
1892     int last_serial = -1;
1893 #endif
1894
1895     for (;;) {
1896 #if CONFIG_AVFILTER
1897         AVRational tb;
1898 #endif
1899         while (is->paused && !is->videoq.abort_request)
1900             SDL_Delay(10);
1901
1902         avcodec_get_frame_defaults(frame);
1903         av_free_packet(&pkt);
1904
1905         ret = get_video_frame(is, frame, &pts_int, &pkt, &serial);
1906         if (ret < 0)
1907             goto the_end;
1908
1909         if (!ret)
1910             continue;
1911
1912 #if CONFIG_AVFILTER
1913         if (   last_w != frame->width
1914             || last_h != frame->height
1915             || last_format != frame->format
1916             || last_serial != serial) {
1917             av_log(NULL, AV_LOG_DEBUG,
1918                    "Video frame changed from size:%dx%d format:%s serial:%d to size:%dx%d format:%s serial:%d\n",
1919                    last_w, last_h,
1920                    (const char *)av_x_if_null(av_get_pix_fmt_name(last_format), "none"), last_serial,
1921                    frame->width, frame->height,
1922                    (const char *)av_x_if_null(av_get_pix_fmt_name(frame->format), "none"), serial);
1923             avfilter_graph_free(&graph);
1924             graph = avfilter_graph_alloc();
1925             if ((ret = configure_video_filters(graph, is, vfilters, frame)) < 0) {
1926                 SDL_Event event;
1927                 event.type = FF_QUIT_EVENT;
1928                 event.user.data1 = is;
1929                 SDL_PushEvent(&event);
1930                 av_free_packet(&pkt);
1931                 goto the_end;
1932             }
1933             filt_in  = is->in_video_filter;
1934             filt_out = is->out_video_filter;
1935             last_w = frame->width;
1936             last_h = frame->height;
1937             last_format = frame->format;
1938             last_serial = serial;
1939         }
1940
1941         frame->pts = pts_int;
1942         frame->sample_aspect_ratio = av_guess_sample_aspect_ratio(is->ic, is->video_st, frame);
1943         ret = av_buffersrc_add_frame(filt_in, frame);
1944         if (ret < 0)
1945             goto the_end;
1946         av_frame_unref(frame);
1947         avcodec_get_frame_defaults(frame);
1948         av_free_packet(&pkt);
1949
1950         while (ret >= 0) {
1951             is->frame_last_returned_time = av_gettime() / 1000000.0;
1952
1953             ret = av_buffersink_get_frame_flags(filt_out, frame, 0);
1954             if (ret < 0) {
1955                 ret = 0;
1956                 break;
1957             }
1958
1959             is->frame_last_filter_delay = av_gettime() / 1000000.0 - is->frame_last_returned_time;
1960             if (fabs(is->frame_last_filter_delay) > AV_NOSYNC_THRESHOLD / 10.0)
1961                 is->frame_last_filter_delay = 0;
1962
1963             pts_int = frame->pts;
1964             tb      = filt_out->inputs[0]->time_base;
1965             pos     = av_frame_get_pkt_pos(frame);
1966             if (av_cmp_q(tb, is->video_st->time_base)) {
1967                 av_unused int64_t pts1 = pts_int;
1968                 pts_int = av_rescale_q(pts_int, tb, is->video_st->time_base);
1969                 av_dlog(NULL, "video_thread(): "
1970                         "tb:%d/%d pts:%"PRId64" -> tb:%d/%d pts:%"PRId64"\n",
1971                         tb.num, tb.den, pts1,
1972                         is->video_st->time_base.num, is->video_st->time_base.den, pts_int);
1973             }
1974             pts = pts_int * av_q2d(is->video_st->time_base);
1975             ret = queue_picture(is, frame, pts, pos, serial);
1976             av_frame_unref(frame);
1977         }
1978 #else
1979         pts = pts_int * av_q2d(is->video_st->time_base);
1980         ret = queue_picture(is, frame, pts, pkt.pos, serial);
1981         av_frame_unref(frame);
1982 #endif
1983
1984         if (ret < 0)
1985             goto the_end;
1986     }
1987  the_end:
1988     avcodec_flush_buffers(is->video_st->codec);
1989 #if CONFIG_AVFILTER
1990     avfilter_graph_free(&graph);
1991 #endif
1992     av_free_packet(&pkt);
1993     av_frame_free(&frame);
1994     return 0;
1995 }
1996
1997 static int subtitle_thread(void *arg)
1998 {
1999     VideoState *is = arg;
2000     SubPicture *sp;
2001     AVPacket pkt1, *pkt = &pkt1;
2002     int got_subtitle;
2003     double pts;
2004     int i, j;
2005     int r, g, b, y, u, v, a;
2006
2007     for (;;) {
2008         while (is->paused && !is->subtitleq.abort_request) {
2009             SDL_Delay(10);
2010         }
2011         if (packet_queue_get(&is->subtitleq, pkt, 1, NULL) < 0)
2012             break;
2013
2014         if (pkt->data == flush_pkt.data) {
2015             avcodec_flush_buffers(is->subtitle_st->codec);
2016             continue;
2017         }
2018         SDL_LockMutex(is->subpq_mutex);
2019         while (is->subpq_size >= SUBPICTURE_QUEUE_SIZE &&
2020                !is->subtitleq.abort_request) {
2021             SDL_CondWait(is->subpq_cond, is->subpq_mutex);
2022         }
2023         SDL_UnlockMutex(is->subpq_mutex);
2024
2025         if (is->subtitleq.abort_request)
2026             return 0;
2027
2028         sp = &is->subpq[is->subpq_windex];
2029
2030        /* NOTE: ipts is the PTS of the _first_ picture beginning in
2031            this packet, if any */
2032         pts = 0;
2033         if (pkt->pts != AV_NOPTS_VALUE)
2034             pts = av_q2d(is->subtitle_st->time_base) * pkt->pts;
2035
2036         avcodec_decode_subtitle2(is->subtitle_st->codec, &sp->sub,
2037                                  &got_subtitle, pkt);
2038         if (got_subtitle && sp->sub.format == 0) {
2039             if (sp->sub.pts != AV_NOPTS_VALUE)
2040                 pts = sp->sub.pts / (double)AV_TIME_BASE;
2041             sp->pts = pts;
2042
2043             for (i = 0; i < sp->sub.num_rects; i++)
2044             {
2045                 for (j = 0; j < sp->sub.rects[i]->nb_colors; j++)
2046                 {
2047                     RGBA_IN(r, g, b, a, (uint32_t*)sp->sub.rects[i]->pict.data[1] + j);
2048                     y = RGB_TO_Y_CCIR(r, g, b);
2049                     u = RGB_TO_U_CCIR(r, g, b, 0);
2050                     v = RGB_TO_V_CCIR(r, g, b, 0);
2051                     YUVA_OUT((uint32_t*)sp->sub.rects[i]->pict.data[1] + j, y, u, v, a);
2052                 }
2053             }
2054
2055             /* now we can update the picture count */
2056             if (++is->subpq_windex == SUBPICTURE_QUEUE_SIZE)
2057                 is->subpq_windex = 0;
2058             SDL_LockMutex(is->subpq_mutex);
2059             is->subpq_size++;
2060             SDL_UnlockMutex(is->subpq_mutex);
2061         }
2062         av_free_packet(pkt);
2063     }
2064     return 0;
2065 }
2066
2067 /* copy samples for viewing in editor window */
2068 static void update_sample_display(VideoState *is, short *samples, int samples_size)
2069 {
2070     int size, len;
2071
2072     size = samples_size / sizeof(short);
2073     while (size > 0) {
2074         len = SAMPLE_ARRAY_SIZE - is->sample_array_index;
2075         if (len > size)
2076             len = size;
2077         memcpy(is->sample_array + is->sample_array_index, samples, len * sizeof(short));
2078         samples += len;
2079         is->sample_array_index += len;
2080         if (is->sample_array_index >= SAMPLE_ARRAY_SIZE)
2081             is->sample_array_index = 0;
2082         size -= len;
2083     }
2084 }
2085
2086 /* return the wanted number of samples to get better sync if sync_type is video
2087  * or external master clock */
2088 static int synchronize_audio(VideoState *is, int nb_samples)
2089 {
2090     int wanted_nb_samples = nb_samples;
2091
2092     /* if not master, then we try to remove or add samples to correct the clock */
2093     if (get_master_sync_type(is) != AV_SYNC_AUDIO_MASTER) {
2094         double diff, avg_diff;
2095         int min_nb_samples, max_nb_samples;
2096
2097         diff = get_audio_clock(is) - get_master_clock(is);
2098
2099         if (!isnan(diff) && fabs(diff) < AV_NOSYNC_THRESHOLD) {
2100             is->audio_diff_cum = diff + is->audio_diff_avg_coef * is->audio_diff_cum;
2101             if (is->audio_diff_avg_count < AUDIO_DIFF_AVG_NB) {
2102                 /* not enough measures to have a correct estimate */
2103                 is->audio_diff_avg_count++;
2104             } else {
2105                 /* estimate the A-V difference */
2106                 avg_diff = is->audio_diff_cum * (1.0 - is->audio_diff_avg_coef);
2107
2108                 if (fabs(avg_diff) >= is->audio_diff_threshold) {
2109                     wanted_nb_samples = nb_samples + (int)(diff * is->audio_src.freq);
2110                     min_nb_samples = ((nb_samples * (100 - SAMPLE_CORRECTION_PERCENT_MAX) / 100));
2111                     max_nb_samples = ((nb_samples * (100 + SAMPLE_CORRECTION_PERCENT_MAX) / 100));
2112                     wanted_nb_samples = FFMIN(FFMAX(wanted_nb_samples, min_nb_samples), max_nb_samples);
2113                 }
2114                 av_dlog(NULL, "diff=%f adiff=%f sample_diff=%d apts=%0.3f %f\n",
2115                         diff, avg_diff, wanted_nb_samples - nb_samples,
2116                         is->audio_clock, is->audio_diff_threshold);
2117             }
2118         } else {
2119             /* too big difference : may be initial PTS errors, so
2120                reset A-V filter */
2121             is->audio_diff_avg_count = 0;
2122             is->audio_diff_cum       = 0;
2123         }
2124     }
2125
2126     return wanted_nb_samples;
2127 }
2128
2129 /**
2130  * Decode one audio frame and return its uncompressed size.
2131  *
2132  * The processed audio frame is decoded, converted if required, and
2133  * stored in is->audio_buf, with size in bytes given by the return
2134  * value.
2135  */
2136 static int audio_decode_frame(VideoState *is)
2137 {
2138     AVPacket *pkt_temp = &is->audio_pkt_temp;
2139     AVPacket *pkt = &is->audio_pkt;
2140     AVCodecContext *dec = is->audio_st->codec;
2141     int len1, data_size, resampled_data_size;
2142     int64_t dec_channel_layout;
2143     int got_frame;
2144     av_unused double audio_clock0;
2145     int new_packet = 0;
2146     int flush_complete = 0;
2147     int wanted_nb_samples;
2148     AVRational tb;
2149
2150     for (;;) {
2151         /* NOTE: the audio packet can contain several frames */
2152         while (pkt_temp->size > 0 || (!pkt_temp->data && new_packet)) {
2153             if (!is->frame) {
2154                 if (!(is->frame = avcodec_alloc_frame()))
2155                     return AVERROR(ENOMEM);
2156             } else {
2157                 av_frame_unref(is->frame);
2158                 avcodec_get_frame_defaults(is->frame);
2159             }
2160
2161             if (is->audioq.serial != is->audio_pkt_temp_serial)
2162                 break;
2163
2164             if (is->paused)
2165                 return -1;
2166
2167             if (flush_complete)
2168                 break;
2169             new_packet = 0;
2170             len1 = avcodec_decode_audio4(dec, is->frame, &got_frame, pkt_temp);
2171             if (len1 < 0) {
2172                 /* if error, we skip the frame */
2173                 pkt_temp->size = 0;
2174                 break;
2175             }
2176
2177             pkt_temp->data += len1;
2178             pkt_temp->size -= len1;
2179
2180             if (!got_frame) {
2181                 /* stop sending empty packets if the decoder is finished */
2182                 if (!pkt_temp->data && dec->codec->capabilities & CODEC_CAP_DELAY)
2183                     flush_complete = 1;
2184                 continue;
2185             }
2186
2187             if (is->frame->pts == AV_NOPTS_VALUE && pkt_temp->pts != AV_NOPTS_VALUE)
2188                 is->frame->pts = av_rescale_q(pkt_temp->pts, is->audio_st->time_base, dec->time_base);
2189             if (pkt_temp->pts != AV_NOPTS_VALUE)
2190                 pkt_temp->pts += (double) is->frame->nb_samples / is->frame->sample_rate / av_q2d(is->audio_st->time_base);
2191             tb = dec->time_base;
2192
2193 #if CONFIG_AVFILTER
2194             {
2195                 int ret;
2196                 int reconfigure;
2197
2198                 dec_channel_layout = get_valid_channel_layout(is->frame->channel_layout, av_frame_get_channels(is->frame));
2199
2200                 reconfigure =
2201                     cmp_audio_fmts(is->audio_filter_src.fmt, is->audio_filter_src.channels,
2202                                    is->frame->format, av_frame_get_channels(is->frame))    ||
2203                     is->audio_filter_src.channel_layout != dec_channel_layout ||
2204                     is->audio_filter_src.freq           != is->frame->sample_rate ||
2205                     is->audio_pkt_temp_serial           != is->audio_last_serial;
2206
2207                 if (reconfigure) {
2208                     char buf1[1024], buf2[1024];
2209                     av_get_channel_layout_string(buf1, sizeof(buf1), -1, is->audio_filter_src.channel_layout);
2210                     av_get_channel_layout_string(buf2, sizeof(buf2), -1, dec_channel_layout);
2211                     av_log(NULL, AV_LOG_DEBUG,
2212                            "Audio frame changed from rate:%d ch:%d fmt:%s layout:%s serial:%d to rate:%d ch:%d fmt:%s layout:%s serial:%d\n",
2213                            is->audio_filter_src.freq, is->audio_filter_src.channels, av_get_sample_fmt_name(is->audio_filter_src.fmt), buf1, is->audio_last_serial,
2214                            is->frame->sample_rate, av_frame_get_channels(is->frame), av_get_sample_fmt_name(is->frame->format), buf2, is->audio_pkt_temp_serial);
2215
2216                     is->audio_filter_src.fmt            = is->frame->format;
2217                     is->audio_filter_src.channels       = av_frame_get_channels(is->frame);
2218                     is->audio_filter_src.channel_layout = dec_channel_layout;
2219                     is->audio_filter_src.freq           = is->frame->sample_rate;
2220                     is->audio_last_serial               = is->audio_pkt_temp_serial;
2221
2222                     if ((ret = configure_audio_filters(is, afilters, 1)) < 0)
2223                         return ret;
2224                 }
2225
2226                 if ((ret = av_buffersrc_add_frame(is->in_audio_filter, is->frame)) < 0)
2227                     return ret;
2228                 av_frame_unref(is->frame);
2229                 if ((ret = av_buffersink_get_frame_flags(is->out_audio_filter, is->frame, 0)) < 0)
2230                     return ret;
2231                 tb = is->out_audio_filter->inputs[0]->time_base;
2232             }
2233 #endif
2234
2235             data_size = av_samples_get_buffer_size(NULL, av_frame_get_channels(is->frame),
2236                                                    is->frame->nb_samples,
2237                                                    is->frame->format, 1);
2238
2239             dec_channel_layout =
2240                 (is->frame->channel_layout && av_frame_get_channels(is->frame) == av_get_channel_layout_nb_channels(is->frame->channel_layout)) ?
2241                 is->frame->channel_layout : av_get_default_channel_layout(av_frame_get_channels(is->frame));
2242             wanted_nb_samples = synchronize_audio(is, is->frame->nb_samples);
2243
2244             if (is->frame->format        != is->audio_src.fmt            ||
2245                 dec_channel_layout       != is->audio_src.channel_layout ||
2246                 is->frame->sample_rate   != is->audio_src.freq           ||
2247                 (wanted_nb_samples       != is->frame->nb_samples && !is->swr_ctx)) {
2248                 swr_free(&is->swr_ctx);
2249                 is->swr_ctx = swr_alloc_set_opts(NULL,
2250                                                  is->audio_tgt.channel_layout, is->audio_tgt.fmt, is->audio_tgt.freq,
2251                                                  dec_channel_layout,           is->frame->format, is->frame->sample_rate,
2252                                                  0, NULL);
2253                 if (!is->swr_ctx || swr_init(is->swr_ctx) < 0) {
2254                     fprintf(stderr, "Cannot create sample rate converter for conversion of %d Hz %s %d channels to %d Hz %s %d channels!\n",
2255                             is->frame->sample_rate, av_get_sample_fmt_name(is->frame->format), av_frame_get_channels(is->frame),
2256                             is->audio_tgt.freq, av_get_sample_fmt_name(is->audio_tgt.fmt), is->audio_tgt.channels);
2257                     break;
2258                 }
2259                 is->audio_src.channel_layout = dec_channel_layout;
2260                 is->audio_src.channels       = av_frame_get_channels(is->frame);
2261                 is->audio_src.freq = is->frame->sample_rate;
2262                 is->audio_src.fmt = is->frame->format;
2263             }
2264
2265             if (is->swr_ctx) {
2266                 const uint8_t **in = (const uint8_t **)is->frame->extended_data;
2267                 uint8_t **out = &is->audio_buf1;
2268                 int out_count = (int64_t)wanted_nb_samples * is->audio_tgt.freq / is->frame->sample_rate + 256;
2269                 int out_size  = av_samples_get_buffer_size(NULL, is->audio_tgt.channels, out_count, is->audio_tgt.fmt, 0);
2270                 int len2;
2271                 if (wanted_nb_samples != is->frame->nb_samples) {
2272                     if (swr_set_compensation(is->swr_ctx, (wanted_nb_samples - is->frame->nb_samples) * is->audio_tgt.freq / is->frame->sample_rate,
2273                                                 wanted_nb_samples * is->audio_tgt.freq / is->frame->sample_rate) < 0) {
2274                         fprintf(stderr, "swr_set_compensation() failed\n");
2275                         break;
2276                     }
2277                 }
2278                 av_fast_malloc(&is->audio_buf1, &is->audio_buf1_size, out_size);
2279                 if (!is->audio_buf1)
2280                     return AVERROR(ENOMEM);
2281                 len2 = swr_convert(is->swr_ctx, out, out_count, in, is->frame->nb_samples);
2282                 if (len2 < 0) {
2283                     fprintf(stderr, "swr_convert() failed\n");
2284                     break;
2285                 }
2286                 if (len2 == out_count) {
2287                     fprintf(stderr, "warning: audio buffer is probably too small\n");
2288                     swr_init(is->swr_ctx);
2289                 }
2290                 is->audio_buf = is->audio_buf1;
2291                 resampled_data_size = len2 * is->audio_tgt.channels * av_get_bytes_per_sample(is->audio_tgt.fmt);
2292             } else {
2293                 is->audio_buf = is->frame->data[0];
2294                 resampled_data_size = data_size;
2295             }
2296
2297             audio_clock0 = is->audio_clock;
2298             /* update the audio clock with the pts */
2299             if (is->frame->pts != AV_NOPTS_VALUE) {
2300                 is->audio_clock = is->frame->pts * av_q2d(tb) + (double) is->frame->nb_samples / is->frame->sample_rate;
2301                 is->audio_clock_serial = is->audio_pkt_temp_serial;
2302             }
2303 #ifdef DEBUG
2304             {
2305                 static double last_clock;
2306                 printf("audio: delay=%0.3f clock=%0.3f clock0=%0.3f\n",
2307                        is->audio_clock - last_clock,
2308                        is->audio_clock, audio_clock0);
2309                 last_clock = is->audio_clock;
2310             }
2311 #endif
2312             return resampled_data_size;
2313         }
2314
2315         /* free the current packet */
2316         if (pkt->data)
2317             av_free_packet(pkt);
2318         memset(pkt_temp, 0, sizeof(*pkt_temp));
2319
2320         if (is->audioq.abort_request) {
2321             return -1;
2322         }
2323
2324         if (is->audioq.nb_packets == 0)
2325             SDL_CondSignal(is->continue_read_thread);
2326
2327         /* read next packet */
2328         if ((new_packet = packet_queue_get(&is->audioq, pkt, 1, &is->audio_pkt_temp_serial)) < 0)
2329             return -1;
2330
2331         if (pkt->data == flush_pkt.data) {
2332             avcodec_flush_buffers(dec);
2333             flush_complete = 0;
2334         }
2335
2336         *pkt_temp = *pkt;
2337     }
2338 }
2339
2340 /* prepare a new audio buffer */
2341 static void sdl_audio_callback(void *opaque, Uint8 *stream, int len)
2342 {
2343     VideoState *is = opaque;
2344     int audio_size, len1;
2345     int bytes_per_sec;
2346     int frame_size = av_samples_get_buffer_size(NULL, is->audio_tgt.channels, 1, is->audio_tgt.fmt, 1);
2347
2348     audio_callback_time = av_gettime();
2349
2350     while (len > 0) {
2351         if (is->audio_buf_index >= is->audio_buf_size) {
2352            audio_size = audio_decode_frame(is);
2353            if (audio_size < 0) {
2354                 /* if error, just output silence */
2355                is->audio_buf      = is->silence_buf;
2356                is->audio_buf_size = sizeof(is->silence_buf) / frame_size * frame_size;
2357            } else {
2358                if (is->show_mode != SHOW_MODE_VIDEO)
2359                    update_sample_display(is, (int16_t *)is->audio_buf, audio_size);
2360                is->audio_buf_size = audio_size;
2361            }
2362            is->audio_buf_index = 0;
2363         }
2364         len1 = is->audio_buf_size - is->audio_buf_index;
2365         if (len1 > len)
2366             len1 = len;
2367         memcpy(stream, (uint8_t *)is->audio_buf + is->audio_buf_index, len1);
2368         len -= len1;
2369         stream += len1;
2370         is->audio_buf_index += len1;
2371     }
2372     bytes_per_sec = is->audio_tgt.freq * is->audio_tgt.channels * av_get_bytes_per_sample(is->audio_tgt.fmt);
2373     is->audio_write_buf_size = is->audio_buf_size - is->audio_buf_index;
2374     /* Let's assume the audio driver that is used by SDL has two periods. */
2375     is->audio_current_pts = is->audio_clock - (double)(2 * is->audio_hw_buf_size + is->audio_write_buf_size) / bytes_per_sec;
2376     is->audio_current_pts_drift = is->audio_current_pts - audio_callback_time / 1000000.0;
2377     if (is->audioq.serial == is->audio_clock_serial)
2378         check_external_clock_sync(is, is->audio_current_pts);
2379 }
2380
2381 static int audio_open(void *opaque, int64_t wanted_channel_layout, int wanted_nb_channels, int wanted_sample_rate, struct AudioParams *audio_hw_params)
2382 {
2383     SDL_AudioSpec wanted_spec, spec;
2384     const char *env;
2385     const int next_nb_channels[] = {0, 0, 1, 6, 2, 6, 4, 6};
2386
2387     env = SDL_getenv("SDL_AUDIO_CHANNELS");
2388     if (env) {
2389         wanted_nb_channels = atoi(env);
2390         wanted_channel_layout = av_get_default_channel_layout(wanted_nb_channels);
2391     }
2392     if (!wanted_channel_layout || wanted_nb_channels != av_get_channel_layout_nb_channels(wanted_channel_layout)) {
2393         wanted_channel_layout = av_get_default_channel_layout(wanted_nb_channels);
2394         wanted_channel_layout &= ~AV_CH_LAYOUT_STEREO_DOWNMIX;
2395     }
2396     wanted_spec.channels = av_get_channel_layout_nb_channels(wanted_channel_layout);
2397     wanted_spec.freq = wanted_sample_rate;
2398     if (wanted_spec.freq <= 0 || wanted_spec.channels <= 0) {
2399         fprintf(stderr, "Invalid sample rate or channel count!\n");
2400         return -1;
2401     }
2402     wanted_spec.format = AUDIO_S16SYS;
2403     wanted_spec.silence = 0;
2404     wanted_spec.samples = SDL_AUDIO_BUFFER_SIZE;
2405     wanted_spec.callback = sdl_audio_callback;
2406     wanted_spec.userdata = opaque;
2407     while (SDL_OpenAudio(&wanted_spec, &spec) < 0) {
2408         fprintf(stderr, "SDL_OpenAudio (%d channels): %s\n", wanted_spec.channels, SDL_GetError());
2409         wanted_spec.channels = next_nb_channels[FFMIN(7, wanted_spec.channels)];
2410         if (!wanted_spec.channels) {
2411             fprintf(stderr, "No more channel combinations to try, audio open failed\n");
2412             return -1;
2413         }
2414         wanted_channel_layout = av_get_default_channel_layout(wanted_spec.channels);
2415     }
2416     if (spec.format != AUDIO_S16SYS) {
2417         fprintf(stderr, "SDL advised audio format %d is not supported!\n", spec.format);
2418         return -1;
2419     }
2420     if (spec.channels != wanted_spec.channels) {
2421         wanted_channel_layout = av_get_default_channel_layout(spec.channels);
2422         if (!wanted_channel_layout) {
2423             fprintf(stderr, "SDL advised channel count %d is not supported!\n", spec.channels);
2424             return -1;
2425         }
2426     }
2427
2428     audio_hw_params->fmt = AV_SAMPLE_FMT_S16;
2429     audio_hw_params->freq = spec.freq;
2430     audio_hw_params->channel_layout = wanted_channel_layout;
2431     audio_hw_params->channels =  spec.channels;
2432     return spec.size;
2433 }
2434
2435 /* open a given stream. Return 0 if OK */
2436 static int stream_component_open(VideoState *is, int stream_index)
2437 {
2438     AVFormatContext *ic = is->ic;
2439     AVCodecContext *avctx;
2440     AVCodec *codec;
2441     const char *forced_codec_name = NULL;
2442     AVDictionary *opts;
2443     AVDictionaryEntry *t = NULL;
2444     int sample_rate, nb_channels;
2445     int64_t channel_layout;
2446     int ret;
2447
2448     if (stream_index < 0 || stream_index >= ic->nb_streams)
2449         return -1;
2450     avctx = ic->streams[stream_index]->codec;
2451
2452     codec = avcodec_find_decoder(avctx->codec_id);
2453
2454     switch(avctx->codec_type){
2455         case AVMEDIA_TYPE_AUDIO   : is->last_audio_stream    = stream_index; forced_codec_name =    audio_codec_name; break;
2456         case AVMEDIA_TYPE_SUBTITLE: is->last_subtitle_stream = stream_index; forced_codec_name = subtitle_codec_name; break;
2457         case AVMEDIA_TYPE_VIDEO   : is->last_video_stream    = stream_index; forced_codec_name =    video_codec_name; break;
2458     }
2459     if (forced_codec_name)
2460         codec = avcodec_find_decoder_by_name(forced_codec_name);
2461     if (!codec) {
2462         if (forced_codec_name) fprintf(stderr, "No codec could be found with name '%s'\n", forced_codec_name);
2463         else                   fprintf(stderr, "No codec could be found with id %d\n", avctx->codec_id);
2464         return -1;
2465     }
2466
2467     avctx->codec_id = codec->id;
2468     avctx->workaround_bugs   = workaround_bugs;
2469     avctx->lowres            = lowres;
2470     if(avctx->lowres > codec->max_lowres){
2471         av_log(avctx, AV_LOG_WARNING, "The maximum value for lowres supported by the decoder is %d\n",
2472                 codec->max_lowres);
2473         avctx->lowres= codec->max_lowres;
2474     }
2475     avctx->idct_algo         = idct;
2476     avctx->error_concealment = error_concealment;
2477
2478     if(avctx->lowres) avctx->flags |= CODEC_FLAG_EMU_EDGE;
2479     if (fast)   avctx->flags2 |= CODEC_FLAG2_FAST;
2480     if(codec->capabilities & CODEC_CAP_DR1)
2481         avctx->flags |= CODEC_FLAG_EMU_EDGE;
2482
2483     opts = filter_codec_opts(codec_opts, avctx->codec_id, ic, ic->streams[stream_index], codec);
2484     if (!av_dict_get(opts, "threads", NULL, 0))
2485         av_dict_set(&opts, "threads", "auto", 0);
2486     if (avctx->codec_type == AVMEDIA_TYPE_VIDEO || avctx->codec_type == AVMEDIA_TYPE_AUDIO)
2487         av_dict_set(&opts, "refcounted_frames", "1", 0);
2488     if (avcodec_open2(avctx, codec, &opts) < 0)
2489         return -1;
2490     if ((t = av_dict_get(opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
2491         av_log(NULL, AV_LOG_ERROR, "Option %s not found.\n", t->key);
2492         return AVERROR_OPTION_NOT_FOUND;
2493     }
2494
2495     ic->streams[stream_index]->discard = AVDISCARD_DEFAULT;
2496     switch (avctx->codec_type) {
2497     case AVMEDIA_TYPE_AUDIO:
2498 #if CONFIG_AVFILTER
2499         {
2500             AVFilterLink *link;
2501
2502             is->audio_filter_src.freq           = avctx->sample_rate;
2503             is->audio_filter_src.channels       = avctx->channels;
2504             is->audio_filter_src.channel_layout = get_valid_channel_layout(avctx->channel_layout, avctx->channels);
2505             is->audio_filter_src.fmt            = avctx->sample_fmt;
2506             if ((ret = configure_audio_filters(is, afilters, 0)) < 0)
2507                 return ret;
2508             link = is->out_audio_filter->inputs[0];
2509             sample_rate    = link->sample_rate;
2510             nb_channels    = link->channels;
2511             channel_layout = link->channel_layout;
2512         }
2513 #else
2514         sample_rate    = avctx->sample_rate;
2515         nb_channels    = avctx->channels;
2516         channel_layout = avctx->channel_layout;
2517 #endif
2518
2519         /* prepare audio output */
2520         if ((ret = audio_open(is, channel_layout, nb_channels, sample_rate, &is->audio_tgt)) < 0)
2521             return ret;
2522         is->audio_hw_buf_size = ret;
2523         is->audio_src = is->audio_tgt;
2524         is->audio_buf_size  = 0;
2525         is->audio_buf_index = 0;
2526
2527         /* init averaging filter */
2528         is->audio_diff_avg_coef  = exp(log(0.01) / AUDIO_DIFF_AVG_NB);
2529         is->audio_diff_avg_count = 0;
2530         /* since we do not have a precise anough audio fifo fullness,
2531            we correct audio sync only if larger than this threshold */
2532         is->audio_diff_threshold = 2.0 * is->audio_hw_buf_size / av_samples_get_buffer_size(NULL, is->audio_tgt.channels, is->audio_tgt.freq, is->audio_tgt.fmt, 1);
2533
2534         memset(&is->audio_pkt, 0, sizeof(is->audio_pkt));
2535         memset(&is->audio_pkt_temp, 0, sizeof(is->audio_pkt_temp));
2536
2537         is->audio_stream = stream_index;
2538         is->audio_st = ic->streams[stream_index];
2539
2540         packet_queue_start(&is->audioq);
2541         SDL_PauseAudio(0);
2542         break;
2543     case AVMEDIA_TYPE_VIDEO:
2544         is->video_stream = stream_index;
2545         is->video_st = ic->streams[stream_index];
2546
2547         packet_queue_start(&is->videoq);
2548         is->video_tid = SDL_CreateThread(video_thread, is);
2549         break;
2550     case AVMEDIA_TYPE_SUBTITLE:
2551         is->subtitle_stream = stream_index;
2552         is->subtitle_st = ic->streams[stream_index];
2553         packet_queue_start(&is->subtitleq);
2554
2555         is->subtitle_tid = SDL_CreateThread(subtitle_thread, is);
2556         break;
2557     default:
2558         break;
2559     }
2560     return 0;
2561 }
2562
2563 static void stream_component_close(VideoState *is, int stream_index)
2564 {
2565     AVFormatContext *ic = is->ic;
2566     AVCodecContext *avctx;
2567
2568     if (stream_index < 0 || stream_index >= ic->nb_streams)
2569         return;
2570     avctx = ic->streams[stream_index]->codec;
2571
2572     switch (avctx->codec_type) {
2573     case AVMEDIA_TYPE_AUDIO:
2574         packet_queue_abort(&is->audioq);
2575
2576         SDL_CloseAudio();
2577
2578         packet_queue_flush(&is->audioq);
2579         av_free_packet(&is->audio_pkt);
2580         swr_free(&is->swr_ctx);
2581         av_freep(&is->audio_buf1);
2582         is->audio_buf1_size = 0;
2583         is->audio_buf = NULL;
2584         av_frame_free(&is->frame);
2585
2586         if (is->rdft) {
2587             av_rdft_end(is->rdft);
2588             av_freep(&is->rdft_data);
2589             is->rdft = NULL;
2590             is->rdft_bits = 0;
2591         }
2592 #if CONFIG_AVFILTER
2593         avfilter_graph_free(&is->agraph);
2594 #endif
2595         break;
2596     case AVMEDIA_TYPE_VIDEO:
2597         packet_queue_abort(&is->videoq);
2598
2599         /* note: we also signal this mutex to make sure we deblock the
2600            video thread in all cases */
2601         SDL_LockMutex(is->pictq_mutex);
2602         SDL_CondSignal(is->pictq_cond);
2603         SDL_UnlockMutex(is->pictq_mutex);
2604
2605         SDL_WaitThread(is->video_tid, NULL);
2606
2607         packet_queue_flush(&is->videoq);
2608         break;
2609     case AVMEDIA_TYPE_SUBTITLE:
2610         packet_queue_abort(&is->subtitleq);
2611
2612         /* note: we also signal this mutex to make sure we deblock the
2613            video thread in all cases */
2614         SDL_LockMutex(is->subpq_mutex);
2615         is->subtitle_stream_changed = 1;
2616
2617         SDL_CondSignal(is->subpq_cond);
2618         SDL_UnlockMutex(is->subpq_mutex);
2619
2620         SDL_WaitThread(is->subtitle_tid, NULL);
2621
2622         packet_queue_flush(&is->subtitleq);
2623         break;
2624     default:
2625         break;
2626     }
2627
2628     ic->streams[stream_index]->discard = AVDISCARD_ALL;
2629     avcodec_close(avctx);
2630     switch (avctx->codec_type) {
2631     case AVMEDIA_TYPE_AUDIO:
2632         is->audio_st = NULL;
2633         is->audio_stream = -1;
2634         break;
2635     case AVMEDIA_TYPE_VIDEO:
2636         is->video_st = NULL;
2637         is->video_stream = -1;
2638         break;
2639     case AVMEDIA_TYPE_SUBTITLE:
2640         is->subtitle_st = NULL;
2641         is->subtitle_stream = -1;
2642         break;
2643     default:
2644         break;
2645     }
2646 }
2647
2648 static int decode_interrupt_cb(void *ctx)
2649 {
2650     VideoState *is = ctx;
2651     return is->abort_request;
2652 }
2653
2654 static int is_realtime(AVFormatContext *s)
2655 {
2656     if(   !strcmp(s->iformat->name, "rtp")
2657        || !strcmp(s->iformat->name, "rtsp")
2658        || !strcmp(s->iformat->name, "sdp")
2659     )
2660         return 1;
2661
2662     if(s->pb && (   !strncmp(s->filename, "rtp:", 4)
2663                  || !strncmp(s->filename, "udp:", 4)
2664                 )
2665     )
2666         return 1;
2667     return 0;
2668 }
2669
2670 /* this thread gets the stream from the disk or the network */
2671 static int read_thread(void *arg)
2672 {
2673     VideoState *is = arg;
2674     AVFormatContext *ic = NULL;
2675     int err, i, ret;
2676     int st_index[AVMEDIA_TYPE_NB];
2677     AVPacket pkt1, *pkt = &pkt1;
2678     int eof = 0;
2679     int pkt_in_play_range = 0;
2680     AVDictionaryEntry *t;
2681     AVDictionary **opts;
2682     int orig_nb_streams;
2683     SDL_mutex *wait_mutex = SDL_CreateMutex();
2684
2685     memset(st_index, -1, sizeof(st_index));
2686     is->last_video_stream = is->video_stream = -1;
2687     is->last_audio_stream = is->audio_stream = -1;
2688     is->last_subtitle_stream = is->subtitle_stream = -1;
2689
2690     ic = avformat_alloc_context();
2691     ic->interrupt_callback.callback = decode_interrupt_cb;
2692     ic->interrupt_callback.opaque = is;
2693     err = avformat_open_input(&ic, is->filename, is->iformat, &format_opts);
2694     if (err < 0) {
2695         print_error(is->filename, err);
2696         ret = -1;
2697         goto fail;
2698     }
2699     if ((t = av_dict_get(format_opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
2700         av_log(NULL, AV_LOG_ERROR, "Option %s not found.\n", t->key);
2701         ret = AVERROR_OPTION_NOT_FOUND;
2702         goto fail;
2703     }
2704     is->ic = ic;
2705
2706     if (genpts)
2707         ic->flags |= AVFMT_FLAG_GENPTS;
2708
2709     opts = setup_find_stream_info_opts(ic, codec_opts);
2710     orig_nb_streams = ic->nb_streams;
2711
2712     err = avformat_find_stream_info(ic, opts);
2713     if (err < 0) {
2714         fprintf(stderr, "%s: could not find codec parameters\n", is->filename);
2715         ret = -1;
2716         goto fail;
2717     }
2718     for (i = 0; i < orig_nb_streams; i++)
2719         av_dict_free(&opts[i]);
2720     av_freep(&opts);
2721
2722     if (ic->pb)
2723         ic->pb->eof_reached = 0; // FIXME hack, ffplay maybe should not use url_feof() to test for the end
2724
2725     if (seek_by_bytes < 0)
2726         seek_by_bytes = !!(ic->iformat->flags & AVFMT_TS_DISCONT);
2727
2728     is->max_frame_duration = (ic->iformat->flags & AVFMT_TS_DISCONT) ? 10.0 : 3600.0;
2729
2730     /* if seeking requested, we execute it */
2731     if (start_time != AV_NOPTS_VALUE) {
2732         int64_t timestamp;
2733
2734         timestamp = start_time;
2735         /* add the stream start time */
2736         if (ic->start_time != AV_NOPTS_VALUE)
2737             timestamp += ic->start_time;
2738         ret = avformat_seek_file(ic, -1, INT64_MIN, timestamp, INT64_MAX, 0);
2739         if (ret < 0) {
2740             fprintf(stderr, "%s: could not seek to position %0.3f\n",
2741                     is->filename, (double)timestamp / AV_TIME_BASE);
2742         }
2743     }
2744
2745     is->realtime = is_realtime(ic);
2746
2747     for (i = 0; i < ic->nb_streams; i++)
2748         ic->streams[i]->discard = AVDISCARD_ALL;
2749     if (!video_disable)
2750         st_index[AVMEDIA_TYPE_VIDEO] =
2751             av_find_best_stream(ic, AVMEDIA_TYPE_VIDEO,
2752                                 wanted_stream[AVMEDIA_TYPE_VIDEO], -1, NULL, 0);
2753     if (!audio_disable)
2754         st_index[AVMEDIA_TYPE_AUDIO] =
2755             av_find_best_stream(ic, AVMEDIA_TYPE_AUDIO,
2756                                 wanted_stream[AVMEDIA_TYPE_AUDIO],
2757                                 st_index[AVMEDIA_TYPE_VIDEO],
2758                                 NULL, 0);
2759     if (!video_disable && !subtitle_disable)
2760         st_index[AVMEDIA_TYPE_SUBTITLE] =
2761             av_find_best_stream(ic, AVMEDIA_TYPE_SUBTITLE,
2762                                 wanted_stream[AVMEDIA_TYPE_SUBTITLE],
2763                                 (st_index[AVMEDIA_TYPE_AUDIO] >= 0 ?
2764                                  st_index[AVMEDIA_TYPE_AUDIO] :
2765                                  st_index[AVMEDIA_TYPE_VIDEO]),
2766                                 NULL, 0);
2767     if (show_status) {
2768         av_dump_format(ic, 0, is->filename, 0);
2769     }
2770
2771     is->show_mode = show_mode;
2772
2773     /* open the streams */
2774     if (st_index[AVMEDIA_TYPE_AUDIO] >= 0) {
2775         stream_component_open(is, st_index[AVMEDIA_TYPE_AUDIO]);
2776     }
2777
2778     ret = -1;
2779     if (st_index[AVMEDIA_TYPE_VIDEO] >= 0) {
2780         ret = stream_component_open(is, st_index[AVMEDIA_TYPE_VIDEO]);
2781     }
2782     if (is->show_mode == SHOW_MODE_NONE)
2783         is->show_mode = ret >= 0 ? SHOW_MODE_VIDEO : SHOW_MODE_RDFT;
2784
2785     if (st_index[AVMEDIA_TYPE_SUBTITLE] >= 0) {
2786         stream_component_open(is, st_index[AVMEDIA_TYPE_SUBTITLE]);
2787     }
2788
2789     if (is->video_stream < 0 && is->audio_stream < 0) {
2790         fprintf(stderr, "%s: could not open codecs\n", is->filename);
2791         ret = -1;
2792         goto fail;
2793     }
2794
2795     if (infinite_buffer < 0 && is->realtime)
2796         infinite_buffer = 1;
2797
2798     for (;;) {
2799         if (is->abort_request)
2800             break;
2801         if (is->paused != is->last_paused) {
2802             is->last_paused = is->paused;
2803             if (is->paused)
2804                 is->read_pause_return = av_read_pause(ic);
2805             else
2806                 av_read_play(ic);
2807         }
2808 #if CONFIG_RTSP_DEMUXER || CONFIG_MMSH_PROTOCOL
2809         if (is->paused &&
2810                 (!strcmp(ic->iformat->name, "rtsp") ||
2811                  (ic->pb && !strncmp(input_filename, "mmsh:", 5)))) {
2812             /* wait 10 ms to avoid trying to get another packet */
2813             /* XXX: horrible */
2814             SDL_Delay(10);
2815             continue;
2816         }
2817 #endif
2818         if (is->seek_req) {
2819             int64_t seek_target = is->seek_pos;
2820             int64_t seek_min    = is->seek_rel > 0 ? seek_target - is->seek_rel + 2: INT64_MIN;
2821             int64_t seek_max    = is->seek_rel < 0 ? seek_target - is->seek_rel - 2: INT64_MAX;
2822 // FIXME the +-2 is due to rounding being not done in the correct direction in generation
2823 //      of the seek_pos/seek_rel variables
2824
2825             ret = avformat_seek_file(is->ic, -1, seek_min, seek_target, seek_max, is->seek_flags);
2826             if (ret < 0) {
2827                 fprintf(stderr, "%s: error while seeking\n", is->ic->filename);
2828             } else {
2829                 if (is->audio_stream >= 0) {
2830                     packet_queue_flush(&is->audioq);
2831                     packet_queue_put(&is->audioq, &flush_pkt);
2832                 }
2833                 if (is->subtitle_stream >= 0) {
2834                     packet_queue_flush(&is->subtitleq);
2835                     packet_queue_put(&is->subtitleq, &flush_pkt);
2836                 }
2837                 if (is->video_stream >= 0) {
2838                     packet_queue_flush(&is->videoq);
2839                     packet_queue_put(&is->videoq, &flush_pkt);
2840                 }
2841                 if (is->seek_flags & AVSEEK_FLAG_BYTE) {
2842                    update_external_clock_pts(is, NAN);
2843                 } else {
2844                    update_external_clock_pts(is, seek_target / (double)AV_TIME_BASE);
2845                 }
2846             }
2847             is->seek_req = 0;
2848             eof = 0;
2849             if (is->paused)
2850                 step_to_next_frame(is);
2851         }
2852         if (is->queue_attachments_req) {
2853             avformat_queue_attached_pictures(ic);
2854             is->queue_attachments_req = 0;
2855         }
2856
2857         /* if the queue are full, no need to read more */
2858         if (infinite_buffer<1 &&
2859               (is->audioq.size + is->videoq.size + is->subtitleq.size > MAX_QUEUE_SIZE
2860             || (   (is->audioq   .nb_packets > MIN_FRAMES || is->audio_stream < 0 || is->audioq.abort_request)
2861                 && (is->videoq   .nb_packets > MIN_FRAMES || is->video_stream < 0 || is->videoq.abort_request)
2862                 && (is->subtitleq.nb_packets > MIN_FRAMES || is->subtitle_stream < 0 || is->subtitleq.abort_request)))) {
2863             /* wait 10 ms */
2864             SDL_LockMutex(wait_mutex);
2865             SDL_CondWaitTimeout(is->continue_read_thread, wait_mutex, 10);
2866             SDL_UnlockMutex(wait_mutex);
2867             continue;
2868         }
2869         if (eof) {
2870             if (is->video_stream >= 0) {
2871                 av_init_packet(pkt);
2872                 pkt->data = NULL;
2873                 pkt->size = 0;
2874                 pkt->stream_index = is->video_stream;
2875                 packet_queue_put(&is->videoq, pkt);
2876             }
2877             if (is->audio_stream >= 0 &&
2878                 is->audio_st->codec->codec->capabilities & CODEC_CAP_DELAY) {
2879                 av_init_packet(pkt);
2880                 pkt->data = NULL;
2881                 pkt->size = 0;
2882                 pkt->stream_index = is->audio_stream;
2883                 packet_queue_put(&is->audioq, pkt);
2884             }
2885             SDL_Delay(10);
2886             if (is->audioq.size + is->videoq.size + is->subtitleq.size == 0) {
2887                 if (loop != 1 && (!loop || --loop)) {
2888                     stream_seek(is, start_time != AV_NOPTS_VALUE ? start_time : 0, 0, 0);
2889                 } else if (autoexit) {
2890                     ret = AVERROR_EOF;
2891                     goto fail;
2892                 }
2893             }
2894             eof=0;
2895             continue;
2896         }
2897         ret = av_read_frame(ic, pkt);
2898         if (ret < 0) {
2899             if (ret == AVERROR_EOF || url_feof(ic->pb))
2900                 eof = 1;
2901             if (ic->pb && ic->pb->error)
2902                 break;
2903             SDL_LockMutex(wait_mutex);
2904             SDL_CondWaitTimeout(is->continue_read_thread, wait_mutex, 10);
2905             SDL_UnlockMutex(wait_mutex);
2906             continue;
2907         }
2908         /* check if packet is in play range specified by user, then queue, otherwise discard */
2909         pkt_in_play_range = duration == AV_NOPTS_VALUE ||
2910                 (pkt->pts - ic->streams[pkt->stream_index]->start_time) *
2911                 av_q2d(ic->streams[pkt->stream_index]->time_base) -
2912                 (double)(start_time != AV_NOPTS_VALUE ? start_time : 0) / 1000000
2913                 <= ((double)duration / 1000000);
2914         if (pkt->stream_index == is->audio_stream && pkt_in_play_range) {
2915             packet_queue_put(&is->audioq, pkt);
2916         } else if (pkt->stream_index == is->video_stream && pkt_in_play_range) {
2917             packet_queue_put(&is->videoq, pkt);
2918         } else if (pkt->stream_index == is->subtitle_stream && pkt_in_play_range) {
2919             packet_queue_put(&is->subtitleq, pkt);
2920         } else {
2921             av_free_packet(pkt);
2922         }
2923     }
2924     /* wait until the end */
2925     while (!is->abort_request) {
2926         SDL_Delay(100);
2927     }
2928
2929     ret = 0;
2930  fail:
2931     /* close each stream */
2932     if (is->audio_stream >= 0)
2933         stream_component_close(is, is->audio_stream);
2934     if (is->video_stream >= 0)
2935         stream_component_close(is, is->video_stream);
2936     if (is->subtitle_stream >= 0)
2937         stream_component_close(is, is->subtitle_stream);
2938     if (is->ic) {
2939         avformat_close_input(&is->ic);
2940     }
2941
2942     if (ret != 0) {
2943         SDL_Event event;
2944
2945         event.type = FF_QUIT_EVENT;
2946         event.user.data1 = is;
2947         SDL_PushEvent(&event);
2948     }
2949     SDL_DestroyMutex(wait_mutex);
2950     return 0;
2951 }
2952
2953 static VideoState *stream_open(const char *filename, AVInputFormat *iformat)
2954 {
2955     VideoState *is;
2956
2957     is = av_mallocz(sizeof(VideoState));
2958     if (!is)
2959         return NULL;
2960     av_strlcpy(is->filename, filename, sizeof(is->filename));
2961     is->iformat = iformat;
2962     is->ytop    = 0;
2963     is->xleft   = 0;
2964
2965     /* start video display */
2966     is->pictq_mutex = SDL_CreateMutex();
2967     is->pictq_cond  = SDL_CreateCond();
2968
2969     is->subpq_mutex = SDL_CreateMutex();
2970     is->subpq_cond  = SDL_CreateCond();
2971
2972     packet_queue_init(&is->videoq);
2973     packet_queue_init(&is->audioq);
2974     packet_queue_init(&is->subtitleq);
2975
2976     is->continue_read_thread = SDL_CreateCond();
2977
2978     update_external_clock_pts(is, NAN);
2979     update_external_clock_speed(is, 1.0);
2980     is->audio_current_pts_drift = -av_gettime() / 1000000.0;
2981     is->video_current_pts_drift = is->audio_current_pts_drift;
2982     is->audio_clock_serial = -1;
2983     is->video_clock_serial = -1;
2984     is->audio_last_serial = -1;
2985     is->av_sync_type = av_sync_type;
2986     is->read_tid     = SDL_CreateThread(read_thread, is);
2987     if (!is->read_tid) {
2988         av_free(is);
2989         return NULL;
2990     }
2991     return is;
2992 }
2993
2994 static void stream_cycle_channel(VideoState *is, int codec_type)
2995 {
2996     AVFormatContext *ic = is->ic;
2997     int start_index, stream_index;
2998     int old_index;
2999     AVStream *st;
3000
3001     if (codec_type == AVMEDIA_TYPE_VIDEO) {
3002         start_index = is->last_video_stream;
3003         old_index = is->video_stream;
3004     } else if (codec_type == AVMEDIA_TYPE_AUDIO) {
3005         start_index = is->last_audio_stream;
3006         old_index = is->audio_stream;
3007     } else {
3008         start_index = is->last_subtitle_stream;
3009         old_index = is->subtitle_stream;
3010     }
3011     stream_index = start_index;
3012     for (;;) {
3013         if (++stream_index >= is->ic->nb_streams)
3014         {
3015             if (codec_type == AVMEDIA_TYPE_SUBTITLE)
3016             {
3017                 stream_index = -1;
3018                 is->last_subtitle_stream = -1;
3019                 goto the_end;
3020             }
3021             if (start_index == -1)
3022                 return;
3023             stream_index = 0;
3024         }
3025         if (stream_index == start_index)
3026             return;
3027         st = ic->streams[stream_index];
3028         if (st->codec->codec_type == codec_type) {
3029             /* check that parameters are OK */
3030             switch (codec_type) {
3031             case AVMEDIA_TYPE_AUDIO:
3032                 if (st->codec->sample_rate != 0 &&
3033                     st->codec->channels != 0)
3034                     goto the_end;
3035                 break;
3036             case AVMEDIA_TYPE_VIDEO:
3037             case AVMEDIA_TYPE_SUBTITLE:
3038                 goto the_end;
3039             default:
3040                 break;
3041             }
3042         }
3043     }
3044  the_end:
3045     stream_component_close(is, old_index);
3046     stream_component_open(is, stream_index);
3047     if (codec_type == AVMEDIA_TYPE_VIDEO)
3048         is->queue_attachments_req = 1;
3049 }
3050
3051
3052 static void toggle_full_screen(VideoState *is)
3053 {
3054 #if defined(__APPLE__) && SDL_VERSION_ATLEAST(1, 2, 14)
3055     /* OS X needs to reallocate the SDL overlays */
3056     int i;
3057     for (i = 0; i < VIDEO_PICTURE_QUEUE_SIZE; i++)
3058         is->pictq[i].reallocate = 1;
3059 #endif
3060     is_full_screen = !is_full_screen;
3061     video_open(is, 1, NULL);
3062 }
3063
3064 static void toggle_audio_display(VideoState *is)
3065 {
3066     int bgcolor = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00);
3067     int next = is->show_mode;
3068     do {
3069         next = (next + 1) % SHOW_MODE_NB;
3070     } while (next != is->show_mode && (next == SHOW_MODE_VIDEO && !is->video_st || next != SHOW_MODE_VIDEO && !is->audio_st));
3071     if (is->show_mode != next) {
3072         fill_rectangle(screen,
3073                     is->xleft, is->ytop, is->width, is->height,
3074                     bgcolor, 1);
3075         is->force_refresh = 1;
3076         is->show_mode = next;
3077     }
3078 }
3079
3080 static void refresh_loop_wait_event(VideoState *is, SDL_Event *event) {
3081     double remaining_time = 0.0;
3082     SDL_PumpEvents();
3083     while (!SDL_PeepEvents(event, 1, SDL_GETEVENT, SDL_ALLEVENTS)) {
3084         if (!cursor_hidden && av_gettime() - cursor_last_shown > CURSOR_HIDE_DELAY) {
3085             SDL_ShowCursor(0);
3086             cursor_hidden = 1;
3087         }
3088         if (remaining_time > 0.0)
3089             av_usleep((int64_t)(remaining_time * 1000000.0));
3090         remaining_time = REFRESH_RATE;
3091         if (is->show_mode != SHOW_MODE_NONE && (!is->paused || is->force_refresh))
3092             video_refresh(is, &remaining_time);
3093         SDL_PumpEvents();
3094     }
3095 }
3096
3097 /* handle an event sent by the GUI */
3098 static void event_loop(VideoState *cur_stream)
3099 {
3100     SDL_Event event;
3101     double incr, pos, frac;
3102
3103     for (;;) {
3104         double x;
3105         refresh_loop_wait_event(cur_stream, &event);
3106         switch (event.type) {
3107         case SDL_KEYDOWN:
3108             if (exit_on_keydown) {
3109                 do_exit(cur_stream);
3110                 break;
3111             }
3112             switch (event.key.keysym.sym) {
3113             case SDLK_ESCAPE:
3114             case SDLK_q:
3115                 do_exit(cur_stream);
3116                 break;
3117             case SDLK_f:
3118                 toggle_full_screen(cur_stream);
3119                 cur_stream->force_refresh = 1;
3120                 break;
3121             case SDLK_p:
3122             case SDLK_SPACE:
3123                 toggle_pause(cur_stream);
3124                 break;
3125             case SDLK_s: // S: Step to next frame
3126                 step_to_next_frame(cur_stream);
3127                 break;
3128             case SDLK_a:
3129                 stream_cycle_channel(cur_stream, AVMEDIA_TYPE_AUDIO);
3130                 break;
3131             case SDLK_v:
3132                 stream_cycle_channel(cur_stream, AVMEDIA_TYPE_VIDEO);
3133                 break;
3134             case SDLK_t:
3135                 stream_cycle_channel(cur_stream, AVMEDIA_TYPE_SUBTITLE);
3136                 break;
3137             case SDLK_w:
3138                 toggle_audio_display(cur_stream);
3139                 break;
3140             case SDLK_PAGEUP:
3141                 incr = 600.0;
3142                 goto do_seek;
3143             case SDLK_PAGEDOWN:
3144                 incr = -600.0;
3145                 goto do_seek;
3146             case SDLK_LEFT:
3147                 incr = -10.0;
3148                 goto do_seek;
3149             case SDLK_RIGHT:
3150                 incr = 10.0;
3151                 goto do_seek;
3152             case SDLK_UP:
3153                 incr = 60.0;
3154                 goto do_seek;
3155             case SDLK_DOWN:
3156                 incr = -60.0;
3157             do_seek:
3158                     if (seek_by_bytes) {
3159                         if (cur_stream->video_stream >= 0 && cur_stream->video_current_pos >= 0) {
3160                             pos = cur_stream->video_current_pos;
3161                         } else if (cur_stream->audio_stream >= 0 && cur_stream->audio_pkt.pos >= 0) {
3162                             pos = cur_stream->audio_pkt.pos;
3163                         } else
3164                             pos = avio_tell(cur_stream->ic->pb);
3165                         if (cur_stream->ic->bit_rate)
3166                             incr *= cur_stream->ic->bit_rate / 8.0;
3167                         else
3168                             incr *= 180000.0;
3169                         pos += incr;
3170                         stream_seek(cur_stream, pos, incr, 1);
3171                     } else {
3172                         pos = get_master_clock(cur_stream);
3173                         if (isnan(pos))
3174                             pos = (double)cur_stream->seek_pos / AV_TIME_BASE;
3175                         pos += incr;
3176                         if (cur_stream->ic->start_time != AV_NOPTS_VALUE && pos < cur_stream->ic->start_time / (double)AV_TIME_BASE)
3177                             pos = cur_stream->ic->start_time / (double)AV_TIME_BASE;
3178                         stream_seek(cur_stream, (int64_t)(pos * AV_TIME_BASE), (int64_t)(incr * AV_TIME_BASE), 0);
3179                     }
3180                 break;
3181             default:
3182                 break;
3183             }
3184             break;
3185         case SDL_VIDEOEXPOSE:
3186             cur_stream->force_refresh = 1;
3187             break;
3188         case SDL_MOUSEBUTTONDOWN:
3189             if (exit_on_mousedown) {
3190                 do_exit(cur_stream);
3191                 break;
3192             }
3193         case SDL_MOUSEMOTION:
3194             if (cursor_hidden) {
3195                 SDL_ShowCursor(1);
3196                 cursor_hidden = 0;
3197             }
3198             cursor_last_shown = av_gettime();
3199             if (event.type == SDL_MOUSEBUTTONDOWN) {
3200                 x = event.button.x;
3201             } else {
3202                 if (event.motion.state != SDL_PRESSED)
3203                     break;
3204                 x = event.motion.x;
3205             }
3206                 if (seek_by_bytes || cur_stream->ic->duration <= 0) {
3207                     uint64_t size =  avio_size(cur_stream->ic->pb);
3208                     stream_seek(cur_stream, size*x/cur_stream->width, 0, 1);
3209                 } else {
3210                     int64_t ts;
3211                     int ns, hh, mm, ss;
3212                     int tns, thh, tmm, tss;
3213                     tns  = cur_stream->ic->duration / 1000000LL;
3214                     thh  = tns / 3600;
3215                     tmm  = (tns % 3600) / 60;
3216                     tss  = (tns % 60);
3217                     frac = x / cur_stream->width;
3218                     ns   = frac * tns;
3219                     hh   = ns / 3600;
3220                     mm   = (ns % 3600) / 60;
3221                     ss   = (ns % 60);
3222                     fprintf(stderr, "Seek to %2.0f%% (%2d:%02d:%02d) of total duration (%2d:%02d:%02d)       \n", frac*100,
3223                             hh, mm, ss, thh, tmm, tss);
3224                     ts = frac * cur_stream->ic->duration;
3225                     if (cur_stream->ic->start_time != AV_NOPTS_VALUE)
3226                         ts += cur_stream->ic->start_time;
3227                     stream_seek(cur_stream, ts, 0, 0);
3228                 }
3229             break;
3230         case SDL_VIDEORESIZE:
3231                 screen = SDL_SetVideoMode(event.resize.w, event.resize.h, 0,
3232                                           SDL_HWSURFACE|SDL_RESIZABLE|SDL_ASYNCBLIT|SDL_HWACCEL);
3233                 screen_width  = cur_stream->width  = event.resize.w;
3234                 screen_height = cur_stream->height = event.resize.h;
3235                 cur_stream->force_refresh = 1;
3236             break;
3237         case SDL_QUIT:
3238         case FF_QUIT_EVENT:
3239             do_exit(cur_stream);
3240             break;
3241         case FF_ALLOC_EVENT:
3242             alloc_picture(event.user.data1);
3243             break;
3244         default:
3245             break;
3246         }
3247     }
3248 }
3249
3250 static int opt_frame_size(void *optctx, const char *opt, const char *arg)
3251 {
3252     av_log(NULL, AV_LOG_WARNING, "Option -s is deprecated, use -video_size.\n");
3253     return opt_default(NULL, "video_size", arg);
3254 }
3255
3256 static int opt_width(void *optctx, const char *opt, const char *arg)
3257 {
3258     screen_width = parse_number_or_die(opt, arg, OPT_INT64, 1, INT_MAX);
3259     return 0;
3260 }
3261
3262 static int opt_height(void *optctx, const char *opt, const char *arg)
3263 {
3264     screen_height = parse_number_or_die(opt, arg, OPT_INT64, 1, INT_MAX);
3265     return 0;
3266 }
3267
3268 static int opt_format(void *optctx, const char *opt, const char *arg)
3269 {
3270     file_iformat = av_find_input_format(arg);
3271     if (!file_iformat) {
3272         fprintf(stderr, "Unknown input format: %s\n", arg);
3273         return AVERROR(EINVAL);
3274     }
3275     return 0;
3276 }
3277
3278 static int opt_frame_pix_fmt(void *optctx, const char *opt, const char *arg)
3279 {
3280     av_log(NULL, AV_LOG_WARNING, "Option -pix_fmt is deprecated, use -pixel_format.\n");
3281     return opt_default(NULL, "pixel_format", arg);
3282 }
3283
3284 static int opt_sync(void *optctx, const char *opt, const char *arg)
3285 {
3286     if (!strcmp(arg, "audio"))
3287         av_sync_type = AV_SYNC_AUDIO_MASTER;
3288     else if (!strcmp(arg, "video"))
3289         av_sync_type = AV_SYNC_VIDEO_MASTER;
3290     else if (!strcmp(arg, "ext"))
3291         av_sync_type = AV_SYNC_EXTERNAL_CLOCK;
3292     else {
3293         fprintf(stderr, "Unknown value for %s: %s\n", opt, arg);
3294         exit(1);
3295     }
3296     return 0;
3297 }
3298
3299 static int opt_seek(void *optctx, const char *opt, const char *arg)
3300 {
3301     start_time = parse_time_or_die(opt, arg, 1);
3302     return 0;
3303 }
3304
3305 static int opt_duration(void *optctx, const char *opt, const char *arg)
3306 {
3307     duration = parse_time_or_die(opt, arg, 1);
3308     return 0;
3309 }
3310
3311 static int opt_show_mode(void *optctx, const char *opt, const char *arg)
3312 {
3313     show_mode = !strcmp(arg, "video") ? SHOW_MODE_VIDEO :
3314                 !strcmp(arg, "waves") ? SHOW_MODE_WAVES :
3315                 !strcmp(arg, "rdft" ) ? SHOW_MODE_RDFT  :
3316                 parse_number_or_die(opt, arg, OPT_INT, 0, SHOW_MODE_NB-1);
3317     return 0;
3318 }
3319
3320 static void opt_input_file(void *optctx, const char *filename)
3321 {
3322     if (input_filename) {
3323         fprintf(stderr, "Argument '%s' provided as input filename, but '%s' was already specified.\n",
3324                 filename, input_filename);
3325         exit(1);
3326     }
3327     if (!strcmp(filename, "-"))
3328         filename = "pipe:";
3329     input_filename = filename;
3330 }
3331
3332 static int opt_codec(void *optctx, const char *opt, const char *arg)
3333 {
3334    const char *spec = strchr(opt, ':');
3335    if (!spec) {
3336        fprintf(stderr, "No media specifier was specified in '%s' in option '%s'\n",
3337                arg, opt);
3338        return AVERROR(EINVAL);
3339    }
3340    spec++;
3341    switch (spec[0]) {
3342    case 'a' :    audio_codec_name = arg; break;
3343    case 's' : subtitle_codec_name = arg; break;
3344    case 'v' :    video_codec_name = arg; break;
3345    default:
3346        fprintf(stderr, "Invalid media specifier '%s' in option '%s'\n", spec, opt);
3347        return AVERROR(EINVAL);
3348    }
3349    return 0;
3350 }
3351
3352 static int dummy;
3353
3354 static const OptionDef options[] = {
3355 #include "cmdutils_common_opts.h"
3356     { "x", HAS_ARG, { .func_arg = opt_width }, "force displayed width", "width" },
3357     { "y", HAS_ARG, { .func_arg = opt_height }, "force displayed height", "height" },
3358     { "s", HAS_ARG | OPT_VIDEO, { .func_arg = opt_frame_size }, "set frame size (WxH or abbreviation)", "size" },
3359     { "fs", OPT_BOOL, { &is_full_screen }, "force full screen" },
3360     { "an", OPT_BOOL, { &audio_disable }, "disable audio" },
3361     { "vn", OPT_BOOL, { &video_disable }, "disable video" },
3362     { "sn", OPT_BOOL, { &subtitle_disable }, "disable subtitling" },
3363     { "ast", OPT_INT | HAS_ARG | OPT_EXPERT, { &wanted_stream[AVMEDIA_TYPE_AUDIO] }, "select desired audio stream", "stream_number" },
3364     { "vst", OPT_INT | HAS_ARG | OPT_EXPERT, { &wanted_stream[AVMEDIA_TYPE_VIDEO] }, "select desired video stream", "stream_number" },
3365     { "sst", OPT_INT | HAS_ARG | OPT_EXPERT, { &wanted_stream[AVMEDIA_TYPE_SUBTITLE] }, "select desired subtitle stream", "stream_number" },
3366     { "ss", HAS_ARG, { .func_arg = opt_seek }, "seek to a given position in seconds", "pos" },
3367     { "t", HAS_ARG, { .func_arg = opt_duration }, "play  \"duration\" seconds of audio/video", "duration" },
3368     { "bytes", OPT_INT | HAS_ARG, { &seek_by_bytes }, "seek by bytes 0=off 1=on -1=auto", "val" },
3369     { "nodisp", OPT_BOOL, { &display_disable }, "disable graphical display" },
3370     { "f", HAS_ARG, { .func_arg = opt_format }, "force format", "fmt" },
3371     { "pix_fmt", HAS_ARG | OPT_EXPERT | OPT_VIDEO, { .func_arg = opt_frame_pix_fmt }, "set pixel format", "format" },
3372     { "stats", OPT_BOOL | OPT_EXPERT, { &show_status }, "show status", "" },
3373     { "bug", OPT_INT | HAS_ARG | OPT_EXPERT, { &workaround_bugs }, "workaround bugs", "" },
3374     { "fast", OPT_BOOL | OPT_EXPERT, { &fast }, "non spec compliant optimizations", "" },
3375     { "genpts", OPT_BOOL | OPT_EXPERT, { &genpts }, "generate pts", "" },
3376     { "drp", OPT_INT | HAS_ARG | OPT_EXPERT, { &decoder_reorder_pts }, "let decoder reorder pts 0=off 1=on -1=auto", ""},
3377     { "lowres", OPT_INT | HAS_ARG | OPT_EXPERT, { &lowres }, "", "" },
3378     { "idct", OPT_INT | HAS_ARG | OPT_EXPERT, { &idct }, "set idct algo",  "algo" },
3379     { "ec", OPT_INT | HAS_ARG | OPT_EXPERT, { &error_concealment }, "set error concealment options",  "bit_mask" },
3380     { "sync", HAS_ARG | OPT_EXPERT, { .func_arg = opt_sync }, "set audio-video sync. type (type=audio/video/ext)", "type" },
3381     { "autoexit", OPT_BOOL | OPT_EXPERT, { &autoexit }, "exit at the end", "" },
3382     { "exitonkeydown", OPT_BOOL | OPT_EXPERT, { &exit_on_keydown }, "exit on key down", "" },
3383     { "exitonmousedown", OPT_BOOL | OPT_EXPERT, { &exit_on_mousedown }, "exit on mouse down", "" },
3384     { "loop", OPT_INT | HAS_ARG | OPT_EXPERT, { &loop }, "set number of times the playback shall be looped", "loop count" },
3385     { "framedrop", OPT_BOOL | OPT_EXPERT, { &framedrop }, "drop frames when cpu is too slow", "" },
3386     { "infbuf", OPT_BOOL | OPT_EXPERT, { &infinite_buffer }, "don't limit the input buffer size (useful with realtime streams)", "" },
3387     { "window_title", OPT_STRING | HAS_ARG, { &window_title }, "set window title", "window title" },
3388 #if CONFIG_AVFILTER
3389     { "vf", OPT_STRING | HAS_ARG, { &vfilters }, "set video filters", "filter_graph" },
3390     { "af", OPT_STRING | HAS_ARG, { &afilters }, "set audio filters", "filter_graph" },
3391 #endif
3392     { "rdftspeed", OPT_INT | HAS_ARG| OPT_AUDIO | OPT_EXPERT, { &rdftspeed }, "rdft speed", "msecs" },
3393     { "showmode", HAS_ARG, { .func_arg = opt_show_mode}, "select show mode (0 = video, 1 = waves, 2 = RDFT)", "mode" },
3394     { "default", HAS_ARG | OPT_AUDIO | OPT_VIDEO | OPT_EXPERT, { .func_arg = opt_default }, "generic catch all option", "" },
3395     { "i", OPT_BOOL, { &dummy}, "read specified file", "input_file"},
3396     { "codec", HAS_ARG, { .func_arg = opt_codec}, "force decoder", "decoder_name" },
3397     { "acodec", HAS_ARG | OPT_STRING | OPT_EXPERT, {    &audio_codec_name }, "force audio decoder",    "decoder_name" },
3398     { "scodec", HAS_ARG | OPT_STRING | OPT_EXPERT, { &subtitle_codec_name }, "force subtitle decoder", "decoder_name" },
3399     { "vcodec", HAS_ARG | OPT_STRING | OPT_EXPERT, {    &video_codec_name }, "force video decoder",    "decoder_name" },
3400     { NULL, },
3401 };
3402
3403 static void show_usage(void)
3404 {
3405     av_log(NULL, AV_LOG_INFO, "Simple media player\n");
3406     av_log(NULL, AV_LOG_INFO, "usage: %s [options] input_file\n", program_name);
3407     av_log(NULL, AV_LOG_INFO, "\n");
3408 }
3409
3410 void show_help_default(const char *opt, const char *arg)
3411 {
3412     av_log_set_callback(log_callback_help);
3413     show_usage();
3414     show_help_options(options, "Main options:", 0, OPT_EXPERT, 0);
3415     show_help_options(options, "Advanced options:", OPT_EXPERT, 0, 0);
3416     printf("\n");
3417     show_help_children(avcodec_get_class(), AV_OPT_FLAG_DECODING_PARAM);
3418     show_help_children(avformat_get_class(), AV_OPT_FLAG_DECODING_PARAM);
3419 #if !CONFIG_AVFILTER
3420     show_help_children(sws_get_class(), AV_OPT_FLAG_ENCODING_PARAM);
3421 #else
3422     show_help_children(avfilter_get_class(), AV_OPT_FLAG_FILTERING_PARAM);
3423 #endif
3424     printf("\nWhile playing:\n"
3425            "q, ESC              quit\n"
3426            "f                   toggle full screen\n"
3427            "p, SPC              pause\n"
3428            "a                   cycle audio channel\n"
3429            "v                   cycle video channel\n"
3430            "t                   cycle subtitle channel\n"
3431            "w                   show audio waves\n"
3432            "s                   activate frame-step mode\n"
3433            "left/right          seek backward/forward 10 seconds\n"
3434            "down/up             seek backward/forward 1 minute\n"
3435            "page down/page up   seek backward/forward 10 minutes\n"
3436            "mouse click         seek to percentage in file corresponding to fraction of width\n"
3437            );
3438 }
3439
3440 static int lockmgr(void **mtx, enum AVLockOp op)
3441 {
3442    switch(op) {
3443       case AV_LOCK_CREATE:
3444           *mtx = SDL_CreateMutex();
3445           if(!*mtx)
3446               return 1;
3447           return 0;
3448       case AV_LOCK_OBTAIN:
3449           return !!SDL_LockMutex(*mtx);
3450       case AV_LOCK_RELEASE:
3451           return !!SDL_UnlockMutex(*mtx);
3452       case AV_LOCK_DESTROY:
3453           SDL_DestroyMutex(*mtx);
3454           return 0;
3455    }
3456    return 1;
3457 }
3458
3459 /* Called from the main */
3460 int main(int argc, char **argv)
3461 {
3462     int flags;
3463     VideoState *is;
3464     char dummy_videodriver[] = "SDL_VIDEODRIVER=dummy";
3465
3466     av_log_set_flags(AV_LOG_SKIP_REPEATED);
3467     parse_loglevel(argc, argv, options);
3468
3469     /* register all codecs, demux and protocols */
3470     avcodec_register_all();
3471 #if CONFIG_AVDEVICE
3472     avdevice_register_all();
3473 #endif
3474 #if CONFIG_AVFILTER
3475     avfilter_register_all();
3476 #endif
3477     av_register_all();
3478     avformat_network_init();
3479
3480     init_opts();
3481
3482     signal(SIGINT , sigterm_handler); /* Interrupt (ANSI).    */
3483     signal(SIGTERM, sigterm_handler); /* Termination (ANSI).  */
3484
3485     show_banner(argc, argv, options);
3486
3487     parse_options(NULL, argc, argv, options, opt_input_file);
3488
3489     if (!input_filename) {
3490         show_usage();
3491         fprintf(stderr, "An input file must be specified\n");
3492         fprintf(stderr, "Use -h to get full help or, even better, run 'man %s'\n", program_name);
3493         exit(1);
3494     }
3495
3496     if (display_disable) {
3497         video_disable = 1;
3498     }
3499     flags = SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER;
3500     if (audio_disable)
3501         flags &= ~SDL_INIT_AUDIO;
3502     if (display_disable)
3503         SDL_putenv(dummy_videodriver); /* For the event queue, we always need a video driver. */
3504 #if !defined(__MINGW32__) && !defined(__APPLE__)
3505     flags |= SDL_INIT_EVENTTHREAD; /* Not supported on Windows or Mac OS X */
3506 #endif
3507     if (SDL_Init (flags)) {
3508         fprintf(stderr, "Could not initialize SDL - %s\n", SDL_GetError());
3509         fprintf(stderr, "(Did you set the DISPLAY variable?)\n");
3510         exit(1);
3511     }
3512
3513     if (!display_disable) {
3514         const SDL_VideoInfo *vi = SDL_GetVideoInfo();
3515         fs_screen_width = vi->current_w;
3516         fs_screen_height = vi->current_h;
3517     }
3518
3519     SDL_EventState(SDL_ACTIVEEVENT, SDL_IGNORE);
3520     SDL_EventState(SDL_SYSWMEVENT, SDL_IGNORE);
3521     SDL_EventState(SDL_USEREVENT, SDL_IGNORE);
3522
3523     if (av_lockmgr_register(lockmgr)) {
3524         fprintf(stderr, "Could not initialize lock manager!\n");
3525         do_exit(NULL);
3526     }
3527
3528     av_init_packet(&flush_pkt);
3529     flush_pkt.data = (char *)(intptr_t)"FLUSH";
3530
3531     is = stream_open(input_filename, file_iformat);
3532     if (!is) {
3533         fprintf(stderr, "Failed to initialize VideoState!\n");
3534         do_exit(NULL);
3535     }
3536
3537     event_loop(is);
3538
3539     /* never returns */
3540
3541     return 0;
3542 }