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() */
31 #if defined(__linux__)
41 #define MAX_VIDEOQ_SIZE (5 * 256 * 1024)
42 #define MAX_AUDIOQ_SIZE (5 * 16 * 1024)
44 /* SDL audio buffer size, in samples. Should be small to have precise
45 A/V sync as SDL does not have hardware buffer fullness info. */
46 #define SDL_AUDIO_BUFFER_SIZE 1024
48 /* no AV sync correction is done if below the AV sync threshold */
49 #define AV_SYNC_THRESHOLD 0.01
50 /* no AV correction is done if too big error */
51 #define AV_NOSYNC_THRESHOLD 10.0
53 /* maximum audio speed change to get correct sync */
54 #define SAMPLE_CORRECTION_PERCENT_MAX 10
56 /* we use about AUDIO_DIFF_AVG_NB A-V differences to make the average */
57 #define AUDIO_DIFF_AVG_NB 20
59 /* NOTE: the size must be big enough to compensate the hardware audio buffersize size */
60 #define SAMPLE_ARRAY_SIZE (2*65536)
62 typedef struct PacketQueue
{
63 AVPacketList
*first_pkt
, *last_pkt
;
71 #define VIDEO_PICTURE_QUEUE_SIZE 1
73 typedef struct VideoPicture
{
74 double pts
; /* presentation time stamp for this picture */
76 int width
, height
; /* source height & width */
81 AV_SYNC_AUDIO_MASTER
, /* default choice */
83 AV_SYNC_EXTERNAL_CLOCK
, /* synchronize to an external clock */
86 typedef struct VideoState
{
87 SDL_Thread
*parse_tid
;
88 SDL_Thread
*video_tid
;
89 AVInputFormat
*iformat
;
98 int dtg_active_format
;
103 double external_clock
; /* external clock base */
104 int64_t external_clock_time
;
107 double audio_diff_cum
; /* used for AV difference average computation */
108 double audio_diff_avg_coef
;
109 double audio_diff_threshold
;
110 int audio_diff_avg_count
;
113 int audio_hw_buf_size
;
114 /* samples output by the codec. we reserve more space for avsync
116 uint8_t audio_buf
[(AVCODEC_MAX_AUDIO_FRAME_SIZE
* 3) / 2];
117 unsigned int audio_buf_size
; /* in bytes */
118 int audio_buf_index
; /* in bytes */
120 uint8_t *audio_pkt_data
;
123 int show_audio
; /* if true, display audio samples */
124 int16_t sample_array
[SAMPLE_ARRAY_SIZE
];
125 int sample_array_index
;
129 double frame_last_pts
;
130 double frame_last_delay
;
135 double video_last_P_pts
; /* pts of the last P picture (needed if B
136 frames are present) */
137 double video_current_pts
; /* current displayed pts (different from
138 video_clock if frame fifos are used) */
139 int64_t video_current_pts_time
; /* time at which we updated
140 video_current_pts - used to
141 have running video pts */
142 VideoPicture pictq
[VIDEO_PICTURE_QUEUE_SIZE
];
143 int pictq_size
, pictq_rindex
, pictq_windex
;
144 SDL_mutex
*pictq_mutex
;
145 SDL_cond
*pictq_cond
;
147 SDL_mutex
*video_decoder_mutex
;
148 SDL_mutex
*audio_decoder_mutex
;
150 // QETimer *video_timer;
152 int width
, height
, xleft
, ytop
;
155 void show_help(void);
156 static int audio_write_get_buf_size(VideoState
*is
);
158 /* options specified by the user */
159 static AVInputFormat
*file_iformat
;
160 static AVImageFormat
*image_format
;
161 static const char *input_filename
;
162 static int fs_screen_width
;
163 static int fs_screen_height
;
164 static int screen_width
= 640;
165 static int screen_height
= 480;
166 static int audio_disable
;
167 static int video_disable
;
168 static int display_disable
;
169 static int show_status
;
170 static int av_sync_type
= AV_SYNC_AUDIO_MASTER
;
171 static int64_t start_time
= AV_NOPTS_VALUE
;
172 static int debug
= 0;
173 static int debug_mv
= 0;
175 static int thread_count
= 1;
176 static int workaround_bugs
= 1;
178 static int lowres
= 0;
179 static int idct
= FF_IDCT_AUTO
;
181 /* current context */
182 static int is_full_screen
;
183 static VideoState
*cur_stream
;
184 static int64_t audio_callback_time
;
186 #define FF_ALLOC_EVENT (SDL_USEREVENT)
187 #define FF_REFRESH_EVENT (SDL_USEREVENT + 1)
188 #define FF_QUIT_EVENT (SDL_USEREVENT + 2)
192 /* packet queue handling */
193 static void packet_queue_init(PacketQueue
*q
)
195 memset(q
, 0, sizeof(PacketQueue
));
196 q
->mutex
= SDL_CreateMutex();
197 q
->cond
= SDL_CreateCond();
200 static void packet_queue_flush(PacketQueue
*q
)
202 AVPacketList
*pkt
, *pkt1
;
204 for(pkt
= q
->first_pkt
; pkt
!= NULL
; pkt
= pkt1
) {
206 av_free_packet(&pkt
->pkt
);
215 static void packet_queue_end(PacketQueue
*q
)
217 packet_queue_flush(q
);
218 SDL_DestroyMutex(q
->mutex
);
219 SDL_DestroyCond(q
->cond
);
222 static int packet_queue_put(PacketQueue
*q
, AVPacket
*pkt
)
226 /* duplicate the packet */
227 if (av_dup_packet(pkt
) < 0)
230 pkt1
= av_malloc(sizeof(AVPacketList
));
237 SDL_LockMutex(q
->mutex
);
243 q
->last_pkt
->next
= pkt1
;
246 q
->size
+= pkt1
->pkt
.size
;
247 /* XXX: should duplicate packet data in DV case */
248 SDL_CondSignal(q
->cond
);
250 SDL_UnlockMutex(q
->mutex
);
254 static void packet_queue_abort(PacketQueue
*q
)
256 SDL_LockMutex(q
->mutex
);
258 q
->abort_request
= 1;
260 SDL_CondSignal(q
->cond
);
262 SDL_UnlockMutex(q
->mutex
);
265 /* return < 0 if aborted, 0 if no packet and > 0 if packet. */
266 static int packet_queue_get(PacketQueue
*q
, AVPacket
*pkt
, int block
)
271 SDL_LockMutex(q
->mutex
);
274 if (q
->abort_request
) {
281 q
->first_pkt
= pkt1
->next
;
285 q
->size
-= pkt1
->pkt
.size
;
294 SDL_CondWait(q
->cond
, q
->mutex
);
297 SDL_UnlockMutex(q
->mutex
);
301 static inline void fill_rectangle(SDL_Surface
*screen
,
302 int x
, int y
, int w
, int h
, int color
)
309 SDL_FillRect(screen
, &rect
, color
);
313 /* draw only the border of a rectangle */
314 void fill_border(VideoState
*s
, int x
, int y
, int w
, int h
, int color
)
318 /* fill the background */
322 w2
= s
->width
- (x
+ w
);
328 h2
= s
->height
- (y
+ h
);
331 fill_rectangle(screen
,
335 fill_rectangle(screen
,
336 s
->xleft
+ s
->width
- w2
, s
->ytop
,
339 fill_rectangle(screen
,
340 s
->xleft
+ w1
, s
->ytop
,
341 s
->width
- w1
- w2
, h1
,
343 fill_rectangle(screen
,
344 s
->xleft
+ w1
, s
->ytop
+ s
->height
- h2
,
345 s
->width
- w1
- w2
, h2
,
350 static void video_image_display(VideoState
*is
)
354 int width
, height
, x
, y
;
357 vp
= &is
->pictq
[is
->pictq_rindex
];
359 /* XXX: use variable in the frame */
360 if (is
->video_st
->codec
.sample_aspect_ratio
.num
== 0)
363 aspect_ratio
= av_q2d(is
->video_st
->codec
.sample_aspect_ratio
)
364 * is
->video_st
->codec
.width
/ is
->video_st
->codec
.height
;;
365 if (aspect_ratio
<= 0.0)
366 aspect_ratio
= (float)is
->video_st
->codec
.width
/
367 (float)is
->video_st
->codec
.height
;
368 /* if an active format is indicated, then it overrides the
371 if (is
->video_st
->codec
.dtg_active_format
!= is
->dtg_active_format
) {
372 is
->dtg_active_format
= is
->video_st
->codec
.dtg_active_format
;
373 printf("dtg_active_format=%d\n", is
->dtg_active_format
);
377 switch(is
->video_st
->codec
.dtg_active_format
) {
378 case FF_DTG_AFD_SAME
:
383 aspect_ratio
= 4.0 / 3.0;
385 case FF_DTG_AFD_16_9
:
386 aspect_ratio
= 16.0 / 9.0;
388 case FF_DTG_AFD_14_9
:
389 aspect_ratio
= 14.0 / 9.0;
391 case FF_DTG_AFD_4_3_SP_14_9
:
392 aspect_ratio
= 14.0 / 9.0;
394 case FF_DTG_AFD_16_9_SP_14_9
:
395 aspect_ratio
= 14.0 / 9.0;
397 case FF_DTG_AFD_SP_4_3
:
398 aspect_ratio
= 4.0 / 3.0;
403 /* XXX: we suppose the screen has a 1.0 pixel ratio */
405 width
= ((int)rint(height
* aspect_ratio
)) & -3;
406 if (width
> is
->width
) {
408 height
= ((int)rint(width
/ aspect_ratio
)) & -3;
410 x
= (is
->width
- width
) / 2;
411 y
= (is
->height
- height
) / 2;
412 if (!is
->no_background
) {
413 /* fill the background */
414 // fill_border(is, x, y, width, height, QERGB(0x00, 0x00, 0x00));
416 is
->no_background
= 0;
418 rect
.x
= is
->xleft
+ x
;
419 rect
.y
= is
->xleft
+ y
;
422 SDL_DisplayYUVOverlay(vp
->bmp
, &rect
);
425 fill_rectangle(screen
,
426 is
->xleft
, is
->ytop
, is
->width
, is
->height
,
427 QERGB(0x00, 0x00, 0x00));
432 static inline int compute_mod(int a
, int b
)
441 static void video_audio_display(VideoState
*s
)
443 int i
, i_start
, x
, y1
, y
, ys
, delay
, n
, nb_display_channels
;
444 int ch
, channels
, h
, h2
, bgcolor
, fgcolor
;
447 /* compute display index : center on currently output samples */
448 channels
= s
->audio_st
->codec
.channels
;
449 nb_display_channels
= channels
;
452 delay
= audio_write_get_buf_size(s
);
455 /* to be more precise, we take into account the time spent since
456 the last buffer computation */
457 if (audio_callback_time
) {
458 time_diff
= av_gettime() - audio_callback_time
;
459 delay
+= (time_diff
* s
->audio_st
->codec
.sample_rate
) / 1000000;
462 delay
-= s
->width
/ 2;
463 if (delay
< s
->width
)
465 i_start
= compute_mod(s
->sample_array_index
- delay
* channels
, SAMPLE_ARRAY_SIZE
);
466 s
->last_i_start
= i_start
;
468 i_start
= s
->last_i_start
;
471 bgcolor
= SDL_MapRGB(screen
->format
, 0x00, 0x00, 0x00);
472 fill_rectangle(screen
,
473 s
->xleft
, s
->ytop
, s
->width
, s
->height
,
476 fgcolor
= SDL_MapRGB(screen
->format
, 0xff, 0xff, 0xff);
478 /* total height for one channel */
479 h
= s
->height
/ nb_display_channels
;
480 /* graph height / 2 */
482 for(ch
= 0;ch
< nb_display_channels
; ch
++) {
484 y1
= s
->ytop
+ ch
* h
+ (h
/ 2); /* position of center line */
485 for(x
= 0; x
< s
->width
; x
++) {
486 y
= (s
->sample_array
[i
] * h2
) >> 15;
493 fill_rectangle(screen
,
494 s
->xleft
+ x
, ys
, 1, y
,
497 if (i
>= SAMPLE_ARRAY_SIZE
)
498 i
-= SAMPLE_ARRAY_SIZE
;
502 fgcolor
= SDL_MapRGB(screen
->format
, 0x00, 0x00, 0xff);
504 for(ch
= 1;ch
< nb_display_channels
; ch
++) {
505 y
= s
->ytop
+ ch
* h
;
506 fill_rectangle(screen
,
507 s
->xleft
, y
, s
->width
, 1,
510 SDL_UpdateRect(screen
, s
->xleft
, s
->ytop
, s
->width
, s
->height
);
513 /* display the current picture, if any */
514 static void video_display(VideoState
*is
)
516 if (is
->audio_st
&& is
->show_audio
)
517 video_audio_display(is
);
518 else if (is
->video_st
)
519 video_image_display(is
);
522 static Uint32
sdl_refresh_timer_cb(Uint32 interval
, void *opaque
)
525 event
.type
= FF_REFRESH_EVENT
;
526 event
.user
.data1
= opaque
;
527 SDL_PushEvent(&event
);
528 return 0; /* 0 means stop timer */
531 /* schedule a video refresh in 'delay' ms */
532 static void schedule_refresh(VideoState
*is
, int delay
)
534 SDL_AddTimer(delay
, sdl_refresh_timer_cb
, is
);
537 /* get the current audio clock value */
538 static double get_audio_clock(VideoState
*is
)
541 int hw_buf_size
, bytes_per_sec
;
542 pts
= is
->audio_clock
;
543 hw_buf_size
= audio_write_get_buf_size(is
);
546 bytes_per_sec
= is
->audio_st
->codec
.sample_rate
*
547 2 * is
->audio_st
->codec
.channels
;
550 pts
-= (double)hw_buf_size
/ bytes_per_sec
;
554 /* get the current video clock value */
555 static double get_video_clock(VideoState
*is
)
561 delta
= (av_gettime() - is
->video_current_pts_time
) / 1000000.0;
563 return is
->video_current_pts
+ delta
;
566 /* get the current external clock value */
567 static double get_external_clock(VideoState
*is
)
571 return is
->external_clock
+ ((ti
- is
->external_clock_time
) * 1e-6);
574 /* get the current master clock value */
575 static double get_master_clock(VideoState
*is
)
579 if (is
->av_sync_type
== AV_SYNC_VIDEO_MASTER
) {
581 val
= get_video_clock(is
);
583 val
= get_audio_clock(is
);
584 } else if (is
->av_sync_type
== AV_SYNC_AUDIO_MASTER
) {
586 val
= get_audio_clock(is
);
588 val
= get_video_clock(is
);
590 val
= get_external_clock(is
);
595 /* seek in the stream */
596 static void stream_seek(VideoState
*is
, int64_t pos
, int rel
)
600 is
->seek_flags
= rel
< 0 ? AVSEEK_FLAG_BACKWARD
: 0;
603 /* pause or resume the video */
604 static void stream_pause(VideoState
*is
)
606 is
->paused
= !is
->paused
;
608 is
->video_current_pts
= get_video_clock(is
);
612 /* called to display each frame */
613 static void video_refresh_timer(void *opaque
)
615 VideoState
*is
= opaque
;
617 double actual_delay
, delay
, sync_threshold
, ref_clock
, diff
;
621 if (is
->pictq_size
== 0) {
622 /* if no picture, need to wait */
623 schedule_refresh(is
, 1);
625 /* dequeue the picture */
626 vp
= &is
->pictq
[is
->pictq_rindex
];
628 /* update current video pts */
629 is
->video_current_pts
= vp
->pts
;
630 is
->video_current_pts_time
= av_gettime();
632 /* compute nominal delay */
633 delay
= vp
->pts
- is
->frame_last_pts
;
634 if (delay
<= 0 || delay
>= 1.0) {
635 /* if incorrect delay, use previous one */
636 delay
= is
->frame_last_delay
;
638 is
->frame_last_delay
= delay
;
639 is
->frame_last_pts
= vp
->pts
;
641 /* update delay to follow master synchronisation source */
642 if (((is
->av_sync_type
== AV_SYNC_AUDIO_MASTER
&& is
->audio_st
) ||
643 is
->av_sync_type
== AV_SYNC_EXTERNAL_CLOCK
)) {
644 /* if video is slave, we try to correct big delays by
645 duplicating or deleting a frame */
646 ref_clock
= get_master_clock(is
);
647 diff
= vp
->pts
- ref_clock
;
649 /* skip or repeat frame. We take into account the
650 delay to compute the threshold. I still don't know
651 if it is the best guess */
652 sync_threshold
= AV_SYNC_THRESHOLD
;
653 if (delay
> sync_threshold
)
654 sync_threshold
= delay
;
655 if (fabs(diff
) < AV_NOSYNC_THRESHOLD
) {
656 if (diff
<= -sync_threshold
)
658 else if (diff
>= sync_threshold
)
663 is
->frame_timer
+= delay
;
664 /* compute the REAL delay (we need to do that to avoid
666 actual_delay
= is
->frame_timer
- (av_gettime() / 1000000.0);
667 if (actual_delay
< 0.010) {
668 /* XXX: should skip picture */
669 actual_delay
= 0.010;
671 /* launch timer for next picture */
672 schedule_refresh(is
, (int)(actual_delay
* 1000 + 0.5));
674 #if defined(DEBUG_SYNC)
675 printf("video: delay=%0.3f actual_delay=%0.3f pts=%0.3f A-V=%f\n",
676 delay
, actual_delay
, vp
->pts
, -diff
);
679 /* display picture */
682 /* update queue size and signal for next picture */
683 if (++is
->pictq_rindex
== VIDEO_PICTURE_QUEUE_SIZE
)
684 is
->pictq_rindex
= 0;
686 SDL_LockMutex(is
->pictq_mutex
);
688 SDL_CondSignal(is
->pictq_cond
);
689 SDL_UnlockMutex(is
->pictq_mutex
);
691 } else if (is
->audio_st
) {
692 /* draw the next audio frame */
694 schedule_refresh(is
, 40);
696 /* if only audio stream, then display the audio bars (better
697 than nothing, just to test the implementation */
699 /* display picture */
702 schedule_refresh(is
, 100);
705 static int64_t last_time
;
710 cur_time
= av_gettime();
711 if (!last_time
|| (cur_time
- last_time
) >= 500 * 1000) {
715 aqsize
= is
->audioq
.size
;
717 vqsize
= is
->videoq
.size
;
719 if (is
->audio_st
&& is
->video_st
)
720 av_diff
= get_audio_clock(is
) - get_video_clock(is
);
721 printf("%7.2f A-V:%7.3f aq=%5dKB vq=%5dKB \r",
722 get_master_clock(is
), av_diff
, aqsize
/ 1024, vqsize
/ 1024);
724 last_time
= cur_time
;
729 /* allocate a picture (needs to do that in main thread to avoid
730 potential locking problems */
731 static void alloc_picture(void *opaque
)
733 VideoState
*is
= opaque
;
736 vp
= &is
->pictq
[is
->pictq_windex
];
739 SDL_FreeYUVOverlay(vp
->bmp
);
742 /* XXX: use generic function */
743 /* XXX: disable overlay if no hardware acceleration or if RGB format */
744 switch(is
->video_st
->codec
.pix_fmt
) {
745 case PIX_FMT_YUV420P
:
746 case PIX_FMT_YUV422P
:
747 case PIX_FMT_YUV444P
:
749 case PIX_FMT_YUV410P
:
750 case PIX_FMT_YUV411P
:
758 vp
->bmp
= SDL_CreateYUVOverlay(is
->video_st
->codec
.width
,
759 is
->video_st
->codec
.height
,
762 vp
->width
= is
->video_st
->codec
.width
;
763 vp
->height
= is
->video_st
->codec
.height
;
765 SDL_LockMutex(is
->pictq_mutex
);
767 SDL_CondSignal(is
->pictq_cond
);
768 SDL_UnlockMutex(is
->pictq_mutex
);
771 static int queue_picture(VideoState
*is
, AVFrame
*src_frame
, double pts
)
777 /* wait until we have space to put a new picture */
778 SDL_LockMutex(is
->pictq_mutex
);
779 while (is
->pictq_size
>= VIDEO_PICTURE_QUEUE_SIZE
&&
780 !is
->videoq
.abort_request
) {
781 SDL_CondWait(is
->pictq_cond
, is
->pictq_mutex
);
783 SDL_UnlockMutex(is
->pictq_mutex
);
785 if (is
->videoq
.abort_request
)
788 vp
= &is
->pictq
[is
->pictq_windex
];
790 /* alloc or resize hardware picture buffer */
792 vp
->width
!= is
->video_st
->codec
.width
||
793 vp
->height
!= is
->video_st
->codec
.height
) {
798 /* the allocation must be done in the main thread to avoid
800 event
.type
= FF_ALLOC_EVENT
;
801 event
.user
.data1
= is
;
802 SDL_PushEvent(&event
);
804 /* wait until the picture is allocated */
805 SDL_LockMutex(is
->pictq_mutex
);
806 while (!vp
->allocated
&& !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
)
815 /* if the frame is not skipped, then display it */
817 /* get a pointer on the bitmap */
818 SDL_LockYUVOverlay (vp
->bmp
);
820 dst_pix_fmt
= PIX_FMT_YUV420P
;
821 pict
.data
[0] = vp
->bmp
->pixels
[0];
822 pict
.data
[1] = vp
->bmp
->pixels
[2];
823 pict
.data
[2] = vp
->bmp
->pixels
[1];
825 pict
.linesize
[0] = vp
->bmp
->pitches
[0];
826 pict
.linesize
[1] = vp
->bmp
->pitches
[2];
827 pict
.linesize
[2] = vp
->bmp
->pitches
[1];
828 img_convert(&pict
, dst_pix_fmt
,
829 (AVPicture
*)src_frame
, is
->video_st
->codec
.pix_fmt
,
830 is
->video_st
->codec
.width
, is
->video_st
->codec
.height
);
831 /* update the bitmap content */
832 SDL_UnlockYUVOverlay(vp
->bmp
);
836 /* now we can update the picture count */
837 if (++is
->pictq_windex
== VIDEO_PICTURE_QUEUE_SIZE
)
838 is
->pictq_windex
= 0;
839 SDL_LockMutex(is
->pictq_mutex
);
841 SDL_UnlockMutex(is
->pictq_mutex
);
846 /* compute the exact PTS for the picture if it is omitted in the stream */
847 static int output_picture2(VideoState
*is
, AVFrame
*src_frame
, double pts1
)
849 double frame_delay
, pts
;
854 /* update video clock with pts, if present */
855 is
->video_clock
= pts
;
857 pts
= is
->video_clock
;
859 /* update video clock for next frame */
860 frame_delay
= (double)is
->video_st
->codec
.frame_rate_base
/
861 (double)is
->video_st
->codec
.frame_rate
;
862 /* for MPEG2, the frame can be repeated, so we update the
864 if (src_frame
->repeat_pict
) {
865 frame_delay
+= src_frame
->repeat_pict
* (frame_delay
* 0.5);
867 is
->video_clock
+= frame_delay
;
869 #if defined(DEBUG_SYNC) && 0
872 if (src_frame
->pict_type
== FF_B_TYPE
)
874 else if (src_frame
->pict_type
== FF_I_TYPE
)
878 printf("frame_type=%c clock=%0.3f pts=%0.3f\n",
882 return queue_picture(is
, src_frame
, pts
);
885 static int video_thread(void *arg
)
887 VideoState
*is
= arg
;
888 AVPacket pkt1
, *pkt
= &pkt1
;
889 int len1
, got_picture
;
890 AVFrame
*frame
= avcodec_alloc_frame();
894 while (is
->paused
&& !is
->videoq
.abort_request
) {
897 if (packet_queue_get(&is
->videoq
, pkt
, 1) < 0)
899 /* NOTE: ipts is the PTS of the _first_ picture beginning in
900 this packet, if any */
902 if (pkt
->dts
!= AV_NOPTS_VALUE
)
903 pts
= (double)pkt
->dts
/ AV_TIME_BASE
;
905 SDL_LockMutex(is
->video_decoder_mutex
);
906 len1
= avcodec_decode_video(&is
->video_st
->codec
,
908 pkt
->data
, pkt
->size
);
909 SDL_UnlockMutex(is
->video_decoder_mutex
);
913 if (output_picture2(is
, frame
, pts
) < 0)
919 stream_pause(cur_stream
);
926 /* copy samples for viewing in editor window */
927 static void update_sample_display(VideoState
*is
, short *samples
, int samples_size
)
929 int size
, len
, channels
;
931 channels
= is
->audio_st
->codec
.channels
;
933 size
= samples_size
/ sizeof(short);
935 len
= SAMPLE_ARRAY_SIZE
- is
->sample_array_index
;
938 memcpy(is
->sample_array
+ is
->sample_array_index
, samples
, len
* sizeof(short));
940 is
->sample_array_index
+= len
;
941 if (is
->sample_array_index
>= SAMPLE_ARRAY_SIZE
)
942 is
->sample_array_index
= 0;
947 /* return the new audio buffer size (samples can be added or deleted
948 to get better sync if video or external master clock) */
949 static int synchronize_audio(VideoState
*is
, short *samples
,
950 int samples_size1
, double pts
)
955 n
= 2 * is
->audio_st
->codec
.channels
;
956 samples_size
= samples_size1
;
958 /* if not master, then we try to remove or add samples to correct the clock */
959 if (((is
->av_sync_type
== AV_SYNC_VIDEO_MASTER
&& is
->video_st
) ||
960 is
->av_sync_type
== AV_SYNC_EXTERNAL_CLOCK
)) {
961 double diff
, avg_diff
;
962 int wanted_size
, min_size
, max_size
, nb_samples
;
964 ref_clock
= get_master_clock(is
);
965 diff
= get_audio_clock(is
) - ref_clock
;
967 if (diff
< AV_NOSYNC_THRESHOLD
) {
968 is
->audio_diff_cum
= diff
+ is
->audio_diff_avg_coef
* is
->audio_diff_cum
;
969 if (is
->audio_diff_avg_count
< AUDIO_DIFF_AVG_NB
) {
970 /* not enough measures to have a correct estimate */
971 is
->audio_diff_avg_count
++;
973 /* estimate the A-V difference */
974 avg_diff
= is
->audio_diff_cum
* (1.0 - is
->audio_diff_avg_coef
);
976 if (fabs(avg_diff
) >= is
->audio_diff_threshold
) {
977 wanted_size
= samples_size
+ ((int)(diff
* is
->audio_st
->codec
.sample_rate
) * n
);
978 nb_samples
= samples_size
/ n
;
980 min_size
= ((nb_samples
* (100 - SAMPLE_CORRECTION_PERCENT_MAX
)) / 100) * n
;
981 max_size
= ((nb_samples
* (100 + SAMPLE_CORRECTION_PERCENT_MAX
)) / 100) * n
;
982 if (wanted_size
< min_size
)
983 wanted_size
= min_size
;
984 else if (wanted_size
> max_size
)
985 wanted_size
= max_size
;
987 /* add or remove samples to correction the synchro */
988 if (wanted_size
< samples_size
) {
990 samples_size
= wanted_size
;
991 } else if (wanted_size
> samples_size
) {
992 uint8_t *samples_end
, *q
;
996 nb
= (samples_size
- wanted_size
);
997 samples_end
= (uint8_t *)samples
+ samples_size
- n
;
1000 memcpy(q
, samples_end
, n
);
1004 samples_size
= wanted_size
;
1008 printf("diff=%f adiff=%f sample_diff=%d apts=%0.3f vpts=%0.3f %f\n",
1009 diff
, avg_diff
, samples_size
- samples_size1
,
1010 is
->audio_clock
, is
->video_clock
, is
->audio_diff_threshold
);
1014 /* too big difference : may be initial PTS errors, so
1016 is
->audio_diff_avg_count
= 0;
1017 is
->audio_diff_cum
= 0;
1021 return samples_size
;
1024 /* decode one audio frame and returns its uncompressed size */
1025 static int audio_decode_frame(VideoState
*is
, uint8_t *audio_buf
, double *pts_ptr
)
1027 AVPacket
*pkt
= &is
->audio_pkt
;
1028 int n
, len1
, data_size
;
1032 /* NOTE: the audio packet can contain several frames */
1033 while (is
->audio_pkt_size
> 0) {
1034 SDL_LockMutex(is
->audio_decoder_mutex
);
1035 len1
= avcodec_decode_audio(&is
->audio_st
->codec
,
1036 (int16_t *)audio_buf
, &data_size
,
1037 is
->audio_pkt_data
, is
->audio_pkt_size
);
1038 SDL_UnlockMutex(is
->audio_decoder_mutex
);
1040 /* if error, we skip the frame */
1041 is
->audio_pkt_size
= 0;
1045 is
->audio_pkt_data
+= len1
;
1046 is
->audio_pkt_size
-= len1
;
1049 /* if no pts, then compute it */
1050 pts
= is
->audio_clock
;
1052 n
= 2 * is
->audio_st
->codec
.channels
;
1053 is
->audio_clock
+= (double)data_size
/
1054 (double)(n
* is
->audio_st
->codec
.sample_rate
);
1055 #if defined(DEBUG_SYNC)
1057 static double last_clock
;
1058 printf("audio: delay=%0.3f clock=%0.3f pts=%0.3f\n",
1059 is
->audio_clock
- last_clock
,
1060 is
->audio_clock
, pts
);
1061 last_clock
= is
->audio_clock
;
1067 /* free the current packet */
1069 av_free_packet(pkt
);
1071 if (is
->paused
|| is
->audioq
.abort_request
) {
1075 /* read next packet */
1076 if (packet_queue_get(&is
->audioq
, pkt
, 1) < 0)
1078 is
->audio_pkt_data
= pkt
->data
;
1079 is
->audio_pkt_size
= pkt
->size
;
1081 /* if update the audio clock with the pts */
1082 if (pkt
->pts
!= AV_NOPTS_VALUE
) {
1083 is
->audio_clock
= (double)pkt
->pts
/ AV_TIME_BASE
;
1088 /* get the current audio output buffer size, in samples. With SDL, we
1089 cannot have a precise information */
1090 static int audio_write_get_buf_size(VideoState
*is
)
1092 return is
->audio_hw_buf_size
- is
->audio_buf_index
;
1096 /* prepare a new audio buffer */
1097 void sdl_audio_callback(void *opaque
, Uint8
*stream
, int len
)
1099 VideoState
*is
= opaque
;
1100 int audio_size
, len1
;
1103 audio_callback_time
= av_gettime();
1106 if (is
->audio_buf_index
>= is
->audio_buf_size
) {
1107 audio_size
= audio_decode_frame(is
, is
->audio_buf
, &pts
);
1108 if (audio_size
< 0) {
1109 /* if error, just output silence */
1110 is
->audio_buf_size
= 1024;
1111 memset(is
->audio_buf
, 0, is
->audio_buf_size
);
1114 update_sample_display(is
, (int16_t *)is
->audio_buf
, audio_size
);
1115 audio_size
= synchronize_audio(is
, (int16_t *)is
->audio_buf
, audio_size
,
1117 is
->audio_buf_size
= audio_size
;
1119 is
->audio_buf_index
= 0;
1121 len1
= is
->audio_buf_size
- is
->audio_buf_index
;
1124 memcpy(stream
, (uint8_t *)is
->audio_buf
+ is
->audio_buf_index
, len1
);
1127 is
->audio_buf_index
+= len1
;
1132 /* open a given stream. Return 0 if OK */
1133 static int stream_component_open(VideoState
*is
, int stream_index
)
1135 AVFormatContext
*ic
= is
->ic
;
1136 AVCodecContext
*enc
;
1138 SDL_AudioSpec wanted_spec
, spec
;
1140 if (stream_index
< 0 || stream_index
>= ic
->nb_streams
)
1142 enc
= &ic
->streams
[stream_index
]->codec
;
1144 /* prepare audio output */
1145 if (enc
->codec_type
== CODEC_TYPE_AUDIO
) {
1146 wanted_spec
.freq
= enc
->sample_rate
;
1147 wanted_spec
.format
= AUDIO_S16SYS
;
1148 /* hack for AC3. XXX: suppress that */
1149 if (enc
->channels
> 2)
1151 wanted_spec
.channels
= enc
->channels
;
1152 wanted_spec
.silence
= 0;
1153 wanted_spec
.samples
= SDL_AUDIO_BUFFER_SIZE
;
1154 wanted_spec
.callback
= sdl_audio_callback
;
1155 wanted_spec
.userdata
= is
;
1156 if (SDL_OpenAudio(&wanted_spec
, &spec
) < 0) {
1157 fprintf(stderr
, "SDL_OpenAudio: %s\n", SDL_GetError());
1160 is
->audio_hw_buf_size
= spec
.size
;
1163 codec
= avcodec_find_decoder(enc
->codec_id
);
1164 enc
->debug_mv
= debug_mv
;
1166 enc
->workaround_bugs
= workaround_bugs
;
1167 enc
->lowres
= lowres
;
1168 if(lowres
) enc
->flags
|= CODEC_FLAG_EMU_EDGE
;
1169 enc
->idct_algo
= idct
;
1170 if(fast
) enc
->flags2
|= CODEC_FLAG2_FAST
;
1172 avcodec_open(enc
, codec
) < 0)
1174 #if defined(HAVE_THREADS)
1176 avcodec_thread_init(enc
, thread_count
);
1178 enc
->thread_count
= thread_count
;
1179 switch(enc
->codec_type
) {
1180 case CODEC_TYPE_AUDIO
:
1181 is
->audio_stream
= stream_index
;
1182 is
->audio_st
= ic
->streams
[stream_index
];
1183 is
->audio_buf_size
= 0;
1184 is
->audio_buf_index
= 0;
1186 /* init averaging filter */
1187 is
->audio_diff_avg_coef
= exp(log(0.01) / AUDIO_DIFF_AVG_NB
);
1188 is
->audio_diff_avg_count
= 0;
1189 /* since we do not have a precise anough audio fifo fullness,
1190 we correct audio sync only if larger than this threshold */
1191 is
->audio_diff_threshold
= 2.0 * SDL_AUDIO_BUFFER_SIZE
/ enc
->sample_rate
;
1193 memset(&is
->audio_pkt
, 0, sizeof(is
->audio_pkt
));
1194 packet_queue_init(&is
->audioq
);
1197 case CODEC_TYPE_VIDEO
:
1198 is
->video_stream
= stream_index
;
1199 is
->video_st
= ic
->streams
[stream_index
];
1201 is
->frame_last_delay
= 40e-3;
1202 is
->frame_timer
= (double)av_gettime() / 1000000.0;
1203 is
->video_current_pts_time
= av_gettime();
1205 packet_queue_init(&is
->videoq
);
1206 is
->video_tid
= SDL_CreateThread(video_thread
, is
);
1214 static void stream_component_close(VideoState
*is
, int stream_index
)
1216 AVFormatContext
*ic
= is
->ic
;
1217 AVCodecContext
*enc
;
1219 enc
= &ic
->streams
[stream_index
]->codec
;
1221 switch(enc
->codec_type
) {
1222 case CODEC_TYPE_AUDIO
:
1223 packet_queue_abort(&is
->audioq
);
1227 packet_queue_end(&is
->audioq
);
1229 case CODEC_TYPE_VIDEO
:
1230 packet_queue_abort(&is
->videoq
);
1232 /* note: we also signal this mutex to make sure we deblock the
1233 video thread in all cases */
1234 SDL_LockMutex(is
->pictq_mutex
);
1235 SDL_CondSignal(is
->pictq_cond
);
1236 SDL_UnlockMutex(is
->pictq_mutex
);
1238 SDL_WaitThread(is
->video_tid
, NULL
);
1240 packet_queue_end(&is
->videoq
);
1247 switch(enc
->codec_type
) {
1248 case CODEC_TYPE_AUDIO
:
1249 is
->audio_st
= NULL
;
1250 is
->audio_stream
= -1;
1252 case CODEC_TYPE_VIDEO
:
1253 is
->video_st
= NULL
;
1254 is
->video_stream
= -1;
1261 void dump_stream_info(AVFormatContext
*s
)
1264 fprintf(stderr
, "Track: %d\n", s
->track
);
1265 if (s
->title
[0] != '\0')
1266 fprintf(stderr
, "Title: %s\n", s
->title
);
1267 if (s
->author
[0] != '\0')
1268 fprintf(stderr
, "Author: %s\n", s
->author
);
1269 if (s
->album
[0] != '\0')
1270 fprintf(stderr
, "Album: %s\n", s
->album
);
1272 fprintf(stderr
, "Year: %d\n", s
->year
);
1273 if (s
->genre
[0] != '\0')
1274 fprintf(stderr
, "Genre: %s\n", s
->genre
);
1277 /* since we have only one decoding thread, we can use a global
1278 variable instead of a thread local variable */
1279 static VideoState
*global_video_state
;
1281 static int decode_interrupt_cb(void)
1283 return (global_video_state
&& global_video_state
->abort_request
);
1286 /* this thread gets the stream from the disk or the network */
1287 static int decode_thread(void *arg
)
1289 VideoState
*is
= arg
;
1290 AVFormatContext
*ic
;
1291 int err
, i
, ret
, video_index
, audio_index
, use_play
;
1292 AVPacket pkt1
, *pkt
= &pkt1
;
1293 AVFormatParameters params
, *ap
= ¶ms
;
1297 is
->video_stream
= -1;
1298 is
->audio_stream
= -1;
1300 global_video_state
= is
;
1301 url_set_interrupt_cb(decode_interrupt_cb
);
1303 memset(ap
, 0, sizeof(*ap
));
1304 ap
->image_format
= image_format
;
1305 ap
->initial_pause
= 1; /* we force a pause when starting an RTSP
1308 err
= av_open_input_file(&ic
, is
->filename
, is
->iformat
, 0, ap
);
1310 print_error(is
->filename
, err
);
1315 #ifdef CONFIG_NETWORK
1316 use_play
= (ic
->iformat
== &rtsp_demux
);
1321 err
= av_find_stream_info(ic
);
1323 fprintf(stderr
, "%s: could not find codec parameters\n", is
->filename
);
1327 ic
->pb
.eof_reached
= 0; //FIXME hack, ffplay maybe shouldnt use url_feof() to test for the end
1330 /* if seeking requested, we execute it */
1331 if (start_time
!= AV_NOPTS_VALUE
) {
1334 timestamp
= start_time
;
1335 /* add the stream start time */
1336 if (ic
->start_time
!= AV_NOPTS_VALUE
)
1337 timestamp
+= ic
->start_time
;
1338 ret
= av_seek_frame(ic
, -1, timestamp
, AVSEEK_FLAG_BACKWARD
);
1340 fprintf(stderr
, "%s: could not seek to position %0.3f\n",
1341 is
->filename
, (double)timestamp
/ AV_TIME_BASE
);
1345 /* now we can begin to play (RTSP stream only) */
1349 err
= av_find_stream_info(ic
);
1351 fprintf(stderr
, "%s: could not find codec parameters\n", is
->filename
);
1357 for(i
= 0; i
< ic
->nb_streams
; i
++) {
1358 AVCodecContext
*enc
= &ic
->streams
[i
]->codec
;
1359 switch(enc
->codec_type
) {
1360 case CODEC_TYPE_AUDIO
:
1361 if (audio_index
< 0 && !audio_disable
)
1364 case CODEC_TYPE_VIDEO
:
1365 if (video_index
< 0 && !video_disable
)
1373 dump_format(ic
, 0, is
->filename
, 0);
1374 dump_stream_info(ic
);
1377 /* open the streams */
1378 if (audio_index
>= 0) {
1379 stream_component_open(is
, audio_index
);
1382 if (video_index
>= 0) {
1383 stream_component_open(is
, video_index
);
1385 if (!display_disable
)
1389 if (is
->video_stream
< 0 && is
->audio_stream
< 0) {
1390 fprintf(stderr
, "%s: could not open codecs\n", is
->filename
);
1396 if (is
->abort_request
)
1398 #ifdef CONFIG_NETWORK
1399 if (is
->paused
!= is
->last_paused
) {
1400 is
->last_paused
= is
->paused
;
1406 if (is
->paused
&& ic
->iformat
== &rtsp_demux
) {
1407 /* wait 10 ms to avoid trying to get another packet */
1414 /* XXX: must lock decoder threads */
1415 ret
= av_seek_frame(is
->ic
, -1, is
->seek_pos
, is
->seek_flags
);
1417 fprintf(stderr
, "%s: error while seeking\n", is
->ic
->filename
);
1419 if (is
->audio_stream
>= 0) {
1420 packet_queue_flush(&is
->audioq
);
1422 if (is
->video_stream
>= 0) {
1423 packet_queue_flush(&is
->videoq
);
1424 SDL_LockMutex(is
->video_decoder_mutex
);
1425 avcodec_flush_buffers(&ic
->streams
[video_index
]->codec
);
1426 SDL_UnlockMutex(is
->video_decoder_mutex
);
1432 /* if the queue are full, no need to read more */
1433 if (is
->audioq
.size
> MAX_AUDIOQ_SIZE
||
1434 is
->videoq
.size
> MAX_VIDEOQ_SIZE
||
1435 url_feof(&ic
->pb
)) {
1440 ret
= av_read_frame(ic
, pkt
);
1442 if (url_feof(&ic
->pb
) && url_ferror(&ic
->pb
) == 0) {
1443 SDL_Delay(100); /* wait for user event */
1448 if (pkt
->stream_index
== is
->audio_stream
) {
1449 packet_queue_put(&is
->audioq
, pkt
);
1450 } else if (pkt
->stream_index
== is
->video_stream
) {
1451 packet_queue_put(&is
->videoq
, pkt
);
1453 av_free_packet(pkt
);
1456 /* wait until the end */
1457 while (!is
->abort_request
) {
1463 /* disable interrupting */
1464 global_video_state
= NULL
;
1466 /* close each stream */
1467 if (is
->audio_stream
>= 0)
1468 stream_component_close(is
, is
->audio_stream
);
1469 if (is
->video_stream
>= 0)
1470 stream_component_close(is
, is
->video_stream
);
1472 av_close_input_file(is
->ic
);
1473 is
->ic
= NULL
; /* safety */
1475 url_set_interrupt_cb(NULL
);
1480 event
.type
= FF_QUIT_EVENT
;
1481 event
.user
.data1
= is
;
1482 SDL_PushEvent(&event
);
1487 static VideoState
*stream_open(const char *filename
, AVInputFormat
*iformat
)
1491 is
= av_mallocz(sizeof(VideoState
));
1494 pstrcpy(is
->filename
, sizeof(is
->filename
), filename
);
1495 is
->iformat
= iformat
;
1497 is
->width
= screen
->w
;
1498 is
->height
= screen
->h
;
1503 /* start video display */
1504 is
->pictq_mutex
= SDL_CreateMutex();
1505 is
->pictq_cond
= SDL_CreateCond();
1507 is
->audio_decoder_mutex
= SDL_CreateMutex();
1508 is
->video_decoder_mutex
= SDL_CreateMutex();
1510 /* add the refresh timer to draw the picture */
1511 schedule_refresh(is
, 40);
1513 is
->av_sync_type
= av_sync_type
;
1514 is
->parse_tid
= SDL_CreateThread(decode_thread
, is
);
1515 if (!is
->parse_tid
) {
1522 static void stream_close(VideoState
*is
)
1526 /* XXX: use a special url_shutdown call to abort parse cleanly */
1527 is
->abort_request
= 1;
1528 SDL_WaitThread(is
->parse_tid
, NULL
);
1530 /* free all pictures */
1531 for(i
=0;i
<VIDEO_PICTURE_QUEUE_SIZE
; i
++) {
1534 SDL_FreeYUVOverlay(vp
->bmp
);
1538 SDL_DestroyMutex(is
->pictq_mutex
);
1539 SDL_DestroyCond(is
->pictq_cond
);
1540 SDL_DestroyMutex(is
->audio_decoder_mutex
);
1541 SDL_DestroyMutex(is
->video_decoder_mutex
);
1544 void stream_cycle_channel(VideoState
*is
, int codec_type
)
1546 AVFormatContext
*ic
= is
->ic
;
1547 int start_index
, stream_index
;
1550 if (codec_type
== CODEC_TYPE_VIDEO
)
1551 start_index
= is
->video_stream
;
1553 start_index
= is
->audio_stream
;
1554 if (start_index
< 0)
1556 stream_index
= start_index
;
1558 if (++stream_index
>= is
->ic
->nb_streams
)
1560 if (stream_index
== start_index
)
1562 st
= ic
->streams
[stream_index
];
1563 if (st
->codec
.codec_type
== codec_type
) {
1564 /* check that parameters are OK */
1565 switch(codec_type
) {
1566 case CODEC_TYPE_AUDIO
:
1567 if (st
->codec
.sample_rate
!= 0 &&
1568 st
->codec
.channels
!= 0)
1571 case CODEC_TYPE_VIDEO
:
1579 stream_component_close(is
, start_index
);
1580 stream_component_open(is
, stream_index
);
1584 void toggle_full_screen(void)
1587 is_full_screen
= !is_full_screen
;
1588 if (!fs_screen_width
) {
1589 /* use default SDL method */
1590 SDL_WM_ToggleFullScreen(screen
);
1592 /* use the recorded resolution */
1593 flags
= SDL_HWSURFACE
|SDL_ASYNCBLIT
|SDL_HWACCEL
;
1594 if (is_full_screen
) {
1595 w
= fs_screen_width
;
1596 h
= fs_screen_height
;
1597 flags
|= SDL_FULLSCREEN
;
1601 flags
|= SDL_RESIZABLE
;
1603 screen
= SDL_SetVideoMode(w
, h
, 0, flags
);
1604 cur_stream
->width
= w
;
1605 cur_stream
->height
= h
;
1609 void toggle_pause(void)
1612 stream_pause(cur_stream
);
1616 void step_to_next_frame(void)
1619 if (cur_stream
->paused
)
1620 cur_stream
->paused
=0;
1621 cur_stream
->video_current_pts
= get_video_clock(cur_stream
);
1629 stream_close(cur_stream
);
1638 void toggle_audio_display(void)
1641 cur_stream
->show_audio
= !cur_stream
->show_audio
;
1645 /* handle an event sent by the GUI */
1646 void event_loop(void)
1649 double incr
, pos
, frac
;
1652 SDL_WaitEvent(&event
);
1653 switch(event
.type
) {
1655 switch(event
.key
.keysym
.sym
) {
1661 toggle_full_screen();
1667 case SDLK_s
: //S: Step to next frame
1668 step_to_next_frame();
1672 stream_cycle_channel(cur_stream
, CODEC_TYPE_AUDIO
);
1676 stream_cycle_channel(cur_stream
, CODEC_TYPE_VIDEO
);
1679 toggle_audio_display();
1694 pos
= get_master_clock(cur_stream
);
1696 stream_seek(cur_stream
, (int64_t)(pos
* AV_TIME_BASE
), incr
);
1703 case SDL_MOUSEBUTTONDOWN
:
1706 int tns
, thh
, tmm
, tss
;
1707 tns
= cur_stream
->ic
->duration
/1000000LL;
1709 tmm
= (tns
%3600)/60;
1711 frac
= (double)event
.button
.x
/(double)cur_stream
->width
;
1716 fprintf(stderr
, "Seek to %2.0f%% (%2d:%02d:%02d) of total duration (%2d:%02d:%02d) \n", frac
*100,
1717 hh
, mm
, ss
, thh
, tmm
, tss
);
1718 stream_seek(cur_stream
, (int64_t)(cur_stream
->ic
->start_time
+frac
*cur_stream
->ic
->duration
), 0);
1721 case SDL_VIDEORESIZE
:
1723 screen
= SDL_SetVideoMode(event
.resize
.w
, event
.resize
.h
, 0,
1724 SDL_HWSURFACE
|SDL_RESIZABLE
|SDL_ASYNCBLIT
|SDL_HWACCEL
);
1725 cur_stream
->width
= event
.resize
.w
;
1726 cur_stream
->height
= event
.resize
.h
;
1733 case FF_ALLOC_EVENT
:
1734 alloc_picture(event
.user
.data1
);
1736 case FF_REFRESH_EVENT
:
1737 video_refresh_timer(event
.user
.data1
);
1745 void opt_width(const char *arg
)
1747 screen_width
= atoi(arg
);
1750 void opt_height(const char *arg
)
1752 screen_height
= atoi(arg
);
1755 static void opt_format(const char *arg
)
1757 file_iformat
= av_find_input_format(arg
);
1758 if (!file_iformat
) {
1759 fprintf(stderr
, "Unknown input format: %s\n", arg
);
1764 static void opt_image_format(const char *arg
)
1768 for(f
= first_image_format
; f
!= NULL
; f
= f
->next
) {
1769 if (!strcmp(arg
, f
->name
))
1773 fprintf(stderr
, "Unknown image format: '%s'\n", arg
);
1779 #ifdef CONFIG_NETWORK
1780 void opt_rtp_tcp(void)
1782 /* only tcp protocol */
1783 rtsp_default_protocols
= (1 << RTSP_PROTOCOL_RTP_TCP
);
1787 void opt_sync(const char *arg
)
1789 if (!strcmp(arg
, "audio"))
1790 av_sync_type
= AV_SYNC_AUDIO_MASTER
;
1791 else if (!strcmp(arg
, "video"))
1792 av_sync_type
= AV_SYNC_VIDEO_MASTER
;
1793 else if (!strcmp(arg
, "ext"))
1794 av_sync_type
= AV_SYNC_EXTERNAL_CLOCK
;
1799 void opt_seek(const char *arg
)
1801 start_time
= parse_date(arg
, 1);
1804 static void opt_debug(const char *arg
)
1809 static void opt_vismv(const char *arg
)
1811 debug_mv
= atoi(arg
);
1814 static void opt_thread_count(const char *arg
)
1816 thread_count
= atoi(arg
);
1817 #if !defined(HAVE_THREADS)
1818 fprintf(stderr
, "Warning: not compiled with thread support, using thread emulation\n");
1822 const OptionDef options
[] = {
1823 { "h", 0, {(void*)show_help
}, "show help" },
1824 { "x", HAS_ARG
, {(void*)opt_width
}, "force displayed width", "width" },
1825 { "y", HAS_ARG
, {(void*)opt_height
}, "force displayed height", "height" },
1827 /* disabled as SDL/X11 does not support it correctly on application launch */
1828 { "fs", OPT_BOOL
, {(void*)&is_full_screen
}, "force full screen" },
1830 { "an", OPT_BOOL
, {(void*)&audio_disable
}, "disable audio" },
1831 { "vn", OPT_BOOL
, {(void*)&video_disable
}, "disable video" },
1832 { "ss", HAS_ARG
, {(void*)&opt_seek
}, "seek to a given position in seconds", "pos" },
1833 { "nodisp", OPT_BOOL
, {(void*)&display_disable
}, "disable graphical display" },
1834 { "f", HAS_ARG
, {(void*)opt_format
}, "force format", "fmt" },
1835 { "img", HAS_ARG
, {(void*)opt_image_format
}, "force image format", "img_fmt" },
1836 { "stats", OPT_BOOL
| OPT_EXPERT
, {(void*)&show_status
}, "show status", "" },
1837 { "debug", HAS_ARG
| OPT_EXPERT
, {(void*)opt_debug
}, "print specific debug info", "" },
1838 { "bug", OPT_INT
| HAS_ARG
| OPT_EXPERT
, {(void*)&workaround_bugs
}, "workaround bugs", "" },
1839 { "vismv", HAS_ARG
| OPT_EXPERT
, {(void*)opt_vismv
}, "visualize motion vectors", "" },
1840 { "fast", OPT_BOOL
| OPT_EXPERT
, {(void*)&fast
}, "non spec compliant optimizations", "" },
1841 { "lowres", OPT_INT
| HAS_ARG
| OPT_EXPERT
, {(void*)&lowres
}, "", "" },
1842 { "idct", OPT_INT
| HAS_ARG
| OPT_EXPERT
, {(void*)&idct
}, "set idct algo", "algo" },
1843 #ifdef CONFIG_NETWORK
1844 { "rtp_tcp", OPT_EXPERT
, {(void*)&opt_rtp_tcp
}, "force RTP/TCP protocol usage", "" },
1846 { "sync", HAS_ARG
| OPT_EXPERT
, {(void*)opt_sync
}, "set audio-video sync. type (type=audio/video/ext)", "type" },
1847 { "threads", HAS_ARG
| OPT_EXPERT
, {(void*)opt_thread_count
}, "thread count", "count" },
1851 void show_help(void)
1853 printf("ffplay version " FFMPEG_VERSION
", Copyright (c) 2003 Fabrice Bellard\n"
1854 "usage: ffplay [options] input_file\n"
1855 "Simple media player\n");
1857 show_help_options(options
, "Main options:\n",
1859 show_help_options(options
, "\nAdvanced options:\n",
1860 OPT_EXPERT
, OPT_EXPERT
);
1861 printf("\nWhile playing:\n"
1863 "f toggle full screen\n"
1865 "a cycle audio channel\n"
1866 "v cycle video channel\n"
1867 "w show audio waves\n"
1868 "left/right seek backward/forward 10 seconds\n"
1869 "down/up seek backward/forward 1 minute\n"
1870 "mouse click seek to percentage in file corresponding to fraction of width\n"
1875 void parse_arg_file(const char *filename
)
1877 if (!strcmp(filename
, "-"))
1879 input_filename
= filename
;
1882 /* Called from the main */
1883 int main(int argc
, char **argv
)
1887 /* register all codecs, demux and protocols */
1890 parse_options(argc
, argv
, options
);
1892 if (!input_filename
)
1895 if (display_disable
) {
1898 flags
= SDL_INIT_VIDEO
| SDL_INIT_AUDIO
| SDL_INIT_TIMER
;
1899 #ifndef CONFIG_WIN32
1900 flags
|= SDL_INIT_EVENTTHREAD
; /* Not supported on win32 */
1902 if (SDL_Init (flags
)) {
1903 fprintf(stderr
, "Could not initialize SDL - %s\n", SDL_GetError());
1907 if (!display_disable
) {
1909 /* save the screen resolution... SDL should allow full screen
1910 by resizing the window */
1913 dpy
= XOpenDisplay(NULL
);
1915 fs_screen_width
= DisplayWidth(dpy
, DefaultScreen(dpy
));
1916 fs_screen_height
= DisplayHeight(dpy
, DefaultScreen(dpy
));
1921 flags
= SDL_HWSURFACE
|SDL_ASYNCBLIT
|SDL_HWACCEL
;
1922 if (is_full_screen
&& fs_screen_width
) {
1923 w
= fs_screen_width
;
1924 h
= fs_screen_height
;
1925 flags
|= SDL_FULLSCREEN
;
1929 flags
|= SDL_RESIZABLE
;
1931 screen
= SDL_SetVideoMode(w
, h
, 0, flags
);
1933 fprintf(stderr
, "SDL: could not set video mode - exiting\n");
1936 SDL_WM_SetCaption("FFplay", "FFplay");
1939 SDL_EventState(SDL_ACTIVEEVENT
, SDL_IGNORE
);
1940 SDL_EventState(SDL_MOUSEMOTION
, SDL_IGNORE
);
1941 SDL_EventState(SDL_SYSWMEVENT
, SDL_IGNORE
);
1942 SDL_EventState(SDL_USEREVENT
, SDL_IGNORE
);
1944 cur_stream
= stream_open(input_filename
, file_iformat
);