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;
78 int dtg_active_format;
83 double external_clock; /* external clock */
85 double audio_clock; /* current audio clock value */
88 int audio_hw_buf_size;
89 /* samples output by the codec. we reserve more space for avsync
91 uint8_t audio_buf[(AVCODEC_MAX_AUDIO_FRAME_SIZE * 3) / 2];
92 int audio_buf_size; /* in bytes */
93 int audio_buf_index; /* in bytes */
95 uint8_t *audio_pkt_data;
97 int64_t audio_pkt_ipts;
99 int show_audio; /* if true, display audio samples */
100 int16_t sample_array[SAMPLE_ARRAY_SIZE];
101 int sample_array_index;
104 double video_clock; /* current video clock value */
109 VideoPicture pictq[VIDEO_PICTURE_QUEUE_SIZE];
110 int pictq_size, pictq_rindex, pictq_windex;
111 SDL_mutex *pictq_mutex;
112 SDL_cond *pictq_cond;
114 // QETimer *video_timer;
116 int width, height, xleft, ytop;
119 void show_help(void);
120 int audio_write_get_buf_size(VideoState *is);
122 /* options specified by the user */
123 static AVInputFormat *file_iformat;
124 static const char *input_filename;
125 static int fs_screen_width;
126 static int fs_screen_height;
127 static int screen_width = 640;
128 static int screen_height = 480;
129 static int audio_disable;
130 static int video_disable;
131 static int display_disable;
132 static int show_status;
134 /* current context */
135 static int is_full_screen;
136 static VideoState *cur_stream;
137 static int64_t audio_callback_time;
139 #define FF_ALLOC_EVENT (SDL_USEREVENT)
140 #define FF_REFRESH_EVENT (SDL_USEREVENT + 1)
144 /* packet queue handling */
145 static void packet_queue_init(PacketQueue *q)
147 memset(q, 0, sizeof(PacketQueue));
148 q->mutex = SDL_CreateMutex();
149 q->cond = SDL_CreateCond();
152 static void packet_queue_end(PacketQueue *q)
154 AVPacketList *pkt, *pkt1;
156 for(pkt = q->first_pkt; pkt != NULL; pkt = pkt1) {
158 av_free_packet(&pkt->pkt);
160 SDL_DestroyMutex(q->mutex);
161 SDL_DestroyCond(q->cond);
164 static int packet_queue_put(PacketQueue *q, AVPacket *pkt)
168 pkt1 = av_malloc(sizeof(AVPacketList));
174 SDL_LockMutex(q->mutex);
180 q->last_pkt->next = pkt1;
183 q->size += pkt1->pkt.size;
184 /* XXX: should duplicate packet data in DV case */
185 SDL_CondSignal(q->cond);
187 SDL_UnlockMutex(q->mutex);
191 static void packet_queue_abort(PacketQueue *q)
193 SDL_LockMutex(q->mutex);
195 q->abort_request = 1;
197 SDL_CondSignal(q->cond);
199 SDL_UnlockMutex(q->mutex);
202 /* return < 0 if aborted, 0 if no packet and > 0 if packet. */
203 static int packet_queue_get(PacketQueue *q, AVPacket *pkt, int block)
208 SDL_LockMutex(q->mutex);
211 if (q->abort_request) {
218 q->first_pkt = pkt1->next;
222 q->size -= pkt1->pkt.size;
231 SDL_CondWait(q->cond, q->mutex);
234 SDL_UnlockMutex(q->mutex);
238 static inline void fill_rectangle(SDL_Surface *screen,
239 int x, int y, int w, int h, int color)
246 SDL_FillRect(screen, &rect, color);
250 /* draw only the border of a rectangle */
251 void fill_border(VideoState *s, int x, int y, int w, int h, int color)
255 /* fill the background */
259 w2 = s->width - (x + w);
265 h2 = s->height - (y + h);
268 fill_rectangle(screen,
272 fill_rectangle(screen,
273 s->xleft + s->width - w2, s->ytop,
276 fill_rectangle(screen,
277 s->xleft + w1, s->ytop,
278 s->width - w1 - w2, h1,
280 fill_rectangle(screen,
281 s->xleft + w1, s->ytop + s->height - h2,
282 s->width - w1 - w2, h2,
287 static void video_image_display(VideoState *is)
291 int width, height, x, y;
294 vp = &is->pictq[is->pictq_rindex];
296 /* XXX: use variable in the frame */
297 aspect_ratio = is->video_st->codec.aspect_ratio;
298 if (aspect_ratio <= 0.0)
299 aspect_ratio = (float)is->video_st->codec.width /
300 (float)is->video_st->codec.height;
301 /* if an active format is indicated, then it overrides the
304 if (is->video_st->codec.dtg_active_format != is->dtg_active_format) {
305 is->dtg_active_format = is->video_st->codec.dtg_active_format;
306 printf("dtg_active_format=%d\n", is->dtg_active_format);
310 switch(is->video_st->codec.dtg_active_format) {
311 case FF_DTG_AFD_SAME:
316 aspect_ratio = 4.0 / 3.0;
318 case FF_DTG_AFD_16_9:
319 aspect_ratio = 16.0 / 9.0;
321 case FF_DTG_AFD_14_9:
322 aspect_ratio = 14.0 / 9.0;
324 case FF_DTG_AFD_4_3_SP_14_9:
325 aspect_ratio = 14.0 / 9.0;
327 case FF_DTG_AFD_16_9_SP_14_9:
328 aspect_ratio = 14.0 / 9.0;
330 case FF_DTG_AFD_SP_4_3:
331 aspect_ratio = 4.0 / 3.0;
336 /* XXX: we suppose the screen has a 1.0 pixel ratio */
338 width = ((int)rint(height * aspect_ratio)) & -3;
339 if (width > is->width) {
341 height = ((int)rint(width / aspect_ratio)) & -3;
343 x = (is->width - width) / 2;
344 y = (is->height - height) / 2;
345 if (!is->no_background) {
346 /* fill the background */
347 // fill_border(is, x, y, width, height, QERGB(0x00, 0x00, 0x00));
349 is->no_background = 0;
351 rect.x = is->xleft + x;
352 rect.y = is->xleft + y;
355 SDL_DisplayYUVOverlay(vp->bmp, &rect);
358 fill_rectangle(screen,
359 is->xleft, is->ytop, is->width, is->height,
360 QERGB(0x00, 0x00, 0x00));
365 static inline int compute_mod(int a, int b)
374 static void video_audio_display(VideoState *s)
376 int i, i_start, x, y1, y, ys, delay, n, nb_display_channels;
377 int ch, channels, h, h2, bgcolor, fgcolor;
380 /* compute display index : center on currently output samples */
381 channels = s->audio_st->codec.channels;
382 nb_display_channels = channels;
385 delay = audio_write_get_buf_size(s);
388 /* to be more precise, we take into account the time spent since
389 the last buffer computation */
390 if (audio_callback_time) {
391 time_diff = av_gettime() - audio_callback_time;
392 delay += (time_diff * s->audio_st->codec.sample_rate) / 1000000;
395 delay -= s->width / 2;
396 if (delay < s->width)
398 i_start = compute_mod(s->sample_array_index - delay * channels, SAMPLE_ARRAY_SIZE);
399 s->last_i_start = i_start;
401 i_start = s->last_i_start;
404 bgcolor = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00);
405 fill_rectangle(screen,
406 s->xleft, s->ytop, s->width, s->height,
409 fgcolor = SDL_MapRGB(screen->format, 0xff, 0xff, 0xff);
411 /* total height for one channel */
412 h = s->height / nb_display_channels;
413 /* graph height / 2 */
415 for(ch = 0;ch < nb_display_channels; ch++) {
417 y1 = s->ytop + ch * h + (h / 2); /* position of center line */
418 for(x = 0; x < s->width; x++) {
419 y = (s->sample_array[i] * h2) >> 15;
426 fill_rectangle(screen,
427 s->xleft + x, ys, 1, y,
430 if (i >= SAMPLE_ARRAY_SIZE)
431 i -= SAMPLE_ARRAY_SIZE;
435 fgcolor = SDL_MapRGB(screen->format, 0x00, 0x00, 0xff);
437 for(ch = 1;ch < nb_display_channels; ch++) {
438 y = s->ytop + ch * h;
439 fill_rectangle(screen,
440 s->xleft, y, s->width, 1,
443 SDL_UpdateRect(screen, s->xleft, s->ytop, s->width, s->height);
446 /* display the current picture, if any */
447 static void video_display(VideoState *is)
449 if (is->audio_st && is->show_audio)
450 video_audio_display(is);
451 else if (is->video_st)
452 video_image_display(is);
455 static Uint32 sdl_refresh_timer_cb(Uint32 interval, void *opaque)
458 event.type = FF_REFRESH_EVENT;
459 event.user.data1 = opaque;
460 SDL_PushEvent(&event);
461 return 0; /* 0 means stop timer */
464 /* schedule a video refresh in 'delay' ms */
465 static void schedule_refresh(VideoState *is, int delay)
467 SDL_AddTimer(delay, sdl_refresh_timer_cb, is);
470 /* called to display each frame */
471 static void video_refresh_timer(void *opaque)
473 VideoState *is = opaque;
477 if (is->pictq_size == 0) {
478 /* if no picture, need to wait */
479 schedule_refresh(is, 40);
481 vp = &is->pictq[is->pictq_rindex];
483 /* launch timer for next picture */
484 schedule_refresh(is, vp->delay);
486 /* display picture */
489 /* update queue size and signal for next picture */
490 if (++is->pictq_rindex == VIDEO_PICTURE_QUEUE_SIZE)
491 is->pictq_rindex = 0;
493 SDL_LockMutex(is->pictq_mutex);
495 SDL_CondSignal(is->pictq_cond);
496 SDL_UnlockMutex(is->pictq_mutex);
498 } else if (is->audio_st) {
499 /* draw the next audio frame */
501 schedule_refresh(is, 40);
503 /* if only audio stream, then display the audio bars (better
504 than nothing, just to test the implementation */
506 /* display picture */
509 schedule_refresh(is, 100);
512 static int64_t last_time;
516 cur_time = av_gettime();
517 if (!last_time || (cur_time - last_time) >= 500 * 1000) {
521 aqsize = is->audioq.size;
523 vqsize = is->videoq.size;
524 printf("A:%7.2f V:%7.2f aq=%5dKB vq=%5dKB \r",
525 is->audio_clock, is->video_clock, aqsize / 1024, vqsize / 1024);
527 last_time = cur_time;
532 /* allocate a picture (needs to do that in main thread to avoid
533 potential locking problems */
534 static void alloc_picture(void *opaque)
536 VideoState *is = opaque;
540 vp = &is->pictq[is->pictq_windex];
543 SDL_FreeYUVOverlay(vp->bmp);
545 /* XXX: use generic function */
546 switch(is->video_st->codec.pix_fmt) {
547 case PIX_FMT_YUV420P:
548 case PIX_FMT_YUV422P:
549 case PIX_FMT_YUV444P:
551 case PIX_FMT_YUV410P:
552 case PIX_FMT_YUV411P:
561 vp->bmp = SDL_CreateYUVOverlay(is->video_st->codec.width,
562 is->video_st->codec.height,
567 vp->bmp = bmp_alloc(screen,
568 is->video_st->codec.width,
569 is->video_st->codec.height,
570 screen->bitmap_format,
575 vp->width = is->video_st->codec.width;
576 vp->height = is->video_st->codec.height;
578 SDL_LockMutex(is->pictq_mutex);
580 SDL_CondSignal(is->pictq_cond);
581 SDL_UnlockMutex(is->pictq_mutex);
584 #define VIDEO_CORRECTION_THRESHOLD 0.2
586 static int output_picture(VideoState *is, AVPicture *src_pict, double pts)
591 double delay, ref_clock, diff;
593 /* wait until we have space to put a new picture */
594 SDL_LockMutex(is->pictq_mutex);
595 while (is->pictq_size >= VIDEO_PICTURE_QUEUE_SIZE &&
596 !is->videoq.abort_request) {
597 SDL_CondWait(is->pictq_cond, is->pictq_mutex);
599 SDL_UnlockMutex(is->pictq_mutex);
601 if (is->videoq.abort_request)
604 vp = &is->pictq[is->pictq_windex];
606 /* alloc or resize hardware picture buffer */
608 vp->width != is->video_st->codec.width ||
609 vp->height != is->video_st->codec.height) {
614 /* the allocation must be done in the main thread to avoid
616 event.type = FF_ALLOC_EVENT;
617 event.user.data1 = is;
618 SDL_PushEvent(&event);
620 /* wait until the picture is allocated */
621 SDL_LockMutex(is->pictq_mutex);
622 while (!vp->allocated && !is->videoq.abort_request) {
623 SDL_CondWait(is->pictq_cond, is->pictq_mutex);
625 SDL_UnlockMutex(is->pictq_mutex);
627 if (is->videoq.abort_request)
632 /* get a pointer on the bitmap */
633 SDL_LockYUVOverlay (vp->bmp);
635 dst_pix_fmt = PIX_FMT_YUV420P;
636 pict.data[0] = vp->bmp->pixels[0];
637 pict.data[1] = vp->bmp->pixels[2];
638 pict.data[2] = vp->bmp->pixels[1];
640 pict.linesize[0] = vp->bmp->pitches[0];
641 pict.linesize[1] = vp->bmp->pitches[2];
642 pict.linesize[2] = vp->bmp->pitches[1];
644 img_convert(&pict, dst_pix_fmt,
645 src_pict, is->video_st->codec.pix_fmt,
646 is->video_st->codec.width, is->video_st->codec.height);
647 /* update the bitmap content */
648 SDL_UnlockYUVOverlay(vp->bmp);
650 /* compute delay for the next frame and take into account the
651 pts if needed to make a correction. Since we do not support
652 correct MPEG B frame PTS, we put a high threshold */
654 if (is->av_sync_type == AV_SYNC_VIDEO_MASTER) {
655 ref_clock = is->video_clock;
656 } else if (is->av_sync_type == AV_SYNC_AUDIO_MASTER) {
657 /* cannot use audio master if no audio, so fall back to no sync */
659 ref_clock = is->video_clock;
661 ref_clock = is->audio_clock;
663 ref_clock = is->external_clock;
665 diff = is->video_clock - ref_clock;
666 delay = (double)is->video_st->codec.frame_rate_base /
667 (double)is->video_st->codec.frame_rate;
668 if (fabs(diff) > VIDEO_CORRECTION_THRESHOLD) {
669 /* if too big difference, then we adjust */
671 /* compute the difference */
674 else if (delay > 1.0)
677 vp->delay = (int)(delay * 1000 + 0.5);
679 /* now we can update the picture count */
680 if (++is->pictq_windex == VIDEO_PICTURE_QUEUE_SIZE)
681 is->pictq_windex = 0;
682 SDL_LockMutex(is->pictq_mutex);
684 SDL_UnlockMutex(is->pictq_mutex);
687 /* update video clock */
689 is->video_clock = pts;
691 is->video_clock += (double)is->video_st->codec.frame_rate_base /
692 (double)is->video_st->codec.frame_rate;
697 static int video_thread(void *arg)
699 VideoState *is = arg;
700 AVPacket pkt1, *pkt = &pkt1;
702 int len, len1, got_picture, i;
709 while (is->paused && !is->videoq.abort_request) {
712 if (packet_queue_get(&is->videoq, pkt, 1) < 0)
716 if (is->video_st->codec.codec_id == CODEC_ID_RAWVIDEO) {
717 avpicture_fill(&pict, ptr,
718 is->video_st->codec.pix_fmt,
719 is->video_st->codec.width,
720 is->video_st->codec.height);
722 if (ipts != AV_NOPTS_VALUE)
723 pts = (double)ipts * is->ic->pts_num / is->ic->pts_den;
724 if (output_picture(is, &pict, pts) < 0)
729 len1 = avcodec_decode_video(&is->video_st->codec,
730 &frame, &got_picture, ptr, len);
735 pict.data[i] = frame.data[i];
736 pict.linesize[i] = frame.linesize[i];
739 if (ipts != AV_NOPTS_VALUE)
740 pts = (double)ipts * is->ic->pts_num / is->ic->pts_den;
741 ipts = AV_NOPTS_VALUE;
742 if (output_picture(is, &pict, pts) < 0)
755 /* copy samples for viewing in editor window */
756 static void update_sample_display(VideoState *is, short *samples, int samples_size)
758 int size, len, channels;
760 channels = is->audio_st->codec.channels;
762 size = samples_size / sizeof(short);
764 len = SAMPLE_ARRAY_SIZE - is->sample_array_index;
767 memcpy(is->sample_array + is->sample_array_index, samples, len * sizeof(short));
769 is->sample_array_index += len;
770 if (is->sample_array_index >= SAMPLE_ARRAY_SIZE)
771 is->sample_array_index = 0;
776 /* maximum audio speed change to get correct sync */
777 #define SAMPLE_CORRECTION_PERCENT_MAX 2
779 /* return the new audio buffer size (samples can be added or deleted
780 to get better sync if video or external master clock) */
781 static int synchronize_audio(VideoState *is, short *samples,
782 int samples_size, double pts)
787 n = 2 * is->audio_st->codec.channels;
789 if (is->av_sync_type == AV_SYNC_EXTERNAL_CLOCK)
790 ref_clock = is->external_clock;
791 else if (is->av_sync_type == AV_SYNC_VIDEO_MASTER && is->video_st)
792 ref_clock = is->video_clock;
794 ref_clock = is->audio_clock;
796 /* if not master, then we try to remove or add samples to correct the clock */
798 if (((is->av_sync_type == AV_SYNC_VIDEO_MASTER && is->video_st) ||
799 is->av_sync_type == AV_SYNC_EXTERNAL_CLOCK) && pts != 0) {
801 int wanted_size, min_size, max_size, nb_samples;
802 delay = audio_write_get_buf_size(is);
803 diff = pts - (double)delay / (double)(n * is->audio_st->codec.sample_rate) - ref_clock;
804 wanted_size = (int)(diff * is->audio_st->codec.sample_rate) * n;
805 nb_samples = samples_size / n;
807 min_size = ((nb_samples * (100 - SAMPLE_CORRECTION_PERCENT_MAX)) / 100) * n;
808 max_size = ((nb_samples * (100 + SAMPLE_CORRECTION_PERCENT_MAX)) / 100) * n;
809 if (wanted_size < min_size)
810 wanted_size = min_size;
811 else if (wanted_size > max_size)
812 wanted_size = max_size;
815 /* XXX: do it better with sample interpolation */
816 if (wanted_size < samples_size) {
818 samples_size = wanted_size;
819 } else if (wanted_size > samples_size) {
820 uint8_t *samples_end, *q;
824 nb = (samples_size - wanted_size);
825 samples_end = (uint8_t *)samples + samples_size - n;
828 memcpy(q, samples_end, n);
832 samples_size = wanted_size;
836 /* update audio clock */
837 if (is->av_sync_type == AV_SYNC_AUDIO_MASTER && pts != 0) {
838 /* a pts is given: we update the audio clock precisely */
839 delay = audio_write_get_buf_size(is);
840 is->audio_clock = pts - (double)delay / (double)(n * is->audio_st->codec.sample_rate);
842 is->audio_clock += (double)samples_size / (double)(n * is->audio_st->codec.sample_rate);
847 /* decode one audio frame and returns its uncompressed size */
848 static int audio_decode_frame(VideoState *is, uint8_t *audio_buf, double *pts_ptr)
850 AVPacket *pkt = &is->audio_pkt;
855 if (is->paused || is->audioq.abort_request) {
858 while (is->audio_pkt_size > 0) {
859 len1 = avcodec_decode_audio(&is->audio_st->codec,
860 (int16_t *)audio_buf, &data_size,
861 is->audio_pkt_data, is->audio_pkt_size);
864 is->audio_pkt_data += len1;
865 is->audio_pkt_size -= len1;
868 if (is->audio_pkt_ipts != AV_NOPTS_VALUE)
869 pts = (double)is->audio_pkt_ipts * is->ic->pts_num / is->ic->pts_den;
871 is->audio_pkt_ipts = AV_NOPTS_VALUE;
872 /* we got samples : we can exit now */
877 /* free previous packet if any */
881 /* read next packet */
882 if (packet_queue_get(&is->audioq, pkt, 1) < 0)
884 is->audio_pkt_data = pkt->data;
885 is->audio_pkt_size = pkt->size;
886 is->audio_pkt_ipts = pkt->pts;
890 int audio_write_get_buf_size(VideoState *is)
893 delay = is->audio_hw_buf_size;
895 /* just a test to check if the estimated delay is OK */
898 if (ioctl(sdl_audio_fd, SNDCTL_DSP_GETODELAY, &val) < 0)
899 perror("SNDCTL_DSP_GETODELAY");
900 printf("real_delay=%d delay=%d\n", val, delay);
907 /* prepare a new audio buffer */
908 void sdl_audio_callback(void *opaque, Uint8 *stream, int len)
910 VideoState *is = opaque;
911 int audio_size, len1;
914 audio_callback_time = av_gettime();
917 if (is->audio_buf_index >= is->audio_buf_size) {
918 audio_size = audio_decode_frame(is, is->audio_buf, &pts);
919 if (audio_size < 0) {
920 /* if error, just output silence */
921 is->audio_buf_size = 1024;
922 memset(is->audio_buf, 0, is->audio_buf_size);
925 update_sample_display(is, (int16_t *)is->audio_buf, audio_size);
926 audio_size = synchronize_audio(is, (int16_t *)is->audio_buf, audio_size,
928 is->audio_buf_size = audio_size;
930 is->audio_buf_index = 0;
932 len1 = is->audio_buf_size - is->audio_buf_index;
935 memcpy(stream, (uint8_t *)is->audio_buf + is->audio_buf_index, len1);
938 is->audio_buf_index += len1;
943 /* open a given stream. Return 0 if OK */
944 static int stream_component_open(VideoState *is, int stream_index)
946 AVFormatContext *ic = is->ic;
949 SDL_AudioSpec wanted_spec, spec;
951 if (stream_index < 0 || stream_index >= ic->nb_streams)
953 enc = &ic->streams[stream_index]->codec;
956 /* prepare audio output */
957 if (enc->codec_type == CODEC_TYPE_AUDIO) {
958 wanted_spec.freq = enc->sample_rate;
959 wanted_spec.format = AUDIO_S16SYS;
960 wanted_spec.channels = enc->channels;
961 wanted_spec.silence = 0;
962 wanted_spec.samples = 8192;
963 wanted_spec.callback = sdl_audio_callback;
964 wanted_spec.userdata = is;
965 if (SDL_OpenAudio(&wanted_spec, &spec) < 0)
967 is->audio_hw_buf_size = spec.size;
970 codec = avcodec_find_decoder(enc->codec_id);
972 avcodec_open(enc, codec) < 0)
974 switch(enc->codec_type) {
975 case CODEC_TYPE_AUDIO:
976 is->audio_stream = stream_index;
977 is->audio_st = ic->streams[stream_index];
978 is->audio_buf_size = 0;
979 is->audio_buf_index = 0;
980 is->audio_pkt_size = 0;
981 memset(&is->audio_pkt, 0, sizeof(is->audio_pkt));
982 packet_queue_init(&is->audioq);
985 case CODEC_TYPE_VIDEO:
986 is->video_stream = stream_index;
987 is->video_st = ic->streams[stream_index];
989 packet_queue_init(&is->videoq);
990 is->video_tid = SDL_CreateThread(video_thread, is);
998 static void stream_component_close(VideoState *is, int stream_index)
1000 AVFormatContext *ic = is->ic;
1001 AVCodecContext *enc;
1003 enc = &ic->streams[stream_index]->codec;
1005 switch(enc->codec_type) {
1006 case CODEC_TYPE_AUDIO:
1007 packet_queue_abort(&is->audioq);
1011 packet_queue_end(&is->audioq);
1013 case CODEC_TYPE_VIDEO:
1014 packet_queue_abort(&is->videoq);
1016 /* note: we also signal this mutex to make sure we deblock the
1017 video thread in all cases */
1018 SDL_LockMutex(is->pictq_mutex);
1019 SDL_CondSignal(is->pictq_cond);
1020 SDL_UnlockMutex(is->pictq_mutex);
1022 SDL_WaitThread(is->video_tid, NULL);
1024 packet_queue_end(&is->videoq);
1031 switch(enc->codec_type) {
1032 case CODEC_TYPE_AUDIO:
1033 is->audio_st = NULL;
1034 is->audio_stream = -1;
1036 case CODEC_TYPE_VIDEO:
1037 is->video_st = NULL;
1038 is->video_stream = -1;
1045 /* since we have only one decoding thread, we can use a global
1046 variable instead of a thread local variable */
1047 static VideoState *global_video_state;
1049 static int decode_interrupt_cb(void)
1051 return (global_video_state && global_video_state->abort_request);
1054 /* this thread gets the stream from the disk or the network */
1055 static int decode_thread(void *arg)
1057 VideoState *is = arg;
1058 AVFormatContext *ic;
1059 int err, i, ret, video_index, audio_index;
1060 AVPacket pkt1, *pkt = &pkt1;
1064 is->video_stream = -1;
1065 is->audio_stream = -1;
1067 global_video_state = is;
1068 url_set_interrupt_cb(decode_interrupt_cb);
1070 err = av_open_input_file(&ic, is->filename, NULL, 0, NULL);
1074 err = av_find_stream_info(ic);
1078 for(i = 0; i < ic->nb_streams; i++) {
1079 AVCodecContext *enc = &ic->streams[i]->codec;
1080 switch(enc->codec_type) {
1081 case CODEC_TYPE_AUDIO:
1082 if (audio_index < 0 && !audio_disable)
1085 case CODEC_TYPE_VIDEO:
1086 if (video_index < 0 && !video_disable)
1094 dump_format(ic, 0, is->filename, 0);
1097 /* open the streams */
1098 if (audio_index >= 0) {
1099 stream_component_open(is, audio_index);
1102 if (video_index >= 0) {
1103 stream_component_open(is, video_index);
1105 if (!display_disable)
1109 if (is->video_stream < 0 && is->audio_stream < 0) {
1114 if (is->abort_request)
1116 if (is->paused != is->last_paused) {
1117 is->last_paused = is->paused;
1118 if (ic->iformat == &rtsp_demux) {
1125 if (is->paused && ic->iformat == &rtsp_demux) {
1126 /* wait 10 ms to avoid trying to get another packet */
1132 /* if the queue are full, no need to read more */
1133 if (is->audioq.size > MAX_AUDIOQ_SIZE ||
1134 is->videoq.size > MAX_VIDEOQ_SIZE) {
1139 ret = av_read_packet(ic, pkt);
1143 if (pkt->stream_index == is->audio_stream) {
1144 packet_queue_put(&is->audioq, pkt);
1145 } else if (pkt->stream_index == is->video_stream) {
1146 packet_queue_put(&is->videoq, pkt);
1148 av_free_packet(pkt);
1151 /* wait until the end */
1152 while (!is->abort_request) {
1157 /* disable interrupting */
1158 global_video_state = NULL;
1160 /* close each stream */
1161 if (is->audio_stream >= 0)
1162 stream_component_close(is, is->audio_stream);
1163 if (is->video_stream >= 0)
1164 stream_component_close(is, is->video_stream);
1166 av_close_input_file(is->ic);
1167 is->ic = NULL; /* safety */
1168 url_set_interrupt_cb(NULL);
1173 /* pause or resume the video */
1174 static void stream_pause(VideoState *is)
1176 is->paused = !is->paused;
1179 static VideoState *stream_open(const char *filename)
1183 is = av_mallocz(sizeof(VideoState));
1186 pstrcpy(is->filename, sizeof(is->filename), filename);
1188 is->width = screen->w;
1189 is->height = screen->h;
1194 /* start video display */
1195 is->pictq_mutex = SDL_CreateMutex();
1196 is->pictq_cond = SDL_CreateCond();
1198 /* add the refresh timer to draw the picture */
1199 schedule_refresh(is, 40);
1201 is->av_sync_type = AV_SYNC_AUDIO_MASTER;
1203 is->parse_tid = SDL_CreateThread(decode_thread, is);
1204 if (!is->parse_tid) {
1211 static void stream_close(VideoState *is)
1215 /* XXX: use a special url_shutdown call to abort parse cleanly */
1216 is->abort_request = 1;
1217 SDL_WaitThread(is->parse_tid, NULL);
1219 /* free all pictures */
1220 for(i=0;i<VIDEO_PICTURE_QUEUE_SIZE; i++) {
1223 SDL_FreeYUVOverlay(vp->bmp);
1227 SDL_DestroyMutex(is->pictq_mutex);
1228 SDL_DestroyCond(is->pictq_cond);
1231 void toggle_full_screen(void)
1234 is_full_screen = !is_full_screen;
1235 if (!fs_screen_width) {
1236 /* use default SDL method */
1237 SDL_WM_ToggleFullScreen(screen);
1239 /* use the recorded resolution */
1240 flags = SDL_HWSURFACE|SDL_ASYNCBLIT|SDL_HWACCEL;
1241 if (is_full_screen) {
1242 w = fs_screen_width;
1243 h = fs_screen_height;
1244 flags |= SDL_FULLSCREEN;
1248 flags |= SDL_RESIZABLE;
1250 screen = SDL_SetVideoMode(w, h, 0, flags);
1251 cur_stream->width = w;
1252 cur_stream->height = h;
1256 void toggle_pause(void)
1259 stream_pause(cur_stream);
1265 stream_close(cur_stream);
1274 void toggle_audio_display(void)
1277 cur_stream->show_audio = !cur_stream->show_audio;
1281 /* handle an event sent by the GUI */
1282 void event_loop(void)
1287 SDL_WaitEvent(&event);
1288 switch(event.type) {
1290 switch(event.key.keysym.sym) {
1296 toggle_full_screen();
1303 toggle_audio_display();
1309 case SDL_VIDEORESIZE:
1311 screen = SDL_SetVideoMode(event.resize.w, event.resize.h, 0,
1312 SDL_HWSURFACE|SDL_RESIZABLE|SDL_ASYNCBLIT|SDL_HWACCEL);
1313 cur_stream->width = event.resize.w;
1314 cur_stream->height = event.resize.h;
1320 case FF_ALLOC_EVENT:
1321 alloc_picture(event.user.data1);
1323 case FF_REFRESH_EVENT:
1324 video_refresh_timer(event.user.data1);
1332 void opt_width(const char *arg)
1334 screen_width = atoi(arg);
1337 void opt_height(const char *arg)
1339 screen_height = atoi(arg);
1342 static void opt_format(const char *arg)
1344 file_iformat = av_find_input_format(arg);
1345 if (!file_iformat) {
1346 fprintf(stderr, "Unknown input format: %s\n", arg);
1351 void opt_rtp_tcp(void)
1353 /* only tcp protocol */
1354 rtsp_default_protocols = (1 << RTSP_PROTOCOL_RTP_TCP);
1357 const OptionDef options[] = {
1358 { "h", 0, {(void*)show_help}, "show help" },
1359 { "x", HAS_ARG, {(void*)opt_width}, "force displayed width", "width" },
1360 { "y", HAS_ARG, {(void*)opt_height}, "force displayed height", "height" },
1361 { "an", OPT_BOOL, {(void*)&audio_disable}, "disable audio" },
1362 { "vn", OPT_BOOL, {(void*)&video_disable}, "disable video" },
1363 { "nodisp", OPT_BOOL, {(void*)&display_disable}, "disable graphical display" },
1364 { "f", HAS_ARG, {(void*)opt_format}, "force format", "fmt" },
1365 { "stats", OPT_BOOL | OPT_EXPERT, {(void*)&show_status}, "show status", "" },
1366 { "rtp_tcp", OPT_EXPERT, {(void*)&opt_rtp_tcp}, "force RTP/TCP protocol usage", "" },
1370 void show_help(void)
1372 printf("usage: ffplay [options] input_file\n"
1373 "Simple media player\n");
1375 show_help_options(options);
1376 printf("\nWhile playing:\n"
1378 "f toggle full screen\n"
1380 "a show audio waves\n"
1385 void parse_arg_file(const char *filename)
1387 input_filename = filename;
1390 /* Called from the main */
1391 int main(int argc, char **argv)
1395 /* register all codecs, demux and protocols */
1398 parse_options(argc, argv, options);
1400 if (!input_filename)
1403 if (display_disable) {
1406 flags = SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER;
1407 #ifndef CONFIG_WIN32
1408 flags |= SDL_INIT_EVENTTHREAD; /* Not supported on win32 */
1410 if (SDL_Init (flags)) {
1411 fprintf(stderr, "Could not initialize SDL - exiting\n");
1415 if (!display_disable) {
1416 screen = SDL_SetVideoMode(screen_width, screen_height, 0,
1417 SDL_HWSURFACE|SDL_RESIZABLE|SDL_ASYNCBLIT|SDL_HWACCEL);
1419 fprintf(stderr, "SDL: could not set video mode - exiting\n");
1422 SDL_WM_SetCaption("FFplay", "FFplay");
1424 /* save the screen resolution... SDL should allow full screen
1425 by resizing the window */
1428 dpy = XOpenDisplay(NULL);
1430 fs_screen_width = DisplayWidth(dpy, DefaultScreen(dpy));
1431 fs_screen_height = DisplayHeight(dpy, DefaultScreen(dpy));
1438 SDL_EventState(SDL_ACTIVEEVENT, SDL_IGNORE);
1439 SDL_EventState(SDL_MOUSEMOTION, SDL_IGNORE);
1440 SDL_EventState(SDL_SYSWMEVENT, SDL_IGNORE);
1441 SDL_EventState(SDL_USEREVENT, SDL_IGNORE);
1443 cur_stream = stream_open(input_filename);