OSDN Git Service

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