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