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