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