2 * Copyright (c) 2003 Fabrice Bellard
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 * simple media player based on the FFmpeg libraries
31 #include "libavutil/avstring.h"
32 #include "libavutil/colorspace.h"
33 #include "libavutil/mathematics.h"
34 #include "libavutil/pixdesc.h"
35 #include "libavutil/imgutils.h"
36 #include "libavutil/dict.h"
37 #include "libavutil/parseutils.h"
38 #include "libavutil/samplefmt.h"
39 #include "libavutil/avassert.h"
40 #include "libavutil/time.h"
41 #include "libavformat/avformat.h"
42 #include "libavdevice/avdevice.h"
43 #include "libswscale/swscale.h"
44 #include "libavutil/opt.h"
45 #include "libavcodec/avfft.h"
46 #include "libswresample/swresample.h"
49 # include "libavfilter/avcodec.h"
50 # include "libavfilter/avfilter.h"
51 # include "libavfilter/avfiltergraph.h"
52 # include "libavfilter/buffersink.h"
53 # include "libavfilter/buffersrc.h"
57 #include <SDL_thread.h>
63 const char program_name[] = "ffplay";
64 const int program_birth_year = 2003;
66 #define MAX_QUEUE_SIZE (15 * 1024 * 1024)
69 /* SDL audio buffer size, in samples. Should be small to have precise
70 A/V sync as SDL does not have hardware buffer fullness info. */
71 #define SDL_AUDIO_BUFFER_SIZE 1024
73 /* no AV sync correction is done if below the AV sync threshold */
74 #define AV_SYNC_THRESHOLD 0.01
75 /* no AV correction is done if too big error */
76 #define AV_NOSYNC_THRESHOLD 10.0
78 /* maximum audio speed change to get correct sync */
79 #define SAMPLE_CORRECTION_PERCENT_MAX 10
81 /* external clock speed adjustment constants for realtime sources based on buffer fullness */
82 #define EXTERNAL_CLOCK_SPEED_MIN 0.900
83 #define EXTERNAL_CLOCK_SPEED_MAX 1.010
84 #define EXTERNAL_CLOCK_SPEED_STEP 0.001
86 /* we use about AUDIO_DIFF_AVG_NB A-V differences to make the average */
87 #define AUDIO_DIFF_AVG_NB 20
89 /* polls for possible required screen refresh at least this often, should be less than 1/fps */
90 #define REFRESH_RATE 0.01
92 /* NOTE: the size must be big enough to compensate the hardware audio buffersize size */
93 /* TODO: We assume that a decoded and resampled frame fits into this buffer */
94 #define SAMPLE_ARRAY_SIZE (8 * 65536)
96 #define CURSOR_HIDE_DELAY 1000000
98 static int64_t sws_flags = SWS_BICUBIC;
100 typedef struct MyAVPacketList {
102 struct MyAVPacketList *next;
106 typedef struct PacketQueue {
107 MyAVPacketList *first_pkt, *last_pkt;
116 #define VIDEO_PICTURE_QUEUE_SIZE 4
117 #define SUBPICTURE_QUEUE_SIZE 4
119 typedef struct VideoPicture {
120 double pts; // presentation timestamp for this picture
121 int64_t pos; // byte position in file
123 int width, height; /* source height & width */
131 typedef struct SubPicture {
132 double pts; /* presentation time stamp for this picture */
136 typedef struct AudioParams {
139 int64_t channel_layout;
140 enum AVSampleFormat fmt;
144 AV_SYNC_AUDIO_MASTER, /* default choice */
145 AV_SYNC_VIDEO_MASTER,
146 AV_SYNC_EXTERNAL_CLOCK, /* synchronize to an external clock */
149 typedef struct VideoState {
150 SDL_Thread *read_tid;
151 SDL_Thread *video_tid;
152 AVInputFormat *iformat;
158 int queue_attachments_req;
163 int read_pause_return;
170 double external_clock; ///< external clock base
171 double external_clock_drift; ///< external clock base - time (av_gettime) at which we updated external_clock
172 int64_t external_clock_time; ///< last reference time
173 double external_clock_speed; ///< speed of the external clock
176 int audio_clock_serial;
177 double audio_diff_cum; /* used for AV difference average computation */
178 double audio_diff_avg_coef;
179 double audio_diff_threshold;
180 int audio_diff_avg_count;
183 int audio_hw_buf_size;
184 uint8_t silence_buf[SDL_AUDIO_BUFFER_SIZE];
187 unsigned int audio_buf_size; /* in bytes */
188 unsigned int audio_buf1_size;
189 int audio_buf_index; /* in bytes */
190 int audio_write_buf_size;
191 AVPacket audio_pkt_temp;
193 int audio_pkt_temp_serial;
194 int audio_last_serial;
195 struct AudioParams audio_src;
197 struct AudioParams audio_filter_src;
199 struct AudioParams audio_tgt;
200 struct SwrContext *swr_ctx;
201 double audio_current_pts;
202 double audio_current_pts_drift;
203 int frame_drops_early;
204 int frame_drops_late;
208 SHOW_MODE_NONE = -1, SHOW_MODE_VIDEO = 0, SHOW_MODE_WAVES, SHOW_MODE_RDFT, SHOW_MODE_NB
210 int16_t sample_array[SAMPLE_ARRAY_SIZE];
211 int sample_array_index;
215 FFTSample *rdft_data;
217 double last_vis_time;
219 SDL_Thread *subtitle_tid;
221 int subtitle_stream_changed;
222 AVStream *subtitle_st;
223 PacketQueue subtitleq;
224 SubPicture subpq[SUBPICTURE_QUEUE_SIZE];
225 int subpq_size, subpq_rindex, subpq_windex;
226 SDL_mutex *subpq_mutex;
227 SDL_cond *subpq_cond;
230 double frame_last_pts;
231 double frame_last_duration;
232 double frame_last_dropped_pts;
233 double frame_last_returned_time;
234 double frame_last_filter_delay;
235 int64_t frame_last_dropped_pos;
236 int frame_last_dropped_serial;
240 double video_current_pts; // current displayed pts
241 double video_current_pts_drift; // video_current_pts - time (av_gettime) at which we updated video_current_pts - used to have running video pts
242 int64_t video_current_pos; // current displayed file pos
243 double max_frame_duration; // maximum duration of a frame - above this, we consider the jump a timestamp discontinuity
244 int video_clock_serial;
245 VideoPicture pictq[VIDEO_PICTURE_QUEUE_SIZE];
246 int pictq_size, pictq_rindex, pictq_windex;
247 SDL_mutex *pictq_mutex;
248 SDL_cond *pictq_cond;
250 struct SwsContext *img_convert_ctx;
252 SDL_Rect last_display_rect;
255 int width, height, xleft, ytop;
259 AVFilterContext *in_video_filter; // the first filter in the video chain
260 AVFilterContext *out_video_filter; // the last filter in the video chain
261 AVFilterContext *in_audio_filter; // the first filter in the audio chain
262 AVFilterContext *out_audio_filter; // the last filter in the audio chain
263 AVFilterGraph *agraph; // audio filter graph
266 int last_video_stream, last_audio_stream, last_subtitle_stream;
268 SDL_cond *continue_read_thread;
271 /* options specified by the user */
272 static AVInputFormat *file_iformat;
273 static const char *input_filename;
274 static const char *window_title;
275 static int fs_screen_width;
276 static int fs_screen_height;
277 static int default_width = 640;
278 static int default_height = 480;
279 static int screen_width = 0;
280 static int screen_height = 0;
281 static int audio_disable;
282 static int video_disable;
283 static int subtitle_disable;
284 static int wanted_stream[AVMEDIA_TYPE_NB] = {
285 [AVMEDIA_TYPE_AUDIO] = -1,
286 [AVMEDIA_TYPE_VIDEO] = -1,
287 [AVMEDIA_TYPE_SUBTITLE] = -1,
289 static int seek_by_bytes = -1;
290 static int display_disable;
291 static int show_status = 1;
292 static int av_sync_type = AV_SYNC_AUDIO_MASTER;
293 static int64_t start_time = AV_NOPTS_VALUE;
294 static int64_t duration = AV_NOPTS_VALUE;
295 static int workaround_bugs = 1;
297 static int genpts = 0;
298 static int lowres = 0;
299 static int idct = FF_IDCT_AUTO;
300 static int error_concealment = 3;
301 static int decoder_reorder_pts = -1;
303 static int exit_on_keydown;
304 static int exit_on_mousedown;
306 static int framedrop = -1;
307 static int infinite_buffer = -1;
308 static enum ShowMode show_mode = SHOW_MODE_NONE;
309 static const char *audio_codec_name;
310 static const char *subtitle_codec_name;
311 static const char *video_codec_name;
312 double rdftspeed = 0.02;
313 static int64_t cursor_last_shown;
314 static int cursor_hidden = 0;
316 static char *vfilters = NULL;
317 static char *afilters = NULL;
320 /* current context */
321 static int is_full_screen;
322 static int64_t audio_callback_time;
324 static AVPacket flush_pkt;
326 #define FF_ALLOC_EVENT (SDL_USEREVENT)
327 #define FF_QUIT_EVENT (SDL_USEREVENT + 2)
329 static SDL_Surface *screen;
332 int cmp_audio_fmts(enum AVSampleFormat fmt1, int64_t channel_count1,
333 enum AVSampleFormat fmt2, int64_t channel_count2)
335 /* If channel count == 1, planar and non-planar formats are the same */
336 if (channel_count1 == 1 && channel_count2 == 1)
337 return av_get_packed_sample_fmt(fmt1) != av_get_packed_sample_fmt(fmt2);
339 return channel_count1 != channel_count2 || fmt1 != fmt2;
343 int64_t get_valid_channel_layout(int64_t channel_layout, int channels)
345 if (channel_layout && av_get_channel_layout_nb_channels(channel_layout) == channels)
346 return channel_layout;
351 static int packet_queue_put(PacketQueue *q, AVPacket *pkt);
353 static int packet_queue_put_private(PacketQueue *q, AVPacket *pkt)
355 MyAVPacketList *pkt1;
357 if (q->abort_request)
360 pkt1 = av_malloc(sizeof(MyAVPacketList));
365 if (pkt == &flush_pkt)
367 pkt1->serial = q->serial;
372 q->last_pkt->next = pkt1;
375 q->size += pkt1->pkt.size + sizeof(*pkt1);
376 /* XXX: should duplicate packet data in DV case */
377 SDL_CondSignal(q->cond);
381 static int packet_queue_put(PacketQueue *q, AVPacket *pkt)
385 /* duplicate the packet */
386 if (pkt != &flush_pkt && av_dup_packet(pkt) < 0)
389 SDL_LockMutex(q->mutex);
390 ret = packet_queue_put_private(q, pkt);
391 SDL_UnlockMutex(q->mutex);
393 if (pkt != &flush_pkt && ret < 0)
399 /* packet queue handling */
400 static void packet_queue_init(PacketQueue *q)
402 memset(q, 0, sizeof(PacketQueue));
403 q->mutex = SDL_CreateMutex();
404 q->cond = SDL_CreateCond();
405 q->abort_request = 1;
408 static void packet_queue_flush(PacketQueue *q)
410 MyAVPacketList *pkt, *pkt1;
412 SDL_LockMutex(q->mutex);
413 for (pkt = q->first_pkt; pkt != NULL; pkt = pkt1) {
415 av_free_packet(&pkt->pkt);
422 SDL_UnlockMutex(q->mutex);
425 static void packet_queue_destroy(PacketQueue *q)
427 packet_queue_flush(q);
428 SDL_DestroyMutex(q->mutex);
429 SDL_DestroyCond(q->cond);
432 static void packet_queue_abort(PacketQueue *q)
434 SDL_LockMutex(q->mutex);
436 q->abort_request = 1;
438 SDL_CondSignal(q->cond);
440 SDL_UnlockMutex(q->mutex);
443 static void packet_queue_start(PacketQueue *q)
445 SDL_LockMutex(q->mutex);
446 q->abort_request = 0;
447 packet_queue_put_private(q, &flush_pkt);
448 SDL_UnlockMutex(q->mutex);
451 /* return < 0 if aborted, 0 if no packet and > 0 if packet. */
452 static int packet_queue_get(PacketQueue *q, AVPacket *pkt, int block, int *serial)
454 MyAVPacketList *pkt1;
457 SDL_LockMutex(q->mutex);
460 if (q->abort_request) {
467 q->first_pkt = pkt1->next;
471 q->size -= pkt1->pkt.size + sizeof(*pkt1);
474 *serial = pkt1->serial;
482 SDL_CondWait(q->cond, q->mutex);
485 SDL_UnlockMutex(q->mutex);
489 static inline void fill_rectangle(SDL_Surface *screen,
490 int x, int y, int w, int h, int color, int update)
497 SDL_FillRect(screen, &rect, color);
498 if (update && w > 0 && h > 0)
499 SDL_UpdateRect(screen, x, y, w, h);
502 /* draw only the border of a rectangle */
503 static void fill_border(int xleft, int ytop, int width, int height, int x, int y, int w, int h, int color, int update)
507 /* fill the background */
511 w2 = width - (x + w);
517 h2 = height - (y + h);
520 fill_rectangle(screen,
524 fill_rectangle(screen,
525 xleft + width - w2, ytop,
528 fill_rectangle(screen,
532 fill_rectangle(screen,
533 xleft + w1, ytop + height - h2,
538 #define ALPHA_BLEND(a, oldp, newp, s)\
539 ((((oldp << s) * (255 - (a))) + (newp * (a))) / (255 << s))
541 #define RGBA_IN(r, g, b, a, s)\
543 unsigned int v = ((const uint32_t *)(s))[0];\
544 a = (v >> 24) & 0xff;\
545 r = (v >> 16) & 0xff;\
546 g = (v >> 8) & 0xff;\
550 #define YUVA_IN(y, u, v, a, s, pal)\
552 unsigned int val = ((const uint32_t *)(pal))[*(const uint8_t*)(s)];\
553 a = (val >> 24) & 0xff;\
554 y = (val >> 16) & 0xff;\
555 u = (val >> 8) & 0xff;\
559 #define YUVA_OUT(d, y, u, v, a)\
561 ((uint32_t *)(d))[0] = (a << 24) | (y << 16) | (u << 8) | v;\
567 static void blend_subrect(AVPicture *dst, const AVSubtitleRect *rect, int imgw, int imgh)
569 int wrap, wrap3, width2, skip2;
570 int y, u, v, a, u1, v1, a1, w, h;
571 uint8_t *lum, *cb, *cr;
574 int dstx, dsty, dstw, dsth;
576 dstw = av_clip(rect->w, 0, imgw);
577 dsth = av_clip(rect->h, 0, imgh);
578 dstx = av_clip(rect->x, 0, imgw - dstw);
579 dsty = av_clip(rect->y, 0, imgh - dsth);
580 lum = dst->data[0] + dsty * dst->linesize[0];
581 cb = dst->data[1] + (dsty >> 1) * dst->linesize[1];
582 cr = dst->data[2] + (dsty >> 1) * dst->linesize[2];
584 width2 = ((dstw + 1) >> 1) + (dstx & ~dstw & 1);
586 wrap = dst->linesize[0];
587 wrap3 = rect->pict.linesize[0];
588 p = rect->pict.data[0];
589 pal = (const uint32_t *)rect->pict.data[1]; /* Now in YCrCb! */
597 YUVA_IN(y, u, v, a, p, pal);
598 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
599 cb[0] = ALPHA_BLEND(a >> 2, cb[0], u, 0);
600 cr[0] = ALPHA_BLEND(a >> 2, cr[0], v, 0);
606 for (w = dstw - (dstx & 1); w >= 2; w -= 2) {
607 YUVA_IN(y, u, v, a, p, pal);
611 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
613 YUVA_IN(y, u, v, a, p + BPP, pal);
617 lum[1] = ALPHA_BLEND(a, lum[1], y, 0);
618 cb[0] = ALPHA_BLEND(a1 >> 2, cb[0], u1, 1);
619 cr[0] = ALPHA_BLEND(a1 >> 2, cr[0], v1, 1);
626 YUVA_IN(y, u, v, a, p, pal);
627 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
628 cb[0] = ALPHA_BLEND(a >> 2, cb[0], u, 0);
629 cr[0] = ALPHA_BLEND(a >> 2, cr[0], v, 0);
633 p += wrap3 - dstw * BPP;
634 lum += wrap - dstw - dstx;
635 cb += dst->linesize[1] - width2 - skip2;
636 cr += dst->linesize[2] - width2 - skip2;
638 for (h = dsth - (dsty & 1); h >= 2; h -= 2) {
644 YUVA_IN(y, u, v, a, p, pal);
648 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
651 YUVA_IN(y, u, v, a, p, pal);
655 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
656 cb[0] = ALPHA_BLEND(a1 >> 2, cb[0], u1, 1);
657 cr[0] = ALPHA_BLEND(a1 >> 2, cr[0], v1, 1);
663 for (w = dstw - (dstx & 1); w >= 2; w -= 2) {
664 YUVA_IN(y, u, v, a, p, pal);
668 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
670 YUVA_IN(y, u, v, a, p + BPP, pal);
674 lum[1] = ALPHA_BLEND(a, lum[1], y, 0);
678 YUVA_IN(y, u, v, a, p, pal);
682 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
684 YUVA_IN(y, u, v, a, p + BPP, pal);
688 lum[1] = ALPHA_BLEND(a, lum[1], y, 0);
690 cb[0] = ALPHA_BLEND(a1 >> 2, cb[0], u1, 2);
691 cr[0] = ALPHA_BLEND(a1 >> 2, cr[0], v1, 2);
695 p += -wrap3 + 2 * BPP;
699 YUVA_IN(y, u, v, a, p, pal);
703 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
706 YUVA_IN(y, u, v, a, p, pal);
710 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
711 cb[0] = ALPHA_BLEND(a1 >> 2, cb[0], u1, 1);
712 cr[0] = ALPHA_BLEND(a1 >> 2, cr[0], v1, 1);
718 p += wrap3 + (wrap3 - dstw * BPP);
719 lum += wrap + (wrap - dstw - dstx);
720 cb += dst->linesize[1] - width2 - skip2;
721 cr += dst->linesize[2] - width2 - skip2;
723 /* handle odd height */
730 YUVA_IN(y, u, v, a, p, pal);
731 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
732 cb[0] = ALPHA_BLEND(a >> 2, cb[0], u, 0);
733 cr[0] = ALPHA_BLEND(a >> 2, cr[0], v, 0);
739 for (w = dstw - (dstx & 1); w >= 2; w -= 2) {
740 YUVA_IN(y, u, v, a, p, pal);
744 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
746 YUVA_IN(y, u, v, a, p + BPP, pal);
750 lum[1] = ALPHA_BLEND(a, lum[1], y, 0);
751 cb[0] = ALPHA_BLEND(a1 >> 2, cb[0], u, 1);
752 cr[0] = ALPHA_BLEND(a1 >> 2, cr[0], v, 1);
759 YUVA_IN(y, u, v, a, p, pal);
760 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
761 cb[0] = ALPHA_BLEND(a >> 2, cb[0], u, 0);
762 cr[0] = ALPHA_BLEND(a >> 2, cr[0], v, 0);
767 static void free_subpicture(SubPicture *sp)
769 avsubtitle_free(&sp->sub);
772 static void calculate_display_rect(SDL_Rect *rect, int scr_xleft, int scr_ytop, int scr_width, int scr_height, VideoPicture *vp)
775 int width, height, x, y;
777 if (vp->sar.num == 0)
780 aspect_ratio = av_q2d(vp->sar);
782 if (aspect_ratio <= 0.0)
784 aspect_ratio *= (float)vp->width / (float)vp->height;
786 /* XXX: we suppose the screen has a 1.0 pixel ratio */
788 width = ((int)rint(height * aspect_ratio)) & ~1;
789 if (width > scr_width) {
791 height = ((int)rint(width / aspect_ratio)) & ~1;
793 x = (scr_width - width) / 2;
794 y = (scr_height - height) / 2;
795 rect->x = scr_xleft + x;
796 rect->y = scr_ytop + y;
797 rect->w = FFMAX(width, 1);
798 rect->h = FFMAX(height, 1);
801 static void video_image_display(VideoState *is)
809 vp = &is->pictq[is->pictq_rindex];
811 if (is->subtitle_st) {
812 if (is->subpq_size > 0) {
813 sp = &is->subpq[is->subpq_rindex];
815 if (vp->pts >= sp->pts + ((float) sp->sub.start_display_time / 1000)) {
816 SDL_LockYUVOverlay (vp->bmp);
818 pict.data[0] = vp->bmp->pixels[0];
819 pict.data[1] = vp->bmp->pixels[2];
820 pict.data[2] = vp->bmp->pixels[1];
822 pict.linesize[0] = vp->bmp->pitches[0];
823 pict.linesize[1] = vp->bmp->pitches[2];
824 pict.linesize[2] = vp->bmp->pitches[1];
826 for (i = 0; i < sp->sub.num_rects; i++)
827 blend_subrect(&pict, sp->sub.rects[i],
828 vp->bmp->w, vp->bmp->h);
830 SDL_UnlockYUVOverlay (vp->bmp);
835 calculate_display_rect(&rect, is->xleft, is->ytop, is->width, is->height, vp);
837 SDL_DisplayYUVOverlay(vp->bmp, &rect);
839 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) {
840 int bgcolor = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00);
841 fill_border(is->xleft, is->ytop, is->width, is->height, rect.x, rect.y, rect.w, rect.h, bgcolor, 1);
842 is->last_display_rect = rect;
847 static inline int compute_mod(int a, int b)
849 return a < 0 ? a%b + b : a%b;
852 static void video_audio_display(VideoState *s)
854 int i, i_start, x, y1, y, ys, delay, n, nb_display_channels;
855 int ch, channels, h, h2, bgcolor, fgcolor;
857 int rdft_bits, nb_freq;
859 for (rdft_bits = 1; (1 << rdft_bits) < 2 * s->height; rdft_bits++)
861 nb_freq = 1 << (rdft_bits - 1);
863 /* compute display index : center on currently output samples */
864 channels = s->audio_tgt.channels;
865 nb_display_channels = channels;
867 int data_used= s->show_mode == SHOW_MODE_WAVES ? s->width : (2*nb_freq);
869 delay = s->audio_write_buf_size;
872 /* to be more precise, we take into account the time spent since
873 the last buffer computation */
874 if (audio_callback_time) {
875 time_diff = av_gettime() - audio_callback_time;
876 delay -= (time_diff * s->audio_tgt.freq) / 1000000;
879 delay += 2 * data_used;
880 if (delay < data_used)
883 i_start= x = compute_mod(s->sample_array_index - delay * channels, SAMPLE_ARRAY_SIZE);
884 if (s->show_mode == SHOW_MODE_WAVES) {
886 for (i = 0; i < 1000; i += channels) {
887 int idx = (SAMPLE_ARRAY_SIZE + x - i) % SAMPLE_ARRAY_SIZE;
888 int a = s->sample_array[idx];
889 int b = s->sample_array[(idx + 4 * channels) % SAMPLE_ARRAY_SIZE];
890 int c = s->sample_array[(idx + 5 * channels) % SAMPLE_ARRAY_SIZE];
891 int d = s->sample_array[(idx + 9 * channels) % SAMPLE_ARRAY_SIZE];
893 if (h < score && (b ^ c) < 0) {
900 s->last_i_start = i_start;
902 i_start = s->last_i_start;
905 bgcolor = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00);
906 if (s->show_mode == SHOW_MODE_WAVES) {
907 fill_rectangle(screen,
908 s->xleft, s->ytop, s->width, s->height,
911 fgcolor = SDL_MapRGB(screen->format, 0xff, 0xff, 0xff);
913 /* total height for one channel */
914 h = s->height / nb_display_channels;
915 /* graph height / 2 */
917 for (ch = 0; ch < nb_display_channels; ch++) {
919 y1 = s->ytop + ch * h + (h / 2); /* position of center line */
920 for (x = 0; x < s->width; x++) {
921 y = (s->sample_array[i] * h2) >> 15;
928 fill_rectangle(screen,
929 s->xleft + x, ys, 1, y,
932 if (i >= SAMPLE_ARRAY_SIZE)
933 i -= SAMPLE_ARRAY_SIZE;
937 fgcolor = SDL_MapRGB(screen->format, 0x00, 0x00, 0xff);
939 for (ch = 1; ch < nb_display_channels; ch++) {
940 y = s->ytop + ch * h;
941 fill_rectangle(screen,
942 s->xleft, y, s->width, 1,
945 SDL_UpdateRect(screen, s->xleft, s->ytop, s->width, s->height);
947 nb_display_channels= FFMIN(nb_display_channels, 2);
948 if (rdft_bits != s->rdft_bits) {
949 av_rdft_end(s->rdft);
950 av_free(s->rdft_data);
951 s->rdft = av_rdft_init(rdft_bits, DFT_R2C);
952 s->rdft_bits = rdft_bits;
953 s->rdft_data = av_malloc(4 * nb_freq * sizeof(*s->rdft_data));
957 for (ch = 0; ch < nb_display_channels; ch++) {
958 data[ch] = s->rdft_data + 2 * nb_freq * ch;
960 for (x = 0; x < 2 * nb_freq; x++) {
961 double w = (x-nb_freq) * (1.0 / nb_freq);
962 data[ch][x] = s->sample_array[i] * (1.0 - w * w);
964 if (i >= SAMPLE_ARRAY_SIZE)
965 i -= SAMPLE_ARRAY_SIZE;
967 av_rdft_calc(s->rdft, data[ch]);
969 // least efficient way to do this, we should of course directly access it but its more than fast enough
970 for (y = 0; y < s->height; y++) {
971 double w = 1 / sqrt(nb_freq);
972 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]));
973 int b = (nb_display_channels == 2 ) ? sqrt(w * sqrt(data[1][2 * y + 0] * data[1][2 * y + 0]
974 + data[1][2 * y + 1] * data[1][2 * y + 1])) : a;
977 fgcolor = SDL_MapRGB(screen->format, a, b, (a + b) / 2);
979 fill_rectangle(screen,
980 s->xpos, s->height-y, 1, 1,
984 SDL_UpdateRect(screen, s->xpos, s->ytop, 1, s->height);
987 if (s->xpos >= s->width)
992 static void stream_close(VideoState *is)
996 /* XXX: use a special url_shutdown call to abort parse cleanly */
997 is->abort_request = 1;
998 SDL_WaitThread(is->read_tid, NULL);
999 packet_queue_destroy(&is->videoq);
1000 packet_queue_destroy(&is->audioq);
1001 packet_queue_destroy(&is->subtitleq);
1003 /* free all pictures */
1004 for (i = 0; i < VIDEO_PICTURE_QUEUE_SIZE; i++) {
1007 SDL_FreeYUVOverlay(vp->bmp);
1011 SDL_DestroyMutex(is->pictq_mutex);
1012 SDL_DestroyCond(is->pictq_cond);
1013 SDL_DestroyMutex(is->subpq_mutex);
1014 SDL_DestroyCond(is->subpq_cond);
1015 SDL_DestroyCond(is->continue_read_thread);
1016 #if !CONFIG_AVFILTER
1017 sws_freeContext(is->img_convert_ctx);
1022 static void do_exit(VideoState *is)
1027 av_lockmgr_register(NULL);
1031 av_freep(&vfilters);
1033 avformat_network_deinit();
1037 av_log(NULL, AV_LOG_QUIET, "%s", "");
1041 static void sigterm_handler(int sig)
1046 static int video_open(VideoState *is, int force_set_video_mode, VideoPicture *vp)
1048 int flags = SDL_HWSURFACE | SDL_ASYNCBLIT | SDL_HWACCEL;
1052 if (is_full_screen) flags |= SDL_FULLSCREEN;
1053 else flags |= SDL_RESIZABLE;
1055 if (vp && vp->width) {
1056 calculate_display_rect(&rect, 0, 0, INT_MAX, vp->height, vp);
1057 default_width = rect.w;
1058 default_height = rect.h;
1061 if (is_full_screen && fs_screen_width) {
1062 w = fs_screen_width;
1063 h = fs_screen_height;
1064 } else if (!is_full_screen && screen_width) {
1071 if (screen && is->width == screen->w && screen->w == w
1072 && is->height== screen->h && screen->h == h && !force_set_video_mode)
1074 screen = SDL_SetVideoMode(w, h, 0, flags);
1076 fprintf(stderr, "SDL: could not set video mode - exiting\n");
1080 window_title = input_filename;
1081 SDL_WM_SetCaption(window_title, window_title);
1083 is->width = screen->w;
1084 is->height = screen->h;
1089 /* display the current picture, if any */
1090 static void video_display(VideoState *is)
1093 video_open(is, 0, NULL);
1094 if (is->audio_st && is->show_mode != SHOW_MODE_VIDEO)
1095 video_audio_display(is);
1096 else if (is->video_st)
1097 video_image_display(is);
1100 /* get the current audio clock value */
1101 static double get_audio_clock(VideoState *is)
1103 if (is->audio_clock_serial != is->audioq.serial)
1106 return is->audio_current_pts;
1108 return is->audio_current_pts_drift + av_gettime() / 1000000.0;
1112 /* get the current video clock value */
1113 static double get_video_clock(VideoState *is)
1115 if (is->video_clock_serial != is->videoq.serial)
1118 return is->video_current_pts;
1120 return is->video_current_pts_drift + av_gettime() / 1000000.0;
1124 /* get the current external clock value */
1125 static double get_external_clock(VideoState *is)
1128 return is->external_clock;
1130 double time = av_gettime() / 1000000.0;
1131 return is->external_clock_drift + time - (time - is->external_clock_time / 1000000.0) * (1.0 - is->external_clock_speed);
1135 static int get_master_sync_type(VideoState *is) {
1136 if (is->av_sync_type == AV_SYNC_VIDEO_MASTER) {
1138 return AV_SYNC_VIDEO_MASTER;
1140 return AV_SYNC_AUDIO_MASTER;
1141 } else if (is->av_sync_type == AV_SYNC_AUDIO_MASTER) {
1143 return AV_SYNC_AUDIO_MASTER;
1145 return AV_SYNC_EXTERNAL_CLOCK;
1147 return AV_SYNC_EXTERNAL_CLOCK;
1151 /* get the current master clock value */
1152 static double get_master_clock(VideoState *is)
1156 switch (get_master_sync_type(is)) {
1157 case AV_SYNC_VIDEO_MASTER:
1158 val = get_video_clock(is);
1160 case AV_SYNC_AUDIO_MASTER:
1161 val = get_audio_clock(is);
1164 val = get_external_clock(is);
1170 static void update_external_clock_pts(VideoState *is, double pts)
1172 is->external_clock_time = av_gettime();
1173 is->external_clock = pts;
1174 is->external_clock_drift = pts - is->external_clock_time / 1000000.0;
1177 static void check_external_clock_sync(VideoState *is, double pts) {
1178 double ext_clock = get_external_clock(is);
1179 if (isnan(ext_clock) || fabs(ext_clock - pts) > AV_NOSYNC_THRESHOLD) {
1180 update_external_clock_pts(is, pts);
1184 static void update_external_clock_speed(VideoState *is, double speed) {
1185 update_external_clock_pts(is, get_external_clock(is));
1186 is->external_clock_speed = speed;
1189 static void check_external_clock_speed(VideoState *is) {
1190 if (is->video_stream >= 0 && is->videoq.nb_packets <= MIN_FRAMES / 2 ||
1191 is->audio_stream >= 0 && is->audioq.nb_packets <= MIN_FRAMES / 2) {
1192 update_external_clock_speed(is, FFMAX(EXTERNAL_CLOCK_SPEED_MIN, is->external_clock_speed - EXTERNAL_CLOCK_SPEED_STEP));
1193 } else if ((is->video_stream < 0 || is->videoq.nb_packets > MIN_FRAMES * 2) &&
1194 (is->audio_stream < 0 || is->audioq.nb_packets > MIN_FRAMES * 2)) {
1195 update_external_clock_speed(is, FFMIN(EXTERNAL_CLOCK_SPEED_MAX, is->external_clock_speed + EXTERNAL_CLOCK_SPEED_STEP));
1197 double speed = is->external_clock_speed;
1199 update_external_clock_speed(is, speed + EXTERNAL_CLOCK_SPEED_STEP * (1.0 - speed) / fabs(1.0 - speed));
1203 /* seek in the stream */
1204 static void stream_seek(VideoState *is, int64_t pos, int64_t rel, int seek_by_bytes)
1206 if (!is->seek_req) {
1209 is->seek_flags &= ~AVSEEK_FLAG_BYTE;
1211 is->seek_flags |= AVSEEK_FLAG_BYTE;
1213 SDL_CondSignal(is->continue_read_thread);
1217 /* pause or resume the video */
1218 static void stream_toggle_pause(VideoState *is)
1221 is->frame_timer += av_gettime() / 1000000.0 + is->video_current_pts_drift - is->video_current_pts;
1222 if (is->read_pause_return != AVERROR(ENOSYS)) {
1223 is->video_current_pts = is->video_current_pts_drift + av_gettime() / 1000000.0;
1225 is->video_current_pts_drift = is->video_current_pts - av_gettime() / 1000000.0;
1227 update_external_clock_pts(is, get_external_clock(is));
1228 is->paused = !is->paused;
1231 static void toggle_pause(VideoState *is)
1233 stream_toggle_pause(is);
1237 static void step_to_next_frame(VideoState *is)
1239 /* if the stream is paused unpause it, then step */
1241 stream_toggle_pause(is);
1245 static double compute_target_delay(double delay, VideoState *is)
1247 double sync_threshold, diff;
1249 /* update delay to follow master synchronisation source */
1250 if (get_master_sync_type(is) != AV_SYNC_VIDEO_MASTER) {
1251 /* if video is slave, we try to correct big delays by
1252 duplicating or deleting a frame */
1253 diff = get_video_clock(is) - get_master_clock(is);
1255 /* skip or repeat frame. We take into account the
1256 delay to compute the threshold. I still don't know
1257 if it is the best guess */
1258 sync_threshold = FFMAX(AV_SYNC_THRESHOLD, delay);
1259 if (!isnan(diff) && fabs(diff) < AV_NOSYNC_THRESHOLD) {
1260 if (diff <= -sync_threshold)
1262 else if (diff >= sync_threshold)
1267 av_dlog(NULL, "video: delay=%0.3f A-V=%f\n",
1273 static void pictq_next_picture(VideoState *is) {
1274 /* update queue size and signal for next picture */
1275 if (++is->pictq_rindex == VIDEO_PICTURE_QUEUE_SIZE)
1276 is->pictq_rindex = 0;
1278 SDL_LockMutex(is->pictq_mutex);
1280 SDL_CondSignal(is->pictq_cond);
1281 SDL_UnlockMutex(is->pictq_mutex);
1284 static int pictq_prev_picture(VideoState *is) {
1285 VideoPicture *prevvp;
1287 /* update queue size and signal for the previous picture */
1288 prevvp = &is->pictq[(is->pictq_rindex + VIDEO_PICTURE_QUEUE_SIZE - 1) % VIDEO_PICTURE_QUEUE_SIZE];
1289 if (prevvp->allocated && prevvp->serial == is->videoq.serial) {
1290 SDL_LockMutex(is->pictq_mutex);
1291 if (is->pictq_size < VIDEO_PICTURE_QUEUE_SIZE - 1) {
1292 if (--is->pictq_rindex == -1)
1293 is->pictq_rindex = VIDEO_PICTURE_QUEUE_SIZE - 1;
1297 SDL_CondSignal(is->pictq_cond);
1298 SDL_UnlockMutex(is->pictq_mutex);
1303 static void update_video_pts(VideoState *is, double pts, int64_t pos, int serial) {
1304 double time = av_gettime() / 1000000.0;
1305 /* update current video pts */
1306 is->video_current_pts = pts;
1307 is->video_current_pts_drift = is->video_current_pts - time;
1308 is->video_current_pos = pos;
1309 is->frame_last_pts = pts;
1310 is->video_clock_serial = serial;
1311 if (is->videoq.serial == serial)
1312 check_external_clock_sync(is, is->video_current_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 (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;
1369 delay = compute_target_delay(is->frame_last_duration, is);
1371 time= av_gettime()/1000000.0;
1372 if (time < is->frame_timer + delay) {
1373 *remaining_time = FFMIN(is->frame_timer + delay - time, *remaining_time);
1378 is->frame_timer += delay * FFMAX(1, floor((time-is->frame_timer) / delay));
1380 SDL_LockMutex(is->pictq_mutex);
1381 update_video_pts(is, vp->pts, vp->pos, vp->serial);
1382 SDL_UnlockMutex(is->pictq_mutex);
1384 if (is->pictq_size > 1) {
1385 VideoPicture *nextvp = &is->pictq[(is->pictq_rindex + 1) % VIDEO_PICTURE_QUEUE_SIZE];
1386 duration = nextvp->pts - vp->pts;
1387 if(!is->step && (redisplay || framedrop>0 || (framedrop && get_master_sync_type(is) != AV_SYNC_VIDEO_MASTER)) && time > is->frame_timer + duration){
1389 is->frame_drops_late++;
1390 pictq_next_picture(is);
1396 if (is->subtitle_st) {
1397 if (is->subtitle_stream_changed) {
1398 SDL_LockMutex(is->subpq_mutex);
1400 while (is->subpq_size) {
1401 free_subpicture(&is->subpq[is->subpq_rindex]);
1403 /* update queue size and signal for next picture */
1404 if (++is->subpq_rindex == SUBPICTURE_QUEUE_SIZE)
1405 is->subpq_rindex = 0;
1409 is->subtitle_stream_changed = 0;
1411 SDL_CondSignal(is->subpq_cond);
1412 SDL_UnlockMutex(is->subpq_mutex);
1414 if (is->subpq_size > 0) {
1415 sp = &is->subpq[is->subpq_rindex];
1417 if (is->subpq_size > 1)
1418 sp2 = &is->subpq[(is->subpq_rindex + 1) % SUBPICTURE_QUEUE_SIZE];
1422 if ((is->video_current_pts > (sp->pts + ((float) sp->sub.end_display_time / 1000)))
1423 || (sp2 && is->video_current_pts > (sp2->pts + ((float) sp2->sub.start_display_time / 1000))))
1425 free_subpicture(sp);
1427 /* update queue size and signal for next picture */
1428 if (++is->subpq_rindex == SUBPICTURE_QUEUE_SIZE)
1429 is->subpq_rindex = 0;
1431 SDL_LockMutex(is->subpq_mutex);
1433 SDL_CondSignal(is->subpq_cond);
1434 SDL_UnlockMutex(is->subpq_mutex);
1441 /* display picture */
1442 if (!display_disable && is->show_mode == SHOW_MODE_VIDEO)
1445 pictq_next_picture(is);
1447 if (is->step && !is->paused)
1448 stream_toggle_pause(is);
1451 is->force_refresh = 0;
1453 static int64_t last_time;
1455 int aqsize, vqsize, sqsize;
1458 cur_time = av_gettime();
1459 if (!last_time || (cur_time - last_time) >= 30000) {
1464 aqsize = is->audioq.size;
1466 vqsize = is->videoq.size;
1467 if (is->subtitle_st)
1468 sqsize = is->subtitleq.size;
1470 if (is->audio_st && is->video_st)
1471 av_diff = get_audio_clock(is) - get_video_clock(is);
1472 printf("%7.2f A-V:%7.3f fd=%4d aq=%5dKB vq=%5dKB sq=%5dB f=%"PRId64"/%"PRId64" \r",
1473 get_master_clock(is),
1475 is->frame_drops_early + is->frame_drops_late,
1479 is->video_st ? is->video_st->codec->pts_correction_num_faulty_dts : 0,
1480 is->video_st ? is->video_st->codec->pts_correction_num_faulty_pts : 0);
1482 last_time = cur_time;
1487 /* allocate a picture (needs to do that in main thread to avoid
1488 potential locking problems */
1489 static void alloc_picture(VideoState *is)
1493 vp = &is->pictq[is->pictq_windex];
1496 SDL_FreeYUVOverlay(vp->bmp);
1498 video_open(is, 0, vp);
1500 vp->bmp = SDL_CreateYUVOverlay(vp->width, vp->height,
1503 if (!vp->bmp || vp->bmp->pitches[0] < vp->width) {
1504 /* SDL allocates a buffer smaller than requested if the video
1505 * overlay hardware is unable to support the requested size. */
1506 fprintf(stderr, "Error: the video system does not support an image\n"
1507 "size of %dx%d pixels. Try using -lowres or -vf \"scale=w:h\"\n"
1508 "to reduce the image size.\n", vp->width, vp->height );
1512 SDL_LockMutex(is->pictq_mutex);
1514 SDL_CondSignal(is->pictq_cond);
1515 SDL_UnlockMutex(is->pictq_mutex);
1518 static void duplicate_right_border_pixels(SDL_Overlay *bmp) {
1519 int i, width, height;
1521 for (i = 0; i < 3; i++) {
1528 if (bmp->pitches[i] > width) {
1529 maxp = bmp->pixels[i] + bmp->pitches[i] * height - 1;
1530 for (p = bmp->pixels[i] + width - 1; p < maxp; p += bmp->pitches[i])
1536 static int queue_picture(VideoState *is, AVFrame *src_frame, double pts, int64_t pos, int serial)
1540 #if defined(DEBUG_SYNC) && 0
1541 printf("frame_type=%c pts=%0.3f\n",
1542 av_get_picture_type_char(src_frame->pict_type), pts);
1545 /* wait until we have space to put a new picture */
1546 SDL_LockMutex(is->pictq_mutex);
1548 /* keep the last already displayed picture in the queue */
1549 while (is->pictq_size >= VIDEO_PICTURE_QUEUE_SIZE - 2 &&
1550 !is->videoq.abort_request) {
1551 SDL_CondWait(is->pictq_cond, is->pictq_mutex);
1553 SDL_UnlockMutex(is->pictq_mutex);
1555 if (is->videoq.abort_request)
1558 vp = &is->pictq[is->pictq_windex];
1561 vp->sar = src_frame->sample_aspect_ratio;
1563 vp->sar = av_guess_sample_aspect_ratio(is->ic, is->video_st, src_frame);
1566 /* alloc or resize hardware picture buffer */
1567 if (!vp->bmp || vp->reallocate || !vp->allocated ||
1568 vp->width != src_frame->width ||
1569 vp->height != src_frame->height) {
1574 vp->width = src_frame->width;
1575 vp->height = src_frame->height;
1577 /* the allocation must be done in the main thread to avoid
1578 locking problems. */
1579 event.type = FF_ALLOC_EVENT;
1580 event.user.data1 = is;
1581 SDL_PushEvent(&event);
1583 /* wait until the picture is allocated */
1584 SDL_LockMutex(is->pictq_mutex);
1585 while (!vp->allocated && !is->videoq.abort_request) {
1586 SDL_CondWait(is->pictq_cond, is->pictq_mutex);
1588 /* if the queue is aborted, we have to pop the pending ALLOC event or wait for the allocation to complete */
1589 if (is->videoq.abort_request && SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_EVENTMASK(FF_ALLOC_EVENT)) != 1) {
1590 while (!vp->allocated) {
1591 SDL_CondWait(is->pictq_cond, is->pictq_mutex);
1594 SDL_UnlockMutex(is->pictq_mutex);
1596 if (is->videoq.abort_request)
1600 /* if the frame is not skipped, then display it */
1602 AVPicture pict = { { 0 } };
1604 /* get a pointer on the bitmap */
1605 SDL_LockYUVOverlay (vp->bmp);
1607 pict.data[0] = vp->bmp->pixels[0];
1608 pict.data[1] = vp->bmp->pixels[2];
1609 pict.data[2] = vp->bmp->pixels[1];
1611 pict.linesize[0] = vp->bmp->pitches[0];
1612 pict.linesize[1] = vp->bmp->pitches[2];
1613 pict.linesize[2] = vp->bmp->pitches[1];
1616 // FIXME use direct rendering
1617 av_picture_copy(&pict, (AVPicture *)src_frame,
1618 src_frame->format, vp->width, vp->height);
1620 av_opt_get_int(sws_opts, "sws_flags", 0, &sws_flags);
1621 is->img_convert_ctx = sws_getCachedContext(is->img_convert_ctx,
1622 vp->width, vp->height, src_frame->format, vp->width, vp->height,
1623 AV_PIX_FMT_YUV420P, sws_flags, NULL, NULL, NULL);
1624 if (is->img_convert_ctx == NULL) {
1625 fprintf(stderr, "Cannot initialize the conversion context\n");
1628 sws_scale(is->img_convert_ctx, src_frame->data, src_frame->linesize,
1629 0, vp->height, pict.data, pict.linesize);
1631 /* workaround SDL PITCH_WORKAROUND */
1632 duplicate_right_border_pixels(vp->bmp);
1633 /* update the bitmap content */
1634 SDL_UnlockYUVOverlay(vp->bmp);
1638 vp->serial = serial;
1640 /* now we can update the picture count */
1641 if (++is->pictq_windex == VIDEO_PICTURE_QUEUE_SIZE)
1642 is->pictq_windex = 0;
1643 SDL_LockMutex(is->pictq_mutex);
1645 SDL_UnlockMutex(is->pictq_mutex);
1650 static int get_video_frame(VideoState *is, AVFrame *frame, int64_t *pts, AVPacket *pkt, int *serial)
1654 if (packet_queue_get(&is->videoq, pkt, 1, serial) < 0)
1657 if (pkt->data == flush_pkt.data) {
1658 avcodec_flush_buffers(is->video_st->codec);
1660 SDL_LockMutex(is->pictq_mutex);
1661 // Make sure there are no long delay timers (ideally we should just flush the queue but that's harder)
1662 while (is->pictq_size && !is->videoq.abort_request) {
1663 SDL_CondWait(is->pictq_cond, is->pictq_mutex);
1665 is->video_current_pos = -1;
1666 is->frame_last_pts = AV_NOPTS_VALUE;
1667 is->frame_last_duration = 0;
1668 is->frame_timer = (double)av_gettime() / 1000000.0;
1669 is->frame_last_dropped_pts = AV_NOPTS_VALUE;
1670 SDL_UnlockMutex(is->pictq_mutex);
1674 if(avcodec_decode_video2(is->video_st->codec, frame, &got_picture, pkt) < 0)
1680 if (decoder_reorder_pts == -1) {
1681 *pts = av_frame_get_best_effort_timestamp(frame);
1682 } else if (decoder_reorder_pts) {
1683 *pts = frame->pkt_pts;
1685 *pts = frame->pkt_dts;
1688 if (*pts == AV_NOPTS_VALUE) {
1692 if (framedrop>0 || (framedrop && get_master_sync_type(is) != AV_SYNC_VIDEO_MASTER)) {
1693 SDL_LockMutex(is->pictq_mutex);
1694 if (is->frame_last_pts != AV_NOPTS_VALUE && *pts) {
1695 double clockdiff = get_video_clock(is) - get_master_clock(is);
1696 double dpts = av_q2d(is->video_st->time_base) * *pts;
1697 double ptsdiff = dpts - is->frame_last_pts;
1698 if (!isnan(clockdiff) && fabs(clockdiff) < AV_NOSYNC_THRESHOLD &&
1699 ptsdiff > 0 && ptsdiff < AV_NOSYNC_THRESHOLD &&
1700 clockdiff + ptsdiff - is->frame_last_filter_delay < 0) {
1701 is->frame_last_dropped_pos = pkt->pos;
1702 is->frame_last_dropped_pts = dpts;
1703 is->frame_last_dropped_serial = *serial;
1704 is->frame_drops_early++;
1705 av_frame_unref(frame);
1709 SDL_UnlockMutex(is->pictq_mutex);
1718 static int configure_filtergraph(AVFilterGraph *graph, const char *filtergraph,
1719 AVFilterContext *source_ctx, AVFilterContext *sink_ctx)
1722 AVFilterInOut *outputs = NULL, *inputs = NULL;
1725 outputs = avfilter_inout_alloc();
1726 inputs = avfilter_inout_alloc();
1727 if (!outputs || !inputs) {
1728 ret = AVERROR(ENOMEM);
1732 outputs->name = av_strdup("in");
1733 outputs->filter_ctx = source_ctx;
1734 outputs->pad_idx = 0;
1735 outputs->next = NULL;
1737 inputs->name = av_strdup("out");
1738 inputs->filter_ctx = sink_ctx;
1739 inputs->pad_idx = 0;
1740 inputs->next = NULL;
1742 if ((ret = avfilter_graph_parse(graph, filtergraph, &inputs, &outputs, NULL)) < 0)
1745 if ((ret = avfilter_link(source_ctx, 0, sink_ctx, 0)) < 0)
1749 ret = avfilter_graph_config(graph, NULL);
1751 avfilter_inout_free(&outputs);
1752 avfilter_inout_free(&inputs);
1756 static int configure_video_filters(AVFilterGraph *graph, VideoState *is, const char *vfilters, AVFrame *frame)
1758 static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE };
1759 char sws_flags_str[128];
1760 char buffersrc_args[256];
1762 AVBufferSinkParams *buffersink_params = av_buffersink_params_alloc();
1763 AVFilterContext *filt_src = NULL, *filt_out = NULL, *filt_crop;
1764 AVCodecContext *codec = is->video_st->codec;
1765 AVRational fr = av_guess_frame_rate(is->ic, is->video_st, NULL);
1767 if (!buffersink_params)
1768 return AVERROR(ENOMEM);
1770 av_opt_get_int(sws_opts, "sws_flags", 0, &sws_flags);
1771 snprintf(sws_flags_str, sizeof(sws_flags_str), "flags=%"PRId64, sws_flags);
1772 graph->scale_sws_opts = av_strdup(sws_flags_str);
1774 snprintf(buffersrc_args, sizeof(buffersrc_args),
1775 "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d",
1776 frame->width, frame->height, frame->format,
1777 is->video_st->time_base.num, is->video_st->time_base.den,
1778 codec->sample_aspect_ratio.num, FFMAX(codec->sample_aspect_ratio.den, 1));
1779 if (fr.num && fr.den)
1780 av_strlcatf(buffersrc_args, sizeof(buffersrc_args), ":frame_rate=%d/%d", fr.num, fr.den);
1782 if ((ret = avfilter_graph_create_filter(&filt_src,
1783 avfilter_get_by_name("buffer"),
1784 "ffplay_buffer", buffersrc_args, NULL,
1788 buffersink_params->pixel_fmts = pix_fmts;
1789 ret = avfilter_graph_create_filter(&filt_out,
1790 avfilter_get_by_name("buffersink"),
1791 "ffplay_buffersink", NULL, buffersink_params, graph);
1795 /* SDL YUV code is not handling odd width/height for some driver
1796 * combinations, therefore we crop the picture to an even width/height. */
1797 if ((ret = avfilter_graph_create_filter(&filt_crop,
1798 avfilter_get_by_name("crop"),
1799 "ffplay_crop", "floor(in_w/2)*2:floor(in_h/2)*2", NULL, graph)) < 0)
1801 if ((ret = avfilter_link(filt_crop, 0, filt_out, 0)) < 0)
1804 if ((ret = configure_filtergraph(graph, vfilters, filt_src, filt_crop)) < 0)
1807 is->in_video_filter = filt_src;
1808 is->out_video_filter = filt_out;
1811 av_freep(&buffersink_params);
1815 static int configure_audio_filters(VideoState *is, const char *afilters, int force_output_format)
1817 static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE };
1818 int sample_rates[2] = { 0, -1 };
1819 int64_t channel_layouts[2] = { 0, -1 };
1820 int channels[2] = { 0, -1 };
1821 AVFilterContext *filt_asrc = NULL, *filt_asink = NULL;
1822 char asrc_args[256];
1823 AVABufferSinkParams *asink_params = NULL;
1826 avfilter_graph_free(&is->agraph);
1827 if (!(is->agraph = avfilter_graph_alloc()))
1828 return AVERROR(ENOMEM);
1830 ret = snprintf(asrc_args, sizeof(asrc_args),
1831 "sample_rate=%d:sample_fmt=%s:channels=%d",
1832 is->audio_filter_src.freq, av_get_sample_fmt_name(is->audio_filter_src.fmt),
1833 is->audio_filter_src.channels);
1834 if (is->audio_filter_src.channel_layout)
1835 snprintf(asrc_args + ret, sizeof(asrc_args) - ret,
1836 ":channel_layout=0x%"PRIx64, is->audio_filter_src.channel_layout);
1838 ret = avfilter_graph_create_filter(&filt_asrc,
1839 avfilter_get_by_name("abuffer"), "ffplay_abuffer",
1840 asrc_args, NULL, is->agraph);
1844 if (!(asink_params = av_abuffersink_params_alloc())) {
1845 ret = AVERROR(ENOMEM);
1848 asink_params->sample_fmts = sample_fmts;
1850 asink_params->all_channel_counts = 1;
1851 if (force_output_format) {
1852 channel_layouts[0] = is->audio_tgt.channel_layout;
1853 asink_params->channel_layouts = channel_layouts;
1854 asink_params->all_channel_counts = 0;
1855 channels[0] = is->audio_tgt.channels;
1856 asink_params->channel_counts = channels;
1857 asink_params->all_channel_counts = 0;
1858 sample_rates[0] = is->audio_tgt.freq;
1859 asink_params->sample_rates = sample_rates;
1862 ret = avfilter_graph_create_filter(&filt_asink,
1863 avfilter_get_by_name("abuffersink"), "ffplay_abuffersink",
1864 NULL, asink_params, is->agraph);
1868 if ((ret = configure_filtergraph(is->agraph, afilters, filt_asrc, filt_asink)) < 0)
1871 is->in_audio_filter = filt_asrc;
1872 is->out_audio_filter = filt_asink;
1875 av_freep(&asink_params);
1877 avfilter_graph_free(&is->agraph);
1880 #endif /* CONFIG_AVFILTER */
1882 static int video_thread(void *arg)
1884 AVPacket pkt = { 0 };
1885 VideoState *is = arg;
1886 AVFrame *frame = av_frame_alloc();
1887 int64_t pts_int = AV_NOPTS_VALUE, pos = -1;
1893 AVFilterGraph *graph = avfilter_graph_alloc();
1894 AVFilterContext *filt_out = NULL, *filt_in = NULL;
1897 enum AVPixelFormat last_format = -2;
1898 int last_serial = -1;
1905 while (is->paused && !is->videoq.abort_request)
1908 avcodec_get_frame_defaults(frame);
1909 av_free_packet(&pkt);
1911 ret = get_video_frame(is, frame, &pts_int, &pkt, &serial);
1919 if ( last_w != frame->width
1920 || last_h != frame->height
1921 || last_format != frame->format
1922 || last_serial != serial) {
1923 av_log(NULL, AV_LOG_DEBUG,
1924 "Video frame changed from size:%dx%d format:%s serial:%d to size:%dx%d format:%s serial:%d\n",
1926 (const char *)av_x_if_null(av_get_pix_fmt_name(last_format), "none"), last_serial,
1927 frame->width, frame->height,
1928 (const char *)av_x_if_null(av_get_pix_fmt_name(frame->format), "none"), serial);
1929 avfilter_graph_free(&graph);
1930 graph = avfilter_graph_alloc();
1931 if ((ret = configure_video_filters(graph, is, vfilters, frame)) < 0) {
1933 event.type = FF_QUIT_EVENT;
1934 event.user.data1 = is;
1935 SDL_PushEvent(&event);
1936 av_free_packet(&pkt);
1939 filt_in = is->in_video_filter;
1940 filt_out = is->out_video_filter;
1941 last_w = frame->width;
1942 last_h = frame->height;
1943 last_format = frame->format;
1944 last_serial = serial;
1947 frame->pts = pts_int;
1948 frame->sample_aspect_ratio = av_guess_sample_aspect_ratio(is->ic, is->video_st, frame);
1949 ret = av_buffersrc_add_frame(filt_in, frame);
1952 av_frame_unref(frame);
1953 avcodec_get_frame_defaults(frame);
1954 av_free_packet(&pkt);
1957 is->frame_last_returned_time = av_gettime() / 1000000.0;
1959 ret = av_buffersink_get_frame_flags(filt_out, frame, 0);
1965 is->frame_last_filter_delay = av_gettime() / 1000000.0 - is->frame_last_returned_time;
1966 if (fabs(is->frame_last_filter_delay) > AV_NOSYNC_THRESHOLD / 10.0)
1967 is->frame_last_filter_delay = 0;
1969 pts_int = frame->pts;
1970 tb = filt_out->inputs[0]->time_base;
1971 pos = av_frame_get_pkt_pos(frame);
1972 if (av_cmp_q(tb, is->video_st->time_base)) {
1973 av_unused int64_t pts1 = pts_int;
1974 pts_int = av_rescale_q(pts_int, tb, is->video_st->time_base);
1975 av_dlog(NULL, "video_thread(): "
1976 "tb:%d/%d pts:%"PRId64" -> tb:%d/%d pts:%"PRId64"\n",
1977 tb.num, tb.den, pts1,
1978 is->video_st->time_base.num, is->video_st->time_base.den, pts_int);
1980 pts = pts_int * av_q2d(is->video_st->time_base);
1981 ret = queue_picture(is, frame, pts, pos, serial);
1982 av_frame_unref(frame);
1985 pts = pts_int * av_q2d(is->video_st->time_base);
1986 ret = queue_picture(is, frame, pts, pkt.pos, serial);
1987 av_frame_unref(frame);
1994 avcodec_flush_buffers(is->video_st->codec);
1996 avfilter_graph_free(&graph);
1998 av_free_packet(&pkt);
1999 av_frame_free(&frame);
2003 static int subtitle_thread(void *arg)
2005 VideoState *is = arg;
2007 AVPacket pkt1, *pkt = &pkt1;
2011 int r, g, b, y, u, v, a;
2014 while (is->paused && !is->subtitleq.abort_request) {
2017 if (packet_queue_get(&is->subtitleq, pkt, 1, NULL) < 0)
2020 if (pkt->data == flush_pkt.data) {
2021 avcodec_flush_buffers(is->subtitle_st->codec);
2024 SDL_LockMutex(is->subpq_mutex);
2025 while (is->subpq_size >= SUBPICTURE_QUEUE_SIZE &&
2026 !is->subtitleq.abort_request) {
2027 SDL_CondWait(is->subpq_cond, is->subpq_mutex);
2029 SDL_UnlockMutex(is->subpq_mutex);
2031 if (is->subtitleq.abort_request)
2034 sp = &is->subpq[is->subpq_windex];
2036 /* NOTE: ipts is the PTS of the _first_ picture beginning in
2037 this packet, if any */
2039 if (pkt->pts != AV_NOPTS_VALUE)
2040 pts = av_q2d(is->subtitle_st->time_base) * pkt->pts;
2042 avcodec_decode_subtitle2(is->subtitle_st->codec, &sp->sub,
2043 &got_subtitle, pkt);
2044 if (got_subtitle && sp->sub.format == 0) {
2045 if (sp->sub.pts != AV_NOPTS_VALUE)
2046 pts = sp->sub.pts / (double)AV_TIME_BASE;
2049 for (i = 0; i < sp->sub.num_rects; i++)
2051 for (j = 0; j < sp->sub.rects[i]->nb_colors; j++)
2053 RGBA_IN(r, g, b, a, (uint32_t*)sp->sub.rects[i]->pict.data[1] + j);
2054 y = RGB_TO_Y_CCIR(r, g, b);
2055 u = RGB_TO_U_CCIR(r, g, b, 0);
2056 v = RGB_TO_V_CCIR(r, g, b, 0);
2057 YUVA_OUT((uint32_t*)sp->sub.rects[i]->pict.data[1] + j, y, u, v, a);
2061 /* now we can update the picture count */
2062 if (++is->subpq_windex == SUBPICTURE_QUEUE_SIZE)
2063 is->subpq_windex = 0;
2064 SDL_LockMutex(is->subpq_mutex);
2066 SDL_UnlockMutex(is->subpq_mutex);
2068 av_free_packet(pkt);
2073 /* copy samples for viewing in editor window */
2074 static void update_sample_display(VideoState *is, short *samples, int samples_size)
2078 size = samples_size / sizeof(short);
2080 len = SAMPLE_ARRAY_SIZE - is->sample_array_index;
2083 memcpy(is->sample_array + is->sample_array_index, samples, len * sizeof(short));
2085 is->sample_array_index += len;
2086 if (is->sample_array_index >= SAMPLE_ARRAY_SIZE)
2087 is->sample_array_index = 0;
2092 /* return the wanted number of samples to get better sync if sync_type is video
2093 * or external master clock */
2094 static int synchronize_audio(VideoState *is, int nb_samples)
2096 int wanted_nb_samples = nb_samples;
2098 /* if not master, then we try to remove or add samples to correct the clock */
2099 if (get_master_sync_type(is) != AV_SYNC_AUDIO_MASTER) {
2100 double diff, avg_diff;
2101 int min_nb_samples, max_nb_samples;
2103 diff = get_audio_clock(is) - get_master_clock(is);
2105 if (!isnan(diff) && fabs(diff) < AV_NOSYNC_THRESHOLD) {
2106 is->audio_diff_cum = diff + is->audio_diff_avg_coef * is->audio_diff_cum;
2107 if (is->audio_diff_avg_count < AUDIO_DIFF_AVG_NB) {
2108 /* not enough measures to have a correct estimate */
2109 is->audio_diff_avg_count++;
2111 /* estimate the A-V difference */
2112 avg_diff = is->audio_diff_cum * (1.0 - is->audio_diff_avg_coef);
2114 if (fabs(avg_diff) >= is->audio_diff_threshold) {
2115 wanted_nb_samples = nb_samples + (int)(diff * is->audio_src.freq);
2116 min_nb_samples = ((nb_samples * (100 - SAMPLE_CORRECTION_PERCENT_MAX) / 100));
2117 max_nb_samples = ((nb_samples * (100 + SAMPLE_CORRECTION_PERCENT_MAX) / 100));
2118 wanted_nb_samples = FFMIN(FFMAX(wanted_nb_samples, min_nb_samples), max_nb_samples);
2120 av_dlog(NULL, "diff=%f adiff=%f sample_diff=%d apts=%0.3f %f\n",
2121 diff, avg_diff, wanted_nb_samples - nb_samples,
2122 is->audio_clock, is->audio_diff_threshold);
2125 /* too big difference : may be initial PTS errors, so
2127 is->audio_diff_avg_count = 0;
2128 is->audio_diff_cum = 0;
2132 return wanted_nb_samples;
2136 * Decode one audio frame and return its uncompressed size.
2138 * The processed audio frame is decoded, converted if required, and
2139 * stored in is->audio_buf, with size in bytes given by the return
2142 static int audio_decode_frame(VideoState *is)
2144 AVPacket *pkt_temp = &is->audio_pkt_temp;
2145 AVPacket *pkt = &is->audio_pkt;
2146 AVCodecContext *dec = is->audio_st->codec;
2147 int len1, data_size, resampled_data_size;
2148 int64_t dec_channel_layout;
2150 av_unused double audio_clock0;
2152 int flush_complete = 0;
2153 int wanted_nb_samples;
2157 /* NOTE: the audio packet can contain several frames */
2158 while (pkt_temp->size > 0 || (!pkt_temp->data && new_packet)) {
2160 if (!(is->frame = avcodec_alloc_frame()))
2161 return AVERROR(ENOMEM);
2163 av_frame_unref(is->frame);
2164 avcodec_get_frame_defaults(is->frame);
2167 if (is->audioq.serial != is->audio_pkt_temp_serial)
2176 len1 = avcodec_decode_audio4(dec, is->frame, &got_frame, pkt_temp);
2178 /* if error, we skip the frame */
2183 pkt_temp->data += len1;
2184 pkt_temp->size -= len1;
2187 /* stop sending empty packets if the decoder is finished */
2188 if (!pkt_temp->data && dec->codec->capabilities & CODEC_CAP_DELAY)
2193 if (is->frame->pts == AV_NOPTS_VALUE && pkt_temp->pts != AV_NOPTS_VALUE)
2194 is->frame->pts = av_rescale_q(pkt_temp->pts, is->audio_st->time_base, dec->time_base);
2195 if (pkt_temp->pts != AV_NOPTS_VALUE)
2196 pkt_temp->pts += (double) is->frame->nb_samples / is->frame->sample_rate / av_q2d(is->audio_st->time_base);
2197 tb = dec->time_base;
2204 dec_channel_layout = get_valid_channel_layout(is->frame->channel_layout, av_frame_get_channels(is->frame));
2207 cmp_audio_fmts(is->audio_filter_src.fmt, is->audio_filter_src.channels,
2208 is->frame->format, av_frame_get_channels(is->frame)) ||
2209 is->audio_filter_src.channel_layout != dec_channel_layout ||
2210 is->audio_filter_src.freq != is->frame->sample_rate ||
2211 is->audio_pkt_temp_serial != is->audio_last_serial;
2214 char buf1[1024], buf2[1024];
2215 av_get_channel_layout_string(buf1, sizeof(buf1), -1, is->audio_filter_src.channel_layout);
2216 av_get_channel_layout_string(buf2, sizeof(buf2), -1, dec_channel_layout);
2217 av_log(NULL, AV_LOG_DEBUG,
2218 "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",
2219 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,
2220 is->frame->sample_rate, av_frame_get_channels(is->frame), av_get_sample_fmt_name(is->frame->format), buf2, is->audio_pkt_temp_serial);
2222 is->audio_filter_src.fmt = is->frame->format;
2223 is->audio_filter_src.channels = av_frame_get_channels(is->frame);
2224 is->audio_filter_src.channel_layout = dec_channel_layout;
2225 is->audio_filter_src.freq = is->frame->sample_rate;
2226 is->audio_last_serial = is->audio_pkt_temp_serial;
2228 if ((ret = configure_audio_filters(is, afilters, 1)) < 0)
2232 if ((ret = av_buffersrc_add_frame(is->in_audio_filter, is->frame)) < 0)
2234 av_frame_unref(is->frame);
2235 if ((ret = av_buffersink_get_frame_flags(is->out_audio_filter, is->frame, 0)) < 0)
2237 tb = is->out_audio_filter->inputs[0]->time_base;
2241 data_size = av_samples_get_buffer_size(NULL, av_frame_get_channels(is->frame),
2242 is->frame->nb_samples,
2243 is->frame->format, 1);
2245 dec_channel_layout =
2246 (is->frame->channel_layout && av_frame_get_channels(is->frame) == av_get_channel_layout_nb_channels(is->frame->channel_layout)) ?
2247 is->frame->channel_layout : av_get_default_channel_layout(av_frame_get_channels(is->frame));
2248 wanted_nb_samples = synchronize_audio(is, is->frame->nb_samples);
2250 if (is->frame->format != is->audio_src.fmt ||
2251 dec_channel_layout != is->audio_src.channel_layout ||
2252 is->frame->sample_rate != is->audio_src.freq ||
2253 (wanted_nb_samples != is->frame->nb_samples && !is->swr_ctx)) {
2254 swr_free(&is->swr_ctx);
2255 is->swr_ctx = swr_alloc_set_opts(NULL,
2256 is->audio_tgt.channel_layout, is->audio_tgt.fmt, is->audio_tgt.freq,
2257 dec_channel_layout, is->frame->format, is->frame->sample_rate,
2259 if (!is->swr_ctx || swr_init(is->swr_ctx) < 0) {
2260 fprintf(stderr, "Cannot create sample rate converter for conversion of %d Hz %s %d channels to %d Hz %s %d channels!\n",
2261 is->frame->sample_rate, av_get_sample_fmt_name(is->frame->format), av_frame_get_channels(is->frame),
2262 is->audio_tgt.freq, av_get_sample_fmt_name(is->audio_tgt.fmt), is->audio_tgt.channels);
2265 is->audio_src.channel_layout = dec_channel_layout;
2266 is->audio_src.channels = av_frame_get_channels(is->frame);
2267 is->audio_src.freq = is->frame->sample_rate;
2268 is->audio_src.fmt = is->frame->format;
2272 const uint8_t **in = (const uint8_t **)is->frame->extended_data;
2273 uint8_t **out = &is->audio_buf1;
2274 int out_count = (int64_t)wanted_nb_samples * is->audio_tgt.freq / is->frame->sample_rate + 256;
2275 int out_size = av_samples_get_buffer_size(NULL, is->audio_tgt.channels, out_count, is->audio_tgt.fmt, 0);
2277 if (wanted_nb_samples != is->frame->nb_samples) {
2278 if (swr_set_compensation(is->swr_ctx, (wanted_nb_samples - is->frame->nb_samples) * is->audio_tgt.freq / is->frame->sample_rate,
2279 wanted_nb_samples * is->audio_tgt.freq / is->frame->sample_rate) < 0) {
2280 fprintf(stderr, "swr_set_compensation() failed\n");
2284 av_fast_malloc(&is->audio_buf1, &is->audio_buf1_size, out_size);
2285 if (!is->audio_buf1)
2286 return AVERROR(ENOMEM);
2287 len2 = swr_convert(is->swr_ctx, out, out_count, in, is->frame->nb_samples);
2289 fprintf(stderr, "swr_convert() failed\n");
2292 if (len2 == out_count) {
2293 fprintf(stderr, "warning: audio buffer is probably too small\n");
2294 swr_init(is->swr_ctx);
2296 is->audio_buf = is->audio_buf1;
2297 resampled_data_size = len2 * is->audio_tgt.channels * av_get_bytes_per_sample(is->audio_tgt.fmt);
2299 is->audio_buf = is->frame->data[0];
2300 resampled_data_size = data_size;
2303 audio_clock0 = is->audio_clock;
2304 /* update the audio clock with the pts */
2305 if (is->frame->pts != AV_NOPTS_VALUE) {
2306 is->audio_clock = is->frame->pts * av_q2d(tb) + (double) is->frame->nb_samples / is->frame->sample_rate;
2307 is->audio_clock_serial = is->audio_pkt_temp_serial;
2311 static double last_clock;
2312 printf("audio: delay=%0.3f clock=%0.3f clock0=%0.3f\n",
2313 is->audio_clock - last_clock,
2314 is->audio_clock, audio_clock0);
2315 last_clock = is->audio_clock;
2318 return resampled_data_size;
2321 /* free the current packet */
2323 av_free_packet(pkt);
2324 memset(pkt_temp, 0, sizeof(*pkt_temp));
2326 if (is->audioq.abort_request) {
2330 if (is->audioq.nb_packets == 0)
2331 SDL_CondSignal(is->continue_read_thread);
2333 /* read next packet */
2334 if ((new_packet = packet_queue_get(&is->audioq, pkt, 1, &is->audio_pkt_temp_serial)) < 0)
2337 if (pkt->data == flush_pkt.data) {
2338 avcodec_flush_buffers(dec);
2346 /* prepare a new audio buffer */
2347 static void sdl_audio_callback(void *opaque, Uint8 *stream, int len)
2349 VideoState *is = opaque;
2350 int audio_size, len1;
2352 int frame_size = av_samples_get_buffer_size(NULL, is->audio_tgt.channels, 1, is->audio_tgt.fmt, 1);
2354 audio_callback_time = av_gettime();
2357 if (is->audio_buf_index >= is->audio_buf_size) {
2358 audio_size = audio_decode_frame(is);
2359 if (audio_size < 0) {
2360 /* if error, just output silence */
2361 is->audio_buf = is->silence_buf;
2362 is->audio_buf_size = sizeof(is->silence_buf) / frame_size * frame_size;
2364 if (is->show_mode != SHOW_MODE_VIDEO)
2365 update_sample_display(is, (int16_t *)is->audio_buf, audio_size);
2366 is->audio_buf_size = audio_size;
2368 is->audio_buf_index = 0;
2370 len1 = is->audio_buf_size - is->audio_buf_index;
2373 memcpy(stream, (uint8_t *)is->audio_buf + is->audio_buf_index, len1);
2376 is->audio_buf_index += len1;
2378 bytes_per_sec = is->audio_tgt.freq * is->audio_tgt.channels * av_get_bytes_per_sample(is->audio_tgt.fmt);
2379 is->audio_write_buf_size = is->audio_buf_size - is->audio_buf_index;
2380 /* Let's assume the audio driver that is used by SDL has two periods. */
2381 is->audio_current_pts = is->audio_clock - (double)(2 * is->audio_hw_buf_size + is->audio_write_buf_size) / bytes_per_sec;
2382 is->audio_current_pts_drift = is->audio_current_pts - audio_callback_time / 1000000.0;
2383 if (is->audioq.serial == is->audio_clock_serial)
2384 check_external_clock_sync(is, is->audio_current_pts);
2387 static int audio_open(void *opaque, int64_t wanted_channel_layout, int wanted_nb_channels, int wanted_sample_rate, struct AudioParams *audio_hw_params)
2389 SDL_AudioSpec wanted_spec, spec;
2391 const int next_nb_channels[] = {0, 0, 1, 6, 2, 6, 4, 6};
2393 env = SDL_getenv("SDL_AUDIO_CHANNELS");
2395 wanted_nb_channels = atoi(env);
2396 wanted_channel_layout = av_get_default_channel_layout(wanted_nb_channels);
2398 if (!wanted_channel_layout || wanted_nb_channels != av_get_channel_layout_nb_channels(wanted_channel_layout)) {
2399 wanted_channel_layout = av_get_default_channel_layout(wanted_nb_channels);
2400 wanted_channel_layout &= ~AV_CH_LAYOUT_STEREO_DOWNMIX;
2402 wanted_spec.channels = av_get_channel_layout_nb_channels(wanted_channel_layout);
2403 wanted_spec.freq = wanted_sample_rate;
2404 if (wanted_spec.freq <= 0 || wanted_spec.channels <= 0) {
2405 fprintf(stderr, "Invalid sample rate or channel count!\n");
2408 wanted_spec.format = AUDIO_S16SYS;
2409 wanted_spec.silence = 0;
2410 wanted_spec.samples = SDL_AUDIO_BUFFER_SIZE;
2411 wanted_spec.callback = sdl_audio_callback;
2412 wanted_spec.userdata = opaque;
2413 while (SDL_OpenAudio(&wanted_spec, &spec) < 0) {
2414 fprintf(stderr, "SDL_OpenAudio (%d channels): %s\n", wanted_spec.channels, SDL_GetError());
2415 wanted_spec.channels = next_nb_channels[FFMIN(7, wanted_spec.channels)];
2416 if (!wanted_spec.channels) {
2417 fprintf(stderr, "No more channel combinations to try, audio open failed\n");
2420 wanted_channel_layout = av_get_default_channel_layout(wanted_spec.channels);
2422 if (spec.format != AUDIO_S16SYS) {
2423 fprintf(stderr, "SDL advised audio format %d is not supported!\n", spec.format);
2426 if (spec.channels != wanted_spec.channels) {
2427 wanted_channel_layout = av_get_default_channel_layout(spec.channels);
2428 if (!wanted_channel_layout) {
2429 fprintf(stderr, "SDL advised channel count %d is not supported!\n", spec.channels);
2434 audio_hw_params->fmt = AV_SAMPLE_FMT_S16;
2435 audio_hw_params->freq = spec.freq;
2436 audio_hw_params->channel_layout = wanted_channel_layout;
2437 audio_hw_params->channels = spec.channels;
2441 /* open a given stream. Return 0 if OK */
2442 static int stream_component_open(VideoState *is, int stream_index)
2444 AVFormatContext *ic = is->ic;
2445 AVCodecContext *avctx;
2447 const char *forced_codec_name = NULL;
2449 AVDictionaryEntry *t = NULL;
2450 int sample_rate, nb_channels;
2451 int64_t channel_layout;
2454 if (stream_index < 0 || stream_index >= ic->nb_streams)
2456 avctx = ic->streams[stream_index]->codec;
2458 codec = avcodec_find_decoder(avctx->codec_id);
2460 switch(avctx->codec_type){
2461 case AVMEDIA_TYPE_AUDIO : is->last_audio_stream = stream_index; forced_codec_name = audio_codec_name; break;
2462 case AVMEDIA_TYPE_SUBTITLE: is->last_subtitle_stream = stream_index; forced_codec_name = subtitle_codec_name; break;
2463 case AVMEDIA_TYPE_VIDEO : is->last_video_stream = stream_index; forced_codec_name = video_codec_name; break;
2465 if (forced_codec_name)
2466 codec = avcodec_find_decoder_by_name(forced_codec_name);
2468 if (forced_codec_name) fprintf(stderr, "No codec could be found with name '%s'\n", forced_codec_name);
2469 else fprintf(stderr, "No codec could be found with id %d\n", avctx->codec_id);
2473 avctx->codec_id = codec->id;
2474 avctx->workaround_bugs = workaround_bugs;
2475 avctx->lowres = lowres;
2476 if(avctx->lowres > codec->max_lowres){
2477 av_log(avctx, AV_LOG_WARNING, "The maximum value for lowres supported by the decoder is %d\n",
2479 avctx->lowres= codec->max_lowres;
2481 avctx->idct_algo = idct;
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);
2492 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO || avctx->codec_type == AVMEDIA_TYPE_AUDIO)
2493 av_dict_set(&opts, "refcounted_frames", "1", 0);
2494 if (avcodec_open2(avctx, codec, &opts) < 0)
2496 if ((t = av_dict_get(opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
2497 av_log(NULL, AV_LOG_ERROR, "Option %s not found.\n", t->key);
2498 return AVERROR_OPTION_NOT_FOUND;
2501 ic->streams[stream_index]->discard = AVDISCARD_DEFAULT;
2502 switch (avctx->codec_type) {
2503 case AVMEDIA_TYPE_AUDIO:
2508 is->audio_filter_src.freq = avctx->sample_rate;
2509 is->audio_filter_src.channels = avctx->channels;
2510 is->audio_filter_src.channel_layout = get_valid_channel_layout(avctx->channel_layout, avctx->channels);
2511 is->audio_filter_src.fmt = avctx->sample_fmt;
2512 if ((ret = configure_audio_filters(is, afilters, 0)) < 0)
2514 link = is->out_audio_filter->inputs[0];
2515 sample_rate = link->sample_rate;
2516 nb_channels = link->channels;
2517 channel_layout = link->channel_layout;
2520 sample_rate = avctx->sample_rate;
2521 nb_channels = avctx->channels;
2522 channel_layout = avctx->channel_layout;
2525 /* prepare audio output */
2526 if ((ret = audio_open(is, channel_layout, nb_channels, sample_rate, &is->audio_tgt)) < 0)
2528 is->audio_hw_buf_size = ret;
2529 is->audio_src = is->audio_tgt;
2530 is->audio_buf_size = 0;
2531 is->audio_buf_index = 0;
2533 /* init averaging filter */
2534 is->audio_diff_avg_coef = exp(log(0.01) / AUDIO_DIFF_AVG_NB);
2535 is->audio_diff_avg_count = 0;
2536 /* since we do not have a precise anough audio fifo fullness,
2537 we correct audio sync only if larger than this threshold */
2538 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);
2540 memset(&is->audio_pkt, 0, sizeof(is->audio_pkt));
2541 memset(&is->audio_pkt_temp, 0, sizeof(is->audio_pkt_temp));
2543 is->audio_stream = stream_index;
2544 is->audio_st = ic->streams[stream_index];
2546 packet_queue_start(&is->audioq);
2549 case AVMEDIA_TYPE_VIDEO:
2550 is->video_stream = stream_index;
2551 is->video_st = ic->streams[stream_index];
2553 packet_queue_start(&is->videoq);
2554 is->video_tid = SDL_CreateThread(video_thread, is);
2555 is->queue_attachments_req = 1;
2557 case AVMEDIA_TYPE_SUBTITLE:
2558 is->subtitle_stream = stream_index;
2559 is->subtitle_st = ic->streams[stream_index];
2560 packet_queue_start(&is->subtitleq);
2562 is->subtitle_tid = SDL_CreateThread(subtitle_thread, is);
2570 static void stream_component_close(VideoState *is, int stream_index)
2572 AVFormatContext *ic = is->ic;
2573 AVCodecContext *avctx;
2575 if (stream_index < 0 || stream_index >= ic->nb_streams)
2577 avctx = ic->streams[stream_index]->codec;
2579 switch (avctx->codec_type) {
2580 case AVMEDIA_TYPE_AUDIO:
2581 packet_queue_abort(&is->audioq);
2585 packet_queue_flush(&is->audioq);
2586 av_free_packet(&is->audio_pkt);
2587 swr_free(&is->swr_ctx);
2588 av_freep(&is->audio_buf1);
2589 is->audio_buf1_size = 0;
2590 is->audio_buf = NULL;
2591 av_frame_free(&is->frame);
2594 av_rdft_end(is->rdft);
2595 av_freep(&is->rdft_data);
2600 avfilter_graph_free(&is->agraph);
2603 case AVMEDIA_TYPE_VIDEO:
2604 packet_queue_abort(&is->videoq);
2606 /* note: we also signal this mutex to make sure we deblock the
2607 video thread in all cases */
2608 SDL_LockMutex(is->pictq_mutex);
2609 SDL_CondSignal(is->pictq_cond);
2610 SDL_UnlockMutex(is->pictq_mutex);
2612 SDL_WaitThread(is->video_tid, NULL);
2614 packet_queue_flush(&is->videoq);
2616 case AVMEDIA_TYPE_SUBTITLE:
2617 packet_queue_abort(&is->subtitleq);
2619 /* note: we also signal this mutex to make sure we deblock the
2620 video thread in all cases */
2621 SDL_LockMutex(is->subpq_mutex);
2622 is->subtitle_stream_changed = 1;
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 int pkt_in_play_range = 0;
2687 AVDictionaryEntry *t;
2688 AVDictionary **opts;
2689 int orig_nb_streams;
2690 SDL_mutex *wait_mutex = SDL_CreateMutex();
2692 memset(st_index, -1, sizeof(st_index));
2693 is->last_video_stream = is->video_stream = -1;
2694 is->last_audio_stream = is->audio_stream = -1;
2695 is->last_subtitle_stream = is->subtitle_stream = -1;
2697 ic = avformat_alloc_context();
2698 ic->interrupt_callback.callback = decode_interrupt_cb;
2699 ic->interrupt_callback.opaque = is;
2700 err = avformat_open_input(&ic, is->filename, is->iformat, &format_opts);
2702 print_error(is->filename, err);
2706 if ((t = av_dict_get(format_opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
2707 av_log(NULL, AV_LOG_ERROR, "Option %s not found.\n", t->key);
2708 ret = AVERROR_OPTION_NOT_FOUND;
2714 ic->flags |= AVFMT_FLAG_GENPTS;
2716 opts = setup_find_stream_info_opts(ic, codec_opts);
2717 orig_nb_streams = ic->nb_streams;
2719 err = avformat_find_stream_info(ic, opts);
2721 fprintf(stderr, "%s: could not find codec parameters\n", is->filename);
2725 for (i = 0; i < orig_nb_streams; i++)
2726 av_dict_free(&opts[i]);
2730 ic->pb->eof_reached = 0; // FIXME hack, ffplay maybe should not use url_feof() to test for the end
2732 if (seek_by_bytes < 0)
2733 seek_by_bytes = !!(ic->iformat->flags & AVFMT_TS_DISCONT) && strcmp("ogg", ic->iformat->name);
2735 is->max_frame_duration = (ic->iformat->flags & AVFMT_TS_DISCONT) ? 10.0 : 3600.0;
2737 /* if seeking requested, we execute it */
2738 if (start_time != AV_NOPTS_VALUE) {
2741 timestamp = start_time;
2742 /* add the stream start time */
2743 if (ic->start_time != AV_NOPTS_VALUE)
2744 timestamp += ic->start_time;
2745 ret = avformat_seek_file(ic, -1, INT64_MIN, timestamp, INT64_MAX, 0);
2747 fprintf(stderr, "%s: could not seek to position %0.3f\n",
2748 is->filename, (double)timestamp / AV_TIME_BASE);
2752 is->realtime = is_realtime(ic);
2754 for (i = 0; i < ic->nb_streams; i++)
2755 ic->streams[i]->discard = AVDISCARD_ALL;
2757 st_index[AVMEDIA_TYPE_VIDEO] =
2758 av_find_best_stream(ic, AVMEDIA_TYPE_VIDEO,
2759 wanted_stream[AVMEDIA_TYPE_VIDEO], -1, NULL, 0);
2761 st_index[AVMEDIA_TYPE_AUDIO] =
2762 av_find_best_stream(ic, AVMEDIA_TYPE_AUDIO,
2763 wanted_stream[AVMEDIA_TYPE_AUDIO],
2764 st_index[AVMEDIA_TYPE_VIDEO],
2766 if (!video_disable && !subtitle_disable)
2767 st_index[AVMEDIA_TYPE_SUBTITLE] =
2768 av_find_best_stream(ic, AVMEDIA_TYPE_SUBTITLE,
2769 wanted_stream[AVMEDIA_TYPE_SUBTITLE],
2770 (st_index[AVMEDIA_TYPE_AUDIO] >= 0 ?
2771 st_index[AVMEDIA_TYPE_AUDIO] :
2772 st_index[AVMEDIA_TYPE_VIDEO]),
2775 av_dump_format(ic, 0, is->filename, 0);
2778 is->show_mode = show_mode;
2780 /* open the streams */
2781 if (st_index[AVMEDIA_TYPE_AUDIO] >= 0) {
2782 stream_component_open(is, st_index[AVMEDIA_TYPE_AUDIO]);
2786 if (st_index[AVMEDIA_TYPE_VIDEO] >= 0) {
2787 ret = stream_component_open(is, st_index[AVMEDIA_TYPE_VIDEO]);
2789 if (is->show_mode == SHOW_MODE_NONE)
2790 is->show_mode = ret >= 0 ? SHOW_MODE_VIDEO : SHOW_MODE_RDFT;
2792 if (st_index[AVMEDIA_TYPE_SUBTITLE] >= 0) {
2793 stream_component_open(is, st_index[AVMEDIA_TYPE_SUBTITLE]);
2796 if (is->video_stream < 0 && is->audio_stream < 0) {
2797 fprintf(stderr, "%s: could not open codecs\n", is->filename);
2802 if (infinite_buffer < 0 && is->realtime)
2803 infinite_buffer = 1;
2806 if (is->abort_request)
2808 if (is->paused != is->last_paused) {
2809 is->last_paused = is->paused;
2811 is->read_pause_return = av_read_pause(ic);
2815 #if CONFIG_RTSP_DEMUXER || CONFIG_MMSH_PROTOCOL
2817 (!strcmp(ic->iformat->name, "rtsp") ||
2818 (ic->pb && !strncmp(input_filename, "mmsh:", 5)))) {
2819 /* wait 10 ms to avoid trying to get another packet */
2826 int64_t seek_target = is->seek_pos;
2827 int64_t seek_min = is->seek_rel > 0 ? seek_target - is->seek_rel + 2: INT64_MIN;
2828 int64_t seek_max = is->seek_rel < 0 ? seek_target - is->seek_rel - 2: INT64_MAX;
2829 // FIXME the +-2 is due to rounding being not done in the correct direction in generation
2830 // of the seek_pos/seek_rel variables
2832 ret = avformat_seek_file(is->ic, -1, seek_min, seek_target, seek_max, is->seek_flags);
2834 fprintf(stderr, "%s: error while seeking\n", is->ic->filename);
2836 if (is->audio_stream >= 0) {
2837 packet_queue_flush(&is->audioq);
2838 packet_queue_put(&is->audioq, &flush_pkt);
2840 if (is->subtitle_stream >= 0) {
2841 packet_queue_flush(&is->subtitleq);
2842 packet_queue_put(&is->subtitleq, &flush_pkt);
2844 if (is->video_stream >= 0) {
2845 packet_queue_flush(&is->videoq);
2846 packet_queue_put(&is->videoq, &flush_pkt);
2848 if (is->seek_flags & AVSEEK_FLAG_BYTE) {
2849 update_external_clock_pts(is, NAN);
2851 update_external_clock_pts(is, seek_target / (double)AV_TIME_BASE);
2855 is->queue_attachments_req = 1;
2858 step_to_next_frame(is);
2860 if (is->queue_attachments_req) {
2861 if (is->video_st && is->video_st->disposition & AV_DISPOSITION_ATTACHED_PIC) {
2863 if ((ret = av_copy_packet(©, &is->video_st->attached_pic)) < 0)
2865 packet_queue_put(&is->videoq, ©);
2867 is->queue_attachments_req = 0;
2870 /* if the queue are full, no need to read more */
2871 if (infinite_buffer<1 &&
2872 (is->audioq.size + is->videoq.size + is->subtitleq.size > MAX_QUEUE_SIZE
2873 || ( (is->audioq .nb_packets > MIN_FRAMES || is->audio_stream < 0 || is->audioq.abort_request)
2874 && (is->videoq .nb_packets > MIN_FRAMES || is->video_stream < 0 || is->videoq.abort_request
2875 || (is->video_st->disposition & AV_DISPOSITION_ATTACHED_PIC))
2876 && (is->subtitleq.nb_packets > MIN_FRAMES || is->subtitle_stream < 0 || is->subtitleq.abort_request)))) {
2878 SDL_LockMutex(wait_mutex);
2879 SDL_CondWaitTimeout(is->continue_read_thread, wait_mutex, 10);
2880 SDL_UnlockMutex(wait_mutex);
2884 if (is->video_stream >= 0) {
2885 av_init_packet(pkt);
2888 pkt->stream_index = is->video_stream;
2889 packet_queue_put(&is->videoq, pkt);
2891 if (is->audio_stream >= 0 &&
2892 is->audio_st->codec->codec->capabilities & CODEC_CAP_DELAY) {
2893 av_init_packet(pkt);
2896 pkt->stream_index = is->audio_stream;
2897 packet_queue_put(&is->audioq, pkt);
2900 if (is->audioq.size + is->videoq.size + is->subtitleq.size == 0) {
2901 if (loop != 1 && (!loop || --loop)) {
2902 stream_seek(is, start_time != AV_NOPTS_VALUE ? start_time : 0, 0, 0);
2903 } else if (autoexit) {
2911 ret = av_read_frame(ic, pkt);
2913 if (ret == AVERROR_EOF || url_feof(ic->pb))
2915 if (ic->pb && ic->pb->error)
2917 SDL_LockMutex(wait_mutex);
2918 SDL_CondWaitTimeout(is->continue_read_thread, wait_mutex, 10);
2919 SDL_UnlockMutex(wait_mutex);
2922 /* check if packet is in play range specified by user, then queue, otherwise discard */
2923 pkt_in_play_range = duration == AV_NOPTS_VALUE ||
2924 (pkt->pts - ic->streams[pkt->stream_index]->start_time) *
2925 av_q2d(ic->streams[pkt->stream_index]->time_base) -
2926 (double)(start_time != AV_NOPTS_VALUE ? start_time : 0) / 1000000
2927 <= ((double)duration / 1000000);
2928 if (pkt->stream_index == is->audio_stream && pkt_in_play_range) {
2929 packet_queue_put(&is->audioq, pkt);
2930 } else if (pkt->stream_index == is->video_stream && pkt_in_play_range
2931 && !(is->video_st->disposition & AV_DISPOSITION_ATTACHED_PIC)) {
2932 packet_queue_put(&is->videoq, pkt);
2933 } else if (pkt->stream_index == is->subtitle_stream && pkt_in_play_range) {
2934 packet_queue_put(&is->subtitleq, pkt);
2936 av_free_packet(pkt);
2939 /* wait until the end */
2940 while (!is->abort_request) {
2946 /* close each stream */
2947 if (is->audio_stream >= 0)
2948 stream_component_close(is, is->audio_stream);
2949 if (is->video_stream >= 0)
2950 stream_component_close(is, is->video_stream);
2951 if (is->subtitle_stream >= 0)
2952 stream_component_close(is, is->subtitle_stream);
2954 avformat_close_input(&is->ic);
2960 event.type = FF_QUIT_EVENT;
2961 event.user.data1 = is;
2962 SDL_PushEvent(&event);
2964 SDL_DestroyMutex(wait_mutex);
2968 static VideoState *stream_open(const char *filename, AVInputFormat *iformat)
2972 is = av_mallocz(sizeof(VideoState));
2975 av_strlcpy(is->filename, filename, sizeof(is->filename));
2976 is->iformat = iformat;
2980 /* start video display */
2981 is->pictq_mutex = SDL_CreateMutex();
2982 is->pictq_cond = SDL_CreateCond();
2984 is->subpq_mutex = SDL_CreateMutex();
2985 is->subpq_cond = SDL_CreateCond();
2987 packet_queue_init(&is->videoq);
2988 packet_queue_init(&is->audioq);
2989 packet_queue_init(&is->subtitleq);
2991 is->continue_read_thread = SDL_CreateCond();
2993 update_external_clock_pts(is, NAN);
2994 update_external_clock_speed(is, 1.0);
2995 is->audio_current_pts_drift = -av_gettime() / 1000000.0;
2996 is->video_current_pts_drift = is->audio_current_pts_drift;
2997 is->audio_clock_serial = -1;
2998 is->video_clock_serial = -1;
2999 is->audio_last_serial = -1;
3000 is->av_sync_type = av_sync_type;
3001 is->read_tid = SDL_CreateThread(read_thread, is);
3002 if (!is->read_tid) {
3009 static void stream_cycle_channel(VideoState *is, int codec_type)
3011 AVFormatContext *ic = is->ic;
3012 int start_index, stream_index;
3016 if (codec_type == AVMEDIA_TYPE_VIDEO) {
3017 start_index = is->last_video_stream;
3018 old_index = is->video_stream;
3019 } else if (codec_type == AVMEDIA_TYPE_AUDIO) {
3020 start_index = is->last_audio_stream;
3021 old_index = is->audio_stream;
3023 start_index = is->last_subtitle_stream;
3024 old_index = is->subtitle_stream;
3026 stream_index = start_index;
3028 if (++stream_index >= is->ic->nb_streams)
3030 if (codec_type == AVMEDIA_TYPE_SUBTITLE)
3033 is->last_subtitle_stream = -1;
3036 if (start_index == -1)
3040 if (stream_index == start_index)
3042 st = ic->streams[stream_index];
3043 if (st->codec->codec_type == codec_type) {
3044 /* check that parameters are OK */
3045 switch (codec_type) {
3046 case AVMEDIA_TYPE_AUDIO:
3047 if (st->codec->sample_rate != 0 &&
3048 st->codec->channels != 0)
3051 case AVMEDIA_TYPE_VIDEO:
3052 case AVMEDIA_TYPE_SUBTITLE:
3060 stream_component_close(is, old_index);
3061 stream_component_open(is, stream_index);
3065 static void toggle_full_screen(VideoState *is)
3067 #if defined(__APPLE__) && SDL_VERSION_ATLEAST(1, 2, 14)
3068 /* OS X needs to reallocate the SDL overlays */
3070 for (i = 0; i < VIDEO_PICTURE_QUEUE_SIZE; i++)
3071 is->pictq[i].reallocate = 1;
3073 is_full_screen = !is_full_screen;
3074 video_open(is, 1, NULL);
3077 static void toggle_audio_display(VideoState *is)
3079 int bgcolor = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00);
3080 int next = is->show_mode;
3082 next = (next + 1) % SHOW_MODE_NB;
3083 } while (next != is->show_mode && (next == SHOW_MODE_VIDEO && !is->video_st || next != SHOW_MODE_VIDEO && !is->audio_st));
3084 if (is->show_mode != next) {
3085 fill_rectangle(screen,
3086 is->xleft, is->ytop, is->width, is->height,
3088 is->force_refresh = 1;
3089 is->show_mode = next;
3093 static void refresh_loop_wait_event(VideoState *is, SDL_Event *event) {
3094 double remaining_time = 0.0;
3096 while (!SDL_PeepEvents(event, 1, SDL_GETEVENT, SDL_ALLEVENTS)) {
3097 if (!cursor_hidden && av_gettime() - cursor_last_shown > CURSOR_HIDE_DELAY) {
3101 if (remaining_time > 0.0)
3102 av_usleep((int64_t)(remaining_time * 1000000.0));
3103 remaining_time = REFRESH_RATE;
3104 if (is->show_mode != SHOW_MODE_NONE && (!is->paused || is->force_refresh))
3105 video_refresh(is, &remaining_time);
3110 /* handle an event sent by the GUI */
3111 static void event_loop(VideoState *cur_stream)
3114 double incr, pos, frac;
3118 refresh_loop_wait_event(cur_stream, &event);
3119 switch (event.type) {
3121 if (exit_on_keydown) {
3122 do_exit(cur_stream);
3125 switch (event.key.keysym.sym) {
3128 do_exit(cur_stream);
3131 toggle_full_screen(cur_stream);
3132 cur_stream->force_refresh = 1;
3136 toggle_pause(cur_stream);
3138 case SDLK_s: // S: Step to next frame
3139 step_to_next_frame(cur_stream);
3142 stream_cycle_channel(cur_stream, AVMEDIA_TYPE_AUDIO);
3145 stream_cycle_channel(cur_stream, AVMEDIA_TYPE_VIDEO);
3148 stream_cycle_channel(cur_stream, AVMEDIA_TYPE_SUBTITLE);
3151 toggle_audio_display(cur_stream);
3171 if (seek_by_bytes) {
3172 if (cur_stream->video_stream >= 0 && cur_stream->video_current_pos >= 0) {
3173 pos = cur_stream->video_current_pos;
3174 } else if (cur_stream->audio_stream >= 0 && cur_stream->audio_pkt.pos >= 0) {
3175 pos = cur_stream->audio_pkt.pos;
3177 pos = avio_tell(cur_stream->ic->pb);
3178 if (cur_stream->ic->bit_rate)
3179 incr *= cur_stream->ic->bit_rate / 8.0;
3183 stream_seek(cur_stream, pos, incr, 1);
3185 pos = get_master_clock(cur_stream);
3187 pos = (double)cur_stream->seek_pos / AV_TIME_BASE;
3189 if (cur_stream->ic->start_time != AV_NOPTS_VALUE && pos < cur_stream->ic->start_time / (double)AV_TIME_BASE)
3190 pos = cur_stream->ic->start_time / (double)AV_TIME_BASE;
3191 stream_seek(cur_stream, (int64_t)(pos * AV_TIME_BASE), (int64_t)(incr * AV_TIME_BASE), 0);
3198 case SDL_VIDEOEXPOSE:
3199 cur_stream->force_refresh = 1;
3201 case SDL_MOUSEBUTTONDOWN:
3202 if (exit_on_mousedown) {
3203 do_exit(cur_stream);
3206 case SDL_MOUSEMOTION:
3207 if (cursor_hidden) {
3211 cursor_last_shown = av_gettime();
3212 if (event.type == SDL_MOUSEBUTTONDOWN) {
3215 if (event.motion.state != SDL_PRESSED)
3219 if (seek_by_bytes || cur_stream->ic->duration <= 0) {
3220 uint64_t size = avio_size(cur_stream->ic->pb);
3221 stream_seek(cur_stream, size*x/cur_stream->width, 0, 1);
3225 int tns, thh, tmm, tss;
3226 tns = cur_stream->ic->duration / 1000000LL;
3228 tmm = (tns % 3600) / 60;
3230 frac = x / cur_stream->width;
3233 mm = (ns % 3600) / 60;
3235 fprintf(stderr, "Seek to %2.0f%% (%2d:%02d:%02d) of total duration (%2d:%02d:%02d) \n", frac*100,
3236 hh, mm, ss, thh, tmm, tss);
3237 ts = frac * cur_stream->ic->duration;
3238 if (cur_stream->ic->start_time != AV_NOPTS_VALUE)
3239 ts += cur_stream->ic->start_time;
3240 stream_seek(cur_stream, ts, 0, 0);
3243 case SDL_VIDEORESIZE:
3244 screen = SDL_SetVideoMode(event.resize.w, event.resize.h, 0,
3245 SDL_HWSURFACE|SDL_RESIZABLE|SDL_ASYNCBLIT|SDL_HWACCEL);
3246 screen_width = cur_stream->width = event.resize.w;
3247 screen_height = cur_stream->height = event.resize.h;
3248 cur_stream->force_refresh = 1;
3252 do_exit(cur_stream);
3254 case FF_ALLOC_EVENT:
3255 alloc_picture(event.user.data1);
3263 static int opt_frame_size(void *optctx, const char *opt, const char *arg)
3265 av_log(NULL, AV_LOG_WARNING, "Option -s is deprecated, use -video_size.\n");
3266 return opt_default(NULL, "video_size", arg);
3269 static int opt_width(void *optctx, const char *opt, const char *arg)
3271 screen_width = parse_number_or_die(opt, arg, OPT_INT64, 1, INT_MAX);
3275 static int opt_height(void *optctx, const char *opt, const char *arg)
3277 screen_height = parse_number_or_die(opt, arg, OPT_INT64, 1, INT_MAX);
3281 static int opt_format(void *optctx, const char *opt, const char *arg)
3283 file_iformat = av_find_input_format(arg);
3284 if (!file_iformat) {
3285 fprintf(stderr, "Unknown input format: %s\n", arg);
3286 return AVERROR(EINVAL);
3291 static int opt_frame_pix_fmt(void *optctx, const char *opt, const char *arg)
3293 av_log(NULL, AV_LOG_WARNING, "Option -pix_fmt is deprecated, use -pixel_format.\n");
3294 return opt_default(NULL, "pixel_format", arg);
3297 static int opt_sync(void *optctx, const char *opt, const char *arg)
3299 if (!strcmp(arg, "audio"))
3300 av_sync_type = AV_SYNC_AUDIO_MASTER;
3301 else if (!strcmp(arg, "video"))
3302 av_sync_type = AV_SYNC_VIDEO_MASTER;
3303 else if (!strcmp(arg, "ext"))
3304 av_sync_type = AV_SYNC_EXTERNAL_CLOCK;
3306 fprintf(stderr, "Unknown value for %s: %s\n", opt, arg);
3312 static int opt_seek(void *optctx, const char *opt, const char *arg)
3314 start_time = parse_time_or_die(opt, arg, 1);
3318 static int opt_duration(void *optctx, const char *opt, const char *arg)
3320 duration = parse_time_or_die(opt, arg, 1);
3324 static int opt_show_mode(void *optctx, const char *opt, const char *arg)
3326 show_mode = !strcmp(arg, "video") ? SHOW_MODE_VIDEO :
3327 !strcmp(arg, "waves") ? SHOW_MODE_WAVES :
3328 !strcmp(arg, "rdft" ) ? SHOW_MODE_RDFT :
3329 parse_number_or_die(opt, arg, OPT_INT, 0, SHOW_MODE_NB-1);
3333 static void opt_input_file(void *optctx, const char *filename)
3335 if (input_filename) {
3336 fprintf(stderr, "Argument '%s' provided as input filename, but '%s' was already specified.\n",
3337 filename, input_filename);
3340 if (!strcmp(filename, "-"))
3342 input_filename = filename;
3345 static int opt_codec(void *optctx, const char *opt, const char *arg)
3347 const char *spec = strchr(opt, ':');
3349 fprintf(stderr, "No media specifier was specified in '%s' in option '%s'\n",
3351 return AVERROR(EINVAL);
3355 case 'a' : audio_codec_name = arg; break;
3356 case 's' : subtitle_codec_name = arg; break;
3357 case 'v' : video_codec_name = arg; break;
3359 fprintf(stderr, "Invalid media specifier '%s' in option '%s'\n", spec, opt);
3360 return AVERROR(EINVAL);
3367 static const OptionDef options[] = {
3368 #include "cmdutils_common_opts.h"
3369 { "x", HAS_ARG, { .func_arg = opt_width }, "force displayed width", "width" },
3370 { "y", HAS_ARG, { .func_arg = opt_height }, "force displayed height", "height" },
3371 { "s", HAS_ARG | OPT_VIDEO, { .func_arg = opt_frame_size }, "set frame size (WxH or abbreviation)", "size" },
3372 { "fs", OPT_BOOL, { &is_full_screen }, "force full screen" },
3373 { "an", OPT_BOOL, { &audio_disable }, "disable audio" },
3374 { "vn", OPT_BOOL, { &video_disable }, "disable video" },
3375 { "sn", OPT_BOOL, { &subtitle_disable }, "disable subtitling" },
3376 { "ast", OPT_INT | HAS_ARG | OPT_EXPERT, { &wanted_stream[AVMEDIA_TYPE_AUDIO] }, "select desired audio stream", "stream_number" },
3377 { "vst", OPT_INT | HAS_ARG | OPT_EXPERT, { &wanted_stream[AVMEDIA_TYPE_VIDEO] }, "select desired video stream", "stream_number" },
3378 { "sst", OPT_INT | HAS_ARG | OPT_EXPERT, { &wanted_stream[AVMEDIA_TYPE_SUBTITLE] }, "select desired subtitle stream", "stream_number" },
3379 { "ss", HAS_ARG, { .func_arg = opt_seek }, "seek to a given position in seconds", "pos" },
3380 { "t", HAS_ARG, { .func_arg = opt_duration }, "play \"duration\" seconds of audio/video", "duration" },
3381 { "bytes", OPT_INT | HAS_ARG, { &seek_by_bytes }, "seek by bytes 0=off 1=on -1=auto", "val" },
3382 { "nodisp", OPT_BOOL, { &display_disable }, "disable graphical display" },
3383 { "f", HAS_ARG, { .func_arg = opt_format }, "force format", "fmt" },
3384 { "pix_fmt", HAS_ARG | OPT_EXPERT | OPT_VIDEO, { .func_arg = opt_frame_pix_fmt }, "set pixel format", "format" },
3385 { "stats", OPT_BOOL | OPT_EXPERT, { &show_status }, "show status", "" },
3386 { "bug", OPT_INT | HAS_ARG | OPT_EXPERT, { &workaround_bugs }, "workaround bugs", "" },
3387 { "fast", OPT_BOOL | OPT_EXPERT, { &fast }, "non spec compliant optimizations", "" },
3388 { "genpts", OPT_BOOL | OPT_EXPERT, { &genpts }, "generate pts", "" },
3389 { "drp", OPT_INT | HAS_ARG | OPT_EXPERT, { &decoder_reorder_pts }, "let decoder reorder pts 0=off 1=on -1=auto", ""},
3390 { "lowres", OPT_INT | HAS_ARG | OPT_EXPERT, { &lowres }, "", "" },
3391 { "idct", OPT_INT | HAS_ARG | OPT_EXPERT, { &idct }, "set idct algo", "algo" },
3392 { "ec", OPT_INT | HAS_ARG | OPT_EXPERT, { &error_concealment }, "set error concealment options", "bit_mask" },
3393 { "sync", HAS_ARG | OPT_EXPERT, { .func_arg = opt_sync }, "set audio-video sync. type (type=audio/video/ext)", "type" },
3394 { "autoexit", OPT_BOOL | OPT_EXPERT, { &autoexit }, "exit at the end", "" },
3395 { "exitonkeydown", OPT_BOOL | OPT_EXPERT, { &exit_on_keydown }, "exit on key down", "" },
3396 { "exitonmousedown", OPT_BOOL | OPT_EXPERT, { &exit_on_mousedown }, "exit on mouse down", "" },
3397 { "loop", OPT_INT | HAS_ARG | OPT_EXPERT, { &loop }, "set number of times the playback shall be looped", "loop count" },
3398 { "framedrop", OPT_BOOL | OPT_EXPERT, { &framedrop }, "drop frames when cpu is too slow", "" },
3399 { "infbuf", OPT_BOOL | OPT_EXPERT, { &infinite_buffer }, "don't limit the input buffer size (useful with realtime streams)", "" },
3400 { "window_title", OPT_STRING | HAS_ARG, { &window_title }, "set window title", "window title" },
3402 { "vf", OPT_STRING | HAS_ARG, { &vfilters }, "set video filters", "filter_graph" },
3403 { "af", OPT_STRING | HAS_ARG, { &afilters }, "set audio filters", "filter_graph" },
3405 { "rdftspeed", OPT_INT | HAS_ARG| OPT_AUDIO | OPT_EXPERT, { &rdftspeed }, "rdft speed", "msecs" },
3406 { "showmode", HAS_ARG, { .func_arg = opt_show_mode}, "select show mode (0 = video, 1 = waves, 2 = RDFT)", "mode" },
3407 { "default", HAS_ARG | OPT_AUDIO | OPT_VIDEO | OPT_EXPERT, { .func_arg = opt_default }, "generic catch all option", "" },
3408 { "i", OPT_BOOL, { &dummy}, "read specified file", "input_file"},
3409 { "codec", HAS_ARG, { .func_arg = opt_codec}, "force decoder", "decoder_name" },
3410 { "acodec", HAS_ARG | OPT_STRING | OPT_EXPERT, { &audio_codec_name }, "force audio decoder", "decoder_name" },
3411 { "scodec", HAS_ARG | OPT_STRING | OPT_EXPERT, { &subtitle_codec_name }, "force subtitle decoder", "decoder_name" },
3412 { "vcodec", HAS_ARG | OPT_STRING | OPT_EXPERT, { &video_codec_name }, "force video decoder", "decoder_name" },
3416 static void show_usage(void)
3418 av_log(NULL, AV_LOG_INFO, "Simple media player\n");
3419 av_log(NULL, AV_LOG_INFO, "usage: %s [options] input_file\n", program_name);
3420 av_log(NULL, AV_LOG_INFO, "\n");
3423 void show_help_default(const char *opt, const char *arg)
3425 av_log_set_callback(log_callback_help);
3427 show_help_options(options, "Main options:", 0, OPT_EXPERT, 0);
3428 show_help_options(options, "Advanced options:", OPT_EXPERT, 0, 0);
3430 show_help_children(avcodec_get_class(), AV_OPT_FLAG_DECODING_PARAM);
3431 show_help_children(avformat_get_class(), AV_OPT_FLAG_DECODING_PARAM);
3432 #if !CONFIG_AVFILTER
3433 show_help_children(sws_get_class(), AV_OPT_FLAG_ENCODING_PARAM);
3435 show_help_children(avfilter_get_class(), AV_OPT_FLAG_FILTERING_PARAM);
3437 printf("\nWhile playing:\n"
3439 "f toggle full screen\n"
3441 "a cycle audio channel\n"
3442 "v cycle video channel\n"
3443 "t cycle subtitle channel\n"
3444 "w show audio waves\n"
3445 "s activate frame-step mode\n"
3446 "left/right seek backward/forward 10 seconds\n"
3447 "down/up seek backward/forward 1 minute\n"
3448 "page down/page up seek backward/forward 10 minutes\n"
3449 "mouse click seek to percentage in file corresponding to fraction of width\n"
3453 static int lockmgr(void **mtx, enum AVLockOp op)
3456 case AV_LOCK_CREATE:
3457 *mtx = SDL_CreateMutex();
3461 case AV_LOCK_OBTAIN:
3462 return !!SDL_LockMutex(*mtx);
3463 case AV_LOCK_RELEASE:
3464 return !!SDL_UnlockMutex(*mtx);
3465 case AV_LOCK_DESTROY:
3466 SDL_DestroyMutex(*mtx);
3472 /* Called from the main */
3473 int main(int argc, char **argv)
3477 char dummy_videodriver[] = "SDL_VIDEODRIVER=dummy";
3479 av_log_set_flags(AV_LOG_SKIP_REPEATED);
3480 parse_loglevel(argc, argv, options);
3482 /* register all codecs, demux and protocols */
3483 avcodec_register_all();
3485 avdevice_register_all();
3488 avfilter_register_all();
3491 avformat_network_init();
3495 signal(SIGINT , sigterm_handler); /* Interrupt (ANSI). */
3496 signal(SIGTERM, sigterm_handler); /* Termination (ANSI). */
3498 show_banner(argc, argv, options);
3500 parse_options(NULL, argc, argv, options, opt_input_file);
3502 if (!input_filename) {
3504 fprintf(stderr, "An input file must be specified\n");
3505 fprintf(stderr, "Use -h to get full help or, even better, run 'man %s'\n", program_name);
3509 if (display_disable) {
3512 flags = SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER;
3514 flags &= ~SDL_INIT_AUDIO;
3515 if (display_disable)
3516 SDL_putenv(dummy_videodriver); /* For the event queue, we always need a video driver. */
3517 #if !defined(__MINGW32__) && !defined(__APPLE__)
3518 flags |= SDL_INIT_EVENTTHREAD; /* Not supported on Windows or Mac OS X */
3520 if (SDL_Init (flags)) {
3521 fprintf(stderr, "Could not initialize SDL - %s\n", SDL_GetError());
3522 fprintf(stderr, "(Did you set the DISPLAY variable?)\n");
3526 if (!display_disable) {
3527 const SDL_VideoInfo *vi = SDL_GetVideoInfo();
3528 fs_screen_width = vi->current_w;
3529 fs_screen_height = vi->current_h;
3532 SDL_EventState(SDL_ACTIVEEVENT, SDL_IGNORE);
3533 SDL_EventState(SDL_SYSWMEVENT, SDL_IGNORE);
3534 SDL_EventState(SDL_USEREVENT, SDL_IGNORE);
3536 if (av_lockmgr_register(lockmgr)) {
3537 fprintf(stderr, "Could not initialize lock manager!\n");
3541 av_init_packet(&flush_pkt);
3542 flush_pkt.data = (char *)(intptr_t)"FLUSH";
3544 is = stream_open(input_filename, file_iformat);
3546 fprintf(stderr, "Failed to initialize VideoState!\n");