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