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