2 * Copyright (c) 2003 Fabrice Bellard
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 * simple media player based on the FFmpeg libraries
31 #include "libavutil/avstring.h"
32 #include "libavutil/colorspace.h"
33 #include "libavutil/mathematics.h"
34 #include "libavutil/pixdesc.h"
35 #include "libavutil/imgutils.h"
36 #include "libavutil/dict.h"
37 #include "libavutil/parseutils.h"
38 #include "libavutil/samplefmt.h"
39 #include "libavutil/avassert.h"
40 #include "libavutil/time.h"
41 #include "libavformat/avformat.h"
42 #include "libavdevice/avdevice.h"
43 #include "libswscale/swscale.h"
44 #include "libavutil/opt.h"
45 #include "libavcodec/avfft.h"
46 #include "libswresample/swresample.h"
49 # include "libavfilter/avcodec.h"
50 # include "libavfilter/avfilter.h"
51 # include "libavfilter/avfiltergraph.h"
52 # include "libavfilter/buffersink.h"
53 # include "libavfilter/buffersrc.h"
57 #include <SDL_thread.h>
63 const char program_name[] = "ffplay";
64 const int program_birth_year = 2003;
66 #define MAX_QUEUE_SIZE (15 * 1024 * 1024)
69 /* SDL audio buffer size, in samples. Should be small to have precise
70 A/V sync as SDL does not have hardware buffer fullness info. */
71 #define SDL_AUDIO_BUFFER_SIZE 1024
73 /* no AV sync correction is done if below the AV sync threshold */
74 #define AV_SYNC_THRESHOLD 0.01
75 /* no AV correction is done if too big error */
76 #define AV_NOSYNC_THRESHOLD 10.0
78 /* maximum audio speed change to get correct sync */
79 #define SAMPLE_CORRECTION_PERCENT_MAX 10
81 /* external clock speed adjustment constants for realtime sources based on buffer fullness */
82 #define EXTERNAL_CLOCK_SPEED_MIN 0.900
83 #define EXTERNAL_CLOCK_SPEED_MAX 1.010
84 #define EXTERNAL_CLOCK_SPEED_STEP 0.001
86 /* we use about AUDIO_DIFF_AVG_NB A-V differences to make the average */
87 #define AUDIO_DIFF_AVG_NB 20
89 /* polls for possible required screen refresh at least this often, should be less than 1/fps */
90 #define REFRESH_RATE 0.01
92 /* NOTE: the size must be big enough to compensate the hardware audio buffersize size */
93 /* TODO: We assume that a decoded and resampled frame fits into this buffer */
94 #define SAMPLE_ARRAY_SIZE (8 * 65536)
96 #define CURSOR_HIDE_DELAY 1000000
98 static int64_t sws_flags = SWS_BICUBIC;
100 typedef struct MyAVPacketList {
102 struct MyAVPacketList *next;
106 typedef struct PacketQueue {
107 MyAVPacketList *first_pkt, *last_pkt;
116 #define VIDEO_PICTURE_QUEUE_SIZE 4
117 #define SUBPICTURE_QUEUE_SIZE 4
119 typedef struct VideoPicture {
120 double pts; // presentation timestamp for this picture
121 int64_t pos; // byte position in file
123 int width, height; /* source height & width */
124 AVRational sample_aspect_ratio;
130 AVFilterBufferRef *picref;
134 typedef struct SubPicture {
135 double pts; /* presentation time stamp for this picture */
139 typedef struct AudioParams {
142 int64_t channel_layout;
143 enum AVSampleFormat fmt;
147 AV_SYNC_AUDIO_MASTER, /* default choice */
148 AV_SYNC_VIDEO_MASTER,
149 AV_SYNC_EXTERNAL_CLOCK, /* synchronize to an external clock */
152 typedef struct VideoState {
153 SDL_Thread *read_tid;
154 SDL_Thread *video_tid;
155 AVInputFormat *iformat;
161 int queue_attachments_req;
166 int read_pause_return;
173 double external_clock; ///< external clock base
174 double external_clock_drift; ///< external clock base - time (av_gettime) at which we updated external_clock
175 int64_t external_clock_time; ///< last reference time
176 double external_clock_speed; ///< speed of the external clock
179 int audio_clock_serial;
180 double audio_diff_cum; /* used for AV difference average computation */
181 double audio_diff_avg_coef;
182 double audio_diff_threshold;
183 int audio_diff_avg_count;
186 int audio_hw_buf_size;
187 uint8_t silence_buf[SDL_AUDIO_BUFFER_SIZE];
190 unsigned int audio_buf_size; /* in bytes */
191 unsigned int audio_buf1_size;
192 int audio_buf_index; /* in bytes */
193 int audio_write_buf_size;
194 AVPacket audio_pkt_temp;
196 int audio_pkt_temp_serial;
197 struct AudioParams audio_src;
198 struct AudioParams audio_tgt;
199 struct SwrContext *swr_ctx;
200 double audio_current_pts;
201 double audio_current_pts_drift;
202 int frame_drops_early;
203 int frame_drops_late;
207 SHOW_MODE_NONE = -1, SHOW_MODE_VIDEO = 0, SHOW_MODE_WAVES, SHOW_MODE_RDFT, SHOW_MODE_NB
209 int16_t sample_array[SAMPLE_ARRAY_SIZE];
210 int sample_array_index;
214 FFTSample *rdft_data;
216 double last_vis_time;
218 SDL_Thread *subtitle_tid;
220 int subtitle_stream_changed;
221 AVStream *subtitle_st;
222 PacketQueue subtitleq;
223 SubPicture subpq[SUBPICTURE_QUEUE_SIZE];
224 int subpq_size, subpq_rindex, subpq_windex;
225 SDL_mutex *subpq_mutex;
226 SDL_cond *subpq_cond;
229 double frame_last_pts;
230 double frame_last_duration;
231 double frame_last_dropped_pts;
232 double frame_last_returned_time;
233 double frame_last_filter_delay;
234 int64_t frame_last_dropped_pos;
238 double video_current_pts; // current displayed pts
239 double video_current_pts_drift; // video_current_pts - time (av_gettime) at which we updated video_current_pts - used to have running video pts
240 int64_t video_current_pos; // current displayed file pos
241 double max_frame_duration; // maximum duration of a frame - above this, we consider the jump a timestamp discontinuity
242 int video_clock_serial;
243 VideoPicture pictq[VIDEO_PICTURE_QUEUE_SIZE];
244 int pictq_size, pictq_rindex, pictq_windex;
245 SDL_mutex *pictq_mutex;
246 SDL_cond *pictq_cond;
248 struct SwsContext *img_convert_ctx;
250 SDL_Rect last_display_rect;
253 int width, height, xleft, ytop;
257 AVFilterContext *in_video_filter; // the first filter in the video chain
258 AVFilterContext *out_video_filter; // the last filter in the video chain
260 FrameBuffer *buffer_pool;
263 int last_video_stream, last_audio_stream, last_subtitle_stream;
265 SDL_cond *continue_read_thread;
268 /* options specified by the user */
269 static AVInputFormat *file_iformat;
270 static const char *input_filename;
271 static const char *window_title;
272 static int fs_screen_width;
273 static int fs_screen_height;
274 static int default_width = 640;
275 static int default_height = 480;
276 static int screen_width = 0;
277 static int screen_height = 0;
278 static int audio_disable;
279 static int video_disable;
280 static int subtitle_disable;
281 static int wanted_stream[AVMEDIA_TYPE_NB] = {
282 [AVMEDIA_TYPE_AUDIO] = -1,
283 [AVMEDIA_TYPE_VIDEO] = -1,
284 [AVMEDIA_TYPE_SUBTITLE] = -1,
286 static int seek_by_bytes = -1;
287 static int display_disable;
288 static int show_status = 1;
289 static int av_sync_type = AV_SYNC_AUDIO_MASTER;
290 static int64_t start_time = AV_NOPTS_VALUE;
291 static int64_t duration = AV_NOPTS_VALUE;
292 static int workaround_bugs = 1;
294 static int genpts = 0;
295 static int lowres = 0;
296 static int idct = FF_IDCT_AUTO;
297 static enum AVDiscard skip_frame = AVDISCARD_DEFAULT;
298 static enum AVDiscard skip_idct = AVDISCARD_DEFAULT;
299 static enum AVDiscard skip_loop_filter = AVDISCARD_DEFAULT;
300 static int error_concealment = 3;
301 static int decoder_reorder_pts = -1;
303 static int exit_on_keydown;
304 static int exit_on_mousedown;
306 static int framedrop = -1;
307 static int infinite_buffer = -1;
308 static enum ShowMode show_mode = SHOW_MODE_NONE;
309 static const char *audio_codec_name;
310 static const char *subtitle_codec_name;
311 static const char *video_codec_name;
312 double rdftspeed = 0.02;
313 static int64_t cursor_last_shown;
314 static int cursor_hidden = 0;
316 static char *vfilters = NULL;
319 /* current context */
320 static int is_full_screen;
321 static int64_t audio_callback_time;
323 static AVPacket flush_pkt;
325 #define FF_ALLOC_EVENT (SDL_USEREVENT)
326 #define FF_QUIT_EVENT (SDL_USEREVENT + 2)
328 static SDL_Surface *screen;
330 static int packet_queue_put(PacketQueue *q, AVPacket *pkt);
332 static int packet_queue_put_private(PacketQueue *q, AVPacket *pkt)
334 MyAVPacketList *pkt1;
336 if (q->abort_request)
339 pkt1 = av_malloc(sizeof(MyAVPacketList));
344 if (pkt == &flush_pkt)
346 pkt1->serial = q->serial;
351 q->last_pkt->next = pkt1;
354 q->size += pkt1->pkt.size + sizeof(*pkt1);
355 /* XXX: should duplicate packet data in DV case */
356 SDL_CondSignal(q->cond);
360 static int packet_queue_put(PacketQueue *q, AVPacket *pkt)
364 /* duplicate the packet */
365 if (pkt != &flush_pkt && av_dup_packet(pkt) < 0)
368 SDL_LockMutex(q->mutex);
369 ret = packet_queue_put_private(q, pkt);
370 SDL_UnlockMutex(q->mutex);
372 if (pkt != &flush_pkt && ret < 0)
378 /* packet queue handling */
379 static void packet_queue_init(PacketQueue *q)
381 memset(q, 0, sizeof(PacketQueue));
382 q->mutex = SDL_CreateMutex();
383 q->cond = SDL_CreateCond();
384 q->abort_request = 1;
387 static void packet_queue_flush(PacketQueue *q)
389 MyAVPacketList *pkt, *pkt1;
391 SDL_LockMutex(q->mutex);
392 for (pkt = q->first_pkt; pkt != NULL; pkt = pkt1) {
394 av_free_packet(&pkt->pkt);
401 SDL_UnlockMutex(q->mutex);
404 static void packet_queue_destroy(PacketQueue *q)
406 packet_queue_flush(q);
407 SDL_DestroyMutex(q->mutex);
408 SDL_DestroyCond(q->cond);
411 static void packet_queue_abort(PacketQueue *q)
413 SDL_LockMutex(q->mutex);
415 q->abort_request = 1;
417 SDL_CondSignal(q->cond);
419 SDL_UnlockMutex(q->mutex);
422 static void packet_queue_start(PacketQueue *q)
424 SDL_LockMutex(q->mutex);
425 q->abort_request = 0;
426 packet_queue_put_private(q, &flush_pkt);
427 SDL_UnlockMutex(q->mutex);
430 /* return < 0 if aborted, 0 if no packet and > 0 if packet. */
431 static int packet_queue_get(PacketQueue *q, AVPacket *pkt, int block, int *serial)
433 MyAVPacketList *pkt1;
436 SDL_LockMutex(q->mutex);
439 if (q->abort_request) {
446 q->first_pkt = pkt1->next;
450 q->size -= pkt1->pkt.size + sizeof(*pkt1);
453 *serial = pkt1->serial;
461 SDL_CondWait(q->cond, q->mutex);
464 SDL_UnlockMutex(q->mutex);
468 static inline void fill_rectangle(SDL_Surface *screen,
469 int x, int y, int w, int h, int color, int update)
476 SDL_FillRect(screen, &rect, color);
477 if (update && w > 0 && h > 0)
478 SDL_UpdateRect(screen, x, y, w, h);
481 /* draw only the border of a rectangle */
482 static void fill_border(int xleft, int ytop, int width, int height, int x, int y, int w, int h, int color, int update)
486 /* fill the background */
490 w2 = width - (x + w);
496 h2 = height - (y + h);
499 fill_rectangle(screen,
503 fill_rectangle(screen,
504 xleft + width - w2, ytop,
507 fill_rectangle(screen,
511 fill_rectangle(screen,
512 xleft + w1, ytop + height - h2,
517 #define ALPHA_BLEND(a, oldp, newp, s)\
518 ((((oldp << s) * (255 - (a))) + (newp * (a))) / (255 << s))
520 #define RGBA_IN(r, g, b, a, s)\
522 unsigned int v = ((const uint32_t *)(s))[0];\
523 a = (v >> 24) & 0xff;\
524 r = (v >> 16) & 0xff;\
525 g = (v >> 8) & 0xff;\
529 #define YUVA_IN(y, u, v, a, s, pal)\
531 unsigned int val = ((const uint32_t *)(pal))[*(const uint8_t*)(s)];\
532 a = (val >> 24) & 0xff;\
533 y = (val >> 16) & 0xff;\
534 u = (val >> 8) & 0xff;\
538 #define YUVA_OUT(d, y, u, v, a)\
540 ((uint32_t *)(d))[0] = (a << 24) | (y << 16) | (u << 8) | v;\
546 static void blend_subrect(AVPicture *dst, const AVSubtitleRect *rect, int imgw, int imgh)
548 int wrap, wrap3, width2, skip2;
549 int y, u, v, a, u1, v1, a1, w, h;
550 uint8_t *lum, *cb, *cr;
553 int dstx, dsty, dstw, dsth;
555 dstw = av_clip(rect->w, 0, imgw);
556 dsth = av_clip(rect->h, 0, imgh);
557 dstx = av_clip(rect->x, 0, imgw - dstw);
558 dsty = av_clip(rect->y, 0, imgh - dsth);
559 lum = dst->data[0] + dsty * dst->linesize[0];
560 cb = dst->data[1] + (dsty >> 1) * dst->linesize[1];
561 cr = dst->data[2] + (dsty >> 1) * dst->linesize[2];
563 width2 = ((dstw + 1) >> 1) + (dstx & ~dstw & 1);
565 wrap = dst->linesize[0];
566 wrap3 = rect->pict.linesize[0];
567 p = rect->pict.data[0];
568 pal = (const uint32_t *)rect->pict.data[1]; /* Now in YCrCb! */
576 YUVA_IN(y, u, v, a, p, pal);
577 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
578 cb[0] = ALPHA_BLEND(a >> 2, cb[0], u, 0);
579 cr[0] = ALPHA_BLEND(a >> 2, cr[0], v, 0);
585 for (w = dstw - (dstx & 1); w >= 2; w -= 2) {
586 YUVA_IN(y, u, v, a, p, pal);
590 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
592 YUVA_IN(y, u, v, a, p + BPP, pal);
596 lum[1] = ALPHA_BLEND(a, lum[1], y, 0);
597 cb[0] = ALPHA_BLEND(a1 >> 2, cb[0], u1, 1);
598 cr[0] = ALPHA_BLEND(a1 >> 2, cr[0], v1, 1);
605 YUVA_IN(y, u, v, a, p, pal);
606 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
607 cb[0] = ALPHA_BLEND(a >> 2, cb[0], u, 0);
608 cr[0] = ALPHA_BLEND(a >> 2, cr[0], v, 0);
612 p += wrap3 - dstw * BPP;
613 lum += wrap - dstw - dstx;
614 cb += dst->linesize[1] - width2 - skip2;
615 cr += dst->linesize[2] - width2 - skip2;
617 for (h = dsth - (dsty & 1); h >= 2; h -= 2) {
623 YUVA_IN(y, u, v, a, p, pal);
627 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
630 YUVA_IN(y, u, v, a, p, pal);
634 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
635 cb[0] = ALPHA_BLEND(a1 >> 2, cb[0], u1, 1);
636 cr[0] = ALPHA_BLEND(a1 >> 2, cr[0], v1, 1);
642 for (w = dstw - (dstx & 1); w >= 2; w -= 2) {
643 YUVA_IN(y, u, v, a, p, pal);
647 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
649 YUVA_IN(y, u, v, a, p + BPP, pal);
653 lum[1] = ALPHA_BLEND(a, lum[1], y, 0);
657 YUVA_IN(y, u, v, a, p, pal);
661 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
663 YUVA_IN(y, u, v, a, p + BPP, pal);
667 lum[1] = ALPHA_BLEND(a, lum[1], y, 0);
669 cb[0] = ALPHA_BLEND(a1 >> 2, cb[0], u1, 2);
670 cr[0] = ALPHA_BLEND(a1 >> 2, cr[0], v1, 2);
674 p += -wrap3 + 2 * BPP;
678 YUVA_IN(y, u, v, a, p, pal);
682 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
685 YUVA_IN(y, u, v, a, p, pal);
689 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
690 cb[0] = ALPHA_BLEND(a1 >> 2, cb[0], u1, 1);
691 cr[0] = ALPHA_BLEND(a1 >> 2, cr[0], v1, 1);
697 p += wrap3 + (wrap3 - dstw * BPP);
698 lum += wrap + (wrap - dstw - dstx);
699 cb += dst->linesize[1] - width2 - skip2;
700 cr += dst->linesize[2] - width2 - skip2;
702 /* handle odd height */
709 YUVA_IN(y, u, v, a, p, pal);
710 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
711 cb[0] = ALPHA_BLEND(a >> 2, cb[0], u, 0);
712 cr[0] = ALPHA_BLEND(a >> 2, cr[0], v, 0);
718 for (w = dstw - (dstx & 1); w >= 2; w -= 2) {
719 YUVA_IN(y, u, v, a, p, pal);
723 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
725 YUVA_IN(y, u, v, a, p + BPP, pal);
729 lum[1] = ALPHA_BLEND(a, lum[1], y, 0);
730 cb[0] = ALPHA_BLEND(a1 >> 2, cb[0], u, 1);
731 cr[0] = ALPHA_BLEND(a1 >> 2, cr[0], v, 1);
738 YUVA_IN(y, u, v, a, p, pal);
739 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
740 cb[0] = ALPHA_BLEND(a >> 2, cb[0], u, 0);
741 cr[0] = ALPHA_BLEND(a >> 2, cr[0], v, 0);
746 static void free_subpicture(SubPicture *sp)
748 avsubtitle_free(&sp->sub);
751 static void calculate_display_rect(SDL_Rect *rect, int scr_xleft, int scr_ytop, int scr_width, int scr_height, VideoPicture *vp)
754 int width, height, x, y;
756 if (vp->sample_aspect_ratio.num == 0)
759 aspect_ratio = av_q2d(vp->sample_aspect_ratio);
761 if (aspect_ratio <= 0.0)
763 aspect_ratio *= (float)vp->width / (float)vp->height;
765 /* XXX: we suppose the screen has a 1.0 pixel ratio */
767 width = ((int)rint(height * aspect_ratio)) & ~1;
768 if (width > scr_width) {
770 height = ((int)rint(width / aspect_ratio)) & ~1;
772 x = (scr_width - width) / 2;
773 y = (scr_height - height) / 2;
774 rect->x = scr_xleft + x;
775 rect->y = scr_ytop + y;
776 rect->w = FFMAX(width, 1);
777 rect->h = FFMAX(height, 1);
780 static void video_image_display(VideoState *is)
788 vp = &is->pictq[is->pictq_rindex];
790 if (is->subtitle_st) {
791 if (is->subpq_size > 0) {
792 sp = &is->subpq[is->subpq_rindex];
794 if (vp->pts >= sp->pts + ((float) sp->sub.start_display_time / 1000)) {
795 SDL_LockYUVOverlay (vp->bmp);
797 pict.data[0] = vp->bmp->pixels[0];
798 pict.data[1] = vp->bmp->pixels[2];
799 pict.data[2] = vp->bmp->pixels[1];
801 pict.linesize[0] = vp->bmp->pitches[0];
802 pict.linesize[1] = vp->bmp->pitches[2];
803 pict.linesize[2] = vp->bmp->pitches[1];
805 for (i = 0; i < sp->sub.num_rects; i++)
806 blend_subrect(&pict, sp->sub.rects[i],
807 vp->bmp->w, vp->bmp->h);
809 SDL_UnlockYUVOverlay (vp->bmp);
814 calculate_display_rect(&rect, is->xleft, is->ytop, is->width, is->height, vp);
816 SDL_DisplayYUVOverlay(vp->bmp, &rect);
818 if (rect.x != is->last_display_rect.x || rect.y != is->last_display_rect.y || rect.w != is->last_display_rect.w || rect.h != is->last_display_rect.h || is->force_refresh) {
819 int bgcolor = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00);
820 fill_border(is->xleft, is->ytop, is->width, is->height, rect.x, rect.y, rect.w, rect.h, bgcolor, 1);
821 is->last_display_rect = rect;
826 static inline int compute_mod(int a, int b)
828 return a < 0 ? a%b + b : a%b;
831 static void video_audio_display(VideoState *s)
833 int i, i_start, x, y1, y, ys, delay, n, nb_display_channels;
834 int ch, channels, h, h2, bgcolor, fgcolor;
836 int rdft_bits, nb_freq;
838 for (rdft_bits = 1; (1 << rdft_bits) < 2 * s->height; rdft_bits++)
840 nb_freq = 1 << (rdft_bits - 1);
842 /* compute display index : center on currently output samples */
843 channels = s->audio_tgt.channels;
844 nb_display_channels = channels;
846 int data_used= s->show_mode == SHOW_MODE_WAVES ? s->width : (2*nb_freq);
848 delay = s->audio_write_buf_size;
851 /* to be more precise, we take into account the time spent since
852 the last buffer computation */
853 if (audio_callback_time) {
854 time_diff = av_gettime() - audio_callback_time;
855 delay -= (time_diff * s->audio_tgt.freq) / 1000000;
858 delay += 2 * data_used;
859 if (delay < data_used)
862 i_start= x = compute_mod(s->sample_array_index - delay * channels, SAMPLE_ARRAY_SIZE);
863 if (s->show_mode == SHOW_MODE_WAVES) {
865 for (i = 0; i < 1000; i += channels) {
866 int idx = (SAMPLE_ARRAY_SIZE + x - i) % SAMPLE_ARRAY_SIZE;
867 int a = s->sample_array[idx];
868 int b = s->sample_array[(idx + 4 * channels) % SAMPLE_ARRAY_SIZE];
869 int c = s->sample_array[(idx + 5 * channels) % SAMPLE_ARRAY_SIZE];
870 int d = s->sample_array[(idx + 9 * channels) % SAMPLE_ARRAY_SIZE];
872 if (h < score && (b ^ c) < 0) {
879 s->last_i_start = i_start;
881 i_start = s->last_i_start;
884 bgcolor = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00);
885 if (s->show_mode == SHOW_MODE_WAVES) {
886 fill_rectangle(screen,
887 s->xleft, s->ytop, s->width, s->height,
890 fgcolor = SDL_MapRGB(screen->format, 0xff, 0xff, 0xff);
892 /* total height for one channel */
893 h = s->height / nb_display_channels;
894 /* graph height / 2 */
896 for (ch = 0; ch < nb_display_channels; ch++) {
898 y1 = s->ytop + ch * h + (h / 2); /* position of center line */
899 for (x = 0; x < s->width; x++) {
900 y = (s->sample_array[i] * h2) >> 15;
907 fill_rectangle(screen,
908 s->xleft + x, ys, 1, y,
911 if (i >= SAMPLE_ARRAY_SIZE)
912 i -= SAMPLE_ARRAY_SIZE;
916 fgcolor = SDL_MapRGB(screen->format, 0x00, 0x00, 0xff);
918 for (ch = 1; ch < nb_display_channels; ch++) {
919 y = s->ytop + ch * h;
920 fill_rectangle(screen,
921 s->xleft, y, s->width, 1,
924 SDL_UpdateRect(screen, s->xleft, s->ytop, s->width, s->height);
926 nb_display_channels= FFMIN(nb_display_channels, 2);
927 if (rdft_bits != s->rdft_bits) {
928 av_rdft_end(s->rdft);
929 av_free(s->rdft_data);
930 s->rdft = av_rdft_init(rdft_bits, DFT_R2C);
931 s->rdft_bits = rdft_bits;
932 s->rdft_data = av_malloc(4 * nb_freq * sizeof(*s->rdft_data));
936 for (ch = 0; ch < nb_display_channels; ch++) {
937 data[ch] = s->rdft_data + 2 * nb_freq * ch;
939 for (x = 0; x < 2 * nb_freq; x++) {
940 double w = (x-nb_freq) * (1.0 / nb_freq);
941 data[ch][x] = s->sample_array[i] * (1.0 - w * w);
943 if (i >= SAMPLE_ARRAY_SIZE)
944 i -= SAMPLE_ARRAY_SIZE;
946 av_rdft_calc(s->rdft, data[ch]);
948 // least efficient way to do this, we should of course directly access it but its more than fast enough
949 for (y = 0; y < s->height; y++) {
950 double w = 1 / sqrt(nb_freq);
951 int a = sqrt(w * sqrt(data[0][2 * y + 0] * data[0][2 * y + 0] + data[0][2 * y + 1] * data[0][2 * y + 1]));
952 int b = (nb_display_channels == 2 ) ? sqrt(w * sqrt(data[1][2 * y + 0] * data[1][2 * y + 0]
953 + data[1][2 * y + 1] * data[1][2 * y + 1])) : a;
956 fgcolor = SDL_MapRGB(screen->format, a, b, (a + b) / 2);
958 fill_rectangle(screen,
959 s->xpos, s->height-y, 1, 1,
963 SDL_UpdateRect(screen, s->xpos, s->ytop, 1, s->height);
966 if (s->xpos >= s->width)
971 static void stream_close(VideoState *is)
975 /* XXX: use a special url_shutdown call to abort parse cleanly */
976 is->abort_request = 1;
977 SDL_WaitThread(is->read_tid, NULL);
978 packet_queue_destroy(&is->videoq);
979 packet_queue_destroy(&is->audioq);
980 packet_queue_destroy(&is->subtitleq);
982 /* free all pictures */
983 for (i = 0; i < VIDEO_PICTURE_QUEUE_SIZE; i++) {
986 avfilter_unref_bufferp(&vp->picref);
989 SDL_FreeYUVOverlay(vp->bmp);
993 SDL_DestroyMutex(is->pictq_mutex);
994 SDL_DestroyCond(is->pictq_cond);
995 SDL_DestroyMutex(is->subpq_mutex);
996 SDL_DestroyCond(is->subpq_cond);
997 SDL_DestroyCond(is->continue_read_thread);
999 sws_freeContext(is->img_convert_ctx);
1004 static void do_exit(VideoState *is)
1009 av_lockmgr_register(NULL);
1013 av_freep(&vfilters);
1015 avformat_network_deinit();
1019 av_log(NULL, AV_LOG_QUIET, "%s", "");
1023 static void sigterm_handler(int sig)
1028 static int video_open(VideoState *is, int force_set_video_mode, VideoPicture *vp)
1030 int flags = SDL_HWSURFACE | SDL_ASYNCBLIT | SDL_HWACCEL;
1034 if (is_full_screen) flags |= SDL_FULLSCREEN;
1035 else flags |= SDL_RESIZABLE;
1037 if (vp && vp->width) {
1038 calculate_display_rect(&rect, 0, 0, INT_MAX, vp->height, vp);
1039 default_width = rect.w;
1040 default_height = rect.h;
1043 if (is_full_screen && fs_screen_width) {
1044 w = fs_screen_width;
1045 h = fs_screen_height;
1046 } else if (!is_full_screen && screen_width) {
1053 if (screen && is->width == screen->w && screen->w == w
1054 && is->height== screen->h && screen->h == h && !force_set_video_mode)
1056 screen = SDL_SetVideoMode(w, h, 0, flags);
1058 fprintf(stderr, "SDL: could not set video mode - exiting\n");
1062 window_title = input_filename;
1063 SDL_WM_SetCaption(window_title, window_title);
1065 is->width = screen->w;
1066 is->height = screen->h;
1071 /* display the current picture, if any */
1072 static void video_display(VideoState *is)
1075 video_open(is, 0, NULL);
1076 if (is->audio_st && is->show_mode != SHOW_MODE_VIDEO)
1077 video_audio_display(is);
1078 else if (is->video_st)
1079 video_image_display(is);
1082 /* get the current audio clock value */
1083 static double get_audio_clock(VideoState *is)
1085 if (is->audio_clock_serial != is->audioq.serial)
1088 return is->audio_current_pts;
1090 return is->audio_current_pts_drift + av_gettime() / 1000000.0;
1094 /* get the current video clock value */
1095 static double get_video_clock(VideoState *is)
1097 if (is->video_clock_serial != is->videoq.serial)
1100 return is->video_current_pts;
1102 return is->video_current_pts_drift + av_gettime() / 1000000.0;
1106 /* get the current external clock value */
1107 static double get_external_clock(VideoState *is)
1110 return is->external_clock;
1112 double time = av_gettime() / 1000000.0;
1113 return is->external_clock_drift + time - (time - is->external_clock_time / 1000000.0) * (1.0 - is->external_clock_speed);
1117 static int get_master_sync_type(VideoState *is) {
1118 if (is->av_sync_type == AV_SYNC_VIDEO_MASTER) {
1120 return AV_SYNC_VIDEO_MASTER;
1122 return AV_SYNC_AUDIO_MASTER;
1123 } else if (is->av_sync_type == AV_SYNC_AUDIO_MASTER) {
1125 return AV_SYNC_AUDIO_MASTER;
1127 return AV_SYNC_EXTERNAL_CLOCK;
1129 return AV_SYNC_EXTERNAL_CLOCK;
1133 /* get the current master clock value */
1134 static double get_master_clock(VideoState *is)
1138 switch (get_master_sync_type(is)) {
1139 case AV_SYNC_VIDEO_MASTER:
1140 val = get_video_clock(is);
1142 case AV_SYNC_AUDIO_MASTER:
1143 val = get_audio_clock(is);
1146 val = get_external_clock(is);
1152 static void update_external_clock_pts(VideoState *is, double pts)
1154 is->external_clock_time = av_gettime();
1155 is->external_clock = pts;
1156 is->external_clock_drift = pts - is->external_clock_time / 1000000.0;
1159 static void check_external_clock_sync(VideoState *is, double pts) {
1160 double ext_clock = get_external_clock(is);
1161 if (isnan(ext_clock) || fabs(ext_clock - pts) > AV_NOSYNC_THRESHOLD) {
1162 update_external_clock_pts(is, pts);
1166 static void update_external_clock_speed(VideoState *is, double speed) {
1167 update_external_clock_pts(is, get_external_clock(is));
1168 is->external_clock_speed = speed;
1171 static void check_external_clock_speed(VideoState *is) {
1172 if (is->video_stream >= 0 && is->videoq.nb_packets <= MIN_FRAMES / 2 ||
1173 is->audio_stream >= 0 && is->audioq.nb_packets <= MIN_FRAMES / 2) {
1174 update_external_clock_speed(is, FFMAX(EXTERNAL_CLOCK_SPEED_MIN, is->external_clock_speed - EXTERNAL_CLOCK_SPEED_STEP));
1175 } else if ((is->video_stream < 0 || is->videoq.nb_packets > MIN_FRAMES * 2) &&
1176 (is->audio_stream < 0 || is->audioq.nb_packets > MIN_FRAMES * 2)) {
1177 update_external_clock_speed(is, FFMIN(EXTERNAL_CLOCK_SPEED_MAX, is->external_clock_speed + EXTERNAL_CLOCK_SPEED_STEP));
1179 double speed = is->external_clock_speed;
1181 update_external_clock_speed(is, speed + EXTERNAL_CLOCK_SPEED_STEP * (1.0 - speed) / fabs(1.0 - speed));
1185 /* seek in the stream */
1186 static void stream_seek(VideoState *is, int64_t pos, int64_t rel, int seek_by_bytes)
1188 if (!is->seek_req) {
1191 is->seek_flags &= ~AVSEEK_FLAG_BYTE;
1193 is->seek_flags |= AVSEEK_FLAG_BYTE;
1195 SDL_CondSignal(is->continue_read_thread);
1199 /* pause or resume the video */
1200 static void stream_toggle_pause(VideoState *is)
1203 is->frame_timer += av_gettime() / 1000000.0 + is->video_current_pts_drift - is->video_current_pts;
1204 if (is->read_pause_return != AVERROR(ENOSYS)) {
1205 is->video_current_pts = is->video_current_pts_drift + av_gettime() / 1000000.0;
1207 is->video_current_pts_drift = is->video_current_pts - av_gettime() / 1000000.0;
1209 update_external_clock_pts(is, get_external_clock(is));
1210 is->paused = !is->paused;
1213 static void toggle_pause(VideoState *is)
1215 stream_toggle_pause(is);
1219 static void step_to_next_frame(VideoState *is)
1221 /* if the stream is paused unpause it, then step */
1223 stream_toggle_pause(is);
1227 static double compute_target_delay(double delay, VideoState *is)
1229 double sync_threshold, diff;
1231 /* update delay to follow master synchronisation source */
1232 if (get_master_sync_type(is) != AV_SYNC_VIDEO_MASTER) {
1233 /* if video is slave, we try to correct big delays by
1234 duplicating or deleting a frame */
1235 diff = get_video_clock(is) - get_master_clock(is);
1237 /* skip or repeat frame. We take into account the
1238 delay to compute the threshold. I still don't know
1239 if it is the best guess */
1240 sync_threshold = FFMAX(AV_SYNC_THRESHOLD, delay);
1241 if (!isnan(diff) && fabs(diff) < AV_NOSYNC_THRESHOLD) {
1242 if (diff <= -sync_threshold)
1244 else if (diff >= sync_threshold)
1249 av_dlog(NULL, "video: delay=%0.3f A-V=%f\n",
1255 static void pictq_next_picture(VideoState *is) {
1256 /* update queue size and signal for next picture */
1257 if (++is->pictq_rindex == VIDEO_PICTURE_QUEUE_SIZE)
1258 is->pictq_rindex = 0;
1260 SDL_LockMutex(is->pictq_mutex);
1262 SDL_CondSignal(is->pictq_cond);
1263 SDL_UnlockMutex(is->pictq_mutex);
1266 static int pictq_prev_picture(VideoState *is) {
1267 VideoPicture *prevvp;
1269 /* update queue size and signal for the previous picture */
1270 prevvp = &is->pictq[(is->pictq_rindex + VIDEO_PICTURE_QUEUE_SIZE - 1) % VIDEO_PICTURE_QUEUE_SIZE];
1271 if (prevvp->allocated && prevvp->serial == is->videoq.serial) {
1272 SDL_LockMutex(is->pictq_mutex);
1273 if (is->pictq_size < VIDEO_PICTURE_QUEUE_SIZE - 1) {
1274 if (--is->pictq_rindex == -1)
1275 is->pictq_rindex = VIDEO_PICTURE_QUEUE_SIZE - 1;
1279 SDL_CondSignal(is->pictq_cond);
1280 SDL_UnlockMutex(is->pictq_mutex);
1285 static void update_video_pts(VideoState *is, double pts, int64_t pos, int serial) {
1286 double time = av_gettime() / 1000000.0;
1287 /* update current video pts */
1288 is->video_current_pts = pts;
1289 is->video_current_pts_drift = is->video_current_pts - time;
1290 is->video_current_pos = pos;
1291 is->frame_last_pts = pts;
1292 is->video_clock_serial = serial;
1293 if (is->videoq.serial == serial)
1294 check_external_clock_sync(is, is->video_current_pts);
1297 /* called to display each frame */
1298 static void video_refresh(void *opaque, double *remaining_time)
1300 VideoState *is = opaque;
1304 SubPicture *sp, *sp2;
1306 if (!is->paused && get_master_sync_type(is) == AV_SYNC_EXTERNAL_CLOCK && is->realtime)
1307 check_external_clock_speed(is);
1309 if (!display_disable && is->show_mode != SHOW_MODE_VIDEO && is->audio_st) {
1310 time = av_gettime() / 1000000.0;
1311 if (is->force_refresh || is->last_vis_time + rdftspeed < time) {
1313 is->last_vis_time = time;
1315 *remaining_time = FFMIN(*remaining_time, is->last_vis_time + rdftspeed - time);
1320 if (is->force_refresh)
1321 redisplay = pictq_prev_picture(is);
1323 if (is->pictq_size == 0) {
1324 SDL_LockMutex(is->pictq_mutex);
1325 if (is->frame_last_dropped_pts != AV_NOPTS_VALUE && is->frame_last_dropped_pts > is->frame_last_pts) {
1326 update_video_pts(is, is->frame_last_dropped_pts, is->frame_last_dropped_pos, 0);
1327 is->frame_last_dropped_pts = AV_NOPTS_VALUE;
1329 SDL_UnlockMutex(is->pictq_mutex);
1330 // nothing to do, no picture to display in the queue
1332 double last_duration, duration, delay;
1333 /* dequeue the picture */
1334 vp = &is->pictq[is->pictq_rindex];
1336 if (vp->serial != is->videoq.serial) {
1337 pictq_next_picture(is);
1345 /* compute nominal last_duration */
1346 last_duration = vp->pts - is->frame_last_pts;
1347 if (last_duration > 0 && last_duration < is->max_frame_duration) {
1348 /* if duration of the last frame was sane, update last_duration in video state */
1349 is->frame_last_duration = last_duration;
1351 delay = compute_target_delay(is->frame_last_duration, is);
1353 time= av_gettime()/1000000.0;
1354 if (time < is->frame_timer + delay) {
1355 *remaining_time = FFMIN(is->frame_timer + delay - time, *remaining_time);
1360 is->frame_timer += delay * FFMAX(1, floor((time-is->frame_timer) / delay));
1362 SDL_LockMutex(is->pictq_mutex);
1363 update_video_pts(is, vp->pts, vp->pos, vp->serial);
1364 SDL_UnlockMutex(is->pictq_mutex);
1366 if (is->pictq_size > 1) {
1367 VideoPicture *nextvp = &is->pictq[(is->pictq_rindex + 1) % VIDEO_PICTURE_QUEUE_SIZE];
1368 duration = nextvp->pts - vp->pts;
1369 if(!is->step && (redisplay || framedrop>0 || (framedrop && get_master_sync_type(is) != AV_SYNC_VIDEO_MASTER)) && time > is->frame_timer + duration){
1371 is->frame_drops_late++;
1372 pictq_next_picture(is);
1378 if (is->subtitle_st) {
1379 if (is->subtitle_stream_changed) {
1380 SDL_LockMutex(is->subpq_mutex);
1382 while (is->subpq_size) {
1383 free_subpicture(&is->subpq[is->subpq_rindex]);
1385 /* update queue size and signal for next picture */
1386 if (++is->subpq_rindex == SUBPICTURE_QUEUE_SIZE)
1387 is->subpq_rindex = 0;
1391 is->subtitle_stream_changed = 0;
1393 SDL_CondSignal(is->subpq_cond);
1394 SDL_UnlockMutex(is->subpq_mutex);
1396 if (is->subpq_size > 0) {
1397 sp = &is->subpq[is->subpq_rindex];
1399 if (is->subpq_size > 1)
1400 sp2 = &is->subpq[(is->subpq_rindex + 1) % SUBPICTURE_QUEUE_SIZE];
1404 if ((is->video_current_pts > (sp->pts + ((float) sp->sub.end_display_time / 1000)))
1405 || (sp2 && is->video_current_pts > (sp2->pts + ((float) sp2->sub.start_display_time / 1000))))
1407 free_subpicture(sp);
1409 /* update queue size and signal for next picture */
1410 if (++is->subpq_rindex == SUBPICTURE_QUEUE_SIZE)
1411 is->subpq_rindex = 0;
1413 SDL_LockMutex(is->subpq_mutex);
1415 SDL_CondSignal(is->subpq_cond);
1416 SDL_UnlockMutex(is->subpq_mutex);
1423 /* display picture */
1424 if (!display_disable && is->show_mode == SHOW_MODE_VIDEO)
1427 pictq_next_picture(is);
1429 if (is->step && !is->paused)
1430 stream_toggle_pause(is);
1433 is->force_refresh = 0;
1435 static int64_t last_time;
1437 int aqsize, vqsize, sqsize;
1440 cur_time = av_gettime();
1441 if (!last_time || (cur_time - last_time) >= 30000) {
1446 aqsize = is->audioq.size;
1448 vqsize = is->videoq.size;
1449 if (is->subtitle_st)
1450 sqsize = is->subtitleq.size;
1452 if (is->audio_st && is->video_st)
1453 av_diff = get_audio_clock(is) - get_video_clock(is);
1454 printf("%7.2f A-V:%7.3f fd=%4d aq=%5dKB vq=%5dKB sq=%5dB f=%"PRId64"/%"PRId64" \r",
1455 get_master_clock(is),
1457 is->frame_drops_early + is->frame_drops_late,
1461 is->video_st ? is->video_st->codec->pts_correction_num_faulty_dts : 0,
1462 is->video_st ? is->video_st->codec->pts_correction_num_faulty_pts : 0);
1464 last_time = cur_time;
1469 /* allocate a picture (needs to do that in main thread to avoid
1470 potential locking problems */
1471 static void alloc_picture(VideoState *is)
1475 vp = &is->pictq[is->pictq_windex];
1478 SDL_FreeYUVOverlay(vp->bmp);
1481 avfilter_unref_bufferp(&vp->picref);
1484 video_open(is, 0, vp);
1486 vp->bmp = SDL_CreateYUVOverlay(vp->width, vp->height,
1489 if (!vp->bmp || vp->bmp->pitches[0] < vp->width) {
1490 /* SDL allocates a buffer smaller than requested if the video
1491 * overlay hardware is unable to support the requested size. */
1492 fprintf(stderr, "Error: the video system does not support an image\n"
1493 "size of %dx%d pixels. Try using -lowres or -vf \"scale=w:h\"\n"
1494 "to reduce the image size.\n", vp->width, vp->height );
1498 SDL_LockMutex(is->pictq_mutex);
1500 SDL_CondSignal(is->pictq_cond);
1501 SDL_UnlockMutex(is->pictq_mutex);
1504 static void duplicate_right_border_pixels(SDL_Overlay *bmp) {
1505 int i, width, height;
1507 for (i = 0; i < 3; i++) {
1514 if (bmp->pitches[i] > width) {
1515 maxp = bmp->pixels[i] + bmp->pitches[i] * height - 1;
1516 for (p = bmp->pixels[i] + width - 1; p < maxp; p += bmp->pitches[i])
1522 static int queue_picture(VideoState *is, AVFrame *src_frame, double pts, int64_t pos, int serial)
1526 #if defined(DEBUG_SYNC) && 0
1527 printf("frame_type=%c pts=%0.3f\n",
1528 av_get_picture_type_char(src_frame->pict_type), pts);
1531 /* wait until we have space to put a new picture */
1532 SDL_LockMutex(is->pictq_mutex);
1534 /* keep the last already displayed picture in the queue */
1535 while (is->pictq_size >= VIDEO_PICTURE_QUEUE_SIZE - 2 &&
1536 !is->videoq.abort_request) {
1537 SDL_CondWait(is->pictq_cond, is->pictq_mutex);
1539 SDL_UnlockMutex(is->pictq_mutex);
1541 if (is->videoq.abort_request)
1544 vp = &is->pictq[is->pictq_windex];
1547 vp->sample_aspect_ratio = ((AVFilterBufferRef *)src_frame->opaque)->video->sample_aspect_ratio;
1549 vp->sample_aspect_ratio = av_guess_sample_aspect_ratio(is->ic, is->video_st, src_frame);
1552 /* alloc or resize hardware picture buffer */
1553 if (!vp->bmp || vp->reallocate || !vp->allocated ||
1554 vp->width != src_frame->width ||
1555 vp->height != src_frame->height) {
1560 vp->width = src_frame->width;
1561 vp->height = src_frame->height;
1563 /* the allocation must be done in the main thread to avoid
1564 locking problems. */
1565 event.type = FF_ALLOC_EVENT;
1566 event.user.data1 = is;
1567 SDL_PushEvent(&event);
1569 /* wait until the picture is allocated */
1570 SDL_LockMutex(is->pictq_mutex);
1571 while (!vp->allocated && !is->videoq.abort_request) {
1572 SDL_CondWait(is->pictq_cond, is->pictq_mutex);
1574 /* if the queue is aborted, we have to pop the pending ALLOC event or wait for the allocation to complete */
1575 if (is->videoq.abort_request && SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_EVENTMASK(FF_ALLOC_EVENT)) != 1) {
1576 while (!vp->allocated) {
1577 SDL_CondWait(is->pictq_cond, is->pictq_mutex);
1580 SDL_UnlockMutex(is->pictq_mutex);
1582 if (is->videoq.abort_request)
1586 /* if the frame is not skipped, then display it */
1588 AVPicture pict = { { 0 } };
1590 avfilter_unref_bufferp(&vp->picref);
1591 vp->picref = src_frame->opaque;
1594 /* get a pointer on the bitmap */
1595 SDL_LockYUVOverlay (vp->bmp);
1597 pict.data[0] = vp->bmp->pixels[0];
1598 pict.data[1] = vp->bmp->pixels[2];
1599 pict.data[2] = vp->bmp->pixels[1];
1601 pict.linesize[0] = vp->bmp->pitches[0];
1602 pict.linesize[1] = vp->bmp->pitches[2];
1603 pict.linesize[2] = vp->bmp->pitches[1];
1606 // FIXME use direct rendering
1607 av_picture_copy(&pict, (AVPicture *)src_frame,
1608 src_frame->format, vp->width, vp->height);
1610 av_opt_get_int(sws_opts, "sws_flags", 0, &sws_flags);
1611 is->img_convert_ctx = sws_getCachedContext(is->img_convert_ctx,
1612 vp->width, vp->height, src_frame->format, vp->width, vp->height,
1613 AV_PIX_FMT_YUV420P, sws_flags, NULL, NULL, NULL);
1614 if (is->img_convert_ctx == NULL) {
1615 fprintf(stderr, "Cannot initialize the conversion context\n");
1618 sws_scale(is->img_convert_ctx, src_frame->data, src_frame->linesize,
1619 0, vp->height, pict.data, pict.linesize);
1621 /* workaround SDL PITCH_WORKAROUND */
1622 duplicate_right_border_pixels(vp->bmp);
1623 /* update the bitmap content */
1624 SDL_UnlockYUVOverlay(vp->bmp);
1628 vp->serial = serial;
1630 /* now we can update the picture count */
1631 if (++is->pictq_windex == VIDEO_PICTURE_QUEUE_SIZE)
1632 is->pictq_windex = 0;
1633 SDL_LockMutex(is->pictq_mutex);
1635 SDL_UnlockMutex(is->pictq_mutex);
1640 static int get_video_frame(VideoState *is, AVFrame *frame, int64_t *pts, AVPacket *pkt, int *serial)
1644 if (packet_queue_get(&is->videoq, pkt, 1, serial) < 0)
1647 if (pkt->data == flush_pkt.data) {
1648 avcodec_flush_buffers(is->video_st->codec);
1650 SDL_LockMutex(is->pictq_mutex);
1651 // Make sure there are no long delay timers (ideally we should just flush the queue but that's harder)
1652 while (is->pictq_size && !is->videoq.abort_request) {
1653 SDL_CondWait(is->pictq_cond, is->pictq_mutex);
1655 is->video_current_pos = -1;
1656 is->frame_last_pts = AV_NOPTS_VALUE;
1657 is->frame_last_duration = 0;
1658 is->frame_timer = (double)av_gettime() / 1000000.0;
1659 is->frame_last_dropped_pts = AV_NOPTS_VALUE;
1660 SDL_UnlockMutex(is->pictq_mutex);
1665 if(avcodec_decode_video2(is->video_st->codec, frame, &got_picture, pkt) < 0)
1671 if (decoder_reorder_pts == -1) {
1672 *pts = av_frame_get_best_effort_timestamp(frame);
1673 } else if (decoder_reorder_pts) {
1674 *pts = frame->pkt_pts;
1676 *pts = frame->pkt_dts;
1679 if (*pts == AV_NOPTS_VALUE) {
1683 if (framedrop>0 || (framedrop && get_master_sync_type(is) != AV_SYNC_VIDEO_MASTER)) {
1684 SDL_LockMutex(is->pictq_mutex);
1685 if (is->frame_last_pts != AV_NOPTS_VALUE && *pts) {
1686 double clockdiff = get_video_clock(is) - get_master_clock(is);
1687 double dpts = av_q2d(is->video_st->time_base) * *pts;
1688 double ptsdiff = dpts - is->frame_last_pts;
1689 if (!isnan(clockdiff) && fabs(clockdiff) < AV_NOSYNC_THRESHOLD &&
1690 ptsdiff > 0 && ptsdiff < AV_NOSYNC_THRESHOLD &&
1691 clockdiff + ptsdiff - is->frame_last_filter_delay < 0) {
1692 is->frame_last_dropped_pos = pkt->pos;
1693 is->frame_last_dropped_pts = dpts;
1694 is->frame_drops_early++;
1698 SDL_UnlockMutex(is->pictq_mutex);
1707 static int configure_filtergraph(AVFilterGraph *graph, const char *filtergraph,
1708 AVFilterContext *source_ctx, AVFilterContext *sink_ctx)
1711 AVFilterInOut *outputs = NULL, *inputs = NULL;
1714 outputs = avfilter_inout_alloc();
1715 inputs = avfilter_inout_alloc();
1716 if (!outputs || !inputs) {
1717 ret = AVERROR(ENOMEM);
1721 outputs->name = av_strdup("in");
1722 outputs->filter_ctx = source_ctx;
1723 outputs->pad_idx = 0;
1724 outputs->next = NULL;
1726 inputs->name = av_strdup("out");
1727 inputs->filter_ctx = sink_ctx;
1728 inputs->pad_idx = 0;
1729 inputs->next = NULL;
1731 if ((ret = avfilter_graph_parse(graph, filtergraph, &inputs, &outputs, NULL)) < 0)
1734 if ((ret = avfilter_link(source_ctx, 0, sink_ctx, 0)) < 0)
1738 ret = avfilter_graph_config(graph, NULL);
1740 avfilter_inout_free(&outputs);
1741 avfilter_inout_free(&inputs);
1745 static int configure_video_filters(AVFilterGraph *graph, VideoState *is, const char *vfilters, AVFrame *frame)
1747 static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE };
1748 char sws_flags_str[128];
1749 char buffersrc_args[256];
1751 AVBufferSinkParams *buffersink_params = av_buffersink_params_alloc();
1752 AVFilterContext *filt_src = NULL, *filt_out = NULL, *filt_crop;
1753 AVCodecContext *codec = is->video_st->codec;
1755 if (!buffersink_params)
1756 return AVERROR(ENOMEM);
1758 av_opt_get_int(sws_opts, "sws_flags", 0, &sws_flags);
1759 snprintf(sws_flags_str, sizeof(sws_flags_str), "flags=%"PRId64, sws_flags);
1760 graph->scale_sws_opts = av_strdup(sws_flags_str);
1762 snprintf(buffersrc_args, sizeof(buffersrc_args),
1763 "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d",
1764 frame->width, frame->height, frame->format,
1765 is->video_st->time_base.num, is->video_st->time_base.den,
1766 codec->sample_aspect_ratio.num, FFMAX(codec->sample_aspect_ratio.den, 1));
1768 if ((ret = avfilter_graph_create_filter(&filt_src,
1769 avfilter_get_by_name("buffer"),
1770 "ffplay_buffer", buffersrc_args, NULL,
1774 buffersink_params->pixel_fmts = pix_fmts;
1775 ret = avfilter_graph_create_filter(&filt_out,
1776 avfilter_get_by_name("ffbuffersink"),
1777 "ffplay_buffersink", NULL, buffersink_params, graph);
1781 /* SDL YUV code is not handling odd width/height for some driver
1782 * combinations, therefore we crop the picture to an even width/height. */
1783 if ((ret = avfilter_graph_create_filter(&filt_crop,
1784 avfilter_get_by_name("crop"),
1785 "ffplay_crop", "floor(in_w/2)*2:floor(in_h/2)*2", NULL, graph)) < 0)
1787 if ((ret = avfilter_link(filt_crop, 0, filt_out, 0)) < 0)
1790 if ((ret = configure_filtergraph(graph, vfilters, filt_src, filt_crop)) < 0)
1793 is->in_video_filter = filt_src;
1794 is->out_video_filter = filt_out;
1797 av_freep(&buffersink_params);
1801 #endif /* CONFIG_AVFILTER */
1803 static int video_thread(void *arg)
1805 AVPacket pkt = { 0 };
1806 VideoState *is = arg;
1807 AVFrame *frame = avcodec_alloc_frame();
1808 int64_t pts_int = AV_NOPTS_VALUE, pos = -1;
1814 AVCodecContext *codec = is->video_st->codec;
1815 AVFilterGraph *graph = avfilter_graph_alloc();
1816 AVFilterContext *filt_out = NULL, *filt_in = NULL;
1819 enum AVPixelFormat last_format = -2;
1820 int last_serial = -1;
1822 if (codec->codec->capabilities & CODEC_CAP_DR1) {
1824 codec->get_buffer = codec_get_buffer;
1825 codec->release_buffer = codec_release_buffer;
1826 codec->opaque = &is->buffer_pool;
1832 AVFilterBufferRef *picref;
1835 while (is->paused && !is->videoq.abort_request)
1838 avcodec_get_frame_defaults(frame);
1839 av_free_packet(&pkt);
1841 ret = get_video_frame(is, frame, &pts_int, &pkt, &serial);
1849 if ( last_w != frame->width
1850 || last_h != frame->height
1851 || last_format != frame->format
1852 || last_serial != serial) {
1853 av_log(NULL, AV_LOG_DEBUG,
1854 "Video frame changed from size:%dx%d format:%s serial:%d to size:%dx%d format:%s serial:%d\n",
1856 (const char *)av_x_if_null(av_get_pix_fmt_name(last_format), "none"), last_serial,
1857 frame->width, frame->height,
1858 (const char *)av_x_if_null(av_get_pix_fmt_name(frame->format), "none"), serial);
1859 avfilter_graph_free(&graph);
1860 graph = avfilter_graph_alloc();
1861 if ((ret = configure_video_filters(graph, is, vfilters, frame)) < 0) {
1863 event.type = FF_QUIT_EVENT;
1864 event.user.data1 = is;
1865 SDL_PushEvent(&event);
1866 av_free_packet(&pkt);
1869 filt_in = is->in_video_filter;
1870 filt_out = is->out_video_filter;
1871 last_w = frame->width;
1872 last_h = frame->height;
1873 last_format = frame->format;
1874 last_serial = serial;
1877 frame->pts = pts_int;
1878 frame->sample_aspect_ratio = av_guess_sample_aspect_ratio(is->ic, is->video_st, frame);
1879 if (is->use_dr1 && frame->opaque) {
1880 FrameBuffer *buf = frame->opaque;
1881 AVFilterBufferRef *fb = avfilter_get_video_buffer_ref_from_arrays(
1882 frame->data, frame->linesize,
1883 AV_PERM_READ | AV_PERM_PRESERVE,
1884 frame->width, frame->height,
1887 avfilter_copy_frame_props(fb, frame);
1888 fb->buf->priv = buf;
1889 fb->buf->free = filter_release_buffer;
1892 av_buffersrc_add_ref(filt_in, fb, AV_BUFFERSRC_FLAG_NO_COPY);
1895 av_buffersrc_write_frame(filt_in, frame);
1897 av_free_packet(&pkt);
1900 is->frame_last_returned_time = av_gettime() / 1000000.0;
1902 ret = av_buffersink_get_buffer_ref(filt_out, &picref, 0);
1908 is->frame_last_filter_delay = av_gettime() / 1000000.0 - is->frame_last_returned_time;
1909 if (fabs(is->frame_last_filter_delay) > AV_NOSYNC_THRESHOLD / 10.0)
1910 is->frame_last_filter_delay = 0;
1912 avfilter_copy_buf_props(frame, picref);
1914 pts_int = picref->pts;
1915 tb = filt_out->inputs[0]->time_base;
1917 frame->opaque = picref;
1919 if (av_cmp_q(tb, is->video_st->time_base)) {
1920 av_unused int64_t pts1 = pts_int;
1921 pts_int = av_rescale_q(pts_int, tb, is->video_st->time_base);
1922 av_dlog(NULL, "video_thread(): "
1923 "tb:%d/%d pts:%"PRId64" -> tb:%d/%d pts:%"PRId64"\n",
1924 tb.num, tb.den, pts1,
1925 is->video_st->time_base.num, is->video_st->time_base.den, pts_int);
1927 pts = pts_int * av_q2d(is->video_st->time_base);
1928 ret = queue_picture(is, frame, pts, pos, serial);
1931 pts = pts_int * av_q2d(is->video_st->time_base);
1932 ret = queue_picture(is, frame, pts, pkt.pos, serial);
1939 avcodec_flush_buffers(is->video_st->codec);
1941 avfilter_graph_free(&graph);
1943 av_free_packet(&pkt);
1944 avcodec_free_frame(&frame);
1948 static int subtitle_thread(void *arg)
1950 VideoState *is = arg;
1952 AVPacket pkt1, *pkt = &pkt1;
1956 int r, g, b, y, u, v, a;
1959 while (is->paused && !is->subtitleq.abort_request) {
1962 if (packet_queue_get(&is->subtitleq, pkt, 1, NULL) < 0)
1965 if (pkt->data == flush_pkt.data) {
1966 avcodec_flush_buffers(is->subtitle_st->codec);
1969 SDL_LockMutex(is->subpq_mutex);
1970 while (is->subpq_size >= SUBPICTURE_QUEUE_SIZE &&
1971 !is->subtitleq.abort_request) {
1972 SDL_CondWait(is->subpq_cond, is->subpq_mutex);
1974 SDL_UnlockMutex(is->subpq_mutex);
1976 if (is->subtitleq.abort_request)
1979 sp = &is->subpq[is->subpq_windex];
1981 /* NOTE: ipts is the PTS of the _first_ picture beginning in
1982 this packet, if any */
1984 if (pkt->pts != AV_NOPTS_VALUE)
1985 pts = av_q2d(is->subtitle_st->time_base) * pkt->pts;
1987 avcodec_decode_subtitle2(is->subtitle_st->codec, &sp->sub,
1988 &got_subtitle, pkt);
1989 if (got_subtitle && sp->sub.format == 0) {
1990 if (sp->sub.pts != AV_NOPTS_VALUE)
1991 pts = sp->sub.pts / (double)AV_TIME_BASE;
1994 for (i = 0; i < sp->sub.num_rects; i++)
1996 for (j = 0; j < sp->sub.rects[i]->nb_colors; j++)
1998 RGBA_IN(r, g, b, a, (uint32_t*)sp->sub.rects[i]->pict.data[1] + j);
1999 y = RGB_TO_Y_CCIR(r, g, b);
2000 u = RGB_TO_U_CCIR(r, g, b, 0);
2001 v = RGB_TO_V_CCIR(r, g, b, 0);
2002 YUVA_OUT((uint32_t*)sp->sub.rects[i]->pict.data[1] + j, y, u, v, a);
2006 /* now we can update the picture count */
2007 if (++is->subpq_windex == SUBPICTURE_QUEUE_SIZE)
2008 is->subpq_windex = 0;
2009 SDL_LockMutex(is->subpq_mutex);
2011 SDL_UnlockMutex(is->subpq_mutex);
2013 av_free_packet(pkt);
2018 /* copy samples for viewing in editor window */
2019 static void update_sample_display(VideoState *is, short *samples, int samples_size)
2023 size = samples_size / sizeof(short);
2025 len = SAMPLE_ARRAY_SIZE - is->sample_array_index;
2028 memcpy(is->sample_array + is->sample_array_index, samples, len * sizeof(short));
2030 is->sample_array_index += len;
2031 if (is->sample_array_index >= SAMPLE_ARRAY_SIZE)
2032 is->sample_array_index = 0;
2037 /* return the wanted number of samples to get better sync if sync_type is video
2038 * or external master clock */
2039 static int synchronize_audio(VideoState *is, int nb_samples)
2041 int wanted_nb_samples = nb_samples;
2043 /* if not master, then we try to remove or add samples to correct the clock */
2044 if (get_master_sync_type(is) != AV_SYNC_AUDIO_MASTER) {
2045 double diff, avg_diff;
2046 int min_nb_samples, max_nb_samples;
2048 diff = get_audio_clock(is) - get_master_clock(is);
2050 if (!isnan(diff) && fabs(diff) < AV_NOSYNC_THRESHOLD) {
2051 is->audio_diff_cum = diff + is->audio_diff_avg_coef * is->audio_diff_cum;
2052 if (is->audio_diff_avg_count < AUDIO_DIFF_AVG_NB) {
2053 /* not enough measures to have a correct estimate */
2054 is->audio_diff_avg_count++;
2056 /* estimate the A-V difference */
2057 avg_diff = is->audio_diff_cum * (1.0 - is->audio_diff_avg_coef);
2059 if (fabs(avg_diff) >= is->audio_diff_threshold) {
2060 wanted_nb_samples = nb_samples + (int)(diff * is->audio_src.freq);
2061 min_nb_samples = ((nb_samples * (100 - SAMPLE_CORRECTION_PERCENT_MAX) / 100));
2062 max_nb_samples = ((nb_samples * (100 + SAMPLE_CORRECTION_PERCENT_MAX) / 100));
2063 wanted_nb_samples = FFMIN(FFMAX(wanted_nb_samples, min_nb_samples), max_nb_samples);
2065 av_dlog(NULL, "diff=%f adiff=%f sample_diff=%d apts=%0.3f %f\n",
2066 diff, avg_diff, wanted_nb_samples - nb_samples,
2067 is->audio_clock, is->audio_diff_threshold);
2070 /* too big difference : may be initial PTS errors, so
2072 is->audio_diff_avg_count = 0;
2073 is->audio_diff_cum = 0;
2077 return wanted_nb_samples;
2081 * Decode one audio frame and return its uncompressed size.
2083 * The processed audio frame is decoded, converted if required, and
2084 * stored in is->audio_buf, with size in bytes given by the return
2087 static int audio_decode_frame(VideoState *is)
2089 AVPacket *pkt_temp = &is->audio_pkt_temp;
2090 AVPacket *pkt = &is->audio_pkt;
2091 AVCodecContext *dec = is->audio_st->codec;
2092 int len1, len2, data_size, resampled_data_size;
2093 int64_t dec_channel_layout;
2095 av_unused double audio_clock0;
2097 int flush_complete = 0;
2098 int wanted_nb_samples;
2101 /* NOTE: the audio packet can contain several frames */
2102 while (pkt_temp->size > 0 || (!pkt_temp->data && new_packet)) {
2104 if (!(is->frame = avcodec_alloc_frame()))
2105 return AVERROR(ENOMEM);
2107 avcodec_get_frame_defaults(is->frame);
2109 if (is->audioq.serial != is->audio_pkt_temp_serial)
2118 len1 = avcodec_decode_audio4(dec, is->frame, &got_frame, pkt_temp);
2120 /* if error, we skip the frame */
2125 pkt_temp->data += len1;
2126 pkt_temp->size -= len1;
2129 /* stop sending empty packets if the decoder is finished */
2130 if (!pkt_temp->data && dec->codec->capabilities & CODEC_CAP_DELAY)
2134 data_size = av_samples_get_buffer_size(NULL, av_frame_get_channels(is->frame),
2135 is->frame->nb_samples,
2136 is->frame->format, 1);
2138 dec_channel_layout =
2139 (is->frame->channel_layout && av_frame_get_channels(is->frame) == av_get_channel_layout_nb_channels(is->frame->channel_layout)) ?
2140 is->frame->channel_layout : av_get_default_channel_layout(av_frame_get_channels(is->frame));
2141 wanted_nb_samples = synchronize_audio(is, is->frame->nb_samples);
2143 if (is->frame->format != is->audio_src.fmt ||
2144 dec_channel_layout != is->audio_src.channel_layout ||
2145 is->frame->sample_rate != is->audio_src.freq ||
2146 (wanted_nb_samples != is->frame->nb_samples && !is->swr_ctx)) {
2147 swr_free(&is->swr_ctx);
2148 is->swr_ctx = swr_alloc_set_opts(NULL,
2149 is->audio_tgt.channel_layout, is->audio_tgt.fmt, is->audio_tgt.freq,
2150 dec_channel_layout, is->frame->format, is->frame->sample_rate,
2152 if (!is->swr_ctx || swr_init(is->swr_ctx) < 0) {
2153 fprintf(stderr, "Cannot create sample rate converter for conversion of %d Hz %s %d channels to %d Hz %s %d channels!\n",
2154 is->frame->sample_rate, av_get_sample_fmt_name(is->frame->format), av_frame_get_channels(is->frame),
2155 is->audio_tgt.freq, av_get_sample_fmt_name(is->audio_tgt.fmt), is->audio_tgt.channels);
2158 is->audio_src.channel_layout = dec_channel_layout;
2159 is->audio_src.channels = av_frame_get_channels(is->frame);
2160 is->audio_src.freq = is->frame->sample_rate;
2161 is->audio_src.fmt = is->frame->format;
2165 const uint8_t **in = (const uint8_t **)is->frame->extended_data;
2166 uint8_t **out = &is->audio_buf1;
2167 int out_count = (int64_t)wanted_nb_samples * is->audio_tgt.freq / is->frame->sample_rate + 256;
2168 int out_size = av_samples_get_buffer_size(NULL, is->audio_tgt.channels, out_count, is->audio_tgt.fmt, 0);
2169 if (wanted_nb_samples != is->frame->nb_samples) {
2170 if (swr_set_compensation(is->swr_ctx, (wanted_nb_samples - is->frame->nb_samples) * is->audio_tgt.freq / is->frame->sample_rate,
2171 wanted_nb_samples * is->audio_tgt.freq / is->frame->sample_rate) < 0) {
2172 fprintf(stderr, "swr_set_compensation() failed\n");
2176 av_fast_malloc(&is->audio_buf1, &is->audio_buf1_size, out_size);
2177 if (!is->audio_buf1)
2178 return AVERROR(ENOMEM);
2179 len2 = swr_convert(is->swr_ctx, out, out_count, in, is->frame->nb_samples);
2181 fprintf(stderr, "swr_convert() failed\n");
2184 if (len2 == out_count) {
2185 fprintf(stderr, "warning: audio buffer is probably too small\n");
2186 swr_init(is->swr_ctx);
2188 is->audio_buf = is->audio_buf1;
2189 resampled_data_size = len2 * is->audio_tgt.channels * av_get_bytes_per_sample(is->audio_tgt.fmt);
2191 is->audio_buf = is->frame->data[0];
2192 resampled_data_size = data_size;
2195 audio_clock0 = is->audio_clock;
2196 is->audio_clock += (double)data_size /
2197 (av_frame_get_channels(is->frame) * is->frame->sample_rate * av_get_bytes_per_sample(is->frame->format));
2200 static double last_clock;
2201 printf("audio: delay=%0.3f clock=%0.3f clock0=%0.3f\n",
2202 is->audio_clock - last_clock,
2203 is->audio_clock, audio_clock0);
2204 last_clock = is->audio_clock;
2207 return resampled_data_size;
2210 /* free the current packet */
2212 av_free_packet(pkt);
2213 memset(pkt_temp, 0, sizeof(*pkt_temp));
2215 if (is->audioq.abort_request) {
2219 if (is->audioq.nb_packets == 0)
2220 SDL_CondSignal(is->continue_read_thread);
2222 /* read next packet */
2223 if ((new_packet = packet_queue_get(&is->audioq, pkt, 1, &is->audio_pkt_temp_serial)) < 0)
2226 if (pkt->data == flush_pkt.data) {
2227 avcodec_flush_buffers(dec);
2233 /* if update the audio clock with the pts */
2234 if (pkt->pts != AV_NOPTS_VALUE) {
2235 is->audio_clock = av_q2d(is->audio_st->time_base)*pkt->pts;
2236 is->audio_clock_serial = is->audio_pkt_temp_serial;
2241 /* prepare a new audio buffer */
2242 static void sdl_audio_callback(void *opaque, Uint8 *stream, int len)
2244 VideoState *is = opaque;
2245 int audio_size, len1;
2247 int frame_size = av_samples_get_buffer_size(NULL, is->audio_tgt.channels, 1, is->audio_tgt.fmt, 1);
2249 audio_callback_time = av_gettime();
2252 if (is->audio_buf_index >= is->audio_buf_size) {
2253 audio_size = audio_decode_frame(is);
2254 if (audio_size < 0) {
2255 /* if error, just output silence */
2256 is->audio_buf = is->silence_buf;
2257 is->audio_buf_size = sizeof(is->silence_buf) / frame_size * frame_size;
2259 if (is->show_mode != SHOW_MODE_VIDEO)
2260 update_sample_display(is, (int16_t *)is->audio_buf, audio_size);
2261 is->audio_buf_size = audio_size;
2263 is->audio_buf_index = 0;
2265 len1 = is->audio_buf_size - is->audio_buf_index;
2268 memcpy(stream, (uint8_t *)is->audio_buf + is->audio_buf_index, len1);
2271 is->audio_buf_index += len1;
2273 bytes_per_sec = is->audio_tgt.freq * is->audio_tgt.channels * av_get_bytes_per_sample(is->audio_tgt.fmt);
2274 is->audio_write_buf_size = is->audio_buf_size - is->audio_buf_index;
2275 /* Let's assume the audio driver that is used by SDL has two periods. */
2276 is->audio_current_pts = is->audio_clock - (double)(2 * is->audio_hw_buf_size + is->audio_write_buf_size) / bytes_per_sec;
2277 is->audio_current_pts_drift = is->audio_current_pts - audio_callback_time / 1000000.0;
2278 if (is->audioq.serial == is->audio_clock_serial)
2279 check_external_clock_sync(is, is->audio_current_pts);
2282 static int audio_open(void *opaque, int64_t wanted_channel_layout, int wanted_nb_channels, int wanted_sample_rate, struct AudioParams *audio_hw_params)
2284 SDL_AudioSpec wanted_spec, spec;
2286 const int next_nb_channels[] = {0, 0, 1, 6, 2, 6, 4, 6};
2288 env = SDL_getenv("SDL_AUDIO_CHANNELS");
2290 wanted_nb_channels = atoi(env);
2291 wanted_channel_layout = av_get_default_channel_layout(wanted_nb_channels);
2293 if (!wanted_channel_layout || wanted_nb_channels != av_get_channel_layout_nb_channels(wanted_channel_layout)) {
2294 wanted_channel_layout = av_get_default_channel_layout(wanted_nb_channels);
2295 wanted_channel_layout &= ~AV_CH_LAYOUT_STEREO_DOWNMIX;
2297 wanted_spec.channels = av_get_channel_layout_nb_channels(wanted_channel_layout);
2298 wanted_spec.freq = wanted_sample_rate;
2299 if (wanted_spec.freq <= 0 || wanted_spec.channels <= 0) {
2300 fprintf(stderr, "Invalid sample rate or channel count!\n");
2303 wanted_spec.format = AUDIO_S16SYS;
2304 wanted_spec.silence = 0;
2305 wanted_spec.samples = SDL_AUDIO_BUFFER_SIZE;
2306 wanted_spec.callback = sdl_audio_callback;
2307 wanted_spec.userdata = opaque;
2308 while (SDL_OpenAudio(&wanted_spec, &spec) < 0) {
2309 fprintf(stderr, "SDL_OpenAudio (%d channels): %s\n", wanted_spec.channels, SDL_GetError());
2310 wanted_spec.channels = next_nb_channels[FFMIN(7, wanted_spec.channels)];
2311 if (!wanted_spec.channels) {
2312 fprintf(stderr, "No more channel combinations to try, audio open failed\n");
2315 wanted_channel_layout = av_get_default_channel_layout(wanted_spec.channels);
2317 if (spec.format != AUDIO_S16SYS) {
2318 fprintf(stderr, "SDL advised audio format %d is not supported!\n", spec.format);
2321 if (spec.channels != wanted_spec.channels) {
2322 wanted_channel_layout = av_get_default_channel_layout(spec.channels);
2323 if (!wanted_channel_layout) {
2324 fprintf(stderr, "SDL advised channel count %d is not supported!\n", spec.channels);
2329 audio_hw_params->fmt = AV_SAMPLE_FMT_S16;
2330 audio_hw_params->freq = spec.freq;
2331 audio_hw_params->channel_layout = wanted_channel_layout;
2332 audio_hw_params->channels = spec.channels;
2336 /* open a given stream. Return 0 if OK */
2337 static int stream_component_open(VideoState *is, int stream_index)
2339 AVFormatContext *ic = is->ic;
2340 AVCodecContext *avctx;
2342 const char *forced_codec_name = NULL;
2344 AVDictionaryEntry *t = NULL;
2346 if (stream_index < 0 || stream_index >= ic->nb_streams)
2348 avctx = ic->streams[stream_index]->codec;
2350 codec = avcodec_find_decoder(avctx->codec_id);
2352 switch(avctx->codec_type){
2353 case AVMEDIA_TYPE_AUDIO : is->last_audio_stream = stream_index; forced_codec_name = audio_codec_name; break;
2354 case AVMEDIA_TYPE_SUBTITLE: is->last_subtitle_stream = stream_index; forced_codec_name = subtitle_codec_name; break;
2355 case AVMEDIA_TYPE_VIDEO : is->last_video_stream = stream_index; forced_codec_name = video_codec_name; break;
2357 if (forced_codec_name)
2358 codec = avcodec_find_decoder_by_name(forced_codec_name);
2360 if (forced_codec_name) fprintf(stderr, "No codec could be found with name '%s'\n", forced_codec_name);
2361 else fprintf(stderr, "No codec could be found with id %d\n", avctx->codec_id);
2365 avctx->codec_id = codec->id;
2366 avctx->workaround_bugs = workaround_bugs;
2367 avctx->lowres = lowres;
2368 if(avctx->lowres > codec->max_lowres){
2369 av_log(avctx, AV_LOG_WARNING, "The maximum value for lowres supported by the decoder is %d\n",
2371 avctx->lowres= codec->max_lowres;
2373 avctx->idct_algo = idct;
2374 avctx->skip_frame = skip_frame;
2375 avctx->skip_idct = skip_idct;
2376 avctx->skip_loop_filter = skip_loop_filter;
2377 avctx->error_concealment = error_concealment;
2379 if(avctx->lowres) avctx->flags |= CODEC_FLAG_EMU_EDGE;
2380 if (fast) avctx->flags2 |= CODEC_FLAG2_FAST;
2381 if(codec->capabilities & CODEC_CAP_DR1)
2382 avctx->flags |= CODEC_FLAG_EMU_EDGE;
2384 opts = filter_codec_opts(codec_opts, avctx->codec_id, ic, ic->streams[stream_index], codec);
2385 if (!av_dict_get(opts, "threads", NULL, 0))
2386 av_dict_set(&opts, "threads", "auto", 0);
2387 if (avcodec_open2(avctx, codec, &opts) < 0)
2389 if ((t = av_dict_get(opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
2390 av_log(NULL, AV_LOG_ERROR, "Option %s not found.\n", t->key);
2391 return AVERROR_OPTION_NOT_FOUND;
2394 /* prepare audio output */
2395 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
2396 int audio_hw_buf_size = audio_open(is, avctx->channel_layout, avctx->channels, avctx->sample_rate, &is->audio_src);
2397 if (audio_hw_buf_size < 0)
2399 is->audio_hw_buf_size = audio_hw_buf_size;
2400 is->audio_tgt = is->audio_src;
2403 ic->streams[stream_index]->discard = AVDISCARD_DEFAULT;
2404 switch (avctx->codec_type) {
2405 case AVMEDIA_TYPE_AUDIO:
2406 is->audio_stream = stream_index;
2407 is->audio_st = ic->streams[stream_index];
2408 is->audio_buf_size = 0;
2409 is->audio_buf_index = 0;
2411 /* init averaging filter */
2412 is->audio_diff_avg_coef = exp(log(0.01) / AUDIO_DIFF_AVG_NB);
2413 is->audio_diff_avg_count = 0;
2414 /* since we do not have a precise anough audio fifo fullness,
2415 we correct audio sync only if larger than this threshold */
2416 is->audio_diff_threshold = 2.0 * is->audio_hw_buf_size / av_samples_get_buffer_size(NULL, is->audio_tgt.channels, is->audio_tgt.freq, is->audio_tgt.fmt, 1);
2418 memset(&is->audio_pkt, 0, sizeof(is->audio_pkt));
2419 memset(&is->audio_pkt_temp, 0, sizeof(is->audio_pkt_temp));
2420 packet_queue_start(&is->audioq);
2423 case AVMEDIA_TYPE_VIDEO:
2424 is->video_stream = stream_index;
2425 is->video_st = ic->streams[stream_index];
2427 packet_queue_start(&is->videoq);
2428 is->video_tid = SDL_CreateThread(video_thread, is);
2430 case AVMEDIA_TYPE_SUBTITLE:
2431 is->subtitle_stream = stream_index;
2432 is->subtitle_st = ic->streams[stream_index];
2433 packet_queue_start(&is->subtitleq);
2435 is->subtitle_tid = SDL_CreateThread(subtitle_thread, is);
2443 static void stream_component_close(VideoState *is, int stream_index)
2445 AVFormatContext *ic = is->ic;
2446 AVCodecContext *avctx;
2448 if (stream_index < 0 || stream_index >= ic->nb_streams)
2450 avctx = ic->streams[stream_index]->codec;
2452 switch (avctx->codec_type) {
2453 case AVMEDIA_TYPE_AUDIO:
2454 packet_queue_abort(&is->audioq);
2458 packet_queue_flush(&is->audioq);
2459 av_free_packet(&is->audio_pkt);
2460 swr_free(&is->swr_ctx);
2461 av_freep(&is->audio_buf1);
2462 is->audio_buf1_size = 0;
2463 is->audio_buf = NULL;
2464 avcodec_free_frame(&is->frame);
2467 av_rdft_end(is->rdft);
2468 av_freep(&is->rdft_data);
2473 case AVMEDIA_TYPE_VIDEO:
2474 packet_queue_abort(&is->videoq);
2476 /* note: we also signal this mutex to make sure we deblock the
2477 video thread in all cases */
2478 SDL_LockMutex(is->pictq_mutex);
2479 SDL_CondSignal(is->pictq_cond);
2480 SDL_UnlockMutex(is->pictq_mutex);
2482 SDL_WaitThread(is->video_tid, NULL);
2484 packet_queue_flush(&is->videoq);
2486 case AVMEDIA_TYPE_SUBTITLE:
2487 packet_queue_abort(&is->subtitleq);
2489 /* note: we also signal this mutex to make sure we deblock the
2490 video thread in all cases */
2491 SDL_LockMutex(is->subpq_mutex);
2492 is->subtitle_stream_changed = 1;
2494 SDL_CondSignal(is->subpq_cond);
2495 SDL_UnlockMutex(is->subpq_mutex);
2497 SDL_WaitThread(is->subtitle_tid, NULL);
2499 packet_queue_flush(&is->subtitleq);
2505 ic->streams[stream_index]->discard = AVDISCARD_ALL;
2506 avcodec_close(avctx);
2508 free_buffer_pool(&is->buffer_pool);
2510 switch (avctx->codec_type) {
2511 case AVMEDIA_TYPE_AUDIO:
2512 is->audio_st = NULL;
2513 is->audio_stream = -1;
2515 case AVMEDIA_TYPE_VIDEO:
2516 is->video_st = NULL;
2517 is->video_stream = -1;
2519 case AVMEDIA_TYPE_SUBTITLE:
2520 is->subtitle_st = NULL;
2521 is->subtitle_stream = -1;
2528 static int decode_interrupt_cb(void *ctx)
2530 VideoState *is = ctx;
2531 return is->abort_request;
2534 static int is_realtime(AVFormatContext *s)
2536 if( !strcmp(s->iformat->name, "rtp")
2537 || !strcmp(s->iformat->name, "rtsp")
2538 || !strcmp(s->iformat->name, "sdp")
2542 if(s->pb && ( !strncmp(s->filename, "rtp:", 4)
2543 || !strncmp(s->filename, "udp:", 4)
2550 /* this thread gets the stream from the disk or the network */
2551 static int read_thread(void *arg)
2553 VideoState *is = arg;
2554 AVFormatContext *ic = NULL;
2556 int st_index[AVMEDIA_TYPE_NB];
2557 AVPacket pkt1, *pkt = &pkt1;
2559 int pkt_in_play_range = 0;
2560 AVDictionaryEntry *t;
2561 AVDictionary **opts;
2562 int orig_nb_streams;
2563 SDL_mutex *wait_mutex = SDL_CreateMutex();
2565 memset(st_index, -1, sizeof(st_index));
2566 is->last_video_stream = is->video_stream = -1;
2567 is->last_audio_stream = is->audio_stream = -1;
2568 is->last_subtitle_stream = is->subtitle_stream = -1;
2570 ic = avformat_alloc_context();
2571 ic->interrupt_callback.callback = decode_interrupt_cb;
2572 ic->interrupt_callback.opaque = is;
2573 err = avformat_open_input(&ic, is->filename, is->iformat, &format_opts);
2575 print_error(is->filename, err);
2579 if ((t = av_dict_get(format_opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
2580 av_log(NULL, AV_LOG_ERROR, "Option %s not found.\n", t->key);
2581 ret = AVERROR_OPTION_NOT_FOUND;
2587 ic->flags |= AVFMT_FLAG_GENPTS;
2589 opts = setup_find_stream_info_opts(ic, codec_opts);
2590 orig_nb_streams = ic->nb_streams;
2592 err = avformat_find_stream_info(ic, opts);
2594 fprintf(stderr, "%s: could not find codec parameters\n", is->filename);
2598 for (i = 0; i < orig_nb_streams; i++)
2599 av_dict_free(&opts[i]);
2603 ic->pb->eof_reached = 0; // FIXME hack, ffplay maybe should not use url_feof() to test for the end
2605 if (seek_by_bytes < 0)
2606 seek_by_bytes = !!(ic->iformat->flags & AVFMT_TS_DISCONT);
2608 is->max_frame_duration = (ic->iformat->flags & AVFMT_TS_DISCONT) ? 10.0 : 3600.0;
2610 /* if seeking requested, we execute it */
2611 if (start_time != AV_NOPTS_VALUE) {
2614 timestamp = start_time;
2615 /* add the stream start time */
2616 if (ic->start_time != AV_NOPTS_VALUE)
2617 timestamp += ic->start_time;
2618 ret = avformat_seek_file(ic, -1, INT64_MIN, timestamp, INT64_MAX, 0);
2620 fprintf(stderr, "%s: could not seek to position %0.3f\n",
2621 is->filename, (double)timestamp / AV_TIME_BASE);
2625 is->realtime = is_realtime(ic);
2627 for (i = 0; i < ic->nb_streams; i++)
2628 ic->streams[i]->discard = AVDISCARD_ALL;
2630 st_index[AVMEDIA_TYPE_VIDEO] =
2631 av_find_best_stream(ic, AVMEDIA_TYPE_VIDEO,
2632 wanted_stream[AVMEDIA_TYPE_VIDEO], -1, NULL, 0);
2634 st_index[AVMEDIA_TYPE_AUDIO] =
2635 av_find_best_stream(ic, AVMEDIA_TYPE_AUDIO,
2636 wanted_stream[AVMEDIA_TYPE_AUDIO],
2637 st_index[AVMEDIA_TYPE_VIDEO],
2639 if (!video_disable && !subtitle_disable)
2640 st_index[AVMEDIA_TYPE_SUBTITLE] =
2641 av_find_best_stream(ic, AVMEDIA_TYPE_SUBTITLE,
2642 wanted_stream[AVMEDIA_TYPE_SUBTITLE],
2643 (st_index[AVMEDIA_TYPE_AUDIO] >= 0 ?
2644 st_index[AVMEDIA_TYPE_AUDIO] :
2645 st_index[AVMEDIA_TYPE_VIDEO]),
2648 av_dump_format(ic, 0, is->filename, 0);
2651 is->show_mode = show_mode;
2653 /* open the streams */
2654 if (st_index[AVMEDIA_TYPE_AUDIO] >= 0) {
2655 stream_component_open(is, st_index[AVMEDIA_TYPE_AUDIO]);
2659 if (st_index[AVMEDIA_TYPE_VIDEO] >= 0) {
2660 ret = stream_component_open(is, st_index[AVMEDIA_TYPE_VIDEO]);
2662 if (is->show_mode == SHOW_MODE_NONE)
2663 is->show_mode = ret >= 0 ? SHOW_MODE_VIDEO : SHOW_MODE_RDFT;
2665 if (st_index[AVMEDIA_TYPE_SUBTITLE] >= 0) {
2666 stream_component_open(is, st_index[AVMEDIA_TYPE_SUBTITLE]);
2669 if (is->video_stream < 0 && is->audio_stream < 0) {
2670 fprintf(stderr, "%s: could not open codecs\n", is->filename);
2675 if (infinite_buffer < 0 && is->realtime)
2676 infinite_buffer = 1;
2679 if (is->abort_request)
2681 if (is->paused != is->last_paused) {
2682 is->last_paused = is->paused;
2684 is->read_pause_return = av_read_pause(ic);
2688 #if CONFIG_RTSP_DEMUXER || CONFIG_MMSH_PROTOCOL
2690 (!strcmp(ic->iformat->name, "rtsp") ||
2691 (ic->pb && !strncmp(input_filename, "mmsh:", 5)))) {
2692 /* wait 10 ms to avoid trying to get another packet */
2699 int64_t seek_target = is->seek_pos;
2700 int64_t seek_min = is->seek_rel > 0 ? seek_target - is->seek_rel + 2: INT64_MIN;
2701 int64_t seek_max = is->seek_rel < 0 ? seek_target - is->seek_rel - 2: INT64_MAX;
2702 // FIXME the +-2 is due to rounding being not done in the correct direction in generation
2703 // of the seek_pos/seek_rel variables
2705 ret = avformat_seek_file(is->ic, -1, seek_min, seek_target, seek_max, is->seek_flags);
2707 fprintf(stderr, "%s: error while seeking\n", is->ic->filename);
2709 if (is->audio_stream >= 0) {
2710 packet_queue_flush(&is->audioq);
2711 packet_queue_put(&is->audioq, &flush_pkt);
2713 if (is->subtitle_stream >= 0) {
2714 packet_queue_flush(&is->subtitleq);
2715 packet_queue_put(&is->subtitleq, &flush_pkt);
2717 if (is->video_stream >= 0) {
2718 packet_queue_flush(&is->videoq);
2719 packet_queue_put(&is->videoq, &flush_pkt);
2721 if (is->seek_flags & AVSEEK_FLAG_BYTE) {
2722 update_external_clock_pts(is, NAN);
2724 update_external_clock_pts(is, seek_target / (double)AV_TIME_BASE);
2730 step_to_next_frame(is);
2732 if (is->queue_attachments_req) {
2733 avformat_queue_attached_pictures(ic);
2734 is->queue_attachments_req = 0;
2737 /* if the queue are full, no need to read more */
2738 if (infinite_buffer<1 &&
2739 (is->audioq.size + is->videoq.size + is->subtitleq.size > MAX_QUEUE_SIZE
2740 || ( (is->audioq .nb_packets > MIN_FRAMES || is->audio_stream < 0 || is->audioq.abort_request)
2741 && (is->videoq .nb_packets > MIN_FRAMES || is->video_stream < 0 || is->videoq.abort_request)
2742 && (is->subtitleq.nb_packets > MIN_FRAMES || is->subtitle_stream < 0 || is->subtitleq.abort_request)))) {
2744 SDL_LockMutex(wait_mutex);
2745 SDL_CondWaitTimeout(is->continue_read_thread, wait_mutex, 10);
2746 SDL_UnlockMutex(wait_mutex);
2750 if (is->video_stream >= 0) {
2751 av_init_packet(pkt);
2754 pkt->stream_index = is->video_stream;
2755 packet_queue_put(&is->videoq, pkt);
2757 if (is->audio_stream >= 0 &&
2758 is->audio_st->codec->codec->capabilities & CODEC_CAP_DELAY) {
2759 av_init_packet(pkt);
2762 pkt->stream_index = is->audio_stream;
2763 packet_queue_put(&is->audioq, pkt);
2766 if (is->audioq.size + is->videoq.size + is->subtitleq.size == 0) {
2767 if (loop != 1 && (!loop || --loop)) {
2768 stream_seek(is, start_time != AV_NOPTS_VALUE ? start_time : 0, 0, 0);
2769 } else if (autoexit) {
2777 ret = av_read_frame(ic, pkt);
2779 if (ret == AVERROR_EOF || url_feof(ic->pb))
2781 if (ic->pb && ic->pb->error)
2783 SDL_LockMutex(wait_mutex);
2784 SDL_CondWaitTimeout(is->continue_read_thread, wait_mutex, 10);
2785 SDL_UnlockMutex(wait_mutex);
2788 /* check if packet is in play range specified by user, then queue, otherwise discard */
2789 pkt_in_play_range = duration == AV_NOPTS_VALUE ||
2790 (pkt->pts - ic->streams[pkt->stream_index]->start_time) *
2791 av_q2d(ic->streams[pkt->stream_index]->time_base) -
2792 (double)(start_time != AV_NOPTS_VALUE ? start_time : 0) / 1000000
2793 <= ((double)duration / 1000000);
2794 if (pkt->stream_index == is->audio_stream && pkt_in_play_range) {
2795 packet_queue_put(&is->audioq, pkt);
2796 } else if (pkt->stream_index == is->video_stream && pkt_in_play_range) {
2797 packet_queue_put(&is->videoq, pkt);
2798 } else if (pkt->stream_index == is->subtitle_stream && pkt_in_play_range) {
2799 packet_queue_put(&is->subtitleq, pkt);
2801 av_free_packet(pkt);
2804 /* wait until the end */
2805 while (!is->abort_request) {
2811 /* close each stream */
2812 if (is->audio_stream >= 0)
2813 stream_component_close(is, is->audio_stream);
2814 if (is->video_stream >= 0)
2815 stream_component_close(is, is->video_stream);
2816 if (is->subtitle_stream >= 0)
2817 stream_component_close(is, is->subtitle_stream);
2819 avformat_close_input(&is->ic);
2825 event.type = FF_QUIT_EVENT;
2826 event.user.data1 = is;
2827 SDL_PushEvent(&event);
2829 SDL_DestroyMutex(wait_mutex);
2833 static VideoState *stream_open(const char *filename, AVInputFormat *iformat)
2837 is = av_mallocz(sizeof(VideoState));
2840 av_strlcpy(is->filename, filename, sizeof(is->filename));
2841 is->iformat = iformat;
2845 /* start video display */
2846 is->pictq_mutex = SDL_CreateMutex();
2847 is->pictq_cond = SDL_CreateCond();
2849 is->subpq_mutex = SDL_CreateMutex();
2850 is->subpq_cond = SDL_CreateCond();
2852 packet_queue_init(&is->videoq);
2853 packet_queue_init(&is->audioq);
2854 packet_queue_init(&is->subtitleq);
2856 is->continue_read_thread = SDL_CreateCond();
2858 update_external_clock_pts(is, NAN);
2859 update_external_clock_speed(is, 1.0);
2860 is->audio_current_pts_drift = -av_gettime() / 1000000.0;
2861 is->video_current_pts_drift = is->audio_current_pts_drift;
2862 is->audio_clock_serial = -1;
2863 is->video_clock_serial = -1;
2864 is->av_sync_type = av_sync_type;
2865 is->read_tid = SDL_CreateThread(read_thread, is);
2866 if (!is->read_tid) {
2873 static void stream_cycle_channel(VideoState *is, int codec_type)
2875 AVFormatContext *ic = is->ic;
2876 int start_index, stream_index;
2880 if (codec_type == AVMEDIA_TYPE_VIDEO) {
2881 start_index = is->last_video_stream;
2882 old_index = is->video_stream;
2883 } else if (codec_type == AVMEDIA_TYPE_AUDIO) {
2884 start_index = is->last_audio_stream;
2885 old_index = is->audio_stream;
2887 start_index = is->last_subtitle_stream;
2888 old_index = is->subtitle_stream;
2890 stream_index = start_index;
2892 if (++stream_index >= is->ic->nb_streams)
2894 if (codec_type == AVMEDIA_TYPE_SUBTITLE)
2897 is->last_subtitle_stream = -1;
2900 if (start_index == -1)
2904 if (stream_index == start_index)
2906 st = ic->streams[stream_index];
2907 if (st->codec->codec_type == codec_type) {
2908 /* check that parameters are OK */
2909 switch (codec_type) {
2910 case AVMEDIA_TYPE_AUDIO:
2911 if (st->codec->sample_rate != 0 &&
2912 st->codec->channels != 0)
2915 case AVMEDIA_TYPE_VIDEO:
2916 case AVMEDIA_TYPE_SUBTITLE:
2924 stream_component_close(is, old_index);
2925 stream_component_open(is, stream_index);
2926 if (codec_type == AVMEDIA_TYPE_VIDEO)
2927 is->queue_attachments_req = 1;
2931 static void toggle_full_screen(VideoState *is)
2933 #if defined(__APPLE__) && SDL_VERSION_ATLEAST(1, 2, 14)
2934 /* OS X needs to reallocate the SDL overlays */
2936 for (i = 0; i < VIDEO_PICTURE_QUEUE_SIZE; i++)
2937 is->pictq[i].reallocate = 1;
2939 is_full_screen = !is_full_screen;
2940 video_open(is, 1, NULL);
2943 static void toggle_audio_display(VideoState *is)
2945 int bgcolor = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00);
2946 int next = is->show_mode;
2948 next = (next + 1) % SHOW_MODE_NB;
2949 } while (next != is->show_mode && (next == SHOW_MODE_VIDEO && !is->video_st || next != SHOW_MODE_VIDEO && !is->audio_st));
2950 if (is->show_mode != next) {
2951 fill_rectangle(screen,
2952 is->xleft, is->ytop, is->width, is->height,
2954 is->force_refresh = 1;
2955 is->show_mode = next;
2959 static void refresh_loop_wait_event(VideoState *is, SDL_Event *event) {
2960 double remaining_time = 0.0;
2962 while (!SDL_PeepEvents(event, 1, SDL_GETEVENT, SDL_ALLEVENTS)) {
2963 if (!cursor_hidden && av_gettime() - cursor_last_shown > CURSOR_HIDE_DELAY) {
2967 if (remaining_time > 0.0)
2968 av_usleep((int64_t)(remaining_time * 1000000.0));
2969 remaining_time = REFRESH_RATE;
2970 if (is->show_mode != SHOW_MODE_NONE && (!is->paused || is->force_refresh))
2971 video_refresh(is, &remaining_time);
2976 /* handle an event sent by the GUI */
2977 static void event_loop(VideoState *cur_stream)
2980 double incr, pos, frac;
2984 refresh_loop_wait_event(cur_stream, &event);
2985 switch (event.type) {
2987 if (exit_on_keydown) {
2988 do_exit(cur_stream);
2991 switch (event.key.keysym.sym) {
2994 do_exit(cur_stream);
2997 toggle_full_screen(cur_stream);
2998 cur_stream->force_refresh = 1;
3002 toggle_pause(cur_stream);
3004 case SDLK_s: // S: Step to next frame
3005 step_to_next_frame(cur_stream);
3008 stream_cycle_channel(cur_stream, AVMEDIA_TYPE_AUDIO);
3011 stream_cycle_channel(cur_stream, AVMEDIA_TYPE_VIDEO);
3014 stream_cycle_channel(cur_stream, AVMEDIA_TYPE_SUBTITLE);
3017 toggle_audio_display(cur_stream);
3037 if (seek_by_bytes) {
3038 if (cur_stream->video_stream >= 0 && cur_stream->video_current_pos >= 0) {
3039 pos = cur_stream->video_current_pos;
3040 } else if (cur_stream->audio_stream >= 0 && cur_stream->audio_pkt.pos >= 0) {
3041 pos = cur_stream->audio_pkt.pos;
3043 pos = avio_tell(cur_stream->ic->pb);
3044 if (cur_stream->ic->bit_rate)
3045 incr *= cur_stream->ic->bit_rate / 8.0;
3049 stream_seek(cur_stream, pos, incr, 1);
3051 pos = get_master_clock(cur_stream);
3053 pos = (double)cur_stream->seek_pos / AV_TIME_BASE;
3055 if (cur_stream->ic->start_time != AV_NOPTS_VALUE && pos < cur_stream->ic->start_time / (double)AV_TIME_BASE)
3056 pos = cur_stream->ic->start_time / (double)AV_TIME_BASE;
3057 stream_seek(cur_stream, (int64_t)(pos * AV_TIME_BASE), (int64_t)(incr * AV_TIME_BASE), 0);
3064 case SDL_VIDEOEXPOSE:
3065 cur_stream->force_refresh = 1;
3067 case SDL_MOUSEBUTTONDOWN:
3068 if (exit_on_mousedown) {
3069 do_exit(cur_stream);
3072 case SDL_MOUSEMOTION:
3073 if (cursor_hidden) {
3077 cursor_last_shown = av_gettime();
3078 if (event.type == SDL_MOUSEBUTTONDOWN) {
3081 if (event.motion.state != SDL_PRESSED)
3085 if (seek_by_bytes || cur_stream->ic->duration <= 0) {
3086 uint64_t size = avio_size(cur_stream->ic->pb);
3087 stream_seek(cur_stream, size*x/cur_stream->width, 0, 1);
3091 int tns, thh, tmm, tss;
3092 tns = cur_stream->ic->duration / 1000000LL;
3094 tmm = (tns % 3600) / 60;
3096 frac = x / cur_stream->width;
3099 mm = (ns % 3600) / 60;
3101 fprintf(stderr, "Seek to %2.0f%% (%2d:%02d:%02d) of total duration (%2d:%02d:%02d) \n", frac*100,
3102 hh, mm, ss, thh, tmm, tss);
3103 ts = frac * cur_stream->ic->duration;
3104 if (cur_stream->ic->start_time != AV_NOPTS_VALUE)
3105 ts += cur_stream->ic->start_time;
3106 stream_seek(cur_stream, ts, 0, 0);
3109 case SDL_VIDEORESIZE:
3110 screen = SDL_SetVideoMode(event.resize.w, event.resize.h, 0,
3111 SDL_HWSURFACE|SDL_RESIZABLE|SDL_ASYNCBLIT|SDL_HWACCEL);
3112 screen_width = cur_stream->width = event.resize.w;
3113 screen_height = cur_stream->height = event.resize.h;
3114 cur_stream->force_refresh = 1;
3118 do_exit(cur_stream);
3120 case FF_ALLOC_EVENT:
3121 alloc_picture(event.user.data1);
3129 static int opt_frame_size(void *optctx, const char *opt, const char *arg)
3131 av_log(NULL, AV_LOG_WARNING, "Option -s is deprecated, use -video_size.\n");
3132 return opt_default(NULL, "video_size", arg);
3135 static int opt_width(void *optctx, const char *opt, const char *arg)
3137 screen_width = parse_number_or_die(opt, arg, OPT_INT64, 1, INT_MAX);
3141 static int opt_height(void *optctx, const char *opt, const char *arg)
3143 screen_height = parse_number_or_die(opt, arg, OPT_INT64, 1, INT_MAX);
3147 static int opt_format(void *optctx, const char *opt, const char *arg)
3149 file_iformat = av_find_input_format(arg);
3150 if (!file_iformat) {
3151 fprintf(stderr, "Unknown input format: %s\n", arg);
3152 return AVERROR(EINVAL);
3157 static int opt_frame_pix_fmt(void *optctx, const char *opt, const char *arg)
3159 av_log(NULL, AV_LOG_WARNING, "Option -pix_fmt is deprecated, use -pixel_format.\n");
3160 return opt_default(NULL, "pixel_format", arg);
3163 static int opt_sync(void *optctx, const char *opt, const char *arg)
3165 if (!strcmp(arg, "audio"))
3166 av_sync_type = AV_SYNC_AUDIO_MASTER;
3167 else if (!strcmp(arg, "video"))
3168 av_sync_type = AV_SYNC_VIDEO_MASTER;
3169 else if (!strcmp(arg, "ext"))
3170 av_sync_type = AV_SYNC_EXTERNAL_CLOCK;
3172 fprintf(stderr, "Unknown value for %s: %s\n", opt, arg);
3178 static int opt_seek(void *optctx, const char *opt, const char *arg)
3180 start_time = parse_time_or_die(opt, arg, 1);
3184 static int opt_duration(void *optctx, const char *opt, const char *arg)
3186 duration = parse_time_or_die(opt, arg, 1);
3190 static int opt_show_mode(void *optctx, const char *opt, const char *arg)
3192 show_mode = !strcmp(arg, "video") ? SHOW_MODE_VIDEO :
3193 !strcmp(arg, "waves") ? SHOW_MODE_WAVES :
3194 !strcmp(arg, "rdft" ) ? SHOW_MODE_RDFT :
3195 parse_number_or_die(opt, arg, OPT_INT, 0, SHOW_MODE_NB-1);
3199 static void opt_input_file(void *optctx, const char *filename)
3201 if (input_filename) {
3202 fprintf(stderr, "Argument '%s' provided as input filename, but '%s' was already specified.\n",
3203 filename, input_filename);
3206 if (!strcmp(filename, "-"))
3208 input_filename = filename;
3211 static int opt_codec(void *optctx, const char *opt, const char *arg)
3213 const char *spec = strchr(opt, ':');
3215 fprintf(stderr, "No media specifier was specified in '%s' in option '%s'\n",
3217 return AVERROR(EINVAL);
3221 case 'a' : audio_codec_name = arg; break;
3222 case 's' : subtitle_codec_name = arg; break;
3223 case 'v' : video_codec_name = arg; break;
3225 fprintf(stderr, "Invalid media specifier '%s' in option '%s'\n", spec, opt);
3226 return AVERROR(EINVAL);
3233 static const OptionDef options[] = {
3234 #include "cmdutils_common_opts.h"
3235 { "x", HAS_ARG, { .func_arg = opt_width }, "force displayed width", "width" },
3236 { "y", HAS_ARG, { .func_arg = opt_height }, "force displayed height", "height" },
3237 { "s", HAS_ARG | OPT_VIDEO, { .func_arg = opt_frame_size }, "set frame size (WxH or abbreviation)", "size" },
3238 { "fs", OPT_BOOL, { &is_full_screen }, "force full screen" },
3239 { "an", OPT_BOOL, { &audio_disable }, "disable audio" },
3240 { "vn", OPT_BOOL, { &video_disable }, "disable video" },
3241 { "sn", OPT_BOOL, { &subtitle_disable }, "disable subtitling" },
3242 { "ast", OPT_INT | HAS_ARG | OPT_EXPERT, { &wanted_stream[AVMEDIA_TYPE_AUDIO] }, "select desired audio stream", "stream_number" },
3243 { "vst", OPT_INT | HAS_ARG | OPT_EXPERT, { &wanted_stream[AVMEDIA_TYPE_VIDEO] }, "select desired video stream", "stream_number" },
3244 { "sst", OPT_INT | HAS_ARG | OPT_EXPERT, { &wanted_stream[AVMEDIA_TYPE_SUBTITLE] }, "select desired subtitle stream", "stream_number" },
3245 { "ss", HAS_ARG, { .func_arg = opt_seek }, "seek to a given position in seconds", "pos" },
3246 { "t", HAS_ARG, { .func_arg = opt_duration }, "play \"duration\" seconds of audio/video", "duration" },
3247 { "bytes", OPT_INT | HAS_ARG, { &seek_by_bytes }, "seek by bytes 0=off 1=on -1=auto", "val" },
3248 { "nodisp", OPT_BOOL, { &display_disable }, "disable graphical display" },
3249 { "f", HAS_ARG, { .func_arg = opt_format }, "force format", "fmt" },
3250 { "pix_fmt", HAS_ARG | OPT_EXPERT | OPT_VIDEO, { .func_arg = opt_frame_pix_fmt }, "set pixel format", "format" },
3251 { "stats", OPT_BOOL | OPT_EXPERT, { &show_status }, "show status", "" },
3252 { "bug", OPT_INT | HAS_ARG | OPT_EXPERT, { &workaround_bugs }, "workaround bugs", "" },
3253 { "fast", OPT_BOOL | OPT_EXPERT, { &fast }, "non spec compliant optimizations", "" },
3254 { "genpts", OPT_BOOL | OPT_EXPERT, { &genpts }, "generate pts", "" },
3255 { "drp", OPT_INT | HAS_ARG | OPT_EXPERT, { &decoder_reorder_pts }, "let decoder reorder pts 0=off 1=on -1=auto", ""},
3256 { "lowres", OPT_INT | HAS_ARG | OPT_EXPERT, { &lowres }, "", "" },
3257 { "skiploop", OPT_INT | HAS_ARG | OPT_EXPERT, { &skip_loop_filter }, "", "" },
3258 { "skipframe", OPT_INT | HAS_ARG | OPT_EXPERT, { &skip_frame }, "", "" },
3259 { "skipidct", OPT_INT | HAS_ARG | OPT_EXPERT, { &skip_idct }, "", "" },
3260 { "idct", OPT_INT | HAS_ARG | OPT_EXPERT, { &idct }, "set idct algo", "algo" },
3261 { "ec", OPT_INT | HAS_ARG | OPT_EXPERT, { &error_concealment }, "set error concealment options", "bit_mask" },
3262 { "sync", HAS_ARG | OPT_EXPERT, { .func_arg = opt_sync }, "set audio-video sync. type (type=audio/video/ext)", "type" },
3263 { "autoexit", OPT_BOOL | OPT_EXPERT, { &autoexit }, "exit at the end", "" },
3264 { "exitonkeydown", OPT_BOOL | OPT_EXPERT, { &exit_on_keydown }, "exit on key down", "" },
3265 { "exitonmousedown", OPT_BOOL | OPT_EXPERT, { &exit_on_mousedown }, "exit on mouse down", "" },
3266 { "loop", OPT_INT | HAS_ARG | OPT_EXPERT, { &loop }, "set number of times the playback shall be looped", "loop count" },
3267 { "framedrop", OPT_BOOL | OPT_EXPERT, { &framedrop }, "drop frames when cpu is too slow", "" },
3268 { "infbuf", OPT_BOOL | OPT_EXPERT, { &infinite_buffer }, "don't limit the input buffer size (useful with realtime streams)", "" },
3269 { "window_title", OPT_STRING | HAS_ARG, { &window_title }, "set window title", "window title" },
3271 { "vf", OPT_STRING | HAS_ARG, { &vfilters }, "set video filters", "filter_graph" },
3273 { "rdftspeed", OPT_INT | HAS_ARG| OPT_AUDIO | OPT_EXPERT, { &rdftspeed }, "rdft speed", "msecs" },
3274 { "showmode", HAS_ARG, { .func_arg = opt_show_mode}, "select show mode (0 = video, 1 = waves, 2 = RDFT)", "mode" },
3275 { "default", HAS_ARG | OPT_AUDIO | OPT_VIDEO | OPT_EXPERT, { .func_arg = opt_default }, "generic catch all option", "" },
3276 { "i", OPT_BOOL, { &dummy}, "read specified file", "input_file"},
3277 { "codec", HAS_ARG, { .func_arg = opt_codec}, "force decoder", "decoder_name" },
3278 { "acodec", HAS_ARG | OPT_STRING | OPT_EXPERT, { &audio_codec_name }, "force audio decoder", "decoder_name" },
3279 { "scodec", HAS_ARG | OPT_STRING | OPT_EXPERT, { &subtitle_codec_name }, "force subtitle decoder", "decoder_name" },
3280 { "vcodec", HAS_ARG | OPT_STRING | OPT_EXPERT, { &video_codec_name }, "force video decoder", "decoder_name" },
3284 static void show_usage(void)
3286 av_log(NULL, AV_LOG_INFO, "Simple media player\n");
3287 av_log(NULL, AV_LOG_INFO, "usage: %s [options] input_file\n", program_name);
3288 av_log(NULL, AV_LOG_INFO, "\n");
3291 void show_help_default(const char *opt, const char *arg)
3293 av_log_set_callback(log_callback_help);
3295 show_help_options(options, "Main options:", 0, OPT_EXPERT, 0);
3296 show_help_options(options, "Advanced options:", OPT_EXPERT, 0, 0);
3298 show_help_children(avcodec_get_class(), AV_OPT_FLAG_DECODING_PARAM);
3299 show_help_children(avformat_get_class(), AV_OPT_FLAG_DECODING_PARAM);
3300 #if !CONFIG_AVFILTER
3301 show_help_children(sws_get_class(), AV_OPT_FLAG_ENCODING_PARAM);
3303 show_help_children(avfilter_get_class(), AV_OPT_FLAG_FILTERING_PARAM);
3305 printf("\nWhile playing:\n"
3307 "f toggle full screen\n"
3309 "a cycle audio channel\n"
3310 "v cycle video channel\n"
3311 "t cycle subtitle channel\n"
3312 "w show audio waves\n"
3313 "s activate frame-step mode\n"
3314 "left/right seek backward/forward 10 seconds\n"
3315 "down/up seek backward/forward 1 minute\n"
3316 "page down/page up seek backward/forward 10 minutes\n"
3317 "mouse click seek to percentage in file corresponding to fraction of width\n"
3321 static int lockmgr(void **mtx, enum AVLockOp op)
3324 case AV_LOCK_CREATE:
3325 *mtx = SDL_CreateMutex();
3329 case AV_LOCK_OBTAIN:
3330 return !!SDL_LockMutex(*mtx);
3331 case AV_LOCK_RELEASE:
3332 return !!SDL_UnlockMutex(*mtx);
3333 case AV_LOCK_DESTROY:
3334 SDL_DestroyMutex(*mtx);
3340 /* Called from the main */
3341 int main(int argc, char **argv)
3345 char dummy_videodriver[] = "SDL_VIDEODRIVER=dummy";
3347 av_log_set_flags(AV_LOG_SKIP_REPEATED);
3348 parse_loglevel(argc, argv, options);
3350 /* register all codecs, demux and protocols */
3351 avcodec_register_all();
3353 avdevice_register_all();
3356 avfilter_register_all();
3359 avformat_network_init();
3363 signal(SIGINT , sigterm_handler); /* Interrupt (ANSI). */
3364 signal(SIGTERM, sigterm_handler); /* Termination (ANSI). */
3366 show_banner(argc, argv, options);
3368 parse_options(NULL, argc, argv, options, opt_input_file);
3370 if (!input_filename) {
3372 fprintf(stderr, "An input file must be specified\n");
3373 fprintf(stderr, "Use -h to get full help or, even better, run 'man %s'\n", program_name);
3377 if (display_disable) {
3380 flags = SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER;
3382 flags &= ~SDL_INIT_AUDIO;
3383 if (display_disable)
3384 SDL_putenv(dummy_videodriver); /* For the event queue, we always need a video driver. */
3385 #if !defined(__MINGW32__) && !defined(__APPLE__)
3386 flags |= SDL_INIT_EVENTTHREAD; /* Not supported on Windows or Mac OS X */
3388 if (SDL_Init (flags)) {
3389 fprintf(stderr, "Could not initialize SDL - %s\n", SDL_GetError());
3390 fprintf(stderr, "(Did you set the DISPLAY variable?)\n");
3394 if (!display_disable) {
3395 #if HAVE_SDL_VIDEO_SIZE
3396 const SDL_VideoInfo *vi = SDL_GetVideoInfo();
3397 fs_screen_width = vi->current_w;
3398 fs_screen_height = vi->current_h;
3402 SDL_EventState(SDL_ACTIVEEVENT, SDL_IGNORE);
3403 SDL_EventState(SDL_SYSWMEVENT, SDL_IGNORE);
3404 SDL_EventState(SDL_USEREVENT, SDL_IGNORE);
3406 if (av_lockmgr_register(lockmgr)) {
3407 fprintf(stderr, "Could not initialize lock manager!\n");
3411 av_init_packet(&flush_pkt);
3412 flush_pkt.data = (char *)(intptr_t)"FLUSH";
3414 is = stream_open(input_filename, file_iformat);
3416 fprintf(stderr, "Failed to initialize VideoState!\n");