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 {
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 double audio_diff_cum; /* used for AV difference average computation */
180 double audio_diff_avg_coef;
181 double audio_diff_threshold;
182 int audio_diff_avg_count;
185 int audio_hw_buf_size;
186 uint8_t silence_buf[SDL_AUDIO_BUFFER_SIZE];
189 unsigned int audio_buf_size; /* in bytes */
190 unsigned int audio_buf1_size;
191 int audio_buf_index; /* in bytes */
192 int audio_write_buf_size;
193 AVPacket audio_pkt_temp;
195 int audio_pkt_temp_serial;
196 struct AudioParams audio_src;
197 struct AudioParams audio_tgt;
198 struct SwrContext *swr_ctx;
199 double audio_current_pts;
200 double audio_current_pts_drift;
201 int frame_drops_early;
202 int frame_drops_late;
206 SHOW_MODE_NONE = -1, SHOW_MODE_VIDEO = 0, SHOW_MODE_WAVES, SHOW_MODE_RDFT, SHOW_MODE_NB
208 int16_t sample_array[SAMPLE_ARRAY_SIZE];
209 int sample_array_index;
213 FFTSample *rdft_data;
215 double last_vis_time;
217 SDL_Thread *subtitle_tid;
219 int subtitle_stream_changed;
220 AVStream *subtitle_st;
221 PacketQueue subtitleq;
222 SubPicture subpq[SUBPICTURE_QUEUE_SIZE];
223 int subpq_size, subpq_rindex, subpq_windex;
224 SDL_mutex *subpq_mutex;
225 SDL_cond *subpq_cond;
228 double frame_last_pts;
229 double frame_last_duration;
230 double frame_last_dropped_pts;
231 double frame_last_returned_time;
232 double frame_last_filter_delay;
233 int64_t frame_last_dropped_pos;
237 double video_current_pts; // current displayed pts
238 double video_current_pts_drift; // video_current_pts - time (av_gettime) at which we updated video_current_pts - used to have running video pts
239 int64_t video_current_pos; // current displayed file pos
240 double max_frame_duration; // maximum duration of a frame - above this, we consider the jump a timestamp discontinuity
241 VideoPicture pictq[VIDEO_PICTURE_QUEUE_SIZE];
242 int pictq_size, pictq_rindex, pictq_windex;
243 SDL_mutex *pictq_mutex;
244 SDL_cond *pictq_cond;
246 struct SwsContext *img_convert_ctx;
248 SDL_Rect last_display_rect;
251 int width, height, xleft, ytop;
255 AVFilterContext *in_video_filter; // the first filter in the video chain
256 AVFilterContext *out_video_filter; // the last filter in the video chain
258 FrameBuffer *buffer_pool;
261 int last_video_stream, last_audio_stream, last_subtitle_stream;
263 SDL_cond *continue_read_thread;
266 /* options specified by the user */
267 static AVInputFormat *file_iformat;
268 static const char *input_filename;
269 static const char *window_title;
270 static int fs_screen_width;
271 static int fs_screen_height;
272 static int default_width = 640;
273 static int default_height = 480;
274 static int screen_width = 0;
275 static int screen_height = 0;
276 static int audio_disable;
277 static int video_disable;
278 static int wanted_stream[AVMEDIA_TYPE_NB] = {
279 [AVMEDIA_TYPE_AUDIO] = -1,
280 [AVMEDIA_TYPE_VIDEO] = -1,
281 [AVMEDIA_TYPE_SUBTITLE] = -1,
283 static int seek_by_bytes = -1;
284 static int display_disable;
285 static int show_status = 1;
286 static int av_sync_type = AV_SYNC_AUDIO_MASTER;
287 static int64_t start_time = AV_NOPTS_VALUE;
288 static int64_t duration = AV_NOPTS_VALUE;
289 static int workaround_bugs = 1;
291 static int genpts = 0;
292 static int lowres = 0;
293 static int idct = FF_IDCT_AUTO;
294 static enum AVDiscard skip_frame = AVDISCARD_DEFAULT;
295 static enum AVDiscard skip_idct = AVDISCARD_DEFAULT;
296 static enum AVDiscard skip_loop_filter = AVDISCARD_DEFAULT;
297 static int error_concealment = 3;
298 static int decoder_reorder_pts = -1;
300 static int exit_on_keydown;
301 static int exit_on_mousedown;
303 static int framedrop = -1;
304 static int infinite_buffer = -1;
305 static enum ShowMode show_mode = SHOW_MODE_NONE;
306 static const char *audio_codec_name;
307 static const char *subtitle_codec_name;
308 static const char *video_codec_name;
309 double rdftspeed = 0.02;
310 static int64_t cursor_last_shown;
311 static int cursor_hidden = 0;
313 static char *vfilters = NULL;
316 /* current context */
317 static int is_full_screen;
318 static int64_t audio_callback_time;
320 static AVPacket flush_pkt;
322 #define FF_ALLOC_EVENT (SDL_USEREVENT)
323 #define FF_QUIT_EVENT (SDL_USEREVENT + 2)
325 static SDL_Surface *screen;
327 static int packet_queue_put(PacketQueue *q, AVPacket *pkt);
329 static int packet_queue_put_private(PacketQueue *q, AVPacket *pkt)
331 MyAVPacketList *pkt1;
333 if (q->abort_request)
336 pkt1 = av_malloc(sizeof(MyAVPacketList));
341 if (pkt == &flush_pkt)
343 pkt1->serial = q->serial;
348 q->last_pkt->next = pkt1;
351 q->size += pkt1->pkt.size + sizeof(*pkt1);
352 /* XXX: should duplicate packet data in DV case */
353 SDL_CondSignal(q->cond);
357 static int packet_queue_put(PacketQueue *q, AVPacket *pkt)
361 /* duplicate the packet */
362 if (pkt != &flush_pkt && av_dup_packet(pkt) < 0)
365 SDL_LockMutex(q->mutex);
366 ret = packet_queue_put_private(q, pkt);
367 SDL_UnlockMutex(q->mutex);
369 if (pkt != &flush_pkt && ret < 0)
375 /* packet queue handling */
376 static void packet_queue_init(PacketQueue *q)
378 memset(q, 0, sizeof(PacketQueue));
379 q->mutex = SDL_CreateMutex();
380 q->cond = SDL_CreateCond();
381 q->abort_request = 1;
384 static void packet_queue_flush(PacketQueue *q)
386 MyAVPacketList *pkt, *pkt1;
388 SDL_LockMutex(q->mutex);
389 for (pkt = q->first_pkt; pkt != NULL; pkt = pkt1) {
391 av_free_packet(&pkt->pkt);
398 SDL_UnlockMutex(q->mutex);
401 static void packet_queue_destroy(PacketQueue *q)
403 packet_queue_flush(q);
404 SDL_DestroyMutex(q->mutex);
405 SDL_DestroyCond(q->cond);
408 static void packet_queue_abort(PacketQueue *q)
410 SDL_LockMutex(q->mutex);
412 q->abort_request = 1;
414 SDL_CondSignal(q->cond);
416 SDL_UnlockMutex(q->mutex);
419 static void packet_queue_start(PacketQueue *q)
421 SDL_LockMutex(q->mutex);
422 q->abort_request = 0;
423 packet_queue_put_private(q, &flush_pkt);
424 SDL_UnlockMutex(q->mutex);
427 /* return < 0 if aborted, 0 if no packet and > 0 if packet. */
428 static int packet_queue_get(PacketQueue *q, AVPacket *pkt, int block, int *serial)
430 MyAVPacketList *pkt1;
433 SDL_LockMutex(q->mutex);
436 if (q->abort_request) {
443 q->first_pkt = pkt1->next;
447 q->size -= pkt1->pkt.size + sizeof(*pkt1);
450 *serial = pkt1->serial;
458 SDL_CondWait(q->cond, q->mutex);
461 SDL_UnlockMutex(q->mutex);
465 static inline void fill_rectangle(SDL_Surface *screen,
466 int x, int y, int w, int h, int color, int update)
473 SDL_FillRect(screen, &rect, color);
474 if (update && w > 0 && h > 0)
475 SDL_UpdateRect(screen, x, y, w, h);
478 /* draw only the border of a rectangle */
479 static void fill_border(int xleft, int ytop, int width, int height, int x, int y, int w, int h, int color, int update)
483 /* fill the background */
487 w2 = width - (x + w);
493 h2 = height - (y + h);
496 fill_rectangle(screen,
500 fill_rectangle(screen,
501 xleft + width - w2, ytop,
504 fill_rectangle(screen,
508 fill_rectangle(screen,
509 xleft + w1, ytop + height - h2,
514 #define ALPHA_BLEND(a, oldp, newp, s)\
515 ((((oldp << s) * (255 - (a))) + (newp * (a))) / (255 << s))
517 #define RGBA_IN(r, g, b, a, s)\
519 unsigned int v = ((const uint32_t *)(s))[0];\
520 a = (v >> 24) & 0xff;\
521 r = (v >> 16) & 0xff;\
522 g = (v >> 8) & 0xff;\
526 #define YUVA_IN(y, u, v, a, s, pal)\
528 unsigned int val = ((const uint32_t *)(pal))[*(const uint8_t*)(s)];\
529 a = (val >> 24) & 0xff;\
530 y = (val >> 16) & 0xff;\
531 u = (val >> 8) & 0xff;\
535 #define YUVA_OUT(d, y, u, v, a)\
537 ((uint32_t *)(d))[0] = (a << 24) | (y << 16) | (u << 8) | v;\
543 static void blend_subrect(AVPicture *dst, const AVSubtitleRect *rect, int imgw, int imgh)
545 int wrap, wrap3, width2, skip2;
546 int y, u, v, a, u1, v1, a1, w, h;
547 uint8_t *lum, *cb, *cr;
550 int dstx, dsty, dstw, dsth;
552 dstw = av_clip(rect->w, 0, imgw);
553 dsth = av_clip(rect->h, 0, imgh);
554 dstx = av_clip(rect->x, 0, imgw - dstw);
555 dsty = av_clip(rect->y, 0, imgh - dsth);
556 lum = dst->data[0] + dsty * dst->linesize[0];
557 cb = dst->data[1] + (dsty >> 1) * dst->linesize[1];
558 cr = dst->data[2] + (dsty >> 1) * dst->linesize[2];
560 width2 = ((dstw + 1) >> 1) + (dstx & ~dstw & 1);
562 wrap = dst->linesize[0];
563 wrap3 = rect->pict.linesize[0];
564 p = rect->pict.data[0];
565 pal = (const uint32_t *)rect->pict.data[1]; /* Now in YCrCb! */
573 YUVA_IN(y, u, v, a, p, pal);
574 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
575 cb[0] = ALPHA_BLEND(a >> 2, cb[0], u, 0);
576 cr[0] = ALPHA_BLEND(a >> 2, cr[0], v, 0);
582 for (w = dstw - (dstx & 1); w >= 2; w -= 2) {
583 YUVA_IN(y, u, v, a, p, pal);
587 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
589 YUVA_IN(y, u, v, a, p + BPP, pal);
593 lum[1] = ALPHA_BLEND(a, lum[1], y, 0);
594 cb[0] = ALPHA_BLEND(a1 >> 2, cb[0], u1, 1);
595 cr[0] = ALPHA_BLEND(a1 >> 2, cr[0], v1, 1);
602 YUVA_IN(y, u, v, a, p, pal);
603 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
604 cb[0] = ALPHA_BLEND(a >> 2, cb[0], u, 0);
605 cr[0] = ALPHA_BLEND(a >> 2, cr[0], v, 0);
609 p += wrap3 - dstw * BPP;
610 lum += wrap - dstw - dstx;
611 cb += dst->linesize[1] - width2 - skip2;
612 cr += dst->linesize[2] - width2 - skip2;
614 for (h = dsth - (dsty & 1); h >= 2; h -= 2) {
620 YUVA_IN(y, u, v, a, p, pal);
624 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
627 YUVA_IN(y, u, v, a, p, pal);
631 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
632 cb[0] = ALPHA_BLEND(a1 >> 2, cb[0], u1, 1);
633 cr[0] = ALPHA_BLEND(a1 >> 2, cr[0], v1, 1);
639 for (w = dstw - (dstx & 1); w >= 2; w -= 2) {
640 YUVA_IN(y, u, v, a, p, pal);
644 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
646 YUVA_IN(y, u, v, a, p + BPP, pal);
650 lum[1] = ALPHA_BLEND(a, lum[1], y, 0);
654 YUVA_IN(y, u, v, a, p, pal);
658 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
660 YUVA_IN(y, u, v, a, p + BPP, pal);
664 lum[1] = ALPHA_BLEND(a, lum[1], y, 0);
666 cb[0] = ALPHA_BLEND(a1 >> 2, cb[0], u1, 2);
667 cr[0] = ALPHA_BLEND(a1 >> 2, cr[0], v1, 2);
671 p += -wrap3 + 2 * BPP;
675 YUVA_IN(y, u, v, a, p, pal);
679 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
682 YUVA_IN(y, u, v, a, p, pal);
686 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
687 cb[0] = ALPHA_BLEND(a1 >> 2, cb[0], u1, 1);
688 cr[0] = ALPHA_BLEND(a1 >> 2, cr[0], v1, 1);
694 p += wrap3 + (wrap3 - dstw * BPP);
695 lum += wrap + (wrap - dstw - dstx);
696 cb += dst->linesize[1] - width2 - skip2;
697 cr += dst->linesize[2] - width2 - skip2;
699 /* handle odd height */
706 YUVA_IN(y, u, v, a, p, pal);
707 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
708 cb[0] = ALPHA_BLEND(a >> 2, cb[0], u, 0);
709 cr[0] = ALPHA_BLEND(a >> 2, cr[0], v, 0);
715 for (w = dstw - (dstx & 1); w >= 2; w -= 2) {
716 YUVA_IN(y, u, v, a, p, pal);
720 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
722 YUVA_IN(y, u, v, a, p + BPP, pal);
726 lum[1] = ALPHA_BLEND(a, lum[1], y, 0);
727 cb[0] = ALPHA_BLEND(a1 >> 2, cb[0], u, 1);
728 cr[0] = ALPHA_BLEND(a1 >> 2, cr[0], v, 1);
735 YUVA_IN(y, u, v, a, p, pal);
736 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
737 cb[0] = ALPHA_BLEND(a >> 2, cb[0], u, 0);
738 cr[0] = ALPHA_BLEND(a >> 2, cr[0], v, 0);
743 static void free_subpicture(SubPicture *sp)
745 avsubtitle_free(&sp->sub);
748 static void calculate_display_rect(SDL_Rect *rect, int scr_xleft, int scr_ytop, int scr_width, int scr_height, VideoPicture *vp)
751 int width, height, x, y;
753 if (vp->sample_aspect_ratio.num == 0)
756 aspect_ratio = av_q2d(vp->sample_aspect_ratio);
758 if (aspect_ratio <= 0.0)
760 aspect_ratio *= (float)vp->width / (float)vp->height;
762 /* XXX: we suppose the screen has a 1.0 pixel ratio */
764 width = ((int)rint(height * aspect_ratio)) & ~1;
765 if (width > scr_width) {
767 height = ((int)rint(width / aspect_ratio)) & ~1;
769 x = (scr_width - width) / 2;
770 y = (scr_height - height) / 2;
771 rect->x = scr_xleft + x;
772 rect->y = scr_ytop + y;
773 rect->w = FFMAX(width, 1);
774 rect->h = FFMAX(height, 1);
777 static void video_image_display(VideoState *is)
785 vp = &is->pictq[is->pictq_rindex];
787 if (is->subtitle_st) {
788 if (is->subpq_size > 0) {
789 sp = &is->subpq[is->subpq_rindex];
791 if (vp->pts >= sp->pts + ((float) sp->sub.start_display_time / 1000)) {
792 SDL_LockYUVOverlay (vp->bmp);
794 pict.data[0] = vp->bmp->pixels[0];
795 pict.data[1] = vp->bmp->pixels[2];
796 pict.data[2] = vp->bmp->pixels[1];
798 pict.linesize[0] = vp->bmp->pitches[0];
799 pict.linesize[1] = vp->bmp->pitches[2];
800 pict.linesize[2] = vp->bmp->pitches[1];
802 for (i = 0; i < sp->sub.num_rects; i++)
803 blend_subrect(&pict, sp->sub.rects[i],
804 vp->bmp->w, vp->bmp->h);
806 SDL_UnlockYUVOverlay (vp->bmp);
811 calculate_display_rect(&rect, is->xleft, is->ytop, is->width, is->height, vp);
813 SDL_DisplayYUVOverlay(vp->bmp, &rect);
815 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) {
816 int bgcolor = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00);
817 fill_border(is->xleft, is->ytop, is->width, is->height, rect.x, rect.y, rect.w, rect.h, bgcolor, 1);
818 is->last_display_rect = rect;
823 static inline int compute_mod(int a, int b)
825 return a < 0 ? a%b + b : a%b;
828 static void video_audio_display(VideoState *s)
830 int i, i_start, x, y1, y, ys, delay, n, nb_display_channels;
831 int ch, channels, h, h2, bgcolor, fgcolor;
833 int rdft_bits, nb_freq;
835 for (rdft_bits = 1; (1 << rdft_bits) < 2 * s->height; rdft_bits++)
837 nb_freq = 1 << (rdft_bits - 1);
839 /* compute display index : center on currently output samples */
840 channels = s->audio_tgt.channels;
841 nb_display_channels = channels;
843 int data_used= s->show_mode == SHOW_MODE_WAVES ? s->width : (2*nb_freq);
845 delay = s->audio_write_buf_size;
848 /* to be more precise, we take into account the time spent since
849 the last buffer computation */
850 if (audio_callback_time) {
851 time_diff = av_gettime() - audio_callback_time;
852 delay -= (time_diff * s->audio_tgt.freq) / 1000000;
855 delay += 2 * data_used;
856 if (delay < data_used)
859 i_start= x = compute_mod(s->sample_array_index - delay * channels, SAMPLE_ARRAY_SIZE);
860 if (s->show_mode == SHOW_MODE_WAVES) {
862 for (i = 0; i < 1000; i += channels) {
863 int idx = (SAMPLE_ARRAY_SIZE + x - i) % SAMPLE_ARRAY_SIZE;
864 int a = s->sample_array[idx];
865 int b = s->sample_array[(idx + 4 * channels) % SAMPLE_ARRAY_SIZE];
866 int c = s->sample_array[(idx + 5 * channels) % SAMPLE_ARRAY_SIZE];
867 int d = s->sample_array[(idx + 9 * channels) % SAMPLE_ARRAY_SIZE];
869 if (h < score && (b ^ c) < 0) {
876 s->last_i_start = i_start;
878 i_start = s->last_i_start;
881 bgcolor = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00);
882 if (s->show_mode == SHOW_MODE_WAVES) {
883 fill_rectangle(screen,
884 s->xleft, s->ytop, s->width, s->height,
887 fgcolor = SDL_MapRGB(screen->format, 0xff, 0xff, 0xff);
889 /* total height for one channel */
890 h = s->height / nb_display_channels;
891 /* graph height / 2 */
893 for (ch = 0; ch < nb_display_channels; ch++) {
895 y1 = s->ytop + ch * h + (h / 2); /* position of center line */
896 for (x = 0; x < s->width; x++) {
897 y = (s->sample_array[i] * h2) >> 15;
904 fill_rectangle(screen,
905 s->xleft + x, ys, 1, y,
908 if (i >= SAMPLE_ARRAY_SIZE)
909 i -= SAMPLE_ARRAY_SIZE;
913 fgcolor = SDL_MapRGB(screen->format, 0x00, 0x00, 0xff);
915 for (ch = 1; ch < nb_display_channels; ch++) {
916 y = s->ytop + ch * h;
917 fill_rectangle(screen,
918 s->xleft, y, s->width, 1,
921 SDL_UpdateRect(screen, s->xleft, s->ytop, s->width, s->height);
923 nb_display_channels= FFMIN(nb_display_channels, 2);
924 if (rdft_bits != s->rdft_bits) {
925 av_rdft_end(s->rdft);
926 av_free(s->rdft_data);
927 s->rdft = av_rdft_init(rdft_bits, DFT_R2C);
928 s->rdft_bits = rdft_bits;
929 s->rdft_data = av_malloc(4 * nb_freq * sizeof(*s->rdft_data));
933 for (ch = 0; ch < nb_display_channels; ch++) {
934 data[ch] = s->rdft_data + 2 * nb_freq * ch;
936 for (x = 0; x < 2 * nb_freq; x++) {
937 double w = (x-nb_freq) * (1.0 / nb_freq);
938 data[ch][x] = s->sample_array[i] * (1.0 - w * w);
940 if (i >= SAMPLE_ARRAY_SIZE)
941 i -= SAMPLE_ARRAY_SIZE;
943 av_rdft_calc(s->rdft, data[ch]);
945 // least efficient way to do this, we should of course directly access it but its more than fast enough
946 for (y = 0; y < s->height; y++) {
947 double w = 1 / sqrt(nb_freq);
948 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]));
949 int b = (nb_display_channels == 2 ) ? sqrt(w * sqrt(data[1][2 * y + 0] * data[1][2 * y + 0]
950 + data[1][2 * y + 1] * data[1][2 * y + 1])) : a;
953 fgcolor = SDL_MapRGB(screen->format, a, b, (a + b) / 2);
955 fill_rectangle(screen,
956 s->xpos, s->height-y, 1, 1,
960 SDL_UpdateRect(screen, s->xpos, s->ytop, 1, s->height);
963 if (s->xpos >= s->width)
968 static void stream_close(VideoState *is)
972 /* XXX: use a special url_shutdown call to abort parse cleanly */
973 is->abort_request = 1;
974 SDL_WaitThread(is->read_tid, NULL);
975 packet_queue_destroy(&is->videoq);
976 packet_queue_destroy(&is->audioq);
977 packet_queue_destroy(&is->subtitleq);
979 /* free all pictures */
980 for (i = 0; i < VIDEO_PICTURE_QUEUE_SIZE; i++) {
983 avfilter_unref_bufferp(&vp->picref);
986 SDL_FreeYUVOverlay(vp->bmp);
990 SDL_DestroyMutex(is->pictq_mutex);
991 SDL_DestroyCond(is->pictq_cond);
992 SDL_DestroyMutex(is->subpq_mutex);
993 SDL_DestroyCond(is->subpq_cond);
994 SDL_DestroyCond(is->continue_read_thread);
996 sws_freeContext(is->img_convert_ctx);
1001 static void do_exit(VideoState *is)
1006 av_lockmgr_register(NULL);
1010 av_freep(&vfilters);
1012 avformat_network_deinit();
1016 av_log(NULL, AV_LOG_QUIET, "%s", "");
1020 static void sigterm_handler(int sig)
1025 static int video_open(VideoState *is, int force_set_video_mode, VideoPicture *vp)
1027 int flags = SDL_HWSURFACE | SDL_ASYNCBLIT | SDL_HWACCEL;
1031 if (is_full_screen) flags |= SDL_FULLSCREEN;
1032 else flags |= SDL_RESIZABLE;
1034 if (vp && vp->width) {
1035 calculate_display_rect(&rect, 0, 0, INT_MAX, vp->height, vp);
1036 default_width = rect.w;
1037 default_height = rect.h;
1040 if (is_full_screen && fs_screen_width) {
1041 w = fs_screen_width;
1042 h = fs_screen_height;
1043 } else if (!is_full_screen && screen_width) {
1050 if (screen && is->width == screen->w && screen->w == w
1051 && is->height== screen->h && screen->h == h && !force_set_video_mode)
1053 screen = SDL_SetVideoMode(w, h, 0, flags);
1055 fprintf(stderr, "SDL: could not set video mode - exiting\n");
1059 window_title = input_filename;
1060 SDL_WM_SetCaption(window_title, window_title);
1062 is->width = screen->w;
1063 is->height = screen->h;
1068 /* display the current picture, if any */
1069 static void video_display(VideoState *is)
1072 video_open(is, 0, NULL);
1073 if (is->audio_st && is->show_mode != SHOW_MODE_VIDEO)
1074 video_audio_display(is);
1075 else if (is->video_st)
1076 video_image_display(is);
1079 /* get the current audio clock value */
1080 static double get_audio_clock(VideoState *is)
1083 return is->audio_current_pts;
1085 return is->audio_current_pts_drift + av_gettime() / 1000000.0;
1089 /* get the current video clock value */
1090 static double get_video_clock(VideoState *is)
1093 return is->video_current_pts;
1095 return is->video_current_pts_drift + av_gettime() / 1000000.0;
1099 /* get the current external clock value */
1100 static double get_external_clock(VideoState *is)
1103 return is->external_clock;
1105 double time = av_gettime() / 1000000.0;
1106 return is->external_clock_drift + time - (time - is->external_clock_time / 1000000.0) * (1.0 - is->external_clock_speed);
1110 static int get_master_sync_type(VideoState *is) {
1111 if (is->av_sync_type == AV_SYNC_VIDEO_MASTER) {
1113 return AV_SYNC_VIDEO_MASTER;
1115 return AV_SYNC_AUDIO_MASTER;
1116 } else if (is->av_sync_type == AV_SYNC_AUDIO_MASTER) {
1118 return AV_SYNC_AUDIO_MASTER;
1120 return AV_SYNC_EXTERNAL_CLOCK;
1122 return AV_SYNC_EXTERNAL_CLOCK;
1126 /* get the current master clock value */
1127 static double get_master_clock(VideoState *is)
1131 switch (get_master_sync_type(is)) {
1132 case AV_SYNC_VIDEO_MASTER:
1133 val = get_video_clock(is);
1135 case AV_SYNC_AUDIO_MASTER:
1136 val = get_audio_clock(is);
1139 val = get_external_clock(is);
1145 static void update_external_clock_pts(VideoState *is, double pts)
1147 is->external_clock_time = av_gettime();
1148 is->external_clock = pts;
1149 is->external_clock_drift = pts - is->external_clock_time / 1000000.0;
1152 static void check_external_clock_sync(VideoState *is, double pts) {
1153 if (fabs(get_external_clock(is) - pts) > AV_NOSYNC_THRESHOLD) {
1154 update_external_clock_pts(is, pts);
1158 static void update_external_clock_speed(VideoState *is, double speed) {
1159 update_external_clock_pts(is, get_external_clock(is));
1160 is->external_clock_speed = speed;
1163 static void check_external_clock_speed(VideoState *is) {
1164 if (is->video_stream >= 0 && is->videoq.nb_packets <= MIN_FRAMES / 2 ||
1165 is->audio_stream >= 0 && is->audioq.nb_packets <= MIN_FRAMES / 2) {
1166 update_external_clock_speed(is, FFMAX(EXTERNAL_CLOCK_SPEED_MIN, is->external_clock_speed - EXTERNAL_CLOCK_SPEED_STEP));
1167 } else if ((is->video_stream < 0 || is->videoq.nb_packets > MIN_FRAMES * 2) &&
1168 (is->audio_stream < 0 || is->audioq.nb_packets > MIN_FRAMES * 2)) {
1169 update_external_clock_speed(is, FFMIN(EXTERNAL_CLOCK_SPEED_MAX, is->external_clock_speed + EXTERNAL_CLOCK_SPEED_STEP));
1171 double speed = is->external_clock_speed;
1173 update_external_clock_speed(is, speed + EXTERNAL_CLOCK_SPEED_STEP * (1.0 - speed) / fabs(1.0 - speed));
1177 /* seek in the stream */
1178 static void stream_seek(VideoState *is, int64_t pos, int64_t rel, int seek_by_bytes)
1180 if (!is->seek_req) {
1183 is->seek_flags &= ~AVSEEK_FLAG_BYTE;
1185 is->seek_flags |= AVSEEK_FLAG_BYTE;
1190 /* pause or resume the video */
1191 static void stream_toggle_pause(VideoState *is)
1194 is->frame_timer += av_gettime() / 1000000.0 + is->video_current_pts_drift - is->video_current_pts;
1195 if (is->read_pause_return != AVERROR(ENOSYS)) {
1196 is->video_current_pts = is->video_current_pts_drift + av_gettime() / 1000000.0;
1198 is->video_current_pts_drift = is->video_current_pts - av_gettime() / 1000000.0;
1200 update_external_clock_pts(is, get_external_clock(is));
1201 is->paused = !is->paused;
1204 static void toggle_pause(VideoState *is)
1206 stream_toggle_pause(is);
1210 static void step_to_next_frame(VideoState *is)
1212 /* if the stream is paused unpause it, then step */
1214 stream_toggle_pause(is);
1218 static double compute_target_delay(double delay, VideoState *is)
1220 double sync_threshold, diff;
1222 /* update delay to follow master synchronisation source */
1223 if (get_master_sync_type(is) != AV_SYNC_VIDEO_MASTER) {
1224 /* if video is slave, we try to correct big delays by
1225 duplicating or deleting a frame */
1226 diff = get_video_clock(is) - get_master_clock(is);
1228 /* skip or repeat frame. We take into account the
1229 delay to compute the threshold. I still don't know
1230 if it is the best guess */
1231 sync_threshold = FFMAX(AV_SYNC_THRESHOLD, delay);
1232 if (fabs(diff) < AV_NOSYNC_THRESHOLD) {
1233 if (diff <= -sync_threshold)
1235 else if (diff >= sync_threshold)
1240 av_dlog(NULL, "video: delay=%0.3f A-V=%f\n",
1246 static void pictq_next_picture(VideoState *is) {
1247 /* update queue size and signal for next picture */
1248 if (++is->pictq_rindex == VIDEO_PICTURE_QUEUE_SIZE)
1249 is->pictq_rindex = 0;
1251 SDL_LockMutex(is->pictq_mutex);
1253 SDL_CondSignal(is->pictq_cond);
1254 SDL_UnlockMutex(is->pictq_mutex);
1257 static void pictq_prev_picture(VideoState *is) {
1258 VideoPicture *prevvp;
1259 /* update queue size and signal for the previous picture */
1260 prevvp = &is->pictq[(is->pictq_rindex + VIDEO_PICTURE_QUEUE_SIZE - 1) % VIDEO_PICTURE_QUEUE_SIZE];
1261 if (prevvp->allocated && prevvp->serial == is->videoq.serial) {
1262 SDL_LockMutex(is->pictq_mutex);
1263 if (is->pictq_size < VIDEO_PICTURE_QUEUE_SIZE - 1) {
1264 if (--is->pictq_rindex == -1)
1265 is->pictq_rindex = VIDEO_PICTURE_QUEUE_SIZE - 1;
1268 SDL_CondSignal(is->pictq_cond);
1269 SDL_UnlockMutex(is->pictq_mutex);
1273 static void update_video_pts(VideoState *is, double pts, int64_t pos, int serial) {
1274 double time = av_gettime() / 1000000.0;
1275 /* update current video pts */
1276 is->video_current_pts = pts;
1277 is->video_current_pts_drift = is->video_current_pts - time;
1278 is->video_current_pos = pos;
1279 is->frame_last_pts = pts;
1280 if (is->videoq.serial == serial)
1281 check_external_clock_sync(is, is->video_current_pts);
1284 /* called to display each frame */
1285 static void video_refresh(void *opaque, double *remaining_time)
1287 VideoState *is = opaque;
1291 SubPicture *sp, *sp2;
1293 if (!is->paused && get_master_sync_type(is) == AV_SYNC_EXTERNAL_CLOCK && is->realtime)
1294 check_external_clock_speed(is);
1296 if (!display_disable && is->show_mode != SHOW_MODE_VIDEO && is->audio_st) {
1297 time = av_gettime() / 1000000.0;
1298 if (is->force_refresh || is->last_vis_time + rdftspeed < time) {
1300 is->last_vis_time = time;
1302 *remaining_time = FFMIN(*remaining_time, is->last_vis_time + rdftspeed - time);
1306 if (is->force_refresh)
1307 pictq_prev_picture(is);
1309 if (is->pictq_size == 0) {
1310 SDL_LockMutex(is->pictq_mutex);
1311 if (is->frame_last_dropped_pts != AV_NOPTS_VALUE && is->frame_last_dropped_pts > is->frame_last_pts) {
1312 update_video_pts(is, is->frame_last_dropped_pts, is->frame_last_dropped_pos, 0);
1313 is->frame_last_dropped_pts = AV_NOPTS_VALUE;
1315 SDL_UnlockMutex(is->pictq_mutex);
1316 // nothing to do, no picture to display in the queue
1318 double last_duration, duration, delay;
1319 /* dequeue the picture */
1320 vp = &is->pictq[is->pictq_rindex];
1322 if (vp->serial != is->videoq.serial) {
1323 pictq_next_picture(is);
1330 /* compute nominal last_duration */
1331 last_duration = vp->pts - is->frame_last_pts;
1332 if (last_duration > 0 && last_duration < is->max_frame_duration) {
1333 /* if duration of the last frame was sane, update last_duration in video state */
1334 is->frame_last_duration = last_duration;
1336 delay = compute_target_delay(is->frame_last_duration, is);
1338 time= av_gettime()/1000000.0;
1339 if (time < is->frame_timer + delay) {
1340 *remaining_time = FFMIN(is->frame_timer + delay - time, *remaining_time);
1345 is->frame_timer += delay * FFMAX(1, floor((time-is->frame_timer) / delay));
1347 SDL_LockMutex(is->pictq_mutex);
1348 update_video_pts(is, vp->pts, vp->pos, vp->serial);
1349 SDL_UnlockMutex(is->pictq_mutex);
1351 if (is->pictq_size > 1) {
1352 VideoPicture *nextvp = &is->pictq[(is->pictq_rindex + 1) % VIDEO_PICTURE_QUEUE_SIZE];
1353 duration = nextvp->pts - vp->pts;
1354 if(!is->step && (framedrop>0 || (framedrop && get_master_sync_type(is) != AV_SYNC_VIDEO_MASTER)) && time > is->frame_timer + duration){
1355 is->frame_drops_late++;
1356 pictq_next_picture(is);
1361 if (is->subtitle_st) {
1362 if (is->subtitle_stream_changed) {
1363 SDL_LockMutex(is->subpq_mutex);
1365 while (is->subpq_size) {
1366 free_subpicture(&is->subpq[is->subpq_rindex]);
1368 /* update queue size and signal for next picture */
1369 if (++is->subpq_rindex == SUBPICTURE_QUEUE_SIZE)
1370 is->subpq_rindex = 0;
1374 is->subtitle_stream_changed = 0;
1376 SDL_CondSignal(is->subpq_cond);
1377 SDL_UnlockMutex(is->subpq_mutex);
1379 if (is->subpq_size > 0) {
1380 sp = &is->subpq[is->subpq_rindex];
1382 if (is->subpq_size > 1)
1383 sp2 = &is->subpq[(is->subpq_rindex + 1) % SUBPICTURE_QUEUE_SIZE];
1387 if ((is->video_current_pts > (sp->pts + ((float) sp->sub.end_display_time / 1000)))
1388 || (sp2 && is->video_current_pts > (sp2->pts + ((float) sp2->sub.start_display_time / 1000))))
1390 free_subpicture(sp);
1392 /* update queue size and signal for next picture */
1393 if (++is->subpq_rindex == SUBPICTURE_QUEUE_SIZE)
1394 is->subpq_rindex = 0;
1396 SDL_LockMutex(is->subpq_mutex);
1398 SDL_CondSignal(is->subpq_cond);
1399 SDL_UnlockMutex(is->subpq_mutex);
1406 /* display picture */
1407 if (!display_disable && is->show_mode == SHOW_MODE_VIDEO)
1410 pictq_next_picture(is);
1412 if (is->step && !is->paused)
1413 stream_toggle_pause(is);
1416 is->force_refresh = 0;
1418 static int64_t last_time;
1420 int aqsize, vqsize, sqsize;
1423 cur_time = av_gettime();
1424 if (!last_time || (cur_time - last_time) >= 30000) {
1429 aqsize = is->audioq.size;
1431 vqsize = is->videoq.size;
1432 if (is->subtitle_st)
1433 sqsize = is->subtitleq.size;
1435 if (is->audio_st && is->video_st)
1436 av_diff = get_audio_clock(is) - get_video_clock(is);
1437 printf("%7.2f A-V:%7.3f fd=%4d aq=%5dKB vq=%5dKB sq=%5dB f=%"PRId64"/%"PRId64" \r",
1438 get_master_clock(is),
1440 is->frame_drops_early + is->frame_drops_late,
1444 is->video_st ? is->video_st->codec->pts_correction_num_faulty_dts : 0,
1445 is->video_st ? is->video_st->codec->pts_correction_num_faulty_pts : 0);
1447 last_time = cur_time;
1452 /* allocate a picture (needs to do that in main thread to avoid
1453 potential locking problems */
1454 static void alloc_picture(VideoState *is)
1458 vp = &is->pictq[is->pictq_windex];
1461 SDL_FreeYUVOverlay(vp->bmp);
1464 avfilter_unref_bufferp(&vp->picref);
1467 video_open(is, 0, vp);
1469 vp->bmp = SDL_CreateYUVOverlay(vp->width, vp->height,
1472 if (!vp->bmp || vp->bmp->pitches[0] < vp->width) {
1473 /* SDL allocates a buffer smaller than requested if the video
1474 * overlay hardware is unable to support the requested size. */
1475 fprintf(stderr, "Error: the video system does not support an image\n"
1476 "size of %dx%d pixels. Try using -lowres or -vf \"scale=w:h\"\n"
1477 "to reduce the image size.\n", vp->width, vp->height );
1481 SDL_LockMutex(is->pictq_mutex);
1483 SDL_CondSignal(is->pictq_cond);
1484 SDL_UnlockMutex(is->pictq_mutex);
1487 static void duplicate_right_border_pixels(SDL_Overlay *bmp) {
1488 int i, width, height;
1490 for (i = 0; i < 3; i++) {
1497 if (bmp->pitches[i] > width) {
1498 maxp = bmp->pixels[i] + bmp->pitches[i] * height - 1;
1499 for (p = bmp->pixels[i] + width - 1; p < maxp; p += bmp->pitches[i])
1505 static int queue_picture(VideoState *is, AVFrame *src_frame, double pts, int64_t pos, int serial)
1509 #if defined(DEBUG_SYNC) && 0
1510 printf("frame_type=%c pts=%0.3f\n",
1511 av_get_picture_type_char(src_frame->pict_type), pts);
1514 /* wait until we have space to put a new picture */
1515 SDL_LockMutex(is->pictq_mutex);
1517 /* keep the last already displayed picture in the queue */
1518 while (is->pictq_size >= VIDEO_PICTURE_QUEUE_SIZE - 2 &&
1519 !is->videoq.abort_request) {
1520 SDL_CondWait(is->pictq_cond, is->pictq_mutex);
1522 SDL_UnlockMutex(is->pictq_mutex);
1524 if (is->videoq.abort_request)
1527 vp = &is->pictq[is->pictq_windex];
1530 vp->sample_aspect_ratio = ((AVFilterBufferRef *)src_frame->opaque)->video->sample_aspect_ratio;
1532 vp->sample_aspect_ratio = av_guess_sample_aspect_ratio(is->ic, is->video_st, src_frame);
1535 /* alloc or resize hardware picture buffer */
1536 if (!vp->bmp || vp->reallocate || !vp->allocated ||
1537 vp->width != src_frame->width ||
1538 vp->height != src_frame->height) {
1543 vp->width = src_frame->width;
1544 vp->height = src_frame->height;
1546 /* the allocation must be done in the main thread to avoid
1547 locking problems. */
1548 event.type = FF_ALLOC_EVENT;
1549 event.user.data1 = is;
1550 SDL_PushEvent(&event);
1552 /* wait until the picture is allocated */
1553 SDL_LockMutex(is->pictq_mutex);
1554 while (!vp->allocated && !is->videoq.abort_request) {
1555 SDL_CondWait(is->pictq_cond, is->pictq_mutex);
1557 /* if the queue is aborted, we have to pop the pending ALLOC event or wait for the allocation to complete */
1558 if (is->videoq.abort_request && SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_EVENTMASK(FF_ALLOC_EVENT)) != 1) {
1559 while (!vp->allocated) {
1560 SDL_CondWait(is->pictq_cond, is->pictq_mutex);
1563 SDL_UnlockMutex(is->pictq_mutex);
1565 if (is->videoq.abort_request)
1569 /* if the frame is not skipped, then display it */
1571 AVPicture pict = { { 0 } };
1573 avfilter_unref_bufferp(&vp->picref);
1574 vp->picref = src_frame->opaque;
1577 /* get a pointer on the bitmap */
1578 SDL_LockYUVOverlay (vp->bmp);
1580 pict.data[0] = vp->bmp->pixels[0];
1581 pict.data[1] = vp->bmp->pixels[2];
1582 pict.data[2] = vp->bmp->pixels[1];
1584 pict.linesize[0] = vp->bmp->pitches[0];
1585 pict.linesize[1] = vp->bmp->pitches[2];
1586 pict.linesize[2] = vp->bmp->pitches[1];
1589 // FIXME use direct rendering
1590 av_picture_copy(&pict, (AVPicture *)src_frame,
1591 src_frame->format, vp->width, vp->height);
1593 av_opt_get_int(sws_opts, "sws_flags", 0, &sws_flags);
1594 is->img_convert_ctx = sws_getCachedContext(is->img_convert_ctx,
1595 vp->width, vp->height, src_frame->format, vp->width, vp->height,
1596 AV_PIX_FMT_YUV420P, sws_flags, NULL, NULL, NULL);
1597 if (is->img_convert_ctx == NULL) {
1598 fprintf(stderr, "Cannot initialize the conversion context\n");
1601 sws_scale(is->img_convert_ctx, src_frame->data, src_frame->linesize,
1602 0, vp->height, pict.data, pict.linesize);
1604 /* workaround SDL PITCH_WORKAROUND */
1605 duplicate_right_border_pixels(vp->bmp);
1606 /* update the bitmap content */
1607 SDL_UnlockYUVOverlay(vp->bmp);
1611 vp->serial = serial;
1613 /* now we can update the picture count */
1614 if (++is->pictq_windex == VIDEO_PICTURE_QUEUE_SIZE)
1615 is->pictq_windex = 0;
1616 SDL_LockMutex(is->pictq_mutex);
1618 SDL_UnlockMutex(is->pictq_mutex);
1623 static int get_video_frame(VideoState *is, AVFrame *frame, int64_t *pts, AVPacket *pkt, int *serial)
1627 if (packet_queue_get(&is->videoq, pkt, 1, serial) < 0)
1630 if (pkt->data == flush_pkt.data) {
1631 avcodec_flush_buffers(is->video_st->codec);
1633 SDL_LockMutex(is->pictq_mutex);
1634 // Make sure there are no long delay timers (ideally we should just flush the queue but that's harder)
1635 while (is->pictq_size && !is->videoq.abort_request) {
1636 SDL_CondWait(is->pictq_cond, is->pictq_mutex);
1638 is->video_current_pos = -1;
1639 is->frame_last_pts = AV_NOPTS_VALUE;
1640 is->frame_last_duration = 0;
1641 is->frame_timer = (double)av_gettime() / 1000000.0;
1642 is->frame_last_dropped_pts = AV_NOPTS_VALUE;
1643 SDL_UnlockMutex(is->pictq_mutex);
1648 if(avcodec_decode_video2(is->video_st->codec, frame, &got_picture, pkt) < 0)
1654 if (decoder_reorder_pts == -1) {
1655 *pts = av_frame_get_best_effort_timestamp(frame);
1656 } else if (decoder_reorder_pts) {
1657 *pts = frame->pkt_pts;
1659 *pts = frame->pkt_dts;
1662 if (*pts == AV_NOPTS_VALUE) {
1666 if (framedrop>0 || (framedrop && get_master_sync_type(is) != AV_SYNC_VIDEO_MASTER)) {
1667 SDL_LockMutex(is->pictq_mutex);
1668 if (is->frame_last_pts != AV_NOPTS_VALUE && *pts) {
1669 double clockdiff = get_video_clock(is) - get_master_clock(is);
1670 double dpts = av_q2d(is->video_st->time_base) * *pts;
1671 double ptsdiff = dpts - is->frame_last_pts;
1672 if (fabs(clockdiff) < AV_NOSYNC_THRESHOLD &&
1673 ptsdiff > 0 && ptsdiff < AV_NOSYNC_THRESHOLD &&
1674 clockdiff + ptsdiff - is->frame_last_filter_delay < 0) {
1675 is->frame_last_dropped_pos = pkt->pos;
1676 is->frame_last_dropped_pts = dpts;
1677 is->frame_drops_early++;
1681 SDL_UnlockMutex(is->pictq_mutex);
1690 static int configure_filtergraph(AVFilterGraph *graph, const char *filtergraph,
1691 AVFilterContext *source_ctx, AVFilterContext *sink_ctx)
1694 AVFilterInOut *outputs = NULL, *inputs = NULL;
1697 outputs = avfilter_inout_alloc();
1698 inputs = avfilter_inout_alloc();
1699 if (!outputs || !inputs) {
1700 ret = AVERROR(ENOMEM);
1704 outputs->name = av_strdup("in");
1705 outputs->filter_ctx = source_ctx;
1706 outputs->pad_idx = 0;
1707 outputs->next = NULL;
1709 inputs->name = av_strdup("out");
1710 inputs->filter_ctx = sink_ctx;
1711 inputs->pad_idx = 0;
1712 inputs->next = NULL;
1714 if ((ret = avfilter_graph_parse(graph, filtergraph, &inputs, &outputs, NULL)) < 0)
1717 if ((ret = avfilter_link(source_ctx, 0, sink_ctx, 0)) < 0)
1721 ret = avfilter_graph_config(graph, NULL);
1723 avfilter_inout_free(&outputs);
1724 avfilter_inout_free(&inputs);
1728 static int configure_video_filters(AVFilterGraph *graph, VideoState *is, const char *vfilters, AVFrame *frame)
1730 static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE };
1731 char sws_flags_str[128];
1732 char buffersrc_args[256];
1734 AVBufferSinkParams *buffersink_params = av_buffersink_params_alloc();
1735 AVFilterContext *filt_src = NULL, *filt_out = NULL, *filt_crop;
1736 AVCodecContext *codec = is->video_st->codec;
1738 if (!buffersink_params)
1739 return AVERROR(ENOMEM);
1741 av_opt_get_int(sws_opts, "sws_flags", 0, &sws_flags);
1742 snprintf(sws_flags_str, sizeof(sws_flags_str), "flags=%"PRId64, sws_flags);
1743 graph->scale_sws_opts = av_strdup(sws_flags_str);
1745 snprintf(buffersrc_args, sizeof(buffersrc_args),
1746 "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d",
1747 frame->width, frame->height, frame->format,
1748 is->video_st->time_base.num, is->video_st->time_base.den,
1749 codec->sample_aspect_ratio.num, FFMAX(codec->sample_aspect_ratio.den, 1));
1751 if ((ret = avfilter_graph_create_filter(&filt_src,
1752 avfilter_get_by_name("buffer"),
1753 "ffplay_buffer", buffersrc_args, NULL,
1757 buffersink_params->pixel_fmts = pix_fmts;
1758 ret = avfilter_graph_create_filter(&filt_out,
1759 avfilter_get_by_name("ffbuffersink"),
1760 "ffplay_buffersink", NULL, buffersink_params, graph);
1764 /* SDL YUV code is not handling odd width/height for some driver
1765 * combinations, therefore we crop the picture to an even width/height. */
1766 if ((ret = avfilter_graph_create_filter(&filt_crop,
1767 avfilter_get_by_name("crop"),
1768 "ffplay_crop", "floor(in_w/2)*2:floor(in_h/2)*2", NULL, graph)) < 0)
1770 if ((ret = avfilter_link(filt_crop, 0, filt_out, 0)) < 0)
1773 if ((ret = configure_filtergraph(graph, vfilters, filt_src, filt_crop)) < 0)
1776 is->in_video_filter = filt_src;
1777 is->out_video_filter = filt_out;
1780 av_freep(&buffersink_params);
1784 #endif /* CONFIG_AVFILTER */
1786 static int video_thread(void *arg)
1788 AVPacket pkt = { 0 };
1789 VideoState *is = arg;
1790 AVFrame *frame = avcodec_alloc_frame();
1791 int64_t pts_int = AV_NOPTS_VALUE, pos = -1;
1797 AVCodecContext *codec = is->video_st->codec;
1798 AVFilterGraph *graph = avfilter_graph_alloc();
1799 AVFilterContext *filt_out = NULL, *filt_in = NULL;
1802 enum AVPixelFormat last_format = -2;
1803 int last_serial = -1;
1805 if (codec->codec->capabilities & CODEC_CAP_DR1) {
1807 codec->get_buffer = codec_get_buffer;
1808 codec->release_buffer = codec_release_buffer;
1809 codec->opaque = &is->buffer_pool;
1815 AVFilterBufferRef *picref;
1818 while (is->paused && !is->videoq.abort_request)
1821 avcodec_get_frame_defaults(frame);
1822 av_free_packet(&pkt);
1824 ret = get_video_frame(is, frame, &pts_int, &pkt, &serial);
1832 if ( last_w != frame->width
1833 || last_h != frame->height
1834 || last_format != frame->format
1835 || last_serial != serial) {
1836 av_log(NULL, AV_LOG_DEBUG,
1837 "Video frame changed from size:%dx%d format:%s serial:%d to size:%dx%d format:%s serial:%d\n",
1839 (const char *)av_x_if_null(av_get_pix_fmt_name(last_format), "none"), last_serial,
1840 frame->width, frame->height,
1841 (const char *)av_x_if_null(av_get_pix_fmt_name(frame->format), "none"), serial);
1842 avfilter_graph_free(&graph);
1843 graph = avfilter_graph_alloc();
1844 if ((ret = configure_video_filters(graph, is, vfilters, frame)) < 0) {
1846 event.type = FF_QUIT_EVENT;
1847 event.user.data1 = is;
1848 SDL_PushEvent(&event);
1849 av_free_packet(&pkt);
1852 filt_in = is->in_video_filter;
1853 filt_out = is->out_video_filter;
1854 last_w = frame->width;
1855 last_h = frame->height;
1856 last_format = frame->format;
1857 last_serial = serial;
1860 frame->pts = pts_int;
1861 frame->sample_aspect_ratio = av_guess_sample_aspect_ratio(is->ic, is->video_st, frame);
1862 if (is->use_dr1 && frame->opaque) {
1863 FrameBuffer *buf = frame->opaque;
1864 AVFilterBufferRef *fb = avfilter_get_video_buffer_ref_from_arrays(
1865 frame->data, frame->linesize,
1866 AV_PERM_READ | AV_PERM_PRESERVE,
1867 frame->width, frame->height,
1870 avfilter_copy_frame_props(fb, frame);
1871 fb->buf->priv = buf;
1872 fb->buf->free = filter_release_buffer;
1875 av_buffersrc_add_ref(filt_in, fb, AV_BUFFERSRC_FLAG_NO_COPY);
1878 av_buffersrc_write_frame(filt_in, frame);
1880 av_free_packet(&pkt);
1883 is->frame_last_returned_time = av_gettime() / 1000000.0;
1885 ret = av_buffersink_get_buffer_ref(filt_out, &picref, 0);
1891 is->frame_last_filter_delay = av_gettime() / 1000000.0 - is->frame_last_returned_time;
1892 if (fabs(is->frame_last_filter_delay) > AV_NOSYNC_THRESHOLD / 10.0)
1893 is->frame_last_filter_delay = 0;
1895 avfilter_copy_buf_props(frame, picref);
1897 pts_int = picref->pts;
1898 tb = filt_out->inputs[0]->time_base;
1900 frame->opaque = picref;
1902 if (av_cmp_q(tb, is->video_st->time_base)) {
1903 av_unused int64_t pts1 = pts_int;
1904 pts_int = av_rescale_q(pts_int, tb, is->video_st->time_base);
1905 av_dlog(NULL, "video_thread(): "
1906 "tb:%d/%d pts:%"PRId64" -> tb:%d/%d pts:%"PRId64"\n",
1907 tb.num, tb.den, pts1,
1908 is->video_st->time_base.num, is->video_st->time_base.den, pts_int);
1910 pts = pts_int * av_q2d(is->video_st->time_base);
1911 ret = queue_picture(is, frame, pts, pos, serial);
1914 pts = pts_int * av_q2d(is->video_st->time_base);
1915 ret = queue_picture(is, frame, pts, pkt.pos, serial);
1922 avcodec_flush_buffers(is->video_st->codec);
1924 avfilter_graph_free(&graph);
1926 av_free_packet(&pkt);
1927 avcodec_free_frame(&frame);
1931 static int subtitle_thread(void *arg)
1933 VideoState *is = arg;
1935 AVPacket pkt1, *pkt = &pkt1;
1939 int r, g, b, y, u, v, a;
1942 while (is->paused && !is->subtitleq.abort_request) {
1945 if (packet_queue_get(&is->subtitleq, pkt, 1, NULL) < 0)
1948 if (pkt->data == flush_pkt.data) {
1949 avcodec_flush_buffers(is->subtitle_st->codec);
1952 SDL_LockMutex(is->subpq_mutex);
1953 while (is->subpq_size >= SUBPICTURE_QUEUE_SIZE &&
1954 !is->subtitleq.abort_request) {
1955 SDL_CondWait(is->subpq_cond, is->subpq_mutex);
1957 SDL_UnlockMutex(is->subpq_mutex);
1959 if (is->subtitleq.abort_request)
1962 sp = &is->subpq[is->subpq_windex];
1964 /* NOTE: ipts is the PTS of the _first_ picture beginning in
1965 this packet, if any */
1967 if (pkt->pts != AV_NOPTS_VALUE)
1968 pts = av_q2d(is->subtitle_st->time_base) * pkt->pts;
1970 avcodec_decode_subtitle2(is->subtitle_st->codec, &sp->sub,
1971 &got_subtitle, pkt);
1972 if (got_subtitle && sp->sub.format == 0) {
1973 if (sp->sub.pts != AV_NOPTS_VALUE)
1974 pts = sp->sub.pts / (double)AV_TIME_BASE;
1977 for (i = 0; i < sp->sub.num_rects; i++)
1979 for (j = 0; j < sp->sub.rects[i]->nb_colors; j++)
1981 RGBA_IN(r, g, b, a, (uint32_t*)sp->sub.rects[i]->pict.data[1] + j);
1982 y = RGB_TO_Y_CCIR(r, g, b);
1983 u = RGB_TO_U_CCIR(r, g, b, 0);
1984 v = RGB_TO_V_CCIR(r, g, b, 0);
1985 YUVA_OUT((uint32_t*)sp->sub.rects[i]->pict.data[1] + j, y, u, v, a);
1989 /* now we can update the picture count */
1990 if (++is->subpq_windex == SUBPICTURE_QUEUE_SIZE)
1991 is->subpq_windex = 0;
1992 SDL_LockMutex(is->subpq_mutex);
1994 SDL_UnlockMutex(is->subpq_mutex);
1996 av_free_packet(pkt);
2001 /* copy samples for viewing in editor window */
2002 static void update_sample_display(VideoState *is, short *samples, int samples_size)
2006 size = samples_size / sizeof(short);
2008 len = SAMPLE_ARRAY_SIZE - is->sample_array_index;
2011 memcpy(is->sample_array + is->sample_array_index, samples, len * sizeof(short));
2013 is->sample_array_index += len;
2014 if (is->sample_array_index >= SAMPLE_ARRAY_SIZE)
2015 is->sample_array_index = 0;
2020 /* return the wanted number of samples to get better sync if sync_type is video
2021 * or external master clock */
2022 static int synchronize_audio(VideoState *is, int nb_samples)
2024 int wanted_nb_samples = nb_samples;
2026 /* if not master, then we try to remove or add samples to correct the clock */
2027 if (get_master_sync_type(is) != AV_SYNC_AUDIO_MASTER) {
2028 double diff, avg_diff;
2029 int min_nb_samples, max_nb_samples;
2031 diff = get_audio_clock(is) - get_master_clock(is);
2033 if (fabs(diff) < AV_NOSYNC_THRESHOLD) {
2034 is->audio_diff_cum = diff + is->audio_diff_avg_coef * is->audio_diff_cum;
2035 if (is->audio_diff_avg_count < AUDIO_DIFF_AVG_NB) {
2036 /* not enough measures to have a correct estimate */
2037 is->audio_diff_avg_count++;
2039 /* estimate the A-V difference */
2040 avg_diff = is->audio_diff_cum * (1.0 - is->audio_diff_avg_coef);
2042 if (fabs(avg_diff) >= is->audio_diff_threshold) {
2043 wanted_nb_samples = nb_samples + (int)(diff * is->audio_src.freq);
2044 min_nb_samples = ((nb_samples * (100 - SAMPLE_CORRECTION_PERCENT_MAX) / 100));
2045 max_nb_samples = ((nb_samples * (100 + SAMPLE_CORRECTION_PERCENT_MAX) / 100));
2046 wanted_nb_samples = FFMIN(FFMAX(wanted_nb_samples, min_nb_samples), max_nb_samples);
2048 av_dlog(NULL, "diff=%f adiff=%f sample_diff=%d apts=%0.3f %f\n",
2049 diff, avg_diff, wanted_nb_samples - nb_samples,
2050 is->audio_clock, is->audio_diff_threshold);
2053 /* too big difference : may be initial PTS errors, so
2055 is->audio_diff_avg_count = 0;
2056 is->audio_diff_cum = 0;
2060 return wanted_nb_samples;
2064 * Decode one audio frame and return its uncompressed size.
2066 * The processed audio frame is decoded, converted if required, and
2067 * stored in is->audio_buf, with size in bytes given by the return
2070 static int audio_decode_frame(VideoState *is)
2072 AVPacket *pkt_temp = &is->audio_pkt_temp;
2073 AVPacket *pkt = &is->audio_pkt;
2074 AVCodecContext *dec = is->audio_st->codec;
2075 int len1, len2, data_size, resampled_data_size;
2076 int64_t dec_channel_layout;
2078 av_unused double audio_clock0;
2080 int flush_complete = 0;
2081 int wanted_nb_samples;
2084 /* NOTE: the audio packet can contain several frames */
2085 while (pkt_temp->size > 0 || (!pkt_temp->data && new_packet)) {
2087 if (!(is->frame = avcodec_alloc_frame()))
2088 return AVERROR(ENOMEM);
2090 avcodec_get_frame_defaults(is->frame);
2098 len1 = avcodec_decode_audio4(dec, is->frame, &got_frame, pkt_temp);
2100 /* if error, we skip the frame */
2105 pkt_temp->data += len1;
2106 pkt_temp->size -= len1;
2109 /* stop sending empty packets if the decoder is finished */
2110 if (!pkt_temp->data && dec->codec->capabilities & CODEC_CAP_DELAY)
2114 data_size = av_samples_get_buffer_size(NULL, is->frame->channels,
2115 is->frame->nb_samples,
2116 is->frame->format, 1);
2118 dec_channel_layout =
2119 (is->frame->channel_layout && is->frame->channels == av_get_channel_layout_nb_channels(is->frame->channel_layout)) ?
2120 is->frame->channel_layout : av_get_default_channel_layout(is->frame->channels);
2121 wanted_nb_samples = synchronize_audio(is, is->frame->nb_samples);
2123 if (is->frame->format != is->audio_src.fmt ||
2124 dec_channel_layout != is->audio_src.channel_layout ||
2125 is->frame->sample_rate != is->audio_src.freq ||
2126 (wanted_nb_samples != is->frame->nb_samples && !is->swr_ctx)) {
2127 swr_free(&is->swr_ctx);
2128 is->swr_ctx = swr_alloc_set_opts(NULL,
2129 is->audio_tgt.channel_layout, is->audio_tgt.fmt, is->audio_tgt.freq,
2130 dec_channel_layout, is->frame->format, is->frame->sample_rate,
2132 if (!is->swr_ctx || swr_init(is->swr_ctx) < 0) {
2133 fprintf(stderr, "Cannot create sample rate converter for conversion of %d Hz %s %d channels to %d Hz %s %d channels!\n",
2134 is->frame->sample_rate, av_get_sample_fmt_name(is->frame->format), (int)is->frame->channels,
2135 is->audio_tgt.freq, av_get_sample_fmt_name(is->audio_tgt.fmt), is->audio_tgt.channels);
2138 is->audio_src.channel_layout = dec_channel_layout;
2139 is->audio_src.channels = is->frame->channels;
2140 is->audio_src.freq = is->frame->sample_rate;
2141 is->audio_src.fmt = is->frame->format;
2145 const uint8_t **in = (const uint8_t **)is->frame->extended_data;
2146 uint8_t **out = &is->audio_buf1;
2147 int out_count = (int64_t)wanted_nb_samples * is->audio_tgt.freq / is->frame->sample_rate + 256;
2148 int out_size = av_samples_get_buffer_size(NULL, is->audio_tgt.channels, out_count, is->audio_tgt.fmt, 0);
2149 if (wanted_nb_samples != is->frame->nb_samples) {
2150 if (swr_set_compensation(is->swr_ctx, (wanted_nb_samples - is->frame->nb_samples) * is->audio_tgt.freq / is->frame->sample_rate,
2151 wanted_nb_samples * is->audio_tgt.freq / is->frame->sample_rate) < 0) {
2152 fprintf(stderr, "swr_set_compensation() failed\n");
2156 av_fast_malloc(&is->audio_buf1, &is->audio_buf1_size, out_size);
2157 if (!is->audio_buf1)
2158 return AVERROR(ENOMEM);
2159 len2 = swr_convert(is->swr_ctx, out, out_count, in, is->frame->nb_samples);
2161 fprintf(stderr, "swr_convert() failed\n");
2164 if (len2 == out_count) {
2165 fprintf(stderr, "warning: audio buffer is probably too small\n");
2166 swr_init(is->swr_ctx);
2168 is->audio_buf = is->audio_buf1;
2169 resampled_data_size = len2 * is->audio_tgt.channels * av_get_bytes_per_sample(is->audio_tgt.fmt);
2171 is->audio_buf = is->frame->data[0];
2172 resampled_data_size = data_size;
2175 audio_clock0 = is->audio_clock;
2176 is->audio_clock += (double)data_size /
2177 (is->frame->channels * is->frame->sample_rate * av_get_bytes_per_sample(is->frame->format));
2180 static double last_clock;
2181 printf("audio: delay=%0.3f clock=%0.3f clock0=%0.3f\n",
2182 is->audio_clock - last_clock,
2183 is->audio_clock, audio_clock0);
2184 last_clock = is->audio_clock;
2187 return resampled_data_size;
2190 /* free the current packet */
2192 av_free_packet(pkt);
2193 memset(pkt_temp, 0, sizeof(*pkt_temp));
2195 if (is->paused || is->audioq.abort_request) {
2199 if (is->audioq.nb_packets == 0)
2200 SDL_CondSignal(is->continue_read_thread);
2202 /* read next packet */
2203 if ((new_packet = packet_queue_get(&is->audioq, pkt, 1, &is->audio_pkt_temp_serial)) < 0)
2206 if (pkt->data == flush_pkt.data) {
2207 avcodec_flush_buffers(dec);
2213 /* if update the audio clock with the pts */
2214 if (pkt->pts != AV_NOPTS_VALUE) {
2215 is->audio_clock = av_q2d(is->audio_st->time_base)*pkt->pts;
2220 /* prepare a new audio buffer */
2221 static void sdl_audio_callback(void *opaque, Uint8 *stream, int len)
2223 VideoState *is = opaque;
2224 int audio_size, len1;
2226 int frame_size = av_samples_get_buffer_size(NULL, is->audio_tgt.channels, 1, is->audio_tgt.fmt, 1);
2228 audio_callback_time = av_gettime();
2231 if (is->audio_buf_index >= is->audio_buf_size) {
2232 audio_size = audio_decode_frame(is);
2233 if (audio_size < 0) {
2234 /* if error, just output silence */
2235 is->audio_buf = is->silence_buf;
2236 is->audio_buf_size = sizeof(is->silence_buf) / frame_size * frame_size;
2238 if (is->show_mode != SHOW_MODE_VIDEO)
2239 update_sample_display(is, (int16_t *)is->audio_buf, audio_size);
2240 is->audio_buf_size = audio_size;
2242 is->audio_buf_index = 0;
2244 len1 = is->audio_buf_size - is->audio_buf_index;
2247 memcpy(stream, (uint8_t *)is->audio_buf + is->audio_buf_index, len1);
2250 is->audio_buf_index += len1;
2252 bytes_per_sec = is->audio_tgt.freq * is->audio_tgt.channels * av_get_bytes_per_sample(is->audio_tgt.fmt);
2253 is->audio_write_buf_size = is->audio_buf_size - is->audio_buf_index;
2254 /* Let's assume the audio driver that is used by SDL has two periods. */
2255 is->audio_current_pts = is->audio_clock - (double)(2 * is->audio_hw_buf_size + is->audio_write_buf_size) / bytes_per_sec;
2256 is->audio_current_pts_drift = is->audio_current_pts - audio_callback_time / 1000000.0;
2257 if (is->audioq.serial == is->audio_pkt_temp_serial)
2258 check_external_clock_sync(is, is->audio_current_pts);
2261 static int audio_open(void *opaque, int64_t wanted_channel_layout, int wanted_nb_channels, int wanted_sample_rate, struct AudioParams *audio_hw_params)
2263 SDL_AudioSpec wanted_spec, spec;
2265 const int next_nb_channels[] = {0, 0, 1, 6, 2, 6, 4, 6};
2267 env = SDL_getenv("SDL_AUDIO_CHANNELS");
2269 wanted_nb_channels = atoi(env);
2270 wanted_channel_layout = av_get_default_channel_layout(wanted_nb_channels);
2272 if (!wanted_channel_layout || wanted_nb_channels != av_get_channel_layout_nb_channels(wanted_channel_layout)) {
2273 wanted_channel_layout = av_get_default_channel_layout(wanted_nb_channels);
2274 wanted_channel_layout &= ~AV_CH_LAYOUT_STEREO_DOWNMIX;
2276 wanted_spec.channels = av_get_channel_layout_nb_channels(wanted_channel_layout);
2277 wanted_spec.freq = wanted_sample_rate;
2278 if (wanted_spec.freq <= 0 || wanted_spec.channels <= 0) {
2279 fprintf(stderr, "Invalid sample rate or channel count!\n");
2282 wanted_spec.format = AUDIO_S16SYS;
2283 wanted_spec.silence = 0;
2284 wanted_spec.samples = SDL_AUDIO_BUFFER_SIZE;
2285 wanted_spec.callback = sdl_audio_callback;
2286 wanted_spec.userdata = opaque;
2287 while (SDL_OpenAudio(&wanted_spec, &spec) < 0) {
2288 fprintf(stderr, "SDL_OpenAudio (%d channels): %s\n", wanted_spec.channels, SDL_GetError());
2289 wanted_spec.channels = next_nb_channels[FFMIN(7, wanted_spec.channels)];
2290 if (!wanted_spec.channels) {
2291 fprintf(stderr, "No more channel combinations to try, audio open failed\n");
2294 wanted_channel_layout = av_get_default_channel_layout(wanted_spec.channels);
2296 if (spec.format != AUDIO_S16SYS) {
2297 fprintf(stderr, "SDL advised audio format %d is not supported!\n", spec.format);
2300 if (spec.channels != wanted_spec.channels) {
2301 wanted_channel_layout = av_get_default_channel_layout(spec.channels);
2302 if (!wanted_channel_layout) {
2303 fprintf(stderr, "SDL advised channel count %d is not supported!\n", spec.channels);
2308 audio_hw_params->fmt = AV_SAMPLE_FMT_S16;
2309 audio_hw_params->freq = spec.freq;
2310 audio_hw_params->channel_layout = wanted_channel_layout;
2311 audio_hw_params->channels = spec.channels;
2315 /* open a given stream. Return 0 if OK */
2316 static int stream_component_open(VideoState *is, int stream_index)
2318 AVFormatContext *ic = is->ic;
2319 AVCodecContext *avctx;
2321 const char *forced_codec_name = NULL;
2323 AVDictionaryEntry *t = NULL;
2325 if (stream_index < 0 || stream_index >= ic->nb_streams)
2327 avctx = ic->streams[stream_index]->codec;
2329 codec = avcodec_find_decoder(avctx->codec_id);
2331 switch(avctx->codec_type){
2332 case AVMEDIA_TYPE_AUDIO : is->last_audio_stream = stream_index; forced_codec_name = audio_codec_name; break;
2333 case AVMEDIA_TYPE_SUBTITLE: is->last_subtitle_stream = stream_index; forced_codec_name = subtitle_codec_name; break;
2334 case AVMEDIA_TYPE_VIDEO : is->last_video_stream = stream_index; forced_codec_name = video_codec_name; break;
2336 if (forced_codec_name)
2337 codec = avcodec_find_decoder_by_name(forced_codec_name);
2339 if (forced_codec_name) fprintf(stderr, "No codec could be found with name '%s'\n", forced_codec_name);
2340 else fprintf(stderr, "No codec could be found with id %d\n", avctx->codec_id);
2344 avctx->codec_id = codec->id;
2345 avctx->workaround_bugs = workaround_bugs;
2346 avctx->lowres = lowres;
2347 if(avctx->lowres > codec->max_lowres){
2348 av_log(avctx, AV_LOG_WARNING, "The maximum value for lowres supported by the decoder is %d\n",
2350 avctx->lowres= codec->max_lowres;
2352 avctx->idct_algo = idct;
2353 avctx->skip_frame = skip_frame;
2354 avctx->skip_idct = skip_idct;
2355 avctx->skip_loop_filter = skip_loop_filter;
2356 avctx->error_concealment = error_concealment;
2358 if(avctx->lowres) avctx->flags |= CODEC_FLAG_EMU_EDGE;
2359 if (fast) avctx->flags2 |= CODEC_FLAG2_FAST;
2360 if(codec->capabilities & CODEC_CAP_DR1)
2361 avctx->flags |= CODEC_FLAG_EMU_EDGE;
2363 opts = filter_codec_opts(codec_opts, avctx->codec_id, ic, ic->streams[stream_index], codec);
2364 if (!av_dict_get(opts, "threads", NULL, 0))
2365 av_dict_set(&opts, "threads", "auto", 0);
2366 if (avcodec_open2(avctx, codec, &opts) < 0)
2368 if ((t = av_dict_get(opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
2369 av_log(NULL, AV_LOG_ERROR, "Option %s not found.\n", t->key);
2370 return AVERROR_OPTION_NOT_FOUND;
2373 /* prepare audio output */
2374 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
2375 int audio_hw_buf_size = audio_open(is, avctx->channel_layout, avctx->channels, avctx->sample_rate, &is->audio_src);
2376 if (audio_hw_buf_size < 0)
2378 is->audio_hw_buf_size = audio_hw_buf_size;
2379 is->audio_tgt = is->audio_src;
2382 ic->streams[stream_index]->discard = AVDISCARD_DEFAULT;
2383 switch (avctx->codec_type) {
2384 case AVMEDIA_TYPE_AUDIO:
2385 is->audio_stream = stream_index;
2386 is->audio_st = ic->streams[stream_index];
2387 is->audio_buf_size = 0;
2388 is->audio_buf_index = 0;
2390 /* init averaging filter */
2391 is->audio_diff_avg_coef = exp(log(0.01) / AUDIO_DIFF_AVG_NB);
2392 is->audio_diff_avg_count = 0;
2393 /* since we do not have a precise anough audio fifo fullness,
2394 we correct audio sync only if larger than this threshold */
2395 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);
2397 memset(&is->audio_pkt, 0, sizeof(is->audio_pkt));
2398 memset(&is->audio_pkt_temp, 0, sizeof(is->audio_pkt_temp));
2399 packet_queue_start(&is->audioq);
2402 case AVMEDIA_TYPE_VIDEO:
2403 is->video_stream = stream_index;
2404 is->video_st = ic->streams[stream_index];
2406 packet_queue_start(&is->videoq);
2407 is->video_tid = SDL_CreateThread(video_thread, is);
2409 case AVMEDIA_TYPE_SUBTITLE:
2410 is->subtitle_stream = stream_index;
2411 is->subtitle_st = ic->streams[stream_index];
2412 packet_queue_start(&is->subtitleq);
2414 is->subtitle_tid = SDL_CreateThread(subtitle_thread, is);
2422 static void stream_component_close(VideoState *is, int stream_index)
2424 AVFormatContext *ic = is->ic;
2425 AVCodecContext *avctx;
2427 if (stream_index < 0 || stream_index >= ic->nb_streams)
2429 avctx = ic->streams[stream_index]->codec;
2431 switch (avctx->codec_type) {
2432 case AVMEDIA_TYPE_AUDIO:
2433 packet_queue_abort(&is->audioq);
2437 packet_queue_flush(&is->audioq);
2438 av_free_packet(&is->audio_pkt);
2439 swr_free(&is->swr_ctx);
2440 av_freep(&is->audio_buf1);
2441 is->audio_buf1_size = 0;
2442 is->audio_buf = NULL;
2443 avcodec_free_frame(&is->frame);
2446 av_rdft_end(is->rdft);
2447 av_freep(&is->rdft_data);
2452 case AVMEDIA_TYPE_VIDEO:
2453 packet_queue_abort(&is->videoq);
2455 /* note: we also signal this mutex to make sure we deblock the
2456 video thread in all cases */
2457 SDL_LockMutex(is->pictq_mutex);
2458 SDL_CondSignal(is->pictq_cond);
2459 SDL_UnlockMutex(is->pictq_mutex);
2461 SDL_WaitThread(is->video_tid, NULL);
2463 packet_queue_flush(&is->videoq);
2465 case AVMEDIA_TYPE_SUBTITLE:
2466 packet_queue_abort(&is->subtitleq);
2468 /* note: we also signal this mutex to make sure we deblock the
2469 video thread in all cases */
2470 SDL_LockMutex(is->subpq_mutex);
2471 is->subtitle_stream_changed = 1;
2473 SDL_CondSignal(is->subpq_cond);
2474 SDL_UnlockMutex(is->subpq_mutex);
2476 SDL_WaitThread(is->subtitle_tid, NULL);
2478 packet_queue_flush(&is->subtitleq);
2484 ic->streams[stream_index]->discard = AVDISCARD_ALL;
2485 avcodec_close(avctx);
2487 free_buffer_pool(&is->buffer_pool);
2489 switch (avctx->codec_type) {
2490 case AVMEDIA_TYPE_AUDIO:
2491 is->audio_st = NULL;
2492 is->audio_stream = -1;
2494 case AVMEDIA_TYPE_VIDEO:
2495 is->video_st = NULL;
2496 is->video_stream = -1;
2498 case AVMEDIA_TYPE_SUBTITLE:
2499 is->subtitle_st = NULL;
2500 is->subtitle_stream = -1;
2507 static int decode_interrupt_cb(void *ctx)
2509 VideoState *is = ctx;
2510 return is->abort_request;
2513 static int is_realtime(AVFormatContext *s)
2515 if( !strcmp(s->iformat->name, "rtp")
2516 || !strcmp(s->iformat->name, "rtsp")
2517 || !strcmp(s->iformat->name, "sdp")
2521 if(s->pb && ( !strncmp(s->filename, "rtp:", 4)
2522 || !strncmp(s->filename, "udp:", 4)
2529 /* this thread gets the stream from the disk or the network */
2530 static int read_thread(void *arg)
2532 VideoState *is = arg;
2533 AVFormatContext *ic = NULL;
2535 int st_index[AVMEDIA_TYPE_NB];
2536 AVPacket pkt1, *pkt = &pkt1;
2538 int pkt_in_play_range = 0;
2539 AVDictionaryEntry *t;
2540 AVDictionary **opts;
2541 int orig_nb_streams;
2542 SDL_mutex *wait_mutex = SDL_CreateMutex();
2544 memset(st_index, -1, sizeof(st_index));
2545 is->last_video_stream = is->video_stream = -1;
2546 is->last_audio_stream = is->audio_stream = -1;
2547 is->last_subtitle_stream = is->subtitle_stream = -1;
2549 ic = avformat_alloc_context();
2550 ic->interrupt_callback.callback = decode_interrupt_cb;
2551 ic->interrupt_callback.opaque = is;
2552 err = avformat_open_input(&ic, is->filename, is->iformat, &format_opts);
2554 print_error(is->filename, err);
2558 if ((t = av_dict_get(format_opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
2559 av_log(NULL, AV_LOG_ERROR, "Option %s not found.\n", t->key);
2560 ret = AVERROR_OPTION_NOT_FOUND;
2566 ic->flags |= AVFMT_FLAG_GENPTS;
2568 opts = setup_find_stream_info_opts(ic, codec_opts);
2569 orig_nb_streams = ic->nb_streams;
2571 err = avformat_find_stream_info(ic, opts);
2573 fprintf(stderr, "%s: could not find codec parameters\n", is->filename);
2577 for (i = 0; i < orig_nb_streams; i++)
2578 av_dict_free(&opts[i]);
2582 ic->pb->eof_reached = 0; // FIXME hack, ffplay maybe should not use url_feof() to test for the end
2584 if (seek_by_bytes < 0)
2585 seek_by_bytes = !!(ic->iformat->flags & AVFMT_TS_DISCONT);
2587 is->max_frame_duration = (ic->iformat->flags & AVFMT_TS_DISCONT) ? 10.0 : 3600.0;
2589 /* if seeking requested, we execute it */
2590 if (start_time != AV_NOPTS_VALUE) {
2593 timestamp = start_time;
2594 /* add the stream start time */
2595 if (ic->start_time != AV_NOPTS_VALUE)
2596 timestamp += ic->start_time;
2597 ret = avformat_seek_file(ic, -1, INT64_MIN, timestamp, INT64_MAX, 0);
2599 fprintf(stderr, "%s: could not seek to position %0.3f\n",
2600 is->filename, (double)timestamp / AV_TIME_BASE);
2604 is->realtime = is_realtime(ic);
2606 for (i = 0; i < ic->nb_streams; i++)
2607 ic->streams[i]->discard = AVDISCARD_ALL;
2609 st_index[AVMEDIA_TYPE_VIDEO] =
2610 av_find_best_stream(ic, AVMEDIA_TYPE_VIDEO,
2611 wanted_stream[AVMEDIA_TYPE_VIDEO], -1, NULL, 0);
2613 st_index[AVMEDIA_TYPE_AUDIO] =
2614 av_find_best_stream(ic, AVMEDIA_TYPE_AUDIO,
2615 wanted_stream[AVMEDIA_TYPE_AUDIO],
2616 st_index[AVMEDIA_TYPE_VIDEO],
2619 st_index[AVMEDIA_TYPE_SUBTITLE] =
2620 av_find_best_stream(ic, AVMEDIA_TYPE_SUBTITLE,
2621 wanted_stream[AVMEDIA_TYPE_SUBTITLE],
2622 (st_index[AVMEDIA_TYPE_AUDIO] >= 0 ?
2623 st_index[AVMEDIA_TYPE_AUDIO] :
2624 st_index[AVMEDIA_TYPE_VIDEO]),
2627 av_dump_format(ic, 0, is->filename, 0);
2630 is->show_mode = show_mode;
2632 /* open the streams */
2633 if (st_index[AVMEDIA_TYPE_AUDIO] >= 0) {
2634 stream_component_open(is, st_index[AVMEDIA_TYPE_AUDIO]);
2638 if (st_index[AVMEDIA_TYPE_VIDEO] >= 0) {
2639 ret = stream_component_open(is, st_index[AVMEDIA_TYPE_VIDEO]);
2641 if (is->show_mode == SHOW_MODE_NONE)
2642 is->show_mode = ret >= 0 ? SHOW_MODE_VIDEO : SHOW_MODE_RDFT;
2644 if (st_index[AVMEDIA_TYPE_SUBTITLE] >= 0) {
2645 stream_component_open(is, st_index[AVMEDIA_TYPE_SUBTITLE]);
2648 if (is->video_stream < 0 && is->audio_stream < 0) {
2649 fprintf(stderr, "%s: could not open codecs\n", is->filename);
2654 if (infinite_buffer < 0 && is->realtime)
2655 infinite_buffer = 1;
2658 if (is->abort_request)
2660 if (is->paused != is->last_paused) {
2661 is->last_paused = is->paused;
2663 is->read_pause_return = av_read_pause(ic);
2667 #if CONFIG_RTSP_DEMUXER || CONFIG_MMSH_PROTOCOL
2669 (!strcmp(ic->iformat->name, "rtsp") ||
2670 (ic->pb && !strncmp(input_filename, "mmsh:", 5)))) {
2671 /* wait 10 ms to avoid trying to get another packet */
2678 int64_t seek_target = is->seek_pos;
2679 int64_t seek_min = is->seek_rel > 0 ? seek_target - is->seek_rel + 2: INT64_MIN;
2680 int64_t seek_max = is->seek_rel < 0 ? seek_target - is->seek_rel - 2: INT64_MAX;
2681 // FIXME the +-2 is due to rounding being not done in the correct direction in generation
2682 // of the seek_pos/seek_rel variables
2684 ret = avformat_seek_file(is->ic, -1, seek_min, seek_target, seek_max, is->seek_flags);
2686 fprintf(stderr, "%s: error while seeking\n", is->ic->filename);
2688 if (is->audio_stream >= 0) {
2689 packet_queue_flush(&is->audioq);
2690 packet_queue_put(&is->audioq, &flush_pkt);
2692 if (is->subtitle_stream >= 0) {
2693 packet_queue_flush(&is->subtitleq);
2694 packet_queue_put(&is->subtitleq, &flush_pkt);
2696 if (is->video_stream >= 0) {
2697 packet_queue_flush(&is->videoq);
2698 packet_queue_put(&is->videoq, &flush_pkt);
2700 if (is->seek_flags & AVSEEK_FLAG_BYTE) {
2701 //FIXME: use a cleaner way to signal obsolete external clock...
2702 update_external_clock_pts(is, (double)AV_NOPTS_VALUE);
2704 update_external_clock_pts(is, seek_target / (double)AV_TIME_BASE);
2710 step_to_next_frame(is);
2712 if (is->queue_attachments_req) {
2713 avformat_queue_attached_pictures(ic);
2714 is->queue_attachments_req = 0;
2717 /* if the queue are full, no need to read more */
2718 if (infinite_buffer<1 &&
2719 (is->audioq.size + is->videoq.size + is->subtitleq.size > MAX_QUEUE_SIZE
2720 || ( (is->audioq .nb_packets > MIN_FRAMES || is->audio_stream < 0 || is->audioq.abort_request)
2721 && (is->videoq .nb_packets > MIN_FRAMES || is->video_stream < 0 || is->videoq.abort_request)
2722 && (is->subtitleq.nb_packets > MIN_FRAMES || is->subtitle_stream < 0 || is->subtitleq.abort_request)))) {
2724 SDL_LockMutex(wait_mutex);
2725 SDL_CondWaitTimeout(is->continue_read_thread, wait_mutex, 10);
2726 SDL_UnlockMutex(wait_mutex);
2730 if (is->video_stream >= 0) {
2731 av_init_packet(pkt);
2734 pkt->stream_index = is->video_stream;
2735 packet_queue_put(&is->videoq, pkt);
2737 if (is->audio_stream >= 0 &&
2738 is->audio_st->codec->codec->capabilities & CODEC_CAP_DELAY) {
2739 av_init_packet(pkt);
2742 pkt->stream_index = is->audio_stream;
2743 packet_queue_put(&is->audioq, pkt);
2746 if (is->audioq.size + is->videoq.size + is->subtitleq.size == 0) {
2747 if (loop != 1 && (!loop || --loop)) {
2748 stream_seek(is, start_time != AV_NOPTS_VALUE ? start_time : 0, 0, 0);
2749 } else if (autoexit) {
2757 ret = av_read_frame(ic, pkt);
2759 if (ret == AVERROR_EOF || url_feof(ic->pb))
2761 if (ic->pb && ic->pb->error)
2763 SDL_LockMutex(wait_mutex);
2764 SDL_CondWaitTimeout(is->continue_read_thread, wait_mutex, 10);
2765 SDL_UnlockMutex(wait_mutex);
2768 /* check if packet is in play range specified by user, then queue, otherwise discard */
2769 pkt_in_play_range = duration == AV_NOPTS_VALUE ||
2770 (pkt->pts - ic->streams[pkt->stream_index]->start_time) *
2771 av_q2d(ic->streams[pkt->stream_index]->time_base) -
2772 (double)(start_time != AV_NOPTS_VALUE ? start_time : 0) / 1000000
2773 <= ((double)duration / 1000000);
2774 if (pkt->stream_index == is->audio_stream && pkt_in_play_range) {
2775 packet_queue_put(&is->audioq, pkt);
2776 } else if (pkt->stream_index == is->video_stream && pkt_in_play_range) {
2777 packet_queue_put(&is->videoq, pkt);
2778 } else if (pkt->stream_index == is->subtitle_stream && pkt_in_play_range) {
2779 packet_queue_put(&is->subtitleq, pkt);
2781 av_free_packet(pkt);
2784 /* wait until the end */
2785 while (!is->abort_request) {
2791 /* close each stream */
2792 if (is->audio_stream >= 0)
2793 stream_component_close(is, is->audio_stream);
2794 if (is->video_stream >= 0)
2795 stream_component_close(is, is->video_stream);
2796 if (is->subtitle_stream >= 0)
2797 stream_component_close(is, is->subtitle_stream);
2799 avformat_close_input(&is->ic);
2805 event.type = FF_QUIT_EVENT;
2806 event.user.data1 = is;
2807 SDL_PushEvent(&event);
2809 SDL_DestroyMutex(wait_mutex);
2813 static VideoState *stream_open(const char *filename, AVInputFormat *iformat)
2817 is = av_mallocz(sizeof(VideoState));
2820 av_strlcpy(is->filename, filename, sizeof(is->filename));
2821 is->iformat = iformat;
2825 /* start video display */
2826 is->pictq_mutex = SDL_CreateMutex();
2827 is->pictq_cond = SDL_CreateCond();
2829 is->subpq_mutex = SDL_CreateMutex();
2830 is->subpq_cond = SDL_CreateCond();
2832 packet_queue_init(&is->videoq);
2833 packet_queue_init(&is->audioq);
2834 packet_queue_init(&is->subtitleq);
2836 is->continue_read_thread = SDL_CreateCond();
2838 //FIXME: use a cleaner way to signal obsolete external clock...
2839 update_external_clock_pts(is, (double)AV_NOPTS_VALUE);
2840 update_external_clock_speed(is, 1.0);
2841 is->audio_current_pts_drift = -av_gettime() / 1000000.0;
2842 is->video_current_pts_drift = is->audio_current_pts_drift;
2843 is->av_sync_type = av_sync_type;
2844 is->read_tid = SDL_CreateThread(read_thread, is);
2845 if (!is->read_tid) {
2852 static void stream_cycle_channel(VideoState *is, int codec_type)
2854 AVFormatContext *ic = is->ic;
2855 int start_index, stream_index;
2859 if (codec_type == AVMEDIA_TYPE_VIDEO) {
2860 start_index = is->last_video_stream;
2861 old_index = is->video_stream;
2862 } else if (codec_type == AVMEDIA_TYPE_AUDIO) {
2863 start_index = is->last_audio_stream;
2864 old_index = is->audio_stream;
2866 start_index = is->last_subtitle_stream;
2867 old_index = is->subtitle_stream;
2869 stream_index = start_index;
2871 if (++stream_index >= is->ic->nb_streams)
2873 if (codec_type == AVMEDIA_TYPE_SUBTITLE)
2876 is->last_subtitle_stream = -1;
2879 if (start_index == -1)
2883 if (stream_index == start_index)
2885 st = ic->streams[stream_index];
2886 if (st->codec->codec_type == codec_type) {
2887 /* check that parameters are OK */
2888 switch (codec_type) {
2889 case AVMEDIA_TYPE_AUDIO:
2890 if (st->codec->sample_rate != 0 &&
2891 st->codec->channels != 0)
2894 case AVMEDIA_TYPE_VIDEO:
2895 case AVMEDIA_TYPE_SUBTITLE:
2903 stream_component_close(is, old_index);
2904 stream_component_open(is, stream_index);
2905 if (codec_type == AVMEDIA_TYPE_VIDEO)
2906 is->queue_attachments_req = 1;
2910 static void toggle_full_screen(VideoState *is)
2912 #if defined(__APPLE__) && SDL_VERSION_ATLEAST(1, 2, 14)
2913 /* OS X needs to reallocate the SDL overlays */
2915 for (i = 0; i < VIDEO_PICTURE_QUEUE_SIZE; i++)
2916 is->pictq[i].reallocate = 1;
2918 is_full_screen = !is_full_screen;
2919 video_open(is, 1, NULL);
2922 static void toggle_audio_display(VideoState *is)
2924 int bgcolor = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00);
2925 is->show_mode = (is->show_mode + 1) % SHOW_MODE_NB;
2926 fill_rectangle(screen,
2927 is->xleft, is->ytop, is->width, is->height,
2931 static void refresh_loop_wait_event(VideoState *is, SDL_Event *event) {
2932 double remaining_time = 0.0;
2934 while (!SDL_PeepEvents(event, 1, SDL_GETEVENT, SDL_ALLEVENTS)) {
2935 if (!cursor_hidden && av_gettime() - cursor_last_shown > CURSOR_HIDE_DELAY) {
2939 if (remaining_time > 0.0)
2940 av_usleep((int64_t)(remaining_time * 1000000.0));
2941 remaining_time = REFRESH_RATE;
2942 if (is->show_mode != SHOW_MODE_NONE && (!is->paused || is->force_refresh))
2943 video_refresh(is, &remaining_time);
2948 /* handle an event sent by the GUI */
2949 static void event_loop(VideoState *cur_stream)
2952 double incr, pos, frac;
2956 refresh_loop_wait_event(cur_stream, &event);
2957 switch (event.type) {
2959 if (exit_on_keydown) {
2960 do_exit(cur_stream);
2963 switch (event.key.keysym.sym) {
2966 do_exit(cur_stream);
2969 toggle_full_screen(cur_stream);
2970 cur_stream->force_refresh = 1;
2974 toggle_pause(cur_stream);
2976 case SDLK_s: // S: Step to next frame
2977 step_to_next_frame(cur_stream);
2980 stream_cycle_channel(cur_stream, AVMEDIA_TYPE_AUDIO);
2983 stream_cycle_channel(cur_stream, AVMEDIA_TYPE_VIDEO);
2986 stream_cycle_channel(cur_stream, AVMEDIA_TYPE_SUBTITLE);
2989 toggle_audio_display(cur_stream);
2990 cur_stream->force_refresh = 1;
3010 if (seek_by_bytes) {
3011 if (cur_stream->video_stream >= 0 && cur_stream->video_current_pos >= 0) {
3012 pos = cur_stream->video_current_pos;
3013 } else if (cur_stream->audio_stream >= 0 && cur_stream->audio_pkt.pos >= 0) {
3014 pos = cur_stream->audio_pkt.pos;
3016 pos = avio_tell(cur_stream->ic->pb);
3017 if (cur_stream->ic->bit_rate)
3018 incr *= cur_stream->ic->bit_rate / 8.0;
3022 stream_seek(cur_stream, pos, incr, 1);
3024 pos = get_master_clock(cur_stream);
3026 if (cur_stream->ic->start_time != AV_NOPTS_VALUE && pos < cur_stream->ic->start_time / (double)AV_TIME_BASE)
3027 pos = cur_stream->ic->start_time / (double)AV_TIME_BASE;
3028 stream_seek(cur_stream, (int64_t)(pos * AV_TIME_BASE), (int64_t)(incr * AV_TIME_BASE), 0);
3035 case SDL_VIDEOEXPOSE:
3036 cur_stream->force_refresh = 1;
3038 case SDL_MOUSEBUTTONDOWN:
3039 if (exit_on_mousedown) {
3040 do_exit(cur_stream);
3043 case SDL_MOUSEMOTION:
3044 if (cursor_hidden) {
3048 cursor_last_shown = av_gettime();
3049 if (event.type == SDL_MOUSEBUTTONDOWN) {
3052 if (event.motion.state != SDL_PRESSED)
3056 if (seek_by_bytes || cur_stream->ic->duration <= 0) {
3057 uint64_t size = avio_size(cur_stream->ic->pb);
3058 stream_seek(cur_stream, size*x/cur_stream->width, 0, 1);
3062 int tns, thh, tmm, tss;
3063 tns = cur_stream->ic->duration / 1000000LL;
3065 tmm = (tns % 3600) / 60;
3067 frac = x / cur_stream->width;
3070 mm = (ns % 3600) / 60;
3072 fprintf(stderr, "Seek to %2.0f%% (%2d:%02d:%02d) of total duration (%2d:%02d:%02d) \n", frac*100,
3073 hh, mm, ss, thh, tmm, tss);
3074 ts = frac * cur_stream->ic->duration;
3075 if (cur_stream->ic->start_time != AV_NOPTS_VALUE)
3076 ts += cur_stream->ic->start_time;
3077 stream_seek(cur_stream, ts, 0, 0);
3080 case SDL_VIDEORESIZE:
3081 screen = SDL_SetVideoMode(event.resize.w, event.resize.h, 0,
3082 SDL_HWSURFACE|SDL_RESIZABLE|SDL_ASYNCBLIT|SDL_HWACCEL);
3083 screen_width = cur_stream->width = event.resize.w;
3084 screen_height = cur_stream->height = event.resize.h;
3085 cur_stream->force_refresh = 1;
3089 do_exit(cur_stream);
3091 case FF_ALLOC_EVENT:
3092 alloc_picture(event.user.data1);
3100 static int opt_frame_size(void *optctx, const char *opt, const char *arg)
3102 av_log(NULL, AV_LOG_WARNING, "Option -s is deprecated, use -video_size.\n");
3103 return opt_default(NULL, "video_size", arg);
3106 static int opt_width(void *optctx, const char *opt, const char *arg)
3108 screen_width = parse_number_or_die(opt, arg, OPT_INT64, 1, INT_MAX);
3112 static int opt_height(void *optctx, const char *opt, const char *arg)
3114 screen_height = parse_number_or_die(opt, arg, OPT_INT64, 1, INT_MAX);
3118 static int opt_format(void *optctx, const char *opt, const char *arg)
3120 file_iformat = av_find_input_format(arg);
3121 if (!file_iformat) {
3122 fprintf(stderr, "Unknown input format: %s\n", arg);
3123 return AVERROR(EINVAL);
3128 static int opt_frame_pix_fmt(void *optctx, const char *opt, const char *arg)
3130 av_log(NULL, AV_LOG_WARNING, "Option -pix_fmt is deprecated, use -pixel_format.\n");
3131 return opt_default(NULL, "pixel_format", arg);
3134 static int opt_sync(void *optctx, const char *opt, const char *arg)
3136 if (!strcmp(arg, "audio"))
3137 av_sync_type = AV_SYNC_AUDIO_MASTER;
3138 else if (!strcmp(arg, "video"))
3139 av_sync_type = AV_SYNC_VIDEO_MASTER;
3140 else if (!strcmp(arg, "ext"))
3141 av_sync_type = AV_SYNC_EXTERNAL_CLOCK;
3143 fprintf(stderr, "Unknown value for %s: %s\n", opt, arg);
3149 static int opt_seek(void *optctx, const char *opt, const char *arg)
3151 start_time = parse_time_or_die(opt, arg, 1);
3155 static int opt_duration(void *optctx, const char *opt, const char *arg)
3157 duration = parse_time_or_die(opt, arg, 1);
3161 static int opt_show_mode(void *optctx, const char *opt, const char *arg)
3163 show_mode = !strcmp(arg, "video") ? SHOW_MODE_VIDEO :
3164 !strcmp(arg, "waves") ? SHOW_MODE_WAVES :
3165 !strcmp(arg, "rdft" ) ? SHOW_MODE_RDFT :
3166 parse_number_or_die(opt, arg, OPT_INT, 0, SHOW_MODE_NB-1);
3170 static void opt_input_file(void *optctx, const char *filename)
3172 if (input_filename) {
3173 fprintf(stderr, "Argument '%s' provided as input filename, but '%s' was already specified.\n",
3174 filename, input_filename);
3177 if (!strcmp(filename, "-"))
3179 input_filename = filename;
3182 static int opt_codec(void *optctx, const char *opt, const char *arg)
3184 const char *spec = strchr(opt, ':');
3186 fprintf(stderr, "No media specifier was specified in '%s' in option '%s'\n",
3188 return AVERROR(EINVAL);
3192 case 'a' : audio_codec_name = arg; break;
3193 case 's' : subtitle_codec_name = arg; break;
3194 case 'v' : video_codec_name = arg; break;
3196 fprintf(stderr, "Invalid media specifier '%s' in option '%s'\n", spec, opt);
3197 return AVERROR(EINVAL);
3204 static const OptionDef options[] = {
3205 #include "cmdutils_common_opts.h"
3206 { "x", HAS_ARG, { .func_arg = opt_width }, "force displayed width", "width" },
3207 { "y", HAS_ARG, { .func_arg = opt_height }, "force displayed height", "height" },
3208 { "s", HAS_ARG | OPT_VIDEO, { .func_arg = opt_frame_size }, "set frame size (WxH or abbreviation)", "size" },
3209 { "fs", OPT_BOOL, { &is_full_screen }, "force full screen" },
3210 { "an", OPT_BOOL, { &audio_disable }, "disable audio" },
3211 { "vn", OPT_BOOL, { &video_disable }, "disable video" },
3212 { "ast", OPT_INT | HAS_ARG | OPT_EXPERT, { &wanted_stream[AVMEDIA_TYPE_AUDIO] }, "select desired audio stream", "stream_number" },
3213 { "vst", OPT_INT | HAS_ARG | OPT_EXPERT, { &wanted_stream[AVMEDIA_TYPE_VIDEO] }, "select desired video stream", "stream_number" },
3214 { "sst", OPT_INT | HAS_ARG | OPT_EXPERT, { &wanted_stream[AVMEDIA_TYPE_SUBTITLE] }, "select desired subtitle stream", "stream_number" },
3215 { "ss", HAS_ARG, { .func_arg = opt_seek }, "seek to a given position in seconds", "pos" },
3216 { "t", HAS_ARG, { .func_arg = opt_duration }, "play \"duration\" seconds of audio/video", "duration" },
3217 { "bytes", OPT_INT | HAS_ARG, { &seek_by_bytes }, "seek by bytes 0=off 1=on -1=auto", "val" },
3218 { "nodisp", OPT_BOOL, { &display_disable }, "disable graphical display" },
3219 { "f", HAS_ARG, { .func_arg = opt_format }, "force format", "fmt" },
3220 { "pix_fmt", HAS_ARG | OPT_EXPERT | OPT_VIDEO, { .func_arg = opt_frame_pix_fmt }, "set pixel format", "format" },
3221 { "stats", OPT_BOOL | OPT_EXPERT, { &show_status }, "show status", "" },
3222 { "bug", OPT_INT | HAS_ARG | OPT_EXPERT, { &workaround_bugs }, "workaround bugs", "" },
3223 { "fast", OPT_BOOL | OPT_EXPERT, { &fast }, "non spec compliant optimizations", "" },
3224 { "genpts", OPT_BOOL | OPT_EXPERT, { &genpts }, "generate pts", "" },
3225 { "drp", OPT_INT | HAS_ARG | OPT_EXPERT, { &decoder_reorder_pts }, "let decoder reorder pts 0=off 1=on -1=auto", ""},
3226 { "lowres", OPT_INT | HAS_ARG | OPT_EXPERT, { &lowres }, "", "" },
3227 { "skiploop", OPT_INT | HAS_ARG | OPT_EXPERT, { &skip_loop_filter }, "", "" },
3228 { "skipframe", OPT_INT | HAS_ARG | OPT_EXPERT, { &skip_frame }, "", "" },
3229 { "skipidct", OPT_INT | HAS_ARG | OPT_EXPERT, { &skip_idct }, "", "" },
3230 { "idct", OPT_INT | HAS_ARG | OPT_EXPERT, { &idct }, "set idct algo", "algo" },
3231 { "ec", OPT_INT | HAS_ARG | OPT_EXPERT, { &error_concealment }, "set error concealment options", "bit_mask" },
3232 { "sync", HAS_ARG | OPT_EXPERT, { .func_arg = opt_sync }, "set audio-video sync. type (type=audio/video/ext)", "type" },
3233 { "autoexit", OPT_BOOL | OPT_EXPERT, { &autoexit }, "exit at the end", "" },
3234 { "exitonkeydown", OPT_BOOL | OPT_EXPERT, { &exit_on_keydown }, "exit on key down", "" },
3235 { "exitonmousedown", OPT_BOOL | OPT_EXPERT, { &exit_on_mousedown }, "exit on mouse down", "" },
3236 { "loop", OPT_INT | HAS_ARG | OPT_EXPERT, { &loop }, "set number of times the playback shall be looped", "loop count" },
3237 { "framedrop", OPT_BOOL | OPT_EXPERT, { &framedrop }, "drop frames when cpu is too slow", "" },
3238 { "infbuf", OPT_BOOL | OPT_EXPERT, { &infinite_buffer }, "don't limit the input buffer size (useful with realtime streams)", "" },
3239 { "window_title", OPT_STRING | HAS_ARG, { &window_title }, "set window title", "window title" },
3241 { "vf", OPT_STRING | HAS_ARG, { &vfilters }, "set video filters", "filter_graph" },
3243 { "rdftspeed", OPT_INT | HAS_ARG| OPT_AUDIO | OPT_EXPERT, { &rdftspeed }, "rdft speed", "msecs" },
3244 { "showmode", HAS_ARG, { .func_arg = opt_show_mode}, "select show mode (0 = video, 1 = waves, 2 = RDFT)", "mode" },
3245 { "default", HAS_ARG | OPT_AUDIO | OPT_VIDEO | OPT_EXPERT, { .func_arg = opt_default }, "generic catch all option", "" },
3246 { "i", OPT_BOOL, { &dummy}, "read specified file", "input_file"},
3247 { "codec", HAS_ARG, { .func_arg = opt_codec}, "force decoder", "decoder_name" },
3248 { "acodec", HAS_ARG | OPT_STRING | OPT_EXPERT, { &audio_codec_name }, "force audio decoder", "decoder_name" },
3249 { "scodec", HAS_ARG | OPT_STRING | OPT_EXPERT, { &subtitle_codec_name }, "force subtitle decoder", "decoder_name" },
3250 { "vcodec", HAS_ARG | OPT_STRING | OPT_EXPERT, { &video_codec_name }, "force video decoder", "decoder_name" },
3254 static void show_usage(void)
3256 av_log(NULL, AV_LOG_INFO, "Simple media player\n");
3257 av_log(NULL, AV_LOG_INFO, "usage: %s [options] input_file\n", program_name);
3258 av_log(NULL, AV_LOG_INFO, "\n");
3261 void show_help_default(const char *opt, const char *arg)
3263 av_log_set_callback(log_callback_help);
3265 show_help_options(options, "Main options:", 0, OPT_EXPERT, 0);
3266 show_help_options(options, "Advanced options:", OPT_EXPERT, 0, 0);
3268 show_help_children(avcodec_get_class(), AV_OPT_FLAG_DECODING_PARAM);
3269 show_help_children(avformat_get_class(), AV_OPT_FLAG_DECODING_PARAM);
3270 #if !CONFIG_AVFILTER
3271 show_help_children(sws_get_class(), AV_OPT_FLAG_ENCODING_PARAM);
3273 show_help_children(avfilter_get_class(), AV_OPT_FLAG_FILTERING_PARAM);
3275 printf("\nWhile playing:\n"
3277 "f toggle full screen\n"
3279 "a cycle audio channel\n"
3280 "v cycle video channel\n"
3281 "t cycle subtitle channel\n"
3282 "w show audio waves\n"
3283 "s activate frame-step mode\n"
3284 "left/right seek backward/forward 10 seconds\n"
3285 "down/up seek backward/forward 1 minute\n"
3286 "page down/page up seek backward/forward 10 minutes\n"
3287 "mouse click seek to percentage in file corresponding to fraction of width\n"
3291 static int lockmgr(void **mtx, enum AVLockOp op)
3294 case AV_LOCK_CREATE:
3295 *mtx = SDL_CreateMutex();
3299 case AV_LOCK_OBTAIN:
3300 return !!SDL_LockMutex(*mtx);
3301 case AV_LOCK_RELEASE:
3302 return !!SDL_UnlockMutex(*mtx);
3303 case AV_LOCK_DESTROY:
3304 SDL_DestroyMutex(*mtx);
3310 /* Called from the main */
3311 int main(int argc, char **argv)
3315 char dummy_videodriver[] = "SDL_VIDEODRIVER=dummy";
3317 av_log_set_flags(AV_LOG_SKIP_REPEATED);
3318 parse_loglevel(argc, argv, options);
3320 /* register all codecs, demux and protocols */
3321 avcodec_register_all();
3323 avdevice_register_all();
3326 avfilter_register_all();
3329 avformat_network_init();
3333 signal(SIGINT , sigterm_handler); /* Interrupt (ANSI). */
3334 signal(SIGTERM, sigterm_handler); /* Termination (ANSI). */
3336 show_banner(argc, argv, options);
3338 parse_options(NULL, argc, argv, options, opt_input_file);
3340 if (!input_filename) {
3342 fprintf(stderr, "An input file must be specified\n");
3343 fprintf(stderr, "Use -h to get full help or, even better, run 'man %s'\n", program_name);
3347 if (display_disable) {
3350 flags = SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER;
3352 flags &= ~SDL_INIT_AUDIO;
3353 if (display_disable)
3354 SDL_putenv(dummy_videodriver); /* For the event queue, we always need a video driver. */
3355 #if !defined(__MINGW32__) && !defined(__APPLE__)
3356 flags |= SDL_INIT_EVENTTHREAD; /* Not supported on Windows or Mac OS X */
3358 if (SDL_Init (flags)) {
3359 fprintf(stderr, "Could not initialize SDL - %s\n", SDL_GetError());
3360 fprintf(stderr, "(Did you set the DISPLAY variable?)\n");
3364 if (!display_disable) {
3365 #if HAVE_SDL_VIDEO_SIZE
3366 const SDL_VideoInfo *vi = SDL_GetVideoInfo();
3367 fs_screen_width = vi->current_w;
3368 fs_screen_height = vi->current_h;
3372 SDL_EventState(SDL_ACTIVEEVENT, SDL_IGNORE);
3373 SDL_EventState(SDL_SYSWMEVENT, SDL_IGNORE);
3374 SDL_EventState(SDL_USEREVENT, SDL_IGNORE);
3376 if (av_lockmgr_register(lockmgr)) {
3377 fprintf(stderr, "Could not initialize lock manager!\n");
3381 av_init_packet(&flush_pkt);
3382 flush_pkt.data = (char *)(intptr_t)"FLUSH";
3384 is = stream_open(input_filename, file_iformat);
3386 fprintf(stderr, "Failed to initialize VideoState!\n");