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