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
;
97 int dtg_active_format
;
102 double external_clock
; /* external clock base */
103 int64_t external_clock_time
;
106 double audio_diff_cum
; /* used for AV difference average computation */
107 double audio_diff_avg_coef
;
108 double audio_diff_threshold
;
109 int audio_diff_avg_count
;
112 int audio_hw_buf_size
;
113 /* samples output by the codec. we reserve more space for avsync
115 uint8_t audio_buf
[(AVCODEC_MAX_AUDIO_FRAME_SIZE
* 3) / 2];
116 int audio_buf_size
; /* in bytes */
117 int audio_buf_index
; /* in bytes */
119 uint8_t *audio_pkt_data
;
122 int show_audio
; /* if true, display audio samples */
123 int16_t sample_array
[SAMPLE_ARRAY_SIZE
];
124 int sample_array_index
;
128 double frame_last_pts
;
129 double frame_last_delay
;
134 double video_last_P_pts
; /* pts of the last P picture (needed if B
135 frames are present) */
136 double video_current_pts
; /* current displayed pts (different from
137 video_clock if frame fifos are used) */
138 int64_t video_current_pts_time
; /* time at which we updated
139 video_current_pts - used to
140 have running video pts */
141 VideoPicture pictq
[VIDEO_PICTURE_QUEUE_SIZE
];
142 int pictq_size
, pictq_rindex
, pictq_windex
;
143 SDL_mutex
*pictq_mutex
;
144 SDL_cond
*pictq_cond
;
146 // QETimer *video_timer;
148 int width
, height
, xleft
, ytop
;
151 void show_help(void);
152 static int audio_write_get_buf_size(VideoState
*is
);
154 /* options specified by the user */
155 static AVInputFormat
*file_iformat
;
156 static AVImageFormat
*image_format
;
157 static const char *input_filename
;
158 static int fs_screen_width
;
159 static int fs_screen_height
;
160 static int screen_width
= 640;
161 static int screen_height
= 480;
162 static int audio_disable
;
163 static int video_disable
;
164 static int display_disable
;
165 static int show_status
;
166 static int av_sync_type
= AV_SYNC_AUDIO_MASTER
;
167 static int64_t start_time
= AV_NOPTS_VALUE
;
168 static int debug
= 0;
169 static int debug_mv
= 0;
171 static int thread_count
= 1;
172 static int workaround_bugs
= 1;
174 static int lowres
= 0;
175 static int idct
= FF_IDCT_AUTO
;
177 /* current context */
178 static int is_full_screen
;
179 static VideoState
*cur_stream
;
180 static int64_t audio_callback_time
;
182 #define FF_ALLOC_EVENT (SDL_USEREVENT)
183 #define FF_REFRESH_EVENT (SDL_USEREVENT + 1)
184 #define FF_QUIT_EVENT (SDL_USEREVENT + 2)
188 /* packet queue handling */
189 static void packet_queue_init(PacketQueue
*q
)
191 memset(q
, 0, sizeof(PacketQueue
));
192 q
->mutex
= SDL_CreateMutex();
193 q
->cond
= SDL_CreateCond();
196 static void packet_queue_flush(PacketQueue
*q
)
198 AVPacketList
*pkt
, *pkt1
;
200 for(pkt
= q
->first_pkt
; pkt
!= NULL
; pkt
= pkt1
) {
202 av_free_packet(&pkt
->pkt
);
211 static void packet_queue_end(PacketQueue
*q
)
213 packet_queue_flush(q
);
214 SDL_DestroyMutex(q
->mutex
);
215 SDL_DestroyCond(q
->cond
);
218 static int packet_queue_put(PacketQueue
*q
, AVPacket
*pkt
)
222 /* duplicate the packet */
223 if (av_dup_packet(pkt
) < 0)
226 pkt1
= av_malloc(sizeof(AVPacketList
));
233 SDL_LockMutex(q
->mutex
);
239 q
->last_pkt
->next
= pkt1
;
242 q
->size
+= pkt1
->pkt
.size
;
243 /* XXX: should duplicate packet data in DV case */
244 SDL_CondSignal(q
->cond
);
246 SDL_UnlockMutex(q
->mutex
);
250 static void packet_queue_abort(PacketQueue
*q
)
252 SDL_LockMutex(q
->mutex
);
254 q
->abort_request
= 1;
256 SDL_CondSignal(q
->cond
);
258 SDL_UnlockMutex(q
->mutex
);
261 /* return < 0 if aborted, 0 if no packet and > 0 if packet. */
262 static int packet_queue_get(PacketQueue
*q
, AVPacket
*pkt
, int block
)
267 SDL_LockMutex(q
->mutex
);
270 if (q
->abort_request
) {
277 q
->first_pkt
= pkt1
->next
;
281 q
->size
-= pkt1
->pkt
.size
;
290 SDL_CondWait(q
->cond
, q
->mutex
);
293 SDL_UnlockMutex(q
->mutex
);
297 static inline void fill_rectangle(SDL_Surface
*screen
,
298 int x
, int y
, int w
, int h
, int color
)
305 SDL_FillRect(screen
, &rect
, color
);
309 /* draw only the border of a rectangle */
310 void fill_border(VideoState
*s
, int x
, int y
, int w
, int h
, int color
)
314 /* fill the background */
318 w2
= s
->width
- (x
+ w
);
324 h2
= s
->height
- (y
+ h
);
327 fill_rectangle(screen
,
331 fill_rectangle(screen
,
332 s
->xleft
+ s
->width
- w2
, s
->ytop
,
335 fill_rectangle(screen
,
336 s
->xleft
+ w1
, s
->ytop
,
337 s
->width
- w1
- w2
, h1
,
339 fill_rectangle(screen
,
340 s
->xleft
+ w1
, s
->ytop
+ s
->height
- h2
,
341 s
->width
- w1
- w2
, h2
,
346 static void video_image_display(VideoState
*is
)
350 int width
, height
, x
, y
;
353 vp
= &is
->pictq
[is
->pictq_rindex
];
355 /* XXX: use variable in the frame */
356 if (is
->video_st
->codec
.sample_aspect_ratio
.num
== 0)
359 aspect_ratio
= av_q2d(is
->video_st
->codec
.sample_aspect_ratio
)
360 * is
->video_st
->codec
.width
/ is
->video_st
->codec
.height
;;
361 if (aspect_ratio
<= 0.0)
362 aspect_ratio
= (float)is
->video_st
->codec
.width
/
363 (float)is
->video_st
->codec
.height
;
364 /* if an active format is indicated, then it overrides the
367 if (is
->video_st
->codec
.dtg_active_format
!= is
->dtg_active_format
) {
368 is
->dtg_active_format
= is
->video_st
->codec
.dtg_active_format
;
369 printf("dtg_active_format=%d\n", is
->dtg_active_format
);
373 switch(is
->video_st
->codec
.dtg_active_format
) {
374 case FF_DTG_AFD_SAME
:
379 aspect_ratio
= 4.0 / 3.0;
381 case FF_DTG_AFD_16_9
:
382 aspect_ratio
= 16.0 / 9.0;
384 case FF_DTG_AFD_14_9
:
385 aspect_ratio
= 14.0 / 9.0;
387 case FF_DTG_AFD_4_3_SP_14_9
:
388 aspect_ratio
= 14.0 / 9.0;
390 case FF_DTG_AFD_16_9_SP_14_9
:
391 aspect_ratio
= 14.0 / 9.0;
393 case FF_DTG_AFD_SP_4_3
:
394 aspect_ratio
= 4.0 / 3.0;
399 /* XXX: we suppose the screen has a 1.0 pixel ratio */
401 width
= ((int)rint(height
* aspect_ratio
)) & -3;
402 if (width
> is
->width
) {
404 height
= ((int)rint(width
/ aspect_ratio
)) & -3;
406 x
= (is
->width
- width
) / 2;
407 y
= (is
->height
- height
) / 2;
408 if (!is
->no_background
) {
409 /* fill the background */
410 // fill_border(is, x, y, width, height, QERGB(0x00, 0x00, 0x00));
412 is
->no_background
= 0;
414 rect
.x
= is
->xleft
+ x
;
415 rect
.y
= is
->xleft
+ y
;
418 SDL_DisplayYUVOverlay(vp
->bmp
, &rect
);
421 fill_rectangle(screen
,
422 is
->xleft
, is
->ytop
, is
->width
, is
->height
,
423 QERGB(0x00, 0x00, 0x00));
428 static inline int compute_mod(int a
, int b
)
437 static void video_audio_display(VideoState
*s
)
439 int i
, i_start
, x
, y1
, y
, ys
, delay
, n
, nb_display_channels
;
440 int ch
, channels
, h
, h2
, bgcolor
, fgcolor
;
443 /* compute display index : center on currently output samples */
444 channels
= s
->audio_st
->codec
.channels
;
445 nb_display_channels
= channels
;
448 delay
= audio_write_get_buf_size(s
);
451 /* to be more precise, we take into account the time spent since
452 the last buffer computation */
453 if (audio_callback_time
) {
454 time_diff
= av_gettime() - audio_callback_time
;
455 delay
+= (time_diff
* s
->audio_st
->codec
.sample_rate
) / 1000000;
458 delay
-= s
->width
/ 2;
459 if (delay
< s
->width
)
461 i_start
= compute_mod(s
->sample_array_index
- delay
* channels
, SAMPLE_ARRAY_SIZE
);
462 s
->last_i_start
= i_start
;
464 i_start
= s
->last_i_start
;
467 bgcolor
= SDL_MapRGB(screen
->format
, 0x00, 0x00, 0x00);
468 fill_rectangle(screen
,
469 s
->xleft
, s
->ytop
, s
->width
, s
->height
,
472 fgcolor
= SDL_MapRGB(screen
->format
, 0xff, 0xff, 0xff);
474 /* total height for one channel */
475 h
= s
->height
/ nb_display_channels
;
476 /* graph height / 2 */
478 for(ch
= 0;ch
< nb_display_channels
; ch
++) {
480 y1
= s
->ytop
+ ch
* h
+ (h
/ 2); /* position of center line */
481 for(x
= 0; x
< s
->width
; x
++) {
482 y
= (s
->sample_array
[i
] * h2
) >> 15;
489 fill_rectangle(screen
,
490 s
->xleft
+ x
, ys
, 1, y
,
493 if (i
>= SAMPLE_ARRAY_SIZE
)
494 i
-= SAMPLE_ARRAY_SIZE
;
498 fgcolor
= SDL_MapRGB(screen
->format
, 0x00, 0x00, 0xff);
500 for(ch
= 1;ch
< nb_display_channels
; ch
++) {
501 y
= s
->ytop
+ ch
* h
;
502 fill_rectangle(screen
,
503 s
->xleft
, y
, s
->width
, 1,
506 SDL_UpdateRect(screen
, s
->xleft
, s
->ytop
, s
->width
, s
->height
);
509 /* display the current picture, if any */
510 static void video_display(VideoState
*is
)
512 if (is
->audio_st
&& is
->show_audio
)
513 video_audio_display(is
);
514 else if (is
->video_st
)
515 video_image_display(is
);
518 static Uint32
sdl_refresh_timer_cb(Uint32 interval
, void *opaque
)
521 event
.type
= FF_REFRESH_EVENT
;
522 event
.user
.data1
= opaque
;
523 SDL_PushEvent(&event
);
524 return 0; /* 0 means stop timer */
527 /* schedule a video refresh in 'delay' ms */
528 static void schedule_refresh(VideoState
*is
, int delay
)
530 SDL_AddTimer(delay
, sdl_refresh_timer_cb
, is
);
533 /* get the current audio clock value */
534 static double get_audio_clock(VideoState
*is
)
537 int hw_buf_size
, bytes_per_sec
;
538 pts
= is
->audio_clock
;
539 hw_buf_size
= audio_write_get_buf_size(is
);
542 bytes_per_sec
= is
->audio_st
->codec
.sample_rate
*
543 2 * is
->audio_st
->codec
.channels
;
546 pts
-= (double)hw_buf_size
/ bytes_per_sec
;
550 /* get the current video clock value */
551 static double get_video_clock(VideoState
*is
)
557 delta
= (av_gettime() - is
->video_current_pts_time
) / 1000000.0;
559 return is
->video_current_pts
+ delta
;
562 /* get the current external clock value */
563 static double get_external_clock(VideoState
*is
)
567 return is
->external_clock
+ ((ti
- is
->external_clock_time
) * 1e-6);
570 /* get the current master clock value */
571 static double get_master_clock(VideoState
*is
)
575 if (is
->av_sync_type
== AV_SYNC_VIDEO_MASTER
) {
577 val
= get_video_clock(is
);
579 val
= get_audio_clock(is
);
580 } else if (is
->av_sync_type
== AV_SYNC_AUDIO_MASTER
) {
582 val
= get_audio_clock(is
);
584 val
= get_video_clock(is
);
586 val
= get_external_clock(is
);
591 /* seek in the stream */
592 static void stream_seek(VideoState
*is
, int64_t pos
)
598 /* pause or resume the video */
599 static void stream_pause(VideoState
*is
)
601 is
->paused
= !is
->paused
;
603 is
->video_current_pts
= get_video_clock(is
);
607 /* called to display each frame */
608 static void video_refresh_timer(void *opaque
)
610 VideoState
*is
= opaque
;
612 double actual_delay
, delay
, sync_threshold
, ref_clock
, diff
;
616 if (is
->pictq_size
== 0) {
617 /* if no picture, need to wait */
618 schedule_refresh(is
, 1);
620 /* dequeue the picture */
621 vp
= &is
->pictq
[is
->pictq_rindex
];
623 /* update current video pts */
624 is
->video_current_pts
= vp
->pts
;
625 is
->video_current_pts_time
= av_gettime();
627 /* compute nominal delay */
628 delay
= vp
->pts
- is
->frame_last_pts
;
629 if (delay
<= 0 || delay
>= 1.0) {
630 /* if incorrect delay, use previous one */
631 delay
= is
->frame_last_delay
;
633 is
->frame_last_delay
= delay
;
634 is
->frame_last_pts
= vp
->pts
;
636 /* update delay to follow master synchronisation source */
637 if (((is
->av_sync_type
== AV_SYNC_AUDIO_MASTER
&& is
->audio_st
) ||
638 is
->av_sync_type
== AV_SYNC_EXTERNAL_CLOCK
)) {
639 /* if video is slave, we try to correct big delays by
640 duplicating or deleting a frame */
641 ref_clock
= get_master_clock(is
);
642 diff
= vp
->pts
- ref_clock
;
644 /* skip or repeat frame. We take into account the
645 delay to compute the threshold. I still don't know
646 if it is the best guess */
647 sync_threshold
= AV_SYNC_THRESHOLD
;
648 if (delay
> sync_threshold
)
649 sync_threshold
= delay
;
650 if (fabs(diff
) < AV_NOSYNC_THRESHOLD
) {
651 if (diff
<= -sync_threshold
)
653 else if (diff
>= sync_threshold
)
658 is
->frame_timer
+= delay
;
659 /* compute the REAL delay (we need to do that to avoid
661 actual_delay
= is
->frame_timer
- (av_gettime() / 1000000.0);
662 if (actual_delay
< 0.010) {
663 /* XXX: should skip picture */
664 actual_delay
= 0.010;
666 /* launch timer for next picture */
667 schedule_refresh(is
, (int)(actual_delay
* 1000 + 0.5));
669 #if defined(DEBUG_SYNC)
670 printf("video: delay=%0.3f actual_delay=%0.3f pts=%0.3f A-V=%f\n",
671 delay
, actual_delay
, vp
->pts
, -diff
);
674 /* display picture */
677 /* update queue size and signal for next picture */
678 if (++is
->pictq_rindex
== VIDEO_PICTURE_QUEUE_SIZE
)
679 is
->pictq_rindex
= 0;
681 SDL_LockMutex(is
->pictq_mutex
);
683 SDL_CondSignal(is
->pictq_cond
);
684 SDL_UnlockMutex(is
->pictq_mutex
);
686 } else if (is
->audio_st
) {
687 /* draw the next audio frame */
689 schedule_refresh(is
, 40);
691 /* if only audio stream, then display the audio bars (better
692 than nothing, just to test the implementation */
694 /* display picture */
697 schedule_refresh(is
, 100);
700 static int64_t last_time
;
705 cur_time
= av_gettime();
706 if (!last_time
|| (cur_time
- last_time
) >= 500 * 1000) {
710 aqsize
= is
->audioq
.size
;
712 vqsize
= is
->videoq
.size
;
714 if (is
->audio_st
&& is
->video_st
)
715 av_diff
= get_audio_clock(is
) - get_video_clock(is
);
716 printf("%7.2f A-V:%7.3f aq=%5dKB vq=%5dKB \r",
717 get_master_clock(is
), av_diff
, aqsize
/ 1024, vqsize
/ 1024);
719 last_time
= cur_time
;
724 /* allocate a picture (needs to do that in main thread to avoid
725 potential locking problems */
726 static void alloc_picture(void *opaque
)
728 VideoState
*is
= opaque
;
731 vp
= &is
->pictq
[is
->pictq_windex
];
734 SDL_FreeYUVOverlay(vp
->bmp
);
737 /* XXX: use generic function */
738 /* XXX: disable overlay if no hardware acceleration or if RGB format */
739 switch(is
->video_st
->codec
.pix_fmt
) {
740 case PIX_FMT_YUV420P
:
741 case PIX_FMT_YUV422P
:
742 case PIX_FMT_YUV444P
:
744 case PIX_FMT_YUV410P
:
745 case PIX_FMT_YUV411P
:
753 vp
->bmp
= SDL_CreateYUVOverlay(is
->video_st
->codec
.width
,
754 is
->video_st
->codec
.height
,
757 vp
->width
= is
->video_st
->codec
.width
;
758 vp
->height
= is
->video_st
->codec
.height
;
760 SDL_LockMutex(is
->pictq_mutex
);
762 SDL_CondSignal(is
->pictq_cond
);
763 SDL_UnlockMutex(is
->pictq_mutex
);
766 static int queue_picture(VideoState
*is
, AVFrame
*src_frame
, double pts
)
772 /* wait until we have space to put a new picture */
773 SDL_LockMutex(is
->pictq_mutex
);
774 while (is
->pictq_size
>= VIDEO_PICTURE_QUEUE_SIZE
&&
775 !is
->videoq
.abort_request
) {
776 SDL_CondWait(is
->pictq_cond
, is
->pictq_mutex
);
778 SDL_UnlockMutex(is
->pictq_mutex
);
780 if (is
->videoq
.abort_request
)
783 vp
= &is
->pictq
[is
->pictq_windex
];
785 /* alloc or resize hardware picture buffer */
787 vp
->width
!= is
->video_st
->codec
.width
||
788 vp
->height
!= is
->video_st
->codec
.height
) {
793 /* the allocation must be done in the main thread to avoid
795 event
.type
= FF_ALLOC_EVENT
;
796 event
.user
.data1
= is
;
797 SDL_PushEvent(&event
);
799 /* wait until the picture is allocated */
800 SDL_LockMutex(is
->pictq_mutex
);
801 while (!vp
->allocated
&& !is
->videoq
.abort_request
) {
802 SDL_CondWait(is
->pictq_cond
, is
->pictq_mutex
);
804 SDL_UnlockMutex(is
->pictq_mutex
);
806 if (is
->videoq
.abort_request
)
810 /* if the frame is not skipped, then display it */
812 /* get a pointer on the bitmap */
813 SDL_LockYUVOverlay (vp
->bmp
);
815 dst_pix_fmt
= PIX_FMT_YUV420P
;
816 pict
.data
[0] = vp
->bmp
->pixels
[0];
817 pict
.data
[1] = vp
->bmp
->pixels
[2];
818 pict
.data
[2] = vp
->bmp
->pixels
[1];
820 pict
.linesize
[0] = vp
->bmp
->pitches
[0];
821 pict
.linesize
[1] = vp
->bmp
->pitches
[2];
822 pict
.linesize
[2] = vp
->bmp
->pitches
[1];
823 img_convert(&pict
, dst_pix_fmt
,
824 (AVPicture
*)src_frame
, is
->video_st
->codec
.pix_fmt
,
825 is
->video_st
->codec
.width
, is
->video_st
->codec
.height
);
826 /* update the bitmap content */
827 SDL_UnlockYUVOverlay(vp
->bmp
);
831 /* now we can update the picture count */
832 if (++is
->pictq_windex
== VIDEO_PICTURE_QUEUE_SIZE
)
833 is
->pictq_windex
= 0;
834 SDL_LockMutex(is
->pictq_mutex
);
836 SDL_UnlockMutex(is
->pictq_mutex
);
841 /* compute the exact PTS for the picture if it is omitted in the stream */
842 static int output_picture2(VideoState
*is
, AVFrame
*src_frame
, double pts1
)
844 double frame_delay
, pts
;
848 /* if B frames are present, and if the current picture is a I
849 or P frame, we use the last pts */
850 if (is
->video_st
->codec
.has_b_frames
&&
851 src_frame
->pict_type
!= FF_B_TYPE
) {
853 pts
= is
->video_last_P_pts
;
854 /* get the pts for the next I or P frame if present */
855 is
->video_last_P_pts
= pts1
;
859 /* update video clock with pts, if present */
860 is
->video_clock
= pts
;
862 pts
= is
->video_clock
;
864 /* update video clock for next frame */
865 frame_delay
= (double)is
->video_st
->codec
.frame_rate_base
/
866 (double)is
->video_st
->codec
.frame_rate
;
867 /* for MPEG2, the frame can be repeated, so we update the
869 if (src_frame
->repeat_pict
) {
870 frame_delay
+= src_frame
->repeat_pict
* (frame_delay
* 0.5);
872 is
->video_clock
+= frame_delay
;
874 #if defined(DEBUG_SYNC) && 0
877 if (src_frame
->pict_type
== FF_B_TYPE
)
879 else if (src_frame
->pict_type
== FF_I_TYPE
)
883 printf("frame_type=%c clock=%0.3f pts=%0.3f\n",
887 return queue_picture(is
, src_frame
, pts
);
890 static int video_thread(void *arg
)
892 VideoState
*is
= arg
;
893 AVPacket pkt1
, *pkt
= &pkt1
;
894 int len1
, got_picture
;
895 AVFrame
*frame
= avcodec_alloc_frame();
899 while (is
->paused
&& !is
->videoq
.abort_request
) {
902 if (packet_queue_get(&is
->videoq
, pkt
, 1) < 0)
904 /* NOTE: ipts is the PTS of the _first_ picture beginning in
905 this packet, if any */
907 if (pkt
->pts
!= AV_NOPTS_VALUE
)
908 pts
= (double)pkt
->pts
/ AV_TIME_BASE
;
910 if (is
->video_st
->codec
.codec_id
== CODEC_ID_RAWVIDEO
) {
911 avpicture_fill((AVPicture
*)frame
, pkt
->data
,
912 is
->video_st
->codec
.pix_fmt
,
913 is
->video_st
->codec
.width
,
914 is
->video_st
->codec
.height
);
915 frame
->pict_type
= FF_I_TYPE
;
916 if (output_picture2(is
, frame
, pts
) < 0)
919 len1
= avcodec_decode_video(&is
->video_st
->codec
,
921 pkt
->data
, pkt
->size
);
925 if (output_picture2(is
, frame
, pts
) < 0)
932 stream_pause(cur_stream
);
939 /* copy samples for viewing in editor window */
940 static void update_sample_display(VideoState
*is
, short *samples
, int samples_size
)
942 int size
, len
, channels
;
944 channels
= is
->audio_st
->codec
.channels
;
946 size
= samples_size
/ sizeof(short);
948 len
= SAMPLE_ARRAY_SIZE
- is
->sample_array_index
;
951 memcpy(is
->sample_array
+ is
->sample_array_index
, samples
, len
* sizeof(short));
953 is
->sample_array_index
+= len
;
954 if (is
->sample_array_index
>= SAMPLE_ARRAY_SIZE
)
955 is
->sample_array_index
= 0;
960 /* return the new audio buffer size (samples can be added or deleted
961 to get better sync if video or external master clock) */
962 static int synchronize_audio(VideoState
*is
, short *samples
,
963 int samples_size1
, double pts
)
968 n
= 2 * is
->audio_st
->codec
.channels
;
969 samples_size
= samples_size1
;
971 /* if not master, then we try to remove or add samples to correct the clock */
972 if (((is
->av_sync_type
== AV_SYNC_VIDEO_MASTER
&& is
->video_st
) ||
973 is
->av_sync_type
== AV_SYNC_EXTERNAL_CLOCK
)) {
974 double diff
, avg_diff
;
975 int wanted_size
, min_size
, max_size
, nb_samples
;
977 ref_clock
= get_master_clock(is
);
978 diff
= get_audio_clock(is
) - ref_clock
;
980 if (diff
< AV_NOSYNC_THRESHOLD
) {
981 is
->audio_diff_cum
= diff
+ is
->audio_diff_avg_coef
* is
->audio_diff_cum
;
982 if (is
->audio_diff_avg_count
< AUDIO_DIFF_AVG_NB
) {
983 /* not enough measures to have a correct estimate */
984 is
->audio_diff_avg_count
++;
986 /* estimate the A-V difference */
987 avg_diff
= is
->audio_diff_cum
* (1.0 - is
->audio_diff_avg_coef
);
989 if (fabs(avg_diff
) >= is
->audio_diff_threshold
) {
990 wanted_size
= samples_size
+ ((int)(diff
* is
->audio_st
->codec
.sample_rate
) * n
);
991 nb_samples
= samples_size
/ n
;
993 min_size
= ((nb_samples
* (100 - SAMPLE_CORRECTION_PERCENT_MAX
)) / 100) * n
;
994 max_size
= ((nb_samples
* (100 + SAMPLE_CORRECTION_PERCENT_MAX
)) / 100) * n
;
995 if (wanted_size
< min_size
)
996 wanted_size
= min_size
;
997 else if (wanted_size
> max_size
)
998 wanted_size
= max_size
;
1000 /* add or remove samples to correction the synchro */
1001 if (wanted_size
< samples_size
) {
1002 /* remove samples */
1003 samples_size
= wanted_size
;
1004 } else if (wanted_size
> samples_size
) {
1005 uint8_t *samples_end
, *q
;
1009 nb
= (samples_size
- wanted_size
);
1010 samples_end
= (uint8_t *)samples
+ samples_size
- n
;
1011 q
= samples_end
+ n
;
1013 memcpy(q
, samples_end
, n
);
1017 samples_size
= wanted_size
;
1021 printf("diff=%f adiff=%f sample_diff=%d apts=%0.3f vpts=%0.3f %f\n",
1022 diff
, avg_diff
, samples_size
- samples_size1
,
1023 is
->audio_clock
, is
->video_clock
, is
->audio_diff_threshold
);
1027 /* too big difference : may be initial PTS errors, so
1029 is
->audio_diff_avg_count
= 0;
1030 is
->audio_diff_cum
= 0;
1034 return samples_size
;
1037 /* decode one audio frame and returns its uncompressed size */
1038 static int audio_decode_frame(VideoState
*is
, uint8_t *audio_buf
, double *pts_ptr
)
1040 AVPacket
*pkt
= &is
->audio_pkt
;
1041 int n
, len1
, data_size
;
1045 /* NOTE: the audio packet can contain several frames */
1046 while (is
->audio_pkt_size
> 0) {
1047 len1
= avcodec_decode_audio(&is
->audio_st
->codec
,
1048 (int16_t *)audio_buf
, &data_size
,
1049 is
->audio_pkt_data
, is
->audio_pkt_size
);
1051 /* if error, we skip the frame */
1052 is
->audio_pkt_size
= 0;
1056 is
->audio_pkt_data
+= len1
;
1057 is
->audio_pkt_size
-= len1
;
1060 /* if no pts, then compute it */
1061 pts
= is
->audio_clock
;
1063 n
= 2 * is
->audio_st
->codec
.channels
;
1064 is
->audio_clock
+= (double)data_size
/
1065 (double)(n
* is
->audio_st
->codec
.sample_rate
);
1066 #if defined(DEBUG_SYNC)
1068 static double last_clock
;
1069 printf("audio: delay=%0.3f clock=%0.3f pts=%0.3f\n",
1070 is
->audio_clock
- last_clock
,
1071 is
->audio_clock
, pts
);
1072 last_clock
= is
->audio_clock
;
1078 /* free the current packet */
1080 av_free_packet(pkt
);
1082 if (is
->paused
|| is
->audioq
.abort_request
) {
1086 /* read next packet */
1087 if (packet_queue_get(&is
->audioq
, pkt
, 1) < 0)
1089 is
->audio_pkt_data
= pkt
->data
;
1090 is
->audio_pkt_size
= pkt
->size
;
1092 /* if update the audio clock with the pts */
1093 if (pkt
->pts
!= AV_NOPTS_VALUE
) {
1094 is
->audio_clock
= (double)pkt
->pts
/ AV_TIME_BASE
;
1099 /* get the current audio output buffer size, in samples. With SDL, we
1100 cannot have a precise information */
1101 static int audio_write_get_buf_size(VideoState
*is
)
1103 return is
->audio_hw_buf_size
- is
->audio_buf_index
;
1107 /* prepare a new audio buffer */
1108 void sdl_audio_callback(void *opaque
, Uint8
*stream
, int len
)
1110 VideoState
*is
= opaque
;
1111 int audio_size
, len1
;
1114 audio_callback_time
= av_gettime();
1117 if (is
->audio_buf_index
>= is
->audio_buf_size
) {
1118 audio_size
= audio_decode_frame(is
, is
->audio_buf
, &pts
);
1119 if (audio_size
< 0) {
1120 /* if error, just output silence */
1121 is
->audio_buf_size
= 1024;
1122 memset(is
->audio_buf
, 0, is
->audio_buf_size
);
1125 update_sample_display(is
, (int16_t *)is
->audio_buf
, audio_size
);
1126 audio_size
= synchronize_audio(is
, (int16_t *)is
->audio_buf
, audio_size
,
1128 is
->audio_buf_size
= audio_size
;
1130 is
->audio_buf_index
= 0;
1132 len1
= is
->audio_buf_size
- is
->audio_buf_index
;
1135 memcpy(stream
, (uint8_t *)is
->audio_buf
+ is
->audio_buf_index
, len1
);
1138 is
->audio_buf_index
+= len1
;
1143 /* open a given stream. Return 0 if OK */
1144 static int stream_component_open(VideoState
*is
, int stream_index
)
1146 AVFormatContext
*ic
= is
->ic
;
1147 AVCodecContext
*enc
;
1149 SDL_AudioSpec wanted_spec
, spec
;
1151 if (stream_index
< 0 || stream_index
>= ic
->nb_streams
)
1153 enc
= &ic
->streams
[stream_index
]->codec
;
1155 /* prepare audio output */
1156 if (enc
->codec_type
== CODEC_TYPE_AUDIO
) {
1157 wanted_spec
.freq
= enc
->sample_rate
;
1158 wanted_spec
.format
= AUDIO_S16SYS
;
1159 /* hack for AC3. XXX: suppress that */
1160 if (enc
->channels
> 2)
1162 wanted_spec
.channels
= enc
->channels
;
1163 wanted_spec
.silence
= 0;
1164 wanted_spec
.samples
= SDL_AUDIO_BUFFER_SIZE
;
1165 wanted_spec
.callback
= sdl_audio_callback
;
1166 wanted_spec
.userdata
= is
;
1167 if (SDL_OpenAudio(&wanted_spec
, &spec
) < 0) {
1168 fprintf(stderr
, "SDL_OpenAudio: %s\n", SDL_GetError());
1171 is
->audio_hw_buf_size
= spec
.size
;
1174 codec
= avcodec_find_decoder(enc
->codec_id
);
1175 enc
->debug_mv
= debug_mv
;
1177 enc
->workaround_bugs
= workaround_bugs
;
1178 enc
->lowres
= lowres
;
1179 enc
->idct_algo
= idct
;
1180 if(fast
) enc
->flags2
|= CODEC_FLAG2_FAST
;
1182 avcodec_open(enc
, codec
) < 0)
1184 #if defined(HAVE_PTHREADS) || defined(HAVE_W32THREADS)
1186 avcodec_thread_init(enc
, thread_count
);
1188 enc
->thread_count
= thread_count
;
1189 switch(enc
->codec_type
) {
1190 case CODEC_TYPE_AUDIO
:
1191 is
->audio_stream
= stream_index
;
1192 is
->audio_st
= ic
->streams
[stream_index
];
1193 is
->audio_buf_size
= 0;
1194 is
->audio_buf_index
= 0;
1196 /* init averaging filter */
1197 is
->audio_diff_avg_coef
= exp(log(0.01) / AUDIO_DIFF_AVG_NB
);
1198 is
->audio_diff_avg_count
= 0;
1199 /* since we do not have a precise anough audio fifo fullness,
1200 we correct audio sync only if larger than this threshold */
1201 is
->audio_diff_threshold
= 2.0 * SDL_AUDIO_BUFFER_SIZE
/ enc
->sample_rate
;
1203 memset(&is
->audio_pkt
, 0, sizeof(is
->audio_pkt
));
1204 packet_queue_init(&is
->audioq
);
1207 case CODEC_TYPE_VIDEO
:
1208 is
->video_stream
= stream_index
;
1209 is
->video_st
= ic
->streams
[stream_index
];
1211 is
->frame_last_delay
= 40e-3;
1212 is
->frame_timer
= (double)av_gettime() / 1000000.0;
1213 is
->video_current_pts_time
= av_gettime();
1215 packet_queue_init(&is
->videoq
);
1216 is
->video_tid
= SDL_CreateThread(video_thread
, is
);
1224 static void stream_component_close(VideoState
*is
, int stream_index
)
1226 AVFormatContext
*ic
= is
->ic
;
1227 AVCodecContext
*enc
;
1229 enc
= &ic
->streams
[stream_index
]->codec
;
1231 switch(enc
->codec_type
) {
1232 case CODEC_TYPE_AUDIO
:
1233 packet_queue_abort(&is
->audioq
);
1237 packet_queue_end(&is
->audioq
);
1239 case CODEC_TYPE_VIDEO
:
1240 packet_queue_abort(&is
->videoq
);
1242 /* note: we also signal this mutex to make sure we deblock the
1243 video thread in all cases */
1244 SDL_LockMutex(is
->pictq_mutex
);
1245 SDL_CondSignal(is
->pictq_cond
);
1246 SDL_UnlockMutex(is
->pictq_mutex
);
1248 SDL_WaitThread(is
->video_tid
, NULL
);
1250 packet_queue_end(&is
->videoq
);
1257 switch(enc
->codec_type
) {
1258 case CODEC_TYPE_AUDIO
:
1259 is
->audio_st
= NULL
;
1260 is
->audio_stream
= -1;
1262 case CODEC_TYPE_VIDEO
:
1263 is
->video_st
= NULL
;
1264 is
->video_stream
= -1;
1271 void dump_stream_info(AVFormatContext
*s
)
1274 fprintf(stderr
, "Track: %d\n", s
->track
);
1275 if (s
->title
[0] != '\0')
1276 fprintf(stderr
, "Title: %s\n", s
->title
);
1277 if (s
->author
[0] != '\0')
1278 fprintf(stderr
, "Author: %s\n", s
->author
);
1279 if (s
->album
[0] != '\0')
1280 fprintf(stderr
, "Album: %s\n", s
->album
);
1282 fprintf(stderr
, "Year: %d\n", s
->year
);
1283 if (s
->genre
[0] != '\0')
1284 fprintf(stderr
, "Genre: %s\n", s
->genre
);
1287 /* since we have only one decoding thread, we can use a global
1288 variable instead of a thread local variable */
1289 static VideoState
*global_video_state
;
1291 static int decode_interrupt_cb(void)
1293 return (global_video_state
&& global_video_state
->abort_request
);
1296 /* this thread gets the stream from the disk or the network */
1297 static int decode_thread(void *arg
)
1299 VideoState
*is
= arg
;
1300 AVFormatContext
*ic
;
1301 int err
, i
, ret
, video_index
, audio_index
, use_play
;
1302 AVPacket pkt1
, *pkt
= &pkt1
;
1303 AVFormatParameters params
, *ap
= ¶ms
;
1307 is
->video_stream
= -1;
1308 is
->audio_stream
= -1;
1310 global_video_state
= is
;
1311 url_set_interrupt_cb(decode_interrupt_cb
);
1313 memset(ap
, 0, sizeof(*ap
));
1314 ap
->image_format
= image_format
;
1315 ap
->initial_pause
= 1; /* we force a pause when starting an RTSP
1318 err
= av_open_input_file(&ic
, is
->filename
, is
->iformat
, 0, ap
);
1320 print_error(is
->filename
, err
);
1325 #ifdef CONFIG_NETWORK
1326 use_play
= (ic
->iformat
== &rtsp_demux
);
1331 err
= av_find_stream_info(ic
);
1333 fprintf(stderr
, "%s: could not find codec parameters\n", is
->filename
);
1339 /* if seeking requested, we execute it */
1340 if (start_time
!= AV_NOPTS_VALUE
) {
1343 timestamp
= start_time
;
1344 /* add the stream start time */
1345 if (ic
->start_time
!= AV_NOPTS_VALUE
)
1346 timestamp
+= ic
->start_time
;
1347 ret
= av_seek_frame(ic
, -1, timestamp
);
1349 fprintf(stderr
, "%s: could not seek to position %0.3f\n",
1350 is
->filename
, (double)timestamp
/ AV_TIME_BASE
);
1354 /* now we can begin to play (RTSP stream only) */
1358 err
= av_find_stream_info(ic
);
1360 fprintf(stderr
, "%s: could not find codec parameters\n", is
->filename
);
1366 for(i
= 0; i
< ic
->nb_streams
; i
++) {
1367 AVCodecContext
*enc
= &ic
->streams
[i
]->codec
;
1368 switch(enc
->codec_type
) {
1369 case CODEC_TYPE_AUDIO
:
1370 if (audio_index
< 0 && !audio_disable
)
1373 case CODEC_TYPE_VIDEO
:
1374 if (video_index
< 0 && !video_disable
)
1382 dump_format(ic
, 0, is
->filename
, 0);
1383 dump_stream_info(ic
);
1386 /* open the streams */
1387 if (audio_index
>= 0) {
1388 stream_component_open(is
, audio_index
);
1391 if (video_index
>= 0) {
1392 stream_component_open(is
, video_index
);
1394 if (!display_disable
)
1398 if (is
->video_stream
< 0 && is
->audio_stream
< 0) {
1399 fprintf(stderr
, "%s: could not open codecs\n", is
->filename
);
1405 if (is
->abort_request
)
1407 #ifdef CONFIG_NETWORK
1408 if (is
->paused
!= is
->last_paused
) {
1409 is
->last_paused
= is
->paused
;
1415 if (is
->paused
&& ic
->iformat
== &rtsp_demux
) {
1416 /* wait 10 ms to avoid trying to get another packet */
1423 /* XXX: must lock decoder threads */
1424 ret
= av_seek_frame(is
->ic
, -1, is
->seek_pos
);
1426 fprintf(stderr
, "%s: error while seeking\n", is
->ic
->filename
);
1428 if (is
->audio_stream
>= 0) {
1429 packet_queue_flush(&is
->audioq
);
1431 if (is
->video_stream
>= 0) {
1432 packet_queue_flush(&is
->videoq
);
1433 avcodec_flush_buffers(&ic
->streams
[video_index
]->codec
);
1439 /* if the queue are full, no need to read more */
1440 if (is
->audioq
.size
> MAX_AUDIOQ_SIZE
||
1441 is
->videoq
.size
> MAX_VIDEOQ_SIZE
||
1442 url_feof(&ic
->pb
)) {
1447 ret
= av_read_frame(ic
, pkt
);
1451 if (pkt
->stream_index
== is
->audio_stream
) {
1452 packet_queue_put(&is
->audioq
, pkt
);
1453 } else if (pkt
->stream_index
== is
->video_stream
) {
1454 packet_queue_put(&is
->videoq
, pkt
);
1456 av_free_packet(pkt
);
1459 /* wait until the end */
1460 while (!is
->abort_request
) {
1466 /* disable interrupting */
1467 global_video_state
= NULL
;
1469 /* close each stream */
1470 if (is
->audio_stream
>= 0)
1471 stream_component_close(is
, is
->audio_stream
);
1472 if (is
->video_stream
>= 0)
1473 stream_component_close(is
, is
->video_stream
);
1475 av_close_input_file(is
->ic
);
1476 is
->ic
= NULL
; /* safety */
1478 url_set_interrupt_cb(NULL
);
1483 event
.type
= FF_QUIT_EVENT
;
1484 event
.user
.data1
= is
;
1485 SDL_PushEvent(&event
);
1490 static VideoState
*stream_open(const char *filename
, AVInputFormat
*iformat
)
1494 is
= av_mallocz(sizeof(VideoState
));
1497 pstrcpy(is
->filename
, sizeof(is
->filename
), filename
);
1498 is
->iformat
= iformat
;
1500 is
->width
= screen
->w
;
1501 is
->height
= screen
->h
;
1506 /* start video display */
1507 is
->pictq_mutex
= SDL_CreateMutex();
1508 is
->pictq_cond
= SDL_CreateCond();
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
);
1542 void stream_cycle_channel(VideoState
*is
, int codec_type
)
1544 AVFormatContext
*ic
= is
->ic
;
1545 int start_index
, stream_index
;
1548 if (codec_type
== CODEC_TYPE_VIDEO
)
1549 start_index
= is
->video_stream
;
1551 start_index
= is
->audio_stream
;
1552 if (start_index
< 0)
1554 stream_index
= start_index
;
1556 if (++stream_index
>= is
->ic
->nb_streams
)
1558 if (stream_index
== start_index
)
1560 st
= ic
->streams
[stream_index
];
1561 if (st
->codec
.codec_type
== codec_type
) {
1562 /* check that parameters are OK */
1563 switch(codec_type
) {
1564 case CODEC_TYPE_AUDIO
:
1565 if (st
->codec
.sample_rate
!= 0 &&
1566 st
->codec
.channels
!= 0)
1569 case CODEC_TYPE_VIDEO
:
1577 stream_component_close(is
, start_index
);
1578 stream_component_open(is
, stream_index
);
1582 void toggle_full_screen(void)
1585 is_full_screen
= !is_full_screen
;
1586 if (!fs_screen_width
) {
1587 /* use default SDL method */
1588 SDL_WM_ToggleFullScreen(screen
);
1590 /* use the recorded resolution */
1591 flags
= SDL_HWSURFACE
|SDL_ASYNCBLIT
|SDL_HWACCEL
;
1592 if (is_full_screen
) {
1593 w
= fs_screen_width
;
1594 h
= fs_screen_height
;
1595 flags
|= SDL_FULLSCREEN
;
1599 flags
|= SDL_RESIZABLE
;
1601 screen
= SDL_SetVideoMode(w
, h
, 0, flags
);
1602 cur_stream
->width
= w
;
1603 cur_stream
->height
= h
;
1607 void toggle_pause(void)
1610 stream_pause(cur_stream
);
1614 void step_to_next_frame(void)
1617 if (cur_stream
->paused
)
1618 cur_stream
->paused
=0;
1619 cur_stream
->video_current_pts
= get_video_clock(cur_stream
);
1627 stream_close(cur_stream
);
1636 void toggle_audio_display(void)
1639 cur_stream
->show_audio
= !cur_stream
->show_audio
;
1643 /* handle an event sent by the GUI */
1644 void event_loop(void)
1647 double incr
, pos
, frac
;
1650 SDL_WaitEvent(&event
);
1651 switch(event
.type
) {
1653 switch(event
.key
.keysym
.sym
) {
1659 toggle_full_screen();
1665 case SDLK_s
: //S: Step to next frame
1666 step_to_next_frame();
1670 stream_cycle_channel(cur_stream
, CODEC_TYPE_AUDIO
);
1674 stream_cycle_channel(cur_stream
, CODEC_TYPE_VIDEO
);
1677 toggle_audio_display();
1692 pos
= get_master_clock(cur_stream
);
1694 stream_seek(cur_stream
, (int64_t)(pos
* AV_TIME_BASE
));
1701 case SDL_MOUSEBUTTONDOWN
:
1704 int tns
, thh
, tmm
, tss
;
1705 tns
= cur_stream
->ic
->duration
/1000000LL;
1707 tmm
= (tns
%3600)/60;
1709 frac
= (double)event
.button
.x
/(double)cur_stream
->width
;
1714 fprintf(stderr
, "Seek to %2.0f%% (%2d:%02d:%02d) of total duration (%2d:%02d:%02d) \n", frac
*100,
1715 hh
, mm
, ss
, thh
, tmm
, tss
);
1716 stream_seek(cur_stream
, (int64_t)(cur_stream
->ic
->start_time
+frac
*cur_stream
->ic
->duration
));
1719 case SDL_VIDEORESIZE
:
1721 screen
= SDL_SetVideoMode(event
.resize
.w
, event
.resize
.h
, 0,
1722 SDL_HWSURFACE
|SDL_RESIZABLE
|SDL_ASYNCBLIT
|SDL_HWACCEL
);
1723 cur_stream
->width
= event
.resize
.w
;
1724 cur_stream
->height
= event
.resize
.h
;
1731 case FF_ALLOC_EVENT
:
1732 alloc_picture(event
.user
.data1
);
1734 case FF_REFRESH_EVENT
:
1735 video_refresh_timer(event
.user
.data1
);
1743 void opt_width(const char *arg
)
1745 screen_width
= atoi(arg
);
1748 void opt_height(const char *arg
)
1750 screen_height
= atoi(arg
);
1753 static void opt_format(const char *arg
)
1755 file_iformat
= av_find_input_format(arg
);
1756 if (!file_iformat
) {
1757 fprintf(stderr
, "Unknown input format: %s\n", arg
);
1762 static void opt_image_format(const char *arg
)
1766 for(f
= first_image_format
; f
!= NULL
; f
= f
->next
) {
1767 if (!strcmp(arg
, f
->name
))
1771 fprintf(stderr
, "Unknown image format: '%s'\n", arg
);
1777 #ifdef CONFIG_NETWORK
1778 void opt_rtp_tcp(void)
1780 /* only tcp protocol */
1781 rtsp_default_protocols
= (1 << RTSP_PROTOCOL_RTP_TCP
);
1785 void opt_sync(const char *arg
)
1787 if (!strcmp(arg
, "audio"))
1788 av_sync_type
= AV_SYNC_AUDIO_MASTER
;
1789 else if (!strcmp(arg
, "video"))
1790 av_sync_type
= AV_SYNC_VIDEO_MASTER
;
1791 else if (!strcmp(arg
, "ext"))
1792 av_sync_type
= AV_SYNC_EXTERNAL_CLOCK
;
1797 void opt_seek(const char *arg
)
1799 start_time
= parse_date(arg
, 1);
1802 static void opt_debug(const char *arg
)
1807 static void opt_vismv(const char *arg
)
1809 debug_mv
= atoi(arg
);
1812 static void opt_thread_count(const char *arg
)
1814 thread_count
= atoi(arg
);
1815 #if !defined(HAVE_PTHREADS) && !defined(HAVE_W32THREADS)
1816 fprintf(stderr
, "Warning: not compiled with thread support, using thread emulation\n");
1820 const OptionDef options
[] = {
1821 { "h", 0, {(void*)show_help
}, "show help" },
1822 { "x", HAS_ARG
, {(void*)opt_width
}, "force displayed width", "width" },
1823 { "y", HAS_ARG
, {(void*)opt_height
}, "force displayed height", "height" },
1825 /* disabled as SDL/X11 does not support it correctly on application launch */
1826 { "fs", OPT_BOOL
, {(void*)&is_full_screen
}, "force full screen" },
1828 { "an", OPT_BOOL
, {(void*)&audio_disable
}, "disable audio" },
1829 { "vn", OPT_BOOL
, {(void*)&video_disable
}, "disable video" },
1830 { "ss", HAS_ARG
, {(void*)&opt_seek
}, "seek to a given position in seconds", "pos" },
1831 { "nodisp", OPT_BOOL
, {(void*)&display_disable
}, "disable graphical display" },
1832 { "f", HAS_ARG
, {(void*)opt_format
}, "force format", "fmt" },
1833 { "img", HAS_ARG
, {(void*)opt_image_format
}, "force image format", "img_fmt" },
1834 { "stats", OPT_BOOL
| OPT_EXPERT
, {(void*)&show_status
}, "show status", "" },
1835 { "debug", HAS_ARG
| OPT_EXPERT
, {(void*)opt_debug
}, "print specific debug info", "" },
1836 { "bug", OPT_INT
| HAS_ARG
| OPT_EXPERT
, {(void*)&workaround_bugs
}, "workaround bugs", "" },
1837 { "vismv", HAS_ARG
| OPT_EXPERT
, {(void*)opt_vismv
}, "visualize motion vectors", "" },
1838 { "fast", OPT_BOOL
| OPT_EXPERT
, {(void*)&fast
}, "non spec compliant optimizations", "" },
1839 { "lowres", OPT_INT
| HAS_ARG
| OPT_EXPERT
, {(void*)&lowres
}, "", "" },
1840 { "idct", OPT_INT
| HAS_ARG
| OPT_EXPERT
, {(void*)&idct
}, "set idct algo", "algo" },
1841 #ifdef CONFIG_NETWORK
1842 { "rtp_tcp", OPT_EXPERT
, {(void*)&opt_rtp_tcp
}, "force RTP/TCP protocol usage", "" },
1844 { "sync", HAS_ARG
| OPT_EXPERT
, {(void*)opt_sync
}, "set audio-video sync. type (type=audio/video/ext)", "type" },
1845 { "threads", HAS_ARG
| OPT_EXPERT
, {(void*)opt_thread_count
}, "thread count", "count" },
1849 void show_help(void)
1851 printf("ffplay version " FFMPEG_VERSION
", Copyright (c) 2003 Fabrice Bellard\n"
1852 "usage: ffplay [options] input_file\n"
1853 "Simple media player\n");
1855 show_help_options(options
, "Main options:\n",
1857 show_help_options(options
, "\nAdvanced options:\n",
1858 OPT_EXPERT
, OPT_EXPERT
);
1859 printf("\nWhile playing:\n"
1861 "f toggle full screen\n"
1863 "a cycle audio channel\n"
1864 "v cycle video channel\n"
1865 "w show audio waves\n"
1866 "left/right seek backward/forward 10 seconds\n"
1867 "down/up seek backward/forward 1 minute\n"
1868 "mouse click seek to percentage in file corresponding to fraction of width\n"
1873 void parse_arg_file(const char *filename
)
1875 if (!strcmp(filename
, "-"))
1877 input_filename
= filename
;
1880 /* Called from the main */
1881 int main(int argc
, char **argv
)
1885 /* register all codecs, demux and protocols */
1888 parse_options(argc
, argv
, options
);
1890 if (!input_filename
)
1893 if (display_disable
) {
1896 flags
= SDL_INIT_VIDEO
| SDL_INIT_AUDIO
| SDL_INIT_TIMER
;
1897 #ifndef CONFIG_WIN32
1898 flags
|= SDL_INIT_EVENTTHREAD
; /* Not supported on win32 */
1900 if (SDL_Init (flags
)) {
1901 fprintf(stderr
, "Could not initialize SDL - %s\n", SDL_GetError());
1905 if (!display_disable
) {
1907 /* save the screen resolution... SDL should allow full screen
1908 by resizing the window */
1911 dpy
= XOpenDisplay(NULL
);
1913 fs_screen_width
= DisplayWidth(dpy
, DefaultScreen(dpy
));
1914 fs_screen_height
= DisplayHeight(dpy
, DefaultScreen(dpy
));
1919 flags
= SDL_HWSURFACE
|SDL_ASYNCBLIT
|SDL_HWACCEL
;
1920 if (is_full_screen
&& fs_screen_width
) {
1921 w
= fs_screen_width
;
1922 h
= fs_screen_height
;
1923 flags
|= SDL_FULLSCREEN
;
1927 flags
|= SDL_RESIZABLE
;
1929 screen
= SDL_SetVideoMode(w
, h
, 0, flags
);
1931 fprintf(stderr
, "SDL: could not set video mode - exiting\n");
1934 SDL_WM_SetCaption("FFplay", "FFplay");
1937 SDL_EventState(SDL_ACTIVEEVENT
, SDL_IGNORE
);
1938 SDL_EventState(SDL_MOUSEMOTION
, SDL_IGNORE
);
1939 SDL_EventState(SDL_SYSWMEVENT
, SDL_IGNORE
);
1940 SDL_EventState(SDL_USEREVENT
, SDL_IGNORE
);
1942 cur_stream
= stream_open(input_filename
, file_iformat
);