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