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