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