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