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