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/dsputil.h"
38 #include <SDL_thread.h>
41 #undef main /* We don't want SDL to override our main() */
48 const char program_name
[] = "FFplay";
49 const int program_birth_year
= 2003;
53 #define MAX_QUEUE_SIZE (15 * 1024 * 1024)
54 #define MIN_AUDIOQ_SIZE (20 * 16 * 1024)
57 /* SDL audio buffer size, in samples. Should be small to have precise
58 A/V sync as SDL does not have hardware buffer fullness info. */
59 #define SDL_AUDIO_BUFFER_SIZE 1024
61 /* no AV sync correction is done if below the AV sync threshold */
62 #define AV_SYNC_THRESHOLD 0.01
63 /* no AV correction is done if too big error */
64 #define AV_NOSYNC_THRESHOLD 10.0
66 /* maximum audio speed change to get correct sync */
67 #define SAMPLE_CORRECTION_PERCENT_MAX 10
69 /* we use about AUDIO_DIFF_AVG_NB A-V differences to make the average */
70 #define AUDIO_DIFF_AVG_NB 20
72 /* NOTE: the size must be big enough to compensate the hardware audio buffersize size */
73 #define SAMPLE_ARRAY_SIZE (2*65536)
75 static int sws_flags
= SWS_BICUBIC
;
77 typedef struct PacketQueue
{
78 AVPacketList
*first_pkt
, *last_pkt
;
86 #define VIDEO_PICTURE_QUEUE_SIZE 1
87 #define SUBPICTURE_QUEUE_SIZE 4
89 typedef struct VideoPicture
{
90 double pts
; ///<presentation time stamp for this picture
91 int64_t pos
; ///<byte position in file
93 int width
, height
; /* source height & width */
98 typedef struct SubPicture
{
99 double pts
; /* presentation time stamp for this picture */
104 AV_SYNC_AUDIO_MASTER
, /* default choice */
105 AV_SYNC_VIDEO_MASTER
,
106 AV_SYNC_EXTERNAL_CLOCK
, /* synchronize to an external clock */
109 typedef struct VideoState
{
110 SDL_Thread
*parse_tid
;
111 SDL_Thread
*video_tid
;
112 AVInputFormat
*iformat
;
121 int read_pause_return
;
123 int dtg_active_format
;
128 double external_clock
; /* external clock base */
129 int64_t external_clock_time
;
132 double audio_diff_cum
; /* used for AV difference average computation */
133 double audio_diff_avg_coef
;
134 double audio_diff_threshold
;
135 int audio_diff_avg_count
;
138 int audio_hw_buf_size
;
139 /* samples output by the codec. we reserve more space for avsync
141 DECLARE_ALIGNED(16,uint8_t,audio_buf1
)[(AVCODEC_MAX_AUDIO_FRAME_SIZE
* 3) / 2];
142 DECLARE_ALIGNED(16,uint8_t,audio_buf2
)[(AVCODEC_MAX_AUDIO_FRAME_SIZE
* 3) / 2];
144 unsigned int audio_buf_size
; /* in bytes */
145 int audio_buf_index
; /* in bytes */
146 AVPacket audio_pkt_temp
;
148 enum SampleFormat audio_src_fmt
;
149 AVAudioConvert
*reformat_ctx
;
151 int show_audio
; /* if true, display audio samples */
152 int16_t sample_array
[SAMPLE_ARRAY_SIZE
];
153 int sample_array_index
;
159 SDL_Thread
*subtitle_tid
;
161 int subtitle_stream_changed
;
162 AVStream
*subtitle_st
;
163 PacketQueue subtitleq
;
164 SubPicture subpq
[SUBPICTURE_QUEUE_SIZE
];
165 int subpq_size
, subpq_rindex
, subpq_windex
;
166 SDL_mutex
*subpq_mutex
;
167 SDL_cond
*subpq_cond
;
170 double frame_last_pts
;
171 double frame_last_delay
;
172 double video_clock
; ///<pts of last decoded frame / predicted pts of next decoded frame
176 double video_current_pts
; ///<current displayed pts (different from video_clock if frame fifos are used)
177 double video_current_pts_drift
; ///<video_current_pts - time (av_gettime) at which we updated video_current_pts - used to have running video pts
178 int64_t video_current_pos
; ///<current displayed file pos
179 VideoPicture pictq
[VIDEO_PICTURE_QUEUE_SIZE
];
180 int pictq_size
, pictq_rindex
, pictq_windex
;
181 SDL_mutex
*pictq_mutex
;
182 SDL_cond
*pictq_cond
;
183 struct SwsContext
*img_convert_ctx
;
185 // QETimer *video_timer;
187 int width
, height
, xleft
, ytop
;
191 int64_t last_dts_for_fault_detection
;
192 int64_t last_pts_for_fault_detection
;
196 static void show_help(void);
197 static int audio_write_get_buf_size(VideoState
*is
);
199 /* options specified by the user */
200 static AVInputFormat
*file_iformat
;
201 static const char *input_filename
;
202 static int fs_screen_width
;
203 static int fs_screen_height
;
204 static int screen_width
= 0;
205 static int screen_height
= 0;
206 static int frame_width
= 0;
207 static int frame_height
= 0;
208 static enum PixelFormat frame_pix_fmt
= PIX_FMT_NONE
;
209 static int audio_disable
;
210 static int video_disable
;
211 static int wanted_stream
[CODEC_TYPE_NB
]={
212 [CODEC_TYPE_AUDIO
]=-1,
213 [CODEC_TYPE_VIDEO
]=-1,
214 [CODEC_TYPE_SUBTITLE
]=-1,
216 static int seek_by_bytes
=-1;
217 static int display_disable
;
218 static int show_status
= 1;
219 static int av_sync_type
= AV_SYNC_AUDIO_MASTER
;
220 static int64_t start_time
= AV_NOPTS_VALUE
;
221 static int debug
= 0;
222 static int debug_mv
= 0;
224 static int thread_count
= 1;
225 static int workaround_bugs
= 1;
227 static int genpts
= 0;
228 static int lowres
= 0;
229 static int idct
= FF_IDCT_AUTO
;
230 static enum AVDiscard skip_frame
= AVDISCARD_DEFAULT
;
231 static enum AVDiscard skip_idct
= AVDISCARD_DEFAULT
;
232 static enum AVDiscard skip_loop_filter
= AVDISCARD_DEFAULT
;
233 static int error_recognition
= FF_ER_CAREFUL
;
234 static int error_concealment
= 3;
235 static int decoder_reorder_pts
= -1;
238 /* current context */
239 static int is_full_screen
;
240 static VideoState
*cur_stream
;
241 static int64_t audio_callback_time
;
243 static AVPacket flush_pkt
;
245 #define FF_ALLOC_EVENT (SDL_USEREVENT)
246 #define FF_REFRESH_EVENT (SDL_USEREVENT + 1)
247 #define FF_QUIT_EVENT (SDL_USEREVENT + 2)
249 static SDL_Surface
*screen
;
251 static int packet_queue_put(PacketQueue
*q
, AVPacket
*pkt
);
253 /* packet queue handling */
254 static void packet_queue_init(PacketQueue
*q
)
256 memset(q
, 0, sizeof(PacketQueue
));
257 q
->mutex
= SDL_CreateMutex();
258 q
->cond
= SDL_CreateCond();
259 packet_queue_put(q
, &flush_pkt
);
262 static void packet_queue_flush(PacketQueue
*q
)
264 AVPacketList
*pkt
, *pkt1
;
266 SDL_LockMutex(q
->mutex
);
267 for(pkt
= q
->first_pkt
; pkt
!= NULL
; pkt
= pkt1
) {
269 av_free_packet(&pkt
->pkt
);
276 SDL_UnlockMutex(q
->mutex
);
279 static void packet_queue_end(PacketQueue
*q
)
281 packet_queue_flush(q
);
282 SDL_DestroyMutex(q
->mutex
);
283 SDL_DestroyCond(q
->cond
);
286 static int packet_queue_put(PacketQueue
*q
, AVPacket
*pkt
)
290 /* duplicate the packet */
291 if (pkt
!=&flush_pkt
&& av_dup_packet(pkt
) < 0)
294 pkt1
= av_malloc(sizeof(AVPacketList
));
301 SDL_LockMutex(q
->mutex
);
307 q
->last_pkt
->next
= pkt1
;
310 q
->size
+= pkt1
->pkt
.size
+ sizeof(*pkt1
);
311 /* XXX: should duplicate packet data in DV case */
312 SDL_CondSignal(q
->cond
);
314 SDL_UnlockMutex(q
->mutex
);
318 static void packet_queue_abort(PacketQueue
*q
)
320 SDL_LockMutex(q
->mutex
);
322 q
->abort_request
= 1;
324 SDL_CondSignal(q
->cond
);
326 SDL_UnlockMutex(q
->mutex
);
329 /* return < 0 if aborted, 0 if no packet and > 0 if packet. */
330 static int packet_queue_get(PacketQueue
*q
, AVPacket
*pkt
, int block
)
335 SDL_LockMutex(q
->mutex
);
338 if (q
->abort_request
) {
345 q
->first_pkt
= pkt1
->next
;
349 q
->size
-= pkt1
->pkt
.size
+ sizeof(*pkt1
);
358 SDL_CondWait(q
->cond
, q
->mutex
);
361 SDL_UnlockMutex(q
->mutex
);
365 static inline void fill_rectangle(SDL_Surface
*screen
,
366 int x
, int y
, int w
, int h
, int color
)
373 SDL_FillRect(screen
, &rect
, color
);
377 /* draw only the border of a rectangle */
378 void fill_border(VideoState
*s
, int x
, int y
, int w
, int h
, int color
)
382 /* fill the background */
386 w2
= s
->width
- (x
+ w
);
392 h2
= s
->height
- (y
+ h
);
395 fill_rectangle(screen
,
399 fill_rectangle(screen
,
400 s
->xleft
+ s
->width
- w2
, s
->ytop
,
403 fill_rectangle(screen
,
404 s
->xleft
+ w1
, s
->ytop
,
405 s
->width
- w1
- w2
, h1
,
407 fill_rectangle(screen
,
408 s
->xleft
+ w1
, s
->ytop
+ s
->height
- h2
,
409 s
->width
- w1
- w2
, h2
,
414 #define ALPHA_BLEND(a, oldp, newp, s)\
415 ((((oldp << s) * (255 - (a))) + (newp * (a))) / (255 << s))
417 #define RGBA_IN(r, g, b, a, s)\
419 unsigned int v = ((const uint32_t *)(s))[0];\
420 a = (v >> 24) & 0xff;\
421 r = (v >> 16) & 0xff;\
422 g = (v >> 8) & 0xff;\
426 #define YUVA_IN(y, u, v, a, s, pal)\
428 unsigned int val = ((const uint32_t *)(pal))[*(const uint8_t*)(s)];\
429 a = (val >> 24) & 0xff;\
430 y = (val >> 16) & 0xff;\
431 u = (val >> 8) & 0xff;\
435 #define YUVA_OUT(d, y, u, v, a)\
437 ((uint32_t *)(d))[0] = (a << 24) | (y << 16) | (u << 8) | v;\
443 static void blend_subrect(AVPicture
*dst
, const AVSubtitleRect
*rect
, int imgw
, int imgh
)
445 int wrap
, wrap3
, width2
, skip2
;
446 int y
, u
, v
, a
, u1
, v1
, a1
, w
, h
;
447 uint8_t *lum
, *cb
, *cr
;
450 int dstx
, dsty
, dstw
, dsth
;
452 dstw
= av_clip(rect
->w
, 0, imgw
);
453 dsth
= av_clip(rect
->h
, 0, imgh
);
454 dstx
= av_clip(rect
->x
, 0, imgw
- dstw
);
455 dsty
= av_clip(rect
->y
, 0, imgh
- dsth
);
456 lum
= dst
->data
[0] + dsty
* dst
->linesize
[0];
457 cb
= dst
->data
[1] + (dsty
>> 1) * dst
->linesize
[1];
458 cr
= dst
->data
[2] + (dsty
>> 1) * dst
->linesize
[2];
460 width2
= ((dstw
+ 1) >> 1) + (dstx
& ~dstw
& 1);
462 wrap
= dst
->linesize
[0];
463 wrap3
= rect
->pict
.linesize
[0];
464 p
= rect
->pict
.data
[0];
465 pal
= (const uint32_t *)rect
->pict
.data
[1]; /* Now in YCrCb! */
473 YUVA_IN(y
, u
, v
, a
, p
, pal
);
474 lum
[0] = ALPHA_BLEND(a
, lum
[0], y
, 0);
475 cb
[0] = ALPHA_BLEND(a
>> 2, cb
[0], u
, 0);
476 cr
[0] = ALPHA_BLEND(a
>> 2, cr
[0], v
, 0);
482 for(w
= dstw
- (dstx
& 1); w
>= 2; w
-= 2) {
483 YUVA_IN(y
, u
, v
, a
, p
, pal
);
487 lum
[0] = ALPHA_BLEND(a
, lum
[0], y
, 0);
489 YUVA_IN(y
, u
, v
, a
, p
+ BPP
, pal
);
493 lum
[1] = ALPHA_BLEND(a
, lum
[1], y
, 0);
494 cb
[0] = ALPHA_BLEND(a1
>> 2, cb
[0], u1
, 1);
495 cr
[0] = ALPHA_BLEND(a1
>> 2, cr
[0], v1
, 1);
502 YUVA_IN(y
, u
, v
, a
, p
, pal
);
503 lum
[0] = ALPHA_BLEND(a
, lum
[0], y
, 0);
504 cb
[0] = ALPHA_BLEND(a
>> 2, cb
[0], u
, 0);
505 cr
[0] = ALPHA_BLEND(a
>> 2, cr
[0], v
, 0);
509 p
+= wrap3
- dstw
* BPP
;
510 lum
+= wrap
- dstw
- dstx
;
511 cb
+= dst
->linesize
[1] - width2
- skip2
;
512 cr
+= dst
->linesize
[2] - width2
- skip2
;
514 for(h
= dsth
- (dsty
& 1); h
>= 2; h
-= 2) {
520 YUVA_IN(y
, u
, v
, a
, p
, pal
);
524 lum
[0] = ALPHA_BLEND(a
, lum
[0], y
, 0);
527 YUVA_IN(y
, u
, v
, a
, p
, pal
);
531 lum
[0] = ALPHA_BLEND(a
, lum
[0], y
, 0);
532 cb
[0] = ALPHA_BLEND(a1
>> 2, cb
[0], u1
, 1);
533 cr
[0] = ALPHA_BLEND(a1
>> 2, cr
[0], v1
, 1);
539 for(w
= dstw
- (dstx
& 1); w
>= 2; w
-= 2) {
540 YUVA_IN(y
, u
, v
, a
, p
, pal
);
544 lum
[0] = ALPHA_BLEND(a
, lum
[0], y
, 0);
546 YUVA_IN(y
, u
, v
, a
, p
+ BPP
, pal
);
550 lum
[1] = ALPHA_BLEND(a
, lum
[1], y
, 0);
554 YUVA_IN(y
, u
, v
, a
, p
, pal
);
558 lum
[0] = ALPHA_BLEND(a
, lum
[0], y
, 0);
560 YUVA_IN(y
, u
, v
, a
, p
+ BPP
, pal
);
564 lum
[1] = ALPHA_BLEND(a
, lum
[1], y
, 0);
566 cb
[0] = ALPHA_BLEND(a1
>> 2, cb
[0], u1
, 2);
567 cr
[0] = ALPHA_BLEND(a1
>> 2, cr
[0], v1
, 2);
571 p
+= -wrap3
+ 2 * BPP
;
575 YUVA_IN(y
, u
, v
, a
, p
, pal
);
579 lum
[0] = ALPHA_BLEND(a
, lum
[0], y
, 0);
582 YUVA_IN(y
, u
, v
, a
, p
, pal
);
586 lum
[0] = ALPHA_BLEND(a
, lum
[0], y
, 0);
587 cb
[0] = ALPHA_BLEND(a1
>> 2, cb
[0], u1
, 1);
588 cr
[0] = ALPHA_BLEND(a1
>> 2, cr
[0], v1
, 1);
594 p
+= wrap3
+ (wrap3
- dstw
* BPP
);
595 lum
+= wrap
+ (wrap
- dstw
- dstx
);
596 cb
+= dst
->linesize
[1] - width2
- skip2
;
597 cr
+= dst
->linesize
[2] - width2
- skip2
;
599 /* handle odd height */
606 YUVA_IN(y
, u
, v
, a
, p
, pal
);
607 lum
[0] = ALPHA_BLEND(a
, lum
[0], y
, 0);
608 cb
[0] = ALPHA_BLEND(a
>> 2, cb
[0], u
, 0);
609 cr
[0] = ALPHA_BLEND(a
>> 2, cr
[0], v
, 0);
615 for(w
= dstw
- (dstx
& 1); w
>= 2; w
-= 2) {
616 YUVA_IN(y
, u
, v
, a
, p
, pal
);
620 lum
[0] = ALPHA_BLEND(a
, lum
[0], y
, 0);
622 YUVA_IN(y
, u
, v
, a
, p
+ BPP
, pal
);
626 lum
[1] = ALPHA_BLEND(a
, lum
[1], y
, 0);
627 cb
[0] = ALPHA_BLEND(a1
>> 2, cb
[0], u
, 1);
628 cr
[0] = ALPHA_BLEND(a1
>> 2, cr
[0], v
, 1);
635 YUVA_IN(y
, u
, v
, a
, p
, pal
);
636 lum
[0] = ALPHA_BLEND(a
, lum
[0], y
, 0);
637 cb
[0] = ALPHA_BLEND(a
>> 2, cb
[0], u
, 0);
638 cr
[0] = ALPHA_BLEND(a
>> 2, cr
[0], v
, 0);
643 static void free_subpicture(SubPicture
*sp
)
647 for (i
= 0; i
< sp
->sub
.num_rects
; i
++)
649 av_freep(&sp
->sub
.rects
[i
]->pict
.data
[0]);
650 av_freep(&sp
->sub
.rects
[i
]->pict
.data
[1]);
651 av_freep(&sp
->sub
.rects
[i
]);
654 av_free(sp
->sub
.rects
);
656 memset(&sp
->sub
, 0, sizeof(AVSubtitle
));
659 static void video_image_display(VideoState
*is
)
665 int width
, height
, x
, y
;
669 vp
= &is
->pictq
[is
->pictq_rindex
];
671 /* XXX: use variable in the frame */
672 if (is
->video_st
->sample_aspect_ratio
.num
)
673 aspect_ratio
= av_q2d(is
->video_st
->sample_aspect_ratio
);
674 else if (is
->video_st
->codec
->sample_aspect_ratio
.num
)
675 aspect_ratio
= av_q2d(is
->video_st
->codec
->sample_aspect_ratio
);
678 if (aspect_ratio
<= 0.0)
680 aspect_ratio
*= (float)is
->video_st
->codec
->width
/ is
->video_st
->codec
->height
;
681 /* if an active format is indicated, then it overrides the
684 if (is
->video_st
->codec
->dtg_active_format
!= is
->dtg_active_format
) {
685 is
->dtg_active_format
= is
->video_st
->codec
->dtg_active_format
;
686 printf("dtg_active_format=%d\n", is
->dtg_active_format
);
690 switch(is
->video_st
->codec
->dtg_active_format
) {
691 case FF_DTG_AFD_SAME
:
696 aspect_ratio
= 4.0 / 3.0;
698 case FF_DTG_AFD_16_9
:
699 aspect_ratio
= 16.0 / 9.0;
701 case FF_DTG_AFD_14_9
:
702 aspect_ratio
= 14.0 / 9.0;
704 case FF_DTG_AFD_4_3_SP_14_9
:
705 aspect_ratio
= 14.0 / 9.0;
707 case FF_DTG_AFD_16_9_SP_14_9
:
708 aspect_ratio
= 14.0 / 9.0;
710 case FF_DTG_AFD_SP_4_3
:
711 aspect_ratio
= 4.0 / 3.0;
718 if (is
->subpq_size
> 0)
720 sp
= &is
->subpq
[is
->subpq_rindex
];
722 if (vp
->pts
>= sp
->pts
+ ((float) sp
->sub
.start_display_time
/ 1000))
724 SDL_LockYUVOverlay (vp
->bmp
);
726 pict
.data
[0] = vp
->bmp
->pixels
[0];
727 pict
.data
[1] = vp
->bmp
->pixels
[2];
728 pict
.data
[2] = vp
->bmp
->pixels
[1];
730 pict
.linesize
[0] = vp
->bmp
->pitches
[0];
731 pict
.linesize
[1] = vp
->bmp
->pitches
[2];
732 pict
.linesize
[2] = vp
->bmp
->pitches
[1];
734 for (i
= 0; i
< sp
->sub
.num_rects
; i
++)
735 blend_subrect(&pict
, sp
->sub
.rects
[i
],
736 vp
->bmp
->w
, vp
->bmp
->h
);
738 SDL_UnlockYUVOverlay (vp
->bmp
);
744 /* XXX: we suppose the screen has a 1.0 pixel ratio */
746 width
= ((int)rint(height
* aspect_ratio
)) & ~1;
747 if (width
> is
->width
) {
749 height
= ((int)rint(width
/ aspect_ratio
)) & ~1;
751 x
= (is
->width
- width
) / 2;
752 y
= (is
->height
- height
) / 2;
753 if (!is
->no_background
) {
754 /* fill the background */
755 // fill_border(is, x, y, width, height, QERGB(0x00, 0x00, 0x00));
757 is
->no_background
= 0;
759 rect
.x
= is
->xleft
+ x
;
760 rect
.y
= is
->ytop
+ y
;
763 SDL_DisplayYUVOverlay(vp
->bmp
, &rect
);
766 fill_rectangle(screen
,
767 is
->xleft
, is
->ytop
, is
->width
, is
->height
,
768 QERGB(0x00, 0x00, 0x00));
773 static inline int compute_mod(int a
, int b
)
782 static void video_audio_display(VideoState
*s
)
784 int i
, i_start
, x
, y1
, y
, ys
, delay
, n
, nb_display_channels
;
785 int ch
, channels
, h
, h2
, bgcolor
, fgcolor
;
787 int rdft_bits
, nb_freq
;
789 for(rdft_bits
=1; (1<<rdft_bits
)<2*s
->height
; rdft_bits
++)
791 nb_freq
= 1<<(rdft_bits
-1);
793 /* compute display index : center on currently output samples */
794 channels
= s
->audio_st
->codec
->channels
;
795 nb_display_channels
= channels
;
797 int data_used
= s
->show_audio
==1 ? s
->width
: (2*nb_freq
);
799 delay
= audio_write_get_buf_size(s
);
802 /* to be more precise, we take into account the time spent since
803 the last buffer computation */
804 if (audio_callback_time
) {
805 time_diff
= av_gettime() - audio_callback_time
;
806 delay
+= (time_diff
* s
->audio_st
->codec
->sample_rate
) / 1000000;
809 delay
-= data_used
/ 2;
810 if (delay
< data_used
)
813 i_start
= x
= compute_mod(s
->sample_array_index
- delay
* channels
, SAMPLE_ARRAY_SIZE
);
814 if(s
->show_audio
==1){
816 for(i
=0; i
<1000; i
+=channels
){
817 int idx
= (SAMPLE_ARRAY_SIZE
+ x
- i
) % SAMPLE_ARRAY_SIZE
;
818 int a
= s
->sample_array
[idx
];
819 int b
= s
->sample_array
[(idx
+ 4*channels
)%SAMPLE_ARRAY_SIZE
];
820 int c
= s
->sample_array
[(idx
+ 5*channels
)%SAMPLE_ARRAY_SIZE
];
821 int d
= s
->sample_array
[(idx
+ 9*channels
)%SAMPLE_ARRAY_SIZE
];
823 if(h
<score
&& (b
^c
)<0){
830 s
->last_i_start
= i_start
;
832 i_start
= s
->last_i_start
;
835 bgcolor
= SDL_MapRGB(screen
->format
, 0x00, 0x00, 0x00);
836 if(s
->show_audio
==1){
837 fill_rectangle(screen
,
838 s
->xleft
, s
->ytop
, s
->width
, s
->height
,
841 fgcolor
= SDL_MapRGB(screen
->format
, 0xff, 0xff, 0xff);
843 /* total height for one channel */
844 h
= s
->height
/ nb_display_channels
;
845 /* graph height / 2 */
847 for(ch
= 0;ch
< nb_display_channels
; ch
++) {
849 y1
= s
->ytop
+ ch
* h
+ (h
/ 2); /* position of center line */
850 for(x
= 0; x
< s
->width
; x
++) {
851 y
= (s
->sample_array
[i
] * h2
) >> 15;
858 fill_rectangle(screen
,
859 s
->xleft
+ x
, ys
, 1, y
,
862 if (i
>= SAMPLE_ARRAY_SIZE
)
863 i
-= SAMPLE_ARRAY_SIZE
;
867 fgcolor
= SDL_MapRGB(screen
->format
, 0x00, 0x00, 0xff);
869 for(ch
= 1;ch
< nb_display_channels
; ch
++) {
870 y
= s
->ytop
+ ch
* h
;
871 fill_rectangle(screen
,
872 s
->xleft
, y
, s
->width
, 1,
875 SDL_UpdateRect(screen
, s
->xleft
, s
->ytop
, s
->width
, s
->height
);
877 nb_display_channels
= FFMIN(nb_display_channels
, 2);
878 if(rdft_bits
!= s
->rdft_bits
){
879 ff_rdft_end(&s
->rdft
);
880 ff_rdft_init(&s
->rdft
, rdft_bits
, RDFT
);
881 s
->rdft_bits
= rdft_bits
;
884 FFTSample data
[2][2*nb_freq
];
885 for(ch
= 0;ch
< nb_display_channels
; ch
++) {
887 for(x
= 0; x
< 2*nb_freq
; x
++) {
888 double w
= (x
-nb_freq
)*(1.0/nb_freq
);
889 data
[ch
][x
]= s
->sample_array
[i
]*(1.0-w
*w
);
891 if (i
>= SAMPLE_ARRAY_SIZE
)
892 i
-= SAMPLE_ARRAY_SIZE
;
894 ff_rdft_calc(&s
->rdft
, data
[ch
]);
896 //least efficient way to do this, we should of course directly access it but its more than fast enough
897 for(y
=0; y
<s
->height
; y
++){
898 double w
= 1/sqrt(nb_freq
);
899 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]));
900 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]));
903 fgcolor
= SDL_MapRGB(screen
->format
, a
, b
, (a
+b
)/2);
905 fill_rectangle(screen
,
906 s
->xpos
, s
->height
-y
, 1, 1,
910 SDL_UpdateRect(screen
, s
->xpos
, s
->ytop
, 1, s
->height
);
912 if(s
->xpos
>= s
->width
)
917 static int video_open(VideoState
*is
){
918 int flags
= SDL_HWSURFACE
|SDL_ASYNCBLIT
|SDL_HWACCEL
;
921 if(is_full_screen
) flags
|= SDL_FULLSCREEN
;
922 else flags
|= SDL_RESIZABLE
;
924 if (is_full_screen
&& fs_screen_width
) {
926 h
= fs_screen_height
;
927 } else if(!is_full_screen
&& screen_width
){
930 }else if (is
->video_st
&& is
->video_st
->codec
->width
){
931 w
= is
->video_st
->codec
->width
;
932 h
= is
->video_st
->codec
->height
;
938 screen
= SDL_SetVideoMode(w
, h
, 0, flags
);
940 /* setting bits_per_pixel = 0 or 32 causes blank video on OS X */
941 screen
= SDL_SetVideoMode(w
, h
, 24, flags
);
944 fprintf(stderr
, "SDL: could not set video mode - exiting\n");
947 SDL_WM_SetCaption("FFplay", "FFplay");
949 is
->width
= screen
->w
;
950 is
->height
= screen
->h
;
955 /* display the current picture, if any */
956 static void video_display(VideoState
*is
)
959 video_open(cur_stream
);
960 if (is
->audio_st
&& is
->show_audio
)
961 video_audio_display(is
);
962 else if (is
->video_st
)
963 video_image_display(is
);
966 static Uint32
sdl_refresh_timer_cb(Uint32 interval
, void *opaque
)
969 event
.type
= FF_REFRESH_EVENT
;
970 event
.user
.data1
= opaque
;
971 SDL_PushEvent(&event
);
972 return 0; /* 0 means stop timer */
975 /* schedule a video refresh in 'delay' ms */
976 static SDL_TimerID
schedule_refresh(VideoState
*is
, int delay
)
978 if(!delay
) delay
=1; //SDL seems to be buggy when the delay is 0
979 return SDL_AddTimer(delay
, sdl_refresh_timer_cb
, is
);
982 /* get the current audio clock value */
983 static double get_audio_clock(VideoState
*is
)
986 int hw_buf_size
, bytes_per_sec
;
987 pts
= is
->audio_clock
;
988 hw_buf_size
= audio_write_get_buf_size(is
);
991 bytes_per_sec
= is
->audio_st
->codec
->sample_rate
*
992 2 * is
->audio_st
->codec
->channels
;
995 pts
-= (double)hw_buf_size
/ bytes_per_sec
;
999 /* get the current video clock value */
1000 static double get_video_clock(VideoState
*is
)
1003 return is
->video_current_pts
;
1005 return is
->video_current_pts_drift
+ av_gettime() / 1000000.0;
1009 /* get the current external clock value */
1010 static double get_external_clock(VideoState
*is
)
1014 return is
->external_clock
+ ((ti
- is
->external_clock_time
) * 1e-6);
1017 /* get the current master clock value */
1018 static double get_master_clock(VideoState
*is
)
1022 if (is
->av_sync_type
== AV_SYNC_VIDEO_MASTER
) {
1024 val
= get_video_clock(is
);
1026 val
= get_audio_clock(is
);
1027 } else if (is
->av_sync_type
== AV_SYNC_AUDIO_MASTER
) {
1029 val
= get_audio_clock(is
);
1031 val
= get_video_clock(is
);
1033 val
= get_external_clock(is
);
1038 /* seek in the stream */
1039 static void stream_seek(VideoState
*is
, int64_t pos
, int64_t rel
, int seek_by_bytes
)
1041 if (!is
->seek_req
) {
1044 is
->seek_flags
&= ~AVSEEK_FLAG_BYTE
;
1046 is
->seek_flags
|= AVSEEK_FLAG_BYTE
;
1051 /* pause or resume the video */
1052 static void stream_pause(VideoState
*is
)
1055 is
->frame_timer
+= av_gettime() / 1000000.0 + is
->video_current_pts_drift
- is
->video_current_pts
;
1056 if(is
->read_pause_return
!= AVERROR(ENOSYS
)){
1057 is
->video_current_pts
= is
->video_current_pts_drift
+ av_gettime() / 1000000.0;
1059 is
->video_current_pts_drift
= is
->video_current_pts
- av_gettime() / 1000000.0;
1061 is
->paused
= !is
->paused
;
1064 static double compute_frame_delay(double frame_current_pts
, VideoState
*is
)
1066 double actual_delay
, delay
, sync_threshold
, diff
;
1068 /* compute nominal delay */
1069 delay
= frame_current_pts
- is
->frame_last_pts
;
1070 if (delay
<= 0 || delay
>= 10.0) {
1071 /* if incorrect delay, use previous one */
1072 delay
= is
->frame_last_delay
;
1074 is
->frame_last_delay
= delay
;
1076 is
->frame_last_pts
= frame_current_pts
;
1078 /* update delay to follow master synchronisation source */
1079 if (((is
->av_sync_type
== AV_SYNC_AUDIO_MASTER
&& is
->audio_st
) ||
1080 is
->av_sync_type
== AV_SYNC_EXTERNAL_CLOCK
)) {
1081 /* if video is slave, we try to correct big delays by
1082 duplicating or deleting a frame */
1083 diff
= get_video_clock(is
) - get_master_clock(is
);
1085 /* skip or repeat frame. We take into account the
1086 delay to compute the threshold. I still don't know
1087 if it is the best guess */
1088 sync_threshold
= FFMAX(AV_SYNC_THRESHOLD
, delay
);
1089 if (fabs(diff
) < AV_NOSYNC_THRESHOLD
) {
1090 if (diff
<= -sync_threshold
)
1092 else if (diff
>= sync_threshold
)
1097 is
->frame_timer
+= delay
;
1098 /* compute the REAL delay (we need to do that to avoid
1100 actual_delay
= is
->frame_timer
- (av_gettime() / 1000000.0);
1101 if (actual_delay
< 0.010) {
1102 /* XXX: should skip picture */
1103 actual_delay
= 0.010;
1106 #if defined(DEBUG_SYNC)
1107 printf("video: delay=%0.3f actual_delay=%0.3f pts=%0.3f A-V=%f\n",
1108 delay
, actual_delay
, frame_current_pts
, -diff
);
1111 return actual_delay
;
1114 /* called to display each frame */
1115 static void video_refresh_timer(void *opaque
)
1117 VideoState
*is
= opaque
;
1120 SubPicture
*sp
, *sp2
;
1123 if (is
->pictq_size
== 0) {
1124 fprintf(stderr
, "Internal error detected in the SDL timer\n");
1126 /* dequeue the picture */
1127 vp
= &is
->pictq
[is
->pictq_rindex
];
1129 /* update current video pts */
1130 is
->video_current_pts
= vp
->pts
;
1131 is
->video_current_pts_drift
= is
->video_current_pts
- av_gettime() / 1000000.0;
1132 is
->video_current_pos
= vp
->pos
;
1134 if(is
->subtitle_st
) {
1135 if (is
->subtitle_stream_changed
) {
1136 SDL_LockMutex(is
->subpq_mutex
);
1138 while (is
->subpq_size
) {
1139 free_subpicture(&is
->subpq
[is
->subpq_rindex
]);
1141 /* update queue size and signal for next picture */
1142 if (++is
->subpq_rindex
== SUBPICTURE_QUEUE_SIZE
)
1143 is
->subpq_rindex
= 0;
1147 is
->subtitle_stream_changed
= 0;
1149 SDL_CondSignal(is
->subpq_cond
);
1150 SDL_UnlockMutex(is
->subpq_mutex
);
1152 if (is
->subpq_size
> 0) {
1153 sp
= &is
->subpq
[is
->subpq_rindex
];
1155 if (is
->subpq_size
> 1)
1156 sp2
= &is
->subpq
[(is
->subpq_rindex
+ 1) % SUBPICTURE_QUEUE_SIZE
];
1160 if ((is
->video_current_pts
> (sp
->pts
+ ((float) sp
->sub
.end_display_time
/ 1000)))
1161 || (sp2
&& is
->video_current_pts
> (sp2
->pts
+ ((float) sp2
->sub
.start_display_time
/ 1000))))
1163 free_subpicture(sp
);
1165 /* update queue size and signal for next picture */
1166 if (++is
->subpq_rindex
== SUBPICTURE_QUEUE_SIZE
)
1167 is
->subpq_rindex
= 0;
1169 SDL_LockMutex(is
->subpq_mutex
);
1171 SDL_CondSignal(is
->subpq_cond
);
1172 SDL_UnlockMutex(is
->subpq_mutex
);
1178 /* display picture */
1181 /* update queue size and signal for next picture */
1182 if (++is
->pictq_rindex
== VIDEO_PICTURE_QUEUE_SIZE
)
1183 is
->pictq_rindex
= 0;
1185 SDL_LockMutex(is
->pictq_mutex
);
1188 SDL_CondSignal(is
->pictq_cond
);
1189 SDL_UnlockMutex(is
->pictq_mutex
);
1191 } else if (is
->audio_st
) {
1192 /* draw the next audio frame */
1194 schedule_refresh(is
, 40);
1196 /* if only audio stream, then display the audio bars (better
1197 than nothing, just to test the implementation */
1199 /* display picture */
1202 schedule_refresh(is
, 100);
1205 static int64_t last_time
;
1207 int aqsize
, vqsize
, sqsize
;
1210 cur_time
= av_gettime();
1211 if (!last_time
|| (cur_time
- last_time
) >= 30000) {
1216 aqsize
= is
->audioq
.size
;
1218 vqsize
= is
->videoq
.size
;
1219 if (is
->subtitle_st
)
1220 sqsize
= is
->subtitleq
.size
;
1222 if (is
->audio_st
&& is
->video_st
)
1223 av_diff
= get_audio_clock(is
) - get_video_clock(is
);
1224 printf("%7.2f A-V:%7.3f aq=%5dKB vq=%5dKB sq=%5dB f=%Ld/%Ld \r",
1225 get_master_clock(is
), av_diff
, aqsize
/ 1024, vqsize
/ 1024, sqsize
, is
->faulty_dts
, is
->faulty_pts
);
1227 last_time
= cur_time
;
1232 /* allocate a picture (needs to do that in main thread to avoid
1233 potential locking problems */
1234 static void alloc_picture(void *opaque
)
1236 VideoState
*is
= opaque
;
1239 vp
= &is
->pictq
[is
->pictq_windex
];
1242 SDL_FreeYUVOverlay(vp
->bmp
);
1244 vp
->bmp
= SDL_CreateYUVOverlay(is
->video_st
->codec
->width
,
1245 is
->video_st
->codec
->height
,
1248 vp
->width
= is
->video_st
->codec
->width
;
1249 vp
->height
= is
->video_st
->codec
->height
;
1251 SDL_LockMutex(is
->pictq_mutex
);
1253 SDL_CondSignal(is
->pictq_cond
);
1254 SDL_UnlockMutex(is
->pictq_mutex
);
1259 * @param pts the dts of the pkt / pts of the frame and guessed if not known
1261 static int queue_picture(VideoState
*is
, AVFrame
*src_frame
, double pts
, int64_t pos
)
1266 /* wait until we have space to put a new picture */
1267 SDL_LockMutex(is
->pictq_mutex
);
1268 while (is
->pictq_size
>= VIDEO_PICTURE_QUEUE_SIZE
&&
1269 !is
->videoq
.abort_request
) {
1270 SDL_CondWait(is
->pictq_cond
, is
->pictq_mutex
);
1272 SDL_UnlockMutex(is
->pictq_mutex
);
1274 if (is
->videoq
.abort_request
)
1277 vp
= &is
->pictq
[is
->pictq_windex
];
1279 /* alloc or resize hardware picture buffer */
1281 vp
->width
!= is
->video_st
->codec
->width
||
1282 vp
->height
!= is
->video_st
->codec
->height
) {
1287 /* the allocation must be done in the main thread to avoid
1289 event
.type
= FF_ALLOC_EVENT
;
1290 event
.user
.data1
= is
;
1291 SDL_PushEvent(&event
);
1293 /* wait until the picture is allocated */
1294 SDL_LockMutex(is
->pictq_mutex
);
1295 while (!vp
->allocated
&& !is
->videoq
.abort_request
) {
1296 SDL_CondWait(is
->pictq_cond
, is
->pictq_mutex
);
1298 SDL_UnlockMutex(is
->pictq_mutex
);
1300 if (is
->videoq
.abort_request
)
1304 /* if the frame is not skipped, then display it */
1308 /* get a pointer on the bitmap */
1309 SDL_LockYUVOverlay (vp
->bmp
);
1311 dst_pix_fmt
= PIX_FMT_YUV420P
;
1312 memset(&pict
,0,sizeof(AVPicture
));
1313 pict
.data
[0] = vp
->bmp
->pixels
[0];
1314 pict
.data
[1] = vp
->bmp
->pixels
[2];
1315 pict
.data
[2] = vp
->bmp
->pixels
[1];
1317 pict
.linesize
[0] = vp
->bmp
->pitches
[0];
1318 pict
.linesize
[1] = vp
->bmp
->pitches
[2];
1319 pict
.linesize
[2] = vp
->bmp
->pitches
[1];
1320 sws_flags
= av_get_int(sws_opts
, "sws_flags", NULL
);
1321 is
->img_convert_ctx
= sws_getCachedContext(is
->img_convert_ctx
,
1322 is
->video_st
->codec
->width
, is
->video_st
->codec
->height
,
1323 is
->video_st
->codec
->pix_fmt
,
1324 is
->video_st
->codec
->width
, is
->video_st
->codec
->height
,
1325 dst_pix_fmt
, sws_flags
, NULL
, NULL
, NULL
);
1326 if (is
->img_convert_ctx
== NULL
) {
1327 fprintf(stderr
, "Cannot initialize the conversion context\n");
1330 sws_scale(is
->img_convert_ctx
, src_frame
->data
, src_frame
->linesize
,
1331 0, is
->video_st
->codec
->height
, pict
.data
, pict
.linesize
);
1332 /* update the bitmap content */
1333 SDL_UnlockYUVOverlay(vp
->bmp
);
1338 /* now we can update the picture count */
1339 if (++is
->pictq_windex
== VIDEO_PICTURE_QUEUE_SIZE
)
1340 is
->pictq_windex
= 0;
1341 SDL_LockMutex(is
->pictq_mutex
);
1343 //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
1344 vp
->timer_id
= schedule_refresh(is
, (int)(compute_frame_delay(vp
->pts
, is
) * 1000 + 0.5));
1345 SDL_UnlockMutex(is
->pictq_mutex
);
1351 * compute the exact PTS for the picture if it is omitted in the stream
1352 * @param pts1 the dts of the pkt / pts of the frame
1354 static int output_picture2(VideoState
*is
, AVFrame
*src_frame
, double pts1
, int64_t pos
)
1356 double frame_delay
, pts
;
1361 /* update video clock with pts, if present */
1362 is
->video_clock
= pts
;
1364 pts
= is
->video_clock
;
1366 /* update video clock for next frame */
1367 frame_delay
= av_q2d(is
->video_st
->codec
->time_base
);
1368 /* for MPEG2, the frame can be repeated, so we update the
1369 clock accordingly */
1370 frame_delay
+= src_frame
->repeat_pict
* (frame_delay
* 0.5);
1371 is
->video_clock
+= frame_delay
;
1373 #if defined(DEBUG_SYNC) && 0
1376 if (src_frame
->pict_type
== FF_B_TYPE
)
1378 else if (src_frame
->pict_type
== FF_I_TYPE
)
1382 printf("frame_type=%c clock=%0.3f pts=%0.3f\n",
1386 return queue_picture(is
, src_frame
, pts
, pos
);
1389 static int video_thread(void *arg
)
1391 VideoState
*is
= arg
;
1392 AVPacket pkt1
, *pkt
= &pkt1
;
1393 int len1
, got_picture
, i
;
1394 AVFrame
*frame
= avcodec_alloc_frame();
1398 while (is
->paused
&& !is
->videoq
.abort_request
) {
1401 if (packet_queue_get(&is
->videoq
, pkt
, 1) < 0)
1404 if(pkt
->data
== flush_pkt
.data
){
1405 avcodec_flush_buffers(is
->video_st
->codec
);
1407 SDL_LockMutex(is
->pictq_mutex
);
1408 //Make sure there are no long delay timers (ideally we should just flush the que but thats harder)
1409 for(i
=0; i
<VIDEO_PICTURE_QUEUE_SIZE
; i
++){
1410 if(is
->pictq
[i
].timer_id
){
1411 SDL_RemoveTimer(is
->pictq
[i
].timer_id
);
1412 is
->pictq
[i
].timer_id
=0;
1413 schedule_refresh(is
, 1);
1416 while (is
->pictq_size
&& !is
->videoq
.abort_request
) {
1417 SDL_CondWait(is
->pictq_cond
, is
->pictq_mutex
);
1419 is
->video_current_pos
= -1;
1420 SDL_UnlockMutex(is
->pictq_mutex
);
1422 is
->last_dts_for_fault_detection
=
1423 is
->last_pts_for_fault_detection
= INT64_MIN
;
1424 is
->frame_last_pts
= AV_NOPTS_VALUE
;
1425 is
->frame_last_delay
= 0;
1426 is
->frame_timer
= (double)av_gettime() / 1000000.0;
1431 /* NOTE: ipts is the PTS of the _first_ picture beginning in
1432 this packet, if any */
1433 is
->video_st
->codec
->reordered_opaque
= pkt
->pts
;
1434 len1
= avcodec_decode_video2(is
->video_st
->codec
,
1435 frame
, &got_picture
,
1439 if(pkt
->dts
!= AV_NOPTS_VALUE
){
1440 is
->faulty_dts
+= pkt
->dts
<= is
->last_dts_for_fault_detection
;
1441 is
->last_dts_for_fault_detection
= pkt
->dts
;
1443 if(frame
->reordered_opaque
!= AV_NOPTS_VALUE
){
1444 is
->faulty_pts
+= frame
->reordered_opaque
<= is
->last_pts_for_fault_detection
;
1445 is
->last_pts_for_fault_detection
= frame
->reordered_opaque
;
1449 if( ( decoder_reorder_pts
==1
1450 || (decoder_reorder_pts
&& is
->faulty_pts
<is
->faulty_dts
)
1451 || pkt
->dts
== AV_NOPTS_VALUE
)
1452 && frame
->reordered_opaque
!= AV_NOPTS_VALUE
)
1453 pts
= frame
->reordered_opaque
;
1454 else if(pkt
->dts
!= AV_NOPTS_VALUE
)
1458 pts
*= av_q2d(is
->video_st
->time_base
);
1463 if (output_picture2(is
, frame
, pts
, pkt
->pos
) < 0)
1466 av_free_packet(pkt
);
1469 stream_pause(cur_stream
);
1476 static int subtitle_thread(void *arg
)
1478 VideoState
*is
= arg
;
1480 AVPacket pkt1
, *pkt
= &pkt1
;
1481 int len1
, got_subtitle
;
1484 int r
, g
, b
, y
, u
, v
, a
;
1487 while (is
->paused
&& !is
->subtitleq
.abort_request
) {
1490 if (packet_queue_get(&is
->subtitleq
, pkt
, 1) < 0)
1493 if(pkt
->data
== flush_pkt
.data
){
1494 avcodec_flush_buffers(is
->subtitle_st
->codec
);
1497 SDL_LockMutex(is
->subpq_mutex
);
1498 while (is
->subpq_size
>= SUBPICTURE_QUEUE_SIZE
&&
1499 !is
->subtitleq
.abort_request
) {
1500 SDL_CondWait(is
->subpq_cond
, is
->subpq_mutex
);
1502 SDL_UnlockMutex(is
->subpq_mutex
);
1504 if (is
->subtitleq
.abort_request
)
1507 sp
= &is
->subpq
[is
->subpq_windex
];
1509 /* NOTE: ipts is the PTS of the _first_ picture beginning in
1510 this packet, if any */
1512 if (pkt
->pts
!= AV_NOPTS_VALUE
)
1513 pts
= av_q2d(is
->subtitle_st
->time_base
)*pkt
->pts
;
1515 len1
= avcodec_decode_subtitle2(is
->subtitle_st
->codec
,
1516 &sp
->sub
, &got_subtitle
,
1520 if (got_subtitle
&& sp
->sub
.format
== 0) {
1523 for (i
= 0; i
< sp
->sub
.num_rects
; i
++)
1525 for (j
= 0; j
< sp
->sub
.rects
[i
]->nb_colors
; j
++)
1527 RGBA_IN(r
, g
, b
, a
, (uint32_t*)sp
->sub
.rects
[i
]->pict
.data
[1] + j
);
1528 y
= RGB_TO_Y_CCIR(r
, g
, b
);
1529 u
= RGB_TO_U_CCIR(r
, g
, b
, 0);
1530 v
= RGB_TO_V_CCIR(r
, g
, b
, 0);
1531 YUVA_OUT((uint32_t*)sp
->sub
.rects
[i
]->pict
.data
[1] + j
, y
, u
, v
, a
);
1535 /* now we can update the picture count */
1536 if (++is
->subpq_windex
== SUBPICTURE_QUEUE_SIZE
)
1537 is
->subpq_windex
= 0;
1538 SDL_LockMutex(is
->subpq_mutex
);
1540 SDL_UnlockMutex(is
->subpq_mutex
);
1542 av_free_packet(pkt
);
1545 // stream_pause(cur_stream);
1551 /* copy samples for viewing in editor window */
1552 static void update_sample_display(VideoState
*is
, short *samples
, int samples_size
)
1554 int size
, len
, channels
;
1556 channels
= is
->audio_st
->codec
->channels
;
1558 size
= samples_size
/ sizeof(short);
1560 len
= SAMPLE_ARRAY_SIZE
- is
->sample_array_index
;
1563 memcpy(is
->sample_array
+ is
->sample_array_index
, samples
, len
* sizeof(short));
1565 is
->sample_array_index
+= len
;
1566 if (is
->sample_array_index
>= SAMPLE_ARRAY_SIZE
)
1567 is
->sample_array_index
= 0;
1572 /* return the new audio buffer size (samples can be added or deleted
1573 to get better sync if video or external master clock) */
1574 static int synchronize_audio(VideoState
*is
, short *samples
,
1575 int samples_size1
, double pts
)
1577 int n
, samples_size
;
1580 n
= 2 * is
->audio_st
->codec
->channels
;
1581 samples_size
= samples_size1
;
1583 /* if not master, then we try to remove or add samples to correct the clock */
1584 if (((is
->av_sync_type
== AV_SYNC_VIDEO_MASTER
&& is
->video_st
) ||
1585 is
->av_sync_type
== AV_SYNC_EXTERNAL_CLOCK
)) {
1586 double diff
, avg_diff
;
1587 int wanted_size
, min_size
, max_size
, nb_samples
;
1589 ref_clock
= get_master_clock(is
);
1590 diff
= get_audio_clock(is
) - ref_clock
;
1592 if (diff
< AV_NOSYNC_THRESHOLD
) {
1593 is
->audio_diff_cum
= diff
+ is
->audio_diff_avg_coef
* is
->audio_diff_cum
;
1594 if (is
->audio_diff_avg_count
< AUDIO_DIFF_AVG_NB
) {
1595 /* not enough measures to have a correct estimate */
1596 is
->audio_diff_avg_count
++;
1598 /* estimate the A-V difference */
1599 avg_diff
= is
->audio_diff_cum
* (1.0 - is
->audio_diff_avg_coef
);
1601 if (fabs(avg_diff
) >= is
->audio_diff_threshold
) {
1602 wanted_size
= samples_size
+ ((int)(diff
* is
->audio_st
->codec
->sample_rate
) * n
);
1603 nb_samples
= samples_size
/ n
;
1605 min_size
= ((nb_samples
* (100 - SAMPLE_CORRECTION_PERCENT_MAX
)) / 100) * n
;
1606 max_size
= ((nb_samples
* (100 + SAMPLE_CORRECTION_PERCENT_MAX
)) / 100) * n
;
1607 if (wanted_size
< min_size
)
1608 wanted_size
= min_size
;
1609 else if (wanted_size
> max_size
)
1610 wanted_size
= max_size
;
1612 /* add or remove samples to correction the synchro */
1613 if (wanted_size
< samples_size
) {
1614 /* remove samples */
1615 samples_size
= wanted_size
;
1616 } else if (wanted_size
> samples_size
) {
1617 uint8_t *samples_end
, *q
;
1621 nb
= (samples_size
- wanted_size
);
1622 samples_end
= (uint8_t *)samples
+ samples_size
- n
;
1623 q
= samples_end
+ n
;
1625 memcpy(q
, samples_end
, n
);
1629 samples_size
= wanted_size
;
1633 printf("diff=%f adiff=%f sample_diff=%d apts=%0.3f vpts=%0.3f %f\n",
1634 diff
, avg_diff
, samples_size
- samples_size1
,
1635 is
->audio_clock
, is
->video_clock
, is
->audio_diff_threshold
);
1639 /* too big difference : may be initial PTS errors, so
1641 is
->audio_diff_avg_count
= 0;
1642 is
->audio_diff_cum
= 0;
1646 return samples_size
;
1649 /* decode one audio frame and returns its uncompressed size */
1650 static int audio_decode_frame(VideoState
*is
, double *pts_ptr
)
1652 AVPacket
*pkt_temp
= &is
->audio_pkt_temp
;
1653 AVPacket
*pkt
= &is
->audio_pkt
;
1654 AVCodecContext
*dec
= is
->audio_st
->codec
;
1655 int n
, len1
, data_size
;
1659 /* NOTE: the audio packet can contain several frames */
1660 while (pkt_temp
->size
> 0) {
1661 data_size
= sizeof(is
->audio_buf1
);
1662 len1
= avcodec_decode_audio3(dec
,
1663 (int16_t *)is
->audio_buf1
, &data_size
,
1666 /* if error, we skip the frame */
1671 pkt_temp
->data
+= len1
;
1672 pkt_temp
->size
-= len1
;
1676 if (dec
->sample_fmt
!= is
->audio_src_fmt
) {
1677 if (is
->reformat_ctx
)
1678 av_audio_convert_free(is
->reformat_ctx
);
1679 is
->reformat_ctx
= av_audio_convert_alloc(SAMPLE_FMT_S16
, 1,
1680 dec
->sample_fmt
, 1, NULL
, 0);
1681 if (!is
->reformat_ctx
) {
1682 fprintf(stderr
, "Cannot convert %s sample format to %s sample format\n",
1683 avcodec_get_sample_fmt_name(dec
->sample_fmt
),
1684 avcodec_get_sample_fmt_name(SAMPLE_FMT_S16
));
1687 is
->audio_src_fmt
= dec
->sample_fmt
;
1690 if (is
->reformat_ctx
) {
1691 const void *ibuf
[6]= {is
->audio_buf1
};
1692 void *obuf
[6]= {is
->audio_buf2
};
1693 int istride
[6]= {av_get_bits_per_sample_format(dec
->sample_fmt
)/8};
1694 int ostride
[6]= {2};
1695 int len
= data_size
/istride
[0];
1696 if (av_audio_convert(is
->reformat_ctx
, obuf
, ostride
, ibuf
, istride
, len
)<0) {
1697 printf("av_audio_convert() failed\n");
1700 is
->audio_buf
= is
->audio_buf2
;
1701 /* FIXME: existing code assume that data_size equals framesize*channels*2
1702 remove this legacy cruft */
1705 is
->audio_buf
= is
->audio_buf1
;
1708 /* if no pts, then compute it */
1709 pts
= is
->audio_clock
;
1711 n
= 2 * dec
->channels
;
1712 is
->audio_clock
+= (double)data_size
/
1713 (double)(n
* dec
->sample_rate
);
1714 #if defined(DEBUG_SYNC)
1716 static double last_clock
;
1717 printf("audio: delay=%0.3f clock=%0.3f pts=%0.3f\n",
1718 is
->audio_clock
- last_clock
,
1719 is
->audio_clock
, pts
);
1720 last_clock
= is
->audio_clock
;
1726 /* free the current packet */
1728 av_free_packet(pkt
);
1730 if (is
->paused
|| is
->audioq
.abort_request
) {
1734 /* read next packet */
1735 if (packet_queue_get(&is
->audioq
, pkt
, 1) < 0)
1737 if(pkt
->data
== flush_pkt
.data
){
1738 avcodec_flush_buffers(dec
);
1742 pkt_temp
->data
= pkt
->data
;
1743 pkt_temp
->size
= pkt
->size
;
1745 /* if update the audio clock with the pts */
1746 if (pkt
->pts
!= AV_NOPTS_VALUE
) {
1747 is
->audio_clock
= av_q2d(is
->audio_st
->time_base
)*pkt
->pts
;
1752 /* get the current audio output buffer size, in samples. With SDL, we
1753 cannot have a precise information */
1754 static int audio_write_get_buf_size(VideoState
*is
)
1756 return is
->audio_buf_size
- is
->audio_buf_index
;
1760 /* prepare a new audio buffer */
1761 static void sdl_audio_callback(void *opaque
, Uint8
*stream
, int len
)
1763 VideoState
*is
= opaque
;
1764 int audio_size
, len1
;
1767 audio_callback_time
= av_gettime();
1770 if (is
->audio_buf_index
>= is
->audio_buf_size
) {
1771 audio_size
= audio_decode_frame(is
, &pts
);
1772 if (audio_size
< 0) {
1773 /* if error, just output silence */
1774 is
->audio_buf
= is
->audio_buf1
;
1775 is
->audio_buf_size
= 1024;
1776 memset(is
->audio_buf
, 0, is
->audio_buf_size
);
1779 update_sample_display(is
, (int16_t *)is
->audio_buf
, audio_size
);
1780 audio_size
= synchronize_audio(is
, (int16_t *)is
->audio_buf
, audio_size
,
1782 is
->audio_buf_size
= audio_size
;
1784 is
->audio_buf_index
= 0;
1786 len1
= is
->audio_buf_size
- is
->audio_buf_index
;
1789 memcpy(stream
, (uint8_t *)is
->audio_buf
+ is
->audio_buf_index
, len1
);
1792 is
->audio_buf_index
+= len1
;
1796 /* open a given stream. Return 0 if OK */
1797 static int stream_component_open(VideoState
*is
, int stream_index
)
1799 AVFormatContext
*ic
= is
->ic
;
1800 AVCodecContext
*avctx
;
1802 SDL_AudioSpec wanted_spec
, spec
;
1804 if (stream_index
< 0 || stream_index
>= ic
->nb_streams
)
1806 avctx
= ic
->streams
[stream_index
]->codec
;
1808 /* prepare audio output */
1809 if (avctx
->codec_type
== CODEC_TYPE_AUDIO
) {
1810 if (avctx
->channels
> 0) {
1811 avctx
->request_channels
= FFMIN(2, avctx
->channels
);
1813 avctx
->request_channels
= 2;
1817 codec
= avcodec_find_decoder(avctx
->codec_id
);
1818 avctx
->debug_mv
= debug_mv
;
1819 avctx
->debug
= debug
;
1820 avctx
->workaround_bugs
= workaround_bugs
;
1821 avctx
->lowres
= lowres
;
1822 if(lowres
) avctx
->flags
|= CODEC_FLAG_EMU_EDGE
;
1823 avctx
->idct_algo
= idct
;
1824 if(fast
) avctx
->flags2
|= CODEC_FLAG2_FAST
;
1825 avctx
->skip_frame
= skip_frame
;
1826 avctx
->skip_idct
= skip_idct
;
1827 avctx
->skip_loop_filter
= skip_loop_filter
;
1828 avctx
->error_recognition
= error_recognition
;
1829 avctx
->error_concealment
= error_concealment
;
1830 avcodec_thread_init(avctx
, thread_count
);
1832 set_context_opts(avctx
, avcodec_opts
[avctx
->codec_type
], 0);
1835 avcodec_open(avctx
, codec
) < 0)
1838 /* prepare audio output */
1839 if (avctx
->codec_type
== CODEC_TYPE_AUDIO
) {
1840 wanted_spec
.freq
= avctx
->sample_rate
;
1841 wanted_spec
.format
= AUDIO_S16SYS
;
1842 wanted_spec
.channels
= avctx
->channels
;
1843 wanted_spec
.silence
= 0;
1844 wanted_spec
.samples
= SDL_AUDIO_BUFFER_SIZE
;
1845 wanted_spec
.callback
= sdl_audio_callback
;
1846 wanted_spec
.userdata
= is
;
1847 if (SDL_OpenAudio(&wanted_spec
, &spec
) < 0) {
1848 fprintf(stderr
, "SDL_OpenAudio: %s\n", SDL_GetError());
1851 is
->audio_hw_buf_size
= spec
.size
;
1852 is
->audio_src_fmt
= SAMPLE_FMT_S16
;
1855 ic
->streams
[stream_index
]->discard
= AVDISCARD_DEFAULT
;
1856 switch(avctx
->codec_type
) {
1857 case CODEC_TYPE_AUDIO
:
1858 is
->audio_stream
= stream_index
;
1859 is
->audio_st
= ic
->streams
[stream_index
];
1860 is
->audio_buf_size
= 0;
1861 is
->audio_buf_index
= 0;
1863 /* init averaging filter */
1864 is
->audio_diff_avg_coef
= exp(log(0.01) / AUDIO_DIFF_AVG_NB
);
1865 is
->audio_diff_avg_count
= 0;
1866 /* since we do not have a precise anough audio fifo fullness,
1867 we correct audio sync only if larger than this threshold */
1868 is
->audio_diff_threshold
= 2.0 * SDL_AUDIO_BUFFER_SIZE
/ avctx
->sample_rate
;
1870 memset(&is
->audio_pkt
, 0, sizeof(is
->audio_pkt
));
1871 packet_queue_init(&is
->audioq
);
1874 case CODEC_TYPE_VIDEO
:
1875 is
->video_stream
= stream_index
;
1876 is
->video_st
= ic
->streams
[stream_index
];
1878 // is->video_current_pts_time = av_gettime();
1880 packet_queue_init(&is
->videoq
);
1881 is
->video_tid
= SDL_CreateThread(video_thread
, is
);
1883 case CODEC_TYPE_SUBTITLE
:
1884 is
->subtitle_stream
= stream_index
;
1885 is
->subtitle_st
= ic
->streams
[stream_index
];
1886 packet_queue_init(&is
->subtitleq
);
1888 is
->subtitle_tid
= SDL_CreateThread(subtitle_thread
, is
);
1896 static void stream_component_close(VideoState
*is
, int stream_index
)
1898 AVFormatContext
*ic
= is
->ic
;
1899 AVCodecContext
*avctx
;
1901 if (stream_index
< 0 || stream_index
>= ic
->nb_streams
)
1903 avctx
= ic
->streams
[stream_index
]->codec
;
1905 switch(avctx
->codec_type
) {
1906 case CODEC_TYPE_AUDIO
:
1907 packet_queue_abort(&is
->audioq
);
1911 packet_queue_end(&is
->audioq
);
1912 if (is
->reformat_ctx
)
1913 av_audio_convert_free(is
->reformat_ctx
);
1914 is
->reformat_ctx
= NULL
;
1916 case CODEC_TYPE_VIDEO
:
1917 packet_queue_abort(&is
->videoq
);
1919 /* note: we also signal this mutex to make sure we deblock the
1920 video thread in all cases */
1921 SDL_LockMutex(is
->pictq_mutex
);
1922 SDL_CondSignal(is
->pictq_cond
);
1923 SDL_UnlockMutex(is
->pictq_mutex
);
1925 SDL_WaitThread(is
->video_tid
, NULL
);
1927 packet_queue_end(&is
->videoq
);
1929 case CODEC_TYPE_SUBTITLE
:
1930 packet_queue_abort(&is
->subtitleq
);
1932 /* note: we also signal this mutex to make sure we deblock the
1933 video thread in all cases */
1934 SDL_LockMutex(is
->subpq_mutex
);
1935 is
->subtitle_stream_changed
= 1;
1937 SDL_CondSignal(is
->subpq_cond
);
1938 SDL_UnlockMutex(is
->subpq_mutex
);
1940 SDL_WaitThread(is
->subtitle_tid
, NULL
);
1942 packet_queue_end(&is
->subtitleq
);
1948 ic
->streams
[stream_index
]->discard
= AVDISCARD_ALL
;
1949 avcodec_close(avctx
);
1950 switch(avctx
->codec_type
) {
1951 case CODEC_TYPE_AUDIO
:
1952 is
->audio_st
= NULL
;
1953 is
->audio_stream
= -1;
1955 case CODEC_TYPE_VIDEO
:
1956 is
->video_st
= NULL
;
1957 is
->video_stream
= -1;
1959 case CODEC_TYPE_SUBTITLE
:
1960 is
->subtitle_st
= NULL
;
1961 is
->subtitle_stream
= -1;
1968 /* since we have only one decoding thread, we can use a global
1969 variable instead of a thread local variable */
1970 static VideoState
*global_video_state
;
1972 static int decode_interrupt_cb(void)
1974 return (global_video_state
&& global_video_state
->abort_request
);
1977 /* this thread gets the stream from the disk or the network */
1978 static int decode_thread(void *arg
)
1980 VideoState
*is
= arg
;
1981 AVFormatContext
*ic
;
1983 int st_index
[CODEC_TYPE_NB
];
1984 int st_count
[CODEC_TYPE_NB
]={0};
1985 int st_best_packet_count
[CODEC_TYPE_NB
];
1986 AVPacket pkt1
, *pkt
= &pkt1
;
1987 AVFormatParameters params
, *ap
= ¶ms
;
1990 ic
= avformat_alloc_context();
1992 memset(st_index
, -1, sizeof(st_index
));
1993 memset(st_best_packet_count
, -1, sizeof(st_best_packet_count
));
1994 is
->video_stream
= -1;
1995 is
->audio_stream
= -1;
1996 is
->subtitle_stream
= -1;
1998 global_video_state
= is
;
1999 url_set_interrupt_cb(decode_interrupt_cb
);
2001 memset(ap
, 0, sizeof(*ap
));
2003 ap
->prealloced_context
= 1;
2004 ap
->width
= frame_width
;
2005 ap
->height
= frame_height
;
2006 ap
->time_base
= (AVRational
){1, 25};
2007 ap
->pix_fmt
= frame_pix_fmt
;
2009 set_context_opts(ic
, avformat_opts
, AV_OPT_FLAG_DECODING_PARAM
);
2011 err
= av_open_input_file(&ic
, is
->filename
, is
->iformat
, 0, ap
);
2013 print_error(is
->filename
, err
);
2020 ic
->flags
|= AVFMT_FLAG_GENPTS
;
2022 err
= av_find_stream_info(ic
);
2024 fprintf(stderr
, "%s: could not find codec parameters\n", is
->filename
);
2029 ic
->pb
->eof_reached
= 0; //FIXME hack, ffplay maybe should not use url_feof() to test for the end
2032 seek_by_bytes
= !!(ic
->iformat
->flags
& AVFMT_TS_DISCONT
);
2034 /* if seeking requested, we execute it */
2035 if (start_time
!= AV_NOPTS_VALUE
) {
2038 timestamp
= start_time
;
2039 /* add the stream start time */
2040 if (ic
->start_time
!= AV_NOPTS_VALUE
)
2041 timestamp
+= ic
->start_time
;
2042 ret
= avformat_seek_file(ic
, -1, INT64_MIN
, timestamp
, INT64_MAX
, 0);
2044 fprintf(stderr
, "%s: could not seek to position %0.3f\n",
2045 is
->filename
, (double)timestamp
/ AV_TIME_BASE
);
2049 for(i
= 0; i
< ic
->nb_streams
; i
++) {
2050 AVStream
*st
= ic
->streams
[i
];
2051 AVCodecContext
*avctx
= st
->codec
;
2052 ic
->streams
[i
]->discard
= AVDISCARD_ALL
;
2053 if(avctx
->codec_type
>= (unsigned)CODEC_TYPE_NB
)
2055 if(st_count
[avctx
->codec_type
]++ != wanted_stream
[avctx
->codec_type
] && wanted_stream
[avctx
->codec_type
] >= 0)
2058 if(st_best_packet_count
[avctx
->codec_type
] >= st
->codec_info_nb_frames
)
2060 st_best_packet_count
[avctx
->codec_type
]= st
->codec_info_nb_frames
;
2062 switch(avctx
->codec_type
) {
2063 case CODEC_TYPE_AUDIO
:
2065 st_index
[CODEC_TYPE_AUDIO
] = i
;
2067 case CODEC_TYPE_VIDEO
:
2068 case CODEC_TYPE_SUBTITLE
:
2070 st_index
[avctx
->codec_type
] = i
;
2077 dump_format(ic
, 0, is
->filename
, 0);
2080 /* open the streams */
2081 if (st_index
[CODEC_TYPE_AUDIO
] >= 0) {
2082 stream_component_open(is
, st_index
[CODEC_TYPE_AUDIO
]);
2086 if (st_index
[CODEC_TYPE_VIDEO
] >= 0) {
2087 ret
= stream_component_open(is
, st_index
[CODEC_TYPE_VIDEO
]);
2090 /* add the refresh timer to draw the picture */
2091 schedule_refresh(is
, 40);
2093 if (!display_disable
)
2097 if (st_index
[CODEC_TYPE_SUBTITLE
] >= 0) {
2098 stream_component_open(is
, st_index
[CODEC_TYPE_SUBTITLE
]);
2101 if (is
->video_stream
< 0 && is
->audio_stream
< 0) {
2102 fprintf(stderr
, "%s: could not open codecs\n", is
->filename
);
2108 if (is
->abort_request
)
2110 if (is
->paused
!= is
->last_paused
) {
2111 is
->last_paused
= is
->paused
;
2113 is
->read_pause_return
= av_read_pause(ic
);
2117 #if CONFIG_RTSP_DEMUXER
2118 if (is
->paused
&& !strcmp(ic
->iformat
->name
, "rtsp")) {
2119 /* wait 10 ms to avoid trying to get another packet */
2126 int64_t seek_target
= is
->seek_pos
;
2127 int64_t seek_min
= is
->seek_rel
> 0 ? seek_target
- is
->seek_rel
+ 2: INT64_MIN
;
2128 int64_t seek_max
= is
->seek_rel
< 0 ? seek_target
- is
->seek_rel
- 2: INT64_MAX
;
2129 //FIXME the +-2 is due to rounding being not done in the correct direction in generation
2130 // of the seek_pos/seek_rel variables
2132 ret
= avformat_seek_file(is
->ic
, -1, seek_min
, seek_target
, seek_max
, is
->seek_flags
);
2134 fprintf(stderr
, "%s: error while seeking\n", is
->ic
->filename
);
2136 if (is
->audio_stream
>= 0) {
2137 packet_queue_flush(&is
->audioq
);
2138 packet_queue_put(&is
->audioq
, &flush_pkt
);
2140 if (is
->subtitle_stream
>= 0) {
2141 packet_queue_flush(&is
->subtitleq
);
2142 packet_queue_put(&is
->subtitleq
, &flush_pkt
);
2144 if (is
->video_stream
>= 0) {
2145 packet_queue_flush(&is
->videoq
);
2146 packet_queue_put(&is
->videoq
, &flush_pkt
);
2153 /* if the queue are full, no need to read more */
2154 if ( is
->audioq
.size
+ is
->videoq
.size
+ is
->subtitleq
.size
> MAX_QUEUE_SIZE
2155 || ( (is
->audioq
.size
> MIN_AUDIOQ_SIZE
|| is
->audio_stream
<0)
2156 && (is
->videoq
.nb_packets
> MIN_FRAMES
|| is
->video_stream
<0)
2157 && (is
->subtitleq
.nb_packets
> MIN_FRAMES
|| is
->subtitle_stream
<0))) {
2162 if(url_feof(ic
->pb
) || eof
) {
2163 if(is
->video_stream
>= 0){
2164 av_init_packet(pkt
);
2167 pkt
->stream_index
= is
->video_stream
;
2168 packet_queue_put(&is
->videoq
, pkt
);
2171 if(autoexit
&& is
->audioq
.size
+ is
->videoq
.size
+ is
->subtitleq
.size
==0){
2177 ret
= av_read_frame(ic
, pkt
);
2179 if (ret
== AVERROR_EOF
)
2181 if (url_ferror(ic
->pb
))
2183 SDL_Delay(100); /* wait for user event */
2186 if (pkt
->stream_index
== is
->audio_stream
) {
2187 packet_queue_put(&is
->audioq
, pkt
);
2188 } else if (pkt
->stream_index
== is
->video_stream
) {
2189 packet_queue_put(&is
->videoq
, pkt
);
2190 } else if (pkt
->stream_index
== is
->subtitle_stream
) {
2191 packet_queue_put(&is
->subtitleq
, pkt
);
2193 av_free_packet(pkt
);
2196 /* wait until the end */
2197 while (!is
->abort_request
) {
2203 /* disable interrupting */
2204 global_video_state
= NULL
;
2206 /* close each stream */
2207 if (is
->audio_stream
>= 0)
2208 stream_component_close(is
, is
->audio_stream
);
2209 if (is
->video_stream
>= 0)
2210 stream_component_close(is
, is
->video_stream
);
2211 if (is
->subtitle_stream
>= 0)
2212 stream_component_close(is
, is
->subtitle_stream
);
2214 av_close_input_file(is
->ic
);
2215 is
->ic
= NULL
; /* safety */
2217 url_set_interrupt_cb(NULL
);
2222 event
.type
= FF_QUIT_EVENT
;
2223 event
.user
.data1
= is
;
2224 SDL_PushEvent(&event
);
2229 static VideoState
*stream_open(const char *filename
, AVInputFormat
*iformat
)
2233 is
= av_mallocz(sizeof(VideoState
));
2236 av_strlcpy(is
->filename
, filename
, sizeof(is
->filename
));
2237 is
->iformat
= iformat
;
2241 /* start video display */
2242 is
->pictq_mutex
= SDL_CreateMutex();
2243 is
->pictq_cond
= SDL_CreateCond();
2245 is
->subpq_mutex
= SDL_CreateMutex();
2246 is
->subpq_cond
= SDL_CreateCond();
2248 is
->av_sync_type
= av_sync_type
;
2249 is
->parse_tid
= SDL_CreateThread(decode_thread
, is
);
2250 if (!is
->parse_tid
) {
2257 static void stream_close(VideoState
*is
)
2261 /* XXX: use a special url_shutdown call to abort parse cleanly */
2262 is
->abort_request
= 1;
2263 SDL_WaitThread(is
->parse_tid
, NULL
);
2265 /* free all pictures */
2266 for(i
=0;i
<VIDEO_PICTURE_QUEUE_SIZE
; i
++) {
2269 SDL_FreeYUVOverlay(vp
->bmp
);
2273 SDL_DestroyMutex(is
->pictq_mutex
);
2274 SDL_DestroyCond(is
->pictq_cond
);
2275 SDL_DestroyMutex(is
->subpq_mutex
);
2276 SDL_DestroyCond(is
->subpq_cond
);
2277 if (is
->img_convert_ctx
)
2278 sws_freeContext(is
->img_convert_ctx
);
2282 static void stream_cycle_channel(VideoState
*is
, int codec_type
)
2284 AVFormatContext
*ic
= is
->ic
;
2285 int start_index
, stream_index
;
2288 if (codec_type
== CODEC_TYPE_VIDEO
)
2289 start_index
= is
->video_stream
;
2290 else if (codec_type
== CODEC_TYPE_AUDIO
)
2291 start_index
= is
->audio_stream
;
2293 start_index
= is
->subtitle_stream
;
2294 if (start_index
< (codec_type
== CODEC_TYPE_SUBTITLE ?
-1 : 0))
2296 stream_index
= start_index
;
2298 if (++stream_index
>= is
->ic
->nb_streams
)
2300 if (codec_type
== CODEC_TYPE_SUBTITLE
)
2307 if (stream_index
== start_index
)
2309 st
= ic
->streams
[stream_index
];
2310 if (st
->codec
->codec_type
== codec_type
) {
2311 /* check that parameters are OK */
2312 switch(codec_type
) {
2313 case CODEC_TYPE_AUDIO
:
2314 if (st
->codec
->sample_rate
!= 0 &&
2315 st
->codec
->channels
!= 0)
2318 case CODEC_TYPE_VIDEO
:
2319 case CODEC_TYPE_SUBTITLE
:
2327 stream_component_close(is
, start_index
);
2328 stream_component_open(is
, stream_index
);
2332 static void toggle_full_screen(void)
2334 is_full_screen
= !is_full_screen
;
2335 if (!fs_screen_width
) {
2336 /* use default SDL method */
2337 // SDL_WM_ToggleFullScreen(screen);
2339 video_open(cur_stream
);
2342 static void toggle_pause(void)
2345 stream_pause(cur_stream
);
2349 static void step_to_next_frame(void)
2352 /* if the stream is paused unpause it, then step */
2353 if (cur_stream
->paused
)
2354 stream_pause(cur_stream
);
2359 static void do_exit(void)
2363 stream_close(cur_stream
);
2366 for (i
= 0; i
< CODEC_TYPE_NB
; i
++)
2367 av_free(avcodec_opts
[i
]);
2368 av_free(avformat_opts
);
2376 static void toggle_audio_display(void)
2379 int bgcolor
= SDL_MapRGB(screen
->format
, 0x00, 0x00, 0x00);
2380 cur_stream
->show_audio
= (cur_stream
->show_audio
+ 1) % 3;
2381 fill_rectangle(screen
,
2382 cur_stream
->xleft
, cur_stream
->ytop
, cur_stream
->width
, cur_stream
->height
,
2384 SDL_UpdateRect(screen
, cur_stream
->xleft
, cur_stream
->ytop
, cur_stream
->width
, cur_stream
->height
);
2388 /* handle an event sent by the GUI */
2389 static void event_loop(void)
2392 double incr
, pos
, frac
;
2396 SDL_WaitEvent(&event
);
2397 switch(event
.type
) {
2399 switch(event
.key
.keysym
.sym
) {
2405 toggle_full_screen();
2411 case SDLK_s
: //S: Step to next frame
2412 step_to_next_frame();
2416 stream_cycle_channel(cur_stream
, CODEC_TYPE_AUDIO
);
2420 stream_cycle_channel(cur_stream
, CODEC_TYPE_VIDEO
);
2424 stream_cycle_channel(cur_stream
, CODEC_TYPE_SUBTITLE
);
2427 toggle_audio_display();
2442 if (seek_by_bytes
) {
2443 if (cur_stream
->video_stream
>= 0 && cur_stream
->video_current_pos
>=0){
2444 pos
= cur_stream
->video_current_pos
;
2445 }else if(cur_stream
->audio_stream
>= 0 && cur_stream
->audio_pkt
.pos
>=0){
2446 pos
= cur_stream
->audio_pkt
.pos
;
2448 pos
= url_ftell(cur_stream
->ic
->pb
);
2449 if (cur_stream
->ic
->bit_rate
)
2450 incr
*= cur_stream
->ic
->bit_rate
/ 8.0;
2454 stream_seek(cur_stream
, pos
, incr
, 1);
2456 pos
= get_master_clock(cur_stream
);
2458 stream_seek(cur_stream
, (int64_t)(pos
* AV_TIME_BASE
), (int64_t)(incr
* AV_TIME_BASE
), 0);
2466 case SDL_MOUSEBUTTONDOWN
:
2467 case SDL_MOUSEMOTION
:
2468 if(event
.type
==SDL_MOUSEBUTTONDOWN
){
2471 if(event
.motion
.state
!= SDL_PRESSED
)
2476 if(seek_by_bytes
|| cur_stream
->ic
->duration
<=0){
2477 uint64_t size
= url_fsize(cur_stream
->ic
->pb
);
2478 stream_seek(cur_stream
, size
*x
/cur_stream
->width
, 0, 1);
2482 int tns
, thh
, tmm
, tss
;
2483 tns
= cur_stream
->ic
->duration
/1000000LL;
2485 tmm
= (tns
%3600)/60;
2487 frac
= x
/cur_stream
->width
;
2492 fprintf(stderr
, "Seek to %2.0f%% (%2d:%02d:%02d) of total duration (%2d:%02d:%02d) \n", frac
*100,
2493 hh
, mm
, ss
, thh
, tmm
, tss
);
2494 ts
= frac
*cur_stream
->ic
->duration
;
2495 if (cur_stream
->ic
->start_time
!= AV_NOPTS_VALUE
)
2496 ts
+= cur_stream
->ic
->start_time
;
2497 stream_seek(cur_stream
, ts
, 0, 0);
2501 case SDL_VIDEORESIZE
:
2503 screen
= SDL_SetVideoMode(event
.resize
.w
, event
.resize
.h
, 0,
2504 SDL_HWSURFACE
|SDL_RESIZABLE
|SDL_ASYNCBLIT
|SDL_HWACCEL
);
2505 screen_width
= cur_stream
->width
= event
.resize
.w
;
2506 screen_height
= cur_stream
->height
= event
.resize
.h
;
2513 case FF_ALLOC_EVENT
:
2514 video_open(event
.user
.data1
);
2515 alloc_picture(event
.user
.data1
);
2517 case FF_REFRESH_EVENT
:
2518 video_refresh_timer(event
.user
.data1
);
2526 static void opt_frame_size(const char *arg
)
2528 if (av_parse_video_frame_size(&frame_width
, &frame_height
, arg
) < 0) {
2529 fprintf(stderr
, "Incorrect frame size\n");
2532 if ((frame_width
% 2) != 0 || (frame_height
% 2) != 0) {
2533 fprintf(stderr
, "Frame size must be a multiple of 2\n");
2538 static int opt_width(const char *opt
, const char *arg
)
2540 screen_width
= parse_number_or_die(opt
, arg
, OPT_INT64
, 1, INT_MAX
);
2544 static int opt_height(const char *opt
, const char *arg
)
2546 screen_height
= parse_number_or_die(opt
, arg
, OPT_INT64
, 1, INT_MAX
);
2550 static void opt_format(const char *arg
)
2552 file_iformat
= av_find_input_format(arg
);
2553 if (!file_iformat
) {
2554 fprintf(stderr
, "Unknown input format: %s\n", arg
);
2559 static void opt_frame_pix_fmt(const char *arg
)
2561 frame_pix_fmt
= av_get_pix_fmt(arg
);
2564 static int opt_sync(const char *opt
, const char *arg
)
2566 if (!strcmp(arg
, "audio"))
2567 av_sync_type
= AV_SYNC_AUDIO_MASTER
;
2568 else if (!strcmp(arg
, "video"))
2569 av_sync_type
= AV_SYNC_VIDEO_MASTER
;
2570 else if (!strcmp(arg
, "ext"))
2571 av_sync_type
= AV_SYNC_EXTERNAL_CLOCK
;
2573 fprintf(stderr
, "Unknown value for %s: %s\n", opt
, arg
);
2579 static int opt_seek(const char *opt
, const char *arg
)
2581 start_time
= parse_time_or_die(opt
, arg
, 1);
2585 static int opt_debug(const char *opt
, const char *arg
)
2587 av_log_set_level(99);
2588 debug
= parse_number_or_die(opt
, arg
, OPT_INT64
, 0, INT_MAX
);
2592 static int opt_vismv(const char *opt
, const char *arg
)
2594 debug_mv
= parse_number_or_die(opt
, arg
, OPT_INT64
, INT_MIN
, INT_MAX
);
2598 static int opt_thread_count(const char *opt
, const char *arg
)
2600 thread_count
= parse_number_or_die(opt
, arg
, OPT_INT64
, 0, INT_MAX
);
2602 fprintf(stderr
, "Warning: not compiled with thread support, using thread emulation\n");
2607 static const OptionDef options
[] = {
2608 #include "cmdutils_common_opts.h"
2609 { "x", HAS_ARG
| OPT_FUNC2
, {(void*)opt_width
}, "force displayed width", "width" },
2610 { "y", HAS_ARG
| OPT_FUNC2
, {(void*)opt_height
}, "force displayed height", "height" },
2611 { "s", HAS_ARG
| OPT_VIDEO
, {(void*)opt_frame_size
}, "set frame size (WxH or abbreviation)", "size" },
2612 { "fs", OPT_BOOL
, {(void*)&is_full_screen
}, "force full screen" },
2613 { "an", OPT_BOOL
, {(void*)&audio_disable
}, "disable audio" },
2614 { "vn", OPT_BOOL
, {(void*)&video_disable
}, "disable video" },
2615 { "ast", OPT_INT
| HAS_ARG
| OPT_EXPERT
, {(void*)&wanted_stream
[CODEC_TYPE_AUDIO
]}, "select desired audio stream", "stream_number" },
2616 { "vst", OPT_INT
| HAS_ARG
| OPT_EXPERT
, {(void*)&wanted_stream
[CODEC_TYPE_VIDEO
]}, "select desired video stream", "stream_number" },
2617 { "sst", OPT_INT
| HAS_ARG
| OPT_EXPERT
, {(void*)&wanted_stream
[CODEC_TYPE_SUBTITLE
]}, "select desired subtitle stream", "stream_number" },
2618 { "ss", HAS_ARG
| OPT_FUNC2
, {(void*)&opt_seek
}, "seek to a given position in seconds", "pos" },
2619 { "bytes", OPT_INT
| HAS_ARG
, {(void*)&seek_by_bytes
}, "seek by bytes 0=off 1=on -1=auto", "val" },
2620 { "nodisp", OPT_BOOL
, {(void*)&display_disable
}, "disable graphical display" },
2621 { "f", HAS_ARG
, {(void*)opt_format
}, "force format", "fmt" },
2622 { "pix_fmt", HAS_ARG
| OPT_EXPERT
| OPT_VIDEO
, {(void*)opt_frame_pix_fmt
}, "set pixel format", "format" },
2623 { "stats", OPT_BOOL
| OPT_EXPERT
, {(void*)&show_status
}, "show status", "" },
2624 { "debug", HAS_ARG
| OPT_FUNC2
| OPT_EXPERT
, {(void*)opt_debug
}, "print specific debug info", "" },
2625 { "bug", OPT_INT
| HAS_ARG
| OPT_EXPERT
, {(void*)&workaround_bugs
}, "workaround bugs", "" },
2626 { "vismv", HAS_ARG
| OPT_FUNC2
| OPT_EXPERT
, {(void*)opt_vismv
}, "visualize motion vectors", "" },
2627 { "fast", OPT_BOOL
| OPT_EXPERT
, {(void*)&fast
}, "non spec compliant optimizations", "" },
2628 { "genpts", OPT_BOOL
| OPT_EXPERT
, {(void*)&genpts
}, "generate pts", "" },
2629 { "drp", OPT_INT
| HAS_ARG
| OPT_EXPERT
, {(void*)&decoder_reorder_pts
}, "let decoder reorder pts 0=off 1=on -1=auto", ""},
2630 { "lowres", OPT_INT
| HAS_ARG
| OPT_EXPERT
, {(void*)&lowres
}, "", "" },
2631 { "skiploop", OPT_INT
| HAS_ARG
| OPT_EXPERT
, {(void*)&skip_loop_filter
}, "", "" },
2632 { "skipframe", OPT_INT
| HAS_ARG
| OPT_EXPERT
, {(void*)&skip_frame
}, "", "" },
2633 { "skipidct", OPT_INT
| HAS_ARG
| OPT_EXPERT
, {(void*)&skip_idct
}, "", "" },
2634 { "idct", OPT_INT
| HAS_ARG
| OPT_EXPERT
, {(void*)&idct
}, "set idct algo", "algo" },
2635 { "er", OPT_INT
| HAS_ARG
| OPT_EXPERT
, {(void*)&error_recognition
}, "set error detection threshold (0-4)", "threshold" },
2636 { "ec", OPT_INT
| HAS_ARG
| OPT_EXPERT
, {(void*)&error_concealment
}, "set error concealment options", "bit_mask" },
2637 { "sync", HAS_ARG
| OPT_FUNC2
| OPT_EXPERT
, {(void*)opt_sync
}, "set audio-video sync. type (type=audio/video/ext)", "type" },
2638 { "threads", HAS_ARG
| OPT_FUNC2
| OPT_EXPERT
, {(void*)opt_thread_count
}, "thread count", "count" },
2639 { "autoexit", OPT_BOOL
| OPT_EXPERT
, {(void*)&autoexit
}, "exit at the end", "" },
2640 { "default", OPT_FUNC2
| HAS_ARG
| OPT_AUDIO
| OPT_VIDEO
| OPT_EXPERT
, {(void*)opt_default
}, "generic catch all option", "" },
2644 static void show_usage(void)
2646 printf("Simple media player\n");
2647 printf("usage: ffplay [options] input_file\n");
2651 static void show_help(void)
2654 show_help_options(options
, "Main options:\n",
2656 show_help_options(options
, "\nAdvanced options:\n",
2657 OPT_EXPERT
, OPT_EXPERT
);
2658 printf("\nWhile playing:\n"
2660 "f toggle full screen\n"
2662 "a cycle audio channel\n"
2663 "v cycle video channel\n"
2664 "t cycle subtitle channel\n"
2665 "w show audio waves\n"
2666 "left/right seek backward/forward 10 seconds\n"
2667 "down/up seek backward/forward 1 minute\n"
2668 "mouse click seek to percentage in file corresponding to fraction of width\n"
2672 static void opt_input_file(const char *filename
)
2674 if (input_filename
) {
2675 fprintf(stderr
, "Argument '%s' provided as input filename, but '%s' was already specified.\n",
2676 filename
, input_filename
);
2679 if (!strcmp(filename
, "-"))
2681 input_filename
= filename
;
2684 /* Called from the main */
2685 int main(int argc
, char **argv
)
2689 /* register all codecs, demux and protocols */
2690 avcodec_register_all();
2691 avdevice_register_all();
2694 for(i
=0; i
<CODEC_TYPE_NB
; i
++){
2695 avcodec_opts
[i
]= avcodec_alloc_context2(i
);
2697 avformat_opts
= avformat_alloc_context();
2698 sws_opts
= sws_getContext(16,16,0, 16,16,0, sws_flags
, NULL
,NULL
,NULL
);
2702 parse_options(argc
, argv
, options
, opt_input_file
);
2704 if (!input_filename
) {
2706 fprintf(stderr
, "An input file must be specified\n");
2707 fprintf(stderr
, "Use -h to get full help or, even better, run 'man ffplay'\n");
2711 if (display_disable
) {
2714 flags
= SDL_INIT_VIDEO
| SDL_INIT_AUDIO
| SDL_INIT_TIMER
;
2715 #if !defined(__MINGW32__) && !defined(__APPLE__)
2716 flags
|= SDL_INIT_EVENTTHREAD
; /* Not supported on Windows or Mac OS X */
2718 if (SDL_Init (flags
)) {
2719 fprintf(stderr
, "Could not initialize SDL - %s\n", SDL_GetError());
2723 if (!display_disable
) {
2724 #if HAVE_SDL_VIDEO_SIZE
2725 const SDL_VideoInfo
*vi
= SDL_GetVideoInfo();
2726 fs_screen_width
= vi
->current_w
;
2727 fs_screen_height
= vi
->current_h
;
2731 SDL_EventState(SDL_ACTIVEEVENT
, SDL_IGNORE
);
2732 SDL_EventState(SDL_SYSWMEVENT
, SDL_IGNORE
);
2733 SDL_EventState(SDL_USEREVENT
, SDL_IGNORE
);
2735 av_init_packet(&flush_pkt
);
2736 flush_pkt
.data
= "FLUSH";
2738 cur_stream
= stream_open(input_filename
, file_iformat
);