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 /* NOTE: the size must be big enough to compensate the hardware audio buffersize size */
90 /* TODO: We assume that a decoded and resampled frame fits into this buffer */
91 #define SAMPLE_ARRAY_SIZE (8 * 65536)
93 static int64_t sws_flags = SWS_BICUBIC;
95 typedef struct MyAVPacketList {
97 struct MyAVPacketList *next;
101 typedef struct PacketQueue {
102 MyAVPacketList *first_pkt, *last_pkt;
111 #define VIDEO_PICTURE_QUEUE_SIZE 4
112 #define SUBPICTURE_QUEUE_SIZE 4
114 typedef struct VideoPicture {
115 double pts; // presentation timestamp for this picture
116 int64_t pos; // byte position in file
119 int width, height; /* source height & width */
120 AVRational sample_aspect_ratio;
126 AVFilterBufferRef *picref;
130 typedef struct SubPicture {
131 double pts; /* presentation time stamp for this picture */
135 typedef struct AudioParams {
139 enum AVSampleFormat fmt;
143 AV_SYNC_AUDIO_MASTER, /* default choice */
144 AV_SYNC_VIDEO_MASTER,
145 AV_SYNC_EXTERNAL_CLOCK, /* synchronize to an external clock */
148 typedef struct VideoState {
149 SDL_Thread *read_tid;
150 SDL_Thread *video_tid;
151 SDL_Thread *refresh_tid;
152 AVInputFormat *iformat;
158 int que_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 double audio_diff_cum; /* used for AV difference average computation */
177 double audio_diff_avg_coef;
178 double audio_diff_threshold;
179 int audio_diff_avg_count;
182 int audio_hw_buf_size;
183 DECLARE_ALIGNED(16,uint8_t,audio_buf2)[AVCODEC_MAX_AUDIO_FRAME_SIZE * 4];
184 uint8_t silence_buf[SDL_AUDIO_BUFFER_SIZE];
187 unsigned int audio_buf_size; /* in bytes */
188 int audio_buf_index; /* in bytes */
189 int audio_write_buf_size;
190 AVPacket audio_pkt_temp;
192 int audio_pkt_temp_serial;
193 struct AudioParams audio_src;
194 struct AudioParams audio_tgt;
195 struct SwrContext *swr_ctx;
196 double audio_current_pts;
197 double audio_current_pts_drift;
198 int frame_drops_early;
199 int frame_drops_late;
203 SHOW_MODE_NONE = -1, SHOW_MODE_VIDEO = 0, SHOW_MODE_WAVES, SHOW_MODE_RDFT, SHOW_MODE_NB
205 int16_t sample_array[SAMPLE_ARRAY_SIZE];
206 int sample_array_index;
210 FFTSample *rdft_data;
213 SDL_Thread *subtitle_tid;
215 int subtitle_stream_changed;
216 AVStream *subtitle_st;
217 PacketQueue subtitleq;
218 SubPicture subpq[SUBPICTURE_QUEUE_SIZE];
219 int subpq_size, subpq_rindex, subpq_windex;
220 SDL_mutex *subpq_mutex;
221 SDL_cond *subpq_cond;
224 double frame_last_pts;
225 double frame_last_duration;
226 double frame_last_dropped_pts;
227 double frame_last_returned_time;
228 double frame_last_filter_delay;
229 int64_t frame_last_dropped_pos;
230 double video_clock; // pts of last decoded frame / predicted pts of next decoded frame
234 double video_current_pts; // current displayed pts (different from video_clock if frame fifos are used)
235 double video_current_pts_drift; // video_current_pts - time (av_gettime) at which we updated video_current_pts - used to have running video pts
236 int64_t video_current_pos; // current displayed file pos
237 double max_frame_duration; // maximum duration of a frame - above this, we consider the jump a timestamp discontinuity
238 VideoPicture pictq[VIDEO_PICTURE_QUEUE_SIZE];
239 int pictq_size, pictq_rindex, pictq_windex;
240 SDL_mutex *pictq_mutex;
241 SDL_cond *pictq_cond;
243 struct SwsContext *img_convert_ctx;
245 SDL_Rect last_display_rect;
248 int width, height, xleft, ytop;
252 AVFilterContext *in_video_filter; // the first filter in the video chain
253 AVFilterContext *out_video_filter; // the last filter in the video chain
255 FrameBuffer *buffer_pool;
259 int last_video_stream, last_audio_stream, last_subtitle_stream;
261 SDL_cond *continue_read_thread;
264 /* options specified by the user */
265 static AVInputFormat *file_iformat;
266 static const char *input_filename;
267 static const char *window_title;
268 static int fs_screen_width;
269 static int fs_screen_height;
270 static int screen_width = 0;
271 static int screen_height = 0;
272 static int audio_disable;
273 static int video_disable;
274 static int wanted_stream[AVMEDIA_TYPE_NB] = {
275 [AVMEDIA_TYPE_AUDIO] = -1,
276 [AVMEDIA_TYPE_VIDEO] = -1,
277 [AVMEDIA_TYPE_SUBTITLE] = -1,
279 static int seek_by_bytes = -1;
280 static int display_disable;
281 static int show_status = 1;
282 static int av_sync_type = AV_SYNC_AUDIO_MASTER;
283 static int64_t start_time = AV_NOPTS_VALUE;
284 static int64_t duration = AV_NOPTS_VALUE;
285 static int workaround_bugs = 1;
287 static int genpts = 0;
288 static int lowres = 0;
289 static int idct = FF_IDCT_AUTO;
290 static enum AVDiscard skip_frame = AVDISCARD_DEFAULT;
291 static enum AVDiscard skip_idct = AVDISCARD_DEFAULT;
292 static enum AVDiscard skip_loop_filter = AVDISCARD_DEFAULT;
293 static int error_concealment = 3;
294 static int decoder_reorder_pts = -1;
296 static int exit_on_keydown;
297 static int exit_on_mousedown;
299 static int framedrop = -1;
300 static int infinite_buffer = -1;
301 static enum ShowMode show_mode = SHOW_MODE_NONE;
302 static const char *audio_codec_name;
303 static const char *subtitle_codec_name;
304 static const char *video_codec_name;
305 static int rdftspeed = 20;
307 static char *vfilters = NULL;
310 /* current context */
311 static int is_full_screen;
312 static int64_t audio_callback_time;
314 static AVPacket flush_pkt;
316 #define FF_ALLOC_EVENT (SDL_USEREVENT)
317 #define FF_REFRESH_EVENT (SDL_USEREVENT + 1)
318 #define FF_QUIT_EVENT (SDL_USEREVENT + 2)
320 static SDL_Surface *screen;
322 static int packet_queue_put(PacketQueue *q, AVPacket *pkt);
324 static int packet_queue_put_private(PacketQueue *q, AVPacket *pkt)
326 MyAVPacketList *pkt1;
328 if (q->abort_request)
331 pkt1 = av_malloc(sizeof(MyAVPacketList));
336 if (pkt == &flush_pkt)
338 pkt1->serial = q->serial;
343 q->last_pkt->next = pkt1;
346 q->size += pkt1->pkt.size + sizeof(*pkt1);
347 /* XXX: should duplicate packet data in DV case */
348 SDL_CondSignal(q->cond);
352 static int packet_queue_put(PacketQueue *q, AVPacket *pkt)
356 /* duplicate the packet */
357 if (pkt != &flush_pkt && av_dup_packet(pkt) < 0)
360 SDL_LockMutex(q->mutex);
361 ret = packet_queue_put_private(q, pkt);
362 SDL_UnlockMutex(q->mutex);
364 if (pkt != &flush_pkt && ret < 0)
370 /* packet queue handling */
371 static void packet_queue_init(PacketQueue *q)
373 memset(q, 0, sizeof(PacketQueue));
374 q->mutex = SDL_CreateMutex();
375 q->cond = SDL_CreateCond();
376 q->abort_request = 1;
379 static void packet_queue_flush(PacketQueue *q)
381 MyAVPacketList *pkt, *pkt1;
383 SDL_LockMutex(q->mutex);
384 for (pkt = q->first_pkt; pkt != NULL; pkt = pkt1) {
386 av_free_packet(&pkt->pkt);
393 SDL_UnlockMutex(q->mutex);
396 static void packet_queue_destroy(PacketQueue *q)
398 packet_queue_flush(q);
399 SDL_DestroyMutex(q->mutex);
400 SDL_DestroyCond(q->cond);
403 static void packet_queue_abort(PacketQueue *q)
405 SDL_LockMutex(q->mutex);
407 q->abort_request = 1;
409 SDL_CondSignal(q->cond);
411 SDL_UnlockMutex(q->mutex);
414 static void packet_queue_start(PacketQueue *q)
416 SDL_LockMutex(q->mutex);
417 q->abort_request = 0;
418 packet_queue_put_private(q, &flush_pkt);
419 SDL_UnlockMutex(q->mutex);
422 /* return < 0 if aborted, 0 if no packet and > 0 if packet. */
423 static int packet_queue_get(PacketQueue *q, AVPacket *pkt, int block, int *serial)
425 MyAVPacketList *pkt1;
428 SDL_LockMutex(q->mutex);
431 if (q->abort_request) {
438 q->first_pkt = pkt1->next;
442 q->size -= pkt1->pkt.size + sizeof(*pkt1);
445 *serial = pkt1->serial;
453 SDL_CondWait(q->cond, q->mutex);
456 SDL_UnlockMutex(q->mutex);
460 static inline void fill_rectangle(SDL_Surface *screen,
461 int x, int y, int w, int h, int color, int update)
468 SDL_FillRect(screen, &rect, color);
469 if (update && w > 0 && h > 0)
470 SDL_UpdateRect(screen, x, y, w, h);
473 /* draw only the border of a rectangle */
474 static void fill_border(int xleft, int ytop, int width, int height, int x, int y, int w, int h, int color, int update)
478 /* fill the background */
482 w2 = width - (x + w);
488 h2 = height - (y + h);
491 fill_rectangle(screen,
495 fill_rectangle(screen,
496 xleft + width - w2, ytop,
499 fill_rectangle(screen,
503 fill_rectangle(screen,
504 xleft + w1, ytop + height - h2,
509 #define ALPHA_BLEND(a, oldp, newp, s)\
510 ((((oldp << s) * (255 - (a))) + (newp * (a))) / (255 << s))
512 #define RGBA_IN(r, g, b, a, s)\
514 unsigned int v = ((const uint32_t *)(s))[0];\
515 a = (v >> 24) & 0xff;\
516 r = (v >> 16) & 0xff;\
517 g = (v >> 8) & 0xff;\
521 #define YUVA_IN(y, u, v, a, s, pal)\
523 unsigned int val = ((const uint32_t *)(pal))[*(const uint8_t*)(s)];\
524 a = (val >> 24) & 0xff;\
525 y = (val >> 16) & 0xff;\
526 u = (val >> 8) & 0xff;\
530 #define YUVA_OUT(d, y, u, v, a)\
532 ((uint32_t *)(d))[0] = (a << 24) | (y << 16) | (u << 8) | v;\
538 static void blend_subrect(AVPicture *dst, const AVSubtitleRect *rect, int imgw, int imgh)
540 int wrap, wrap3, width2, skip2;
541 int y, u, v, a, u1, v1, a1, w, h;
542 uint8_t *lum, *cb, *cr;
545 int dstx, dsty, dstw, dsth;
547 dstw = av_clip(rect->w, 0, imgw);
548 dsth = av_clip(rect->h, 0, imgh);
549 dstx = av_clip(rect->x, 0, imgw - dstw);
550 dsty = av_clip(rect->y, 0, imgh - dsth);
551 lum = dst->data[0] + dsty * dst->linesize[0];
552 cb = dst->data[1] + (dsty >> 1) * dst->linesize[1];
553 cr = dst->data[2] + (dsty >> 1) * dst->linesize[2];
555 width2 = ((dstw + 1) >> 1) + (dstx & ~dstw & 1);
557 wrap = dst->linesize[0];
558 wrap3 = rect->pict.linesize[0];
559 p = rect->pict.data[0];
560 pal = (const uint32_t *)rect->pict.data[1]; /* Now in YCrCb! */
568 YUVA_IN(y, u, v, a, p, pal);
569 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
570 cb[0] = ALPHA_BLEND(a >> 2, cb[0], u, 0);
571 cr[0] = ALPHA_BLEND(a >> 2, cr[0], v, 0);
577 for (w = dstw - (dstx & 1); w >= 2; w -= 2) {
578 YUVA_IN(y, u, v, a, p, pal);
582 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
584 YUVA_IN(y, u, v, a, p + BPP, pal);
588 lum[1] = ALPHA_BLEND(a, lum[1], y, 0);
589 cb[0] = ALPHA_BLEND(a1 >> 2, cb[0], u1, 1);
590 cr[0] = ALPHA_BLEND(a1 >> 2, cr[0], v1, 1);
597 YUVA_IN(y, u, v, a, p, pal);
598 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
599 cb[0] = ALPHA_BLEND(a >> 2, cb[0], u, 0);
600 cr[0] = ALPHA_BLEND(a >> 2, cr[0], v, 0);
604 p += wrap3 - dstw * BPP;
605 lum += wrap - dstw - dstx;
606 cb += dst->linesize[1] - width2 - skip2;
607 cr += dst->linesize[2] - width2 - skip2;
609 for (h = dsth - (dsty & 1); h >= 2; h -= 2) {
615 YUVA_IN(y, u, v, a, p, pal);
619 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
622 YUVA_IN(y, u, v, a, p, pal);
626 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
627 cb[0] = ALPHA_BLEND(a1 >> 2, cb[0], u1, 1);
628 cr[0] = ALPHA_BLEND(a1 >> 2, cr[0], v1, 1);
634 for (w = dstw - (dstx & 1); w >= 2; w -= 2) {
635 YUVA_IN(y, u, v, a, p, pal);
639 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
641 YUVA_IN(y, u, v, a, p + BPP, pal);
645 lum[1] = ALPHA_BLEND(a, lum[1], y, 0);
649 YUVA_IN(y, u, v, a, p, pal);
653 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
655 YUVA_IN(y, u, v, a, p + BPP, pal);
659 lum[1] = ALPHA_BLEND(a, lum[1], y, 0);
661 cb[0] = ALPHA_BLEND(a1 >> 2, cb[0], u1, 2);
662 cr[0] = ALPHA_BLEND(a1 >> 2, cr[0], v1, 2);
666 p += -wrap3 + 2 * BPP;
670 YUVA_IN(y, u, v, a, p, pal);
674 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
677 YUVA_IN(y, u, v, a, p, pal);
681 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
682 cb[0] = ALPHA_BLEND(a1 >> 2, cb[0], u1, 1);
683 cr[0] = ALPHA_BLEND(a1 >> 2, cr[0], v1, 1);
689 p += wrap3 + (wrap3 - dstw * BPP);
690 lum += wrap + (wrap - dstw - dstx);
691 cb += dst->linesize[1] - width2 - skip2;
692 cr += dst->linesize[2] - width2 - skip2;
694 /* handle odd height */
701 YUVA_IN(y, u, v, a, p, pal);
702 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
703 cb[0] = ALPHA_BLEND(a >> 2, cb[0], u, 0);
704 cr[0] = ALPHA_BLEND(a >> 2, cr[0], v, 0);
710 for (w = dstw - (dstx & 1); w >= 2; w -= 2) {
711 YUVA_IN(y, u, v, a, p, pal);
715 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
717 YUVA_IN(y, u, v, a, p + BPP, pal);
721 lum[1] = ALPHA_BLEND(a, lum[1], y, 0);
722 cb[0] = ALPHA_BLEND(a1 >> 2, cb[0], u, 1);
723 cr[0] = ALPHA_BLEND(a1 >> 2, cr[0], v, 1);
730 YUVA_IN(y, u, v, a, p, pal);
731 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
732 cb[0] = ALPHA_BLEND(a >> 2, cb[0], u, 0);
733 cr[0] = ALPHA_BLEND(a >> 2, cr[0], v, 0);
738 static void free_subpicture(SubPicture *sp)
740 avsubtitle_free(&sp->sub);
743 static void calculate_display_rect(SDL_Rect *rect, int scr_xleft, int scr_ytop, int scr_width, int scr_height, VideoPicture *vp)
746 int width, height, x, y;
748 if (vp->sample_aspect_ratio.num == 0)
751 aspect_ratio = av_q2d(vp->sample_aspect_ratio);
753 if (aspect_ratio <= 0.0)
755 aspect_ratio *= (float)vp->width / (float)vp->height;
757 /* XXX: we suppose the screen has a 1.0 pixel ratio */
759 width = ((int)rint(height * aspect_ratio)) & ~1;
760 if (width > scr_width) {
762 height = ((int)rint(width / aspect_ratio)) & ~1;
764 x = (scr_width - width) / 2;
765 y = (scr_height - height) / 2;
766 rect->x = scr_xleft + x;
767 rect->y = scr_ytop + y;
768 rect->w = FFMAX(width, 1);
769 rect->h = FFMAX(height, 1);
772 static void video_image_display(VideoState *is)
780 vp = &is->pictq[is->pictq_rindex];
782 if (is->subtitle_st) {
783 if (is->subpq_size > 0) {
784 sp = &is->subpq[is->subpq_rindex];
786 if (vp->pts >= sp->pts + ((float) sp->sub.start_display_time / 1000)) {
787 SDL_LockYUVOverlay (vp->bmp);
789 pict.data[0] = vp->bmp->pixels[0];
790 pict.data[1] = vp->bmp->pixels[2];
791 pict.data[2] = vp->bmp->pixels[1];
793 pict.linesize[0] = vp->bmp->pitches[0];
794 pict.linesize[1] = vp->bmp->pitches[2];
795 pict.linesize[2] = vp->bmp->pitches[1];
797 for (i = 0; i < sp->sub.num_rects; i++)
798 blend_subrect(&pict, sp->sub.rects[i],
799 vp->bmp->w, vp->bmp->h);
801 SDL_UnlockYUVOverlay (vp->bmp);
806 calculate_display_rect(&rect, is->xleft, is->ytop, is->width, is->height, vp);
808 SDL_DisplayYUVOverlay(vp->bmp, &rect);
810 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) {
811 int bgcolor = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00);
812 fill_border(is->xleft, is->ytop, is->width, is->height, rect.x, rect.y, rect.w, rect.h, bgcolor, 1);
813 is->last_display_rect = rect;
818 static inline int compute_mod(int a, int b)
820 return a < 0 ? a%b + b : a%b;
823 static void video_audio_display(VideoState *s)
825 int i, i_start, x, y1, y, ys, delay, n, nb_display_channels;
826 int ch, channels, h, h2, bgcolor, fgcolor;
828 int rdft_bits, nb_freq;
830 for (rdft_bits = 1; (1 << rdft_bits) < 2 * s->height; rdft_bits++)
832 nb_freq = 1 << (rdft_bits - 1);
834 /* compute display index : center on currently output samples */
835 channels = s->audio_tgt.channels;
836 nb_display_channels = channels;
838 int data_used= s->show_mode == SHOW_MODE_WAVES ? s->width : (2*nb_freq);
840 delay = s->audio_write_buf_size;
843 /* to be more precise, we take into account the time spent since
844 the last buffer computation */
845 if (audio_callback_time) {
846 time_diff = av_gettime() - audio_callback_time;
847 delay -= (time_diff * s->audio_tgt.freq) / 1000000;
850 delay += 2 * data_used;
851 if (delay < data_used)
854 i_start= x = compute_mod(s->sample_array_index - delay * channels, SAMPLE_ARRAY_SIZE);
855 if (s->show_mode == SHOW_MODE_WAVES) {
857 for (i = 0; i < 1000; i += channels) {
858 int idx = (SAMPLE_ARRAY_SIZE + x - i) % SAMPLE_ARRAY_SIZE;
859 int a = s->sample_array[idx];
860 int b = s->sample_array[(idx + 4 * channels) % SAMPLE_ARRAY_SIZE];
861 int c = s->sample_array[(idx + 5 * channels) % SAMPLE_ARRAY_SIZE];
862 int d = s->sample_array[(idx + 9 * channels) % SAMPLE_ARRAY_SIZE];
864 if (h < score && (b ^ c) < 0) {
871 s->last_i_start = i_start;
873 i_start = s->last_i_start;
876 bgcolor = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00);
877 if (s->show_mode == SHOW_MODE_WAVES) {
878 fill_rectangle(screen,
879 s->xleft, s->ytop, s->width, s->height,
882 fgcolor = SDL_MapRGB(screen->format, 0xff, 0xff, 0xff);
884 /* total height for one channel */
885 h = s->height / nb_display_channels;
886 /* graph height / 2 */
888 for (ch = 0; ch < nb_display_channels; ch++) {
890 y1 = s->ytop + ch * h + (h / 2); /* position of center line */
891 for (x = 0; x < s->width; x++) {
892 y = (s->sample_array[i] * h2) >> 15;
899 fill_rectangle(screen,
900 s->xleft + x, ys, 1, y,
903 if (i >= SAMPLE_ARRAY_SIZE)
904 i -= SAMPLE_ARRAY_SIZE;
908 fgcolor = SDL_MapRGB(screen->format, 0x00, 0x00, 0xff);
910 for (ch = 1; ch < nb_display_channels; ch++) {
911 y = s->ytop + ch * h;
912 fill_rectangle(screen,
913 s->xleft, y, s->width, 1,
916 SDL_UpdateRect(screen, s->xleft, s->ytop, s->width, s->height);
918 nb_display_channels= FFMIN(nb_display_channels, 2);
919 if (rdft_bits != s->rdft_bits) {
920 av_rdft_end(s->rdft);
921 av_free(s->rdft_data);
922 s->rdft = av_rdft_init(rdft_bits, DFT_R2C);
923 s->rdft_bits = rdft_bits;
924 s->rdft_data = av_malloc(4 * nb_freq * sizeof(*s->rdft_data));
928 for (ch = 0; ch < nb_display_channels; ch++) {
929 data[ch] = s->rdft_data + 2 * nb_freq * ch;
931 for (x = 0; x < 2 * nb_freq; x++) {
932 double w = (x-nb_freq) * (1.0 / nb_freq);
933 data[ch][x] = s->sample_array[i] * (1.0 - w * w);
935 if (i >= SAMPLE_ARRAY_SIZE)
936 i -= SAMPLE_ARRAY_SIZE;
938 av_rdft_calc(s->rdft, data[ch]);
940 // least efficient way to do this, we should of course directly access it but its more than fast enough
941 for (y = 0; y < s->height; y++) {
942 double w = 1 / sqrt(nb_freq);
943 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]));
944 int b = (nb_display_channels == 2 ) ? sqrt(w * sqrt(data[1][2 * y + 0] * data[1][2 * y + 0]
945 + data[1][2 * y + 1] * data[1][2 * y + 1])) : a;
948 fgcolor = SDL_MapRGB(screen->format, a, b, (a + b) / 2);
950 fill_rectangle(screen,
951 s->xpos, s->height-y, 1, 1,
955 SDL_UpdateRect(screen, s->xpos, s->ytop, 1, s->height);
958 if (s->xpos >= s->width)
963 static void stream_close(VideoState *is)
967 /* XXX: use a special url_shutdown call to abort parse cleanly */
968 is->abort_request = 1;
969 SDL_WaitThread(is->read_tid, NULL);
970 SDL_WaitThread(is->refresh_tid, NULL);
971 packet_queue_destroy(&is->videoq);
972 packet_queue_destroy(&is->audioq);
973 packet_queue_destroy(&is->subtitleq);
975 /* free all pictures */
976 for (i = 0; i < VIDEO_PICTURE_QUEUE_SIZE; i++) {
979 avfilter_unref_bufferp(&vp->picref);
982 SDL_FreeYUVOverlay(vp->bmp);
986 SDL_DestroyMutex(is->pictq_mutex);
987 SDL_DestroyCond(is->pictq_cond);
988 SDL_DestroyMutex(is->subpq_mutex);
989 SDL_DestroyCond(is->subpq_cond);
990 SDL_DestroyCond(is->continue_read_thread);
992 if (is->img_convert_ctx)
993 sws_freeContext(is->img_convert_ctx);
998 static void do_exit(VideoState *is)
1003 av_lockmgr_register(NULL);
1007 av_freep(&vfilters);
1009 avformat_network_deinit();
1013 av_log(NULL, AV_LOG_QUIET, "%s", "");
1017 static void sigterm_handler(int sig)
1022 static int video_open(VideoState *is, int force_set_video_mode)
1024 int flags = SDL_HWSURFACE | SDL_ASYNCBLIT | SDL_HWACCEL;
1026 VideoPicture *vp = &is->pictq[is->pictq_rindex];
1029 if (is_full_screen) flags |= SDL_FULLSCREEN;
1030 else flags |= SDL_RESIZABLE;
1032 if (is_full_screen && fs_screen_width) {
1033 w = fs_screen_width;
1034 h = fs_screen_height;
1035 } else if (!is_full_screen && screen_width) {
1038 } else if (vp->width) {
1039 calculate_display_rect(&rect, 0, 0, INT_MAX, vp->height, vp);
1046 if (screen && is->width == screen->w && screen->w == w
1047 && is->height== screen->h && screen->h == h && !force_set_video_mode)
1049 screen = SDL_SetVideoMode(w, h, 0, flags);
1051 fprintf(stderr, "SDL: could not set video mode - exiting\n");
1055 window_title = input_filename;
1056 SDL_WM_SetCaption(window_title, window_title);
1058 is->width = screen->w;
1059 is->height = screen->h;
1064 /* display the current picture, if any */
1065 static void video_display(VideoState *is)
1069 if (is->audio_st && is->show_mode != SHOW_MODE_VIDEO)
1070 video_audio_display(is);
1071 else if (is->video_st)
1072 video_image_display(is);
1075 static int refresh_thread(void *opaque)
1077 VideoState *is= opaque;
1078 while (!is->abort_request) {
1080 event.type = FF_REFRESH_EVENT;
1081 event.user.data1 = opaque;
1082 if (!is->refresh && (!is->paused || is->force_refresh)) {
1084 SDL_PushEvent(&event);
1086 //FIXME ideally we should wait the correct time but SDLs event passing is so slow it would be silly
1087 av_usleep(is->audio_st && is->show_mode != SHOW_MODE_VIDEO ? rdftspeed*1000 : 5000);
1092 /* get the current audio clock value */
1093 static double get_audio_clock(VideoState *is)
1096 return is->audio_current_pts;
1098 return is->audio_current_pts_drift + av_gettime() / 1000000.0;
1102 /* get the current video clock value */
1103 static double get_video_clock(VideoState *is)
1106 return is->video_current_pts;
1108 return is->video_current_pts_drift + av_gettime() / 1000000.0;
1112 /* get the current external clock value */
1113 static double get_external_clock(VideoState *is)
1116 return is->external_clock;
1118 double time = av_gettime() / 1000000.0;
1119 return is->external_clock_drift + time - (time - is->external_clock_time / 1000000.0) * (1.0 - is->external_clock_speed);
1123 static int get_master_sync_type(VideoState *is) {
1124 if (is->av_sync_type == AV_SYNC_VIDEO_MASTER) {
1126 return AV_SYNC_VIDEO_MASTER;
1128 return AV_SYNC_AUDIO_MASTER;
1129 } else if (is->av_sync_type == AV_SYNC_AUDIO_MASTER) {
1131 return AV_SYNC_AUDIO_MASTER;
1133 return AV_SYNC_EXTERNAL_CLOCK;
1135 return AV_SYNC_EXTERNAL_CLOCK;
1139 /* get the current master clock value */
1140 static double get_master_clock(VideoState *is)
1144 switch (get_master_sync_type(is)) {
1145 case AV_SYNC_VIDEO_MASTER:
1146 val = get_video_clock(is);
1148 case AV_SYNC_AUDIO_MASTER:
1149 val = get_audio_clock(is);
1152 val = get_external_clock(is);
1158 static void update_external_clock_pts(VideoState *is, double pts)
1160 is->external_clock_time = av_gettime();
1161 is->external_clock = pts;
1162 is->external_clock_drift = pts - is->external_clock_time / 1000000.0;
1165 static void check_external_clock_sync(VideoState *is, double pts) {
1166 if (fabs(get_external_clock(is) - pts) > AV_NOSYNC_THRESHOLD) {
1167 update_external_clock_pts(is, pts);
1171 static void update_external_clock_speed(VideoState *is, double speed) {
1172 update_external_clock_pts(is, get_external_clock(is));
1173 is->external_clock_speed = speed;
1176 static void check_external_clock_speed(VideoState *is) {
1177 if (is->video_stream >= 0 && is->videoq.nb_packets <= MIN_FRAMES / 2 ||
1178 is->audio_stream >= 0 && is->audioq.nb_packets <= MIN_FRAMES / 2) {
1179 update_external_clock_speed(is, FFMAX(EXTERNAL_CLOCK_SPEED_MIN, is->external_clock_speed - EXTERNAL_CLOCK_SPEED_STEP));
1180 } else if ((is->video_stream < 0 || is->videoq.nb_packets > MIN_FRAMES * 2) &&
1181 (is->audio_stream < 0 || is->audioq.nb_packets > MIN_FRAMES * 2)) {
1182 update_external_clock_speed(is, FFMIN(EXTERNAL_CLOCK_SPEED_MAX, is->external_clock_speed + EXTERNAL_CLOCK_SPEED_STEP));
1184 double speed = is->external_clock_speed;
1186 update_external_clock_speed(is, speed + EXTERNAL_CLOCK_SPEED_STEP * (1.0 - speed) / fabs(1.0 - speed));
1190 /* seek in the stream */
1191 static void stream_seek(VideoState *is, int64_t pos, int64_t rel, int seek_by_bytes)
1193 if (!is->seek_req) {
1196 is->seek_flags &= ~AVSEEK_FLAG_BYTE;
1198 is->seek_flags |= AVSEEK_FLAG_BYTE;
1203 /* pause or resume the video */
1204 static void stream_toggle_pause(VideoState *is)
1207 is->frame_timer += av_gettime() / 1000000.0 + is->video_current_pts_drift - is->video_current_pts;
1208 if (is->read_pause_return != AVERROR(ENOSYS)) {
1209 is->video_current_pts = is->video_current_pts_drift + av_gettime() / 1000000.0;
1211 is->video_current_pts_drift = is->video_current_pts - av_gettime() / 1000000.0;
1213 update_external_clock_pts(is, get_external_clock(is));
1214 is->paused = !is->paused;
1217 static double compute_target_delay(double delay, VideoState *is)
1219 double sync_threshold, diff;
1221 /* update delay to follow master synchronisation source */
1222 if (get_master_sync_type(is) != AV_SYNC_VIDEO_MASTER) {
1223 /* if video is slave, we try to correct big delays by
1224 duplicating or deleting a frame */
1225 diff = get_video_clock(is) - get_master_clock(is);
1227 /* skip or repeat frame. We take into account the
1228 delay to compute the threshold. I still don't know
1229 if it is the best guess */
1230 sync_threshold = FFMAX(AV_SYNC_THRESHOLD, delay);
1231 if (fabs(diff) < AV_NOSYNC_THRESHOLD) {
1232 if (diff <= -sync_threshold)
1234 else if (diff >= sync_threshold)
1239 av_dlog(NULL, "video: delay=%0.3f A-V=%f\n",
1245 static void pictq_next_picture(VideoState *is) {
1246 /* update queue size and signal for next picture */
1247 if (++is->pictq_rindex == VIDEO_PICTURE_QUEUE_SIZE)
1248 is->pictq_rindex = 0;
1250 SDL_LockMutex(is->pictq_mutex);
1252 SDL_CondSignal(is->pictq_cond);
1253 SDL_UnlockMutex(is->pictq_mutex);
1256 static void pictq_prev_picture(VideoState *is) {
1257 VideoPicture *prevvp;
1258 /* update queue size and signal for the previous picture */
1259 prevvp = &is->pictq[(is->pictq_rindex + VIDEO_PICTURE_QUEUE_SIZE - 1) % VIDEO_PICTURE_QUEUE_SIZE];
1260 if (prevvp->allocated && !prevvp->skip) {
1261 SDL_LockMutex(is->pictq_mutex);
1262 if (is->pictq_size < VIDEO_PICTURE_QUEUE_SIZE - 1) {
1263 if (--is->pictq_rindex == -1)
1264 is->pictq_rindex = VIDEO_PICTURE_QUEUE_SIZE - 1;
1267 SDL_CondSignal(is->pictq_cond);
1268 SDL_UnlockMutex(is->pictq_mutex);
1272 static void update_video_pts(VideoState *is, double pts, int64_t pos, int serial) {
1273 double time = av_gettime() / 1000000.0;
1274 /* update current video pts */
1275 is->video_current_pts = pts;
1276 is->video_current_pts_drift = is->video_current_pts - time;
1277 is->video_current_pos = pos;
1278 is->frame_last_pts = pts;
1279 if (is->videoq.serial == serial)
1280 check_external_clock_sync(is, is->video_current_pts);
1283 /* called to display each frame */
1284 static void video_refresh(void *opaque)
1286 VideoState *is = opaque;
1290 SubPicture *sp, *sp2;
1292 if (!is->paused && get_master_sync_type(is) == AV_SYNC_EXTERNAL_CLOCK && is->realtime)
1293 check_external_clock_speed(is);
1296 if (is->force_refresh)
1297 pictq_prev_picture(is);
1299 if (is->pictq_size == 0) {
1300 SDL_LockMutex(is->pictq_mutex);
1301 if (is->frame_last_dropped_pts != AV_NOPTS_VALUE && is->frame_last_dropped_pts > is->frame_last_pts) {
1302 update_video_pts(is, is->frame_last_dropped_pts, is->frame_last_dropped_pos, 0);
1303 is->frame_last_dropped_pts = AV_NOPTS_VALUE;
1305 SDL_UnlockMutex(is->pictq_mutex);
1306 // nothing to do, no picture to display in the que
1308 double last_duration, duration, delay;
1309 /* dequeue the picture */
1310 vp = &is->pictq[is->pictq_rindex];
1313 pictq_next_picture(is);
1320 /* compute nominal last_duration */
1321 last_duration = vp->pts - is->frame_last_pts;
1322 if (last_duration > 0 && last_duration < is->max_frame_duration) {
1323 /* if duration of the last frame was sane, update last_duration in video state */
1324 is->frame_last_duration = last_duration;
1326 delay = compute_target_delay(is->frame_last_duration, is);
1328 time= av_gettime()/1000000.0;
1329 if (time < is->frame_timer + delay)
1333 is->frame_timer += delay * FFMAX(1, floor((time-is->frame_timer) / delay));
1335 SDL_LockMutex(is->pictq_mutex);
1336 update_video_pts(is, vp->pts, vp->pos, vp->serial);
1337 SDL_UnlockMutex(is->pictq_mutex);
1339 if (is->pictq_size > 1) {
1340 VideoPicture *nextvp = &is->pictq[(is->pictq_rindex + 1) % VIDEO_PICTURE_QUEUE_SIZE];
1341 duration = nextvp->pts - vp->pts;
1342 if((framedrop>0 || (framedrop && get_master_sync_type(is) != AV_SYNC_VIDEO_MASTER)) && time > is->frame_timer + duration){
1343 is->frame_drops_late++;
1344 pictq_next_picture(is);
1349 if (is->subtitle_st) {
1350 if (is->subtitle_stream_changed) {
1351 SDL_LockMutex(is->subpq_mutex);
1353 while (is->subpq_size) {
1354 free_subpicture(&is->subpq[is->subpq_rindex]);
1356 /* update queue size and signal for next picture */
1357 if (++is->subpq_rindex == SUBPICTURE_QUEUE_SIZE)
1358 is->subpq_rindex = 0;
1362 is->subtitle_stream_changed = 0;
1364 SDL_CondSignal(is->subpq_cond);
1365 SDL_UnlockMutex(is->subpq_mutex);
1367 if (is->subpq_size > 0) {
1368 sp = &is->subpq[is->subpq_rindex];
1370 if (is->subpq_size > 1)
1371 sp2 = &is->subpq[(is->subpq_rindex + 1) % SUBPICTURE_QUEUE_SIZE];
1375 if ((is->video_current_pts > (sp->pts + ((float) sp->sub.end_display_time / 1000)))
1376 || (sp2 && is->video_current_pts > (sp2->pts + ((float) sp2->sub.start_display_time / 1000))))
1378 free_subpicture(sp);
1380 /* update queue size and signal for next picture */
1381 if (++is->subpq_rindex == SUBPICTURE_QUEUE_SIZE)
1382 is->subpq_rindex = 0;
1384 SDL_LockMutex(is->subpq_mutex);
1386 SDL_CondSignal(is->subpq_cond);
1387 SDL_UnlockMutex(is->subpq_mutex);
1394 /* display picture */
1395 if (!display_disable)
1398 pictq_next_picture(is);
1400 } else if (is->audio_st) {
1401 /* draw the next audio frame */
1403 /* if only audio stream, then display the audio bars (better
1404 than nothing, just to test the implementation */
1406 /* display picture */
1407 if (!display_disable)
1410 is->force_refresh = 0;
1412 static int64_t last_time;
1414 int aqsize, vqsize, sqsize;
1417 cur_time = av_gettime();
1418 if (!last_time || (cur_time - last_time) >= 30000) {
1423 aqsize = is->audioq.size;
1425 vqsize = is->videoq.size;
1426 if (is->subtitle_st)
1427 sqsize = is->subtitleq.size;
1429 if (is->audio_st && is->video_st)
1430 av_diff = get_audio_clock(is) - get_video_clock(is);
1431 printf("%7.2f A-V:%7.3f fd=%4d aq=%5dKB vq=%5dKB sq=%5dB f=%"PRId64"/%"PRId64" \r",
1432 get_master_clock(is),
1434 is->frame_drops_early + is->frame_drops_late,
1438 is->video_st ? is->video_st->codec->pts_correction_num_faulty_dts : 0,
1439 is->video_st ? is->video_st->codec->pts_correction_num_faulty_pts : 0);
1441 last_time = cur_time;
1446 /* allocate a picture (needs to do that in main thread to avoid
1447 potential locking problems */
1448 static void alloc_picture(VideoState *is)
1452 vp = &is->pictq[is->pictq_windex];
1455 SDL_FreeYUVOverlay(vp->bmp);
1458 avfilter_unref_bufferp(&vp->picref);
1463 vp->bmp = SDL_CreateYUVOverlay(vp->width, vp->height,
1466 if (!vp->bmp || vp->bmp->pitches[0] < vp->width) {
1467 /* SDL allocates a buffer smaller than requested if the video
1468 * overlay hardware is unable to support the requested size. */
1469 fprintf(stderr, "Error: the video system does not support an image\n"
1470 "size of %dx%d pixels. Try using -lowres or -vf \"scale=w:h\"\n"
1471 "to reduce the image size.\n", vp->width, vp->height );
1475 SDL_LockMutex(is->pictq_mutex);
1477 SDL_CondSignal(is->pictq_cond);
1478 SDL_UnlockMutex(is->pictq_mutex);
1481 static int queue_picture(VideoState *is, AVFrame *src_frame, double pts1, int64_t pos, int serial)
1484 double frame_delay, pts = pts1;
1486 /* compute the exact PTS for the picture if it is omitted in the stream
1487 * pts1 is the dts of the pkt / pts of the frame */
1489 /* update video clock with pts, if present */
1490 is->video_clock = pts;
1492 pts = is->video_clock;
1494 /* update video clock for next frame */
1495 frame_delay = av_q2d(is->video_st->codec->time_base);
1496 /* for MPEG2, the frame can be repeated, so we update the
1497 clock accordingly */
1498 frame_delay += src_frame->repeat_pict * (frame_delay * 0.5);
1499 is->video_clock += frame_delay;
1501 #if defined(DEBUG_SYNC) && 0
1502 printf("frame_type=%c clock=%0.3f pts=%0.3f\n",
1503 av_get_picture_type_char(src_frame->pict_type), pts, pts1);
1506 /* wait until we have space to put a new picture */
1507 SDL_LockMutex(is->pictq_mutex);
1509 /* keep the last already displayed picture in the queue */
1510 while (is->pictq_size >= VIDEO_PICTURE_QUEUE_SIZE - 2 &&
1511 !is->videoq.abort_request) {
1512 SDL_CondWait(is->pictq_cond, is->pictq_mutex);
1514 SDL_UnlockMutex(is->pictq_mutex);
1516 if (is->videoq.abort_request)
1519 vp = &is->pictq[is->pictq_windex];
1522 vp->sample_aspect_ratio = ((AVFilterBufferRef *)src_frame->opaque)->video->sample_aspect_ratio;
1524 vp->sample_aspect_ratio = av_guess_sample_aspect_ratio(is->ic, is->video_st, src_frame);
1527 /* alloc or resize hardware picture buffer */
1528 if (!vp->bmp || vp->reallocate || !vp->allocated ||
1529 vp->width != src_frame->width ||
1530 vp->height != src_frame->height) {
1535 vp->width = src_frame->width;
1536 vp->height = src_frame->height;
1538 /* the allocation must be done in the main thread to avoid
1539 locking problems. */
1540 event.type = FF_ALLOC_EVENT;
1541 event.user.data1 = is;
1542 SDL_PushEvent(&event);
1544 /* wait until the picture is allocated */
1545 SDL_LockMutex(is->pictq_mutex);
1546 while (!vp->allocated && !is->videoq.abort_request) {
1547 SDL_CondWait(is->pictq_cond, is->pictq_mutex);
1549 /* if the queue is aborted, we have to pop the pending ALLOC event or wait for the allocation to complete */
1550 if (is->videoq.abort_request && SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_EVENTMASK(FF_ALLOC_EVENT)) != 1) {
1551 while (!vp->allocated) {
1552 SDL_CondWait(is->pictq_cond, is->pictq_mutex);
1555 SDL_UnlockMutex(is->pictq_mutex);
1557 if (is->videoq.abort_request)
1561 /* if the frame is not skipped, then display it */
1563 AVPicture pict = { { 0 } };
1565 avfilter_unref_bufferp(&vp->picref);
1566 vp->picref = src_frame->opaque;
1569 /* get a pointer on the bitmap */
1570 SDL_LockYUVOverlay (vp->bmp);
1572 pict.data[0] = vp->bmp->pixels[0];
1573 pict.data[1] = vp->bmp->pixels[2];
1574 pict.data[2] = vp->bmp->pixels[1];
1576 pict.linesize[0] = vp->bmp->pitches[0];
1577 pict.linesize[1] = vp->bmp->pitches[2];
1578 pict.linesize[2] = vp->bmp->pitches[1];
1581 // FIXME use direct rendering
1582 av_picture_copy(&pict, (AVPicture *)src_frame,
1583 src_frame->format, vp->width, vp->height);
1585 av_opt_get_int(sws_opts, "sws_flags", 0, &sws_flags);
1586 is->img_convert_ctx = sws_getCachedContext(is->img_convert_ctx,
1587 vp->width, vp->height, src_frame->format, vp->width, vp->height,
1588 AV_PIX_FMT_YUV420P, sws_flags, NULL, NULL, NULL);
1589 if (is->img_convert_ctx == NULL) {
1590 fprintf(stderr, "Cannot initialize the conversion context\n");
1593 sws_scale(is->img_convert_ctx, src_frame->data, src_frame->linesize,
1594 0, vp->height, pict.data, pict.linesize);
1596 /* update the bitmap content */
1597 SDL_UnlockYUVOverlay(vp->bmp);
1602 vp->serial = serial;
1604 /* now we can update the picture count */
1605 if (++is->pictq_windex == VIDEO_PICTURE_QUEUE_SIZE)
1606 is->pictq_windex = 0;
1607 SDL_LockMutex(is->pictq_mutex);
1609 SDL_UnlockMutex(is->pictq_mutex);
1614 static int get_video_frame(VideoState *is, AVFrame *frame, int64_t *pts, AVPacket *pkt, int *serial)
1618 if (packet_queue_get(&is->videoq, pkt, 1, serial) < 0)
1621 if (pkt->data == flush_pkt.data) {
1622 avcodec_flush_buffers(is->video_st->codec);
1624 SDL_LockMutex(is->pictq_mutex);
1625 // Make sure there are no long delay timers (ideally we should just flush the que but thats harder)
1626 for (i = 0; i < VIDEO_PICTURE_QUEUE_SIZE; i++) {
1627 is->pictq[i].skip = 1;
1629 while (is->pictq_size && !is->videoq.abort_request) {
1630 SDL_CondWait(is->pictq_cond, is->pictq_mutex);
1632 is->video_current_pos = -1;
1633 is->frame_last_pts = AV_NOPTS_VALUE;
1634 is->frame_last_duration = 0;
1635 is->frame_timer = (double)av_gettime() / 1000000.0;
1636 is->frame_last_dropped_pts = AV_NOPTS_VALUE;
1637 SDL_UnlockMutex(is->pictq_mutex);
1642 if(avcodec_decode_video2(is->video_st->codec, frame, &got_picture, pkt) < 0)
1648 if (decoder_reorder_pts == -1) {
1649 *pts = av_frame_get_best_effort_timestamp(frame);
1650 } else if (decoder_reorder_pts) {
1651 *pts = frame->pkt_pts;
1653 *pts = frame->pkt_dts;
1656 if (*pts == AV_NOPTS_VALUE) {
1660 if (framedrop>0 || (framedrop && get_master_sync_type(is) != AV_SYNC_VIDEO_MASTER)) {
1661 SDL_LockMutex(is->pictq_mutex);
1662 if (is->frame_last_pts != AV_NOPTS_VALUE && *pts) {
1663 double clockdiff = get_video_clock(is) - get_master_clock(is);
1664 double dpts = av_q2d(is->video_st->time_base) * *pts;
1665 double ptsdiff = dpts - is->frame_last_pts;
1666 if (fabs(clockdiff) < AV_NOSYNC_THRESHOLD &&
1667 ptsdiff > 0 && ptsdiff < AV_NOSYNC_THRESHOLD &&
1668 clockdiff + ptsdiff - is->frame_last_filter_delay < 0) {
1669 is->frame_last_dropped_pos = pkt->pos;
1670 is->frame_last_dropped_pts = dpts;
1671 is->frame_drops_early++;
1675 SDL_UnlockMutex(is->pictq_mutex);
1684 static int configure_filtergraph(AVFilterGraph *graph, const char *filtergraph,
1685 AVFilterContext *source_ctx, AVFilterContext *sink_ctx)
1688 AVFilterInOut *outputs = NULL, *inputs = NULL;
1691 outputs = avfilter_inout_alloc();
1692 inputs = avfilter_inout_alloc();
1693 if (!outputs || !inputs) {
1694 ret = AVERROR(ENOMEM);
1698 outputs->name = av_strdup("in");
1699 outputs->filter_ctx = source_ctx;
1700 outputs->pad_idx = 0;
1701 outputs->next = NULL;
1703 inputs->name = av_strdup("out");
1704 inputs->filter_ctx = sink_ctx;
1705 inputs->pad_idx = 0;
1706 inputs->next = NULL;
1708 if ((ret = avfilter_graph_parse(graph, filtergraph, &inputs, &outputs, NULL)) < 0)
1711 if ((ret = avfilter_link(source_ctx, 0, sink_ctx, 0)) < 0)
1715 ret = avfilter_graph_config(graph, NULL);
1717 avfilter_inout_free(&outputs);
1718 avfilter_inout_free(&inputs);
1722 static int configure_video_filters(AVFilterGraph *graph, VideoState *is, const char *vfilters, AVFrame *frame)
1724 static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE };
1725 char sws_flags_str[128];
1726 char buffersrc_args[256];
1728 AVBufferSinkParams *buffersink_params = av_buffersink_params_alloc();
1729 AVFilterContext *filt_src = NULL, *filt_out = NULL, *filt_crop;
1730 AVCodecContext *codec = is->video_st->codec;
1732 if (!buffersink_params)
1733 return AVERROR(ENOMEM);
1735 snprintf(sws_flags_str, sizeof(sws_flags_str), "flags=%"PRId64, sws_flags);
1736 graph->scale_sws_opts = av_strdup(sws_flags_str);
1738 snprintf(buffersrc_args, sizeof(buffersrc_args),
1739 "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d",
1740 frame->width, frame->height, frame->format,
1741 is->video_st->time_base.num, is->video_st->time_base.den,
1742 codec->sample_aspect_ratio.num, FFMAX(codec->sample_aspect_ratio.den, 1));
1744 if ((ret = avfilter_graph_create_filter(&filt_src,
1745 avfilter_get_by_name("buffer"),
1746 "ffplay_buffer", buffersrc_args, NULL,
1750 buffersink_params->pixel_fmts = pix_fmts;
1751 ret = avfilter_graph_create_filter(&filt_out,
1752 avfilter_get_by_name("ffbuffersink"),
1753 "ffplay_buffersink", NULL, buffersink_params, graph);
1757 /* SDL YUV code is not handling odd width/height for some driver
1758 * combinations, therefore we crop the picture to an even width/height. */
1759 if ((ret = avfilter_graph_create_filter(&filt_crop,
1760 avfilter_get_by_name("crop"),
1761 "ffplay_crop", "floor(in_w/2)*2:floor(in_h/2)*2", NULL, graph)) < 0)
1763 if ((ret = avfilter_link(filt_crop, 0, filt_out, 0)) < 0)
1766 if ((ret = configure_filtergraph(graph, vfilters, filt_src, filt_crop)) < 0)
1769 is->in_video_filter = filt_src;
1770 is->out_video_filter = filt_out;
1773 av_freep(&buffersink_params);
1777 #endif /* CONFIG_AVFILTER */
1779 static int video_thread(void *arg)
1781 AVPacket pkt = { 0 };
1782 VideoState *is = arg;
1783 AVFrame *frame = avcodec_alloc_frame();
1784 int64_t pts_int = AV_NOPTS_VALUE, pos = -1;
1790 AVCodecContext *codec = is->video_st->codec;
1791 AVFilterGraph *graph = avfilter_graph_alloc();
1792 AVFilterContext *filt_out = NULL, *filt_in = NULL;
1795 enum AVPixelFormat last_format = -2;
1797 if (codec->codec->capabilities & CODEC_CAP_DR1) {
1799 codec->get_buffer = codec_get_buffer;
1800 codec->release_buffer = codec_release_buffer;
1801 codec->opaque = &is->buffer_pool;
1807 AVFilterBufferRef *picref;
1810 while (is->paused && !is->videoq.abort_request)
1813 avcodec_get_frame_defaults(frame);
1814 av_free_packet(&pkt);
1816 ret = get_video_frame(is, frame, &pts_int, &pkt, &serial);
1824 if ( last_w != frame->width
1825 || last_h != frame->height
1826 || last_format != frame->format) {
1827 av_log(NULL, AV_LOG_INFO, "Frame changed from size:%dx%d to size:%dx%d\n",
1828 last_w, last_h, frame->width, frame->height);
1829 avfilter_graph_free(&graph);
1830 graph = avfilter_graph_alloc();
1831 if ((ret = configure_video_filters(graph, is, vfilters, frame)) < 0) {
1833 event.type = FF_QUIT_EVENT;
1834 event.user.data1 = is;
1835 SDL_PushEvent(&event);
1836 av_free_packet(&pkt);
1839 filt_in = is->in_video_filter;
1840 filt_out = is->out_video_filter;
1841 last_w = frame->width;
1842 last_h = frame->height;
1843 last_format = frame->format;
1846 frame->pts = pts_int;
1847 frame->sample_aspect_ratio = av_guess_sample_aspect_ratio(is->ic, is->video_st, frame);
1848 if (is->use_dr1 && frame->opaque) {
1849 FrameBuffer *buf = frame->opaque;
1850 AVFilterBufferRef *fb = avfilter_get_video_buffer_ref_from_arrays(
1851 frame->data, frame->linesize,
1852 AV_PERM_READ | AV_PERM_PRESERVE,
1853 frame->width, frame->height,
1856 avfilter_copy_frame_props(fb, frame);
1857 fb->buf->priv = buf;
1858 fb->buf->free = filter_release_buffer;
1861 av_buffersrc_add_ref(filt_in, fb, AV_BUFFERSRC_FLAG_NO_COPY);
1864 av_buffersrc_write_frame(filt_in, frame);
1866 av_free_packet(&pkt);
1869 is->frame_last_returned_time = av_gettime() / 1000000.0;
1871 ret = av_buffersink_get_buffer_ref(filt_out, &picref, 0);
1877 is->frame_last_filter_delay = av_gettime() / 1000000.0 - is->frame_last_returned_time;
1878 if (fabs(is->frame_last_filter_delay) > AV_NOSYNC_THRESHOLD / 10.0)
1879 is->frame_last_filter_delay = 0;
1881 avfilter_copy_buf_props(frame, picref);
1883 pts_int = picref->pts;
1884 tb = filt_out->inputs[0]->time_base;
1886 frame->opaque = picref;
1888 if (av_cmp_q(tb, is->video_st->time_base)) {
1889 av_unused int64_t pts1 = pts_int;
1890 pts_int = av_rescale_q(pts_int, tb, is->video_st->time_base);
1891 av_dlog(NULL, "video_thread(): "
1892 "tb:%d/%d pts:%"PRId64" -> tb:%d/%d pts:%"PRId64"\n",
1893 tb.num, tb.den, pts1,
1894 is->video_st->time_base.num, is->video_st->time_base.den, pts_int);
1896 pts = pts_int * av_q2d(is->video_st->time_base);
1897 ret = queue_picture(is, frame, pts, pos, serial);
1900 pts = pts_int * av_q2d(is->video_st->time_base);
1901 ret = queue_picture(is, frame, pts, pkt.pos, serial);
1908 stream_toggle_pause(is);
1911 avcodec_flush_buffers(is->video_st->codec);
1913 avfilter_graph_free(&graph);
1915 av_free_packet(&pkt);
1916 avcodec_free_frame(&frame);
1920 static int subtitle_thread(void *arg)
1922 VideoState *is = arg;
1924 AVPacket pkt1, *pkt = &pkt1;
1928 int r, g, b, y, u, v, a;
1931 while (is->paused && !is->subtitleq.abort_request) {
1934 if (packet_queue_get(&is->subtitleq, pkt, 1, NULL) < 0)
1937 if (pkt->data == flush_pkt.data) {
1938 avcodec_flush_buffers(is->subtitle_st->codec);
1941 SDL_LockMutex(is->subpq_mutex);
1942 while (is->subpq_size >= SUBPICTURE_QUEUE_SIZE &&
1943 !is->subtitleq.abort_request) {
1944 SDL_CondWait(is->subpq_cond, is->subpq_mutex);
1946 SDL_UnlockMutex(is->subpq_mutex);
1948 if (is->subtitleq.abort_request)
1951 sp = &is->subpq[is->subpq_windex];
1953 /* NOTE: ipts is the PTS of the _first_ picture beginning in
1954 this packet, if any */
1956 if (pkt->pts != AV_NOPTS_VALUE)
1957 pts = av_q2d(is->subtitle_st->time_base) * pkt->pts;
1959 avcodec_decode_subtitle2(is->subtitle_st->codec, &sp->sub,
1960 &got_subtitle, pkt);
1961 if (got_subtitle && sp->sub.format == 0) {
1962 if (sp->sub.pts != AV_NOPTS_VALUE)
1963 pts = sp->sub.pts / (double)AV_TIME_BASE;
1966 for (i = 0; i < sp->sub.num_rects; i++)
1968 for (j = 0; j < sp->sub.rects[i]->nb_colors; j++)
1970 RGBA_IN(r, g, b, a, (uint32_t*)sp->sub.rects[i]->pict.data[1] + j);
1971 y = RGB_TO_Y_CCIR(r, g, b);
1972 u = RGB_TO_U_CCIR(r, g, b, 0);
1973 v = RGB_TO_V_CCIR(r, g, b, 0);
1974 YUVA_OUT((uint32_t*)sp->sub.rects[i]->pict.data[1] + j, y, u, v, a);
1978 /* now we can update the picture count */
1979 if (++is->subpq_windex == SUBPICTURE_QUEUE_SIZE)
1980 is->subpq_windex = 0;
1981 SDL_LockMutex(is->subpq_mutex);
1983 SDL_UnlockMutex(is->subpq_mutex);
1985 av_free_packet(pkt);
1990 /* copy samples for viewing in editor window */
1991 static void update_sample_display(VideoState *is, short *samples, int samples_size)
1995 size = samples_size / sizeof(short);
1997 len = SAMPLE_ARRAY_SIZE - is->sample_array_index;
2000 memcpy(is->sample_array + is->sample_array_index, samples, len * sizeof(short));
2002 is->sample_array_index += len;
2003 if (is->sample_array_index >= SAMPLE_ARRAY_SIZE)
2004 is->sample_array_index = 0;
2009 /* return the wanted number of samples to get better sync if sync_type is video
2010 * or external master clock */
2011 static int synchronize_audio(VideoState *is, int nb_samples)
2013 int wanted_nb_samples = nb_samples;
2015 /* if not master, then we try to remove or add samples to correct the clock */
2016 if (get_master_sync_type(is) != AV_SYNC_AUDIO_MASTER) {
2017 double diff, avg_diff;
2018 int min_nb_samples, max_nb_samples;
2020 diff = get_audio_clock(is) - get_master_clock(is);
2022 if (fabs(diff) < AV_NOSYNC_THRESHOLD) {
2023 is->audio_diff_cum = diff + is->audio_diff_avg_coef * is->audio_diff_cum;
2024 if (is->audio_diff_avg_count < AUDIO_DIFF_AVG_NB) {
2025 /* not enough measures to have a correct estimate */
2026 is->audio_diff_avg_count++;
2028 /* estimate the A-V difference */
2029 avg_diff = is->audio_diff_cum * (1.0 - is->audio_diff_avg_coef);
2031 if (fabs(avg_diff) >= is->audio_diff_threshold) {
2032 wanted_nb_samples = nb_samples + (int)(diff * is->audio_src.freq);
2033 min_nb_samples = ((nb_samples * (100 - SAMPLE_CORRECTION_PERCENT_MAX) / 100));
2034 max_nb_samples = ((nb_samples * (100 + SAMPLE_CORRECTION_PERCENT_MAX) / 100));
2035 wanted_nb_samples = FFMIN(FFMAX(wanted_nb_samples, min_nb_samples), max_nb_samples);
2037 av_dlog(NULL, "diff=%f adiff=%f sample_diff=%d apts=%0.3f vpts=%0.3f %f\n",
2038 diff, avg_diff, wanted_nb_samples - nb_samples,
2039 is->audio_clock, is->video_clock, is->audio_diff_threshold);
2042 /* too big difference : may be initial PTS errors, so
2044 is->audio_diff_avg_count = 0;
2045 is->audio_diff_cum = 0;
2049 return wanted_nb_samples;
2052 /* decode one audio frame and returns its uncompressed size */
2053 static int audio_decode_frame(VideoState *is, double *pts_ptr)
2055 AVPacket *pkt_temp = &is->audio_pkt_temp;
2056 AVPacket *pkt = &is->audio_pkt;
2057 AVCodecContext *dec = is->audio_st->codec;
2058 int len1, len2, data_size, resampled_data_size;
2059 int64_t dec_channel_layout;
2063 int flush_complete = 0;
2064 int wanted_nb_samples;
2067 /* NOTE: the audio packet can contain several frames */
2068 while (pkt_temp->size > 0 || (!pkt_temp->data && new_packet)) {
2070 if (!(is->frame = avcodec_alloc_frame()))
2071 return AVERROR(ENOMEM);
2073 avcodec_get_frame_defaults(is->frame);
2081 len1 = avcodec_decode_audio4(dec, is->frame, &got_frame, pkt_temp);
2083 /* if error, we skip the frame */
2088 pkt_temp->data += len1;
2089 pkt_temp->size -= len1;
2092 /* stop sending empty packets if the decoder is finished */
2093 if (!pkt_temp->data && dec->codec->capabilities & CODEC_CAP_DELAY)
2097 data_size = av_samples_get_buffer_size(NULL, is->frame->channels,
2098 is->frame->nb_samples,
2099 is->frame->format, 1);
2101 dec_channel_layout =
2102 (is->frame->channel_layout && is->frame->channels == av_get_channel_layout_nb_channels(is->frame->channel_layout)) ?
2103 is->frame->channel_layout : av_get_default_channel_layout(is->frame->channels);
2104 wanted_nb_samples = synchronize_audio(is, is->frame->nb_samples);
2106 if (is->frame->format != is->audio_src.fmt ||
2107 dec_channel_layout != is->audio_src.channel_layout ||
2108 is->frame->sample_rate != is->audio_src.freq ||
2109 (wanted_nb_samples != is->frame->nb_samples && !is->swr_ctx)) {
2110 swr_free(&is->swr_ctx);
2111 is->swr_ctx = swr_alloc_set_opts(NULL,
2112 is->audio_tgt.channel_layout, is->audio_tgt.fmt, is->audio_tgt.freq,
2113 dec_channel_layout, is->frame->format, is->frame->sample_rate,
2115 if (!is->swr_ctx || swr_init(is->swr_ctx) < 0) {
2116 fprintf(stderr, "Cannot create sample rate converter for conversion of %d Hz %s %d channels to %d Hz %s %d channels!\n",
2117 is->frame->sample_rate, av_get_sample_fmt_name(is->frame->format), (int)is->frame->channels,
2118 is->audio_tgt.freq, av_get_sample_fmt_name(is->audio_tgt.fmt), is->audio_tgt.channels);
2121 is->audio_src.channel_layout = dec_channel_layout;
2122 is->audio_src.channels = is->frame->channels;
2123 is->audio_src.freq = is->frame->sample_rate;
2124 is->audio_src.fmt = is->frame->format;
2128 const uint8_t **in = (const uint8_t **)is->frame->extended_data;
2129 uint8_t *out[] = {is->audio_buf2};
2130 int out_count = sizeof(is->audio_buf2) / is->audio_tgt.channels / av_get_bytes_per_sample(is->audio_tgt.fmt);
2131 if (wanted_nb_samples != is->frame->nb_samples) {
2132 if (swr_set_compensation(is->swr_ctx, (wanted_nb_samples - is->frame->nb_samples) * is->audio_tgt.freq / is->frame->sample_rate,
2133 wanted_nb_samples * is->audio_tgt.freq / is->frame->sample_rate) < 0) {
2134 fprintf(stderr, "swr_set_compensation() failed\n");
2138 len2 = swr_convert(is->swr_ctx, out, out_count, in, is->frame->nb_samples);
2140 fprintf(stderr, "swr_convert() failed\n");
2143 if (len2 == out_count) {
2144 fprintf(stderr, "warning: audio buffer is probably too small\n");
2145 swr_init(is->swr_ctx);
2147 is->audio_buf = is->audio_buf2;
2148 resampled_data_size = len2 * is->audio_tgt.channels * av_get_bytes_per_sample(is->audio_tgt.fmt);
2150 is->audio_buf = is->frame->data[0];
2151 resampled_data_size = data_size;
2154 /* if no pts, then compute it */
2155 pts = is->audio_clock;
2157 is->audio_clock += (double)data_size /
2158 (is->frame->channels * is->frame->sample_rate * av_get_bytes_per_sample(is->frame->format));
2161 static double last_clock;
2162 printf("audio: delay=%0.3f clock=%0.3f pts=%0.3f\n",
2163 is->audio_clock - last_clock,
2164 is->audio_clock, pts);
2165 last_clock = is->audio_clock;
2168 return resampled_data_size;
2171 /* free the current packet */
2173 av_free_packet(pkt);
2174 memset(pkt_temp, 0, sizeof(*pkt_temp));
2176 if (is->paused || is->audioq.abort_request) {
2180 if (is->audioq.nb_packets == 0)
2181 SDL_CondSignal(is->continue_read_thread);
2183 /* read next packet */
2184 if ((new_packet = packet_queue_get(&is->audioq, pkt, 1, &is->audio_pkt_temp_serial)) < 0)
2187 if (pkt->data == flush_pkt.data) {
2188 avcodec_flush_buffers(dec);
2194 /* if update the audio clock with the pts */
2195 if (pkt->pts != AV_NOPTS_VALUE) {
2196 is->audio_clock = av_q2d(is->audio_st->time_base)*pkt->pts;
2201 /* prepare a new audio buffer */
2202 static void sdl_audio_callback(void *opaque, Uint8 *stream, int len)
2204 VideoState *is = opaque;
2205 int audio_size, len1;
2207 int frame_size = av_samples_get_buffer_size(NULL, is->audio_tgt.channels, 1, is->audio_tgt.fmt, 1);
2210 audio_callback_time = av_gettime();
2213 if (is->audio_buf_index >= is->audio_buf_size) {
2214 audio_size = audio_decode_frame(is, &pts);
2215 if (audio_size < 0) {
2216 /* if error, just output silence */
2217 is->audio_buf = is->silence_buf;
2218 is->audio_buf_size = sizeof(is->silence_buf) / frame_size * frame_size;
2220 if (is->show_mode != SHOW_MODE_VIDEO)
2221 update_sample_display(is, (int16_t *)is->audio_buf, audio_size);
2222 is->audio_buf_size = audio_size;
2224 is->audio_buf_index = 0;
2226 len1 = is->audio_buf_size - is->audio_buf_index;
2229 memcpy(stream, (uint8_t *)is->audio_buf + is->audio_buf_index, len1);
2232 is->audio_buf_index += len1;
2234 bytes_per_sec = is->audio_tgt.freq * is->audio_tgt.channels * av_get_bytes_per_sample(is->audio_tgt.fmt);
2235 is->audio_write_buf_size = is->audio_buf_size - is->audio_buf_index;
2236 /* Let's assume the audio driver that is used by SDL has two periods. */
2237 is->audio_current_pts = is->audio_clock - (double)(2 * is->audio_hw_buf_size + is->audio_write_buf_size) / bytes_per_sec;
2238 is->audio_current_pts_drift = is->audio_current_pts - audio_callback_time / 1000000.0;
2239 if (is->audioq.serial == is->audio_pkt_temp_serial)
2240 check_external_clock_sync(is, is->audio_current_pts);
2243 static int audio_open(void *opaque, int64_t wanted_channel_layout, int wanted_nb_channels, int wanted_sample_rate, struct AudioParams *audio_hw_params)
2245 SDL_AudioSpec wanted_spec, spec;
2247 const int next_nb_channels[] = {0, 0, 1, 6, 2, 6, 4, 6};
2249 env = SDL_getenv("SDL_AUDIO_CHANNELS");
2251 wanted_nb_channels = atoi(env);
2252 wanted_channel_layout = av_get_default_channel_layout(wanted_nb_channels);
2254 if (!wanted_channel_layout || wanted_nb_channels != av_get_channel_layout_nb_channels(wanted_channel_layout)) {
2255 wanted_channel_layout = av_get_default_channel_layout(wanted_nb_channels);
2256 wanted_channel_layout &= ~AV_CH_LAYOUT_STEREO_DOWNMIX;
2258 wanted_spec.channels = av_get_channel_layout_nb_channels(wanted_channel_layout);
2259 wanted_spec.freq = wanted_sample_rate;
2260 if (wanted_spec.freq <= 0 || wanted_spec.channels <= 0) {
2261 fprintf(stderr, "Invalid sample rate or channel count!\n");
2264 wanted_spec.format = AUDIO_S16SYS;
2265 wanted_spec.silence = 0;
2266 wanted_spec.samples = SDL_AUDIO_BUFFER_SIZE;
2267 wanted_spec.callback = sdl_audio_callback;
2268 wanted_spec.userdata = opaque;
2269 while (SDL_OpenAudio(&wanted_spec, &spec) < 0) {
2270 fprintf(stderr, "SDL_OpenAudio (%d channels): %s\n", wanted_spec.channels, SDL_GetError());
2271 wanted_spec.channels = next_nb_channels[FFMIN(7, wanted_spec.channels)];
2272 if (!wanted_spec.channels) {
2273 fprintf(stderr, "No more channel combinations to try, audio open failed\n");
2276 wanted_channel_layout = av_get_default_channel_layout(wanted_spec.channels);
2278 if (spec.format != AUDIO_S16SYS) {
2279 fprintf(stderr, "SDL advised audio format %d is not supported!\n", spec.format);
2282 if (spec.channels != wanted_spec.channels) {
2283 wanted_channel_layout = av_get_default_channel_layout(spec.channels);
2284 if (!wanted_channel_layout) {
2285 fprintf(stderr, "SDL advised channel count %d is not supported!\n", spec.channels);
2290 audio_hw_params->fmt = AV_SAMPLE_FMT_S16;
2291 audio_hw_params->freq = spec.freq;
2292 audio_hw_params->channel_layout = wanted_channel_layout;
2293 audio_hw_params->channels = spec.channels;
2297 /* open a given stream. Return 0 if OK */
2298 static int stream_component_open(VideoState *is, int stream_index)
2300 AVFormatContext *ic = is->ic;
2301 AVCodecContext *avctx;
2303 const char *forced_codec_name = NULL;
2305 AVDictionaryEntry *t = NULL;
2307 if (stream_index < 0 || stream_index >= ic->nb_streams)
2309 avctx = ic->streams[stream_index]->codec;
2311 codec = avcodec_find_decoder(avctx->codec_id);
2313 switch(avctx->codec_type){
2314 case AVMEDIA_TYPE_AUDIO : is->last_audio_stream = stream_index; forced_codec_name = audio_codec_name; break;
2315 case AVMEDIA_TYPE_SUBTITLE: is->last_subtitle_stream = stream_index; forced_codec_name = subtitle_codec_name; break;
2316 case AVMEDIA_TYPE_VIDEO : is->last_video_stream = stream_index; forced_codec_name = video_codec_name; break;
2318 if (forced_codec_name)
2319 codec = avcodec_find_decoder_by_name(forced_codec_name);
2321 if (forced_codec_name) fprintf(stderr, "No codec could be found with name '%s'\n", forced_codec_name);
2322 else fprintf(stderr, "No codec could be found with id %d\n", avctx->codec_id);
2326 avctx->codec_id = codec->id;
2327 avctx->workaround_bugs = workaround_bugs;
2328 avctx->lowres = lowres;
2329 if(avctx->lowres > codec->max_lowres){
2330 av_log(avctx, AV_LOG_WARNING, "The maximum value for lowres supported by the decoder is %d\n",
2332 avctx->lowres= codec->max_lowres;
2334 avctx->idct_algo = idct;
2335 avctx->skip_frame = skip_frame;
2336 avctx->skip_idct = skip_idct;
2337 avctx->skip_loop_filter = skip_loop_filter;
2338 avctx->error_concealment = error_concealment;
2340 if(avctx->lowres) avctx->flags |= CODEC_FLAG_EMU_EDGE;
2341 if (fast) avctx->flags2 |= CODEC_FLAG2_FAST;
2342 if(codec->capabilities & CODEC_CAP_DR1)
2343 avctx->flags |= CODEC_FLAG_EMU_EDGE;
2345 opts = filter_codec_opts(codec_opts, avctx->codec_id, ic, ic->streams[stream_index], codec);
2346 if (!av_dict_get(opts, "threads", NULL, 0))
2347 av_dict_set(&opts, "threads", "auto", 0);
2348 if (avcodec_open2(avctx, codec, &opts) < 0)
2350 if ((t = av_dict_get(opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
2351 av_log(NULL, AV_LOG_ERROR, "Option %s not found.\n", t->key);
2352 return AVERROR_OPTION_NOT_FOUND;
2355 /* prepare audio output */
2356 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
2357 int audio_hw_buf_size = audio_open(is, avctx->channel_layout, avctx->channels, avctx->sample_rate, &is->audio_src);
2358 if (audio_hw_buf_size < 0)
2360 is->audio_hw_buf_size = audio_hw_buf_size;
2361 is->audio_tgt = is->audio_src;
2364 ic->streams[stream_index]->discard = AVDISCARD_DEFAULT;
2365 switch (avctx->codec_type) {
2366 case AVMEDIA_TYPE_AUDIO:
2367 is->audio_stream = stream_index;
2368 is->audio_st = ic->streams[stream_index];
2369 is->audio_buf_size = 0;
2370 is->audio_buf_index = 0;
2372 /* init averaging filter */
2373 is->audio_diff_avg_coef = exp(log(0.01) / AUDIO_DIFF_AVG_NB);
2374 is->audio_diff_avg_count = 0;
2375 /* since we do not have a precise anough audio fifo fullness,
2376 we correct audio sync only if larger than this threshold */
2377 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);
2379 memset(&is->audio_pkt, 0, sizeof(is->audio_pkt));
2380 memset(&is->audio_pkt_temp, 0, sizeof(is->audio_pkt_temp));
2381 packet_queue_start(&is->audioq);
2384 case AVMEDIA_TYPE_VIDEO:
2385 is->video_stream = stream_index;
2386 is->video_st = ic->streams[stream_index];
2388 packet_queue_start(&is->videoq);
2389 is->video_tid = SDL_CreateThread(video_thread, is);
2391 case AVMEDIA_TYPE_SUBTITLE:
2392 is->subtitle_stream = stream_index;
2393 is->subtitle_st = ic->streams[stream_index];
2394 packet_queue_start(&is->subtitleq);
2396 is->subtitle_tid = SDL_CreateThread(subtitle_thread, is);
2404 static void stream_component_close(VideoState *is, int stream_index)
2406 AVFormatContext *ic = is->ic;
2407 AVCodecContext *avctx;
2409 if (stream_index < 0 || stream_index >= ic->nb_streams)
2411 avctx = ic->streams[stream_index]->codec;
2413 switch (avctx->codec_type) {
2414 case AVMEDIA_TYPE_AUDIO:
2415 packet_queue_abort(&is->audioq);
2419 packet_queue_flush(&is->audioq);
2420 av_free_packet(&is->audio_pkt);
2421 swr_free(&is->swr_ctx);
2422 av_freep(&is->audio_buf1);
2423 is->audio_buf = NULL;
2424 avcodec_free_frame(&is->frame);
2427 av_rdft_end(is->rdft);
2428 av_freep(&is->rdft_data);
2433 case AVMEDIA_TYPE_VIDEO:
2434 packet_queue_abort(&is->videoq);
2436 /* note: we also signal this mutex to make sure we deblock the
2437 video thread in all cases */
2438 SDL_LockMutex(is->pictq_mutex);
2439 SDL_CondSignal(is->pictq_cond);
2440 SDL_UnlockMutex(is->pictq_mutex);
2442 SDL_WaitThread(is->video_tid, NULL);
2444 packet_queue_flush(&is->videoq);
2446 case AVMEDIA_TYPE_SUBTITLE:
2447 packet_queue_abort(&is->subtitleq);
2449 /* note: we also signal this mutex to make sure we deblock the
2450 video thread in all cases */
2451 SDL_LockMutex(is->subpq_mutex);
2452 is->subtitle_stream_changed = 1;
2454 SDL_CondSignal(is->subpq_cond);
2455 SDL_UnlockMutex(is->subpq_mutex);
2457 SDL_WaitThread(is->subtitle_tid, NULL);
2459 packet_queue_flush(&is->subtitleq);
2465 ic->streams[stream_index]->discard = AVDISCARD_ALL;
2466 avcodec_close(avctx);
2468 free_buffer_pool(&is->buffer_pool);
2470 switch (avctx->codec_type) {
2471 case AVMEDIA_TYPE_AUDIO:
2472 is->audio_st = NULL;
2473 is->audio_stream = -1;
2475 case AVMEDIA_TYPE_VIDEO:
2476 is->video_st = NULL;
2477 is->video_stream = -1;
2479 case AVMEDIA_TYPE_SUBTITLE:
2480 is->subtitle_st = NULL;
2481 is->subtitle_stream = -1;
2488 static int decode_interrupt_cb(void *ctx)
2490 VideoState *is = ctx;
2491 return is->abort_request;
2494 static int is_realtime(AVFormatContext *s)
2496 if( !strcmp(s->iformat->name, "rtp")
2497 || !strcmp(s->iformat->name, "rtsp")
2498 || !strcmp(s->iformat->name, "sdp")
2502 if(s->pb && ( !strncmp(s->filename, "rtp:", 4)
2503 || !strncmp(s->filename, "udp:", 4)
2510 /* this thread gets the stream from the disk or the network */
2511 static int read_thread(void *arg)
2513 VideoState *is = arg;
2514 AVFormatContext *ic = NULL;
2516 int st_index[AVMEDIA_TYPE_NB];
2517 AVPacket pkt1, *pkt = &pkt1;
2519 int pkt_in_play_range = 0;
2520 AVDictionaryEntry *t;
2521 AVDictionary **opts;
2522 int orig_nb_streams;
2523 SDL_mutex *wait_mutex = SDL_CreateMutex();
2525 memset(st_index, -1, sizeof(st_index));
2526 is->last_video_stream = is->video_stream = -1;
2527 is->last_audio_stream = is->audio_stream = -1;
2528 is->last_subtitle_stream = is->subtitle_stream = -1;
2530 ic = avformat_alloc_context();
2531 ic->interrupt_callback.callback = decode_interrupt_cb;
2532 ic->interrupt_callback.opaque = is;
2533 err = avformat_open_input(&ic, is->filename, is->iformat, &format_opts);
2535 print_error(is->filename, err);
2539 if ((t = av_dict_get(format_opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
2540 av_log(NULL, AV_LOG_ERROR, "Option %s not found.\n", t->key);
2541 ret = AVERROR_OPTION_NOT_FOUND;
2547 ic->flags |= AVFMT_FLAG_GENPTS;
2549 opts = setup_find_stream_info_opts(ic, codec_opts);
2550 orig_nb_streams = ic->nb_streams;
2552 err = avformat_find_stream_info(ic, opts);
2554 fprintf(stderr, "%s: could not find codec parameters\n", is->filename);
2558 for (i = 0; i < orig_nb_streams; i++)
2559 av_dict_free(&opts[i]);
2563 ic->pb->eof_reached = 0; // FIXME hack, ffplay maybe should not use url_feof() to test for the end
2565 if (seek_by_bytes < 0)
2566 seek_by_bytes = !!(ic->iformat->flags & AVFMT_TS_DISCONT);
2568 is->max_frame_duration = (ic->iformat->flags & AVFMT_TS_DISCONT) ? 10.0 : 3600.0;
2570 /* if seeking requested, we execute it */
2571 if (start_time != AV_NOPTS_VALUE) {
2574 timestamp = start_time;
2575 /* add the stream start time */
2576 if (ic->start_time != AV_NOPTS_VALUE)
2577 timestamp += ic->start_time;
2578 ret = avformat_seek_file(ic, -1, INT64_MIN, timestamp, INT64_MAX, 0);
2580 fprintf(stderr, "%s: could not seek to position %0.3f\n",
2581 is->filename, (double)timestamp / AV_TIME_BASE);
2585 is->realtime = is_realtime(ic);
2587 for (i = 0; i < ic->nb_streams; i++)
2588 ic->streams[i]->discard = AVDISCARD_ALL;
2590 st_index[AVMEDIA_TYPE_VIDEO] =
2591 av_find_best_stream(ic, AVMEDIA_TYPE_VIDEO,
2592 wanted_stream[AVMEDIA_TYPE_VIDEO], -1, NULL, 0);
2594 st_index[AVMEDIA_TYPE_AUDIO] =
2595 av_find_best_stream(ic, AVMEDIA_TYPE_AUDIO,
2596 wanted_stream[AVMEDIA_TYPE_AUDIO],
2597 st_index[AVMEDIA_TYPE_VIDEO],
2600 st_index[AVMEDIA_TYPE_SUBTITLE] =
2601 av_find_best_stream(ic, AVMEDIA_TYPE_SUBTITLE,
2602 wanted_stream[AVMEDIA_TYPE_SUBTITLE],
2603 (st_index[AVMEDIA_TYPE_AUDIO] >= 0 ?
2604 st_index[AVMEDIA_TYPE_AUDIO] :
2605 st_index[AVMEDIA_TYPE_VIDEO]),
2608 av_dump_format(ic, 0, is->filename, 0);
2611 is->show_mode = show_mode;
2613 /* open the streams */
2614 if (st_index[AVMEDIA_TYPE_AUDIO] >= 0) {
2615 stream_component_open(is, st_index[AVMEDIA_TYPE_AUDIO]);
2619 if (st_index[AVMEDIA_TYPE_VIDEO] >= 0) {
2620 ret = stream_component_open(is, st_index[AVMEDIA_TYPE_VIDEO]);
2622 is->refresh_tid = SDL_CreateThread(refresh_thread, is);
2623 if (is->show_mode == SHOW_MODE_NONE)
2624 is->show_mode = ret >= 0 ? SHOW_MODE_VIDEO : SHOW_MODE_RDFT;
2626 if (st_index[AVMEDIA_TYPE_SUBTITLE] >= 0) {
2627 stream_component_open(is, st_index[AVMEDIA_TYPE_SUBTITLE]);
2630 if (is->video_stream < 0 && is->audio_stream < 0) {
2631 fprintf(stderr, "%s: could not open codecs\n", is->filename);
2636 if (infinite_buffer < 0 && is->realtime)
2637 infinite_buffer = 1;
2640 if (is->abort_request)
2642 if (is->paused != is->last_paused) {
2643 is->last_paused = is->paused;
2645 is->read_pause_return = av_read_pause(ic);
2649 #if CONFIG_RTSP_DEMUXER || CONFIG_MMSH_PROTOCOL
2651 (!strcmp(ic->iformat->name, "rtsp") ||
2652 (ic->pb && !strncmp(input_filename, "mmsh:", 5)))) {
2653 /* wait 10 ms to avoid trying to get another packet */
2660 int64_t seek_target = is->seek_pos;
2661 int64_t seek_min = is->seek_rel > 0 ? seek_target - is->seek_rel + 2: INT64_MIN;
2662 int64_t seek_max = is->seek_rel < 0 ? seek_target - is->seek_rel - 2: INT64_MAX;
2663 // FIXME the +-2 is due to rounding being not done in the correct direction in generation
2664 // of the seek_pos/seek_rel variables
2666 ret = avformat_seek_file(is->ic, -1, seek_min, seek_target, seek_max, is->seek_flags);
2668 fprintf(stderr, "%s: error while seeking\n", is->ic->filename);
2670 if (is->audio_stream >= 0) {
2671 packet_queue_flush(&is->audioq);
2672 packet_queue_put(&is->audioq, &flush_pkt);
2674 if (is->subtitle_stream >= 0) {
2675 packet_queue_flush(&is->subtitleq);
2676 packet_queue_put(&is->subtitleq, &flush_pkt);
2678 if (is->video_stream >= 0) {
2679 packet_queue_flush(&is->videoq);
2680 packet_queue_put(&is->videoq, &flush_pkt);
2682 if (is->seek_flags & AVSEEK_FLAG_BYTE) {
2683 //FIXME: use a cleaner way to signal obsolete external clock...
2684 update_external_clock_pts(is, (double)AV_NOPTS_VALUE);
2686 update_external_clock_pts(is, seek_target / (double)AV_TIME_BASE);
2692 if (is->que_attachments_req) {
2693 avformat_queue_attached_pictures(ic);
2694 is->que_attachments_req = 0;
2697 /* if the queue are full, no need to read more */
2698 if (infinite_buffer<1 &&
2699 (is->audioq.size + is->videoq.size + is->subtitleq.size > MAX_QUEUE_SIZE
2700 || ( (is->audioq .nb_packets > MIN_FRAMES || is->audio_stream < 0 || is->audioq.abort_request)
2701 && (is->videoq .nb_packets > MIN_FRAMES || is->video_stream < 0 || is->videoq.abort_request)
2702 && (is->subtitleq.nb_packets > MIN_FRAMES || is->subtitle_stream < 0 || is->subtitleq.abort_request)))) {
2704 SDL_LockMutex(wait_mutex);
2705 SDL_CondWaitTimeout(is->continue_read_thread, wait_mutex, 10);
2706 SDL_UnlockMutex(wait_mutex);
2710 if (is->video_stream >= 0) {
2711 av_init_packet(pkt);
2714 pkt->stream_index = is->video_stream;
2715 packet_queue_put(&is->videoq, pkt);
2717 if (is->audio_stream >= 0 &&
2718 is->audio_st->codec->codec->capabilities & CODEC_CAP_DELAY) {
2719 av_init_packet(pkt);
2722 pkt->stream_index = is->audio_stream;
2723 packet_queue_put(&is->audioq, pkt);
2726 if (is->audioq.size + is->videoq.size + is->subtitleq.size == 0) {
2727 if (loop != 1 && (!loop || --loop)) {
2728 stream_seek(is, start_time != AV_NOPTS_VALUE ? start_time : 0, 0, 0);
2729 } else if (autoexit) {
2737 ret = av_read_frame(ic, pkt);
2739 if (ret == AVERROR_EOF || url_feof(ic->pb))
2741 if (ic->pb && ic->pb->error)
2743 SDL_LockMutex(wait_mutex);
2744 SDL_CondWaitTimeout(is->continue_read_thread, wait_mutex, 10);
2745 SDL_UnlockMutex(wait_mutex);
2748 /* check if packet is in play range specified by user, then queue, otherwise discard */
2749 pkt_in_play_range = duration == AV_NOPTS_VALUE ||
2750 (pkt->pts - ic->streams[pkt->stream_index]->start_time) *
2751 av_q2d(ic->streams[pkt->stream_index]->time_base) -
2752 (double)(start_time != AV_NOPTS_VALUE ? start_time : 0) / 1000000
2753 <= ((double)duration / 1000000);
2754 if (pkt->stream_index == is->audio_stream && pkt_in_play_range) {
2755 packet_queue_put(&is->audioq, pkt);
2756 } else if (pkt->stream_index == is->video_stream && pkt_in_play_range) {
2757 packet_queue_put(&is->videoq, pkt);
2758 } else if (pkt->stream_index == is->subtitle_stream && pkt_in_play_range) {
2759 packet_queue_put(&is->subtitleq, pkt);
2761 av_free_packet(pkt);
2764 /* wait until the end */
2765 while (!is->abort_request) {
2771 /* close each stream */
2772 if (is->audio_stream >= 0)
2773 stream_component_close(is, is->audio_stream);
2774 if (is->video_stream >= 0)
2775 stream_component_close(is, is->video_stream);
2776 if (is->subtitle_stream >= 0)
2777 stream_component_close(is, is->subtitle_stream);
2779 avformat_close_input(&is->ic);
2785 event.type = FF_QUIT_EVENT;
2786 event.user.data1 = is;
2787 SDL_PushEvent(&event);
2789 SDL_DestroyMutex(wait_mutex);
2793 static VideoState *stream_open(const char *filename, AVInputFormat *iformat)
2797 is = av_mallocz(sizeof(VideoState));
2800 av_strlcpy(is->filename, filename, sizeof(is->filename));
2801 is->iformat = iformat;
2805 /* start video display */
2806 is->pictq_mutex = SDL_CreateMutex();
2807 is->pictq_cond = SDL_CreateCond();
2809 is->subpq_mutex = SDL_CreateMutex();
2810 is->subpq_cond = SDL_CreateCond();
2812 packet_queue_init(&is->videoq);
2813 packet_queue_init(&is->audioq);
2814 packet_queue_init(&is->subtitleq);
2816 is->continue_read_thread = SDL_CreateCond();
2818 update_external_clock_pts(is, 0.0);
2819 update_external_clock_speed(is, 1.0);
2820 is->audio_current_pts_drift = -av_gettime() / 1000000.0;
2821 is->video_current_pts_drift = is->audio_current_pts_drift;
2822 is->av_sync_type = av_sync_type;
2823 is->read_tid = SDL_CreateThread(read_thread, is);
2824 if (!is->read_tid) {
2831 static void stream_cycle_channel(VideoState *is, int codec_type)
2833 AVFormatContext *ic = is->ic;
2834 int start_index, stream_index;
2838 if (codec_type == AVMEDIA_TYPE_VIDEO) {
2839 start_index = is->last_video_stream;
2840 old_index = is->video_stream;
2841 } else if (codec_type == AVMEDIA_TYPE_AUDIO) {
2842 start_index = is->last_audio_stream;
2843 old_index = is->audio_stream;
2845 start_index = is->last_subtitle_stream;
2846 old_index = is->subtitle_stream;
2848 stream_index = start_index;
2850 if (++stream_index >= is->ic->nb_streams)
2852 if (codec_type == AVMEDIA_TYPE_SUBTITLE)
2855 is->last_subtitle_stream = -1;
2858 if (start_index == -1)
2862 if (stream_index == start_index)
2864 st = ic->streams[stream_index];
2865 if (st->codec->codec_type == codec_type) {
2866 /* check that parameters are OK */
2867 switch (codec_type) {
2868 case AVMEDIA_TYPE_AUDIO:
2869 if (st->codec->sample_rate != 0 &&
2870 st->codec->channels != 0)
2873 case AVMEDIA_TYPE_VIDEO:
2874 case AVMEDIA_TYPE_SUBTITLE:
2882 stream_component_close(is, old_index);
2883 stream_component_open(is, stream_index);
2884 if (codec_type == AVMEDIA_TYPE_VIDEO)
2885 is->que_attachments_req = 1;
2889 static void toggle_full_screen(VideoState *is)
2891 #if defined(__APPLE__) && SDL_VERSION_ATLEAST(1, 2, 14)
2892 /* OS X needs to reallocate the SDL overlays */
2894 for (i = 0; i < VIDEO_PICTURE_QUEUE_SIZE; i++)
2895 is->pictq[i].reallocate = 1;
2897 is_full_screen = !is_full_screen;
2901 static void toggle_pause(VideoState *is)
2903 stream_toggle_pause(is);
2907 static void step_to_next_frame(VideoState *is)
2909 /* if the stream is paused unpause it, then step */
2911 stream_toggle_pause(is);
2915 static void toggle_audio_display(VideoState *is)
2917 int bgcolor = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00);
2918 is->show_mode = (is->show_mode + 1) % SHOW_MODE_NB;
2919 fill_rectangle(screen,
2920 is->xleft, is->ytop, is->width, is->height,
2924 /* handle an event sent by the GUI */
2925 static void event_loop(VideoState *cur_stream)
2928 double incr, pos, frac;
2932 SDL_WaitEvent(&event);
2933 switch (event.type) {
2935 if (exit_on_keydown) {
2936 do_exit(cur_stream);
2939 switch (event.key.keysym.sym) {
2942 do_exit(cur_stream);
2945 toggle_full_screen(cur_stream);
2946 cur_stream->force_refresh = 1;
2950 toggle_pause(cur_stream);
2952 case SDLK_s: // S: Step to next frame
2953 step_to_next_frame(cur_stream);
2956 stream_cycle_channel(cur_stream, AVMEDIA_TYPE_AUDIO);
2959 stream_cycle_channel(cur_stream, AVMEDIA_TYPE_VIDEO);
2962 stream_cycle_channel(cur_stream, AVMEDIA_TYPE_SUBTITLE);
2965 toggle_audio_display(cur_stream);
2966 cur_stream->force_refresh = 1;
2986 if (seek_by_bytes) {
2987 if (cur_stream->video_stream >= 0 && cur_stream->video_current_pos >= 0) {
2988 pos = cur_stream->video_current_pos;
2989 } else if (cur_stream->audio_stream >= 0 && cur_stream->audio_pkt.pos >= 0) {
2990 pos = cur_stream->audio_pkt.pos;
2992 pos = avio_tell(cur_stream->ic->pb);
2993 if (cur_stream->ic->bit_rate)
2994 incr *= cur_stream->ic->bit_rate / 8.0;
2998 stream_seek(cur_stream, pos, incr, 1);
3000 pos = get_master_clock(cur_stream);
3002 if (cur_stream->ic->start_time != AV_NOPTS_VALUE && pos < cur_stream->ic->start_time / (double)AV_TIME_BASE)
3003 pos = cur_stream->ic->start_time / (double)AV_TIME_BASE;
3004 stream_seek(cur_stream, (int64_t)(pos * AV_TIME_BASE), (int64_t)(incr * AV_TIME_BASE), 0);
3011 case SDL_VIDEOEXPOSE:
3012 cur_stream->force_refresh = 1;
3014 case SDL_MOUSEBUTTONDOWN:
3015 if (exit_on_mousedown) {
3016 do_exit(cur_stream);
3019 case SDL_MOUSEMOTION:
3020 if (event.type == SDL_MOUSEBUTTONDOWN) {
3023 if (event.motion.state != SDL_PRESSED)
3027 if (seek_by_bytes || cur_stream->ic->duration <= 0) {
3028 uint64_t size = avio_size(cur_stream->ic->pb);
3029 stream_seek(cur_stream, size*x/cur_stream->width, 0, 1);
3033 int tns, thh, tmm, tss;
3034 tns = cur_stream->ic->duration / 1000000LL;
3036 tmm = (tns % 3600) / 60;
3038 frac = x / cur_stream->width;
3041 mm = (ns % 3600) / 60;
3043 fprintf(stderr, "Seek to %2.0f%% (%2d:%02d:%02d) of total duration (%2d:%02d:%02d) \n", frac*100,
3044 hh, mm, ss, thh, tmm, tss);
3045 ts = frac * cur_stream->ic->duration;
3046 if (cur_stream->ic->start_time != AV_NOPTS_VALUE)
3047 ts += cur_stream->ic->start_time;
3048 stream_seek(cur_stream, ts, 0, 0);
3051 case SDL_VIDEORESIZE:
3052 screen = SDL_SetVideoMode(event.resize.w, event.resize.h, 0,
3053 SDL_HWSURFACE|SDL_RESIZABLE|SDL_ASYNCBLIT|SDL_HWACCEL);
3054 screen_width = cur_stream->width = event.resize.w;
3055 screen_height = cur_stream->height = event.resize.h;
3056 cur_stream->force_refresh = 1;
3060 do_exit(cur_stream);
3062 case FF_ALLOC_EVENT:
3063 alloc_picture(event.user.data1);
3065 case FF_REFRESH_EVENT:
3066 video_refresh(event.user.data1);
3067 cur_stream->refresh = 0;
3075 static int opt_frame_size(void *optctx, const char *opt, const char *arg)
3077 av_log(NULL, AV_LOG_WARNING, "Option -s is deprecated, use -video_size.\n");
3078 return opt_default(NULL, "video_size", arg);
3081 static int opt_width(void *optctx, const char *opt, const char *arg)
3083 screen_width = parse_number_or_die(opt, arg, OPT_INT64, 1, INT_MAX);
3087 static int opt_height(void *optctx, const char *opt, const char *arg)
3089 screen_height = parse_number_or_die(opt, arg, OPT_INT64, 1, INT_MAX);
3093 static int opt_format(void *optctx, const char *opt, const char *arg)
3095 file_iformat = av_find_input_format(arg);
3096 if (!file_iformat) {
3097 fprintf(stderr, "Unknown input format: %s\n", arg);
3098 return AVERROR(EINVAL);
3103 static int opt_frame_pix_fmt(void *optctx, const char *opt, const char *arg)
3105 av_log(NULL, AV_LOG_WARNING, "Option -pix_fmt is deprecated, use -pixel_format.\n");
3106 return opt_default(NULL, "pixel_format", arg);
3109 static int opt_sync(void *optctx, const char *opt, const char *arg)
3111 if (!strcmp(arg, "audio"))
3112 av_sync_type = AV_SYNC_AUDIO_MASTER;
3113 else if (!strcmp(arg, "video"))
3114 av_sync_type = AV_SYNC_VIDEO_MASTER;
3115 else if (!strcmp(arg, "ext"))
3116 av_sync_type = AV_SYNC_EXTERNAL_CLOCK;
3118 fprintf(stderr, "Unknown value for %s: %s\n", opt, arg);
3124 static int opt_seek(void *optctx, const char *opt, const char *arg)
3126 start_time = parse_time_or_die(opt, arg, 1);
3130 static int opt_duration(void *optctx, const char *opt, const char *arg)
3132 duration = parse_time_or_die(opt, arg, 1);
3136 static int opt_show_mode(void *optctx, const char *opt, const char *arg)
3138 show_mode = !strcmp(arg, "video") ? SHOW_MODE_VIDEO :
3139 !strcmp(arg, "waves") ? SHOW_MODE_WAVES :
3140 !strcmp(arg, "rdft" ) ? SHOW_MODE_RDFT :
3141 parse_number_or_die(opt, arg, OPT_INT, 0, SHOW_MODE_NB-1);
3145 static void opt_input_file(void *optctx, const char *filename)
3147 if (input_filename) {
3148 fprintf(stderr, "Argument '%s' provided as input filename, but '%s' was already specified.\n",
3149 filename, input_filename);
3152 if (!strcmp(filename, "-"))
3154 input_filename = filename;
3157 static int opt_codec(void *optctx, const char *opt, const char *arg)
3159 const char *spec = strchr(opt, ':');
3161 fprintf(stderr, "No media specifier was specified in '%s' in option '%s'\n",
3163 return AVERROR(EINVAL);
3167 case 'a' : audio_codec_name = arg; break;
3168 case 's' : subtitle_codec_name = arg; break;
3169 case 'v' : video_codec_name = arg; break;
3171 fprintf(stderr, "Invalid media specifier '%s' in option '%s'\n", spec, opt);
3172 return AVERROR(EINVAL);
3179 static const OptionDef options[] = {
3180 #include "cmdutils_common_opts.h"
3181 { "x", HAS_ARG, { .func_arg = opt_width }, "force displayed width", "width" },
3182 { "y", HAS_ARG, { .func_arg = opt_height }, "force displayed height", "height" },
3183 { "s", HAS_ARG | OPT_VIDEO, { .func_arg = opt_frame_size }, "set frame size (WxH or abbreviation)", "size" },
3184 { "fs", OPT_BOOL, { &is_full_screen }, "force full screen" },
3185 { "an", OPT_BOOL, { &audio_disable }, "disable audio" },
3186 { "vn", OPT_BOOL, { &video_disable }, "disable video" },
3187 { "ast", OPT_INT | HAS_ARG | OPT_EXPERT, { &wanted_stream[AVMEDIA_TYPE_AUDIO] }, "select desired audio stream", "stream_number" },
3188 { "vst", OPT_INT | HAS_ARG | OPT_EXPERT, { &wanted_stream[AVMEDIA_TYPE_VIDEO] }, "select desired video stream", "stream_number" },
3189 { "sst", OPT_INT | HAS_ARG | OPT_EXPERT, { &wanted_stream[AVMEDIA_TYPE_SUBTITLE] }, "select desired subtitle stream", "stream_number" },
3190 { "ss", HAS_ARG, { .func_arg = opt_seek }, "seek to a given position in seconds", "pos" },
3191 { "t", HAS_ARG, { .func_arg = opt_duration }, "play \"duration\" seconds of audio/video", "duration" },
3192 { "bytes", OPT_INT | HAS_ARG, { &seek_by_bytes }, "seek by bytes 0=off 1=on -1=auto", "val" },
3193 { "nodisp", OPT_BOOL, { &display_disable }, "disable graphical display" },
3194 { "f", HAS_ARG, { .func_arg = opt_format }, "force format", "fmt" },
3195 { "pix_fmt", HAS_ARG | OPT_EXPERT | OPT_VIDEO, { .func_arg = opt_frame_pix_fmt }, "set pixel format", "format" },
3196 { "stats", OPT_BOOL | OPT_EXPERT, { &show_status }, "show status", "" },
3197 { "bug", OPT_INT | HAS_ARG | OPT_EXPERT, { &workaround_bugs }, "workaround bugs", "" },
3198 { "fast", OPT_BOOL | OPT_EXPERT, { &fast }, "non spec compliant optimizations", "" },
3199 { "genpts", OPT_BOOL | OPT_EXPERT, { &genpts }, "generate pts", "" },
3200 { "drp", OPT_INT | HAS_ARG | OPT_EXPERT, { &decoder_reorder_pts }, "let decoder reorder pts 0=off 1=on -1=auto", ""},
3201 { "lowres", OPT_INT | HAS_ARG | OPT_EXPERT, { &lowres }, "", "" },
3202 { "skiploop", OPT_INT | HAS_ARG | OPT_EXPERT, { &skip_loop_filter }, "", "" },
3203 { "skipframe", OPT_INT | HAS_ARG | OPT_EXPERT, { &skip_frame }, "", "" },
3204 { "skipidct", OPT_INT | HAS_ARG | OPT_EXPERT, { &skip_idct }, "", "" },
3205 { "idct", OPT_INT | HAS_ARG | OPT_EXPERT, { &idct }, "set idct algo", "algo" },
3206 { "ec", OPT_INT | HAS_ARG | OPT_EXPERT, { &error_concealment }, "set error concealment options", "bit_mask" },
3207 { "sync", HAS_ARG | OPT_EXPERT, { .func_arg = opt_sync }, "set audio-video sync. type (type=audio/video/ext)", "type" },
3208 { "autoexit", OPT_BOOL | OPT_EXPERT, { &autoexit }, "exit at the end", "" },
3209 { "exitonkeydown", OPT_BOOL | OPT_EXPERT, { &exit_on_keydown }, "exit on key down", "" },
3210 { "exitonmousedown", OPT_BOOL | OPT_EXPERT, { &exit_on_mousedown }, "exit on mouse down", "" },
3211 { "loop", OPT_INT | HAS_ARG | OPT_EXPERT, { &loop }, "set number of times the playback shall be looped", "loop count" },
3212 { "framedrop", OPT_BOOL | OPT_EXPERT, { &framedrop }, "drop frames when cpu is too slow", "" },
3213 { "infbuf", OPT_BOOL | OPT_EXPERT, { &infinite_buffer }, "don't limit the input buffer size (useful with realtime streams)", "" },
3214 { "window_title", OPT_STRING | HAS_ARG, { &window_title }, "set window title", "window title" },
3216 { "vf", OPT_STRING | HAS_ARG, { &vfilters }, "video filters", "filter list" },
3218 { "rdftspeed", OPT_INT | HAS_ARG| OPT_AUDIO | OPT_EXPERT, { &rdftspeed }, "rdft speed", "msecs" },
3219 { "showmode", HAS_ARG, { .func_arg = opt_show_mode}, "select show mode (0 = video, 1 = waves, 2 = RDFT)", "mode" },
3220 { "default", HAS_ARG | OPT_AUDIO | OPT_VIDEO | OPT_EXPERT, { .func_arg = opt_default }, "generic catch all option", "" },
3221 { "i", OPT_BOOL, { &dummy}, "read specified file", "input_file"},
3222 { "codec", HAS_ARG, { .func_arg = opt_codec}, "force decoder", "decoder_name" },
3223 { "acodec", HAS_ARG | OPT_STRING | OPT_EXPERT, { &audio_codec_name }, "force audio decoder", "decoder_name" },
3224 { "scodec", HAS_ARG | OPT_STRING | OPT_EXPERT, { &subtitle_codec_name }, "force subtitle decoder", "decoder_name" },
3225 { "vcodec", HAS_ARG | OPT_STRING | OPT_EXPERT, { &video_codec_name }, "force video decoder", "decoder_name" },
3229 static void show_usage(void)
3231 av_log(NULL, AV_LOG_INFO, "Simple media player\n");
3232 av_log(NULL, AV_LOG_INFO, "usage: %s [options] input_file\n", program_name);
3233 av_log(NULL, AV_LOG_INFO, "\n");
3236 void show_help_default(const char *opt, const char *arg)
3238 av_log_set_callback(log_callback_help);
3240 show_help_options(options, "Main options:", 0, OPT_EXPERT, 0);
3241 show_help_options(options, "Advanced options:", OPT_EXPERT, 0, 0);
3243 show_help_children(avcodec_get_class(), AV_OPT_FLAG_DECODING_PARAM);
3244 show_help_children(avformat_get_class(), AV_OPT_FLAG_DECODING_PARAM);
3245 #if !CONFIG_AVFILTER
3246 show_help_children(sws_get_class(), AV_OPT_FLAG_ENCODING_PARAM);
3248 show_help_children(avfilter_get_class(), AV_OPT_FLAG_FILTERING_PARAM);
3250 printf("\nWhile playing:\n"
3252 "f toggle full screen\n"
3254 "a cycle audio channel\n"
3255 "v cycle video channel\n"
3256 "t cycle subtitle channel\n"
3257 "w show audio waves\n"
3258 "s activate frame-step mode\n"
3259 "left/right seek backward/forward 10 seconds\n"
3260 "down/up seek backward/forward 1 minute\n"
3261 "page down/page up seek backward/forward 10 minutes\n"
3262 "mouse click seek to percentage in file corresponding to fraction of width\n"
3266 static int lockmgr(void **mtx, enum AVLockOp op)
3269 case AV_LOCK_CREATE:
3270 *mtx = SDL_CreateMutex();
3274 case AV_LOCK_OBTAIN:
3275 return !!SDL_LockMutex(*mtx);
3276 case AV_LOCK_RELEASE:
3277 return !!SDL_UnlockMutex(*mtx);
3278 case AV_LOCK_DESTROY:
3279 SDL_DestroyMutex(*mtx);
3285 /* Called from the main */
3286 int main(int argc, char **argv)
3290 char dummy_videodriver[] = "SDL_VIDEODRIVER=dummy";
3292 av_log_set_flags(AV_LOG_SKIP_REPEATED);
3293 parse_loglevel(argc, argv, options);
3295 /* register all codecs, demux and protocols */
3296 avcodec_register_all();
3298 avdevice_register_all();
3301 avfilter_register_all();
3304 avformat_network_init();
3308 signal(SIGINT , sigterm_handler); /* Interrupt (ANSI). */
3309 signal(SIGTERM, sigterm_handler); /* Termination (ANSI). */
3311 show_banner(argc, argv, options);
3313 parse_options(NULL, argc, argv, options, opt_input_file);
3315 if (!input_filename) {
3317 fprintf(stderr, "An input file must be specified\n");
3318 fprintf(stderr, "Use -h to get full help or, even better, run 'man %s'\n", program_name);
3322 if (display_disable) {
3325 flags = SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER;
3327 flags &= ~SDL_INIT_AUDIO;
3328 if (display_disable)
3329 SDL_putenv(dummy_videodriver); /* For the event queue, we always need a video driver. */
3330 #if !defined(__MINGW32__) && !defined(__APPLE__)
3331 flags |= SDL_INIT_EVENTTHREAD; /* Not supported on Windows or Mac OS X */
3333 if (SDL_Init (flags)) {
3334 fprintf(stderr, "Could not initialize SDL - %s\n", SDL_GetError());
3335 fprintf(stderr, "(Did you set the DISPLAY variable?)\n");
3339 if (!display_disable) {
3340 #if HAVE_SDL_VIDEO_SIZE
3341 const SDL_VideoInfo *vi = SDL_GetVideoInfo();
3342 fs_screen_width = vi->current_w;
3343 fs_screen_height = vi->current_h;
3347 SDL_EventState(SDL_ACTIVEEVENT, SDL_IGNORE);
3348 SDL_EventState(SDL_SYSWMEVENT, SDL_IGNORE);
3349 SDL_EventState(SDL_USEREVENT, SDL_IGNORE);
3351 if (av_lockmgr_register(lockmgr)) {
3352 fprintf(stderr, "Could not initialize lock manager!\n");
3356 av_init_packet(&flush_pkt);
3357 flush_pkt.data = (char *)(intptr_t)"FLUSH";
3359 is = stream_open(input_filename, file_iformat);
3361 fprintf(stderr, "Failed to initialize VideoState!\n");