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