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)
60 #define MAX_SUBTITLEQ_SIZE (5 * 16 * 1024)
62 /* SDL audio buffer size, in samples. Should be small to have precise
63 A/V sync as SDL does not have hardware buffer fullness info. */
64 #define SDL_AUDIO_BUFFER_SIZE 1024
66 /* no AV sync correction is done if below the AV sync threshold */
67 #define AV_SYNC_THRESHOLD 0.01
68 /* no AV correction is done if too big error */
69 #define AV_NOSYNC_THRESHOLD 10.0
71 /* maximum audio speed change to get correct sync */
72 #define SAMPLE_CORRECTION_PERCENT_MAX 10
74 /* we use about AUDIO_DIFF_AVG_NB A-V differences to make the average */
75 #define AUDIO_DIFF_AVG_NB 20
77 /* NOTE: the size must be big enough to compensate the hardware audio buffersize size */
78 #define SAMPLE_ARRAY_SIZE (2*65536)
80 typedef struct PacketQueue {
81 AVPacketList *first_pkt, *last_pkt;
89 #define VIDEO_PICTURE_QUEUE_SIZE 1
90 #define SUBPICTURE_QUEUE_SIZE 4
92 typedef struct VideoPicture {
93 double pts; /* presentation time stamp for this picture */
95 int width, height; /* source height & width */
99 typedef struct SubPicture {
100 double pts; /* presentation time stamp for this picture */
105 AV_SYNC_AUDIO_MASTER, /* default choice */
106 AV_SYNC_VIDEO_MASTER,
107 AV_SYNC_EXTERNAL_CLOCK, /* synchronize to an external clock */
110 typedef struct VideoState {
111 SDL_Thread *parse_tid;
112 SDL_Thread *video_tid;
113 AVInputFormat *iformat;
122 int dtg_active_format;
127 double external_clock; /* external clock base */
128 int64_t external_clock_time;
131 double audio_diff_cum; /* used for AV difference average computation */
132 double audio_diff_avg_coef;
133 double audio_diff_threshold;
134 int audio_diff_avg_count;
137 int audio_hw_buf_size;
138 /* samples output by the codec. we reserve more space for avsync
140 uint8_t audio_buf[(AVCODEC_MAX_AUDIO_FRAME_SIZE * 3) / 2];
141 unsigned int audio_buf_size; /* in bytes */
142 int audio_buf_index; /* in bytes */
144 uint8_t *audio_pkt_data;
147 int show_audio; /* if true, display audio samples */
148 int16_t sample_array[SAMPLE_ARRAY_SIZE];
149 int sample_array_index;
152 SDL_Thread *subtitle_tid;
154 int subtitle_stream_changed;
155 AVStream *subtitle_st;
156 PacketQueue subtitleq;
157 SubPicture subpq[SUBPICTURE_QUEUE_SIZE];
158 int subpq_size, subpq_rindex, subpq_windex;
159 SDL_mutex *subpq_mutex;
160 SDL_cond *subpq_cond;
163 double frame_last_pts;
164 double frame_last_delay;
169 double video_last_P_pts; /* pts of the last P picture (needed if B
170 frames are present) */
171 double video_current_pts; /* current displayed pts (different from
172 video_clock if frame fifos are used) */
173 int64_t video_current_pts_time; /* time at which we updated
174 video_current_pts - used to
175 have running video pts */
176 VideoPicture pictq[VIDEO_PICTURE_QUEUE_SIZE];
177 int pictq_size, pictq_rindex, pictq_windex;
178 SDL_mutex *pictq_mutex;
179 SDL_cond *pictq_cond;
181 SDL_mutex *video_decoder_mutex;
182 SDL_mutex *audio_decoder_mutex;
183 SDL_mutex *subtitle_decoder_mutex;
185 // QETimer *video_timer;
187 int width, height, xleft, ytop;
190 void show_help(void);
191 static int audio_write_get_buf_size(VideoState *is);
193 /* options specified by the user */
194 static AVInputFormat *file_iformat;
195 static AVImageFormat *image_format;
196 static const char *input_filename;
197 static int fs_screen_width;
198 static int fs_screen_height;
199 static int screen_width = 640;
200 static int screen_height = 480;
201 static int audio_disable;
202 static int video_disable;
203 static int display_disable;
204 static int show_status;
205 static int av_sync_type = AV_SYNC_AUDIO_MASTER;
206 static int64_t start_time = AV_NOPTS_VALUE;
207 static int debug = 0;
208 static int debug_mv = 0;
210 static int thread_count = 1;
211 static int workaround_bugs = 1;
213 static int lowres = 0;
214 static int idct = FF_IDCT_AUTO;
215 static enum AVDiscard skip_frame= AVDISCARD_DEFAULT;
216 static enum AVDiscard skip_idct= AVDISCARD_DEFAULT;
217 static enum AVDiscard skip_loop_filter= AVDISCARD_DEFAULT;
218 static int error_resilience = FF_ER_CAREFULL;
219 static int error_concealment = 3;
221 /* current context */
222 static int is_full_screen;
223 static VideoState *cur_stream;
224 static int64_t audio_callback_time;
226 #define FF_ALLOC_EVENT (SDL_USEREVENT)
227 #define FF_REFRESH_EVENT (SDL_USEREVENT + 1)
228 #define FF_QUIT_EVENT (SDL_USEREVENT + 2)
232 /* packet queue handling */
233 static void packet_queue_init(PacketQueue *q)
235 memset(q, 0, sizeof(PacketQueue));
236 q->mutex = SDL_CreateMutex();
237 q->cond = SDL_CreateCond();
240 static void packet_queue_flush(PacketQueue *q)
242 AVPacketList *pkt, *pkt1;
244 SDL_LockMutex(q->mutex);
245 for(pkt = q->first_pkt; pkt != NULL; pkt = pkt1) {
247 av_free_packet(&pkt->pkt);
254 SDL_UnlockMutex(q->mutex);
257 static void packet_queue_end(PacketQueue *q)
259 packet_queue_flush(q);
260 SDL_DestroyMutex(q->mutex);
261 SDL_DestroyCond(q->cond);
264 static int packet_queue_put(PacketQueue *q, AVPacket *pkt)
268 /* duplicate the packet */
269 if (av_dup_packet(pkt) < 0)
272 pkt1 = av_malloc(sizeof(AVPacketList));
279 SDL_LockMutex(q->mutex);
285 q->last_pkt->next = pkt1;
288 q->size += pkt1->pkt.size;
289 /* XXX: should duplicate packet data in DV case */
290 SDL_CondSignal(q->cond);
292 SDL_UnlockMutex(q->mutex);
296 static void packet_queue_abort(PacketQueue *q)
298 SDL_LockMutex(q->mutex);
300 q->abort_request = 1;
302 SDL_CondSignal(q->cond);
304 SDL_UnlockMutex(q->mutex);
307 /* return < 0 if aborted, 0 if no packet and > 0 if packet. */
308 static int packet_queue_get(PacketQueue *q, AVPacket *pkt, int block)
313 SDL_LockMutex(q->mutex);
316 if (q->abort_request) {
323 q->first_pkt = pkt1->next;
327 q->size -= pkt1->pkt.size;
336 SDL_CondWait(q->cond, q->mutex);
339 SDL_UnlockMutex(q->mutex);
343 static inline void fill_rectangle(SDL_Surface *screen,
344 int x, int y, int w, int h, int color)
351 SDL_FillRect(screen, &rect, color);
355 /* draw only the border of a rectangle */
356 void fill_border(VideoState *s, int x, int y, int w, int h, int color)
360 /* fill the background */
364 w2 = s->width - (x + w);
370 h2 = s->height - (y + h);
373 fill_rectangle(screen,
377 fill_rectangle(screen,
378 s->xleft + s->width - w2, s->ytop,
381 fill_rectangle(screen,
382 s->xleft + w1, s->ytop,
383 s->width - w1 - w2, h1,
385 fill_rectangle(screen,
386 s->xleft + w1, s->ytop + s->height - h2,
387 s->width - w1 - w2, h2,
395 #define ONE_HALF (1 << (SCALEBITS - 1))
396 #define FIX(x) ((int) ((x) * (1<<SCALEBITS) + 0.5))
398 #define RGB_TO_Y_CCIR(r, g, b) \
399 ((FIX(0.29900*219.0/255.0) * (r) + FIX(0.58700*219.0/255.0) * (g) + \
400 FIX(0.11400*219.0/255.0) * (b) + (ONE_HALF + (16 << SCALEBITS))) >> SCALEBITS)
402 #define RGB_TO_U_CCIR(r1, g1, b1, shift)\
403 (((- FIX(0.16874*224.0/255.0) * r1 - FIX(0.33126*224.0/255.0) * g1 + \
404 FIX(0.50000*224.0/255.0) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
406 #define RGB_TO_V_CCIR(r1, g1, b1, shift)\
407 (((FIX(0.50000*224.0/255.0) * r1 - FIX(0.41869*224.0/255.0) * g1 - \
408 FIX(0.08131*224.0/255.0) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
410 #define ALPHA_BLEND(a, oldp, newp, s)\
411 ((((oldp << s) * (255 - (a))) + (newp * (a))) / (255 << s))
413 #define RGBA_IN(r, g, b, a, s)\
415 unsigned int v = ((const uint32_t *)(s))[0];\
416 a = (v >> 24) & 0xff;\
417 r = (v >> 16) & 0xff;\
418 g = (v >> 8) & 0xff;\
422 #define YUVA_IN(y, u, v, a, s, pal)\
424 unsigned int val = ((const uint32_t *)(pal))[*(const uint8_t*)s];\
425 a = (val >> 24) & 0xff;\
426 y = (val >> 16) & 0xff;\
427 u = (val >> 8) & 0xff;\
431 #define YUVA_OUT(d, y, u, v, a)\
433 ((uint32_t *)(d))[0] = (a << 24) | (y << 16) | (u << 8) | v;\
439 static void blend_subrect(AVPicture *dst, const AVSubtitleRect *rect)
441 int wrap, wrap3, width2, skip2;
442 int y, u, v, a, u1, v1, a1, w, h;
443 uint8_t *lum, *cb, *cr;
447 lum = dst->data[0] + rect->y * dst->linesize[0];
448 cb = dst->data[1] + (rect->y >> 1) * dst->linesize[1];
449 cr = dst->data[2] + (rect->y >> 1) * dst->linesize[2];
451 width2 = (rect->w + 1) >> 1;
452 skip2 = rect->x >> 1;
453 wrap = dst->linesize[0];
454 wrap3 = rect->linesize;
456 pal = rect->rgba_palette; /* Now in YCrCb! */
464 YUVA_IN(y, u, v, a, p, pal);
465 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
466 cb[0] = ALPHA_BLEND(a >> 2, cb[0], u, 0);
467 cr[0] = ALPHA_BLEND(a >> 2, cr[0], v, 0);
473 for(w = rect->w - (rect->x & 1); w >= 2; w -= 2) {
474 YUVA_IN(y, u, v, a, p, pal);
478 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
480 YUVA_IN(y, u, v, a, p + BPP, pal);
484 lum[1] = ALPHA_BLEND(a, lum[1], y, 0);
485 cb[0] = ALPHA_BLEND(a1 >> 2, cb[0], u1, 1);
486 cr[0] = ALPHA_BLEND(a1 >> 2, cr[0], v1, 1);
493 YUVA_IN(y, u, v, a, p, pal);
494 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
495 cb[0] = ALPHA_BLEND(a >> 2, cb[0], u, 0);
496 cr[0] = ALPHA_BLEND(a >> 2, cr[0], v, 0);
498 p += wrap3 + (wrap3 - rect->w * BPP);
499 lum += wrap + (wrap - rect->w - rect->x);
500 cb += dst->linesize[1] - width2 - skip2;
501 cr += dst->linesize[2] - width2 - skip2;
503 for(h = rect->h - (rect->y & 1); h >= 2; h -= 2) {
509 YUVA_IN(y, u, v, a, p, pal);
513 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
516 YUVA_IN(y, u, v, a, p, pal);
520 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
521 cb[0] = ALPHA_BLEND(a1 >> 2, cb[0], u1, 1);
522 cr[0] = ALPHA_BLEND(a1 >> 2, cr[0], v1, 1);
528 for(w = rect->w - (rect->x & 1); w >= 2; w -= 2) {
529 YUVA_IN(y, u, v, a, p, pal);
533 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
535 YUVA_IN(y, u, v, a, p, pal);
539 lum[1] = ALPHA_BLEND(a, lum[1], y, 0);
543 YUVA_IN(y, u, v, a, p, pal);
547 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
549 YUVA_IN(y, u, v, a, p, pal);
553 lum[1] = ALPHA_BLEND(a, lum[1], y, 0);
555 cb[0] = ALPHA_BLEND(a1 >> 2, cb[0], u1, 2);
556 cr[0] = ALPHA_BLEND(a1 >> 2, cr[0], v1, 2);
560 p += -wrap3 + 2 * BPP;
564 YUVA_IN(y, u, v, a, p, pal);
568 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
571 YUVA_IN(y, u, v, a, p, pal);
575 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
576 cb[0] = ALPHA_BLEND(a1 >> 2, cb[0], u1, 1);
577 cr[0] = ALPHA_BLEND(a1 >> 2, cr[0], v1, 1);
583 p += wrap3 + (wrap3 - rect->w * BPP);
584 lum += wrap + (wrap - rect->w - rect->x);
585 cb += dst->linesize[1] - width2 - skip2;
586 cr += dst->linesize[2] - width2 - skip2;
588 /* handle odd height */
595 YUVA_IN(y, u, v, a, p, pal);
596 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
597 cb[0] = ALPHA_BLEND(a >> 2, cb[0], u, 0);
598 cr[0] = ALPHA_BLEND(a >> 2, cr[0], v, 0);
604 for(w = rect->w - (rect->x & 1); w >= 2; w -= 2) {
605 YUVA_IN(y, u, v, a, p, pal);
609 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
611 YUVA_IN(y, u, v, a, p + BPP, pal);
615 lum[1] = ALPHA_BLEND(a, lum[1], y, 0);
616 cb[0] = ALPHA_BLEND(a1 >> 2, cb[0], u, 1);
617 cr[0] = ALPHA_BLEND(a1 >> 2, cr[0], v, 1);
624 YUVA_IN(y, u, v, a, p, pal);
625 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
626 cb[0] = ALPHA_BLEND(a >> 2, cb[0], u, 0);
627 cr[0] = ALPHA_BLEND(a >> 2, cr[0], v, 0);
632 static void free_subpicture(SubPicture *sp)
636 for (i = 0; i < sp->sub.num_rects; i++)
638 av_free(sp->sub.rects[i].bitmap);
639 av_free(sp->sub.rects[i].rgba_palette);
642 av_free(sp->sub.rects);
644 memset(&sp->sub, 0, sizeof(AVSubtitle));
647 static void video_image_display(VideoState *is)
653 int width, height, x, y;
657 vp = &is->pictq[is->pictq_rindex];
659 /* XXX: use variable in the frame */
660 if (is->video_st->codec->sample_aspect_ratio.num == 0)
663 aspect_ratio = av_q2d(is->video_st->codec->sample_aspect_ratio)
664 * is->video_st->codec->width / is->video_st->codec->height;;
665 if (aspect_ratio <= 0.0)
666 aspect_ratio = (float)is->video_st->codec->width /
667 (float)is->video_st->codec->height;
668 /* if an active format is indicated, then it overrides the
671 if (is->video_st->codec->dtg_active_format != is->dtg_active_format) {
672 is->dtg_active_format = is->video_st->codec->dtg_active_format;
673 printf("dtg_active_format=%d\n", is->dtg_active_format);
677 switch(is->video_st->codec->dtg_active_format) {
678 case FF_DTG_AFD_SAME:
683 aspect_ratio = 4.0 / 3.0;
685 case FF_DTG_AFD_16_9:
686 aspect_ratio = 16.0 / 9.0;
688 case FF_DTG_AFD_14_9:
689 aspect_ratio = 14.0 / 9.0;
691 case FF_DTG_AFD_4_3_SP_14_9:
692 aspect_ratio = 14.0 / 9.0;
694 case FF_DTG_AFD_16_9_SP_14_9:
695 aspect_ratio = 14.0 / 9.0;
697 case FF_DTG_AFD_SP_4_3:
698 aspect_ratio = 4.0 / 3.0;
705 if (is->subpq_size > 0)
707 sp = &is->subpq[is->subpq_rindex];
709 if (vp->pts >= sp->pts + ((float) sp->sub.start_display_time / 1000))
711 SDL_LockYUVOverlay (vp->bmp);
713 pict.data[0] = vp->bmp->pixels[0];
714 pict.data[1] = vp->bmp->pixels[2];
715 pict.data[2] = vp->bmp->pixels[1];
717 pict.linesize[0] = vp->bmp->pitches[0];
718 pict.linesize[1] = vp->bmp->pitches[2];
719 pict.linesize[2] = vp->bmp->pitches[1];
721 for (i = 0; i < sp->sub.num_rects; i++)
722 blend_subrect(&pict, &sp->sub.rects[i]);
724 SDL_UnlockYUVOverlay (vp->bmp);
730 /* XXX: we suppose the screen has a 1.0 pixel ratio */
732 width = ((int)rint(height * aspect_ratio)) & -3;
733 if (width > is->width) {
735 height = ((int)rint(width / aspect_ratio)) & -3;
737 x = (is->width - width) / 2;
738 y = (is->height - height) / 2;
739 if (!is->no_background) {
740 /* fill the background */
741 // fill_border(is, x, y, width, height, QERGB(0x00, 0x00, 0x00));
743 is->no_background = 0;
745 rect.x = is->xleft + x;
746 rect.y = is->xleft + y;
749 SDL_DisplayYUVOverlay(vp->bmp, &rect);
752 fill_rectangle(screen,
753 is->xleft, is->ytop, is->width, is->height,
754 QERGB(0x00, 0x00, 0x00));
759 static inline int compute_mod(int a, int b)
768 static void video_audio_display(VideoState *s)
770 int i, i_start, x, y1, y, ys, delay, n, nb_display_channels;
771 int ch, channels, h, h2, bgcolor, fgcolor;
774 /* compute display index : center on currently output samples */
775 channels = s->audio_st->codec->channels;
776 nb_display_channels = channels;
779 delay = audio_write_get_buf_size(s);
782 /* to be more precise, we take into account the time spent since
783 the last buffer computation */
784 if (audio_callback_time) {
785 time_diff = av_gettime() - audio_callback_time;
786 delay += (time_diff * s->audio_st->codec->sample_rate) / 1000000;
789 delay -= s->width / 2;
790 if (delay < s->width)
792 i_start = compute_mod(s->sample_array_index - delay * channels, SAMPLE_ARRAY_SIZE);
793 s->last_i_start = i_start;
795 i_start = s->last_i_start;
798 bgcolor = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00);
799 fill_rectangle(screen,
800 s->xleft, s->ytop, s->width, s->height,
803 fgcolor = SDL_MapRGB(screen->format, 0xff, 0xff, 0xff);
805 /* total height for one channel */
806 h = s->height / nb_display_channels;
807 /* graph height / 2 */
809 for(ch = 0;ch < nb_display_channels; ch++) {
811 y1 = s->ytop + ch * h + (h / 2); /* position of center line */
812 for(x = 0; x < s->width; x++) {
813 y = (s->sample_array[i] * h2) >> 15;
820 fill_rectangle(screen,
821 s->xleft + x, ys, 1, y,
824 if (i >= SAMPLE_ARRAY_SIZE)
825 i -= SAMPLE_ARRAY_SIZE;
829 fgcolor = SDL_MapRGB(screen->format, 0x00, 0x00, 0xff);
831 for(ch = 1;ch < nb_display_channels; ch++) {
832 y = s->ytop + ch * h;
833 fill_rectangle(screen,
834 s->xleft, y, s->width, 1,
837 SDL_UpdateRect(screen, s->xleft, s->ytop, s->width, s->height);
840 /* display the current picture, if any */
841 static void video_display(VideoState *is)
843 if (is->audio_st && is->show_audio)
844 video_audio_display(is);
845 else if (is->video_st)
846 video_image_display(is);
849 static Uint32 sdl_refresh_timer_cb(Uint32 interval, void *opaque)
852 event.type = FF_REFRESH_EVENT;
853 event.user.data1 = opaque;
854 SDL_PushEvent(&event);
855 return 0; /* 0 means stop timer */
858 /* schedule a video refresh in 'delay' ms */
859 static void schedule_refresh(VideoState *is, int delay)
861 SDL_AddTimer(delay, sdl_refresh_timer_cb, is);
864 /* get the current audio clock value */
865 static double get_audio_clock(VideoState *is)
868 int hw_buf_size, bytes_per_sec;
869 pts = is->audio_clock;
870 hw_buf_size = audio_write_get_buf_size(is);
873 bytes_per_sec = is->audio_st->codec->sample_rate *
874 2 * is->audio_st->codec->channels;
877 pts -= (double)hw_buf_size / bytes_per_sec;
881 /* get the current video clock value */
882 static double get_video_clock(VideoState *is)
888 delta = (av_gettime() - is->video_current_pts_time) / 1000000.0;
890 return is->video_current_pts + delta;
893 /* get the current external clock value */
894 static double get_external_clock(VideoState *is)
898 return is->external_clock + ((ti - is->external_clock_time) * 1e-6);
901 /* get the current master clock value */
902 static double get_master_clock(VideoState *is)
906 if (is->av_sync_type == AV_SYNC_VIDEO_MASTER) {
908 val = get_video_clock(is);
910 val = get_audio_clock(is);
911 } else if (is->av_sync_type == AV_SYNC_AUDIO_MASTER) {
913 val = get_audio_clock(is);
915 val = get_video_clock(is);
917 val = get_external_clock(is);
922 /* seek in the stream */
923 static void stream_seek(VideoState *is, int64_t pos, int rel)
927 is->seek_flags = rel < 0 ? AVSEEK_FLAG_BACKWARD : 0;
932 /* pause or resume the video */
933 static void stream_pause(VideoState *is)
935 is->paused = !is->paused;
937 is->video_current_pts = get_video_clock(is);
941 /* called to display each frame */
942 static void video_refresh_timer(void *opaque)
944 VideoState *is = opaque;
946 double actual_delay, delay, sync_threshold, ref_clock, diff;
948 SubPicture *sp, *sp2;
951 if (is->pictq_size == 0) {
952 /* if no picture, need to wait */
953 schedule_refresh(is, 1);
955 /* dequeue the picture */
956 vp = &is->pictq[is->pictq_rindex];
958 /* update current video pts */
959 is->video_current_pts = vp->pts;
960 is->video_current_pts_time = av_gettime();
962 /* compute nominal delay */
963 delay = vp->pts - is->frame_last_pts;
964 if (delay <= 0 || delay >= 1.0) {
965 /* if incorrect delay, use previous one */
966 delay = is->frame_last_delay;
968 is->frame_last_delay = delay;
969 is->frame_last_pts = vp->pts;
971 /* update delay to follow master synchronisation source */
972 if (((is->av_sync_type == AV_SYNC_AUDIO_MASTER && is->audio_st) ||
973 is->av_sync_type == AV_SYNC_EXTERNAL_CLOCK)) {
974 /* if video is slave, we try to correct big delays by
975 duplicating or deleting a frame */
976 ref_clock = get_master_clock(is);
977 diff = vp->pts - ref_clock;
979 /* skip or repeat frame. We take into account the
980 delay to compute the threshold. I still don't know
981 if it is the best guess */
982 sync_threshold = AV_SYNC_THRESHOLD;
983 if (delay > sync_threshold)
984 sync_threshold = delay;
985 if (fabs(diff) < AV_NOSYNC_THRESHOLD) {
986 if (diff <= -sync_threshold)
988 else if (diff >= sync_threshold)
993 is->frame_timer += delay;
994 /* compute the REAL delay (we need to do that to avoid
996 actual_delay = is->frame_timer - (av_gettime() / 1000000.0);
997 if (actual_delay < 0.010) {
998 /* XXX: should skip picture */
999 actual_delay = 0.010;
1001 /* launch timer for next picture */
1002 schedule_refresh(is, (int)(actual_delay * 1000 + 0.5));
1004 #if defined(DEBUG_SYNC)
1005 printf("video: delay=%0.3f actual_delay=%0.3f pts=%0.3f A-V=%f\n",
1006 delay, actual_delay, vp->pts, -diff);
1009 if(is->subtitle_st) {
1010 if (is->subtitle_stream_changed) {
1011 SDL_LockMutex(is->subpq_mutex);
1013 while (is->subpq_size) {
1014 free_subpicture(&is->subpq[is->subpq_rindex]);
1016 /* update queue size and signal for next picture */
1017 if (++is->subpq_rindex == SUBPICTURE_QUEUE_SIZE)
1018 is->subpq_rindex = 0;
1022 is->subtitle_stream_changed = 0;
1024 SDL_CondSignal(is->subpq_cond);
1025 SDL_UnlockMutex(is->subpq_mutex);
1027 if (is->subpq_size > 0) {
1028 sp = &is->subpq[is->subpq_rindex];
1030 if (is->subpq_size > 1)
1031 sp2 = &is->subpq[(is->subpq_rindex + 1) % SUBPICTURE_QUEUE_SIZE];
1035 if ((is->video_current_pts > (sp->pts + ((float) sp->sub.end_display_time / 1000)))
1036 || (sp2 && is->video_current_pts > (sp2->pts + ((float) sp2->sub.start_display_time / 1000))))
1038 free_subpicture(sp);
1040 /* update queue size and signal for next picture */
1041 if (++is->subpq_rindex == SUBPICTURE_QUEUE_SIZE)
1042 is->subpq_rindex = 0;
1044 SDL_LockMutex(is->subpq_mutex);
1046 SDL_CondSignal(is->subpq_cond);
1047 SDL_UnlockMutex(is->subpq_mutex);
1053 /* display picture */
1056 /* update queue size and signal for next picture */
1057 if (++is->pictq_rindex == VIDEO_PICTURE_QUEUE_SIZE)
1058 is->pictq_rindex = 0;
1060 SDL_LockMutex(is->pictq_mutex);
1062 SDL_CondSignal(is->pictq_cond);
1063 SDL_UnlockMutex(is->pictq_mutex);
1065 } else if (is->audio_st) {
1066 /* draw the next audio frame */
1068 schedule_refresh(is, 40);
1070 /* if only audio stream, then display the audio bars (better
1071 than nothing, just to test the implementation */
1073 /* display picture */
1076 schedule_refresh(is, 100);
1079 static int64_t last_time;
1081 int aqsize, vqsize, sqsize;
1084 cur_time = av_gettime();
1085 if (!last_time || (cur_time - last_time) >= 500 * 1000) {
1090 aqsize = is->audioq.size;
1092 vqsize = is->videoq.size;
1093 if (is->subtitle_st)
1094 sqsize = is->subtitleq.size;
1096 if (is->audio_st && is->video_st)
1097 av_diff = get_audio_clock(is) - get_video_clock(is);
1098 printf("%7.2f A-V:%7.3f aq=%5dKB vq=%5dKB sq=%5dB \r",
1099 get_master_clock(is), av_diff, aqsize / 1024, vqsize / 1024, sqsize);
1101 last_time = cur_time;
1106 /* allocate a picture (needs to do that in main thread to avoid
1107 potential locking problems */
1108 static void alloc_picture(void *opaque)
1110 VideoState *is = opaque;
1113 vp = &is->pictq[is->pictq_windex];
1116 SDL_FreeYUVOverlay(vp->bmp);
1119 /* XXX: use generic function */
1120 /* XXX: disable overlay if no hardware acceleration or if RGB format */
1121 switch(is->video_st->codec->pix_fmt) {
1122 case PIX_FMT_YUV420P:
1123 case PIX_FMT_YUV422P:
1124 case PIX_FMT_YUV444P:
1125 case PIX_FMT_YUV422:
1126 case PIX_FMT_YUV410P:
1127 case PIX_FMT_YUV411P:
1135 vp->bmp = SDL_CreateYUVOverlay(is->video_st->codec->width,
1136 is->video_st->codec->height,
1139 vp->width = is->video_st->codec->width;
1140 vp->height = is->video_st->codec->height;
1142 SDL_LockMutex(is->pictq_mutex);
1144 SDL_CondSignal(is->pictq_cond);
1145 SDL_UnlockMutex(is->pictq_mutex);
1148 static int queue_picture(VideoState *is, AVFrame *src_frame, double pts)
1154 /* wait until we have space to put a new picture */
1155 SDL_LockMutex(is->pictq_mutex);
1156 while (is->pictq_size >= VIDEO_PICTURE_QUEUE_SIZE &&
1157 !is->videoq.abort_request) {
1158 SDL_CondWait(is->pictq_cond, is->pictq_mutex);
1160 SDL_UnlockMutex(is->pictq_mutex);
1162 if (is->videoq.abort_request)
1165 vp = &is->pictq[is->pictq_windex];
1167 /* alloc or resize hardware picture buffer */
1169 vp->width != is->video_st->codec->width ||
1170 vp->height != is->video_st->codec->height) {
1175 /* the allocation must be done in the main thread to avoid
1177 event.type = FF_ALLOC_EVENT;
1178 event.user.data1 = is;
1179 SDL_PushEvent(&event);
1181 /* wait until the picture is allocated */
1182 SDL_LockMutex(is->pictq_mutex);
1183 while (!vp->allocated && !is->videoq.abort_request) {
1184 SDL_CondWait(is->pictq_cond, is->pictq_mutex);
1186 SDL_UnlockMutex(is->pictq_mutex);
1188 if (is->videoq.abort_request)
1192 /* if the frame is not skipped, then display it */
1194 /* get a pointer on the bitmap */
1195 SDL_LockYUVOverlay (vp->bmp);
1197 dst_pix_fmt = PIX_FMT_YUV420P;
1198 pict.data[0] = vp->bmp->pixels[0];
1199 pict.data[1] = vp->bmp->pixels[2];
1200 pict.data[2] = vp->bmp->pixels[1];
1202 pict.linesize[0] = vp->bmp->pitches[0];
1203 pict.linesize[1] = vp->bmp->pitches[2];
1204 pict.linesize[2] = vp->bmp->pitches[1];
1205 img_convert(&pict, dst_pix_fmt,
1206 (AVPicture *)src_frame, is->video_st->codec->pix_fmt,
1207 is->video_st->codec->width, is->video_st->codec->height);
1208 /* update the bitmap content */
1209 SDL_UnlockYUVOverlay(vp->bmp);
1213 /* now we can update the picture count */
1214 if (++is->pictq_windex == VIDEO_PICTURE_QUEUE_SIZE)
1215 is->pictq_windex = 0;
1216 SDL_LockMutex(is->pictq_mutex);
1218 SDL_UnlockMutex(is->pictq_mutex);
1223 /* compute the exact PTS for the picture if it is omitted in the stream */
1224 static int output_picture2(VideoState *is, AVFrame *src_frame, double pts1)
1226 double frame_delay, pts;
1231 /* update video clock with pts, if present */
1232 is->video_clock = pts;
1234 pts = is->video_clock;
1236 /* update video clock for next frame */
1237 frame_delay = av_q2d(is->video_st->codec->time_base);
1238 /* for MPEG2, the frame can be repeated, so we update the
1239 clock accordingly */
1240 if (src_frame->repeat_pict) {
1241 frame_delay += src_frame->repeat_pict * (frame_delay * 0.5);
1243 is->video_clock += frame_delay;
1245 #if defined(DEBUG_SYNC) && 0
1248 if (src_frame->pict_type == FF_B_TYPE)
1250 else if (src_frame->pict_type == FF_I_TYPE)
1254 printf("frame_type=%c clock=%0.3f pts=%0.3f\n",
1258 return queue_picture(is, src_frame, pts);
1261 static int video_thread(void *arg)
1263 VideoState *is = arg;
1264 AVPacket pkt1, *pkt = &pkt1;
1265 int len1, got_picture;
1266 AVFrame *frame= avcodec_alloc_frame();
1270 while (is->paused && !is->videoq.abort_request) {
1273 if (packet_queue_get(&is->videoq, pkt, 1) < 0)
1275 /* NOTE: ipts is the PTS of the _first_ picture beginning in
1276 this packet, if any */
1278 if (pkt->dts != AV_NOPTS_VALUE)
1279 pts = av_q2d(is->video_st->time_base)*pkt->dts;
1281 SDL_LockMutex(is->video_decoder_mutex);
1282 len1 = avcodec_decode_video(is->video_st->codec,
1283 frame, &got_picture,
1284 pkt->data, pkt->size);
1285 SDL_UnlockMutex(is->video_decoder_mutex);
1289 if (output_picture2(is, frame, pts) < 0)
1292 av_free_packet(pkt);
1295 stream_pause(cur_stream);
1302 static int subtitle_thread(void *arg)
1304 VideoState *is = arg;
1306 AVPacket pkt1, *pkt = &pkt1;
1307 int len1, got_subtitle;
1310 int r, g, b, y, u, v, a;
1313 while (is->paused && !is->subtitleq.abort_request) {
1316 if (packet_queue_get(&is->subtitleq, pkt, 1) < 0)
1319 SDL_LockMutex(is->subpq_mutex);
1320 while (is->subpq_size >= SUBPICTURE_QUEUE_SIZE &&
1321 !is->subtitleq.abort_request) {
1322 SDL_CondWait(is->subpq_cond, is->subpq_mutex);
1324 SDL_UnlockMutex(is->subpq_mutex);
1326 if (is->subtitleq.abort_request)
1329 sp = &is->subpq[is->subpq_windex];
1331 /* NOTE: ipts is the PTS of the _first_ picture beginning in
1332 this packet, if any */
1334 if (pkt->pts != AV_NOPTS_VALUE)
1335 pts = av_q2d(is->subtitle_st->time_base)*pkt->pts;
1337 SDL_LockMutex(is->subtitle_decoder_mutex);
1338 len1 = avcodec_decode_subtitle(is->subtitle_st->codec,
1339 &sp->sub, &got_subtitle,
1340 pkt->data, pkt->size);
1341 SDL_UnlockMutex(is->subtitle_decoder_mutex);
1344 if (got_subtitle && sp->sub.format == 0) {
1347 for (i = 0; i < sp->sub.num_rects; i++)
1349 for (j = 0; j < sp->sub.rects[i].nb_colors; j++)
1351 RGBA_IN(r, g, b, a, sp->sub.rects[i].rgba_palette + j);
1352 y = RGB_TO_Y_CCIR(r, g, b);
1353 u = RGB_TO_U_CCIR(r, g, b, 0);
1354 v = RGB_TO_V_CCIR(r, g, b, 0);
1355 YUVA_OUT(sp->sub.rects[i].rgba_palette + j, y, u, v, a);
1359 /* now we can update the picture count */
1360 if (++is->subpq_windex == SUBPICTURE_QUEUE_SIZE)
1361 is->subpq_windex = 0;
1362 SDL_LockMutex(is->subpq_mutex);
1364 SDL_UnlockMutex(is->subpq_mutex);
1366 av_free_packet(pkt);
1369 // stream_pause(cur_stream);
1375 /* copy samples for viewing in editor window */
1376 static void update_sample_display(VideoState *is, short *samples, int samples_size)
1378 int size, len, channels;
1380 channels = is->audio_st->codec->channels;
1382 size = samples_size / sizeof(short);
1384 len = SAMPLE_ARRAY_SIZE - is->sample_array_index;
1387 memcpy(is->sample_array + is->sample_array_index, samples, len * sizeof(short));
1389 is->sample_array_index += len;
1390 if (is->sample_array_index >= SAMPLE_ARRAY_SIZE)
1391 is->sample_array_index = 0;
1396 /* return the new audio buffer size (samples can be added or deleted
1397 to get better sync if video or external master clock) */
1398 static int synchronize_audio(VideoState *is, short *samples,
1399 int samples_size1, double pts)
1401 int n, samples_size;
1404 n = 2 * is->audio_st->codec->channels;
1405 samples_size = samples_size1;
1407 /* if not master, then we try to remove or add samples to correct the clock */
1408 if (((is->av_sync_type == AV_SYNC_VIDEO_MASTER && is->video_st) ||
1409 is->av_sync_type == AV_SYNC_EXTERNAL_CLOCK)) {
1410 double diff, avg_diff;
1411 int wanted_size, min_size, max_size, nb_samples;
1413 ref_clock = get_master_clock(is);
1414 diff = get_audio_clock(is) - ref_clock;
1416 if (diff < AV_NOSYNC_THRESHOLD) {
1417 is->audio_diff_cum = diff + is->audio_diff_avg_coef * is->audio_diff_cum;
1418 if (is->audio_diff_avg_count < AUDIO_DIFF_AVG_NB) {
1419 /* not enough measures to have a correct estimate */
1420 is->audio_diff_avg_count++;
1422 /* estimate the A-V difference */
1423 avg_diff = is->audio_diff_cum * (1.0 - is->audio_diff_avg_coef);
1425 if (fabs(avg_diff) >= is->audio_diff_threshold) {
1426 wanted_size = samples_size + ((int)(diff * is->audio_st->codec->sample_rate) * n);
1427 nb_samples = samples_size / n;
1429 min_size = ((nb_samples * (100 - SAMPLE_CORRECTION_PERCENT_MAX)) / 100) * n;
1430 max_size = ((nb_samples * (100 + SAMPLE_CORRECTION_PERCENT_MAX)) / 100) * n;
1431 if (wanted_size < min_size)
1432 wanted_size = min_size;
1433 else if (wanted_size > max_size)
1434 wanted_size = max_size;
1436 /* add or remove samples to correction the synchro */
1437 if (wanted_size < samples_size) {
1438 /* remove samples */
1439 samples_size = wanted_size;
1440 } else if (wanted_size > samples_size) {
1441 uint8_t *samples_end, *q;
1445 nb = (samples_size - wanted_size);
1446 samples_end = (uint8_t *)samples + samples_size - n;
1447 q = samples_end + n;
1449 memcpy(q, samples_end, n);
1453 samples_size = wanted_size;
1457 printf("diff=%f adiff=%f sample_diff=%d apts=%0.3f vpts=%0.3f %f\n",
1458 diff, avg_diff, samples_size - samples_size1,
1459 is->audio_clock, is->video_clock, is->audio_diff_threshold);
1463 /* too big difference : may be initial PTS errors, so
1465 is->audio_diff_avg_count = 0;
1466 is->audio_diff_cum = 0;
1470 return samples_size;
1473 /* decode one audio frame and returns its uncompressed size */
1474 static int audio_decode_frame(VideoState *is, uint8_t *audio_buf, double *pts_ptr)
1476 AVPacket *pkt = &is->audio_pkt;
1477 int n, len1, data_size;
1481 /* NOTE: the audio packet can contain several frames */
1482 while (is->audio_pkt_size > 0) {
1483 SDL_LockMutex(is->audio_decoder_mutex);
1484 len1 = avcodec_decode_audio(is->audio_st->codec,
1485 (int16_t *)audio_buf, &data_size,
1486 is->audio_pkt_data, is->audio_pkt_size);
1487 SDL_UnlockMutex(is->audio_decoder_mutex);
1489 /* if error, we skip the frame */
1490 is->audio_pkt_size = 0;
1494 is->audio_pkt_data += len1;
1495 is->audio_pkt_size -= len1;
1498 /* if no pts, then compute it */
1499 pts = is->audio_clock;
1501 n = 2 * is->audio_st->codec->channels;
1502 is->audio_clock += (double)data_size /
1503 (double)(n * is->audio_st->codec->sample_rate);
1504 #if defined(DEBUG_SYNC)
1506 static double last_clock;
1507 printf("audio: delay=%0.3f clock=%0.3f pts=%0.3f\n",
1508 is->audio_clock - last_clock,
1509 is->audio_clock, pts);
1510 last_clock = is->audio_clock;
1516 /* free the current packet */
1518 av_free_packet(pkt);
1520 if (is->paused || is->audioq.abort_request) {
1524 /* read next packet */
1525 if (packet_queue_get(&is->audioq, pkt, 1) < 0)
1527 is->audio_pkt_data = pkt->data;
1528 is->audio_pkt_size = pkt->size;
1530 /* if update the audio clock with the pts */
1531 if (pkt->pts != AV_NOPTS_VALUE) {
1532 is->audio_clock = av_q2d(is->audio_st->time_base)*pkt->pts;
1537 /* get the current audio output buffer size, in samples. With SDL, we
1538 cannot have a precise information */
1539 static int audio_write_get_buf_size(VideoState *is)
1541 return is->audio_hw_buf_size - is->audio_buf_index;
1545 /* prepare a new audio buffer */
1546 void sdl_audio_callback(void *opaque, Uint8 *stream, int len)
1548 VideoState *is = opaque;
1549 int audio_size, len1;
1552 audio_callback_time = av_gettime();
1555 if (is->audio_buf_index >= is->audio_buf_size) {
1556 audio_size = audio_decode_frame(is, is->audio_buf, &pts);
1557 if (audio_size < 0) {
1558 /* if error, just output silence */
1559 is->audio_buf_size = 1024;
1560 memset(is->audio_buf, 0, is->audio_buf_size);
1563 update_sample_display(is, (int16_t *)is->audio_buf, audio_size);
1564 audio_size = synchronize_audio(is, (int16_t *)is->audio_buf, audio_size,
1566 is->audio_buf_size = audio_size;
1568 is->audio_buf_index = 0;
1570 len1 = is->audio_buf_size - is->audio_buf_index;
1573 memcpy(stream, (uint8_t *)is->audio_buf + is->audio_buf_index, len1);
1576 is->audio_buf_index += len1;
1581 /* open a given stream. Return 0 if OK */
1582 static int stream_component_open(VideoState *is, int stream_index)
1584 AVFormatContext *ic = is->ic;
1585 AVCodecContext *enc;
1587 SDL_AudioSpec wanted_spec, spec;
1589 if (stream_index < 0 || stream_index >= ic->nb_streams)
1591 enc = ic->streams[stream_index]->codec;
1593 /* prepare audio output */
1594 if (enc->codec_type == CODEC_TYPE_AUDIO) {
1595 wanted_spec.freq = enc->sample_rate;
1596 wanted_spec.format = AUDIO_S16SYS;
1597 /* hack for AC3. XXX: suppress that */
1598 if (enc->channels > 2)
1600 wanted_spec.channels = enc->channels;
1601 wanted_spec.silence = 0;
1602 wanted_spec.samples = SDL_AUDIO_BUFFER_SIZE;
1603 wanted_spec.callback = sdl_audio_callback;
1604 wanted_spec.userdata = is;
1605 if (SDL_OpenAudio(&wanted_spec, &spec) < 0) {
1606 fprintf(stderr, "SDL_OpenAudio: %s\n", SDL_GetError());
1609 is->audio_hw_buf_size = spec.size;
1612 codec = avcodec_find_decoder(enc->codec_id);
1613 enc->debug_mv = debug_mv;
1616 av_log_set_level(AV_LOG_DEBUG);
1617 enc->workaround_bugs = workaround_bugs;
1618 enc->lowres = lowres;
1619 if(lowres) enc->flags |= CODEC_FLAG_EMU_EDGE;
1620 enc->idct_algo= idct;
1621 if(fast) enc->flags2 |= CODEC_FLAG2_FAST;
1622 enc->skip_frame= skip_frame;
1623 enc->skip_idct= skip_idct;
1624 enc->skip_loop_filter= skip_loop_filter;
1625 enc->error_resilience= error_resilience;
1626 enc->error_concealment= error_concealment;
1628 avcodec_open(enc, codec) < 0)
1630 #if defined(HAVE_THREADS)
1632 avcodec_thread_init(enc, thread_count);
1634 enc->thread_count= thread_count;
1635 switch(enc->codec_type) {
1636 case CODEC_TYPE_AUDIO:
1637 is->audio_stream = stream_index;
1638 is->audio_st = ic->streams[stream_index];
1639 is->audio_buf_size = 0;
1640 is->audio_buf_index = 0;
1642 /* init averaging filter */
1643 is->audio_diff_avg_coef = exp(log(0.01) / AUDIO_DIFF_AVG_NB);
1644 is->audio_diff_avg_count = 0;
1645 /* since we do not have a precise anough audio fifo fullness,
1646 we correct audio sync only if larger than this threshold */
1647 is->audio_diff_threshold = 2.0 * SDL_AUDIO_BUFFER_SIZE / enc->sample_rate;
1649 memset(&is->audio_pkt, 0, sizeof(is->audio_pkt));
1650 packet_queue_init(&is->audioq);
1653 case CODEC_TYPE_VIDEO:
1654 is->video_stream = stream_index;
1655 is->video_st = ic->streams[stream_index];
1657 is->frame_last_delay = 40e-3;
1658 is->frame_timer = (double)av_gettime() / 1000000.0;
1659 is->video_current_pts_time = av_gettime();
1661 packet_queue_init(&is->videoq);
1662 is->video_tid = SDL_CreateThread(video_thread, is);
1664 case CODEC_TYPE_SUBTITLE:
1665 is->subtitle_stream = stream_index;
1666 is->subtitle_st = ic->streams[stream_index];
1667 packet_queue_init(&is->subtitleq);
1669 is->subtitle_tid = SDL_CreateThread(subtitle_thread, is);
1677 static void stream_component_close(VideoState *is, int stream_index)
1679 AVFormatContext *ic = is->ic;
1680 AVCodecContext *enc;
1682 if (stream_index < 0 || stream_index >= ic->nb_streams)
1684 enc = ic->streams[stream_index]->codec;
1686 switch(enc->codec_type) {
1687 case CODEC_TYPE_AUDIO:
1688 packet_queue_abort(&is->audioq);
1692 packet_queue_end(&is->audioq);
1694 case CODEC_TYPE_VIDEO:
1695 packet_queue_abort(&is->videoq);
1697 /* note: we also signal this mutex to make sure we deblock the
1698 video thread in all cases */
1699 SDL_LockMutex(is->pictq_mutex);
1700 SDL_CondSignal(is->pictq_cond);
1701 SDL_UnlockMutex(is->pictq_mutex);
1703 SDL_WaitThread(is->video_tid, NULL);
1705 packet_queue_end(&is->videoq);
1707 case CODEC_TYPE_SUBTITLE:
1708 packet_queue_abort(&is->subtitleq);
1710 /* note: we also signal this mutex to make sure we deblock the
1711 video thread in all cases */
1712 SDL_LockMutex(is->subpq_mutex);
1713 is->subtitle_stream_changed = 1;
1715 SDL_CondSignal(is->subpq_cond);
1716 SDL_UnlockMutex(is->subpq_mutex);
1718 SDL_WaitThread(is->subtitle_tid, NULL);
1720 packet_queue_end(&is->subtitleq);
1727 switch(enc->codec_type) {
1728 case CODEC_TYPE_AUDIO:
1729 is->audio_st = NULL;
1730 is->audio_stream = -1;
1732 case CODEC_TYPE_VIDEO:
1733 is->video_st = NULL;
1734 is->video_stream = -1;
1736 case CODEC_TYPE_SUBTITLE:
1737 is->subtitle_st = NULL;
1738 is->subtitle_stream = -1;
1745 void dump_stream_info(AVFormatContext *s)
1748 fprintf(stderr, "Track: %d\n", s->track);
1749 if (s->title[0] != '\0')
1750 fprintf(stderr, "Title: %s\n", s->title);
1751 if (s->author[0] != '\0')
1752 fprintf(stderr, "Author: %s\n", s->author);
1753 if (s->album[0] != '\0')
1754 fprintf(stderr, "Album: %s\n", s->album);
1756 fprintf(stderr, "Year: %d\n", s->year);
1757 if (s->genre[0] != '\0')
1758 fprintf(stderr, "Genre: %s\n", s->genre);
1761 /* since we have only one decoding thread, we can use a global
1762 variable instead of a thread local variable */
1763 static VideoState *global_video_state;
1765 static int decode_interrupt_cb(void)
1767 return (global_video_state && global_video_state->abort_request);
1770 /* this thread gets the stream from the disk or the network */
1771 static int decode_thread(void *arg)
1773 VideoState *is = arg;
1774 AVFormatContext *ic;
1775 int err, i, ret, video_index, audio_index, use_play;
1776 AVPacket pkt1, *pkt = &pkt1;
1777 AVFormatParameters params, *ap = ¶ms;
1781 is->video_stream = -1;
1782 is->audio_stream = -1;
1783 is->subtitle_stream = -1;
1785 global_video_state = is;
1786 url_set_interrupt_cb(decode_interrupt_cb);
1788 memset(ap, 0, sizeof(*ap));
1789 ap->image_format = image_format;
1790 ap->initial_pause = 1; /* we force a pause when starting an RTSP
1793 err = av_open_input_file(&ic, is->filename, is->iformat, 0, ap);
1795 print_error(is->filename, err);
1800 #ifdef CONFIG_NETWORK
1801 use_play = (ic->iformat == &rtsp_demux);
1806 err = av_find_stream_info(ic);
1808 fprintf(stderr, "%s: could not find codec parameters\n", is->filename);
1812 ic->pb.eof_reached= 0; //FIXME hack, ffplay maybe shouldnt use url_feof() to test for the end
1815 /* if seeking requested, we execute it */
1816 if (start_time != AV_NOPTS_VALUE) {
1819 timestamp = start_time;
1820 /* add the stream start time */
1821 if (ic->start_time != AV_NOPTS_VALUE)
1822 timestamp += ic->start_time;
1823 ret = av_seek_frame(ic, -1, timestamp, AVSEEK_FLAG_BACKWARD);
1825 fprintf(stderr, "%s: could not seek to position %0.3f\n",
1826 is->filename, (double)timestamp / AV_TIME_BASE);
1830 /* now we can begin to play (RTSP stream only) */
1834 err = av_find_stream_info(ic);
1836 fprintf(stderr, "%s: could not find codec parameters\n", is->filename);
1842 for(i = 0; i < ic->nb_streams; i++) {
1843 AVCodecContext *enc = ic->streams[i]->codec;
1844 switch(enc->codec_type) {
1845 case CODEC_TYPE_AUDIO:
1846 if (audio_index < 0 && !audio_disable)
1849 case CODEC_TYPE_VIDEO:
1850 if (video_index < 0 && !video_disable)
1858 dump_format(ic, 0, is->filename, 0);
1859 dump_stream_info(ic);
1862 /* open the streams */
1863 if (audio_index >= 0) {
1864 stream_component_open(is, audio_index);
1867 if (video_index >= 0) {
1868 stream_component_open(is, video_index);
1870 if (!display_disable)
1874 if (is->video_stream < 0 && is->audio_stream < 0) {
1875 fprintf(stderr, "%s: could not open codecs\n", is->filename);
1881 if (is->abort_request)
1883 #ifdef CONFIG_NETWORK
1884 if (is->paused != is->last_paused) {
1885 is->last_paused = is->paused;
1891 if (is->paused && ic->iformat == &rtsp_demux) {
1892 /* wait 10 ms to avoid trying to get another packet */
1899 /* XXX: must lock decoder threads */
1900 SDL_LockMutex(is->video_decoder_mutex);
1901 SDL_LockMutex(is->audio_decoder_mutex);
1902 SDL_LockMutex(is->subtitle_decoder_mutex);
1903 ret = av_seek_frame(is->ic, -1, is->seek_pos, is->seek_flags);
1905 fprintf(stderr, "%s: error while seeking\n", is->ic->filename);
1907 if (is->audio_stream >= 0) {
1908 packet_queue_flush(&is->audioq);
1910 if (is->subtitle_stream >= 0) {
1911 packet_queue_flush(&is->subtitleq);
1913 if (is->video_stream >= 0) {
1914 packet_queue_flush(&is->videoq);
1915 avcodec_flush_buffers(ic->streams[video_index]->codec);
1918 SDL_UnlockMutex(is->subtitle_decoder_mutex);
1919 SDL_UnlockMutex(is->audio_decoder_mutex);
1920 SDL_UnlockMutex(is->video_decoder_mutex);
1924 /* if the queue are full, no need to read more */
1925 if (is->audioq.size > MAX_AUDIOQ_SIZE ||
1926 is->videoq.size > MAX_VIDEOQ_SIZE ||
1927 is->subtitleq.size > MAX_SUBTITLEQ_SIZE ||
1928 url_feof(&ic->pb)) {
1933 ret = av_read_frame(ic, pkt);
1935 if (url_ferror(&ic->pb) == 0) {
1936 SDL_Delay(100); /* wait for user event */
1941 if (pkt->stream_index == is->audio_stream) {
1942 packet_queue_put(&is->audioq, pkt);
1943 } else if (pkt->stream_index == is->video_stream) {
1944 packet_queue_put(&is->videoq, pkt);
1945 } else if (pkt->stream_index == is->subtitle_stream) {
1946 packet_queue_put(&is->subtitleq, pkt);
1948 av_free_packet(pkt);
1951 /* wait until the end */
1952 while (!is->abort_request) {
1958 /* disable interrupting */
1959 global_video_state = NULL;
1961 /* close each stream */
1962 if (is->audio_stream >= 0)
1963 stream_component_close(is, is->audio_stream);
1964 if (is->video_stream >= 0)
1965 stream_component_close(is, is->video_stream);
1966 if (is->subtitle_stream >= 0)
1967 stream_component_close(is, is->subtitle_stream);
1969 av_close_input_file(is->ic);
1970 is->ic = NULL; /* safety */
1972 url_set_interrupt_cb(NULL);
1977 event.type = FF_QUIT_EVENT;
1978 event.user.data1 = is;
1979 SDL_PushEvent(&event);
1984 static VideoState *stream_open(const char *filename, AVInputFormat *iformat)
1988 is = av_mallocz(sizeof(VideoState));
1991 pstrcpy(is->filename, sizeof(is->filename), filename);
1992 is->iformat = iformat;
1994 is->width = screen->w;
1995 is->height = screen->h;
2000 /* start video display */
2001 is->pictq_mutex = SDL_CreateMutex();
2002 is->pictq_cond = SDL_CreateCond();
2004 is->subpq_mutex = SDL_CreateMutex();
2005 is->subpq_cond = SDL_CreateCond();
2007 is->subtitle_decoder_mutex = SDL_CreateMutex();
2008 is->audio_decoder_mutex = SDL_CreateMutex();
2009 is->video_decoder_mutex = SDL_CreateMutex();
2011 /* add the refresh timer to draw the picture */
2012 schedule_refresh(is, 40);
2014 is->av_sync_type = av_sync_type;
2015 is->parse_tid = SDL_CreateThread(decode_thread, is);
2016 if (!is->parse_tid) {
2023 static void stream_close(VideoState *is)
2027 /* XXX: use a special url_shutdown call to abort parse cleanly */
2028 is->abort_request = 1;
2029 SDL_WaitThread(is->parse_tid, NULL);
2031 /* free all pictures */
2032 for(i=0;i<VIDEO_PICTURE_QUEUE_SIZE; i++) {
2035 SDL_FreeYUVOverlay(vp->bmp);
2039 SDL_DestroyMutex(is->pictq_mutex);
2040 SDL_DestroyCond(is->pictq_cond);
2041 SDL_DestroyMutex(is->subpq_mutex);
2042 SDL_DestroyCond(is->subpq_cond);
2043 SDL_DestroyMutex(is->subtitle_decoder_mutex);
2044 SDL_DestroyMutex(is->audio_decoder_mutex);
2045 SDL_DestroyMutex(is->video_decoder_mutex);
2048 void stream_cycle_channel(VideoState *is, int codec_type)
2050 AVFormatContext *ic = is->ic;
2051 int start_index, stream_index;
2054 if (codec_type == CODEC_TYPE_VIDEO)
2055 start_index = is->video_stream;
2056 else if (codec_type == CODEC_TYPE_AUDIO)
2057 start_index = is->audio_stream;
2059 start_index = is->subtitle_stream;
2060 if (start_index < (codec_type == CODEC_TYPE_SUBTITLE ? -1 : 0))
2062 stream_index = start_index;
2064 if (++stream_index >= is->ic->nb_streams)
2066 if (codec_type == CODEC_TYPE_SUBTITLE)
2073 if (stream_index == start_index)
2075 st = ic->streams[stream_index];
2076 if (st->codec->codec_type == codec_type) {
2077 /* check that parameters are OK */
2078 switch(codec_type) {
2079 case CODEC_TYPE_AUDIO:
2080 if (st->codec->sample_rate != 0 &&
2081 st->codec->channels != 0)
2084 case CODEC_TYPE_VIDEO:
2085 case CODEC_TYPE_SUBTITLE:
2093 stream_component_close(is, start_index);
2094 stream_component_open(is, stream_index);
2098 void toggle_full_screen(void)
2101 is_full_screen = !is_full_screen;
2102 if (!fs_screen_width) {
2103 /* use default SDL method */
2104 SDL_WM_ToggleFullScreen(screen);
2106 /* use the recorded resolution */
2107 flags = SDL_HWSURFACE|SDL_ASYNCBLIT|SDL_HWACCEL;
2108 if (is_full_screen) {
2109 w = fs_screen_width;
2110 h = fs_screen_height;
2111 flags |= SDL_FULLSCREEN;
2115 flags |= SDL_RESIZABLE;
2117 screen = SDL_SetVideoMode(w, h, 0, flags);
2118 cur_stream->width = w;
2119 cur_stream->height = h;
2123 void toggle_pause(void)
2126 stream_pause(cur_stream);
2130 void step_to_next_frame(void)
2133 if (cur_stream->paused)
2134 cur_stream->paused=0;
2135 cur_stream->video_current_pts = get_video_clock(cur_stream);
2143 stream_close(cur_stream);
2152 void toggle_audio_display(void)
2155 cur_stream->show_audio = !cur_stream->show_audio;
2159 /* handle an event sent by the GUI */
2160 void event_loop(void)
2163 double incr, pos, frac;
2166 SDL_WaitEvent(&event);
2167 switch(event.type) {
2169 switch(event.key.keysym.sym) {
2175 toggle_full_screen();
2181 case SDLK_s: //S: Step to next frame
2182 step_to_next_frame();
2186 stream_cycle_channel(cur_stream, CODEC_TYPE_AUDIO);
2190 stream_cycle_channel(cur_stream, CODEC_TYPE_VIDEO);
2194 stream_cycle_channel(cur_stream, CODEC_TYPE_SUBTITLE);
2197 toggle_audio_display();
2212 pos = get_master_clock(cur_stream);
2214 stream_seek(cur_stream, (int64_t)(pos * AV_TIME_BASE), incr);
2221 case SDL_MOUSEBUTTONDOWN:
2224 int tns, thh, tmm, tss;
2225 tns = cur_stream->ic->duration/1000000LL;
2227 tmm = (tns%3600)/60;
2229 frac = (double)event.button.x/(double)cur_stream->width;
2234 fprintf(stderr, "Seek to %2.0f%% (%2d:%02d:%02d) of total duration (%2d:%02d:%02d) \n", frac*100,
2235 hh, mm, ss, thh, tmm, tss);
2236 stream_seek(cur_stream, (int64_t)(cur_stream->ic->start_time+frac*cur_stream->ic->duration), 0);
2239 case SDL_VIDEORESIZE:
2241 screen = SDL_SetVideoMode(event.resize.w, event.resize.h, 0,
2242 SDL_HWSURFACE|SDL_RESIZABLE|SDL_ASYNCBLIT|SDL_HWACCEL);
2243 cur_stream->width = event.resize.w;
2244 cur_stream->height = event.resize.h;
2251 case FF_ALLOC_EVENT:
2252 alloc_picture(event.user.data1);
2254 case FF_REFRESH_EVENT:
2255 video_refresh_timer(event.user.data1);
2263 void opt_width(const char *arg)
2265 screen_width = atoi(arg);
2268 void opt_height(const char *arg)
2270 screen_height = atoi(arg);
2273 static void opt_format(const char *arg)
2275 file_iformat = av_find_input_format(arg);
2276 if (!file_iformat) {
2277 fprintf(stderr, "Unknown input format: %s\n", arg);
2282 static void opt_image_format(const char *arg)
2286 for(f = first_image_format; f != NULL; f = f->next) {
2287 if (!strcmp(arg, f->name))
2291 fprintf(stderr, "Unknown image format: '%s'\n", arg);
2297 #ifdef CONFIG_NETWORK
2298 void opt_rtp_tcp(void)
2300 /* only tcp protocol */
2301 rtsp_default_protocols = (1 << RTSP_PROTOCOL_RTP_TCP);
2305 void opt_sync(const char *arg)
2307 if (!strcmp(arg, "audio"))
2308 av_sync_type = AV_SYNC_AUDIO_MASTER;
2309 else if (!strcmp(arg, "video"))
2310 av_sync_type = AV_SYNC_VIDEO_MASTER;
2311 else if (!strcmp(arg, "ext"))
2312 av_sync_type = AV_SYNC_EXTERNAL_CLOCK;
2317 void opt_seek(const char *arg)
2319 start_time = parse_date(arg, 1);
2322 static void opt_debug(const char *arg)
2327 static void opt_vismv(const char *arg)
2329 debug_mv = atoi(arg);
2332 static void opt_thread_count(const char *arg)
2334 thread_count= atoi(arg);
2335 #if !defined(HAVE_THREADS)
2336 fprintf(stderr, "Warning: not compiled with thread support, using thread emulation\n");
2340 const OptionDef options[] = {
2341 { "h", 0, {(void*)show_help}, "show help" },
2342 { "x", HAS_ARG, {(void*)opt_width}, "force displayed width", "width" },
2343 { "y", HAS_ARG, {(void*)opt_height}, "force displayed height", "height" },
2345 /* disabled as SDL/X11 does not support it correctly on application launch */
2346 { "fs", OPT_BOOL, {(void*)&is_full_screen}, "force full screen" },
2348 { "an", OPT_BOOL, {(void*)&audio_disable}, "disable audio" },
2349 { "vn", OPT_BOOL, {(void*)&video_disable}, "disable video" },
2350 { "ss", HAS_ARG, {(void*)&opt_seek}, "seek to a given position in seconds", "pos" },
2351 { "nodisp", OPT_BOOL, {(void*)&display_disable}, "disable graphical display" },
2352 { "f", HAS_ARG, {(void*)opt_format}, "force format", "fmt" },
2353 { "img", HAS_ARG, {(void*)opt_image_format}, "force image format", "img_fmt" },
2354 { "stats", OPT_BOOL | OPT_EXPERT, {(void*)&show_status}, "show status", "" },
2355 { "debug", HAS_ARG | OPT_EXPERT, {(void*)opt_debug}, "print specific debug info", "" },
2356 { "bug", OPT_INT | HAS_ARG | OPT_EXPERT, {(void*)&workaround_bugs}, "workaround bugs", "" },
2357 { "vismv", HAS_ARG | OPT_EXPERT, {(void*)opt_vismv}, "visualize motion vectors", "" },
2358 { "fast", OPT_BOOL | OPT_EXPERT, {(void*)&fast}, "non spec compliant optimizations", "" },
2359 { "lowres", OPT_INT | HAS_ARG | OPT_EXPERT, {(void*)&lowres}, "", "" },
2360 { "skiploop", OPT_INT | HAS_ARG | OPT_EXPERT, {(void*)&skip_loop_filter}, "", "" },
2361 { "skipframe", OPT_INT | HAS_ARG | OPT_EXPERT, {(void*)&skip_frame}, "", "" },
2362 { "skipidct", OPT_INT | HAS_ARG | OPT_EXPERT, {(void*)&skip_idct}, "", "" },
2363 { "idct", OPT_INT | HAS_ARG | OPT_EXPERT, {(void*)&idct}, "set idct algo", "algo" },
2364 { "er", OPT_INT | HAS_ARG | OPT_EXPERT, {(void*)&error_resilience}, "set error detection threshold (0-4)", "threshold" },
2365 { "ec", OPT_INT | HAS_ARG | OPT_EXPERT, {(void*)&error_concealment}, "set error concealment options", "bit_mask" },
2366 #ifdef CONFIG_NETWORK
2367 { "rtp_tcp", OPT_EXPERT, {(void*)&opt_rtp_tcp}, "force RTP/TCP protocol usage", "" },
2369 { "sync", HAS_ARG | OPT_EXPERT, {(void*)opt_sync}, "set audio-video sync. type (type=audio/video/ext)", "type" },
2370 { "threads", HAS_ARG | OPT_EXPERT, {(void*)opt_thread_count}, "thread count", "count" },
2374 void show_help(void)
2376 printf("ffplay version " FFMPEG_VERSION ", Copyright (c) 2003 Fabrice Bellard\n"
2377 "usage: ffplay [options] input_file\n"
2378 "Simple media player\n");
2380 show_help_options(options, "Main options:\n",
2382 show_help_options(options, "\nAdvanced options:\n",
2383 OPT_EXPERT, OPT_EXPERT);
2384 printf("\nWhile playing:\n"
2386 "f toggle full screen\n"
2388 "a cycle audio channel\n"
2389 "v cycle video channel\n"
2390 "t cycle subtitle channel\n"
2391 "w show audio waves\n"
2392 "left/right seek backward/forward 10 seconds\n"
2393 "down/up seek backward/forward 1 minute\n"
2394 "mouse click seek to percentage in file corresponding to fraction of width\n"
2399 void parse_arg_file(const char *filename)
2401 if (!strcmp(filename, "-"))
2403 input_filename = filename;
2406 /* Called from the main */
2407 int main(int argc, char **argv)
2411 /* register all codecs, demux and protocols */
2415 MorphToPM(); // Morph the VIO application to a PM one to be able to use Win* functions
2417 // Make stdout and stderr unbuffered
2418 setbuf( stdout, NULL );
2419 setbuf( stderr, NULL );
2422 parse_options(argc, argv, options);
2424 if (!input_filename)
2427 if (display_disable) {
2430 flags = SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER;
2431 #ifndef CONFIG_WIN32
2432 flags |= SDL_INIT_EVENTTHREAD; /* Not supported on win32 */
2434 if (SDL_Init (flags)) {
2435 fprintf(stderr, "Could not initialize SDL - %s\n", SDL_GetError());
2439 if (!display_disable) {
2441 /* save the screen resolution... SDL should allow full screen
2442 by resizing the window */
2445 dpy = XOpenDisplay(NULL);
2447 fs_screen_width = DisplayWidth(dpy, DefaultScreen(dpy));
2448 fs_screen_height = DisplayHeight(dpy, DefaultScreen(dpy));
2453 flags = SDL_HWSURFACE|SDL_ASYNCBLIT|SDL_HWACCEL;
2454 if (is_full_screen && fs_screen_width) {
2455 w = fs_screen_width;
2456 h = fs_screen_height;
2457 flags |= SDL_FULLSCREEN;
2461 flags |= SDL_RESIZABLE;
2463 screen = SDL_SetVideoMode(w, h, 0, flags);
2465 fprintf(stderr, "SDL: could not set video mode - exiting\n");
2468 SDL_WM_SetCaption("FFplay", "FFplay");
2471 SDL_EventState(SDL_ACTIVEEVENT, SDL_IGNORE);
2472 SDL_EventState(SDL_MOUSEMOTION, SDL_IGNORE);
2473 SDL_EventState(SDL_SYSWMEVENT, SDL_IGNORE);
2474 SDL_EventState(SDL_USEREVENT, SDL_IGNORE);
2476 cur_stream = stream_open(input_filename, file_iformat);