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