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
26 #include <SDL_thread.h>
29 #undef main /* We don't want SDL to override our main() */
32 #if defined(__linux__)
40 #define MAX_VIDEOQ_SIZE (5 * 256 * 1024)
41 #define MAX_AUDIOQ_SIZE (5 * 16 * 1024)
43 /* NOTE: the size must be big enough to compensate the hardware audio buffersize size */
44 #define SAMPLE_ARRAY_SIZE (2*65536)
46 typedef struct PacketQueue {
47 AVPacketList *first_pkt, *last_pkt;
55 #define VIDEO_PICTURE_QUEUE_SIZE 1
57 typedef struct VideoPicture {
58 int delay; /* delay before showing the next picture */
60 int width, height; /* source height & width */
65 AV_SYNC_AUDIO_MASTER, /* default choice */
67 AV_SYNC_EXTERNAL_CLOCK, /* if external clock, then you must update external_clock yourself */
70 typedef struct VideoState {
71 SDL_Thread *parse_tid;
72 SDL_Thread *video_tid;
77 int dtg_active_format;
82 double external_clock; /* external clock */
84 double audio_clock; /* current audio clock value */
87 int audio_hw_buf_size;
88 /* samples output by the codec. we reserve more space for avsync
90 uint8_t audio_buf[(AVCODEC_MAX_AUDIO_FRAME_SIZE * 3) / 2];
91 int audio_buf_size; /* in bytes */
92 int audio_buf_index; /* in bytes */
94 uint8_t *audio_pkt_data;
96 int64_t audio_pkt_ipts;
98 int show_audio; /* if true, display audio samples */
99 int16_t sample_array[SAMPLE_ARRAY_SIZE];
100 int sample_array_index;
103 double video_clock; /* current video clock value */
108 VideoPicture pictq[VIDEO_PICTURE_QUEUE_SIZE];
109 int pictq_size, pictq_rindex, pictq_windex;
110 SDL_mutex *pictq_mutex;
111 SDL_cond *pictq_cond;
113 // QETimer *video_timer;
115 int width, height, xleft, ytop;
118 void show_help(void);
119 int audio_write_get_buf_size(VideoState *is);
121 /* options specified by the user */
122 static AVInputFormat *file_iformat;
123 static const char *input_filename;
124 static int fs_screen_width;
125 static int fs_screen_height;
126 static int screen_width = 640;
127 static int screen_height = 480;
128 static int audio_disable;
129 static int video_disable;
130 static int display_disable;
131 static int show_status;
133 /* current context */
134 static int is_full_screen;
135 static VideoState *cur_stream;
136 static int64_t audio_callback_time;
138 #define FF_ALLOC_EVENT (SDL_USEREVENT)
139 #define FF_REFRESH_EVENT (SDL_USEREVENT + 1)
143 /* packet queue handling */
144 static void packet_queue_init(PacketQueue *q)
146 memset(q, 0, sizeof(PacketQueue));
147 q->mutex = SDL_CreateMutex();
148 q->cond = SDL_CreateCond();
151 static void packet_queue_end(PacketQueue *q)
153 AVPacketList *pkt, *pkt1;
155 for(pkt = q->first_pkt; pkt != NULL; pkt = pkt1) {
157 av_free_packet(&pkt->pkt);
159 SDL_DestroyMutex(q->mutex);
160 SDL_DestroyCond(q->cond);
163 static int packet_queue_put(PacketQueue *q, AVPacket *pkt)
167 pkt1 = av_malloc(sizeof(AVPacketList));
173 SDL_LockMutex(q->mutex);
179 q->last_pkt->next = pkt1;
182 q->size += pkt1->pkt.size;
183 /* XXX: should duplicate packet data in DV case */
184 SDL_CondSignal(q->cond);
186 SDL_UnlockMutex(q->mutex);
190 static void packet_queue_abort(PacketQueue *q)
192 SDL_LockMutex(q->mutex);
194 q->abort_request = 1;
196 SDL_CondSignal(q->cond);
198 SDL_UnlockMutex(q->mutex);
201 /* return < 0 if aborted, 0 if no packet and > 0 if packet. */
202 static int packet_queue_get(PacketQueue *q, AVPacket *pkt, int block)
207 SDL_LockMutex(q->mutex);
210 if (q->abort_request) {
217 q->first_pkt = pkt1->next;
221 q->size -= pkt1->pkt.size;
230 SDL_CondWait(q->cond, q->mutex);
233 SDL_UnlockMutex(q->mutex);
237 static inline void fill_rectangle(SDL_Surface *screen,
238 int x, int y, int w, int h, int color)
245 SDL_FillRect(screen, &rect, color);
249 /* draw only the border of a rectangle */
250 void fill_border(VideoState *s, int x, int y, int w, int h, int color)
254 /* fill the background */
258 w2 = s->width - (x + w);
264 h2 = s->height - (y + h);
267 fill_rectangle(screen,
271 fill_rectangle(screen,
272 s->xleft + s->width - w2, s->ytop,
275 fill_rectangle(screen,
276 s->xleft + w1, s->ytop,
277 s->width - w1 - w2, h1,
279 fill_rectangle(screen,
280 s->xleft + w1, s->ytop + s->height - h2,
281 s->width - w1 - w2, h2,
286 static void video_image_display(VideoState *is)
290 int width, height, x, y;
293 vp = &is->pictq[is->pictq_rindex];
295 /* XXX: use variable in the frame */
296 aspect_ratio = is->video_st->codec.aspect_ratio;
297 if (aspect_ratio <= 0.0)
298 aspect_ratio = (float)is->video_st->codec.width /
299 (float)is->video_st->codec.height;
300 /* if an active format is indicated, then it overrides the
303 if (is->video_st->codec.dtg_active_format != is->dtg_active_format) {
304 is->dtg_active_format = is->video_st->codec.dtg_active_format;
305 printf("dtg_active_format=%d\n", is->dtg_active_format);
309 switch(is->video_st->codec.dtg_active_format) {
310 case FF_DTG_AFD_SAME:
315 aspect_ratio = 4.0 / 3.0;
317 case FF_DTG_AFD_16_9:
318 aspect_ratio = 16.0 / 9.0;
320 case FF_DTG_AFD_14_9:
321 aspect_ratio = 14.0 / 9.0;
323 case FF_DTG_AFD_4_3_SP_14_9:
324 aspect_ratio = 14.0 / 9.0;
326 case FF_DTG_AFD_16_9_SP_14_9:
327 aspect_ratio = 14.0 / 9.0;
329 case FF_DTG_AFD_SP_4_3:
330 aspect_ratio = 4.0 / 3.0;
335 /* XXX: we suppose the screen has a 1.0 pixel ratio */
337 width = ((int)rint(height * aspect_ratio)) & -3;
338 if (width > is->width) {
340 height = ((int)rint(width / aspect_ratio)) & -3;
342 x = (is->width - width) / 2;
343 y = (is->height - height) / 2;
344 if (!is->no_background) {
345 /* fill the background */
346 // fill_border(is, x, y, width, height, QERGB(0x00, 0x00, 0x00));
348 is->no_background = 0;
350 rect.x = is->xleft + x;
351 rect.y = is->xleft + y;
354 SDL_DisplayYUVOverlay(vp->bmp, &rect);
357 fill_rectangle(screen,
358 is->xleft, is->ytop, is->width, is->height,
359 QERGB(0x00, 0x00, 0x00));
364 static inline int compute_mod(int a, int b)
373 static void video_audio_display(VideoState *s)
375 int i, i_start, x, y1, y, ys, delay, n, nb_display_channels;
376 int ch, channels, h, h2, bgcolor, fgcolor;
379 /* compute display index : center on currently output samples */
380 channels = s->audio_st->codec.channels;
381 nb_display_channels = channels;
384 delay = audio_write_get_buf_size(s);
387 /* to be more precise, we take into account the time spent since
388 the last buffer computation */
389 if (audio_callback_time) {
390 time_diff = av_gettime() - audio_callback_time;
391 delay += (time_diff * s->audio_st->codec.sample_rate) / 1000000;
394 delay -= s->width / 2;
395 if (delay < s->width)
397 i_start = compute_mod(s->sample_array_index - delay * channels, SAMPLE_ARRAY_SIZE);
398 s->last_i_start = i_start;
400 i_start = s->last_i_start;
403 bgcolor = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00);
404 fill_rectangle(screen,
405 s->xleft, s->ytop, s->width, s->height,
408 fgcolor = SDL_MapRGB(screen->format, 0xff, 0xff, 0xff);
410 /* total height for one channel */
411 h = s->height / nb_display_channels;
412 /* graph height / 2 */
414 for(ch = 0;ch < nb_display_channels; ch++) {
416 y1 = s->ytop + ch * h + (h / 2); /* position of center line */
417 for(x = 0; x < s->width; x++) {
418 y = (s->sample_array[i] * h2) >> 15;
425 fill_rectangle(screen,
426 s->xleft + x, ys, 1, y,
429 if (i >= SAMPLE_ARRAY_SIZE)
430 i -= SAMPLE_ARRAY_SIZE;
434 fgcolor = SDL_MapRGB(screen->format, 0x00, 0x00, 0xff);
436 for(ch = 1;ch < nb_display_channels; ch++) {
437 y = s->ytop + ch * h;
438 fill_rectangle(screen,
439 s->xleft, y, s->width, 1,
442 SDL_UpdateRect(screen, s->xleft, s->ytop, s->width, s->height);
445 /* display the current picture, if any */
446 static void video_display(VideoState *is)
448 if (is->audio_st && is->show_audio)
449 video_audio_display(is);
450 else if (is->video_st)
451 video_image_display(is);
454 static Uint32 sdl_refresh_timer_cb(Uint32 interval, void *opaque)
457 event.type = FF_REFRESH_EVENT;
458 event.user.data1 = opaque;
459 SDL_PushEvent(&event);
460 return 0; /* 0 means stop timer */
463 /* schedule a video refresh in 'delay' ms */
464 static void schedule_refresh(VideoState *is, int delay)
466 SDL_AddTimer(delay, sdl_refresh_timer_cb, is);
469 /* called to display each frame */
470 static void video_refresh_timer(void *opaque)
472 VideoState *is = opaque;
476 if (is->pictq_size == 0) {
477 /* if no picture, need to wait */
478 schedule_refresh(is, 40);
480 vp = &is->pictq[is->pictq_rindex];
482 /* launch timer for next picture */
483 schedule_refresh(is, vp->delay);
485 /* display picture */
488 /* update queue size and signal for next picture */
489 if (++is->pictq_rindex == VIDEO_PICTURE_QUEUE_SIZE)
490 is->pictq_rindex = 0;
492 SDL_LockMutex(is->pictq_mutex);
494 SDL_CondSignal(is->pictq_cond);
495 SDL_UnlockMutex(is->pictq_mutex);
497 } else if (is->audio_st) {
498 /* draw the next audio frame */
500 schedule_refresh(is, 40);
502 /* if only audio stream, then display the audio bars (better
503 than nothing, just to test the implementation */
505 /* display picture */
508 schedule_refresh(is, 100);
511 static int64_t last_time;
515 cur_time = av_gettime();
516 if (!last_time || (cur_time - last_time) >= 500 * 1000) {
520 aqsize = is->audioq.size;
522 vqsize = is->videoq.size;
523 printf("A:%7.2f V:%7.2f aq=%5dKB vq=%5dKB \r",
524 is->audio_clock, is->video_clock, aqsize / 1024, vqsize / 1024);
526 last_time = cur_time;
531 /* allocate a picture (needs to do that in main thread to avoid
532 potential locking problems */
533 static void alloc_picture(void *opaque)
535 VideoState *is = opaque;
539 vp = &is->pictq[is->pictq_windex];
542 SDL_FreeYUVOverlay(vp->bmp);
544 /* XXX: use generic function */
545 switch(is->video_st->codec.pix_fmt) {
546 case PIX_FMT_YUV420P:
547 case PIX_FMT_YUV422P:
548 case PIX_FMT_YUV444P:
550 case PIX_FMT_YUV410P:
551 case PIX_FMT_YUV411P:
560 vp->bmp = SDL_CreateYUVOverlay(is->video_st->codec.width,
561 is->video_st->codec.height,
566 vp->bmp = bmp_alloc(screen,
567 is->video_st->codec.width,
568 is->video_st->codec.height,
569 screen->bitmap_format,
574 vp->width = is->video_st->codec.width;
575 vp->height = is->video_st->codec.height;
577 SDL_LockMutex(is->pictq_mutex);
579 SDL_CondSignal(is->pictq_cond);
580 SDL_UnlockMutex(is->pictq_mutex);
583 #define VIDEO_CORRECTION_THRESHOLD 0.2
585 static int output_picture(VideoState *is, AVPicture *src_pict, double pts)
590 double delay, ref_clock, diff;
592 /* wait until we have space to put a new picture */
593 SDL_LockMutex(is->pictq_mutex);
594 while (is->pictq_size >= VIDEO_PICTURE_QUEUE_SIZE &&
595 !is->videoq.abort_request) {
596 SDL_CondWait(is->pictq_cond, is->pictq_mutex);
598 SDL_UnlockMutex(is->pictq_mutex);
600 if (is->videoq.abort_request)
603 vp = &is->pictq[is->pictq_windex];
605 /* alloc or resize hardware picture buffer */
607 vp->width != is->video_st->codec.width ||
608 vp->height != is->video_st->codec.height) {
613 /* the allocation must be done in the main thread to avoid
615 event.type = FF_ALLOC_EVENT;
616 event.user.data1 = is;
617 SDL_PushEvent(&event);
619 /* wait until the picture is allocated */
620 SDL_LockMutex(is->pictq_mutex);
621 while (!vp->allocated && !is->videoq.abort_request) {
622 SDL_CondWait(is->pictq_cond, is->pictq_mutex);
624 SDL_UnlockMutex(is->pictq_mutex);
626 if (is->videoq.abort_request)
631 /* get a pointer on the bitmap */
632 SDL_LockYUVOverlay (vp->bmp);
634 dst_pix_fmt = PIX_FMT_YUV420P;
635 pict.data[0] = vp->bmp->pixels[0];
636 pict.data[1] = vp->bmp->pixels[2];
637 pict.data[2] = vp->bmp->pixels[1];
639 pict.linesize[0] = vp->bmp->pitches[0];
640 pict.linesize[1] = vp->bmp->pitches[2];
641 pict.linesize[2] = vp->bmp->pitches[1];
643 img_convert(&pict, dst_pix_fmt,
644 src_pict, is->video_st->codec.pix_fmt,
645 is->video_st->codec.width, is->video_st->codec.height);
646 /* update the bitmap content */
647 SDL_UnlockYUVOverlay(vp->bmp);
649 /* compute delay for the next frame and take into account the
650 pts if needed to make a correction. Since we do not support
651 correct MPEG B frame PTS, we put a high threshold */
653 if (is->av_sync_type == AV_SYNC_VIDEO_MASTER) {
654 ref_clock = is->video_clock;
655 } else if (is->av_sync_type == AV_SYNC_AUDIO_MASTER) {
656 /* cannot use audio master if no audio, so fall back to no sync */
658 ref_clock = is->video_clock;
660 ref_clock = is->audio_clock;
662 ref_clock = is->external_clock;
664 diff = is->video_clock - ref_clock;
665 delay = (double)is->video_st->codec.frame_rate_base /
666 (double)is->video_st->codec.frame_rate;
667 if (fabs(diff) > VIDEO_CORRECTION_THRESHOLD) {
668 /* if too big difference, then we adjust */
670 /* compute the difference */
673 else if (delay > 1.0)
676 vp->delay = (int)(delay * 1000 + 0.5);
678 /* now we can update the picture count */
679 if (++is->pictq_windex == VIDEO_PICTURE_QUEUE_SIZE)
680 is->pictq_windex = 0;
681 SDL_LockMutex(is->pictq_mutex);
683 SDL_UnlockMutex(is->pictq_mutex);
686 /* update video clock */
688 is->video_clock = pts;
690 is->video_clock += (double)is->video_st->codec.frame_rate_base /
691 (double)is->video_st->codec.frame_rate;
696 static int video_thread(void *arg)
698 VideoState *is = arg;
699 AVPacket pkt1, *pkt = &pkt1;
701 int len, len1, got_picture, i;
708 while (is->paused && !is->videoq.abort_request) {
711 if (packet_queue_get(&is->videoq, pkt, 1) < 0)
715 if (is->video_st->codec.codec_id == CODEC_ID_RAWVIDEO) {
716 avpicture_fill(&pict, ptr,
717 is->video_st->codec.pix_fmt,
718 is->video_st->codec.width,
719 is->video_st->codec.height);
721 if (ipts != AV_NOPTS_VALUE)
722 pts = (double)ipts * is->ic->pts_num / is->ic->pts_den;
723 if (output_picture(is, &pict, pts) < 0)
728 len1 = avcodec_decode_video(&is->video_st->codec,
729 &frame, &got_picture, ptr, len);
734 pict.data[i] = frame.data[i];
735 pict.linesize[i] = frame.linesize[i];
738 if (ipts != AV_NOPTS_VALUE)
739 pts = (double)ipts * is->ic->pts_num / is->ic->pts_den;
740 ipts = AV_NOPTS_VALUE;
741 if (output_picture(is, &pict, pts) < 0)
754 /* copy samples for viewing in editor window */
755 static void update_sample_display(VideoState *is, short *samples, int samples_size)
757 int size, len, channels;
759 channels = is->audio_st->codec.channels;
761 size = samples_size / sizeof(short);
763 len = SAMPLE_ARRAY_SIZE - is->sample_array_index;
766 memcpy(is->sample_array + is->sample_array_index, samples, len * sizeof(short));
768 is->sample_array_index += len;
769 if (is->sample_array_index >= SAMPLE_ARRAY_SIZE)
770 is->sample_array_index = 0;
775 /* maximum audio speed change to get correct sync */
776 #define SAMPLE_CORRECTION_PERCENT_MAX 2
778 /* return the new audio buffer size (samples can be added or deleted
779 to get better sync if video or external master clock) */
780 static int synchronize_audio(VideoState *is, short *samples,
781 int samples_size, double pts)
786 n = 2 * is->audio_st->codec.channels;
788 if (is->av_sync_type == AV_SYNC_EXTERNAL_CLOCK)
789 ref_clock = is->external_clock;
790 else if (is->av_sync_type == AV_SYNC_VIDEO_MASTER && is->video_st)
791 ref_clock = is->video_clock;
793 ref_clock = is->audio_clock;
795 /* if not master, then we try to remove or add samples to correct the clock */
797 if (((is->av_sync_type == AV_SYNC_VIDEO_MASTER && is->video_st) ||
798 is->av_sync_type == AV_SYNC_EXTERNAL_CLOCK) && pts != 0) {
800 int wanted_size, min_size, max_size, nb_samples;
801 delay = audio_write_get_buf_size(is);
802 diff = pts - (double)delay / (double)(n * is->audio_st->codec.sample_rate) - ref_clock;
803 wanted_size = (int)(diff * is->audio_st->codec.sample_rate) * n;
804 nb_samples = samples_size / n;
806 min_size = ((nb_samples * (100 - SAMPLE_CORRECTION_PERCENT_MAX)) / 100) * n;
807 max_size = ((nb_samples * (100 + SAMPLE_CORRECTION_PERCENT_MAX)) / 100) * n;
808 if (wanted_size < min_size)
809 wanted_size = min_size;
810 else if (wanted_size > max_size)
811 wanted_size = max_size;
814 /* XXX: do it better with sample interpolation */
815 if (wanted_size < samples_size) {
817 samples_size = wanted_size;
818 } else if (wanted_size > samples_size) {
819 uint8_t *samples_end, *q;
823 nb = (samples_size - wanted_size);
824 samples_end = (uint8_t *)samples + samples_size - n;
827 memcpy(q, samples_end, n);
831 samples_size = wanted_size;
835 /* update audio clock */
836 if (is->av_sync_type == AV_SYNC_AUDIO_MASTER && pts != 0) {
837 /* a pts is given: we update the audio clock precisely */
838 delay = audio_write_get_buf_size(is);
839 is->audio_clock = pts - (double)delay / (double)(n * is->audio_st->codec.sample_rate);
841 is->audio_clock += (double)samples_size / (double)(n * is->audio_st->codec.sample_rate);
846 /* decode one audio frame and returns its uncompressed size */
847 static int audio_decode_frame(VideoState *is, uint8_t *audio_buf, double *pts_ptr)
849 AVPacket *pkt = &is->audio_pkt;
854 if (is->paused || is->audioq.abort_request) {
857 while (is->audio_pkt_size > 0) {
858 len1 = avcodec_decode_audio(&is->audio_st->codec,
859 (int16_t *)audio_buf, &data_size,
860 is->audio_pkt_data, is->audio_pkt_size);
863 is->audio_pkt_data += len1;
864 is->audio_pkt_size -= len1;
867 if (is->audio_pkt_ipts != AV_NOPTS_VALUE)
868 pts = (double)is->audio_pkt_ipts * is->ic->pts_num / is->ic->pts_den;
870 is->audio_pkt_ipts = AV_NOPTS_VALUE;
871 /* we got samples : we can exit now */
876 /* free previous packet if any */
880 /* read next packet */
881 if (packet_queue_get(&is->audioq, pkt, 1) < 0)
883 is->audio_pkt_data = pkt->data;
884 is->audio_pkt_size = pkt->size;
885 is->audio_pkt_ipts = pkt->pts;
889 int audio_write_get_buf_size(VideoState *is)
892 delay = is->audio_hw_buf_size;
894 /* just a test to check if the estimated delay is OK */
897 if (ioctl(sdl_audio_fd, SNDCTL_DSP_GETODELAY, &val) < 0)
898 perror("SNDCTL_DSP_GETODELAY");
899 printf("real_delay=%d delay=%d\n", val, delay);
906 /* prepare a new audio buffer */
907 void sdl_audio_callback(void *opaque, Uint8 *stream, int len)
909 VideoState *is = opaque;
910 int audio_size, len1;
913 audio_callback_time = av_gettime();
916 if (is->audio_buf_index >= is->audio_buf_size) {
917 audio_size = audio_decode_frame(is, is->audio_buf, &pts);
918 if (audio_size < 0) {
919 /* if error, just output silence */
920 is->audio_buf_size = 1024;
921 memset(is->audio_buf, 0, is->audio_buf_size);
924 update_sample_display(is, (int16_t *)is->audio_buf, audio_size);
925 audio_size = synchronize_audio(is, (int16_t *)is->audio_buf, audio_size,
927 is->audio_buf_size = audio_size;
929 is->audio_buf_index = 0;
931 len1 = is->audio_buf_size - is->audio_buf_index;
934 memcpy(stream, (uint8_t *)is->audio_buf + is->audio_buf_index, len1);
937 is->audio_buf_index += len1;
942 /* open a given stream. Return 0 if OK */
943 static int stream_component_open(VideoState *is, int stream_index)
945 AVFormatContext *ic = is->ic;
948 SDL_AudioSpec wanted_spec, spec;
950 if (stream_index < 0 || stream_index >= ic->nb_streams)
952 enc = &ic->streams[stream_index]->codec;
955 /* prepare audio output */
956 if (enc->codec_type == CODEC_TYPE_AUDIO) {
957 wanted_spec.freq = enc->sample_rate;
958 wanted_spec.format = AUDIO_S16SYS;
959 wanted_spec.channels = enc->channels;
960 wanted_spec.silence = 0;
961 wanted_spec.samples = 8192;
962 wanted_spec.callback = sdl_audio_callback;
963 wanted_spec.userdata = is;
964 if (SDL_OpenAudio(&wanted_spec, &spec) < 0)
966 is->audio_hw_buf_size = spec.size;
969 codec = avcodec_find_decoder(enc->codec_id);
971 avcodec_open(enc, codec) < 0)
973 switch(enc->codec_type) {
974 case CODEC_TYPE_AUDIO:
975 is->audio_stream = stream_index;
976 is->audio_st = ic->streams[stream_index];
977 is->audio_buf_size = 0;
978 is->audio_buf_index = 0;
979 is->audio_pkt_size = 0;
980 memset(&is->audio_pkt, 0, sizeof(is->audio_pkt));
981 packet_queue_init(&is->audioq);
984 case CODEC_TYPE_VIDEO:
985 is->video_stream = stream_index;
986 is->video_st = ic->streams[stream_index];
988 packet_queue_init(&is->videoq);
989 is->video_tid = SDL_CreateThread(video_thread, is);
997 static void stream_component_close(VideoState *is, int stream_index)
999 AVFormatContext *ic = is->ic;
1000 AVCodecContext *enc;
1002 enc = &ic->streams[stream_index]->codec;
1004 switch(enc->codec_type) {
1005 case CODEC_TYPE_AUDIO:
1006 packet_queue_abort(&is->audioq);
1010 packet_queue_end(&is->audioq);
1012 case CODEC_TYPE_VIDEO:
1013 packet_queue_abort(&is->videoq);
1015 /* note: we also signal this mutex to make sure we deblock the
1016 video thread in all cases */
1017 SDL_LockMutex(is->pictq_mutex);
1018 SDL_CondSignal(is->pictq_cond);
1019 SDL_UnlockMutex(is->pictq_mutex);
1021 SDL_WaitThread(is->video_tid, NULL);
1023 packet_queue_end(&is->videoq);
1030 switch(enc->codec_type) {
1031 case CODEC_TYPE_AUDIO:
1032 is->audio_st = NULL;
1033 is->audio_stream = -1;
1035 case CODEC_TYPE_VIDEO:
1036 is->video_st = NULL;
1037 is->video_stream = -1;
1045 /* this thread gets the stream from the disk or the network */
1046 static int decode_thread(void *arg)
1048 VideoState *is = arg;
1049 AVFormatContext *ic;
1050 int err, i, ret, video_index, audio_index;
1051 AVPacket pkt1, *pkt = &pkt1;
1055 is->video_stream = -1;
1056 is->audio_stream = -1;
1058 err = av_open_input_file(&ic, is->filename, NULL, 0, NULL);
1062 err = av_find_stream_info(ic);
1066 for(i = 0; i < ic->nb_streams; i++) {
1067 AVCodecContext *enc = &ic->streams[i]->codec;
1068 switch(enc->codec_type) {
1069 case CODEC_TYPE_AUDIO:
1070 if (audio_index < 0 && !audio_disable)
1073 case CODEC_TYPE_VIDEO:
1074 if (video_index < 0 && !video_disable)
1082 dump_format(ic, 0, is->filename, 0);
1085 /* open the streams */
1086 if (audio_index >= 0) {
1087 stream_component_open(is, audio_index);
1090 if (video_index >= 0) {
1091 stream_component_open(is, video_index);
1093 if (!display_disable)
1097 if (is->video_stream < 0 && is->audio_stream < 0) {
1102 if (is->abort_request)
1104 /* if the queue are full, no need to read more */
1105 if (is->audioq.size > MAX_AUDIOQ_SIZE ||
1106 is->videoq.size > MAX_VIDEOQ_SIZE) {
1111 ret = av_read_packet(ic, pkt);
1115 if (pkt->stream_index == is->audio_stream) {
1116 packet_queue_put(&is->audioq, pkt);
1117 } else if (pkt->stream_index == is->video_stream) {
1118 packet_queue_put(&is->videoq, pkt);
1120 av_free_packet(pkt);
1123 /* wait until the end */
1124 while (!is->abort_request) {
1129 /* close each stream */
1130 if (is->audio_stream >= 0)
1131 stream_component_close(is, is->audio_stream);
1132 if (is->video_stream >= 0)
1133 stream_component_close(is, is->video_stream);
1135 av_close_input_file(is->ic);
1136 is->ic = NULL; /* safety */
1140 /* pause or resume the video */
1141 static void stream_pause(VideoState *is)
1143 is->paused = !is->paused;
1146 static VideoState *stream_open(const char *filename)
1150 is = av_mallocz(sizeof(VideoState));
1153 pstrcpy(is->filename, sizeof(is->filename), filename);
1155 is->width = screen->w;
1156 is->height = screen->h;
1161 /* start video display */
1162 is->pictq_mutex = SDL_CreateMutex();
1163 is->pictq_cond = SDL_CreateCond();
1165 /* add the refresh timer to draw the picture */
1166 schedule_refresh(is, 40);
1168 is->av_sync_type = AV_SYNC_AUDIO_MASTER;
1170 is->parse_tid = SDL_CreateThread(decode_thread, is);
1171 if (!is->parse_tid) {
1178 static void stream_close(VideoState *is)
1182 /* XXX: use a special url_shutdown call to abort parse cleanly */
1183 is->abort_request = 1;
1184 SDL_WaitThread(is->parse_tid, NULL);
1186 /* free all pictures */
1187 for(i=0;i<VIDEO_PICTURE_QUEUE_SIZE; i++) {
1190 SDL_FreeYUVOverlay(vp->bmp);
1194 SDL_DestroyMutex(is->pictq_mutex);
1195 SDL_DestroyCond(is->pictq_cond);
1198 void toggle_full_screen(void)
1201 is_full_screen = !is_full_screen;
1202 if (!fs_screen_width) {
1203 /* use default SDL method */
1204 SDL_WM_ToggleFullScreen(screen);
1206 /* use the recorded resolution */
1207 flags = SDL_HWSURFACE|SDL_ASYNCBLIT|SDL_HWACCEL;
1208 if (is_full_screen) {
1209 w = fs_screen_width;
1210 h = fs_screen_height;
1211 flags |= SDL_FULLSCREEN;
1215 flags |= SDL_RESIZABLE;
1217 screen = SDL_SetVideoMode(w, h, 0, flags);
1218 cur_stream->width = w;
1219 cur_stream->height = h;
1223 void toggle_pause(void)
1226 stream_pause(cur_stream);
1232 stream_close(cur_stream);
1241 void toggle_audio_display(void)
1244 cur_stream->show_audio = !cur_stream->show_audio;
1248 /* handle an event sent by the GUI */
1249 void event_loop(void)
1254 SDL_WaitEvent(&event);
1255 switch(event.type) {
1257 switch(event.key.keysym.sym) {
1263 toggle_full_screen();
1270 toggle_audio_display();
1276 case SDL_VIDEORESIZE:
1278 screen = SDL_SetVideoMode(event.resize.w, event.resize.h, 0,
1279 SDL_HWSURFACE|SDL_RESIZABLE|SDL_ASYNCBLIT|SDL_HWACCEL);
1280 cur_stream->width = event.resize.w;
1281 cur_stream->height = event.resize.h;
1287 case FF_ALLOC_EVENT:
1288 alloc_picture(event.user.data1);
1290 case FF_REFRESH_EVENT:
1291 video_refresh_timer(event.user.data1);
1299 void opt_width(const char *arg)
1301 screen_width = atoi(arg);
1304 void opt_height(const char *arg)
1306 screen_height = atoi(arg);
1309 static void opt_format(const char *arg)
1311 file_iformat = av_find_input_format(arg);
1312 if (!file_iformat) {
1313 fprintf(stderr, "Unknown input format: %s\n", arg);
1318 const OptionDef options[] = {
1319 { "h", 0, {(void*)show_help}, "show help" },
1320 { "x", HAS_ARG, {(void*)opt_width}, "force displayed width", "width" },
1321 { "y", HAS_ARG, {(void*)opt_height}, "force displayed height", "height" },
1322 { "an", OPT_BOOL, {(void*)&audio_disable}, "disable audio" },
1323 { "vn", OPT_BOOL, {(void*)&video_disable}, "disable video" },
1324 { "nodisp", OPT_BOOL, {(void*)&display_disable}, "disable graphical display" },
1325 { "f", HAS_ARG, {(void*)opt_format}, "force format", "fmt" },
1326 { "stats", OPT_BOOL | OPT_EXPERT, {(void*)&show_status}, "show status", "" },
1330 void show_help(void)
1332 printf("usage: ffplay [options] input_file\n"
1333 "Simple media player\n");
1335 show_help_options(options);
1336 printf("\nWhile playing:\n"
1338 "f toggle full screen\n"
1340 "a show audio waves\n"
1345 void parse_arg_file(const char *filename)
1347 input_filename = filename;
1350 /* Called from the main */
1351 int main(int argc, char **argv)
1355 /* register all codecs, demux and protocols */
1358 parse_options(argc, argv, options);
1360 if (!input_filename)
1363 if (display_disable) {
1366 flags = SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER;
1367 #ifndef CONFIG_WIN32
1368 flags |= SDL_INIT_EVENTTHREAD; /* Not supported on win32 */
1370 if (SDL_Init (flags)) {
1371 fprintf(stderr, "Could not initialize SDL - exiting\n");
1375 if (!display_disable) {
1376 screen = SDL_SetVideoMode(screen_width, screen_height, 0,
1377 SDL_HWSURFACE|SDL_RESIZABLE|SDL_ASYNCBLIT|SDL_HWACCEL);
1379 fprintf(stderr, "SDL: could not set video mode - exiting\n");
1382 SDL_WM_SetCaption("FFplay", "FFplay");
1384 /* save the screen resolution... SDL should allow full screen
1385 by resizing the window */
1388 dpy = XOpenDisplay(NULL);
1390 fs_screen_width = DisplayWidth(dpy, DefaultScreen(dpy));
1391 fs_screen_height = DisplayHeight(dpy, DefaultScreen(dpy));
1398 SDL_EventState(SDL_ACTIVEEVENT, SDL_IGNORE);
1399 SDL_EventState(SDL_MOUSEMOTION, SDL_IGNORE);
1400 SDL_EventState(SDL_SYSWMEVENT, SDL_IGNORE);
1401 SDL_EventState(SDL_USEREVENT, SDL_IGNORE);
1403 cur_stream = stream_open(input_filename);