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 int audio_buf_frames_pending;
192 AVPacket audio_pkt_temp;
194 int audio_pkt_temp_serial;
195 int audio_last_serial;
196 struct AudioParams audio_src;
198 struct AudioParams audio_filter_src;
200 struct AudioParams audio_tgt;
201 struct SwrContext *swr_ctx;
202 double audio_current_pts;
203 double audio_current_pts_drift;
204 int frame_drops_early;
205 int frame_drops_late;
209 SHOW_MODE_NONE = -1, SHOW_MODE_VIDEO = 0, SHOW_MODE_WAVES, SHOW_MODE_RDFT, SHOW_MODE_NB
211 int16_t sample_array[SAMPLE_ARRAY_SIZE];
212 int sample_array_index;
216 FFTSample *rdft_data;
218 double last_vis_time;
220 SDL_Thread *subtitle_tid;
222 int subtitle_stream_changed;
223 AVStream *subtitle_st;
224 PacketQueue subtitleq;
225 SubPicture subpq[SUBPICTURE_QUEUE_SIZE];
226 int subpq_size, subpq_rindex, subpq_windex;
227 SDL_mutex *subpq_mutex;
228 SDL_cond *subpq_cond;
231 double frame_last_pts;
232 double frame_last_duration;
233 double frame_last_dropped_pts;
234 double frame_last_returned_time;
235 double frame_last_filter_delay;
236 int64_t frame_last_dropped_pos;
237 int frame_last_dropped_serial;
241 double video_current_pts; // current displayed pts
242 double video_current_pts_drift; // video_current_pts - time (av_gettime) at which we updated video_current_pts - used to have running video pts
243 int64_t video_current_pos; // current displayed file pos
244 double max_frame_duration; // maximum duration of a frame - above this, we consider the jump a timestamp discontinuity
245 int video_clock_serial;
246 VideoPicture pictq[VIDEO_PICTURE_QUEUE_SIZE];
247 int pictq_size, pictq_rindex, pictq_windex;
248 SDL_mutex *pictq_mutex;
249 SDL_cond *pictq_cond;
251 struct SwsContext *img_convert_ctx;
253 SDL_Rect last_display_rect;
256 int width, height, xleft, ytop;
260 AVFilterContext *in_video_filter; // the first filter in the video chain
261 AVFilterContext *out_video_filter; // the last filter in the video chain
262 AVFilterContext *in_audio_filter; // the first filter in the audio chain
263 AVFilterContext *out_audio_filter; // the last filter in the audio chain
264 AVFilterGraph *agraph; // audio filter graph
267 int last_video_stream, last_audio_stream, last_subtitle_stream;
269 SDL_cond *continue_read_thread;
272 /* options specified by the user */
273 static AVInputFormat *file_iformat;
274 static const char *input_filename;
275 static const char *window_title;
276 static int fs_screen_width;
277 static int fs_screen_height;
278 static int default_width = 640;
279 static int default_height = 480;
280 static int screen_width = 0;
281 static int screen_height = 0;
282 static int audio_disable;
283 static int video_disable;
284 static int subtitle_disable;
285 static int wanted_stream[AVMEDIA_TYPE_NB] = {
286 [AVMEDIA_TYPE_AUDIO] = -1,
287 [AVMEDIA_TYPE_VIDEO] = -1,
288 [AVMEDIA_TYPE_SUBTITLE] = -1,
290 static int seek_by_bytes = -1;
291 static int display_disable;
292 static int show_status = 1;
293 static int av_sync_type = AV_SYNC_AUDIO_MASTER;
294 static int64_t start_time = AV_NOPTS_VALUE;
295 static int64_t duration = AV_NOPTS_VALUE;
296 static int workaround_bugs = 1;
298 static int genpts = 0;
299 static int lowres = 0;
300 static int idct = FF_IDCT_AUTO;
301 static int error_concealment = 3;
302 static int decoder_reorder_pts = -1;
304 static int exit_on_keydown;
305 static int exit_on_mousedown;
307 static int framedrop = -1;
308 static int infinite_buffer = -1;
309 static enum ShowMode show_mode = SHOW_MODE_NONE;
310 static const char *audio_codec_name;
311 static const char *subtitle_codec_name;
312 static const char *video_codec_name;
313 double rdftspeed = 0.02;
314 static int64_t cursor_last_shown;
315 static int cursor_hidden = 0;
317 static char *vfilters = NULL;
318 static char *afilters = NULL;
321 /* current context */
322 static int is_full_screen;
323 static int64_t audio_callback_time;
325 static AVPacket flush_pkt;
327 #define FF_ALLOC_EVENT (SDL_USEREVENT)
328 #define FF_QUIT_EVENT (SDL_USEREVENT + 2)
330 static SDL_Surface *screen;
333 int cmp_audio_fmts(enum AVSampleFormat fmt1, int64_t channel_count1,
334 enum AVSampleFormat fmt2, int64_t channel_count2)
336 /* If channel count == 1, planar and non-planar formats are the same */
337 if (channel_count1 == 1 && channel_count2 == 1)
338 return av_get_packed_sample_fmt(fmt1) != av_get_packed_sample_fmt(fmt2);
340 return channel_count1 != channel_count2 || fmt1 != fmt2;
344 int64_t get_valid_channel_layout(int64_t channel_layout, int channels)
346 if (channel_layout && av_get_channel_layout_nb_channels(channel_layout) == channels)
347 return channel_layout;
352 static int packet_queue_put(PacketQueue *q, AVPacket *pkt);
354 static int packet_queue_put_private(PacketQueue *q, AVPacket *pkt)
356 MyAVPacketList *pkt1;
358 if (q->abort_request)
361 pkt1 = av_malloc(sizeof(MyAVPacketList));
366 if (pkt == &flush_pkt)
368 pkt1->serial = q->serial;
373 q->last_pkt->next = pkt1;
376 q->size += pkt1->pkt.size + sizeof(*pkt1);
377 /* XXX: should duplicate packet data in DV case */
378 SDL_CondSignal(q->cond);
382 static int packet_queue_put(PacketQueue *q, AVPacket *pkt)
386 /* duplicate the packet */
387 if (pkt != &flush_pkt && av_dup_packet(pkt) < 0)
390 SDL_LockMutex(q->mutex);
391 ret = packet_queue_put_private(q, pkt);
392 SDL_UnlockMutex(q->mutex);
394 if (pkt != &flush_pkt && ret < 0)
400 /* packet queue handling */
401 static void packet_queue_init(PacketQueue *q)
403 memset(q, 0, sizeof(PacketQueue));
404 q->mutex = SDL_CreateMutex();
405 q->cond = SDL_CreateCond();
406 q->abort_request = 1;
409 static void packet_queue_flush(PacketQueue *q)
411 MyAVPacketList *pkt, *pkt1;
413 SDL_LockMutex(q->mutex);
414 for (pkt = q->first_pkt; pkt != NULL; pkt = pkt1) {
416 av_free_packet(&pkt->pkt);
423 SDL_UnlockMutex(q->mutex);
426 static void packet_queue_destroy(PacketQueue *q)
428 packet_queue_flush(q);
429 SDL_DestroyMutex(q->mutex);
430 SDL_DestroyCond(q->cond);
433 static void packet_queue_abort(PacketQueue *q)
435 SDL_LockMutex(q->mutex);
437 q->abort_request = 1;
439 SDL_CondSignal(q->cond);
441 SDL_UnlockMutex(q->mutex);
444 static void packet_queue_start(PacketQueue *q)
446 SDL_LockMutex(q->mutex);
447 q->abort_request = 0;
448 packet_queue_put_private(q, &flush_pkt);
449 SDL_UnlockMutex(q->mutex);
452 /* return < 0 if aborted, 0 if no packet and > 0 if packet. */
453 static int packet_queue_get(PacketQueue *q, AVPacket *pkt, int block, int *serial)
455 MyAVPacketList *pkt1;
458 SDL_LockMutex(q->mutex);
461 if (q->abort_request) {
468 q->first_pkt = pkt1->next;
472 q->size -= pkt1->pkt.size + sizeof(*pkt1);
475 *serial = pkt1->serial;
483 SDL_CondWait(q->cond, q->mutex);
486 SDL_UnlockMutex(q->mutex);
490 static inline void fill_rectangle(SDL_Surface *screen,
491 int x, int y, int w, int h, int color, int update)
498 SDL_FillRect(screen, &rect, color);
499 if (update && w > 0 && h > 0)
500 SDL_UpdateRect(screen, x, y, w, h);
503 /* draw only the border of a rectangle */
504 static void fill_border(int xleft, int ytop, int width, int height, int x, int y, int w, int h, int color, int update)
508 /* fill the background */
512 w2 = width - (x + w);
518 h2 = height - (y + h);
521 fill_rectangle(screen,
525 fill_rectangle(screen,
526 xleft + width - w2, ytop,
529 fill_rectangle(screen,
533 fill_rectangle(screen,
534 xleft + w1, ytop + height - h2,
539 #define ALPHA_BLEND(a, oldp, newp, s)\
540 ((((oldp << s) * (255 - (a))) + (newp * (a))) / (255 << s))
542 #define RGBA_IN(r, g, b, a, s)\
544 unsigned int v = ((const uint32_t *)(s))[0];\
545 a = (v >> 24) & 0xff;\
546 r = (v >> 16) & 0xff;\
547 g = (v >> 8) & 0xff;\
551 #define YUVA_IN(y, u, v, a, s, pal)\
553 unsigned int val = ((const uint32_t *)(pal))[*(const uint8_t*)(s)];\
554 a = (val >> 24) & 0xff;\
555 y = (val >> 16) & 0xff;\
556 u = (val >> 8) & 0xff;\
560 #define YUVA_OUT(d, y, u, v, a)\
562 ((uint32_t *)(d))[0] = (a << 24) | (y << 16) | (u << 8) | v;\
568 static void blend_subrect(AVPicture *dst, const AVSubtitleRect *rect, int imgw, int imgh)
570 int wrap, wrap3, width2, skip2;
571 int y, u, v, a, u1, v1, a1, w, h;
572 uint8_t *lum, *cb, *cr;
575 int dstx, dsty, dstw, dsth;
577 dstw = av_clip(rect->w, 0, imgw);
578 dsth = av_clip(rect->h, 0, imgh);
579 dstx = av_clip(rect->x, 0, imgw - dstw);
580 dsty = av_clip(rect->y, 0, imgh - dsth);
581 lum = dst->data[0] + dsty * dst->linesize[0];
582 cb = dst->data[1] + (dsty >> 1) * dst->linesize[1];
583 cr = dst->data[2] + (dsty >> 1) * dst->linesize[2];
585 width2 = ((dstw + 1) >> 1) + (dstx & ~dstw & 1);
587 wrap = dst->linesize[0];
588 wrap3 = rect->pict.linesize[0];
589 p = rect->pict.data[0];
590 pal = (const uint32_t *)rect->pict.data[1]; /* Now in YCrCb! */
598 YUVA_IN(y, u, v, a, p, pal);
599 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
600 cb[0] = ALPHA_BLEND(a >> 2, cb[0], u, 0);
601 cr[0] = ALPHA_BLEND(a >> 2, cr[0], v, 0);
607 for (w = dstw - (dstx & 1); w >= 2; w -= 2) {
608 YUVA_IN(y, u, v, a, p, pal);
612 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
614 YUVA_IN(y, u, v, a, p + BPP, pal);
618 lum[1] = ALPHA_BLEND(a, lum[1], y, 0);
619 cb[0] = ALPHA_BLEND(a1 >> 2, cb[0], u1, 1);
620 cr[0] = ALPHA_BLEND(a1 >> 2, cr[0], v1, 1);
627 YUVA_IN(y, u, v, a, p, pal);
628 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
629 cb[0] = ALPHA_BLEND(a >> 2, cb[0], u, 0);
630 cr[0] = ALPHA_BLEND(a >> 2, cr[0], v, 0);
634 p += wrap3 - dstw * BPP;
635 lum += wrap - dstw - dstx;
636 cb += dst->linesize[1] - width2 - skip2;
637 cr += dst->linesize[2] - width2 - skip2;
639 for (h = dsth - (dsty & 1); h >= 2; h -= 2) {
645 YUVA_IN(y, u, v, a, p, pal);
649 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
652 YUVA_IN(y, u, v, a, p, pal);
656 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
657 cb[0] = ALPHA_BLEND(a1 >> 2, cb[0], u1, 1);
658 cr[0] = ALPHA_BLEND(a1 >> 2, cr[0], v1, 1);
664 for (w = dstw - (dstx & 1); w >= 2; w -= 2) {
665 YUVA_IN(y, u, v, a, p, pal);
669 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
671 YUVA_IN(y, u, v, a, p + BPP, pal);
675 lum[1] = ALPHA_BLEND(a, lum[1], y, 0);
679 YUVA_IN(y, u, v, a, p, pal);
683 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
685 YUVA_IN(y, u, v, a, p + BPP, pal);
689 lum[1] = ALPHA_BLEND(a, lum[1], y, 0);
691 cb[0] = ALPHA_BLEND(a1 >> 2, cb[0], u1, 2);
692 cr[0] = ALPHA_BLEND(a1 >> 2, cr[0], v1, 2);
696 p += -wrap3 + 2 * BPP;
700 YUVA_IN(y, u, v, a, p, pal);
704 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
707 YUVA_IN(y, u, v, a, p, pal);
711 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
712 cb[0] = ALPHA_BLEND(a1 >> 2, cb[0], u1, 1);
713 cr[0] = ALPHA_BLEND(a1 >> 2, cr[0], v1, 1);
719 p += wrap3 + (wrap3 - dstw * BPP);
720 lum += wrap + (wrap - dstw - dstx);
721 cb += dst->linesize[1] - width2 - skip2;
722 cr += dst->linesize[2] - width2 - skip2;
724 /* handle odd height */
731 YUVA_IN(y, u, v, a, p, pal);
732 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
733 cb[0] = ALPHA_BLEND(a >> 2, cb[0], u, 0);
734 cr[0] = ALPHA_BLEND(a >> 2, cr[0], v, 0);
740 for (w = dstw - (dstx & 1); w >= 2; w -= 2) {
741 YUVA_IN(y, u, v, a, p, pal);
745 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
747 YUVA_IN(y, u, v, a, p + BPP, pal);
751 lum[1] = ALPHA_BLEND(a, lum[1], y, 0);
752 cb[0] = ALPHA_BLEND(a1 >> 2, cb[0], u, 1);
753 cr[0] = ALPHA_BLEND(a1 >> 2, cr[0], v, 1);
760 YUVA_IN(y, u, v, a, p, pal);
761 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
762 cb[0] = ALPHA_BLEND(a >> 2, cb[0], u, 0);
763 cr[0] = ALPHA_BLEND(a >> 2, cr[0], v, 0);
768 static void free_subpicture(SubPicture *sp)
770 avsubtitle_free(&sp->sub);
773 static void calculate_display_rect(SDL_Rect *rect, int scr_xleft, int scr_ytop, int scr_width, int scr_height, VideoPicture *vp)
776 int width, height, x, y;
778 if (vp->sar.num == 0)
781 aspect_ratio = av_q2d(vp->sar);
783 if (aspect_ratio <= 0.0)
785 aspect_ratio *= (float)vp->width / (float)vp->height;
787 /* XXX: we suppose the screen has a 1.0 pixel ratio */
789 width = ((int)rint(height * aspect_ratio)) & ~1;
790 if (width > scr_width) {
792 height = ((int)rint(width / aspect_ratio)) & ~1;
794 x = (scr_width - width) / 2;
795 y = (scr_height - height) / 2;
796 rect->x = scr_xleft + x;
797 rect->y = scr_ytop + y;
798 rect->w = FFMAX(width, 1);
799 rect->h = FFMAX(height, 1);
802 static void video_image_display(VideoState *is)
810 vp = &is->pictq[is->pictq_rindex];
812 if (is->subtitle_st) {
813 if (is->subpq_size > 0) {
814 sp = &is->subpq[is->subpq_rindex];
816 if (vp->pts >= sp->pts + ((float) sp->sub.start_display_time / 1000)) {
817 SDL_LockYUVOverlay (vp->bmp);
819 pict.data[0] = vp->bmp->pixels[0];
820 pict.data[1] = vp->bmp->pixels[2];
821 pict.data[2] = vp->bmp->pixels[1];
823 pict.linesize[0] = vp->bmp->pitches[0];
824 pict.linesize[1] = vp->bmp->pitches[2];
825 pict.linesize[2] = vp->bmp->pitches[1];
827 for (i = 0; i < sp->sub.num_rects; i++)
828 blend_subrect(&pict, sp->sub.rects[i],
829 vp->bmp->w, vp->bmp->h);
831 SDL_UnlockYUVOverlay (vp->bmp);
836 calculate_display_rect(&rect, is->xleft, is->ytop, is->width, is->height, vp);
838 SDL_DisplayYUVOverlay(vp->bmp, &rect);
840 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) {
841 int bgcolor = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00);
842 fill_border(is->xleft, is->ytop, is->width, is->height, rect.x, rect.y, rect.w, rect.h, bgcolor, 1);
843 is->last_display_rect = rect;
848 static inline int compute_mod(int a, int b)
850 return a < 0 ? a%b + b : a%b;
853 static void video_audio_display(VideoState *s)
855 int i, i_start, x, y1, y, ys, delay, n, nb_display_channels;
856 int ch, channels, h, h2, bgcolor, fgcolor;
858 int rdft_bits, nb_freq;
860 for (rdft_bits = 1; (1 << rdft_bits) < 2 * s->height; rdft_bits++)
862 nb_freq = 1 << (rdft_bits - 1);
864 /* compute display index : center on currently output samples */
865 channels = s->audio_tgt.channels;
866 nb_display_channels = channels;
868 int data_used= s->show_mode == SHOW_MODE_WAVES ? s->width : (2*nb_freq);
870 delay = s->audio_write_buf_size;
873 /* to be more precise, we take into account the time spent since
874 the last buffer computation */
875 if (audio_callback_time) {
876 time_diff = av_gettime() - audio_callback_time;
877 delay -= (time_diff * s->audio_tgt.freq) / 1000000;
880 delay += 2 * data_used;
881 if (delay < data_used)
884 i_start= x = compute_mod(s->sample_array_index - delay * channels, SAMPLE_ARRAY_SIZE);
885 if (s->show_mode == SHOW_MODE_WAVES) {
887 for (i = 0; i < 1000; i += channels) {
888 int idx = (SAMPLE_ARRAY_SIZE + x - i) % SAMPLE_ARRAY_SIZE;
889 int a = s->sample_array[idx];
890 int b = s->sample_array[(idx + 4 * channels) % SAMPLE_ARRAY_SIZE];
891 int c = s->sample_array[(idx + 5 * channels) % SAMPLE_ARRAY_SIZE];
892 int d = s->sample_array[(idx + 9 * channels) % SAMPLE_ARRAY_SIZE];
894 if (h < score && (b ^ c) < 0) {
901 s->last_i_start = i_start;
903 i_start = s->last_i_start;
906 bgcolor = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00);
907 if (s->show_mode == SHOW_MODE_WAVES) {
908 fill_rectangle(screen,
909 s->xleft, s->ytop, s->width, s->height,
912 fgcolor = SDL_MapRGB(screen->format, 0xff, 0xff, 0xff);
914 /* total height for one channel */
915 h = s->height / nb_display_channels;
916 /* graph height / 2 */
918 for (ch = 0; ch < nb_display_channels; ch++) {
920 y1 = s->ytop + ch * h + (h / 2); /* position of center line */
921 for (x = 0; x < s->width; x++) {
922 y = (s->sample_array[i] * h2) >> 15;
929 fill_rectangle(screen,
930 s->xleft + x, ys, 1, y,
933 if (i >= SAMPLE_ARRAY_SIZE)
934 i -= SAMPLE_ARRAY_SIZE;
938 fgcolor = SDL_MapRGB(screen->format, 0x00, 0x00, 0xff);
940 for (ch = 1; ch < nb_display_channels; ch++) {
941 y = s->ytop + ch * h;
942 fill_rectangle(screen,
943 s->xleft, y, s->width, 1,
946 SDL_UpdateRect(screen, s->xleft, s->ytop, s->width, s->height);
948 nb_display_channels= FFMIN(nb_display_channels, 2);
949 if (rdft_bits != s->rdft_bits) {
950 av_rdft_end(s->rdft);
951 av_free(s->rdft_data);
952 s->rdft = av_rdft_init(rdft_bits, DFT_R2C);
953 s->rdft_bits = rdft_bits;
954 s->rdft_data = av_malloc(4 * nb_freq * sizeof(*s->rdft_data));
958 for (ch = 0; ch < nb_display_channels; ch++) {
959 data[ch] = s->rdft_data + 2 * nb_freq * ch;
961 for (x = 0; x < 2 * nb_freq; x++) {
962 double w = (x-nb_freq) * (1.0 / nb_freq);
963 data[ch][x] = s->sample_array[i] * (1.0 - w * w);
965 if (i >= SAMPLE_ARRAY_SIZE)
966 i -= SAMPLE_ARRAY_SIZE;
968 av_rdft_calc(s->rdft, data[ch]);
970 // least efficient way to do this, we should of course directly access it but its more than fast enough
971 for (y = 0; y < s->height; y++) {
972 double w = 1 / sqrt(nb_freq);
973 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]));
974 int b = (nb_display_channels == 2 ) ? sqrt(w * sqrt(data[1][2 * y + 0] * data[1][2 * y + 0]
975 + data[1][2 * y + 1] * data[1][2 * y + 1])) : a;
978 fgcolor = SDL_MapRGB(screen->format, a, b, (a + b) / 2);
980 fill_rectangle(screen,
981 s->xpos, s->height-y, 1, 1,
985 SDL_UpdateRect(screen, s->xpos, s->ytop, 1, s->height);
988 if (s->xpos >= s->width)
993 static void stream_close(VideoState *is)
997 /* XXX: use a special url_shutdown call to abort parse cleanly */
998 is->abort_request = 1;
999 SDL_WaitThread(is->read_tid, NULL);
1000 packet_queue_destroy(&is->videoq);
1001 packet_queue_destroy(&is->audioq);
1002 packet_queue_destroy(&is->subtitleq);
1004 /* free all pictures */
1005 for (i = 0; i < VIDEO_PICTURE_QUEUE_SIZE; i++) {
1008 SDL_FreeYUVOverlay(vp->bmp);
1012 SDL_DestroyMutex(is->pictq_mutex);
1013 SDL_DestroyCond(is->pictq_cond);
1014 SDL_DestroyMutex(is->subpq_mutex);
1015 SDL_DestroyCond(is->subpq_cond);
1016 SDL_DestroyCond(is->continue_read_thread);
1017 #if !CONFIG_AVFILTER
1018 sws_freeContext(is->img_convert_ctx);
1023 static void do_exit(VideoState *is)
1028 av_lockmgr_register(NULL);
1032 av_freep(&vfilters);
1034 avformat_network_deinit();
1038 av_log(NULL, AV_LOG_QUIET, "%s", "");
1042 static void sigterm_handler(int sig)
1047 static int video_open(VideoState *is, int force_set_video_mode, VideoPicture *vp)
1049 int flags = SDL_HWSURFACE | SDL_ASYNCBLIT | SDL_HWACCEL;
1053 if (is_full_screen) flags |= SDL_FULLSCREEN;
1054 else flags |= SDL_RESIZABLE;
1056 if (vp && vp->width) {
1057 calculate_display_rect(&rect, 0, 0, INT_MAX, vp->height, vp);
1058 default_width = rect.w;
1059 default_height = rect.h;
1062 if (is_full_screen && fs_screen_width) {
1063 w = fs_screen_width;
1064 h = fs_screen_height;
1065 } else if (!is_full_screen && screen_width) {
1072 if (screen && is->width == screen->w && screen->w == w
1073 && is->height== screen->h && screen->h == h && !force_set_video_mode)
1075 screen = SDL_SetVideoMode(w, h, 0, flags);
1077 fprintf(stderr, "SDL: could not set video mode - exiting\n");
1081 window_title = input_filename;
1082 SDL_WM_SetCaption(window_title, window_title);
1084 is->width = screen->w;
1085 is->height = screen->h;
1090 /* display the current picture, if any */
1091 static void video_display(VideoState *is)
1094 video_open(is, 0, NULL);
1095 if (is->audio_st && is->show_mode != SHOW_MODE_VIDEO)
1096 video_audio_display(is);
1097 else if (is->video_st)
1098 video_image_display(is);
1101 /* get the current audio clock value */
1102 static double get_audio_clock(VideoState *is)
1104 if (is->audio_clock_serial != is->audioq.serial)
1107 return is->audio_current_pts;
1109 return is->audio_current_pts_drift + av_gettime() / 1000000.0;
1113 /* get the current video clock value */
1114 static double get_video_clock(VideoState *is)
1116 if (is->video_clock_serial != is->videoq.serial)
1119 return is->video_current_pts;
1121 return is->video_current_pts_drift + av_gettime() / 1000000.0;
1125 /* get the current external clock value */
1126 static double get_external_clock(VideoState *is)
1129 return is->external_clock;
1131 double time = av_gettime() / 1000000.0;
1132 return is->external_clock_drift + time - (time - is->external_clock_time / 1000000.0) * (1.0 - is->external_clock_speed);
1136 static int get_master_sync_type(VideoState *is) {
1137 if (is->av_sync_type == AV_SYNC_VIDEO_MASTER) {
1139 return AV_SYNC_VIDEO_MASTER;
1141 return AV_SYNC_AUDIO_MASTER;
1142 } else if (is->av_sync_type == AV_SYNC_AUDIO_MASTER) {
1144 return AV_SYNC_AUDIO_MASTER;
1146 return AV_SYNC_EXTERNAL_CLOCK;
1148 return AV_SYNC_EXTERNAL_CLOCK;
1152 /* get the current master clock value */
1153 static double get_master_clock(VideoState *is)
1157 switch (get_master_sync_type(is)) {
1158 case AV_SYNC_VIDEO_MASTER:
1159 val = get_video_clock(is);
1161 case AV_SYNC_AUDIO_MASTER:
1162 val = get_audio_clock(is);
1165 val = get_external_clock(is);
1171 static void update_external_clock_pts(VideoState *is, double pts)
1173 is->external_clock_time = av_gettime();
1174 is->external_clock = pts;
1175 is->external_clock_drift = pts - is->external_clock_time / 1000000.0;
1178 static void check_external_clock_sync(VideoState *is, double pts) {
1179 double ext_clock = get_external_clock(is);
1180 if (isnan(ext_clock) || fabs(ext_clock - pts) > AV_NOSYNC_THRESHOLD) {
1181 update_external_clock_pts(is, pts);
1185 static void update_external_clock_speed(VideoState *is, double speed) {
1186 update_external_clock_pts(is, get_external_clock(is));
1187 is->external_clock_speed = speed;
1190 static void check_external_clock_speed(VideoState *is) {
1191 if (is->video_stream >= 0 && is->videoq.nb_packets <= MIN_FRAMES / 2 ||
1192 is->audio_stream >= 0 && is->audioq.nb_packets <= MIN_FRAMES / 2) {
1193 update_external_clock_speed(is, FFMAX(EXTERNAL_CLOCK_SPEED_MIN, is->external_clock_speed - EXTERNAL_CLOCK_SPEED_STEP));
1194 } else if ((is->video_stream < 0 || is->videoq.nb_packets > MIN_FRAMES * 2) &&
1195 (is->audio_stream < 0 || is->audioq.nb_packets > MIN_FRAMES * 2)) {
1196 update_external_clock_speed(is, FFMIN(EXTERNAL_CLOCK_SPEED_MAX, is->external_clock_speed + EXTERNAL_CLOCK_SPEED_STEP));
1198 double speed = is->external_clock_speed;
1200 update_external_clock_speed(is, speed + EXTERNAL_CLOCK_SPEED_STEP * (1.0 - speed) / fabs(1.0 - speed));
1204 /* seek in the stream */
1205 static void stream_seek(VideoState *is, int64_t pos, int64_t rel, int seek_by_bytes)
1207 if (!is->seek_req) {
1210 is->seek_flags &= ~AVSEEK_FLAG_BYTE;
1212 is->seek_flags |= AVSEEK_FLAG_BYTE;
1214 SDL_CondSignal(is->continue_read_thread);
1218 /* pause or resume the video */
1219 static void stream_toggle_pause(VideoState *is)
1222 is->frame_timer += av_gettime() / 1000000.0 + is->video_current_pts_drift - is->video_current_pts;
1223 if (is->read_pause_return != AVERROR(ENOSYS)) {
1224 is->video_current_pts = is->video_current_pts_drift + av_gettime() / 1000000.0;
1226 is->video_current_pts_drift = is->video_current_pts - av_gettime() / 1000000.0;
1228 update_external_clock_pts(is, get_external_clock(is));
1229 is->paused = !is->paused;
1232 static void toggle_pause(VideoState *is)
1234 stream_toggle_pause(is);
1238 static void step_to_next_frame(VideoState *is)
1240 /* if the stream is paused unpause it, then step */
1242 stream_toggle_pause(is);
1246 static double compute_target_delay(double delay, VideoState *is)
1248 double sync_threshold, diff;
1250 /* update delay to follow master synchronisation source */
1251 if (get_master_sync_type(is) != AV_SYNC_VIDEO_MASTER) {
1252 /* if video is slave, we try to correct big delays by
1253 duplicating or deleting a frame */
1254 diff = get_video_clock(is) - get_master_clock(is);
1256 /* skip or repeat frame. We take into account the
1257 delay to compute the threshold. I still don't know
1258 if it is the best guess */
1259 sync_threshold = FFMAX(AV_SYNC_THRESHOLD, delay);
1260 if (!isnan(diff) && fabs(diff) < AV_NOSYNC_THRESHOLD) {
1261 if (diff <= -sync_threshold)
1263 else if (diff >= sync_threshold)
1268 av_dlog(NULL, "video: delay=%0.3f A-V=%f\n",
1274 static void pictq_next_picture(VideoState *is) {
1275 /* update queue size and signal for next picture */
1276 if (++is->pictq_rindex == VIDEO_PICTURE_QUEUE_SIZE)
1277 is->pictq_rindex = 0;
1279 SDL_LockMutex(is->pictq_mutex);
1281 SDL_CondSignal(is->pictq_cond);
1282 SDL_UnlockMutex(is->pictq_mutex);
1285 static int pictq_prev_picture(VideoState *is) {
1286 VideoPicture *prevvp;
1288 /* update queue size and signal for the previous picture */
1289 prevvp = &is->pictq[(is->pictq_rindex + VIDEO_PICTURE_QUEUE_SIZE - 1) % VIDEO_PICTURE_QUEUE_SIZE];
1290 if (prevvp->allocated && prevvp->serial == is->videoq.serial) {
1291 SDL_LockMutex(is->pictq_mutex);
1292 if (is->pictq_size < VIDEO_PICTURE_QUEUE_SIZE - 1) {
1293 if (--is->pictq_rindex == -1)
1294 is->pictq_rindex = VIDEO_PICTURE_QUEUE_SIZE - 1;
1298 SDL_CondSignal(is->pictq_cond);
1299 SDL_UnlockMutex(is->pictq_mutex);
1304 static void update_video_pts(VideoState *is, double pts, int64_t pos, int serial) {
1305 double time = av_gettime() / 1000000.0;
1306 /* update current video pts */
1307 is->video_current_pts = pts;
1308 is->video_current_pts_drift = is->video_current_pts - time;
1309 is->video_current_pos = pos;
1310 is->frame_last_pts = pts;
1311 is->video_clock_serial = serial;
1312 if (is->videoq.serial == serial)
1313 check_external_clock_sync(is, is->video_current_pts);
1316 /* called to display each frame */
1317 static void video_refresh(void *opaque, double *remaining_time)
1319 VideoState *is = opaque;
1323 SubPicture *sp, *sp2;
1325 if (!is->paused && get_master_sync_type(is) == AV_SYNC_EXTERNAL_CLOCK && is->realtime)
1326 check_external_clock_speed(is);
1328 if (!display_disable && is->show_mode != SHOW_MODE_VIDEO && is->audio_st) {
1329 time = av_gettime() / 1000000.0;
1330 if (is->force_refresh || is->last_vis_time + rdftspeed < time) {
1332 is->last_vis_time = time;
1334 *remaining_time = FFMIN(*remaining_time, is->last_vis_time + rdftspeed - time);
1339 if (is->force_refresh)
1340 redisplay = pictq_prev_picture(is);
1342 if (is->pictq_size == 0) {
1343 SDL_LockMutex(is->pictq_mutex);
1344 if (is->frame_last_dropped_pts != AV_NOPTS_VALUE && is->frame_last_dropped_pts > is->frame_last_pts) {
1345 update_video_pts(is, is->frame_last_dropped_pts, is->frame_last_dropped_pos, is->frame_last_dropped_serial);
1346 is->frame_last_dropped_pts = AV_NOPTS_VALUE;
1348 SDL_UnlockMutex(is->pictq_mutex);
1349 // nothing to do, no picture to display in the queue
1351 double last_duration, duration, delay;
1352 /* dequeue the picture */
1353 vp = &is->pictq[is->pictq_rindex];
1355 if (vp->serial != is->videoq.serial) {
1356 pictq_next_picture(is);
1364 /* compute nominal last_duration */
1365 last_duration = vp->pts - is->frame_last_pts;
1366 if (last_duration > 0 && last_duration < is->max_frame_duration) {
1367 /* if duration of the last frame was sane, update last_duration in video state */
1368 is->frame_last_duration = last_duration;
1370 delay = compute_target_delay(is->frame_last_duration, is);
1372 time= av_gettime()/1000000.0;
1373 if (time < is->frame_timer + delay) {
1374 *remaining_time = FFMIN(is->frame_timer + delay - time, *remaining_time);
1379 is->frame_timer += delay * FFMAX(1, floor((time-is->frame_timer) / delay));
1381 SDL_LockMutex(is->pictq_mutex);
1382 update_video_pts(is, vp->pts, vp->pos, vp->serial);
1383 SDL_UnlockMutex(is->pictq_mutex);
1385 if (is->pictq_size > 1) {
1386 VideoPicture *nextvp = &is->pictq[(is->pictq_rindex + 1) % VIDEO_PICTURE_QUEUE_SIZE];
1387 duration = nextvp->pts - vp->pts;
1388 if(!is->step && (redisplay || framedrop>0 || (framedrop && get_master_sync_type(is) != AV_SYNC_VIDEO_MASTER)) && time > is->frame_timer + duration){
1390 is->frame_drops_late++;
1391 pictq_next_picture(is);
1397 if (is->subtitle_st) {
1398 if (is->subtitle_stream_changed) {
1399 SDL_LockMutex(is->subpq_mutex);
1401 while (is->subpq_size) {
1402 free_subpicture(&is->subpq[is->subpq_rindex]);
1404 /* update queue size and signal for next picture */
1405 if (++is->subpq_rindex == SUBPICTURE_QUEUE_SIZE)
1406 is->subpq_rindex = 0;
1410 is->subtitle_stream_changed = 0;
1412 SDL_CondSignal(is->subpq_cond);
1413 SDL_UnlockMutex(is->subpq_mutex);
1415 if (is->subpq_size > 0) {
1416 sp = &is->subpq[is->subpq_rindex];
1418 if (is->subpq_size > 1)
1419 sp2 = &is->subpq[(is->subpq_rindex + 1) % SUBPICTURE_QUEUE_SIZE];
1423 if ((is->video_current_pts > (sp->pts + ((float) sp->sub.end_display_time / 1000)))
1424 || (sp2 && is->video_current_pts > (sp2->pts + ((float) sp2->sub.start_display_time / 1000))))
1426 free_subpicture(sp);
1428 /* update queue size and signal for next picture */
1429 if (++is->subpq_rindex == SUBPICTURE_QUEUE_SIZE)
1430 is->subpq_rindex = 0;
1432 SDL_LockMutex(is->subpq_mutex);
1434 SDL_CondSignal(is->subpq_cond);
1435 SDL_UnlockMutex(is->subpq_mutex);
1442 /* display picture */
1443 if (!display_disable && is->show_mode == SHOW_MODE_VIDEO)
1446 pictq_next_picture(is);
1448 if (is->step && !is->paused)
1449 stream_toggle_pause(is);
1452 is->force_refresh = 0;
1454 static int64_t last_time;
1456 int aqsize, vqsize, sqsize;
1459 cur_time = av_gettime();
1460 if (!last_time || (cur_time - last_time) >= 30000) {
1465 aqsize = is->audioq.size;
1467 vqsize = is->videoq.size;
1468 if (is->subtitle_st)
1469 sqsize = is->subtitleq.size;
1471 if (is->audio_st && is->video_st)
1472 av_diff = get_audio_clock(is) - get_video_clock(is);
1473 printf("%7.2f A-V:%7.3f fd=%4d aq=%5dKB vq=%5dKB sq=%5dB f=%"PRId64"/%"PRId64" \r",
1474 get_master_clock(is),
1476 is->frame_drops_early + is->frame_drops_late,
1480 is->video_st ? is->video_st->codec->pts_correction_num_faulty_dts : 0,
1481 is->video_st ? is->video_st->codec->pts_correction_num_faulty_pts : 0);
1483 last_time = cur_time;
1488 /* allocate a picture (needs to do that in main thread to avoid
1489 potential locking problems */
1490 static void alloc_picture(VideoState *is)
1494 vp = &is->pictq[is->pictq_windex];
1497 SDL_FreeYUVOverlay(vp->bmp);
1499 video_open(is, 0, vp);
1501 vp->bmp = SDL_CreateYUVOverlay(vp->width, vp->height,
1504 if (!vp->bmp || vp->bmp->pitches[0] < vp->width) {
1505 /* SDL allocates a buffer smaller than requested if the video
1506 * overlay hardware is unable to support the requested size. */
1507 fprintf(stderr, "Error: the video system does not support an image\n"
1508 "size of %dx%d pixels. Try using -lowres or -vf \"scale=w:h\"\n"
1509 "to reduce the image size.\n", vp->width, vp->height );
1513 SDL_LockMutex(is->pictq_mutex);
1515 SDL_CondSignal(is->pictq_cond);
1516 SDL_UnlockMutex(is->pictq_mutex);
1519 static void duplicate_right_border_pixels(SDL_Overlay *bmp) {
1520 int i, width, height;
1522 for (i = 0; i < 3; i++) {
1529 if (bmp->pitches[i] > width) {
1530 maxp = bmp->pixels[i] + bmp->pitches[i] * height - 1;
1531 for (p = bmp->pixels[i] + width - 1; p < maxp; p += bmp->pitches[i])
1537 static int queue_picture(VideoState *is, AVFrame *src_frame, double pts, int64_t pos, int serial)
1541 #if defined(DEBUG_SYNC) && 0
1542 printf("frame_type=%c pts=%0.3f\n",
1543 av_get_picture_type_char(src_frame->pict_type), pts);
1546 /* wait until we have space to put a new picture */
1547 SDL_LockMutex(is->pictq_mutex);
1549 /* keep the last already displayed picture in the queue */
1550 while (is->pictq_size >= VIDEO_PICTURE_QUEUE_SIZE - 2 &&
1551 !is->videoq.abort_request) {
1552 SDL_CondWait(is->pictq_cond, is->pictq_mutex);
1554 SDL_UnlockMutex(is->pictq_mutex);
1556 if (is->videoq.abort_request)
1559 vp = &is->pictq[is->pictq_windex];
1562 vp->sar = src_frame->sample_aspect_ratio;
1564 vp->sar = av_guess_sample_aspect_ratio(is->ic, is->video_st, src_frame);
1567 /* alloc or resize hardware picture buffer */
1568 if (!vp->bmp || vp->reallocate || !vp->allocated ||
1569 vp->width != src_frame->width ||
1570 vp->height != src_frame->height) {
1575 vp->width = src_frame->width;
1576 vp->height = src_frame->height;
1578 /* the allocation must be done in the main thread to avoid
1579 locking problems. */
1580 event.type = FF_ALLOC_EVENT;
1581 event.user.data1 = is;
1582 SDL_PushEvent(&event);
1584 /* wait until the picture is allocated */
1585 SDL_LockMutex(is->pictq_mutex);
1586 while (!vp->allocated && !is->videoq.abort_request) {
1587 SDL_CondWait(is->pictq_cond, is->pictq_mutex);
1589 /* if the queue is aborted, we have to pop the pending ALLOC event or wait for the allocation to complete */
1590 if (is->videoq.abort_request && SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_EVENTMASK(FF_ALLOC_EVENT)) != 1) {
1591 while (!vp->allocated) {
1592 SDL_CondWait(is->pictq_cond, is->pictq_mutex);
1595 SDL_UnlockMutex(is->pictq_mutex);
1597 if (is->videoq.abort_request)
1601 /* if the frame is not skipped, then display it */
1603 AVPicture pict = { { 0 } };
1605 /* get a pointer on the bitmap */
1606 SDL_LockYUVOverlay (vp->bmp);
1608 pict.data[0] = vp->bmp->pixels[0];
1609 pict.data[1] = vp->bmp->pixels[2];
1610 pict.data[2] = vp->bmp->pixels[1];
1612 pict.linesize[0] = vp->bmp->pitches[0];
1613 pict.linesize[1] = vp->bmp->pitches[2];
1614 pict.linesize[2] = vp->bmp->pitches[1];
1617 // FIXME use direct rendering
1618 av_picture_copy(&pict, (AVPicture *)src_frame,
1619 src_frame->format, vp->width, vp->height);
1621 av_opt_get_int(sws_opts, "sws_flags", 0, &sws_flags);
1622 is->img_convert_ctx = sws_getCachedContext(is->img_convert_ctx,
1623 vp->width, vp->height, src_frame->format, vp->width, vp->height,
1624 AV_PIX_FMT_YUV420P, sws_flags, NULL, NULL, NULL);
1625 if (is->img_convert_ctx == NULL) {
1626 fprintf(stderr, "Cannot initialize the conversion context\n");
1629 sws_scale(is->img_convert_ctx, src_frame->data, src_frame->linesize,
1630 0, vp->height, pict.data, pict.linesize);
1632 /* workaround SDL PITCH_WORKAROUND */
1633 duplicate_right_border_pixels(vp->bmp);
1634 /* update the bitmap content */
1635 SDL_UnlockYUVOverlay(vp->bmp);
1639 vp->serial = serial;
1641 /* now we can update the picture count */
1642 if (++is->pictq_windex == VIDEO_PICTURE_QUEUE_SIZE)
1643 is->pictq_windex = 0;
1644 SDL_LockMutex(is->pictq_mutex);
1646 SDL_UnlockMutex(is->pictq_mutex);
1651 static int get_video_frame(VideoState *is, AVFrame *frame, AVPacket *pkt, int *serial)
1655 if (packet_queue_get(&is->videoq, pkt, 1, serial) < 0)
1658 if (pkt->data == flush_pkt.data) {
1659 avcodec_flush_buffers(is->video_st->codec);
1661 SDL_LockMutex(is->pictq_mutex);
1662 // Make sure there are no long delay timers (ideally we should just flush the queue but that's harder)
1663 while (is->pictq_size && !is->videoq.abort_request) {
1664 SDL_CondWait(is->pictq_cond, is->pictq_mutex);
1666 is->video_current_pos = -1;
1667 is->frame_last_pts = AV_NOPTS_VALUE;
1668 is->frame_last_duration = 0;
1669 is->frame_timer = (double)av_gettime() / 1000000.0;
1670 is->frame_last_dropped_pts = AV_NOPTS_VALUE;
1671 SDL_UnlockMutex(is->pictq_mutex);
1675 if(avcodec_decode_video2(is->video_st->codec, frame, &got_picture, pkt) < 0)
1681 if (decoder_reorder_pts == -1) {
1682 frame->pts = av_frame_get_best_effort_timestamp(frame);
1683 } else if (decoder_reorder_pts) {
1684 frame->pts = frame->pkt_pts;
1686 frame->pts = frame->pkt_dts;
1689 if (frame->pts == AV_NOPTS_VALUE) {
1693 if (framedrop>0 || (framedrop && get_master_sync_type(is) != AV_SYNC_VIDEO_MASTER)) {
1694 SDL_LockMutex(is->pictq_mutex);
1695 if (is->frame_last_pts != AV_NOPTS_VALUE && frame->pts) {
1696 double clockdiff = get_video_clock(is) - get_master_clock(is);
1697 double dpts = av_q2d(is->video_st->time_base) * frame->pts;
1698 double ptsdiff = dpts - is->frame_last_pts;
1699 if (!isnan(clockdiff) && fabs(clockdiff) < AV_NOSYNC_THRESHOLD &&
1700 ptsdiff > 0 && ptsdiff < AV_NOSYNC_THRESHOLD &&
1701 clockdiff + ptsdiff - is->frame_last_filter_delay < 0) {
1702 is->frame_last_dropped_pos = pkt->pos;
1703 is->frame_last_dropped_pts = dpts;
1704 is->frame_last_dropped_serial = *serial;
1705 is->frame_drops_early++;
1706 av_frame_unref(frame);
1710 SDL_UnlockMutex(is->pictq_mutex);
1719 static int configure_filtergraph(AVFilterGraph *graph, const char *filtergraph,
1720 AVFilterContext *source_ctx, AVFilterContext *sink_ctx)
1723 AVFilterInOut *outputs = NULL, *inputs = NULL;
1726 outputs = avfilter_inout_alloc();
1727 inputs = avfilter_inout_alloc();
1728 if (!outputs || !inputs) {
1729 ret = AVERROR(ENOMEM);
1733 outputs->name = av_strdup("in");
1734 outputs->filter_ctx = source_ctx;
1735 outputs->pad_idx = 0;
1736 outputs->next = NULL;
1738 inputs->name = av_strdup("out");
1739 inputs->filter_ctx = sink_ctx;
1740 inputs->pad_idx = 0;
1741 inputs->next = NULL;
1743 if ((ret = avfilter_graph_parse(graph, filtergraph, &inputs, &outputs, NULL)) < 0)
1746 if ((ret = avfilter_link(source_ctx, 0, sink_ctx, 0)) < 0)
1750 ret = avfilter_graph_config(graph, NULL);
1752 avfilter_inout_free(&outputs);
1753 avfilter_inout_free(&inputs);
1757 static int configure_video_filters(AVFilterGraph *graph, VideoState *is, const char *vfilters, AVFrame *frame)
1759 static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE };
1760 char sws_flags_str[128];
1761 char buffersrc_args[256];
1763 AVBufferSinkParams *buffersink_params = av_buffersink_params_alloc();
1764 AVFilterContext *filt_src = NULL, *filt_out = NULL, *filt_crop;
1765 AVCodecContext *codec = is->video_st->codec;
1766 AVRational fr = av_guess_frame_rate(is->ic, is->video_st, NULL);
1768 if (!buffersink_params)
1769 return AVERROR(ENOMEM);
1771 av_opt_get_int(sws_opts, "sws_flags", 0, &sws_flags);
1772 snprintf(sws_flags_str, sizeof(sws_flags_str), "flags=%"PRId64, sws_flags);
1773 graph->scale_sws_opts = av_strdup(sws_flags_str);
1775 snprintf(buffersrc_args, sizeof(buffersrc_args),
1776 "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d",
1777 frame->width, frame->height, frame->format,
1778 is->video_st->time_base.num, is->video_st->time_base.den,
1779 codec->sample_aspect_ratio.num, FFMAX(codec->sample_aspect_ratio.den, 1));
1780 if (fr.num && fr.den)
1781 av_strlcatf(buffersrc_args, sizeof(buffersrc_args), ":frame_rate=%d/%d", fr.num, fr.den);
1783 if ((ret = avfilter_graph_create_filter(&filt_src,
1784 avfilter_get_by_name("buffer"),
1785 "ffplay_buffer", buffersrc_args, NULL,
1789 buffersink_params->pixel_fmts = pix_fmts;
1790 ret = avfilter_graph_create_filter(&filt_out,
1791 avfilter_get_by_name("buffersink"),
1792 "ffplay_buffersink", NULL, buffersink_params, graph);
1796 /* SDL YUV code is not handling odd width/height for some driver
1797 * combinations, therefore we crop the picture to an even width/height. */
1798 if ((ret = avfilter_graph_create_filter(&filt_crop,
1799 avfilter_get_by_name("crop"),
1800 "ffplay_crop", "floor(in_w/2)*2:floor(in_h/2)*2", NULL, graph)) < 0)
1802 if ((ret = avfilter_link(filt_crop, 0, filt_out, 0)) < 0)
1805 if ((ret = configure_filtergraph(graph, vfilters, filt_src, filt_crop)) < 0)
1808 is->in_video_filter = filt_src;
1809 is->out_video_filter = filt_out;
1812 av_freep(&buffersink_params);
1816 static int configure_audio_filters(VideoState *is, const char *afilters, int force_output_format)
1818 static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE };
1819 int sample_rates[2] = { 0, -1 };
1820 int64_t channel_layouts[2] = { 0, -1 };
1821 int channels[2] = { 0, -1 };
1822 AVFilterContext *filt_asrc = NULL, *filt_asink = NULL;
1823 char asrc_args[256];
1824 AVABufferSinkParams *asink_params = NULL;
1827 avfilter_graph_free(&is->agraph);
1828 if (!(is->agraph = avfilter_graph_alloc()))
1829 return AVERROR(ENOMEM);
1831 ret = snprintf(asrc_args, sizeof(asrc_args),
1832 "sample_rate=%d:sample_fmt=%s:channels=%d:time_base=%d/%d",
1833 is->audio_filter_src.freq, av_get_sample_fmt_name(is->audio_filter_src.fmt),
1834 is->audio_filter_src.channels,
1835 1, is->audio_filter_src.freq);
1836 if (is->audio_filter_src.channel_layout)
1837 snprintf(asrc_args + ret, sizeof(asrc_args) - ret,
1838 ":channel_layout=0x%"PRIx64, is->audio_filter_src.channel_layout);
1840 ret = avfilter_graph_create_filter(&filt_asrc,
1841 avfilter_get_by_name("abuffer"), "ffplay_abuffer",
1842 asrc_args, NULL, is->agraph);
1846 if (!(asink_params = av_abuffersink_params_alloc())) {
1847 ret = AVERROR(ENOMEM);
1850 asink_params->sample_fmts = sample_fmts;
1852 asink_params->all_channel_counts = 1;
1853 if (force_output_format) {
1854 channel_layouts[0] = is->audio_tgt.channel_layout;
1855 asink_params->channel_layouts = channel_layouts;
1856 asink_params->all_channel_counts = 0;
1857 channels[0] = is->audio_tgt.channels;
1858 asink_params->channel_counts = channels;
1859 asink_params->all_channel_counts = 0;
1860 sample_rates[0] = is->audio_tgt.freq;
1861 asink_params->sample_rates = sample_rates;
1864 ret = avfilter_graph_create_filter(&filt_asink,
1865 avfilter_get_by_name("abuffersink"), "ffplay_abuffersink",
1866 NULL, asink_params, is->agraph);
1870 if ((ret = configure_filtergraph(is->agraph, afilters, filt_asrc, filt_asink)) < 0)
1873 is->in_audio_filter = filt_asrc;
1874 is->out_audio_filter = filt_asink;
1877 av_freep(&asink_params);
1879 avfilter_graph_free(&is->agraph);
1882 #endif /* CONFIG_AVFILTER */
1884 static int video_thread(void *arg)
1886 AVPacket pkt = { 0 };
1887 VideoState *is = arg;
1888 AVFrame *frame = av_frame_alloc();
1894 AVFilterGraph *graph = avfilter_graph_alloc();
1895 AVFilterContext *filt_out = NULL, *filt_in = NULL;
1898 enum AVPixelFormat last_format = -2;
1899 int last_serial = -1;
1903 while (is->paused && !is->videoq.abort_request)
1906 avcodec_get_frame_defaults(frame);
1907 av_free_packet(&pkt);
1909 ret = get_video_frame(is, frame, &pkt, &serial);
1916 if ( last_w != frame->width
1917 || last_h != frame->height
1918 || last_format != frame->format
1919 || last_serial != serial) {
1920 av_log(NULL, AV_LOG_DEBUG,
1921 "Video frame changed from size:%dx%d format:%s serial:%d to size:%dx%d format:%s serial:%d\n",
1923 (const char *)av_x_if_null(av_get_pix_fmt_name(last_format), "none"), last_serial,
1924 frame->width, frame->height,
1925 (const char *)av_x_if_null(av_get_pix_fmt_name(frame->format), "none"), serial);
1926 avfilter_graph_free(&graph);
1927 graph = avfilter_graph_alloc();
1928 if ((ret = configure_video_filters(graph, is, vfilters, frame)) < 0) {
1930 event.type = FF_QUIT_EVENT;
1931 event.user.data1 = is;
1932 SDL_PushEvent(&event);
1933 av_free_packet(&pkt);
1936 filt_in = is->in_video_filter;
1937 filt_out = is->out_video_filter;
1938 last_w = frame->width;
1939 last_h = frame->height;
1940 last_format = frame->format;
1941 last_serial = serial;
1944 frame->sample_aspect_ratio = av_guess_sample_aspect_ratio(is->ic, is->video_st, frame);
1945 ret = av_buffersrc_add_frame(filt_in, frame);
1948 av_frame_unref(frame);
1949 avcodec_get_frame_defaults(frame);
1950 av_free_packet(&pkt);
1953 is->frame_last_returned_time = av_gettime() / 1000000.0;
1955 ret = av_buffersink_get_frame_flags(filt_out, frame, 0);
1961 is->frame_last_filter_delay = av_gettime() / 1000000.0 - is->frame_last_returned_time;
1962 if (fabs(is->frame_last_filter_delay) > AV_NOSYNC_THRESHOLD / 10.0)
1963 is->frame_last_filter_delay = 0;
1965 pts = frame->pts * av_q2d(filt_out->inputs[0]->time_base);
1966 ret = queue_picture(is, frame, pts, av_frame_get_pkt_pos(frame), serial);
1967 av_frame_unref(frame);
1970 pts = frame->pts * av_q2d(is->video_st->time_base);
1971 ret = queue_picture(is, frame, pts, pkt.pos, serial);
1972 av_frame_unref(frame);
1979 avcodec_flush_buffers(is->video_st->codec);
1981 avfilter_graph_free(&graph);
1983 av_free_packet(&pkt);
1984 av_frame_free(&frame);
1988 static int subtitle_thread(void *arg)
1990 VideoState *is = arg;
1992 AVPacket pkt1, *pkt = &pkt1;
1996 int r, g, b, y, u, v, a;
1999 while (is->paused && !is->subtitleq.abort_request) {
2002 if (packet_queue_get(&is->subtitleq, pkt, 1, NULL) < 0)
2005 if (pkt->data == flush_pkt.data) {
2006 avcodec_flush_buffers(is->subtitle_st->codec);
2009 SDL_LockMutex(is->subpq_mutex);
2010 while (is->subpq_size >= SUBPICTURE_QUEUE_SIZE &&
2011 !is->subtitleq.abort_request) {
2012 SDL_CondWait(is->subpq_cond, is->subpq_mutex);
2014 SDL_UnlockMutex(is->subpq_mutex);
2016 if (is->subtitleq.abort_request)
2019 sp = &is->subpq[is->subpq_windex];
2021 /* NOTE: ipts is the PTS of the _first_ picture beginning in
2022 this packet, if any */
2024 if (pkt->pts != AV_NOPTS_VALUE)
2025 pts = av_q2d(is->subtitle_st->time_base) * pkt->pts;
2027 avcodec_decode_subtitle2(is->subtitle_st->codec, &sp->sub,
2028 &got_subtitle, pkt);
2029 if (got_subtitle && sp->sub.format == 0) {
2030 if (sp->sub.pts != AV_NOPTS_VALUE)
2031 pts = sp->sub.pts / (double)AV_TIME_BASE;
2034 for (i = 0; i < sp->sub.num_rects; i++)
2036 for (j = 0; j < sp->sub.rects[i]->nb_colors; j++)
2038 RGBA_IN(r, g, b, a, (uint32_t*)sp->sub.rects[i]->pict.data[1] + j);
2039 y = RGB_TO_Y_CCIR(r, g, b);
2040 u = RGB_TO_U_CCIR(r, g, b, 0);
2041 v = RGB_TO_V_CCIR(r, g, b, 0);
2042 YUVA_OUT((uint32_t*)sp->sub.rects[i]->pict.data[1] + j, y, u, v, a);
2046 /* now we can update the picture count */
2047 if (++is->subpq_windex == SUBPICTURE_QUEUE_SIZE)
2048 is->subpq_windex = 0;
2049 SDL_LockMutex(is->subpq_mutex);
2051 SDL_UnlockMutex(is->subpq_mutex);
2053 av_free_packet(pkt);
2058 /* copy samples for viewing in editor window */
2059 static void update_sample_display(VideoState *is, short *samples, int samples_size)
2063 size = samples_size / sizeof(short);
2065 len = SAMPLE_ARRAY_SIZE - is->sample_array_index;
2068 memcpy(is->sample_array + is->sample_array_index, samples, len * sizeof(short));
2070 is->sample_array_index += len;
2071 if (is->sample_array_index >= SAMPLE_ARRAY_SIZE)
2072 is->sample_array_index = 0;
2077 /* return the wanted number of samples to get better sync if sync_type is video
2078 * or external master clock */
2079 static int synchronize_audio(VideoState *is, int nb_samples)
2081 int wanted_nb_samples = nb_samples;
2083 /* if not master, then we try to remove or add samples to correct the clock */
2084 if (get_master_sync_type(is) != AV_SYNC_AUDIO_MASTER) {
2085 double diff, avg_diff;
2086 int min_nb_samples, max_nb_samples;
2088 diff = get_audio_clock(is) - get_master_clock(is);
2090 if (!isnan(diff) && fabs(diff) < AV_NOSYNC_THRESHOLD) {
2091 is->audio_diff_cum = diff + is->audio_diff_avg_coef * is->audio_diff_cum;
2092 if (is->audio_diff_avg_count < AUDIO_DIFF_AVG_NB) {
2093 /* not enough measures to have a correct estimate */
2094 is->audio_diff_avg_count++;
2096 /* estimate the A-V difference */
2097 avg_diff = is->audio_diff_cum * (1.0 - is->audio_diff_avg_coef);
2099 if (fabs(avg_diff) >= is->audio_diff_threshold) {
2100 wanted_nb_samples = nb_samples + (int)(diff * is->audio_src.freq);
2101 min_nb_samples = ((nb_samples * (100 - SAMPLE_CORRECTION_PERCENT_MAX) / 100));
2102 max_nb_samples = ((nb_samples * (100 + SAMPLE_CORRECTION_PERCENT_MAX) / 100));
2103 wanted_nb_samples = FFMIN(FFMAX(wanted_nb_samples, min_nb_samples), max_nb_samples);
2105 av_dlog(NULL, "diff=%f adiff=%f sample_diff=%d apts=%0.3f %f\n",
2106 diff, avg_diff, wanted_nb_samples - nb_samples,
2107 is->audio_clock, is->audio_diff_threshold);
2110 /* too big difference : may be initial PTS errors, so
2112 is->audio_diff_avg_count = 0;
2113 is->audio_diff_cum = 0;
2117 return wanted_nb_samples;
2121 * Decode one audio frame and return its uncompressed size.
2123 * The processed audio frame is decoded, converted if required, and
2124 * stored in is->audio_buf, with size in bytes given by the return
2127 static int audio_decode_frame(VideoState *is)
2129 AVPacket *pkt_temp = &is->audio_pkt_temp;
2130 AVPacket *pkt = &is->audio_pkt;
2131 AVCodecContext *dec = is->audio_st->codec;
2132 int len1, data_size, resampled_data_size;
2133 int64_t dec_channel_layout;
2135 av_unused double audio_clock0;
2137 int flush_complete = 0;
2138 int wanted_nb_samples;
2144 /* NOTE: the audio packet can contain several frames */
2145 while (pkt_temp->size > 0 || (!pkt_temp->data && new_packet) || is->audio_buf_frames_pending) {
2147 if (!(is->frame = avcodec_alloc_frame()))
2148 return AVERROR(ENOMEM);
2150 av_frame_unref(is->frame);
2151 avcodec_get_frame_defaults(is->frame);
2154 if (is->audioq.serial != is->audio_pkt_temp_serial)
2160 if (!is->audio_buf_frames_pending) {
2164 len1 = avcodec_decode_audio4(dec, is->frame, &got_frame, pkt_temp);
2166 /* if error, we skip the frame */
2171 pkt_temp->data += len1;
2172 pkt_temp->size -= len1;
2175 /* stop sending empty packets if the decoder is finished */
2176 if (!pkt_temp->data && dec->codec->capabilities & CODEC_CAP_DELAY)
2181 tb = (AVRational){1, is->frame->sample_rate};
2182 if (is->frame->pts != AV_NOPTS_VALUE)
2183 is->frame->pts = av_rescale_q(is->frame->pts, dec->time_base, tb);
2184 if (is->frame->pts == AV_NOPTS_VALUE && pkt_temp->pts != AV_NOPTS_VALUE)
2185 is->frame->pts = av_rescale_q(pkt_temp->pts, is->audio_st->time_base, tb);
2186 if (pkt_temp->pts != AV_NOPTS_VALUE)
2187 pkt_temp->pts += (double) is->frame->nb_samples / is->frame->sample_rate / av_q2d(is->audio_st->time_base);
2190 dec_channel_layout = get_valid_channel_layout(is->frame->channel_layout, av_frame_get_channels(is->frame));
2193 cmp_audio_fmts(is->audio_filter_src.fmt, is->audio_filter_src.channels,
2194 is->frame->format, av_frame_get_channels(is->frame)) ||
2195 is->audio_filter_src.channel_layout != dec_channel_layout ||
2196 is->audio_filter_src.freq != is->frame->sample_rate ||
2197 is->audio_pkt_temp_serial != is->audio_last_serial;
2200 char buf1[1024], buf2[1024];
2201 av_get_channel_layout_string(buf1, sizeof(buf1), -1, is->audio_filter_src.channel_layout);
2202 av_get_channel_layout_string(buf2, sizeof(buf2), -1, dec_channel_layout);
2203 av_log(NULL, AV_LOG_DEBUG,
2204 "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",
2205 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,
2206 is->frame->sample_rate, av_frame_get_channels(is->frame), av_get_sample_fmt_name(is->frame->format), buf2, is->audio_pkt_temp_serial);
2208 is->audio_filter_src.fmt = is->frame->format;
2209 is->audio_filter_src.channels = av_frame_get_channels(is->frame);
2210 is->audio_filter_src.channel_layout = dec_channel_layout;
2211 is->audio_filter_src.freq = is->frame->sample_rate;
2212 is->audio_last_serial = is->audio_pkt_temp_serial;
2214 if ((ret = configure_audio_filters(is, afilters, 1)) < 0)
2218 if ((ret = av_buffersrc_add_frame(is->in_audio_filter, is->frame)) < 0)
2220 av_frame_unref(is->frame);
2224 if ((ret = av_buffersink_get_frame_flags(is->out_audio_filter, is->frame, 0)) < 0) {
2225 if (ret == AVERROR(EAGAIN)) {
2226 is->audio_buf_frames_pending = 0;
2231 is->audio_buf_frames_pending = 1;
2232 tb = is->out_audio_filter->inputs[0]->time_base;
2235 data_size = av_samples_get_buffer_size(NULL, av_frame_get_channels(is->frame),
2236 is->frame->nb_samples,
2237 is->frame->format, 1);
2239 dec_channel_layout =
2240 (is->frame->channel_layout && av_frame_get_channels(is->frame) == av_get_channel_layout_nb_channels(is->frame->channel_layout)) ?
2241 is->frame->channel_layout : av_get_default_channel_layout(av_frame_get_channels(is->frame));
2242 wanted_nb_samples = synchronize_audio(is, is->frame->nb_samples);
2244 if (is->frame->format != is->audio_src.fmt ||
2245 dec_channel_layout != is->audio_src.channel_layout ||
2246 is->frame->sample_rate != is->audio_src.freq ||
2247 (wanted_nb_samples != is->frame->nb_samples && !is->swr_ctx)) {
2248 swr_free(&is->swr_ctx);
2249 is->swr_ctx = swr_alloc_set_opts(NULL,
2250 is->audio_tgt.channel_layout, is->audio_tgt.fmt, is->audio_tgt.freq,
2251 dec_channel_layout, is->frame->format, is->frame->sample_rate,
2253 if (!is->swr_ctx || swr_init(is->swr_ctx) < 0) {
2254 fprintf(stderr, "Cannot create sample rate converter for conversion of %d Hz %s %d channels to %d Hz %s %d channels!\n",
2255 is->frame->sample_rate, av_get_sample_fmt_name(is->frame->format), av_frame_get_channels(is->frame),
2256 is->audio_tgt.freq, av_get_sample_fmt_name(is->audio_tgt.fmt), is->audio_tgt.channels);
2259 is->audio_src.channel_layout = dec_channel_layout;
2260 is->audio_src.channels = av_frame_get_channels(is->frame);
2261 is->audio_src.freq = is->frame->sample_rate;
2262 is->audio_src.fmt = is->frame->format;
2266 const uint8_t **in = (const uint8_t **)is->frame->extended_data;
2267 uint8_t **out = &is->audio_buf1;
2268 int out_count = (int64_t)wanted_nb_samples * is->audio_tgt.freq / is->frame->sample_rate + 256;
2269 int out_size = av_samples_get_buffer_size(NULL, is->audio_tgt.channels, out_count, is->audio_tgt.fmt, 0);
2271 if (wanted_nb_samples != is->frame->nb_samples) {
2272 if (swr_set_compensation(is->swr_ctx, (wanted_nb_samples - is->frame->nb_samples) * is->audio_tgt.freq / is->frame->sample_rate,
2273 wanted_nb_samples * is->audio_tgt.freq / is->frame->sample_rate) < 0) {
2274 fprintf(stderr, "swr_set_compensation() failed\n");
2278 av_fast_malloc(&is->audio_buf1, &is->audio_buf1_size, out_size);
2279 if (!is->audio_buf1)
2280 return AVERROR(ENOMEM);
2281 len2 = swr_convert(is->swr_ctx, out, out_count, in, is->frame->nb_samples);
2283 fprintf(stderr, "swr_convert() failed\n");
2286 if (len2 == out_count) {
2287 fprintf(stderr, "warning: audio buffer is probably too small\n");
2288 swr_init(is->swr_ctx);
2290 is->audio_buf = is->audio_buf1;
2291 resampled_data_size = len2 * is->audio_tgt.channels * av_get_bytes_per_sample(is->audio_tgt.fmt);
2293 is->audio_buf = is->frame->data[0];
2294 resampled_data_size = data_size;
2297 audio_clock0 = is->audio_clock;
2298 /* update the audio clock with the pts */
2299 if (is->frame->pts != AV_NOPTS_VALUE) {
2300 is->audio_clock = is->frame->pts * av_q2d(tb) + (double) is->frame->nb_samples / is->frame->sample_rate;
2301 is->audio_clock_serial = is->audio_pkt_temp_serial;
2305 static double last_clock;
2306 printf("audio: delay=%0.3f clock=%0.3f clock0=%0.3f\n",
2307 is->audio_clock - last_clock,
2308 is->audio_clock, audio_clock0);
2309 last_clock = is->audio_clock;
2312 return resampled_data_size;
2315 /* free the current packet */
2317 av_free_packet(pkt);
2318 memset(pkt_temp, 0, sizeof(*pkt_temp));
2320 if (is->audioq.abort_request) {
2324 if (is->audioq.nb_packets == 0)
2325 SDL_CondSignal(is->continue_read_thread);
2327 /* read next packet */
2328 if ((new_packet = packet_queue_get(&is->audioq, pkt, 1, &is->audio_pkt_temp_serial)) < 0)
2331 if (pkt->data == flush_pkt.data) {
2332 avcodec_flush_buffers(dec);
2334 is->audio_buf_frames_pending = 0;
2341 /* prepare a new audio buffer */
2342 static void sdl_audio_callback(void *opaque, Uint8 *stream, int len)
2344 VideoState *is = opaque;
2345 int audio_size, len1;
2347 int frame_size = av_samples_get_buffer_size(NULL, is->audio_tgt.channels, 1, is->audio_tgt.fmt, 1);
2349 audio_callback_time = av_gettime();
2352 if (is->audio_buf_index >= is->audio_buf_size) {
2353 audio_size = audio_decode_frame(is);
2354 if (audio_size < 0) {
2355 /* if error, just output silence */
2356 is->audio_buf = is->silence_buf;
2357 is->audio_buf_size = sizeof(is->silence_buf) / frame_size * frame_size;
2359 if (is->show_mode != SHOW_MODE_VIDEO)
2360 update_sample_display(is, (int16_t *)is->audio_buf, audio_size);
2361 is->audio_buf_size = audio_size;
2363 is->audio_buf_index = 0;
2365 len1 = is->audio_buf_size - is->audio_buf_index;
2368 memcpy(stream, (uint8_t *)is->audio_buf + is->audio_buf_index, len1);
2371 is->audio_buf_index += len1;
2373 bytes_per_sec = is->audio_tgt.freq * is->audio_tgt.channels * av_get_bytes_per_sample(is->audio_tgt.fmt);
2374 is->audio_write_buf_size = is->audio_buf_size - is->audio_buf_index;
2375 /* Let's assume the audio driver that is used by SDL has two periods. */
2376 is->audio_current_pts = is->audio_clock - (double)(2 * is->audio_hw_buf_size + is->audio_write_buf_size) / bytes_per_sec;
2377 is->audio_current_pts_drift = is->audio_current_pts - audio_callback_time / 1000000.0;
2378 if (is->audioq.serial == is->audio_clock_serial)
2379 check_external_clock_sync(is, is->audio_current_pts);
2382 static int audio_open(void *opaque, int64_t wanted_channel_layout, int wanted_nb_channels, int wanted_sample_rate, struct AudioParams *audio_hw_params)
2384 SDL_AudioSpec wanted_spec, spec;
2386 const int next_nb_channels[] = {0, 0, 1, 6, 2, 6, 4, 6};
2388 env = SDL_getenv("SDL_AUDIO_CHANNELS");
2390 wanted_nb_channels = atoi(env);
2391 wanted_channel_layout = av_get_default_channel_layout(wanted_nb_channels);
2393 if (!wanted_channel_layout || wanted_nb_channels != av_get_channel_layout_nb_channels(wanted_channel_layout)) {
2394 wanted_channel_layout = av_get_default_channel_layout(wanted_nb_channels);
2395 wanted_channel_layout &= ~AV_CH_LAYOUT_STEREO_DOWNMIX;
2397 wanted_spec.channels = av_get_channel_layout_nb_channels(wanted_channel_layout);
2398 wanted_spec.freq = wanted_sample_rate;
2399 if (wanted_spec.freq <= 0 || wanted_spec.channels <= 0) {
2400 fprintf(stderr, "Invalid sample rate or channel count!\n");
2403 wanted_spec.format = AUDIO_S16SYS;
2404 wanted_spec.silence = 0;
2405 wanted_spec.samples = SDL_AUDIO_BUFFER_SIZE;
2406 wanted_spec.callback = sdl_audio_callback;
2407 wanted_spec.userdata = opaque;
2408 while (SDL_OpenAudio(&wanted_spec, &spec) < 0) {
2409 fprintf(stderr, "SDL_OpenAudio (%d channels): %s\n", wanted_spec.channels, SDL_GetError());
2410 wanted_spec.channels = next_nb_channels[FFMIN(7, wanted_spec.channels)];
2411 if (!wanted_spec.channels) {
2412 fprintf(stderr, "No more channel combinations to try, audio open failed\n");
2415 wanted_channel_layout = av_get_default_channel_layout(wanted_spec.channels);
2417 if (spec.format != AUDIO_S16SYS) {
2418 fprintf(stderr, "SDL advised audio format %d is not supported!\n", spec.format);
2421 if (spec.channels != wanted_spec.channels) {
2422 wanted_channel_layout = av_get_default_channel_layout(spec.channels);
2423 if (!wanted_channel_layout) {
2424 fprintf(stderr, "SDL advised channel count %d is not supported!\n", spec.channels);
2429 audio_hw_params->fmt = AV_SAMPLE_FMT_S16;
2430 audio_hw_params->freq = spec.freq;
2431 audio_hw_params->channel_layout = wanted_channel_layout;
2432 audio_hw_params->channels = spec.channels;
2436 /* open a given stream. Return 0 if OK */
2437 static int stream_component_open(VideoState *is, int stream_index)
2439 AVFormatContext *ic = is->ic;
2440 AVCodecContext *avctx;
2442 const char *forced_codec_name = NULL;
2444 AVDictionaryEntry *t = NULL;
2445 int sample_rate, nb_channels;
2446 int64_t channel_layout;
2449 if (stream_index < 0 || stream_index >= ic->nb_streams)
2451 avctx = ic->streams[stream_index]->codec;
2453 codec = avcodec_find_decoder(avctx->codec_id);
2455 switch(avctx->codec_type){
2456 case AVMEDIA_TYPE_AUDIO : is->last_audio_stream = stream_index; forced_codec_name = audio_codec_name; break;
2457 case AVMEDIA_TYPE_SUBTITLE: is->last_subtitle_stream = stream_index; forced_codec_name = subtitle_codec_name; break;
2458 case AVMEDIA_TYPE_VIDEO : is->last_video_stream = stream_index; forced_codec_name = video_codec_name; break;
2460 if (forced_codec_name)
2461 codec = avcodec_find_decoder_by_name(forced_codec_name);
2463 if (forced_codec_name) fprintf(stderr, "No codec could be found with name '%s'\n", forced_codec_name);
2464 else fprintf(stderr, "No codec could be found with id %d\n", avctx->codec_id);
2468 avctx->codec_id = codec->id;
2469 avctx->workaround_bugs = workaround_bugs;
2470 avctx->lowres = lowres;
2471 if(avctx->lowres > codec->max_lowres){
2472 av_log(avctx, AV_LOG_WARNING, "The maximum value for lowres supported by the decoder is %d\n",
2474 avctx->lowres= codec->max_lowres;
2476 avctx->idct_algo = idct;
2477 avctx->error_concealment = error_concealment;
2479 if(avctx->lowres) avctx->flags |= CODEC_FLAG_EMU_EDGE;
2480 if (fast) avctx->flags2 |= CODEC_FLAG2_FAST;
2481 if(codec->capabilities & CODEC_CAP_DR1)
2482 avctx->flags |= CODEC_FLAG_EMU_EDGE;
2484 opts = filter_codec_opts(codec_opts, avctx->codec_id, ic, ic->streams[stream_index], codec);
2485 if (!av_dict_get(opts, "threads", NULL, 0))
2486 av_dict_set(&opts, "threads", "auto", 0);
2487 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO || avctx->codec_type == AVMEDIA_TYPE_AUDIO)
2488 av_dict_set(&opts, "refcounted_frames", "1", 0);
2489 if (avcodec_open2(avctx, codec, &opts) < 0)
2491 if ((t = av_dict_get(opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
2492 av_log(NULL, AV_LOG_ERROR, "Option %s not found.\n", t->key);
2493 return AVERROR_OPTION_NOT_FOUND;
2496 ic->streams[stream_index]->discard = AVDISCARD_DEFAULT;
2497 switch (avctx->codec_type) {
2498 case AVMEDIA_TYPE_AUDIO:
2503 is->audio_filter_src.freq = avctx->sample_rate;
2504 is->audio_filter_src.channels = avctx->channels;
2505 is->audio_filter_src.channel_layout = get_valid_channel_layout(avctx->channel_layout, avctx->channels);
2506 is->audio_filter_src.fmt = avctx->sample_fmt;
2507 if ((ret = configure_audio_filters(is, afilters, 0)) < 0)
2509 link = is->out_audio_filter->inputs[0];
2510 sample_rate = link->sample_rate;
2511 nb_channels = link->channels;
2512 channel_layout = link->channel_layout;
2515 sample_rate = avctx->sample_rate;
2516 nb_channels = avctx->channels;
2517 channel_layout = avctx->channel_layout;
2520 /* prepare audio output */
2521 if ((ret = audio_open(is, channel_layout, nb_channels, sample_rate, &is->audio_tgt)) < 0)
2523 is->audio_hw_buf_size = ret;
2524 is->audio_src = is->audio_tgt;
2525 is->audio_buf_size = 0;
2526 is->audio_buf_index = 0;
2528 /* init averaging filter */
2529 is->audio_diff_avg_coef = exp(log(0.01) / AUDIO_DIFF_AVG_NB);
2530 is->audio_diff_avg_count = 0;
2531 /* since we do not have a precise anough audio fifo fullness,
2532 we correct audio sync only if larger than this threshold */
2533 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);
2535 memset(&is->audio_pkt, 0, sizeof(is->audio_pkt));
2536 memset(&is->audio_pkt_temp, 0, sizeof(is->audio_pkt_temp));
2538 is->audio_stream = stream_index;
2539 is->audio_st = ic->streams[stream_index];
2541 packet_queue_start(&is->audioq);
2544 case AVMEDIA_TYPE_VIDEO:
2545 is->video_stream = stream_index;
2546 is->video_st = ic->streams[stream_index];
2548 packet_queue_start(&is->videoq);
2549 is->video_tid = SDL_CreateThread(video_thread, is);
2550 is->queue_attachments_req = 1;
2552 case AVMEDIA_TYPE_SUBTITLE:
2553 is->subtitle_stream = stream_index;
2554 is->subtitle_st = ic->streams[stream_index];
2555 packet_queue_start(&is->subtitleq);
2557 is->subtitle_tid = SDL_CreateThread(subtitle_thread, is);
2565 static void stream_component_close(VideoState *is, int stream_index)
2567 AVFormatContext *ic = is->ic;
2568 AVCodecContext *avctx;
2570 if (stream_index < 0 || stream_index >= ic->nb_streams)
2572 avctx = ic->streams[stream_index]->codec;
2574 switch (avctx->codec_type) {
2575 case AVMEDIA_TYPE_AUDIO:
2576 packet_queue_abort(&is->audioq);
2580 packet_queue_flush(&is->audioq);
2581 av_free_packet(&is->audio_pkt);
2582 swr_free(&is->swr_ctx);
2583 av_freep(&is->audio_buf1);
2584 is->audio_buf1_size = 0;
2585 is->audio_buf = NULL;
2586 av_frame_free(&is->frame);
2589 av_rdft_end(is->rdft);
2590 av_freep(&is->rdft_data);
2595 avfilter_graph_free(&is->agraph);
2598 case AVMEDIA_TYPE_VIDEO:
2599 packet_queue_abort(&is->videoq);
2601 /* note: we also signal this mutex to make sure we deblock the
2602 video thread in all cases */
2603 SDL_LockMutex(is->pictq_mutex);
2604 SDL_CondSignal(is->pictq_cond);
2605 SDL_UnlockMutex(is->pictq_mutex);
2607 SDL_WaitThread(is->video_tid, NULL);
2609 packet_queue_flush(&is->videoq);
2611 case AVMEDIA_TYPE_SUBTITLE:
2612 packet_queue_abort(&is->subtitleq);
2614 /* note: we also signal this mutex to make sure we deblock the
2615 video thread in all cases */
2616 SDL_LockMutex(is->subpq_mutex);
2617 is->subtitle_stream_changed = 1;
2619 SDL_CondSignal(is->subpq_cond);
2620 SDL_UnlockMutex(is->subpq_mutex);
2622 SDL_WaitThread(is->subtitle_tid, NULL);
2624 packet_queue_flush(&is->subtitleq);
2630 ic->streams[stream_index]->discard = AVDISCARD_ALL;
2631 avcodec_close(avctx);
2632 switch (avctx->codec_type) {
2633 case AVMEDIA_TYPE_AUDIO:
2634 is->audio_st = NULL;
2635 is->audio_stream = -1;
2637 case AVMEDIA_TYPE_VIDEO:
2638 is->video_st = NULL;
2639 is->video_stream = -1;
2641 case AVMEDIA_TYPE_SUBTITLE:
2642 is->subtitle_st = NULL;
2643 is->subtitle_stream = -1;
2650 static int decode_interrupt_cb(void *ctx)
2652 VideoState *is = ctx;
2653 return is->abort_request;
2656 static int is_realtime(AVFormatContext *s)
2658 if( !strcmp(s->iformat->name, "rtp")
2659 || !strcmp(s->iformat->name, "rtsp")
2660 || !strcmp(s->iformat->name, "sdp")
2664 if(s->pb && ( !strncmp(s->filename, "rtp:", 4)
2665 || !strncmp(s->filename, "udp:", 4)
2672 /* this thread gets the stream from the disk or the network */
2673 static int read_thread(void *arg)
2675 VideoState *is = arg;
2676 AVFormatContext *ic = NULL;
2678 int st_index[AVMEDIA_TYPE_NB];
2679 AVPacket pkt1, *pkt = &pkt1;
2681 int pkt_in_play_range = 0;
2682 AVDictionaryEntry *t;
2683 AVDictionary **opts;
2684 int orig_nb_streams;
2685 SDL_mutex *wait_mutex = SDL_CreateMutex();
2687 memset(st_index, -1, sizeof(st_index));
2688 is->last_video_stream = is->video_stream = -1;
2689 is->last_audio_stream = is->audio_stream = -1;
2690 is->last_subtitle_stream = is->subtitle_stream = -1;
2692 ic = avformat_alloc_context();
2693 ic->interrupt_callback.callback = decode_interrupt_cb;
2694 ic->interrupt_callback.opaque = is;
2695 err = avformat_open_input(&ic, is->filename, is->iformat, &format_opts);
2697 print_error(is->filename, err);
2701 if ((t = av_dict_get(format_opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
2702 av_log(NULL, AV_LOG_ERROR, "Option %s not found.\n", t->key);
2703 ret = AVERROR_OPTION_NOT_FOUND;
2709 ic->flags |= AVFMT_FLAG_GENPTS;
2711 opts = setup_find_stream_info_opts(ic, codec_opts);
2712 orig_nb_streams = ic->nb_streams;
2714 err = avformat_find_stream_info(ic, opts);
2716 fprintf(stderr, "%s: could not find codec parameters\n", is->filename);
2720 for (i = 0; i < orig_nb_streams; i++)
2721 av_dict_free(&opts[i]);
2725 ic->pb->eof_reached = 0; // FIXME hack, ffplay maybe should not use url_feof() to test for the end
2727 if (seek_by_bytes < 0)
2728 seek_by_bytes = !!(ic->iformat->flags & AVFMT_TS_DISCONT) && strcmp("ogg", ic->iformat->name);
2730 is->max_frame_duration = (ic->iformat->flags & AVFMT_TS_DISCONT) ? 10.0 : 3600.0;
2732 /* if seeking requested, we execute it */
2733 if (start_time != AV_NOPTS_VALUE) {
2736 timestamp = start_time;
2737 /* add the stream start time */
2738 if (ic->start_time != AV_NOPTS_VALUE)
2739 timestamp += ic->start_time;
2740 ret = avformat_seek_file(ic, -1, INT64_MIN, timestamp, INT64_MAX, 0);
2742 fprintf(stderr, "%s: could not seek to position %0.3f\n",
2743 is->filename, (double)timestamp / AV_TIME_BASE);
2747 is->realtime = is_realtime(ic);
2749 for (i = 0; i < ic->nb_streams; i++)
2750 ic->streams[i]->discard = AVDISCARD_ALL;
2752 st_index[AVMEDIA_TYPE_VIDEO] =
2753 av_find_best_stream(ic, AVMEDIA_TYPE_VIDEO,
2754 wanted_stream[AVMEDIA_TYPE_VIDEO], -1, NULL, 0);
2756 st_index[AVMEDIA_TYPE_AUDIO] =
2757 av_find_best_stream(ic, AVMEDIA_TYPE_AUDIO,
2758 wanted_stream[AVMEDIA_TYPE_AUDIO],
2759 st_index[AVMEDIA_TYPE_VIDEO],
2761 if (!video_disable && !subtitle_disable)
2762 st_index[AVMEDIA_TYPE_SUBTITLE] =
2763 av_find_best_stream(ic, AVMEDIA_TYPE_SUBTITLE,
2764 wanted_stream[AVMEDIA_TYPE_SUBTITLE],
2765 (st_index[AVMEDIA_TYPE_AUDIO] >= 0 ?
2766 st_index[AVMEDIA_TYPE_AUDIO] :
2767 st_index[AVMEDIA_TYPE_VIDEO]),
2770 av_dump_format(ic, 0, is->filename, 0);
2773 is->show_mode = show_mode;
2775 /* open the streams */
2776 if (st_index[AVMEDIA_TYPE_AUDIO] >= 0) {
2777 stream_component_open(is, st_index[AVMEDIA_TYPE_AUDIO]);
2781 if (st_index[AVMEDIA_TYPE_VIDEO] >= 0) {
2782 ret = stream_component_open(is, st_index[AVMEDIA_TYPE_VIDEO]);
2784 if (is->show_mode == SHOW_MODE_NONE)
2785 is->show_mode = ret >= 0 ? SHOW_MODE_VIDEO : SHOW_MODE_RDFT;
2787 if (st_index[AVMEDIA_TYPE_SUBTITLE] >= 0) {
2788 stream_component_open(is, st_index[AVMEDIA_TYPE_SUBTITLE]);
2791 if (is->video_stream < 0 && is->audio_stream < 0) {
2792 fprintf(stderr, "%s: could not open codecs\n", is->filename);
2797 if (infinite_buffer < 0 && is->realtime)
2798 infinite_buffer = 1;
2801 if (is->abort_request)
2803 if (is->paused != is->last_paused) {
2804 is->last_paused = is->paused;
2806 is->read_pause_return = av_read_pause(ic);
2810 #if CONFIG_RTSP_DEMUXER || CONFIG_MMSH_PROTOCOL
2812 (!strcmp(ic->iformat->name, "rtsp") ||
2813 (ic->pb && !strncmp(input_filename, "mmsh:", 5)))) {
2814 /* wait 10 ms to avoid trying to get another packet */
2821 int64_t seek_target = is->seek_pos;
2822 int64_t seek_min = is->seek_rel > 0 ? seek_target - is->seek_rel + 2: INT64_MIN;
2823 int64_t seek_max = is->seek_rel < 0 ? seek_target - is->seek_rel - 2: INT64_MAX;
2824 // FIXME the +-2 is due to rounding being not done in the correct direction in generation
2825 // of the seek_pos/seek_rel variables
2827 ret = avformat_seek_file(is->ic, -1, seek_min, seek_target, seek_max, is->seek_flags);
2829 fprintf(stderr, "%s: error while seeking\n", is->ic->filename);
2831 if (is->audio_stream >= 0) {
2832 packet_queue_flush(&is->audioq);
2833 packet_queue_put(&is->audioq, &flush_pkt);
2835 if (is->subtitle_stream >= 0) {
2836 packet_queue_flush(&is->subtitleq);
2837 packet_queue_put(&is->subtitleq, &flush_pkt);
2839 if (is->video_stream >= 0) {
2840 packet_queue_flush(&is->videoq);
2841 packet_queue_put(&is->videoq, &flush_pkt);
2843 if (is->seek_flags & AVSEEK_FLAG_BYTE) {
2844 update_external_clock_pts(is, NAN);
2846 update_external_clock_pts(is, seek_target / (double)AV_TIME_BASE);
2850 is->queue_attachments_req = 1;
2853 step_to_next_frame(is);
2855 if (is->queue_attachments_req) {
2856 if (is->video_st && is->video_st->disposition & AV_DISPOSITION_ATTACHED_PIC) {
2858 if ((ret = av_copy_packet(©, &is->video_st->attached_pic)) < 0)
2860 packet_queue_put(&is->videoq, ©);
2862 is->queue_attachments_req = 0;
2865 /* if the queue are full, no need to read more */
2866 if (infinite_buffer<1 &&
2867 (is->audioq.size + is->videoq.size + is->subtitleq.size > MAX_QUEUE_SIZE
2868 || ( (is->audioq .nb_packets > MIN_FRAMES || is->audio_stream < 0 || is->audioq.abort_request)
2869 && (is->videoq .nb_packets > MIN_FRAMES || is->video_stream < 0 || is->videoq.abort_request
2870 || (is->video_st->disposition & AV_DISPOSITION_ATTACHED_PIC))
2871 && (is->subtitleq.nb_packets > MIN_FRAMES || is->subtitle_stream < 0 || is->subtitleq.abort_request)))) {
2873 SDL_LockMutex(wait_mutex);
2874 SDL_CondWaitTimeout(is->continue_read_thread, wait_mutex, 10);
2875 SDL_UnlockMutex(wait_mutex);
2879 if (is->video_stream >= 0) {
2880 av_init_packet(pkt);
2883 pkt->stream_index = is->video_stream;
2884 packet_queue_put(&is->videoq, pkt);
2886 if (is->audio_stream >= 0 &&
2887 is->audio_st->codec->codec->capabilities & CODEC_CAP_DELAY) {
2888 av_init_packet(pkt);
2891 pkt->stream_index = is->audio_stream;
2892 packet_queue_put(&is->audioq, pkt);
2895 if (is->audioq.size + is->videoq.size + is->subtitleq.size == 0) {
2896 if (loop != 1 && (!loop || --loop)) {
2897 stream_seek(is, start_time != AV_NOPTS_VALUE ? start_time : 0, 0, 0);
2898 } else if (autoexit) {
2906 ret = av_read_frame(ic, pkt);
2908 if (ret == AVERROR_EOF || url_feof(ic->pb))
2910 if (ic->pb && ic->pb->error)
2912 SDL_LockMutex(wait_mutex);
2913 SDL_CondWaitTimeout(is->continue_read_thread, wait_mutex, 10);
2914 SDL_UnlockMutex(wait_mutex);
2917 /* check if packet is in play range specified by user, then queue, otherwise discard */
2918 pkt_in_play_range = duration == AV_NOPTS_VALUE ||
2919 (pkt->pts - ic->streams[pkt->stream_index]->start_time) *
2920 av_q2d(ic->streams[pkt->stream_index]->time_base) -
2921 (double)(start_time != AV_NOPTS_VALUE ? start_time : 0) / 1000000
2922 <= ((double)duration / 1000000);
2923 if (pkt->stream_index == is->audio_stream && pkt_in_play_range) {
2924 packet_queue_put(&is->audioq, pkt);
2925 } else if (pkt->stream_index == is->video_stream && pkt_in_play_range
2926 && !(is->video_st->disposition & AV_DISPOSITION_ATTACHED_PIC)) {
2927 packet_queue_put(&is->videoq, pkt);
2928 } else if (pkt->stream_index == is->subtitle_stream && pkt_in_play_range) {
2929 packet_queue_put(&is->subtitleq, pkt);
2931 av_free_packet(pkt);
2934 /* wait until the end */
2935 while (!is->abort_request) {
2941 /* close each stream */
2942 if (is->audio_stream >= 0)
2943 stream_component_close(is, is->audio_stream);
2944 if (is->video_stream >= 0)
2945 stream_component_close(is, is->video_stream);
2946 if (is->subtitle_stream >= 0)
2947 stream_component_close(is, is->subtitle_stream);
2949 avformat_close_input(&is->ic);
2955 event.type = FF_QUIT_EVENT;
2956 event.user.data1 = is;
2957 SDL_PushEvent(&event);
2959 SDL_DestroyMutex(wait_mutex);
2963 static VideoState *stream_open(const char *filename, AVInputFormat *iformat)
2967 is = av_mallocz(sizeof(VideoState));
2970 av_strlcpy(is->filename, filename, sizeof(is->filename));
2971 is->iformat = iformat;
2975 /* start video display */
2976 is->pictq_mutex = SDL_CreateMutex();
2977 is->pictq_cond = SDL_CreateCond();
2979 is->subpq_mutex = SDL_CreateMutex();
2980 is->subpq_cond = SDL_CreateCond();
2982 packet_queue_init(&is->videoq);
2983 packet_queue_init(&is->audioq);
2984 packet_queue_init(&is->subtitleq);
2986 is->continue_read_thread = SDL_CreateCond();
2988 update_external_clock_pts(is, NAN);
2989 update_external_clock_speed(is, 1.0);
2990 is->audio_current_pts_drift = -av_gettime() / 1000000.0;
2991 is->video_current_pts_drift = is->audio_current_pts_drift;
2992 is->audio_clock_serial = -1;
2993 is->video_clock_serial = -1;
2994 is->audio_last_serial = -1;
2995 is->av_sync_type = av_sync_type;
2996 is->read_tid = SDL_CreateThread(read_thread, is);
2997 if (!is->read_tid) {
3004 static void stream_cycle_channel(VideoState *is, int codec_type)
3006 AVFormatContext *ic = is->ic;
3007 int start_index, stream_index;
3011 if (codec_type == AVMEDIA_TYPE_VIDEO) {
3012 start_index = is->last_video_stream;
3013 old_index = is->video_stream;
3014 } else if (codec_type == AVMEDIA_TYPE_AUDIO) {
3015 start_index = is->last_audio_stream;
3016 old_index = is->audio_stream;
3018 start_index = is->last_subtitle_stream;
3019 old_index = is->subtitle_stream;
3021 stream_index = start_index;
3023 if (++stream_index >= is->ic->nb_streams)
3025 if (codec_type == AVMEDIA_TYPE_SUBTITLE)
3028 is->last_subtitle_stream = -1;
3031 if (start_index == -1)
3035 if (stream_index == start_index)
3037 st = ic->streams[stream_index];
3038 if (st->codec->codec_type == codec_type) {
3039 /* check that parameters are OK */
3040 switch (codec_type) {
3041 case AVMEDIA_TYPE_AUDIO:
3042 if (st->codec->sample_rate != 0 &&
3043 st->codec->channels != 0)
3046 case AVMEDIA_TYPE_VIDEO:
3047 case AVMEDIA_TYPE_SUBTITLE:
3055 stream_component_close(is, old_index);
3056 stream_component_open(is, stream_index);
3060 static void toggle_full_screen(VideoState *is)
3062 #if defined(__APPLE__) && SDL_VERSION_ATLEAST(1, 2, 14)
3063 /* OS X needs to reallocate the SDL overlays */
3065 for (i = 0; i < VIDEO_PICTURE_QUEUE_SIZE; i++)
3066 is->pictq[i].reallocate = 1;
3068 is_full_screen = !is_full_screen;
3069 video_open(is, 1, NULL);
3072 static void toggle_audio_display(VideoState *is)
3074 int bgcolor = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00);
3075 int next = is->show_mode;
3077 next = (next + 1) % SHOW_MODE_NB;
3078 } while (next != is->show_mode && (next == SHOW_MODE_VIDEO && !is->video_st || next != SHOW_MODE_VIDEO && !is->audio_st));
3079 if (is->show_mode != next) {
3080 fill_rectangle(screen,
3081 is->xleft, is->ytop, is->width, is->height,
3083 is->force_refresh = 1;
3084 is->show_mode = next;
3088 static void refresh_loop_wait_event(VideoState *is, SDL_Event *event) {
3089 double remaining_time = 0.0;
3091 while (!SDL_PeepEvents(event, 1, SDL_GETEVENT, SDL_ALLEVENTS)) {
3092 if (!cursor_hidden && av_gettime() - cursor_last_shown > CURSOR_HIDE_DELAY) {
3096 if (remaining_time > 0.0)
3097 av_usleep((int64_t)(remaining_time * 1000000.0));
3098 remaining_time = REFRESH_RATE;
3099 if (is->show_mode != SHOW_MODE_NONE && (!is->paused || is->force_refresh))
3100 video_refresh(is, &remaining_time);
3105 /* handle an event sent by the GUI */
3106 static void event_loop(VideoState *cur_stream)
3109 double incr, pos, frac;
3113 refresh_loop_wait_event(cur_stream, &event);
3114 switch (event.type) {
3116 if (exit_on_keydown) {
3117 do_exit(cur_stream);
3120 switch (event.key.keysym.sym) {
3123 do_exit(cur_stream);
3126 toggle_full_screen(cur_stream);
3127 cur_stream->force_refresh = 1;
3131 toggle_pause(cur_stream);
3133 case SDLK_s: // S: Step to next frame
3134 step_to_next_frame(cur_stream);
3137 stream_cycle_channel(cur_stream, AVMEDIA_TYPE_AUDIO);
3140 stream_cycle_channel(cur_stream, AVMEDIA_TYPE_VIDEO);
3143 stream_cycle_channel(cur_stream, AVMEDIA_TYPE_SUBTITLE);
3146 toggle_audio_display(cur_stream);
3166 if (seek_by_bytes) {
3167 if (cur_stream->video_stream >= 0 && cur_stream->video_current_pos >= 0) {
3168 pos = cur_stream->video_current_pos;
3169 } else if (cur_stream->audio_stream >= 0 && cur_stream->audio_pkt.pos >= 0) {
3170 pos = cur_stream->audio_pkt.pos;
3172 pos = avio_tell(cur_stream->ic->pb);
3173 if (cur_stream->ic->bit_rate)
3174 incr *= cur_stream->ic->bit_rate / 8.0;
3178 stream_seek(cur_stream, pos, incr, 1);
3180 pos = get_master_clock(cur_stream);
3182 pos = (double)cur_stream->seek_pos / AV_TIME_BASE;
3184 if (cur_stream->ic->start_time != AV_NOPTS_VALUE && pos < cur_stream->ic->start_time / (double)AV_TIME_BASE)
3185 pos = cur_stream->ic->start_time / (double)AV_TIME_BASE;
3186 stream_seek(cur_stream, (int64_t)(pos * AV_TIME_BASE), (int64_t)(incr * AV_TIME_BASE), 0);
3193 case SDL_VIDEOEXPOSE:
3194 cur_stream->force_refresh = 1;
3196 case SDL_MOUSEBUTTONDOWN:
3197 if (exit_on_mousedown) {
3198 do_exit(cur_stream);
3201 case SDL_MOUSEMOTION:
3202 if (cursor_hidden) {
3206 cursor_last_shown = av_gettime();
3207 if (event.type == SDL_MOUSEBUTTONDOWN) {
3210 if (event.motion.state != SDL_PRESSED)
3214 if (seek_by_bytes || cur_stream->ic->duration <= 0) {
3215 uint64_t size = avio_size(cur_stream->ic->pb);
3216 stream_seek(cur_stream, size*x/cur_stream->width, 0, 1);
3220 int tns, thh, tmm, tss;
3221 tns = cur_stream->ic->duration / 1000000LL;
3223 tmm = (tns % 3600) / 60;
3225 frac = x / cur_stream->width;
3228 mm = (ns % 3600) / 60;
3230 fprintf(stderr, "Seek to %2.0f%% (%2d:%02d:%02d) of total duration (%2d:%02d:%02d) \n", frac*100,
3231 hh, mm, ss, thh, tmm, tss);
3232 ts = frac * cur_stream->ic->duration;
3233 if (cur_stream->ic->start_time != AV_NOPTS_VALUE)
3234 ts += cur_stream->ic->start_time;
3235 stream_seek(cur_stream, ts, 0, 0);
3238 case SDL_VIDEORESIZE:
3239 screen = SDL_SetVideoMode(event.resize.w, event.resize.h, 0,
3240 SDL_HWSURFACE|SDL_RESIZABLE|SDL_ASYNCBLIT|SDL_HWACCEL);
3241 screen_width = cur_stream->width = event.resize.w;
3242 screen_height = cur_stream->height = event.resize.h;
3243 cur_stream->force_refresh = 1;
3247 do_exit(cur_stream);
3249 case FF_ALLOC_EVENT:
3250 alloc_picture(event.user.data1);
3258 static int opt_frame_size(void *optctx, const char *opt, const char *arg)
3260 av_log(NULL, AV_LOG_WARNING, "Option -s is deprecated, use -video_size.\n");
3261 return opt_default(NULL, "video_size", arg);
3264 static int opt_width(void *optctx, const char *opt, const char *arg)
3266 screen_width = parse_number_or_die(opt, arg, OPT_INT64, 1, INT_MAX);
3270 static int opt_height(void *optctx, const char *opt, const char *arg)
3272 screen_height = parse_number_or_die(opt, arg, OPT_INT64, 1, INT_MAX);
3276 static int opt_format(void *optctx, const char *opt, const char *arg)
3278 file_iformat = av_find_input_format(arg);
3279 if (!file_iformat) {
3280 fprintf(stderr, "Unknown input format: %s\n", arg);
3281 return AVERROR(EINVAL);
3286 static int opt_frame_pix_fmt(void *optctx, const char *opt, const char *arg)
3288 av_log(NULL, AV_LOG_WARNING, "Option -pix_fmt is deprecated, use -pixel_format.\n");
3289 return opt_default(NULL, "pixel_format", arg);
3292 static int opt_sync(void *optctx, const char *opt, const char *arg)
3294 if (!strcmp(arg, "audio"))
3295 av_sync_type = AV_SYNC_AUDIO_MASTER;
3296 else if (!strcmp(arg, "video"))
3297 av_sync_type = AV_SYNC_VIDEO_MASTER;
3298 else if (!strcmp(arg, "ext"))
3299 av_sync_type = AV_SYNC_EXTERNAL_CLOCK;
3301 fprintf(stderr, "Unknown value for %s: %s\n", opt, arg);
3307 static int opt_seek(void *optctx, const char *opt, const char *arg)
3309 start_time = parse_time_or_die(opt, arg, 1);
3313 static int opt_duration(void *optctx, const char *opt, const char *arg)
3315 duration = parse_time_or_die(opt, arg, 1);
3319 static int opt_show_mode(void *optctx, const char *opt, const char *arg)
3321 show_mode = !strcmp(arg, "video") ? SHOW_MODE_VIDEO :
3322 !strcmp(arg, "waves") ? SHOW_MODE_WAVES :
3323 !strcmp(arg, "rdft" ) ? SHOW_MODE_RDFT :
3324 parse_number_or_die(opt, arg, OPT_INT, 0, SHOW_MODE_NB-1);
3328 static void opt_input_file(void *optctx, const char *filename)
3330 if (input_filename) {
3331 fprintf(stderr, "Argument '%s' provided as input filename, but '%s' was already specified.\n",
3332 filename, input_filename);
3335 if (!strcmp(filename, "-"))
3337 input_filename = filename;
3340 static int opt_codec(void *optctx, const char *opt, const char *arg)
3342 const char *spec = strchr(opt, ':');
3344 fprintf(stderr, "No media specifier was specified in '%s' in option '%s'\n",
3346 return AVERROR(EINVAL);
3350 case 'a' : audio_codec_name = arg; break;
3351 case 's' : subtitle_codec_name = arg; break;
3352 case 'v' : video_codec_name = arg; break;
3354 fprintf(stderr, "Invalid media specifier '%s' in option '%s'\n", spec, opt);
3355 return AVERROR(EINVAL);
3362 static const OptionDef options[] = {
3363 #include "cmdutils_common_opts.h"
3364 { "x", HAS_ARG, { .func_arg = opt_width }, "force displayed width", "width" },
3365 { "y", HAS_ARG, { .func_arg = opt_height }, "force displayed height", "height" },
3366 { "s", HAS_ARG | OPT_VIDEO, { .func_arg = opt_frame_size }, "set frame size (WxH or abbreviation)", "size" },
3367 { "fs", OPT_BOOL, { &is_full_screen }, "force full screen" },
3368 { "an", OPT_BOOL, { &audio_disable }, "disable audio" },
3369 { "vn", OPT_BOOL, { &video_disable }, "disable video" },
3370 { "sn", OPT_BOOL, { &subtitle_disable }, "disable subtitling" },
3371 { "ast", OPT_INT | HAS_ARG | OPT_EXPERT, { &wanted_stream[AVMEDIA_TYPE_AUDIO] }, "select desired audio stream", "stream_number" },
3372 { "vst", OPT_INT | HAS_ARG | OPT_EXPERT, { &wanted_stream[AVMEDIA_TYPE_VIDEO] }, "select desired video stream", "stream_number" },
3373 { "sst", OPT_INT | HAS_ARG | OPT_EXPERT, { &wanted_stream[AVMEDIA_TYPE_SUBTITLE] }, "select desired subtitle stream", "stream_number" },
3374 { "ss", HAS_ARG, { .func_arg = opt_seek }, "seek to a given position in seconds", "pos" },
3375 { "t", HAS_ARG, { .func_arg = opt_duration }, "play \"duration\" seconds of audio/video", "duration" },
3376 { "bytes", OPT_INT | HAS_ARG, { &seek_by_bytes }, "seek by bytes 0=off 1=on -1=auto", "val" },
3377 { "nodisp", OPT_BOOL, { &display_disable }, "disable graphical display" },
3378 { "f", HAS_ARG, { .func_arg = opt_format }, "force format", "fmt" },
3379 { "pix_fmt", HAS_ARG | OPT_EXPERT | OPT_VIDEO, { .func_arg = opt_frame_pix_fmt }, "set pixel format", "format" },
3380 { "stats", OPT_BOOL | OPT_EXPERT, { &show_status }, "show status", "" },
3381 { "bug", OPT_INT | HAS_ARG | OPT_EXPERT, { &workaround_bugs }, "workaround bugs", "" },
3382 { "fast", OPT_BOOL | OPT_EXPERT, { &fast }, "non spec compliant optimizations", "" },
3383 { "genpts", OPT_BOOL | OPT_EXPERT, { &genpts }, "generate pts", "" },
3384 { "drp", OPT_INT | HAS_ARG | OPT_EXPERT, { &decoder_reorder_pts }, "let decoder reorder pts 0=off 1=on -1=auto", ""},
3385 { "lowres", OPT_INT | HAS_ARG | OPT_EXPERT, { &lowres }, "", "" },
3386 { "idct", OPT_INT | HAS_ARG | OPT_EXPERT, { &idct }, "set idct algo", "algo" },
3387 { "ec", OPT_INT | HAS_ARG | OPT_EXPERT, { &error_concealment }, "set error concealment options", "bit_mask" },
3388 { "sync", HAS_ARG | OPT_EXPERT, { .func_arg = opt_sync }, "set audio-video sync. type (type=audio/video/ext)", "type" },
3389 { "autoexit", OPT_BOOL | OPT_EXPERT, { &autoexit }, "exit at the end", "" },
3390 { "exitonkeydown", OPT_BOOL | OPT_EXPERT, { &exit_on_keydown }, "exit on key down", "" },
3391 { "exitonmousedown", OPT_BOOL | OPT_EXPERT, { &exit_on_mousedown }, "exit on mouse down", "" },
3392 { "loop", OPT_INT | HAS_ARG | OPT_EXPERT, { &loop }, "set number of times the playback shall be looped", "loop count" },
3393 { "framedrop", OPT_BOOL | OPT_EXPERT, { &framedrop }, "drop frames when cpu is too slow", "" },
3394 { "infbuf", OPT_BOOL | OPT_EXPERT, { &infinite_buffer }, "don't limit the input buffer size (useful with realtime streams)", "" },
3395 { "window_title", OPT_STRING | HAS_ARG, { &window_title }, "set window title", "window title" },
3397 { "vf", OPT_STRING | HAS_ARG, { &vfilters }, "set video filters", "filter_graph" },
3398 { "af", OPT_STRING | HAS_ARG, { &afilters }, "set audio filters", "filter_graph" },
3400 { "rdftspeed", OPT_INT | HAS_ARG| OPT_AUDIO | OPT_EXPERT, { &rdftspeed }, "rdft speed", "msecs" },
3401 { "showmode", HAS_ARG, { .func_arg = opt_show_mode}, "select show mode (0 = video, 1 = waves, 2 = RDFT)", "mode" },
3402 { "default", HAS_ARG | OPT_AUDIO | OPT_VIDEO | OPT_EXPERT, { .func_arg = opt_default }, "generic catch all option", "" },
3403 { "i", OPT_BOOL, { &dummy}, "read specified file", "input_file"},
3404 { "codec", HAS_ARG, { .func_arg = opt_codec}, "force decoder", "decoder_name" },
3405 { "acodec", HAS_ARG | OPT_STRING | OPT_EXPERT, { &audio_codec_name }, "force audio decoder", "decoder_name" },
3406 { "scodec", HAS_ARG | OPT_STRING | OPT_EXPERT, { &subtitle_codec_name }, "force subtitle decoder", "decoder_name" },
3407 { "vcodec", HAS_ARG | OPT_STRING | OPT_EXPERT, { &video_codec_name }, "force video decoder", "decoder_name" },
3411 static void show_usage(void)
3413 av_log(NULL, AV_LOG_INFO, "Simple media player\n");
3414 av_log(NULL, AV_LOG_INFO, "usage: %s [options] input_file\n", program_name);
3415 av_log(NULL, AV_LOG_INFO, "\n");
3418 void show_help_default(const char *opt, const char *arg)
3420 av_log_set_callback(log_callback_help);
3422 show_help_options(options, "Main options:", 0, OPT_EXPERT, 0);
3423 show_help_options(options, "Advanced options:", OPT_EXPERT, 0, 0);
3425 show_help_children(avcodec_get_class(), AV_OPT_FLAG_DECODING_PARAM);
3426 show_help_children(avformat_get_class(), AV_OPT_FLAG_DECODING_PARAM);
3427 #if !CONFIG_AVFILTER
3428 show_help_children(sws_get_class(), AV_OPT_FLAG_ENCODING_PARAM);
3430 show_help_children(avfilter_get_class(), AV_OPT_FLAG_FILTERING_PARAM);
3432 printf("\nWhile playing:\n"
3434 "f toggle full screen\n"
3436 "a cycle audio channel\n"
3437 "v cycle video channel\n"
3438 "t cycle subtitle channel\n"
3439 "w show audio waves\n"
3440 "s activate frame-step mode\n"
3441 "left/right seek backward/forward 10 seconds\n"
3442 "down/up seek backward/forward 1 minute\n"
3443 "page down/page up seek backward/forward 10 minutes\n"
3444 "mouse click seek to percentage in file corresponding to fraction of width\n"
3448 static int lockmgr(void **mtx, enum AVLockOp op)
3451 case AV_LOCK_CREATE:
3452 *mtx = SDL_CreateMutex();
3456 case AV_LOCK_OBTAIN:
3457 return !!SDL_LockMutex(*mtx);
3458 case AV_LOCK_RELEASE:
3459 return !!SDL_UnlockMutex(*mtx);
3460 case AV_LOCK_DESTROY:
3461 SDL_DestroyMutex(*mtx);
3467 /* Called from the main */
3468 int main(int argc, char **argv)
3472 char dummy_videodriver[] = "SDL_VIDEODRIVER=dummy";
3474 av_log_set_flags(AV_LOG_SKIP_REPEATED);
3475 parse_loglevel(argc, argv, options);
3477 /* register all codecs, demux and protocols */
3478 avcodec_register_all();
3480 avdevice_register_all();
3483 avfilter_register_all();
3486 avformat_network_init();
3490 signal(SIGINT , sigterm_handler); /* Interrupt (ANSI). */
3491 signal(SIGTERM, sigterm_handler); /* Termination (ANSI). */
3493 show_banner(argc, argv, options);
3495 parse_options(NULL, argc, argv, options, opt_input_file);
3497 if (!input_filename) {
3499 fprintf(stderr, "An input file must be specified\n");
3500 fprintf(stderr, "Use -h to get full help or, even better, run 'man %s'\n", program_name);
3504 if (display_disable) {
3507 flags = SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER;
3509 flags &= ~SDL_INIT_AUDIO;
3510 if (display_disable)
3511 SDL_putenv(dummy_videodriver); /* For the event queue, we always need a video driver. */
3512 #if !defined(__MINGW32__) && !defined(__APPLE__)
3513 flags |= SDL_INIT_EVENTTHREAD; /* Not supported on Windows or Mac OS X */
3515 if (SDL_Init (flags)) {
3516 fprintf(stderr, "Could not initialize SDL - %s\n", SDL_GetError());
3517 fprintf(stderr, "(Did you set the DISPLAY variable?)\n");
3521 if (!display_disable) {
3522 const SDL_VideoInfo *vi = SDL_GetVideoInfo();
3523 fs_screen_width = vi->current_w;
3524 fs_screen_height = vi->current_h;
3527 SDL_EventState(SDL_ACTIVEEVENT, SDL_IGNORE);
3528 SDL_EventState(SDL_SYSWMEVENT, SDL_IGNORE);
3529 SDL_EventState(SDL_USEREVENT, SDL_IGNORE);
3531 if (av_lockmgr_register(lockmgr)) {
3532 fprintf(stderr, "Could not initialize lock manager!\n");
3536 av_init_packet(&flush_pkt);
3537 flush_pkt.data = (char *)(intptr_t)"FLUSH";
3539 is = stream_open(input_filename, file_iformat);
3541 fprintf(stderr, "Failed to initialize VideoState!\n");