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