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