2 * FFplay : Simple Media Player based on the ffmpeg libraries
3 * Copyright (c) 2003 Fabrice Bellard
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #define HAVE_AV_CONFIG_H
25 #include <SDL_thread.h>
28 #undef main /* We don't want SDL to override our main() */
41 DosGetInfoBlocks(&tib
, &pib
);
43 // Change flag from VIO to PM:
44 if (pib
->pib_ultype
==2) pib
->pib_ultype
= 3;
48 #if defined(__linux__)
58 #define MAX_VIDEOQ_SIZE (5 * 256 * 1024)
59 #define MAX_AUDIOQ_SIZE (5 * 16 * 1024)
61 /* SDL audio buffer size, in samples. Should be small to have precise
62 A/V sync as SDL does not have hardware buffer fullness info. */
63 #define SDL_AUDIO_BUFFER_SIZE 1024
65 /* no AV sync correction is done if below the AV sync threshold */
66 #define AV_SYNC_THRESHOLD 0.01
67 /* no AV correction is done if too big error */
68 #define AV_NOSYNC_THRESHOLD 10.0
70 /* maximum audio speed change to get correct sync */
71 #define SAMPLE_CORRECTION_PERCENT_MAX 10
73 /* we use about AUDIO_DIFF_AVG_NB A-V differences to make the average */
74 #define AUDIO_DIFF_AVG_NB 20
76 /* NOTE: the size must be big enough to compensate the hardware audio buffersize size */
77 #define SAMPLE_ARRAY_SIZE (2*65536)
79 typedef struct PacketQueue
{
80 AVPacketList
*first_pkt
, *last_pkt
;
88 #define VIDEO_PICTURE_QUEUE_SIZE 1
90 typedef struct VideoPicture
{
91 double pts
; /* presentation time stamp for this picture */
93 int width
, height
; /* source height & width */
98 AV_SYNC_AUDIO_MASTER
, /* default choice */
100 AV_SYNC_EXTERNAL_CLOCK
, /* synchronize to an external clock */
103 typedef struct VideoState
{
104 SDL_Thread
*parse_tid
;
105 SDL_Thread
*video_tid
;
106 AVInputFormat
*iformat
;
115 int dtg_active_format
;
120 double external_clock
; /* external clock base */
121 int64_t external_clock_time
;
124 double audio_diff_cum
; /* used for AV difference average computation */
125 double audio_diff_avg_coef
;
126 double audio_diff_threshold
;
127 int audio_diff_avg_count
;
130 int audio_hw_buf_size
;
131 /* samples output by the codec. we reserve more space for avsync
133 uint8_t audio_buf
[(AVCODEC_MAX_AUDIO_FRAME_SIZE
* 3) / 2];
134 unsigned int audio_buf_size
; /* in bytes */
135 int audio_buf_index
; /* in bytes */
137 uint8_t *audio_pkt_data
;
140 int show_audio
; /* if true, display audio samples */
141 int16_t sample_array
[SAMPLE_ARRAY_SIZE
];
142 int sample_array_index
;
146 double frame_last_pts
;
147 double frame_last_delay
;
152 double video_last_P_pts
; /* pts of the last P picture (needed if B
153 frames are present) */
154 double video_current_pts
; /* current displayed pts (different from
155 video_clock if frame fifos are used) */
156 int64_t video_current_pts_time
; /* time at which we updated
157 video_current_pts - used to
158 have running video pts */
159 VideoPicture pictq
[VIDEO_PICTURE_QUEUE_SIZE
];
160 int pictq_size
, pictq_rindex
, pictq_windex
;
161 SDL_mutex
*pictq_mutex
;
162 SDL_cond
*pictq_cond
;
164 SDL_mutex
*video_decoder_mutex
;
165 SDL_mutex
*audio_decoder_mutex
;
167 // QETimer *video_timer;
169 int width
, height
, xleft
, ytop
;
172 void show_help(void);
173 static int audio_write_get_buf_size(VideoState
*is
);
175 /* options specified by the user */
176 static AVInputFormat
*file_iformat
;
177 static AVImageFormat
*image_format
;
178 static const char *input_filename
;
179 static int fs_screen_width
;
180 static int fs_screen_height
;
181 static int screen_width
= 640;
182 static int screen_height
= 480;
183 static int audio_disable
;
184 static int video_disable
;
185 static int display_disable
;
186 static int show_status
;
187 static int av_sync_type
= AV_SYNC_AUDIO_MASTER
;
188 static int64_t start_time
= AV_NOPTS_VALUE
;
189 static int debug
= 0;
190 static int debug_mv
= 0;
192 static int thread_count
= 1;
193 static int workaround_bugs
= 1;
195 static int lowres
= 0;
196 static int idct
= FF_IDCT_AUTO
;
197 static enum AVDiscard skip_frame
= AVDISCARD_DEFAULT
;
198 static enum AVDiscard skip_idct
= AVDISCARD_DEFAULT
;
199 static enum AVDiscard skip_loop_filter
= AVDISCARD_DEFAULT
;
200 static int error_resilience
= FF_ER_CAREFUL
;
201 static int error_concealment
= 3;
203 /* current context */
204 static int is_full_screen
;
205 static VideoState
*cur_stream
;
206 static int64_t audio_callback_time
;
208 #define FF_ALLOC_EVENT (SDL_USEREVENT)
209 #define FF_REFRESH_EVENT (SDL_USEREVENT + 1)
210 #define FF_QUIT_EVENT (SDL_USEREVENT + 2)
214 /* packet queue handling */
215 static void packet_queue_init(PacketQueue
*q
)
217 memset(q
, 0, sizeof(PacketQueue
));
218 q
->mutex
= SDL_CreateMutex();
219 q
->cond
= SDL_CreateCond();
222 static void packet_queue_flush(PacketQueue
*q
)
224 AVPacketList
*pkt
, *pkt1
;
226 SDL_LockMutex(q
->mutex
);
227 for(pkt
= q
->first_pkt
; pkt
!= NULL
; pkt
= pkt1
) {
229 av_free_packet(&pkt
->pkt
);
236 SDL_UnlockMutex(q
->mutex
);
239 static void packet_queue_end(PacketQueue
*q
)
241 packet_queue_flush(q
);
242 SDL_DestroyMutex(q
->mutex
);
243 SDL_DestroyCond(q
->cond
);
246 static int packet_queue_put(PacketQueue
*q
, AVPacket
*pkt
)
250 /* duplicate the packet */
251 if (av_dup_packet(pkt
) < 0)
254 pkt1
= av_malloc(sizeof(AVPacketList
));
261 SDL_LockMutex(q
->mutex
);
267 q
->last_pkt
->next
= pkt1
;
270 q
->size
+= pkt1
->pkt
.size
;
271 /* XXX: should duplicate packet data in DV case */
272 SDL_CondSignal(q
->cond
);
274 SDL_UnlockMutex(q
->mutex
);
278 static void packet_queue_abort(PacketQueue
*q
)
280 SDL_LockMutex(q
->mutex
);
282 q
->abort_request
= 1;
284 SDL_CondSignal(q
->cond
);
286 SDL_UnlockMutex(q
->mutex
);
289 /* return < 0 if aborted, 0 if no packet and > 0 if packet. */
290 static int packet_queue_get(PacketQueue
*q
, AVPacket
*pkt
, int block
)
295 SDL_LockMutex(q
->mutex
);
298 if (q
->abort_request
) {
305 q
->first_pkt
= pkt1
->next
;
309 q
->size
-= pkt1
->pkt
.size
;
318 SDL_CondWait(q
->cond
, q
->mutex
);
321 SDL_UnlockMutex(q
->mutex
);
325 static inline void fill_rectangle(SDL_Surface
*screen
,
326 int x
, int y
, int w
, int h
, int color
)
333 SDL_FillRect(screen
, &rect
, color
);
337 /* draw only the border of a rectangle */
338 void fill_border(VideoState
*s
, int x
, int y
, int w
, int h
, int color
)
342 /* fill the background */
346 w2
= s
->width
- (x
+ w
);
352 h2
= s
->height
- (y
+ h
);
355 fill_rectangle(screen
,
359 fill_rectangle(screen
,
360 s
->xleft
+ s
->width
- w2
, s
->ytop
,
363 fill_rectangle(screen
,
364 s
->xleft
+ w1
, s
->ytop
,
365 s
->width
- w1
- w2
, h1
,
367 fill_rectangle(screen
,
368 s
->xleft
+ w1
, s
->ytop
+ s
->height
- h2
,
369 s
->width
- w1
- w2
, h2
,
374 static void video_image_display(VideoState
*is
)
378 int width
, height
, x
, y
;
381 vp
= &is
->pictq
[is
->pictq_rindex
];
383 /* XXX: use variable in the frame */
384 if (is
->video_st
->codec
->sample_aspect_ratio
.num
== 0)
387 aspect_ratio
= av_q2d(is
->video_st
->codec
->sample_aspect_ratio
)
388 * is
->video_st
->codec
->width
/ is
->video_st
->codec
->height
;;
389 if (aspect_ratio
<= 0.0)
390 aspect_ratio
= (float)is
->video_st
->codec
->width
/
391 (float)is
->video_st
->codec
->height
;
392 /* if an active format is indicated, then it overrides the
395 if (is
->video_st
->codec
->dtg_active_format
!= is
->dtg_active_format
) {
396 is
->dtg_active_format
= is
->video_st
->codec
->dtg_active_format
;
397 printf("dtg_active_format=%d\n", is
->dtg_active_format
);
401 switch(is
->video_st
->codec
->dtg_active_format
) {
402 case FF_DTG_AFD_SAME
:
407 aspect_ratio
= 4.0 / 3.0;
409 case FF_DTG_AFD_16_9
:
410 aspect_ratio
= 16.0 / 9.0;
412 case FF_DTG_AFD_14_9
:
413 aspect_ratio
= 14.0 / 9.0;
415 case FF_DTG_AFD_4_3_SP_14_9
:
416 aspect_ratio
= 14.0 / 9.0;
418 case FF_DTG_AFD_16_9_SP_14_9
:
419 aspect_ratio
= 14.0 / 9.0;
421 case FF_DTG_AFD_SP_4_3
:
422 aspect_ratio
= 4.0 / 3.0;
427 /* XXX: we suppose the screen has a 1.0 pixel ratio */
429 width
= ((int)rint(height
* aspect_ratio
)) & -3;
430 if (width
> is
->width
) {
432 height
= ((int)rint(width
/ aspect_ratio
)) & -3;
434 x
= (is
->width
- width
) / 2;
435 y
= (is
->height
- height
) / 2;
436 if (!is
->no_background
) {
437 /* fill the background */
438 // fill_border(is, x, y, width, height, QERGB(0x00, 0x00, 0x00));
440 is
->no_background
= 0;
442 rect
.x
= is
->xleft
+ x
;
443 rect
.y
= is
->xleft
+ y
;
446 SDL_DisplayYUVOverlay(vp
->bmp
, &rect
);
449 fill_rectangle(screen
,
450 is
->xleft
, is
->ytop
, is
->width
, is
->height
,
451 QERGB(0x00, 0x00, 0x00));
456 static inline int compute_mod(int a
, int b
)
465 static void video_audio_display(VideoState
*s
)
467 int i
, i_start
, x
, y1
, y
, ys
, delay
, n
, nb_display_channels
;
468 int ch
, channels
, h
, h2
, bgcolor
, fgcolor
;
471 /* compute display index : center on currently output samples */
472 channels
= s
->audio_st
->codec
->channels
;
473 nb_display_channels
= channels
;
476 delay
= audio_write_get_buf_size(s
);
479 /* to be more precise, we take into account the time spent since
480 the last buffer computation */
481 if (audio_callback_time
) {
482 time_diff
= av_gettime() - audio_callback_time
;
483 delay
+= (time_diff
* s
->audio_st
->codec
->sample_rate
) / 1000000;
486 delay
-= s
->width
/ 2;
487 if (delay
< s
->width
)
489 i_start
= compute_mod(s
->sample_array_index
- delay
* channels
, SAMPLE_ARRAY_SIZE
);
490 s
->last_i_start
= i_start
;
492 i_start
= s
->last_i_start
;
495 bgcolor
= SDL_MapRGB(screen
->format
, 0x00, 0x00, 0x00);
496 fill_rectangle(screen
,
497 s
->xleft
, s
->ytop
, s
->width
, s
->height
,
500 fgcolor
= SDL_MapRGB(screen
->format
, 0xff, 0xff, 0xff);
502 /* total height for one channel */
503 h
= s
->height
/ nb_display_channels
;
504 /* graph height / 2 */
506 for(ch
= 0;ch
< nb_display_channels
; ch
++) {
508 y1
= s
->ytop
+ ch
* h
+ (h
/ 2); /* position of center line */
509 for(x
= 0; x
< s
->width
; x
++) {
510 y
= (s
->sample_array
[i
] * h2
) >> 15;
517 fill_rectangle(screen
,
518 s
->xleft
+ x
, ys
, 1, y
,
521 if (i
>= SAMPLE_ARRAY_SIZE
)
522 i
-= SAMPLE_ARRAY_SIZE
;
526 fgcolor
= SDL_MapRGB(screen
->format
, 0x00, 0x00, 0xff);
528 for(ch
= 1;ch
< nb_display_channels
; ch
++) {
529 y
= s
->ytop
+ ch
* h
;
530 fill_rectangle(screen
,
531 s
->xleft
, y
, s
->width
, 1,
534 SDL_UpdateRect(screen
, s
->xleft
, s
->ytop
, s
->width
, s
->height
);
537 /* display the current picture, if any */
538 static void video_display(VideoState
*is
)
540 if (is
->audio_st
&& is
->show_audio
)
541 video_audio_display(is
);
542 else if (is
->video_st
)
543 video_image_display(is
);
546 static Uint32
sdl_refresh_timer_cb(Uint32 interval
, void *opaque
)
549 event
.type
= FF_REFRESH_EVENT
;
550 event
.user
.data1
= opaque
;
551 SDL_PushEvent(&event
);
552 return 0; /* 0 means stop timer */
555 /* schedule a video refresh in 'delay' ms */
556 static void schedule_refresh(VideoState
*is
, int delay
)
558 SDL_AddTimer(delay
, sdl_refresh_timer_cb
, is
);
561 /* get the current audio clock value */
562 static double get_audio_clock(VideoState
*is
)
565 int hw_buf_size
, bytes_per_sec
;
566 pts
= is
->audio_clock
;
567 hw_buf_size
= audio_write_get_buf_size(is
);
570 bytes_per_sec
= is
->audio_st
->codec
->sample_rate
*
571 2 * is
->audio_st
->codec
->channels
;
574 pts
-= (double)hw_buf_size
/ bytes_per_sec
;
578 /* get the current video clock value */
579 static double get_video_clock(VideoState
*is
)
585 delta
= (av_gettime() - is
->video_current_pts_time
) / 1000000.0;
587 return is
->video_current_pts
+ delta
;
590 /* get the current external clock value */
591 static double get_external_clock(VideoState
*is
)
595 return is
->external_clock
+ ((ti
- is
->external_clock_time
) * 1e-6);
598 /* get the current master clock value */
599 static double get_master_clock(VideoState
*is
)
603 if (is
->av_sync_type
== AV_SYNC_VIDEO_MASTER
) {
605 val
= get_video_clock(is
);
607 val
= get_audio_clock(is
);
608 } else if (is
->av_sync_type
== AV_SYNC_AUDIO_MASTER
) {
610 val
= get_audio_clock(is
);
612 val
= get_video_clock(is
);
614 val
= get_external_clock(is
);
619 /* seek in the stream */
620 static void stream_seek(VideoState
*is
, int64_t pos
, int rel
)
624 is
->seek_flags
= rel
< 0 ? AVSEEK_FLAG_BACKWARD
: 0;
629 /* pause or resume the video */
630 static void stream_pause(VideoState
*is
)
632 is
->paused
= !is
->paused
;
634 is
->video_current_pts
= get_video_clock(is
);
638 /* called to display each frame */
639 static void video_refresh_timer(void *opaque
)
641 VideoState
*is
= opaque
;
643 double actual_delay
, delay
, sync_threshold
, ref_clock
, diff
;
647 if (is
->pictq_size
== 0) {
648 /* if no picture, need to wait */
649 schedule_refresh(is
, 1);
651 /* dequeue the picture */
652 vp
= &is
->pictq
[is
->pictq_rindex
];
654 /* update current video pts */
655 is
->video_current_pts
= vp
->pts
;
656 is
->video_current_pts_time
= av_gettime();
658 /* compute nominal delay */
659 delay
= vp
->pts
- is
->frame_last_pts
;
660 if (delay
<= 0 || delay
>= 1.0) {
661 /* if incorrect delay, use previous one */
662 delay
= is
->frame_last_delay
;
664 is
->frame_last_delay
= delay
;
665 is
->frame_last_pts
= vp
->pts
;
667 /* update delay to follow master synchronisation source */
668 if (((is
->av_sync_type
== AV_SYNC_AUDIO_MASTER
&& is
->audio_st
) ||
669 is
->av_sync_type
== AV_SYNC_EXTERNAL_CLOCK
)) {
670 /* if video is slave, we try to correct big delays by
671 duplicating or deleting a frame */
672 ref_clock
= get_master_clock(is
);
673 diff
= vp
->pts
- ref_clock
;
675 /* skip or repeat frame. We take into account the
676 delay to compute the threshold. I still don't know
677 if it is the best guess */
678 sync_threshold
= AV_SYNC_THRESHOLD
;
679 if (delay
> sync_threshold
)
680 sync_threshold
= delay
;
681 if (fabs(diff
) < AV_NOSYNC_THRESHOLD
) {
682 if (diff
<= -sync_threshold
)
684 else if (diff
>= sync_threshold
)
689 is
->frame_timer
+= delay
;
690 /* compute the REAL delay (we need to do that to avoid
692 actual_delay
= is
->frame_timer
- (av_gettime() / 1000000.0);
693 if (actual_delay
< 0.010) {
694 /* XXX: should skip picture */
695 actual_delay
= 0.010;
697 /* launch timer for next picture */
698 schedule_refresh(is
, (int)(actual_delay
* 1000 + 0.5));
700 #if defined(DEBUG_SYNC)
701 printf("video: delay=%0.3f actual_delay=%0.3f pts=%0.3f A-V=%f\n",
702 delay
, actual_delay
, vp
->pts
, -diff
);
705 /* display picture */
708 /* update queue size and signal for next picture */
709 if (++is
->pictq_rindex
== VIDEO_PICTURE_QUEUE_SIZE
)
710 is
->pictq_rindex
= 0;
712 SDL_LockMutex(is
->pictq_mutex
);
714 SDL_CondSignal(is
->pictq_cond
);
715 SDL_UnlockMutex(is
->pictq_mutex
);
717 } else if (is
->audio_st
) {
718 /* draw the next audio frame */
720 schedule_refresh(is
, 40);
722 /* if only audio stream, then display the audio bars (better
723 than nothing, just to test the implementation */
725 /* display picture */
728 schedule_refresh(is
, 100);
731 static int64_t last_time
;
736 cur_time
= av_gettime();
737 if (!last_time
|| (cur_time
- last_time
) >= 500 * 1000) {
741 aqsize
= is
->audioq
.size
;
743 vqsize
= is
->videoq
.size
;
745 if (is
->audio_st
&& is
->video_st
)
746 av_diff
= get_audio_clock(is
) - get_video_clock(is
);
747 printf("%7.2f A-V:%7.3f aq=%5dKB vq=%5dKB \r",
748 get_master_clock(is
), av_diff
, aqsize
/ 1024, vqsize
/ 1024);
750 last_time
= cur_time
;
755 /* allocate a picture (needs to do that in main thread to avoid
756 potential locking problems */
757 static void alloc_picture(void *opaque
)
759 VideoState
*is
= opaque
;
762 vp
= &is
->pictq
[is
->pictq_windex
];
765 SDL_FreeYUVOverlay(vp
->bmp
);
768 /* XXX: use generic function */
769 /* XXX: disable overlay if no hardware acceleration or if RGB format */
770 switch(is
->video_st
->codec
->pix_fmt
) {
771 case PIX_FMT_YUV420P
:
772 case PIX_FMT_YUV422P
:
773 case PIX_FMT_YUV444P
:
775 case PIX_FMT_YUV410P
:
776 case PIX_FMT_YUV411P
:
784 vp
->bmp
= SDL_CreateYUVOverlay(is
->video_st
->codec
->width
,
785 is
->video_st
->codec
->height
,
788 vp
->width
= is
->video_st
->codec
->width
;
789 vp
->height
= is
->video_st
->codec
->height
;
791 SDL_LockMutex(is
->pictq_mutex
);
793 SDL_CondSignal(is
->pictq_cond
);
794 SDL_UnlockMutex(is
->pictq_mutex
);
797 static int queue_picture(VideoState
*is
, AVFrame
*src_frame
, double pts
)
803 /* wait until we have space to put a new picture */
804 SDL_LockMutex(is
->pictq_mutex
);
805 while (is
->pictq_size
>= VIDEO_PICTURE_QUEUE_SIZE
&&
806 !is
->videoq
.abort_request
) {
807 SDL_CondWait(is
->pictq_cond
, is
->pictq_mutex
);
809 SDL_UnlockMutex(is
->pictq_mutex
);
811 if (is
->videoq
.abort_request
)
814 vp
= &is
->pictq
[is
->pictq_windex
];
816 /* alloc or resize hardware picture buffer */
818 vp
->width
!= is
->video_st
->codec
->width
||
819 vp
->height
!= is
->video_st
->codec
->height
) {
824 /* the allocation must be done in the main thread to avoid
826 event
.type
= FF_ALLOC_EVENT
;
827 event
.user
.data1
= is
;
828 SDL_PushEvent(&event
);
830 /* wait until the picture is allocated */
831 SDL_LockMutex(is
->pictq_mutex
);
832 while (!vp
->allocated
&& !is
->videoq
.abort_request
) {
833 SDL_CondWait(is
->pictq_cond
, is
->pictq_mutex
);
835 SDL_UnlockMutex(is
->pictq_mutex
);
837 if (is
->videoq
.abort_request
)
841 /* if the frame is not skipped, then display it */
843 /* get a pointer on the bitmap */
844 SDL_LockYUVOverlay (vp
->bmp
);
846 dst_pix_fmt
= PIX_FMT_YUV420P
;
847 pict
.data
[0] = vp
->bmp
->pixels
[0];
848 pict
.data
[1] = vp
->bmp
->pixels
[2];
849 pict
.data
[2] = vp
->bmp
->pixels
[1];
851 pict
.linesize
[0] = vp
->bmp
->pitches
[0];
852 pict
.linesize
[1] = vp
->bmp
->pitches
[2];
853 pict
.linesize
[2] = vp
->bmp
->pitches
[1];
854 img_convert(&pict
, dst_pix_fmt
,
855 (AVPicture
*)src_frame
, is
->video_st
->codec
->pix_fmt
,
856 is
->video_st
->codec
->width
, is
->video_st
->codec
->height
);
857 /* update the bitmap content */
858 SDL_UnlockYUVOverlay(vp
->bmp
);
862 /* now we can update the picture count */
863 if (++is
->pictq_windex
== VIDEO_PICTURE_QUEUE_SIZE
)
864 is
->pictq_windex
= 0;
865 SDL_LockMutex(is
->pictq_mutex
);
867 SDL_UnlockMutex(is
->pictq_mutex
);
872 /* compute the exact PTS for the picture if it is omitted in the stream */
873 static int output_picture2(VideoState
*is
, AVFrame
*src_frame
, double pts1
)
875 double frame_delay
, pts
;
880 /* update video clock with pts, if present */
881 is
->video_clock
= pts
;
883 pts
= is
->video_clock
;
885 /* update video clock for next frame */
886 frame_delay
= av_q2d(is
->video_st
->codec
->time_base
);
887 /* for MPEG2, the frame can be repeated, so we update the
889 if (src_frame
->repeat_pict
) {
890 frame_delay
+= src_frame
->repeat_pict
* (frame_delay
* 0.5);
892 is
->video_clock
+= frame_delay
;
894 #if defined(DEBUG_SYNC) && 0
897 if (src_frame
->pict_type
== FF_B_TYPE
)
899 else if (src_frame
->pict_type
== FF_I_TYPE
)
903 printf("frame_type=%c clock=%0.3f pts=%0.3f\n",
907 return queue_picture(is
, src_frame
, pts
);
910 static int video_thread(void *arg
)
912 VideoState
*is
= arg
;
913 AVPacket pkt1
, *pkt
= &pkt1
;
914 int len1
, got_picture
;
915 AVFrame
*frame
= avcodec_alloc_frame();
919 while (is
->paused
&& !is
->videoq
.abort_request
) {
922 if (packet_queue_get(&is
->videoq
, pkt
, 1) < 0)
924 /* NOTE: ipts is the PTS of the _first_ picture beginning in
925 this packet, if any */
927 if (pkt
->dts
!= AV_NOPTS_VALUE
)
928 pts
= av_q2d(is
->video_st
->time_base
)*pkt
->dts
;
930 SDL_LockMutex(is
->video_decoder_mutex
);
931 len1
= avcodec_decode_video(is
->video_st
->codec
,
933 pkt
->data
, pkt
->size
);
934 SDL_UnlockMutex(is
->video_decoder_mutex
);
938 if (output_picture2(is
, frame
, pts
) < 0)
944 stream_pause(cur_stream
);
951 /* copy samples for viewing in editor window */
952 static void update_sample_display(VideoState
*is
, short *samples
, int samples_size
)
954 int size
, len
, channels
;
956 channels
= is
->audio_st
->codec
->channels
;
958 size
= samples_size
/ sizeof(short);
960 len
= SAMPLE_ARRAY_SIZE
- is
->sample_array_index
;
963 memcpy(is
->sample_array
+ is
->sample_array_index
, samples
, len
* sizeof(short));
965 is
->sample_array_index
+= len
;
966 if (is
->sample_array_index
>= SAMPLE_ARRAY_SIZE
)
967 is
->sample_array_index
= 0;
972 /* return the new audio buffer size (samples can be added or deleted
973 to get better sync if video or external master clock) */
974 static int synchronize_audio(VideoState
*is
, short *samples
,
975 int samples_size1
, double pts
)
980 n
= 2 * is
->audio_st
->codec
->channels
;
981 samples_size
= samples_size1
;
983 /* if not master, then we try to remove or add samples to correct the clock */
984 if (((is
->av_sync_type
== AV_SYNC_VIDEO_MASTER
&& is
->video_st
) ||
985 is
->av_sync_type
== AV_SYNC_EXTERNAL_CLOCK
)) {
986 double diff
, avg_diff
;
987 int wanted_size
, min_size
, max_size
, nb_samples
;
989 ref_clock
= get_master_clock(is
);
990 diff
= get_audio_clock(is
) - ref_clock
;
992 if (diff
< AV_NOSYNC_THRESHOLD
) {
993 is
->audio_diff_cum
= diff
+ is
->audio_diff_avg_coef
* is
->audio_diff_cum
;
994 if (is
->audio_diff_avg_count
< AUDIO_DIFF_AVG_NB
) {
995 /* not enough measures to have a correct estimate */
996 is
->audio_diff_avg_count
++;
998 /* estimate the A-V difference */
999 avg_diff
= is
->audio_diff_cum
* (1.0 - is
->audio_diff_avg_coef
);
1001 if (fabs(avg_diff
) >= is
->audio_diff_threshold
) {
1002 wanted_size
= samples_size
+ ((int)(diff
* is
->audio_st
->codec
->sample_rate
) * n
);
1003 nb_samples
= samples_size
/ n
;
1005 min_size
= ((nb_samples
* (100 - SAMPLE_CORRECTION_PERCENT_MAX
)) / 100) * n
;
1006 max_size
= ((nb_samples
* (100 + SAMPLE_CORRECTION_PERCENT_MAX
)) / 100) * n
;
1007 if (wanted_size
< min_size
)
1008 wanted_size
= min_size
;
1009 else if (wanted_size
> max_size
)
1010 wanted_size
= max_size
;
1012 /* add or remove samples to correction the synchro */
1013 if (wanted_size
< samples_size
) {
1014 /* remove samples */
1015 samples_size
= wanted_size
;
1016 } else if (wanted_size
> samples_size
) {
1017 uint8_t *samples_end
, *q
;
1021 nb
= (samples_size
- wanted_size
);
1022 samples_end
= (uint8_t *)samples
+ samples_size
- n
;
1023 q
= samples_end
+ n
;
1025 memcpy(q
, samples_end
, n
);
1029 samples_size
= wanted_size
;
1033 printf("diff=%f adiff=%f sample_diff=%d apts=%0.3f vpts=%0.3f %f\n",
1034 diff
, avg_diff
, samples_size
- samples_size1
,
1035 is
->audio_clock
, is
->video_clock
, is
->audio_diff_threshold
);
1039 /* too big difference : may be initial PTS errors, so
1041 is
->audio_diff_avg_count
= 0;
1042 is
->audio_diff_cum
= 0;
1046 return samples_size
;
1049 /* decode one audio frame and returns its uncompressed size */
1050 static int audio_decode_frame(VideoState
*is
, uint8_t *audio_buf
, double *pts_ptr
)
1052 AVPacket
*pkt
= &is
->audio_pkt
;
1053 int n
, len1
, data_size
;
1057 /* NOTE: the audio packet can contain several frames */
1058 while (is
->audio_pkt_size
> 0) {
1059 SDL_LockMutex(is
->audio_decoder_mutex
);
1060 len1
= avcodec_decode_audio(is
->audio_st
->codec
,
1061 (int16_t *)audio_buf
, &data_size
,
1062 is
->audio_pkt_data
, is
->audio_pkt_size
);
1063 SDL_UnlockMutex(is
->audio_decoder_mutex
);
1065 /* if error, we skip the frame */
1066 is
->audio_pkt_size
= 0;
1070 is
->audio_pkt_data
+= len1
;
1071 is
->audio_pkt_size
-= len1
;
1074 /* if no pts, then compute it */
1075 pts
= is
->audio_clock
;
1077 n
= 2 * is
->audio_st
->codec
->channels
;
1078 is
->audio_clock
+= (double)data_size
/
1079 (double)(n
* is
->audio_st
->codec
->sample_rate
);
1080 #if defined(DEBUG_SYNC)
1082 static double last_clock
;
1083 printf("audio: delay=%0.3f clock=%0.3f pts=%0.3f\n",
1084 is
->audio_clock
- last_clock
,
1085 is
->audio_clock
, pts
);
1086 last_clock
= is
->audio_clock
;
1092 /* free the current packet */
1094 av_free_packet(pkt
);
1096 if (is
->paused
|| is
->audioq
.abort_request
) {
1100 /* read next packet */
1101 if (packet_queue_get(&is
->audioq
, pkt
, 1) < 0)
1103 is
->audio_pkt_data
= pkt
->data
;
1104 is
->audio_pkt_size
= pkt
->size
;
1106 /* if update the audio clock with the pts */
1107 if (pkt
->pts
!= AV_NOPTS_VALUE
) {
1108 is
->audio_clock
= av_q2d(is
->audio_st
->time_base
)*pkt
->pts
;
1113 /* get the current audio output buffer size, in samples. With SDL, we
1114 cannot have a precise information */
1115 static int audio_write_get_buf_size(VideoState
*is
)
1117 return is
->audio_hw_buf_size
- is
->audio_buf_index
;
1121 /* prepare a new audio buffer */
1122 void sdl_audio_callback(void *opaque
, Uint8
*stream
, int len
)
1124 VideoState
*is
= opaque
;
1125 int audio_size
, len1
;
1128 audio_callback_time
= av_gettime();
1131 if (is
->audio_buf_index
>= is
->audio_buf_size
) {
1132 audio_size
= audio_decode_frame(is
, is
->audio_buf
, &pts
);
1133 if (audio_size
< 0) {
1134 /* if error, just output silence */
1135 is
->audio_buf_size
= 1024;
1136 memset(is
->audio_buf
, 0, is
->audio_buf_size
);
1139 update_sample_display(is
, (int16_t *)is
->audio_buf
, audio_size
);
1140 audio_size
= synchronize_audio(is
, (int16_t *)is
->audio_buf
, audio_size
,
1142 is
->audio_buf_size
= audio_size
;
1144 is
->audio_buf_index
= 0;
1146 len1
= is
->audio_buf_size
- is
->audio_buf_index
;
1149 memcpy(stream
, (uint8_t *)is
->audio_buf
+ is
->audio_buf_index
, len1
);
1152 is
->audio_buf_index
+= len1
;
1157 /* open a given stream. Return 0 if OK */
1158 static int stream_component_open(VideoState
*is
, int stream_index
)
1160 AVFormatContext
*ic
= is
->ic
;
1161 AVCodecContext
*enc
;
1163 SDL_AudioSpec wanted_spec
, spec
;
1165 if (stream_index
< 0 || stream_index
>= ic
->nb_streams
)
1167 enc
= ic
->streams
[stream_index
]->codec
;
1169 /* prepare audio output */
1170 if (enc
->codec_type
== CODEC_TYPE_AUDIO
) {
1171 wanted_spec
.freq
= enc
->sample_rate
;
1172 wanted_spec
.format
= AUDIO_S16SYS
;
1173 /* hack for AC3. XXX: suppress that */
1174 if (enc
->channels
> 2)
1176 wanted_spec
.channels
= enc
->channels
;
1177 wanted_spec
.silence
= 0;
1178 wanted_spec
.samples
= SDL_AUDIO_BUFFER_SIZE
;
1179 wanted_spec
.callback
= sdl_audio_callback
;
1180 wanted_spec
.userdata
= is
;
1181 if (SDL_OpenAudio(&wanted_spec
, &spec
) < 0) {
1182 fprintf(stderr
, "SDL_OpenAudio: %s\n", SDL_GetError());
1185 is
->audio_hw_buf_size
= spec
.size
;
1188 codec
= avcodec_find_decoder(enc
->codec_id
);
1189 enc
->debug_mv
= debug_mv
;
1192 av_log_set_level(AV_LOG_DEBUG
);
1193 enc
->workaround_bugs
= workaround_bugs
;
1194 enc
->lowres
= lowres
;
1195 if(lowres
) enc
->flags
|= CODEC_FLAG_EMU_EDGE
;
1196 enc
->idct_algo
= idct
;
1197 if(fast
) enc
->flags2
|= CODEC_FLAG2_FAST
;
1198 enc
->skip_frame
= skip_frame
;
1199 enc
->skip_idct
= skip_idct
;
1200 enc
->skip_loop_filter
= skip_loop_filter
;
1201 enc
->error_resilience
= error_resilience
;
1202 enc
->error_concealment
= error_concealment
;
1204 avcodec_open(enc
, codec
) < 0)
1206 #if defined(HAVE_THREADS)
1208 avcodec_thread_init(enc
, thread_count
);
1210 enc
->thread_count
= thread_count
;
1211 switch(enc
->codec_type
) {
1212 case CODEC_TYPE_AUDIO
:
1213 is
->audio_stream
= stream_index
;
1214 is
->audio_st
= ic
->streams
[stream_index
];
1215 is
->audio_buf_size
= 0;
1216 is
->audio_buf_index
= 0;
1218 /* init averaging filter */
1219 is
->audio_diff_avg_coef
= exp(log(0.01) / AUDIO_DIFF_AVG_NB
);
1220 is
->audio_diff_avg_count
= 0;
1221 /* since we do not have a precise anough audio fifo fullness,
1222 we correct audio sync only if larger than this threshold */
1223 is
->audio_diff_threshold
= 2.0 * SDL_AUDIO_BUFFER_SIZE
/ enc
->sample_rate
;
1225 memset(&is
->audio_pkt
, 0, sizeof(is
->audio_pkt
));
1226 packet_queue_init(&is
->audioq
);
1229 case CODEC_TYPE_VIDEO
:
1230 is
->video_stream
= stream_index
;
1231 is
->video_st
= ic
->streams
[stream_index
];
1233 is
->frame_last_delay
= 40e-3;
1234 is
->frame_timer
= (double)av_gettime() / 1000000.0;
1235 is
->video_current_pts_time
= av_gettime();
1237 packet_queue_init(&is
->videoq
);
1238 is
->video_tid
= SDL_CreateThread(video_thread
, is
);
1246 static void stream_component_close(VideoState
*is
, int stream_index
)
1248 AVFormatContext
*ic
= is
->ic
;
1249 AVCodecContext
*enc
;
1251 enc
= ic
->streams
[stream_index
]->codec
;
1253 switch(enc
->codec_type
) {
1254 case CODEC_TYPE_AUDIO
:
1255 packet_queue_abort(&is
->audioq
);
1259 packet_queue_end(&is
->audioq
);
1261 case CODEC_TYPE_VIDEO
:
1262 packet_queue_abort(&is
->videoq
);
1264 /* note: we also signal this mutex to make sure we deblock the
1265 video thread in all cases */
1266 SDL_LockMutex(is
->pictq_mutex
);
1267 SDL_CondSignal(is
->pictq_cond
);
1268 SDL_UnlockMutex(is
->pictq_mutex
);
1270 SDL_WaitThread(is
->video_tid
, NULL
);
1272 packet_queue_end(&is
->videoq
);
1279 switch(enc
->codec_type
) {
1280 case CODEC_TYPE_AUDIO
:
1281 is
->audio_st
= NULL
;
1282 is
->audio_stream
= -1;
1284 case CODEC_TYPE_VIDEO
:
1285 is
->video_st
= NULL
;
1286 is
->video_stream
= -1;
1293 void dump_stream_info(AVFormatContext
*s
)
1296 fprintf(stderr
, "Track: %d\n", s
->track
);
1297 if (s
->title
[0] != '\0')
1298 fprintf(stderr
, "Title: %s\n", s
->title
);
1299 if (s
->author
[0] != '\0')
1300 fprintf(stderr
, "Author: %s\n", s
->author
);
1301 if (s
->album
[0] != '\0')
1302 fprintf(stderr
, "Album: %s\n", s
->album
);
1304 fprintf(stderr
, "Year: %d\n", s
->year
);
1305 if (s
->genre
[0] != '\0')
1306 fprintf(stderr
, "Genre: %s\n", s
->genre
);
1309 /* since we have only one decoding thread, we can use a global
1310 variable instead of a thread local variable */
1311 static VideoState
*global_video_state
;
1313 static int decode_interrupt_cb(void)
1315 return (global_video_state
&& global_video_state
->abort_request
);
1318 /* this thread gets the stream from the disk or the network */
1319 static int decode_thread(void *arg
)
1321 VideoState
*is
= arg
;
1322 AVFormatContext
*ic
;
1323 int err
, i
, ret
, video_index
, audio_index
, use_play
;
1324 AVPacket pkt1
, *pkt
= &pkt1
;
1325 AVFormatParameters params
, *ap
= ¶ms
;
1329 is
->video_stream
= -1;
1330 is
->audio_stream
= -1;
1332 global_video_state
= is
;
1333 url_set_interrupt_cb(decode_interrupt_cb
);
1335 memset(ap
, 0, sizeof(*ap
));
1336 ap
->image_format
= image_format
;
1337 ap
->initial_pause
= 1; /* we force a pause when starting an RTSP
1340 err
= av_open_input_file(&ic
, is
->filename
, is
->iformat
, 0, ap
);
1342 print_error(is
->filename
, err
);
1347 #ifdef CONFIG_NETWORK
1348 use_play
= (ic
->iformat
== &rtsp_demux
);
1353 err
= av_find_stream_info(ic
);
1355 fprintf(stderr
, "%s: could not find codec parameters\n", is
->filename
);
1359 ic
->pb
.eof_reached
= 0; //FIXME hack, ffplay maybe shouldnt use url_feof() to test for the end
1362 /* if seeking requested, we execute it */
1363 if (start_time
!= AV_NOPTS_VALUE
) {
1366 timestamp
= start_time
;
1367 /* add the stream start time */
1368 if (ic
->start_time
!= AV_NOPTS_VALUE
)
1369 timestamp
+= ic
->start_time
;
1370 ret
= av_seek_frame(ic
, -1, timestamp
, AVSEEK_FLAG_BACKWARD
);
1372 fprintf(stderr
, "%s: could not seek to position %0.3f\n",
1373 is
->filename
, (double)timestamp
/ AV_TIME_BASE
);
1377 /* now we can begin to play (RTSP stream only) */
1381 err
= av_find_stream_info(ic
);
1383 fprintf(stderr
, "%s: could not find codec parameters\n", is
->filename
);
1389 for(i
= 0; i
< ic
->nb_streams
; i
++) {
1390 AVCodecContext
*enc
= ic
->streams
[i
]->codec
;
1391 switch(enc
->codec_type
) {
1392 case CODEC_TYPE_AUDIO
:
1393 if (audio_index
< 0 && !audio_disable
)
1396 case CODEC_TYPE_VIDEO
:
1397 if (video_index
< 0 && !video_disable
)
1405 dump_format(ic
, 0, is
->filename
, 0);
1406 dump_stream_info(ic
);
1409 /* open the streams */
1410 if (audio_index
>= 0) {
1411 stream_component_open(is
, audio_index
);
1414 if (video_index
>= 0) {
1415 stream_component_open(is
, video_index
);
1417 if (!display_disable
)
1421 if (is
->video_stream
< 0 && is
->audio_stream
< 0) {
1422 fprintf(stderr
, "%s: could not open codecs\n", is
->filename
);
1428 if (is
->abort_request
)
1430 #ifdef CONFIG_NETWORK
1431 if (is
->paused
!= is
->last_paused
) {
1432 is
->last_paused
= is
->paused
;
1438 if (is
->paused
&& ic
->iformat
== &rtsp_demux
) {
1439 /* wait 10 ms to avoid trying to get another packet */
1446 /* XXX: must lock decoder threads */
1447 SDL_LockMutex(is
->video_decoder_mutex
);
1448 SDL_LockMutex(is
->audio_decoder_mutex
);
1449 ret
= av_seek_frame(is
->ic
, -1, is
->seek_pos
, is
->seek_flags
);
1451 fprintf(stderr
, "%s: error while seeking\n", is
->ic
->filename
);
1453 if (is
->audio_stream
>= 0) {
1454 packet_queue_flush(&is
->audioq
);
1456 if (is
->video_stream
>= 0) {
1457 packet_queue_flush(&is
->videoq
);
1458 avcodec_flush_buffers(ic
->streams
[video_index
]->codec
);
1461 SDL_UnlockMutex(is
->audio_decoder_mutex
);
1462 SDL_UnlockMutex(is
->video_decoder_mutex
);
1466 /* if the queue are full, no need to read more */
1467 if (is
->audioq
.size
> MAX_AUDIOQ_SIZE
||
1468 is
->videoq
.size
> MAX_VIDEOQ_SIZE
||
1469 url_feof(&ic
->pb
)) {
1474 ret
= av_read_frame(ic
, pkt
);
1476 if (url_ferror(&ic
->pb
) == 0) {
1477 SDL_Delay(100); /* wait for user event */
1482 if (pkt
->stream_index
== is
->audio_stream
) {
1483 packet_queue_put(&is
->audioq
, pkt
);
1484 } else if (pkt
->stream_index
== is
->video_stream
) {
1485 packet_queue_put(&is
->videoq
, pkt
);
1487 av_free_packet(pkt
);
1490 /* wait until the end */
1491 while (!is
->abort_request
) {
1497 /* disable interrupting */
1498 global_video_state
= NULL
;
1500 /* close each stream */
1501 if (is
->audio_stream
>= 0)
1502 stream_component_close(is
, is
->audio_stream
);
1503 if (is
->video_stream
>= 0)
1504 stream_component_close(is
, is
->video_stream
);
1506 av_close_input_file(is
->ic
);
1507 is
->ic
= NULL
; /* safety */
1509 url_set_interrupt_cb(NULL
);
1514 event
.type
= FF_QUIT_EVENT
;
1515 event
.user
.data1
= is
;
1516 SDL_PushEvent(&event
);
1521 static VideoState
*stream_open(const char *filename
, AVInputFormat
*iformat
)
1525 is
= av_mallocz(sizeof(VideoState
));
1528 pstrcpy(is
->filename
, sizeof(is
->filename
), filename
);
1529 is
->iformat
= iformat
;
1531 is
->width
= screen
->w
;
1532 is
->height
= screen
->h
;
1537 /* start video display */
1538 is
->pictq_mutex
= SDL_CreateMutex();
1539 is
->pictq_cond
= SDL_CreateCond();
1541 is
->audio_decoder_mutex
= SDL_CreateMutex();
1542 is
->video_decoder_mutex
= SDL_CreateMutex();
1544 /* add the refresh timer to draw the picture */
1545 schedule_refresh(is
, 40);
1547 is
->av_sync_type
= av_sync_type
;
1548 is
->parse_tid
= SDL_CreateThread(decode_thread
, is
);
1549 if (!is
->parse_tid
) {
1556 static void stream_close(VideoState
*is
)
1560 /* XXX: use a special url_shutdown call to abort parse cleanly */
1561 is
->abort_request
= 1;
1562 SDL_WaitThread(is
->parse_tid
, NULL
);
1564 /* free all pictures */
1565 for(i
=0;i
<VIDEO_PICTURE_QUEUE_SIZE
; i
++) {
1568 SDL_FreeYUVOverlay(vp
->bmp
);
1572 SDL_DestroyMutex(is
->pictq_mutex
);
1573 SDL_DestroyCond(is
->pictq_cond
);
1574 SDL_DestroyMutex(is
->audio_decoder_mutex
);
1575 SDL_DestroyMutex(is
->video_decoder_mutex
);
1578 void stream_cycle_channel(VideoState
*is
, int codec_type
)
1580 AVFormatContext
*ic
= is
->ic
;
1581 int start_index
, stream_index
;
1584 if (codec_type
== CODEC_TYPE_VIDEO
)
1585 start_index
= is
->video_stream
;
1587 start_index
= is
->audio_stream
;
1588 if (start_index
< 0)
1590 stream_index
= start_index
;
1592 if (++stream_index
>= is
->ic
->nb_streams
)
1594 if (stream_index
== start_index
)
1596 st
= ic
->streams
[stream_index
];
1597 if (st
->codec
->codec_type
== codec_type
) {
1598 /* check that parameters are OK */
1599 switch(codec_type
) {
1600 case CODEC_TYPE_AUDIO
:
1601 if (st
->codec
->sample_rate
!= 0 &&
1602 st
->codec
->channels
!= 0)
1605 case CODEC_TYPE_VIDEO
:
1613 stream_component_close(is
, start_index
);
1614 stream_component_open(is
, stream_index
);
1618 void toggle_full_screen(void)
1621 is_full_screen
= !is_full_screen
;
1622 if (!fs_screen_width
) {
1623 /* use default SDL method */
1624 SDL_WM_ToggleFullScreen(screen
);
1626 /* use the recorded resolution */
1627 flags
= SDL_HWSURFACE
|SDL_ASYNCBLIT
|SDL_HWACCEL
;
1628 if (is_full_screen
) {
1629 w
= fs_screen_width
;
1630 h
= fs_screen_height
;
1631 flags
|= SDL_FULLSCREEN
;
1635 flags
|= SDL_RESIZABLE
;
1637 screen
= SDL_SetVideoMode(w
, h
, 0, flags
);
1638 cur_stream
->width
= w
;
1639 cur_stream
->height
= h
;
1643 void toggle_pause(void)
1646 stream_pause(cur_stream
);
1650 void step_to_next_frame(void)
1653 if (cur_stream
->paused
)
1654 cur_stream
->paused
=0;
1655 cur_stream
->video_current_pts
= get_video_clock(cur_stream
);
1663 stream_close(cur_stream
);
1672 void toggle_audio_display(void)
1675 cur_stream
->show_audio
= !cur_stream
->show_audio
;
1679 /* handle an event sent by the GUI */
1680 void event_loop(void)
1683 double incr
, pos
, frac
;
1686 SDL_WaitEvent(&event
);
1687 switch(event
.type
) {
1689 switch(event
.key
.keysym
.sym
) {
1695 toggle_full_screen();
1701 case SDLK_s
: //S: Step to next frame
1702 step_to_next_frame();
1706 stream_cycle_channel(cur_stream
, CODEC_TYPE_AUDIO
);
1710 stream_cycle_channel(cur_stream
, CODEC_TYPE_VIDEO
);
1713 toggle_audio_display();
1728 pos
= get_master_clock(cur_stream
);
1730 stream_seek(cur_stream
, (int64_t)(pos
* AV_TIME_BASE
), incr
);
1737 case SDL_MOUSEBUTTONDOWN
:
1740 int tns
, thh
, tmm
, tss
;
1741 tns
= cur_stream
->ic
->duration
/1000000LL;
1743 tmm
= (tns
%3600)/60;
1745 frac
= (double)event
.button
.x
/(double)cur_stream
->width
;
1750 fprintf(stderr
, "Seek to %2.0f%% (%2d:%02d:%02d) of total duration (%2d:%02d:%02d) \n", frac
*100,
1751 hh
, mm
, ss
, thh
, tmm
, tss
);
1752 stream_seek(cur_stream
, (int64_t)(cur_stream
->ic
->start_time
+frac
*cur_stream
->ic
->duration
), 0);
1755 case SDL_VIDEORESIZE
:
1757 screen
= SDL_SetVideoMode(event
.resize
.w
, event
.resize
.h
, 0,
1758 SDL_HWSURFACE
|SDL_RESIZABLE
|SDL_ASYNCBLIT
|SDL_HWACCEL
);
1759 cur_stream
->width
= event
.resize
.w
;
1760 cur_stream
->height
= event
.resize
.h
;
1767 case FF_ALLOC_EVENT
:
1768 alloc_picture(event
.user
.data1
);
1770 case FF_REFRESH_EVENT
:
1771 video_refresh_timer(event
.user
.data1
);
1779 void opt_width(const char *arg
)
1781 screen_width
= atoi(arg
);
1784 void opt_height(const char *arg
)
1786 screen_height
= atoi(arg
);
1789 static void opt_format(const char *arg
)
1791 file_iformat
= av_find_input_format(arg
);
1792 if (!file_iformat
) {
1793 fprintf(stderr
, "Unknown input format: %s\n", arg
);
1798 static void opt_image_format(const char *arg
)
1802 for(f
= first_image_format
; f
!= NULL
; f
= f
->next
) {
1803 if (!strcmp(arg
, f
->name
))
1807 fprintf(stderr
, "Unknown image format: '%s'\n", arg
);
1813 #ifdef CONFIG_NETWORK
1814 void opt_rtp_tcp(void)
1816 /* only tcp protocol */
1817 rtsp_default_protocols
= (1 << RTSP_PROTOCOL_RTP_TCP
);
1821 void opt_sync(const char *arg
)
1823 if (!strcmp(arg
, "audio"))
1824 av_sync_type
= AV_SYNC_AUDIO_MASTER
;
1825 else if (!strcmp(arg
, "video"))
1826 av_sync_type
= AV_SYNC_VIDEO_MASTER
;
1827 else if (!strcmp(arg
, "ext"))
1828 av_sync_type
= AV_SYNC_EXTERNAL_CLOCK
;
1833 void opt_seek(const char *arg
)
1835 start_time
= parse_date(arg
, 1);
1838 static void opt_debug(const char *arg
)
1843 static void opt_vismv(const char *arg
)
1845 debug_mv
= atoi(arg
);
1848 static void opt_thread_count(const char *arg
)
1850 thread_count
= atoi(arg
);
1851 #if !defined(HAVE_THREADS)
1852 fprintf(stderr
, "Warning: not compiled with thread support, using thread emulation\n");
1856 const OptionDef options
[] = {
1857 { "h", 0, {(void*)show_help
}, "show help" },
1858 { "x", HAS_ARG
, {(void*)opt_width
}, "force displayed width", "width" },
1859 { "y", HAS_ARG
, {(void*)opt_height
}, "force displayed height", "height" },
1861 /* disabled as SDL/X11 does not support it correctly on application launch */
1862 { "fs", OPT_BOOL
, {(void*)&is_full_screen
}, "force full screen" },
1864 { "an", OPT_BOOL
, {(void*)&audio_disable
}, "disable audio" },
1865 { "vn", OPT_BOOL
, {(void*)&video_disable
}, "disable video" },
1866 { "ss", HAS_ARG
, {(void*)&opt_seek
}, "seek to a given position in seconds", "pos" },
1867 { "nodisp", OPT_BOOL
, {(void*)&display_disable
}, "disable graphical display" },
1868 { "f", HAS_ARG
, {(void*)opt_format
}, "force format", "fmt" },
1869 { "img", HAS_ARG
, {(void*)opt_image_format
}, "force image format", "img_fmt" },
1870 { "stats", OPT_BOOL
| OPT_EXPERT
, {(void*)&show_status
}, "show status", "" },
1871 { "debug", HAS_ARG
| OPT_EXPERT
, {(void*)opt_debug
}, "print specific debug info", "" },
1872 { "bug", OPT_INT
| HAS_ARG
| OPT_EXPERT
, {(void*)&workaround_bugs
}, "workaround bugs", "" },
1873 { "vismv", HAS_ARG
| OPT_EXPERT
, {(void*)opt_vismv
}, "visualize motion vectors", "" },
1874 { "fast", OPT_BOOL
| OPT_EXPERT
, {(void*)&fast
}, "non spec compliant optimizations", "" },
1875 { "lowres", OPT_INT
| HAS_ARG
| OPT_EXPERT
, {(void*)&lowres
}, "", "" },
1876 { "skiploop", OPT_INT
| HAS_ARG
| OPT_EXPERT
, {(void*)&skip_loop_filter
}, "", "" },
1877 { "skipframe", OPT_INT
| HAS_ARG
| OPT_EXPERT
, {(void*)&skip_frame
}, "", "" },
1878 { "skipidct", OPT_INT
| HAS_ARG
| OPT_EXPERT
, {(void*)&skip_idct
}, "", "" },
1879 { "idct", OPT_INT
| HAS_ARG
| OPT_EXPERT
, {(void*)&idct
}, "set idct algo", "algo" },
1880 { "er", OPT_INT
| HAS_ARG
| OPT_EXPERT
, {(void*)&error_resilience
}, "set error detection threshold (0-4)", "threshold" },
1881 { "ec", OPT_INT
| HAS_ARG
| OPT_EXPERT
, {(void*)&error_concealment
}, "set error concealment options", "bit_mask" },
1882 #ifdef CONFIG_NETWORK
1883 { "rtp_tcp", OPT_EXPERT
, {(void*)&opt_rtp_tcp
}, "force RTP/TCP protocol usage", "" },
1885 { "sync", HAS_ARG
| OPT_EXPERT
, {(void*)opt_sync
}, "set audio-video sync. type (type=audio/video/ext)", "type" },
1886 { "threads", HAS_ARG
| OPT_EXPERT
, {(void*)opt_thread_count
}, "thread count", "count" },
1890 void show_help(void)
1892 printf("ffplay version " FFMPEG_VERSION
", Copyright (c) 2003 Fabrice Bellard\n"
1893 "usage: ffplay [options] input_file\n"
1894 "Simple media player\n");
1896 show_help_options(options
, "Main options:\n",
1898 show_help_options(options
, "\nAdvanced options:\n",
1899 OPT_EXPERT
, OPT_EXPERT
);
1900 printf("\nWhile playing:\n"
1902 "f toggle full screen\n"
1904 "a cycle audio channel\n"
1905 "v cycle video channel\n"
1906 "w show audio waves\n"
1907 "left/right seek backward/forward 10 seconds\n"
1908 "down/up seek backward/forward 1 minute\n"
1909 "mouse click seek to percentage in file corresponding to fraction of width\n"
1914 void parse_arg_file(const char *filename
)
1916 if (!strcmp(filename
, "-"))
1918 input_filename
= filename
;
1921 /* Called from the main */
1922 int main(int argc
, char **argv
)
1926 /* register all codecs, demux and protocols */
1930 MorphToPM(); // Morph the VIO application to a PM one to be able to use Win* functions
1932 // Make stdout and stderr unbuffered
1933 setbuf( stdout
, NULL
);
1934 setbuf( stderr
, NULL
);
1937 parse_options(argc
, argv
, options
);
1939 if (!input_filename
)
1942 if (display_disable
) {
1945 flags
= SDL_INIT_VIDEO
| SDL_INIT_AUDIO
| SDL_INIT_TIMER
;
1946 #ifndef CONFIG_WIN32
1947 flags
|= SDL_INIT_EVENTTHREAD
; /* Not supported on win32 */
1949 if (SDL_Init (flags
)) {
1950 fprintf(stderr
, "Could not initialize SDL - %s\n", SDL_GetError());
1954 if (!display_disable
) {
1956 /* save the screen resolution... SDL should allow full screen
1957 by resizing the window */
1960 dpy
= XOpenDisplay(NULL
);
1962 fs_screen_width
= DisplayWidth(dpy
, DefaultScreen(dpy
));
1963 fs_screen_height
= DisplayHeight(dpy
, DefaultScreen(dpy
));
1968 flags
= SDL_HWSURFACE
|SDL_ASYNCBLIT
|SDL_HWACCEL
;
1969 if (is_full_screen
&& fs_screen_width
) {
1970 w
= fs_screen_width
;
1971 h
= fs_screen_height
;
1972 flags
|= SDL_FULLSCREEN
;
1976 flags
|= SDL_RESIZABLE
;
1978 screen
= SDL_SetVideoMode(w
, h
, 0, flags
);
1980 fprintf(stderr
, "SDL: could not set video mode - exiting\n");
1983 SDL_WM_SetCaption("FFplay", "FFplay");
1986 SDL_EventState(SDL_ACTIVEEVENT
, SDL_IGNORE
);
1987 SDL_EventState(SDL_MOUSEMOTION
, SDL_IGNORE
);
1988 SDL_EventState(SDL_SYSWMEVENT
, SDL_IGNORE
);
1989 SDL_EventState(SDL_USEREVENT
, SDL_IGNORE
);
1991 cur_stream
= stream_open(input_filename
, file_iformat
);