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