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