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