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