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