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