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