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
;
198 /* current context */
199 static int is_full_screen
;
200 static VideoState
*cur_stream
;
201 static int64_t audio_callback_time
;
203 #define FF_ALLOC_EVENT (SDL_USEREVENT)
204 #define FF_REFRESH_EVENT (SDL_USEREVENT + 1)
205 #define FF_QUIT_EVENT (SDL_USEREVENT + 2)
209 /* packet queue handling */
210 static void packet_queue_init(PacketQueue
*q
)
212 memset(q
, 0, sizeof(PacketQueue
));
213 q
->mutex
= SDL_CreateMutex();
214 q
->cond
= SDL_CreateCond();
217 static void packet_queue_flush(PacketQueue
*q
)
219 AVPacketList
*pkt
, *pkt1
;
221 for(pkt
= q
->first_pkt
; pkt
!= NULL
; pkt
= pkt1
) {
223 av_free_packet(&pkt
->pkt
);
232 static void packet_queue_end(PacketQueue
*q
)
234 packet_queue_flush(q
);
235 SDL_DestroyMutex(q
->mutex
);
236 SDL_DestroyCond(q
->cond
);
239 static int packet_queue_put(PacketQueue
*q
, AVPacket
*pkt
)
243 /* duplicate the packet */
244 if (av_dup_packet(pkt
) < 0)
247 pkt1
= av_malloc(sizeof(AVPacketList
));
254 SDL_LockMutex(q
->mutex
);
260 q
->last_pkt
->next
= pkt1
;
263 q
->size
+= pkt1
->pkt
.size
;
264 /* XXX: should duplicate packet data in DV case */
265 SDL_CondSignal(q
->cond
);
267 SDL_UnlockMutex(q
->mutex
);
271 static void packet_queue_abort(PacketQueue
*q
)
273 SDL_LockMutex(q
->mutex
);
275 q
->abort_request
= 1;
277 SDL_CondSignal(q
->cond
);
279 SDL_UnlockMutex(q
->mutex
);
282 /* return < 0 if aborted, 0 if no packet and > 0 if packet. */
283 static int packet_queue_get(PacketQueue
*q
, AVPacket
*pkt
, int block
)
288 SDL_LockMutex(q
->mutex
);
291 if (q
->abort_request
) {
298 q
->first_pkt
= pkt1
->next
;
302 q
->size
-= pkt1
->pkt
.size
;
311 SDL_CondWait(q
->cond
, q
->mutex
);
314 SDL_UnlockMutex(q
->mutex
);
318 static inline void fill_rectangle(SDL_Surface
*screen
,
319 int x
, int y
, int w
, int h
, int color
)
326 SDL_FillRect(screen
, &rect
, color
);
330 /* draw only the border of a rectangle */
331 void fill_border(VideoState
*s
, int x
, int y
, int w
, int h
, int color
)
335 /* fill the background */
339 w2
= s
->width
- (x
+ w
);
345 h2
= s
->height
- (y
+ h
);
348 fill_rectangle(screen
,
352 fill_rectangle(screen
,
353 s
->xleft
+ s
->width
- w2
, s
->ytop
,
356 fill_rectangle(screen
,
357 s
->xleft
+ w1
, s
->ytop
,
358 s
->width
- w1
- w2
, h1
,
360 fill_rectangle(screen
,
361 s
->xleft
+ w1
, s
->ytop
+ s
->height
- h2
,
362 s
->width
- w1
- w2
, h2
,
367 static void video_image_display(VideoState
*is
)
371 int width
, height
, x
, y
;
374 vp
= &is
->pictq
[is
->pictq_rindex
];
376 /* XXX: use variable in the frame */
377 if (is
->video_st
->codec
.sample_aspect_ratio
.num
== 0)
380 aspect_ratio
= av_q2d(is
->video_st
->codec
.sample_aspect_ratio
)
381 * is
->video_st
->codec
.width
/ is
->video_st
->codec
.height
;;
382 if (aspect_ratio
<= 0.0)
383 aspect_ratio
= (float)is
->video_st
->codec
.width
/
384 (float)is
->video_st
->codec
.height
;
385 /* if an active format is indicated, then it overrides the
388 if (is
->video_st
->codec
.dtg_active_format
!= is
->dtg_active_format
) {
389 is
->dtg_active_format
= is
->video_st
->codec
.dtg_active_format
;
390 printf("dtg_active_format=%d\n", is
->dtg_active_format
);
394 switch(is
->video_st
->codec
.dtg_active_format
) {
395 case FF_DTG_AFD_SAME
:
400 aspect_ratio
= 4.0 / 3.0;
402 case FF_DTG_AFD_16_9
:
403 aspect_ratio
= 16.0 / 9.0;
405 case FF_DTG_AFD_14_9
:
406 aspect_ratio
= 14.0 / 9.0;
408 case FF_DTG_AFD_4_3_SP_14_9
:
409 aspect_ratio
= 14.0 / 9.0;
411 case FF_DTG_AFD_16_9_SP_14_9
:
412 aspect_ratio
= 14.0 / 9.0;
414 case FF_DTG_AFD_SP_4_3
:
415 aspect_ratio
= 4.0 / 3.0;
420 /* XXX: we suppose the screen has a 1.0 pixel ratio */
422 width
= ((int)rint(height
* aspect_ratio
)) & -3;
423 if (width
> is
->width
) {
425 height
= ((int)rint(width
/ aspect_ratio
)) & -3;
427 x
= (is
->width
- width
) / 2;
428 y
= (is
->height
- height
) / 2;
429 if (!is
->no_background
) {
430 /* fill the background */
431 // fill_border(is, x, y, width, height, QERGB(0x00, 0x00, 0x00));
433 is
->no_background
= 0;
435 rect
.x
= is
->xleft
+ x
;
436 rect
.y
= is
->xleft
+ y
;
439 SDL_DisplayYUVOverlay(vp
->bmp
, &rect
);
442 fill_rectangle(screen
,
443 is
->xleft
, is
->ytop
, is
->width
, is
->height
,
444 QERGB(0x00, 0x00, 0x00));
449 static inline int compute_mod(int a
, int b
)
458 static void video_audio_display(VideoState
*s
)
460 int i
, i_start
, x
, y1
, y
, ys
, delay
, n
, nb_display_channels
;
461 int ch
, channels
, h
, h2
, bgcolor
, fgcolor
;
464 /* compute display index : center on currently output samples */
465 channels
= s
->audio_st
->codec
.channels
;
466 nb_display_channels
= channels
;
469 delay
= audio_write_get_buf_size(s
);
472 /* to be more precise, we take into account the time spent since
473 the last buffer computation */
474 if (audio_callback_time
) {
475 time_diff
= av_gettime() - audio_callback_time
;
476 delay
+= (time_diff
* s
->audio_st
->codec
.sample_rate
) / 1000000;
479 delay
-= s
->width
/ 2;
480 if (delay
< s
->width
)
482 i_start
= compute_mod(s
->sample_array_index
- delay
* channels
, SAMPLE_ARRAY_SIZE
);
483 s
->last_i_start
= i_start
;
485 i_start
= s
->last_i_start
;
488 bgcolor
= SDL_MapRGB(screen
->format
, 0x00, 0x00, 0x00);
489 fill_rectangle(screen
,
490 s
->xleft
, s
->ytop
, s
->width
, s
->height
,
493 fgcolor
= SDL_MapRGB(screen
->format
, 0xff, 0xff, 0xff);
495 /* total height for one channel */
496 h
= s
->height
/ nb_display_channels
;
497 /* graph height / 2 */
499 for(ch
= 0;ch
< nb_display_channels
; ch
++) {
501 y1
= s
->ytop
+ ch
* h
+ (h
/ 2); /* position of center line */
502 for(x
= 0; x
< s
->width
; x
++) {
503 y
= (s
->sample_array
[i
] * h2
) >> 15;
510 fill_rectangle(screen
,
511 s
->xleft
+ x
, ys
, 1, y
,
514 if (i
>= SAMPLE_ARRAY_SIZE
)
515 i
-= SAMPLE_ARRAY_SIZE
;
519 fgcolor
= SDL_MapRGB(screen
->format
, 0x00, 0x00, 0xff);
521 for(ch
= 1;ch
< nb_display_channels
; ch
++) {
522 y
= s
->ytop
+ ch
* h
;
523 fill_rectangle(screen
,
524 s
->xleft
, y
, s
->width
, 1,
527 SDL_UpdateRect(screen
, s
->xleft
, s
->ytop
, s
->width
, s
->height
);
530 /* display the current picture, if any */
531 static void video_display(VideoState
*is
)
533 if (is
->audio_st
&& is
->show_audio
)
534 video_audio_display(is
);
535 else if (is
->video_st
)
536 video_image_display(is
);
539 static Uint32
sdl_refresh_timer_cb(Uint32 interval
, void *opaque
)
542 event
.type
= FF_REFRESH_EVENT
;
543 event
.user
.data1
= opaque
;
544 SDL_PushEvent(&event
);
545 return 0; /* 0 means stop timer */
548 /* schedule a video refresh in 'delay' ms */
549 static void schedule_refresh(VideoState
*is
, int delay
)
551 SDL_AddTimer(delay
, sdl_refresh_timer_cb
, is
);
554 /* get the current audio clock value */
555 static double get_audio_clock(VideoState
*is
)
558 int hw_buf_size
, bytes_per_sec
;
559 pts
= is
->audio_clock
;
560 hw_buf_size
= audio_write_get_buf_size(is
);
563 bytes_per_sec
= is
->audio_st
->codec
.sample_rate
*
564 2 * is
->audio_st
->codec
.channels
;
567 pts
-= (double)hw_buf_size
/ bytes_per_sec
;
571 /* get the current video clock value */
572 static double get_video_clock(VideoState
*is
)
578 delta
= (av_gettime() - is
->video_current_pts_time
) / 1000000.0;
580 return is
->video_current_pts
+ delta
;
583 /* get the current external clock value */
584 static double get_external_clock(VideoState
*is
)
588 return is
->external_clock
+ ((ti
- is
->external_clock_time
) * 1e-6);
591 /* get the current master clock value */
592 static double get_master_clock(VideoState
*is
)
596 if (is
->av_sync_type
== AV_SYNC_VIDEO_MASTER
) {
598 val
= get_video_clock(is
);
600 val
= get_audio_clock(is
);
601 } else if (is
->av_sync_type
== AV_SYNC_AUDIO_MASTER
) {
603 val
= get_audio_clock(is
);
605 val
= get_video_clock(is
);
607 val
= get_external_clock(is
);
612 /* seek in the stream */
613 static void stream_seek(VideoState
*is
, int64_t pos
, int rel
)
617 is
->seek_flags
= rel
< 0 ? AVSEEK_FLAG_BACKWARD
: 0;
620 /* pause or resume the video */
621 static void stream_pause(VideoState
*is
)
623 is
->paused
= !is
->paused
;
625 is
->video_current_pts
= get_video_clock(is
);
629 /* called to display each frame */
630 static void video_refresh_timer(void *opaque
)
632 VideoState
*is
= opaque
;
634 double actual_delay
, delay
, sync_threshold
, ref_clock
, diff
;
638 if (is
->pictq_size
== 0) {
639 /* if no picture, need to wait */
640 schedule_refresh(is
, 1);
642 /* dequeue the picture */
643 vp
= &is
->pictq
[is
->pictq_rindex
];
645 /* update current video pts */
646 is
->video_current_pts
= vp
->pts
;
647 is
->video_current_pts_time
= av_gettime();
649 /* compute nominal delay */
650 delay
= vp
->pts
- is
->frame_last_pts
;
651 if (delay
<= 0 || delay
>= 1.0) {
652 /* if incorrect delay, use previous one */
653 delay
= is
->frame_last_delay
;
655 is
->frame_last_delay
= delay
;
656 is
->frame_last_pts
= vp
->pts
;
658 /* update delay to follow master synchronisation source */
659 if (((is
->av_sync_type
== AV_SYNC_AUDIO_MASTER
&& is
->audio_st
) ||
660 is
->av_sync_type
== AV_SYNC_EXTERNAL_CLOCK
)) {
661 /* if video is slave, we try to correct big delays by
662 duplicating or deleting a frame */
663 ref_clock
= get_master_clock(is
);
664 diff
= vp
->pts
- ref_clock
;
666 /* skip or repeat frame. We take into account the
667 delay to compute the threshold. I still don't know
668 if it is the best guess */
669 sync_threshold
= AV_SYNC_THRESHOLD
;
670 if (delay
> sync_threshold
)
671 sync_threshold
= delay
;
672 if (fabs(diff
) < AV_NOSYNC_THRESHOLD
) {
673 if (diff
<= -sync_threshold
)
675 else if (diff
>= sync_threshold
)
680 is
->frame_timer
+= delay
;
681 /* compute the REAL delay (we need to do that to avoid
683 actual_delay
= is
->frame_timer
- (av_gettime() / 1000000.0);
684 if (actual_delay
< 0.010) {
685 /* XXX: should skip picture */
686 actual_delay
= 0.010;
688 /* launch timer for next picture */
689 schedule_refresh(is
, (int)(actual_delay
* 1000 + 0.5));
691 #if defined(DEBUG_SYNC)
692 printf("video: delay=%0.3f actual_delay=%0.3f pts=%0.3f A-V=%f\n",
693 delay
, actual_delay
, vp
->pts
, -diff
);
696 /* display picture */
699 /* update queue size and signal for next picture */
700 if (++is
->pictq_rindex
== VIDEO_PICTURE_QUEUE_SIZE
)
701 is
->pictq_rindex
= 0;
703 SDL_LockMutex(is
->pictq_mutex
);
705 SDL_CondSignal(is
->pictq_cond
);
706 SDL_UnlockMutex(is
->pictq_mutex
);
708 } else if (is
->audio_st
) {
709 /* draw the next audio frame */
711 schedule_refresh(is
, 40);
713 /* if only audio stream, then display the audio bars (better
714 than nothing, just to test the implementation */
716 /* display picture */
719 schedule_refresh(is
, 100);
722 static int64_t last_time
;
727 cur_time
= av_gettime();
728 if (!last_time
|| (cur_time
- last_time
) >= 500 * 1000) {
732 aqsize
= is
->audioq
.size
;
734 vqsize
= is
->videoq
.size
;
736 if (is
->audio_st
&& is
->video_st
)
737 av_diff
= get_audio_clock(is
) - get_video_clock(is
);
738 printf("%7.2f A-V:%7.3f aq=%5dKB vq=%5dKB \r",
739 get_master_clock(is
), av_diff
, aqsize
/ 1024, vqsize
/ 1024);
741 last_time
= cur_time
;
746 /* allocate a picture (needs to do that in main thread to avoid
747 potential locking problems */
748 static void alloc_picture(void *opaque
)
750 VideoState
*is
= opaque
;
753 vp
= &is
->pictq
[is
->pictq_windex
];
756 SDL_FreeYUVOverlay(vp
->bmp
);
759 /* XXX: use generic function */
760 /* XXX: disable overlay if no hardware acceleration or if RGB format */
761 switch(is
->video_st
->codec
.pix_fmt
) {
762 case PIX_FMT_YUV420P
:
763 case PIX_FMT_YUV422P
:
764 case PIX_FMT_YUV444P
:
766 case PIX_FMT_YUV410P
:
767 case PIX_FMT_YUV411P
:
775 vp
->bmp
= SDL_CreateYUVOverlay(is
->video_st
->codec
.width
,
776 is
->video_st
->codec
.height
,
779 vp
->width
= is
->video_st
->codec
.width
;
780 vp
->height
= is
->video_st
->codec
.height
;
782 SDL_LockMutex(is
->pictq_mutex
);
784 SDL_CondSignal(is
->pictq_cond
);
785 SDL_UnlockMutex(is
->pictq_mutex
);
788 static int queue_picture(VideoState
*is
, AVFrame
*src_frame
, double pts
)
794 /* wait until we have space to put a new picture */
795 SDL_LockMutex(is
->pictq_mutex
);
796 while (is
->pictq_size
>= VIDEO_PICTURE_QUEUE_SIZE
&&
797 !is
->videoq
.abort_request
) {
798 SDL_CondWait(is
->pictq_cond
, is
->pictq_mutex
);
800 SDL_UnlockMutex(is
->pictq_mutex
);
802 if (is
->videoq
.abort_request
)
805 vp
= &is
->pictq
[is
->pictq_windex
];
807 /* alloc or resize hardware picture buffer */
809 vp
->width
!= is
->video_st
->codec
.width
||
810 vp
->height
!= is
->video_st
->codec
.height
) {
815 /* the allocation must be done in the main thread to avoid
817 event
.type
= FF_ALLOC_EVENT
;
818 event
.user
.data1
= is
;
819 SDL_PushEvent(&event
);
821 /* wait until the picture is allocated */
822 SDL_LockMutex(is
->pictq_mutex
);
823 while (!vp
->allocated
&& !is
->videoq
.abort_request
) {
824 SDL_CondWait(is
->pictq_cond
, is
->pictq_mutex
);
826 SDL_UnlockMutex(is
->pictq_mutex
);
828 if (is
->videoq
.abort_request
)
832 /* if the frame is not skipped, then display it */
834 /* get a pointer on the bitmap */
835 SDL_LockYUVOverlay (vp
->bmp
);
837 dst_pix_fmt
= PIX_FMT_YUV420P
;
838 pict
.data
[0] = vp
->bmp
->pixels
[0];
839 pict
.data
[1] = vp
->bmp
->pixels
[2];
840 pict
.data
[2] = vp
->bmp
->pixels
[1];
842 pict
.linesize
[0] = vp
->bmp
->pitches
[0];
843 pict
.linesize
[1] = vp
->bmp
->pitches
[2];
844 pict
.linesize
[2] = vp
->bmp
->pitches
[1];
845 img_convert(&pict
, dst_pix_fmt
,
846 (AVPicture
*)src_frame
, is
->video_st
->codec
.pix_fmt
,
847 is
->video_st
->codec
.width
, is
->video_st
->codec
.height
);
848 /* update the bitmap content */
849 SDL_UnlockYUVOverlay(vp
->bmp
);
853 /* now we can update the picture count */
854 if (++is
->pictq_windex
== VIDEO_PICTURE_QUEUE_SIZE
)
855 is
->pictq_windex
= 0;
856 SDL_LockMutex(is
->pictq_mutex
);
858 SDL_UnlockMutex(is
->pictq_mutex
);
863 /* compute the exact PTS for the picture if it is omitted in the stream */
864 static int output_picture2(VideoState
*is
, AVFrame
*src_frame
, double pts1
)
866 double frame_delay
, pts
;
871 /* update video clock with pts, if present */
872 is
->video_clock
= pts
;
874 pts
= is
->video_clock
;
876 /* update video clock for next frame */
877 frame_delay
= av_q2d(is
->video_st
->codec
.time_base
);
878 /* for MPEG2, the frame can be repeated, so we update the
880 if (src_frame
->repeat_pict
) {
881 frame_delay
+= src_frame
->repeat_pict
* (frame_delay
* 0.5);
883 is
->video_clock
+= frame_delay
;
885 #if defined(DEBUG_SYNC) && 0
888 if (src_frame
->pict_type
== FF_B_TYPE
)
890 else if (src_frame
->pict_type
== FF_I_TYPE
)
894 printf("frame_type=%c clock=%0.3f pts=%0.3f\n",
898 return queue_picture(is
, src_frame
, pts
);
901 static int video_thread(void *arg
)
903 VideoState
*is
= arg
;
904 AVPacket pkt1
, *pkt
= &pkt1
;
905 int len1
, got_picture
;
906 AVFrame
*frame
= avcodec_alloc_frame();
910 while (is
->paused
&& !is
->videoq
.abort_request
) {
913 if (packet_queue_get(&is
->videoq
, pkt
, 1) < 0)
915 /* NOTE: ipts is the PTS of the _first_ picture beginning in
916 this packet, if any */
918 if (pkt
->dts
!= AV_NOPTS_VALUE
)
919 pts
= av_q2d(is
->video_st
->time_base
)*pkt
->dts
;
921 SDL_LockMutex(is
->video_decoder_mutex
);
922 len1
= avcodec_decode_video(&is
->video_st
->codec
,
924 pkt
->data
, pkt
->size
);
925 SDL_UnlockMutex(is
->video_decoder_mutex
);
929 if (output_picture2(is
, frame
, pts
) < 0)
935 stream_pause(cur_stream
);
942 /* copy samples for viewing in editor window */
943 static void update_sample_display(VideoState
*is
, short *samples
, int samples_size
)
945 int size
, len
, channels
;
947 channels
= is
->audio_st
->codec
.channels
;
949 size
= samples_size
/ sizeof(short);
951 len
= SAMPLE_ARRAY_SIZE
- is
->sample_array_index
;
954 memcpy(is
->sample_array
+ is
->sample_array_index
, samples
, len
* sizeof(short));
956 is
->sample_array_index
+= len
;
957 if (is
->sample_array_index
>= SAMPLE_ARRAY_SIZE
)
958 is
->sample_array_index
= 0;
963 /* return the new audio buffer size (samples can be added or deleted
964 to get better sync if video or external master clock) */
965 static int synchronize_audio(VideoState
*is
, short *samples
,
966 int samples_size1
, double pts
)
971 n
= 2 * is
->audio_st
->codec
.channels
;
972 samples_size
= samples_size1
;
974 /* if not master, then we try to remove or add samples to correct the clock */
975 if (((is
->av_sync_type
== AV_SYNC_VIDEO_MASTER
&& is
->video_st
) ||
976 is
->av_sync_type
== AV_SYNC_EXTERNAL_CLOCK
)) {
977 double diff
, avg_diff
;
978 int wanted_size
, min_size
, max_size
, nb_samples
;
980 ref_clock
= get_master_clock(is
);
981 diff
= get_audio_clock(is
) - ref_clock
;
983 if (diff
< AV_NOSYNC_THRESHOLD
) {
984 is
->audio_diff_cum
= diff
+ is
->audio_diff_avg_coef
* is
->audio_diff_cum
;
985 if (is
->audio_diff_avg_count
< AUDIO_DIFF_AVG_NB
) {
986 /* not enough measures to have a correct estimate */
987 is
->audio_diff_avg_count
++;
989 /* estimate the A-V difference */
990 avg_diff
= is
->audio_diff_cum
* (1.0 - is
->audio_diff_avg_coef
);
992 if (fabs(avg_diff
) >= is
->audio_diff_threshold
) {
993 wanted_size
= samples_size
+ ((int)(diff
* is
->audio_st
->codec
.sample_rate
) * n
);
994 nb_samples
= samples_size
/ n
;
996 min_size
= ((nb_samples
* (100 - SAMPLE_CORRECTION_PERCENT_MAX
)) / 100) * n
;
997 max_size
= ((nb_samples
* (100 + SAMPLE_CORRECTION_PERCENT_MAX
)) / 100) * n
;
998 if (wanted_size
< min_size
)
999 wanted_size
= min_size
;
1000 else if (wanted_size
> max_size
)
1001 wanted_size
= max_size
;
1003 /* add or remove samples to correction the synchro */
1004 if (wanted_size
< samples_size
) {
1005 /* remove samples */
1006 samples_size
= wanted_size
;
1007 } else if (wanted_size
> samples_size
) {
1008 uint8_t *samples_end
, *q
;
1012 nb
= (samples_size
- wanted_size
);
1013 samples_end
= (uint8_t *)samples
+ samples_size
- n
;
1014 q
= samples_end
+ n
;
1016 memcpy(q
, samples_end
, n
);
1020 samples_size
= wanted_size
;
1024 printf("diff=%f adiff=%f sample_diff=%d apts=%0.3f vpts=%0.3f %f\n",
1025 diff
, avg_diff
, samples_size
- samples_size1
,
1026 is
->audio_clock
, is
->video_clock
, is
->audio_diff_threshold
);
1030 /* too big difference : may be initial PTS errors, so
1032 is
->audio_diff_avg_count
= 0;
1033 is
->audio_diff_cum
= 0;
1037 return samples_size
;
1040 /* decode one audio frame and returns its uncompressed size */
1041 static int audio_decode_frame(VideoState
*is
, uint8_t *audio_buf
, double *pts_ptr
)
1043 AVPacket
*pkt
= &is
->audio_pkt
;
1044 int n
, len1
, data_size
;
1048 /* NOTE: the audio packet can contain several frames */
1049 while (is
->audio_pkt_size
> 0) {
1050 SDL_LockMutex(is
->audio_decoder_mutex
);
1051 len1
= avcodec_decode_audio(&is
->audio_st
->codec
,
1052 (int16_t *)audio_buf
, &data_size
,
1053 is
->audio_pkt_data
, is
->audio_pkt_size
);
1054 SDL_UnlockMutex(is
->audio_decoder_mutex
);
1056 /* if error, we skip the frame */
1057 is
->audio_pkt_size
= 0;
1061 is
->audio_pkt_data
+= len1
;
1062 is
->audio_pkt_size
-= len1
;
1065 /* if no pts, then compute it */
1066 pts
= is
->audio_clock
;
1068 n
= 2 * is
->audio_st
->codec
.channels
;
1069 is
->audio_clock
+= (double)data_size
/
1070 (double)(n
* is
->audio_st
->codec
.sample_rate
);
1071 #if defined(DEBUG_SYNC)
1073 static double last_clock
;
1074 printf("audio: delay=%0.3f clock=%0.3f pts=%0.3f\n",
1075 is
->audio_clock
- last_clock
,
1076 is
->audio_clock
, pts
);
1077 last_clock
= is
->audio_clock
;
1083 /* free the current packet */
1085 av_free_packet(pkt
);
1087 if (is
->paused
|| is
->audioq
.abort_request
) {
1091 /* read next packet */
1092 if (packet_queue_get(&is
->audioq
, pkt
, 1) < 0)
1094 is
->audio_pkt_data
= pkt
->data
;
1095 is
->audio_pkt_size
= pkt
->size
;
1097 /* if update the audio clock with the pts */
1098 if (pkt
->pts
!= AV_NOPTS_VALUE
) {
1099 is
->audio_clock
= av_q2d(is
->audio_st
->time_base
)*pkt
->pts
;
1104 /* get the current audio output buffer size, in samples. With SDL, we
1105 cannot have a precise information */
1106 static int audio_write_get_buf_size(VideoState
*is
)
1108 return is
->audio_hw_buf_size
- is
->audio_buf_index
;
1112 /* prepare a new audio buffer */
1113 void sdl_audio_callback(void *opaque
, Uint8
*stream
, int len
)
1115 VideoState
*is
= opaque
;
1116 int audio_size
, len1
;
1119 audio_callback_time
= av_gettime();
1122 if (is
->audio_buf_index
>= is
->audio_buf_size
) {
1123 audio_size
= audio_decode_frame(is
, is
->audio_buf
, &pts
);
1124 if (audio_size
< 0) {
1125 /* if error, just output silence */
1126 is
->audio_buf_size
= 1024;
1127 memset(is
->audio_buf
, 0, is
->audio_buf_size
);
1130 update_sample_display(is
, (int16_t *)is
->audio_buf
, audio_size
);
1131 audio_size
= synchronize_audio(is
, (int16_t *)is
->audio_buf
, audio_size
,
1133 is
->audio_buf_size
= audio_size
;
1135 is
->audio_buf_index
= 0;
1137 len1
= is
->audio_buf_size
- is
->audio_buf_index
;
1140 memcpy(stream
, (uint8_t *)is
->audio_buf
+ is
->audio_buf_index
, len1
);
1143 is
->audio_buf_index
+= len1
;
1148 /* open a given stream. Return 0 if OK */
1149 static int stream_component_open(VideoState
*is
, int stream_index
)
1151 AVFormatContext
*ic
= is
->ic
;
1152 AVCodecContext
*enc
;
1154 SDL_AudioSpec wanted_spec
, spec
;
1156 if (stream_index
< 0 || stream_index
>= ic
->nb_streams
)
1158 enc
= &ic
->streams
[stream_index
]->codec
;
1160 /* prepare audio output */
1161 if (enc
->codec_type
== CODEC_TYPE_AUDIO
) {
1162 wanted_spec
.freq
= enc
->sample_rate
;
1163 wanted_spec
.format
= AUDIO_S16SYS
;
1164 /* hack for AC3. XXX: suppress that */
1165 if (enc
->channels
> 2)
1167 wanted_spec
.channels
= enc
->channels
;
1168 wanted_spec
.silence
= 0;
1169 wanted_spec
.samples
= SDL_AUDIO_BUFFER_SIZE
;
1170 wanted_spec
.callback
= sdl_audio_callback
;
1171 wanted_spec
.userdata
= is
;
1172 if (SDL_OpenAudio(&wanted_spec
, &spec
) < 0) {
1173 fprintf(stderr
, "SDL_OpenAudio: %s\n", SDL_GetError());
1176 is
->audio_hw_buf_size
= spec
.size
;
1179 codec
= avcodec_find_decoder(enc
->codec_id
);
1180 enc
->debug_mv
= debug_mv
;
1182 enc
->workaround_bugs
= workaround_bugs
;
1183 enc
->lowres
= lowres
;
1184 if(lowres
) enc
->flags
|= CODEC_FLAG_EMU_EDGE
;
1185 enc
->idct_algo
= idct
;
1186 if(fast
) enc
->flags2
|= CODEC_FLAG2_FAST
;
1188 avcodec_open(enc
, codec
) < 0)
1190 #if defined(HAVE_THREADS)
1192 avcodec_thread_init(enc
, thread_count
);
1194 enc
->thread_count
= thread_count
;
1195 switch(enc
->codec_type
) {
1196 case CODEC_TYPE_AUDIO
:
1197 is
->audio_stream
= stream_index
;
1198 is
->audio_st
= ic
->streams
[stream_index
];
1199 is
->audio_buf_size
= 0;
1200 is
->audio_buf_index
= 0;
1202 /* init averaging filter */
1203 is
->audio_diff_avg_coef
= exp(log(0.01) / AUDIO_DIFF_AVG_NB
);
1204 is
->audio_diff_avg_count
= 0;
1205 /* since we do not have a precise anough audio fifo fullness,
1206 we correct audio sync only if larger than this threshold */
1207 is
->audio_diff_threshold
= 2.0 * SDL_AUDIO_BUFFER_SIZE
/ enc
->sample_rate
;
1209 memset(&is
->audio_pkt
, 0, sizeof(is
->audio_pkt
));
1210 packet_queue_init(&is
->audioq
);
1213 case CODEC_TYPE_VIDEO
:
1214 is
->video_stream
= stream_index
;
1215 is
->video_st
= ic
->streams
[stream_index
];
1217 is
->frame_last_delay
= 40e-3;
1218 is
->frame_timer
= (double)av_gettime() / 1000000.0;
1219 is
->video_current_pts_time
= av_gettime();
1221 packet_queue_init(&is
->videoq
);
1222 is
->video_tid
= SDL_CreateThread(video_thread
, is
);
1230 static void stream_component_close(VideoState
*is
, int stream_index
)
1232 AVFormatContext
*ic
= is
->ic
;
1233 AVCodecContext
*enc
;
1235 enc
= &ic
->streams
[stream_index
]->codec
;
1237 switch(enc
->codec_type
) {
1238 case CODEC_TYPE_AUDIO
:
1239 packet_queue_abort(&is
->audioq
);
1243 packet_queue_end(&is
->audioq
);
1245 case CODEC_TYPE_VIDEO
:
1246 packet_queue_abort(&is
->videoq
);
1248 /* note: we also signal this mutex to make sure we deblock the
1249 video thread in all cases */
1250 SDL_LockMutex(is
->pictq_mutex
);
1251 SDL_CondSignal(is
->pictq_cond
);
1252 SDL_UnlockMutex(is
->pictq_mutex
);
1254 SDL_WaitThread(is
->video_tid
, NULL
);
1256 packet_queue_end(&is
->videoq
);
1263 switch(enc
->codec_type
) {
1264 case CODEC_TYPE_AUDIO
:
1265 is
->audio_st
= NULL
;
1266 is
->audio_stream
= -1;
1268 case CODEC_TYPE_VIDEO
:
1269 is
->video_st
= NULL
;
1270 is
->video_stream
= -1;
1277 void dump_stream_info(AVFormatContext
*s
)
1280 fprintf(stderr
, "Track: %d\n", s
->track
);
1281 if (s
->title
[0] != '\0')
1282 fprintf(stderr
, "Title: %s\n", s
->title
);
1283 if (s
->author
[0] != '\0')
1284 fprintf(stderr
, "Author: %s\n", s
->author
);
1285 if (s
->album
[0] != '\0')
1286 fprintf(stderr
, "Album: %s\n", s
->album
);
1288 fprintf(stderr
, "Year: %d\n", s
->year
);
1289 if (s
->genre
[0] != '\0')
1290 fprintf(stderr
, "Genre: %s\n", s
->genre
);
1293 /* since we have only one decoding thread, we can use a global
1294 variable instead of a thread local variable */
1295 static VideoState
*global_video_state
;
1297 static int decode_interrupt_cb(void)
1299 return (global_video_state
&& global_video_state
->abort_request
);
1302 /* this thread gets the stream from the disk or the network */
1303 static int decode_thread(void *arg
)
1305 VideoState
*is
= arg
;
1306 AVFormatContext
*ic
;
1307 int err
, i
, ret
, video_index
, audio_index
, use_play
;
1308 AVPacket pkt1
, *pkt
= &pkt1
;
1309 AVFormatParameters params
, *ap
= ¶ms
;
1313 is
->video_stream
= -1;
1314 is
->audio_stream
= -1;
1316 global_video_state
= is
;
1317 url_set_interrupt_cb(decode_interrupt_cb
);
1319 memset(ap
, 0, sizeof(*ap
));
1320 ap
->image_format
= image_format
;
1321 ap
->initial_pause
= 1; /* we force a pause when starting an RTSP
1324 err
= av_open_input_file(&ic
, is
->filename
, is
->iformat
, 0, ap
);
1326 print_error(is
->filename
, err
);
1331 #ifdef CONFIG_NETWORK
1332 use_play
= (ic
->iformat
== &rtsp_demux
);
1337 err
= av_find_stream_info(ic
);
1339 fprintf(stderr
, "%s: could not find codec parameters\n", is
->filename
);
1343 ic
->pb
.eof_reached
= 0; //FIXME hack, ffplay maybe shouldnt use url_feof() to test for the end
1346 /* if seeking requested, we execute it */
1347 if (start_time
!= AV_NOPTS_VALUE
) {
1350 timestamp
= start_time
;
1351 /* add the stream start time */
1352 if (ic
->start_time
!= AV_NOPTS_VALUE
)
1353 timestamp
+= ic
->start_time
;
1354 ret
= av_seek_frame(ic
, -1, timestamp
, AVSEEK_FLAG_BACKWARD
);
1356 fprintf(stderr
, "%s: could not seek to position %0.3f\n",
1357 is
->filename
, (double)timestamp
/ AV_TIME_BASE
);
1361 /* now we can begin to play (RTSP stream only) */
1365 err
= av_find_stream_info(ic
);
1367 fprintf(stderr
, "%s: could not find codec parameters\n", is
->filename
);
1373 for(i
= 0; i
< ic
->nb_streams
; i
++) {
1374 AVCodecContext
*enc
= &ic
->streams
[i
]->codec
;
1375 switch(enc
->codec_type
) {
1376 case CODEC_TYPE_AUDIO
:
1377 if (audio_index
< 0 && !audio_disable
)
1380 case CODEC_TYPE_VIDEO
:
1381 if (video_index
< 0 && !video_disable
)
1389 dump_format(ic
, 0, is
->filename
, 0);
1390 dump_stream_info(ic
);
1393 /* open the streams */
1394 if (audio_index
>= 0) {
1395 stream_component_open(is
, audio_index
);
1398 if (video_index
>= 0) {
1399 stream_component_open(is
, video_index
);
1401 if (!display_disable
)
1405 if (is
->video_stream
< 0 && is
->audio_stream
< 0) {
1406 fprintf(stderr
, "%s: could not open codecs\n", is
->filename
);
1412 if (is
->abort_request
)
1414 #ifdef CONFIG_NETWORK
1415 if (is
->paused
!= is
->last_paused
) {
1416 is
->last_paused
= is
->paused
;
1422 if (is
->paused
&& ic
->iformat
== &rtsp_demux
) {
1423 /* wait 10 ms to avoid trying to get another packet */
1430 /* XXX: must lock decoder threads */
1431 ret
= av_seek_frame(is
->ic
, -1, is
->seek_pos
, is
->seek_flags
);
1433 fprintf(stderr
, "%s: error while seeking\n", is
->ic
->filename
);
1435 if (is
->audio_stream
>= 0) {
1436 packet_queue_flush(&is
->audioq
);
1438 if (is
->video_stream
>= 0) {
1439 packet_queue_flush(&is
->videoq
);
1440 SDL_LockMutex(is
->video_decoder_mutex
);
1441 avcodec_flush_buffers(&ic
->streams
[video_index
]->codec
);
1442 SDL_UnlockMutex(is
->video_decoder_mutex
);
1448 /* if the queue are full, no need to read more */
1449 if (is
->audioq
.size
> MAX_AUDIOQ_SIZE
||
1450 is
->videoq
.size
> MAX_VIDEOQ_SIZE
||
1451 url_feof(&ic
->pb
)) {
1456 ret
= av_read_frame(ic
, pkt
);
1458 if (url_feof(&ic
->pb
) && url_ferror(&ic
->pb
) == 0) {
1459 SDL_Delay(100); /* wait for user event */
1464 if (pkt
->stream_index
== is
->audio_stream
) {
1465 packet_queue_put(&is
->audioq
, pkt
);
1466 } else if (pkt
->stream_index
== is
->video_stream
) {
1467 packet_queue_put(&is
->videoq
, pkt
);
1469 av_free_packet(pkt
);
1472 /* wait until the end */
1473 while (!is
->abort_request
) {
1479 /* disable interrupting */
1480 global_video_state
= NULL
;
1482 /* close each stream */
1483 if (is
->audio_stream
>= 0)
1484 stream_component_close(is
, is
->audio_stream
);
1485 if (is
->video_stream
>= 0)
1486 stream_component_close(is
, is
->video_stream
);
1488 av_close_input_file(is
->ic
);
1489 is
->ic
= NULL
; /* safety */
1491 url_set_interrupt_cb(NULL
);
1496 event
.type
= FF_QUIT_EVENT
;
1497 event
.user
.data1
= is
;
1498 SDL_PushEvent(&event
);
1503 static VideoState
*stream_open(const char *filename
, AVInputFormat
*iformat
)
1507 is
= av_mallocz(sizeof(VideoState
));
1510 pstrcpy(is
->filename
, sizeof(is
->filename
), filename
);
1511 is
->iformat
= iformat
;
1513 is
->width
= screen
->w
;
1514 is
->height
= screen
->h
;
1519 /* start video display */
1520 is
->pictq_mutex
= SDL_CreateMutex();
1521 is
->pictq_cond
= SDL_CreateCond();
1523 is
->audio_decoder_mutex
= SDL_CreateMutex();
1524 is
->video_decoder_mutex
= SDL_CreateMutex();
1526 /* add the refresh timer to draw the picture */
1527 schedule_refresh(is
, 40);
1529 is
->av_sync_type
= av_sync_type
;
1530 is
->parse_tid
= SDL_CreateThread(decode_thread
, is
);
1531 if (!is
->parse_tid
) {
1538 static void stream_close(VideoState
*is
)
1542 /* XXX: use a special url_shutdown call to abort parse cleanly */
1543 is
->abort_request
= 1;
1544 SDL_WaitThread(is
->parse_tid
, NULL
);
1546 /* free all pictures */
1547 for(i
=0;i
<VIDEO_PICTURE_QUEUE_SIZE
; i
++) {
1550 SDL_FreeYUVOverlay(vp
->bmp
);
1554 SDL_DestroyMutex(is
->pictq_mutex
);
1555 SDL_DestroyCond(is
->pictq_cond
);
1556 SDL_DestroyMutex(is
->audio_decoder_mutex
);
1557 SDL_DestroyMutex(is
->video_decoder_mutex
);
1560 void stream_cycle_channel(VideoState
*is
, int codec_type
)
1562 AVFormatContext
*ic
= is
->ic
;
1563 int start_index
, stream_index
;
1566 if (codec_type
== CODEC_TYPE_VIDEO
)
1567 start_index
= is
->video_stream
;
1569 start_index
= is
->audio_stream
;
1570 if (start_index
< 0)
1572 stream_index
= start_index
;
1574 if (++stream_index
>= is
->ic
->nb_streams
)
1576 if (stream_index
== start_index
)
1578 st
= ic
->streams
[stream_index
];
1579 if (st
->codec
.codec_type
== codec_type
) {
1580 /* check that parameters are OK */
1581 switch(codec_type
) {
1582 case CODEC_TYPE_AUDIO
:
1583 if (st
->codec
.sample_rate
!= 0 &&
1584 st
->codec
.channels
!= 0)
1587 case CODEC_TYPE_VIDEO
:
1595 stream_component_close(is
, start_index
);
1596 stream_component_open(is
, stream_index
);
1600 void toggle_full_screen(void)
1603 is_full_screen
= !is_full_screen
;
1604 if (!fs_screen_width
) {
1605 /* use default SDL method */
1606 SDL_WM_ToggleFullScreen(screen
);
1608 /* use the recorded resolution */
1609 flags
= SDL_HWSURFACE
|SDL_ASYNCBLIT
|SDL_HWACCEL
;
1610 if (is_full_screen
) {
1611 w
= fs_screen_width
;
1612 h
= fs_screen_height
;
1613 flags
|= SDL_FULLSCREEN
;
1617 flags
|= SDL_RESIZABLE
;
1619 screen
= SDL_SetVideoMode(w
, h
, 0, flags
);
1620 cur_stream
->width
= w
;
1621 cur_stream
->height
= h
;
1625 void toggle_pause(void)
1628 stream_pause(cur_stream
);
1632 void step_to_next_frame(void)
1635 if (cur_stream
->paused
)
1636 cur_stream
->paused
=0;
1637 cur_stream
->video_current_pts
= get_video_clock(cur_stream
);
1645 stream_close(cur_stream
);
1654 void toggle_audio_display(void)
1657 cur_stream
->show_audio
= !cur_stream
->show_audio
;
1661 /* handle an event sent by the GUI */
1662 void event_loop(void)
1665 double incr
, pos
, frac
;
1668 SDL_WaitEvent(&event
);
1669 switch(event
.type
) {
1671 switch(event
.key
.keysym
.sym
) {
1677 toggle_full_screen();
1683 case SDLK_s
: //S: Step to next frame
1684 step_to_next_frame();
1688 stream_cycle_channel(cur_stream
, CODEC_TYPE_AUDIO
);
1692 stream_cycle_channel(cur_stream
, CODEC_TYPE_VIDEO
);
1695 toggle_audio_display();
1710 pos
= get_master_clock(cur_stream
);
1712 stream_seek(cur_stream
, (int64_t)(pos
* AV_TIME_BASE
), incr
);
1719 case SDL_MOUSEBUTTONDOWN
:
1722 int tns
, thh
, tmm
, tss
;
1723 tns
= cur_stream
->ic
->duration
/1000000LL;
1725 tmm
= (tns
%3600)/60;
1727 frac
= (double)event
.button
.x
/(double)cur_stream
->width
;
1732 fprintf(stderr
, "Seek to %2.0f%% (%2d:%02d:%02d) of total duration (%2d:%02d:%02d) \n", frac
*100,
1733 hh
, mm
, ss
, thh
, tmm
, tss
);
1734 stream_seek(cur_stream
, (int64_t)(cur_stream
->ic
->start_time
+frac
*cur_stream
->ic
->duration
), 0);
1737 case SDL_VIDEORESIZE
:
1739 screen
= SDL_SetVideoMode(event
.resize
.w
, event
.resize
.h
, 0,
1740 SDL_HWSURFACE
|SDL_RESIZABLE
|SDL_ASYNCBLIT
|SDL_HWACCEL
);
1741 cur_stream
->width
= event
.resize
.w
;
1742 cur_stream
->height
= event
.resize
.h
;
1749 case FF_ALLOC_EVENT
:
1750 alloc_picture(event
.user
.data1
);
1752 case FF_REFRESH_EVENT
:
1753 video_refresh_timer(event
.user
.data1
);
1761 void opt_width(const char *arg
)
1763 screen_width
= atoi(arg
);
1766 void opt_height(const char *arg
)
1768 screen_height
= atoi(arg
);
1771 static void opt_format(const char *arg
)
1773 file_iformat
= av_find_input_format(arg
);
1774 if (!file_iformat
) {
1775 fprintf(stderr
, "Unknown input format: %s\n", arg
);
1780 static void opt_image_format(const char *arg
)
1784 for(f
= first_image_format
; f
!= NULL
; f
= f
->next
) {
1785 if (!strcmp(arg
, f
->name
))
1789 fprintf(stderr
, "Unknown image format: '%s'\n", arg
);
1795 #ifdef CONFIG_NETWORK
1796 void opt_rtp_tcp(void)
1798 /* only tcp protocol */
1799 rtsp_default_protocols
= (1 << RTSP_PROTOCOL_RTP_TCP
);
1803 void opt_sync(const char *arg
)
1805 if (!strcmp(arg
, "audio"))
1806 av_sync_type
= AV_SYNC_AUDIO_MASTER
;
1807 else if (!strcmp(arg
, "video"))
1808 av_sync_type
= AV_SYNC_VIDEO_MASTER
;
1809 else if (!strcmp(arg
, "ext"))
1810 av_sync_type
= AV_SYNC_EXTERNAL_CLOCK
;
1815 void opt_seek(const char *arg
)
1817 start_time
= parse_date(arg
, 1);
1820 static void opt_debug(const char *arg
)
1825 static void opt_vismv(const char *arg
)
1827 debug_mv
= atoi(arg
);
1830 static void opt_thread_count(const char *arg
)
1832 thread_count
= atoi(arg
);
1833 #if !defined(HAVE_THREADS)
1834 fprintf(stderr
, "Warning: not compiled with thread support, using thread emulation\n");
1838 const OptionDef options
[] = {
1839 { "h", 0, {(void*)show_help
}, "show help" },
1840 { "x", HAS_ARG
, {(void*)opt_width
}, "force displayed width", "width" },
1841 { "y", HAS_ARG
, {(void*)opt_height
}, "force displayed height", "height" },
1843 /* disabled as SDL/X11 does not support it correctly on application launch */
1844 { "fs", OPT_BOOL
, {(void*)&is_full_screen
}, "force full screen" },
1846 { "an", OPT_BOOL
, {(void*)&audio_disable
}, "disable audio" },
1847 { "vn", OPT_BOOL
, {(void*)&video_disable
}, "disable video" },
1848 { "ss", HAS_ARG
, {(void*)&opt_seek
}, "seek to a given position in seconds", "pos" },
1849 { "nodisp", OPT_BOOL
, {(void*)&display_disable
}, "disable graphical display" },
1850 { "f", HAS_ARG
, {(void*)opt_format
}, "force format", "fmt" },
1851 { "img", HAS_ARG
, {(void*)opt_image_format
}, "force image format", "img_fmt" },
1852 { "stats", OPT_BOOL
| OPT_EXPERT
, {(void*)&show_status
}, "show status", "" },
1853 { "debug", HAS_ARG
| OPT_EXPERT
, {(void*)opt_debug
}, "print specific debug info", "" },
1854 { "bug", OPT_INT
| HAS_ARG
| OPT_EXPERT
, {(void*)&workaround_bugs
}, "workaround bugs", "" },
1855 { "vismv", HAS_ARG
| OPT_EXPERT
, {(void*)opt_vismv
}, "visualize motion vectors", "" },
1856 { "fast", OPT_BOOL
| OPT_EXPERT
, {(void*)&fast
}, "non spec compliant optimizations", "" },
1857 { "lowres", OPT_INT
| HAS_ARG
| OPT_EXPERT
, {(void*)&lowres
}, "", "" },
1858 { "idct", OPT_INT
| HAS_ARG
| OPT_EXPERT
, {(void*)&idct
}, "set idct algo", "algo" },
1859 #ifdef CONFIG_NETWORK
1860 { "rtp_tcp", OPT_EXPERT
, {(void*)&opt_rtp_tcp
}, "force RTP/TCP protocol usage", "" },
1862 { "sync", HAS_ARG
| OPT_EXPERT
, {(void*)opt_sync
}, "set audio-video sync. type (type=audio/video/ext)", "type" },
1863 { "threads", HAS_ARG
| OPT_EXPERT
, {(void*)opt_thread_count
}, "thread count", "count" },
1867 void show_help(void)
1869 printf("ffplay version " FFMPEG_VERSION
", Copyright (c) 2003 Fabrice Bellard\n"
1870 "usage: ffplay [options] input_file\n"
1871 "Simple media player\n");
1873 show_help_options(options
, "Main options:\n",
1875 show_help_options(options
, "\nAdvanced options:\n",
1876 OPT_EXPERT
, OPT_EXPERT
);
1877 printf("\nWhile playing:\n"
1879 "f toggle full screen\n"
1881 "a cycle audio channel\n"
1882 "v cycle video channel\n"
1883 "w show audio waves\n"
1884 "left/right seek backward/forward 10 seconds\n"
1885 "down/up seek backward/forward 1 minute\n"
1886 "mouse click seek to percentage in file corresponding to fraction of width\n"
1891 void parse_arg_file(const char *filename
)
1893 if (!strcmp(filename
, "-"))
1895 input_filename
= filename
;
1898 /* Called from the main */
1899 int main(int argc
, char **argv
)
1903 /* register all codecs, demux and protocols */
1907 MorphToPM(); // Morph the VIO application to a PM one to be able to use Win* functions
1909 // Make stdout and stderr unbuffered
1910 setbuf( stdout
, NULL
);
1911 setbuf( stderr
, NULL
);
1914 parse_options(argc
, argv
, options
);
1916 if (!input_filename
)
1919 if (display_disable
) {
1922 flags
= SDL_INIT_VIDEO
| SDL_INIT_AUDIO
| SDL_INIT_TIMER
;
1923 #ifndef CONFIG_WIN32
1924 flags
|= SDL_INIT_EVENTTHREAD
; /* Not supported on win32 */
1926 if (SDL_Init (flags
)) {
1927 fprintf(stderr
, "Could not initialize SDL - %s\n", SDL_GetError());
1931 if (!display_disable
) {
1933 /* save the screen resolution... SDL should allow full screen
1934 by resizing the window */
1937 dpy
= XOpenDisplay(NULL
);
1939 fs_screen_width
= DisplayWidth(dpy
, DefaultScreen(dpy
));
1940 fs_screen_height
= DisplayHeight(dpy
, DefaultScreen(dpy
));
1945 flags
= SDL_HWSURFACE
|SDL_ASYNCBLIT
|SDL_HWACCEL
;
1946 if (is_full_screen
&& fs_screen_width
) {
1947 w
= fs_screen_width
;
1948 h
= fs_screen_height
;
1949 flags
|= SDL_FULLSCREEN
;
1953 flags
|= SDL_RESIZABLE
;
1955 screen
= SDL_SetVideoMode(w
, h
, 0, flags
);
1957 fprintf(stderr
, "SDL: could not set video mode - exiting\n");
1960 SDL_WM_SetCaption("FFplay", "FFplay");
1963 SDL_EventState(SDL_ACTIVEEVENT
, SDL_IGNORE
);
1964 SDL_EventState(SDL_MOUSEMOTION
, SDL_IGNORE
);
1965 SDL_EventState(SDL_SYSWMEVENT
, SDL_IGNORE
);
1966 SDL_EventState(SDL_USEREVENT
, SDL_IGNORE
);
1968 cur_stream
= stream_open(input_filename
, file_iformat
);