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