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 */
131 typedef struct SubPicture {
132 double pts; /* presentation time stamp for this picture */
136 typedef struct AudioParams {
139 int64_t channel_layout;
140 enum AVSampleFormat fmt;
144 AV_SYNC_AUDIO_MASTER, /* default choice */
145 AV_SYNC_VIDEO_MASTER,
146 AV_SYNC_EXTERNAL_CLOCK, /* synchronize to an external clock */
149 typedef struct VideoState {
150 SDL_Thread *read_tid;
151 SDL_Thread *video_tid;
152 AVInputFormat *iformat;
158 int queue_attachments_req;
163 int read_pause_return;
170 double external_clock; ///< external clock base
171 double external_clock_drift; ///< external clock base - time (av_gettime) at which we updated external_clock
172 int64_t external_clock_time; ///< last reference time
173 double external_clock_speed; ///< speed of the external clock
176 int audio_clock_serial;
177 double audio_diff_cum; /* used for AV difference average computation */
178 double audio_diff_avg_coef;
179 double audio_diff_threshold;
180 int audio_diff_avg_count;
183 int audio_hw_buf_size;
184 uint8_t silence_buf[SDL_AUDIO_BUFFER_SIZE];
187 unsigned int audio_buf_size; /* in bytes */
188 unsigned int audio_buf1_size;
189 int audio_buf_index; /* in bytes */
190 int audio_write_buf_size;
191 AVPacket audio_pkt_temp;
193 int audio_pkt_temp_serial;
194 struct AudioParams audio_src;
195 struct AudioParams audio_tgt;
196 struct SwrContext *swr_ctx;
197 double audio_current_pts;
198 double audio_current_pts_drift;
199 int frame_drops_early;
200 int frame_drops_late;
204 SHOW_MODE_NONE = -1, SHOW_MODE_VIDEO = 0, SHOW_MODE_WAVES, SHOW_MODE_RDFT, SHOW_MODE_NB
206 int16_t sample_array[SAMPLE_ARRAY_SIZE];
207 int sample_array_index;
211 FFTSample *rdft_data;
213 double last_vis_time;
215 SDL_Thread *subtitle_tid;
217 int subtitle_stream_changed;
218 AVStream *subtitle_st;
219 PacketQueue subtitleq;
220 SubPicture subpq[SUBPICTURE_QUEUE_SIZE];
221 int subpq_size, subpq_rindex, subpq_windex;
222 SDL_mutex *subpq_mutex;
223 SDL_cond *subpq_cond;
226 double frame_last_pts;
227 double frame_last_duration;
228 double frame_last_dropped_pts;
229 double frame_last_returned_time;
230 double frame_last_filter_delay;
231 int64_t frame_last_dropped_pos;
235 double video_current_pts; // current displayed pts
236 double video_current_pts_drift; // video_current_pts - time (av_gettime) at which we updated video_current_pts - used to have running video pts
237 int64_t video_current_pos; // current displayed file pos
238 double max_frame_duration; // maximum duration of a frame - above this, we consider the jump a timestamp discontinuity
239 int video_clock_serial;
240 VideoPicture pictq[VIDEO_PICTURE_QUEUE_SIZE];
241 int pictq_size, pictq_rindex, pictq_windex;
242 SDL_mutex *pictq_mutex;
243 SDL_cond *pictq_cond;
245 struct SwsContext *img_convert_ctx;
247 SDL_Rect last_display_rect;
250 int width, height, xleft, ytop;
254 AVFilterContext *in_video_filter; // the first filter in the video chain
255 AVFilterContext *out_video_filter; // the last filter in the video chain
258 int last_video_stream, last_audio_stream, last_subtitle_stream;
260 SDL_cond *continue_read_thread;
263 /* options specified by the user */
264 static AVInputFormat *file_iformat;
265 static const char *input_filename;
266 static const char *window_title;
267 static int fs_screen_width;
268 static int fs_screen_height;
269 static int default_width = 640;
270 static int default_height = 480;
271 static int screen_width = 0;
272 static int screen_height = 0;
273 static int audio_disable;
274 static int video_disable;
275 static int subtitle_disable;
276 static int wanted_stream[AVMEDIA_TYPE_NB] = {
277 [AVMEDIA_TYPE_AUDIO] = -1,
278 [AVMEDIA_TYPE_VIDEO] = -1,
279 [AVMEDIA_TYPE_SUBTITLE] = -1,
281 static int seek_by_bytes = -1;
282 static int display_disable;
283 static int show_status = 1;
284 static int av_sync_type = AV_SYNC_AUDIO_MASTER;
285 static int64_t start_time = AV_NOPTS_VALUE;
286 static int64_t duration = AV_NOPTS_VALUE;
287 static int workaround_bugs = 1;
289 static int genpts = 0;
290 static int lowres = 0;
291 static int idct = FF_IDCT_AUTO;
292 static enum AVDiscard skip_frame = AVDISCARD_DEFAULT;
293 static enum AVDiscard skip_idct = AVDISCARD_DEFAULT;
294 static enum AVDiscard skip_loop_filter = AVDISCARD_DEFAULT;
295 static int error_concealment = 3;
296 static int decoder_reorder_pts = -1;
298 static int exit_on_keydown;
299 static int exit_on_mousedown;
301 static int framedrop = -1;
302 static int infinite_buffer = -1;
303 static enum ShowMode show_mode = SHOW_MODE_NONE;
304 static const char *audio_codec_name;
305 static const char *subtitle_codec_name;
306 static const char *video_codec_name;
307 double rdftspeed = 0.02;
308 static int64_t cursor_last_shown;
309 static int cursor_hidden = 0;
311 static char *vfilters = NULL;
314 /* current context */
315 static int is_full_screen;
316 static int64_t audio_callback_time;
318 static AVPacket flush_pkt;
320 #define FF_ALLOC_EVENT (SDL_USEREVENT)
321 #define FF_QUIT_EVENT (SDL_USEREVENT + 2)
323 static SDL_Surface *screen;
325 static int packet_queue_put(PacketQueue *q, AVPacket *pkt);
327 static int packet_queue_put_private(PacketQueue *q, AVPacket *pkt)
329 MyAVPacketList *pkt1;
331 if (q->abort_request)
334 pkt1 = av_malloc(sizeof(MyAVPacketList));
339 if (pkt == &flush_pkt)
341 pkt1->serial = q->serial;
346 q->last_pkt->next = pkt1;
349 q->size += pkt1->pkt.size + sizeof(*pkt1);
350 /* XXX: should duplicate packet data in DV case */
351 SDL_CondSignal(q->cond);
355 static int packet_queue_put(PacketQueue *q, AVPacket *pkt)
359 /* duplicate the packet */
360 if (pkt != &flush_pkt && av_dup_packet(pkt) < 0)
363 SDL_LockMutex(q->mutex);
364 ret = packet_queue_put_private(q, pkt);
365 SDL_UnlockMutex(q->mutex);
367 if (pkt != &flush_pkt && ret < 0)
373 /* packet queue handling */
374 static void packet_queue_init(PacketQueue *q)
376 memset(q, 0, sizeof(PacketQueue));
377 q->mutex = SDL_CreateMutex();
378 q->cond = SDL_CreateCond();
379 q->abort_request = 1;
382 static void packet_queue_flush(PacketQueue *q)
384 MyAVPacketList *pkt, *pkt1;
386 SDL_LockMutex(q->mutex);
387 for (pkt = q->first_pkt; pkt != NULL; pkt = pkt1) {
389 av_free_packet(&pkt->pkt);
396 SDL_UnlockMutex(q->mutex);
399 static void packet_queue_destroy(PacketQueue *q)
401 packet_queue_flush(q);
402 SDL_DestroyMutex(q->mutex);
403 SDL_DestroyCond(q->cond);
406 static void packet_queue_abort(PacketQueue *q)
408 SDL_LockMutex(q->mutex);
410 q->abort_request = 1;
412 SDL_CondSignal(q->cond);
414 SDL_UnlockMutex(q->mutex);
417 static void packet_queue_start(PacketQueue *q)
419 SDL_LockMutex(q->mutex);
420 q->abort_request = 0;
421 packet_queue_put_private(q, &flush_pkt);
422 SDL_UnlockMutex(q->mutex);
425 /* return < 0 if aborted, 0 if no packet and > 0 if packet. */
426 static int packet_queue_get(PacketQueue *q, AVPacket *pkt, int block, int *serial)
428 MyAVPacketList *pkt1;
431 SDL_LockMutex(q->mutex);
434 if (q->abort_request) {
441 q->first_pkt = pkt1->next;
445 q->size -= pkt1->pkt.size + sizeof(*pkt1);
448 *serial = pkt1->serial;
456 SDL_CondWait(q->cond, q->mutex);
459 SDL_UnlockMutex(q->mutex);
463 static inline void fill_rectangle(SDL_Surface *screen,
464 int x, int y, int w, int h, int color, int update)
471 SDL_FillRect(screen, &rect, color);
472 if (update && w > 0 && h > 0)
473 SDL_UpdateRect(screen, x, y, w, h);
476 /* draw only the border of a rectangle */
477 static void fill_border(int xleft, int ytop, int width, int height, int x, int y, int w, int h, int color, int update)
481 /* fill the background */
485 w2 = width - (x + w);
491 h2 = height - (y + h);
494 fill_rectangle(screen,
498 fill_rectangle(screen,
499 xleft + width - w2, ytop,
502 fill_rectangle(screen,
506 fill_rectangle(screen,
507 xleft + w1, ytop + height - h2,
512 #define ALPHA_BLEND(a, oldp, newp, s)\
513 ((((oldp << s) * (255 - (a))) + (newp * (a))) / (255 << s))
515 #define RGBA_IN(r, g, b, a, s)\
517 unsigned int v = ((const uint32_t *)(s))[0];\
518 a = (v >> 24) & 0xff;\
519 r = (v >> 16) & 0xff;\
520 g = (v >> 8) & 0xff;\
524 #define YUVA_IN(y, u, v, a, s, pal)\
526 unsigned int val = ((const uint32_t *)(pal))[*(const uint8_t*)(s)];\
527 a = (val >> 24) & 0xff;\
528 y = (val >> 16) & 0xff;\
529 u = (val >> 8) & 0xff;\
533 #define YUVA_OUT(d, y, u, v, a)\
535 ((uint32_t *)(d))[0] = (a << 24) | (y << 16) | (u << 8) | v;\
541 static void blend_subrect(AVPicture *dst, const AVSubtitleRect *rect, int imgw, int imgh)
543 int wrap, wrap3, width2, skip2;
544 int y, u, v, a, u1, v1, a1, w, h;
545 uint8_t *lum, *cb, *cr;
548 int dstx, dsty, dstw, dsth;
550 dstw = av_clip(rect->w, 0, imgw);
551 dsth = av_clip(rect->h, 0, imgh);
552 dstx = av_clip(rect->x, 0, imgw - dstw);
553 dsty = av_clip(rect->y, 0, imgh - dsth);
554 lum = dst->data[0] + dsty * dst->linesize[0];
555 cb = dst->data[1] + (dsty >> 1) * dst->linesize[1];
556 cr = dst->data[2] + (dsty >> 1) * dst->linesize[2];
558 width2 = ((dstw + 1) >> 1) + (dstx & ~dstw & 1);
560 wrap = dst->linesize[0];
561 wrap3 = rect->pict.linesize[0];
562 p = rect->pict.data[0];
563 pal = (const uint32_t *)rect->pict.data[1]; /* Now in YCrCb! */
571 YUVA_IN(y, u, v, a, p, pal);
572 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
573 cb[0] = ALPHA_BLEND(a >> 2, cb[0], u, 0);
574 cr[0] = ALPHA_BLEND(a >> 2, cr[0], v, 0);
580 for (w = dstw - (dstx & 1); w >= 2; w -= 2) {
581 YUVA_IN(y, u, v, a, p, pal);
585 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
587 YUVA_IN(y, u, v, a, p + BPP, pal);
591 lum[1] = ALPHA_BLEND(a, lum[1], y, 0);
592 cb[0] = ALPHA_BLEND(a1 >> 2, cb[0], u1, 1);
593 cr[0] = ALPHA_BLEND(a1 >> 2, cr[0], v1, 1);
600 YUVA_IN(y, u, v, a, p, pal);
601 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
602 cb[0] = ALPHA_BLEND(a >> 2, cb[0], u, 0);
603 cr[0] = ALPHA_BLEND(a >> 2, cr[0], v, 0);
607 p += wrap3 - dstw * BPP;
608 lum += wrap - dstw - dstx;
609 cb += dst->linesize[1] - width2 - skip2;
610 cr += dst->linesize[2] - width2 - skip2;
612 for (h = dsth - (dsty & 1); h >= 2; h -= 2) {
618 YUVA_IN(y, u, v, a, p, pal);
622 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
625 YUVA_IN(y, u, v, a, p, pal);
629 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
630 cb[0] = ALPHA_BLEND(a1 >> 2, cb[0], u1, 1);
631 cr[0] = ALPHA_BLEND(a1 >> 2, cr[0], v1, 1);
637 for (w = dstw - (dstx & 1); w >= 2; w -= 2) {
638 YUVA_IN(y, u, v, a, p, pal);
642 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
644 YUVA_IN(y, u, v, a, p + BPP, pal);
648 lum[1] = ALPHA_BLEND(a, lum[1], y, 0);
652 YUVA_IN(y, u, v, a, p, pal);
656 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
658 YUVA_IN(y, u, v, a, p + BPP, pal);
662 lum[1] = ALPHA_BLEND(a, lum[1], y, 0);
664 cb[0] = ALPHA_BLEND(a1 >> 2, cb[0], u1, 2);
665 cr[0] = ALPHA_BLEND(a1 >> 2, cr[0], v1, 2);
669 p += -wrap3 + 2 * BPP;
673 YUVA_IN(y, u, v, a, p, pal);
677 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
680 YUVA_IN(y, u, v, a, p, pal);
684 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
685 cb[0] = ALPHA_BLEND(a1 >> 2, cb[0], u1, 1);
686 cr[0] = ALPHA_BLEND(a1 >> 2, cr[0], v1, 1);
692 p += wrap3 + (wrap3 - dstw * BPP);
693 lum += wrap + (wrap - dstw - dstx);
694 cb += dst->linesize[1] - width2 - skip2;
695 cr += dst->linesize[2] - width2 - skip2;
697 /* handle odd height */
704 YUVA_IN(y, u, v, a, p, pal);
705 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
706 cb[0] = ALPHA_BLEND(a >> 2, cb[0], u, 0);
707 cr[0] = ALPHA_BLEND(a >> 2, cr[0], v, 0);
713 for (w = dstw - (dstx & 1); w >= 2; w -= 2) {
714 YUVA_IN(y, u, v, a, p, pal);
718 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
720 YUVA_IN(y, u, v, a, p + BPP, pal);
724 lum[1] = ALPHA_BLEND(a, lum[1], y, 0);
725 cb[0] = ALPHA_BLEND(a1 >> 2, cb[0], u, 1);
726 cr[0] = ALPHA_BLEND(a1 >> 2, cr[0], v, 1);
733 YUVA_IN(y, u, v, a, p, pal);
734 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
735 cb[0] = ALPHA_BLEND(a >> 2, cb[0], u, 0);
736 cr[0] = ALPHA_BLEND(a >> 2, cr[0], v, 0);
741 static void free_subpicture(SubPicture *sp)
743 avsubtitle_free(&sp->sub);
746 static void calculate_display_rect(SDL_Rect *rect, int scr_xleft, int scr_ytop, int scr_width, int scr_height, VideoPicture *vp)
749 int width, height, x, y;
751 if (vp->sar.num == 0)
754 aspect_ratio = av_q2d(vp->sar);
756 if (aspect_ratio <= 0.0)
758 aspect_ratio *= (float)vp->width / (float)vp->height;
760 /* XXX: we suppose the screen has a 1.0 pixel ratio */
762 width = ((int)rint(height * aspect_ratio)) & ~1;
763 if (width > scr_width) {
765 height = ((int)rint(width / aspect_ratio)) & ~1;
767 x = (scr_width - width) / 2;
768 y = (scr_height - height) / 2;
769 rect->x = scr_xleft + x;
770 rect->y = scr_ytop + y;
771 rect->w = FFMAX(width, 1);
772 rect->h = FFMAX(height, 1);
775 static void video_image_display(VideoState *is)
783 vp = &is->pictq[is->pictq_rindex];
785 if (is->subtitle_st) {
786 if (is->subpq_size > 0) {
787 sp = &is->subpq[is->subpq_rindex];
789 if (vp->pts >= sp->pts + ((float) sp->sub.start_display_time / 1000)) {
790 SDL_LockYUVOverlay (vp->bmp);
792 pict.data[0] = vp->bmp->pixels[0];
793 pict.data[1] = vp->bmp->pixels[2];
794 pict.data[2] = vp->bmp->pixels[1];
796 pict.linesize[0] = vp->bmp->pitches[0];
797 pict.linesize[1] = vp->bmp->pitches[2];
798 pict.linesize[2] = vp->bmp->pitches[1];
800 for (i = 0; i < sp->sub.num_rects; i++)
801 blend_subrect(&pict, sp->sub.rects[i],
802 vp->bmp->w, vp->bmp->h);
804 SDL_UnlockYUVOverlay (vp->bmp);
809 calculate_display_rect(&rect, is->xleft, is->ytop, is->width, is->height, vp);
811 SDL_DisplayYUVOverlay(vp->bmp, &rect);
813 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) {
814 int bgcolor = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00);
815 fill_border(is->xleft, is->ytop, is->width, is->height, rect.x, rect.y, rect.w, rect.h, bgcolor, 1);
816 is->last_display_rect = rect;
821 static inline int compute_mod(int a, int b)
823 return a < 0 ? a%b + b : a%b;
826 static void video_audio_display(VideoState *s)
828 int i, i_start, x, y1, y, ys, delay, n, nb_display_channels;
829 int ch, channels, h, h2, bgcolor, fgcolor;
831 int rdft_bits, nb_freq;
833 for (rdft_bits = 1; (1 << rdft_bits) < 2 * s->height; rdft_bits++)
835 nb_freq = 1 << (rdft_bits - 1);
837 /* compute display index : center on currently output samples */
838 channels = s->audio_tgt.channels;
839 nb_display_channels = channels;
841 int data_used= s->show_mode == SHOW_MODE_WAVES ? s->width : (2*nb_freq);
843 delay = s->audio_write_buf_size;
846 /* to be more precise, we take into account the time spent since
847 the last buffer computation */
848 if (audio_callback_time) {
849 time_diff = av_gettime() - audio_callback_time;
850 delay -= (time_diff * s->audio_tgt.freq) / 1000000;
853 delay += 2 * data_used;
854 if (delay < data_used)
857 i_start= x = compute_mod(s->sample_array_index - delay * channels, SAMPLE_ARRAY_SIZE);
858 if (s->show_mode == SHOW_MODE_WAVES) {
860 for (i = 0; i < 1000; i += channels) {
861 int idx = (SAMPLE_ARRAY_SIZE + x - i) % SAMPLE_ARRAY_SIZE;
862 int a = s->sample_array[idx];
863 int b = s->sample_array[(idx + 4 * channels) % SAMPLE_ARRAY_SIZE];
864 int c = s->sample_array[(idx + 5 * channels) % SAMPLE_ARRAY_SIZE];
865 int d = s->sample_array[(idx + 9 * channels) % SAMPLE_ARRAY_SIZE];
867 if (h < score && (b ^ c) < 0) {
874 s->last_i_start = i_start;
876 i_start = s->last_i_start;
879 bgcolor = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00);
880 if (s->show_mode == SHOW_MODE_WAVES) {
881 fill_rectangle(screen,
882 s->xleft, s->ytop, s->width, s->height,
885 fgcolor = SDL_MapRGB(screen->format, 0xff, 0xff, 0xff);
887 /* total height for one channel */
888 h = s->height / nb_display_channels;
889 /* graph height / 2 */
891 for (ch = 0; ch < nb_display_channels; ch++) {
893 y1 = s->ytop + ch * h + (h / 2); /* position of center line */
894 for (x = 0; x < s->width; x++) {
895 y = (s->sample_array[i] * h2) >> 15;
902 fill_rectangle(screen,
903 s->xleft + x, ys, 1, y,
906 if (i >= SAMPLE_ARRAY_SIZE)
907 i -= SAMPLE_ARRAY_SIZE;
911 fgcolor = SDL_MapRGB(screen->format, 0x00, 0x00, 0xff);
913 for (ch = 1; ch < nb_display_channels; ch++) {
914 y = s->ytop + ch * h;
915 fill_rectangle(screen,
916 s->xleft, y, s->width, 1,
919 SDL_UpdateRect(screen, s->xleft, s->ytop, s->width, s->height);
921 nb_display_channels= FFMIN(nb_display_channels, 2);
922 if (rdft_bits != s->rdft_bits) {
923 av_rdft_end(s->rdft);
924 av_free(s->rdft_data);
925 s->rdft = av_rdft_init(rdft_bits, DFT_R2C);
926 s->rdft_bits = rdft_bits;
927 s->rdft_data = av_malloc(4 * nb_freq * sizeof(*s->rdft_data));
931 for (ch = 0; ch < nb_display_channels; ch++) {
932 data[ch] = s->rdft_data + 2 * nb_freq * ch;
934 for (x = 0; x < 2 * nb_freq; x++) {
935 double w = (x-nb_freq) * (1.0 / nb_freq);
936 data[ch][x] = s->sample_array[i] * (1.0 - w * w);
938 if (i >= SAMPLE_ARRAY_SIZE)
939 i -= SAMPLE_ARRAY_SIZE;
941 av_rdft_calc(s->rdft, data[ch]);
943 // least efficient way to do this, we should of course directly access it but its more than fast enough
944 for (y = 0; y < s->height; y++) {
945 double w = 1 / sqrt(nb_freq);
946 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]));
947 int b = (nb_display_channels == 2 ) ? sqrt(w * sqrt(data[1][2 * y + 0] * data[1][2 * y + 0]
948 + data[1][2 * y + 1] * data[1][2 * y + 1])) : a;
951 fgcolor = SDL_MapRGB(screen->format, a, b, (a + b) / 2);
953 fill_rectangle(screen,
954 s->xpos, s->height-y, 1, 1,
958 SDL_UpdateRect(screen, s->xpos, s->ytop, 1, s->height);
961 if (s->xpos >= s->width)
966 static void stream_close(VideoState *is)
970 /* XXX: use a special url_shutdown call to abort parse cleanly */
971 is->abort_request = 1;
972 SDL_WaitThread(is->read_tid, NULL);
973 packet_queue_destroy(&is->videoq);
974 packet_queue_destroy(&is->audioq);
975 packet_queue_destroy(&is->subtitleq);
977 /* free all pictures */
978 for (i = 0; i < VIDEO_PICTURE_QUEUE_SIZE; i++) {
981 SDL_FreeYUVOverlay(vp->bmp);
985 SDL_DestroyMutex(is->pictq_mutex);
986 SDL_DestroyCond(is->pictq_cond);
987 SDL_DestroyMutex(is->subpq_mutex);
988 SDL_DestroyCond(is->subpq_cond);
989 SDL_DestroyCond(is->continue_read_thread);
991 sws_freeContext(is->img_convert_ctx);
996 static void do_exit(VideoState *is)
1001 av_lockmgr_register(NULL);
1005 av_freep(&vfilters);
1007 avformat_network_deinit();
1011 av_log(NULL, AV_LOG_QUIET, "%s", "");
1015 static void sigterm_handler(int sig)
1020 static int video_open(VideoState *is, int force_set_video_mode, VideoPicture *vp)
1022 int flags = SDL_HWSURFACE | SDL_ASYNCBLIT | SDL_HWACCEL;
1026 if (is_full_screen) flags |= SDL_FULLSCREEN;
1027 else flags |= SDL_RESIZABLE;
1029 if (vp && vp->width) {
1030 calculate_display_rect(&rect, 0, 0, INT_MAX, vp->height, vp);
1031 default_width = rect.w;
1032 default_height = rect.h;
1035 if (is_full_screen && fs_screen_width) {
1036 w = fs_screen_width;
1037 h = fs_screen_height;
1038 } else if (!is_full_screen && screen_width) {
1045 if (screen && is->width == screen->w && screen->w == w
1046 && is->height== screen->h && screen->h == h && !force_set_video_mode)
1048 screen = SDL_SetVideoMode(w, h, 0, flags);
1050 fprintf(stderr, "SDL: could not set video mode - exiting\n");
1054 window_title = input_filename;
1055 SDL_WM_SetCaption(window_title, window_title);
1057 is->width = screen->w;
1058 is->height = screen->h;
1063 /* display the current picture, if any */
1064 static void video_display(VideoState *is)
1067 video_open(is, 0, NULL);
1068 if (is->audio_st && is->show_mode != SHOW_MODE_VIDEO)
1069 video_audio_display(is);
1070 else if (is->video_st)
1071 video_image_display(is);
1074 /* get the current audio clock value */
1075 static double get_audio_clock(VideoState *is)
1077 if (is->audio_clock_serial != is->audioq.serial)
1080 return is->audio_current_pts;
1082 return is->audio_current_pts_drift + av_gettime() / 1000000.0;
1086 /* get the current video clock value */
1087 static double get_video_clock(VideoState *is)
1089 if (is->video_clock_serial != is->videoq.serial)
1092 return is->video_current_pts;
1094 return is->video_current_pts_drift + av_gettime() / 1000000.0;
1098 /* get the current external clock value */
1099 static double get_external_clock(VideoState *is)
1102 return is->external_clock;
1104 double time = av_gettime() / 1000000.0;
1105 return is->external_clock_drift + time - (time - is->external_clock_time / 1000000.0) * (1.0 - is->external_clock_speed);
1109 static int get_master_sync_type(VideoState *is) {
1110 if (is->av_sync_type == AV_SYNC_VIDEO_MASTER) {
1112 return AV_SYNC_VIDEO_MASTER;
1114 return AV_SYNC_AUDIO_MASTER;
1115 } else if (is->av_sync_type == AV_SYNC_AUDIO_MASTER) {
1117 return AV_SYNC_AUDIO_MASTER;
1119 return AV_SYNC_EXTERNAL_CLOCK;
1121 return AV_SYNC_EXTERNAL_CLOCK;
1125 /* get the current master clock value */
1126 static double get_master_clock(VideoState *is)
1130 switch (get_master_sync_type(is)) {
1131 case AV_SYNC_VIDEO_MASTER:
1132 val = get_video_clock(is);
1134 case AV_SYNC_AUDIO_MASTER:
1135 val = get_audio_clock(is);
1138 val = get_external_clock(is);
1144 static void update_external_clock_pts(VideoState *is, double pts)
1146 is->external_clock_time = av_gettime();
1147 is->external_clock = pts;
1148 is->external_clock_drift = pts - is->external_clock_time / 1000000.0;
1151 static void check_external_clock_sync(VideoState *is, double pts) {
1152 double ext_clock = get_external_clock(is);
1153 if (isnan(ext_clock) || fabs(ext_clock - 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;
1187 SDL_CondSignal(is->continue_read_thread);
1191 /* pause or resume the video */
1192 static void stream_toggle_pause(VideoState *is)
1195 is->frame_timer += av_gettime() / 1000000.0 + is->video_current_pts_drift - is->video_current_pts;
1196 if (is->read_pause_return != AVERROR(ENOSYS)) {
1197 is->video_current_pts = is->video_current_pts_drift + av_gettime() / 1000000.0;
1199 is->video_current_pts_drift = is->video_current_pts - av_gettime() / 1000000.0;
1201 update_external_clock_pts(is, get_external_clock(is));
1202 is->paused = !is->paused;
1205 static void toggle_pause(VideoState *is)
1207 stream_toggle_pause(is);
1211 static void step_to_next_frame(VideoState *is)
1213 /* if the stream is paused unpause it, then step */
1215 stream_toggle_pause(is);
1219 static double compute_target_delay(double delay, VideoState *is)
1221 double sync_threshold, diff;
1223 /* update delay to follow master synchronisation source */
1224 if (get_master_sync_type(is) != AV_SYNC_VIDEO_MASTER) {
1225 /* if video is slave, we try to correct big delays by
1226 duplicating or deleting a frame */
1227 diff = get_video_clock(is) - get_master_clock(is);
1229 /* skip or repeat frame. We take into account the
1230 delay to compute the threshold. I still don't know
1231 if it is the best guess */
1232 sync_threshold = FFMAX(AV_SYNC_THRESHOLD, delay);
1233 if (!isnan(diff) && fabs(diff) < AV_NOSYNC_THRESHOLD) {
1234 if (diff <= -sync_threshold)
1236 else if (diff >= sync_threshold)
1241 av_dlog(NULL, "video: delay=%0.3f A-V=%f\n",
1247 static void pictq_next_picture(VideoState *is) {
1248 /* update queue size and signal for next picture */
1249 if (++is->pictq_rindex == VIDEO_PICTURE_QUEUE_SIZE)
1250 is->pictq_rindex = 0;
1252 SDL_LockMutex(is->pictq_mutex);
1254 SDL_CondSignal(is->pictq_cond);
1255 SDL_UnlockMutex(is->pictq_mutex);
1258 static int pictq_prev_picture(VideoState *is) {
1259 VideoPicture *prevvp;
1261 /* update queue size and signal for the previous picture */
1262 prevvp = &is->pictq[(is->pictq_rindex + VIDEO_PICTURE_QUEUE_SIZE - 1) % VIDEO_PICTURE_QUEUE_SIZE];
1263 if (prevvp->allocated && prevvp->serial == is->videoq.serial) {
1264 SDL_LockMutex(is->pictq_mutex);
1265 if (is->pictq_size < VIDEO_PICTURE_QUEUE_SIZE - 1) {
1266 if (--is->pictq_rindex == -1)
1267 is->pictq_rindex = VIDEO_PICTURE_QUEUE_SIZE - 1;
1271 SDL_CondSignal(is->pictq_cond);
1272 SDL_UnlockMutex(is->pictq_mutex);
1277 static void update_video_pts(VideoState *is, double pts, int64_t pos, int serial) {
1278 double time = av_gettime() / 1000000.0;
1279 /* update current video pts */
1280 is->video_current_pts = pts;
1281 is->video_current_pts_drift = is->video_current_pts - time;
1282 is->video_current_pos = pos;
1283 is->frame_last_pts = pts;
1284 is->video_clock_serial = serial;
1285 if (is->videoq.serial == serial)
1286 check_external_clock_sync(is, is->video_current_pts);
1289 /* called to display each frame */
1290 static void video_refresh(void *opaque, double *remaining_time)
1292 VideoState *is = opaque;
1296 SubPicture *sp, *sp2;
1298 if (!is->paused && get_master_sync_type(is) == AV_SYNC_EXTERNAL_CLOCK && is->realtime)
1299 check_external_clock_speed(is);
1301 if (!display_disable && is->show_mode != SHOW_MODE_VIDEO && is->audio_st) {
1302 time = av_gettime() / 1000000.0;
1303 if (is->force_refresh || is->last_vis_time + rdftspeed < time) {
1305 is->last_vis_time = time;
1307 *remaining_time = FFMIN(*remaining_time, is->last_vis_time + rdftspeed - time);
1312 if (is->force_refresh)
1313 redisplay = pictq_prev_picture(is);
1315 if (is->pictq_size == 0) {
1316 SDL_LockMutex(is->pictq_mutex);
1317 if (is->frame_last_dropped_pts != AV_NOPTS_VALUE && is->frame_last_dropped_pts > is->frame_last_pts) {
1318 update_video_pts(is, is->frame_last_dropped_pts, is->frame_last_dropped_pos, 0);
1319 is->frame_last_dropped_pts = AV_NOPTS_VALUE;
1321 SDL_UnlockMutex(is->pictq_mutex);
1322 // nothing to do, no picture to display in the queue
1324 double last_duration, duration, delay;
1325 /* dequeue the picture */
1326 vp = &is->pictq[is->pictq_rindex];
1328 if (vp->serial != is->videoq.serial) {
1329 pictq_next_picture(is);
1337 /* compute nominal last_duration */
1338 last_duration = vp->pts - is->frame_last_pts;
1339 if (last_duration > 0 && last_duration < is->max_frame_duration) {
1340 /* if duration of the last frame was sane, update last_duration in video state */
1341 is->frame_last_duration = last_duration;
1343 delay = compute_target_delay(is->frame_last_duration, is);
1345 time= av_gettime()/1000000.0;
1346 if (time < is->frame_timer + delay) {
1347 *remaining_time = FFMIN(is->frame_timer + delay - time, *remaining_time);
1352 is->frame_timer += delay * FFMAX(1, floor((time-is->frame_timer) / delay));
1354 SDL_LockMutex(is->pictq_mutex);
1355 update_video_pts(is, vp->pts, vp->pos, vp->serial);
1356 SDL_UnlockMutex(is->pictq_mutex);
1358 if (is->pictq_size > 1) {
1359 VideoPicture *nextvp = &is->pictq[(is->pictq_rindex + 1) % VIDEO_PICTURE_QUEUE_SIZE];
1360 duration = nextvp->pts - vp->pts;
1361 if(!is->step && (redisplay || framedrop>0 || (framedrop && get_master_sync_type(is) != AV_SYNC_VIDEO_MASTER)) && time > is->frame_timer + duration){
1363 is->frame_drops_late++;
1364 pictq_next_picture(is);
1370 if (is->subtitle_st) {
1371 if (is->subtitle_stream_changed) {
1372 SDL_LockMutex(is->subpq_mutex);
1374 while (is->subpq_size) {
1375 free_subpicture(&is->subpq[is->subpq_rindex]);
1377 /* update queue size and signal for next picture */
1378 if (++is->subpq_rindex == SUBPICTURE_QUEUE_SIZE)
1379 is->subpq_rindex = 0;
1383 is->subtitle_stream_changed = 0;
1385 SDL_CondSignal(is->subpq_cond);
1386 SDL_UnlockMutex(is->subpq_mutex);
1388 if (is->subpq_size > 0) {
1389 sp = &is->subpq[is->subpq_rindex];
1391 if (is->subpq_size > 1)
1392 sp2 = &is->subpq[(is->subpq_rindex + 1) % SUBPICTURE_QUEUE_SIZE];
1396 if ((is->video_current_pts > (sp->pts + ((float) sp->sub.end_display_time / 1000)))
1397 || (sp2 && is->video_current_pts > (sp2->pts + ((float) sp2->sub.start_display_time / 1000))))
1399 free_subpicture(sp);
1401 /* update queue size and signal for next picture */
1402 if (++is->subpq_rindex == SUBPICTURE_QUEUE_SIZE)
1403 is->subpq_rindex = 0;
1405 SDL_LockMutex(is->subpq_mutex);
1407 SDL_CondSignal(is->subpq_cond);
1408 SDL_UnlockMutex(is->subpq_mutex);
1415 /* display picture */
1416 if (!display_disable && is->show_mode == SHOW_MODE_VIDEO)
1419 pictq_next_picture(is);
1421 if (is->step && !is->paused)
1422 stream_toggle_pause(is);
1425 is->force_refresh = 0;
1427 static int64_t last_time;
1429 int aqsize, vqsize, sqsize;
1432 cur_time = av_gettime();
1433 if (!last_time || (cur_time - last_time) >= 30000) {
1438 aqsize = is->audioq.size;
1440 vqsize = is->videoq.size;
1441 if (is->subtitle_st)
1442 sqsize = is->subtitleq.size;
1444 if (is->audio_st && is->video_st)
1445 av_diff = get_audio_clock(is) - get_video_clock(is);
1446 printf("%7.2f A-V:%7.3f fd=%4d aq=%5dKB vq=%5dKB sq=%5dB f=%"PRId64"/%"PRId64" \r",
1447 get_master_clock(is),
1449 is->frame_drops_early + is->frame_drops_late,
1453 is->video_st ? is->video_st->codec->pts_correction_num_faulty_dts : 0,
1454 is->video_st ? is->video_st->codec->pts_correction_num_faulty_pts : 0);
1456 last_time = cur_time;
1461 /* allocate a picture (needs to do that in main thread to avoid
1462 potential locking problems */
1463 static void alloc_picture(VideoState *is)
1467 vp = &is->pictq[is->pictq_windex];
1470 SDL_FreeYUVOverlay(vp->bmp);
1472 video_open(is, 0, vp);
1474 vp->bmp = SDL_CreateYUVOverlay(vp->width, vp->height,
1477 if (!vp->bmp || vp->bmp->pitches[0] < vp->width) {
1478 /* SDL allocates a buffer smaller than requested if the video
1479 * overlay hardware is unable to support the requested size. */
1480 fprintf(stderr, "Error: the video system does not support an image\n"
1481 "size of %dx%d pixels. Try using -lowres or -vf \"scale=w:h\"\n"
1482 "to reduce the image size.\n", vp->width, vp->height );
1486 SDL_LockMutex(is->pictq_mutex);
1488 SDL_CondSignal(is->pictq_cond);
1489 SDL_UnlockMutex(is->pictq_mutex);
1492 static void duplicate_right_border_pixels(SDL_Overlay *bmp) {
1493 int i, width, height;
1495 for (i = 0; i < 3; i++) {
1502 if (bmp->pitches[i] > width) {
1503 maxp = bmp->pixels[i] + bmp->pitches[i] * height - 1;
1504 for (p = bmp->pixels[i] + width - 1; p < maxp; p += bmp->pitches[i])
1510 static int queue_picture(VideoState *is, AVFrame *src_frame, double pts, int64_t pos, int serial)
1514 #if defined(DEBUG_SYNC) && 0
1515 printf("frame_type=%c pts=%0.3f\n",
1516 av_get_picture_type_char(src_frame->pict_type), pts);
1519 /* wait until we have space to put a new picture */
1520 SDL_LockMutex(is->pictq_mutex);
1522 /* keep the last already displayed picture in the queue */
1523 while (is->pictq_size >= VIDEO_PICTURE_QUEUE_SIZE - 2 &&
1524 !is->videoq.abort_request) {
1525 SDL_CondWait(is->pictq_cond, is->pictq_mutex);
1527 SDL_UnlockMutex(is->pictq_mutex);
1529 if (is->videoq.abort_request)
1532 vp = &is->pictq[is->pictq_windex];
1535 vp->sar = src_frame->sample_aspect_ratio;
1537 vp->sar = av_guess_sample_aspect_ratio(is->ic, is->video_st, src_frame);
1540 /* alloc or resize hardware picture buffer */
1541 if (!vp->bmp || vp->reallocate || !vp->allocated ||
1542 vp->width != src_frame->width ||
1543 vp->height != src_frame->height) {
1548 vp->width = src_frame->width;
1549 vp->height = src_frame->height;
1551 /* the allocation must be done in the main thread to avoid
1552 locking problems. */
1553 event.type = FF_ALLOC_EVENT;
1554 event.user.data1 = is;
1555 SDL_PushEvent(&event);
1557 /* wait until the picture is allocated */
1558 SDL_LockMutex(is->pictq_mutex);
1559 while (!vp->allocated && !is->videoq.abort_request) {
1560 SDL_CondWait(is->pictq_cond, is->pictq_mutex);
1562 /* if the queue is aborted, we have to pop the pending ALLOC event or wait for the allocation to complete */
1563 if (is->videoq.abort_request && SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_EVENTMASK(FF_ALLOC_EVENT)) != 1) {
1564 while (!vp->allocated) {
1565 SDL_CondWait(is->pictq_cond, is->pictq_mutex);
1568 SDL_UnlockMutex(is->pictq_mutex);
1570 if (is->videoq.abort_request)
1574 /* if the frame is not skipped, then display it */
1576 AVPicture pict = { { 0 } };
1578 /* get a pointer on the bitmap */
1579 SDL_LockYUVOverlay (vp->bmp);
1581 pict.data[0] = vp->bmp->pixels[0];
1582 pict.data[1] = vp->bmp->pixels[2];
1583 pict.data[2] = vp->bmp->pixels[1];
1585 pict.linesize[0] = vp->bmp->pitches[0];
1586 pict.linesize[1] = vp->bmp->pitches[2];
1587 pict.linesize[2] = vp->bmp->pitches[1];
1590 // FIXME use direct rendering
1591 av_picture_copy(&pict, (AVPicture *)src_frame,
1592 src_frame->format, vp->width, vp->height);
1594 av_opt_get_int(sws_opts, "sws_flags", 0, &sws_flags);
1595 is->img_convert_ctx = sws_getCachedContext(is->img_convert_ctx,
1596 vp->width, vp->height, src_frame->format, vp->width, vp->height,
1597 AV_PIX_FMT_YUV420P, sws_flags, NULL, NULL, NULL);
1598 if (is->img_convert_ctx == NULL) {
1599 fprintf(stderr, "Cannot initialize the conversion context\n");
1602 sws_scale(is->img_convert_ctx, src_frame->data, src_frame->linesize,
1603 0, vp->height, pict.data, pict.linesize);
1605 /* workaround SDL PITCH_WORKAROUND */
1606 duplicate_right_border_pixels(vp->bmp);
1607 /* update the bitmap content */
1608 SDL_UnlockYUVOverlay(vp->bmp);
1612 vp->serial = serial;
1614 /* now we can update the picture count */
1615 if (++is->pictq_windex == VIDEO_PICTURE_QUEUE_SIZE)
1616 is->pictq_windex = 0;
1617 SDL_LockMutex(is->pictq_mutex);
1619 SDL_UnlockMutex(is->pictq_mutex);
1624 static int get_video_frame(VideoState *is, AVFrame *frame, int64_t *pts, AVPacket *pkt, int *serial)
1628 if (packet_queue_get(&is->videoq, pkt, 1, serial) < 0)
1631 if (pkt->data == flush_pkt.data) {
1632 avcodec_flush_buffers(is->video_st->codec);
1634 SDL_LockMutex(is->pictq_mutex);
1635 // Make sure there are no long delay timers (ideally we should just flush the queue but that's harder)
1636 while (is->pictq_size && !is->videoq.abort_request) {
1637 SDL_CondWait(is->pictq_cond, is->pictq_mutex);
1639 is->video_current_pos = -1;
1640 is->frame_last_pts = AV_NOPTS_VALUE;
1641 is->frame_last_duration = 0;
1642 is->frame_timer = (double)av_gettime() / 1000000.0;
1643 is->frame_last_dropped_pts = AV_NOPTS_VALUE;
1644 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 (!isnan(clockdiff) && 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("buffersink"),
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 = av_frame_alloc();
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;
1810 while (is->paused && !is->videoq.abort_request)
1813 avcodec_get_frame_defaults(frame);
1814 av_free_packet(&pkt);
1816 ret = get_video_frame(is, frame, &pts_int, &pkt, &serial);
1824 if ( last_w != frame->width
1825 || last_h != frame->height
1826 || last_format != frame->format
1827 || last_serial != serial) {
1828 av_log(NULL, AV_LOG_DEBUG,
1829 "Video frame changed from size:%dx%d format:%s serial:%d to size:%dx%d format:%s serial:%d\n",
1831 (const char *)av_x_if_null(av_get_pix_fmt_name(last_format), "none"), last_serial,
1832 frame->width, frame->height,
1833 (const char *)av_x_if_null(av_get_pix_fmt_name(frame->format), "none"), serial);
1834 avfilter_graph_free(&graph);
1835 graph = avfilter_graph_alloc();
1836 if ((ret = configure_video_filters(graph, is, vfilters, frame)) < 0) {
1838 event.type = FF_QUIT_EVENT;
1839 event.user.data1 = is;
1840 SDL_PushEvent(&event);
1841 av_free_packet(&pkt);
1844 filt_in = is->in_video_filter;
1845 filt_out = is->out_video_filter;
1846 last_w = frame->width;
1847 last_h = frame->height;
1848 last_format = frame->format;
1849 last_serial = serial;
1852 frame->pts = pts_int;
1853 frame->sample_aspect_ratio = av_guess_sample_aspect_ratio(is->ic, is->video_st, frame);
1854 ret = av_buffersrc_add_frame(filt_in, frame);
1857 av_frame_unref(frame);
1858 avcodec_get_frame_defaults(frame);
1859 av_free_packet(&pkt);
1862 is->frame_last_returned_time = av_gettime() / 1000000.0;
1864 ret = av_buffersink_get_frame_flags(filt_out, frame, 0);
1870 is->frame_last_filter_delay = av_gettime() / 1000000.0 - is->frame_last_returned_time;
1871 if (fabs(is->frame_last_filter_delay) > AV_NOSYNC_THRESHOLD / 10.0)
1872 is->frame_last_filter_delay = 0;
1874 pts_int = frame->pts;
1875 tb = filt_out->inputs[0]->time_base;
1876 pos = av_frame_get_pkt_pos(frame);
1877 if (av_cmp_q(tb, is->video_st->time_base)) {
1878 av_unused int64_t pts1 = pts_int;
1879 pts_int = av_rescale_q(pts_int, tb, is->video_st->time_base);
1880 av_dlog(NULL, "video_thread(): "
1881 "tb:%d/%d pts:%"PRId64" -> tb:%d/%d pts:%"PRId64"\n",
1882 tb.num, tb.den, pts1,
1883 is->video_st->time_base.num, is->video_st->time_base.den, pts_int);
1885 pts = pts_int * av_q2d(is->video_st->time_base);
1886 ret = queue_picture(is, frame, pts, pos, serial);
1887 av_frame_unref(frame);
1890 pts = pts_int * av_q2d(is->video_st->time_base);
1891 ret = queue_picture(is, frame, pts, pkt.pos, serial);
1892 av_frame_unref(frame);
1899 avcodec_flush_buffers(is->video_st->codec);
1901 avfilter_graph_free(&graph);
1903 av_free_packet(&pkt);
1904 av_frame_free(&frame);
1908 static int subtitle_thread(void *arg)
1910 VideoState *is = arg;
1912 AVPacket pkt1, *pkt = &pkt1;
1916 int r, g, b, y, u, v, a;
1919 while (is->paused && !is->subtitleq.abort_request) {
1922 if (packet_queue_get(&is->subtitleq, pkt, 1, NULL) < 0)
1925 if (pkt->data == flush_pkt.data) {
1926 avcodec_flush_buffers(is->subtitle_st->codec);
1929 SDL_LockMutex(is->subpq_mutex);
1930 while (is->subpq_size >= SUBPICTURE_QUEUE_SIZE &&
1931 !is->subtitleq.abort_request) {
1932 SDL_CondWait(is->subpq_cond, is->subpq_mutex);
1934 SDL_UnlockMutex(is->subpq_mutex);
1936 if (is->subtitleq.abort_request)
1939 sp = &is->subpq[is->subpq_windex];
1941 /* NOTE: ipts is the PTS of the _first_ picture beginning in
1942 this packet, if any */
1944 if (pkt->pts != AV_NOPTS_VALUE)
1945 pts = av_q2d(is->subtitle_st->time_base) * pkt->pts;
1947 avcodec_decode_subtitle2(is->subtitle_st->codec, &sp->sub,
1948 &got_subtitle, pkt);
1949 if (got_subtitle && sp->sub.format == 0) {
1950 if (sp->sub.pts != AV_NOPTS_VALUE)
1951 pts = sp->sub.pts / (double)AV_TIME_BASE;
1954 for (i = 0; i < sp->sub.num_rects; i++)
1956 for (j = 0; j < sp->sub.rects[i]->nb_colors; j++)
1958 RGBA_IN(r, g, b, a, (uint32_t*)sp->sub.rects[i]->pict.data[1] + j);
1959 y = RGB_TO_Y_CCIR(r, g, b);
1960 u = RGB_TO_U_CCIR(r, g, b, 0);
1961 v = RGB_TO_V_CCIR(r, g, b, 0);
1962 YUVA_OUT((uint32_t*)sp->sub.rects[i]->pict.data[1] + j, y, u, v, a);
1966 /* now we can update the picture count */
1967 if (++is->subpq_windex == SUBPICTURE_QUEUE_SIZE)
1968 is->subpq_windex = 0;
1969 SDL_LockMutex(is->subpq_mutex);
1971 SDL_UnlockMutex(is->subpq_mutex);
1973 av_free_packet(pkt);
1978 /* copy samples for viewing in editor window */
1979 static void update_sample_display(VideoState *is, short *samples, int samples_size)
1983 size = samples_size / sizeof(short);
1985 len = SAMPLE_ARRAY_SIZE - is->sample_array_index;
1988 memcpy(is->sample_array + is->sample_array_index, samples, len * sizeof(short));
1990 is->sample_array_index += len;
1991 if (is->sample_array_index >= SAMPLE_ARRAY_SIZE)
1992 is->sample_array_index = 0;
1997 /* return the wanted number of samples to get better sync if sync_type is video
1998 * or external master clock */
1999 static int synchronize_audio(VideoState *is, int nb_samples)
2001 int wanted_nb_samples = nb_samples;
2003 /* if not master, then we try to remove or add samples to correct the clock */
2004 if (get_master_sync_type(is) != AV_SYNC_AUDIO_MASTER) {
2005 double diff, avg_diff;
2006 int min_nb_samples, max_nb_samples;
2008 diff = get_audio_clock(is) - get_master_clock(is);
2010 if (!isnan(diff) && fabs(diff) < AV_NOSYNC_THRESHOLD) {
2011 is->audio_diff_cum = diff + is->audio_diff_avg_coef * is->audio_diff_cum;
2012 if (is->audio_diff_avg_count < AUDIO_DIFF_AVG_NB) {
2013 /* not enough measures to have a correct estimate */
2014 is->audio_diff_avg_count++;
2016 /* estimate the A-V difference */
2017 avg_diff = is->audio_diff_cum * (1.0 - is->audio_diff_avg_coef);
2019 if (fabs(avg_diff) >= is->audio_diff_threshold) {
2020 wanted_nb_samples = nb_samples + (int)(diff * is->audio_src.freq);
2021 min_nb_samples = ((nb_samples * (100 - SAMPLE_CORRECTION_PERCENT_MAX) / 100));
2022 max_nb_samples = ((nb_samples * (100 + SAMPLE_CORRECTION_PERCENT_MAX) / 100));
2023 wanted_nb_samples = FFMIN(FFMAX(wanted_nb_samples, min_nb_samples), max_nb_samples);
2025 av_dlog(NULL, "diff=%f adiff=%f sample_diff=%d apts=%0.3f %f\n",
2026 diff, avg_diff, wanted_nb_samples - nb_samples,
2027 is->audio_clock, is->audio_diff_threshold);
2030 /* too big difference : may be initial PTS errors, so
2032 is->audio_diff_avg_count = 0;
2033 is->audio_diff_cum = 0;
2037 return wanted_nb_samples;
2041 * Decode one audio frame and return its uncompressed size.
2043 * The processed audio frame is decoded, converted if required, and
2044 * stored in is->audio_buf, with size in bytes given by the return
2047 static int audio_decode_frame(VideoState *is)
2049 AVPacket *pkt_temp = &is->audio_pkt_temp;
2050 AVPacket *pkt = &is->audio_pkt;
2051 AVCodecContext *dec = is->audio_st->codec;
2052 int len1, len2, data_size, resampled_data_size;
2053 int64_t dec_channel_layout;
2055 av_unused double audio_clock0;
2057 int flush_complete = 0;
2058 int wanted_nb_samples;
2061 /* NOTE: the audio packet can contain several frames */
2062 while (pkt_temp->size > 0 || (!pkt_temp->data && new_packet)) {
2064 if (!(is->frame = avcodec_alloc_frame()))
2065 return AVERROR(ENOMEM);
2067 avcodec_get_frame_defaults(is->frame);
2069 if (is->audioq.serial != is->audio_pkt_temp_serial)
2078 len1 = avcodec_decode_audio4(dec, is->frame, &got_frame, pkt_temp);
2080 /* if error, we skip the frame */
2085 pkt_temp->data += len1;
2086 pkt_temp->size -= len1;
2089 /* stop sending empty packets if the decoder is finished */
2090 if (!pkt_temp->data && dec->codec->capabilities & CODEC_CAP_DELAY)
2094 data_size = av_samples_get_buffer_size(NULL, av_frame_get_channels(is->frame),
2095 is->frame->nb_samples,
2096 is->frame->format, 1);
2098 dec_channel_layout =
2099 (is->frame->channel_layout && av_frame_get_channels(is->frame) == av_get_channel_layout_nb_channels(is->frame->channel_layout)) ?
2100 is->frame->channel_layout : av_get_default_channel_layout(av_frame_get_channels(is->frame));
2101 wanted_nb_samples = synchronize_audio(is, is->frame->nb_samples);
2103 if (is->frame->format != is->audio_src.fmt ||
2104 dec_channel_layout != is->audio_src.channel_layout ||
2105 is->frame->sample_rate != is->audio_src.freq ||
2106 (wanted_nb_samples != is->frame->nb_samples && !is->swr_ctx)) {
2107 swr_free(&is->swr_ctx);
2108 is->swr_ctx = swr_alloc_set_opts(NULL,
2109 is->audio_tgt.channel_layout, is->audio_tgt.fmt, is->audio_tgt.freq,
2110 dec_channel_layout, is->frame->format, is->frame->sample_rate,
2112 if (!is->swr_ctx || swr_init(is->swr_ctx) < 0) {
2113 fprintf(stderr, "Cannot create sample rate converter for conversion of %d Hz %s %d channels to %d Hz %s %d channels!\n",
2114 is->frame->sample_rate, av_get_sample_fmt_name(is->frame->format), av_frame_get_channels(is->frame),
2115 is->audio_tgt.freq, av_get_sample_fmt_name(is->audio_tgt.fmt), is->audio_tgt.channels);
2118 is->audio_src.channel_layout = dec_channel_layout;
2119 is->audio_src.channels = av_frame_get_channels(is->frame);
2120 is->audio_src.freq = is->frame->sample_rate;
2121 is->audio_src.fmt = is->frame->format;
2125 const uint8_t **in = (const uint8_t **)is->frame->extended_data;
2126 uint8_t **out = &is->audio_buf1;
2127 int out_count = (int64_t)wanted_nb_samples * is->audio_tgt.freq / is->frame->sample_rate + 256;
2128 int out_size = av_samples_get_buffer_size(NULL, is->audio_tgt.channels, out_count, is->audio_tgt.fmt, 0);
2129 if (wanted_nb_samples != is->frame->nb_samples) {
2130 if (swr_set_compensation(is->swr_ctx, (wanted_nb_samples - is->frame->nb_samples) * is->audio_tgt.freq / is->frame->sample_rate,
2131 wanted_nb_samples * is->audio_tgt.freq / is->frame->sample_rate) < 0) {
2132 fprintf(stderr, "swr_set_compensation() failed\n");
2136 av_fast_malloc(&is->audio_buf1, &is->audio_buf1_size, out_size);
2137 if (!is->audio_buf1)
2138 return AVERROR(ENOMEM);
2139 len2 = swr_convert(is->swr_ctx, out, out_count, in, is->frame->nb_samples);
2141 fprintf(stderr, "swr_convert() failed\n");
2144 if (len2 == out_count) {
2145 fprintf(stderr, "warning: audio buffer is probably too small\n");
2146 swr_init(is->swr_ctx);
2148 is->audio_buf = is->audio_buf1;
2149 resampled_data_size = len2 * is->audio_tgt.channels * av_get_bytes_per_sample(is->audio_tgt.fmt);
2151 is->audio_buf = is->frame->data[0];
2152 resampled_data_size = data_size;
2155 audio_clock0 = is->audio_clock;
2156 is->audio_clock += (double)data_size /
2157 (av_frame_get_channels(is->frame) * is->frame->sample_rate * av_get_bytes_per_sample(is->frame->format));
2160 static double last_clock;
2161 printf("audio: delay=%0.3f clock=%0.3f clock0=%0.3f\n",
2162 is->audio_clock - last_clock,
2163 is->audio_clock, audio_clock0);
2164 last_clock = is->audio_clock;
2167 return resampled_data_size;
2170 /* free the current packet */
2172 av_free_packet(pkt);
2173 memset(pkt_temp, 0, sizeof(*pkt_temp));
2175 if (is->audioq.abort_request) {
2179 if (is->audioq.nb_packets == 0)
2180 SDL_CondSignal(is->continue_read_thread);
2182 /* read next packet */
2183 if ((new_packet = packet_queue_get(&is->audioq, pkt, 1, &is->audio_pkt_temp_serial)) < 0)
2186 if (pkt->data == flush_pkt.data) {
2187 avcodec_flush_buffers(dec);
2193 /* if update the audio clock with the pts */
2194 if (pkt->pts != AV_NOPTS_VALUE) {
2195 is->audio_clock = av_q2d(is->audio_st->time_base)*pkt->pts;
2196 is->audio_clock_serial = is->audio_pkt_temp_serial;
2201 /* prepare a new audio buffer */
2202 static void sdl_audio_callback(void *opaque, Uint8 *stream, int len)
2204 VideoState *is = opaque;
2205 int audio_size, len1;
2207 int frame_size = av_samples_get_buffer_size(NULL, is->audio_tgt.channels, 1, is->audio_tgt.fmt, 1);
2209 audio_callback_time = av_gettime();
2212 if (is->audio_buf_index >= is->audio_buf_size) {
2213 audio_size = audio_decode_frame(is);
2214 if (audio_size < 0) {
2215 /* if error, just output silence */
2216 is->audio_buf = is->silence_buf;
2217 is->audio_buf_size = sizeof(is->silence_buf) / frame_size * frame_size;
2219 if (is->show_mode != SHOW_MODE_VIDEO)
2220 update_sample_display(is, (int16_t *)is->audio_buf, audio_size);
2221 is->audio_buf_size = audio_size;
2223 is->audio_buf_index = 0;
2225 len1 = is->audio_buf_size - is->audio_buf_index;
2228 memcpy(stream, (uint8_t *)is->audio_buf + is->audio_buf_index, len1);
2231 is->audio_buf_index += len1;
2233 bytes_per_sec = is->audio_tgt.freq * is->audio_tgt.channels * av_get_bytes_per_sample(is->audio_tgt.fmt);
2234 is->audio_write_buf_size = is->audio_buf_size - is->audio_buf_index;
2235 /* Let's assume the audio driver that is used by SDL has two periods. */
2236 is->audio_current_pts = is->audio_clock - (double)(2 * is->audio_hw_buf_size + is->audio_write_buf_size) / bytes_per_sec;
2237 is->audio_current_pts_drift = is->audio_current_pts - audio_callback_time / 1000000.0;
2238 if (is->audioq.serial == is->audio_clock_serial)
2239 check_external_clock_sync(is, is->audio_current_pts);
2242 static int audio_open(void *opaque, int64_t wanted_channel_layout, int wanted_nb_channels, int wanted_sample_rate, struct AudioParams *audio_hw_params)
2244 SDL_AudioSpec wanted_spec, spec;
2246 const int next_nb_channels[] = {0, 0, 1, 6, 2, 6, 4, 6};
2248 env = SDL_getenv("SDL_AUDIO_CHANNELS");
2250 wanted_nb_channels = atoi(env);
2251 wanted_channel_layout = av_get_default_channel_layout(wanted_nb_channels);
2253 if (!wanted_channel_layout || wanted_nb_channels != av_get_channel_layout_nb_channels(wanted_channel_layout)) {
2254 wanted_channel_layout = av_get_default_channel_layout(wanted_nb_channels);
2255 wanted_channel_layout &= ~AV_CH_LAYOUT_STEREO_DOWNMIX;
2257 wanted_spec.channels = av_get_channel_layout_nb_channels(wanted_channel_layout);
2258 wanted_spec.freq = wanted_sample_rate;
2259 if (wanted_spec.freq <= 0 || wanted_spec.channels <= 0) {
2260 fprintf(stderr, "Invalid sample rate or channel count!\n");
2263 wanted_spec.format = AUDIO_S16SYS;
2264 wanted_spec.silence = 0;
2265 wanted_spec.samples = SDL_AUDIO_BUFFER_SIZE;
2266 wanted_spec.callback = sdl_audio_callback;
2267 wanted_spec.userdata = opaque;
2268 while (SDL_OpenAudio(&wanted_spec, &spec) < 0) {
2269 fprintf(stderr, "SDL_OpenAudio (%d channels): %s\n", wanted_spec.channels, SDL_GetError());
2270 wanted_spec.channels = next_nb_channels[FFMIN(7, wanted_spec.channels)];
2271 if (!wanted_spec.channels) {
2272 fprintf(stderr, "No more channel combinations to try, audio open failed\n");
2275 wanted_channel_layout = av_get_default_channel_layout(wanted_spec.channels);
2277 if (spec.format != AUDIO_S16SYS) {
2278 fprintf(stderr, "SDL advised audio format %d is not supported!\n", spec.format);
2281 if (spec.channels != wanted_spec.channels) {
2282 wanted_channel_layout = av_get_default_channel_layout(spec.channels);
2283 if (!wanted_channel_layout) {
2284 fprintf(stderr, "SDL advised channel count %d is not supported!\n", spec.channels);
2289 audio_hw_params->fmt = AV_SAMPLE_FMT_S16;
2290 audio_hw_params->freq = spec.freq;
2291 audio_hw_params->channel_layout = wanted_channel_layout;
2292 audio_hw_params->channels = spec.channels;
2296 /* open a given stream. Return 0 if OK */
2297 static int stream_component_open(VideoState *is, int stream_index)
2299 AVFormatContext *ic = is->ic;
2300 AVCodecContext *avctx;
2302 const char *forced_codec_name = NULL;
2304 AVDictionaryEntry *t = NULL;
2306 if (stream_index < 0 || stream_index >= ic->nb_streams)
2308 avctx = ic->streams[stream_index]->codec;
2310 codec = avcodec_find_decoder(avctx->codec_id);
2312 switch(avctx->codec_type){
2313 case AVMEDIA_TYPE_AUDIO : is->last_audio_stream = stream_index; forced_codec_name = audio_codec_name; break;
2314 case AVMEDIA_TYPE_SUBTITLE: is->last_subtitle_stream = stream_index; forced_codec_name = subtitle_codec_name; break;
2315 case AVMEDIA_TYPE_VIDEO : is->last_video_stream = stream_index; forced_codec_name = video_codec_name; break;
2317 if (forced_codec_name)
2318 codec = avcodec_find_decoder_by_name(forced_codec_name);
2320 if (forced_codec_name) fprintf(stderr, "No codec could be found with name '%s'\n", forced_codec_name);
2321 else fprintf(stderr, "No codec could be found with id %d\n", avctx->codec_id);
2325 avctx->codec_id = codec->id;
2326 avctx->workaround_bugs = workaround_bugs;
2327 avctx->lowres = lowres;
2328 if(avctx->lowres > codec->max_lowres){
2329 av_log(avctx, AV_LOG_WARNING, "The maximum value for lowres supported by the decoder is %d\n",
2331 avctx->lowres= codec->max_lowres;
2333 avctx->idct_algo = idct;
2334 avctx->skip_frame = skip_frame;
2335 avctx->skip_idct = skip_idct;
2336 avctx->skip_loop_filter = skip_loop_filter;
2337 avctx->error_concealment = error_concealment;
2339 if(avctx->lowres) avctx->flags |= CODEC_FLAG_EMU_EDGE;
2340 if (fast) avctx->flags2 |= CODEC_FLAG2_FAST;
2341 if(codec->capabilities & CODEC_CAP_DR1)
2342 avctx->flags |= CODEC_FLAG_EMU_EDGE;
2344 opts = filter_codec_opts(codec_opts, avctx->codec_id, ic, ic->streams[stream_index], codec);
2345 if (!av_dict_get(opts, "threads", NULL, 0))
2346 av_dict_set(&opts, "threads", "auto", 0);
2347 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO)
2348 av_dict_set(&opts, "refcounted_frames", "1", 0);
2349 if (avcodec_open2(avctx, codec, &opts) < 0)
2351 if ((t = av_dict_get(opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
2352 av_log(NULL, AV_LOG_ERROR, "Option %s not found.\n", t->key);
2353 return AVERROR_OPTION_NOT_FOUND;
2356 /* prepare audio output */
2357 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
2358 int audio_hw_buf_size = audio_open(is, avctx->channel_layout, avctx->channels, avctx->sample_rate, &is->audio_src);
2359 if (audio_hw_buf_size < 0)
2361 is->audio_hw_buf_size = audio_hw_buf_size;
2362 is->audio_tgt = is->audio_src;
2365 ic->streams[stream_index]->discard = AVDISCARD_DEFAULT;
2366 switch (avctx->codec_type) {
2367 case AVMEDIA_TYPE_AUDIO:
2368 is->audio_stream = stream_index;
2369 is->audio_st = ic->streams[stream_index];
2370 is->audio_buf_size = 0;
2371 is->audio_buf_index = 0;
2373 /* init averaging filter */
2374 is->audio_diff_avg_coef = exp(log(0.01) / AUDIO_DIFF_AVG_NB);
2375 is->audio_diff_avg_count = 0;
2376 /* since we do not have a precise anough audio fifo fullness,
2377 we correct audio sync only if larger than this threshold */
2378 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);
2380 memset(&is->audio_pkt, 0, sizeof(is->audio_pkt));
2381 memset(&is->audio_pkt_temp, 0, sizeof(is->audio_pkt_temp));
2382 packet_queue_start(&is->audioq);
2385 case AVMEDIA_TYPE_VIDEO:
2386 is->video_stream = stream_index;
2387 is->video_st = ic->streams[stream_index];
2389 packet_queue_start(&is->videoq);
2390 is->video_tid = SDL_CreateThread(video_thread, is);
2392 case AVMEDIA_TYPE_SUBTITLE:
2393 is->subtitle_stream = stream_index;
2394 is->subtitle_st = ic->streams[stream_index];
2395 packet_queue_start(&is->subtitleq);
2397 is->subtitle_tid = SDL_CreateThread(subtitle_thread, is);
2405 static void stream_component_close(VideoState *is, int stream_index)
2407 AVFormatContext *ic = is->ic;
2408 AVCodecContext *avctx;
2410 if (stream_index < 0 || stream_index >= ic->nb_streams)
2412 avctx = ic->streams[stream_index]->codec;
2414 switch (avctx->codec_type) {
2415 case AVMEDIA_TYPE_AUDIO:
2416 packet_queue_abort(&is->audioq);
2420 packet_queue_flush(&is->audioq);
2421 av_free_packet(&is->audio_pkt);
2422 swr_free(&is->swr_ctx);
2423 av_freep(&is->audio_buf1);
2424 is->audio_buf1_size = 0;
2425 is->audio_buf = NULL;
2426 avcodec_free_frame(&is->frame);
2429 av_rdft_end(is->rdft);
2430 av_freep(&is->rdft_data);
2435 case AVMEDIA_TYPE_VIDEO:
2436 packet_queue_abort(&is->videoq);
2438 /* note: we also signal this mutex to make sure we deblock the
2439 video thread in all cases */
2440 SDL_LockMutex(is->pictq_mutex);
2441 SDL_CondSignal(is->pictq_cond);
2442 SDL_UnlockMutex(is->pictq_mutex);
2444 SDL_WaitThread(is->video_tid, NULL);
2446 packet_queue_flush(&is->videoq);
2448 case AVMEDIA_TYPE_SUBTITLE:
2449 packet_queue_abort(&is->subtitleq);
2451 /* note: we also signal this mutex to make sure we deblock the
2452 video thread in all cases */
2453 SDL_LockMutex(is->subpq_mutex);
2454 is->subtitle_stream_changed = 1;
2456 SDL_CondSignal(is->subpq_cond);
2457 SDL_UnlockMutex(is->subpq_mutex);
2459 SDL_WaitThread(is->subtitle_tid, NULL);
2461 packet_queue_flush(&is->subtitleq);
2467 ic->streams[stream_index]->discard = AVDISCARD_ALL;
2468 avcodec_close(avctx);
2469 switch (avctx->codec_type) {
2470 case AVMEDIA_TYPE_AUDIO:
2471 is->audio_st = NULL;
2472 is->audio_stream = -1;
2474 case AVMEDIA_TYPE_VIDEO:
2475 is->video_st = NULL;
2476 is->video_stream = -1;
2478 case AVMEDIA_TYPE_SUBTITLE:
2479 is->subtitle_st = NULL;
2480 is->subtitle_stream = -1;
2487 static int decode_interrupt_cb(void *ctx)
2489 VideoState *is = ctx;
2490 return is->abort_request;
2493 static int is_realtime(AVFormatContext *s)
2495 if( !strcmp(s->iformat->name, "rtp")
2496 || !strcmp(s->iformat->name, "rtsp")
2497 || !strcmp(s->iformat->name, "sdp")
2501 if(s->pb && ( !strncmp(s->filename, "rtp:", 4)
2502 || !strncmp(s->filename, "udp:", 4)
2509 /* this thread gets the stream from the disk or the network */
2510 static int read_thread(void *arg)
2512 VideoState *is = arg;
2513 AVFormatContext *ic = NULL;
2515 int st_index[AVMEDIA_TYPE_NB];
2516 AVPacket pkt1, *pkt = &pkt1;
2518 int pkt_in_play_range = 0;
2519 AVDictionaryEntry *t;
2520 AVDictionary **opts;
2521 int orig_nb_streams;
2522 SDL_mutex *wait_mutex = SDL_CreateMutex();
2524 memset(st_index, -1, sizeof(st_index));
2525 is->last_video_stream = is->video_stream = -1;
2526 is->last_audio_stream = is->audio_stream = -1;
2527 is->last_subtitle_stream = is->subtitle_stream = -1;
2529 ic = avformat_alloc_context();
2530 ic->interrupt_callback.callback = decode_interrupt_cb;
2531 ic->interrupt_callback.opaque = is;
2532 err = avformat_open_input(&ic, is->filename, is->iformat, &format_opts);
2534 print_error(is->filename, err);
2538 if ((t = av_dict_get(format_opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
2539 av_log(NULL, AV_LOG_ERROR, "Option %s not found.\n", t->key);
2540 ret = AVERROR_OPTION_NOT_FOUND;
2546 ic->flags |= AVFMT_FLAG_GENPTS;
2548 opts = setup_find_stream_info_opts(ic, codec_opts);
2549 orig_nb_streams = ic->nb_streams;
2551 err = avformat_find_stream_info(ic, opts);
2553 fprintf(stderr, "%s: could not find codec parameters\n", is->filename);
2557 for (i = 0; i < orig_nb_streams; i++)
2558 av_dict_free(&opts[i]);
2562 ic->pb->eof_reached = 0; // FIXME hack, ffplay maybe should not use url_feof() to test for the end
2564 if (seek_by_bytes < 0)
2565 seek_by_bytes = !!(ic->iformat->flags & AVFMT_TS_DISCONT);
2567 is->max_frame_duration = (ic->iformat->flags & AVFMT_TS_DISCONT) ? 10.0 : 3600.0;
2569 /* if seeking requested, we execute it */
2570 if (start_time != AV_NOPTS_VALUE) {
2573 timestamp = start_time;
2574 /* add the stream start time */
2575 if (ic->start_time != AV_NOPTS_VALUE)
2576 timestamp += ic->start_time;
2577 ret = avformat_seek_file(ic, -1, INT64_MIN, timestamp, INT64_MAX, 0);
2579 fprintf(stderr, "%s: could not seek to position %0.3f\n",
2580 is->filename, (double)timestamp / AV_TIME_BASE);
2584 is->realtime = is_realtime(ic);
2586 for (i = 0; i < ic->nb_streams; i++)
2587 ic->streams[i]->discard = AVDISCARD_ALL;
2589 st_index[AVMEDIA_TYPE_VIDEO] =
2590 av_find_best_stream(ic, AVMEDIA_TYPE_VIDEO,
2591 wanted_stream[AVMEDIA_TYPE_VIDEO], -1, NULL, 0);
2593 st_index[AVMEDIA_TYPE_AUDIO] =
2594 av_find_best_stream(ic, AVMEDIA_TYPE_AUDIO,
2595 wanted_stream[AVMEDIA_TYPE_AUDIO],
2596 st_index[AVMEDIA_TYPE_VIDEO],
2598 if (!video_disable && !subtitle_disable)
2599 st_index[AVMEDIA_TYPE_SUBTITLE] =
2600 av_find_best_stream(ic, AVMEDIA_TYPE_SUBTITLE,
2601 wanted_stream[AVMEDIA_TYPE_SUBTITLE],
2602 (st_index[AVMEDIA_TYPE_AUDIO] >= 0 ?
2603 st_index[AVMEDIA_TYPE_AUDIO] :
2604 st_index[AVMEDIA_TYPE_VIDEO]),
2607 av_dump_format(ic, 0, is->filename, 0);
2610 is->show_mode = show_mode;
2612 /* open the streams */
2613 if (st_index[AVMEDIA_TYPE_AUDIO] >= 0) {
2614 stream_component_open(is, st_index[AVMEDIA_TYPE_AUDIO]);
2618 if (st_index[AVMEDIA_TYPE_VIDEO] >= 0) {
2619 ret = stream_component_open(is, st_index[AVMEDIA_TYPE_VIDEO]);
2621 if (is->show_mode == SHOW_MODE_NONE)
2622 is->show_mode = ret >= 0 ? SHOW_MODE_VIDEO : SHOW_MODE_RDFT;
2624 if (st_index[AVMEDIA_TYPE_SUBTITLE] >= 0) {
2625 stream_component_open(is, st_index[AVMEDIA_TYPE_SUBTITLE]);
2628 if (is->video_stream < 0 && is->audio_stream < 0) {
2629 fprintf(stderr, "%s: could not open codecs\n", is->filename);
2634 if (infinite_buffer < 0 && is->realtime)
2635 infinite_buffer = 1;
2638 if (is->abort_request)
2640 if (is->paused != is->last_paused) {
2641 is->last_paused = is->paused;
2643 is->read_pause_return = av_read_pause(ic);
2647 #if CONFIG_RTSP_DEMUXER || CONFIG_MMSH_PROTOCOL
2649 (!strcmp(ic->iformat->name, "rtsp") ||
2650 (ic->pb && !strncmp(input_filename, "mmsh:", 5)))) {
2651 /* wait 10 ms to avoid trying to get another packet */
2658 int64_t seek_target = is->seek_pos;
2659 int64_t seek_min = is->seek_rel > 0 ? seek_target - is->seek_rel + 2: INT64_MIN;
2660 int64_t seek_max = is->seek_rel < 0 ? seek_target - is->seek_rel - 2: INT64_MAX;
2661 // FIXME the +-2 is due to rounding being not done in the correct direction in generation
2662 // of the seek_pos/seek_rel variables
2664 ret = avformat_seek_file(is->ic, -1, seek_min, seek_target, seek_max, is->seek_flags);
2666 fprintf(stderr, "%s: error while seeking\n", is->ic->filename);
2668 if (is->audio_stream >= 0) {
2669 packet_queue_flush(&is->audioq);
2670 packet_queue_put(&is->audioq, &flush_pkt);
2672 if (is->subtitle_stream >= 0) {
2673 packet_queue_flush(&is->subtitleq);
2674 packet_queue_put(&is->subtitleq, &flush_pkt);
2676 if (is->video_stream >= 0) {
2677 packet_queue_flush(&is->videoq);
2678 packet_queue_put(&is->videoq, &flush_pkt);
2680 if (is->seek_flags & AVSEEK_FLAG_BYTE) {
2681 update_external_clock_pts(is, NAN);
2683 update_external_clock_pts(is, seek_target / (double)AV_TIME_BASE);
2689 step_to_next_frame(is);
2691 if (is->queue_attachments_req) {
2692 avformat_queue_attached_pictures(ic);
2693 is->queue_attachments_req = 0;
2696 /* if the queue are full, no need to read more */
2697 if (infinite_buffer<1 &&
2698 (is->audioq.size + is->videoq.size + is->subtitleq.size > MAX_QUEUE_SIZE
2699 || ( (is->audioq .nb_packets > MIN_FRAMES || is->audio_stream < 0 || is->audioq.abort_request)
2700 && (is->videoq .nb_packets > MIN_FRAMES || is->video_stream < 0 || is->videoq.abort_request)
2701 && (is->subtitleq.nb_packets > MIN_FRAMES || is->subtitle_stream < 0 || is->subtitleq.abort_request)))) {
2703 SDL_LockMutex(wait_mutex);
2704 SDL_CondWaitTimeout(is->continue_read_thread, wait_mutex, 10);
2705 SDL_UnlockMutex(wait_mutex);
2709 if (is->video_stream >= 0) {
2710 av_init_packet(pkt);
2713 pkt->stream_index = is->video_stream;
2714 packet_queue_put(&is->videoq, pkt);
2716 if (is->audio_stream >= 0 &&
2717 is->audio_st->codec->codec->capabilities & CODEC_CAP_DELAY) {
2718 av_init_packet(pkt);
2721 pkt->stream_index = is->audio_stream;
2722 packet_queue_put(&is->audioq, pkt);
2725 if (is->audioq.size + is->videoq.size + is->subtitleq.size == 0) {
2726 if (loop != 1 && (!loop || --loop)) {
2727 stream_seek(is, start_time != AV_NOPTS_VALUE ? start_time : 0, 0, 0);
2728 } else if (autoexit) {
2736 ret = av_read_frame(ic, pkt);
2738 if (ret == AVERROR_EOF || url_feof(ic->pb))
2740 if (ic->pb && ic->pb->error)
2742 SDL_LockMutex(wait_mutex);
2743 SDL_CondWaitTimeout(is->continue_read_thread, wait_mutex, 10);
2744 SDL_UnlockMutex(wait_mutex);
2747 /* check if packet is in play range specified by user, then queue, otherwise discard */
2748 pkt_in_play_range = duration == AV_NOPTS_VALUE ||
2749 (pkt->pts - ic->streams[pkt->stream_index]->start_time) *
2750 av_q2d(ic->streams[pkt->stream_index]->time_base) -
2751 (double)(start_time != AV_NOPTS_VALUE ? start_time : 0) / 1000000
2752 <= ((double)duration / 1000000);
2753 if (pkt->stream_index == is->audio_stream && pkt_in_play_range) {
2754 packet_queue_put(&is->audioq, pkt);
2755 } else if (pkt->stream_index == is->video_stream && pkt_in_play_range) {
2756 packet_queue_put(&is->videoq, pkt);
2757 } else if (pkt->stream_index == is->subtitle_stream && pkt_in_play_range) {
2758 packet_queue_put(&is->subtitleq, pkt);
2760 av_free_packet(pkt);
2763 /* wait until the end */
2764 while (!is->abort_request) {
2770 /* close each stream */
2771 if (is->audio_stream >= 0)
2772 stream_component_close(is, is->audio_stream);
2773 if (is->video_stream >= 0)
2774 stream_component_close(is, is->video_stream);
2775 if (is->subtitle_stream >= 0)
2776 stream_component_close(is, is->subtitle_stream);
2778 avformat_close_input(&is->ic);
2784 event.type = FF_QUIT_EVENT;
2785 event.user.data1 = is;
2786 SDL_PushEvent(&event);
2788 SDL_DestroyMutex(wait_mutex);
2792 static VideoState *stream_open(const char *filename, AVInputFormat *iformat)
2796 is = av_mallocz(sizeof(VideoState));
2799 av_strlcpy(is->filename, filename, sizeof(is->filename));
2800 is->iformat = iformat;
2804 /* start video display */
2805 is->pictq_mutex = SDL_CreateMutex();
2806 is->pictq_cond = SDL_CreateCond();
2808 is->subpq_mutex = SDL_CreateMutex();
2809 is->subpq_cond = SDL_CreateCond();
2811 packet_queue_init(&is->videoq);
2812 packet_queue_init(&is->audioq);
2813 packet_queue_init(&is->subtitleq);
2815 is->continue_read_thread = SDL_CreateCond();
2817 update_external_clock_pts(is, NAN);
2818 update_external_clock_speed(is, 1.0);
2819 is->audio_current_pts_drift = -av_gettime() / 1000000.0;
2820 is->video_current_pts_drift = is->audio_current_pts_drift;
2821 is->audio_clock_serial = -1;
2822 is->video_clock_serial = -1;
2823 is->av_sync_type = av_sync_type;
2824 is->read_tid = SDL_CreateThread(read_thread, is);
2825 if (!is->read_tid) {
2832 static void stream_cycle_channel(VideoState *is, int codec_type)
2834 AVFormatContext *ic = is->ic;
2835 int start_index, stream_index;
2839 if (codec_type == AVMEDIA_TYPE_VIDEO) {
2840 start_index = is->last_video_stream;
2841 old_index = is->video_stream;
2842 } else if (codec_type == AVMEDIA_TYPE_AUDIO) {
2843 start_index = is->last_audio_stream;
2844 old_index = is->audio_stream;
2846 start_index = is->last_subtitle_stream;
2847 old_index = is->subtitle_stream;
2849 stream_index = start_index;
2851 if (++stream_index >= is->ic->nb_streams)
2853 if (codec_type == AVMEDIA_TYPE_SUBTITLE)
2856 is->last_subtitle_stream = -1;
2859 if (start_index == -1)
2863 if (stream_index == start_index)
2865 st = ic->streams[stream_index];
2866 if (st->codec->codec_type == codec_type) {
2867 /* check that parameters are OK */
2868 switch (codec_type) {
2869 case AVMEDIA_TYPE_AUDIO:
2870 if (st->codec->sample_rate != 0 &&
2871 st->codec->channels != 0)
2874 case AVMEDIA_TYPE_VIDEO:
2875 case AVMEDIA_TYPE_SUBTITLE:
2883 stream_component_close(is, old_index);
2884 stream_component_open(is, stream_index);
2885 if (codec_type == AVMEDIA_TYPE_VIDEO)
2886 is->queue_attachments_req = 1;
2890 static void toggle_full_screen(VideoState *is)
2892 #if defined(__APPLE__) && SDL_VERSION_ATLEAST(1, 2, 14)
2893 /* OS X needs to reallocate the SDL overlays */
2895 for (i = 0; i < VIDEO_PICTURE_QUEUE_SIZE; i++)
2896 is->pictq[i].reallocate = 1;
2898 is_full_screen = !is_full_screen;
2899 video_open(is, 1, NULL);
2902 static void toggle_audio_display(VideoState *is)
2904 int bgcolor = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00);
2905 int next = is->show_mode;
2907 next = (next + 1) % SHOW_MODE_NB;
2908 } while (next != is->show_mode && (next == SHOW_MODE_VIDEO && !is->video_st || next != SHOW_MODE_VIDEO && !is->audio_st));
2909 if (is->show_mode != next) {
2910 fill_rectangle(screen,
2911 is->xleft, is->ytop, is->width, is->height,
2913 is->force_refresh = 1;
2914 is->show_mode = next;
2918 static void refresh_loop_wait_event(VideoState *is, SDL_Event *event) {
2919 double remaining_time = 0.0;
2921 while (!SDL_PeepEvents(event, 1, SDL_GETEVENT, SDL_ALLEVENTS)) {
2922 if (!cursor_hidden && av_gettime() - cursor_last_shown > CURSOR_HIDE_DELAY) {
2926 if (remaining_time > 0.0)
2927 av_usleep((int64_t)(remaining_time * 1000000.0));
2928 remaining_time = REFRESH_RATE;
2929 if (is->show_mode != SHOW_MODE_NONE && (!is->paused || is->force_refresh))
2930 video_refresh(is, &remaining_time);
2935 /* handle an event sent by the GUI */
2936 static void event_loop(VideoState *cur_stream)
2939 double incr, pos, frac;
2943 refresh_loop_wait_event(cur_stream, &event);
2944 switch (event.type) {
2946 if (exit_on_keydown) {
2947 do_exit(cur_stream);
2950 switch (event.key.keysym.sym) {
2953 do_exit(cur_stream);
2956 toggle_full_screen(cur_stream);
2957 cur_stream->force_refresh = 1;
2961 toggle_pause(cur_stream);
2963 case SDLK_s: // S: Step to next frame
2964 step_to_next_frame(cur_stream);
2967 stream_cycle_channel(cur_stream, AVMEDIA_TYPE_AUDIO);
2970 stream_cycle_channel(cur_stream, AVMEDIA_TYPE_VIDEO);
2973 stream_cycle_channel(cur_stream, AVMEDIA_TYPE_SUBTITLE);
2976 toggle_audio_display(cur_stream);
2996 if (seek_by_bytes) {
2997 if (cur_stream->video_stream >= 0 && cur_stream->video_current_pos >= 0) {
2998 pos = cur_stream->video_current_pos;
2999 } else if (cur_stream->audio_stream >= 0 && cur_stream->audio_pkt.pos >= 0) {
3000 pos = cur_stream->audio_pkt.pos;
3002 pos = avio_tell(cur_stream->ic->pb);
3003 if (cur_stream->ic->bit_rate)
3004 incr *= cur_stream->ic->bit_rate / 8.0;
3008 stream_seek(cur_stream, pos, incr, 1);
3010 pos = get_master_clock(cur_stream);
3012 pos = (double)cur_stream->seek_pos / AV_TIME_BASE;
3014 if (cur_stream->ic->start_time != AV_NOPTS_VALUE && pos < cur_stream->ic->start_time / (double)AV_TIME_BASE)
3015 pos = cur_stream->ic->start_time / (double)AV_TIME_BASE;
3016 stream_seek(cur_stream, (int64_t)(pos * AV_TIME_BASE), (int64_t)(incr * AV_TIME_BASE), 0);
3023 case SDL_VIDEOEXPOSE:
3024 cur_stream->force_refresh = 1;
3026 case SDL_MOUSEBUTTONDOWN:
3027 if (exit_on_mousedown) {
3028 do_exit(cur_stream);
3031 case SDL_MOUSEMOTION:
3032 if (cursor_hidden) {
3036 cursor_last_shown = av_gettime();
3037 if (event.type == SDL_MOUSEBUTTONDOWN) {
3040 if (event.motion.state != SDL_PRESSED)
3044 if (seek_by_bytes || cur_stream->ic->duration <= 0) {
3045 uint64_t size = avio_size(cur_stream->ic->pb);
3046 stream_seek(cur_stream, size*x/cur_stream->width, 0, 1);
3050 int tns, thh, tmm, tss;
3051 tns = cur_stream->ic->duration / 1000000LL;
3053 tmm = (tns % 3600) / 60;
3055 frac = x / cur_stream->width;
3058 mm = (ns % 3600) / 60;
3060 fprintf(stderr, "Seek to %2.0f%% (%2d:%02d:%02d) of total duration (%2d:%02d:%02d) \n", frac*100,
3061 hh, mm, ss, thh, tmm, tss);
3062 ts = frac * cur_stream->ic->duration;
3063 if (cur_stream->ic->start_time != AV_NOPTS_VALUE)
3064 ts += cur_stream->ic->start_time;
3065 stream_seek(cur_stream, ts, 0, 0);
3068 case SDL_VIDEORESIZE:
3069 screen = SDL_SetVideoMode(event.resize.w, event.resize.h, 0,
3070 SDL_HWSURFACE|SDL_RESIZABLE|SDL_ASYNCBLIT|SDL_HWACCEL);
3071 screen_width = cur_stream->width = event.resize.w;
3072 screen_height = cur_stream->height = event.resize.h;
3073 cur_stream->force_refresh = 1;
3077 do_exit(cur_stream);
3079 case FF_ALLOC_EVENT:
3080 alloc_picture(event.user.data1);
3088 static int opt_frame_size(void *optctx, const char *opt, const char *arg)
3090 av_log(NULL, AV_LOG_WARNING, "Option -s is deprecated, use -video_size.\n");
3091 return opt_default(NULL, "video_size", arg);
3094 static int opt_width(void *optctx, const char *opt, const char *arg)
3096 screen_width = parse_number_or_die(opt, arg, OPT_INT64, 1, INT_MAX);
3100 static int opt_height(void *optctx, const char *opt, const char *arg)
3102 screen_height = parse_number_or_die(opt, arg, OPT_INT64, 1, INT_MAX);
3106 static int opt_format(void *optctx, const char *opt, const char *arg)
3108 file_iformat = av_find_input_format(arg);
3109 if (!file_iformat) {
3110 fprintf(stderr, "Unknown input format: %s\n", arg);
3111 return AVERROR(EINVAL);
3116 static int opt_frame_pix_fmt(void *optctx, const char *opt, const char *arg)
3118 av_log(NULL, AV_LOG_WARNING, "Option -pix_fmt is deprecated, use -pixel_format.\n");
3119 return opt_default(NULL, "pixel_format", arg);
3122 static int opt_sync(void *optctx, const char *opt, const char *arg)
3124 if (!strcmp(arg, "audio"))
3125 av_sync_type = AV_SYNC_AUDIO_MASTER;
3126 else if (!strcmp(arg, "video"))
3127 av_sync_type = AV_SYNC_VIDEO_MASTER;
3128 else if (!strcmp(arg, "ext"))
3129 av_sync_type = AV_SYNC_EXTERNAL_CLOCK;
3131 fprintf(stderr, "Unknown value for %s: %s\n", opt, arg);
3137 static int opt_seek(void *optctx, const char *opt, const char *arg)
3139 start_time = parse_time_or_die(opt, arg, 1);
3143 static int opt_duration(void *optctx, const char *opt, const char *arg)
3145 duration = parse_time_or_die(opt, arg, 1);
3149 static int opt_show_mode(void *optctx, const char *opt, const char *arg)
3151 show_mode = !strcmp(arg, "video") ? SHOW_MODE_VIDEO :
3152 !strcmp(arg, "waves") ? SHOW_MODE_WAVES :
3153 !strcmp(arg, "rdft" ) ? SHOW_MODE_RDFT :
3154 parse_number_or_die(opt, arg, OPT_INT, 0, SHOW_MODE_NB-1);
3158 static void opt_input_file(void *optctx, const char *filename)
3160 if (input_filename) {
3161 fprintf(stderr, "Argument '%s' provided as input filename, but '%s' was already specified.\n",
3162 filename, input_filename);
3165 if (!strcmp(filename, "-"))
3167 input_filename = filename;
3170 static int opt_codec(void *optctx, const char *opt, const char *arg)
3172 const char *spec = strchr(opt, ':');
3174 fprintf(stderr, "No media specifier was specified in '%s' in option '%s'\n",
3176 return AVERROR(EINVAL);
3180 case 'a' : audio_codec_name = arg; break;
3181 case 's' : subtitle_codec_name = arg; break;
3182 case 'v' : video_codec_name = arg; break;
3184 fprintf(stderr, "Invalid media specifier '%s' in option '%s'\n", spec, opt);
3185 return AVERROR(EINVAL);
3192 static const OptionDef options[] = {
3193 #include "cmdutils_common_opts.h"
3194 { "x", HAS_ARG, { .func_arg = opt_width }, "force displayed width", "width" },
3195 { "y", HAS_ARG, { .func_arg = opt_height }, "force displayed height", "height" },
3196 { "s", HAS_ARG | OPT_VIDEO, { .func_arg = opt_frame_size }, "set frame size (WxH or abbreviation)", "size" },
3197 { "fs", OPT_BOOL, { &is_full_screen }, "force full screen" },
3198 { "an", OPT_BOOL, { &audio_disable }, "disable audio" },
3199 { "vn", OPT_BOOL, { &video_disable }, "disable video" },
3200 { "sn", OPT_BOOL, { &subtitle_disable }, "disable subtitling" },
3201 { "ast", OPT_INT | HAS_ARG | OPT_EXPERT, { &wanted_stream[AVMEDIA_TYPE_AUDIO] }, "select desired audio stream", "stream_number" },
3202 { "vst", OPT_INT | HAS_ARG | OPT_EXPERT, { &wanted_stream[AVMEDIA_TYPE_VIDEO] }, "select desired video stream", "stream_number" },
3203 { "sst", OPT_INT | HAS_ARG | OPT_EXPERT, { &wanted_stream[AVMEDIA_TYPE_SUBTITLE] }, "select desired subtitle stream", "stream_number" },
3204 { "ss", HAS_ARG, { .func_arg = opt_seek }, "seek to a given position in seconds", "pos" },
3205 { "t", HAS_ARG, { .func_arg = opt_duration }, "play \"duration\" seconds of audio/video", "duration" },
3206 { "bytes", OPT_INT | HAS_ARG, { &seek_by_bytes }, "seek by bytes 0=off 1=on -1=auto", "val" },
3207 { "nodisp", OPT_BOOL, { &display_disable }, "disable graphical display" },
3208 { "f", HAS_ARG, { .func_arg = opt_format }, "force format", "fmt" },
3209 { "pix_fmt", HAS_ARG | OPT_EXPERT | OPT_VIDEO, { .func_arg = opt_frame_pix_fmt }, "set pixel format", "format" },
3210 { "stats", OPT_BOOL | OPT_EXPERT, { &show_status }, "show status", "" },
3211 { "bug", OPT_INT | HAS_ARG | OPT_EXPERT, { &workaround_bugs }, "workaround bugs", "" },
3212 { "fast", OPT_BOOL | OPT_EXPERT, { &fast }, "non spec compliant optimizations", "" },
3213 { "genpts", OPT_BOOL | OPT_EXPERT, { &genpts }, "generate pts", "" },
3214 { "drp", OPT_INT | HAS_ARG | OPT_EXPERT, { &decoder_reorder_pts }, "let decoder reorder pts 0=off 1=on -1=auto", ""},
3215 { "lowres", OPT_INT | HAS_ARG | OPT_EXPERT, { &lowres }, "", "" },
3216 { "skiploop", OPT_INT | HAS_ARG | OPT_EXPERT, { &skip_loop_filter }, "", "" },
3217 { "skipframe", OPT_INT | HAS_ARG | OPT_EXPERT, { &skip_frame }, "", "" },
3218 { "skipidct", OPT_INT | HAS_ARG | OPT_EXPERT, { &skip_idct }, "", "" },
3219 { "idct", OPT_INT | HAS_ARG | OPT_EXPERT, { &idct }, "set idct algo", "algo" },
3220 { "ec", OPT_INT | HAS_ARG | OPT_EXPERT, { &error_concealment }, "set error concealment options", "bit_mask" },
3221 { "sync", HAS_ARG | OPT_EXPERT, { .func_arg = opt_sync }, "set audio-video sync. type (type=audio/video/ext)", "type" },
3222 { "autoexit", OPT_BOOL | OPT_EXPERT, { &autoexit }, "exit at the end", "" },
3223 { "exitonkeydown", OPT_BOOL | OPT_EXPERT, { &exit_on_keydown }, "exit on key down", "" },
3224 { "exitonmousedown", OPT_BOOL | OPT_EXPERT, { &exit_on_mousedown }, "exit on mouse down", "" },
3225 { "loop", OPT_INT | HAS_ARG | OPT_EXPERT, { &loop }, "set number of times the playback shall be looped", "loop count" },
3226 { "framedrop", OPT_BOOL | OPT_EXPERT, { &framedrop }, "drop frames when cpu is too slow", "" },
3227 { "infbuf", OPT_BOOL | OPT_EXPERT, { &infinite_buffer }, "don't limit the input buffer size (useful with realtime streams)", "" },
3228 { "window_title", OPT_STRING | HAS_ARG, { &window_title }, "set window title", "window title" },
3230 { "vf", OPT_STRING | HAS_ARG, { &vfilters }, "set video filters", "filter_graph" },
3232 { "rdftspeed", OPT_INT | HAS_ARG| OPT_AUDIO | OPT_EXPERT, { &rdftspeed }, "rdft speed", "msecs" },
3233 { "showmode", HAS_ARG, { .func_arg = opt_show_mode}, "select show mode (0 = video, 1 = waves, 2 = RDFT)", "mode" },
3234 { "default", HAS_ARG | OPT_AUDIO | OPT_VIDEO | OPT_EXPERT, { .func_arg = opt_default }, "generic catch all option", "" },
3235 { "i", OPT_BOOL, { &dummy}, "read specified file", "input_file"},
3236 { "codec", HAS_ARG, { .func_arg = opt_codec}, "force decoder", "decoder_name" },
3237 { "acodec", HAS_ARG | OPT_STRING | OPT_EXPERT, { &audio_codec_name }, "force audio decoder", "decoder_name" },
3238 { "scodec", HAS_ARG | OPT_STRING | OPT_EXPERT, { &subtitle_codec_name }, "force subtitle decoder", "decoder_name" },
3239 { "vcodec", HAS_ARG | OPT_STRING | OPT_EXPERT, { &video_codec_name }, "force video decoder", "decoder_name" },
3243 static void show_usage(void)
3245 av_log(NULL, AV_LOG_INFO, "Simple media player\n");
3246 av_log(NULL, AV_LOG_INFO, "usage: %s [options] input_file\n", program_name);
3247 av_log(NULL, AV_LOG_INFO, "\n");
3250 void show_help_default(const char *opt, const char *arg)
3252 av_log_set_callback(log_callback_help);
3254 show_help_options(options, "Main options:", 0, OPT_EXPERT, 0);
3255 show_help_options(options, "Advanced options:", OPT_EXPERT, 0, 0);
3257 show_help_children(avcodec_get_class(), AV_OPT_FLAG_DECODING_PARAM);
3258 show_help_children(avformat_get_class(), AV_OPT_FLAG_DECODING_PARAM);
3259 #if !CONFIG_AVFILTER
3260 show_help_children(sws_get_class(), AV_OPT_FLAG_ENCODING_PARAM);
3262 show_help_children(avfilter_get_class(), AV_OPT_FLAG_FILTERING_PARAM);
3264 printf("\nWhile playing:\n"
3266 "f toggle full screen\n"
3268 "a cycle audio channel\n"
3269 "v cycle video channel\n"
3270 "t cycle subtitle channel\n"
3271 "w show audio waves\n"
3272 "s activate frame-step mode\n"
3273 "left/right seek backward/forward 10 seconds\n"
3274 "down/up seek backward/forward 1 minute\n"
3275 "page down/page up seek backward/forward 10 minutes\n"
3276 "mouse click seek to percentage in file corresponding to fraction of width\n"
3280 static int lockmgr(void **mtx, enum AVLockOp op)
3283 case AV_LOCK_CREATE:
3284 *mtx = SDL_CreateMutex();
3288 case AV_LOCK_OBTAIN:
3289 return !!SDL_LockMutex(*mtx);
3290 case AV_LOCK_RELEASE:
3291 return !!SDL_UnlockMutex(*mtx);
3292 case AV_LOCK_DESTROY:
3293 SDL_DestroyMutex(*mtx);
3299 /* Called from the main */
3300 int main(int argc, char **argv)
3304 char dummy_videodriver[] = "SDL_VIDEODRIVER=dummy";
3306 av_log_set_flags(AV_LOG_SKIP_REPEATED);
3307 parse_loglevel(argc, argv, options);
3309 /* register all codecs, demux and protocols */
3310 avcodec_register_all();
3312 avdevice_register_all();
3315 avfilter_register_all();
3318 avformat_network_init();
3322 signal(SIGINT , sigterm_handler); /* Interrupt (ANSI). */
3323 signal(SIGTERM, sigterm_handler); /* Termination (ANSI). */
3325 show_banner(argc, argv, options);
3327 parse_options(NULL, argc, argv, options, opt_input_file);
3329 if (!input_filename) {
3331 fprintf(stderr, "An input file must be specified\n");
3332 fprintf(stderr, "Use -h to get full help or, even better, run 'man %s'\n", program_name);
3336 if (display_disable) {
3339 flags = SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER;
3341 flags &= ~SDL_INIT_AUDIO;
3342 if (display_disable)
3343 SDL_putenv(dummy_videodriver); /* For the event queue, we always need a video driver. */
3344 #if !defined(__MINGW32__) && !defined(__APPLE__)
3345 flags |= SDL_INIT_EVENTTHREAD; /* Not supported on Windows or Mac OS X */
3347 if (SDL_Init (flags)) {
3348 fprintf(stderr, "Could not initialize SDL - %s\n", SDL_GetError());
3349 fprintf(stderr, "(Did you set the DISPLAY variable?)\n");
3353 if (!display_disable) {
3354 const SDL_VideoInfo *vi = SDL_GetVideoInfo();
3355 fs_screen_width = vi->current_w;
3356 fs_screen_height = vi->current_h;
3359 SDL_EventState(SDL_ACTIVEEVENT, SDL_IGNORE);
3360 SDL_EventState(SDL_SYSWMEVENT, SDL_IGNORE);
3361 SDL_EventState(SDL_USEREVENT, SDL_IGNORE);
3363 if (av_lockmgr_register(lockmgr)) {
3364 fprintf(stderr, "Could not initialize lock manager!\n");
3368 av_init_packet(&flush_pkt);
3369 flush_pkt.data = (char *)(intptr_t)"FLUSH";
3371 is = stream_open(input_filename, file_iformat);
3373 fprintf(stderr, "Failed to initialize VideoState!\n");