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