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