e0b113c9dbeaec1a256b70ab0551314e399f34c3
[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     double ext_clock = get_external_clock(is);
1160     if (isnan(ext_clock) || fabs(ext_clock - pts) > AV_NOSYNC_THRESHOLD) {
1161         update_external_clock_pts(is, pts);
1162     }
1163 }
1164
1165 static void update_external_clock_speed(VideoState *is, double speed) {
1166     update_external_clock_pts(is, get_external_clock(is));
1167     is->external_clock_speed = speed;
1168 }
1169
1170 static void check_external_clock_speed(VideoState *is) {
1171    if (is->video_stream >= 0 && is->videoq.nb_packets <= MIN_FRAMES / 2 ||
1172        is->audio_stream >= 0 && is->audioq.nb_packets <= MIN_FRAMES / 2) {
1173        update_external_clock_speed(is, FFMAX(EXTERNAL_CLOCK_SPEED_MIN, is->external_clock_speed - EXTERNAL_CLOCK_SPEED_STEP));
1174    } else if ((is->video_stream < 0 || is->videoq.nb_packets > MIN_FRAMES * 2) &&
1175               (is->audio_stream < 0 || is->audioq.nb_packets > MIN_FRAMES * 2)) {
1176        update_external_clock_speed(is, FFMIN(EXTERNAL_CLOCK_SPEED_MAX, is->external_clock_speed + EXTERNAL_CLOCK_SPEED_STEP));
1177    } else {
1178        double speed = is->external_clock_speed;
1179        if (speed != 1.0)
1180            update_external_clock_speed(is, speed + EXTERNAL_CLOCK_SPEED_STEP * (1.0 - speed) / fabs(1.0 - speed));
1181    }
1182 }
1183
1184 /* seek in the stream */
1185 static void stream_seek(VideoState *is, int64_t pos, int64_t rel, int seek_by_bytes)
1186 {
1187     if (!is->seek_req) {
1188         is->seek_pos = pos;
1189         is->seek_rel = rel;
1190         is->seek_flags &= ~AVSEEK_FLAG_BYTE;
1191         if (seek_by_bytes)
1192             is->seek_flags |= AVSEEK_FLAG_BYTE;
1193         is->seek_req = 1;
1194         SDL_CondSignal(is->continue_read_thread);
1195     }
1196 }
1197
1198 /* pause or resume the video */
1199 static void stream_toggle_pause(VideoState *is)
1200 {
1201     if (is->paused) {
1202         is->frame_timer += av_gettime() / 1000000.0 + is->video_current_pts_drift - is->video_current_pts;
1203         if (is->read_pause_return != AVERROR(ENOSYS)) {
1204             is->video_current_pts = is->video_current_pts_drift + av_gettime() / 1000000.0;
1205         }
1206         is->video_current_pts_drift = is->video_current_pts - av_gettime() / 1000000.0;
1207     }
1208     update_external_clock_pts(is, get_external_clock(is));
1209     is->paused = !is->paused;
1210 }
1211
1212 static void toggle_pause(VideoState *is)
1213 {
1214     stream_toggle_pause(is);
1215     is->step = 0;
1216 }
1217
1218 static void step_to_next_frame(VideoState *is)
1219 {
1220     /* if the stream is paused unpause it, then step */
1221     if (is->paused)
1222         stream_toggle_pause(is);
1223     is->step = 1;
1224 }
1225
1226 static double compute_target_delay(double delay, VideoState *is)
1227 {
1228     double sync_threshold, diff;
1229
1230     /* update delay to follow master synchronisation source */
1231     if (get_master_sync_type(is) != AV_SYNC_VIDEO_MASTER) {
1232         /* if video is slave, we try to correct big delays by
1233            duplicating or deleting a frame */
1234         diff = get_video_clock(is) - get_master_clock(is);
1235
1236         /* skip or repeat frame. We take into account the
1237            delay to compute the threshold. I still don't know
1238            if it is the best guess */
1239         sync_threshold = FFMAX(AV_SYNC_THRESHOLD, delay);
1240         if (!isnan(diff) && fabs(diff) < AV_NOSYNC_THRESHOLD) {
1241             if (diff <= -sync_threshold)
1242                 delay = 0;
1243             else if (diff >= sync_threshold)
1244                 delay = 2 * delay;
1245         }
1246     }
1247
1248     av_dlog(NULL, "video: delay=%0.3f A-V=%f\n",
1249             delay, -diff);
1250
1251     return delay;
1252 }
1253
1254 static void pictq_next_picture(VideoState *is) {
1255     /* update queue size and signal for next picture */
1256     if (++is->pictq_rindex == VIDEO_PICTURE_QUEUE_SIZE)
1257         is->pictq_rindex = 0;
1258
1259     SDL_LockMutex(is->pictq_mutex);
1260     is->pictq_size--;
1261     SDL_CondSignal(is->pictq_cond);
1262     SDL_UnlockMutex(is->pictq_mutex);
1263 }
1264
1265 static int pictq_prev_picture(VideoState *is) {
1266     VideoPicture *prevvp;
1267     int ret = 0;
1268     /* update queue size and signal for the previous picture */
1269     prevvp = &is->pictq[(is->pictq_rindex + VIDEO_PICTURE_QUEUE_SIZE - 1) % VIDEO_PICTURE_QUEUE_SIZE];
1270     if (prevvp->allocated && prevvp->serial == is->videoq.serial) {
1271         SDL_LockMutex(is->pictq_mutex);
1272         if (is->pictq_size < VIDEO_PICTURE_QUEUE_SIZE - 1) {
1273             if (--is->pictq_rindex == -1)
1274                 is->pictq_rindex = VIDEO_PICTURE_QUEUE_SIZE - 1;
1275             is->pictq_size++;
1276             ret = 1;
1277         }
1278         SDL_CondSignal(is->pictq_cond);
1279         SDL_UnlockMutex(is->pictq_mutex);
1280     }
1281     return ret;
1282 }
1283
1284 static void update_video_pts(VideoState *is, double pts, int64_t pos, int serial) {
1285     double time = av_gettime() / 1000000.0;
1286     /* update current video pts */
1287     is->video_current_pts = pts;
1288     is->video_current_pts_drift = is->video_current_pts - time;
1289     is->video_current_pos = pos;
1290     is->frame_last_pts = pts;
1291     is->video_clock_serial = serial;
1292     if (is->videoq.serial == serial)
1293         check_external_clock_sync(is, is->video_current_pts);
1294 }
1295
1296 /* called to display each frame */
1297 static void video_refresh(void *opaque, double *remaining_time)
1298 {
1299     VideoState *is = opaque;
1300     VideoPicture *vp;
1301     double time;
1302
1303     SubPicture *sp, *sp2;
1304
1305     if (!is->paused && get_master_sync_type(is) == AV_SYNC_EXTERNAL_CLOCK && is->realtime)
1306         check_external_clock_speed(is);
1307
1308     if (!display_disable && is->show_mode != SHOW_MODE_VIDEO && is->audio_st) {
1309         time = av_gettime() / 1000000.0;
1310         if (is->force_refresh || is->last_vis_time + rdftspeed < time) {
1311             video_display(is);
1312             is->last_vis_time = time;
1313         }
1314         *remaining_time = FFMIN(*remaining_time, is->last_vis_time + rdftspeed - time);
1315     }
1316
1317     if (is->video_st) {
1318         int redisplay = 0;
1319         if (is->force_refresh)
1320             redisplay = pictq_prev_picture(is);
1321 retry:
1322         if (is->pictq_size == 0) {
1323             SDL_LockMutex(is->pictq_mutex);
1324             if (is->frame_last_dropped_pts != AV_NOPTS_VALUE && is->frame_last_dropped_pts > is->frame_last_pts) {
1325                 update_video_pts(is, is->frame_last_dropped_pts, is->frame_last_dropped_pos, 0);
1326                 is->frame_last_dropped_pts = AV_NOPTS_VALUE;
1327             }
1328             SDL_UnlockMutex(is->pictq_mutex);
1329             // nothing to do, no picture to display in the queue
1330         } else {
1331             double last_duration, duration, delay;
1332             /* dequeue the picture */
1333             vp = &is->pictq[is->pictq_rindex];
1334
1335             if (vp->serial != is->videoq.serial) {
1336                 pictq_next_picture(is);
1337                 redisplay = 0;
1338                 goto retry;
1339             }
1340
1341             if (is->paused)
1342                 goto display;
1343
1344             /* compute nominal last_duration */
1345             last_duration = vp->pts - is->frame_last_pts;
1346             if (last_duration > 0 && last_duration < is->max_frame_duration) {
1347                 /* if duration of the last frame was sane, update last_duration in video state */
1348                 is->frame_last_duration = last_duration;
1349             }
1350             delay = compute_target_delay(is->frame_last_duration, is);
1351
1352             time= av_gettime()/1000000.0;
1353             if (time < is->frame_timer + delay) {
1354                 *remaining_time = FFMIN(is->frame_timer + delay - time, *remaining_time);
1355                 return;
1356             }
1357
1358             if (delay > 0)
1359                 is->frame_timer += delay * FFMAX(1, floor((time-is->frame_timer) / delay));
1360
1361             SDL_LockMutex(is->pictq_mutex);
1362             update_video_pts(is, vp->pts, vp->pos, vp->serial);
1363             SDL_UnlockMutex(is->pictq_mutex);
1364
1365             if (is->pictq_size > 1) {
1366                 VideoPicture *nextvp = &is->pictq[(is->pictq_rindex + 1) % VIDEO_PICTURE_QUEUE_SIZE];
1367                 duration = nextvp->pts - vp->pts;
1368                 if(!is->step && (redisplay || framedrop>0 || (framedrop && get_master_sync_type(is) != AV_SYNC_VIDEO_MASTER)) && time > is->frame_timer + duration){
1369                     if (!redisplay)
1370                         is->frame_drops_late++;
1371                     pictq_next_picture(is);
1372                     redisplay = 0;
1373                     goto retry;
1374                 }
1375             }
1376
1377             if (is->subtitle_st) {
1378                 if (is->subtitle_stream_changed) {
1379                     SDL_LockMutex(is->subpq_mutex);
1380
1381                     while (is->subpq_size) {
1382                         free_subpicture(&is->subpq[is->subpq_rindex]);
1383
1384                         /* update queue size and signal for next picture */
1385                         if (++is->subpq_rindex == SUBPICTURE_QUEUE_SIZE)
1386                             is->subpq_rindex = 0;
1387
1388                         is->subpq_size--;
1389                     }
1390                     is->subtitle_stream_changed = 0;
1391
1392                     SDL_CondSignal(is->subpq_cond);
1393                     SDL_UnlockMutex(is->subpq_mutex);
1394                 } else {
1395                     if (is->subpq_size > 0) {
1396                         sp = &is->subpq[is->subpq_rindex];
1397
1398                         if (is->subpq_size > 1)
1399                             sp2 = &is->subpq[(is->subpq_rindex + 1) % SUBPICTURE_QUEUE_SIZE];
1400                         else
1401                             sp2 = NULL;
1402
1403                         if ((is->video_current_pts > (sp->pts + ((float) sp->sub.end_display_time / 1000)))
1404                                 || (sp2 && is->video_current_pts > (sp2->pts + ((float) sp2->sub.start_display_time / 1000))))
1405                         {
1406                             free_subpicture(sp);
1407
1408                             /* update queue size and signal for next picture */
1409                             if (++is->subpq_rindex == SUBPICTURE_QUEUE_SIZE)
1410                                 is->subpq_rindex = 0;
1411
1412                             SDL_LockMutex(is->subpq_mutex);
1413                             is->subpq_size--;
1414                             SDL_CondSignal(is->subpq_cond);
1415                             SDL_UnlockMutex(is->subpq_mutex);
1416                         }
1417                     }
1418                 }
1419             }
1420
1421 display:
1422             /* display picture */
1423             if (!display_disable && is->show_mode == SHOW_MODE_VIDEO)
1424                 video_display(is);
1425
1426             pictq_next_picture(is);
1427
1428             if (is->step && !is->paused)
1429                 stream_toggle_pause(is);
1430         }
1431     }
1432     is->force_refresh = 0;
1433     if (show_status) {
1434         static int64_t last_time;
1435         int64_t cur_time;
1436         int aqsize, vqsize, sqsize;
1437         double av_diff;
1438
1439         cur_time = av_gettime();
1440         if (!last_time || (cur_time - last_time) >= 30000) {
1441             aqsize = 0;
1442             vqsize = 0;
1443             sqsize = 0;
1444             if (is->audio_st)
1445                 aqsize = is->audioq.size;
1446             if (is->video_st)
1447                 vqsize = is->videoq.size;
1448             if (is->subtitle_st)
1449                 sqsize = is->subtitleq.size;
1450             av_diff = 0;
1451             if (is->audio_st && is->video_st)
1452                 av_diff = get_audio_clock(is) - get_video_clock(is);
1453             printf("%7.2f A-V:%7.3f fd=%4d aq=%5dKB vq=%5dKB sq=%5dB f=%"PRId64"/%"PRId64"   \r",
1454                    get_master_clock(is),
1455                    av_diff,
1456                    is->frame_drops_early + is->frame_drops_late,
1457                    aqsize / 1024,
1458                    vqsize / 1024,
1459                    sqsize,
1460                    is->video_st ? is->video_st->codec->pts_correction_num_faulty_dts : 0,
1461                    is->video_st ? is->video_st->codec->pts_correction_num_faulty_pts : 0);
1462             fflush(stdout);
1463             last_time = cur_time;
1464         }
1465     }
1466 }
1467
1468 /* allocate a picture (needs to do that in main thread to avoid
1469    potential locking problems */
1470 static void alloc_picture(VideoState *is)
1471 {
1472     VideoPicture *vp;
1473
1474     vp = &is->pictq[is->pictq_windex];
1475
1476     if (vp->bmp)
1477         SDL_FreeYUVOverlay(vp->bmp);
1478
1479 #if CONFIG_AVFILTER
1480     avfilter_unref_bufferp(&vp->picref);
1481 #endif
1482
1483     video_open(is, 0, vp);
1484
1485     vp->bmp = SDL_CreateYUVOverlay(vp->width, vp->height,
1486                                    SDL_YV12_OVERLAY,
1487                                    screen);
1488     if (!vp->bmp || vp->bmp->pitches[0] < vp->width) {
1489         /* SDL allocates a buffer smaller than requested if the video
1490          * overlay hardware is unable to support the requested size. */
1491         fprintf(stderr, "Error: the video system does not support an image\n"
1492                         "size of %dx%d pixels. Try using -lowres or -vf \"scale=w:h\"\n"
1493                         "to reduce the image size.\n", vp->width, vp->height );
1494         do_exit(is);
1495     }
1496
1497     SDL_LockMutex(is->pictq_mutex);
1498     vp->allocated = 1;
1499     SDL_CondSignal(is->pictq_cond);
1500     SDL_UnlockMutex(is->pictq_mutex);
1501 }
1502
1503 static void duplicate_right_border_pixels(SDL_Overlay *bmp) {
1504     int i, width, height;
1505     Uint8 *p, *maxp;
1506     for (i = 0; i < 3; i++) {
1507         width  = bmp->w;
1508         height = bmp->h;
1509         if (i > 0) {
1510             width  >>= 1;
1511             height >>= 1;
1512         }
1513         if (bmp->pitches[i] > width) {
1514             maxp = bmp->pixels[i] + bmp->pitches[i] * height - 1;
1515             for (p = bmp->pixels[i] + width - 1; p < maxp; p += bmp->pitches[i])
1516                 *(p+1) = *p;
1517         }
1518     }
1519 }
1520
1521 static int queue_picture(VideoState *is, AVFrame *src_frame, double pts, int64_t pos, int serial)
1522 {
1523     VideoPicture *vp;
1524
1525 #if defined(DEBUG_SYNC) && 0
1526     printf("frame_type=%c pts=%0.3f\n",
1527            av_get_picture_type_char(src_frame->pict_type), pts);
1528 #endif
1529
1530     /* wait until we have space to put a new picture */
1531     SDL_LockMutex(is->pictq_mutex);
1532
1533     /* keep the last already displayed picture in the queue */
1534     while (is->pictq_size >= VIDEO_PICTURE_QUEUE_SIZE - 2 &&
1535            !is->videoq.abort_request) {
1536         SDL_CondWait(is->pictq_cond, is->pictq_mutex);
1537     }
1538     SDL_UnlockMutex(is->pictq_mutex);
1539
1540     if (is->videoq.abort_request)
1541         return -1;
1542
1543     vp = &is->pictq[is->pictq_windex];
1544
1545 #if CONFIG_AVFILTER
1546     vp->sample_aspect_ratio = ((AVFilterBufferRef *)src_frame->opaque)->video->sample_aspect_ratio;
1547 #else
1548     vp->sample_aspect_ratio = av_guess_sample_aspect_ratio(is->ic, is->video_st, src_frame);
1549 #endif
1550
1551     /* alloc or resize hardware picture buffer */
1552     if (!vp->bmp || vp->reallocate || !vp->allocated ||
1553         vp->width  != src_frame->width ||
1554         vp->height != src_frame->height) {
1555         SDL_Event event;
1556
1557         vp->allocated  = 0;
1558         vp->reallocate = 0;
1559         vp->width = src_frame->width;
1560         vp->height = src_frame->height;
1561
1562         /* the allocation must be done in the main thread to avoid
1563            locking problems. */
1564         event.type = FF_ALLOC_EVENT;
1565         event.user.data1 = is;
1566         SDL_PushEvent(&event);
1567
1568         /* wait until the picture is allocated */
1569         SDL_LockMutex(is->pictq_mutex);
1570         while (!vp->allocated && !is->videoq.abort_request) {
1571             SDL_CondWait(is->pictq_cond, is->pictq_mutex);
1572         }
1573         /* if the queue is aborted, we have to pop the pending ALLOC event or wait for the allocation to complete */
1574         if (is->videoq.abort_request && SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_EVENTMASK(FF_ALLOC_EVENT)) != 1) {
1575             while (!vp->allocated) {
1576                 SDL_CondWait(is->pictq_cond, is->pictq_mutex);
1577             }
1578         }
1579         SDL_UnlockMutex(is->pictq_mutex);
1580
1581         if (is->videoq.abort_request)
1582             return -1;
1583     }
1584
1585     /* if the frame is not skipped, then display it */
1586     if (vp->bmp) {
1587         AVPicture pict = { { 0 } };
1588 #if CONFIG_AVFILTER
1589         avfilter_unref_bufferp(&vp->picref);
1590         vp->picref = src_frame->opaque;
1591 #endif
1592
1593         /* get a pointer on the bitmap */
1594         SDL_LockYUVOverlay (vp->bmp);
1595
1596         pict.data[0] = vp->bmp->pixels[0];
1597         pict.data[1] = vp->bmp->pixels[2];
1598         pict.data[2] = vp->bmp->pixels[1];
1599
1600         pict.linesize[0] = vp->bmp->pitches[0];
1601         pict.linesize[1] = vp->bmp->pitches[2];
1602         pict.linesize[2] = vp->bmp->pitches[1];
1603
1604 #if CONFIG_AVFILTER
1605         // FIXME use direct rendering
1606         av_picture_copy(&pict, (AVPicture *)src_frame,
1607                         src_frame->format, vp->width, vp->height);
1608 #else
1609         av_opt_get_int(sws_opts, "sws_flags", 0, &sws_flags);
1610         is->img_convert_ctx = sws_getCachedContext(is->img_convert_ctx,
1611             vp->width, vp->height, src_frame->format, vp->width, vp->height,
1612             AV_PIX_FMT_YUV420P, sws_flags, NULL, NULL, NULL);
1613         if (is->img_convert_ctx == NULL) {
1614             fprintf(stderr, "Cannot initialize the conversion context\n");
1615             exit(1);
1616         }
1617         sws_scale(is->img_convert_ctx, src_frame->data, src_frame->linesize,
1618                   0, vp->height, pict.data, pict.linesize);
1619 #endif
1620         /* workaround SDL PITCH_WORKAROUND */
1621         duplicate_right_border_pixels(vp->bmp);
1622         /* update the bitmap content */
1623         SDL_UnlockYUVOverlay(vp->bmp);
1624
1625         vp->pts = pts;
1626         vp->pos = pos;
1627         vp->serial = serial;
1628
1629         /* now we can update the picture count */
1630         if (++is->pictq_windex == VIDEO_PICTURE_QUEUE_SIZE)
1631             is->pictq_windex = 0;
1632         SDL_LockMutex(is->pictq_mutex);
1633         is->pictq_size++;
1634         SDL_UnlockMutex(is->pictq_mutex);
1635     }
1636     return 0;
1637 }
1638
1639 static int get_video_frame(VideoState *is, AVFrame *frame, int64_t *pts, AVPacket *pkt, int *serial)
1640 {
1641     int got_picture;
1642
1643     if (packet_queue_get(&is->videoq, pkt, 1, serial) < 0)
1644         return -1;
1645
1646     if (pkt->data == flush_pkt.data) {
1647         avcodec_flush_buffers(is->video_st->codec);
1648
1649         SDL_LockMutex(is->pictq_mutex);
1650         // Make sure there are no long delay timers (ideally we should just flush the queue but that's harder)
1651         while (is->pictq_size && !is->videoq.abort_request) {
1652             SDL_CondWait(is->pictq_cond, is->pictq_mutex);
1653         }
1654         is->video_current_pos = -1;
1655         is->frame_last_pts = AV_NOPTS_VALUE;
1656         is->frame_last_duration = 0;
1657         is->frame_timer = (double)av_gettime() / 1000000.0;
1658         is->frame_last_dropped_pts = AV_NOPTS_VALUE;
1659         SDL_UnlockMutex(is->pictq_mutex);
1660
1661         return 0;
1662     }
1663
1664     if(avcodec_decode_video2(is->video_st->codec, frame, &got_picture, pkt) < 0)
1665         return 0;
1666
1667     if (got_picture) {
1668         int ret = 1;
1669
1670         if (decoder_reorder_pts == -1) {
1671             *pts = av_frame_get_best_effort_timestamp(frame);
1672         } else if (decoder_reorder_pts) {
1673             *pts = frame->pkt_pts;
1674         } else {
1675             *pts = frame->pkt_dts;
1676         }
1677
1678         if (*pts == AV_NOPTS_VALUE) {
1679             *pts = 0;
1680         }
1681
1682         if (framedrop>0 || (framedrop && get_master_sync_type(is) != AV_SYNC_VIDEO_MASTER)) {
1683             SDL_LockMutex(is->pictq_mutex);
1684             if (is->frame_last_pts != AV_NOPTS_VALUE && *pts) {
1685                 double clockdiff = get_video_clock(is) - get_master_clock(is);
1686                 double dpts = av_q2d(is->video_st->time_base) * *pts;
1687                 double ptsdiff = dpts - is->frame_last_pts;
1688                 if (!isnan(clockdiff) && fabs(clockdiff) < AV_NOSYNC_THRESHOLD &&
1689                      ptsdiff > 0 && ptsdiff < AV_NOSYNC_THRESHOLD &&
1690                      clockdiff + ptsdiff - is->frame_last_filter_delay < 0) {
1691                     is->frame_last_dropped_pos = pkt->pos;
1692                     is->frame_last_dropped_pts = dpts;
1693                     is->frame_drops_early++;
1694                     ret = 0;
1695                 }
1696             }
1697             SDL_UnlockMutex(is->pictq_mutex);
1698         }
1699
1700         return ret;
1701     }
1702     return 0;
1703 }
1704
1705 #if CONFIG_AVFILTER
1706 static int configure_filtergraph(AVFilterGraph *graph, const char *filtergraph,
1707                                  AVFilterContext *source_ctx, AVFilterContext *sink_ctx)
1708 {
1709     int ret;
1710     AVFilterInOut *outputs = NULL, *inputs = NULL;
1711
1712     if (filtergraph) {
1713         outputs = avfilter_inout_alloc();
1714         inputs  = avfilter_inout_alloc();
1715         if (!outputs || !inputs) {
1716             ret = AVERROR(ENOMEM);
1717             goto fail;
1718         }
1719
1720         outputs->name       = av_strdup("in");
1721         outputs->filter_ctx = source_ctx;
1722         outputs->pad_idx    = 0;
1723         outputs->next       = NULL;
1724
1725         inputs->name        = av_strdup("out");
1726         inputs->filter_ctx  = sink_ctx;
1727         inputs->pad_idx     = 0;
1728         inputs->next        = NULL;
1729
1730         if ((ret = avfilter_graph_parse(graph, filtergraph, &inputs, &outputs, NULL)) < 0)
1731             goto fail;
1732     } else {
1733         if ((ret = avfilter_link(source_ctx, 0, sink_ctx, 0)) < 0)
1734             goto fail;
1735     }
1736
1737     ret = avfilter_graph_config(graph, NULL);
1738 fail:
1739     avfilter_inout_free(&outputs);
1740     avfilter_inout_free(&inputs);
1741     return ret;
1742 }
1743
1744 static int configure_video_filters(AVFilterGraph *graph, VideoState *is, const char *vfilters, AVFrame *frame)
1745 {
1746     static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE };
1747     char sws_flags_str[128];
1748     char buffersrc_args[256];
1749     int ret;
1750     AVBufferSinkParams *buffersink_params = av_buffersink_params_alloc();
1751     AVFilterContext *filt_src = NULL, *filt_out = NULL, *filt_crop;
1752     AVCodecContext *codec = is->video_st->codec;
1753
1754     if (!buffersink_params)
1755         return AVERROR(ENOMEM);
1756
1757     av_opt_get_int(sws_opts, "sws_flags", 0, &sws_flags);
1758     snprintf(sws_flags_str, sizeof(sws_flags_str), "flags=%"PRId64, sws_flags);
1759     graph->scale_sws_opts = av_strdup(sws_flags_str);
1760
1761     snprintf(buffersrc_args, sizeof(buffersrc_args),
1762              "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d",
1763              frame->width, frame->height, frame->format,
1764              is->video_st->time_base.num, is->video_st->time_base.den,
1765              codec->sample_aspect_ratio.num, FFMAX(codec->sample_aspect_ratio.den, 1));
1766
1767     if ((ret = avfilter_graph_create_filter(&filt_src,
1768                                             avfilter_get_by_name("buffer"),
1769                                             "ffplay_buffer", buffersrc_args, NULL,
1770                                             graph)) < 0)
1771         goto fail;
1772
1773     buffersink_params->pixel_fmts = pix_fmts;
1774     ret = avfilter_graph_create_filter(&filt_out,
1775                                        avfilter_get_by_name("ffbuffersink"),
1776                                        "ffplay_buffersink", NULL, buffersink_params, graph);
1777     if (ret < 0)
1778         goto fail;
1779
1780     /* SDL YUV code is not handling odd width/height for some driver
1781      * combinations, therefore we crop the picture to an even width/height. */
1782     if ((ret = avfilter_graph_create_filter(&filt_crop,
1783                                             avfilter_get_by_name("crop"),
1784                                             "ffplay_crop", "floor(in_w/2)*2:floor(in_h/2)*2", NULL, graph)) < 0)
1785         goto fail;
1786     if ((ret = avfilter_link(filt_crop, 0, filt_out, 0)) < 0)
1787         goto fail;
1788
1789     if ((ret = configure_filtergraph(graph, vfilters, filt_src, filt_crop)) < 0)
1790         goto fail;
1791
1792     is->in_video_filter  = filt_src;
1793     is->out_video_filter = filt_out;
1794
1795 fail:
1796     av_freep(&buffersink_params);
1797     return ret;
1798 }
1799
1800 #endif  /* CONFIG_AVFILTER */
1801
1802 static int video_thread(void *arg)
1803 {
1804     AVPacket pkt = { 0 };
1805     VideoState *is = arg;
1806     AVFrame *frame = avcodec_alloc_frame();
1807     int64_t pts_int = AV_NOPTS_VALUE, pos = -1;
1808     double pts;
1809     int ret;
1810     int serial = 0;
1811
1812 #if CONFIG_AVFILTER
1813     AVCodecContext *codec = is->video_st->codec;
1814     AVFilterGraph *graph = avfilter_graph_alloc();
1815     AVFilterContext *filt_out = NULL, *filt_in = NULL;
1816     int last_w = 0;
1817     int last_h = 0;
1818     enum AVPixelFormat last_format = -2;
1819     int last_serial = -1;
1820
1821     if (codec->codec->capabilities & CODEC_CAP_DR1) {
1822         is->use_dr1 = 1;
1823         codec->get_buffer     = codec_get_buffer;
1824         codec->release_buffer = codec_release_buffer;
1825         codec->opaque         = &is->buffer_pool;
1826     }
1827 #endif
1828
1829     for (;;) {
1830 #if CONFIG_AVFILTER
1831         AVFilterBufferRef *picref;
1832         AVRational tb;
1833 #endif
1834         while (is->paused && !is->videoq.abort_request)
1835             SDL_Delay(10);
1836
1837         avcodec_get_frame_defaults(frame);
1838         av_free_packet(&pkt);
1839
1840         ret = get_video_frame(is, frame, &pts_int, &pkt, &serial);
1841         if (ret < 0)
1842             goto the_end;
1843
1844         if (!ret)
1845             continue;
1846
1847 #if CONFIG_AVFILTER
1848         if (   last_w != frame->width
1849             || last_h != frame->height
1850             || last_format != frame->format
1851             || last_serial != serial) {
1852             av_log(NULL, AV_LOG_DEBUG,
1853                    "Video frame changed from size:%dx%d format:%s serial:%d to size:%dx%d format:%s serial:%d\n",
1854                    last_w, last_h,
1855                    (const char *)av_x_if_null(av_get_pix_fmt_name(last_format), "none"), last_serial,
1856                    frame->width, frame->height,
1857                    (const char *)av_x_if_null(av_get_pix_fmt_name(frame->format), "none"), serial);
1858             avfilter_graph_free(&graph);
1859             graph = avfilter_graph_alloc();
1860             if ((ret = configure_video_filters(graph, is, vfilters, frame)) < 0) {
1861                 SDL_Event event;
1862                 event.type = FF_QUIT_EVENT;
1863                 event.user.data1 = is;
1864                 SDL_PushEvent(&event);
1865                 av_free_packet(&pkt);
1866                 goto the_end;
1867             }
1868             filt_in  = is->in_video_filter;
1869             filt_out = is->out_video_filter;
1870             last_w = frame->width;
1871             last_h = frame->height;
1872             last_format = frame->format;
1873             last_serial = serial;
1874         }
1875
1876         frame->pts = pts_int;
1877         frame->sample_aspect_ratio = av_guess_sample_aspect_ratio(is->ic, is->video_st, frame);
1878         if (is->use_dr1 && frame->opaque) {
1879             FrameBuffer      *buf = frame->opaque;
1880             AVFilterBufferRef *fb = avfilter_get_video_buffer_ref_from_arrays(
1881                                         frame->data, frame->linesize,
1882                                         AV_PERM_READ | AV_PERM_PRESERVE,
1883                                         frame->width, frame->height,
1884                                         frame->format);
1885
1886             avfilter_copy_frame_props(fb, frame);
1887             fb->buf->priv           = buf;
1888             fb->buf->free           = filter_release_buffer;
1889
1890             buf->refcount++;
1891             av_buffersrc_add_ref(filt_in, fb, AV_BUFFERSRC_FLAG_NO_COPY);
1892
1893         } else
1894             av_buffersrc_write_frame(filt_in, frame);
1895
1896         av_free_packet(&pkt);
1897
1898         while (ret >= 0) {
1899             is->frame_last_returned_time = av_gettime() / 1000000.0;
1900
1901             ret = av_buffersink_get_buffer_ref(filt_out, &picref, 0);
1902             if (ret < 0) {
1903                 ret = 0;
1904                 break;
1905             }
1906
1907             is->frame_last_filter_delay = av_gettime() / 1000000.0 - is->frame_last_returned_time;
1908             if (fabs(is->frame_last_filter_delay) > AV_NOSYNC_THRESHOLD / 10.0)
1909                 is->frame_last_filter_delay = 0;
1910
1911             avfilter_copy_buf_props(frame, picref);
1912
1913             pts_int = picref->pts;
1914             tb      = filt_out->inputs[0]->time_base;
1915             pos     = picref->pos;
1916             frame->opaque = picref;
1917
1918             if (av_cmp_q(tb, is->video_st->time_base)) {
1919                 av_unused int64_t pts1 = pts_int;
1920                 pts_int = av_rescale_q(pts_int, tb, is->video_st->time_base);
1921                 av_dlog(NULL, "video_thread(): "
1922                         "tb:%d/%d pts:%"PRId64" -> tb:%d/%d pts:%"PRId64"\n",
1923                         tb.num, tb.den, pts1,
1924                         is->video_st->time_base.num, is->video_st->time_base.den, pts_int);
1925             }
1926             pts = pts_int * av_q2d(is->video_st->time_base);
1927             ret = queue_picture(is, frame, pts, pos, serial);
1928         }
1929 #else
1930         pts = pts_int * av_q2d(is->video_st->time_base);
1931         ret = queue_picture(is, frame, pts, pkt.pos, serial);
1932 #endif
1933
1934         if (ret < 0)
1935             goto the_end;
1936     }
1937  the_end:
1938     avcodec_flush_buffers(is->video_st->codec);
1939 #if CONFIG_AVFILTER
1940     avfilter_graph_free(&graph);
1941 #endif
1942     av_free_packet(&pkt);
1943     avcodec_free_frame(&frame);
1944     return 0;
1945 }
1946
1947 static int subtitle_thread(void *arg)
1948 {
1949     VideoState *is = arg;
1950     SubPicture *sp;
1951     AVPacket pkt1, *pkt = &pkt1;
1952     int got_subtitle;
1953     double pts;
1954     int i, j;
1955     int r, g, b, y, u, v, a;
1956
1957     for (;;) {
1958         while (is->paused && !is->subtitleq.abort_request) {
1959             SDL_Delay(10);
1960         }
1961         if (packet_queue_get(&is->subtitleq, pkt, 1, NULL) < 0)
1962             break;
1963
1964         if (pkt->data == flush_pkt.data) {
1965             avcodec_flush_buffers(is->subtitle_st->codec);
1966             continue;
1967         }
1968         SDL_LockMutex(is->subpq_mutex);
1969         while (is->subpq_size >= SUBPICTURE_QUEUE_SIZE &&
1970                !is->subtitleq.abort_request) {
1971             SDL_CondWait(is->subpq_cond, is->subpq_mutex);
1972         }
1973         SDL_UnlockMutex(is->subpq_mutex);
1974
1975         if (is->subtitleq.abort_request)
1976             return 0;
1977
1978         sp = &is->subpq[is->subpq_windex];
1979
1980        /* NOTE: ipts is the PTS of the _first_ picture beginning in
1981            this packet, if any */
1982         pts = 0;
1983         if (pkt->pts != AV_NOPTS_VALUE)
1984             pts = av_q2d(is->subtitle_st->time_base) * pkt->pts;
1985
1986         avcodec_decode_subtitle2(is->subtitle_st->codec, &sp->sub,
1987                                  &got_subtitle, pkt);
1988         if (got_subtitle && sp->sub.format == 0) {
1989             if (sp->sub.pts != AV_NOPTS_VALUE)
1990                 pts = sp->sub.pts / (double)AV_TIME_BASE;
1991             sp->pts = pts;
1992
1993             for (i = 0; i < sp->sub.num_rects; i++)
1994             {
1995                 for (j = 0; j < sp->sub.rects[i]->nb_colors; j++)
1996                 {
1997                     RGBA_IN(r, g, b, a, (uint32_t*)sp->sub.rects[i]->pict.data[1] + j);
1998                     y = RGB_TO_Y_CCIR(r, g, b);
1999                     u = RGB_TO_U_CCIR(r, g, b, 0);
2000                     v = RGB_TO_V_CCIR(r, g, b, 0);
2001                     YUVA_OUT((uint32_t*)sp->sub.rects[i]->pict.data[1] + j, y, u, v, a);
2002                 }
2003             }
2004
2005             /* now we can update the picture count */
2006             if (++is->subpq_windex == SUBPICTURE_QUEUE_SIZE)
2007                 is->subpq_windex = 0;
2008             SDL_LockMutex(is->subpq_mutex);
2009             is->subpq_size++;
2010             SDL_UnlockMutex(is->subpq_mutex);
2011         }
2012         av_free_packet(pkt);
2013     }
2014     return 0;
2015 }
2016
2017 /* copy samples for viewing in editor window */
2018 static void update_sample_display(VideoState *is, short *samples, int samples_size)
2019 {
2020     int size, len;
2021
2022     size = samples_size / sizeof(short);
2023     while (size > 0) {
2024         len = SAMPLE_ARRAY_SIZE - is->sample_array_index;
2025         if (len > size)
2026             len = size;
2027         memcpy(is->sample_array + is->sample_array_index, samples, len * sizeof(short));
2028         samples += len;
2029         is->sample_array_index += len;
2030         if (is->sample_array_index >= SAMPLE_ARRAY_SIZE)
2031             is->sample_array_index = 0;
2032         size -= len;
2033     }
2034 }
2035
2036 /* return the wanted number of samples to get better sync if sync_type is video
2037  * or external master clock */
2038 static int synchronize_audio(VideoState *is, int nb_samples)
2039 {
2040     int wanted_nb_samples = nb_samples;
2041
2042     /* if not master, then we try to remove or add samples to correct the clock */
2043     if (get_master_sync_type(is) != AV_SYNC_AUDIO_MASTER) {
2044         double diff, avg_diff;
2045         int min_nb_samples, max_nb_samples;
2046
2047         diff = get_audio_clock(is) - get_master_clock(is);
2048
2049         if (!isnan(diff) && fabs(diff) < AV_NOSYNC_THRESHOLD) {
2050             is->audio_diff_cum = diff + is->audio_diff_avg_coef * is->audio_diff_cum;
2051             if (is->audio_diff_avg_count < AUDIO_DIFF_AVG_NB) {
2052                 /* not enough measures to have a correct estimate */
2053                 is->audio_diff_avg_count++;
2054             } else {
2055                 /* estimate the A-V difference */
2056                 avg_diff = is->audio_diff_cum * (1.0 - is->audio_diff_avg_coef);
2057
2058                 if (fabs(avg_diff) >= is->audio_diff_threshold) {
2059                     wanted_nb_samples = nb_samples + (int)(diff * is->audio_src.freq);
2060                     min_nb_samples = ((nb_samples * (100 - SAMPLE_CORRECTION_PERCENT_MAX) / 100));
2061                     max_nb_samples = ((nb_samples * (100 + SAMPLE_CORRECTION_PERCENT_MAX) / 100));
2062                     wanted_nb_samples = FFMIN(FFMAX(wanted_nb_samples, min_nb_samples), max_nb_samples);
2063                 }
2064                 av_dlog(NULL, "diff=%f adiff=%f sample_diff=%d apts=%0.3f %f\n",
2065                         diff, avg_diff, wanted_nb_samples - nb_samples,
2066                         is->audio_clock, is->audio_diff_threshold);
2067             }
2068         } else {
2069             /* too big difference : may be initial PTS errors, so
2070                reset A-V filter */
2071             is->audio_diff_avg_count = 0;
2072             is->audio_diff_cum       = 0;
2073         }
2074     }
2075
2076     return wanted_nb_samples;
2077 }
2078
2079 /**
2080  * Decode one audio frame and return its uncompressed size.
2081  *
2082  * The processed audio frame is decoded, converted if required, and
2083  * stored in is->audio_buf, with size in bytes given by the return
2084  * value.
2085  */
2086 static int audio_decode_frame(VideoState *is)
2087 {
2088     AVPacket *pkt_temp = &is->audio_pkt_temp;
2089     AVPacket *pkt = &is->audio_pkt;
2090     AVCodecContext *dec = is->audio_st->codec;
2091     int len1, len2, data_size, resampled_data_size;
2092     int64_t dec_channel_layout;
2093     int got_frame;
2094     av_unused double audio_clock0;
2095     int new_packet = 0;
2096     int flush_complete = 0;
2097     int wanted_nb_samples;
2098
2099     for (;;) {
2100         /* NOTE: the audio packet can contain several frames */
2101         while (pkt_temp->size > 0 || (!pkt_temp->data && new_packet)) {
2102             if (!is->frame) {
2103                 if (!(is->frame = avcodec_alloc_frame()))
2104                     return AVERROR(ENOMEM);
2105             } else
2106                 avcodec_get_frame_defaults(is->frame);
2107
2108             if (is->audioq.serial != is->audio_pkt_temp_serial)
2109                 break;
2110
2111             if (is->paused)
2112                 return -1;
2113
2114             if (flush_complete)
2115                 break;
2116             new_packet = 0;
2117             len1 = avcodec_decode_audio4(dec, is->frame, &got_frame, pkt_temp);
2118             if (len1 < 0) {
2119                 /* if error, we skip the frame */
2120                 pkt_temp->size = 0;
2121                 break;
2122             }
2123
2124             pkt_temp->data += len1;
2125             pkt_temp->size -= len1;
2126
2127             if (!got_frame) {
2128                 /* stop sending empty packets if the decoder is finished */
2129                 if (!pkt_temp->data && dec->codec->capabilities & CODEC_CAP_DELAY)
2130                     flush_complete = 1;
2131                 continue;
2132             }
2133             data_size = av_samples_get_buffer_size(NULL, is->frame->channels,
2134                                                    is->frame->nb_samples,
2135                                                    is->frame->format, 1);
2136
2137             dec_channel_layout =
2138                 (is->frame->channel_layout && is->frame->channels == av_get_channel_layout_nb_channels(is->frame->channel_layout)) ?
2139                 is->frame->channel_layout : av_get_default_channel_layout(is->frame->channels);
2140             wanted_nb_samples = synchronize_audio(is, is->frame->nb_samples);
2141
2142             if (is->frame->format        != is->audio_src.fmt            ||
2143                 dec_channel_layout       != is->audio_src.channel_layout ||
2144                 is->frame->sample_rate   != is->audio_src.freq           ||
2145                 (wanted_nb_samples       != is->frame->nb_samples && !is->swr_ctx)) {
2146                 swr_free(&is->swr_ctx);
2147                 is->swr_ctx = swr_alloc_set_opts(NULL,
2148                                                  is->audio_tgt.channel_layout, is->audio_tgt.fmt, is->audio_tgt.freq,
2149                                                  dec_channel_layout,           is->frame->format, is->frame->sample_rate,
2150                                                  0, NULL);
2151                 if (!is->swr_ctx || swr_init(is->swr_ctx) < 0) {
2152                     fprintf(stderr, "Cannot create sample rate converter for conversion of %d Hz %s %d channels to %d Hz %s %d channels!\n",
2153                             is->frame->sample_rate, av_get_sample_fmt_name(is->frame->format), is->frame->channels,
2154                             is->audio_tgt.freq, av_get_sample_fmt_name(is->audio_tgt.fmt), is->audio_tgt.channels);
2155                     break;
2156                 }
2157                 is->audio_src.channel_layout = dec_channel_layout;
2158                 is->audio_src.channels = is->frame->channels;
2159                 is->audio_src.freq = is->frame->sample_rate;
2160                 is->audio_src.fmt = is->frame->format;
2161             }
2162
2163             if (is->swr_ctx) {
2164                 const uint8_t **in = (const uint8_t **)is->frame->extended_data;
2165                 uint8_t **out = &is->audio_buf1;
2166                 int out_count = (int64_t)wanted_nb_samples * is->audio_tgt.freq / is->frame->sample_rate + 256;
2167                 int out_size  = av_samples_get_buffer_size(NULL, is->audio_tgt.channels, out_count, is->audio_tgt.fmt, 0);
2168                 if (wanted_nb_samples != is->frame->nb_samples) {
2169                     if (swr_set_compensation(is->swr_ctx, (wanted_nb_samples - is->frame->nb_samples) * is->audio_tgt.freq / is->frame->sample_rate,
2170                                                 wanted_nb_samples * is->audio_tgt.freq / is->frame->sample_rate) < 0) {
2171                         fprintf(stderr, "swr_set_compensation() failed\n");
2172                         break;
2173                     }
2174                 }
2175                 av_fast_malloc(&is->audio_buf1, &is->audio_buf1_size, out_size);
2176                 if (!is->audio_buf1)
2177                     return AVERROR(ENOMEM);
2178                 len2 = swr_convert(is->swr_ctx, out, out_count, in, is->frame->nb_samples);
2179                 if (len2 < 0) {
2180                     fprintf(stderr, "swr_convert() failed\n");
2181                     break;
2182                 }
2183                 if (len2 == out_count) {
2184                     fprintf(stderr, "warning: audio buffer is probably too small\n");
2185                     swr_init(is->swr_ctx);
2186                 }
2187                 is->audio_buf = is->audio_buf1;
2188                 resampled_data_size = len2 * is->audio_tgt.channels * av_get_bytes_per_sample(is->audio_tgt.fmt);
2189             } else {
2190                 is->audio_buf = is->frame->data[0];
2191                 resampled_data_size = data_size;
2192             }
2193
2194             audio_clock0 = is->audio_clock;
2195             is->audio_clock += (double)data_size /
2196                 (is->frame->channels * is->frame->sample_rate * av_get_bytes_per_sample(is->frame->format));
2197 #ifdef DEBUG
2198             {
2199                 static double last_clock;
2200                 printf("audio: delay=%0.3f clock=%0.3f clock0=%0.3f\n",
2201                        is->audio_clock - last_clock,
2202                        is->audio_clock, audio_clock0);
2203                 last_clock = is->audio_clock;
2204             }
2205 #endif
2206             return resampled_data_size;
2207         }
2208
2209         /* free the current packet */
2210         if (pkt->data)
2211             av_free_packet(pkt);
2212         memset(pkt_temp, 0, sizeof(*pkt_temp));
2213
2214         if (is->audioq.abort_request) {
2215             return -1;
2216         }
2217
2218         if (is->audioq.nb_packets == 0)
2219             SDL_CondSignal(is->continue_read_thread);
2220
2221         /* read next packet */
2222         if ((new_packet = packet_queue_get(&is->audioq, pkt, 1, &is->audio_pkt_temp_serial)) < 0)
2223             return -1;
2224
2225         if (pkt->data == flush_pkt.data) {
2226             avcodec_flush_buffers(dec);
2227             flush_complete = 0;
2228         }
2229
2230         *pkt_temp = *pkt;
2231
2232         /* if update the audio clock with the pts */
2233         if (pkt->pts != AV_NOPTS_VALUE) {
2234             is->audio_clock = av_q2d(is->audio_st->time_base)*pkt->pts;
2235             is->audio_clock_serial = is->audio_pkt_temp_serial;
2236         }
2237     }
2238 }
2239
2240 /* prepare a new audio buffer */
2241 static void sdl_audio_callback(void *opaque, Uint8 *stream, int len)
2242 {
2243     VideoState *is = opaque;
2244     int audio_size, len1;
2245     int bytes_per_sec;
2246     int frame_size = av_samples_get_buffer_size(NULL, is->audio_tgt.channels, 1, is->audio_tgt.fmt, 1);
2247
2248     audio_callback_time = av_gettime();
2249
2250     while (len > 0) {
2251         if (is->audio_buf_index >= is->audio_buf_size) {
2252            audio_size = audio_decode_frame(is);
2253            if (audio_size < 0) {
2254                 /* if error, just output silence */
2255                is->audio_buf      = is->silence_buf;
2256                is->audio_buf_size = sizeof(is->silence_buf) / frame_size * frame_size;
2257            } else {
2258                if (is->show_mode != SHOW_MODE_VIDEO)
2259                    update_sample_display(is, (int16_t *)is->audio_buf, audio_size);
2260                is->audio_buf_size = audio_size;
2261            }
2262            is->audio_buf_index = 0;
2263         }
2264         len1 = is->audio_buf_size - is->audio_buf_index;
2265         if (len1 > len)
2266             len1 = len;
2267         memcpy(stream, (uint8_t *)is->audio_buf + is->audio_buf_index, len1);
2268         len -= len1;
2269         stream += len1;
2270         is->audio_buf_index += len1;
2271     }
2272     bytes_per_sec = is->audio_tgt.freq * is->audio_tgt.channels * av_get_bytes_per_sample(is->audio_tgt.fmt);
2273     is->audio_write_buf_size = is->audio_buf_size - is->audio_buf_index;
2274     /* Let's assume the audio driver that is used by SDL has two periods. */
2275     is->audio_current_pts = is->audio_clock - (double)(2 * is->audio_hw_buf_size + is->audio_write_buf_size) / bytes_per_sec;
2276     is->audio_current_pts_drift = is->audio_current_pts - audio_callback_time / 1000000.0;
2277     if (is->audioq.serial == is->audio_clock_serial)
2278         check_external_clock_sync(is, is->audio_current_pts);
2279 }
2280
2281 static int audio_open(void *opaque, int64_t wanted_channel_layout, int wanted_nb_channels, int wanted_sample_rate, struct AudioParams *audio_hw_params)
2282 {
2283     SDL_AudioSpec wanted_spec, spec;
2284     const char *env;
2285     const int next_nb_channels[] = {0, 0, 1, 6, 2, 6, 4, 6};
2286
2287     env = SDL_getenv("SDL_AUDIO_CHANNELS");
2288     if (env) {
2289         wanted_nb_channels = atoi(env);
2290         wanted_channel_layout = av_get_default_channel_layout(wanted_nb_channels);
2291     }
2292     if (!wanted_channel_layout || wanted_nb_channels != av_get_channel_layout_nb_channels(wanted_channel_layout)) {
2293         wanted_channel_layout = av_get_default_channel_layout(wanted_nb_channels);
2294         wanted_channel_layout &= ~AV_CH_LAYOUT_STEREO_DOWNMIX;
2295     }
2296     wanted_spec.channels = av_get_channel_layout_nb_channels(wanted_channel_layout);
2297     wanted_spec.freq = wanted_sample_rate;
2298     if (wanted_spec.freq <= 0 || wanted_spec.channels <= 0) {
2299         fprintf(stderr, "Invalid sample rate or channel count!\n");
2300         return -1;
2301     }
2302     wanted_spec.format = AUDIO_S16SYS;
2303     wanted_spec.silence = 0;
2304     wanted_spec.samples = SDL_AUDIO_BUFFER_SIZE;
2305     wanted_spec.callback = sdl_audio_callback;
2306     wanted_spec.userdata = opaque;
2307     while (SDL_OpenAudio(&wanted_spec, &spec) < 0) {
2308         fprintf(stderr, "SDL_OpenAudio (%d channels): %s\n", wanted_spec.channels, SDL_GetError());
2309         wanted_spec.channels = next_nb_channels[FFMIN(7, wanted_spec.channels)];
2310         if (!wanted_spec.channels) {
2311             fprintf(stderr, "No more channel combinations to try, audio open failed\n");
2312             return -1;
2313         }
2314         wanted_channel_layout = av_get_default_channel_layout(wanted_spec.channels);
2315     }
2316     if (spec.format != AUDIO_S16SYS) {
2317         fprintf(stderr, "SDL advised audio format %d is not supported!\n", spec.format);
2318         return -1;
2319     }
2320     if (spec.channels != wanted_spec.channels) {
2321         wanted_channel_layout = av_get_default_channel_layout(spec.channels);
2322         if (!wanted_channel_layout) {
2323             fprintf(stderr, "SDL advised channel count %d is not supported!\n", spec.channels);
2324             return -1;
2325         }
2326     }
2327
2328     audio_hw_params->fmt = AV_SAMPLE_FMT_S16;
2329     audio_hw_params->freq = spec.freq;
2330     audio_hw_params->channel_layout = wanted_channel_layout;
2331     audio_hw_params->channels =  spec.channels;
2332     return spec.size;
2333 }
2334
2335 /* open a given stream. Return 0 if OK */
2336 static int stream_component_open(VideoState *is, int stream_index)
2337 {
2338     AVFormatContext *ic = is->ic;
2339     AVCodecContext *avctx;
2340     AVCodec *codec;
2341     const char *forced_codec_name = NULL;
2342     AVDictionary *opts;
2343     AVDictionaryEntry *t = NULL;
2344
2345     if (stream_index < 0 || stream_index >= ic->nb_streams)
2346         return -1;
2347     avctx = ic->streams[stream_index]->codec;
2348
2349     codec = avcodec_find_decoder(avctx->codec_id);
2350
2351     switch(avctx->codec_type){
2352         case AVMEDIA_TYPE_AUDIO   : is->last_audio_stream    = stream_index; forced_codec_name =    audio_codec_name; break;
2353         case AVMEDIA_TYPE_SUBTITLE: is->last_subtitle_stream = stream_index; forced_codec_name = subtitle_codec_name; break;
2354         case AVMEDIA_TYPE_VIDEO   : is->last_video_stream    = stream_index; forced_codec_name =    video_codec_name; break;
2355     }
2356     if (forced_codec_name)
2357         codec = avcodec_find_decoder_by_name(forced_codec_name);
2358     if (!codec) {
2359         if (forced_codec_name) fprintf(stderr, "No codec could be found with name '%s'\n", forced_codec_name);
2360         else                   fprintf(stderr, "No codec could be found with id %d\n", avctx->codec_id);
2361         return -1;
2362     }
2363
2364     avctx->codec_id = codec->id;
2365     avctx->workaround_bugs   = workaround_bugs;
2366     avctx->lowres            = lowres;
2367     if(avctx->lowres > codec->max_lowres){
2368         av_log(avctx, AV_LOG_WARNING, "The maximum value for lowres supported by the decoder is %d\n",
2369                 codec->max_lowres);
2370         avctx->lowres= codec->max_lowres;
2371     }
2372     avctx->idct_algo         = idct;
2373     avctx->skip_frame        = skip_frame;
2374     avctx->skip_idct         = skip_idct;
2375     avctx->skip_loop_filter  = skip_loop_filter;
2376     avctx->error_concealment = error_concealment;
2377
2378     if(avctx->lowres) avctx->flags |= CODEC_FLAG_EMU_EDGE;
2379     if (fast)   avctx->flags2 |= CODEC_FLAG2_FAST;
2380     if(codec->capabilities & CODEC_CAP_DR1)
2381         avctx->flags |= CODEC_FLAG_EMU_EDGE;
2382
2383     opts = filter_codec_opts(codec_opts, avctx->codec_id, ic, ic->streams[stream_index], codec);
2384     if (!av_dict_get(opts, "threads", NULL, 0))
2385         av_dict_set(&opts, "threads", "auto", 0);
2386     if (avcodec_open2(avctx, codec, &opts) < 0)
2387         return -1;
2388     if ((t = av_dict_get(opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
2389         av_log(NULL, AV_LOG_ERROR, "Option %s not found.\n", t->key);
2390         return AVERROR_OPTION_NOT_FOUND;
2391     }
2392
2393     /* prepare audio output */
2394     if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
2395         int audio_hw_buf_size = audio_open(is, avctx->channel_layout, avctx->channels, avctx->sample_rate, &is->audio_src);
2396         if (audio_hw_buf_size < 0)
2397             return -1;
2398         is->audio_hw_buf_size = audio_hw_buf_size;
2399         is->audio_tgt = is->audio_src;
2400     }
2401
2402     ic->streams[stream_index]->discard = AVDISCARD_DEFAULT;
2403     switch (avctx->codec_type) {
2404     case AVMEDIA_TYPE_AUDIO:
2405         is->audio_stream = stream_index;
2406         is->audio_st = ic->streams[stream_index];
2407         is->audio_buf_size  = 0;
2408         is->audio_buf_index = 0;
2409
2410         /* init averaging filter */
2411         is->audio_diff_avg_coef  = exp(log(0.01) / AUDIO_DIFF_AVG_NB);
2412         is->audio_diff_avg_count = 0;
2413         /* since we do not have a precise anough audio fifo fullness,
2414            we correct audio sync only if larger than this threshold */
2415         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);
2416
2417         memset(&is->audio_pkt, 0, sizeof(is->audio_pkt));
2418         memset(&is->audio_pkt_temp, 0, sizeof(is->audio_pkt_temp));
2419         packet_queue_start(&is->audioq);
2420         SDL_PauseAudio(0);
2421         break;
2422     case AVMEDIA_TYPE_VIDEO:
2423         is->video_stream = stream_index;
2424         is->video_st = ic->streams[stream_index];
2425
2426         packet_queue_start(&is->videoq);
2427         is->video_tid = SDL_CreateThread(video_thread, is);
2428         break;
2429     case AVMEDIA_TYPE_SUBTITLE:
2430         is->subtitle_stream = stream_index;
2431         is->subtitle_st = ic->streams[stream_index];
2432         packet_queue_start(&is->subtitleq);
2433
2434         is->subtitle_tid = SDL_CreateThread(subtitle_thread, is);
2435         break;
2436     default:
2437         break;
2438     }
2439     return 0;
2440 }
2441
2442 static void stream_component_close(VideoState *is, int stream_index)
2443 {
2444     AVFormatContext *ic = is->ic;
2445     AVCodecContext *avctx;
2446
2447     if (stream_index < 0 || stream_index >= ic->nb_streams)
2448         return;
2449     avctx = ic->streams[stream_index]->codec;
2450
2451     switch (avctx->codec_type) {
2452     case AVMEDIA_TYPE_AUDIO:
2453         packet_queue_abort(&is->audioq);
2454
2455         SDL_CloseAudio();
2456
2457         packet_queue_flush(&is->audioq);
2458         av_free_packet(&is->audio_pkt);
2459         swr_free(&is->swr_ctx);
2460         av_freep(&is->audio_buf1);
2461         is->audio_buf1_size = 0;
2462         is->audio_buf = NULL;
2463         avcodec_free_frame(&is->frame);
2464
2465         if (is->rdft) {
2466             av_rdft_end(is->rdft);
2467             av_freep(&is->rdft_data);
2468             is->rdft = NULL;
2469             is->rdft_bits = 0;
2470         }
2471         break;
2472     case AVMEDIA_TYPE_VIDEO:
2473         packet_queue_abort(&is->videoq);
2474
2475         /* note: we also signal this mutex to make sure we deblock the
2476            video thread in all cases */
2477         SDL_LockMutex(is->pictq_mutex);
2478         SDL_CondSignal(is->pictq_cond);
2479         SDL_UnlockMutex(is->pictq_mutex);
2480
2481         SDL_WaitThread(is->video_tid, NULL);
2482
2483         packet_queue_flush(&is->videoq);
2484         break;
2485     case AVMEDIA_TYPE_SUBTITLE:
2486         packet_queue_abort(&is->subtitleq);
2487
2488         /* note: we also signal this mutex to make sure we deblock the
2489            video thread in all cases */
2490         SDL_LockMutex(is->subpq_mutex);
2491         is->subtitle_stream_changed = 1;
2492
2493         SDL_CondSignal(is->subpq_cond);
2494         SDL_UnlockMutex(is->subpq_mutex);
2495
2496         SDL_WaitThread(is->subtitle_tid, NULL);
2497
2498         packet_queue_flush(&is->subtitleq);
2499         break;
2500     default:
2501         break;
2502     }
2503
2504     ic->streams[stream_index]->discard = AVDISCARD_ALL;
2505     avcodec_close(avctx);
2506 #if CONFIG_AVFILTER
2507     free_buffer_pool(&is->buffer_pool);
2508 #endif
2509     switch (avctx->codec_type) {
2510     case AVMEDIA_TYPE_AUDIO:
2511         is->audio_st = NULL;
2512         is->audio_stream = -1;
2513         break;
2514     case AVMEDIA_TYPE_VIDEO:
2515         is->video_st = NULL;
2516         is->video_stream = -1;
2517         break;
2518     case AVMEDIA_TYPE_SUBTITLE:
2519         is->subtitle_st = NULL;
2520         is->subtitle_stream = -1;
2521         break;
2522     default:
2523         break;
2524     }
2525 }
2526
2527 static int decode_interrupt_cb(void *ctx)
2528 {
2529     VideoState *is = ctx;
2530     return is->abort_request;
2531 }
2532
2533 static int is_realtime(AVFormatContext *s)
2534 {
2535     if(   !strcmp(s->iformat->name, "rtp")
2536        || !strcmp(s->iformat->name, "rtsp")
2537        || !strcmp(s->iformat->name, "sdp")
2538     )
2539         return 1;
2540
2541     if(s->pb && (   !strncmp(s->filename, "rtp:", 4)
2542                  || !strncmp(s->filename, "udp:", 4)
2543                 )
2544     )
2545         return 1;
2546     return 0;
2547 }
2548
2549 /* this thread gets the stream from the disk or the network */
2550 static int read_thread(void *arg)
2551 {
2552     VideoState *is = arg;
2553     AVFormatContext *ic = NULL;
2554     int err, i, ret;
2555     int st_index[AVMEDIA_TYPE_NB];
2556     AVPacket pkt1, *pkt = &pkt1;
2557     int eof = 0;
2558     int pkt_in_play_range = 0;
2559     AVDictionaryEntry *t;
2560     AVDictionary **opts;
2561     int orig_nb_streams;
2562     SDL_mutex *wait_mutex = SDL_CreateMutex();
2563
2564     memset(st_index, -1, sizeof(st_index));
2565     is->last_video_stream = is->video_stream = -1;
2566     is->last_audio_stream = is->audio_stream = -1;
2567     is->last_subtitle_stream = is->subtitle_stream = -1;
2568
2569     ic = avformat_alloc_context();
2570     ic->interrupt_callback.callback = decode_interrupt_cb;
2571     ic->interrupt_callback.opaque = is;
2572     err = avformat_open_input(&ic, is->filename, is->iformat, &format_opts);
2573     if (err < 0) {
2574         print_error(is->filename, err);
2575         ret = -1;
2576         goto fail;
2577     }
2578     if ((t = av_dict_get(format_opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
2579         av_log(NULL, AV_LOG_ERROR, "Option %s not found.\n", t->key);
2580         ret = AVERROR_OPTION_NOT_FOUND;
2581         goto fail;
2582     }
2583     is->ic = ic;
2584
2585     if (genpts)
2586         ic->flags |= AVFMT_FLAG_GENPTS;
2587
2588     opts = setup_find_stream_info_opts(ic, codec_opts);
2589     orig_nb_streams = ic->nb_streams;
2590
2591     err = avformat_find_stream_info(ic, opts);
2592     if (err < 0) {
2593         fprintf(stderr, "%s: could not find codec parameters\n", is->filename);
2594         ret = -1;
2595         goto fail;
2596     }
2597     for (i = 0; i < orig_nb_streams; i++)
2598         av_dict_free(&opts[i]);
2599     av_freep(&opts);
2600
2601     if (ic->pb)
2602         ic->pb->eof_reached = 0; // FIXME hack, ffplay maybe should not use url_feof() to test for the end
2603
2604     if (seek_by_bytes < 0)
2605         seek_by_bytes = !!(ic->iformat->flags & AVFMT_TS_DISCONT);
2606
2607     is->max_frame_duration = (ic->iformat->flags & AVFMT_TS_DISCONT) ? 10.0 : 3600.0;
2608
2609     /* if seeking requested, we execute it */
2610     if (start_time != AV_NOPTS_VALUE) {
2611         int64_t timestamp;
2612
2613         timestamp = start_time;
2614         /* add the stream start time */
2615         if (ic->start_time != AV_NOPTS_VALUE)
2616             timestamp += ic->start_time;
2617         ret = avformat_seek_file(ic, -1, INT64_MIN, timestamp, INT64_MAX, 0);
2618         if (ret < 0) {
2619             fprintf(stderr, "%s: could not seek to position %0.3f\n",
2620                     is->filename, (double)timestamp / AV_TIME_BASE);
2621         }
2622     }
2623
2624     is->realtime = is_realtime(ic);
2625
2626     for (i = 0; i < ic->nb_streams; i++)
2627         ic->streams[i]->discard = AVDISCARD_ALL;
2628     if (!video_disable)
2629         st_index[AVMEDIA_TYPE_VIDEO] =
2630             av_find_best_stream(ic, AVMEDIA_TYPE_VIDEO,
2631                                 wanted_stream[AVMEDIA_TYPE_VIDEO], -1, NULL, 0);
2632     if (!audio_disable)
2633         st_index[AVMEDIA_TYPE_AUDIO] =
2634             av_find_best_stream(ic, AVMEDIA_TYPE_AUDIO,
2635                                 wanted_stream[AVMEDIA_TYPE_AUDIO],
2636                                 st_index[AVMEDIA_TYPE_VIDEO],
2637                                 NULL, 0);
2638     if (!video_disable)
2639         st_index[AVMEDIA_TYPE_SUBTITLE] =
2640             av_find_best_stream(ic, AVMEDIA_TYPE_SUBTITLE,
2641                                 wanted_stream[AVMEDIA_TYPE_SUBTITLE],
2642                                 (st_index[AVMEDIA_TYPE_AUDIO] >= 0 ?
2643                                  st_index[AVMEDIA_TYPE_AUDIO] :
2644                                  st_index[AVMEDIA_TYPE_VIDEO]),
2645                                 NULL, 0);
2646     if (show_status) {
2647         av_dump_format(ic, 0, is->filename, 0);
2648     }
2649
2650     is->show_mode = show_mode;
2651
2652     /* open the streams */
2653     if (st_index[AVMEDIA_TYPE_AUDIO] >= 0) {
2654         stream_component_open(is, st_index[AVMEDIA_TYPE_AUDIO]);
2655     }
2656
2657     ret = -1;
2658     if (st_index[AVMEDIA_TYPE_VIDEO] >= 0) {
2659         ret = stream_component_open(is, st_index[AVMEDIA_TYPE_VIDEO]);
2660     }
2661     if (is->show_mode == SHOW_MODE_NONE)
2662         is->show_mode = ret >= 0 ? SHOW_MODE_VIDEO : SHOW_MODE_RDFT;
2663
2664     if (st_index[AVMEDIA_TYPE_SUBTITLE] >= 0) {
2665         stream_component_open(is, st_index[AVMEDIA_TYPE_SUBTITLE]);
2666     }
2667
2668     if (is->video_stream < 0 && is->audio_stream < 0) {
2669         fprintf(stderr, "%s: could not open codecs\n", is->filename);
2670         ret = -1;
2671         goto fail;
2672     }
2673
2674     if (infinite_buffer < 0 && is->realtime)
2675         infinite_buffer = 1;
2676
2677     for (;;) {
2678         if (is->abort_request)
2679             break;
2680         if (is->paused != is->last_paused) {
2681             is->last_paused = is->paused;
2682             if (is->paused)
2683                 is->read_pause_return = av_read_pause(ic);
2684             else
2685                 av_read_play(ic);
2686         }
2687 #if CONFIG_RTSP_DEMUXER || CONFIG_MMSH_PROTOCOL
2688         if (is->paused &&
2689                 (!strcmp(ic->iformat->name, "rtsp") ||
2690                  (ic->pb && !strncmp(input_filename, "mmsh:", 5)))) {
2691             /* wait 10 ms to avoid trying to get another packet */
2692             /* XXX: horrible */
2693             SDL_Delay(10);
2694             continue;
2695         }
2696 #endif
2697         if (is->seek_req) {
2698             int64_t seek_target = is->seek_pos;
2699             int64_t seek_min    = is->seek_rel > 0 ? seek_target - is->seek_rel + 2: INT64_MIN;
2700             int64_t seek_max    = is->seek_rel < 0 ? seek_target - is->seek_rel - 2: INT64_MAX;
2701 // FIXME the +-2 is due to rounding being not done in the correct direction in generation
2702 //      of the seek_pos/seek_rel variables
2703
2704             ret = avformat_seek_file(is->ic, -1, seek_min, seek_target, seek_max, is->seek_flags);
2705             if (ret < 0) {
2706                 fprintf(stderr, "%s: error while seeking\n", is->ic->filename);
2707             } else {
2708                 if (is->audio_stream >= 0) {
2709                     packet_queue_flush(&is->audioq);
2710                     packet_queue_put(&is->audioq, &flush_pkt);
2711                 }
2712                 if (is->subtitle_stream >= 0) {
2713                     packet_queue_flush(&is->subtitleq);
2714                     packet_queue_put(&is->subtitleq, &flush_pkt);
2715                 }
2716                 if (is->video_stream >= 0) {
2717                     packet_queue_flush(&is->videoq);
2718                     packet_queue_put(&is->videoq, &flush_pkt);
2719                 }
2720                 if (is->seek_flags & AVSEEK_FLAG_BYTE) {
2721                    update_external_clock_pts(is, NAN);
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     update_external_clock_pts(is, NAN);
2858     update_external_clock_speed(is, 1.0);
2859     is->audio_current_pts_drift = -av_gettime() / 1000000.0;
2860     is->video_current_pts_drift = is->audio_current_pts_drift;
2861     is->audio_clock_serial = -1;
2862     is->video_clock_serial = -1;
2863     is->av_sync_type = av_sync_type;
2864     is->read_tid     = SDL_CreateThread(read_thread, is);
2865     if (!is->read_tid) {
2866         av_free(is);
2867         return NULL;
2868     }
2869     return is;
2870 }
2871
2872 static void stream_cycle_channel(VideoState *is, int codec_type)
2873 {
2874     AVFormatContext *ic = is->ic;
2875     int start_index, stream_index;
2876     int old_index;
2877     AVStream *st;
2878
2879     if (codec_type == AVMEDIA_TYPE_VIDEO) {
2880         start_index = is->last_video_stream;
2881         old_index = is->video_stream;
2882     } else if (codec_type == AVMEDIA_TYPE_AUDIO) {
2883         start_index = is->last_audio_stream;
2884         old_index = is->audio_stream;
2885     } else {
2886         start_index = is->last_subtitle_stream;
2887         old_index = is->subtitle_stream;
2888     }
2889     stream_index = start_index;
2890     for (;;) {
2891         if (++stream_index >= is->ic->nb_streams)
2892         {
2893             if (codec_type == AVMEDIA_TYPE_SUBTITLE)
2894             {
2895                 stream_index = -1;
2896                 is->last_subtitle_stream = -1;
2897                 goto the_end;
2898             }
2899             if (start_index == -1)
2900                 return;
2901             stream_index = 0;
2902         }
2903         if (stream_index == start_index)
2904             return;
2905         st = ic->streams[stream_index];
2906         if (st->codec->codec_type == codec_type) {
2907             /* check that parameters are OK */
2908             switch (codec_type) {
2909             case AVMEDIA_TYPE_AUDIO:
2910                 if (st->codec->sample_rate != 0 &&
2911                     st->codec->channels != 0)
2912                     goto the_end;
2913                 break;
2914             case AVMEDIA_TYPE_VIDEO:
2915             case AVMEDIA_TYPE_SUBTITLE:
2916                 goto the_end;
2917             default:
2918                 break;
2919             }
2920         }
2921     }
2922  the_end:
2923     stream_component_close(is, old_index);
2924     stream_component_open(is, stream_index);
2925     if (codec_type == AVMEDIA_TYPE_VIDEO)
2926         is->queue_attachments_req = 1;
2927 }
2928
2929
2930 static void toggle_full_screen(VideoState *is)
2931 {
2932 #if defined(__APPLE__) && SDL_VERSION_ATLEAST(1, 2, 14)
2933     /* OS X needs to reallocate the SDL overlays */
2934     int i;
2935     for (i = 0; i < VIDEO_PICTURE_QUEUE_SIZE; i++)
2936         is->pictq[i].reallocate = 1;
2937 #endif
2938     is_full_screen = !is_full_screen;
2939     video_open(is, 1, NULL);
2940 }
2941
2942 static void toggle_audio_display(VideoState *is)
2943 {
2944     int bgcolor = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00);
2945     is->show_mode = (is->show_mode + 1) % SHOW_MODE_NB;
2946     fill_rectangle(screen,
2947                 is->xleft, is->ytop, is->width, is->height,
2948                 bgcolor, 1);
2949 }
2950
2951 static void refresh_loop_wait_event(VideoState *is, SDL_Event *event) {
2952     double remaining_time = 0.0;
2953     SDL_PumpEvents();
2954     while (!SDL_PeepEvents(event, 1, SDL_GETEVENT, SDL_ALLEVENTS)) {
2955         if (!cursor_hidden && av_gettime() - cursor_last_shown > CURSOR_HIDE_DELAY) {
2956             SDL_ShowCursor(0);
2957             cursor_hidden = 1;
2958         }
2959         if (remaining_time > 0.0)
2960             av_usleep((int64_t)(remaining_time * 1000000.0));
2961         remaining_time = REFRESH_RATE;
2962         if (is->show_mode != SHOW_MODE_NONE && (!is->paused || is->force_refresh))
2963             video_refresh(is, &remaining_time);
2964         SDL_PumpEvents();
2965     }
2966 }
2967
2968 /* handle an event sent by the GUI */
2969 static void event_loop(VideoState *cur_stream)
2970 {
2971     SDL_Event event;
2972     double incr, pos, frac;
2973
2974     for (;;) {
2975         double x;
2976         refresh_loop_wait_event(cur_stream, &event);
2977         switch (event.type) {
2978         case SDL_KEYDOWN:
2979             if (exit_on_keydown) {
2980                 do_exit(cur_stream);
2981                 break;
2982             }
2983             switch (event.key.keysym.sym) {
2984             case SDLK_ESCAPE:
2985             case SDLK_q:
2986                 do_exit(cur_stream);
2987                 break;
2988             case SDLK_f:
2989                 toggle_full_screen(cur_stream);
2990                 cur_stream->force_refresh = 1;
2991                 break;
2992             case SDLK_p:
2993             case SDLK_SPACE:
2994                 toggle_pause(cur_stream);
2995                 break;
2996             case SDLK_s: // S: Step to next frame
2997                 step_to_next_frame(cur_stream);
2998                 break;
2999             case SDLK_a:
3000                 stream_cycle_channel(cur_stream, AVMEDIA_TYPE_AUDIO);
3001                 break;
3002             case SDLK_v:
3003                 stream_cycle_channel(cur_stream, AVMEDIA_TYPE_VIDEO);
3004                 break;
3005             case SDLK_t:
3006                 stream_cycle_channel(cur_stream, AVMEDIA_TYPE_SUBTITLE);
3007                 break;
3008             case SDLK_w:
3009                 toggle_audio_display(cur_stream);
3010                 cur_stream->force_refresh = 1;
3011                 break;
3012             case SDLK_PAGEUP:
3013                 incr = 600.0;
3014                 goto do_seek;
3015             case SDLK_PAGEDOWN:
3016                 incr = -600.0;
3017                 goto do_seek;
3018             case SDLK_LEFT:
3019                 incr = -10.0;
3020                 goto do_seek;
3021             case SDLK_RIGHT:
3022                 incr = 10.0;
3023                 goto do_seek;
3024             case SDLK_UP:
3025                 incr = 60.0;
3026                 goto do_seek;
3027             case SDLK_DOWN:
3028                 incr = -60.0;
3029             do_seek:
3030                     if (seek_by_bytes) {
3031                         if (cur_stream->video_stream >= 0 && cur_stream->video_current_pos >= 0) {
3032                             pos = cur_stream->video_current_pos;
3033                         } else if (cur_stream->audio_stream >= 0 && cur_stream->audio_pkt.pos >= 0) {
3034                             pos = cur_stream->audio_pkt.pos;
3035                         } else
3036                             pos = avio_tell(cur_stream->ic->pb);
3037                         if (cur_stream->ic->bit_rate)
3038                             incr *= cur_stream->ic->bit_rate / 8.0;
3039                         else
3040                             incr *= 180000.0;
3041                         pos += incr;
3042                         stream_seek(cur_stream, pos, incr, 1);
3043                     } else {
3044                         pos = get_master_clock(cur_stream);
3045                         if (isnan(pos))
3046                             pos = (double)cur_stream->seek_pos / AV_TIME_BASE;
3047                         pos += incr;
3048                         if (cur_stream->ic->start_time != AV_NOPTS_VALUE && pos < cur_stream->ic->start_time / (double)AV_TIME_BASE)
3049                             pos = cur_stream->ic->start_time / (double)AV_TIME_BASE;
3050                         stream_seek(cur_stream, (int64_t)(pos * AV_TIME_BASE), (int64_t)(incr * AV_TIME_BASE), 0);
3051                     }
3052                 break;
3053             default:
3054                 break;
3055             }
3056             break;
3057         case SDL_VIDEOEXPOSE:
3058             cur_stream->force_refresh = 1;
3059             break;
3060         case SDL_MOUSEBUTTONDOWN:
3061             if (exit_on_mousedown) {
3062                 do_exit(cur_stream);
3063                 break;
3064             }
3065         case SDL_MOUSEMOTION:
3066             if (cursor_hidden) {
3067                 SDL_ShowCursor(1);
3068                 cursor_hidden = 0;
3069             }
3070             cursor_last_shown = av_gettime();
3071             if (event.type == SDL_MOUSEBUTTONDOWN) {
3072                 x = event.button.x;
3073             } else {
3074                 if (event.motion.state != SDL_PRESSED)
3075                     break;
3076                 x = event.motion.x;
3077             }
3078                 if (seek_by_bytes || cur_stream->ic->duration <= 0) {
3079                     uint64_t size =  avio_size(cur_stream->ic->pb);
3080                     stream_seek(cur_stream, size*x/cur_stream->width, 0, 1);
3081                 } else {
3082                     int64_t ts;
3083                     int ns, hh, mm, ss;
3084                     int tns, thh, tmm, tss;
3085                     tns  = cur_stream->ic->duration / 1000000LL;
3086                     thh  = tns / 3600;
3087                     tmm  = (tns % 3600) / 60;
3088                     tss  = (tns % 60);
3089                     frac = x / cur_stream->width;
3090                     ns   = frac * tns;
3091                     hh   = ns / 3600;
3092                     mm   = (ns % 3600) / 60;
3093                     ss   = (ns % 60);
3094                     fprintf(stderr, "Seek to %2.0f%% (%2d:%02d:%02d) of total duration (%2d:%02d:%02d)       \n", frac*100,
3095                             hh, mm, ss, thh, tmm, tss);
3096                     ts = frac * cur_stream->ic->duration;
3097                     if (cur_stream->ic->start_time != AV_NOPTS_VALUE)
3098                         ts += cur_stream->ic->start_time;
3099                     stream_seek(cur_stream, ts, 0, 0);
3100                 }
3101             break;
3102         case SDL_VIDEORESIZE:
3103                 screen = SDL_SetVideoMode(event.resize.w, event.resize.h, 0,
3104                                           SDL_HWSURFACE|SDL_RESIZABLE|SDL_ASYNCBLIT|SDL_HWACCEL);
3105                 screen_width  = cur_stream->width  = event.resize.w;
3106                 screen_height = cur_stream->height = event.resize.h;
3107                 cur_stream->force_refresh = 1;
3108             break;
3109         case SDL_QUIT:
3110         case FF_QUIT_EVENT:
3111             do_exit(cur_stream);
3112             break;
3113         case FF_ALLOC_EVENT:
3114             alloc_picture(event.user.data1);
3115             break;
3116         default:
3117             break;
3118         }
3119     }
3120 }
3121
3122 static int opt_frame_size(void *optctx, const char *opt, const char *arg)
3123 {
3124     av_log(NULL, AV_LOG_WARNING, "Option -s is deprecated, use -video_size.\n");
3125     return opt_default(NULL, "video_size", arg);
3126 }
3127
3128 static int opt_width(void *optctx, const char *opt, const char *arg)
3129 {
3130     screen_width = parse_number_or_die(opt, arg, OPT_INT64, 1, INT_MAX);
3131     return 0;
3132 }
3133
3134 static int opt_height(void *optctx, const char *opt, const char *arg)
3135 {
3136     screen_height = parse_number_or_die(opt, arg, OPT_INT64, 1, INT_MAX);
3137     return 0;
3138 }
3139
3140 static int opt_format(void *optctx, const char *opt, const char *arg)
3141 {
3142     file_iformat = av_find_input_format(arg);
3143     if (!file_iformat) {
3144         fprintf(stderr, "Unknown input format: %s\n", arg);
3145         return AVERROR(EINVAL);
3146     }
3147     return 0;
3148 }
3149
3150 static int opt_frame_pix_fmt(void *optctx, const char *opt, const char *arg)
3151 {
3152     av_log(NULL, AV_LOG_WARNING, "Option -pix_fmt is deprecated, use -pixel_format.\n");
3153     return opt_default(NULL, "pixel_format", arg);
3154 }
3155
3156 static int opt_sync(void *optctx, const char *opt, const char *arg)
3157 {
3158     if (!strcmp(arg, "audio"))
3159         av_sync_type = AV_SYNC_AUDIO_MASTER;
3160     else if (!strcmp(arg, "video"))
3161         av_sync_type = AV_SYNC_VIDEO_MASTER;
3162     else if (!strcmp(arg, "ext"))
3163         av_sync_type = AV_SYNC_EXTERNAL_CLOCK;
3164     else {
3165         fprintf(stderr, "Unknown value for %s: %s\n", opt, arg);
3166         exit(1);
3167     }
3168     return 0;
3169 }
3170
3171 static int opt_seek(void *optctx, const char *opt, const char *arg)
3172 {
3173     start_time = parse_time_or_die(opt, arg, 1);
3174     return 0;
3175 }
3176
3177 static int opt_duration(void *optctx, const char *opt, const char *arg)
3178 {
3179     duration = parse_time_or_die(opt, arg, 1);
3180     return 0;
3181 }
3182
3183 static int opt_show_mode(void *optctx, const char *opt, const char *arg)
3184 {
3185     show_mode = !strcmp(arg, "video") ? SHOW_MODE_VIDEO :
3186                 !strcmp(arg, "waves") ? SHOW_MODE_WAVES :
3187                 !strcmp(arg, "rdft" ) ? SHOW_MODE_RDFT  :
3188                 parse_number_or_die(opt, arg, OPT_INT, 0, SHOW_MODE_NB-1);
3189     return 0;
3190 }
3191
3192 static void opt_input_file(void *optctx, const char *filename)
3193 {
3194     if (input_filename) {
3195         fprintf(stderr, "Argument '%s' provided as input filename, but '%s' was already specified.\n",
3196                 filename, input_filename);
3197         exit(1);
3198     }
3199     if (!strcmp(filename, "-"))
3200         filename = "pipe:";
3201     input_filename = filename;
3202 }
3203
3204 static int opt_codec(void *optctx, const char *opt, const char *arg)
3205 {
3206    const char *spec = strchr(opt, ':');
3207    if (!spec) {
3208        fprintf(stderr, "No media specifier was specified in '%s' in option '%s'\n",
3209                arg, opt);
3210        return AVERROR(EINVAL);
3211    }
3212    spec++;
3213    switch (spec[0]) {
3214    case 'a' :    audio_codec_name = arg; break;
3215    case 's' : subtitle_codec_name = arg; break;
3216    case 'v' :    video_codec_name = arg; break;
3217    default:
3218        fprintf(stderr, "Invalid media specifier '%s' in option '%s'\n", spec, opt);
3219        return AVERROR(EINVAL);
3220    }
3221    return 0;
3222 }
3223
3224 static int dummy;
3225
3226 static const OptionDef options[] = {
3227 #include "cmdutils_common_opts.h"
3228     { "x", HAS_ARG, { .func_arg = opt_width }, "force displayed width", "width" },
3229     { "y", HAS_ARG, { .func_arg = opt_height }, "force displayed height", "height" },
3230     { "s", HAS_ARG | OPT_VIDEO, { .func_arg = opt_frame_size }, "set frame size (WxH or abbreviation)", "size" },
3231     { "fs", OPT_BOOL, { &is_full_screen }, "force full screen" },
3232     { "an", OPT_BOOL, { &audio_disable }, "disable audio" },
3233     { "vn", OPT_BOOL, { &video_disable }, "disable video" },
3234     { "ast", OPT_INT | HAS_ARG | OPT_EXPERT, { &wanted_stream[AVMEDIA_TYPE_AUDIO] }, "select desired audio stream", "stream_number" },
3235     { "vst", OPT_INT | HAS_ARG | OPT_EXPERT, { &wanted_stream[AVMEDIA_TYPE_VIDEO] }, "select desired video stream", "stream_number" },
3236     { "sst", OPT_INT | HAS_ARG | OPT_EXPERT, { &wanted_stream[AVMEDIA_TYPE_SUBTITLE] }, "select desired subtitle stream", "stream_number" },
3237     { "ss", HAS_ARG, { .func_arg = opt_seek }, "seek to a given position in seconds", "pos" },
3238     { "t", HAS_ARG, { .func_arg = opt_duration }, "play  \"duration\" seconds of audio/video", "duration" },
3239     { "bytes", OPT_INT | HAS_ARG, { &seek_by_bytes }, "seek by bytes 0=off 1=on -1=auto", "val" },
3240     { "nodisp", OPT_BOOL, { &display_disable }, "disable graphical display" },
3241     { "f", HAS_ARG, { .func_arg = opt_format }, "force format", "fmt" },
3242     { "pix_fmt", HAS_ARG | OPT_EXPERT | OPT_VIDEO, { .func_arg = opt_frame_pix_fmt }, "set pixel format", "format" },
3243     { "stats", OPT_BOOL | OPT_EXPERT, { &show_status }, "show status", "" },
3244     { "bug", OPT_INT | HAS_ARG | OPT_EXPERT, { &workaround_bugs }, "workaround bugs", "" },
3245     { "fast", OPT_BOOL | OPT_EXPERT, { &fast }, "non spec compliant optimizations", "" },
3246     { "genpts", OPT_BOOL | OPT_EXPERT, { &genpts }, "generate pts", "" },
3247     { "drp", OPT_INT | HAS_ARG | OPT_EXPERT, { &decoder_reorder_pts }, "let decoder reorder pts 0=off 1=on -1=auto", ""},
3248     { "lowres", OPT_INT | HAS_ARG | OPT_EXPERT, { &lowres }, "", "" },
3249     { "skiploop", OPT_INT | HAS_ARG | OPT_EXPERT, { &skip_loop_filter }, "", "" },
3250     { "skipframe", OPT_INT | HAS_ARG | OPT_EXPERT, { &skip_frame }, "", "" },
3251     { "skipidct", OPT_INT | HAS_ARG | OPT_EXPERT, { &skip_idct }, "", "" },
3252     { "idct", OPT_INT | HAS_ARG | OPT_EXPERT, { &idct }, "set idct algo",  "algo" },
3253     { "ec", OPT_INT | HAS_ARG | OPT_EXPERT, { &error_concealment }, "set error concealment options",  "bit_mask" },
3254     { "sync", HAS_ARG | OPT_EXPERT, { .func_arg = opt_sync }, "set audio-video sync. type (type=audio/video/ext)", "type" },
3255     { "autoexit", OPT_BOOL | OPT_EXPERT, { &autoexit }, "exit at the end", "" },
3256     { "exitonkeydown", OPT_BOOL | OPT_EXPERT, { &exit_on_keydown }, "exit on key down", "" },
3257     { "exitonmousedown", OPT_BOOL | OPT_EXPERT, { &exit_on_mousedown }, "exit on mouse down", "" },
3258     { "loop", OPT_INT | HAS_ARG | OPT_EXPERT, { &loop }, "set number of times the playback shall be looped", "loop count" },
3259     { "framedrop", OPT_BOOL | OPT_EXPERT, { &framedrop }, "drop frames when cpu is too slow", "" },
3260     { "infbuf", OPT_BOOL | OPT_EXPERT, { &infinite_buffer }, "don't limit the input buffer size (useful with realtime streams)", "" },
3261     { "window_title", OPT_STRING | HAS_ARG, { &window_title }, "set window title", "window title" },
3262 #if CONFIG_AVFILTER
3263     { "vf", OPT_STRING | HAS_ARG, { &vfilters }, "set video filters", "filter_graph" },
3264 #endif
3265     { "rdftspeed", OPT_INT | HAS_ARG| OPT_AUDIO | OPT_EXPERT, { &rdftspeed }, "rdft speed", "msecs" },
3266     { "showmode", HAS_ARG, { .func_arg = opt_show_mode}, "select show mode (0 = video, 1 = waves, 2 = RDFT)", "mode" },
3267     { "default", HAS_ARG | OPT_AUDIO | OPT_VIDEO | OPT_EXPERT, { .func_arg = opt_default }, "generic catch all option", "" },
3268     { "i", OPT_BOOL, { &dummy}, "read specified file", "input_file"},
3269     { "codec", HAS_ARG, { .func_arg = opt_codec}, "force decoder", "decoder_name" },
3270     { "acodec", HAS_ARG | OPT_STRING | OPT_EXPERT, {    &audio_codec_name }, "force audio decoder",    "decoder_name" },
3271     { "scodec", HAS_ARG | OPT_STRING | OPT_EXPERT, { &subtitle_codec_name }, "force subtitle decoder", "decoder_name" },
3272     { "vcodec", HAS_ARG | OPT_STRING | OPT_EXPERT, {    &video_codec_name }, "force video decoder",    "decoder_name" },
3273     { NULL, },
3274 };
3275
3276 static void show_usage(void)
3277 {
3278     av_log(NULL, AV_LOG_INFO, "Simple media player\n");
3279     av_log(NULL, AV_LOG_INFO, "usage: %s [options] input_file\n", program_name);
3280     av_log(NULL, AV_LOG_INFO, "\n");
3281 }
3282
3283 void show_help_default(const char *opt, const char *arg)
3284 {
3285     av_log_set_callback(log_callback_help);
3286     show_usage();
3287     show_help_options(options, "Main options:", 0, OPT_EXPERT, 0);
3288     show_help_options(options, "Advanced options:", OPT_EXPERT, 0, 0);
3289     printf("\n");
3290     show_help_children(avcodec_get_class(), AV_OPT_FLAG_DECODING_PARAM);
3291     show_help_children(avformat_get_class(), AV_OPT_FLAG_DECODING_PARAM);
3292 #if !CONFIG_AVFILTER
3293     show_help_children(sws_get_class(), AV_OPT_FLAG_ENCODING_PARAM);
3294 #else
3295     show_help_children(avfilter_get_class(), AV_OPT_FLAG_FILTERING_PARAM);
3296 #endif
3297     printf("\nWhile playing:\n"
3298            "q, ESC              quit\n"
3299            "f                   toggle full screen\n"
3300            "p, SPC              pause\n"
3301            "a                   cycle audio channel\n"
3302            "v                   cycle video channel\n"
3303            "t                   cycle subtitle channel\n"
3304            "w                   show audio waves\n"
3305            "s                   activate frame-step mode\n"
3306            "left/right          seek backward/forward 10 seconds\n"
3307            "down/up             seek backward/forward 1 minute\n"
3308            "page down/page up   seek backward/forward 10 minutes\n"
3309            "mouse click         seek to percentage in file corresponding to fraction of width\n"
3310            );
3311 }
3312
3313 static int lockmgr(void **mtx, enum AVLockOp op)
3314 {
3315    switch(op) {
3316       case AV_LOCK_CREATE:
3317           *mtx = SDL_CreateMutex();
3318           if(!*mtx)
3319               return 1;
3320           return 0;
3321       case AV_LOCK_OBTAIN:
3322           return !!SDL_LockMutex(*mtx);
3323       case AV_LOCK_RELEASE:
3324           return !!SDL_UnlockMutex(*mtx);
3325       case AV_LOCK_DESTROY:
3326           SDL_DestroyMutex(*mtx);
3327           return 0;
3328    }
3329    return 1;
3330 }
3331
3332 /* Called from the main */
3333 int main(int argc, char **argv)
3334 {
3335     int flags;
3336     VideoState *is;
3337     char dummy_videodriver[] = "SDL_VIDEODRIVER=dummy";
3338
3339     av_log_set_flags(AV_LOG_SKIP_REPEATED);
3340     parse_loglevel(argc, argv, options);
3341
3342     /* register all codecs, demux and protocols */
3343     avcodec_register_all();
3344 #if CONFIG_AVDEVICE
3345     avdevice_register_all();
3346 #endif
3347 #if CONFIG_AVFILTER
3348     avfilter_register_all();
3349 #endif
3350     av_register_all();
3351     avformat_network_init();
3352
3353     init_opts();
3354
3355     signal(SIGINT , sigterm_handler); /* Interrupt (ANSI).    */
3356     signal(SIGTERM, sigterm_handler); /* Termination (ANSI).  */
3357
3358     show_banner(argc, argv, options);
3359
3360     parse_options(NULL, argc, argv, options, opt_input_file);
3361
3362     if (!input_filename) {
3363         show_usage();
3364         fprintf(stderr, "An input file must be specified\n");
3365         fprintf(stderr, "Use -h to get full help or, even better, run 'man %s'\n", program_name);
3366         exit(1);
3367     }
3368
3369     if (display_disable) {
3370         video_disable = 1;
3371     }
3372     flags = SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER;
3373     if (audio_disable)
3374         flags &= ~SDL_INIT_AUDIO;
3375     if (display_disable)
3376         SDL_putenv(dummy_videodriver); /* For the event queue, we always need a video driver. */
3377 #if !defined(__MINGW32__) && !defined(__APPLE__)
3378     flags |= SDL_INIT_EVENTTHREAD; /* Not supported on Windows or Mac OS X */
3379 #endif
3380     if (SDL_Init (flags)) {
3381         fprintf(stderr, "Could not initialize SDL - %s\n", SDL_GetError());
3382         fprintf(stderr, "(Did you set the DISPLAY variable?)\n");
3383         exit(1);
3384     }
3385
3386     if (!display_disable) {
3387 #if HAVE_SDL_VIDEO_SIZE
3388         const SDL_VideoInfo *vi = SDL_GetVideoInfo();
3389         fs_screen_width = vi->current_w;
3390         fs_screen_height = vi->current_h;
3391 #endif
3392     }
3393
3394     SDL_EventState(SDL_ACTIVEEVENT, SDL_IGNORE);
3395     SDL_EventState(SDL_SYSWMEVENT, SDL_IGNORE);
3396     SDL_EventState(SDL_USEREVENT, SDL_IGNORE);
3397
3398     if (av_lockmgr_register(lockmgr)) {
3399         fprintf(stderr, "Could not initialize lock manager!\n");
3400         do_exit(NULL);
3401     }
3402
3403     av_init_packet(&flush_pkt);
3404     flush_pkt.data = (char *)(intptr_t)"FLUSH";
3405
3406     is = stream_open(input_filename, file_iformat);
3407     if (!is) {
3408         fprintf(stderr, "Failed to initialize VideoState!\n");
3409         do_exit(NULL);
3410     }
3411
3412     event_loop(is);
3413
3414     /* never returns */
3415
3416     return 0;
3417 }