2 * FFplay : Simple Media Player based on the FFmpeg libraries
3 * Copyright (c) 2003 Fabrice Bellard
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 #include "libavutil/avstring.h"
26 #include "libavutil/pixdesc.h"
27 #include "libavformat/avformat.h"
28 #include "libavdevice/avdevice.h"
29 #include "libswscale/swscale.h"
30 #include "libavcodec/audioconvert.h"
31 #include "libavcodec/colorspace.h"
32 #include "libavcodec/opt.h"
33 #include "libavcodec/fft.h"
36 # include "libavfilter/avfilter.h"
37 # include "libavfilter/avfiltergraph.h"
38 # include "libavfilter/graphparser.h"
44 #include <SDL_thread.h>
47 #undef main /* We don't want SDL to override our main() */
54 const char program_name
[] = "FFplay";
55 const int program_birth_year
= 2003;
59 #define MAX_QUEUE_SIZE (15 * 1024 * 1024)
60 #define MIN_AUDIOQ_SIZE (20 * 16 * 1024)
63 /* SDL audio buffer size, in samples. Should be small to have precise
64 A/V sync as SDL does not have hardware buffer fullness info. */
65 #define SDL_AUDIO_BUFFER_SIZE 1024
67 /* no AV sync correction is done if below the AV sync threshold */
68 #define AV_SYNC_THRESHOLD 0.01
69 /* no AV correction is done if too big error */
70 #define AV_NOSYNC_THRESHOLD 10.0
72 /* maximum audio speed change to get correct sync */
73 #define SAMPLE_CORRECTION_PERCENT_MAX 10
75 /* we use about AUDIO_DIFF_AVG_NB A-V differences to make the average */
76 #define AUDIO_DIFF_AVG_NB 20
78 /* NOTE: the size must be big enough to compensate the hardware audio buffersize size */
79 #define SAMPLE_ARRAY_SIZE (2*65536)
82 static int sws_flags
= SWS_BICUBIC
;
85 typedef struct PacketQueue
{
86 AVPacketList
*first_pkt
, *last_pkt
;
94 #define VIDEO_PICTURE_QUEUE_SIZE 1
95 #define SUBPICTURE_QUEUE_SIZE 4
97 typedef struct VideoPicture
{
98 double pts
; ///<presentation time stamp for this picture
99 int64_t pos
; ///<byte position in file
101 int width
, height
; /* source height & width */
103 SDL_TimerID timer_id
;
104 enum PixelFormat pix_fmt
;
107 AVFilterPicRef
*picref
;
111 typedef struct SubPicture
{
112 double pts
; /* presentation time stamp for this picture */
117 AV_SYNC_AUDIO_MASTER
, /* default choice */
118 AV_SYNC_VIDEO_MASTER
,
119 AV_SYNC_EXTERNAL_CLOCK
, /* synchronize to an external clock */
122 typedef struct VideoState
{
123 SDL_Thread
*parse_tid
;
124 SDL_Thread
*video_tid
;
125 AVInputFormat
*iformat
;
134 int read_pause_return
;
136 int dtg_active_format
;
141 double external_clock
; /* external clock base */
142 int64_t external_clock_time
;
145 double audio_diff_cum
; /* used for AV difference average computation */
146 double audio_diff_avg_coef
;
147 double audio_diff_threshold
;
148 int audio_diff_avg_count
;
151 int audio_hw_buf_size
;
152 /* samples output by the codec. we reserve more space for avsync
154 DECLARE_ALIGNED(16,uint8_t,audio_buf1
)[(AVCODEC_MAX_AUDIO_FRAME_SIZE
* 3) / 2];
155 DECLARE_ALIGNED(16,uint8_t,audio_buf2
)[(AVCODEC_MAX_AUDIO_FRAME_SIZE
* 3) / 2];
157 unsigned int audio_buf_size
; /* in bytes */
158 int audio_buf_index
; /* in bytes */
159 AVPacket audio_pkt_temp
;
161 enum SampleFormat audio_src_fmt
;
162 AVAudioConvert
*reformat_ctx
;
164 int show_audio
; /* if true, display audio samples */
165 int16_t sample_array
[SAMPLE_ARRAY_SIZE
];
166 int sample_array_index
;
172 SDL_Thread
*subtitle_tid
;
174 int subtitle_stream_changed
;
175 AVStream
*subtitle_st
;
176 PacketQueue subtitleq
;
177 SubPicture subpq
[SUBPICTURE_QUEUE_SIZE
];
178 int subpq_size
, subpq_rindex
, subpq_windex
;
179 SDL_mutex
*subpq_mutex
;
180 SDL_cond
*subpq_cond
;
183 double frame_last_pts
;
184 double frame_last_delay
;
185 double video_clock
; ///<pts of last decoded frame / predicted pts of next decoded frame
189 double video_current_pts
; ///<current displayed pts (different from video_clock if frame fifos are used)
190 double video_current_pts_drift
; ///<video_current_pts - time (av_gettime) at which we updated video_current_pts - used to have running video pts
191 int64_t video_current_pos
; ///<current displayed file pos
192 VideoPicture pictq
[VIDEO_PICTURE_QUEUE_SIZE
];
193 int pictq_size
, pictq_rindex
, pictq_windex
;
194 SDL_mutex
*pictq_mutex
;
195 SDL_cond
*pictq_cond
;
197 struct SwsContext
*img_convert_ctx
;
200 // QETimer *video_timer;
202 int width
, height
, xleft
, ytop
;
206 int64_t last_dts_for_fault_detection
;
207 int64_t last_pts_for_fault_detection
;
210 AVFilterContext
*out_video_filter
; ///<the last filter in the video chain
214 static void show_help(void);
215 static int audio_write_get_buf_size(VideoState
*is
);
217 /* options specified by the user */
218 static AVInputFormat
*file_iformat
;
219 static const char *input_filename
;
220 static int fs_screen_width
;
221 static int fs_screen_height
;
222 static int screen_width
= 0;
223 static int screen_height
= 0;
224 static int frame_width
= 0;
225 static int frame_height
= 0;
226 static enum PixelFormat frame_pix_fmt
= PIX_FMT_NONE
;
227 static int audio_disable
;
228 static int video_disable
;
229 static int wanted_stream
[CODEC_TYPE_NB
]={
230 [CODEC_TYPE_AUDIO
]=-1,
231 [CODEC_TYPE_VIDEO
]=-1,
232 [CODEC_TYPE_SUBTITLE
]=-1,
234 static int seek_by_bytes
=-1;
235 static int display_disable
;
236 static int show_status
= 1;
237 static int av_sync_type
= AV_SYNC_AUDIO_MASTER
;
238 static int64_t start_time
= AV_NOPTS_VALUE
;
239 static int debug
= 0;
240 static int debug_mv
= 0;
242 static int thread_count
= 1;
243 static int workaround_bugs
= 1;
245 static int genpts
= 0;
246 static int lowres
= 0;
247 static int idct
= FF_IDCT_AUTO
;
248 static enum AVDiscard skip_frame
= AVDISCARD_DEFAULT
;
249 static enum AVDiscard skip_idct
= AVDISCARD_DEFAULT
;
250 static enum AVDiscard skip_loop_filter
= AVDISCARD_DEFAULT
;
251 static int error_recognition
= FF_ER_CAREFUL
;
252 static int error_concealment
= 3;
253 static int decoder_reorder_pts
= -1;
256 static char *vfilters
= NULL
;
259 /* current context */
260 static int is_full_screen
;
261 static VideoState
*cur_stream
;
262 static int64_t audio_callback_time
;
264 static AVPacket flush_pkt
;
266 #define FF_ALLOC_EVENT (SDL_USEREVENT)
267 #define FF_REFRESH_EVENT (SDL_USEREVENT + 1)
268 #define FF_QUIT_EVENT (SDL_USEREVENT + 2)
270 static SDL_Surface
*screen
;
272 static int packet_queue_put(PacketQueue
*q
, AVPacket
*pkt
);
274 /* packet queue handling */
275 static void packet_queue_init(PacketQueue
*q
)
277 memset(q
, 0, sizeof(PacketQueue
));
278 q
->mutex
= SDL_CreateMutex();
279 q
->cond
= SDL_CreateCond();
280 packet_queue_put(q
, &flush_pkt
);
283 static void packet_queue_flush(PacketQueue
*q
)
285 AVPacketList
*pkt
, *pkt1
;
287 SDL_LockMutex(q
->mutex
);
288 for(pkt
= q
->first_pkt
; pkt
!= NULL
; pkt
= pkt1
) {
290 av_free_packet(&pkt
->pkt
);
297 SDL_UnlockMutex(q
->mutex
);
300 static void packet_queue_end(PacketQueue
*q
)
302 packet_queue_flush(q
);
303 SDL_DestroyMutex(q
->mutex
);
304 SDL_DestroyCond(q
->cond
);
307 static int packet_queue_put(PacketQueue
*q
, AVPacket
*pkt
)
311 /* duplicate the packet */
312 if (pkt
!=&flush_pkt
&& av_dup_packet(pkt
) < 0)
315 pkt1
= av_malloc(sizeof(AVPacketList
));
322 SDL_LockMutex(q
->mutex
);
328 q
->last_pkt
->next
= pkt1
;
331 q
->size
+= pkt1
->pkt
.size
+ sizeof(*pkt1
);
332 /* XXX: should duplicate packet data in DV case */
333 SDL_CondSignal(q
->cond
);
335 SDL_UnlockMutex(q
->mutex
);
339 static void packet_queue_abort(PacketQueue
*q
)
341 SDL_LockMutex(q
->mutex
);
343 q
->abort_request
= 1;
345 SDL_CondSignal(q
->cond
);
347 SDL_UnlockMutex(q
->mutex
);
350 /* return < 0 if aborted, 0 if no packet and > 0 if packet. */
351 static int packet_queue_get(PacketQueue
*q
, AVPacket
*pkt
, int block
)
356 SDL_LockMutex(q
->mutex
);
359 if (q
->abort_request
) {
366 q
->first_pkt
= pkt1
->next
;
370 q
->size
-= pkt1
->pkt
.size
+ sizeof(*pkt1
);
379 SDL_CondWait(q
->cond
, q
->mutex
);
382 SDL_UnlockMutex(q
->mutex
);
386 static inline void fill_rectangle(SDL_Surface
*screen
,
387 int x
, int y
, int w
, int h
, int color
)
394 SDL_FillRect(screen
, &rect
, color
);
398 /* draw only the border of a rectangle */
399 void fill_border(VideoState
*s
, int x
, int y
, int w
, int h
, int color
)
403 /* fill the background */
407 w2
= s
->width
- (x
+ w
);
413 h2
= s
->height
- (y
+ h
);
416 fill_rectangle(screen
,
420 fill_rectangle(screen
,
421 s
->xleft
+ s
->width
- w2
, s
->ytop
,
424 fill_rectangle(screen
,
425 s
->xleft
+ w1
, s
->ytop
,
426 s
->width
- w1
- w2
, h1
,
428 fill_rectangle(screen
,
429 s
->xleft
+ w1
, s
->ytop
+ s
->height
- h2
,
430 s
->width
- w1
- w2
, h2
,
435 #define ALPHA_BLEND(a, oldp, newp, s)\
436 ((((oldp << s) * (255 - (a))) + (newp * (a))) / (255 << s))
438 #define RGBA_IN(r, g, b, a, s)\
440 unsigned int v = ((const uint32_t *)(s))[0];\
441 a = (v >> 24) & 0xff;\
442 r = (v >> 16) & 0xff;\
443 g = (v >> 8) & 0xff;\
447 #define YUVA_IN(y, u, v, a, s, pal)\
449 unsigned int val = ((const uint32_t *)(pal))[*(const uint8_t*)(s)];\
450 a = (val >> 24) & 0xff;\
451 y = (val >> 16) & 0xff;\
452 u = (val >> 8) & 0xff;\
456 #define YUVA_OUT(d, y, u, v, a)\
458 ((uint32_t *)(d))[0] = (a << 24) | (y << 16) | (u << 8) | v;\
464 static void blend_subrect(AVPicture
*dst
, const AVSubtitleRect
*rect
, int imgw
, int imgh
)
466 int wrap
, wrap3
, width2
, skip2
;
467 int y
, u
, v
, a
, u1
, v1
, a1
, w
, h
;
468 uint8_t *lum
, *cb
, *cr
;
471 int dstx
, dsty
, dstw
, dsth
;
473 dstw
= av_clip(rect
->w
, 0, imgw
);
474 dsth
= av_clip(rect
->h
, 0, imgh
);
475 dstx
= av_clip(rect
->x
, 0, imgw
- dstw
);
476 dsty
= av_clip(rect
->y
, 0, imgh
- dsth
);
477 lum
= dst
->data
[0] + dsty
* dst
->linesize
[0];
478 cb
= dst
->data
[1] + (dsty
>> 1) * dst
->linesize
[1];
479 cr
= dst
->data
[2] + (dsty
>> 1) * dst
->linesize
[2];
481 width2
= ((dstw
+ 1) >> 1) + (dstx
& ~dstw
& 1);
483 wrap
= dst
->linesize
[0];
484 wrap3
= rect
->pict
.linesize
[0];
485 p
= rect
->pict
.data
[0];
486 pal
= (const uint32_t *)rect
->pict
.data
[1]; /* Now in YCrCb! */
494 YUVA_IN(y
, u
, v
, a
, p
, pal
);
495 lum
[0] = ALPHA_BLEND(a
, lum
[0], y
, 0);
496 cb
[0] = ALPHA_BLEND(a
>> 2, cb
[0], u
, 0);
497 cr
[0] = ALPHA_BLEND(a
>> 2, cr
[0], v
, 0);
503 for(w
= dstw
- (dstx
& 1); w
>= 2; w
-= 2) {
504 YUVA_IN(y
, u
, v
, a
, p
, pal
);
508 lum
[0] = ALPHA_BLEND(a
, lum
[0], y
, 0);
510 YUVA_IN(y
, u
, v
, a
, p
+ BPP
, pal
);
514 lum
[1] = ALPHA_BLEND(a
, lum
[1], y
, 0);
515 cb
[0] = ALPHA_BLEND(a1
>> 2, cb
[0], u1
, 1);
516 cr
[0] = ALPHA_BLEND(a1
>> 2, cr
[0], v1
, 1);
523 YUVA_IN(y
, u
, v
, a
, p
, pal
);
524 lum
[0] = ALPHA_BLEND(a
, lum
[0], y
, 0);
525 cb
[0] = ALPHA_BLEND(a
>> 2, cb
[0], u
, 0);
526 cr
[0] = ALPHA_BLEND(a
>> 2, cr
[0], v
, 0);
530 p
+= wrap3
- dstw
* BPP
;
531 lum
+= wrap
- dstw
- dstx
;
532 cb
+= dst
->linesize
[1] - width2
- skip2
;
533 cr
+= dst
->linesize
[2] - width2
- skip2
;
535 for(h
= dsth
- (dsty
& 1); h
>= 2; h
-= 2) {
541 YUVA_IN(y
, u
, v
, a
, p
, pal
);
545 lum
[0] = ALPHA_BLEND(a
, lum
[0], y
, 0);
548 YUVA_IN(y
, u
, v
, a
, p
, pal
);
552 lum
[0] = ALPHA_BLEND(a
, lum
[0], y
, 0);
553 cb
[0] = ALPHA_BLEND(a1
>> 2, cb
[0], u1
, 1);
554 cr
[0] = ALPHA_BLEND(a1
>> 2, cr
[0], v1
, 1);
560 for(w
= dstw
- (dstx
& 1); w
>= 2; w
-= 2) {
561 YUVA_IN(y
, u
, v
, a
, p
, pal
);
565 lum
[0] = ALPHA_BLEND(a
, lum
[0], y
, 0);
567 YUVA_IN(y
, u
, v
, a
, p
+ BPP
, pal
);
571 lum
[1] = ALPHA_BLEND(a
, lum
[1], y
, 0);
575 YUVA_IN(y
, u
, v
, a
, p
, pal
);
579 lum
[0] = ALPHA_BLEND(a
, lum
[0], y
, 0);
581 YUVA_IN(y
, u
, v
, a
, p
+ BPP
, pal
);
585 lum
[1] = ALPHA_BLEND(a
, lum
[1], y
, 0);
587 cb
[0] = ALPHA_BLEND(a1
>> 2, cb
[0], u1
, 2);
588 cr
[0] = ALPHA_BLEND(a1
>> 2, cr
[0], v1
, 2);
592 p
+= -wrap3
+ 2 * BPP
;
596 YUVA_IN(y
, u
, v
, a
, p
, pal
);
600 lum
[0] = ALPHA_BLEND(a
, lum
[0], y
, 0);
603 YUVA_IN(y
, u
, v
, a
, p
, pal
);
607 lum
[0] = ALPHA_BLEND(a
, lum
[0], y
, 0);
608 cb
[0] = ALPHA_BLEND(a1
>> 2, cb
[0], u1
, 1);
609 cr
[0] = ALPHA_BLEND(a1
>> 2, cr
[0], v1
, 1);
615 p
+= wrap3
+ (wrap3
- dstw
* BPP
);
616 lum
+= wrap
+ (wrap
- dstw
- dstx
);
617 cb
+= dst
->linesize
[1] - width2
- skip2
;
618 cr
+= dst
->linesize
[2] - width2
- skip2
;
620 /* handle odd height */
627 YUVA_IN(y
, u
, v
, a
, p
, pal
);
628 lum
[0] = ALPHA_BLEND(a
, lum
[0], y
, 0);
629 cb
[0] = ALPHA_BLEND(a
>> 2, cb
[0], u
, 0);
630 cr
[0] = ALPHA_BLEND(a
>> 2, cr
[0], v
, 0);
636 for(w
= dstw
- (dstx
& 1); w
>= 2; w
-= 2) {
637 YUVA_IN(y
, u
, v
, a
, p
, pal
);
641 lum
[0] = ALPHA_BLEND(a
, lum
[0], y
, 0);
643 YUVA_IN(y
, u
, v
, a
, p
+ BPP
, pal
);
647 lum
[1] = ALPHA_BLEND(a
, lum
[1], y
, 0);
648 cb
[0] = ALPHA_BLEND(a1
>> 2, cb
[0], u
, 1);
649 cr
[0] = ALPHA_BLEND(a1
>> 2, cr
[0], v
, 1);
656 YUVA_IN(y
, u
, v
, a
, p
, pal
);
657 lum
[0] = ALPHA_BLEND(a
, lum
[0], y
, 0);
658 cb
[0] = ALPHA_BLEND(a
>> 2, cb
[0], u
, 0);
659 cr
[0] = ALPHA_BLEND(a
>> 2, cr
[0], v
, 0);
664 static void free_subpicture(SubPicture
*sp
)
668 for (i
= 0; i
< sp
->sub
.num_rects
; i
++)
670 av_freep(&sp
->sub
.rects
[i
]->pict
.data
[0]);
671 av_freep(&sp
->sub
.rects
[i
]->pict
.data
[1]);
672 av_freep(&sp
->sub
.rects
[i
]);
675 av_free(sp
->sub
.rects
);
677 memset(&sp
->sub
, 0, sizeof(AVSubtitle
));
680 static void video_image_display(VideoState
*is
)
686 int width
, height
, x
, y
;
690 vp
= &is
->pictq
[is
->pictq_rindex
];
693 if (vp
->picref
->pixel_aspect
.num
== 0)
696 aspect_ratio
= av_q2d(vp
->picref
->pixel_aspect
);
699 /* XXX: use variable in the frame */
700 if (is
->video_st
->sample_aspect_ratio
.num
)
701 aspect_ratio
= av_q2d(is
->video_st
->sample_aspect_ratio
);
702 else if (is
->video_st
->codec
->sample_aspect_ratio
.num
)
703 aspect_ratio
= av_q2d(is
->video_st
->codec
->sample_aspect_ratio
);
707 if (aspect_ratio
<= 0.0)
709 aspect_ratio
*= (float)vp
->width
/ (float)vp
->height
;
710 /* if an active format is indicated, then it overrides the
713 if (is
->video_st
->codec
->dtg_active_format
!= is
->dtg_active_format
) {
714 is
->dtg_active_format
= is
->video_st
->codec
->dtg_active_format
;
715 printf("dtg_active_format=%d\n", is
->dtg_active_format
);
719 switch(is
->video_st
->codec
->dtg_active_format
) {
720 case FF_DTG_AFD_SAME
:
725 aspect_ratio
= 4.0 / 3.0;
727 case FF_DTG_AFD_16_9
:
728 aspect_ratio
= 16.0 / 9.0;
730 case FF_DTG_AFD_14_9
:
731 aspect_ratio
= 14.0 / 9.0;
733 case FF_DTG_AFD_4_3_SP_14_9
:
734 aspect_ratio
= 14.0 / 9.0;
736 case FF_DTG_AFD_16_9_SP_14_9
:
737 aspect_ratio
= 14.0 / 9.0;
739 case FF_DTG_AFD_SP_4_3
:
740 aspect_ratio
= 4.0 / 3.0;
747 if (is
->subpq_size
> 0)
749 sp
= &is
->subpq
[is
->subpq_rindex
];
751 if (vp
->pts
>= sp
->pts
+ ((float) sp
->sub
.start_display_time
/ 1000))
753 SDL_LockYUVOverlay (vp
->bmp
);
755 pict
.data
[0] = vp
->bmp
->pixels
[0];
756 pict
.data
[1] = vp
->bmp
->pixels
[2];
757 pict
.data
[2] = vp
->bmp
->pixels
[1];
759 pict
.linesize
[0] = vp
->bmp
->pitches
[0];
760 pict
.linesize
[1] = vp
->bmp
->pitches
[2];
761 pict
.linesize
[2] = vp
->bmp
->pitches
[1];
763 for (i
= 0; i
< sp
->sub
.num_rects
; i
++)
764 blend_subrect(&pict
, sp
->sub
.rects
[i
],
765 vp
->bmp
->w
, vp
->bmp
->h
);
767 SDL_UnlockYUVOverlay (vp
->bmp
);
773 /* XXX: we suppose the screen has a 1.0 pixel ratio */
775 width
= ((int)rint(height
* aspect_ratio
)) & ~1;
776 if (width
> is
->width
) {
778 height
= ((int)rint(width
/ aspect_ratio
)) & ~1;
780 x
= (is
->width
- width
) / 2;
781 y
= (is
->height
- height
) / 2;
782 if (!is
->no_background
) {
783 /* fill the background */
784 // fill_border(is, x, y, width, height, QERGB(0x00, 0x00, 0x00));
786 is
->no_background
= 0;
788 rect
.x
= is
->xleft
+ x
;
789 rect
.y
= is
->ytop
+ y
;
792 SDL_DisplayYUVOverlay(vp
->bmp
, &rect
);
795 fill_rectangle(screen
,
796 is
->xleft
, is
->ytop
, is
->width
, is
->height
,
797 QERGB(0x00, 0x00, 0x00));
802 static inline int compute_mod(int a
, int b
)
811 static void video_audio_display(VideoState
*s
)
813 int i
, i_start
, x
, y1
, y
, ys
, delay
, n
, nb_display_channels
;
814 int ch
, channels
, h
, h2
, bgcolor
, fgcolor
;
816 int rdft_bits
, nb_freq
;
818 for(rdft_bits
=1; (1<<rdft_bits
)<2*s
->height
; rdft_bits
++)
820 nb_freq
= 1<<(rdft_bits
-1);
822 /* compute display index : center on currently output samples */
823 channels
= s
->audio_st
->codec
->channels
;
824 nb_display_channels
= channels
;
826 int data_used
= s
->show_audio
==1 ? s
->width
: (2*nb_freq
);
828 delay
= audio_write_get_buf_size(s
);
831 /* to be more precise, we take into account the time spent since
832 the last buffer computation */
833 if (audio_callback_time
) {
834 time_diff
= av_gettime() - audio_callback_time
;
835 delay
+= (time_diff
* s
->audio_st
->codec
->sample_rate
) / 1000000;
838 delay
-= data_used
/ 2;
839 if (delay
< data_used
)
842 i_start
= x
= compute_mod(s
->sample_array_index
- delay
* channels
, SAMPLE_ARRAY_SIZE
);
843 if(s
->show_audio
==1){
845 for(i
=0; i
<1000; i
+=channels
){
846 int idx
= (SAMPLE_ARRAY_SIZE
+ x
- i
) % SAMPLE_ARRAY_SIZE
;
847 int a
= s
->sample_array
[idx
];
848 int b
= s
->sample_array
[(idx
+ 4*channels
)%SAMPLE_ARRAY_SIZE
];
849 int c
= s
->sample_array
[(idx
+ 5*channels
)%SAMPLE_ARRAY_SIZE
];
850 int d
= s
->sample_array
[(idx
+ 9*channels
)%SAMPLE_ARRAY_SIZE
];
852 if(h
<score
&& (b
^c
)<0){
859 s
->last_i_start
= i_start
;
861 i_start
= s
->last_i_start
;
864 bgcolor
= SDL_MapRGB(screen
->format
, 0x00, 0x00, 0x00);
865 if(s
->show_audio
==1){
866 fill_rectangle(screen
,
867 s
->xleft
, s
->ytop
, s
->width
, s
->height
,
870 fgcolor
= SDL_MapRGB(screen
->format
, 0xff, 0xff, 0xff);
872 /* total height for one channel */
873 h
= s
->height
/ nb_display_channels
;
874 /* graph height / 2 */
876 for(ch
= 0;ch
< nb_display_channels
; ch
++) {
878 y1
= s
->ytop
+ ch
* h
+ (h
/ 2); /* position of center line */
879 for(x
= 0; x
< s
->width
; x
++) {
880 y
= (s
->sample_array
[i
] * h2
) >> 15;
887 fill_rectangle(screen
,
888 s
->xleft
+ x
, ys
, 1, y
,
891 if (i
>= SAMPLE_ARRAY_SIZE
)
892 i
-= SAMPLE_ARRAY_SIZE
;
896 fgcolor
= SDL_MapRGB(screen
->format
, 0x00, 0x00, 0xff);
898 for(ch
= 1;ch
< nb_display_channels
; ch
++) {
899 y
= s
->ytop
+ ch
* h
;
900 fill_rectangle(screen
,
901 s
->xleft
, y
, s
->width
, 1,
904 SDL_UpdateRect(screen
, s
->xleft
, s
->ytop
, s
->width
, s
->height
);
906 nb_display_channels
= FFMIN(nb_display_channels
, 2);
907 if(rdft_bits
!= s
->rdft_bits
){
908 ff_rdft_end(&s
->rdft
);
909 ff_rdft_init(&s
->rdft
, rdft_bits
, DFT_R2C
);
910 s
->rdft_bits
= rdft_bits
;
913 FFTSample data
[2][2*nb_freq
];
914 for(ch
= 0;ch
< nb_display_channels
; ch
++) {
916 for(x
= 0; x
< 2*nb_freq
; x
++) {
917 double w
= (x
-nb_freq
)*(1.0/nb_freq
);
918 data
[ch
][x
]= s
->sample_array
[i
]*(1.0-w
*w
);
920 if (i
>= SAMPLE_ARRAY_SIZE
)
921 i
-= SAMPLE_ARRAY_SIZE
;
923 ff_rdft_calc(&s
->rdft
, data
[ch
]);
925 //least efficient way to do this, we should of course directly access it but its more than fast enough
926 for(y
=0; y
<s
->height
; y
++){
927 double w
= 1/sqrt(nb_freq
);
928 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]));
929 int b
= sqrt(w
*sqrt(data
[1][2*y
+0]*data
[1][2*y
+0] + data
[1][2*y
+1]*data
[1][2*y
+1]));
932 fgcolor
= SDL_MapRGB(screen
->format
, a
, b
, (a
+b
)/2);
934 fill_rectangle(screen
,
935 s
->xpos
, s
->height
-y
, 1, 1,
939 SDL_UpdateRect(screen
, s
->xpos
, s
->ytop
, 1, s
->height
);
941 if(s
->xpos
>= s
->width
)
946 static int video_open(VideoState
*is
){
947 int flags
= SDL_HWSURFACE
|SDL_ASYNCBLIT
|SDL_HWACCEL
;
950 if(is_full_screen
) flags
|= SDL_FULLSCREEN
;
951 else flags
|= SDL_RESIZABLE
;
953 if (is_full_screen
&& fs_screen_width
) {
955 h
= fs_screen_height
;
956 } else if(!is_full_screen
&& screen_width
){
960 }else if (is
->out_video_filter
&& is
->out_video_filter
->inputs
[0]){
961 w
= is
->out_video_filter
->inputs
[0]->w
;
962 h
= is
->out_video_filter
->inputs
[0]->h
;
964 }else if (is
->video_st
&& is
->video_st
->codec
->width
){
965 w
= is
->video_st
->codec
->width
;
966 h
= is
->video_st
->codec
->height
;
973 screen
= SDL_SetVideoMode(w
, h
, 0, flags
);
975 /* setting bits_per_pixel = 0 or 32 causes blank video on OS X */
976 screen
= SDL_SetVideoMode(w
, h
, 24, flags
);
979 fprintf(stderr
, "SDL: could not set video mode - exiting\n");
982 SDL_WM_SetCaption("FFplay", "FFplay");
984 is
->width
= screen
->w
;
985 is
->height
= screen
->h
;
990 /* display the current picture, if any */
991 static void video_display(VideoState
*is
)
994 video_open(cur_stream
);
995 if (is
->audio_st
&& is
->show_audio
)
996 video_audio_display(is
);
997 else if (is
->video_st
)
998 video_image_display(is
);
1001 static Uint32
sdl_refresh_timer_cb(Uint32 interval
, void *opaque
)
1004 event
.type
= FF_REFRESH_EVENT
;
1005 event
.user
.data1
= opaque
;
1006 SDL_PushEvent(&event
);
1007 return 0; /* 0 means stop timer */
1010 /* schedule a video refresh in 'delay' ms */
1011 static SDL_TimerID
schedule_refresh(VideoState
*is
, int delay
)
1013 if(!delay
) delay
=1; //SDL seems to be buggy when the delay is 0
1014 return SDL_AddTimer(delay
, sdl_refresh_timer_cb
, is
);
1017 /* get the current audio clock value */
1018 static double get_audio_clock(VideoState
*is
)
1021 int hw_buf_size
, bytes_per_sec
;
1022 pts
= is
->audio_clock
;
1023 hw_buf_size
= audio_write_get_buf_size(is
);
1026 bytes_per_sec
= is
->audio_st
->codec
->sample_rate
*
1027 2 * is
->audio_st
->codec
->channels
;
1030 pts
-= (double)hw_buf_size
/ bytes_per_sec
;
1034 /* get the current video clock value */
1035 static double get_video_clock(VideoState
*is
)
1038 return is
->video_current_pts
;
1040 return is
->video_current_pts_drift
+ av_gettime() / 1000000.0;
1044 /* get the current external clock value */
1045 static double get_external_clock(VideoState
*is
)
1049 return is
->external_clock
+ ((ti
- is
->external_clock_time
) * 1e-6);
1052 /* get the current master clock value */
1053 static double get_master_clock(VideoState
*is
)
1057 if (is
->av_sync_type
== AV_SYNC_VIDEO_MASTER
) {
1059 val
= get_video_clock(is
);
1061 val
= get_audio_clock(is
);
1062 } else if (is
->av_sync_type
== AV_SYNC_AUDIO_MASTER
) {
1064 val
= get_audio_clock(is
);
1066 val
= get_video_clock(is
);
1068 val
= get_external_clock(is
);
1073 /* seek in the stream */
1074 static void stream_seek(VideoState
*is
, int64_t pos
, int64_t rel
, int seek_by_bytes
)
1076 if (!is
->seek_req
) {
1079 is
->seek_flags
&= ~AVSEEK_FLAG_BYTE
;
1081 is
->seek_flags
|= AVSEEK_FLAG_BYTE
;
1086 /* pause or resume the video */
1087 static void stream_pause(VideoState
*is
)
1090 is
->frame_timer
+= av_gettime() / 1000000.0 + is
->video_current_pts_drift
- is
->video_current_pts
;
1091 if(is
->read_pause_return
!= AVERROR(ENOSYS
)){
1092 is
->video_current_pts
= is
->video_current_pts_drift
+ av_gettime() / 1000000.0;
1094 is
->video_current_pts_drift
= is
->video_current_pts
- av_gettime() / 1000000.0;
1096 is
->paused
= !is
->paused
;
1099 static double compute_frame_delay(double frame_current_pts
, VideoState
*is
)
1101 double actual_delay
, delay
, sync_threshold
, diff
;
1103 /* compute nominal delay */
1104 delay
= frame_current_pts
- is
->frame_last_pts
;
1105 if (delay
<= 0 || delay
>= 10.0) {
1106 /* if incorrect delay, use previous one */
1107 delay
= is
->frame_last_delay
;
1109 is
->frame_last_delay
= delay
;
1111 is
->frame_last_pts
= frame_current_pts
;
1113 /* update delay to follow master synchronisation source */
1114 if (((is
->av_sync_type
== AV_SYNC_AUDIO_MASTER
&& is
->audio_st
) ||
1115 is
->av_sync_type
== AV_SYNC_EXTERNAL_CLOCK
)) {
1116 /* if video is slave, we try to correct big delays by
1117 duplicating or deleting a frame */
1118 diff
= get_video_clock(is
) - get_master_clock(is
);
1120 /* skip or repeat frame. We take into account the
1121 delay to compute the threshold. I still don't know
1122 if it is the best guess */
1123 sync_threshold
= FFMAX(AV_SYNC_THRESHOLD
, delay
);
1124 if (fabs(diff
) < AV_NOSYNC_THRESHOLD
) {
1125 if (diff
<= -sync_threshold
)
1127 else if (diff
>= sync_threshold
)
1132 is
->frame_timer
+= delay
;
1133 /* compute the REAL delay (we need to do that to avoid
1135 actual_delay
= is
->frame_timer
- (av_gettime() / 1000000.0);
1136 if (actual_delay
< 0.010) {
1137 /* XXX: should skip picture */
1138 actual_delay
= 0.010;
1141 #if defined(DEBUG_SYNC)
1142 printf("video: delay=%0.3f actual_delay=%0.3f pts=%0.3f A-V=%f\n",
1143 delay
, actual_delay
, frame_current_pts
, -diff
);
1146 return actual_delay
;
1149 /* called to display each frame */
1150 static void video_refresh_timer(void *opaque
)
1152 VideoState
*is
= opaque
;
1155 SubPicture
*sp
, *sp2
;
1158 if (is
->pictq_size
== 0) {
1159 fprintf(stderr
, "Internal error detected in the SDL timer\n");
1161 /* dequeue the picture */
1162 vp
= &is
->pictq
[is
->pictq_rindex
];
1164 /* update current video pts */
1165 is
->video_current_pts
= vp
->pts
;
1166 is
->video_current_pts_drift
= is
->video_current_pts
- av_gettime() / 1000000.0;
1167 is
->video_current_pos
= vp
->pos
;
1169 if(is
->subtitle_st
) {
1170 if (is
->subtitle_stream_changed
) {
1171 SDL_LockMutex(is
->subpq_mutex
);
1173 while (is
->subpq_size
) {
1174 free_subpicture(&is
->subpq
[is
->subpq_rindex
]);
1176 /* update queue size and signal for next picture */
1177 if (++is
->subpq_rindex
== SUBPICTURE_QUEUE_SIZE
)
1178 is
->subpq_rindex
= 0;
1182 is
->subtitle_stream_changed
= 0;
1184 SDL_CondSignal(is
->subpq_cond
);
1185 SDL_UnlockMutex(is
->subpq_mutex
);
1187 if (is
->subpq_size
> 0) {
1188 sp
= &is
->subpq
[is
->subpq_rindex
];
1190 if (is
->subpq_size
> 1)
1191 sp2
= &is
->subpq
[(is
->subpq_rindex
+ 1) % SUBPICTURE_QUEUE_SIZE
];
1195 if ((is
->video_current_pts
> (sp
->pts
+ ((float) sp
->sub
.end_display_time
/ 1000)))
1196 || (sp2
&& is
->video_current_pts
> (sp2
->pts
+ ((float) sp2
->sub
.start_display_time
/ 1000))))
1198 free_subpicture(sp
);
1200 /* update queue size and signal for next picture */
1201 if (++is
->subpq_rindex
== SUBPICTURE_QUEUE_SIZE
)
1202 is
->subpq_rindex
= 0;
1204 SDL_LockMutex(is
->subpq_mutex
);
1206 SDL_CondSignal(is
->subpq_cond
);
1207 SDL_UnlockMutex(is
->subpq_mutex
);
1213 /* display picture */
1216 /* update queue size and signal for next picture */
1217 if (++is
->pictq_rindex
== VIDEO_PICTURE_QUEUE_SIZE
)
1218 is
->pictq_rindex
= 0;
1220 SDL_LockMutex(is
->pictq_mutex
);
1223 SDL_CondSignal(is
->pictq_cond
);
1224 SDL_UnlockMutex(is
->pictq_mutex
);
1226 } else if (is
->audio_st
) {
1227 /* draw the next audio frame */
1229 schedule_refresh(is
, 40);
1231 /* if only audio stream, then display the audio bars (better
1232 than nothing, just to test the implementation */
1234 /* display picture */
1237 schedule_refresh(is
, 100);
1240 static int64_t last_time
;
1242 int aqsize
, vqsize
, sqsize
;
1245 cur_time
= av_gettime();
1246 if (!last_time
|| (cur_time
- last_time
) >= 30000) {
1251 aqsize
= is
->audioq
.size
;
1253 vqsize
= is
->videoq
.size
;
1254 if (is
->subtitle_st
)
1255 sqsize
= is
->subtitleq
.size
;
1257 if (is
->audio_st
&& is
->video_st
)
1258 av_diff
= get_audio_clock(is
) - get_video_clock(is
);
1259 printf("%7.2f A-V:%7.3f aq=%5dKB vq=%5dKB sq=%5dB f=%Ld/%Ld \r",
1260 get_master_clock(is
), av_diff
, aqsize
/ 1024, vqsize
/ 1024, sqsize
, is
->faulty_dts
, is
->faulty_pts
);
1262 last_time
= cur_time
;
1267 /* allocate a picture (needs to do that in main thread to avoid
1268 potential locking problems */
1269 static void alloc_picture(void *opaque
)
1271 VideoState
*is
= opaque
;
1274 vp
= &is
->pictq
[is
->pictq_windex
];
1277 SDL_FreeYUVOverlay(vp
->bmp
);
1281 avfilter_unref_pic(vp
->picref
);
1284 vp
->width
= is
->out_video_filter
->inputs
[0]->w
;
1285 vp
->height
= is
->out_video_filter
->inputs
[0]->h
;
1286 vp
->pix_fmt
= is
->out_video_filter
->inputs
[0]->format
;
1288 vp
->width
= is
->video_st
->codec
->width
;
1289 vp
->height
= is
->video_st
->codec
->height
;
1290 vp
->pix_fmt
= is
->video_st
->codec
->pix_fmt
;
1293 vp
->bmp
= SDL_CreateYUVOverlay(vp
->width
, vp
->height
,
1297 SDL_LockMutex(is
->pictq_mutex
);
1299 SDL_CondSignal(is
->pictq_cond
);
1300 SDL_UnlockMutex(is
->pictq_mutex
);
1305 * @param pts the dts of the pkt / pts of the frame and guessed if not known
1307 static int queue_picture(VideoState
*is
, AVFrame
*src_frame
, double pts
, int64_t pos
)
1314 /* wait until we have space to put a new picture */
1315 SDL_LockMutex(is
->pictq_mutex
);
1316 while (is
->pictq_size
>= VIDEO_PICTURE_QUEUE_SIZE
&&
1317 !is
->videoq
.abort_request
) {
1318 SDL_CondWait(is
->pictq_cond
, is
->pictq_mutex
);
1320 SDL_UnlockMutex(is
->pictq_mutex
);
1322 if (is
->videoq
.abort_request
)
1325 vp
= &is
->pictq
[is
->pictq_windex
];
1327 /* alloc or resize hardware picture buffer */
1330 vp
->width
!= is
->out_video_filter
->inputs
[0]->w
||
1331 vp
->height
!= is
->out_video_filter
->inputs
[0]->h
) {
1333 vp
->width
!= is
->video_st
->codec
->width
||
1334 vp
->height
!= is
->video_st
->codec
->height
) {
1340 /* the allocation must be done in the main thread to avoid
1342 event
.type
= FF_ALLOC_EVENT
;
1343 event
.user
.data1
= is
;
1344 SDL_PushEvent(&event
);
1346 /* wait until the picture is allocated */
1347 SDL_LockMutex(is
->pictq_mutex
);
1348 while (!vp
->allocated
&& !is
->videoq
.abort_request
) {
1349 SDL_CondWait(is
->pictq_cond
, is
->pictq_mutex
);
1351 SDL_UnlockMutex(is
->pictq_mutex
);
1353 if (is
->videoq
.abort_request
)
1357 /* if the frame is not skipped, then display it */
1362 avfilter_unref_pic(vp
->picref
);
1363 vp
->picref
= src_frame
->opaque
;
1366 /* get a pointer on the bitmap */
1367 SDL_LockYUVOverlay (vp
->bmp
);
1369 dst_pix_fmt
= PIX_FMT_YUV420P
;
1370 memset(&pict
,0,sizeof(AVPicture
));
1371 pict
.data
[0] = vp
->bmp
->pixels
[0];
1372 pict
.data
[1] = vp
->bmp
->pixels
[2];
1373 pict
.data
[2] = vp
->bmp
->pixels
[1];
1375 pict
.linesize
[0] = vp
->bmp
->pitches
[0];
1376 pict
.linesize
[1] = vp
->bmp
->pitches
[2];
1377 pict
.linesize
[2] = vp
->bmp
->pitches
[1];
1380 pict_src
.data
[0] = src_frame
->data
[0];
1381 pict_src
.data
[1] = src_frame
->data
[1];
1382 pict_src
.data
[2] = src_frame
->data
[2];
1384 pict_src
.linesize
[0] = src_frame
->linesize
[0];
1385 pict_src
.linesize
[1] = src_frame
->linesize
[1];
1386 pict_src
.linesize
[2] = src_frame
->linesize
[2];
1388 //FIXME use direct rendering
1389 av_picture_copy(&pict
, &pict_src
,
1390 vp
->pix_fmt
, vp
->width
, vp
->height
);
1392 sws_flags
= av_get_int(sws_opts
, "sws_flags", NULL
);
1393 is
->img_convert_ctx
= sws_getCachedContext(is
->img_convert_ctx
,
1394 vp
->width
, vp
->height
, vp
->pix_fmt
, vp
->width
, vp
->height
,
1395 dst_pix_fmt
, sws_flags
, NULL
, NULL
, NULL
);
1396 if (is
->img_convert_ctx
== NULL
) {
1397 fprintf(stderr
, "Cannot initialize the conversion context\n");
1400 sws_scale(is
->img_convert_ctx
, src_frame
->data
, src_frame
->linesize
,
1401 0, vp
->height
, pict
.data
, pict
.linesize
);
1403 /* update the bitmap content */
1404 SDL_UnlockYUVOverlay(vp
->bmp
);
1409 /* now we can update the picture count */
1410 if (++is
->pictq_windex
== VIDEO_PICTURE_QUEUE_SIZE
)
1411 is
->pictq_windex
= 0;
1412 SDL_LockMutex(is
->pictq_mutex
);
1414 //We must schedule in a mutex as we must store the timer id before the timer dies or might end up freeing a alraedy freed id
1415 vp
->timer_id
= schedule_refresh(is
, (int)(compute_frame_delay(vp
->pts
, is
) * 1000 + 0.5));
1416 SDL_UnlockMutex(is
->pictq_mutex
);
1422 * compute the exact PTS for the picture if it is omitted in the stream
1423 * @param pts1 the dts of the pkt / pts of the frame
1425 static int output_picture2(VideoState
*is
, AVFrame
*src_frame
, double pts1
, int64_t pos
)
1427 double frame_delay
, pts
;
1432 /* update video clock with pts, if present */
1433 is
->video_clock
= pts
;
1435 pts
= is
->video_clock
;
1437 /* update video clock for next frame */
1438 frame_delay
= av_q2d(is
->video_st
->codec
->time_base
);
1439 /* for MPEG2, the frame can be repeated, so we update the
1440 clock accordingly */
1441 frame_delay
+= src_frame
->repeat_pict
* (frame_delay
* 0.5);
1442 is
->video_clock
+= frame_delay
;
1444 #if defined(DEBUG_SYNC) && 0
1445 printf("frame_type=%c clock=%0.3f pts=%0.3f\n",
1446 av_get_pict_type_char(src_frame
->pict_type
), pts
, pts1
);
1448 return queue_picture(is
, src_frame
, pts
, pos
);
1451 static int get_video_frame(VideoState
*is
, AVFrame
*frame
, int64_t *pts
, AVPacket
*pkt
)
1453 int len1
, got_picture
, i
;
1455 if (packet_queue_get(&is
->videoq
, pkt
, 1) < 0)
1458 if(pkt
->data
== flush_pkt
.data
){
1459 avcodec_flush_buffers(is
->video_st
->codec
);
1461 SDL_LockMutex(is
->pictq_mutex
);
1462 //Make sure there are no long delay timers (ideally we should just flush the que but thats harder)
1463 for(i
=0; i
<VIDEO_PICTURE_QUEUE_SIZE
; i
++){
1464 if(is
->pictq
[i
].timer_id
){
1465 SDL_RemoveTimer(is
->pictq
[i
].timer_id
);
1466 is
->pictq
[i
].timer_id
=0;
1467 schedule_refresh(is
, 1);
1470 while (is
->pictq_size
&& !is
->videoq
.abort_request
) {
1471 SDL_CondWait(is
->pictq_cond
, is
->pictq_mutex
);
1473 is
->video_current_pos
= -1;
1474 SDL_UnlockMutex(is
->pictq_mutex
);
1476 is
->last_dts_for_fault_detection
=
1477 is
->last_pts_for_fault_detection
= INT64_MIN
;
1478 is
->frame_last_pts
= AV_NOPTS_VALUE
;
1479 is
->frame_last_delay
= 0;
1480 is
->frame_timer
= (double)av_gettime() / 1000000.0;
1485 /* NOTE: ipts is the PTS of the _first_ picture beginning in
1486 this packet, if any */
1487 is
->video_st
->codec
->reordered_opaque
= pkt
->pts
;
1488 len1
= avcodec_decode_video2(is
->video_st
->codec
,
1489 frame
, &got_picture
,
1493 if(pkt
->dts
!= AV_NOPTS_VALUE
){
1494 is
->faulty_dts
+= pkt
->dts
<= is
->last_dts_for_fault_detection
;
1495 is
->last_dts_for_fault_detection
= pkt
->dts
;
1497 if(frame
->reordered_opaque
!= AV_NOPTS_VALUE
){
1498 is
->faulty_pts
+= frame
->reordered_opaque
<= is
->last_pts_for_fault_detection
;
1499 is
->last_pts_for_fault_detection
= frame
->reordered_opaque
;
1503 if( ( decoder_reorder_pts
==1
1504 || (decoder_reorder_pts
&& is
->faulty_pts
<is
->faulty_dts
)
1505 || pkt
->dts
== AV_NOPTS_VALUE
)
1506 && frame
->reordered_opaque
!= AV_NOPTS_VALUE
)
1507 *pts
= frame
->reordered_opaque
;
1508 else if(pkt
->dts
!= AV_NOPTS_VALUE
)
1526 static int input_init(AVFilterContext
*ctx
, const char *args
, void *opaque
)
1528 FilterPriv
*priv
= ctx
->priv
;
1529 if(!opaque
) return -1;
1532 priv
->frame
= avcodec_alloc_frame();
1537 static void input_uninit(AVFilterContext
*ctx
)
1539 FilterPriv
*priv
= ctx
->priv
;
1540 av_free(priv
->frame
);
1543 static int input_request_frame(AVFilterLink
*link
)
1545 FilterPriv
*priv
= link
->src
->priv
;
1546 AVFilterPicRef
*picref
;
1551 while (!(ret
= get_video_frame(priv
->is
, priv
->frame
, &pts
, &pkt
)))
1552 av_free_packet(&pkt
);
1556 /* FIXME: until I figure out how to hook everything up to the codec
1557 * right, we're just copying the entire frame. */
1558 picref
= avfilter_get_video_buffer(link
, AV_PERM_WRITE
, link
->w
, link
->h
);
1559 av_picture_copy((AVPicture
*)&picref
->data
, (AVPicture
*)priv
->frame
,
1560 picref
->pic
->format
, link
->w
, link
->h
);
1561 av_free_packet(&pkt
);
1564 picref
->pixel_aspect
= priv
->is
->video_st
->codec
->sample_aspect_ratio
;
1565 avfilter_start_frame(link
, avfilter_ref_pic(picref
, ~0));
1566 avfilter_draw_slice(link
, 0, link
->h
, 1);
1567 avfilter_end_frame(link
);
1568 avfilter_unref_pic(picref
);
1573 static int input_query_formats(AVFilterContext
*ctx
)
1575 FilterPriv
*priv
= ctx
->priv
;
1576 enum PixelFormat pix_fmts
[] = {
1577 priv
->is
->video_st
->codec
->pix_fmt
, PIX_FMT_NONE
1580 avfilter_set_common_formats(ctx
, avfilter_make_format_list(pix_fmts
));
1584 static int input_config_props(AVFilterLink
*link
)
1586 FilterPriv
*priv
= link
->src
->priv
;
1587 AVCodecContext
*c
= priv
->is
->video_st
->codec
;
1590 link
->h
= c
->height
;
1595 static AVFilter input_filter
=
1597 .name
= "ffplay_input",
1599 .priv_size
= sizeof(FilterPriv
),
1602 .uninit
= input_uninit
,
1604 .query_formats
= input_query_formats
,
1606 .inputs
= (AVFilterPad
[]) {{ .name
= NULL
}},
1607 .outputs
= (AVFilterPad
[]) {{ .name
= "default",
1608 .type
= CODEC_TYPE_VIDEO
,
1609 .request_frame
= input_request_frame
,
1610 .config_props
= input_config_props
, },
1614 static void output_end_frame(AVFilterLink
*link
)
1618 static int output_query_formats(AVFilterContext
*ctx
)
1620 enum PixelFormat pix_fmts
[] = { PIX_FMT_YUV420P
, PIX_FMT_NONE
};
1622 avfilter_set_common_formats(ctx
, avfilter_make_format_list(pix_fmts
));
1626 static int get_filtered_video_frame(AVFilterContext
*ctx
, AVFrame
*frame
,
1629 AVFilterPicRef
*pic
;
1631 if(avfilter_request_frame(ctx
->inputs
[0]))
1633 if(!(pic
= ctx
->inputs
[0]->cur_pic
))
1635 ctx
->inputs
[0]->cur_pic
= NULL
;
1637 frame
->opaque
= pic
;
1640 memcpy(frame
->data
, pic
->data
, sizeof(frame
->data
));
1641 memcpy(frame
->linesize
, pic
->linesize
, sizeof(frame
->linesize
));
1646 static AVFilter output_filter
=
1648 .name
= "ffplay_output",
1650 .query_formats
= output_query_formats
,
1652 .inputs
= (AVFilterPad
[]) {{ .name
= "default",
1653 .type
= CODEC_TYPE_VIDEO
,
1654 .end_frame
= output_end_frame
,
1655 .min_perms
= AV_PERM_READ
, },
1657 .outputs
= (AVFilterPad
[]) {{ .name
= NULL
}},
1659 #endif /* CONFIG_AVFILTER */
1661 static int video_thread(void *arg
)
1663 VideoState
*is
= arg
;
1664 AVFrame
*frame
= avcodec_alloc_frame();
1670 AVFilterContext
*filt_src
= NULL
, *filt_out
= NULL
;
1671 AVFilterGraph
*graph
= av_mallocz(sizeof(AVFilterGraph
));
1672 graph
->scale_sws_opts
= av_strdup("sws_flags=bilinear");
1674 if(!(filt_src
= avfilter_open(&input_filter
, "src"))) goto the_end
;
1675 if(!(filt_out
= avfilter_open(&output_filter
, "out"))) goto the_end
;
1677 if(avfilter_init_filter(filt_src
, NULL
, is
)) goto the_end
;
1678 if(avfilter_init_filter(filt_out
, NULL
, frame
)) goto the_end
;
1682 AVFilterInOut
*outputs
= av_malloc(sizeof(AVFilterInOut
));
1683 AVFilterInOut
*inputs
= av_malloc(sizeof(AVFilterInOut
));
1685 outputs
->name
= av_strdup("in");
1686 outputs
->filter
= filt_src
;
1687 outputs
->pad_idx
= 0;
1688 outputs
->next
= NULL
;
1690 inputs
->name
= av_strdup("out");
1691 inputs
->filter
= filt_out
;
1692 inputs
->pad_idx
= 0;
1693 inputs
->next
= NULL
;
1695 if (avfilter_graph_parse(graph
, vfilters
, inputs
, outputs
, NULL
) < 0)
1697 av_freep(&vfilters
);
1699 if(avfilter_link(filt_src
, 0, filt_out
, 0) < 0) goto the_end
;
1701 avfilter_graph_add_filter(graph
, filt_src
);
1702 avfilter_graph_add_filter(graph
, filt_out
);
1704 if(avfilter_graph_check_validity(graph
, NULL
)) goto the_end
;
1705 if(avfilter_graph_config_formats(graph
, NULL
)) goto the_end
;
1706 if(avfilter_graph_config_links(graph
, NULL
)) goto the_end
;
1708 is
->out_video_filter
= filt_out
;
1712 #if !CONFIG_AVFILTER
1715 while (is
->paused
&& !is
->videoq
.abort_request
)
1718 ret
= get_filtered_video_frame(filt_out
, frame
, &pts_int
);
1720 ret
= get_video_frame(is
, frame
, &pts_int
, &pkt
);
1723 if (ret
< 0) goto the_end
;
1728 pts
= pts_int
*av_q2d(is
->video_st
->time_base
);
1731 ret
= output_picture2(is
, frame
, pts
, -1); /* fixme: unknown pos */
1733 ret
= output_picture2(is
, frame
, pts
, pkt
.pos
);
1734 av_free_packet(&pkt
);
1741 stream_pause(cur_stream
);
1745 avfilter_graph_destroy(graph
);
1752 static int subtitle_thread(void *arg
)
1754 VideoState
*is
= arg
;
1756 AVPacket pkt1
, *pkt
= &pkt1
;
1757 int len1
, got_subtitle
;
1760 int r
, g
, b
, y
, u
, v
, a
;
1763 while (is
->paused
&& !is
->subtitleq
.abort_request
) {
1766 if (packet_queue_get(&is
->subtitleq
, pkt
, 1) < 0)
1769 if(pkt
->data
== flush_pkt
.data
){
1770 avcodec_flush_buffers(is
->subtitle_st
->codec
);
1773 SDL_LockMutex(is
->subpq_mutex
);
1774 while (is
->subpq_size
>= SUBPICTURE_QUEUE_SIZE
&&
1775 !is
->subtitleq
.abort_request
) {
1776 SDL_CondWait(is
->subpq_cond
, is
->subpq_mutex
);
1778 SDL_UnlockMutex(is
->subpq_mutex
);
1780 if (is
->subtitleq
.abort_request
)
1783 sp
= &is
->subpq
[is
->subpq_windex
];
1785 /* NOTE: ipts is the PTS of the _first_ picture beginning in
1786 this packet, if any */
1788 if (pkt
->pts
!= AV_NOPTS_VALUE
)
1789 pts
= av_q2d(is
->subtitle_st
->time_base
)*pkt
->pts
;
1791 len1
= avcodec_decode_subtitle2(is
->subtitle_st
->codec
,
1792 &sp
->sub
, &got_subtitle
,
1796 if (got_subtitle
&& sp
->sub
.format
== 0) {
1799 for (i
= 0; i
< sp
->sub
.num_rects
; i
++)
1801 for (j
= 0; j
< sp
->sub
.rects
[i
]->nb_colors
; j
++)
1803 RGBA_IN(r
, g
, b
, a
, (uint32_t*)sp
->sub
.rects
[i
]->pict
.data
[1] + j
);
1804 y
= RGB_TO_Y_CCIR(r
, g
, b
);
1805 u
= RGB_TO_U_CCIR(r
, g
, b
, 0);
1806 v
= RGB_TO_V_CCIR(r
, g
, b
, 0);
1807 YUVA_OUT((uint32_t*)sp
->sub
.rects
[i
]->pict
.data
[1] + j
, y
, u
, v
, a
);
1811 /* now we can update the picture count */
1812 if (++is
->subpq_windex
== SUBPICTURE_QUEUE_SIZE
)
1813 is
->subpq_windex
= 0;
1814 SDL_LockMutex(is
->subpq_mutex
);
1816 SDL_UnlockMutex(is
->subpq_mutex
);
1818 av_free_packet(pkt
);
1821 // stream_pause(cur_stream);
1827 /* copy samples for viewing in editor window */
1828 static void update_sample_display(VideoState
*is
, short *samples
, int samples_size
)
1830 int size
, len
, channels
;
1832 channels
= is
->audio_st
->codec
->channels
;
1834 size
= samples_size
/ sizeof(short);
1836 len
= SAMPLE_ARRAY_SIZE
- is
->sample_array_index
;
1839 memcpy(is
->sample_array
+ is
->sample_array_index
, samples
, len
* sizeof(short));
1841 is
->sample_array_index
+= len
;
1842 if (is
->sample_array_index
>= SAMPLE_ARRAY_SIZE
)
1843 is
->sample_array_index
= 0;
1848 /* return the new audio buffer size (samples can be added or deleted
1849 to get better sync if video or external master clock) */
1850 static int synchronize_audio(VideoState
*is
, short *samples
,
1851 int samples_size1
, double pts
)
1853 int n
, samples_size
;
1856 n
= 2 * is
->audio_st
->codec
->channels
;
1857 samples_size
= samples_size1
;
1859 /* if not master, then we try to remove or add samples to correct the clock */
1860 if (((is
->av_sync_type
== AV_SYNC_VIDEO_MASTER
&& is
->video_st
) ||
1861 is
->av_sync_type
== AV_SYNC_EXTERNAL_CLOCK
)) {
1862 double diff
, avg_diff
;
1863 int wanted_size
, min_size
, max_size
, nb_samples
;
1865 ref_clock
= get_master_clock(is
);
1866 diff
= get_audio_clock(is
) - ref_clock
;
1868 if (diff
< AV_NOSYNC_THRESHOLD
) {
1869 is
->audio_diff_cum
= diff
+ is
->audio_diff_avg_coef
* is
->audio_diff_cum
;
1870 if (is
->audio_diff_avg_count
< AUDIO_DIFF_AVG_NB
) {
1871 /* not enough measures to have a correct estimate */
1872 is
->audio_diff_avg_count
++;
1874 /* estimate the A-V difference */
1875 avg_diff
= is
->audio_diff_cum
* (1.0 - is
->audio_diff_avg_coef
);
1877 if (fabs(avg_diff
) >= is
->audio_diff_threshold
) {
1878 wanted_size
= samples_size
+ ((int)(diff
* is
->audio_st
->codec
->sample_rate
) * n
);
1879 nb_samples
= samples_size
/ n
;
1881 min_size
= ((nb_samples
* (100 - SAMPLE_CORRECTION_PERCENT_MAX
)) / 100) * n
;
1882 max_size
= ((nb_samples
* (100 + SAMPLE_CORRECTION_PERCENT_MAX
)) / 100) * n
;
1883 if (wanted_size
< min_size
)
1884 wanted_size
= min_size
;
1885 else if (wanted_size
> max_size
)
1886 wanted_size
= max_size
;
1888 /* add or remove samples to correction the synchro */
1889 if (wanted_size
< samples_size
) {
1890 /* remove samples */
1891 samples_size
= wanted_size
;
1892 } else if (wanted_size
> samples_size
) {
1893 uint8_t *samples_end
, *q
;
1897 nb
= (samples_size
- wanted_size
);
1898 samples_end
= (uint8_t *)samples
+ samples_size
- n
;
1899 q
= samples_end
+ n
;
1901 memcpy(q
, samples_end
, n
);
1905 samples_size
= wanted_size
;
1909 printf("diff=%f adiff=%f sample_diff=%d apts=%0.3f vpts=%0.3f %f\n",
1910 diff
, avg_diff
, samples_size
- samples_size1
,
1911 is
->audio_clock
, is
->video_clock
, is
->audio_diff_threshold
);
1915 /* too big difference : may be initial PTS errors, so
1917 is
->audio_diff_avg_count
= 0;
1918 is
->audio_diff_cum
= 0;
1922 return samples_size
;
1925 /* decode one audio frame and returns its uncompressed size */
1926 static int audio_decode_frame(VideoState
*is
, double *pts_ptr
)
1928 AVPacket
*pkt_temp
= &is
->audio_pkt_temp
;
1929 AVPacket
*pkt
= &is
->audio_pkt
;
1930 AVCodecContext
*dec
= is
->audio_st
->codec
;
1931 int n
, len1
, data_size
;
1935 /* NOTE: the audio packet can contain several frames */
1936 while (pkt_temp
->size
> 0) {
1937 data_size
= sizeof(is
->audio_buf1
);
1938 len1
= avcodec_decode_audio3(dec
,
1939 (int16_t *)is
->audio_buf1
, &data_size
,
1942 /* if error, we skip the frame */
1947 pkt_temp
->data
+= len1
;
1948 pkt_temp
->size
-= len1
;
1952 if (dec
->sample_fmt
!= is
->audio_src_fmt
) {
1953 if (is
->reformat_ctx
)
1954 av_audio_convert_free(is
->reformat_ctx
);
1955 is
->reformat_ctx
= av_audio_convert_alloc(SAMPLE_FMT_S16
, 1,
1956 dec
->sample_fmt
, 1, NULL
, 0);
1957 if (!is
->reformat_ctx
) {
1958 fprintf(stderr
, "Cannot convert %s sample format to %s sample format\n",
1959 avcodec_get_sample_fmt_name(dec
->sample_fmt
),
1960 avcodec_get_sample_fmt_name(SAMPLE_FMT_S16
));
1963 is
->audio_src_fmt
= dec
->sample_fmt
;
1966 if (is
->reformat_ctx
) {
1967 const void *ibuf
[6]= {is
->audio_buf1
};
1968 void *obuf
[6]= {is
->audio_buf2
};
1969 int istride
[6]= {av_get_bits_per_sample_format(dec
->sample_fmt
)/8};
1970 int ostride
[6]= {2};
1971 int len
= data_size
/istride
[0];
1972 if (av_audio_convert(is
->reformat_ctx
, obuf
, ostride
, ibuf
, istride
, len
)<0) {
1973 printf("av_audio_convert() failed\n");
1976 is
->audio_buf
= is
->audio_buf2
;
1977 /* FIXME: existing code assume that data_size equals framesize*channels*2
1978 remove this legacy cruft */
1981 is
->audio_buf
= is
->audio_buf1
;
1984 /* if no pts, then compute it */
1985 pts
= is
->audio_clock
;
1987 n
= 2 * dec
->channels
;
1988 is
->audio_clock
+= (double)data_size
/
1989 (double)(n
* dec
->sample_rate
);
1990 #if defined(DEBUG_SYNC)
1992 static double last_clock
;
1993 printf("audio: delay=%0.3f clock=%0.3f pts=%0.3f\n",
1994 is
->audio_clock
- last_clock
,
1995 is
->audio_clock
, pts
);
1996 last_clock
= is
->audio_clock
;
2002 /* free the current packet */
2004 av_free_packet(pkt
);
2006 if (is
->paused
|| is
->audioq
.abort_request
) {
2010 /* read next packet */
2011 if (packet_queue_get(&is
->audioq
, pkt
, 1) < 0)
2013 if(pkt
->data
== flush_pkt
.data
){
2014 avcodec_flush_buffers(dec
);
2018 pkt_temp
->data
= pkt
->data
;
2019 pkt_temp
->size
= pkt
->size
;
2021 /* if update the audio clock with the pts */
2022 if (pkt
->pts
!= AV_NOPTS_VALUE
) {
2023 is
->audio_clock
= av_q2d(is
->audio_st
->time_base
)*pkt
->pts
;
2028 /* get the current audio output buffer size, in samples. With SDL, we
2029 cannot have a precise information */
2030 static int audio_write_get_buf_size(VideoState
*is
)
2032 return is
->audio_buf_size
- is
->audio_buf_index
;
2036 /* prepare a new audio buffer */
2037 static void sdl_audio_callback(void *opaque
, Uint8
*stream
, int len
)
2039 VideoState
*is
= opaque
;
2040 int audio_size
, len1
;
2043 audio_callback_time
= av_gettime();
2046 if (is
->audio_buf_index
>= is
->audio_buf_size
) {
2047 audio_size
= audio_decode_frame(is
, &pts
);
2048 if (audio_size
< 0) {
2049 /* if error, just output silence */
2050 is
->audio_buf
= is
->audio_buf1
;
2051 is
->audio_buf_size
= 1024;
2052 memset(is
->audio_buf
, 0, is
->audio_buf_size
);
2055 update_sample_display(is
, (int16_t *)is
->audio_buf
, audio_size
);
2056 audio_size
= synchronize_audio(is
, (int16_t *)is
->audio_buf
, audio_size
,
2058 is
->audio_buf_size
= audio_size
;
2060 is
->audio_buf_index
= 0;
2062 len1
= is
->audio_buf_size
- is
->audio_buf_index
;
2065 memcpy(stream
, (uint8_t *)is
->audio_buf
+ is
->audio_buf_index
, len1
);
2068 is
->audio_buf_index
+= len1
;
2072 /* open a given stream. Return 0 if OK */
2073 static int stream_component_open(VideoState
*is
, int stream_index
)
2075 AVFormatContext
*ic
= is
->ic
;
2076 AVCodecContext
*avctx
;
2078 SDL_AudioSpec wanted_spec
, spec
;
2080 if (stream_index
< 0 || stream_index
>= ic
->nb_streams
)
2082 avctx
= ic
->streams
[stream_index
]->codec
;
2084 /* prepare audio output */
2085 if (avctx
->codec_type
== CODEC_TYPE_AUDIO
) {
2086 if (avctx
->channels
> 0) {
2087 avctx
->request_channels
= FFMIN(2, avctx
->channels
);
2089 avctx
->request_channels
= 2;
2093 codec
= avcodec_find_decoder(avctx
->codec_id
);
2094 avctx
->debug_mv
= debug_mv
;
2095 avctx
->debug
= debug
;
2096 avctx
->workaround_bugs
= workaround_bugs
;
2097 avctx
->lowres
= lowres
;
2098 if(lowres
) avctx
->flags
|= CODEC_FLAG_EMU_EDGE
;
2099 avctx
->idct_algo
= idct
;
2100 if(fast
) avctx
->flags2
|= CODEC_FLAG2_FAST
;
2101 avctx
->skip_frame
= skip_frame
;
2102 avctx
->skip_idct
= skip_idct
;
2103 avctx
->skip_loop_filter
= skip_loop_filter
;
2104 avctx
->error_recognition
= error_recognition
;
2105 avctx
->error_concealment
= error_concealment
;
2106 avcodec_thread_init(avctx
, thread_count
);
2108 set_context_opts(avctx
, avcodec_opts
[avctx
->codec_type
], 0);
2111 avcodec_open(avctx
, codec
) < 0)
2114 /* prepare audio output */
2115 if (avctx
->codec_type
== CODEC_TYPE_AUDIO
) {
2116 wanted_spec
.freq
= avctx
->sample_rate
;
2117 wanted_spec
.format
= AUDIO_S16SYS
;
2118 wanted_spec
.channels
= avctx
->channels
;
2119 wanted_spec
.silence
= 0;
2120 wanted_spec
.samples
= SDL_AUDIO_BUFFER_SIZE
;
2121 wanted_spec
.callback
= sdl_audio_callback
;
2122 wanted_spec
.userdata
= is
;
2123 if (SDL_OpenAudio(&wanted_spec
, &spec
) < 0) {
2124 fprintf(stderr
, "SDL_OpenAudio: %s\n", SDL_GetError());
2127 is
->audio_hw_buf_size
= spec
.size
;
2128 is
->audio_src_fmt
= SAMPLE_FMT_S16
;
2131 ic
->streams
[stream_index
]->discard
= AVDISCARD_DEFAULT
;
2132 switch(avctx
->codec_type
) {
2133 case CODEC_TYPE_AUDIO
:
2134 is
->audio_stream
= stream_index
;
2135 is
->audio_st
= ic
->streams
[stream_index
];
2136 is
->audio_buf_size
= 0;
2137 is
->audio_buf_index
= 0;
2139 /* init averaging filter */
2140 is
->audio_diff_avg_coef
= exp(log(0.01) / AUDIO_DIFF_AVG_NB
);
2141 is
->audio_diff_avg_count
= 0;
2142 /* since we do not have a precise anough audio fifo fullness,
2143 we correct audio sync only if larger than this threshold */
2144 is
->audio_diff_threshold
= 2.0 * SDL_AUDIO_BUFFER_SIZE
/ avctx
->sample_rate
;
2146 memset(&is
->audio_pkt
, 0, sizeof(is
->audio_pkt
));
2147 packet_queue_init(&is
->audioq
);
2150 case CODEC_TYPE_VIDEO
:
2151 is
->video_stream
= stream_index
;
2152 is
->video_st
= ic
->streams
[stream_index
];
2154 // is->video_current_pts_time = av_gettime();
2156 packet_queue_init(&is
->videoq
);
2157 is
->video_tid
= SDL_CreateThread(video_thread
, is
);
2159 case CODEC_TYPE_SUBTITLE
:
2160 is
->subtitle_stream
= stream_index
;
2161 is
->subtitle_st
= ic
->streams
[stream_index
];
2162 packet_queue_init(&is
->subtitleq
);
2164 is
->subtitle_tid
= SDL_CreateThread(subtitle_thread
, is
);
2172 static void stream_component_close(VideoState
*is
, int stream_index
)
2174 AVFormatContext
*ic
= is
->ic
;
2175 AVCodecContext
*avctx
;
2177 if (stream_index
< 0 || stream_index
>= ic
->nb_streams
)
2179 avctx
= ic
->streams
[stream_index
]->codec
;
2181 switch(avctx
->codec_type
) {
2182 case CODEC_TYPE_AUDIO
:
2183 packet_queue_abort(&is
->audioq
);
2187 packet_queue_end(&is
->audioq
);
2188 if (is
->reformat_ctx
)
2189 av_audio_convert_free(is
->reformat_ctx
);
2190 is
->reformat_ctx
= NULL
;
2192 case CODEC_TYPE_VIDEO
:
2193 packet_queue_abort(&is
->videoq
);
2195 /* note: we also signal this mutex to make sure we deblock the
2196 video thread in all cases */
2197 SDL_LockMutex(is
->pictq_mutex
);
2198 SDL_CondSignal(is
->pictq_cond
);
2199 SDL_UnlockMutex(is
->pictq_mutex
);
2201 SDL_WaitThread(is
->video_tid
, NULL
);
2203 packet_queue_end(&is
->videoq
);
2205 case CODEC_TYPE_SUBTITLE
:
2206 packet_queue_abort(&is
->subtitleq
);
2208 /* note: we also signal this mutex to make sure we deblock the
2209 video thread in all cases */
2210 SDL_LockMutex(is
->subpq_mutex
);
2211 is
->subtitle_stream_changed
= 1;
2213 SDL_CondSignal(is
->subpq_cond
);
2214 SDL_UnlockMutex(is
->subpq_mutex
);
2216 SDL_WaitThread(is
->subtitle_tid
, NULL
);
2218 packet_queue_end(&is
->subtitleq
);
2224 ic
->streams
[stream_index
]->discard
= AVDISCARD_ALL
;
2225 avcodec_close(avctx
);
2226 switch(avctx
->codec_type
) {
2227 case CODEC_TYPE_AUDIO
:
2228 is
->audio_st
= NULL
;
2229 is
->audio_stream
= -1;
2231 case CODEC_TYPE_VIDEO
:
2232 is
->video_st
= NULL
;
2233 is
->video_stream
= -1;
2235 case CODEC_TYPE_SUBTITLE
:
2236 is
->subtitle_st
= NULL
;
2237 is
->subtitle_stream
= -1;
2244 /* since we have only one decoding thread, we can use a global
2245 variable instead of a thread local variable */
2246 static VideoState
*global_video_state
;
2248 static int decode_interrupt_cb(void)
2250 return (global_video_state
&& global_video_state
->abort_request
);
2253 /* this thread gets the stream from the disk or the network */
2254 static int decode_thread(void *arg
)
2256 VideoState
*is
= arg
;
2257 AVFormatContext
*ic
;
2259 int st_index
[CODEC_TYPE_NB
];
2260 int st_count
[CODEC_TYPE_NB
]={0};
2261 int st_best_packet_count
[CODEC_TYPE_NB
];
2262 AVPacket pkt1
, *pkt
= &pkt1
;
2263 AVFormatParameters params
, *ap
= ¶ms
;
2266 ic
= avformat_alloc_context();
2268 memset(st_index
, -1, sizeof(st_index
));
2269 memset(st_best_packet_count
, -1, sizeof(st_best_packet_count
));
2270 is
->video_stream
= -1;
2271 is
->audio_stream
= -1;
2272 is
->subtitle_stream
= -1;
2274 global_video_state
= is
;
2275 url_set_interrupt_cb(decode_interrupt_cb
);
2277 memset(ap
, 0, sizeof(*ap
));
2279 ap
->prealloced_context
= 1;
2280 ap
->width
= frame_width
;
2281 ap
->height
= frame_height
;
2282 ap
->time_base
= (AVRational
){1, 25};
2283 ap
->pix_fmt
= frame_pix_fmt
;
2285 set_context_opts(ic
, avformat_opts
, AV_OPT_FLAG_DECODING_PARAM
);
2287 err
= av_open_input_file(&ic
, is
->filename
, is
->iformat
, 0, ap
);
2289 print_error(is
->filename
, err
);
2296 ic
->flags
|= AVFMT_FLAG_GENPTS
;
2298 err
= av_find_stream_info(ic
);
2300 fprintf(stderr
, "%s: could not find codec parameters\n", is
->filename
);
2305 ic
->pb
->eof_reached
= 0; //FIXME hack, ffplay maybe should not use url_feof() to test for the end
2308 seek_by_bytes
= !!(ic
->iformat
->flags
& AVFMT_TS_DISCONT
);
2310 /* if seeking requested, we execute it */
2311 if (start_time
!= AV_NOPTS_VALUE
) {
2314 timestamp
= start_time
;
2315 /* add the stream start time */
2316 if (ic
->start_time
!= AV_NOPTS_VALUE
)
2317 timestamp
+= ic
->start_time
;
2318 ret
= avformat_seek_file(ic
, -1, INT64_MIN
, timestamp
, INT64_MAX
, 0);
2320 fprintf(stderr
, "%s: could not seek to position %0.3f\n",
2321 is
->filename
, (double)timestamp
/ AV_TIME_BASE
);
2325 for(i
= 0; i
< ic
->nb_streams
; i
++) {
2326 AVStream
*st
= ic
->streams
[i
];
2327 AVCodecContext
*avctx
= st
->codec
;
2328 ic
->streams
[i
]->discard
= AVDISCARD_ALL
;
2329 if(avctx
->codec_type
>= (unsigned)CODEC_TYPE_NB
)
2331 if(st_count
[avctx
->codec_type
]++ != wanted_stream
[avctx
->codec_type
] && wanted_stream
[avctx
->codec_type
] >= 0)
2334 if(st_best_packet_count
[avctx
->codec_type
] >= st
->codec_info_nb_frames
)
2336 st_best_packet_count
[avctx
->codec_type
]= st
->codec_info_nb_frames
;
2338 switch(avctx
->codec_type
) {
2339 case CODEC_TYPE_AUDIO
:
2341 st_index
[CODEC_TYPE_AUDIO
] = i
;
2343 case CODEC_TYPE_VIDEO
:
2344 case CODEC_TYPE_SUBTITLE
:
2346 st_index
[avctx
->codec_type
] = i
;
2353 dump_format(ic
, 0, is
->filename
, 0);
2356 /* open the streams */
2357 if (st_index
[CODEC_TYPE_AUDIO
] >= 0) {
2358 stream_component_open(is
, st_index
[CODEC_TYPE_AUDIO
]);
2362 if (st_index
[CODEC_TYPE_VIDEO
] >= 0) {
2363 ret
= stream_component_open(is
, st_index
[CODEC_TYPE_VIDEO
]);
2366 /* add the refresh timer to draw the picture */
2367 schedule_refresh(is
, 40);
2369 if (!display_disable
)
2373 if (st_index
[CODEC_TYPE_SUBTITLE
] >= 0) {
2374 stream_component_open(is
, st_index
[CODEC_TYPE_SUBTITLE
]);
2377 if (is
->video_stream
< 0 && is
->audio_stream
< 0) {
2378 fprintf(stderr
, "%s: could not open codecs\n", is
->filename
);
2384 if (is
->abort_request
)
2386 if (is
->paused
!= is
->last_paused
) {
2387 is
->last_paused
= is
->paused
;
2389 is
->read_pause_return
= av_read_pause(ic
);
2393 #if CONFIG_RTSP_DEMUXER
2394 if (is
->paused
&& !strcmp(ic
->iformat
->name
, "rtsp")) {
2395 /* wait 10 ms to avoid trying to get another packet */
2402 int64_t seek_target
= is
->seek_pos
;
2403 int64_t seek_min
= is
->seek_rel
> 0 ? seek_target
- is
->seek_rel
+ 2: INT64_MIN
;
2404 int64_t seek_max
= is
->seek_rel
< 0 ? seek_target
- is
->seek_rel
- 2: INT64_MAX
;
2405 //FIXME the +-2 is due to rounding being not done in the correct direction in generation
2406 // of the seek_pos/seek_rel variables
2408 ret
= avformat_seek_file(is
->ic
, -1, seek_min
, seek_target
, seek_max
, is
->seek_flags
);
2410 fprintf(stderr
, "%s: error while seeking\n", is
->ic
->filename
);
2412 if (is
->audio_stream
>= 0) {
2413 packet_queue_flush(&is
->audioq
);
2414 packet_queue_put(&is
->audioq
, &flush_pkt
);
2416 if (is
->subtitle_stream
>= 0) {
2417 packet_queue_flush(&is
->subtitleq
);
2418 packet_queue_put(&is
->subtitleq
, &flush_pkt
);
2420 if (is
->video_stream
>= 0) {
2421 packet_queue_flush(&is
->videoq
);
2422 packet_queue_put(&is
->videoq
, &flush_pkt
);
2429 /* if the queue are full, no need to read more */
2430 if ( is
->audioq
.size
+ is
->videoq
.size
+ is
->subtitleq
.size
> MAX_QUEUE_SIZE
2431 || ( (is
->audioq
.size
> MIN_AUDIOQ_SIZE
|| is
->audio_stream
<0)
2432 && (is
->videoq
.nb_packets
> MIN_FRAMES
|| is
->video_stream
<0)
2433 && (is
->subtitleq
.nb_packets
> MIN_FRAMES
|| is
->subtitle_stream
<0))) {
2438 if(url_feof(ic
->pb
) || eof
) {
2439 if(is
->video_stream
>= 0){
2440 av_init_packet(pkt
);
2443 pkt
->stream_index
= is
->video_stream
;
2444 packet_queue_put(&is
->videoq
, pkt
);
2447 if(autoexit
&& is
->audioq
.size
+ is
->videoq
.size
+ is
->subtitleq
.size
==0){
2453 ret
= av_read_frame(ic
, pkt
);
2455 if (ret
== AVERROR_EOF
)
2457 if (url_ferror(ic
->pb
))
2459 SDL_Delay(100); /* wait for user event */
2462 if (pkt
->stream_index
== is
->audio_stream
) {
2463 packet_queue_put(&is
->audioq
, pkt
);
2464 } else if (pkt
->stream_index
== is
->video_stream
) {
2465 packet_queue_put(&is
->videoq
, pkt
);
2466 } else if (pkt
->stream_index
== is
->subtitle_stream
) {
2467 packet_queue_put(&is
->subtitleq
, pkt
);
2469 av_free_packet(pkt
);
2472 /* wait until the end */
2473 while (!is
->abort_request
) {
2479 /* disable interrupting */
2480 global_video_state
= NULL
;
2482 /* close each stream */
2483 if (is
->audio_stream
>= 0)
2484 stream_component_close(is
, is
->audio_stream
);
2485 if (is
->video_stream
>= 0)
2486 stream_component_close(is
, is
->video_stream
);
2487 if (is
->subtitle_stream
>= 0)
2488 stream_component_close(is
, is
->subtitle_stream
);
2490 av_close_input_file(is
->ic
);
2491 is
->ic
= NULL
; /* safety */
2493 url_set_interrupt_cb(NULL
);
2498 event
.type
= FF_QUIT_EVENT
;
2499 event
.user
.data1
= is
;
2500 SDL_PushEvent(&event
);
2505 static VideoState
*stream_open(const char *filename
, AVInputFormat
*iformat
)
2509 is
= av_mallocz(sizeof(VideoState
));
2512 av_strlcpy(is
->filename
, filename
, sizeof(is
->filename
));
2513 is
->iformat
= iformat
;
2517 /* start video display */
2518 is
->pictq_mutex
= SDL_CreateMutex();
2519 is
->pictq_cond
= SDL_CreateCond();
2521 is
->subpq_mutex
= SDL_CreateMutex();
2522 is
->subpq_cond
= SDL_CreateCond();
2524 is
->av_sync_type
= av_sync_type
;
2525 is
->parse_tid
= SDL_CreateThread(decode_thread
, is
);
2526 if (!is
->parse_tid
) {
2533 static void stream_close(VideoState
*is
)
2537 /* XXX: use a special url_shutdown call to abort parse cleanly */
2538 is
->abort_request
= 1;
2539 SDL_WaitThread(is
->parse_tid
, NULL
);
2541 /* free all pictures */
2542 for(i
=0;i
<VIDEO_PICTURE_QUEUE_SIZE
; i
++) {
2546 avfilter_unref_pic(vp
->picref
);
2551 SDL_FreeYUVOverlay(vp
->bmp
);
2555 SDL_DestroyMutex(is
->pictq_mutex
);
2556 SDL_DestroyCond(is
->pictq_cond
);
2557 SDL_DestroyMutex(is
->subpq_mutex
);
2558 SDL_DestroyCond(is
->subpq_cond
);
2559 #if !CONFIG_AVFILTER
2560 if (is
->img_convert_ctx
)
2561 sws_freeContext(is
->img_convert_ctx
);
2566 static void stream_cycle_channel(VideoState
*is
, int codec_type
)
2568 AVFormatContext
*ic
= is
->ic
;
2569 int start_index
, stream_index
;
2572 if (codec_type
== CODEC_TYPE_VIDEO
)
2573 start_index
= is
->video_stream
;
2574 else if (codec_type
== CODEC_TYPE_AUDIO
)
2575 start_index
= is
->audio_stream
;
2577 start_index
= is
->subtitle_stream
;
2578 if (start_index
< (codec_type
== CODEC_TYPE_SUBTITLE ?
-1 : 0))
2580 stream_index
= start_index
;
2582 if (++stream_index
>= is
->ic
->nb_streams
)
2584 if (codec_type
== CODEC_TYPE_SUBTITLE
)
2591 if (stream_index
== start_index
)
2593 st
= ic
->streams
[stream_index
];
2594 if (st
->codec
->codec_type
== codec_type
) {
2595 /* check that parameters are OK */
2596 switch(codec_type
) {
2597 case CODEC_TYPE_AUDIO
:
2598 if (st
->codec
->sample_rate
!= 0 &&
2599 st
->codec
->channels
!= 0)
2602 case CODEC_TYPE_VIDEO
:
2603 case CODEC_TYPE_SUBTITLE
:
2611 stream_component_close(is
, start_index
);
2612 stream_component_open(is
, stream_index
);
2616 static void toggle_full_screen(void)
2618 is_full_screen
= !is_full_screen
;
2619 if (!fs_screen_width
) {
2620 /* use default SDL method */
2621 // SDL_WM_ToggleFullScreen(screen);
2623 video_open(cur_stream
);
2626 static void toggle_pause(void)
2629 stream_pause(cur_stream
);
2633 static void step_to_next_frame(void)
2636 /* if the stream is paused unpause it, then step */
2637 if (cur_stream
->paused
)
2638 stream_pause(cur_stream
);
2643 static void do_exit(void)
2647 stream_close(cur_stream
);
2650 for (i
= 0; i
< CODEC_TYPE_NB
; i
++)
2651 av_free(avcodec_opts
[i
]);
2652 av_free(avformat_opts
);
2663 static void toggle_audio_display(void)
2666 int bgcolor
= SDL_MapRGB(screen
->format
, 0x00, 0x00, 0x00);
2667 cur_stream
->show_audio
= (cur_stream
->show_audio
+ 1) % 3;
2668 fill_rectangle(screen
,
2669 cur_stream
->xleft
, cur_stream
->ytop
, cur_stream
->width
, cur_stream
->height
,
2671 SDL_UpdateRect(screen
, cur_stream
->xleft
, cur_stream
->ytop
, cur_stream
->width
, cur_stream
->height
);
2675 /* handle an event sent by the GUI */
2676 static void event_loop(void)
2679 double incr
, pos
, frac
;
2683 SDL_WaitEvent(&event
);
2684 switch(event
.type
) {
2686 switch(event
.key
.keysym
.sym
) {
2692 toggle_full_screen();
2698 case SDLK_s
: //S: Step to next frame
2699 step_to_next_frame();
2703 stream_cycle_channel(cur_stream
, CODEC_TYPE_AUDIO
);
2707 stream_cycle_channel(cur_stream
, CODEC_TYPE_VIDEO
);
2711 stream_cycle_channel(cur_stream
, CODEC_TYPE_SUBTITLE
);
2714 toggle_audio_display();
2729 if (seek_by_bytes
) {
2730 if (cur_stream
->video_stream
>= 0 && cur_stream
->video_current_pos
>=0){
2731 pos
= cur_stream
->video_current_pos
;
2732 }else if(cur_stream
->audio_stream
>= 0 && cur_stream
->audio_pkt
.pos
>=0){
2733 pos
= cur_stream
->audio_pkt
.pos
;
2735 pos
= url_ftell(cur_stream
->ic
->pb
);
2736 if (cur_stream
->ic
->bit_rate
)
2737 incr
*= cur_stream
->ic
->bit_rate
/ 8.0;
2741 stream_seek(cur_stream
, pos
, incr
, 1);
2743 pos
= get_master_clock(cur_stream
);
2745 stream_seek(cur_stream
, (int64_t)(pos
* AV_TIME_BASE
), (int64_t)(incr
* AV_TIME_BASE
), 0);
2753 case SDL_MOUSEBUTTONDOWN
:
2754 case SDL_MOUSEMOTION
:
2755 if(event
.type
==SDL_MOUSEBUTTONDOWN
){
2758 if(event
.motion
.state
!= SDL_PRESSED
)
2763 if(seek_by_bytes
|| cur_stream
->ic
->duration
<=0){
2764 uint64_t size
= url_fsize(cur_stream
->ic
->pb
);
2765 stream_seek(cur_stream
, size
*x
/cur_stream
->width
, 0, 1);
2769 int tns
, thh
, tmm
, tss
;
2770 tns
= cur_stream
->ic
->duration
/1000000LL;
2772 tmm
= (tns
%3600)/60;
2774 frac
= x
/cur_stream
->width
;
2779 fprintf(stderr
, "Seek to %2.0f%% (%2d:%02d:%02d) of total duration (%2d:%02d:%02d) \n", frac
*100,
2780 hh
, mm
, ss
, thh
, tmm
, tss
);
2781 ts
= frac
*cur_stream
->ic
->duration
;
2782 if (cur_stream
->ic
->start_time
!= AV_NOPTS_VALUE
)
2783 ts
+= cur_stream
->ic
->start_time
;
2784 stream_seek(cur_stream
, ts
, 0, 0);
2788 case SDL_VIDEORESIZE
:
2790 screen
= SDL_SetVideoMode(event
.resize
.w
, event
.resize
.h
, 0,
2791 SDL_HWSURFACE
|SDL_RESIZABLE
|SDL_ASYNCBLIT
|SDL_HWACCEL
);
2792 screen_width
= cur_stream
->width
= event
.resize
.w
;
2793 screen_height
= cur_stream
->height
= event
.resize
.h
;
2800 case FF_ALLOC_EVENT
:
2801 video_open(event
.user
.data1
);
2802 alloc_picture(event
.user
.data1
);
2804 case FF_REFRESH_EVENT
:
2805 video_refresh_timer(event
.user
.data1
);
2813 static void opt_frame_size(const char *arg
)
2815 if (av_parse_video_frame_size(&frame_width
, &frame_height
, arg
) < 0) {
2816 fprintf(stderr
, "Incorrect frame size\n");
2819 if ((frame_width
% 2) != 0 || (frame_height
% 2) != 0) {
2820 fprintf(stderr
, "Frame size must be a multiple of 2\n");
2825 static int opt_width(const char *opt
, const char *arg
)
2827 screen_width
= parse_number_or_die(opt
, arg
, OPT_INT64
, 1, INT_MAX
);
2831 static int opt_height(const char *opt
, const char *arg
)
2833 screen_height
= parse_number_or_die(opt
, arg
, OPT_INT64
, 1, INT_MAX
);
2837 static void opt_format(const char *arg
)
2839 file_iformat
= av_find_input_format(arg
);
2840 if (!file_iformat
) {
2841 fprintf(stderr
, "Unknown input format: %s\n", arg
);
2846 static void opt_frame_pix_fmt(const char *arg
)
2848 frame_pix_fmt
= av_get_pix_fmt(arg
);
2851 static int opt_sync(const char *opt
, const char *arg
)
2853 if (!strcmp(arg
, "audio"))
2854 av_sync_type
= AV_SYNC_AUDIO_MASTER
;
2855 else if (!strcmp(arg
, "video"))
2856 av_sync_type
= AV_SYNC_VIDEO_MASTER
;
2857 else if (!strcmp(arg
, "ext"))
2858 av_sync_type
= AV_SYNC_EXTERNAL_CLOCK
;
2860 fprintf(stderr
, "Unknown value for %s: %s\n", opt
, arg
);
2866 static int opt_seek(const char *opt
, const char *arg
)
2868 start_time
= parse_time_or_die(opt
, arg
, 1);
2872 static int opt_debug(const char *opt
, const char *arg
)
2874 av_log_set_level(99);
2875 debug
= parse_number_or_die(opt
, arg
, OPT_INT64
, 0, INT_MAX
);
2879 static int opt_vismv(const char *opt
, const char *arg
)
2881 debug_mv
= parse_number_or_die(opt
, arg
, OPT_INT64
, INT_MIN
, INT_MAX
);
2885 static int opt_thread_count(const char *opt
, const char *arg
)
2887 thread_count
= parse_number_or_die(opt
, arg
, OPT_INT64
, 0, INT_MAX
);
2889 fprintf(stderr
, "Warning: not compiled with thread support, using thread emulation\n");
2894 static const OptionDef options
[] = {
2895 #include "cmdutils_common_opts.h"
2896 { "x", HAS_ARG
| OPT_FUNC2
, {(void*)opt_width
}, "force displayed width", "width" },
2897 { "y", HAS_ARG
| OPT_FUNC2
, {(void*)opt_height
}, "force displayed height", "height" },
2898 { "s", HAS_ARG
| OPT_VIDEO
, {(void*)opt_frame_size
}, "set frame size (WxH or abbreviation)", "size" },
2899 { "fs", OPT_BOOL
, {(void*)&is_full_screen
}, "force full screen" },
2900 { "an", OPT_BOOL
, {(void*)&audio_disable
}, "disable audio" },
2901 { "vn", OPT_BOOL
, {(void*)&video_disable
}, "disable video" },
2902 { "ast", OPT_INT
| HAS_ARG
| OPT_EXPERT
, {(void*)&wanted_stream
[CODEC_TYPE_AUDIO
]}, "select desired audio stream", "stream_number" },
2903 { "vst", OPT_INT
| HAS_ARG
| OPT_EXPERT
, {(void*)&wanted_stream
[CODEC_TYPE_VIDEO
]}, "select desired video stream", "stream_number" },
2904 { "sst", OPT_INT
| HAS_ARG
| OPT_EXPERT
, {(void*)&wanted_stream
[CODEC_TYPE_SUBTITLE
]}, "select desired subtitle stream", "stream_number" },
2905 { "ss", HAS_ARG
| OPT_FUNC2
, {(void*)&opt_seek
}, "seek to a given position in seconds", "pos" },
2906 { "bytes", OPT_INT
| HAS_ARG
, {(void*)&seek_by_bytes
}, "seek by bytes 0=off 1=on -1=auto", "val" },
2907 { "nodisp", OPT_BOOL
, {(void*)&display_disable
}, "disable graphical display" },
2908 { "f", HAS_ARG
, {(void*)opt_format
}, "force format", "fmt" },
2909 { "pix_fmt", HAS_ARG
| OPT_EXPERT
| OPT_VIDEO
, {(void*)opt_frame_pix_fmt
}, "set pixel format", "format" },
2910 { "stats", OPT_BOOL
| OPT_EXPERT
, {(void*)&show_status
}, "show status", "" },
2911 { "debug", HAS_ARG
| OPT_FUNC2
| OPT_EXPERT
, {(void*)opt_debug
}, "print specific debug info", "" },
2912 { "bug", OPT_INT
| HAS_ARG
| OPT_EXPERT
, {(void*)&workaround_bugs
}, "workaround bugs", "" },
2913 { "vismv", HAS_ARG
| OPT_FUNC2
| OPT_EXPERT
, {(void*)opt_vismv
}, "visualize motion vectors", "" },
2914 { "fast", OPT_BOOL
| OPT_EXPERT
, {(void*)&fast
}, "non spec compliant optimizations", "" },
2915 { "genpts", OPT_BOOL
| OPT_EXPERT
, {(void*)&genpts
}, "generate pts", "" },
2916 { "drp", OPT_INT
| HAS_ARG
| OPT_EXPERT
, {(void*)&decoder_reorder_pts
}, "let decoder reorder pts 0=off 1=on -1=auto", ""},
2917 { "lowres", OPT_INT
| HAS_ARG
| OPT_EXPERT
, {(void*)&lowres
}, "", "" },
2918 { "skiploop", OPT_INT
| HAS_ARG
| OPT_EXPERT
, {(void*)&skip_loop_filter
}, "", "" },
2919 { "skipframe", OPT_INT
| HAS_ARG
| OPT_EXPERT
, {(void*)&skip_frame
}, "", "" },
2920 { "skipidct", OPT_INT
| HAS_ARG
| OPT_EXPERT
, {(void*)&skip_idct
}, "", "" },
2921 { "idct", OPT_INT
| HAS_ARG
| OPT_EXPERT
, {(void*)&idct
}, "set idct algo", "algo" },
2922 { "er", OPT_INT
| HAS_ARG
| OPT_EXPERT
, {(void*)&error_recognition
}, "set error detection threshold (0-4)", "threshold" },
2923 { "ec", OPT_INT
| HAS_ARG
| OPT_EXPERT
, {(void*)&error_concealment
}, "set error concealment options", "bit_mask" },
2924 { "sync", HAS_ARG
| OPT_FUNC2
| OPT_EXPERT
, {(void*)opt_sync
}, "set audio-video sync. type (type=audio/video/ext)", "type" },
2925 { "threads", HAS_ARG
| OPT_FUNC2
| OPT_EXPERT
, {(void*)opt_thread_count
}, "thread count", "count" },
2926 { "autoexit", OPT_BOOL
| OPT_EXPERT
, {(void*)&autoexit
}, "exit at the end", "" },
2928 { "vfilters", OPT_STRING
| HAS_ARG
, {(void*)&vfilters
}, "video filters", "filter list" },
2930 { "default", OPT_FUNC2
| HAS_ARG
| OPT_AUDIO
| OPT_VIDEO
| OPT_EXPERT
, {(void*)opt_default
}, "generic catch all option", "" },
2934 static void show_usage(void)
2936 printf("Simple media player\n");
2937 printf("usage: ffplay [options] input_file\n");
2941 static void show_help(void)
2944 show_help_options(options
, "Main options:\n",
2946 show_help_options(options
, "\nAdvanced options:\n",
2947 OPT_EXPERT
, OPT_EXPERT
);
2948 printf("\nWhile playing:\n"
2950 "f toggle full screen\n"
2952 "a cycle audio channel\n"
2953 "v cycle video channel\n"
2954 "t cycle subtitle channel\n"
2955 "w show audio waves\n"
2956 "left/right seek backward/forward 10 seconds\n"
2957 "down/up seek backward/forward 1 minute\n"
2958 "mouse click seek to percentage in file corresponding to fraction of width\n"
2962 static void opt_input_file(const char *filename
)
2964 if (input_filename
) {
2965 fprintf(stderr
, "Argument '%s' provided as input filename, but '%s' was already specified.\n",
2966 filename
, input_filename
);
2969 if (!strcmp(filename
, "-"))
2971 input_filename
= filename
;
2974 /* Called from the main */
2975 int main(int argc
, char **argv
)
2979 /* register all codecs, demux and protocols */
2980 avcodec_register_all();
2981 avdevice_register_all();
2983 avfilter_register_all();
2987 for(i
=0; i
<CODEC_TYPE_NB
; i
++){
2988 avcodec_opts
[i
]= avcodec_alloc_context2(i
);
2990 avformat_opts
= avformat_alloc_context();
2991 #if !CONFIG_AVFILTER
2992 sws_opts
= sws_getContext(16,16,0, 16,16,0, sws_flags
, NULL
,NULL
,NULL
);
2997 parse_options(argc
, argv
, options
, opt_input_file
);
2999 if (!input_filename
) {
3001 fprintf(stderr
, "An input file must be specified\n");
3002 fprintf(stderr
, "Use -h to get full help or, even better, run 'man ffplay'\n");
3006 if (display_disable
) {
3009 flags
= SDL_INIT_VIDEO
| SDL_INIT_AUDIO
| SDL_INIT_TIMER
;
3010 #if !defined(__MINGW32__) && !defined(__APPLE__)
3011 flags
|= SDL_INIT_EVENTTHREAD
; /* Not supported on Windows or Mac OS X */
3013 if (SDL_Init (flags
)) {
3014 fprintf(stderr
, "Could not initialize SDL - %s\n", SDL_GetError());
3018 if (!display_disable
) {
3019 #if HAVE_SDL_VIDEO_SIZE
3020 const SDL_VideoInfo
*vi
= SDL_GetVideoInfo();
3021 fs_screen_width
= vi
->current_w
;
3022 fs_screen_height
= vi
->current_h
;
3026 SDL_EventState(SDL_ACTIVEEVENT
, SDL_IGNORE
);
3027 SDL_EventState(SDL_SYSWMEVENT
, SDL_IGNORE
);
3028 SDL_EventState(SDL_USEREVENT
, SDL_IGNORE
);
3030 av_init_packet(&flush_pkt
);
3031 flush_pkt
.data
= "FLUSH";
3033 cur_stream
= stream_open(input_filename
, file_iformat
);