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