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