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