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