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