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