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/buffersink.h"
52 # include "libavfilter/buffersrc.h"
56 #include <SDL_thread.h>
62 const char program_name[] = "ffplay";
63 const int program_birth_year = 2003;
65 #define MAX_QUEUE_SIZE (15 * 1024 * 1024)
68 /* SDL audio buffer size, in samples. Should be small to have precise
69 A/V sync as SDL does not have hardware buffer fullness info. */
70 #define SDL_AUDIO_BUFFER_SIZE 1024
72 /* no AV sync correction is done if below the minimum AV sync threshold */
73 #define AV_SYNC_THRESHOLD_MIN 0.01
74 /* AV sync correction is done if above the maximum AV sync threshold */
75 #define AV_SYNC_THRESHOLD_MAX 0.1
76 /* If a frame duration is longer than this, it will not be duplicated to compensate AV sync */
77 #define AV_SYNC_FRAMEDUP_THRESHOLD 0.1
78 /* no AV correction is done if too big error */
79 #define AV_NOSYNC_THRESHOLD 10.0
81 /* maximum audio speed change to get correct sync */
82 #define SAMPLE_CORRECTION_PERCENT_MAX 10
84 /* external clock speed adjustment constants for realtime sources based on buffer fullness */
85 #define EXTERNAL_CLOCK_SPEED_MIN 0.900
86 #define EXTERNAL_CLOCK_SPEED_MAX 1.010
87 #define EXTERNAL_CLOCK_SPEED_STEP 0.001
89 /* we use about AUDIO_DIFF_AVG_NB A-V differences to make the average */
90 #define AUDIO_DIFF_AVG_NB 20
92 /* polls for possible required screen refresh at least this often, should be less than 1/fps */
93 #define REFRESH_RATE 0.01
95 /* NOTE: the size must be big enough to compensate the hardware audio buffersize size */
96 /* TODO: We assume that a decoded and resampled frame fits into this buffer */
97 #define SAMPLE_ARRAY_SIZE (8 * 65536)
99 #define CURSOR_HIDE_DELAY 1000000
101 static int64_t sws_flags = SWS_BICUBIC;
103 typedef struct MyAVPacketList {
105 struct MyAVPacketList *next;
109 typedef struct PacketQueue {
110 MyAVPacketList *first_pkt, *last_pkt;
119 #define VIDEO_PICTURE_QUEUE_SIZE 3
120 #define SUBPICTURE_QUEUE_SIZE 4
122 typedef struct VideoPicture {
123 double pts; // presentation timestamp for this picture
124 int64_t pos; // byte position in file
126 int width, height; /* source height & width */
134 typedef struct SubPicture {
135 double pts; /* presentation time stamp for this picture */
140 typedef struct AudioParams {
143 int64_t channel_layout;
144 enum AVSampleFormat fmt;
147 typedef struct Clock {
148 double pts; /* clock base */
149 double pts_drift; /* clock base minus time at which we updated the clock */
152 int serial; /* clock is based on a packet with this serial */
154 int *queue_serial; /* pointer to the current packet queue serial, used for obsolete clock detection */
158 AV_SYNC_AUDIO_MASTER, /* default choice */
159 AV_SYNC_VIDEO_MASTER,
160 AV_SYNC_EXTERNAL_CLOCK, /* synchronize to an external clock */
163 typedef struct VideoState {
164 SDL_Thread *read_tid;
165 SDL_Thread *video_tid;
166 AVInputFormat *iformat;
172 int queue_attachments_req;
177 int read_pause_return;
190 int audio_clock_serial;
191 double audio_diff_cum; /* used for AV difference average computation */
192 double audio_diff_avg_coef;
193 double audio_diff_threshold;
194 int audio_diff_avg_count;
197 int audio_hw_buf_size;
198 uint8_t silence_buf[SDL_AUDIO_BUFFER_SIZE];
201 unsigned int audio_buf_size; /* in bytes */
202 unsigned int audio_buf1_size;
203 int audio_buf_index; /* in bytes */
204 int audio_write_buf_size;
205 int audio_buf_frames_pending;
206 AVPacket audio_pkt_temp;
208 int audio_pkt_temp_serial;
209 int audio_last_serial;
210 struct AudioParams audio_src;
212 struct AudioParams audio_filter_src;
214 struct AudioParams audio_tgt;
215 struct SwrContext *swr_ctx;
216 int frame_drops_early;
217 int frame_drops_late;
221 SHOW_MODE_NONE = -1, SHOW_MODE_VIDEO = 0, SHOW_MODE_WAVES, SHOW_MODE_RDFT, SHOW_MODE_NB
223 int16_t sample_array[SAMPLE_ARRAY_SIZE];
224 int sample_array_index;
228 FFTSample *rdft_data;
230 double last_vis_time;
232 SDL_Thread *subtitle_tid;
234 AVStream *subtitle_st;
235 PacketQueue subtitleq;
236 SubPicture subpq[SUBPICTURE_QUEUE_SIZE];
237 int subpq_size, subpq_rindex, subpq_windex;
238 SDL_mutex *subpq_mutex;
239 SDL_cond *subpq_cond;
242 double frame_last_pts;
243 double frame_last_duration;
244 double frame_last_dropped_pts;
245 double frame_last_returned_time;
246 double frame_last_filter_delay;
247 int64_t frame_last_dropped_pos;
248 int frame_last_dropped_serial;
252 int64_t video_current_pos; // current displayed file pos
253 double max_frame_duration; // maximum duration of a frame - above this, we consider the jump a timestamp discontinuity
254 VideoPicture pictq[VIDEO_PICTURE_QUEUE_SIZE];
255 int pictq_size, pictq_rindex, pictq_windex;
256 SDL_mutex *pictq_mutex;
257 SDL_cond *pictq_cond;
259 struct SwsContext *img_convert_ctx;
261 SDL_Rect last_display_rect;
264 int width, height, xleft, ytop;
268 AVFilterContext *in_video_filter; // the first filter in the video chain
269 AVFilterContext *out_video_filter; // the last filter in the video chain
270 AVFilterContext *in_audio_filter; // the first filter in the audio chain
271 AVFilterContext *out_audio_filter; // the last filter in the audio chain
272 AVFilterGraph *agraph; // audio filter graph
275 int last_video_stream, last_audio_stream, last_subtitle_stream;
277 SDL_cond *continue_read_thread;
280 /* options specified by the user */
281 static AVInputFormat *file_iformat;
282 static const char *input_filename;
283 static const char *window_title;
284 static int fs_screen_width;
285 static int fs_screen_height;
286 static int default_width = 640;
287 static int default_height = 480;
288 static int screen_width = 0;
289 static int screen_height = 0;
290 static int audio_disable;
291 static int video_disable;
292 static int subtitle_disable;
293 static int wanted_stream[AVMEDIA_TYPE_NB] = {
294 [AVMEDIA_TYPE_AUDIO] = -1,
295 [AVMEDIA_TYPE_VIDEO] = -1,
296 [AVMEDIA_TYPE_SUBTITLE] = -1,
298 static int seek_by_bytes = -1;
299 static int display_disable;
300 static int show_status = 1;
301 static int av_sync_type = AV_SYNC_AUDIO_MASTER;
302 static int64_t start_time = AV_NOPTS_VALUE;
303 static int64_t duration = AV_NOPTS_VALUE;
304 static int workaround_bugs = 1;
306 static int genpts = 0;
307 static int lowres = 0;
308 static int error_concealment = 3;
309 static int decoder_reorder_pts = -1;
311 static int exit_on_keydown;
312 static int exit_on_mousedown;
314 static int framedrop = -1;
315 static int infinite_buffer = -1;
316 static enum ShowMode show_mode = SHOW_MODE_NONE;
317 static const char *audio_codec_name;
318 static const char *subtitle_codec_name;
319 static const char *video_codec_name;
320 double rdftspeed = 0.02;
321 static int64_t cursor_last_shown;
322 static int cursor_hidden = 0;
324 static char *vfilters = NULL;
325 static char *afilters = NULL;
328 /* current context */
329 static int is_full_screen;
330 static int64_t audio_callback_time;
332 static AVPacket flush_pkt;
334 #define FF_ALLOC_EVENT (SDL_USEREVENT)
335 #define FF_QUIT_EVENT (SDL_USEREVENT + 2)
337 static SDL_Surface *screen;
340 int cmp_audio_fmts(enum AVSampleFormat fmt1, int64_t channel_count1,
341 enum AVSampleFormat fmt2, int64_t channel_count2)
343 /* If channel count == 1, planar and non-planar formats are the same */
344 if (channel_count1 == 1 && channel_count2 == 1)
345 return av_get_packed_sample_fmt(fmt1) != av_get_packed_sample_fmt(fmt2);
347 return channel_count1 != channel_count2 || fmt1 != fmt2;
351 int64_t get_valid_channel_layout(int64_t channel_layout, int channels)
353 if (channel_layout && av_get_channel_layout_nb_channels(channel_layout) == channels)
354 return channel_layout;
359 static int packet_queue_put(PacketQueue *q, AVPacket *pkt);
361 static int packet_queue_put_private(PacketQueue *q, AVPacket *pkt)
363 MyAVPacketList *pkt1;
365 if (q->abort_request)
368 pkt1 = av_malloc(sizeof(MyAVPacketList));
373 if (pkt == &flush_pkt)
375 pkt1->serial = q->serial;
380 q->last_pkt->next = pkt1;
383 q->size += pkt1->pkt.size + sizeof(*pkt1);
384 /* XXX: should duplicate packet data in DV case */
385 SDL_CondSignal(q->cond);
389 static int packet_queue_put(PacketQueue *q, AVPacket *pkt)
393 /* duplicate the packet */
394 if (pkt != &flush_pkt && av_dup_packet(pkt) < 0)
397 SDL_LockMutex(q->mutex);
398 ret = packet_queue_put_private(q, pkt);
399 SDL_UnlockMutex(q->mutex);
401 if (pkt != &flush_pkt && ret < 0)
407 /* packet queue handling */
408 static void packet_queue_init(PacketQueue *q)
410 memset(q, 0, sizeof(PacketQueue));
411 q->mutex = SDL_CreateMutex();
412 q->cond = SDL_CreateCond();
413 q->abort_request = 1;
416 static void packet_queue_flush(PacketQueue *q)
418 MyAVPacketList *pkt, *pkt1;
420 SDL_LockMutex(q->mutex);
421 for (pkt = q->first_pkt; pkt != NULL; pkt = pkt1) {
423 av_free_packet(&pkt->pkt);
430 SDL_UnlockMutex(q->mutex);
433 static void packet_queue_destroy(PacketQueue *q)
435 packet_queue_flush(q);
436 SDL_DestroyMutex(q->mutex);
437 SDL_DestroyCond(q->cond);
440 static void packet_queue_abort(PacketQueue *q)
442 SDL_LockMutex(q->mutex);
444 q->abort_request = 1;
446 SDL_CondSignal(q->cond);
448 SDL_UnlockMutex(q->mutex);
451 static void packet_queue_start(PacketQueue *q)
453 SDL_LockMutex(q->mutex);
454 q->abort_request = 0;
455 packet_queue_put_private(q, &flush_pkt);
456 SDL_UnlockMutex(q->mutex);
459 /* return < 0 if aborted, 0 if no packet and > 0 if packet. */
460 static int packet_queue_get(PacketQueue *q, AVPacket *pkt, int block, int *serial)
462 MyAVPacketList *pkt1;
465 SDL_LockMutex(q->mutex);
468 if (q->abort_request) {
475 q->first_pkt = pkt1->next;
479 q->size -= pkt1->pkt.size + sizeof(*pkt1);
482 *serial = pkt1->serial;
490 SDL_CondWait(q->cond, q->mutex);
493 SDL_UnlockMutex(q->mutex);
497 static inline void fill_rectangle(SDL_Surface *screen,
498 int x, int y, int w, int h, int color, int update)
505 SDL_FillRect(screen, &rect, color);
506 if (update && w > 0 && h > 0)
507 SDL_UpdateRect(screen, x, y, w, h);
510 /* draw only the border of a rectangle */
511 static void fill_border(int xleft, int ytop, int width, int height, int x, int y, int w, int h, int color, int update)
515 /* fill the background */
519 w2 = width - (x + w);
525 h2 = height - (y + h);
528 fill_rectangle(screen,
532 fill_rectangle(screen,
533 xleft + width - w2, ytop,
536 fill_rectangle(screen,
540 fill_rectangle(screen,
541 xleft + w1, ytop + height - h2,
546 #define ALPHA_BLEND(a, oldp, newp, s)\
547 ((((oldp << s) * (255 - (a))) + (newp * (a))) / (255 << s))
549 #define RGBA_IN(r, g, b, a, s)\
551 unsigned int v = ((const uint32_t *)(s))[0];\
552 a = (v >> 24) & 0xff;\
553 r = (v >> 16) & 0xff;\
554 g = (v >> 8) & 0xff;\
558 #define YUVA_IN(y, u, v, a, s, pal)\
560 unsigned int val = ((const uint32_t *)(pal))[*(const uint8_t*)(s)];\
561 a = (val >> 24) & 0xff;\
562 y = (val >> 16) & 0xff;\
563 u = (val >> 8) & 0xff;\
567 #define YUVA_OUT(d, y, u, v, a)\
569 ((uint32_t *)(d))[0] = (a << 24) | (y << 16) | (u << 8) | v;\
575 static void blend_subrect(AVPicture *dst, const AVSubtitleRect *rect, int imgw, int imgh)
577 int wrap, wrap3, width2, skip2;
578 int y, u, v, a, u1, v1, a1, w, h;
579 uint8_t *lum, *cb, *cr;
582 int dstx, dsty, dstw, dsth;
584 dstw = av_clip(rect->w, 0, imgw);
585 dsth = av_clip(rect->h, 0, imgh);
586 dstx = av_clip(rect->x, 0, imgw - dstw);
587 dsty = av_clip(rect->y, 0, imgh - dsth);
588 lum = dst->data[0] + dsty * dst->linesize[0];
589 cb = dst->data[1] + (dsty >> 1) * dst->linesize[1];
590 cr = dst->data[2] + (dsty >> 1) * dst->linesize[2];
592 width2 = ((dstw + 1) >> 1) + (dstx & ~dstw & 1);
594 wrap = dst->linesize[0];
595 wrap3 = rect->pict.linesize[0];
596 p = rect->pict.data[0];
597 pal = (const uint32_t *)rect->pict.data[1]; /* Now in YCrCb! */
605 YUVA_IN(y, u, v, a, p, pal);
606 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
607 cb[0] = ALPHA_BLEND(a >> 2, cb[0], u, 0);
608 cr[0] = ALPHA_BLEND(a >> 2, cr[0], v, 0);
614 for (w = dstw - (dstx & 1); w >= 2; w -= 2) {
615 YUVA_IN(y, u, v, a, p, pal);
619 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
621 YUVA_IN(y, u, v, a, p + BPP, pal);
625 lum[1] = ALPHA_BLEND(a, lum[1], y, 0);
626 cb[0] = ALPHA_BLEND(a1 >> 2, cb[0], u1, 1);
627 cr[0] = ALPHA_BLEND(a1 >> 2, cr[0], v1, 1);
634 YUVA_IN(y, u, v, a, p, pal);
635 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
636 cb[0] = ALPHA_BLEND(a >> 2, cb[0], u, 0);
637 cr[0] = ALPHA_BLEND(a >> 2, cr[0], v, 0);
641 p += wrap3 - dstw * BPP;
642 lum += wrap - dstw - dstx;
643 cb += dst->linesize[1] - width2 - skip2;
644 cr += dst->linesize[2] - width2 - skip2;
646 for (h = dsth - (dsty & 1); h >= 2; h -= 2) {
652 YUVA_IN(y, u, v, a, p, pal);
656 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
659 YUVA_IN(y, u, v, a, p, pal);
663 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
664 cb[0] = ALPHA_BLEND(a1 >> 2, cb[0], u1, 1);
665 cr[0] = ALPHA_BLEND(a1 >> 2, cr[0], v1, 1);
671 for (w = dstw - (dstx & 1); w >= 2; w -= 2) {
672 YUVA_IN(y, u, v, a, p, pal);
676 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
678 YUVA_IN(y, u, v, a, p + BPP, pal);
682 lum[1] = ALPHA_BLEND(a, lum[1], y, 0);
686 YUVA_IN(y, u, v, a, p, pal);
690 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
692 YUVA_IN(y, u, v, a, p + BPP, pal);
696 lum[1] = ALPHA_BLEND(a, lum[1], y, 0);
698 cb[0] = ALPHA_BLEND(a1 >> 2, cb[0], u1, 2);
699 cr[0] = ALPHA_BLEND(a1 >> 2, cr[0], v1, 2);
703 p += -wrap3 + 2 * BPP;
707 YUVA_IN(y, u, v, a, p, pal);
711 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
714 YUVA_IN(y, u, v, a, p, pal);
718 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
719 cb[0] = ALPHA_BLEND(a1 >> 2, cb[0], u1, 1);
720 cr[0] = ALPHA_BLEND(a1 >> 2, cr[0], v1, 1);
726 p += wrap3 + (wrap3 - dstw * BPP);
727 lum += wrap + (wrap - dstw - dstx);
728 cb += dst->linesize[1] - width2 - skip2;
729 cr += dst->linesize[2] - width2 - skip2;
731 /* handle odd height */
738 YUVA_IN(y, u, v, a, p, pal);
739 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
740 cb[0] = ALPHA_BLEND(a >> 2, cb[0], u, 0);
741 cr[0] = ALPHA_BLEND(a >> 2, cr[0], v, 0);
747 for (w = dstw - (dstx & 1); w >= 2; w -= 2) {
748 YUVA_IN(y, u, v, a, p, pal);
752 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
754 YUVA_IN(y, u, v, a, p + BPP, pal);
758 lum[1] = ALPHA_BLEND(a, lum[1], y, 0);
759 cb[0] = ALPHA_BLEND(a1 >> 2, cb[0], u, 1);
760 cr[0] = ALPHA_BLEND(a1 >> 2, cr[0], v, 1);
767 YUVA_IN(y, u, v, a, p, pal);
768 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
769 cb[0] = ALPHA_BLEND(a >> 2, cb[0], u, 0);
770 cr[0] = ALPHA_BLEND(a >> 2, cr[0], v, 0);
775 static void free_subpicture(SubPicture *sp)
777 avsubtitle_free(&sp->sub);
780 static void calculate_display_rect(SDL_Rect *rect, int scr_xleft, int scr_ytop, int scr_width, int scr_height, VideoPicture *vp)
783 int width, height, x, y;
785 if (vp->sar.num == 0)
788 aspect_ratio = av_q2d(vp->sar);
790 if (aspect_ratio <= 0.0)
792 aspect_ratio *= (float)vp->width / (float)vp->height;
794 /* XXX: we suppose the screen has a 1.0 pixel ratio */
796 width = ((int)rint(height * aspect_ratio)) & ~1;
797 if (width > scr_width) {
799 height = ((int)rint(width / aspect_ratio)) & ~1;
801 x = (scr_width - width) / 2;
802 y = (scr_height - height) / 2;
803 rect->x = scr_xleft + x;
804 rect->y = scr_ytop + y;
805 rect->w = FFMAX(width, 1);
806 rect->h = FFMAX(height, 1);
809 static void video_image_display(VideoState *is)
817 vp = &is->pictq[is->pictq_rindex];
819 if (is->subtitle_st) {
820 if (is->subpq_size > 0) {
821 sp = &is->subpq[is->subpq_rindex];
823 if (vp->pts >= sp->pts + ((float) sp->sub.start_display_time / 1000)) {
824 SDL_LockYUVOverlay (vp->bmp);
826 pict.data[0] = vp->bmp->pixels[0];
827 pict.data[1] = vp->bmp->pixels[2];
828 pict.data[2] = vp->bmp->pixels[1];
830 pict.linesize[0] = vp->bmp->pitches[0];
831 pict.linesize[1] = vp->bmp->pitches[2];
832 pict.linesize[2] = vp->bmp->pitches[1];
834 for (i = 0; i < sp->sub.num_rects; i++)
835 blend_subrect(&pict, sp->sub.rects[i],
836 vp->bmp->w, vp->bmp->h);
838 SDL_UnlockYUVOverlay (vp->bmp);
843 calculate_display_rect(&rect, is->xleft, is->ytop, is->width, is->height, vp);
845 SDL_DisplayYUVOverlay(vp->bmp, &rect);
847 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) {
848 int bgcolor = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00);
849 fill_border(is->xleft, is->ytop, is->width, is->height, rect.x, rect.y, rect.w, rect.h, bgcolor, 1);
850 is->last_display_rect = rect;
855 static inline int compute_mod(int a, int b)
857 return a < 0 ? a%b + b : a%b;
860 static void video_audio_display(VideoState *s)
862 int i, i_start, x, y1, y, ys, delay, n, nb_display_channels;
863 int ch, channels, h, h2, bgcolor, fgcolor;
865 int rdft_bits, nb_freq;
867 for (rdft_bits = 1; (1 << rdft_bits) < 2 * s->height; rdft_bits++)
869 nb_freq = 1 << (rdft_bits - 1);
871 /* compute display index : center on currently output samples */
872 channels = s->audio_tgt.channels;
873 nb_display_channels = channels;
875 int data_used= s->show_mode == SHOW_MODE_WAVES ? s->width : (2*nb_freq);
877 delay = s->audio_write_buf_size;
880 /* to be more precise, we take into account the time spent since
881 the last buffer computation */
882 if (audio_callback_time) {
883 time_diff = av_gettime() - audio_callback_time;
884 delay -= (time_diff * s->audio_tgt.freq) / 1000000;
887 delay += 2 * data_used;
888 if (delay < data_used)
891 i_start= x = compute_mod(s->sample_array_index - delay * channels, SAMPLE_ARRAY_SIZE);
892 if (s->show_mode == SHOW_MODE_WAVES) {
894 for (i = 0; i < 1000; i += channels) {
895 int idx = (SAMPLE_ARRAY_SIZE + x - i) % SAMPLE_ARRAY_SIZE;
896 int a = s->sample_array[idx];
897 int b = s->sample_array[(idx + 4 * channels) % SAMPLE_ARRAY_SIZE];
898 int c = s->sample_array[(idx + 5 * channels) % SAMPLE_ARRAY_SIZE];
899 int d = s->sample_array[(idx + 9 * channels) % SAMPLE_ARRAY_SIZE];
901 if (h < score && (b ^ c) < 0) {
908 s->last_i_start = i_start;
910 i_start = s->last_i_start;
913 bgcolor = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00);
914 if (s->show_mode == SHOW_MODE_WAVES) {
915 fill_rectangle(screen,
916 s->xleft, s->ytop, s->width, s->height,
919 fgcolor = SDL_MapRGB(screen->format, 0xff, 0xff, 0xff);
921 /* total height for one channel */
922 h = s->height / nb_display_channels;
923 /* graph height / 2 */
925 for (ch = 0; ch < nb_display_channels; ch++) {
927 y1 = s->ytop + ch * h + (h / 2); /* position of center line */
928 for (x = 0; x < s->width; x++) {
929 y = (s->sample_array[i] * h2) >> 15;
936 fill_rectangle(screen,
937 s->xleft + x, ys, 1, y,
940 if (i >= SAMPLE_ARRAY_SIZE)
941 i -= SAMPLE_ARRAY_SIZE;
945 fgcolor = SDL_MapRGB(screen->format, 0x00, 0x00, 0xff);
947 for (ch = 1; ch < nb_display_channels; ch++) {
948 y = s->ytop + ch * h;
949 fill_rectangle(screen,
950 s->xleft, y, s->width, 1,
953 SDL_UpdateRect(screen, s->xleft, s->ytop, s->width, s->height);
955 nb_display_channels= FFMIN(nb_display_channels, 2);
956 if (rdft_bits != s->rdft_bits) {
957 av_rdft_end(s->rdft);
958 av_free(s->rdft_data);
959 s->rdft = av_rdft_init(rdft_bits, DFT_R2C);
960 s->rdft_bits = rdft_bits;
961 s->rdft_data = av_malloc(4 * nb_freq * sizeof(*s->rdft_data));
965 for (ch = 0; ch < nb_display_channels; ch++) {
966 data[ch] = s->rdft_data + 2 * nb_freq * ch;
968 for (x = 0; x < 2 * nb_freq; x++) {
969 double w = (x-nb_freq) * (1.0 / nb_freq);
970 data[ch][x] = s->sample_array[i] * (1.0 - w * w);
972 if (i >= SAMPLE_ARRAY_SIZE)
973 i -= SAMPLE_ARRAY_SIZE;
975 av_rdft_calc(s->rdft, data[ch]);
977 // least efficient way to do this, we should of course directly access it but its more than fast enough
978 for (y = 0; y < s->height; y++) {
979 double w = 1 / sqrt(nb_freq);
980 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]));
981 int b = (nb_display_channels == 2 ) ? sqrt(w * sqrt(data[1][2 * y + 0] * data[1][2 * y + 0]
982 + data[1][2 * y + 1] * data[1][2 * y + 1])) : a;
985 fgcolor = SDL_MapRGB(screen->format, a, b, (a + b) / 2);
987 fill_rectangle(screen,
988 s->xpos, s->height-y, 1, 1,
992 SDL_UpdateRect(screen, s->xpos, s->ytop, 1, s->height);
995 if (s->xpos >= s->width)
1000 static void stream_close(VideoState *is)
1004 /* XXX: use a special url_shutdown call to abort parse cleanly */
1005 is->abort_request = 1;
1006 SDL_WaitThread(is->read_tid, NULL);
1007 packet_queue_destroy(&is->videoq);
1008 packet_queue_destroy(&is->audioq);
1009 packet_queue_destroy(&is->subtitleq);
1011 /* free all pictures */
1012 for (i = 0; i < VIDEO_PICTURE_QUEUE_SIZE; i++) {
1015 SDL_FreeYUVOverlay(vp->bmp);
1019 SDL_DestroyMutex(is->pictq_mutex);
1020 SDL_DestroyCond(is->pictq_cond);
1021 SDL_DestroyMutex(is->subpq_mutex);
1022 SDL_DestroyCond(is->subpq_cond);
1023 SDL_DestroyCond(is->continue_read_thread);
1024 #if !CONFIG_AVFILTER
1025 sws_freeContext(is->img_convert_ctx);
1030 static void do_exit(VideoState *is)
1035 av_lockmgr_register(NULL);
1038 av_freep(&vfilters);
1040 avformat_network_deinit();
1044 av_log(NULL, AV_LOG_QUIET, "%s", "");
1048 static void sigterm_handler(int sig)
1053 static int video_open(VideoState *is, int force_set_video_mode, VideoPicture *vp)
1055 int flags = SDL_HWSURFACE | SDL_ASYNCBLIT | SDL_HWACCEL;
1059 if (is_full_screen) flags |= SDL_FULLSCREEN;
1060 else flags |= SDL_RESIZABLE;
1062 if (vp && vp->width) {
1063 calculate_display_rect(&rect, 0, 0, INT_MAX, vp->height, vp);
1064 default_width = rect.w;
1065 default_height = rect.h;
1068 if (is_full_screen && fs_screen_width) {
1069 w = fs_screen_width;
1070 h = fs_screen_height;
1071 } else if (!is_full_screen && screen_width) {
1078 w = FFMIN(16383, w);
1079 if (screen && is->width == screen->w && screen->w == w
1080 && is->height== screen->h && screen->h == h && !force_set_video_mode)
1082 screen = SDL_SetVideoMode(w, h, 0, flags);
1084 av_log(NULL, AV_LOG_FATAL, "SDL: could not set video mode - exiting\n");
1088 window_title = input_filename;
1089 SDL_WM_SetCaption(window_title, window_title);
1091 is->width = screen->w;
1092 is->height = screen->h;
1097 /* display the current picture, if any */
1098 static void video_display(VideoState *is)
1101 video_open(is, 0, NULL);
1102 if (is->audio_st && is->show_mode != SHOW_MODE_VIDEO)
1103 video_audio_display(is);
1104 else if (is->video_st)
1105 video_image_display(is);
1108 static double get_clock(Clock *c)
1110 if (*c->queue_serial != c->serial)
1115 double time = av_gettime() / 1000000.0;
1116 return c->pts_drift + time - (time - c->last_updated) * (1.0 - c->speed);
1120 static void set_clock_at(Clock *c, double pts, int serial, double time)
1123 c->last_updated = time;
1124 c->pts_drift = c->pts - time;
1128 static void set_clock(Clock *c, double pts, int serial)
1130 double time = av_gettime() / 1000000.0;
1131 set_clock_at(c, pts, serial, time);
1134 static void set_clock_speed(Clock *c, double speed)
1136 set_clock(c, get_clock(c), c->serial);
1140 static void init_clock(Clock *c, int *queue_serial)
1144 c->queue_serial = queue_serial;
1145 set_clock(c, NAN, -1);
1148 static void sync_clock_to_slave(Clock *c, Clock *slave)
1150 double clock = get_clock(c);
1151 double slave_clock = get_clock(slave);
1152 if (!isnan(slave_clock) && (isnan(clock) || fabs(clock - slave_clock) > AV_NOSYNC_THRESHOLD))
1153 set_clock(c, slave_clock, slave->serial);
1156 static int get_master_sync_type(VideoState *is) {
1157 if (is->av_sync_type == AV_SYNC_VIDEO_MASTER) {
1159 return AV_SYNC_VIDEO_MASTER;
1161 return AV_SYNC_AUDIO_MASTER;
1162 } else if (is->av_sync_type == AV_SYNC_AUDIO_MASTER) {
1164 return AV_SYNC_AUDIO_MASTER;
1166 return AV_SYNC_EXTERNAL_CLOCK;
1168 return AV_SYNC_EXTERNAL_CLOCK;
1172 /* get the current master clock value */
1173 static double get_master_clock(VideoState *is)
1177 switch (get_master_sync_type(is)) {
1178 case AV_SYNC_VIDEO_MASTER:
1179 val = get_clock(&is->vidclk);
1181 case AV_SYNC_AUDIO_MASTER:
1182 val = get_clock(&is->audclk);
1185 val = get_clock(&is->extclk);
1191 static void check_external_clock_speed(VideoState *is) {
1192 if (is->video_stream >= 0 && is->videoq.nb_packets <= MIN_FRAMES / 2 ||
1193 is->audio_stream >= 0 && is->audioq.nb_packets <= MIN_FRAMES / 2) {
1194 set_clock_speed(&is->extclk, FFMAX(EXTERNAL_CLOCK_SPEED_MIN, is->extclk.speed - EXTERNAL_CLOCK_SPEED_STEP));
1195 } else if ((is->video_stream < 0 || is->videoq.nb_packets > MIN_FRAMES * 2) &&
1196 (is->audio_stream < 0 || is->audioq.nb_packets > MIN_FRAMES * 2)) {
1197 set_clock_speed(&is->extclk, FFMIN(EXTERNAL_CLOCK_SPEED_MAX, is->extclk.speed + EXTERNAL_CLOCK_SPEED_STEP));
1199 double speed = is->extclk.speed;
1201 set_clock_speed(&is->extclk, speed + EXTERNAL_CLOCK_SPEED_STEP * (1.0 - speed) / fabs(1.0 - speed));
1205 /* seek in the stream */
1206 static void stream_seek(VideoState *is, int64_t pos, int64_t rel, int seek_by_bytes)
1208 if (!is->seek_req) {
1211 is->seek_flags &= ~AVSEEK_FLAG_BYTE;
1213 is->seek_flags |= AVSEEK_FLAG_BYTE;
1215 SDL_CondSignal(is->continue_read_thread);
1219 /* pause or resume the video */
1220 static void stream_toggle_pause(VideoState *is)
1223 is->frame_timer += av_gettime() / 1000000.0 + is->vidclk.pts_drift - is->vidclk.pts;
1224 if (is->read_pause_return != AVERROR(ENOSYS)) {
1225 is->vidclk.paused = 0;
1227 set_clock(&is->vidclk, get_clock(&is->vidclk), is->vidclk.serial);
1229 set_clock(&is->extclk, get_clock(&is->extclk), is->extclk.serial);
1230 is->paused = is->audclk.paused = is->vidclk.paused = is->extclk.paused = !is->paused;
1233 static void toggle_pause(VideoState *is)
1235 stream_toggle_pause(is);
1239 static void step_to_next_frame(VideoState *is)
1241 /* if the stream is paused unpause it, then step */
1243 stream_toggle_pause(is);
1247 static double compute_target_delay(double delay, VideoState *is)
1249 double sync_threshold, diff;
1251 /* update delay to follow master synchronisation source */
1252 if (get_master_sync_type(is) != AV_SYNC_VIDEO_MASTER) {
1253 /* if video is slave, we try to correct big delays by
1254 duplicating or deleting a frame */
1255 diff = get_clock(&is->vidclk) - get_master_clock(is);
1257 /* skip or repeat frame. We take into account the
1258 delay to compute the threshold. I still don't know
1259 if it is the best guess */
1260 sync_threshold = FFMAX(AV_SYNC_THRESHOLD_MIN, FFMIN(AV_SYNC_THRESHOLD_MAX, delay));
1261 if (!isnan(diff) && fabs(diff) < is->max_frame_duration) {
1262 if (diff <= -sync_threshold)
1263 delay = FFMAX(0, delay + diff);
1264 else if (diff >= sync_threshold && delay > AV_SYNC_FRAMEDUP_THRESHOLD)
1265 delay = delay + diff;
1266 else if (diff >= sync_threshold)
1271 av_dlog(NULL, "video: delay=%0.3f A-V=%f\n",
1277 static void pictq_next_picture(VideoState *is) {
1278 /* update queue size and signal for next picture */
1279 if (++is->pictq_rindex == VIDEO_PICTURE_QUEUE_SIZE)
1280 is->pictq_rindex = 0;
1282 SDL_LockMutex(is->pictq_mutex);
1284 SDL_CondSignal(is->pictq_cond);
1285 SDL_UnlockMutex(is->pictq_mutex);
1288 static int pictq_prev_picture(VideoState *is) {
1289 VideoPicture *prevvp;
1291 /* update queue size and signal for the previous picture */
1292 prevvp = &is->pictq[(is->pictq_rindex + VIDEO_PICTURE_QUEUE_SIZE - 1) % VIDEO_PICTURE_QUEUE_SIZE];
1293 if (prevvp->allocated && prevvp->serial == is->videoq.serial) {
1294 SDL_LockMutex(is->pictq_mutex);
1295 if (is->pictq_size < VIDEO_PICTURE_QUEUE_SIZE) {
1296 if (--is->pictq_rindex == -1)
1297 is->pictq_rindex = VIDEO_PICTURE_QUEUE_SIZE - 1;
1301 SDL_CondSignal(is->pictq_cond);
1302 SDL_UnlockMutex(is->pictq_mutex);
1307 static void update_video_pts(VideoState *is, double pts, int64_t pos, int serial) {
1308 /* update current video pts */
1309 set_clock(&is->vidclk, pts, serial);
1310 sync_clock_to_slave(&is->extclk, &is->vidclk);
1311 is->video_current_pos = pos;
1312 is->frame_last_pts = pts;
1315 /* called to display each frame */
1316 static void video_refresh(void *opaque, double *remaining_time)
1318 VideoState *is = opaque;
1322 SubPicture *sp, *sp2;
1324 if (!is->paused && get_master_sync_type(is) == AV_SYNC_EXTERNAL_CLOCK && is->realtime)
1325 check_external_clock_speed(is);
1327 if (!display_disable && is->show_mode != SHOW_MODE_VIDEO && is->audio_st) {
1328 time = av_gettime() / 1000000.0;
1329 if (is->force_refresh || is->last_vis_time + rdftspeed < time) {
1331 is->last_vis_time = time;
1333 *remaining_time = FFMIN(*remaining_time, is->last_vis_time + rdftspeed - time);
1338 if (is->force_refresh)
1339 redisplay = pictq_prev_picture(is);
1341 if (is->pictq_size == 0) {
1342 SDL_LockMutex(is->pictq_mutex);
1343 if (is->frame_last_dropped_pts != AV_NOPTS_VALUE && is->frame_last_dropped_pts > is->frame_last_pts) {
1344 update_video_pts(is, is->frame_last_dropped_pts, is->frame_last_dropped_pos, is->frame_last_dropped_serial);
1345 is->frame_last_dropped_pts = AV_NOPTS_VALUE;
1347 SDL_UnlockMutex(is->pictq_mutex);
1348 // nothing to do, no picture to display in the queue
1350 double last_duration, duration, delay;
1351 /* dequeue the picture */
1352 vp = &is->pictq[is->pictq_rindex];
1354 if (vp->serial != is->videoq.serial) {
1355 pictq_next_picture(is);
1363 /* compute nominal last_duration */
1364 last_duration = vp->pts - is->frame_last_pts;
1365 if (!isnan(last_duration) && last_duration > 0 && last_duration < is->max_frame_duration) {
1366 /* if duration of the last frame was sane, update last_duration in video state */
1367 is->frame_last_duration = last_duration;
1372 delay = compute_target_delay(is->frame_last_duration, is);
1374 time= av_gettime()/1000000.0;
1375 if (time < is->frame_timer + delay && !redisplay) {
1376 *remaining_time = FFMIN(is->frame_timer + delay - time, *remaining_time);
1380 is->frame_timer += delay;
1381 if (delay > 0 && time - is->frame_timer > AV_SYNC_THRESHOLD_MAX)
1382 is->frame_timer = time;
1384 SDL_LockMutex(is->pictq_mutex);
1385 if (!redisplay && !isnan(vp->pts))
1386 update_video_pts(is, vp->pts, vp->pos, vp->serial);
1387 SDL_UnlockMutex(is->pictq_mutex);
1389 if (is->pictq_size > 1) {
1390 VideoPicture *nextvp = &is->pictq[(is->pictq_rindex + 1) % VIDEO_PICTURE_QUEUE_SIZE];
1391 duration = nextvp->pts - vp->pts;
1392 if(!is->step && (redisplay || framedrop>0 || (framedrop && get_master_sync_type(is) != AV_SYNC_VIDEO_MASTER)) && time > is->frame_timer + duration){
1394 is->frame_drops_late++;
1395 pictq_next_picture(is);
1401 if (is->subtitle_st) {
1402 while (is->subpq_size > 0) {
1403 sp = &is->subpq[is->subpq_rindex];
1405 if (is->subpq_size > 1)
1406 sp2 = &is->subpq[(is->subpq_rindex + 1) % SUBPICTURE_QUEUE_SIZE];
1410 if (sp->serial != is->subtitleq.serial
1411 || (is->vidclk.pts > (sp->pts + ((float) sp->sub.end_display_time / 1000)))
1412 || (sp2 && is->vidclk.pts > (sp2->pts + ((float) sp2->sub.start_display_time / 1000))))
1414 free_subpicture(sp);
1416 /* update queue size and signal for next picture */
1417 if (++is->subpq_rindex == SUBPICTURE_QUEUE_SIZE)
1418 is->subpq_rindex = 0;
1420 SDL_LockMutex(is->subpq_mutex);
1422 SDL_CondSignal(is->subpq_cond);
1423 SDL_UnlockMutex(is->subpq_mutex);
1431 /* display picture */
1432 if (!display_disable && is->show_mode == SHOW_MODE_VIDEO)
1435 pictq_next_picture(is);
1437 if (is->step && !is->paused)
1438 stream_toggle_pause(is);
1441 is->force_refresh = 0;
1443 static int64_t last_time;
1445 int aqsize, vqsize, sqsize;
1448 cur_time = av_gettime();
1449 if (!last_time || (cur_time - last_time) >= 30000) {
1454 aqsize = is->audioq.size;
1456 vqsize = is->videoq.size;
1457 if (is->subtitle_st)
1458 sqsize = is->subtitleq.size;
1460 if (is->audio_st && is->video_st)
1461 av_diff = get_clock(&is->audclk) - get_clock(&is->vidclk);
1462 else if (is->video_st)
1463 av_diff = get_master_clock(is) - get_clock(&is->vidclk);
1464 else if (is->audio_st)
1465 av_diff = get_master_clock(is) - get_clock(&is->audclk);
1466 av_log(NULL, AV_LOG_INFO,
1467 "%7.2f %s:%7.3f fd=%4d aq=%5dKB vq=%5dKB sq=%5dB f=%"PRId64"/%"PRId64" \r",
1468 get_master_clock(is),
1469 (is->audio_st && is->video_st) ? "A-V" : (is->video_st ? "M-V" : (is->audio_st ? "M-A" : " ")),
1471 is->frame_drops_early + is->frame_drops_late,
1475 is->video_st ? is->video_st->codec->pts_correction_num_faulty_dts : 0,
1476 is->video_st ? is->video_st->codec->pts_correction_num_faulty_pts : 0);
1478 last_time = cur_time;
1483 /* allocate a picture (needs to do that in main thread to avoid
1484 potential locking problems */
1485 static void alloc_picture(VideoState *is)
1490 vp = &is->pictq[is->pictq_windex];
1493 SDL_FreeYUVOverlay(vp->bmp);
1495 video_open(is, 0, vp);
1497 vp->bmp = SDL_CreateYUVOverlay(vp->width, vp->height,
1500 bufferdiff = vp->bmp ? FFMAX(vp->bmp->pixels[0], vp->bmp->pixels[1]) - FFMIN(vp->bmp->pixels[0], vp->bmp->pixels[1]) : 0;
1501 if (!vp->bmp || vp->bmp->pitches[0] < vp->width || bufferdiff < (int64_t)vp->height * vp->bmp->pitches[0]) {
1502 /* SDL allocates a buffer smaller than requested if the video
1503 * overlay hardware is unable to support the requested size. */
1504 av_log(NULL, AV_LOG_FATAL,
1505 "Error: the video system does not support an image\n"
1506 "size of %dx%d pixels. Try using -lowres or -vf \"scale=w:h\"\n"
1507 "to reduce the image size.\n", vp->width, vp->height );
1511 SDL_LockMutex(is->pictq_mutex);
1513 SDL_CondSignal(is->pictq_cond);
1514 SDL_UnlockMutex(is->pictq_mutex);
1517 static void duplicate_right_border_pixels(SDL_Overlay *bmp) {
1518 int i, width, height;
1520 for (i = 0; i < 3; i++) {
1527 if (bmp->pitches[i] > width) {
1528 maxp = bmp->pixels[i] + bmp->pitches[i] * height - 1;
1529 for (p = bmp->pixels[i] + width - 1; p < maxp; p += bmp->pitches[i])
1535 static int queue_picture(VideoState *is, AVFrame *src_frame, double pts, int64_t pos, int serial)
1539 #if defined(DEBUG_SYNC) && 0
1540 printf("frame_type=%c pts=%0.3f\n",
1541 av_get_picture_type_char(src_frame->pict_type), pts);
1544 /* wait until we have space to put a new picture */
1545 SDL_LockMutex(is->pictq_mutex);
1547 /* keep the last already displayed picture in the queue */
1548 while (is->pictq_size >= VIDEO_PICTURE_QUEUE_SIZE - 1 &&
1549 !is->videoq.abort_request) {
1550 SDL_CondWait(is->pictq_cond, is->pictq_mutex);
1552 SDL_UnlockMutex(is->pictq_mutex);
1554 if (is->videoq.abort_request)
1557 vp = &is->pictq[is->pictq_windex];
1559 vp->sar = src_frame->sample_aspect_ratio;
1561 /* alloc or resize hardware picture buffer */
1562 if (!vp->bmp || vp->reallocate || !vp->allocated ||
1563 vp->width != src_frame->width ||
1564 vp->height != src_frame->height) {
1569 vp->width = src_frame->width;
1570 vp->height = src_frame->height;
1572 /* the allocation must be done in the main thread to avoid
1573 locking problems. */
1574 event.type = FF_ALLOC_EVENT;
1575 event.user.data1 = is;
1576 SDL_PushEvent(&event);
1578 /* wait until the picture is allocated */
1579 SDL_LockMutex(is->pictq_mutex);
1580 while (!vp->allocated && !is->videoq.abort_request) {
1581 SDL_CondWait(is->pictq_cond, is->pictq_mutex);
1583 /* if the queue is aborted, we have to pop the pending ALLOC event or wait for the allocation to complete */
1584 if (is->videoq.abort_request && SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_EVENTMASK(FF_ALLOC_EVENT)) != 1) {
1585 while (!vp->allocated) {
1586 SDL_CondWait(is->pictq_cond, is->pictq_mutex);
1589 SDL_UnlockMutex(is->pictq_mutex);
1591 if (is->videoq.abort_request)
1595 /* if the frame is not skipped, then display it */
1597 AVPicture pict = { { 0 } };
1599 /* get a pointer on the bitmap */
1600 SDL_LockYUVOverlay (vp->bmp);
1602 pict.data[0] = vp->bmp->pixels[0];
1603 pict.data[1] = vp->bmp->pixels[2];
1604 pict.data[2] = vp->bmp->pixels[1];
1606 pict.linesize[0] = vp->bmp->pitches[0];
1607 pict.linesize[1] = vp->bmp->pitches[2];
1608 pict.linesize[2] = vp->bmp->pitches[1];
1611 // FIXME use direct rendering
1612 av_picture_copy(&pict, (AVPicture *)src_frame,
1613 src_frame->format, vp->width, vp->height);
1615 av_opt_get_int(sws_opts, "sws_flags", 0, &sws_flags);
1616 is->img_convert_ctx = sws_getCachedContext(is->img_convert_ctx,
1617 vp->width, vp->height, src_frame->format, vp->width, vp->height,
1618 AV_PIX_FMT_YUV420P, sws_flags, NULL, NULL, NULL);
1619 if (is->img_convert_ctx == NULL) {
1620 av_log(NULL, AV_LOG_FATAL, "Cannot initialize the conversion context\n");
1623 sws_scale(is->img_convert_ctx, src_frame->data, src_frame->linesize,
1624 0, vp->height, pict.data, pict.linesize);
1626 /* workaround SDL PITCH_WORKAROUND */
1627 duplicate_right_border_pixels(vp->bmp);
1628 /* update the bitmap content */
1629 SDL_UnlockYUVOverlay(vp->bmp);
1633 vp->serial = serial;
1635 /* now we can update the picture count */
1636 if (++is->pictq_windex == VIDEO_PICTURE_QUEUE_SIZE)
1637 is->pictq_windex = 0;
1638 SDL_LockMutex(is->pictq_mutex);
1640 SDL_UnlockMutex(is->pictq_mutex);
1645 static int get_video_frame(VideoState *is, AVFrame *frame, AVPacket *pkt, int *serial)
1649 if (packet_queue_get(&is->videoq, pkt, 1, serial) < 0)
1652 if (pkt->data == flush_pkt.data) {
1653 avcodec_flush_buffers(is->video_st->codec);
1655 SDL_LockMutex(is->pictq_mutex);
1656 // Make sure there are no long delay timers (ideally we should just flush the queue but that's harder)
1657 while (is->pictq_size && !is->videoq.abort_request) {
1658 SDL_CondWait(is->pictq_cond, is->pictq_mutex);
1660 is->video_current_pos = -1;
1661 is->frame_last_pts = AV_NOPTS_VALUE;
1662 is->frame_last_duration = 0;
1663 is->frame_timer = (double)av_gettime() / 1000000.0;
1664 is->frame_last_dropped_pts = AV_NOPTS_VALUE;
1665 SDL_UnlockMutex(is->pictq_mutex);
1669 if(avcodec_decode_video2(is->video_st->codec, frame, &got_picture, pkt) < 0)
1676 if (decoder_reorder_pts == -1) {
1677 frame->pts = av_frame_get_best_effort_timestamp(frame);
1678 } else if (decoder_reorder_pts) {
1679 frame->pts = frame->pkt_pts;
1681 frame->pts = frame->pkt_dts;
1684 if (frame->pts != AV_NOPTS_VALUE)
1685 dpts = av_q2d(is->video_st->time_base) * frame->pts;
1687 frame->sample_aspect_ratio = av_guess_sample_aspect_ratio(is->ic, is->video_st, frame);
1689 if (framedrop>0 || (framedrop && get_master_sync_type(is) != AV_SYNC_VIDEO_MASTER)) {
1690 SDL_LockMutex(is->pictq_mutex);
1691 if (is->frame_last_pts != AV_NOPTS_VALUE && frame->pts != AV_NOPTS_VALUE) {
1692 double clockdiff = get_clock(&is->vidclk) - get_master_clock(is);
1693 double ptsdiff = dpts - is->frame_last_pts;
1694 if (!isnan(clockdiff) && fabs(clockdiff) < AV_NOSYNC_THRESHOLD &&
1695 !isnan(ptsdiff) && ptsdiff > 0 && ptsdiff < AV_NOSYNC_THRESHOLD &&
1696 clockdiff + ptsdiff - is->frame_last_filter_delay < 0 &&
1697 is->videoq.nb_packets) {
1698 is->frame_last_dropped_pos = pkt->pos;
1699 is->frame_last_dropped_pts = dpts;
1700 is->frame_last_dropped_serial = *serial;
1701 is->frame_drops_early++;
1702 av_frame_unref(frame);
1706 SDL_UnlockMutex(is->pictq_mutex);
1715 static int configure_filtergraph(AVFilterGraph *graph, const char *filtergraph,
1716 AVFilterContext *source_ctx, AVFilterContext *sink_ctx)
1719 AVFilterInOut *outputs = NULL, *inputs = NULL;
1722 outputs = avfilter_inout_alloc();
1723 inputs = avfilter_inout_alloc();
1724 if (!outputs || !inputs) {
1725 ret = AVERROR(ENOMEM);
1729 outputs->name = av_strdup("in");
1730 outputs->filter_ctx = source_ctx;
1731 outputs->pad_idx = 0;
1732 outputs->next = NULL;
1734 inputs->name = av_strdup("out");
1735 inputs->filter_ctx = sink_ctx;
1736 inputs->pad_idx = 0;
1737 inputs->next = NULL;
1739 if ((ret = avfilter_graph_parse_ptr(graph, filtergraph, &inputs, &outputs, NULL)) < 0)
1742 if ((ret = avfilter_link(source_ctx, 0, sink_ctx, 0)) < 0)
1746 ret = avfilter_graph_config(graph, NULL);
1748 avfilter_inout_free(&outputs);
1749 avfilter_inout_free(&inputs);
1753 static int configure_video_filters(AVFilterGraph *graph, VideoState *is, const char *vfilters, AVFrame *frame)
1755 static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE };
1756 char sws_flags_str[128];
1757 char buffersrc_args[256];
1759 AVFilterContext *filt_src = NULL, *filt_out = NULL, *filt_crop;
1760 AVCodecContext *codec = is->video_st->codec;
1761 AVRational fr = av_guess_frame_rate(is->ic, is->video_st, NULL);
1763 av_opt_get_int(sws_opts, "sws_flags", 0, &sws_flags);
1764 snprintf(sws_flags_str, sizeof(sws_flags_str), "flags=%"PRId64, sws_flags);
1765 graph->scale_sws_opts = av_strdup(sws_flags_str);
1767 snprintf(buffersrc_args, sizeof(buffersrc_args),
1768 "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d",
1769 frame->width, frame->height, frame->format,
1770 is->video_st->time_base.num, is->video_st->time_base.den,
1771 codec->sample_aspect_ratio.num, FFMAX(codec->sample_aspect_ratio.den, 1));
1772 if (fr.num && fr.den)
1773 av_strlcatf(buffersrc_args, sizeof(buffersrc_args), ":frame_rate=%d/%d", fr.num, fr.den);
1775 if ((ret = avfilter_graph_create_filter(&filt_src,
1776 avfilter_get_by_name("buffer"),
1777 "ffplay_buffer", buffersrc_args, NULL,
1781 ret = avfilter_graph_create_filter(&filt_out,
1782 avfilter_get_by_name("buffersink"),
1783 "ffplay_buffersink", NULL, NULL, graph);
1787 if ((ret = av_opt_set_int_list(filt_out, "pix_fmts", pix_fmts, AV_PIX_FMT_NONE, AV_OPT_SEARCH_CHILDREN)) < 0)
1790 /* SDL YUV code is not handling odd width/height for some driver
1791 * combinations, therefore we crop the picture to an even width/height. */
1792 if ((ret = avfilter_graph_create_filter(&filt_crop,
1793 avfilter_get_by_name("crop"),
1794 "ffplay_crop", "floor(in_w/2)*2:floor(in_h/2)*2", NULL, graph)) < 0)
1796 if ((ret = avfilter_link(filt_crop, 0, filt_out, 0)) < 0)
1799 if ((ret = configure_filtergraph(graph, vfilters, filt_src, filt_crop)) < 0)
1802 is->in_video_filter = filt_src;
1803 is->out_video_filter = filt_out;
1809 static int configure_audio_filters(VideoState *is, const char *afilters, int force_output_format)
1811 static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE };
1812 int sample_rates[2] = { 0, -1 };
1813 int64_t channel_layouts[2] = { 0, -1 };
1814 int channels[2] = { 0, -1 };
1815 AVFilterContext *filt_asrc = NULL, *filt_asink = NULL;
1816 char asrc_args[256];
1819 avfilter_graph_free(&is->agraph);
1820 if (!(is->agraph = avfilter_graph_alloc()))
1821 return AVERROR(ENOMEM);
1823 ret = snprintf(asrc_args, sizeof(asrc_args),
1824 "sample_rate=%d:sample_fmt=%s:channels=%d:time_base=%d/%d",
1825 is->audio_filter_src.freq, av_get_sample_fmt_name(is->audio_filter_src.fmt),
1826 is->audio_filter_src.channels,
1827 1, is->audio_filter_src.freq);
1828 if (is->audio_filter_src.channel_layout)
1829 snprintf(asrc_args + ret, sizeof(asrc_args) - ret,
1830 ":channel_layout=0x%"PRIx64, is->audio_filter_src.channel_layout);
1832 ret = avfilter_graph_create_filter(&filt_asrc,
1833 avfilter_get_by_name("abuffer"), "ffplay_abuffer",
1834 asrc_args, NULL, is->agraph);
1839 ret = avfilter_graph_create_filter(&filt_asink,
1840 avfilter_get_by_name("abuffersink"), "ffplay_abuffersink",
1841 NULL, NULL, is->agraph);
1845 if ((ret = av_opt_set_int_list(filt_asink, "sample_fmts", sample_fmts, AV_SAMPLE_FMT_NONE, AV_OPT_SEARCH_CHILDREN)) < 0)
1847 if ((ret = av_opt_set_int(filt_asink, "all_channel_counts", 1, AV_OPT_SEARCH_CHILDREN)) < 0)
1850 if (force_output_format) {
1851 channel_layouts[0] = is->audio_tgt.channel_layout;
1852 channels [0] = is->audio_tgt.channels;
1853 sample_rates [0] = is->audio_tgt.freq;
1854 if ((ret = av_opt_set_int(filt_asink, "all_channel_counts", 0, AV_OPT_SEARCH_CHILDREN)) < 0)
1856 if ((ret = av_opt_set_int_list(filt_asink, "channel_layouts", channel_layouts, -1, AV_OPT_SEARCH_CHILDREN)) < 0)
1858 if ((ret = av_opt_set_int_list(filt_asink, "channel_counts" , channels , -1, AV_OPT_SEARCH_CHILDREN)) < 0)
1860 if ((ret = av_opt_set_int_list(filt_asink, "sample_rates" , sample_rates , -1, AV_OPT_SEARCH_CHILDREN)) < 0)
1865 if ((ret = configure_filtergraph(is->agraph, afilters, filt_asrc, filt_asink)) < 0)
1868 is->in_audio_filter = filt_asrc;
1869 is->out_audio_filter = filt_asink;
1873 avfilter_graph_free(&is->agraph);
1876 #endif /* CONFIG_AVFILTER */
1878 static int video_thread(void *arg)
1880 AVPacket pkt = { 0 };
1881 VideoState *is = arg;
1882 AVFrame *frame = av_frame_alloc();
1888 AVFilterGraph *graph = avfilter_graph_alloc();
1889 AVFilterContext *filt_out = NULL, *filt_in = NULL;
1892 enum AVPixelFormat last_format = -2;
1893 int last_serial = -1;
1897 while (is->paused && !is->videoq.abort_request)
1900 avcodec_get_frame_defaults(frame);
1901 av_free_packet(&pkt);
1903 ret = get_video_frame(is, frame, &pkt, &serial);
1910 if ( last_w != frame->width
1911 || last_h != frame->height
1912 || last_format != frame->format
1913 || last_serial != serial) {
1914 av_log(NULL, AV_LOG_DEBUG,
1915 "Video frame changed from size:%dx%d format:%s serial:%d to size:%dx%d format:%s serial:%d\n",
1917 (const char *)av_x_if_null(av_get_pix_fmt_name(last_format), "none"), last_serial,
1918 frame->width, frame->height,
1919 (const char *)av_x_if_null(av_get_pix_fmt_name(frame->format), "none"), serial);
1920 avfilter_graph_free(&graph);
1921 graph = avfilter_graph_alloc();
1922 if ((ret = configure_video_filters(graph, is, vfilters, frame)) < 0) {
1924 event.type = FF_QUIT_EVENT;
1925 event.user.data1 = is;
1926 SDL_PushEvent(&event);
1927 av_free_packet(&pkt);
1930 filt_in = is->in_video_filter;
1931 filt_out = is->out_video_filter;
1932 last_w = frame->width;
1933 last_h = frame->height;
1934 last_format = frame->format;
1935 last_serial = serial;
1938 ret = av_buffersrc_add_frame(filt_in, frame);
1941 av_frame_unref(frame);
1942 avcodec_get_frame_defaults(frame);
1943 av_free_packet(&pkt);
1946 is->frame_last_returned_time = av_gettime() / 1000000.0;
1948 ret = av_buffersink_get_frame_flags(filt_out, frame, 0);
1954 is->frame_last_filter_delay = av_gettime() / 1000000.0 - is->frame_last_returned_time;
1955 if (fabs(is->frame_last_filter_delay) > AV_NOSYNC_THRESHOLD / 10.0)
1956 is->frame_last_filter_delay = 0;
1958 pts = (frame->pts == AV_NOPTS_VALUE) ? NAN : frame->pts * av_q2d(filt_out->inputs[0]->time_base);
1959 ret = queue_picture(is, frame, pts, av_frame_get_pkt_pos(frame), serial);
1960 av_frame_unref(frame);
1963 pts = (frame->pts == AV_NOPTS_VALUE) ? NAN : frame->pts * av_q2d(is->video_st->time_base);
1964 ret = queue_picture(is, frame, pts, pkt.pos, serial);
1965 av_frame_unref(frame);
1972 avcodec_flush_buffers(is->video_st->codec);
1974 avfilter_graph_free(&graph);
1976 av_free_packet(&pkt);
1977 av_frame_free(&frame);
1981 static int subtitle_thread(void *arg)
1983 VideoState *is = arg;
1985 AVPacket pkt1, *pkt = &pkt1;
1990 int r, g, b, y, u, v, a;
1993 while (is->paused && !is->subtitleq.abort_request) {
1996 if (packet_queue_get(&is->subtitleq, pkt, 1, &serial) < 0)
1999 if (pkt->data == flush_pkt.data) {
2000 avcodec_flush_buffers(is->subtitle_st->codec);
2003 SDL_LockMutex(is->subpq_mutex);
2004 while (is->subpq_size >= SUBPICTURE_QUEUE_SIZE &&
2005 !is->subtitleq.abort_request) {
2006 SDL_CondWait(is->subpq_cond, is->subpq_mutex);
2008 SDL_UnlockMutex(is->subpq_mutex);
2010 if (is->subtitleq.abort_request)
2013 sp = &is->subpq[is->subpq_windex];
2015 /* NOTE: ipts is the PTS of the _first_ picture beginning in
2016 this packet, if any */
2018 if (pkt->pts != AV_NOPTS_VALUE)
2019 pts = av_q2d(is->subtitle_st->time_base) * pkt->pts;
2021 avcodec_decode_subtitle2(is->subtitle_st->codec, &sp->sub,
2022 &got_subtitle, pkt);
2023 if (got_subtitle && sp->sub.format == 0) {
2024 if (sp->sub.pts != AV_NOPTS_VALUE)
2025 pts = sp->sub.pts / (double)AV_TIME_BASE;
2027 sp->serial = serial;
2029 for (i = 0; i < sp->sub.num_rects; i++)
2031 for (j = 0; j < sp->sub.rects[i]->nb_colors; j++)
2033 RGBA_IN(r, g, b, a, (uint32_t*)sp->sub.rects[i]->pict.data[1] + j);
2034 y = RGB_TO_Y_CCIR(r, g, b);
2035 u = RGB_TO_U_CCIR(r, g, b, 0);
2036 v = RGB_TO_V_CCIR(r, g, b, 0);
2037 YUVA_OUT((uint32_t*)sp->sub.rects[i]->pict.data[1] + j, y, u, v, a);
2041 /* now we can update the picture count */
2042 if (++is->subpq_windex == SUBPICTURE_QUEUE_SIZE)
2043 is->subpq_windex = 0;
2044 SDL_LockMutex(is->subpq_mutex);
2046 SDL_UnlockMutex(is->subpq_mutex);
2048 av_free_packet(pkt);
2053 /* copy samples for viewing in editor window */
2054 static void update_sample_display(VideoState *is, short *samples, int samples_size)
2058 size = samples_size / sizeof(short);
2060 len = SAMPLE_ARRAY_SIZE - is->sample_array_index;
2063 memcpy(is->sample_array + is->sample_array_index, samples, len * sizeof(short));
2065 is->sample_array_index += len;
2066 if (is->sample_array_index >= SAMPLE_ARRAY_SIZE)
2067 is->sample_array_index = 0;
2072 /* return the wanted number of samples to get better sync if sync_type is video
2073 * or external master clock */
2074 static int synchronize_audio(VideoState *is, int nb_samples)
2076 int wanted_nb_samples = nb_samples;
2078 /* if not master, then we try to remove or add samples to correct the clock */
2079 if (get_master_sync_type(is) != AV_SYNC_AUDIO_MASTER) {
2080 double diff, avg_diff;
2081 int min_nb_samples, max_nb_samples;
2083 diff = get_clock(&is->audclk) - get_master_clock(is);
2085 if (!isnan(diff) && fabs(diff) < AV_NOSYNC_THRESHOLD) {
2086 is->audio_diff_cum = diff + is->audio_diff_avg_coef * is->audio_diff_cum;
2087 if (is->audio_diff_avg_count < AUDIO_DIFF_AVG_NB) {
2088 /* not enough measures to have a correct estimate */
2089 is->audio_diff_avg_count++;
2091 /* estimate the A-V difference */
2092 avg_diff = is->audio_diff_cum * (1.0 - is->audio_diff_avg_coef);
2094 if (fabs(avg_diff) >= is->audio_diff_threshold) {
2095 wanted_nb_samples = nb_samples + (int)(diff * is->audio_src.freq);
2096 min_nb_samples = ((nb_samples * (100 - SAMPLE_CORRECTION_PERCENT_MAX) / 100));
2097 max_nb_samples = ((nb_samples * (100 + SAMPLE_CORRECTION_PERCENT_MAX) / 100));
2098 wanted_nb_samples = FFMIN(FFMAX(wanted_nb_samples, min_nb_samples), max_nb_samples);
2100 av_dlog(NULL, "diff=%f adiff=%f sample_diff=%d apts=%0.3f %f\n",
2101 diff, avg_diff, wanted_nb_samples - nb_samples,
2102 is->audio_clock, is->audio_diff_threshold);
2105 /* too big difference : may be initial PTS errors, so
2107 is->audio_diff_avg_count = 0;
2108 is->audio_diff_cum = 0;
2112 return wanted_nb_samples;
2116 * Decode one audio frame and return its uncompressed size.
2118 * The processed audio frame is decoded, converted if required, and
2119 * stored in is->audio_buf, with size in bytes given by the return
2122 static int audio_decode_frame(VideoState *is)
2124 AVPacket *pkt_temp = &is->audio_pkt_temp;
2125 AVPacket *pkt = &is->audio_pkt;
2126 AVCodecContext *dec = is->audio_st->codec;
2127 int len1, data_size, resampled_data_size;
2128 int64_t dec_channel_layout;
2130 av_unused double audio_clock0;
2132 int flush_complete = 0;
2133 int wanted_nb_samples;
2139 /* NOTE: the audio packet can contain several frames */
2140 while (pkt_temp->size > 0 || (!pkt_temp->data && new_packet) || is->audio_buf_frames_pending) {
2142 if (!(is->frame = avcodec_alloc_frame()))
2143 return AVERROR(ENOMEM);
2145 av_frame_unref(is->frame);
2146 avcodec_get_frame_defaults(is->frame);
2149 if (is->audioq.serial != is->audio_pkt_temp_serial)
2155 if (!is->audio_buf_frames_pending) {
2159 len1 = avcodec_decode_audio4(dec, is->frame, &got_frame, pkt_temp);
2161 /* if error, we skip the frame */
2166 pkt_temp->data += len1;
2167 pkt_temp->size -= len1;
2170 /* stop sending empty packets if the decoder is finished */
2171 if (!pkt_temp->data && dec->codec->capabilities & CODEC_CAP_DELAY)
2176 tb = (AVRational){1, is->frame->sample_rate};
2177 if (is->frame->pts != AV_NOPTS_VALUE)
2178 is->frame->pts = av_rescale_q(is->frame->pts, dec->time_base, tb);
2179 else if (is->frame->pkt_pts != AV_NOPTS_VALUE)
2180 is->frame->pts = av_rescale_q(is->frame->pkt_pts, is->audio_st->time_base, tb);
2181 if (pkt_temp->pts != AV_NOPTS_VALUE)
2182 pkt_temp->pts += (double) is->frame->nb_samples / is->frame->sample_rate / av_q2d(is->audio_st->time_base);
2185 dec_channel_layout = get_valid_channel_layout(is->frame->channel_layout, av_frame_get_channels(is->frame));
2188 cmp_audio_fmts(is->audio_filter_src.fmt, is->audio_filter_src.channels,
2189 is->frame->format, av_frame_get_channels(is->frame)) ||
2190 is->audio_filter_src.channel_layout != dec_channel_layout ||
2191 is->audio_filter_src.freq != is->frame->sample_rate ||
2192 is->audio_pkt_temp_serial != is->audio_last_serial;
2195 char buf1[1024], buf2[1024];
2196 av_get_channel_layout_string(buf1, sizeof(buf1), -1, is->audio_filter_src.channel_layout);
2197 av_get_channel_layout_string(buf2, sizeof(buf2), -1, dec_channel_layout);
2198 av_log(NULL, AV_LOG_DEBUG,
2199 "Audio frame changed from rate:%d ch:%d fmt:%s layout:%s serial:%d to rate:%d ch:%d fmt:%s layout:%s serial:%d\n",
2200 is->audio_filter_src.freq, is->audio_filter_src.channels, av_get_sample_fmt_name(is->audio_filter_src.fmt), buf1, is->audio_last_serial,
2201 is->frame->sample_rate, av_frame_get_channels(is->frame), av_get_sample_fmt_name(is->frame->format), buf2, is->audio_pkt_temp_serial);
2203 is->audio_filter_src.fmt = is->frame->format;
2204 is->audio_filter_src.channels = av_frame_get_channels(is->frame);
2205 is->audio_filter_src.channel_layout = dec_channel_layout;
2206 is->audio_filter_src.freq = is->frame->sample_rate;
2207 is->audio_last_serial = is->audio_pkt_temp_serial;
2209 if ((ret = configure_audio_filters(is, afilters, 1)) < 0)
2213 if ((ret = av_buffersrc_add_frame(is->in_audio_filter, is->frame)) < 0)
2215 av_frame_unref(is->frame);
2219 if ((ret = av_buffersink_get_frame_flags(is->out_audio_filter, is->frame, 0)) < 0) {
2220 if (ret == AVERROR(EAGAIN)) {
2221 is->audio_buf_frames_pending = 0;
2226 is->audio_buf_frames_pending = 1;
2227 tb = is->out_audio_filter->inputs[0]->time_base;
2230 data_size = av_samples_get_buffer_size(NULL, av_frame_get_channels(is->frame),
2231 is->frame->nb_samples,
2232 is->frame->format, 1);
2234 dec_channel_layout =
2235 (is->frame->channel_layout && av_frame_get_channels(is->frame) == av_get_channel_layout_nb_channels(is->frame->channel_layout)) ?
2236 is->frame->channel_layout : av_get_default_channel_layout(av_frame_get_channels(is->frame));
2237 wanted_nb_samples = synchronize_audio(is, is->frame->nb_samples);
2239 if (is->frame->format != is->audio_src.fmt ||
2240 dec_channel_layout != is->audio_src.channel_layout ||
2241 is->frame->sample_rate != is->audio_src.freq ||
2242 (wanted_nb_samples != is->frame->nb_samples && !is->swr_ctx)) {
2243 swr_free(&is->swr_ctx);
2244 is->swr_ctx = swr_alloc_set_opts(NULL,
2245 is->audio_tgt.channel_layout, is->audio_tgt.fmt, is->audio_tgt.freq,
2246 dec_channel_layout, is->frame->format, is->frame->sample_rate,
2248 if (!is->swr_ctx || swr_init(is->swr_ctx) < 0) {
2249 av_log(NULL, AV_LOG_ERROR,
2250 "Cannot create sample rate converter for conversion of %d Hz %s %d channels to %d Hz %s %d channels!\n",
2251 is->frame->sample_rate, av_get_sample_fmt_name(is->frame->format), av_frame_get_channels(is->frame),
2252 is->audio_tgt.freq, av_get_sample_fmt_name(is->audio_tgt.fmt), is->audio_tgt.channels);
2255 is->audio_src.channel_layout = dec_channel_layout;
2256 is->audio_src.channels = av_frame_get_channels(is->frame);
2257 is->audio_src.freq = is->frame->sample_rate;
2258 is->audio_src.fmt = is->frame->format;
2262 const uint8_t **in = (const uint8_t **)is->frame->extended_data;
2263 uint8_t **out = &is->audio_buf1;
2264 int out_count = (int64_t)wanted_nb_samples * is->audio_tgt.freq / is->frame->sample_rate + 256;
2265 int out_size = av_samples_get_buffer_size(NULL, is->audio_tgt.channels, out_count, is->audio_tgt.fmt, 0);
2268 av_log(NULL, AV_LOG_ERROR, "av_samples_get_buffer_size() failed\n");
2271 if (wanted_nb_samples != is->frame->nb_samples) {
2272 if (swr_set_compensation(is->swr_ctx, (wanted_nb_samples - is->frame->nb_samples) * is->audio_tgt.freq / is->frame->sample_rate,
2273 wanted_nb_samples * is->audio_tgt.freq / is->frame->sample_rate) < 0) {
2274 av_log(NULL, AV_LOG_ERROR, "swr_set_compensation() failed\n");
2278 av_fast_malloc(&is->audio_buf1, &is->audio_buf1_size, out_size);
2279 if (!is->audio_buf1)
2280 return AVERROR(ENOMEM);
2281 len2 = swr_convert(is->swr_ctx, out, out_count, in, is->frame->nb_samples);
2283 av_log(NULL, AV_LOG_ERROR, "swr_convert() failed\n");
2286 if (len2 == out_count) {
2287 av_log(NULL, AV_LOG_WARNING, "audio buffer is probably too small\n");
2288 swr_init(is->swr_ctx);
2290 is->audio_buf = is->audio_buf1;
2291 resampled_data_size = len2 * is->audio_tgt.channels * av_get_bytes_per_sample(is->audio_tgt.fmt);
2293 is->audio_buf = is->frame->data[0];
2294 resampled_data_size = data_size;
2297 audio_clock0 = is->audio_clock;
2298 /* update the audio clock with the pts */
2299 if (is->frame->pts != AV_NOPTS_VALUE)
2300 is->audio_clock = is->frame->pts * av_q2d(tb) + (double) is->frame->nb_samples / is->frame->sample_rate;
2302 is->audio_clock = NAN;
2303 is->audio_clock_serial = is->audio_pkt_temp_serial;
2306 static double last_clock;
2307 printf("audio: delay=%0.3f clock=%0.3f clock0=%0.3f\n",
2308 is->audio_clock - last_clock,
2309 is->audio_clock, audio_clock0);
2310 last_clock = is->audio_clock;
2313 return resampled_data_size;
2316 /* free the current packet */
2318 av_free_packet(pkt);
2319 memset(pkt_temp, 0, sizeof(*pkt_temp));
2321 if (is->audioq.abort_request) {
2325 if (is->audioq.nb_packets == 0)
2326 SDL_CondSignal(is->continue_read_thread);
2328 /* read next packet */
2329 if ((new_packet = packet_queue_get(&is->audioq, pkt, 1, &is->audio_pkt_temp_serial)) < 0)
2332 if (pkt->data == flush_pkt.data) {
2333 avcodec_flush_buffers(dec);
2335 is->audio_buf_frames_pending = 0;
2342 /* prepare a new audio buffer */
2343 static void sdl_audio_callback(void *opaque, Uint8 *stream, int len)
2345 VideoState *is = opaque;
2346 int audio_size, len1;
2348 int frame_size = av_samples_get_buffer_size(NULL, is->audio_tgt.channels, 1, is->audio_tgt.fmt, 1);
2350 audio_callback_time = av_gettime();
2353 if (is->audio_buf_index >= is->audio_buf_size) {
2354 audio_size = audio_decode_frame(is);
2355 if (audio_size < 0) {
2356 /* if error, just output silence */
2357 is->audio_buf = is->silence_buf;
2358 is->audio_buf_size = sizeof(is->silence_buf) / frame_size * frame_size;
2360 if (is->show_mode != SHOW_MODE_VIDEO)
2361 update_sample_display(is, (int16_t *)is->audio_buf, audio_size);
2362 is->audio_buf_size = audio_size;
2364 is->audio_buf_index = 0;
2366 len1 = is->audio_buf_size - is->audio_buf_index;
2369 memcpy(stream, (uint8_t *)is->audio_buf + is->audio_buf_index, len1);
2372 is->audio_buf_index += len1;
2374 bytes_per_sec = is->audio_tgt.freq * is->audio_tgt.channels * av_get_bytes_per_sample(is->audio_tgt.fmt);
2375 is->audio_write_buf_size = is->audio_buf_size - is->audio_buf_index;
2376 /* Let's assume the audio driver that is used by SDL has two periods. */
2377 if (!isnan(is->audio_clock)) {
2378 set_clock_at(&is->audclk, is->audio_clock - (double)(2 * is->audio_hw_buf_size + is->audio_write_buf_size) / bytes_per_sec, is->audio_clock_serial, audio_callback_time / 1000000.0);
2379 sync_clock_to_slave(&is->extclk, &is->audclk);
2383 static int audio_open(void *opaque, int64_t wanted_channel_layout, int wanted_nb_channels, int wanted_sample_rate, struct AudioParams *audio_hw_params)
2385 SDL_AudioSpec wanted_spec, spec;
2387 const int next_nb_channels[] = {0, 0, 1, 6, 2, 6, 4, 6};
2389 env = SDL_getenv("SDL_AUDIO_CHANNELS");
2391 wanted_nb_channels = atoi(env);
2392 wanted_channel_layout = av_get_default_channel_layout(wanted_nb_channels);
2394 if (!wanted_channel_layout || wanted_nb_channels != av_get_channel_layout_nb_channels(wanted_channel_layout)) {
2395 wanted_channel_layout = av_get_default_channel_layout(wanted_nb_channels);
2396 wanted_channel_layout &= ~AV_CH_LAYOUT_STEREO_DOWNMIX;
2398 wanted_spec.channels = av_get_channel_layout_nb_channels(wanted_channel_layout);
2399 wanted_spec.freq = wanted_sample_rate;
2400 if (wanted_spec.freq <= 0 || wanted_spec.channels <= 0) {
2401 av_log(NULL, AV_LOG_ERROR, "Invalid sample rate or channel count!\n");
2404 wanted_spec.format = AUDIO_S16SYS;
2405 wanted_spec.silence = 0;
2406 wanted_spec.samples = SDL_AUDIO_BUFFER_SIZE;
2407 wanted_spec.callback = sdl_audio_callback;
2408 wanted_spec.userdata = opaque;
2409 while (SDL_OpenAudio(&wanted_spec, &spec) < 0) {
2410 av_log(NULL, AV_LOG_WARNING, "SDL_OpenAudio (%d channels): %s\n", wanted_spec.channels, SDL_GetError());
2411 wanted_spec.channels = next_nb_channels[FFMIN(7, wanted_spec.channels)];
2412 if (!wanted_spec.channels) {
2413 av_log(NULL, AV_LOG_ERROR,
2414 "No more channel combinations to try, audio open failed\n");
2417 wanted_channel_layout = av_get_default_channel_layout(wanted_spec.channels);
2419 if (spec.format != AUDIO_S16SYS) {
2420 av_log(NULL, AV_LOG_ERROR,
2421 "SDL advised audio format %d is not supported!\n", spec.format);
2424 if (spec.channels != wanted_spec.channels) {
2425 wanted_channel_layout = av_get_default_channel_layout(spec.channels);
2426 if (!wanted_channel_layout) {
2427 av_log(NULL, AV_LOG_ERROR,
2428 "SDL advised channel count %d is not supported!\n", spec.channels);
2433 audio_hw_params->fmt = AV_SAMPLE_FMT_S16;
2434 audio_hw_params->freq = spec.freq;
2435 audio_hw_params->channel_layout = wanted_channel_layout;
2436 audio_hw_params->channels = spec.channels;
2440 /* open a given stream. Return 0 if OK */
2441 static int stream_component_open(VideoState *is, int stream_index)
2443 AVFormatContext *ic = is->ic;
2444 AVCodecContext *avctx;
2446 const char *forced_codec_name = NULL;
2448 AVDictionaryEntry *t = NULL;
2449 int sample_rate, nb_channels;
2450 int64_t channel_layout;
2453 if (stream_index < 0 || stream_index >= ic->nb_streams)
2455 avctx = ic->streams[stream_index]->codec;
2457 codec = avcodec_find_decoder(avctx->codec_id);
2459 switch(avctx->codec_type){
2460 case AVMEDIA_TYPE_AUDIO : is->last_audio_stream = stream_index; forced_codec_name = audio_codec_name; break;
2461 case AVMEDIA_TYPE_SUBTITLE: is->last_subtitle_stream = stream_index; forced_codec_name = subtitle_codec_name; break;
2462 case AVMEDIA_TYPE_VIDEO : is->last_video_stream = stream_index; forced_codec_name = video_codec_name; break;
2464 if (forced_codec_name)
2465 codec = avcodec_find_decoder_by_name(forced_codec_name);
2467 if (forced_codec_name) av_log(NULL, AV_LOG_WARNING,
2468 "No codec could be found with name '%s'\n", forced_codec_name);
2469 else av_log(NULL, AV_LOG_WARNING,
2470 "No codec could be found with id %d\n", avctx->codec_id);
2474 avctx->codec_id = codec->id;
2475 avctx->workaround_bugs = workaround_bugs;
2476 avctx->lowres = lowres;
2477 if(avctx->lowres > codec->max_lowres){
2478 av_log(avctx, AV_LOG_WARNING, "The maximum value for lowres supported by the decoder is %d\n",
2480 avctx->lowres= codec->max_lowres;
2482 avctx->error_concealment = error_concealment;
2484 if(avctx->lowres) avctx->flags |= CODEC_FLAG_EMU_EDGE;
2485 if (fast) avctx->flags2 |= CODEC_FLAG2_FAST;
2486 if(codec->capabilities & CODEC_CAP_DR1)
2487 avctx->flags |= CODEC_FLAG_EMU_EDGE;
2489 opts = filter_codec_opts(codec_opts, avctx->codec_id, ic, ic->streams[stream_index], codec);
2490 if (!av_dict_get(opts, "threads", NULL, 0))
2491 av_dict_set(&opts, "threads", "auto", 0);
2493 av_dict_set(&opts, "lowres", av_asprintf("%d", avctx->lowres), AV_DICT_DONT_STRDUP_VAL);
2494 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO || avctx->codec_type == AVMEDIA_TYPE_AUDIO)
2495 av_dict_set(&opts, "refcounted_frames", "1", 0);
2496 if (avcodec_open2(avctx, codec, &opts) < 0)
2498 if ((t = av_dict_get(opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
2499 av_log(NULL, AV_LOG_ERROR, "Option %s not found.\n", t->key);
2500 return AVERROR_OPTION_NOT_FOUND;
2503 ic->streams[stream_index]->discard = AVDISCARD_DEFAULT;
2504 switch (avctx->codec_type) {
2505 case AVMEDIA_TYPE_AUDIO:
2510 is->audio_filter_src.freq = avctx->sample_rate;
2511 is->audio_filter_src.channels = avctx->channels;
2512 is->audio_filter_src.channel_layout = get_valid_channel_layout(avctx->channel_layout, avctx->channels);
2513 is->audio_filter_src.fmt = avctx->sample_fmt;
2514 if ((ret = configure_audio_filters(is, afilters, 0)) < 0)
2516 link = is->out_audio_filter->inputs[0];
2517 sample_rate = link->sample_rate;
2518 nb_channels = link->channels;
2519 channel_layout = link->channel_layout;
2522 sample_rate = avctx->sample_rate;
2523 nb_channels = avctx->channels;
2524 channel_layout = avctx->channel_layout;
2527 /* prepare audio output */
2528 if ((ret = audio_open(is, channel_layout, nb_channels, sample_rate, &is->audio_tgt)) < 0)
2530 is->audio_hw_buf_size = ret;
2531 is->audio_src = is->audio_tgt;
2532 is->audio_buf_size = 0;
2533 is->audio_buf_index = 0;
2535 /* init averaging filter */
2536 is->audio_diff_avg_coef = exp(log(0.01) / AUDIO_DIFF_AVG_NB);
2537 is->audio_diff_avg_count = 0;
2538 /* since we do not have a precise anough audio fifo fullness,
2539 we correct audio sync only if larger than this threshold */
2540 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);
2542 memset(&is->audio_pkt, 0, sizeof(is->audio_pkt));
2543 memset(&is->audio_pkt_temp, 0, sizeof(is->audio_pkt_temp));
2545 is->audio_stream = stream_index;
2546 is->audio_st = ic->streams[stream_index];
2548 packet_queue_start(&is->audioq);
2551 case AVMEDIA_TYPE_VIDEO:
2552 is->video_stream = stream_index;
2553 is->video_st = ic->streams[stream_index];
2555 packet_queue_start(&is->videoq);
2556 is->video_tid = SDL_CreateThread(video_thread, is);
2557 is->queue_attachments_req = 1;
2559 case AVMEDIA_TYPE_SUBTITLE:
2560 is->subtitle_stream = stream_index;
2561 is->subtitle_st = ic->streams[stream_index];
2562 packet_queue_start(&is->subtitleq);
2564 is->subtitle_tid = SDL_CreateThread(subtitle_thread, is);
2572 static void stream_component_close(VideoState *is, int stream_index)
2574 AVFormatContext *ic = is->ic;
2575 AVCodecContext *avctx;
2577 if (stream_index < 0 || stream_index >= ic->nb_streams)
2579 avctx = ic->streams[stream_index]->codec;
2581 switch (avctx->codec_type) {
2582 case AVMEDIA_TYPE_AUDIO:
2583 packet_queue_abort(&is->audioq);
2587 packet_queue_flush(&is->audioq);
2588 av_free_packet(&is->audio_pkt);
2589 swr_free(&is->swr_ctx);
2590 av_freep(&is->audio_buf1);
2591 is->audio_buf1_size = 0;
2592 is->audio_buf = NULL;
2593 av_frame_free(&is->frame);
2596 av_rdft_end(is->rdft);
2597 av_freep(&is->rdft_data);
2602 avfilter_graph_free(&is->agraph);
2605 case AVMEDIA_TYPE_VIDEO:
2606 packet_queue_abort(&is->videoq);
2608 /* note: we also signal this mutex to make sure we deblock the
2609 video thread in all cases */
2610 SDL_LockMutex(is->pictq_mutex);
2611 SDL_CondSignal(is->pictq_cond);
2612 SDL_UnlockMutex(is->pictq_mutex);
2614 SDL_WaitThread(is->video_tid, NULL);
2616 packet_queue_flush(&is->videoq);
2618 case AVMEDIA_TYPE_SUBTITLE:
2619 packet_queue_abort(&is->subtitleq);
2621 /* note: we also signal this mutex to make sure we deblock the
2622 video thread in all cases */
2623 SDL_LockMutex(is->subpq_mutex);
2624 SDL_CondSignal(is->subpq_cond);
2625 SDL_UnlockMutex(is->subpq_mutex);
2627 SDL_WaitThread(is->subtitle_tid, NULL);
2629 packet_queue_flush(&is->subtitleq);
2635 ic->streams[stream_index]->discard = AVDISCARD_ALL;
2636 avcodec_close(avctx);
2637 switch (avctx->codec_type) {
2638 case AVMEDIA_TYPE_AUDIO:
2639 is->audio_st = NULL;
2640 is->audio_stream = -1;
2642 case AVMEDIA_TYPE_VIDEO:
2643 is->video_st = NULL;
2644 is->video_stream = -1;
2646 case AVMEDIA_TYPE_SUBTITLE:
2647 is->subtitle_st = NULL;
2648 is->subtitle_stream = -1;
2655 static int decode_interrupt_cb(void *ctx)
2657 VideoState *is = ctx;
2658 return is->abort_request;
2661 static int is_realtime(AVFormatContext *s)
2663 if( !strcmp(s->iformat->name, "rtp")
2664 || !strcmp(s->iformat->name, "rtsp")
2665 || !strcmp(s->iformat->name, "sdp")
2669 if(s->pb && ( !strncmp(s->filename, "rtp:", 4)
2670 || !strncmp(s->filename, "udp:", 4)
2677 /* this thread gets the stream from the disk or the network */
2678 static int read_thread(void *arg)
2680 VideoState *is = arg;
2681 AVFormatContext *ic = NULL;
2683 int st_index[AVMEDIA_TYPE_NB];
2684 AVPacket pkt1, *pkt = &pkt1;
2686 int64_t stream_start_time;
2687 int pkt_in_play_range = 0;
2688 AVDictionaryEntry *t;
2689 AVDictionary **opts;
2690 int orig_nb_streams;
2691 SDL_mutex *wait_mutex = SDL_CreateMutex();
2693 memset(st_index, -1, sizeof(st_index));
2694 is->last_video_stream = is->video_stream = -1;
2695 is->last_audio_stream = is->audio_stream = -1;
2696 is->last_subtitle_stream = is->subtitle_stream = -1;
2698 ic = avformat_alloc_context();
2699 ic->interrupt_callback.callback = decode_interrupt_cb;
2700 ic->interrupt_callback.opaque = is;
2701 err = avformat_open_input(&ic, is->filename, is->iformat, &format_opts);
2703 print_error(is->filename, err);
2707 if ((t = av_dict_get(format_opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
2708 av_log(NULL, AV_LOG_ERROR, "Option %s not found.\n", t->key);
2709 ret = AVERROR_OPTION_NOT_FOUND;
2715 ic->flags |= AVFMT_FLAG_GENPTS;
2717 opts = setup_find_stream_info_opts(ic, codec_opts);
2718 orig_nb_streams = ic->nb_streams;
2720 err = avformat_find_stream_info(ic, opts);
2722 av_log(NULL, AV_LOG_WARNING,
2723 "%s: could not find codec parameters\n", is->filename);
2727 for (i = 0; i < orig_nb_streams; i++)
2728 av_dict_free(&opts[i]);
2732 ic->pb->eof_reached = 0; // FIXME hack, ffplay maybe should not use url_feof() to test for the end
2734 if (seek_by_bytes < 0)
2735 seek_by_bytes = !!(ic->iformat->flags & AVFMT_TS_DISCONT) && strcmp("ogg", ic->iformat->name);
2737 is->max_frame_duration = (ic->iformat->flags & AVFMT_TS_DISCONT) ? 10.0 : 3600.0;
2739 if (!window_title && (t = av_dict_get(ic->metadata, "title", NULL, 0)))
2740 window_title = av_asprintf("%s - %s", t->value, input_filename);
2742 /* if seeking requested, we execute it */
2743 if (start_time != AV_NOPTS_VALUE) {
2746 timestamp = start_time;
2747 /* add the stream start time */
2748 if (ic->start_time != AV_NOPTS_VALUE)
2749 timestamp += ic->start_time;
2750 ret = avformat_seek_file(ic, -1, INT64_MIN, timestamp, INT64_MAX, 0);
2752 av_log(NULL, AV_LOG_WARNING, "%s: could not seek to position %0.3f\n",
2753 is->filename, (double)timestamp / AV_TIME_BASE);
2757 is->realtime = is_realtime(ic);
2759 for (i = 0; i < ic->nb_streams; i++)
2760 ic->streams[i]->discard = AVDISCARD_ALL;
2762 st_index[AVMEDIA_TYPE_VIDEO] =
2763 av_find_best_stream(ic, AVMEDIA_TYPE_VIDEO,
2764 wanted_stream[AVMEDIA_TYPE_VIDEO], -1, NULL, 0);
2766 st_index[AVMEDIA_TYPE_AUDIO] =
2767 av_find_best_stream(ic, AVMEDIA_TYPE_AUDIO,
2768 wanted_stream[AVMEDIA_TYPE_AUDIO],
2769 st_index[AVMEDIA_TYPE_VIDEO],
2771 if (!video_disable && !subtitle_disable)
2772 st_index[AVMEDIA_TYPE_SUBTITLE] =
2773 av_find_best_stream(ic, AVMEDIA_TYPE_SUBTITLE,
2774 wanted_stream[AVMEDIA_TYPE_SUBTITLE],
2775 (st_index[AVMEDIA_TYPE_AUDIO] >= 0 ?
2776 st_index[AVMEDIA_TYPE_AUDIO] :
2777 st_index[AVMEDIA_TYPE_VIDEO]),
2780 av_dump_format(ic, 0, is->filename, 0);
2783 is->show_mode = show_mode;
2785 /* open the streams */
2786 if (st_index[AVMEDIA_TYPE_AUDIO] >= 0) {
2787 stream_component_open(is, st_index[AVMEDIA_TYPE_AUDIO]);
2791 if (st_index[AVMEDIA_TYPE_VIDEO] >= 0) {
2792 ret = stream_component_open(is, st_index[AVMEDIA_TYPE_VIDEO]);
2794 if (is->show_mode == SHOW_MODE_NONE)
2795 is->show_mode = ret >= 0 ? SHOW_MODE_VIDEO : SHOW_MODE_RDFT;
2797 if (st_index[AVMEDIA_TYPE_SUBTITLE] >= 0) {
2798 stream_component_open(is, st_index[AVMEDIA_TYPE_SUBTITLE]);
2801 if (is->video_stream < 0 && is->audio_stream < 0) {
2802 av_log(NULL, AV_LOG_FATAL, "%s: could not open codecs\n", is->filename);
2807 if (infinite_buffer < 0 && is->realtime)
2808 infinite_buffer = 1;
2811 if (is->abort_request)
2813 if (is->paused != is->last_paused) {
2814 is->last_paused = is->paused;
2816 is->read_pause_return = av_read_pause(ic);
2820 #if CONFIG_RTSP_DEMUXER || CONFIG_MMSH_PROTOCOL
2822 (!strcmp(ic->iformat->name, "rtsp") ||
2823 (ic->pb && !strncmp(input_filename, "mmsh:", 5)))) {
2824 /* wait 10 ms to avoid trying to get another packet */
2831 int64_t seek_target = is->seek_pos;
2832 int64_t seek_min = is->seek_rel > 0 ? seek_target - is->seek_rel + 2: INT64_MIN;
2833 int64_t seek_max = is->seek_rel < 0 ? seek_target - is->seek_rel - 2: INT64_MAX;
2834 // FIXME the +-2 is due to rounding being not done in the correct direction in generation
2835 // of the seek_pos/seek_rel variables
2837 ret = avformat_seek_file(is->ic, -1, seek_min, seek_target, seek_max, is->seek_flags);
2839 av_log(NULL, AV_LOG_ERROR,
2840 "%s: error while seeking\n", is->ic->filename);
2842 if (is->audio_stream >= 0) {
2843 packet_queue_flush(&is->audioq);
2844 packet_queue_put(&is->audioq, &flush_pkt);
2846 if (is->subtitle_stream >= 0) {
2847 packet_queue_flush(&is->subtitleq);
2848 packet_queue_put(&is->subtitleq, &flush_pkt);
2850 if (is->video_stream >= 0) {
2851 packet_queue_flush(&is->videoq);
2852 packet_queue_put(&is->videoq, &flush_pkt);
2854 if (is->seek_flags & AVSEEK_FLAG_BYTE) {
2855 set_clock(&is->extclk, NAN, 0);
2857 set_clock(&is->extclk, seek_target / (double)AV_TIME_BASE, 0);
2861 is->queue_attachments_req = 1;
2864 step_to_next_frame(is);
2866 if (is->queue_attachments_req) {
2867 if (is->video_st && is->video_st->disposition & AV_DISPOSITION_ATTACHED_PIC) {
2869 if ((ret = av_copy_packet(©, &is->video_st->attached_pic)) < 0)
2871 packet_queue_put(&is->videoq, ©);
2873 is->queue_attachments_req = 0;
2876 /* if the queue are full, no need to read more */
2877 if (infinite_buffer<1 &&
2878 (is->audioq.size + is->videoq.size + is->subtitleq.size > MAX_QUEUE_SIZE
2879 || ( (is->audioq .nb_packets > MIN_FRAMES || is->audio_stream < 0 || is->audioq.abort_request)
2880 && (is->videoq .nb_packets > MIN_FRAMES || is->video_stream < 0 || is->videoq.abort_request
2881 || (is->video_st->disposition & AV_DISPOSITION_ATTACHED_PIC))
2882 && (is->subtitleq.nb_packets > MIN_FRAMES || is->subtitle_stream < 0 || is->subtitleq.abort_request)))) {
2884 SDL_LockMutex(wait_mutex);
2885 SDL_CondWaitTimeout(is->continue_read_thread, wait_mutex, 10);
2886 SDL_UnlockMutex(wait_mutex);
2890 if (is->video_stream >= 0) {
2891 av_init_packet(pkt);
2894 pkt->stream_index = is->video_stream;
2895 packet_queue_put(&is->videoq, pkt);
2897 if (is->audio_stream >= 0) {
2898 av_init_packet(pkt);
2901 pkt->stream_index = is->audio_stream;
2902 packet_queue_put(&is->audioq, pkt);
2905 if (is->audioq.size + is->videoq.size + is->subtitleq.size == 0) {
2906 if (loop != 1 && (!loop || --loop)) {
2907 stream_seek(is, start_time != AV_NOPTS_VALUE ? start_time : 0, 0, 0);
2908 } else if (autoexit) {
2916 ret = av_read_frame(ic, pkt);
2918 if (ret == AVERROR_EOF || url_feof(ic->pb))
2920 if (ic->pb && ic->pb->error)
2922 SDL_LockMutex(wait_mutex);
2923 SDL_CondWaitTimeout(is->continue_read_thread, wait_mutex, 10);
2924 SDL_UnlockMutex(wait_mutex);
2927 /* check if packet is in play range specified by user, then queue, otherwise discard */
2928 stream_start_time = ic->streams[pkt->stream_index]->start_time;
2929 pkt_in_play_range = duration == AV_NOPTS_VALUE ||
2930 (pkt->pts - (stream_start_time != AV_NOPTS_VALUE ? stream_start_time : 0)) *
2931 av_q2d(ic->streams[pkt->stream_index]->time_base) -
2932 (double)(start_time != AV_NOPTS_VALUE ? start_time : 0) / 1000000
2933 <= ((double)duration / 1000000);
2934 if (pkt->stream_index == is->audio_stream && pkt_in_play_range) {
2935 packet_queue_put(&is->audioq, pkt);
2936 } else if (pkt->stream_index == is->video_stream && pkt_in_play_range
2937 && !(is->video_st->disposition & AV_DISPOSITION_ATTACHED_PIC)) {
2938 packet_queue_put(&is->videoq, pkt);
2939 } else if (pkt->stream_index == is->subtitle_stream && pkt_in_play_range) {
2940 packet_queue_put(&is->subtitleq, pkt);
2942 av_free_packet(pkt);
2945 /* wait until the end */
2946 while (!is->abort_request) {
2952 /* close each stream */
2953 if (is->audio_stream >= 0)
2954 stream_component_close(is, is->audio_stream);
2955 if (is->video_stream >= 0)
2956 stream_component_close(is, is->video_stream);
2957 if (is->subtitle_stream >= 0)
2958 stream_component_close(is, is->subtitle_stream);
2960 avformat_close_input(&is->ic);
2966 event.type = FF_QUIT_EVENT;
2967 event.user.data1 = is;
2968 SDL_PushEvent(&event);
2970 SDL_DestroyMutex(wait_mutex);
2974 static VideoState *stream_open(const char *filename, AVInputFormat *iformat)
2978 is = av_mallocz(sizeof(VideoState));
2981 av_strlcpy(is->filename, filename, sizeof(is->filename));
2982 is->iformat = iformat;
2986 /* start video display */
2987 is->pictq_mutex = SDL_CreateMutex();
2988 is->pictq_cond = SDL_CreateCond();
2990 is->subpq_mutex = SDL_CreateMutex();
2991 is->subpq_cond = SDL_CreateCond();
2993 packet_queue_init(&is->videoq);
2994 packet_queue_init(&is->audioq);
2995 packet_queue_init(&is->subtitleq);
2997 is->continue_read_thread = SDL_CreateCond();
2999 init_clock(&is->vidclk, &is->videoq.serial);
3000 init_clock(&is->audclk, &is->audioq.serial);
3001 init_clock(&is->extclk, &is->extclk.serial);
3002 is->audio_clock_serial = -1;
3003 is->audio_last_serial = -1;
3004 is->av_sync_type = av_sync_type;
3005 is->read_tid = SDL_CreateThread(read_thread, is);
3006 if (!is->read_tid) {
3013 static void stream_cycle_channel(VideoState *is, int codec_type)
3015 AVFormatContext *ic = is->ic;
3016 int start_index, stream_index;
3020 if (codec_type == AVMEDIA_TYPE_VIDEO) {
3021 start_index = is->last_video_stream;
3022 old_index = is->video_stream;
3023 } else if (codec_type == AVMEDIA_TYPE_AUDIO) {
3024 start_index = is->last_audio_stream;
3025 old_index = is->audio_stream;
3027 start_index = is->last_subtitle_stream;
3028 old_index = is->subtitle_stream;
3030 stream_index = start_index;
3032 if (++stream_index >= is->ic->nb_streams)
3034 if (codec_type == AVMEDIA_TYPE_SUBTITLE)
3037 is->last_subtitle_stream = -1;
3040 if (start_index == -1)
3044 if (stream_index == start_index)
3046 st = ic->streams[stream_index];
3047 if (st->codec->codec_type == codec_type) {
3048 /* check that parameters are OK */
3049 switch (codec_type) {
3050 case AVMEDIA_TYPE_AUDIO:
3051 if (st->codec->sample_rate != 0 &&
3052 st->codec->channels != 0)
3055 case AVMEDIA_TYPE_VIDEO:
3056 case AVMEDIA_TYPE_SUBTITLE:
3064 stream_component_close(is, old_index);
3065 stream_component_open(is, stream_index);
3069 static void toggle_full_screen(VideoState *is)
3071 #if defined(__APPLE__) && SDL_VERSION_ATLEAST(1, 2, 14)
3072 /* OS X needs to reallocate the SDL overlays */
3074 for (i = 0; i < VIDEO_PICTURE_QUEUE_SIZE; i++)
3075 is->pictq[i].reallocate = 1;
3077 is_full_screen = !is_full_screen;
3078 video_open(is, 1, NULL);
3081 static void toggle_audio_display(VideoState *is)
3083 int bgcolor = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00);
3084 int next = is->show_mode;
3086 next = (next + 1) % SHOW_MODE_NB;
3087 } while (next != is->show_mode && (next == SHOW_MODE_VIDEO && !is->video_st || next != SHOW_MODE_VIDEO && !is->audio_st));
3088 if (is->show_mode != next) {
3089 fill_rectangle(screen,
3090 is->xleft, is->ytop, is->width, is->height,
3092 is->force_refresh = 1;
3093 is->show_mode = next;
3097 static void refresh_loop_wait_event(VideoState *is, SDL_Event *event) {
3098 double remaining_time = 0.0;
3100 while (!SDL_PeepEvents(event, 1, SDL_GETEVENT, SDL_ALLEVENTS)) {
3101 if (!cursor_hidden && av_gettime() - cursor_last_shown > CURSOR_HIDE_DELAY) {
3105 if (remaining_time > 0.0)
3106 av_usleep((int64_t)(remaining_time * 1000000.0));
3107 remaining_time = REFRESH_RATE;
3108 if (is->show_mode != SHOW_MODE_NONE && (!is->paused || is->force_refresh))
3109 video_refresh(is, &remaining_time);
3114 /* handle an event sent by the GUI */
3115 static void event_loop(VideoState *cur_stream)
3118 double incr, pos, frac;
3122 refresh_loop_wait_event(cur_stream, &event);
3123 switch (event.type) {
3125 if (exit_on_keydown) {
3126 do_exit(cur_stream);
3129 switch (event.key.keysym.sym) {
3132 do_exit(cur_stream);
3135 toggle_full_screen(cur_stream);
3136 cur_stream->force_refresh = 1;
3140 toggle_pause(cur_stream);
3142 case SDLK_s: // S: Step to next frame
3143 step_to_next_frame(cur_stream);
3146 stream_cycle_channel(cur_stream, AVMEDIA_TYPE_AUDIO);
3149 stream_cycle_channel(cur_stream, AVMEDIA_TYPE_VIDEO);
3152 stream_cycle_channel(cur_stream, AVMEDIA_TYPE_SUBTITLE);
3155 toggle_audio_display(cur_stream);
3175 if (seek_by_bytes) {
3176 if (cur_stream->video_stream >= 0 && cur_stream->video_current_pos >= 0) {
3177 pos = cur_stream->video_current_pos;
3178 } else if (cur_stream->audio_stream >= 0 && cur_stream->audio_pkt.pos >= 0) {
3179 pos = cur_stream->audio_pkt.pos;
3181 pos = avio_tell(cur_stream->ic->pb);
3182 if (cur_stream->ic->bit_rate)
3183 incr *= cur_stream->ic->bit_rate / 8.0;
3187 stream_seek(cur_stream, pos, incr, 1);
3189 pos = get_master_clock(cur_stream);
3191 pos = (double)cur_stream->seek_pos / AV_TIME_BASE;
3193 if (cur_stream->ic->start_time != AV_NOPTS_VALUE && pos < cur_stream->ic->start_time / (double)AV_TIME_BASE)
3194 pos = cur_stream->ic->start_time / (double)AV_TIME_BASE;
3195 stream_seek(cur_stream, (int64_t)(pos * AV_TIME_BASE), (int64_t)(incr * AV_TIME_BASE), 0);
3202 case SDL_VIDEOEXPOSE:
3203 cur_stream->force_refresh = 1;
3205 case SDL_MOUSEBUTTONDOWN:
3206 if (exit_on_mousedown) {
3207 do_exit(cur_stream);
3210 case SDL_MOUSEMOTION:
3211 if (cursor_hidden) {
3215 cursor_last_shown = av_gettime();
3216 if (event.type == SDL_MOUSEBUTTONDOWN) {
3219 if (event.motion.state != SDL_PRESSED)
3223 if (seek_by_bytes || cur_stream->ic->duration <= 0) {
3224 uint64_t size = avio_size(cur_stream->ic->pb);
3225 stream_seek(cur_stream, size*x/cur_stream->width, 0, 1);
3229 int tns, thh, tmm, tss;
3230 tns = cur_stream->ic->duration / 1000000LL;
3232 tmm = (tns % 3600) / 60;
3234 frac = x / cur_stream->width;
3237 mm = (ns % 3600) / 60;
3239 av_log(NULL, AV_LOG_INFO,
3240 "Seek to %2.0f%% (%2d:%02d:%02d) of total duration (%2d:%02d:%02d) \n", frac*100,
3241 hh, mm, ss, thh, tmm, tss);
3242 ts = frac * cur_stream->ic->duration;
3243 if (cur_stream->ic->start_time != AV_NOPTS_VALUE)
3244 ts += cur_stream->ic->start_time;
3245 stream_seek(cur_stream, ts, 0, 0);
3248 case SDL_VIDEORESIZE:
3249 screen = SDL_SetVideoMode(FFMIN(16383, event.resize.w), event.resize.h, 0,
3250 SDL_HWSURFACE|SDL_RESIZABLE|SDL_ASYNCBLIT|SDL_HWACCEL);
3252 av_log(NULL, AV_LOG_FATAL, "Failed to set video mode\n");
3253 do_exit(cur_stream);
3255 screen_width = cur_stream->width = screen->w;
3256 screen_height = cur_stream->height = screen->h;
3257 cur_stream->force_refresh = 1;
3261 do_exit(cur_stream);
3263 case FF_ALLOC_EVENT:
3264 alloc_picture(event.user.data1);
3272 static int opt_frame_size(void *optctx, const char *opt, const char *arg)
3274 av_log(NULL, AV_LOG_WARNING, "Option -s is deprecated, use -video_size.\n");
3275 return opt_default(NULL, "video_size", arg);
3278 static int opt_width(void *optctx, const char *opt, const char *arg)
3280 screen_width = parse_number_or_die(opt, arg, OPT_INT64, 1, INT_MAX);
3284 static int opt_height(void *optctx, const char *opt, const char *arg)
3286 screen_height = parse_number_or_die(opt, arg, OPT_INT64, 1, INT_MAX);
3290 static int opt_format(void *optctx, const char *opt, const char *arg)
3292 file_iformat = av_find_input_format(arg);
3293 if (!file_iformat) {
3294 av_log(NULL, AV_LOG_FATAL, "Unknown input format: %s\n", arg);
3295 return AVERROR(EINVAL);
3300 static int opt_frame_pix_fmt(void *optctx, const char *opt, const char *arg)
3302 av_log(NULL, AV_LOG_WARNING, "Option -pix_fmt is deprecated, use -pixel_format.\n");
3303 return opt_default(NULL, "pixel_format", arg);
3306 static int opt_sync(void *optctx, const char *opt, const char *arg)
3308 if (!strcmp(arg, "audio"))
3309 av_sync_type = AV_SYNC_AUDIO_MASTER;
3310 else if (!strcmp(arg, "video"))
3311 av_sync_type = AV_SYNC_VIDEO_MASTER;
3312 else if (!strcmp(arg, "ext"))
3313 av_sync_type = AV_SYNC_EXTERNAL_CLOCK;
3315 av_log(NULL, AV_LOG_ERROR, "Unknown value for %s: %s\n", opt, arg);
3321 static int opt_seek(void *optctx, const char *opt, const char *arg)
3323 start_time = parse_time_or_die(opt, arg, 1);
3327 static int opt_duration(void *optctx, const char *opt, const char *arg)
3329 duration = parse_time_or_die(opt, arg, 1);
3333 static int opt_show_mode(void *optctx, const char *opt, const char *arg)
3335 show_mode = !strcmp(arg, "video") ? SHOW_MODE_VIDEO :
3336 !strcmp(arg, "waves") ? SHOW_MODE_WAVES :
3337 !strcmp(arg, "rdft" ) ? SHOW_MODE_RDFT :
3338 parse_number_or_die(opt, arg, OPT_INT, 0, SHOW_MODE_NB-1);
3342 static void opt_input_file(void *optctx, const char *filename)
3344 if (input_filename) {
3345 av_log(NULL, AV_LOG_FATAL,
3346 "Argument '%s' provided as input filename, but '%s' was already specified.\n",
3347 filename, input_filename);
3350 if (!strcmp(filename, "-"))
3352 input_filename = filename;
3355 static int opt_codec(void *optctx, const char *opt, const char *arg)
3357 const char *spec = strchr(opt, ':');
3359 av_log(NULL, AV_LOG_ERROR,
3360 "No media specifier was specified in '%s' in option '%s'\n",
3362 return AVERROR(EINVAL);
3366 case 'a' : audio_codec_name = arg; break;
3367 case 's' : subtitle_codec_name = arg; break;
3368 case 'v' : video_codec_name = arg; break;
3370 av_log(NULL, AV_LOG_ERROR,
3371 "Invalid media specifier '%s' in option '%s'\n", spec, opt);
3372 return AVERROR(EINVAL);
3379 static const OptionDef options[] = {
3380 #include "cmdutils_common_opts.h"
3381 { "x", HAS_ARG, { .func_arg = opt_width }, "force displayed width", "width" },
3382 { "y", HAS_ARG, { .func_arg = opt_height }, "force displayed height", "height" },
3383 { "s", HAS_ARG | OPT_VIDEO, { .func_arg = opt_frame_size }, "set frame size (WxH or abbreviation)", "size" },
3384 { "fs", OPT_BOOL, { &is_full_screen }, "force full screen" },
3385 { "an", OPT_BOOL, { &audio_disable }, "disable audio" },
3386 { "vn", OPT_BOOL, { &video_disable }, "disable video" },
3387 { "sn", OPT_BOOL, { &subtitle_disable }, "disable subtitling" },
3388 { "ast", OPT_INT | HAS_ARG | OPT_EXPERT, { &wanted_stream[AVMEDIA_TYPE_AUDIO] }, "select desired audio stream", "stream_number" },
3389 { "vst", OPT_INT | HAS_ARG | OPT_EXPERT, { &wanted_stream[AVMEDIA_TYPE_VIDEO] }, "select desired video stream", "stream_number" },
3390 { "sst", OPT_INT | HAS_ARG | OPT_EXPERT, { &wanted_stream[AVMEDIA_TYPE_SUBTITLE] }, "select desired subtitle stream", "stream_number" },
3391 { "ss", HAS_ARG, { .func_arg = opt_seek }, "seek to a given position in seconds", "pos" },
3392 { "t", HAS_ARG, { .func_arg = opt_duration }, "play \"duration\" seconds of audio/video", "duration" },
3393 { "bytes", OPT_INT | HAS_ARG, { &seek_by_bytes }, "seek by bytes 0=off 1=on -1=auto", "val" },
3394 { "nodisp", OPT_BOOL, { &display_disable }, "disable graphical display" },
3395 { "f", HAS_ARG, { .func_arg = opt_format }, "force format", "fmt" },
3396 { "pix_fmt", HAS_ARG | OPT_EXPERT | OPT_VIDEO, { .func_arg = opt_frame_pix_fmt }, "set pixel format", "format" },
3397 { "stats", OPT_BOOL | OPT_EXPERT, { &show_status }, "show status", "" },
3398 { "bug", OPT_INT | HAS_ARG | OPT_EXPERT, { &workaround_bugs }, "workaround bugs", "" },
3399 { "fast", OPT_BOOL | OPT_EXPERT, { &fast }, "non spec compliant optimizations", "" },
3400 { "genpts", OPT_BOOL | OPT_EXPERT, { &genpts }, "generate pts", "" },
3401 { "drp", OPT_INT | HAS_ARG | OPT_EXPERT, { &decoder_reorder_pts }, "let decoder reorder pts 0=off 1=on -1=auto", ""},
3402 { "lowres", OPT_INT | HAS_ARG | OPT_EXPERT, { &lowres }, "", "" },
3403 { "ec", OPT_INT | HAS_ARG | OPT_EXPERT, { &error_concealment }, "set error concealment options", "bit_mask" },
3404 { "sync", HAS_ARG | OPT_EXPERT, { .func_arg = opt_sync }, "set audio-video sync. type (type=audio/video/ext)", "type" },
3405 { "autoexit", OPT_BOOL | OPT_EXPERT, { &autoexit }, "exit at the end", "" },
3406 { "exitonkeydown", OPT_BOOL | OPT_EXPERT, { &exit_on_keydown }, "exit on key down", "" },
3407 { "exitonmousedown", OPT_BOOL | OPT_EXPERT, { &exit_on_mousedown }, "exit on mouse down", "" },
3408 { "loop", OPT_INT | HAS_ARG | OPT_EXPERT, { &loop }, "set number of times the playback shall be looped", "loop count" },
3409 { "framedrop", OPT_BOOL | OPT_EXPERT, { &framedrop }, "drop frames when cpu is too slow", "" },
3410 { "infbuf", OPT_BOOL | OPT_EXPERT, { &infinite_buffer }, "don't limit the input buffer size (useful with realtime streams)", "" },
3411 { "window_title", OPT_STRING | HAS_ARG, { &window_title }, "set window title", "window title" },
3413 { "vf", OPT_STRING | HAS_ARG, { &vfilters }, "set video filters", "filter_graph" },
3414 { "af", OPT_STRING | HAS_ARG, { &afilters }, "set audio filters", "filter_graph" },
3416 { "rdftspeed", OPT_INT | HAS_ARG| OPT_AUDIO | OPT_EXPERT, { &rdftspeed }, "rdft speed", "msecs" },
3417 { "showmode", HAS_ARG, { .func_arg = opt_show_mode}, "select show mode (0 = video, 1 = waves, 2 = RDFT)", "mode" },
3418 { "default", HAS_ARG | OPT_AUDIO | OPT_VIDEO | OPT_EXPERT, { .func_arg = opt_default }, "generic catch all option", "" },
3419 { "i", OPT_BOOL, { &dummy}, "read specified file", "input_file"},
3420 { "codec", HAS_ARG, { .func_arg = opt_codec}, "force decoder", "decoder_name" },
3421 { "acodec", HAS_ARG | OPT_STRING | OPT_EXPERT, { &audio_codec_name }, "force audio decoder", "decoder_name" },
3422 { "scodec", HAS_ARG | OPT_STRING | OPT_EXPERT, { &subtitle_codec_name }, "force subtitle decoder", "decoder_name" },
3423 { "vcodec", HAS_ARG | OPT_STRING | OPT_EXPERT, { &video_codec_name }, "force video decoder", "decoder_name" },
3427 static void show_usage(void)
3429 av_log(NULL, AV_LOG_INFO, "Simple media player\n");
3430 av_log(NULL, AV_LOG_INFO, "usage: %s [options] input_file\n", program_name);
3431 av_log(NULL, AV_LOG_INFO, "\n");
3434 void show_help_default(const char *opt, const char *arg)
3436 av_log_set_callback(log_callback_help);
3438 show_help_options(options, "Main options:", 0, OPT_EXPERT, 0);
3439 show_help_options(options, "Advanced options:", OPT_EXPERT, 0, 0);
3441 show_help_children(avcodec_get_class(), AV_OPT_FLAG_DECODING_PARAM);
3442 show_help_children(avformat_get_class(), AV_OPT_FLAG_DECODING_PARAM);
3443 #if !CONFIG_AVFILTER
3444 show_help_children(sws_get_class(), AV_OPT_FLAG_ENCODING_PARAM);
3446 show_help_children(avfilter_get_class(), AV_OPT_FLAG_FILTERING_PARAM);
3448 printf("\nWhile playing:\n"
3450 "f toggle full screen\n"
3452 "a cycle audio channel\n"
3453 "v cycle video channel\n"
3454 "t cycle subtitle channel\n"
3455 "w show audio waves\n"
3456 "s activate frame-step mode\n"
3457 "left/right seek backward/forward 10 seconds\n"
3458 "down/up seek backward/forward 1 minute\n"
3459 "page down/page up seek backward/forward 10 minutes\n"
3460 "mouse click seek to percentage in file corresponding to fraction of width\n"
3464 static int lockmgr(void **mtx, enum AVLockOp op)
3467 case AV_LOCK_CREATE:
3468 *mtx = SDL_CreateMutex();
3472 case AV_LOCK_OBTAIN:
3473 return !!SDL_LockMutex(*mtx);
3474 case AV_LOCK_RELEASE:
3475 return !!SDL_UnlockMutex(*mtx);
3476 case AV_LOCK_DESTROY:
3477 SDL_DestroyMutex(*mtx);
3483 /* Called from the main */
3484 int main(int argc, char **argv)
3488 char dummy_videodriver[] = "SDL_VIDEODRIVER=dummy";
3490 av_log_set_flags(AV_LOG_SKIP_REPEATED);
3491 parse_loglevel(argc, argv, options);
3493 /* register all codecs, demux and protocols */
3494 avcodec_register_all();
3496 avdevice_register_all();
3499 avfilter_register_all();
3502 avformat_network_init();
3506 signal(SIGINT , sigterm_handler); /* Interrupt (ANSI). */
3507 signal(SIGTERM, sigterm_handler); /* Termination (ANSI). */
3509 show_banner(argc, argv, options);
3511 parse_options(NULL, argc, argv, options, opt_input_file);
3513 if (!input_filename) {
3515 av_log(NULL, AV_LOG_FATAL, "An input file must be specified\n");
3516 av_log(NULL, AV_LOG_FATAL,
3517 "Use -h to get full help or, even better, run 'man %s'\n", program_name);
3521 if (display_disable) {
3524 flags = SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER;
3526 flags &= ~SDL_INIT_AUDIO;
3527 if (display_disable)
3528 SDL_putenv(dummy_videodriver); /* For the event queue, we always need a video driver. */
3529 #if !defined(__MINGW32__) && !defined(__APPLE__)
3530 flags |= SDL_INIT_EVENTTHREAD; /* Not supported on Windows or Mac OS X */
3532 if (SDL_Init (flags)) {
3533 av_log(NULL, AV_LOG_FATAL, "Could not initialize SDL - %s\n", SDL_GetError());
3534 av_log(NULL, AV_LOG_FATAL, "(Did you set the DISPLAY variable?)\n");
3538 if (!display_disable) {
3539 const SDL_VideoInfo *vi = SDL_GetVideoInfo();
3540 fs_screen_width = vi->current_w;
3541 fs_screen_height = vi->current_h;
3544 SDL_EventState(SDL_ACTIVEEVENT, SDL_IGNORE);
3545 SDL_EventState(SDL_SYSWMEVENT, SDL_IGNORE);
3546 SDL_EventState(SDL_USEREVENT, SDL_IGNORE);
3548 if (av_lockmgr_register(lockmgr)) {
3549 av_log(NULL, AV_LOG_FATAL, "Could not initialize lock manager!\n");
3553 av_init_packet(&flush_pkt);
3554 flush_pkt.data = (uint8_t *)&flush_pkt;
3556 is = stream_open(input_filename, file_iformat);
3558 av_log(NULL, AV_LOG_FATAL, "Failed to initialize VideoState!\n");