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